Skip to content

Commit

Permalink
#423 #422 Allow a font-face rule src property to contain several sources
Browse files Browse the repository at this point in the history
with format tags. We select the truetype font. This allows some Google font stylesheets to be used (where a truetype font is provided), however, consider the network load issue if running a template multiple times.

With tests for:
+ Simple uri which was previously all we supported.
+ Multiple uris/local with format tags.
+ Import of google CSS font sheet (manual test to avoid network load on each test run).
  • Loading branch information
danfickle committed Dec 9, 2019
1 parent 6af81c0 commit c520a52
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1488,14 +1488,27 @@ public static class Right extends LengthLikeWithAuto {
public static class Src extends GenericURIWithNone {
@Override
public List<PropertyDeclaration> buildDeclarations(CSSName cssName, List<PropertyValue> values, int origin, boolean important, boolean inheritAllowed) {
if (values.size() == 1) {
return super.buildDeclarations(cssName, values, origin, important, inheritAllowed);
}

for (int i = 0; i < values.size(); i++) {
PropertyValue value = values.get(i);
PropertyValue next = (i + 1 < values.size()) ? values.get(i + 1) : null;

if (value.getPrimitiveType() != CSSPrimitiveValue.CSS_URI ||
next == null ||
next.getPropertyValueType() != PropertyValue.VALUE_TYPE_FUNCTION ||
!"format".equals(next.getFunction().getName()) ||
next.getFunction().getParameters().size() < 1 ||
!"truetype".equals(next.getFunction().getParameters().get(0).getStringValue())) {
continue;
} else {
return super.buildDeclarations(cssName, Collections.singletonList(value), origin, important, inheritAllowed);
}
}






return super.buildDeclarations(cssName, values, origin, important, inheritAllowed);
throw new CSSParseException("Could not find font src with format(truetype) in list (" + values.toString() + ") of fonts", -1);
}
}

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>
<head>
<style>
@page {
size: 200px 200px;
}
@font-face {
font-family: 'Karla';
src: url(http://example.com/should-not-load.woff) format(woff),
url(fonts/Karla-Bold.ttf) format(truetype),
url(http://example.com/should-not-load.eot) format(eot);
}
body {
font-family: 'Karla';
}
</style>
</head>
<body>
This is a test. Text should be bold.
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>
<head>
<style>
@import url('https://fonts.googleapis.com/css?family=PT+Sans&amp;display=swap&amp;subset=cyrillic,cyrillic-ext,latin-ext');
@page {
size: 20cm 6cm;
}
body {
font-family: 'PT Sans', sans-serif;
}
</style>
</head>
<body>
ABCČĆDĐEFGHIJKLMNOPQRSŠTUVWXYZŽabcčćdđefghijklmnopqrsštuvwxyzž
АБВГҐДЂЕЁЄЖЗЅИІЇЙЈКЛЉМНЊОПРСТЋ
УЎФХЦЧЏШЩЪЫЬЭЮЯабвгґдђеёєжзѕиіїйјклљмнњопрстћуўфхцчџшщъыьэюя
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>
<head>
<style>
@page {
size: 200px 200px;
}
@font-face {
font-family: 'Karla';
src: url(fonts/Karla-Bold.ttf);
}
body {
font-family: 'Karla';
}
</style>
</head>
<body>
This is a test. Text should be bold.
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,33 @@ public void testIssue420JustifyTextWhiteSpacePre() throws IOException {
public void testIssue309ClassCastExceptionOnFloatTd() throws IOException {
assertTrue(vt.runTest("issue-309-classcastexception-on-float-td"));
}

/**
* Tests that a font-face rule with multiple sources in different formats
* loads the truetype font only.
*/
@Test
public void testCssFontFaceRuleAdvanced() throws IOException {
assertTrue(vt.runTest("css-font-face-rule-advanced"));
}

/**
* Tests that a simple font-face rule continues to work.
*/
@Test
public void testCssFontFaceRuleSimple() throws IOException {
assertTrue(vt.runTest("css-font-face-rule-simple"));
}

/**
* Tests that a google font import will work (provided that truetype font is included).
*/
@Test
@Ignore // Passing manual test - we do not want to rely on google always returning the same thing
// and network load of font slows down the tests.
public void testCssFontFaceRuleGoogle() throws IOException {
assertTrue(vt.runTest("css-font-face-rule-google"));
}

// TODO:
// + Elements that appear just on generated overflow pages.
Expand Down

0 comments on commit c520a52

Please sign in to comment.