@@ -45,7 +45,11 @@ This file is part of the iText (R) project.
45
45
46
46
import com .itextpdf .io .font .AdobeGlyphList ;
47
47
import com .itextpdf .io .font .FontEncoding ;
48
+ import com .itextpdf .io .font .FontMetrics ;
49
+ import com .itextpdf .io .font .FontNames ;
48
50
import com .itextpdf .io .font .cmap .CMapToUnicode ;
51
+ import com .itextpdf .io .font .constants .FontStretches ;
52
+ import com .itextpdf .io .font .constants .FontWeights ;
49
53
import com .itextpdf .io .font .otf .Glyph ;
50
54
import com .itextpdf .kernel .PdfException ;
51
55
import com .itextpdf .kernel .pdf .PdfArray ;
@@ -55,6 +59,7 @@ This file is part of the iText (R) project.
55
59
import com .itextpdf .kernel .pdf .PdfNumber ;
56
60
import com .itextpdf .kernel .pdf .PdfObject ;
57
61
import com .itextpdf .kernel .pdf .PdfObjectWrapper ;
62
+ import com .itextpdf .kernel .pdf .PdfString ;
58
63
59
64
/**
60
65
* Low-level API class for Type 3 fonts.
@@ -70,14 +75,15 @@ This file is part of the iText (R) project.
70
75
public class PdfType3Font extends PdfSimpleFont <Type3Font > {
71
76
72
77
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 ;
75
79
76
80
/**
77
- * Creates a Type3 font.
81
+ * Creates a Type 3 font.
78
82
*
79
83
* @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.
80
85
*/
86
+ @ Deprecated
81
87
PdfType3Font (PdfDocument document , boolean colorized ) {
82
88
super ();
83
89
makeIndirect (document );
@@ -88,7 +94,21 @@ public class PdfType3Font extends PdfSimpleFont<Type3Font> {
88
94
}
89
95
90
96
/**
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.
92
112
*
93
113
* @param fontDictionary a dictionary of type <code>/Font</code>, must have an indirect reference.
94
114
*/
@@ -127,6 +147,53 @@ public class PdfType3Font extends PdfSimpleFont<Type3Font> {
127
147
}
128
148
}
129
149
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
+
130
197
public Type3Glyph getType3Glyph (int unicode ) {
131
198
return ((Type3Font ) getFontProgram ()).getType3Glyph (unicode );
132
199
}
@@ -213,7 +280,29 @@ public boolean containsGlyph(int unicode) {
213
280
214
281
@ Override
215
282
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 ;
217
306
}
218
307
219
308
@ Override
@@ -240,9 +329,21 @@ public void flush() {
240
329
}
241
330
}
242
331
}
332
+
243
333
getPdfObject ().put (PdfName .CharProcs , charProcs );
244
334
getPdfObject ().put (PdfName .FontMatrix , new PdfArray (getFontMatrix ()));
245
335
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
+ }
246
347
super .flushFontData (null , PdfName .Type3 );
247
348
super .flush ();
248
349
}
0 commit comments