Skip to content

Commit 108ccc2

Browse files
committed
Refactor TextRenderInfo#splitString using PdfFont#decodeIntoGlyphLine
Consider separately cases of one-to-one byte <-> character mapping and many bytes to one character. Get rid of inconsistency of glyph widths in PdfType0Font#decodeIntoGlyphLine vs PdfType0Font#getContentWidth DEVSIX-1058
1 parent 01a8816 commit 108ccc2

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -528,24 +528,18 @@ public GlyphLine decodeIntoGlyphLine(PdfString content) {
528528
if (glyph != null && glyph.getChars() != null) {
529529
glyphs.add(glyph);
530530
} else {
531-
glyphs.add(new Glyph(0, -1));
531+
glyphs.add(new Glyph(0, fontProgram.getGlyphByCode(0).getWidth(), -1));
532532
}
533533
}
534534
return new GlyphLine(glyphs);
535535
}
536536

537537
@Override
538538
public float getContentWidth(PdfString content) {
539-
Glyph notdef = fontProgram.getGlyphByCode(0);
540539
float width = 0;
541540
GlyphLine glyphLine = decodeIntoGlyphLine(content);
542541
for (int i = glyphLine.start; i < glyphLine.end; i++) {
543-
Glyph glyph = glyphLine.get(i);
544-
if (glyph.getCode() >= 0) {
545-
width += glyph.getWidth();
546-
} else {
547-
width += notdef.getWidth();
548-
}
542+
width += glyphLine.get(i).getWidth();
549543
}
550544
return width;
551545
}

kernel/src/main/java/com/itextpdf/kernel/pdf/canvas/parser/data/TextRenderInfo.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This file is part of the iText (R) project.
4646
import com.itextpdf.io.font.otf.GlyphLine;
4747
import com.itextpdf.kernel.colors.Color;
4848
import com.itextpdf.kernel.font.PdfFont;
49+
import com.itextpdf.kernel.font.PdfType0Font;
4950
import com.itextpdf.kernel.geom.LineSegment;
5051
import com.itextpdf.kernel.geom.Matrix;
5152
import com.itextpdf.kernel.geom.Vector;
@@ -546,24 +547,28 @@ private int getCharCode(String string) {
546547
/**
547548
* Split PDF string into array of single character PDF strings.
548549
*
549-
* @param string PDF string to be splitted.
550-
* @return splitted PDF string.
550+
* @param string PDF string to be split.
551+
* @return split PDF string.
551552
*/
552553
private PdfString[] splitString(PdfString string) {
553554
checkGraphicsState();
554-
List<PdfString> strings = new ArrayList<>();
555-
String stringValue = string.getValue();
556-
for (int i = 0; i < stringValue.length(); i++) {
557-
PdfString newString = new PdfString(stringValue.substring(i, i + 1), string.getEncoding());
558-
559-
String text = gs.getFont().decode(newString);
560-
if (text.length() == 0 && i < stringValue.length() - 1) {
561-
newString = new PdfString(stringValue.substring(i, i + 2), string.getEncoding());
562-
i++;
555+
PdfFont font = gs.getFont();
556+
if (font instanceof PdfType0Font) {
557+
// Number of bytes forming one glyph can be arbitrary from [1; 4] range
558+
List<PdfString> strings = new ArrayList<>();
559+
GlyphLine glyphLine = gs.getFont().decodeIntoGlyphLine(string);
560+
for (int i = glyphLine.start; i < glyphLine.end; i++) {
561+
strings.add(new PdfString(gs.getFont().convertToBytes(glyphLine.get(i))));
563562
}
564-
strings.add(newString);
563+
return strings.toArray(new PdfString[strings.size()]);
564+
} else {
565+
// One byte corresponds to one character
566+
PdfString[] strings = new PdfString[string.getValue().length()];
567+
for (int i = 0; i < string.getValue().length(); i++) {
568+
strings[i] = new PdfString(string.getValue().substring(i, i + 1), string.getEncoding());
569+
}
570+
return strings;
565571
}
566-
return strings.toArray(new PdfString[strings.size()]);
567572
}
568573

569574
private float[] getAscentDescent() {

0 commit comments

Comments
 (0)