Skip to content

Commit d9ea18d

Browse files
committed
Fix issue in font-face#font-family property with quotes.
Embed full font on any subset issues. Improve behaviour in case equal font families, but different subfamilies DEVSIX-1369
1 parent 45a41aa commit d9ea18d

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

io/src/main/java/com/itextpdf/io/LogMessageConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public final class LogMessageConstant {
7171
public static final String FLUSHED_OBJECT_CONTAINS_REFERENCE_WHICH_NOT_REFER_TO_ANY_OBJECT = "Flushed object contains indirect reference which doesn't refer to any other object (e.g. this reference might be a free reference). Null object will be written instead.";
7272
public static final String FONT_HAS_INVALID_GLYPH = "Font {0} has invalid glyph: {1}";
7373
public static final String FONT_PROPERTY_MUST_BE_PDF_FONT_OBJECT = "The Font Property must be a PdfFont object";
74+
public static final String FONT_SUBSET_ISSUE = "Font subset issue. Full font will be embedded.";
7475
public static final String FORBID_RELEASE_IS_SET = "ForbidRelease flag is set and release is called. Releasing will not be performed.";
7576
public static final String FORM_FIELD_WAS_FLUSHED = "A form field was flushed. There's no way to create this field in the AcroForm dictionary.";
7677
public static final String GRAPHICS_STATE_WAS_DELETED = "Graphics state is always deleted after event dispatching. If you want to preserve it in renderer info, use preserveGraphicsState method after receiving renderer info.";

kernel/src/main/java/com/itextpdf/kernel/font/PdfType0Font.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,10 +849,17 @@ private void flushFontData() {
849849
new PdfName(MessageFormatUtil.format("{0}-{1}", fontName, cmapEncoding.getCmapName())));
850850
fontDescriptor.put(PdfName.FontFile3, fontStream);
851851
} else {
852-
byte[] ttfBytes;
852+
byte[] ttfBytes = null;
853853
if (subset || ttf.getDirectoryOffset() != 0) {
854-
ttfBytes = ttf.getSubset(new LinkedHashSet<>(longTag.keySet()), true);
855-
} else {
854+
try {
855+
ttfBytes = ttf.getSubset(new LinkedHashSet<>(longTag.keySet()), true);
856+
} catch (com.itextpdf.io.IOException e) {
857+
Logger logger = LoggerFactory.getLogger(PdfType0Font.class);
858+
logger.warn(LogMessageConstant.FONT_SUBSET_ISSUE);
859+
ttfBytes = null;
860+
}
861+
}
862+
if (ttfBytes == null) {
856863
ttfBytes = ttf.getFontStreamBytes();
857864
}
858865
fontStream = getPdfFontStream(ttfBytes, new int[]{ttfBytes.length});

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public final class FontFamilySplitter {
5656
private static final Pattern FONT_FAMILY_PATTERN_QUOTED = Pattern.compile("^ *(('[\\w -]+')|(\"[\\w -]+\")) *$");
5757
private static final Pattern FONT_FAMILY_PATTERN_QUOTED_SELECT = Pattern.compile("[\\w-]+( +[\\w-]+)*");
5858

59-
public static List<String> splitFontFamily(String fontFamily) {
60-
if (fontFamily == null) {
59+
public static List<String> splitFontFamily(String fontFamilies) {
60+
if (fontFamilies == null) {
6161
return null;
6262
}
63-
String[] names = fontFamily.split(",");
63+
String[] names = fontFamilies.split(",");
6464
List<String> result = new ArrayList<>(names.length);
6565
for (String name : names) {
6666
if (FONT_FAMILY_PATTERN.matcher(name).matches()) {
@@ -74,4 +74,12 @@ public static List<String> splitFontFamily(String fontFamily) {
7474
}
7575
return result;
7676
}
77+
78+
public static String removeQuotes(String fontFamily) {
79+
Matcher selectMatcher = FONT_FAMILY_PATTERN_QUOTED_SELECT.matcher(fontFamily);
80+
if (selectMatcher.find()) {
81+
return selectMatcher.group();
82+
}
83+
return null;
84+
}
7785
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,24 @@ private static int characteristicsSimilarity(String fontName, FontCharacteristic
177177
FontProgramDescriptor descriptor = fontInfo.getDescriptor();
178178
// Note, aliases are custom behaviour, so in FontSelector will find only exact name,
179179
// it should not be any 'contains' with aliases.
180-
if (fontName.equals(descriptor.getFullNameLowerCase()) || fontName.equals(descriptor.getFontNameLowerCase())
181-
|| fontName.equals(fontInfo.getAlias())) {
182-
score += 10;
183-
} else if (descriptor.getFullNameLowerCase().contains(fontName) || descriptor.getFontNameLowerCase().contains(fontName)) {
180+
boolean checkContains = true;
181+
if (fontName.equals(descriptor.getFullNameLowerCase())) {
182+
score += 4;
183+
checkContains = false;
184+
}
185+
if (fontName.equals(descriptor.getFontNameLowerCase())) {
186+
score += 4;
187+
checkContains = false;
188+
}
189+
if (fontName.equals(fontInfo.getAlias())) {
190+
score += 4;
191+
checkContains = false;
192+
}
193+
194+
if (checkContains) {
184195
//yes, we will not find contains for each alias.
185-
score += 7;
196+
if (descriptor.getFullNameLowerCase().contains(fontName)) score += 3;
197+
if (descriptor.getFontNameLowerCase().contains(fontName)) score += 3;
186198
}
187199

188200
return score;

0 commit comments

Comments
 (0)