Skip to content

Commit

Permalink
#612 #608 #405 MathML Add fonts as files instead of input streams.
Browse files Browse the repository at this point in the history
  • Loading branch information
danfickle committed Nov 30, 2020
1 parent 9a9ccd0 commit 4af4f08
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
<html>
<head>
<style>
@font-face {
src: url(fonts/Karla-Bold.ttf);
font-family: 'MyFont';
font-weight: normal;
}
@page {
size: 500px 1500px;
margin: 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.openhtmltopdf.visualregressiontests;

import java.awt.Font;
import java.awt.FontFormatException;
import java.io.*;

Expand All @@ -18,6 +17,7 @@
import com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder.FontStyle;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import com.openhtmltopdf.latexsupport.LaTeXDOMMutator;
Expand All @@ -33,7 +33,12 @@

public class VisualRegressionTest {
private VisualTester vt;


@BeforeClass
public static void configureTests() throws IOException {
TestSupport.makeFontFiles();
}

@Before
public void configureTester() {
File outputDirectory = new File("target/test/visual-tests/test-output/");
Expand Down Expand Up @@ -837,6 +842,9 @@ public void testReplacedSizingSvgNonCss() throws IOException {
public void testReplacedSizingMathMl() throws IOException {
assertTrue(vt.runTest("replaced-sizing-mathml", (builder) -> {
builder.useMathMLDrawer(new MathMLDrawer());
builder.useFont(
new File("target/test/visual-tests/Karla-Bold.ttf"),
"MyFont", 400, FontStyle.NORMAL, true, EnumSet.of(FSFontUseCase.MATHML));
}));
}

Expand Down Expand Up @@ -1307,8 +1315,6 @@ public void testPr610ForcePageBreakLine() throws IOException {
*/
@Test
public void testSVGFontFileAddition() throws IOException, FontFormatException {
TestSupport.makeFontFiles();

assertTrue(vt.runTest("svg-font-file-addition",
builder -> {
TestSupport.WITH_SVG.configure(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public class MathMLDrawer implements SVGDrawer {
private final FontFactory _fontFactory;
private SharedContext _sharedCtx;
private final Set<String> _loadedFontFamilies = new HashSet<>();
private final Map<String, List<String>> _availabelFontFamilies = new HashMap<>();
private final Map<String, List<FontEntry>> _availabelFontFamilies = new HashMap<>();

private static class FontEntry {
String src;
File file;
}

public MathMLDrawer() {
this._fontFactory = new DefaultFontFactory();
Expand All @@ -61,15 +66,13 @@ public void importFontFaceRules(List<FontFaceRule> fontFaces,
!rule.hasFontFamily()) {
continue;
}

String family = style.valueByName(CSSName.FONT_FAMILY).asString();

if (_availabelFontFamilies.containsKey(family)) {
_availabelFontFamilies.get(family).add(src.asString());
} else {
_availabelFontFamilies.put(family, new ArrayList<>());
_availabelFontFamilies.get(family).add(src.asString());
}

String family = style.valueByName(CSSName.FONT_FAMILY).asString();

FontEntry entry = new FontEntry();
entry.src = src.asString();

_availabelFontFamilies.computeIfAbsent(family, f -> new ArrayList<>()).add(entry);
}
}

Expand All @@ -87,22 +90,29 @@ private void loadFamilyFonts(String family) {
XRLog.log(Level.WARNING, LogMessageId.LogMessageId1Param.GENERAL_COULD_NOT_FIND_FONT_SPECIFIED_FOR_MATHML_OBJECT_IN_FONT_FACE_RULES,family);
return;
}

for (String src : _availabelFontFamilies.get(family)) {
byte[] font1 = _sharedCtx.getUserAgentCallback().getBinaryResource(src);
if (font1 == null) {
XRLog.log(Level.WARNING, LogMessageId.LogMessageId1Param.EXCEPTION_COULD_NOT_LOAD_FONT, src);
continue;
}

try {
_fontFactory.registerFont(Font.TRUETYPE_FONT, new ByteArrayInputStream(font1));
} catch (IOException | FontFormatException e) {
XRLog.log(Level.WARNING, LogMessageId.LogMessageId0Param.EXCEPTION_MATHML_COULD_NOT_REGISTER_FONT, e);
}
}

}
for (FontEntry entry : _availabelFontFamilies.get(family)) {
if (entry.src != null) {
byte[] font1 = _sharedCtx.getUserAgentCallback().getBinaryResource(entry.src);
if (font1 == null) {
XRLog.log(Level.WARNING, LogMessageId.LogMessageId1Param.EXCEPTION_COULD_NOT_LOAD_FONT, entry.src);
continue;
}

try {
_fontFactory.registerFont(Font.TRUETYPE_FONT, new ByteArrayInputStream(font1));
} catch (IOException | FontFormatException e) {
XRLog.log(Level.WARNING, LogMessageId.LogMessageId0Param.EXCEPTION_MATHML_COULD_NOT_REGISTER_FONT, e);
}
} else if (entry.file != null) {
try {
_fontFactory.registerFont(Font.TRUETYPE_FONT, entry.file);
} catch (IOException | FontFormatException e) {
XRLog.log(Level.WARNING, LogMessageId.LogMessageId0Param.EXCEPTION_MATHML_COULD_NOT_REGISTER_FONT, e);
}
}
}
}

@Override
public SVGImage buildSVGImage(Element mathMlElement, Box box, CssContext c, double cssWidth,
Expand All @@ -117,7 +127,7 @@ public SVGImage buildSVGImage(Element mathMlElement, Box box, CssContext c, doub
double cssMaxWidth = CalculatedStyle.getCSSMaxWidth(c, box);
double cssMaxHeight = CalculatedStyle.getCSSMaxHeight(c, box);
List<String> fontList = Arrays.asList(fonts);

MathMLImage img = new MathMLImage(mathMlElement, box, cssWidth, cssHeight, cssMaxWidth, cssMaxHeight, dotsPerPixel, fontList);

return img;
Expand All @@ -129,7 +139,9 @@ public void close() throws IOException {
}

public void addFontFile(File fontFile, String family, Integer weight, FontStyle style) {
// TODO Auto-generated method stub

FontEntry entry = new FontEntry();
entry.file = fontFile;

this._availabelFontFamilies.computeIfAbsent(family, f -> new ArrayList<>()).add(entry);
}
}

0 comments on commit 4af4f08

Please sign in to comment.