Skip to content

Commit a0654a1

Browse files
dmitry.radchukars18wrw
authored andcommitted
Improve font characteristics parsing at FontSelector level
Parse font characteristics of specific font-family rather than falsely reuse the characteristics of the previously parse family DEVSIX-5587
1 parent 4ef5318 commit a0654a1

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

layout/src/main/java/com/itextpdf/layout/font/FontSelector.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,31 +130,31 @@ public int compare(FontInfo o1, FontInfo o2) {
130130
// It's important to mention that at the FontProvider level we add the default font-family
131131
// which is to be processed if for all provided font-families the score is 0.
132132
for (int i = 0; i < fontFamilies.size() && res == 0; i++) {
133-
FontCharacteristics fc = fontStyles.get(i);
133+
final FontCharacteristics fc = fontStyles.get(i);
134134
String fontFamily = fontFamilies.get(i);
135-
136-
if ("monospace".equalsIgnoreCase(fontFamily)) {
137-
fc.setMonospaceFlag(true);
138-
}
139135
boolean isLastFontFamilyToBeProcessed = i == fontFamilies.size() - 1;
140136
res = characteristicsSimilarity(fontFamily, fc, o2, isLastFontFamilyToBeProcessed) - characteristicsSimilarity(fontFamily, fc, o1, isLastFontFamilyToBeProcessed);
141137
}
142138
return res;
143139
}
144140

145-
private static FontCharacteristics parseFontStyle(String fontFamily, FontCharacteristics fc) {
146-
if (fc == null) {
147-
fc = new FontCharacteristics();
141+
private static FontCharacteristics parseFontStyle(String fontFamily, FontCharacteristics defaultFc) {
142+
if (defaultFc == null) {
143+
defaultFc = new FontCharacteristics();
148144
}
149-
if (fc.isUndefined()) {
145+
final FontCharacteristics parsedFc = new FontCharacteristics(defaultFc);
146+
if (parsedFc.isUndefined()) {
147+
if ("monospace".equalsIgnoreCase(fontFamily)) {
148+
parsedFc.setMonospaceFlag(true);
149+
}
150150
if (fontFamily.contains("bold")) {
151-
fc.setBoldFlag(true);
151+
parsedFc.setBoldFlag(true);
152152
}
153153
if (fontFamily.contains("italic") || fontFamily.contains("oblique")) {
154-
fc.setItalicFlag(true);
154+
parsedFc.setItalicFlag(true);
155155
}
156156
}
157-
return fc;
157+
return parsedFc;
158158
}
159159

160160
/**

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,37 @@ public void openSansOutOfNotBoldFontWeightTest() {
10631063
new FontSelector(set.getFonts(), fontFamilies, fc).bestMatch().getDescriptor().getFontName());
10641064
}
10651065

1066+
@Test
1067+
public void fontCharacteristicIsUnmodifiedTest() {
1068+
FontSet set = getOpenSansFontSet();
1069+
1070+
List<String> fontFamilies = new ArrayList<>();
1071+
fontFamilies.add("OpenSans italic");
1072+
fontFamilies.add("OpenSans bold");
1073+
1074+
FontCharacteristics fc = new FontCharacteristics();
1075+
FontCharacteristics expectedFc = new FontCharacteristics(fc);
1076+
1077+
// previously font characteristics might have been updated while sorting fonts
1078+
new FontSelector(set.getFonts(), fontFamilies, fc);
1079+
1080+
Assert.assertEquals(expectedFc, fc);
1081+
}
1082+
1083+
@Test
1084+
public void fontCharacteristicsNullTest() {
1085+
FontSet set = getOpenSansFontSet();
1086+
1087+
List<String> fontFamilies = new ArrayList<>();
1088+
fontFamilies.add("test");
1089+
1090+
FontCharacteristics fc = null;
1091+
FontSelector fontSelector = new FontSelector(set.getFonts(), fontFamilies, fc);
1092+
1093+
// We expect default font characteristics to be used, e.g. as a result regular font must be the best
1094+
Assert.assertEquals("OpenSans-Regular", fontSelector.bestMatch().getDescriptor().getFontName());
1095+
}
1096+
10661097
private void checkSelector(Collection<FontInfo> fontInfoCollection, String fontFamily,
10671098
String expectedNormal, String expectedBold, String expectedItalic, String expectedBoldItalic) {
10681099
List<String> fontFamilies = new ArrayList<>();

0 commit comments

Comments
 (0)