Skip to content

Commit 0ecae2c

Browse files
committed
Fonts refactoring for autoport.
1 parent 2066c32 commit 0ecae2c

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ private static Map<String, Object> readFontProperties(String name) throws java.i
9595
IntHashtable W2 = createMetric(p.getProperty("W2"));
9696
p.remove("W2");
9797
Map<String, Object> map = new HashMap<String, Object>();
98-
for (Enumeration<Object> e = p.keys(); e.hasMoreElements();) {
99-
Object obj = e.nextElement();
98+
for (Object obj : p.keySet()) {
10099
map.put((String)obj, p.getProperty((String)obj));
101100
}
102101
map.put("W", W);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static void loadRegistry() throws java.io.IOException {
145145
Set<String> set = new HashSet<>();
146146

147147
for (String s : splitValue) {
148-
if (!s.isEmpty()) {
148+
if (s.length() != 0) {
149149
set.add(s);
150150
}
151151
}
@@ -166,7 +166,10 @@ private static Map<String, Object> readFontProperties(String name) throws java.i
166166
Properties p = new Properties();
167167
p.load(resource);
168168

169-
Map<String, Object> fontProperties = new HashMap<>((Map) p);
169+
Map<String, Object> fontProperties = new HashMap<>();
170+
for (Map.Entry<Object, Object> entry : p.entrySet()) {
171+
fontProperties.put((String) entry.getKey(), entry.getValue());
172+
}
170173
fontProperties.put(W_PROP, createMetric((String) fontProperties.get(W_PROP)));
171174
fontProperties.put(W2_PROP, createMetric((String) fontProperties.get(W2_PROP)));
172175

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.itextpdf.io.font;
22

33
import com.itextpdf.io.IOException;
4+
import com.itextpdf.io.util.ArrayUtil;
45

5-
import java.util.Arrays;
6+
import java.text.MessageFormat;
67
import java.util.Set;
78

89
/**
@@ -437,14 +438,14 @@ public static FontProgram createFont(String ttcPath, int ttcIndex) throws java.i
437438
// TODO should we cache fonts based on byte array?
438439
static FontProgram createFont(byte[] ttc, int ttcIndex, boolean cached) throws java.io.IOException {
439440
if (cached) {
440-
String ttcNameKey = String.valueOf(Arrays.deepHashCode(new Object[]{ttc})) + ttcIndex;
441+
String ttcNameKey = MessageFormat.format("{0}{1}", ArrayUtil.hashCode(ttc), ttcIndex);
441442
FontProgram fontFound = FontCache.getFont(ttcNameKey);
442443
if (fontFound != null) {
443444
return fontFound;
444445
}
445446
}
446447
FontProgram fontBuilt = new TrueTypeFont(ttc, ttcIndex);
447-
String ttcNameKey = String.valueOf(Arrays.deepHashCode(new Object[]{ttc})) + ttcIndex;
448+
String ttcNameKey = MessageFormat.format("{0}{1}", ArrayUtil.hashCode(ttc), ttcIndex);
448449
return cached ? FontCache.saveFont(fontBuilt, ttcNameKey) : fontBuilt;
449450
}
450451

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77

8-
import java.io.File;
98
import java.text.MessageFormat;
109
import java.util.ArrayList;
1110
import java.util.HashMap;
@@ -68,29 +67,29 @@ public FontRegisterProvider() {
6867
trueTypeFonts.put(FontConstants.ZAPFDINGBATS.toLowerCase(), FontConstants.ZAPFDINGBATS);
6968

7069
List<String> tmp;
71-
tmp = new ArrayList();
70+
tmp = new ArrayList<>();
7271
tmp.add(FontConstants.COURIER);
7372
tmp.add(FontConstants.COURIER_BOLD);
7473
tmp.add(FontConstants.COURIER_OBLIQUE);
7574
tmp.add(FontConstants.COURIER_BOLDOBLIQUE);
7675
fontFamilies.put(FontConstants.COURIER.toLowerCase(), tmp);
77-
tmp = new ArrayList();
76+
tmp = new ArrayList<>();
7877
tmp.add(FontConstants.HELVETICA);
7978
tmp.add(FontConstants.HELVETICA_BOLD);
8079
tmp.add(FontConstants.HELVETICA_OBLIQUE);
8180
tmp.add(FontConstants.HELVETICA_BOLDOBLIQUE);
8281
fontFamilies.put(FontConstants.HELVETICA.toLowerCase(), tmp);
83-
tmp = new ArrayList();
82+
tmp = new ArrayList<>();
8483
tmp.add(FontConstants.SYMBOL);
8584
fontFamilies.put(FontConstants.SYMBOL.toLowerCase(), tmp);
86-
tmp = new ArrayList();
85+
tmp = new ArrayList<>();
8786
tmp.add(FontConstants.TIMES_ROMAN);
8887
tmp.add(FontConstants.TIMES_BOLD);
8988
tmp.add(FontConstants.TIMES_ITALIC);
9089
tmp.add(FontConstants.TIMES_BOLDITALIC);
9190
fontFamilies.put(FontConstants.TIMES.toLowerCase(), tmp);
9291
fontFamilies.put(FontConstants.TIMES_ROMAN.toLowerCase(), tmp);
93-
tmp = new ArrayList();
92+
tmp = new ArrayList<>();
9493
tmp.add(FontConstants.ZAPFDINGBATS);
9594
fontFamilies.put(FontConstants.ZAPFDINGBATS.toLowerCase(), tmp);
9695
}
@@ -181,7 +180,7 @@ public void registerFamily(final String familyName, final String fullName, final
181180
synchronized (fontFamilies) {
182181
tmp = fontFamilies.get(familyName);
183182
if (tmp == null) {
184-
tmp = new ArrayList();
183+
tmp = new ArrayList<>();
185184
fontFamilies.put(familyName, tmp);
186185
}
187186
}
@@ -296,9 +295,7 @@ public void register(final String path, final String alias) {
296295
trueTypeFonts.put(psName, path);
297296
trueTypeFonts.put(fullName, path);
298297
}
299-
if (LOGGER.isTraceEnabled()) {
300-
LOGGER.trace(MessageFormat.format("Registered {0}", path));
301-
}
298+
LOGGER.trace(MessageFormat.format("Registered {0}", path));
302299
} catch (java.io.IOException e){
303300
throw new IOException(e);
304301
}
@@ -335,9 +332,7 @@ public int registerDirectory(final String dir) {
335332
* @return the number of fonts registered
336333
*/
337334
public int registerDirectory(final String dir, final boolean scanSubdirectories) {
338-
if (LOGGER.isDebugEnabled()) {
339-
LOGGER.debug(MessageFormat.format("Registering directory {0}, looking for fonts", dir));
340-
}
335+
LOGGER.debug(MessageFormat.format("Registering directory {0}, looking for fonts", dir));
341336
int count = 0;
342337
try {
343338
String[] files = FileUtil.getDirectoryList(dir);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,10 @@ protected void process() throws java.io.IOException {
320320
if (dirIdx >= dirCount) {
321321
if (fileName != null) {
322322
throw new IOException("the.font.index.for.1.must.be.between.0.and.2.it.was.3")
323-
.setMessageParams(fileName, String.valueOf(dirCount - 1), String.valueOf(dirIdx));
323+
.setMessageParams(fileName, dirCount - 1, dirIdx);
324324
} else {
325325
throw new IOException("the.font.index.must.be.between.0.and.1.it.was.2")
326-
.setMessageParams(String.valueOf(dirCount - 1), String.valueOf(dirIdx));
326+
.setMessageParams(dirCount - 1, dirIdx);
327327
}
328328
}
329329
raf.skipBytes(dirIdx * 4);
@@ -568,9 +568,9 @@ private void readNameTable() throws java.io.IOException {
568568
name = readStandardString(length);
569569
}
570570
names.add(new String[]{
571-
String.valueOf(platformID),
572-
String.valueOf(platformEncodingID),
573-
String.valueOf(languageID),
571+
Integer.toString(platformID),
572+
Integer.toString(platformEncodingID),
573+
Integer.toString(languageID),
574574
name
575575
});
576576
raf.seek(pos);

io/src/main/java/com/itextpdf/io/util/ArrayUtil.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,15 @@ public static int[] toArray(Collection<Integer> collection) {
2525
return array;
2626
}
2727

28+
public static int hashCode(byte a[]) {
29+
if (a == null)
30+
return 0;
31+
32+
int result = 1;
33+
for (byte element : a)
34+
result = 31 * result + element;
35+
36+
return result;
37+
}
38+
2839
}

0 commit comments

Comments
 (0)