1 /* Copyright (C) 2000 artofcode LLC.  All rights reserved.
2 
3   This program is free software; you can redistribute it and/or modify it
4   under the terms of the GNU General Public License as published by the
5   Free Software Foundation; either version 2 of the License, or (at your
6   option) any later version.
7 
8   This program is distributed in the hope that it will be useful, but
9   WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   General Public License for more details.
12 
13   You should have received a copy of the GNU General Public License along
14   with this program; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA, 02111-1307.
16 
17 */
18 
19 /*$Id: gdevpsf.h,v 1.10.2.2.2.1 2003/01/17 00:49:01 giles Exp $ */
20 /* PostScript/PDF font writing interface */
21 
22 #ifndef gdevpsf_INCLUDED
23 #  define gdevpsf_INCLUDED
24 
25 #include "gsccode.h"
26 
27 /* ---------------- Embedded font writing ---------------- */
28 
29 #ifndef gs_font_DEFINED
30 #  define gs_font_DEFINED
31 typedef struct gs_font_s gs_font;
32 #endif
33 #ifndef gs_font_base_DEFINED
34 #  define gs_font_base_DEFINED
35 typedef struct gs_font_base_s gs_font_base;
36 #endif
37 
38 /*
39  * Define the structure used for enumerating the glyphs in a font or a
40  * font subset.  This type is opaque to clients: we declare it here only
41  * so that clients can allocate it on the stack.
42  */
43 typedef struct psf_glyph_enum_s psf_glyph_enum_t;
44 struct psf_glyph_enum_s {
45     gs_font *font;
46     struct su_ {
47 	union sus_ {
48 	    const gs_glyph *list;	/* if subset given by a list */
49 	    const byte *bits;	/* if CID or TT subset given by a bitmap */
50 	} selected;
51 	uint size;
52     } subset;
53     gs_glyph_space_t glyph_space;
54     ulong index;
55     int (*enumerate_next)(P2(psf_glyph_enum_t *, gs_glyph *));
56 };
57 
58 /*
59  * Begin enumerating the glyphs in a font or a font subset.  If subset_size
60  * > 0 but subset_glyphs == 0, enumerate all glyphs in [0 .. subset_size-1]
61  * (as integer glyphs, i.e., offset by gs_min_cid_glyph).
62  */
63 void psf_enumerate_list_begin(P5(psf_glyph_enum_t *ppge, gs_font *font,
64 				 const gs_glyph *subset_list,
65 				 uint subset_size,
66 				 gs_glyph_space_t glyph_space));
67 /* Backward compatibility */
68 #define psf_enumerate_glyphs_begin psf_enumerate_list_begin
69 
70 /* Begin enumerating CID or TT glyphs in a subset given by a bit vector. */
71 /* Note that subset_size is given in bits, not in bytes. */
72 void psf_enumerate_bits_begin(P5(psf_glyph_enum_t *ppge, gs_font *font,
73 				 const byte *subset_bits, uint subset_size,
74 				 gs_glyph_space_t glyph_space));
75 /* Backward compatibility */
76 #define psf_enumerate_cids_begin(ppge, font, bits, size)\
77    psf_enumerate_bits_begin(ppge, font, bits, size, GLYPH_SPACE_NAME)
78 
79 /*
80  * Reset a glyph enumeration.
81  */
82 void psf_enumerate_glyphs_reset(P1(psf_glyph_enum_t *ppge));
83 
84 /*
85  * Enumerate the next glyph in a font or a font subset.
86  * Return 0 if more glyphs, 1 if done, <0 if error.
87  */
88 int psf_enumerate_glyphs_next(P2(psf_glyph_enum_t *ppge, gs_glyph *pglyph));
89 
90 /*
91  * Get the set of referenced glyphs (indices) for writing a subset font.
92  * Does not sort or remove duplicates.
93  */
94 int psf_subset_glyphs(P3(gs_glyph glyphs[256], gs_font *font,
95 			 const byte used[32]));
96 
97 /*
98  * Add composite glyph pieces to a list of glyphs.  Does not sort or
99  * remove duplicates.  max_pieces is the maximum number of pieces that a
100  * single glyph can have: if this value is not known, the caller should
101  * use max_count.
102  */
103 int psf_add_subset_pieces(P5(gs_glyph *glyphs, uint *pcount, uint max_count,
104 			     uint max_pieces, gs_font *font));
105 
106 /*
107  * Sort a list of glyphs and remove duplicates.  Return the number of glyphs
108  * in the result.
109  */
110 int psf_sort_glyphs(P2(gs_glyph *glyphs, int count));
111 
112 /*
113  * Return the index of a given glyph in a sorted list of glyphs, or -1
114  * if the glyph is not present.
115  */
116 int psf_sorted_glyphs_index_of(P3(const gs_glyph *glyphs, int count,
117 				  gs_glyph glyph));
118 /*
119  * Determine whether a sorted list of glyphs includes a given glyph.
120  */
121 bool psf_sorted_glyphs_include(P3(const gs_glyph *glyphs, int count,
122 				  gs_glyph glyph));
123 
124 /*
125  * Define the internal structure that holds glyph information for an
126  * outline-based font to be written.  Currently this only applies to
127  * Type 1, Type 2, and CIDFontType 0 fonts, but someday it might also
128  * be usable with TrueType (Type 42) and CIDFontType 2 fonts.
129  */
130 typedef struct psf_outline_glyphs_s {
131     gs_glyph notdef;
132     gs_glyph subset_data[256 * 3 + 1]; /* *3 for seac, +1 for .notdef */
133     gs_glyph *subset_glyphs;	/* 0 or subset_data */
134     uint subset_size;
135 } psf_outline_glyphs_t;
136 
137 #ifndef gs_font_type1_DEFINED
138 #  define gs_font_type1_DEFINED
139 typedef struct gs_font_type1_s gs_font_type1;
140 #endif
141 
142 /* Define the type for the glyph data callback procedure. */
143 typedef int (*glyph_data_proc_t)(P4(gs_font_base *, gs_glyph,
144 				    gs_const_string *, gs_font_type1 **));
145 
146 /* Check that all selected glyphs can be written. */
147 int psf_check_outline_glyphs(P3(gs_font_base *pfont,
148 				psf_glyph_enum_t *ppge,
149 				glyph_data_proc_t glyph_data));
150 
151 /*
152  * Gather glyph information for a Type 1, Type 2, or CIDFontType 0 font.
153  * Note that the glyph_data procedure returns both the outline string and
154  * a gs_font_type1 (Type 1 or Type 2) font: for Type 1 or Type 2 fonts,
155  * this is the original font, but for CIDFontType 0 fonts, it is the
156  * FDArray element.  If subset_glyphs != 0, this procedure removes
157  * undefined glyphs from the list it builds.
158  */
159 int psf_get_outline_glyphs(P5(psf_outline_glyphs_t *pglyphs,
160 			      gs_font_base *pfont, gs_glyph *subset_glyphs,
161 			      uint subset_size, glyph_data_proc_t glyph_data));
162 
163 /* ------ Exported by gdevpsf1.c ------ */
164 
165 /* Gather glyph information for a Type 1 or Type 2 font. */
166 int psf_type1_glyph_data(P4(gs_font_base *, gs_glyph, gs_const_string *,
167 			    gs_font_type1 **));
168 int psf_get_type1_glyphs(P4(psf_outline_glyphs_t *pglyphs,
169 			    gs_font_type1 *pfont,
170 			    gs_glyph *subset_glyphs, uint subset_size));
171 
172 /*
173  * Write out a Type 1 font definition.  This procedure does not allocate
174  * or free any data.
175  */
176 #define WRITE_TYPE1_EEXEC 1
177 #define WRITE_TYPE1_ASCIIHEX 2  /* use ASCII hex rather than binary */
178 #define WRITE_TYPE1_EEXEC_PAD 4  /* add 512 0s */
179 #define WRITE_TYPE1_EEXEC_MARK 8  /* assume 512 0s will be added */
180 #define WRITE_TYPE1_POSTSCRIPT 16  /* don't observe ATM restrictions */
181 #define WRITE_TYPE1_WITH_LENIV 32  /* don't allow lenIV = -1 */
182 int psf_write_type1_font(P7(stream *s, gs_font_type1 *pfont, int options,
183 			    gs_glyph *subset_glyphs, uint subset_size,
184 			    const gs_const_string *alt_font_name,
185 			    int lengths[3]));
186 
187 
188 /* ------ Exported by gdevpsf2.c ------ */
189 
190 /*
191  * Write out a Type 1 or Type 2 font definition as CFF.
192  * This procedure does not allocate or free any data.
193  */
194 #define WRITE_TYPE2_NO_LENIV 1	/* always use lenIV = -1 */
195 #define WRITE_TYPE2_CHARSTRINGS 2 /* convert T1 charstrings to T2 */
196 #define WRITE_TYPE2_AR3 4	/* work around bugs in Acrobat Reader 3 */
197 int psf_write_type2_font(P7(stream *s, gs_font_type1 *pfont, int options,
198 			    gs_glyph *subset_glyphs, uint subset_size,
199 			    const gs_const_string *alt_font_name,
200 			    gs_int_rect *FontBBox));
201 
202 #ifndef gs_font_cid0_DEFINED
203 #  define gs_font_cid0_DEFINED
204 typedef struct gs_font_cid0_s gs_font_cid0;
205 #endif
206 
207 /*
208  * Write out a CIDFontType 0 font definition as CFF.  The options are
209  * the same as for psf_write_type2_font.  subset_cids is a bit vector of
210  * subset_size bits (not bytes).
211  * This procedure does not allocate or free any data.
212  */
213 int psf_write_cid0_font(P6(stream *s, gs_font_cid0 *pfont, int options,
214 			   const byte *subset_cids, uint subset_size,
215 			   const gs_const_string *alt_font_name));
216 
217 /* ------ Exported by gdevpsfm.c ------ */
218 
219 /*
220  * Write out a CMap in its customary (source) form.
221  * This procedure does not allocate or free any data.
222  */
223 #ifndef gs_cmap_DEFINED
224 #  define gs_cmap_DEFINED
225 typedef struct gs_cmap_s gs_cmap_t;
226 #endif
227 typedef int (*psf_put_name_chars_proc_t)(P3(stream *, const byte *, uint));
228 int psf_write_cmap(P4(stream *s, const gs_cmap_t *pcmap,
229 		      psf_put_name_chars_proc_t put_name_chars,
230 		      const gs_const_string *alt_cmap_name));
231 
232 /* ------ Exported by gdevpsft.c ------ */
233 
234 /*
235  * Write out a TrueType (Type 42) font definition.
236  * This procedure does not allocate or free any data.
237  */
238 #ifndef gs_font_type42_DEFINED
239 #  define gs_font_type42_DEFINED
240 typedef struct gs_font_type42_s gs_font_type42;
241 #endif
242 #define WRITE_TRUETYPE_CMAP 1	/* generate cmap from the Encoding */
243 #define WRITE_TRUETYPE_NAME 2	/* generate name if missing */
244 #define WRITE_TRUETYPE_POST 4	/* generate post if missing */
245 #define WRITE_TRUETYPE_NO_TRIMMED_TABLE 8  /* not OK to use cmap format 6 */
246 int psf_write_truetype_font(P6(stream *s, gs_font_type42 *pfont, int options,
247 			       gs_glyph *subset_glyphs, uint subset_size,
248 			       const gs_const_string *alt_font_name));
249 
250 #ifndef gs_font_cid2_DEFINED
251 #  define gs_font_cid2_DEFINED
252 typedef struct gs_font_cid2_s gs_font_cid2;
253 #endif
254 
255 /*
256  * Write out a CIDFontType 2 font definition.  This differs from
257  * psf_write_truetype_font in that the subset, if any, is specified
258  * as a bit vector (as for psf_write_cid0_font) rather than a list of glyphs.
259  * Also, none of the options currently have any effect.  The only tables
260  * written are:
261  *	- The "required" tables: head, hhea, loca, maxp, cvt_, prep, glyf,
262  *	  hmtx, fpgm.
263  *	- If present in the font: post, gasp, kern, vhea, vmtx.
264  * Note that in particular, the cmap, name, and OS/2 tables are omitted.
265  * NOTE: it is the client's responsibility to ensure that if the subset
266  * contains any composite glyphs, the components of the composites are
267  * included explicitly in the subset.
268  * This procedure does not allocate or free any data.
269  */
270 #define WRITE_TRUETYPE_CID 0x1000 /* internal */
271 int psf_write_cid2_font(P6(stream *s, gs_font_cid2 *pfont, int options,
272 			   const byte *subset_glyphs, uint subset_size,
273 			   const gs_const_string *alt_font_name));
274 
275 /* ------ Exported by gdevpsfx.c ------ */
276 
277 /*
278  * Convert a Type 1 CharString to (unencrypted) Type 2.
279  * This procedure does not allocate or free any data.
280  * NOTE: this procedure expands all Subrs in-line.
281  */
282 int psf_convert_type1_to_type2(P3(stream *s, const gs_const_string *pstr,
283 				  gs_font_type1 *pfont));
284 
285 #endif /* gdevpsf_INCLUDED */
286