Skip to content

Commit c72ff11

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Set typo ascender/descender for type3 fonts based on font dictionary
Refactor type 3 fonts glyph space normalization. DEVSIX-5199
1 parent c7802e3 commit c72ff11

File tree

13 files changed

+353
-99
lines changed

13 files changed

+353
-99
lines changed

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

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,39 @@ public int[] getGlyphWidths() {
118118
return glyphWidths;
119119
}
120120

121+
/**
122+
* Gets typo (a.k.a. sTypo or OS/2) vertical metric corresponding to ascender.
123+
*
124+
* <p>
125+
* Typo vertical metrics are the primary source for iText ascender/descender calculations.
126+
*
127+
* @return typo ascender value in normalized 1000-units
128+
*/
121129
public int getTypoAscender() {
122130
return typoAscender;
123131
}
124132

133+
/**
134+
* Gets typo (a.k.a. sTypo or OS/2) vertical metric corresponding to descender.
135+
*
136+
* <p>
137+
* Typo vertical metrics are the primary source for iText ascender/descender calculations.
138+
*
139+
* @return typo descender value in normalized 1000-units
140+
*/
125141
public int getTypoDescender() {
126142
return typoDescender;
127143
}
128144

145+
/**
146+
* Gets the capital letters height.
147+
*
148+
* <p>
149+
* This property defines the vertical coordinate of the top of flat capital letters,
150+
* measured from the baseline.
151+
*
152+
* @return cap height in 1000-units
153+
*/
129154
public int getCapHeight() {
130155
return capHeight;
131156
}
@@ -237,14 +262,39 @@ protected void setGlyphWidths(int[] glyphWidths) {
237262
this.glyphWidths = glyphWidths;
238263
}
239264

265+
/**
266+
* Sets typo (a.k.a. sTypo or OS/2) vertical metric corresponding to ascender.
267+
*
268+
* <p>
269+
* Typo vertical metrics are the primary source for iText ascender/descender calculations.
270+
*
271+
* @param typoAscender typo ascender value in normalized 1000-units
272+
*/
240273
protected void setTypoAscender(int typoAscender) {
241274
this.typoAscender = (int) (typoAscender * normalizationCoef);
242275
}
243276

244-
protected void setTypoDescender(int typoDesctender) {
245-
this.typoDescender = (int) (typoDesctender * normalizationCoef);
246-
}
247-
277+
/**
278+
* Sets typo (a.k.a. sTypo or OS/2) vertical metric corresponding to descender.
279+
*
280+
* <p>
281+
* Typo vertical metrics are the primary source for iText ascender/descender calculations.
282+
*
283+
* @param typoDescender typo descender value in normalized 1000-units
284+
*/
285+
protected void setTypoDescender(int typoDescender) {
286+
this.typoDescender = (int) (typoDescender * normalizationCoef);
287+
}
288+
289+
/**
290+
* Sets the capital letters height.
291+
*
292+
* <p>
293+
* This property defines the vertical coordinate of the top of flat capital letters,
294+
* measured from the baseline.
295+
*
296+
* @param capHeight cap height in 1000-units
297+
*/
248298
protected void setCapHeight(int capHeight) {
249299
this.capHeight = (int) (capHeight * normalizationCoef);
250300
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,29 @@ static String trimFontStyle(String name) {
200200
}
201201
}
202202

203+
/**
204+
* Sets typo ascender. See also {@link FontMetrics#setTypoAscender(int)}.
205+
*
206+
* @param ascender typo ascender value in 1000-units
207+
*/
203208
protected void setTypoAscender(int ascender) {
204209
fontMetrics.setTypoAscender(ascender);
205210
}
206211

212+
/**
213+
* Sets typo descender. See also {@link FontMetrics#setTypoDescender(int)}.
214+
*
215+
* @param descender typo descender value in 1000-units
216+
*/
207217
protected void setTypoDescender(int descender) {
208218
fontMetrics.setTypoDescender(descender);
209219
}
210220

221+
/**
222+
* Sets the capital letters height. See also {@link FontMetrics#setCapHeight(int)}.
223+
*
224+
* @param capHeight cap height in 1000-units
225+
*/
211226
protected void setCapHeight(int capHeight) {
212227
fontMetrics.setCapHeight(capHeight);
213228
}
@@ -217,7 +232,8 @@ protected void setXHeight(int xHeight) {
217232
}
218233

