xref: /plan9/sys/src/cmd/gs/src/gdevpdtb.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: gdevpdtb.h,v 1.11 2005/04/04 08:53:07 igor Exp $ */
18 /* BaseFont structure and API for pdfwrite */
19 
20 #ifndef gdevpdtb_INCLUDED
21 #  define gdevpdtb_INCLUDED
22 
23 #include "gdevpdtx.h"
24 
25 /* ================ Types and structures ================ */
26 
27 /*
28  * A "base font" pdf_base_font_t contains a stable copy of a gs_font.  The
29  * only supported font types are Type 1/2, TrueType / Type 42, CIDFontType
30  * 0, and CIDFontType 2.
31  *
32  * At the time a base font is created, we copy the fixed elements of the
33  * gs_font (that is, everything except the data for individual glyphs) into
34  * stable memory; we then copy individual glyphs as they are used.  If
35  * subsetting is not mandatory (that is, if the entire font might be
36  * embedded or its Widths written), we also save a separate copy of the
37  * entire font, since the subsetting decision cannot be made until the font
38  * is written out (normally at the end of the document).
39  *
40  * In an earlier design, we deferred making the complete copy until the font
41  * was about to be freed.  We decided that the substantial extra complexity
42  * of this approach did not justify the space that would be saved in the
43  * usual (subsetted) case.
44  *
45  * The term "base font" is used, confusingly, for at least 3 different
46  * concepts in Ghostscript.  However, the above meaning is used consistently
47  * within the PDF text code (gdevpdt*.[ch]).
48  */
49 /*
50  * Font names in PDF files have caused an enormous amount of trouble, so we
51  * document specifically how they are handled in each structure.
52  *
53  * The PDF Reference doesn't place any constraints on the [CID]FontName of
54  * base fonts, although it does say that the BaseFont name in the font
55  * resource is "usually" derived from the [CID]FontName of the base font.
56  * The code in this module allows setting the font name.  It initializes
57  * the name to the key_name of the base font, or to the font_name if the
58  * base font has no key_name, minus any XXXXXX+ subset prefix; the
59  * pdf_do_subset_font procedure adds the XXXXXX+ prefix if the font will
60  * be subsetted.
61  */
62 
63 #ifndef pdf_base_font_DEFINED
64 #  define pdf_base_font_DEFINED
65 typedef struct pdf_base_font_s pdf_base_font_t;
66 #endif
67 
68 /* ================ Procedures ================ */
69 
70 /*
71  * Allocate and initialize a base font structure, making the required
72  * stable copy/ies of the gs_font.  Note that this removes any XXXXXX+
73  * font name prefix from the copy.  If complete is true, the copy is
74  * a complete one, and adding glyphs or Encoding entries is not allowed.
75  */
76 int pdf_base_font_alloc(gx_device_pdf *pdev, pdf_base_font_t **ppbfont,
77 		    gs_font_base *font, const gs_matrix *orig_matrix,
78 		    bool is_standard, bool orig_name);
79 
80 /*
81  * Return a reference to the name of a base font.  This name is guaranteed
82  * not to have a XXXXXX+ prefix.  The client may change the name at will,
83  * but must not add a XXXXXX+ prefix.
84  */
85 gs_string *pdf_base_font_name(pdf_base_font_t *pbfont);
86 
87 /*
88  * Return the (copied, subset or complete) font associated with a base font.
89  * This procedure probably shouldn't exist....
90  */
91 gs_font_base *pdf_base_font_font(const pdf_base_font_t *pbfont, bool complete);
92 
93 /*
94  * Check for subset font.
95  */
96 bool pdf_base_font_is_subset(const pdf_base_font_t *pbfont);
97 
98 /*
99  * Drop the copied complete font associated with a base font.
100  */
101 void pdf_base_font_drop_complete(pdf_base_font_t *pbfont);
102 
103 /*
104  * Copy a glyph (presumably one that was just used) into a saved base
105  * font.  Note that it is the client's responsibility to determine that
106  * the source font is compatible with the target font.  (Normally they
107  * will be the same.)
108  */
109 int pdf_base_font_copy_glyph(pdf_base_font_t *pbfont, gs_glyph glyph,
110 			     gs_font_base *font);
111 
112 /*
113  * Determine whether a font is a subset font by examining the name.
114  */
115 bool pdf_has_subset_prefix(const byte *str, uint size);
116 
117 /*
118  * Add the XXXXXX+ prefix for a subset font.
119  */
120 int pdf_add_subset_prefix(const gx_device_pdf *pdev, gs_string *pstr,
121 			byte *used, int count);
122 
123 /*
124  * Determine whether a copied font should be subsetted.
125  */
126 bool pdf_do_subset_font(gx_device_pdf *pdev, pdf_base_font_t *pbfont,
127 			gs_id rid);
128 
129 /*
130  * Write the FontFile entry for an embedded font, /FontFile<n> # # R.
131  */
132 int pdf_write_FontFile_entry(gx_device_pdf *pdev, pdf_base_font_t *pbfont);
133 
134 /*
135  * Write an embedded font, possibly subsetted.
136  */
137 int pdf_write_embedded_font(gx_device_pdf *pdev, pdf_base_font_t *pbfont,
138 			gs_int_rect *FontBBox, gs_id rid, cos_dict_t **ppcd);
139 
140 /*
141  * Write the CharSet data for a subsetted font, as a PDF string.
142  */
143 int pdf_write_CharSet(gx_device_pdf *pdev, pdf_base_font_t *pbfont);
144 
145 /*
146  * Write the CIDSet object for a subsetted CIDFont.
147  */
148 int pdf_write_CIDSet(gx_device_pdf *pdev, pdf_base_font_t *pbfont,
149 		     long *pcidset_id);
150 
151 /*
152  * Check whether a base font is standard.
153  */
154 bool pdf_is_standard_font(pdf_base_font_t *bfont);
155 
156 void pdf_set_FontFile_object(pdf_base_font_t *bfont, cos_dict_t *pcd);
157 const cos_dict_t * pdf_get_FontFile_object(pdf_base_font_t *bfont);
158 
159 #endif /* gdevpdtb_INCLUDED */
160