Skip to content

Commit 30c58cc

Browse files
author
Artyom Yanchevsky
committed
Improve FontSelector with comparing font weights
DEVSIX-4147
1 parent 189827b commit 30c58cc

File tree

4 files changed

+374
-12
lines changed

4 files changed

+374
-12
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.layout.font;
4444

45+
import com.itextpdf.io.font.FontProgramDescriptor;
46+
4547
import java.util.ArrayList;
4648
import java.util.Collection;
4749
import java.util.Collections;
@@ -57,6 +59,8 @@ public class FontSelector {
5759

5860
private static final int EXPECTED_FONT_IS_BOLD_AWARD = 5;
5961
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;
6064
private static final int EXPECTED_FONT_IS_ITALIC_AWARD = 5;
6165
private static final int EXPECTED_FONT_IS_NOT_ITALIC_AWARD = 3;
6266
private static final int EXPECTED_FONT_IS_MONOSPACED_AWARD = 5;
@@ -171,9 +175,10 @@ private static FontCharacteristics parseFontStyle(String fontFamily, FontCharact
171175
*/
172176
// TODO DEVSIX-2120 Update javadoc if necessary
173177
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();
177182
int score = 0;
178183

179184
// 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
197202
// if alias is set, fontInfo's descriptor should not be checked
198203
if (!"".equals(fontFamily)
199204
&& (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)))) {
203208
score += FONT_FAMILY_EQUALS_AWARD;
204209
} else {
205210
if (!isLastFontFamilyToBeProcessed) {
@@ -209,6 +214,13 @@ private static int characteristicsSimilarity(String fontFamily, FontCharacterist
209214
}
210215

211216
// 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+
}
212224
if (fc.isBold()) {
213225
if (isFontBold) {
214226
score += EXPECTED_FONT_IS_BOLD_AWARD;

0 commit comments

Comments
 (0)