219234
/**
220-
* Sets the PostScript italic angel.
235+
* Sets the PostScript italic angle.
236+
*
221237
* <p>
222238
* Italic angle in counter-clockwise degrees from the vertical. Zero for upright text, negative for text that leans to the right (forward).
223239
*

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ This file is part of the iText (R) project.
5858
import com.itextpdf.kernel.pdf.PdfObject;
5959
import com.itextpdf.kernel.pdf.PdfStream;
6060

61+
import java.util.Arrays;
6162
import java.util.HashMap;
6263

6364
import org.slf4j.Logger;
@@ -127,9 +128,7 @@ static String createRandomFontName() {
127128

128129
static int[] convertSimpleWidthsArray(PdfArray widthsArray, int first, int missingWidth) {
129130
int[] res = new int[256];
130-
for (int i = 0; i < res.length; i++) {
131-
res[i] = missingWidth;
132-
}
131+
Arrays.fill(res, missingWidth);
133132
if (widthsArray == null) {
134133
Logger logger = LoggerFactory.getLogger(FontUtil.class);
135134
logger.warn(LogMessageConstant.FONT_DICTIONARY_WITH_NO_WIDTHS);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public abstract class PdfFont extends PdfObjectWrapper<PdfDictionary> {
7777
protected FontProgram fontProgram;
7878

7979
protected static final byte[] EMPTY_BYTES = new byte[0];
80+
81+
@Deprecated
8082
protected static final double[] DEFAULT_FONT_MATRIX = {0.001, 0, 0, 0.001, 0, 0};
8183

8284
protected Map<Integer, Glyph> notdefGlyphs = new HashMap<>();
@@ -208,6 +210,7 @@ public boolean appendDecodedCodesToGlyphsList(List<Glyph> list, PdfString charac
208210

209211
public abstract void writeText(String text, PdfOutputStream stream);
210212

213+
@Deprecated
211214
public double[] getFontMatrix() {
212215
return DEFAULT_FONT_MATRIX;
213216
}

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,17 +439,7 @@ protected void flushFontData(String fontName, PdfName subtype) {
439439
if (isForceWidthsOutput() || !isBuiltInFont() || fontEncoding.hasDifferences()) {
440440
getPdfObject().put(PdfName.FirstChar, new PdfNumber(firstChar));
441441
getPdfObject().put(PdfName.LastChar, new PdfNumber(lastChar));
442-
PdfArray wd = new PdfArray();
443-
for (int k = firstChar; k <= lastChar; ++k) {
444-
if (shortTag[k] == 0) {
445-
wd.add(new PdfNumber(0));
446-
} else {
447-
//prevent lost of widths info
448-
int uni = fontEncoding.getUnicode(k);
449-
Glyph glyph = uni > -1 ? getGlyph(uni) : fontProgram.getGlyphByCode(k);
450-
wd.add(new PdfNumber(getGlyphWidth(glyph)));
451-
}
452-
}
442+
PdfArray wd = buildWidthsArray(firstChar, lastChar);
453443
getPdfObject().put(PdfName.Widths, wd);
454444
}
455445
PdfDictionary fontDescriptor = !isBuiltInFont() ? getFontDescriptor(fontName) : null;
@@ -514,12 +504,27 @@ protected PdfDictionary getFontDescriptor(String fontName) {
514504
return fontDescriptor;
515505
}
516506

507+
protected PdfArray buildWidthsArray(int firstChar, int lastChar) {
508+
PdfArray wd = new PdfArray();
509+
for (int k = firstChar; k <= lastChar; ++k) {
510+
if (shortTag[k] == 0) {
511+
wd.add(new PdfNumber(0));
512+
} else {
513+
int uni = fontEncoding.getUnicode(k);
514+
Glyph glyph = uni > -1 ? getGlyph(uni) : fontProgram.getGlyphByCode(k);
515+
wd.add(new PdfNumber(glyph != null ? glyph.getWidth() : 0));
516+
}
517+
}
518+
return wd;
519+
}
520+
517521
protected abstract void addFontStream(PdfDictionary fontDescriptor);
518522

519523
protected void setFontProgram(T fontProgram) {
520524
this.fontProgram = fontProgram;
521525
}
522526

527+
@Deprecated
523528
protected double getGlyphWidth(Glyph glyph) {
524529
return glyph != null ? glyph.getWidth() : 0;
525530
}

0 commit comments

Comments
 (0)