Skip to content

Commit 1ec2da9

Browse files
ars18wrwpavel-alay
authored andcommitted
Add new unit fontselector tests for open sans font-family.
Add a html template to see how browsers select fonts. DEVSIX-2097
1 parent d3d32cc commit 1ec2da9

File tree

13 files changed

+432
-34
lines changed

13 files changed

+432
-34
lines changed

layout/src/test/java/com/itextpdf/layout/FontSelectorTest.java

Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ This file is part of the iText (R) project.
6363
import com.itextpdf.test.annotations.type.IntegrationTest;
6464
import org.junit.Assert;
6565
import org.junit.BeforeClass;
66+
import org.junit.Ignore;
6667
import org.junit.Test;
6768
import org.junit.experimental.categories.Category;
6869

@@ -78,8 +79,6 @@ public class FontSelectorTest extends ExtendedITextTest {
7879
public static final String destinationFolder = "./target/test/com/itextpdf/layout/FontSelectorTest/";
7980
public static final String fontsFolder = "./src/test/resources/com/itextpdf/layout/fonts/";
8081

81-
private static final String PANGRAM = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
82-
8382
@BeforeClass
8483
public static void beforeClass() {
8584
createDestinationFolder(destinationFolder);
@@ -112,6 +111,7 @@ public void cyrillicAndLatinGroup() throws Exception {
112111
}
113112

114113
@Test
114+
// TODO DEVSIX-2120 The font-family name of Puritan2.otf is 'Puritan 2.0' but this name doesn't match font-family name pattern
115115
public void cyrillicAndLatinGroup2() throws Exception {
116116
String fileName = "cyrillicAndLatinGroup2";
117117
String outFileName = destinationFolder + fileName + ".pdf";
@@ -413,124 +413,157 @@ public void singleFontWithUnicodeRange() throws Exception {
413413
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff" + fileName));
414414
}
415415

416-
417416
@Test
418417
public void standardFontSetTimesTest01() {
419-
standardFontSetTest("Times", "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic");
418+
checkSelector(getStandardFontSet().getFonts(), "Times", "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic");
420419
}
421420

422421
@Test
423422
public void standardFontSetHelveticaTest01() {
424-
standardFontSetTest("Helvetica", "Helvetica", "Helvetica-Bold", "Helvetica-Oblique", "Helvetica-BoldOblique");
423+
checkSelector(getStandardFontSet().getFonts(), "Helvetica", "Helvetica", "Helvetica-Bold", "Helvetica-Oblique", "Helvetica-BoldOblique");
425424
}
426425

427426
@Test
428-
public void standardFontSetCourierTest() {
429-
standardFontSetTest("Courier", "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique");
427+
public void standardFontSetCourierTest01() {
428+
checkSelector(getStandardFontSet().getFonts(), "Courier", "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique");
430429
}
431430

432-
private void standardFontSetTest(String fontFamily, String expectedNormal, String expectedBold, String expectedItalic, String expectedBoldItalic) {
433-
Collection<FontInfo> fontInfoCollection = getStandardFontSet().getFonts();
431+
@Test
432+
// TODO DEVSIX-2120 Currently both light and regular fonts have the same score so that light is picked up lexicographically. After the changes are implemented the correct one (regular) font shall be selected and the expected constants should be updated
433+
// TODO Default font shall be specified.
434+
public void openSansFontSetIncorrectNameTest01() {
435+
FontSet set = getOpenSansFontSet();
436+
addTimesFonts(set);
437+
checkSelector(set.getFonts(), "OpenSans", "OpenSans-Light", "OpenSans-Bold", "OpenSans-LightItalic", "OpenSans-BoldItalic");
438+
}
439+
440+
@Test
441+
// TODO DEVSIX-2120 Currently both light and regular fonts have the same score so that light is picked up lexicographically. After the changes are implemented the correct one (regular) font shall be selected and the expected constants should be updated
442+
// TODO Default font shall be specified.
443+
public void openSansFontSetRegularTest01() {
444+
FontSet set = getOpenSansFontSet();
445+
addTimesFonts(set);
446+
checkSelector(set.getFonts(), "Open Sans", "OpenSans-Light", "OpenSans-Bold", "OpenSans-LightItalic", "OpenSans-BoldItalic");
447+
}
448+
449+
@Test
450+
// TODO DEVSIX-2127 After DEVSIX-2120 the font should be selected correctly, but the text will still need to be bolded via emulation
451+
// TODO DEVSIX-2120 Light subfamily is not processed
452+
// TODO Default font shall be specified.
453+
public void openSansFontSetLightTest01() {
454+
FontSet set = getOpenSansFontSet();
455+
addTimesFonts(set);
456+
checkSelector(set.getFonts(), "Open Sans Light", "OpenSans-Light", "OpenSans-Bold", "OpenSans-LightItalic", "OpenSans-BoldItalic");
457+
}
458+
459+
@Test
460+
// TODO DEVSIX-2120 ExtraBold subfamily is not processed
461+
// TODO DEVSIX-2135 if FontCharacteristics instance is not modified, font-family is parsed and 'bold' substring is considered as a reason to set bold flag in FontCharacteristics instance. That should be reviewed.
462+
@Ignore("DEVSIX-2120: we cannot set a wrong expected string for normal font characteristics because in different contexts iText selects different fonts")
463+
public void openSansFontSetExtraBoldTest01() {
464+
FontSet set = getOpenSansFontSet();
465+
addTimesFonts(set);
466+
checkSelector(set.getFonts(), "Open Sans ExtraBold", "Times-Bold", "Times-Bold", "Times-BoldItalic", "Times-BoldItalic");
467+
}
434468

469+
private void checkSelector(Collection<FontInfo> fontInfoCollection, String fontFamily,
470+
String expectedNormal, String expectedBold, String expectedItalic, String expectedBoldItalic) {
435471
List<String> fontFamilies = new ArrayList<>();
436472
fontFamilies.add(fontFamily);
437473

438-
FontCharacteristics fc;
439-
440474
// Normal
441475

442-
fc = new FontCharacteristics();
443-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedNormal));
476+
FontCharacteristics fc = new FontCharacteristics();
477+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedNormal);
444478

445479
fc = new FontCharacteristics();
446480
fc.setFontWeight((short) 300);
447-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedNormal));
481+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedNormal);
448482

