Skip to content

Commit 3873717

Browse files
Avoid FontProgram#getBaseName method usage when it's not appropriate, update documentation
ITXT-CR-284
1 parent 7b0a4be commit 3873717

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

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

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

6868
try {
69-
if (isBuiltinFonts14 || fontName.toLowerCase().endsWith(".afm") || fontName.toLowerCase().endsWith(".pfm")) {
69+
String fontNameLowerCase = fontName.toLowerCase();
70+
if (isBuiltinFonts14 || fontNameLowerCase.endsWith(".afm") || fontNameLowerCase.endsWith(".pfm")) {
7071
fontDescriptor = fetchType1FontDescriptor(fontName, null);
7172
} else if (isCidFont) {
7273
fontDescriptor = fetchCidFontDescriptor(fontName);
73-
} else if (baseName.toLowerCase().endsWith(".ttf") || baseName.toLowerCase().endsWith(".otf")) {
74+
} else if (fontNameLowerCase.endsWith(".ttf") || fontNameLowerCase.endsWith(".otf")) {
7475
fontDescriptor = fetchTrueTypeFontDescriptor(fontName);
7576
} else {
76-
fontDescriptor = fetchTTCDescriptor(baseName);
77+
fontDescriptor = fetchTTCDescriptor(fontName);
7778
}
7879
} catch (Exception ignored) {
7980
fontDescriptor = null;

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,9 @@ public static FontProgram createFont() throws java.io.IOException {
7575

7676
/**
7777
* Creates a new font program. This font program can be one of the 14 built in fonts,
78-
* a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or one from collection) or
78+
* a Type1 font referred to by an AFM or PFM file, a TrueType font or
7979
* a CJK font from the Adobe Asian Font Pack.
80-
* TrueType fonts and CJK fonts can have an optional style modifier
81-
* appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
82-
* example would be "STSong-Light,Bold". Note that this modifiers do not work if
83-
* the font is embedded. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
80+
* Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
8481
* This would get the second font (indexes start at 0), in this case "MS PGothic".
8582
* <p/>
8683
* The fonts are cached and if they already exist they are extracted from the cache,
@@ -96,12 +93,9 @@ public static FontProgram createFont(String fontProgram) throws java.io.IOExcept
9693

9794
/**
9895
* Creates a new font program. This font program can be one of the 14 built in fonts,
99-
* a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or one from collection) or
96+
* a Type1 font referred to by an AFM or PFM file, a TrueType font or
10097
* a CJK font from the Adobe Asian Font Pack.
101-
* TrueType fonts and CJK fonts can have an optional style modifier
102-
* appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
103-
* example would be "STSong-Light,Bold". Note that this modifiers do not work if
104-
* the font is embedded. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
98+
* Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
10599
* This would get the second font (indexes start at 0), in this case "MS PGothic".
106100
* <p/>
107101
* The fonts are cached and if they already exist they are extracted from the cache,
@@ -118,12 +112,9 @@ public static FontProgram createFont(String fontProgram, boolean cached) throws
118112

119113
/**
120114
* Creates a new font program. This font program can be one of the 14 built in fonts,
121-
* a Type1 font referred to by an AFM or PFM file, a TrueType font (simple only) or
115+
* a Type1 font referred to by an AFM or PFM file, a TrueType font or
122116
* a CJK font from the Adobe Asian Font Pack.
123-
* TrueType fonts and CJK fonts can have an optional style modifier
124-
* appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
125-
* example would be "STSong-Light,Bold". Note that this modifiers do not work if
126-
* the font is embedded. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
117+
* Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
127118
* This would get the second font (indexes start at 0), in this case "MS PGothic".
128119
* <p/>
129120
* The fonts are cached and if they already exist they are extracted from the cache,
@@ -139,12 +130,9 @@ public static FontProgram createFont(byte[] fontProgram) throws java.io.IOExcept
139130

140131
/**
141132
* Creates a new font program. This font program can be one of the 14 built in fonts,
142-
* a Type 1 font referred to by an AFM or PFM file, a TrueType font (simple only) or
133+
* a Type 1 font referred to by an AFM or PFM file, a TrueType font or
143134
* a CJK font from the Adobe Asian Font Pack.
144-
* TrueType fonts and CJK fonts can have an optional style modifier
145-
* appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
146-
* example would be "STSong-Light,Bold". Note that this modifiers do not work if
147-
* the font is embedded. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
135+
* Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
148136
* This would get the second font (indexes start at 0), in this case "MS PGothic".
149137
* <p/>
150138
* The fonts are cached and if they already exist they are extracted from the cache,
@@ -199,14 +187,19 @@ public static FontProgram createFont(String name, byte[] fontProgram, boolean ca
199187
}
200188
}
201189
} else {
202-
if (isBuiltinFonts14 || name.toLowerCase().endsWith(".afm") || name.toLowerCase().endsWith(".pfm")) {
190+
String fontFileExtension = null;
191+
int extensionBeginIndex = name.lastIndexOf('.');
192+
if (extensionBeginIndex > 0) {
193+
fontFileExtension = name.substring(extensionBeginIndex).toLowerCase();
194+
}
195+
if (isBuiltinFonts14 || ".afm".equals(fontFileExtension) || ".pfm".equals(fontFileExtension)) {
203196
fontBuilt = new Type1Font(name, null, null, null);
204197
} else if (isCidFont) {
205198
fontBuilt = new CidFont(name, FontCache.getCompatibleCmaps(baseName));
206-
} else if (baseName.toLowerCase().endsWith(".ttf") || baseName.toLowerCase().endsWith(".otf") || baseName.toLowerCase().endsWith(".woff")) {
207-
if (baseName.toLowerCase().endsWith(".woff")) {
199+
} else if (".ttf".equals(fontFileExtension) || ".otf".equals(fontFileExtension) || ".woff".equals(fontFileExtension)) {
200+
if (".woff".equals(fontFileExtension)) {
208201
if (fontProgram == null) {
209-
fontProgram = StreamUtil.inputStreamToArray(new FileInputStream(baseName));
202+
fontProgram = StreamUtil.inputStreamToArray(new FileInputStream(name));
210203
}
211204
try {
212205
fontProgram = WoffConverter.convert(fontProgram);
@@ -220,11 +213,11 @@ public static FontProgram createFont(String name, byte[] fontProgram, boolean ca
220213
fontBuilt = new TrueTypeFont(name);
221214
}
222215
} else {
223-
int ttcSplit = baseName.toLowerCase().indexOf(".ttc,");
216+
int ttcSplit = name.toLowerCase().indexOf(".ttc,");
224217
if (ttcSplit > 0) {
225218
try {
226-
String ttcName = baseName.substring(0, ttcSplit + 4);//count(.ttc) = 4
227-
int ttcIndex = Integer.parseInt(baseName.substring(ttcSplit + 5));//count(.ttc,) = 5)
219+
String ttcName = name.substring(0, ttcSplit + 4); // count(.ttc) = 4
220+
int ttcIndex = Integer.parseInt(name.substring(ttcSplit + 5)); // count(.ttc,) = 5)
228221
fontBuilt = new TrueTypeFont(ttcName, ttcIndex);
229222
} catch (NumberFormatException nfe) {
230223
throw new IOException(nfe.getMessage(), nfe);

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

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

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

0 commit comments

Comments
 (0)