xref: /plan9/sys/src/cmd/gs/src/gxtype1.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1990, 2000, 2001 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: gxtype1.h,v 1.19 2004/09/22 13:52:33 igor Exp $ */
18 /* Private Adobe Type 1 / Type 2 charstring interpreter definitions */
19 
20 #ifndef gxtype1_INCLUDED
21 #  define gxtype1_INCLUDED
22 
23 #include "gscrypt1.h"
24 #include "gsgdata.h"
25 #include "gstype1.h"
26 #include "gxhintn.h"
27 
28 /* This file defines the structures for the state of a Type 1 / */
29 /* Type 2 charstring interpreter. */
30 
31 /*
32  * Because of oversampling, one pixel in the Type 1 interpreter may
33  * correspond to several device pixels.  This is also true of the hint data,
34  * since the CTM reflects the transformation to the oversampled space.
35  * To help keep the font level hints separated from the character level hints,
36  * we store the scaling factor separately with each set of hints.
37  */
38 typedef struct pixel_scale_s {
39     fixed unit;			/* # of pixels per device pixel */
40     fixed half;			/* unit / 2 */
41     int log2_unit;		/* log2(unit / fixed_1) */
42 } pixel_scale;
43 typedef struct point_scale_s {
44     pixel_scale x, y;
45 } point_scale;
46 
47 #define set_pixel_scale(pps, log2)\
48   (pps)->unit = ((pps)->half = fixed_half << ((pps)->log2_unit = log2)) << 1
49 #define scaled_rounded(v, pps)\
50   (((v) + (pps)->half) & -(pps)->unit)
51 
52 
53 /*
54  * The Type 2 charstring documentation says that the total number of hints
55  * is limited to 96.
56  */
57 
58 #define max_total_stem_hints 96
59 
60 /* ------ Interpreter state ------ */
61 
62 /* Define the control state of the interpreter. */
63 /* This is what must be saved and restored */
64 /* when calling a CharString subroutine. */
65 typedef struct {
66     const byte *ip;
67     crypt_state dstate;
68     gs_glyph_data_t cs_data;	/* original CharString or Subr, */
69 				/* for GC */
70 } ip_state_t;
71 
72 /* Get the next byte from a CharString.  It may or may not be encrypted. */
73 #define charstring_this(ch, state, encrypted)\
74   (encrypted ? decrypt_this(ch, state) : ch)
75 #define charstring_next(ch, state, chvar, encrypted)\
76   (encrypted ? (chvar = decrypt_this(ch, state),\
77 		decrypt_skip_next(ch, state)) :\
78    (chvar = ch))
79 #define charstring_skip_next(ch, state, encrypted)\
80   (encrypted ? decrypt_skip_next(ch, state) : 0)
81 
82 #ifndef gx_path_DEFINED
83 #  define gx_path_DEFINED
84 typedef struct gx_path_s gx_path;
85 #endif
86 
87 #ifndef segment_DEFINED
88 #  define segment_DEFINED
89 typedef struct segment_s segment;
90 #endif
91 
92 /* This is the full state of the Type 1 interpreter. */
93 #define ostack_size 48		/* per Type 2 documentation */
94 #define ipstack_size 10		/* per documentation */
95 struct gs_type1_state_s {
96     t1_hinter h;
97     /* The following are set at initialization */
98     gs_font_type1 *pfont;	/* font-specific data */
99     gs_imager_state *pis;	/* imager state */
100     gx_path *path;		/* path for appending */
101     bool no_grid_fitting;
102     int paint_type;		/* 0/3 for fill, 1/2 for stroke */
103     void *callback_data;
104     fixed_coeff fc;		/* cached fixed coefficients */
105     float flatness;		/* flatness for character curves */
106     point_scale scale;		/* oversampling scale */
107     gs_log2_scale_point log2_subpixels;	/* log2 of the number of subpixels */
108     gs_fixed_point origin;	/* character origin */
109     /* The following are updated dynamically */
110     fixed ostack[ostack_size];	/* the Type 1 operand stack */
111     int os_count;		/* # of occupied stack entries */
112     ip_state_t ipstack[ipstack_size + 1];	/* control stack */
113     int ips_count;		/* # of occupied entries */
114     int init_done;		/* -1 if not done & not needed, */
115 				/* 0 if not done & needed, 1 if done */
116     bool sb_set;		/* true if lsb is preset */
117     bool width_set;		/* true if width is set (for seac parts) */
118     /* (Type 2 charstrings only) */
119     int num_hints;		/* number of hints (Type 2 only) */
120     gs_fixed_point lsb;		/* left side bearing (char coords) */
121     gs_fixed_point width;	/* character width (char coords) */
122     int seac_accent;		/* accent character code for seac, or -1 */
123     fixed save_asb;		/* save seac asb */
124     gs_fixed_point save_lsb;	/* save seac accented lsb */
125     gs_fixed_point save_adxy;	/* save seac adx/ady */
126     fixed asb_diff;		/* save_asb - save_lsb.x, */
127 				/* needed to adjust Flex endpoint */
128     gs_fixed_point adxy;	/* seac accent displacement, */
129 				/* needed to adjust currentpoint */
130     int flex_path_state_flags;	/* record whether path was open */
131 				/* at start of Flex section */
132 #define flex_max 8
133     int flex_count;
134     int ignore_pops;		/* # of pops to ignore (after */
135 				/* a known othersubr call) */
136     /* The following are set dynamically. */
137     gs_fixed_point vs_offset;	/* device space offset for centering */
138 				/* middle stem of vstem3 */
139 				/* of subpath */
140     fixed transient_array[32];	/* Type 2 transient array, */
141     /* will be variable-size someday */
142 };
143 
144 extern_st(st_gs_type1_state);
145 #define public_st_gs_type1_state() /* in gxtype1.c */\
146   gs_public_st_composite(st_gs_type1_state, gs_type1_state, "gs_type1_state",\
147     gs_type1_state_enum_ptrs, gs_type1_state_reloc_ptrs)
148 
149 /* ------ Shared Type 1 / Type 2 interpreter fragments ------ */
150 
151 /* Define a pointer to the charstring interpreter stack. */
152 typedef fixed *cs_ptr;
153 
154 /* Clear the operand stack. */
155 /* The cast avoids compiler warning about a "negative subscript." */
156 #define CLEAR_CSTACK(cstack, csp)\
157   (csp = (cs_ptr)(cstack) - 1)
158 
159 /* Copy the operand stack out of the saved state. */
160 #define INIT_CSTACK(cstack, csp, pcis)\
161   BEGIN\
162     if ( pcis->os_count == 0 )\
163       CLEAR_CSTACK(cstack, csp);\
164     else {\
165       memcpy(cstack, pcis->ostack, pcis->os_count * sizeof(fixed));\
166       csp = &cstack[pcis->os_count - 1];\
167     }\
168   END
169 
170 #define CS_CHECK_PUSH(csp, cstack)\
171   BEGIN\
172     if (csp >= &cstack[countof(cstack)-1])\
173       return_error(gs_error_invalidfont);\
174   END
175 
176 /* Decode a 1-byte number. */
177 #define decode_num1(var, c)\
178   (var = c_value_num1(c))
179 #define decode_push_num1(csp, cstack, c)\
180   BEGIN\
181     CS_CHECK_PUSH(csp, cstack);\
182     *++csp = int2fixed(c_value_num1(c));\
183   END
184 
185 /* Decode a 2-byte number. */
186 #define decode_num2(var, c, cip, state, encrypted)\
187   BEGIN\
188     uint c2 = *cip++;\
189     int cn = charstring_this(c2, state, encrypted);\
190 \
191     var = (c < c_neg2_0 ? c_value_pos2(c, 0) + cn :\
192 	   c_value_neg2(c, 0) - cn);\
193     charstring_skip_next(c2, state, encrypted);\
194   END
195 #define decode_push_num2(csp, cstack, c, cip, state, encrypted)\
196   BEGIN\
197     uint c2 = *cip++;\
198     int cn;\
199 \
200     CS_CHECK_PUSH(csp, cstack);\
201     cn = charstring_this(c2, state, encrypted);\
202     if ( c < c_neg2_0 )\
203       { if_debug2('1', "[1] (%d)+%d\n", c_value_pos2(c, 0), cn);\
204         *++csp = int2fixed(c_value_pos2(c, 0) + (int)cn);\
205       }\
206     else\
207       { if_debug2('1', "[1] (%d)-%d\n", c_value_neg2(c, 0), cn);\
208         *++csp = int2fixed(c_value_neg2(c, 0) - (int)cn);\
209       }\
210     charstring_skip_next(c2, state, encrypted);\
211   END
212 
213 /* Decode a 4-byte number, but don't push it, because Type 1 and Type 2 */
214 /* charstrings scale it differently. */
215 #if arch_sizeof_long > 4
216 #  define sign_extend_num4(lw)\
217      lw = (lw ^ 0x80000000L) - 0x80000000L
218 #else
219 #  define sign_extend_num4(lw) DO_NOTHING
220 #endif
221 #define decode_num4(lw, cip, state, encrypted)\
222   BEGIN\
223     int i;\
224     uint c4;\
225 \
226     lw = 0;\
227     for ( i = 4; --i >= 0; )\
228       { charstring_next(*cip, state, c4, encrypted);\
229         lw = (lw << 8) + c4;\
230 	cip++;\
231       }\
232     sign_extend_num4(lw);\
233   END
234 
235 /* ------ Shared Type 1 / Type 2 charstring utilities ------ */
236 
237 void gs_type1_finish_init(gs_type1_state * pcis);
238 
239 int gs_type1_sbw(gs_type1_state * pcis, fixed sbx, fixed sby,
240 		 fixed wx, fixed wy);
241 
242 /* blend returns the number of values to pop. */
243 int gs_type1_blend(gs_type1_state *pcis, fixed *csp, int num_results);
244 
245 int gs_type1_seac(gs_type1_state * pcis, const fixed * cstack,
246 		  fixed asb_diff, ip_state_t * ipsp);
247 
248 int gs_type1_endchar(gs_type1_state * pcis);
249 
250 /* Get the metrics (l.s.b. and width) from the Type 1 interpreter. */
251 void type1_cis_get_metrics(const gs_type1_state * pcis, double psbw[4]);
252 
253 #endif /* gxtype1_INCLUDED */
254