Skip to content

Commit 3403f65

Browse files
committed
PdfFont#containsGlyph(int) refactoring
#containsGlyph used to save junk glyphs. DEVSIX-1043
1 parent e481f70 commit 3403f65

File tree

5 files changed

+59
-22
lines changed

5 files changed

+59
-22
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ public boolean containsGlyph(int unicode) {
142142
}
143143
}
144144

145+
/**
146+
* Check whether font contains glyph with specified unicode.
147+
*
148+
* @param text a java unicode string
149+
* @param from start index. one or two char may be used.
150+
* @return true if font contains glyph, represented with the unicode code point,
151+
* otherwise false.
152+
*/
153+
public abstract boolean containsGlyph(String text, int from);
154+
145155
public abstract GlyphLine createGlyphLine(String content);
146156

147157
/**
@@ -167,8 +177,6 @@ public boolean containsGlyph(int unicode) {
167177
*/
168178
public abstract int appendAnyGlyph(String text, int from, List<Glyph> glyphs);
169179

170-
public abstract boolean containsGlyph(String text, int from);
171-
172180
/**
173181
* Converts the text into bytes to be placed in the document.
174182
* The conversion is done according to the font and the encoding and the characters

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,19 @@ public Glyph getGlyph(int unicode) {
117117

118118
@Override
119119
public boolean containsGlyph(String text, int from) {
120-
//TODO TrueType what if font is specific?
121-
int ch = text.charAt(from);
120+
return containsGlyph((int) text.charAt(from));
121+
}
122+
123+
@Override
124+
public boolean containsGlyph(int unicode) {
122125
if (fontEncoding.isFontSpecific()) {
123-
return fontProgram.getGlyphByCode(ch) != null;
126+
return fontProgram.getGlyphByCode(unicode) != null;
124127
} else {
125-
return fontEncoding.canEncode(ch)
126-
&& getFontProgram().getGlyph(fontEncoding.getUnicodeDifference(ch)) != null;
128+
return fontEncoding.canEncode(unicode)
129+
&& getFontProgram().getGlyph(fontEncoding.getUnicodeDifference(unicode)) != null;
127130
}
128131
}
129132

130-
131133
@Override
132134
public void flush() {
133135
//TODO make subtype class member and simplify this method

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,28 @@ public boolean containsGlyph(String text, int from) {
245245
return containsUnicodeGlyph(text, from);
246246
}
247247
} else {
248-
throw new PdfException("font.has.no.suitable.cmap");
248+
throw new PdfException("Invalid CID font type: " + cidFontType);
249249
}
250250
}
251251

252-
private boolean containsUnicodeGlyph(String text, int from) {
253-
int ch;
254-
if (TextUtil.isSurrogatePair(text, from)) {
255-
ch = TextUtil.convertToUtf32(text, from);
252+
@Override
253+
public boolean containsGlyph(int unicode) {
254+
if (cidFontType == CID_FONT_TYPE_0) {
255+
if (cmapEncoding.isDirect()) {
256+
return fontProgram.getGlyphByCode(unicode) != null;
257+
} else {
258+
return getFontProgram().getGlyph(unicode) != null;
259+
}
260+
} else if (cidFontType == CID_FONT_TYPE_2) {
261+
if (fontProgram.isFontSpecific()) {
262+
byte[] b = PdfEncodings.convertToBytes((char) unicode, "symboltt");
263+
return b.length > 0 && fontProgram.getGlyph(b[0] & 0xff) != null;
264+
} else {
265+
return getFontProgram().getGlyph(unicode) != null;
266+
}
256267
} else {
257-
ch = text.charAt(from);
268+
throw new PdfException("Invalid CID font type: " + cidFontType);
258269
}
259-
return getFontProgram().getGlyph(ch) != null;
260270
}
261271

262272
@Override
@@ -758,6 +768,16 @@ protected void addRangeUni(TrueTypeFont ttf, Map<Integer, int[]> longTag, boolea
758768
}
759769
}
760770

771+
private boolean containsUnicodeGlyph(String text, int from) {
772+
int ch;
773+
if (TextUtil.isSurrogatePair(text, from)) {
774+
ch = TextUtil.convertToUtf32(text, from);
775+
} else {
776+
ch = text.charAt(from);
777+
}
778+
return getFontProgram().getGlyph(ch) != null;
779+
}
780+
761781
private static String getOrdering(PdfDictionary cidFont) {
762782
PdfDictionary cidinfo = cidFont.getAsDictionary(PdfName.CIDSystemInfo);
763783
if (cidinfo == null)

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,16 @@ public Glyph getGlyph(int unicode) {
129129

130130
@Override
131131
public boolean containsGlyph(String text, int from) {
132-
int ch = text.charAt(from);
133-
if (fontEncoding.canEncode(ch)) {
132+
return containsGlyph((int) text.charAt(from));
133+
}
134+
135+
@Override
136+
public boolean containsGlyph(int unicode) {
137+
if (fontEncoding.canEncode(unicode)) {
134138
if (fontEncoding.isFontSpecific()) {
135-
return getFontProgram().getGlyphByCode(ch) != null;
139+
return getFontProgram().getGlyphByCode(unicode) != null;
136140
} else {
137-
return getFontProgram().getGlyph(fontEncoding.getUnicodeDifference(ch)) != null;
141+
return getFontProgram().getGlyph(fontEncoding.getUnicodeDifference(unicode)) != null;
138142
}
139143
} else {
140144
return false;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,14 @@ public Glyph getGlyph(int unicode) {
204204

205205
@Override
206206
public boolean containsGlyph(String text, int from) {
207-
int ch = text.charAt(from);
208-
return (fontEncoding.canEncode(ch) || ch < 33)
209-
&& getFontProgram().getGlyph(fontEncoding.getUnicodeDifference(ch)) != null;
207+
return containsGlyph((int) text.charAt(from));
210208
}
211209

210+
@Override
211+
public boolean containsGlyph(int unicode) {
212+
return (fontEncoding.canEncode(unicode) || unicode < 33)
213+
&& getFontProgram().getGlyph(fontEncoding.getUnicodeDifference(unicode)) != null;
214+
}
212215

213216
@Override
214217
protected PdfDictionary getFontDescriptor(String fontName) {

0 commit comments

Comments
 (0)