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