Skip to content

Commit d6dd84a

Browse files
yulian-gaponenkoitext-teamcity
authored andcommitted
Avoid FontProgram#getBaseName method usage when it's not appropriate, update documentation
ITXT-CR-284 Autoported commit. Original commit hash: [3873717]
1 parent 84b8912 commit d6dd84a

File tree

4 files changed

+31
-40
lines changed

4 files changed

+31
-40
lines changed

itext/itext.io/itext/io/font/FontProgramDescriptorFactory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ public static FontProgramDescriptor FetchDescriptor(String fontName) {
6262
}
6363
}
6464
try {
65-
if (isBuiltinFonts14 || fontName.ToLowerInvariant().EndsWith(".afm") || fontName.ToLowerInvariant().EndsWith
66-
(".pfm")) {
65+
String fontNameLowerCase = fontName.ToLowerInvariant();
66+
if (isBuiltinFonts14 || fontNameLowerCase.EndsWith(".afm") || fontNameLowerCase.EndsWith(".pfm")) {
6767
fontDescriptor = FetchType1FontDescriptor(fontName, null);
6868
}
6969
else {
7070
if (isCidFont) {
7171
fontDescriptor = FetchCidFontDescriptor(fontName);
7272
}
7373
else {
74-
if (baseName.ToLowerInvariant().EndsWith(".ttf") || baseName.ToLowerInvariant().EndsWith(".otf")) {
74+
if (fontNameLowerCase.EndsWith(".ttf") || fontNameLowerCase.EndsWith(".otf")) {
7575
fontDescriptor = FetchTrueTypeFontDescriptor(fontName);
7676
}
7777
else {
78-
fontDescriptor = FetchTTCDescriptor(baseName);
78+
fontDescriptor = FetchTTCDescriptor(fontName);
7979
}
8080
}
8181
}

itext/itext.io/itext/io/font/FontProgramFactory.cs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,9 @@ public static FontProgram CreateFont() {
7171
/// <summary>Creates a new font program.</summary>
7272
/// <remarks>
7373
/// Creates a new font program. This font program can be one of the 14 built in fonts,
74-
/// a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or one from collection) or
74+
/// a Type1 font referred to by an AFM or PFM file, a TrueType font or
7575
/// a CJK font from the Adobe Asian Font Pack.
76-
/// TrueType fonts and CJK fonts can have an optional style modifier
77-
/// appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
78-
/// example would be "STSong-Light,Bold". Note that this modifiers do not work if
79-
/// the font is embedded. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
76+
/// Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
8077
/// This would get the second font (indexes start at 0), in this case "MS PGothic".
8178
/// <p/>
8279
/// The fonts are cached and if they already exist they are extracted from the cache,
@@ -97,12 +94,9 @@ public static FontProgram CreateFont(String fontProgram) {
9794
/// <summary>Creates a new font program.</summary>
9895
/// <remarks>
9996
/// Creates a new font program. This font program can be one of the 14 built in fonts,
100-
/// a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or one from collection) or
97+
/// a Type1 font referred to by an AFM or PFM file, a TrueType font or
10198
/// a CJK font from the Adobe Asian Font Pack.
102-
/// TrueType fonts and CJK fonts can have an optional style modifier
103-
/// appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
104-
/// example would be "STSong-Light,Bold". Note that this modifiers do not work if
105-
/// the font is embedded. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
99+
/// Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
106100
/// This would get the second font (indexes start at 0), in this case "MS PGothic".
107101
/// <p/>
108102
/// The fonts are cached and if they already exist they are extracted from the cache,
@@ -124,12 +118,9 @@ public static FontProgram CreateFont(String fontProgram, bool cached) {
124118
/// <summary>Creates a new font program.</summary>
125119
/// <remarks>
126120
/// Creates a new font program. This font program can be one of the 14 built in fonts,
127-
/// a Type1 font referred to by an AFM or PFM file, a TrueType font (simple only) or
121+
/// a Type1 font referred to by an AFM or PFM file, a TrueType font or
128122
/// a CJK font from the Adobe Asian Font Pack.
129-
/// TrueType fonts and CJK fonts can have an optional style modifier
130-
/// appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
131-
/// example would be "STSong-Light,Bold". Note that this modifiers do not work if
132-
/// the font is embedded. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
123+
/// Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
133124
/// This would get the second font (indexes start at 0), in this case "MS PGothic".
134125
/// <p/>
135126
/// The fonts are cached and if they already exist they are extracted from the cache,
@@ -150,12 +141,9 @@ public static FontProgram CreateFont(byte[] fontProgram) {
150141
/// <summary>Creates a new font program.</summary>
151142
/// <remarks>
152143
/// Creates a new font program. This font program can be one of the 14 built in fonts,
153-
/// a Type 1 font referred to by an AFM or PFM file, a TrueType font (simple only) or
144+
/// a Type 1 font referred to by an AFM or PFM file, a TrueType font or
154145
/// a CJK font from the Adobe Asian Font Pack.
155-
/// TrueType fonts and CJK fonts can have an optional style modifier
156-
/// appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
157-
/// example would be "STSong-Light,Bold". Note that this modifiers do not work if
158-
/// the font is embedded. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
146+
/// Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
159147
/// This would get the second font (indexes start at 0), in this case "MS PGothic".
160148
/// <p/>
161149
/// The fonts are cached and if they already exist they are extracted from the cache,
@@ -212,20 +200,24 @@ public static FontProgram CreateFont(String name, byte[] fontProgram, bool cache
212200
}
213201
}
214202
else {
215-
if (isBuiltinFonts14 || name.ToLowerInvariant().EndsWith(".afm") || name.ToLowerInvariant().EndsWith(".pfm"
216-
)) {
203+
String fontFileExtension = null;
204+
int extensionBeginIndex = name.LastIndexOf('.');
205+
if (extensionBeginIndex > 0) {
206+
fontFileExtension = name.Substring(extensionBeginIndex).ToLowerInvariant();
207+
}
208+
if (isBuiltinFonts14 || ".afm".Equals(fontFileExtension) || ".pfm".Equals(fontFileExtension)) {
217209
fontBuilt = new Type1Font(name, null, null, null);
218210
}
219211
else {
220212
if (isCidFont) {
221213
fontBuilt = new CidFont(name, FontCache.GetCompatibleCmaps(baseName));
222214
}
223215
else {
224-
if (baseName.ToLowerInvariant().EndsWith(".ttf") || baseName.ToLowerInvariant().EndsWith(".otf") || baseName
225-
.ToLowerInvariant().EndsWith(".woff")) {
226-
if (baseName.ToLowerInvariant().EndsWith(".woff")) {
216+
if (".ttf".Equals(fontFileExtension) || ".otf".Equals(fontFileExtension) || ".woff".Equals(fontFileExtension
217+
)) {
218+
if (".woff".Equals(fontFileExtension)) {
227219
if (fontProgram == null) {
228-
fontProgram = StreamUtil.InputStreamToArray(new FileStream(baseName, FileMode.Open, FileAccess.Read));
220+
fontProgram = StreamUtil.InputStreamToArray(new FileStream(name, FileMode.Open, FileAccess.Read));
229221
}
230222
try {
231223
fontProgram = WoffConverter.Convert(fontProgram);
@@ -242,13 +234,13 @@ public static FontProgram CreateFont(String name, byte[] fontProgram, bool cache
242234
}
243235
}
244236
else {
245-
int ttcSplit = baseName.ToLowerInvariant().IndexOf(".ttc,", StringComparison.Ordinal);
237+
int ttcSplit = name.ToLowerInvariant().IndexOf(".ttc,", StringComparison.Ordinal);
246238
if (ttcSplit > 0) {
247239
try {
248-
String ttcName = baseName.JSubstring(0, ttcSplit + 4);
249-
//count(.ttc) = 4
250-
int ttcIndex = System.Convert.ToInt32(baseName.Substring(ttcSplit + 5));
251-
//count(.ttc,) = 5)
240+
String ttcName = name.JSubstring(0, ttcSplit + 4);
241+
// count(.ttc) = 4
242+
int ttcIndex = System.Convert.ToInt32(name.Substring(ttcSplit + 5));
243+
// count(.ttc,) = 5)
252244
fontBuilt = new TrueTypeFont(ttcName, ttcIndex);
253245
}
254246
catch (FormatException nfe) {

itext/itext.io/itext/io/font/OpenTypeParser.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,10 @@ public OpenTypeParser(String ttcPath, int ttcIndex) {
278278

279279
/// <exception cref="System.IO.IOException"/>
280280
public OpenTypeParser(String name) {
281-
String nameBase = FontProgram.GetBaseName(name);
282-
String ttcName = GetTTCName(nameBase);
281+
String ttcName = GetTTCName(name);
283282
this.fileName = ttcName;
284-
if (ttcName.Length < nameBase.Length) {
285-
ttcIndex = System.Convert.ToInt32(nameBase.Substring(ttcName.Length + 1));
283+
if (ttcName.Length < name.Length) {
284+
ttcIndex = System.Convert.ToInt32(name.Substring(ttcName.Length + 1));
286285
}
287286
raf = new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateBestSource(fileName));
288287
InitializeSfntTables();

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7b0a4be987e60090352475e4d9c897ff7f73aea9
1+
38737174a7ce7345f417aa84d761937e54d10da7

0 commit comments

Comments
 (0)