1 /* Copyright (C) 2001-2012 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.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
13    CA  94903, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* Font copying for high-level output */
18 
19 #ifndef gxfcopy_INCLUDED
20 #  define gxfcopy_INCLUDED
21 
22 #include "gsccode.h"
23 
24 #ifndef gs_font_DEFINED
25 #  define gs_font_DEFINED
26 typedef struct gs_font_s gs_font;
27 #endif
28 
29 #ifndef gs_matrix_DEFINED
30 #  define gs_matrix_DEFINED
31 typedef struct gs_matrix_s gs_matrix;
32 #endif
33 
34 /*
35  * Copy a font, aside from its glyphs.  Note that PostScript-specific data
36  * -- that is, data that do not appear in the C structure that is the
37  * relevant subclass of gs_font -- are NOT copied.  In particular,
38  * Metrics[2], CDevProc, and FontInfo are not copied, except for the
39  * information returned by font->procs.font_info (see the definition of
40  * gs_font_info_t in gxfont.h).
41  *
42  * Note that in some cases the font must have a definition for the "not
43  * defined" glyph, as noted below, or a rangecheck error occurs.
44  *
45  * The following FontTypes are supported:
46  *
47  *	Type 1, Type 2 - Encoding and CharStrings are not copied.  Subrs and
48  *	GlobalSubrs (for CFFs) are copied; OtherSubrs are not copied.  The
49  *	font must have a glyph named .notdef; its definition is copied.
50  *
51  *	Type 42 (TrueType) - Encoding and CharStrings are not copied.  The
52  *	TrueType glyf and loca tables are not copied, nor are the bogus
53  *	Adobe gdir, glyx, and locx "tables".  If the font has a definition
54  *	for a glyph named .notdef (in the CharStrings dictionary), the
55  *	definition is copied.
56  *
57  *	CIDFontType 0 (Type 1/2-based CIDFonts) - the glyph data are not
58  *	copied.  The Type 1/2 subfonts *are* copied, as are the Subrs (both
59  *	local and global).
60  *
61  *	CIDFontType 2 (TrueType-based CIDFonts) - the glyph data and CIDMap
62  *	are not copied.
63  *
64  * The resulting font supports querying (font_info, glyph_info, etc.) and
65  * rendering (glyph_outline, etc.), but it does not support make_font.
66  *
67  * max_reserved_glyphs limits the maount of space allocated for the glyph names
68  * and glyph outlines of type 1 or 2 fonts. -1 reserves as many as required for
69  * a full copy of the original font. This is used by pdfwrite to limit the size
70  * of a copy when creating a subset font.
71  */
72 int gs_copy_font(gs_font *font, const gs_matrix *orig_matrix,
73                     gs_memory_t *mem, gs_font **pfont_new,
74                     int max_reserved_glyphs);
75 
76 int gs_free_copied_font(gs_font *font);
77 
78 /*
79  * Copy a glyph, including any sub-glyphs.  The destination font ("copied"
80  * argument) must be a font created by gs_copy_font.  The source font
81  * ("font" argument) must have the same FontType as the destination, and in
82  * addition must be "compatible" with it as described under the individual
83  * FontTypes just below; however, gs_copy_glyph does not check
84  * compatibility.
85  *
86  * If any glyph has already been copied but does not have the same
87  * definition as the one being copied now, gs_copy_glyph returns an
88  * invalidaccess error.  If the top-level glyph has already been copied
89  * (with the same definition), gs_copy_glyph returns 1.  Otherwise,
90  * gs_copy_glyph returns 0.
91  *
92  *	Type 1, Type 2 - the destination and source must have the same
93  *	Subrs.  glyph must be a name (not an integer).  Copies the
94  *	CharString entry.  Note that the Type 1/2 'seac' operator requires
95  *	copying not only the sub-glyphs but their Encoding entries as well.
96  *
97  *	Type 42 - the destination and source must have compatible tables
98  *	other than glyf and loca.  In practice this means that the source
99  *	must be the same font that was passed to gs_copy_font.  If glyph is
100  *	an integer, it is interpreted as a GID; if glyph is a name, both
101  *	the CharString entry and the glyph data are copied.
102  *
103  *	CIDFontType 0 - the destination and source must have the same Subrs,
104  *	and the Type 1/2 subfont(s) referenced by the glyph(s) being copied
105  *	must be compatible.  glyph must be a CID.  Copies the CharString.
106  *
107  *	CIDFontType 2 - compatibility is as for Type 42.  glyph must be a
108  *	CID (integer), not a GID.  Copies the glyph data and the CIDMap
109  *	entry.
110  *
111  * Metrics[2] and CDevProc in the source font are ignored.  Currently,
112  * for CIDFontType 2 fonts with MetricsCount != 0, the metrics attached to
113  * the individual glyph outlines are also ignored (not copied).
114  */
115 int gs_copy_glyph(gs_font *font, gs_glyph glyph, gs_font *copied);
116 
117 /*
118  * Copy a glyph with additional checking options.  If options includes
119  * COPY_GLYPH_NO_OLD, then if the top-level glyph has already been copied,
120  * return an invalidaccess error rather than 1.  If options includes
121  * COPY_GLYPH_NO_NEW, then if the top-level glyph has *not* already been
122  * copied, return an undefined error rather than 0.
123  *
124  * Returns an error if a glyph is added after calling copied_order_font.
125  */
126 #define COPY_GLYPH_NO_OLD 1
127 #define COPY_GLYPH_NO_NEW 2
128 #define COPY_GLYPH_BY_INDEX 4
129 int gs_copy_glyph_options(gs_font *font, gs_glyph glyph, gs_font *copied,
130                           int options);
131 
132 int gs_copied_font_free_glyphs(gs_font *font);
133 int gs_copied_font_free_data(gs_font *font);
134 
135 /*
136  * Add an encoding entry to a copied font.  If the given encoding entry is
137  * not empty and is not the same as the new value, gs_copied_font_encode
138  * returns an invalidaccess error.
139  *
140  * The action depends on FontType as follows:
141  *
142  *	Type 1, Type 2 - glyph must be a name, not a CID (integer).  Makes
143  *	an entry in the font's Encoding.  A glyph by that name must exist
144  *	in the copied font.
145  *
146  *	Type 42 - same as Type 1.
147  *
148  *	CIDFontType 0 - gives an error.
149  *
150  *	CIDFontType 2 - gives an error.
151  */
152 int gs_copied_font_add_encoding(gs_font *copied, gs_char chr, gs_glyph glyph);
153 
154 /*
155  * Copy all the glyphs and, if relevant, Encoding entries from a font.  This
156  * is equivalent to copying the glyphs and Encoding entries individually,
157  * and returns errors under the same conditions.
158  */
159 int gs_copy_font_complete(gs_font *font, gs_font *copied);
160 
161 /*
162  * Check whether specified glyphs can be copied from another font.
163  * It means that (1) fonts have same hinting parameters and
164  * (2) font subsets for the specified glyph set don't include different
165  * outlines or metrics. Possible returned values :
166  * 0 (incompatible), 1 (compatible), < 0 (error)
167  */
168 int gs_copied_can_copy_glyphs(const gs_font *cfont, const gs_font *ofont,
169                     gs_glyph *glyphs, int num_glyphs, int glyphs_step,
170                     bool check_hinting);
171 
172 /* Extension glyphs may be added to a font to resolve
173    glyph name conflicts while conwerting a PDF Widths into Metrics.
174    This function drops them before writing out an embedded font. */
175 int copied_drop_extension_glyphs(gs_font *cfont);
176 
177 /* Order font to avoid a serialization indeterminism.
178    An indeterminizm can happen due to PS name indices
179    depend on memory allocation.
180    Must not add glyphs after calling this function.
181    After calling this function, enumerate_glyph
182    enumerates glyphs in the alphabetic order of glyph names.
183    Currently works for Type 1,2 only,
184    because other fonts type don't use PS name indices
185    when enumerate glyphs.
186 */
187 int copied_order_font(gs_font *font);
188 
189 /* Get .notdef glyph. */
190 gs_glyph copied_get_notdef(const gs_font *font);
191 
192 #endif /* gxfcopy_INCLUDED */
193