Skip to content

Commit 2762266

Browse files
Revert getBaseName usage avoiding in order to not fail on redundant font names usage, add woff support in FontProgramDescriptorFactory
ITXT-CR-284, DEVSIX-1148
1 parent 5d7fa18 commit 2762266

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

io/src/main/java/com/itextpdf/io/font/FontProgramDescriptorFactory.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,20 @@ public static FontProgramDescriptor fetchDescriptor(String fontName) {
6666
}
6767

6868
try {
69-
String fontNameLowerCase = fontName.toLowerCase();
69+
String fontNameLowerCase = baseName.toLowerCase();
7070
if (isBuiltinFonts14 || fontNameLowerCase.endsWith(".afm") || fontNameLowerCase.endsWith(".pfm")) {
7171
fontDescriptor = fetchType1FontDescriptor(fontName, null);
7272
} else if (isCidFont) {
7373
fontDescriptor = fetchCidFontDescriptor(fontName);
74-
} else if (fontNameLowerCase.endsWith(".ttf") || fontNameLowerCase.endsWith(".otf")) {
75-
fontDescriptor = fetchTrueTypeFontDescriptor(fontName);
74+
} else if (fontNameLowerCase.endsWith(".ttf") || fontNameLowerCase.endsWith(".otf") || fontNameLowerCase.endsWith(".woff")) {
75+
if (fontNameLowerCase.endsWith(".woff")) {
76+
byte[] fontProgram = WoffConverter.convert(FontProgramFactory.readFontBytesFromPath(baseName));
77+
fontDescriptor = fetchTrueTypeFontDescriptor(fontProgram);
78+
} else {
79+
fontDescriptor = fetchTrueTypeFontDescriptor(fontName);
80+
}
7681
} else {
77-
fontDescriptor = fetchTTCDescriptor(fontName);
82+
fontDescriptor = fetchTTCDescriptor(baseName);
7883
}
7984
} catch (Exception ignored) {
8085
fontDescriptor = null;
@@ -131,8 +136,8 @@ private static FontProgramDescriptor fetchTTCDescriptor(String baseName) throws
131136
String ttcName;
132137
int ttcIndex;
133138
try {
134-
ttcName = baseName.substring(0, ttcSplit + 4);//count(.ttc) = 4
135-
ttcIndex = Integer.parseInt(baseName.substring(ttcSplit + 5));//count(.ttc,) = 5)
139+
ttcName = baseName.substring(0, ttcSplit + 4); // count(.ttc) = 4
140+
ttcIndex = Integer.parseInt(baseName.substring(ttcSplit + 5)); // count(.ttc,) = 5)
136141
} catch (NumberFormatException nfe) {
137142
throw new IOException(nfe.getMessage(), nfe);
138143
}

io/src/main/java/com/itextpdf/io/font/FontProgramFactory.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ This file is part of the iText (R) project.
4444
package com.itextpdf.io.font;
4545

4646
import com.itextpdf.io.IOException;
47-
import com.itextpdf.io.source.IRandomAccessSource;
4847
import com.itextpdf.io.source.RandomAccessFileOrArray;
4948
import com.itextpdf.io.source.RandomAccessSourceFactory;
50-
import com.itextpdf.io.util.StreamUtil;
5149

52-
import java.io.FileInputStream;
5350
import java.text.MessageFormat;
5451
import java.util.Set;
5552

@@ -192,9 +189,9 @@ public static FontProgram createFont(String name, byte[] fontProgram, boolean ca
192189
}
193190
} else {
194191
String fontFileExtension = null;
195-
int extensionBeginIndex = name.lastIndexOf('.');
192+
int extensionBeginIndex = baseName.lastIndexOf('.');
196193
if (extensionBeginIndex > 0) {
197-
fontFileExtension = name.substring(extensionBeginIndex).toLowerCase();
194+
fontFileExtension = baseName.substring(extensionBeginIndex).toLowerCase();
198195
}
199196
if (isBuiltinFonts14 || ".afm".equals(fontFileExtension) || ".pfm".equals(fontFileExtension)) {
200197
fontBuilt = new Type1Font(name, null, null, null);
@@ -203,7 +200,7 @@ public static FontProgram createFont(String name, byte[] fontProgram, boolean ca
203200
} else if (".ttf".equals(fontFileExtension) || ".otf".equals(fontFileExtension) || ".woff".equals(fontFileExtension)) {
204201
if (".woff".equals(fontFileExtension)) {
205202
if (fontProgram == null) {
206-
fontProgram = readBytesFromPath(name);
203+
fontProgram = readFontBytesFromPath(baseName);
207204
}
208205
try {
209206
fontProgram = WoffConverter.convert(fontProgram);
@@ -217,11 +214,11 @@ public static FontProgram createFont(String name, byte[] fontProgram, boolean ca
217214
fontBuilt = new TrueTypeFont(name);
218215
}
219216
} else {
220-
int ttcSplit = name.toLowerCase().indexOf(".ttc,");
217+
int ttcSplit = baseName.toLowerCase().indexOf(".ttc,");
221218
if (ttcSplit > 0) {
222219
try {
223-
String ttcName = name.substring(0, ttcSplit + 4); // count(.ttc) = 4
224-
int ttcIndex = Integer.parseInt(name.substring(ttcSplit + 5)); // count(.ttc,) = 5)
220+
String ttcName = baseName.substring(0, ttcSplit + 4); // count(.ttc) = 4
221+
int ttcIndex = Integer.parseInt(baseName.substring(ttcSplit + 5)); // count(.ttc,) = 5)
225222
fontBuilt = new TrueTypeFont(ttcName, ttcIndex);
226223
} catch (NumberFormatException nfe) {
227224
throw new IOException(nfe.getMessage(), nfe);
@@ -490,7 +487,7 @@ private static FontCacheKey createFontCacheKey(String name, byte[] fontProgram)
490487

491488
public static void clearRegisteredFontFamilies() { fontRegisterProvider.clearRegisteredFontFamilies(); }
492489

493-
private static byte[] readBytesFromPath(String path) throws java.io.IOException {
490+
static byte[] readFontBytesFromPath(String path) throws java.io.IOException {
494491
RandomAccessFileOrArray raf = new RandomAccessFileOrArray(new RandomAccessSourceFactory().createBestSource(path));
495492
int bufLen = (int) raf.length();
496493
if (bufLen < raf.length()) {

io/src/main/java/com/itextpdf/io/font/OpenTypeParser.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,11 @@ public OpenTypeParser(String ttcPath, int ttcIndex) throws java.io.IOException {
237237
}
238238

239239
public OpenTypeParser(String name) throws java.io.IOException {
240-
String ttcName = getTTCName(name);
240+
String baseName = FontProgram.getBaseName(name);
241+
String ttcName = getTTCName(baseName);
241242
this.fileName = ttcName;
242-
if (ttcName.length() < name.length()) {
243-
ttcIndex = Integer.parseInt(name.substring(ttcName.length() + 1));
243+
if (ttcName.length() < baseName.length()) {
244+
ttcIndex = Integer.parseInt(baseName.substring(ttcName.length() + 1));
244245
}
245246
raf = new RandomAccessFileOrArray(new RandomAccessSourceFactory().createBestSource(fileName));
246247
initializeSfntTables();

0 commit comments

Comments
 (0)