Skip to content

Commit 7fc5f23

Browse files
committed
Glyph refactoring: change type of unicode field to int.
1 parent 979033f commit 7fc5f23

File tree

16 files changed

+81
-59
lines changed

16 files changed

+81
-59
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class FontEncoding implements Serializable {
2828
*/
2929
protected IntHashtable unicodeToCode;
3030

31-
protected Integer[] codeToUnicode;
31+
protected int[] codeToUnicode;
3232

3333
/**
3434
* Encoding names.
@@ -41,7 +41,8 @@ public class FontEncoding implements Serializable {
4141

4242
protected FontEncoding() {
4343
unicodeToCode = new IntHashtable(256);
44-
codeToUnicode = new Integer[256];
44+
codeToUnicode = new int[256];
45+
ArrayUtil.fillWithValue(codeToUnicode, -1);
4546
unicodeDifferences = new IntHashtable(256);
4647
fontSpecific = false;
4748
}
@@ -106,7 +107,12 @@ public boolean addSymbol(int code, int unicode) {
106107
}
107108
}
108109

109-
public Integer getUnicode(int index) {
110+
/**
111+
* Gets unicode value for corresponding font's char code.
112+
* @param index font's char code
113+
* @return -1, if the char code unsupported or valid unicode.
114+
*/
115+
public int getUnicode(int index) {
110116
return codeToUnicode[index];
111117
}
112118

@@ -173,7 +179,7 @@ public boolean canEncode(int unicode) {
173179
* @return {@code true} if {@code code} could be decoded.
174180
*/
175181
public boolean canDecode(int code) {
176-
return codeToUnicode[code] != null;
182+
return codeToUnicode[code] > -1;
177183
}
178184

179185
protected void fillCustomEncoding() {
@@ -261,7 +267,7 @@ protected void fillStandardEncoding() {
261267
name = FontConstants.notdef;
262268
} else {
263269
unicodeToCode.put(uni, ch);
264-
codeToUnicode[ch] = (int) uni;
270+
codeToUnicode[ch] = uni;
265271
unicodeDifferences.put(uni, uni);
266272
}
267273
if (differences != null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ private void initializeFontProperties() throws java.io.IOException {
285285
if (codeToGlyph.containsKey(index)) {
286286
continue;
287287
}
288-
Glyph glyph = new Glyph(index, glyphWidths[index], (Integer) null);
288+
Glyph glyph = new Glyph(index, glyphWidths[index], -1);
289289
codeToGlyph.put(index, glyph);
290290
avgWidth += glyph.getWidth();
291291
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public boolean hasKernPairs() {
9393

9494
@Override
9595
public int getKerning(Glyph first, Glyph second) {
96-
if (first.getUnicode() != null && second.getUnicode() != null) {
96+
if (first.hasValidUnicode() && second.hasValidUnicode()) {
9797
Long record = ((long)first.getUnicode() << 32) + second.getUnicode();
9898
if (kernPairs.containsKey(record)) {
9999
return kernPairs.get(record);
@@ -314,7 +314,7 @@ protected void process() throws java.io.IOException {
314314
}
315315
}
316316
Integer unicode = AdobeGlyphList.nameToUnicode(N);
317-
Glyph glyph = new Glyph(C, WX, unicode, B);
317+
Glyph glyph = new Glyph(C, WX, unicode != null ? unicode : -1, B);
318318
if (C >= 0) {
319319
codeToGlyph.put(C, glyph);
320320
}

io/src/main/java/com/itextpdf/io/font/otf/ActualTextIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private boolean glyphLinePartNeedsActualText(GlyphLine.GlyphLinePart glyphLinePa
7575
StringBuilder toUnicodeMapResult = new StringBuilder();
7676
for (int i = glyphLinePart.start; i < glyphLinePart.end; i++) {
7777
Glyph currentGlyph = glyphLine.glyphs.get(i);
78-
if (currentGlyph.getUnicode() == null) {
78+
if (!currentGlyph.hasValidUnicode()) {
7979
needsActualText = true;
8080
break;
8181
}

io/src/main/java/com/itextpdf/io/font/otf/Glyph.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public class Glyph implements Serializable {
6060
private final int width;
6161
// The normalized bbox of this Glyph.
6262
private int[] bbox = null;
63-
// utf-32 representation of glyph if appears. Zer
64-
private Integer unicode;
63+
// utf-32 representation of glyph if appears. Correct value is > -1
64+
private int unicode;
6565
// The Unicode text represented by this Glyph
6666
private char[] chars;
6767
// ture, if this Glyph is Mark
@@ -77,24 +77,24 @@ public class Glyph implements Serializable {
7777
// Index delta to base glyph. If after a glyph there are several anchored glyphs we should know we to find base glyph.
7878
byte anchorDelta = 0;
7979

80-
public Glyph(int code, int width, Integer unicode) {
80+
public Glyph(int code, int width, int unicode) {
8181
this(code, width, unicode, null, false);
8282
}
8383

8484
public Glyph(int code, int width, char[] chars) {
8585
this(code, width, codePoint(chars), chars, false);
8686
}
8787

88-
public Glyph(int code, int width, Integer unicode, int[] bbox) {
88+
public Glyph(int code, int width, int unicode, int[] bbox) {
8989
this(code, width, unicode, null, false);
9090
this.bbox = bbox;
9191
}
9292

93-
public Glyph(int width, Integer unicode) {
93+
public Glyph(int width, int unicode) {
9494
this(-1, width, unicode, getChars(unicode), false);
9595
}
9696

97-
public Glyph(int code, int width, Integer unicode, char[] chars, boolean IsMark) {
97+
public Glyph(int code, int width, int unicode, char[] chars, boolean IsMark) {
9898
this.code = code;
9999
this.width = width;
100100
this.unicode = unicode;
@@ -125,7 +125,7 @@ public Glyph(Glyph glyph, int xPlacement, int yPlacement, int xAdvance, int yAdv
125125
this.anchorDelta = (byte) anchorDelta;
126126
}
127127

128-
public Glyph(Glyph glyph, Integer unicode) {
128+
public Glyph(Glyph glyph, int unicode) {
129129
this(glyph.code, glyph.width, unicode, getChars(unicode), glyph.isMark());
130130
}
131131

@@ -141,11 +141,15 @@ public int[] getBbox() {
141141
return bbox;
142142
}
143143

144+
public boolean hasValidUnicode() {
145+
return unicode > -1;
146+
}
147+
144148
public Integer getUnicode() {
145149
return unicode;
146150
}
147151

148-
public void setUnicode(Integer unicode) {
152+
public void setUnicode(int unicode) {
149153
this.unicode = unicode;
150154
this.chars = getChars(unicode);
151155
}
@@ -242,18 +246,18 @@ public String toString() {
242246
code, chars != null ? Arrays.toString(chars) : "null", unicode, width);
243247
}
244248

245-
private static Integer codePoint(char[] a) {
249+
private static int codePoint(char[] a) {
246250
if (a != null) {
247251
if (a.length == 1 && Character.isValidCodePoint(a[0])) {
248252
return (int) a[0];
249253
} else if (a.length == 2 && Character.isHighSurrogate(a[0]) && Character.isLowSurrogate(a[1])) {
250254
return Character.toCodePoint(a[0], a[1]);
251255
}
252256
}
253-
return null;
257+
return -1;
254258
}
255259

256-
private static char[] getChars(Integer unicode) {
257-
return unicode != null ? TextUtil.convertFromUtf32(unicode) : null;
260+
private static char[] getChars(int unicode) {
261+
return unicode > -1 ? TextUtil.convertFromUtf32(unicode) : null;
258262
}
259263
}

io/src/main/java/com/itextpdf/io/font/otf/GlyphLine.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public String toUnicodeString(int start, int end) {
6363
for (int i = part.start; i < part.end; i++) {
6464
if (glyphs.get(i).getChars() != null) {
6565
str.append(glyphs.get(i).getChars());
66-
} else if (glyphs.get(i).getUnicode() != null) {
66+
} else if (glyphs.get(i).hasValidUnicode()) {
6767
str.append(TextUtil.convertFromUtf32(glyphs.get(i).getUnicode()));
6868
}
6969
}
@@ -139,7 +139,7 @@ public void substituteManyToOne(OpenTypeFontTableReader tableReader, int lookupF
139139
Glyph currentGlyph = glyphs.get(idx);
140140
if (currentGlyph.getChars() != null) {
141141
chars.append(currentGlyph.getChars());
142-
} else if (currentGlyph.getUnicode() != null) {
142+
} else if (currentGlyph.hasValidUnicode()) {
143143
chars.append(TextUtil.convertFromUtf32(currentGlyph.getUnicode()));
144144
}
145145

@@ -148,7 +148,7 @@ public void substituteManyToOne(OpenTypeFontTableReader tableReader, int lookupF
148148
currentGlyph = glyphs.get(gidx.idx);
149149
if (currentGlyph.getChars() != null) {
150150
chars.append(currentGlyph.getChars());
151-
} else if (currentGlyph.getUnicode() != null) {
151+
} else if (currentGlyph.hasValidUnicode()) {
152152
chars.append(TextUtil.convertFromUtf32(currentGlyph.getUnicode()));
153153
}
154154
removeGlyph(gidx.idx--);
@@ -166,9 +166,9 @@ public void substituteOneToOne(OpenTypeFontTableReader tableReader, int substitu
166166
Glyph newGlyph = tableReader.getGlyph(substitutionGlyphIndex);
167167
if (oldGlyph.getChars() != null) {
168168
newGlyph.setChars(oldGlyph.getChars());
169-
} else if (newGlyph.getUnicode() != null) {
169+
} else if (newGlyph.hasValidUnicode()) {
170170
newGlyph.setChars(TextUtil.convertFromUtf32(newGlyph.getUnicode()));
171-
} else if (oldGlyph.getUnicode() != null) {
171+
} else if (oldGlyph.hasValidUnicode()) {
172172
newGlyph.setChars(TextUtil.convertFromUtf32(oldGlyph.getUnicode()));
173173
}
174174
glyphs.set(idx, newGlyph);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,15 @@ public static int hashCode(byte a[]) {
3636
return result;
3737
}
3838

39+
public static void fillWithValue(int[] a, int value) {
40+
for (int i = 0; i < a.length; i++) {
41+
a[i] = value;
42+
}
43+
}
44+
45+
public static <T> void fillWithValue(T[] a, T value) {
46+
for (int i = 0; i < a.length; i++) {
47+
a[i] = value;
48+
}
49+
}
3950
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static TrueTypeFont createFontProgram(PdfDictionary fontDictionary, FontEncoding
4343
for (int i = 0; i < 256; i++) {
4444
Glyph glyph = new Glyph(i, widths[i], fontEncoding.getUnicode(i));
4545
fontProgram.codeToGlyph.put(i, glyph);
46-
if (glyph.getUnicode() != null) {
46+
if (glyph.hasValidUnicode()) {
4747
fontProgram.unicodeToGlyph.put(glyph.getUnicode(), glyph);
4848
}
4949
if (widths[i] > 0) {
@@ -69,8 +69,10 @@ static TrueTypeFont createFontProgram(PdfDictionary fontDictionary, CMapToUnicod
6969
for (int cid : toUnicode.getCodes()) {
7070
int width = widths.containsKey(cid) ? widths.get(cid) : dw;
7171
Glyph glyph = new Glyph(cid, width, toUnicode.lookup(cid));
72+
if (glyph.hasValidUnicode()) {
73+
fontProgram.unicodeToGlyph.put(glyph.getUnicode(), glyph);
74+
}
7275
fontProgram.codeToGlyph.put(cid, glyph);
73-
fontProgram.unicodeToGlyph.put(glyph.getUnicode(), glyph);
7476
fontProgram.avgWidth += width;
7577
}
7678
if (fontProgram.codeToGlyph.size() != 0) {
@@ -79,7 +81,7 @@ static TrueTypeFont createFontProgram(PdfDictionary fontDictionary, CMapToUnicod
7981
}
8082

8183
if (fontProgram.codeToGlyph.get(0) == null) {
82-
fontProgram.codeToGlyph.put(0, new Glyph(0, dw, (Integer) null));
84+
fontProgram.codeToGlyph.put(0, new Glyph(0, dw, -1));
8385
}
8486
return fontProgram;
8587
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static Type1Font createFontProgram(PdfDictionary fontDictionary, FontEncoding fo
5656
for (int i = 0; i < 256; i++) {
5757
Glyph glyph = new Glyph(i, widths[i], fontEncoding.getUnicode(i));
5858
fontProgram.codeToGlyph.put(i, glyph);
59-
if (glyph.getUnicode() != null) {
59+
if (glyph.hasValidUnicode()) {
6060
fontProgram.unicodeToGlyph.put(glyph.getUnicode(), glyph);
6161
}
6262
if (widths[i] > 0) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ public String decode(PdfString content) {
151151
byte[] contentBytes = content.getValueBytes();
152152
StringBuilder builder = new StringBuilder(contentBytes.length);
153153
for (byte b : contentBytes) {
154-
Integer uni = fontEncoding.getUnicode(b & 0xff);
155-
if (uni != null) {
154+
int uni = fontEncoding.getUnicode(b & 0xff);
155+
if (uni > -1) {
156156
builder.append((char) (int) uni);
157157
}
158158
}
@@ -164,8 +164,8 @@ public float getContentWidth(PdfString content) {
164164
float width = 0;
165165
byte[] contentBytes = content.getValueBytes();
166166
for (byte b : contentBytes) {
167-
Integer uni = fontEncoding.getUnicode(b & 0xff);
168-
Glyph glyph = uni != null ? getGlyph(uni) : fontProgram.getGlyphByCode(b);
167+
int uni = fontEncoding.getUnicode(b & 0xff);
168+
Glyph glyph = uni > -1 ? getGlyph(uni) : fontProgram.getGlyphByCode(b);
169169
width += glyph != null ? glyph.getWidth() : 0;
170170
}
171171
return width;
@@ -212,7 +212,7 @@ protected void flushFontData(String fontName, PdfName subtype) {
212212
for (int k = 0; k < shortTag.length; ++k) {
213213
// remove unsupported by encoding values in case custom encoding.
214214
// save widths information in case standard pdf encodings (winansi or macroman)
215-
if (fontEncoding.getUnicode(k) != null) {
215+
if (fontEncoding.canDecode(k)) {
216216
shortTag[k] = 1;
217217
} else if (!fontEncoding.hasDifferences() && fontProgram.getGlyphByCode(k) != null) {
218218
shortTag[k] = 1;
@@ -268,8 +268,8 @@ protected void flushFontData(String fontName, PdfName subtype) {
268268
wd.add(new PdfNumber(0));
269269
} else {
270270
//prevent lost of widths info
271-
Integer uni = fontEncoding.getUnicode(k);
272-
Glyph glyph = uni != null ? getGlyph(uni) : fontProgram.getGlyphByCode(k);
271+
int uni = fontEncoding.getUnicode(k);
272+
Glyph glyph = uni > -1 ? getGlyph(uni) : fontProgram.getGlyphByCode(k);
273273
wd.add(new PdfNumber(glyph != null ? glyph.getWidth() : 0));
274274
}
275275
}

0 commit comments

Comments
 (0)