449483
fc = new FontCharacteristics();
450484
fc.setFontWeight((short) 100);
451-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedNormal));
485+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedNormal);
452486

453487
fc = new FontCharacteristics();
454488
fc.setFontWeight("normal");
455-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedNormal));
489+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedNormal);
456490

457491
fc = new FontCharacteristics();
458492
fc.setFontStyle("normal");
459-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedNormal));
493+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedNormal);
460494

461495

462496
// Bold
463497

464498
fc = new FontCharacteristics();
465499
fc.setBoldFlag(true);
466-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedBold));
500+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedBold);
467501

468502
fc = new FontCharacteristics();
469503
fc.setFontWeight("bold");
470-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedBold));
504+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedBold);
471505

472506
fc = new FontCharacteristics();
473507
fc.setFontWeight((short) 700);
474-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedBold));
508+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedBold);
475509

476510
fc = new FontCharacteristics();
477511
fc.setFontWeight((short) 800);
478-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedBold));
512+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedBold);
479513

480514

481515
// Italic
482516

483517
fc = new FontCharacteristics();
484518
fc.setFontStyle("italic");
485-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedItalic));
519+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedItalic);
486520

487521
fc = new FontCharacteristics();
488522
fc.setFontStyle("italic");
489523
fc.setFontWeight("normal");
490-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedItalic));
524+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedItalic);
491525

492526
fc = new FontCharacteristics();
493527
fc.setFontStyle("italic");
494528
fc.setFontWeight((short) 300);
495-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedItalic));
529+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedItalic);
496530

497531
fc = new FontCharacteristics();
498532
fc.setFontStyle("italic");
499533
fc.setFontWeight((short) 500);
500-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedItalic));
534+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedItalic);
501535

502536
fc = new FontCharacteristics();
503537
fc.setFontStyle("oblique");
504-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedItalic));
538+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedItalic);
505539

506540

507541
// BoldItalic
508542

509543
fc = new FontCharacteristics();
510544
fc.setFontStyle("italic");
511545
fc.setFontWeight("bold");
512-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedBoldItalic));
546+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedBoldItalic);
513547

514548
fc = new FontCharacteristics();
515549
fc.setFontStyle("oblique");
516550
fc.setFontWeight("bold");
517-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedBoldItalic));
551+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedBoldItalic);
518552

