xref: /plan9/sys/src/cmd/gs/src/gdevpdtf.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 2002 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gdevpdtf.h,v 1.28 2005/04/05 09:48:46 igor Exp $ */
18 /* Font and CMap resource structure and API for pdfwrite */
19 
20 #ifndef gdevpdtf_INCLUDED
21 #  define gdevpdtf_INCLUDED
22 
23 #include "gdevpdtx.h"
24 
25 /* ================ Types and structures ================ */
26 
27 /* ---------------- Font resources ---------------- */
28 
29 /*
30  * pdfwrite manages several different flavors of font resources:
31  *
32  *  Those that have neither a FontDescriptor nor a base_font:
33  *	Type 0 (composite) fonts
34  *  Those that have no FontDescriptor, but do have a base_font:
35  *	Standard 14 fonts
36  *  Those that have a FontDescriptor but no base_font:
37  *	Type 3 bitmap fonts
38  *  Those that have a FontDescriptor with a base_font:
39  *	Type 1 / Type 2 fonts
40  *	Type 42 (TrueType) fonts
41  *	CIDFontType 0 (Type 1/2) CIDFonts
42  *	CIDFontType 2 (TrueType) CIDFonts
43  */
44 /*
45  * Font names in PDF files have caused an enormous amount of trouble, so we
46  * document specifically how they are handled in each structure.
47  *
48  * The PDF Reference specifies the BaseFont of a font resource as follows,
49  * depending on the font type:
50  *
51  *   Type 0 - if the descendant font is CIDFontType 0, the descendant font
52  *     name followed by a hyphen and the CMap name (the value of Encoding,
53  *     if a name, otherwise the CMapName from the CMap); if the descendant
54  *     font is CIDFontType 2, the descendant font name.
55  *
56  *   Type 1 - "usually" the same as the FontName in the base font.
57  *
58  *   MM Type 1 - if embedded, the same as Type 1; if not embedded, spaces
59  *     in the font name are replaced with underscores.
60  *
61  *   Type 3 - not used.
62  *
63  *   TrueType - initially, the PostScript name from the 'name' table in
64  *     the font; if none, the "name by which the font is known in the host
65  *     operating system".  Spaces are removed.  Then, under circumstances
66  *     not defined, the string ",Bold", ",Italic", or ",BoldItalic" is
67  *     appended if the font has the corresponding style properties.
68  *     [We do not do this: we simply use the key_name or font_name.]
69  *
70  *   CIDFontType 0 - "usually" the same as the CIDFontName in the base font.
71  *
72  *   CIDFontType 2 - the same as TrueType.
73  *
74  * In addition, the BaseFont has a XXXXXX+ prefix if the font is a subset
75  * (whether embedded or not).
76  *
77  * We would like to compute the BaseFont at the time that we create the font
78  * resource object.  The font descriptor (which is needed to provide
79  * information about embedding) and the base font are both available at that
80  * time.  Unfortunately, the information as to whether the font will be
81  * subsetted is not available.  Therefore, we do compute the BaseFont from
82  * the base font name when the font resource is created, to allow checking
83  * for duplicate names and for standard font names, but we compute it again
84  * after writing out the base font.
85  */
86 
87 #ifndef gs_cmap_DEFINED
88 #  define gs_cmap_DEFINED
89 typedef struct gs_cmap_s gs_cmap_t;
90 #endif
91 
92 #ifndef gs_font_type0_DEFINED
93 #  define gs_font_type0_DEFINED
94 typedef struct gs_font_type0_s gs_font_type0;
95 #endif
96 
97 #ifndef pdf_base_font_DEFINED
98 #  define pdf_base_font_DEFINED
99 typedef struct pdf_base_font_s pdf_base_font_t;
100 #endif
101 
102 #ifndef pdf_font_descriptor_DEFINED
103 #  define pdf_font_descriptor_DEFINED
104 typedef struct pdf_font_descriptor_s pdf_font_descriptor_t;
105 #endif
106 
107 #ifndef pdf_char_glyph_pair_DEFINED
108 #  define pdf_char_glyph_pair_DEFINED
109 typedef struct pdf_char_glyph_pair_s pdf_char_glyph_pair_t;
110 #endif
111 
112 struct pdf_char_glyph_pair_s {
113     gs_char chr;
114     gs_glyph glyph;
115 };
116 
117 /*
118  * The write_contents procedure is set by the implementation when the
119  * font resource is created.  It is called after generic code has opened
120  * the resource object and written the Type, BaseFont (if any),
121  * FontDescriptor reference (if any), ToUnicode CMap reference (if any),
122  * and additional dictionary entries (if any).
123  * The write_contents procedure must write any remaining entries specific
124  * to the FontType, followed by the closing ">>", and then call
125  * pdf_end_separate.  The reason for this division of function is to allow
126  * the write_contents procedure to write additional objects that the
127  * resource object references, after closing the resource object.
128  */
129 typedef int (*pdf_font_write_contents_proc_t)
130      (gx_device_pdf *, pdf_font_resource_t *);
131 
132 /*
133  * Define an element of an Encoding.  The element is unused if glyph ==
134  * GS_NO_GLYPH.
135  */
136 typedef struct pdf_encoding_element_s {
137     gs_glyph glyph;
138     gs_const_string str;
139     bool is_difference;		/* true if must be written in Differences */
140 } pdf_encoding_element_t;
141 #define private_st_pdf_encoding1() /* gdevpdtf.c */\
142   gs_private_st_const_strings1(st_pdf_encoding1,\
143     pdf_encoding_element_t, "pdf_encoding_element_t",\
144     pdf_encoding1_enum_ptrs, pdf_encoding1_reloc_ptrs, str)
145 #define private_st_pdf_encoding_element() /* gdevpdtf.c */\
146   gs_private_st_element(st_pdf_encoding_element, pdf_encoding_element_t,\
147     "pdf_encoding_element_t[]", pdf_encoding_elt_enum_ptrs,\
148     pdf_encoding_elt_reloc_ptrs, st_pdf_encoding1)
149 
150 typedef struct {
151     gs_id id;
152     pdf_resource_type_t type;
153 } pdf_resource_ref_t;
154 
155 /*
156  * Widths are the widths in the outlines: this is what PDF interpreters
157  * use, and what will be written in the PDF file.  real_widths are the
158  * widths possibly modified by Metrics[2] and CDevProc: these define the
159  * actual advance widths of the characters in the PostScript text.
160  */
161 struct pdf_font_resource_s {
162     pdf_resource_common(pdf_font_resource_t);
163     font_type FontType;		/* copied from font, if any */
164     pdf_font_write_contents_proc_t write_contents;
165     gs_string BaseFont;		/* (not used for Type 3) */
166     pdf_font_descriptor_t *FontDescriptor; /* (not used for Type 0, Type 3, */
167 				/* or standard 14 fonts) */
168     /*
169      * The base_font member is only used for
170      * the standard 14 fonts, which do not have a FontDescriptor.
171      */
172     pdf_base_font_t *base_font;	/* == FontDescriptor->base_font */
173     uint count;			/* # of chars/CIDs */
174     double *Widths;		/* [count] (not used for Type 0) */
175     byte *used;			/* [ceil(count/8)] bitmap of chars/CIDs used */
176 				/* (not used for Type 0 or Type 3) */
177     pdf_resource_t *res_ToUnicode; /* CMap (not used for CIDFonts) */
178     gs_cmap_t *cmap_ToUnicode;	   /* CMap (not used for CIDFonts) */
179     union {
180 
181 	struct /*type0*/ {
182 
183 	    pdf_font_resource_t *DescendantFont; /* CIDFont */
184 	    /*
185 	     * The Encoding_name must be long enough to hold either the
186 	     * longest standard CMap name defined in the PDF Reference,
187 	     * or the longest reference to an embedded CMap (# 0 R).
188 	     */
189 	    char Encoding_name[max( /* standard name or <id> 0 R */
190 		      17,	/* /UniJIS-UCS2-HW-H */
191 		      sizeof(long) * 8 / 3 + 1 + 4 /* <id> 0 R */
192 		      ) + 1	/* \0 terminator */
193 	    ];
194 	    gs_const_string CMapName; /* copied from the original CMap, */
195 				/* or references the table of standard names */
196 	    bool cmap_is_standard;
197 	    int WMode;		/* of CMap */
198 
199 	} type0;
200 
201 	struct /*cidfont*/ {
202 
203 	    /* [D]W[2] is Widths. */
204 	    long CIDSystemInfo_id; /* (written when font is allocated) */
205 	    ushort *CIDToGIDMap; /* (CIDFontType 2 only) [count] */
206  	    gs_id glyphshow_font_id;
207 	    double *Widths2;	/* [count * 2] (x, y) */
208 	    double *v;		/* [count] */
209 	    byte *used2;	/* [(count + 7) / 8] */
210 	    pdf_font_resource_t *parent;
211 
212 	} cidfont;
213 
214 	struct /*simple*/ {
215 
216 	    int FirstChar, LastChar; /* 0 <= FirstChar <= LastChar <= 255 */
217 	    /*
218 	     * The BaseEncoding can only be ENCODING_INDEX_WINANSI,
219 	     * ENCODING_INDEX_MACROMAN, ENCODING_INDEX_MACEXPERT, or -1.
220 	     */
221 	    gs_encoding_index_t BaseEncoding;
222 	    pdf_encoding_element_t *Encoding; /* [256], not for Type 3 */
223 	    gs_point *v; /* [256], glyph origin for WMode 1 */
224 
225 	    union {
226 
227 		struct /*type1*/ {
228 		    bool is_MM_instance;
229 		} type1;
230 
231 		struct /*truetype*/ {
232 		    /*
233 		     * No extra info needed, but the ANSI standard doesn't
234 		     * allow empty structs.
235 		     */
236 		    int _dummy;
237 		} truetype;
238 
239 		struct /*type3*/ {
240 		    gs_int_rect FontBBox;
241 		    gs_matrix FontMatrix;
242 		    pdf_char_proc_t *char_procs;
243 		    int max_y_offset;
244 		    bool bitmap_font;
245 		    pdf_resource_ref_t *used_resources;
246 		    int used_resources_count;
247 		    int used_resources_max;
248 		    byte *cached;
249 		} type3;
250 
251 	    } s;
252 
253 	} simple;
254 
255     } u;
256 };
257 /* The GC descriptor for resource types must be public. */
258 #define public_st_pdf_font_resource() /* gdevpdtf.c */\
259   gs_public_st_composite(st_pdf_font_resource, pdf_font_resource_t,\
260     "pdf_font_resource_t", pdf_font_resource_enum_ptrs,\
261     pdf_font_resource_reloc_ptrs)
262 
263 /*
264  * Define the possible embedding statuses of a font.
265  */
266 typedef enum {
267     FONT_EMBED_STANDARD,	/* 14 standard fonts */
268     FONT_EMBED_NO,
269     FONT_EMBED_YES
270 } pdf_font_embed_t;
271 
272 /* ---------------- Global structures ---------------- */
273 
274 /*
275  * Define a structure for keeping track of the (unique) resource for
276  * each standard font.  Note that standard fonts do not have descriptors:
277  * the base_font and copied_font members of the font resource provide the
278  * necessary information.
279  */
280 typedef struct pdf_standard_font_s {
281     pdf_font_resource_t *pdfont;
282     gs_matrix orig_matrix;	/* ****** do we need this? */
283 } pdf_standard_font_t;
284 #define private_st_pdf_standard_font() /* gdevpdtf.c */\
285   gs_private_st_ptrs1(st_pdf_standard_font, pdf_standard_font_t,\
286     "pdf_standard_font_t", pdf_std_font_enum_ptrs, pdf_std_font_reloc_ptrs,\
287     pdfont)
288 #define private_st_pdf_standard_font_element() /* gdevpdtf.c */\
289   gs_private_st_element(st_pdf_standard_font_element, pdf_standard_font_t,\
290     "pdf_standard_font_t[]", pdf_std_font_elt_enum_ptrs,\
291     pdf_std_font_elt_reloc_ptrs, st_pdf_standard_font)
292 
293 /*
294  * There is a single instance (per device) of a structure that tracks global
295  * information about outline fonts.  It is defined here, rather than
296  * opaquely in the implementation file, because the text processing code
297  * needs access to it.
298  */
299 
300 /*typedef struct pdf_outline_fonts_s pdf_outline_fonts_t;*/  /* gdevpdtx.h */
301 struct pdf_outline_fonts_s {
302     pdf_standard_font_t *standard_fonts; /* [PDF_NUM_STANDARD_FONTS] */
303 };
304 #define private_st_pdf_outline_fonts() /* gdevpdtf.c */\
305   gs_private_st_ptrs1(st_pdf_outline_fonts, pdf_outline_fonts_t,\
306     "pdf_outline_fonts_t", pdf_outline_fonts_enum_ptrs,\
307     pdf_outline_fonts_reloc_ptrs, standard_fonts)
308 
309 /* ================ Procedures ================ */
310 
311 /* ---------------- Font resources ---------------- */
312 
313 /*
314  * Allocate and initialize bookkeeping for outline fonts.
315  */
316 pdf_outline_fonts_t *pdf_outline_fonts_alloc(gs_memory_t *mem);
317 
318 /*
319  * Return the standard fonts array.
320  */
321 pdf_standard_font_t *pdf_standard_fonts(const gx_device_pdf *pdev);
322 
323 /*
324  * Clean the standard fonts array.
325  */
326 void pdf_clean_standard_fonts(const gx_device_pdf *pdev);
327 
328 /* Free font cache. */
329 int pdf_free_font_cache(gx_device_pdf *pdev);
330 
331 /*
332  * Allocate specific types of font resource.
333  */
334 int pdf_font_type0_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
335 			 gs_id rid, pdf_font_resource_t *DescendantFont,
336 			 const gs_const_string *CMapName);
337 int pdf_font_type3_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
338 			 pdf_font_write_contents_proc_t write_contents);
339 int pdf_font_std_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
340 		   bool is_original, gs_id rid, gs_font_base *pfont, int index);
341 int pdf_font_simple_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
342 			  gs_id rid, pdf_font_descriptor_t *pfd);
343 int pdf_font_cidfont_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
344 			   gs_id rid, pdf_font_descriptor_t *pfd);
345 int pdf_obtain_cidfont_widths_arrays(gx_device_pdf *pdev, pdf_font_resource_t *pdfont,
346 		    int wmode, double **w, double **w0, double **v);
347 int font_resource_encoded_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
348 			    gs_id rid, font_type ftype,
349 			    pdf_font_write_contents_proc_t write_contents);
350 
351 /* Resize font resource arrays. */
352 int pdf_resize_resource_arrays(gx_device_pdf *pdev, pdf_font_resource_t *pfres,
353 	int chars_count);
354 
355 /*
356  * Return the (copied, subset or complete) font associated with a font resource.
357  * If this font resource doesn't have a descriptor (Type 0, Type 3, or
358  * standard 14), return 0.
359  */
360 gs_font_base *pdf_font_resource_font(const pdf_font_resource_t *pdfont, bool complete);
361 
362 /*
363  * Determine the embedding status of a font.  If the font is in the base
364  * 14, store its index (0..13) in *pindex and its similarity to the base
365  * font (as determined by the font's same_font procedure) in *psame.
366  * (pindex and/or psame may be NULL.)
367  */
368 pdf_font_embed_t pdf_font_embed_status(gx_device_pdf *pdev, gs_font *font,
369 				       int *pindex,
370 				       pdf_char_glyph_pair_t *pairs, int num_glyphs);
371 
372 /*
373  * Compute the BaseFont of a font according to the algorithm described
374  * above.
375  */
376 int pdf_compute_BaseFont(gx_device_pdf *pdev, pdf_font_resource_t *pdfont, bool finish);
377 
378 /*
379  * Close the text-related parts of a document, including writing out font
380  * and related resources.
381  */
382 int pdf_close_text_document(gx_device_pdf *pdev); /* in gdevpdtw.c */
383 
384 /*
385  * Choose a name for embedded font.
386  */
387 const gs_font_name *pdf_choose_font_name(gs_font *font, bool key_name);
388 
389 /* ---------------- CMap resources ---------------- */
390 
391 /*
392  * Allocate a CMap resource.
393  */
394 int pdf_cmap_alloc(gx_device_pdf *pdev, const gs_cmap_t *pcmap,
395 		   pdf_resource_t **ppres /* CMap */, int font_index_only);
396 
397 /*
398  * Add a CID-to-GID mapping to a CIDFontType 2 font resource.
399  */
400 int pdf_font_add_cid_to_gid(pdf_font_resource_t *pdfont, uint cid, uint gid);
401 
402 #endif /* gdevpdtf_INCLUDED */
403