@@ -44,7 +44,11 @@ This file is part of the iText (R) project.
4444
4545import com .itextpdf .io .font .FontProgramDescriptor ;
4646
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 ;
4852
4953/**
5054 * Sort given set of fonts according to font name and style.
@@ -74,8 +78,9 @@ public class FontSelector {
7478
7579 /**
7680 * 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.
7984 */
8085 public FontSelector (Collection <FontInfo > allFonts , List <String > fontFamilies , FontCharacteristics fc ) {
8186 this .fonts = new ArrayList <>(allFonts );
@@ -95,6 +100,7 @@ public final FontInfo bestMatch() {
95100
96101 /**
97102 * Sorted set of fonts.
103+ *
98104 * @return sorted set of fonts
99105 */
100106 public final Iterable <FontInfo > getFonts () {
@@ -215,39 +221,42 @@ private static int characteristicsSimilarity(String fontName, FontCharacteristic
215221 }
216222 }
217223
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 ;
236230
237- if (checkContains ) {
238- boolean conditionHasBeenSatisfied = false ;
239- if (descriptor .getFullNameLowerCase ().contains (fontName )) {
231+ if (fontName .equals (descriptor .getFullNameLowerCase ())) {
240232 // 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 ;
243235 }
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 ;
247239 }
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+ }
251260 }
252261 }
253262
0 commit comments