@@ -44,7 +44,11 @@ This file is part of the iText (R) project.
44
44
45
45
import com .itextpdf .io .font .FontProgramDescriptor ;
46
46
47
- import java .util .*;
47
+ import java .util .ArrayList ;
48
+ import java .util .Collection ;
49
+ import java .util .Collections ;
50
+ import java .util .Comparator ;
51
+ import java .util .List ;
48
52
49
53
/**
50
54
* Sort given set of fonts according to font name and style.
@@ -74,8 +78,9 @@ public class FontSelector {
74
78
75
79
/**
76
80
* Create new FontSelector instance.
77
- * @param allFonts Unsorted set of all available fonts.
78
- * @param fontFamilies Sorted list of preferred font families.
81
+ *
82
+ * @param allFonts Unsorted set of all available fonts.
83
+ * @param fontFamilies Sorted list of preferred font families.
79
84
*/
80
85
public FontSelector (Collection <FontInfo > allFonts , List <String > fontFamilies , FontCharacteristics fc ) {
81
86
this .fonts = new ArrayList <>(allFonts );
@@ -95,6 +100,7 @@ public final FontInfo bestMatch() {
95
100
96
101
/**
97
102
* Sorted set of fonts.
103
+ *
98
104
* @return sorted set of fonts
99
105
*/
100
106
public final Iterable <FontInfo > getFonts () {
@@ -215,39 +221,42 @@ private static int characteristicsSimilarity(String fontName, FontCharacteristic
215
221
}
216
222
}
217
223
218
- FontProgramDescriptor descriptor = fontInfo .getDescriptor ();
219
- // Note, aliases are custom behaviour, so in FontSelector will find only exact name,
220
- // it should not be any 'contains' with aliases.
221
- boolean checkContains = true ;
222
-
223
- if (fontName .equals (descriptor .getFullNameLowerCase ())) {
224
- // the next condition can be simplified. it's been written that way to prevent mistakes if the condition is moved.
225
- score += checkContains ? FULL_NAME_EQUALS_AWARD : EQUALS_ADDITIONAL_AWARD ;
226
- checkContains = false ;
227
- }
228
- if (fontName .equals (descriptor .getFontNameLowerCase ())) {
229
- score += checkContains ? FONT_NAME_EQUALS_AWARD : EQUALS_ADDITIONAL_AWARD ;
230
- checkContains = false ;
231
- }
232
- if (fontName .equals (fontInfo .getAlias ())) {
233
- score += checkContains ? ALIAS_EQUALS_AWARD : EQUALS_ADDITIONAL_AWARD ;
234
- checkContains = false ;
235
- }
224
+ // empty font name means that font family wasn't detected. in that case one should compare only style characteristics
225
+ if (!"" .equals (fontName )) {
226
+ FontProgramDescriptor descriptor = fontInfo .getDescriptor ();
227
+ // Note, aliases are custom behaviour, so in FontSelector will find only exact name,
228
+ // it should not be any 'contains' with aliases.
229
+ boolean checkContains = true ;
236
230
237
- if (checkContains ) {
238
- boolean conditionHasBeenSatisfied = false ;
239
- if (descriptor .getFullNameLowerCase ().contains (fontName )) {
231
+ if (fontName .equals (descriptor .getFullNameLowerCase ())) {
240
232
// the next condition can be simplified. it's been written that way to prevent mistakes if the condition is moved.
241
- score += conditionHasBeenSatisfied ? FULL_NAME_CONTAINS_AWARD : CONTAINS_ADDITIONAL_AWARD ;
242
- conditionHasBeenSatisfied = true ;
233
+ score += checkContains ? FULL_NAME_EQUALS_AWARD : EQUALS_ADDITIONAL_AWARD ;
234
+ checkContains = false ;
243
235
}
244
- if (descriptor .getFontNameLowerCase (). contains ( fontName )) {
245
- score += conditionHasBeenSatisfied ? FONT_NAME_CONTAINS_AWARD : CONTAINS_ADDITIONAL_AWARD ;
246
- conditionHasBeenSatisfied = true ;
236
+ if (fontName . equals ( descriptor .getFontNameLowerCase ())) {
237
+ score += checkContains ? FONT_NAME_EQUALS_AWARD : EQUALS_ADDITIONAL_AWARD ;
238
+ checkContains = false ;
247
239
}
248
- if (null != fontInfo .getAlias () && fontInfo .getAlias ().contains (fontName )) {
249
- score += conditionHasBeenSatisfied ? ALIAS_CONTAINS_AWARD : CONTAINS_ADDITIONAL_AWARD ;
250
- conditionHasBeenSatisfied = true ; // this line is redundant. it's added to prevent mistakes if other condition is added.
240
+ if (fontName .equals (fontInfo .getAlias ())) {
241
+ score += checkContains ? ALIAS_EQUALS_AWARD : EQUALS_ADDITIONAL_AWARD ;
242
+ checkContains = false ;
243
+ }
244
+
245
+ if (checkContains ) {
246
+ boolean conditionHasBeenSatisfied = false ;
247
+ if (descriptor .getFullNameLowerCase ().contains (fontName )) {
248
+ // the next condition can be simplified. it's been written that way to prevent mistakes if the condition is moved.
249
+ score += conditionHasBeenSatisfied ? FULL_NAME_CONTAINS_AWARD : CONTAINS_ADDITIONAL_AWARD ;
250
+ conditionHasBeenSatisfied = true ;
251
+ }
252
+ if (descriptor .getFontNameLowerCase ().contains (fontName )) {
253
+ score += conditionHasBeenSatisfied ? FONT_NAME_CONTAINS_AWARD : CONTAINS_ADDITIONAL_AWARD ;
254
+ conditionHasBeenSatisfied = true ;
255
+ }
256
+ if (null != fontInfo .getAlias () && fontInfo .getAlias ().contains (fontName )) {
257
+ score += conditionHasBeenSatisfied ? ALIAS_CONTAINS_AWARD : CONTAINS_ADDITIONAL_AWARD ;
258
+ conditionHasBeenSatisfied = true ; // this line is redundant. it's added to prevent mistakes if other condition is added.
259
+ }
251
260
}
252
261
}
253
262
0 commit comments