519553
fc = new FontCharacteristics();
520554
fc.setFontStyle("italic");
521555
fc.setFontWeight((short) 700);
522-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedBoldItalic));
556+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedBoldItalic);
523557

524558
fc = new FontCharacteristics();
525559
fc.setFontStyle("italic");
526560
fc.setFontWeight((short) 800);
527-
Assert.assertTrue(checkFontSelector(fontInfoCollection, fontFamilies, fc, expectedBoldItalic));
561+
assertSelectedFont(fontInfoCollection, fontFamilies, fc, expectedBoldItalic);
528562
}
529563

530564

531-
private boolean checkFontSelector(Collection<FontInfo> fontInfoCollection, List<String> fontFamilies, FontCharacteristics fc, String expectedFontName) {
532-
return expectedFontName.equals(new FontSelector(fontInfoCollection, fontFamilies, fc).bestMatch().getFontName());
533-
565+
private void assertSelectedFont(Collection<FontInfo> fontInfoCollection, List<String> fontFamilies, FontCharacteristics fc, String expectedFontName) {
566+
Assert.assertEquals(expectedFontName, new FontSelector(fontInfoCollection, fontFamilies, fc).bestMatch().getDescriptor().getFontName());
534567
}
535568

536569
private static FontSet getStandardFontSet() {
@@ -544,11 +577,32 @@ private static FontSet getStandardFontSet() {
544577
set.addFont(StandardFonts.HELVETICA_BOLDOBLIQUE);
545578
set.addFont(StandardFonts.HELVETICA_OBLIQUE);
546579
set.addFont(StandardFonts.SYMBOL);
580+
set.addFont(StandardFonts.ZAPFDINGBATS);
581+
addTimesFonts(set);
582+
return set;
583+
}
584+
585+
private static FontSet getOpenSansFontSet() {
586+
String openSansFolder = "Open_Sans/";
587+
FontSet set = new FontSet();
588+
set.addFont(fontsFolder + openSansFolder + "OpenSans-Bold.ttf");
589+
set.addFont(fontsFolder + openSansFolder + "OpenSans-BoldItalic.ttf");
590+
set.addFont(fontsFolder + openSansFolder + "OpenSans-ExtraBold.ttf");
591+
set.addFont(fontsFolder + openSansFolder + "OpenSans-ExtraBoldItalic.ttf");
592+
set.addFont(fontsFolder + openSansFolder + "OpenSans-Light.ttf");
593+
set.addFont(fontsFolder + openSansFolder + "OpenSans-LightItalic.ttf");
594+
set.addFont(fontsFolder + openSansFolder + "OpenSans-Regular.ttf");
595+
set.addFont(fontsFolder + openSansFolder + "OpenSans-Italic.ttf");
596+
set.addFont(fontsFolder + openSansFolder + "OpenSans-SemiBold.ttf");
597+
set.addFont(fontsFolder + openSansFolder + "OpenSans-SemiBoldItalic.ttf");
598+
return set;
599+
}
600+
601+
private static FontSet addTimesFonts(FontSet set) {
547602
set.addFont(StandardFonts.TIMES_ROMAN);
548603
set.addFont(StandardFonts.TIMES_BOLD);
549604
set.addFont(StandardFonts.TIMES_BOLDITALIC);
550605
set.addFont(StandardFonts.TIMES_ITALIC);
551-
set.addFont(StandardFonts.ZAPFDINGBATS);
552606
return set;
553607
}
554608

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<html>
2+
<head>
3+
<style>
4+
th, td {
5+
text-align: center;
6+
height: 45px;
7+
border: 1px solid black;
8+
}
9+
10+
table {
11+
font-family: Courier;
12+
}
13+
14+
tr.separator {
15+
background-color: red;
16+
}
17+
td.lastColumn {
18+
font-family: 'Open Sans Light';
19+
}
20+
</style>
21+
</head>
22+
<body>
23+
<table>
24+
<col width="50">
25+
<col width="50">
26+
<col width="50">
27+
<col width="300">
28+
<tr>
29+
<th scope="col">Font-family</th>
30+
<th scope="col">Font-weight</th>
31+
<th scope="col">Font-style</th>
32+
<th scope="col">Result</th>
33+
</tr>
34+
<tr class="separator"><td colspan="4">Normal</td></tr>
35+
<tr>
36+
<td>open sans</td>
37+
<td>300</td>
38+
<td></td>
39+
<td class="lastColumn" style="font-weight: 300;">quick brown fox jumps over the lazy dog</td>
40+
</tr>
41+
<tr>
42+
<td>open sans</td>
43+
<td>100</td>
44+
<td></td>
45+
<td class="lastColumn" style="font-weight: 100;">quick brown fox jumps over the lazy dog</td>
46+
</tr>
47+
<tr>
48+
<td>open sans</td>
49+
<td>normal</td>
50+
<td></td>
51+
<td class="lastColumn" style="font-weight: normal;">quick brown fox jumps over the lazy dog</td>
52+
</tr>
53+
<tr>
54+
<td>open sans</td>
55+
<td></td>
56+
<td>normal</td>
57+
<td class="lastColumn" style="font-style: normal;">quick brown fox jumps over the lazy dog</td>
58+
</tr>
59+
<tr class="separator"><td colspan="4">Bold</td></tr>
60+
<tr>
61+
<td>open sans</td>
62+
<td>bold as flag</td>
63+
<td></td>
64+
<td class="lastColumn" style="font-weight: bold;">quick brown fox jumps over the lazy dog</td>
65+
</tr>
66+
<tr>
67+
<td>open sans</td>
68+
<td>bold</td>
69+
<td></td>
70+
<td class="lastColumn" style="font-weight: bold;">quick brown fox jumps over the lazy dog</td>
71+
</tr>
72+
<tr>
73+
<td>open sans</td>
74+
<td>700</td>
75+
<td></td>
76+
<td class="lastColumn" style="font-weight: 700;">quick brown fox jumps over the lazy dog</td>
77+
</tr>
78+
<tr>
79+
<td>open sans</td>
80+
<td>800</td>
81+
<td></td>
82+
<td class="lastColumn" style="font-weight: 800;">quick brown fox jumps over the lazy dog</td>
83+
</tr>
84+
<tr class="separator"><td colspan="4">Italic</td></tr>
85+
<tr>
86+
<td>open sans</td>
87+
<td></td>
88+
<td>italic</td>
89+
<td class="lastColumn" style="font-style: italic;">quick brown fox jumps over the lazy dog</td>
90+
</tr>
91+
<tr>
92+
<td>open sans</td>
93+
<td>normal</td>
94+
<td>italic</td>
95+
<td class="lastColumn" style="font-weight: normal; font-style: italic;">quick brown fox jumps over the lazy dog</td>
96+
</tr>
97+
<tr>
98+
<td>open sans</td>
99+
<td>300</td>
100+
<td>italic</td>
101+
<td class="lastColumn" style="font-weight: 300; font-style: italic;">quick brown fox jumps over the lazy dog</td>
102+
</tr>
103+
<tr>
104+
<td>open sans</td>
105+
<td>500</td>
106+
<td>italic</td>
107+
<td class="lastColumn" style="font-weight: 500; font-style: italic;">quick brown fox jumps over the lazy dog</td>
108+
</tr>
109+
<tr>
110+
<td>open sans</td>
111+
<td></td>
112+
<td>oblique</td>
113+
<td class="lastColumn" style="font-style: oblique;">quick brown fox jumps over the lazy dog</td>
114+
</tr>
115+
<tr class="separator"><td colspan="4">BoldItalic</td></tr>
116+
<tr>
117+
<td>open sans</td>
118+
<td>bold</td>
119+
<td>italic</td>
120+
<td class="lastColumn" style="font-weight: bold; font-style: italic;">quick brown fox jumps over the lazy dog</td>
121+
</tr>
122+
<tr>
123+
<td>open sans</td>
124+
<td>bold</td>
125+
<td>oblique</td>
126+
<td class="lastColumn" style="font-weight: bold; font-style: oblique;">quick brown fox jumps over the lazy dog</td>
127+
</tr>
128+
<tr>
129+
<td>open sans</td>
130+
<td>700</td>
131+
<td>italic</td>
132+
<td class="lastColumn" style="font-weight: 700; font-style: italic;">quick brown fox jumps over the lazy dog</td>
133+
</tr>
134+
<tr>
135+
<td>open sans</td>
136+
<td>900</td>
137+
<td>oblique</td>
138+
<td class="lastColumn" style="font-weight: 900;font-style: oblique;">quick brown fox jumps over the lazy dog</td>
139+
</tr>
140+
</table>
141+
</body>
142+
</html>

0 commit comments

Comments
 (0)