Skip to content

Commit 991b0bb

Browse files
committed
PdfType3Font refactoring.
DEVSIX-1468
1 parent cd4781a commit 991b0bb

File tree

7 files changed

+214
-16
lines changed

7 files changed

+214
-16
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,12 @@ public int getFontWeight() {
117117
return weight;
118118
}
119119

120-
public void setFontWeight(int weight) {
121-
this.weight = weight;
120+
/**
121+
* Sets font weight.
122+
* @param weight integer form 100 to 900. See {@link FontWeights}.
123+
*/
124+
protected void setFontWeight(int weight) {
125+
this.weight = FontWeights.normalizeFontWeight(weight);
122126
}
123127

124128
/**
@@ -135,7 +139,7 @@ public String getFontStretch() {
135139
*
136140
* @param fontStretch {@link FontStretches}.
137141
*/
138-
public void setFontStretch(String fontStretch) {
142+
protected void setFontStretch(String fontStretch) {
139143
this.fontStretch = fontStretch;
140144
}
141145

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

Lines changed: 26 additions & 3 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.IOException;
4747
import com.itextpdf.io.font.constants.FontMacStyleFlags;
4848
import com.itextpdf.io.font.constants.FontStretches;
49+
import com.itextpdf.io.font.constants.FontWeights;
4950
import com.itextpdf.io.font.constants.StandardFonts;
5051
import com.itextpdf.io.font.otf.Glyph;
5152
import com.itextpdf.io.util.FileUtil;
@@ -207,6 +208,13 @@ protected void setXHeight(int xHeight) {
207208
fontMetrics.setXHeight(xHeight);
208209
}
209210

211+
/**
212+
* Sets the PostScript italic angel.
213+
* <br/>
214+
* Italic angle in counter-clockwise degrees from the vertical. Zero for upright text, negative for text that leans to the right (forward).
215+
*
216+
* @param italicAngle in counter-clockwise degrees from the vertical
217+
*/
210218
protected void setItalicAngle(int italicAngle) {
211219
fontMetrics.setItalicAngle(italicAngle);
212220
}
@@ -219,6 +227,11 @@ protected void setStemH(int stemH) {
219227
fontMetrics.setStemH(stemH);
220228
}
221229

230+
/**
231+
* Sets font weight.
232+
*
233+
* @param fontWeight integer form 100 to 900. See {@link FontWeights}.
234+
*/
222235
protected void setFontWeight(int fontWeight) {
223236
fontNames.setFontWeight(fontWeight);
224237
}
@@ -228,7 +241,7 @@ protected void setFontWeight(int fontWeight) {
228241
*
229242
* @param fontWidth {@link FontStretches}.
230243
*/
231-
protected void setFontWidth(String fontWidth) {
244+
protected void setFontStretch(String fontWidth) {
232245
fontNames.setFontStretch(fontWidth);
233246
}
234247

@@ -248,12 +261,22 @@ protected void setBbox(int[] bbox) {
248261
fontMetrics.setBbox(bbox[0], bbox[1], bbox[2], bbox[3]);
249262
}
250263

264+
/**
265+
* Sets a preferred font family name.
266+
*
267+
* @param fontFamily a preferred font family name.
268+
*/
251269
protected void setFontFamily(String fontFamily) {
252270
fontNames.setFamilyName(fontFamily);
253271
}
254272

255-
protected void setFontName(String psFontName) {
256-
fontNames.setFontName(psFontName);
273+
/**
274+
* Sets the PostScript name of the font.
275+
*
276+
* @param fontName the PostScript name of the font, shall not be null or empty.
277+
*/
278+
protected void setFontName(String fontName) {
279+
fontNames.setFontName(fontName);
257280
}
258281

259282
protected void checkFilePath(String path) {

io/src/main/java/com/itextpdf/io/font/constants/FontWeights.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,11 @@ public static int fromType1FontWeight(String weight) {
7777
}
7878
return fontWeight;
7979
}
80+
81+
public static int normalizeFontWeight(int fontWeight) {
82+
fontWeight = (fontWeight/100)*100;
83+
if (fontWeight < FontWeights.THIN) return FontWeights.THIN;
84+
if (fontWeight > FontWeights.BLACK) return FontWeights.BLACK;
85+
return fontWeight;
86+
}
8087
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ static void fillFontDescriptor(DocTrueTypeFont font, PdfDictionary fontDesc) {
201201

202202
PdfName fontStretch = fontDesc.getAsName(PdfName.FontStretch);
203203
if (fontStretch != null) {
204-
font.setFontWidth(fontStretch.getValue());
204+
font.setFontStretch(fontStretch.getValue());
205205
}
206206

207207
PdfArray bboxValue = fontDesc.getAsArray(PdfName.FontBBox);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ static void fillFontDescriptor(DocType1Font font, PdfDictionary fontDesc) {
180180

181181
PdfName fontStretch = fontDesc.getAsName(PdfName.FontStretch);
182182
if (fontStretch != null) {
183-
font.setFontWidth(fontStretch.getValue());
183+
font.setFontStretch(fontStretch.getValue());
184184
}
185185

186186
PdfArray bboxValue = fontDesc.getAsArray(PdfName.FontBBox);

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

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ This file is part of the iText (R) project.
4545

4646
import com.itextpdf.io.font.AdobeGlyphList;
4747
import com.itextpdf.io.font.FontEncoding;
48+
import com.itextpdf.io.font.FontMetrics;
49+
import com.itextpdf.io.font.FontNames;
4850
import com.itextpdf.io.font.cmap.CMapToUnicode;
51+
import com.itextpdf.io.font.constants.FontStretches;
52+
import com.itextpdf.io.font.constants.FontWeights;
4953
import com.itextpdf.io.font.otf.Glyph;
5054
import com.itextpdf.kernel.PdfException;
5155
import com.itextpdf.kernel.pdf.PdfArray;
@@ -55,6 +59,7 @@ This file is part of the iText (R) project.
5559
import com.itextpdf.kernel.pdf.PdfNumber;
5660
import com.itextpdf.kernel.pdf.PdfObject;
5761
import com.itextpdf.kernel.pdf.PdfObjectWrapper;
62+
import com.itextpdf.kernel.pdf.PdfString;
5863

5964
/**
6065
* Low-level API class for Type 3 fonts.
@@ -70,14 +75,15 @@ This file is part of the iText (R) project.
7075
public class PdfType3Font extends PdfSimpleFont<Type3Font> {
7176

7277
private static final long serialVersionUID = 4940119184993066859L;
73-
//todo use default font matrix constant
74-
private double[] fontMatrix = {0.001, 0, 0, 0.001, 0, 0};
78+
private double[] fontMatrix = DEFAULT_FONT_MATRIX;
7579

7680
/**
77-
* Creates a Type3 font.
81+
* Creates a Type 3 font.
7882
*
7983
* @param colorized defines whether the glyph color is specified in the glyph descriptions in the font.
84+
* @deprecated Type 3 font should contain font name and font family in case tagged PDF.
8085
*/
86+
@Deprecated
8187
PdfType3Font(PdfDocument document, boolean colorized) {
8288
super();
8389
makeIndirect(document);
@@ -88,7 +94,21 @@ public class PdfType3Font extends PdfSimpleFont<Type3Font> {
8894
}
8995

9096
/**
91-
* Creates a Type3 font based on an existing font dictionary, which must be an indirect object.
97+
* Creates a Type 3 font.
98+
*
99+
* @param document the target document of the new font.
100+
* @param fontName the PostScript name of the font, shall not be null or empty.
101+
* @param fontFamily a preferred font family name.
102+
* @param colorized indicates whether the font will be colorized
103+
*/
104+
PdfType3Font(PdfDocument document, String fontName, String fontFamily, boolean colorized) {
105+
this(document, colorized);
106+
((Type3Font)fontProgram).setFontName(fontName);
107+
((Type3Font)fontProgram).setFontFamily(fontFamily);
108+
}
109+
110+
/**
111+
* Creates a Type 3 font based on an existing font dictionary, which must be an indirect object.
92112
*
93113
* @param fontDictionary a dictionary of type <code>/Font</code>, must have an indirect reference.
94114
*/
@@ -127,6 +147,53 @@ public class PdfType3Font extends PdfSimpleFont<Type3Font> {
127147
}
128148
}
129149

150+
/**
151+
* Sets the PostScript name of the font.
152+
*
153+
* @param fontName the PostScript name of the font, shall not be null or empty.
154+
*/
155+
public void setFontName(String fontName) {
156+
((Type3Font)fontProgram).setFontName(fontName);
157+
}
158+
159+
/**
160+
* Sets a preferred font family name.
161+
*
162+
* @param fontFamily a preferred font family name.
163+
*/
164+
public void setFontFamily(String fontFamily) {
165+
((Type3Font)fontProgram).setFontFamily(fontFamily);
166+
}
167+
168+
/**
169+
* Sets font weight.
170+
*
171+
* @param fontWeight integer form 100 to 900. See {@link FontWeights}.
172+
*/
173+
public void setFontWeight(int fontWeight) {
174+
((Type3Font)fontProgram).setFontWeight(fontWeight);
175+
}
176+
177+
/**
178+
* Sets the PostScript italic angel.
179+
* <br/>
180+
* Italic angle in counter-clockwise degrees from the vertical. Zero for upright text, negative for text that leans to the right (forward).
181+
*
182+
* @param italicAngle in counter-clockwise degrees from the vertical
183+
*/
184+
public void setItalicAngle(int italicAngle) {
185+
((Type3Font)fontProgram).setItalicAngle(italicAngle);
186+
}
187+
188+
/**
189+
* Sets font width in css notation (font-stretch property)
190+
*
191+
* @param fontWidth {@link FontStretches}.
192+
*/
193+
public void setFontStretch(String fontWidth) {
194+
((Type3Font)fontProgram).setFontStretch(fontWidth);
195+
}
196+
130197
public Type3Glyph getType3Glyph(int unicode) {
131198
return ((Type3Font) getFontProgram()).getType3Glyph(unicode);
132199
}
@@ -213,7 +280,29 @@ public boolean containsGlyph(int unicode) {
213280

214281
@Override
215282
protected PdfDictionary getFontDescriptor(String fontName) {
216-
return null;
283+
assert fontName != null && fontName.length() > 0;
284+
PdfDictionary fontDescriptor = new PdfDictionary();
285+
makeObjectIndirect(fontDescriptor);
286+
287+
FontMetrics fontMetrics = fontProgram.getFontMetrics();
288+
FontNames fontNames = fontProgram.getFontNames();
289+
fontDescriptor.put(PdfName.Type, PdfName.FontDescriptor);
290+
fontDescriptor.put(PdfName.FontName, new PdfName(fontName));
291+
fontDescriptor.put(PdfName.CapHeight, new PdfNumber(fontMetrics.getCapHeight()));
292+
fontDescriptor.put(PdfName.ItalicAngle, new PdfNumber(fontMetrics.getItalicAngle()));
293+
fontDescriptor.put(PdfName.FontWeight, new PdfNumber(fontNames.getFontWeight()));
294+
if (fontNames.getFamilyName() != null && fontNames.getFamilyName().length > 0 && fontNames.getFamilyName()[0].length >= 4) {
295+
fontDescriptor.put(PdfName.FontFamily, new PdfString(fontNames.getFamilyName()[0][3]));
296+
}
297+
//add font stream and flush it immediately
298+
addFontStream(fontDescriptor);
299+
int flags = fontProgram.getPdfFontFlags();
300+
if (fontProgram.isFontSpecific() != fontEncoding.isFontSpecific()) {
301+
flags &= ~(4 | 32); // reset both flags
302+
flags |= fontEncoding.isFontSpecific() ? 4 : 32; // set based on font encoding
303+
}
304+
fontDescriptor.put(PdfName.Flags, new PdfNumber(flags));
305+
return fontDescriptor;
217306
}
218307

219308
@Override
@@ -240,9 +329,21 @@ public void flush() {
240329
}
241330
}
242331
}
332+
243333
getPdfObject().put(PdfName.CharProcs, charProcs);
244334
getPdfObject().put(PdfName.FontMatrix, new PdfArray(getFontMatrix()));
245335
getPdfObject().put(PdfName.FontBBox, new PdfArray(fontProgram.getFontMetrics().getBbox()));
336+
337+
if (fontProgram.getFontNames().getFontName() != null) {
338+
assert fontProgram.getFontNames().getFontName().length() > 0;
339+
PdfDictionary fontDescriptor = !isBuiltInFont() ? getFontDescriptor(fontProgram.getFontNames().getFontName()) : null;
340+
if (fontDescriptor != null) {
341+
getPdfObject().put(PdfName.FontDescriptor, fontDescriptor);
342+
if (fontDescriptor.getIndirectReference() != null) {
343+
fontDescriptor.flush();
344+
}
345+
}
346+
}
246347
super.flushFontData(null, PdfName.Type3);
247348
super.flush();
248349
}

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

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ This file is part of the iText (R) project.
4545

4646
import com.itextpdf.io.font.FontNames;
4747
import com.itextpdf.io.font.FontProgram;
48+
import com.itextpdf.io.font.constants.FontStretches;
49+
import com.itextpdf.io.font.constants.FontWeights;
4850
import com.itextpdf.io.font.otf.Glyph;
4951

5052
import java.util.HashMap;
@@ -56,12 +58,12 @@ public class Type3Font extends FontProgram {
5658

5759
private final Map<Integer, Type3Glyph> type3Glyphs = new HashMap<>();
5860
private boolean colorized = false;
59-
61+
private int flags = 0;
6062

6163
public Type3Font(boolean colorized) {
6264
this.colorized = colorized;
65+
this.fontNames = new FontNames();
6366
getFontMetrics().setBbox(0, 0, 0, 0);
64-
fontNames = new FontNames();
6567
}
6668

6769
public Type3Glyph getType3Glyph(int unicode) {
@@ -70,7 +72,16 @@ public Type3Glyph getType3Glyph(int unicode) {
7072

7173
@Override
7274
public int getPdfFontFlags() {
73-
return 0;
75+
return flags;
76+
}
77+
78+
/**
79+
* Sets Font descriptor flags.
80+
*
81+
* @param flags
82+
*/
83+
public void setPdfFontFlags(int flags) {
84+
this.flags = flags;
7485
}
7586

7687
@Override
@@ -91,6 +102,58 @@ public int getGlyphsCount() {
91102
return type3Glyphs.size();
92103
}
93104

105+
/**
106+
* Sets the PostScript name of the font.
107+
*
108+
* @param fontName the PostScript name of the font, shall not be null or empty.
109+
*/
110+
@Override //This dummy override allows PdfType3Font to set font name.
111+
protected void setFontName(String fontName) {
112+
super.setFontName(fontName);
113+
}
114+
115+
/**
116+
* Sets a preferred font family name.
117+
*
118+
* @param fontFamily a preferred font family name.
119+
*/
120+
@Override //This dummy override allows PdfType3Font to set font family.
121+
protected void setFontFamily(String fontFamily) {
122+
super.setFontFamily(fontFamily);
123+
}
124+
125+
/**
126+
* Sets font weight.
127+
*
128+
* @param fontWeight integer form 100 to 900. See {@link FontWeights}.
129+
*/
130+
@Override //This dummy override allows PdfType3Font to set font weight.
131+
protected void setFontWeight(int fontWeight) {
132+
super.setFontWeight(fontWeight);
133+
}
134+
135+
/**
136+
* Sets font width in css notation (font-stretch property)
137+
*
138+
* @param fontWidth {@link FontStretches}.
139+
*/
140+
@Override //This dummy override allows PdfType3Font to set font stretch.
141+
protected void setFontStretch(String fontWidth) {
142+
super.setFontStretch(fontWidth);
143+
}
144+
145+
/**
146+
* Sets the PostScript italic angel.
147+
* <br/>
148+
* Italic angle in counter-clockwise degrees from the vertical. Zero for upright text, negative for text that leans to the right (forward).
149+
*
150+
* @param italicAngle in counter-clockwise degrees from the vertical
151+
*/
152+
@Override //This dummy override allows PdfType3Font to set the PostScript italicAngel.
153+
protected void setItalicAngle(int italicAngle) {
154+
super.setItalicAngle(italicAngle);
155+
}
156+
94157
void addGlyph(int code, int unicode, int width, int[] bbox, Type3Glyph type3Glyph) {
95158
Glyph glyph = new Glyph(code, width, unicode, bbox);
96159
codeToGlyph.put(code, glyph);

0 commit comments

Comments
 (0)