Debian Codegraph @

pdf/pdf_font_types.h

raw
1 /* Copyright (C) 2019-2025 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 /* font structures for the PDF interpreter */
17
18 /* Fonts are, unfortunately, complicated by the fact that the graphics state font object
19 * the 'gs_font' structure is not reference counted. The PostScript interpreter relies upon
20 * the garbage collector to clean these up, but since we aren't using a garbage collector
21 * we have to manage the lifetime of these ourselves.
22 *
23 * The complication is that the gs_font pointer is stored in the graphics state and there
24 * is no simple mechanism to know when a font in a graphics state is no longer being used
25 * (in the absence of a garbage collector). So what we do is use the 'client_data' member
26 * of the gs_font structure to point to our font object, a pdf_font *, which itself
27 * contains a pointer to the gs_font structure.
28 *
29 * On each pdfi_gsave() we look at the gs_font in the current graphics state (if any) and
30 * use the client_data to find the corresponding pdf_font, we then cout that object up. On
31 * each pdfi_grestore we again use the client_data to find our pdf_font object, and count
32 * it down. The Tf operator replaces the gs_font in the current graphics state, and we count
33 * the current one down, and the new one up, before replacing it.
34 *
35 * When the pdf_font object's reference count reaches 0 we can safely free the associated
36 * gs_font structure. Note that the cache may still contain a reference to the pdf_font
37 * object, so it may not be freed immediately it is no longer in use. This helps us avoid
38 * too much redefinition of fonts.
39 *
40 * When we create a pdf_font object, we replace the cached object, which is a dictionary,
41 * with the pdf_font object. So when we call pdfi_dereference() if we get back a pdf_font
42 * object we know that this is an existing font (keyed by object number) and don't need to
43 * rebuild it. If we get a pdf_dict object then we do need to build the graphics library
44 * font.
45 *
46 * This still does leave the (admittedly remote) possibility of using a font in one graphics
47 * state, gsave'ing, setting a new font, and then dereferencing enough objects to force the
48 * original font out of the cache, then using that same font again. While there is no
49 * obvious problem with that, it is inefficient. I think we can evade the problem by using
50 * the next and prev pointers in the gs_fotn structure. If we keep the linked list correct
51 * when creating and freeing fonts, we can walk the list of 'active' fonts, following
52 * the clien_data pointer to get the pdf_font object, and checking its object number against
53 * the one we are looking for. We only need to do this if the result of pdfi_derference()
54 * Is a pdf_dict object. Obviously if we don't find an active font then we need to go
55 * through the effort of creating one.
56 *
57 * FIXME - none of the above is yet implemented!
58 */
59
60 #ifndef PDF_FONT_DATA
61 #define PDF_FONT_DATA
62
63 #include "pdf_types.h"
64 #include "pdf_cmap.h"
65 #include "gxfont.h"
66 #include "gsfont.h"
67
68 typedef enum pdf_font_type_e {
69 e_pdf_font_type0, /* Somewhat special a font type 0 is a placeholder for a CIDFont */
70 e_pdf_font_type1,
71 e_pdf_font_cff,
72 e_pdf_font_type3,
73 e_pdf_cidfont_type0 = 9,
74 e_pdf_cidfont_type1,
75 e_pdf_cidfont_type2,
76 e_pdf_cidfont_type4 = 32,
77 e_pdf_font_truetype = 42,
78 e_pdf_font_microtype = 51 /* MicroType 51 is arbitrary, to match graphics lib */
79 } pdf_font_type;
80
81 #define pdf_font_base \
82 pdf_obj_common;\
83 gs_font_base *pfont; /* Graphics library font structure */\
84 pdf_font_type pdfi_font_type; /* See above */\
85 pdf_dict *PDF_font; /* The original font dictionary from the PDF file */\
86 pdf_obj *BaseFont; /* Should be a name object, but best allow for strings as well */\
87 pdf_dict *FontDescriptor; /* For PDF up to 1.4 this may be absent for the base 14 */ \
88 int64_t descflags; \
89 pdf_obj *ToUnicode; /* Name or stream (technically should be a stream, but we've seen Identity names */ \
90 bool substitute; /* We need to know what a CIDFont is a substitute */ \
91 pdf_string *filename; /* If we read this from disk, this is the file it came from */ \
92 font_proc_font_info((*default_font_info)); \
93 font_type orig_FontType; \
94 pdf_string *copyright; \
95 pdf_string *notice; \
96 pdf_string *fullname; \
97 pdf_string *familyname
98
99 #define pdf_font_common \
100 pdf_font_base;\
101 pdf_obj *Name; /* Should be a name object, but best allow for strings as well */\
102 unsigned int FirstChar; /* For PDF up to 1.4 this may be absent for the base 14 */\
103 unsigned int LastChar; /* For PDF up to 1.4 this may be absent for the base 14 */\
104 double MissingWidth; \
105 double *Widths; /* For PDF up to 1.4 this may be absent for the base 14 */\
106 pdf_array *Encoding /* Array built from name or dictionary */
107
108
109 /* The registry and ordering strings in gs_font_cid0_data are just references to
110 strings assumed to be managed be managed by the interpreter - so we have to stash
111 them in the pdfi font, too.
112 */
113 #define pdf_cid_font_common \
114 pdf_font_base;\
115 pdf_dict *CIDSystemInfo; \
116 double DW; \
117 pdf_array *W; \
118 pdf_array *DW2; \
119 pdf_array *W2; \
120 pdf_string *registry; \
121 pdf_string *ordering; \
122 int supplement; \
123 pdf_buffer *cidtogidmap; \
124 pdfi_cid_decoding_t *decoding; /* Used when substituting a non-Identity CIDFont */ \
125 pdfi_cid_subst_nwp_table_t *substnwp; /* Also used for CIDFont substitions */ \
126 font_proc_glyph_info((*orig_glyph_info))
127
128 typedef struct pdf_font_s {
129 pdf_font_common;
130 }pdf_font;
131
132 /* Decoding tables are statically defined, so require no memory management */
133 typedef struct pdfi_cid_decoding_s {
134 const char *s_order;
135 const char *d_order;
136 const int nranges;
137 const int val_sizes;
138 const int ranges[114][256][4];
139 } pdfi_cid_decoding_t;
140
141 struct pdfi_cid_subst_nwp_rec_s {
142 const char s_type; /* Orig type: n==narrow, w==wide, p==proportional */
143 const unsigned int s_scid; /* Start source CID */
144 const unsigned int e_scid; /* End source CID */
145 const unsigned int s_dcid; /* Start destination CID */
146 const char d_type; /* Destination type */
147 };
148
149 typedef struct pdfi_cid_subst_nwp_table_s {
150 const char *ordering;
151 struct pdfi_cid_subst_nwp_rec_s subst[32];
152 } pdfi_cid_subst_nwp_table_t;
153
154 typedef struct pdfi_font_decoding_entry_s {
155 const char *glyph_name;
156 const int32_t glyph_name_len;
157 int32_t code_point;
158 } pdfi_font_decoding_entry_t;
159
160 typedef struct pdfi_font_decoding_s {
161 const char *dec_name;
162 const int32_t dec_name_len;
163 const int32_t nentries;
164 const pdfi_font_decoding_entry_t entries[3376];
165 } pdfi_font_decoding_t;
166
167 typedef struct pdf_font_type0_s {
168 pdf_font_base; /* For this font type, the FontDescriptor will be NULL, as will the pfont, (we use the DescendantFont) */
169
170 pdf_obj *Encoding; /* CMap */
171 pdf_array *DescendantFonts; /* A single element array specifying the CIDFont dictionary */
172 pdfi_cid_decoding_t *decoding; /* Used when substituting a non-Identity CIDFont */
173 pdfi_cid_subst_nwp_table_t *substnwp; /* Also used for CIDFont substitions */
174 } pdf_font_type0;
175
176 typedef struct pdf_font_type1_s {
177 pdf_font_common;
178 pdf_array *Subrs;
179 pdf_dict *CharStrings;
180 /* Multiple Master Support - weightvector is stored in gs_font_type1 */
181 pdf_array *blenddesignpositions;
182 pdf_array *blenddesignmap;
183 pdf_array *blendfontbbox;
184 pdf_array *blendaxistypes;
185 } pdf_font_type1;
186
187 typedef struct pdf_font_cff_s {
188 pdf_font_common;
189 pdf_array *Subrs;
190 int NumSubrs;
191 pdf_array *GlobalSubrs;
192 int NumGlobalSubrs;
193 pdf_dict *CharStrings;
194 int ncharstrings;
195 } pdf_font_cff;
196
197 typedef struct pdf_font_type3_s {
198 pdf_font_common;
199
200 pdf_array *FontBBox;
201 pdf_array *FontMatrix;
202 pdf_dict *CharProcs;
203 } pdf_font_type3;
204
205 typedef enum {
206 pdfi_truetype_cmap_none,
207 pdfi_truetype_cmap_10,
208 pdfi_truetype_cmap_30,
209 pdfi_truetype_cmap_31,
210 pdfi_truetype_cmap_310
211 } pdfi_truetype_cmap;
212
213 typedef struct pdf_font_truetype_s {
214 pdf_font_common;
215 pdf_buffer *sfnt;
216 pdfi_truetype_cmap cmap;
217 pdf_dict *post;
218 } pdf_font_truetype;
219
220 typedef struct pdf_cidfont_s {
221 pdf_cid_font_common;
222 } pdf_cidfont_t;
223
224 typedef struct pdf_cidfont_type0_s {
225 pdf_cid_font_common;
226 pdf_array *Subrs;
227 int NumSubrs;
228 pdf_array *GlobalSubrs;
229 int NumGlobalSubrs;
230 pdf_dict *CharStrings;
231 int ncharstrings;
232 pdf_array *FDArray;
233 int cidcount;
234 int uidbase;
235 } pdf_cidfont_type0;
236
237 typedef struct pdf_cidfont_type2_s {
238 pdf_cid_font_common;
239 pdf_buffer *sfnt;
240 } pdf_cidfont_type2;
241
242 typedef struct pdf_font_microtype_s {
243 pdf_font_common;
244 int32_t fco_index;
245 char *DecodingID;
246 } pdf_font_microtype;
247
248 #endif