@@ -42,6 +42,8 @@ This file is part of the iText (R) project.
42
42
*/
43
43
package com .itextpdf .layout .font ;
44
44
45
+ import com .itextpdf .io .font .FontProgramDescriptor ;
46
+
45
47
import java .util .ArrayList ;
46
48
import java .util .Collection ;
47
49
import java .util .Collections ;
@@ -57,6 +59,8 @@ public class FontSelector {
57
59
58
60
private static final int EXPECTED_FONT_IS_BOLD_AWARD = 5 ;
59
61
private static final int EXPECTED_FONT_IS_NOT_BOLD_AWARD = 3 ;
62
+ private static final int EXPECTED_FONT_WEIGHT_IS_EQUALS_AWARD = 1 ;
63
+ private static final int EXPECTED_FONT_WEIGHT_IS_FAR_AWARD = 1 ;
60
64
private static final int EXPECTED_FONT_IS_ITALIC_AWARD = 5 ;
61
65
private static final int EXPECTED_FONT_IS_NOT_ITALIC_AWARD = 3 ;
62
66
private static final int EXPECTED_FONT_IS_MONOSPACED_AWARD = 5 ;
@@ -171,9 +175,10 @@ private static FontCharacteristics parseFontStyle(String fontFamily, FontCharact
171
175
*/
172
176
// TODO DEVSIX-2120 Update javadoc if necessary
173
177
private static int characteristicsSimilarity (String fontFamily , FontCharacteristics fc , FontInfo fontInfo , boolean isLastFontFamilyToBeProcessed ) {
174
- boolean isFontBold = fontInfo .getDescriptor ().isBold () || fontInfo .getDescriptor ().getFontWeight () > 500 ;
175
- boolean isFontItalic = fontInfo .getDescriptor ().isItalic () || fontInfo .getDescriptor ().getItalicAngle () < 0 ;
176
- boolean isFontMonospace = fontInfo .getDescriptor ().isMonospace ();
178
+ FontProgramDescriptor fontDescriptor = fontInfo .getDescriptor ();
179
+ boolean isFontBold = fontDescriptor .isBold () || fontDescriptor .getFontWeight () > 500 ;
180
+ boolean isFontItalic = fontDescriptor .isItalic () || fontDescriptor .getItalicAngle () < 0 ;
181
+ boolean isFontMonospace = fontDescriptor .isMonospace ();
177
182
int score = 0 ;
178
183
179
184
// if font-family is monospace, serif or sans-serif, actual font's name shouldn't be checked
@@ -197,9 +202,9 @@ private static int characteristicsSimilarity(String fontFamily, FontCharacterist
197
202
// if alias is set, fontInfo's descriptor should not be checked
198
203
if (!"" .equals (fontFamily )
199
204
&& (null == fontInfo .getAlias ()
200
- && null != fontInfo . getDescriptor () .getFamilyNameLowerCase ()
201
- && fontInfo . getDescriptor () .getFamilyNameLowerCase ().equals (fontFamily )
202
- || (null != fontInfo .getAlias () && fontInfo .getAlias ().toLowerCase ().equals (fontFamily )))) {
205
+ && null != fontDescriptor .getFamilyNameLowerCase ()
206
+ && fontDescriptor .getFamilyNameLowerCase ().equals (fontFamily )
207
+ || (null != fontInfo .getAlias () && fontInfo .getAlias ().toLowerCase ().equals (fontFamily )))) {
203
208
score += FONT_FAMILY_EQUALS_AWARD ;
204
209
} else {
205
210
if (!isLastFontFamilyToBeProcessed ) {
@@ -209,6 +214,13 @@ private static int characteristicsSimilarity(String fontFamily, FontCharacterist
209
214
}
210
215
211
216
// calculate style characteristics
217
+ int maxWeight = Math .max (fontDescriptor .getFontWeight (), fc .getFontWeight ());
218
+ int minWeight = Math .min (fontDescriptor .getFontWeight (), fc .getFontWeight ());
219
+ if (maxWeight == minWeight ) {
220
+ score += EXPECTED_FONT_WEIGHT_IS_EQUALS_AWARD ;
221
+ } else if (maxWeight - minWeight >= 300 ) {
222
+ score -= EXPECTED_FONT_WEIGHT_IS_FAR_AWARD ;
223
+ }
212
224
if (fc .isBold ()) {
213
225
if (isFontBold ) {
214
226
score += EXPECTED_FONT_IS_BOLD_AWARD ;
0 commit comments