Skip to content

Commit fed193e

Browse files
committed
Fix CJK font generation
1 parent b80ff8d commit fed193e

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ private void initializeCidFontProperties(Map<String, Object> fontDesc) {
8484
IntHashtable metrics = (IntHashtable) fontDesc.get("W");
8585
CMapCidUni cid2Uni = FontCache.getCid2UniCmap(uniMap);
8686
avgWidth = 0;
87-
for (int cid : metrics.getKeys()) {
87+
for (int cid : cid2Uni.getCids()) {
8888
int uni = cid2Uni.lookup(cid);
89-
if (uni != 0) {
90-
Glyph glyph = new Glyph(cid, metrics.get(cid), uni);
91-
avgWidth += glyph.getWidth();
92-
codeToGlyph.put(cid, glyph);
93-
unicodeToGlyph.put(uni, glyph);
94-
}
89+
int width = metrics.containsKey(cid) ? metrics.get(cid) : DEFAULT_WIDTH;
90+
Glyph glyph = new Glyph(cid, width, uni);
91+
avgWidth += glyph.getWidth();
92+
codeToGlyph.put(cid, glyph);
93+
unicodeToGlyph.put(uni, glyph);
9594
}
95+
fixSpaceIssue();
9696
if (codeToGlyph.size() != 0) {
9797
avgWidth /= codeToGlyph.size();
9898
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public boolean hasKernPairs() {
100100
/**
101101
* Gets the kerning between two glyphs.
102102
*
103-
* @param first the first unicode value
103+
* @param first the first unicode value
104104
* @param second the second unicode value
105105
* @return the kerning to be applied
106106
*/
@@ -111,7 +111,7 @@ public int getKerning(int first, int second) {
111111
/**
112112
* Gets the kerning between two glyphs.
113113
*
114-
* @param first the first glyph
114+
* @param first the first glyph
115115
* @param second the second glyph
116116
* @return the kerning to be applied
117117
*/
@@ -230,12 +230,19 @@ protected void setFontFamily(String fontFamily) {
230230
fontNames.setFamilyName(fontFamily);
231231
}
232232

233-
protected void checkFilePath(String path){
234-
if(path != null) {
233+
protected void checkFilePath(String path) {
234+
if (path != null) {
235235
File f = new File(path);
236236
if (!FontConstants.BUILTIN_FONTS_14.contains(path) && (!f.exists() || !f.isFile())) {
237237
throw new IOException(IOException.FontFile1NotFound).setMessageParams(path);
238238
}
239239
}
240240
}
241+
242+
protected void fixSpaceIssue() {
243+
Glyph space = unicodeToGlyph.get(32);
244+
if (space != null) {
245+
codeToGlyph.put(space.getCode(), space);
246+
}
247+
}
241248
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,7 @@ private void initializeFontProperties() throws java.io.IOException {
278278
codeToGlyph.put(index, glyph);
279279
avgWidth += glyph.getWidth();
280280
}
281-
282-
Glyph space = unicodeToGlyph.get(32);
283-
if (space != null) {
284-
codeToGlyph.put(space.getCode(), space);
285-
}
286-
281+
fixSpaceIssue();
287282
for (int index = 0; index < glyphWidths.length; index++) {
288283
if (codeToGlyph.containsKey(index)) {
289284
continue;

io/src/main/java/com/itextpdf/io/font/cmap/CMapCidUni.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ void addChar(String mark, CMapObject code) {
2727
public int lookup(int character) {
2828
return map.get(character);
2929
}
30+
31+
public int[] getCids(){
32+
return map.getKeys();
33+
}
3034
}

0 commit comments

Comments
 (0)