]> git.ipfire.org Git - thirdparty/cups.git/blob - pstoraster/gxtype1.h
Import cups.org releases
[thirdparty/cups.git] / pstoraster / gxtype1.h
1 /* Copyright (C) 1990, 1995, 1996, 1997, 1998 Aladdin Enterprises. All rights reserved.
2
3 This file is part of GNU Ghostscript.
4
5 GNU Ghostscript is distributed in the hope that it will be useful, but
6 WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
7 to anyone for the consequences of using it or for whether it serves any
8 particular purpose or works at all, unless he says so in writing. Refer
9 to the GNU General Public License for full details.
10
11 Everyone is granted permission to copy, modify and redistribute GNU
12 Ghostscript, but only under the conditions described in the GNU General
13 Public License. A copy of this license is supposed to have been given
14 to you along with GNU Ghostscript so you can know your rights and
15 responsibilities. It should be in a file named COPYING. Among other
16 things, the copyright notice and this notice must be preserved on all
17 copies.
18
19 Aladdin Enterprises supports the work of the GNU Project, but is not
20 affiliated with the Free Software Foundation or the GNU Project. GNU
21 Ghostscript, as distributed by Aladdin Enterprises, does not require any
22 GNU software to build or run it.
23 */
24
25 /*$Id$ */
26 /* Private Adobe Type 1 / Type 2 charstring interpreter definitions */
27
28 #ifndef gxtype1_INCLUDED
29 # define gxtype1_INCLUDED
30
31 #include "gscrypt1.h"
32 #include "gstype1.h"
33 #include "gxop1.h"
34
35 /* This file defines the structures for the state of a Type 1 / */
36 /* Type 2 charstring interpreter. */
37
38 /*
39 * Because of oversampling, one pixel in the Type 1 interpreter may
40 * correspond to several device pixels. This is also true of the hint data,
41 * since the CTM reflects the transformation to the oversampled space.
42 * To help keep the font level hints separated from the character level hints,
43 * we store the scaling factor separately with each set of hints.
44 */
45 typedef struct pixel_scale_s {
46 fixed unit; /* # of pixels per device pixel */
47 fixed half; /* unit / 2 */
48 int log2_unit; /* log2(unit / fixed_1) */
49 } pixel_scale;
50 typedef struct point_scale_s {
51 pixel_scale x, y;
52 } point_scale;
53
54 #define set_pixel_scale(pps, log2)\
55 (pps)->unit = ((pps)->half = fixed_half << ((pps)->log2_unit = log2)) << 1
56 #define scaled_rounded(v, pps)\
57 (((v) + (pps)->half) & -(pps)->unit)
58
59 /* ------ Font level hints ------ */
60
61 /* Define the standard stem width tables. */
62 /* Each table is sorted, since the StemSnap arrays are sorted. */
63 #define max_snaps (1 + max_StemSnap)
64 typedef struct {
65 int count;
66 fixed data[max_snaps];
67 } stem_snap_table;
68
69 /* Define the alignment zone structure. */
70 /* These are in device coordinates also. */
71 #define max_a_zones (max_BlueValues + max_OtherBlues)
72 typedef struct {
73 int is_top_zone;
74 fixed v0, v1; /* range for testing */
75 fixed flat; /* flat position */
76 } alignment_zone;
77
78 /* Define the structure for hints that depend only on the font and CTM, */
79 /* not on the individual character. Eventually these should be cached */
80 /* with the font/matrix pair. */
81 typedef struct font_hints_s {
82 bool axes_swapped; /* true if x & y axes interchanged */
83 /* (only set if using hints) */
84 bool x_inverted, y_inverted; /* true if axis is inverted */
85 bool use_x_hints; /* true if we should use hints */
86 /* for char space x coords (vstem) */
87 bool use_y_hints; /* true if we should use hints */
88 /* for char space y coords (hstem) */
89 point_scale scale; /* oversampling scale */
90 stem_snap_table snap_h; /* StdHW, StemSnapH */
91 stem_snap_table snap_v; /* StdVW, StemSnapV */
92 fixed blue_fuzz, blue_shift; /* alignment zone parameters */
93 /* in device pixels */
94 bool suppress_overshoot; /* (computed from BlueScale) */
95 int a_zone_count; /* # of alignment zones */
96 alignment_zone a_zones[max_a_zones]; /* the alignment zones */
97 } font_hints;
98
99 /* ------ Character level hints ------ */
100
101 /*
102 * Define the stem hint tables. Each stem hint table is kept sorted.
103 * Stem hints are in device coordinates. We have to retain replaced hints
104 * so that we can make consistent rounding choices for stem edges.
105 * This is clunky, but I don't see any other way to do it.
106 *
107 * The Type 2 charstring documentation says that the total number of hints
108 * is limited to 96, but since we store horizontal and vertical hints
109 * separately, we must set max_stems large enough to allow either one to
110 * get this big.
111 */
112 #define max_total_stem_hints 96
113 #define max_stems 96
114 typedef struct {
115 fixed v0, v1; /* coordinates (widened a little) */
116 fixed dv0, dv1; /* adjustment values */
117 ushort index; /* sequential index of hint, */
118 /* needed for implementing hintmask */
119 ushort active; /* true if hint is active (hintmask) */
120 } stem_hint;
121 typedef struct {
122 int count;
123 int current; /* cache cursor for search */
124 /*
125 * For dotsection and Type 1 Charstring hint replacement,
126 * we store active hints at the bottom of the table, and
127 * replaced hints at the top.
128 */
129 int replaced_count; /* # of replaced hints at top */
130 stem_hint data[max_stems];
131 } stem_hint_table;
132
133 /* ------ Interpreter state ------ */
134
135 /* Define the control state of the interpreter. */
136 /* This is what must be saved and restored */
137 /* when calling a CharString subroutine. */
138 typedef struct {
139 const byte *ip;
140 crypt_state dstate;
141 gs_const_string char_string; /* original CharString or Subr, */
142 /* for GC */
143 } ip_state;
144
145 /* Get the next byte from a CharString. It may or may not be encrypted. */
146 #define charstring_this(ch, state, encrypted)\
147 (encrypted ? decrypt_this(ch, state) : ch)
148 #define charstring_next(ch, state, chvar, encrypted)\
149 (encrypted ? (chvar = decrypt_this(ch, state),\
150 decrypt_skip_next(ch, state)) :\
151 (chvar = ch))
152 #define charstring_skip_next(ch, state, encrypted)\
153 (encrypted ? decrypt_skip_next(ch, state) : 0)
154
155 #ifndef gx_path_DEFINED
156 # define gx_path_DEFINED
157 typedef struct gx_path_s gx_path;
158
159 #endif
160
161 #ifndef segment_DEFINED
162 # define segment_DEFINED
163 typedef struct segment_s segment;
164
165 #endif
166
167 /* This is the full state of the Type 1 interpreter. */
168 #define ostack_size 48 /* per Type 2 documentation */
169 #define ipstack_size 10 /* per documentation */
170 struct gs_type1_state_s {
171 /* The following are set at initialization */
172 gs_font_type1 *pfont; /* font-specific data */
173 gs_imager_state *pis; /* imager state */
174 gx_path *path; /* path for appending */
175 bool charpath_flag; /* false if show, true if charpath */
176 int paint_type; /* 0/3 for fill, 1/2 for stroke */
177 fixed_coeff fc; /* cached fixed coefficients */
178 float flatness; /* flatness for character curves */
179 point_scale scale; /* oversampling scale */
180 font_hints fh; /* font-level hints */
181 gs_fixed_point origin; /* character origin */
182 /* The following are updated dynamically */
183 fixed ostack[ostack_size]; /* the Type 1 operand stack */
184 int os_count; /* # of occupied stack entries */
185 ip_state ipstack[ipstack_size + 1]; /* control stack */
186 int ips_count; /* # of occupied entries */
187 int init_done; /* -1 if not done & not needed, */
188 /* 0 if not done & needed, 1 if done */
189 bool sb_set; /* true if lsb is preset */
190 bool width_set; /* true if width is set (for */
191 /* seac components) */
192 bool have_hintmask; /* true if using a hint mask */
193 /* (Type 2 charstrings only) */
194 int num_hints; /* number of hints (Type 2 only) */
195 gs_fixed_point lsb; /* left side bearing (char coords) */
196 gs_fixed_point width; /* character width (char coords) */
197 int seac_accent; /* accent character code for seac, */
198 /* or -1 */
199 fixed save_asb; /* save seac asb */
200 gs_fixed_point save_adxy; /* save seac adx/ady */
201 fixed asb_diff; /* seac asb - accented char lsb.x, */
202 /* needed to adjust Flex endpoint */
203 gs_fixed_point adxy; /* seac accent displacement, */
204 /* needed to adjust currentpoint */
205 gs_fixed_point position; /* save unadjusted position */
206 /* when returning temporarily */
207 /* to caller */
208 int flex_path_state_flags; /* record whether path was open */
209 /* at start of Flex section */
210 #define flex_max 8
211 gs_fixed_point flex_points[flex_max]; /* points for Flex */
212 int flex_count;
213 int ignore_pops; /* # of pops to ignore (after */
214 /* a known othersubr call) */
215 /* The following are set dynamically. */
216 #define dotsection_in 0
217 #define dotsection_out (-1)
218 int dotsection_flag; /* 0 if inside dotsection, */
219 /* -1 if outside */
220 bool vstem3_set; /* true if vstem3 seen */
221 gs_fixed_point vs_offset; /* device space offset for centering */
222 /* middle stem of vstem3 */
223 int hints_initial; /* hints applied to initial point */
224 /* of subpath */
225 gs_fixed_point unmoved_start; /* original initial point of subpath */
226 segment *hint_next; /* last segment where hints have */
227 /* been applied, 0 means none of */
228 /* current subpath has been hinted */
229 int hints_pending; /* hints applied to end of hint_next */
230 gs_fixed_point unmoved_end; /* original hint_next->pt */
231 stem_hint_table hstem_hints; /* horizontal stem hints */
232 stem_hint_table vstem_hints; /* vertical stem hints */
233 fixed transient_array[32]; /* Type 2 transient array, */
234 /* will be variable-size someday */
235 };
236
237 extern_st(st_gs_type1_state);
238 #define public_st_gs_type1_state() /* in gstype1.c */\
239 gs_public_st_composite(st_gs_type1_state, gs_type1_state, "gs_type1_state",\
240 gs_type1_state_enum_ptrs, gs_type1_state_reloc_ptrs)
241
242 /* ------ Shared Type 1 / Type 2 interpreter fragments ------ */
243
244 /* Declare the array of charstring interpreters, indexed by CharstringType. */
245 extern int (*gs_charstring_interpreter[3])
246 (P3(gs_type1_state * pcis, const gs_const_string * str, int *pindex));
247
248 /* Copy the operand stack out of the saved state. */
249 #define init_cstack(cstack, csp, pcis)\
250 BEGIN\
251 if ( pcis->os_count == 0 )\
252 csp = cstack - 1;\
253 else\
254 { memcpy(cstack, pcis->ostack, pcis->os_count * sizeof(fixed));\
255 csp = &cstack[pcis->os_count - 1];\
256 }\
257 END
258
259 /* Decode and push a 1-byte number. */
260 #define decode_push_num1(csp, c)\
261 (*++csp = int2fixed(c_value_num1(c)))
262
263 /* Decode and push a 2-byte number. */
264 #define decode_push_num2(csp, c, cip, state, encrypted)\
265 BEGIN\
266 uint c2 = *cip++;\
267 int cn;\
268 \
269 cn = charstring_this(c2, state, encrypted);\
270 if ( c < c_neg2_0 )\
271 { if_debug2('1', "[1] (%d)+%d\n", c_value_pos2(c, 0), cn);\
272 *++csp = int2fixed(c_value_pos2(c, 0) + (int)cn);\
273 }\
274 else\
275 { if_debug2('1', "[1] (%d)-%d\n", c_value_neg2(c, 0), cn);\
276 *++csp = int2fixed(c_value_neg2(c, 0) - (int)cn);\
277 }\
278 charstring_skip_next(c2, state, encrypted);\
279 END
280
281 /* Decode a 4-byte number, but don't push it, because Type 1 and Type 2 */
282 /* charstrings scale it differently. */
283 #if arch_sizeof_long > 4
284 # define sign_extend_num4(lw)\
285 lw = (lw ^ 0x80000000L) - 0x80000000L
286 #else
287 # define sign_extend_num4(lw) DO_NOTHING
288 #endif
289 #define decode_num4(lw, cip, state, encrypted)\
290 BEGIN\
291 int i;\
292 uint c4;\
293 \
294 lw = 0;\
295 for ( i = 4; --i >= 0; )\
296 { charstring_next(*cip, state, c4, encrypted);\
297 lw = (lw << 8) + c4;\
298 cip++;\
299 }\
300 sign_extend_num4(lw);\
301 END
302
303 /* ------ Shared Type 1 / Type 2 charstring utilities ------ */
304
305 void gs_type1_finish_init(P2(gs_type1_state * pcis, is_ptr ps));
306
307 int gs_type1_sbw(P5(gs_type1_state * pcis, fixed sbx, fixed sby,
308 fixed wx, fixed wy));
309
310 int gs_type1_seac(P4(gs_type1_state * pcis, const fixed * cstack,
311 fixed asb_diff, ip_state * ipsp));
312
313 int gs_type1_endchar(P1(gs_type1_state * pcis));
314
315 /* ----- Interface between main Type 1 interpreter and hint routines ----- */
316
317 /* Font level hints */
318 void reset_font_hints(P2(font_hints *, const gs_log2_scale_point *));
319 void compute_font_hints(P4(font_hints *, const gs_matrix_fixed *,
320 const gs_log2_scale_point *,
321 const gs_type1_data *));
322
323 /* Character level hints */
324 void reset_stem_hints(P1(gs_type1_state *)), update_stem_hints(P1(gs_type1_state *)),
325 type1_replace_stem_hints(P1(gs_type1_state *)),
326 #define replace_stem_hints(pcis)\
327 (apply_path_hints(pcis, false),\
328 type1_replace_stem_hints(pcis))
329 type1_apply_path_hints(P3(gs_type1_state *, bool, gx_path *)),
330 #define apply_path_hints(pcis, closing)\
331 type1_apply_path_hints(pcis, closing, pcis->path)
332 type1_do_hstem(P4(gs_type1_state *, fixed, fixed,
333 const gs_matrix_fixed *)),
334 #define type1_hstem(pcis, y, dy)\
335 type1_do_hstem(pcis, y, dy, &(pcis)->pis->ctm)
336 type1_do_vstem(P4(gs_type1_state *, fixed, fixed,
337 const gs_matrix_fixed *)),
338 #define type1_vstem(pcis, x, dx)\
339 type1_do_vstem(pcis, x, dx, &(pcis)->pis->ctm)
340 type1_do_center_vstem(P4(gs_type1_state *, fixed, fixed,
341 const gs_matrix_fixed *));
342
343 #define center_vstem(pcis, x0, dx)\
344 type1_do_center_vstem(pcis, x0, dx, &(pcis)->pis->ctm)
345
346 #endif /* gxtype1_INCLUDED */