/src/ghostpdl/psi/zchar1.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2023 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., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* Type 1 character display operator */ |
18 | | #include "memory_.h" |
19 | | #include "ghost.h" |
20 | | #include "oper.h" |
21 | | #include "gsstruct.h" |
22 | | #include "gxfixed.h" |
23 | | #include "gxmatrix.h" |
24 | | #include "gxdevice.h" /* for gxfont.h */ |
25 | | #include "gxfont.h" |
26 | | #include "gxfont1.h" |
27 | | #include "gxtype1.h" |
28 | | #include "gxfcid.h" |
29 | | #include "gxchar.h" |
30 | | #include "gxfcache.h" /* for gs_purge_font_from_char_caches_completely */ |
31 | | #include "gzstate.h" /* for path for gs_type1_init */ |
32 | | /* (should only be gsstate.h) */ |
33 | | #include "gscencs.h" |
34 | | #include "gspaint.h" /* for gs_fill, gs_stroke */ |
35 | | #include "gspath.h" |
36 | | #include "gsrect.h" |
37 | | #include "estack.h" |
38 | | #include "ialloc.h" |
39 | | #include "ichar.h" |
40 | | #include "ichar1.h" |
41 | | #include "icharout.h" |
42 | | #include "idict.h" |
43 | | #include "ifont.h" |
44 | | #include "igstate.h" |
45 | | #include "iname.h" |
46 | | #include "iutil.h" |
47 | | #include "store.h" |
48 | | |
49 | | /* |
50 | | * Properly designed fonts, which have no self-intersecting outlines |
51 | | * and in which outer and inner outlines are drawn in opposite |
52 | | * directions, aren't affected by choice of filling rule; but some |
53 | | * badly designed fonts in the Genoa test suite seem to require |
54 | | * using the even-odd rule to match Adobe interpreters. |
55 | | * |
56 | | * Properly designed fonts will render correctly with: eofill |
57 | | * (required for Adobe CPSI compliant behavior |
58 | | */ |
59 | | /* |
60 | | * On April 4, 2002, we received bug report #539359 |
61 | | * which we interpret as some Genoa test are now obsolete, |
62 | | * so we need to drop the bad font tolerance feature |
63 | | * explained above. This temporary patch changes |
64 | | * the even-odd rule back to non-zero rule. |
65 | | * This patch to be kept until we accumulate |
66 | | * enough information from regression testing and |
67 | | * from user responses. |
68 | | */ |
69 | | |
70 | | /* ********************************************************************* |
71 | | * Make this dynamic via a global (somewhat better than a COMPILE option |
72 | | ***********************************************************************/ |
73 | 0 | #define GS_CHAR_FILL gs_fill |
74 | | |
75 | | /* ---------------- Utilities ---------------- */ |
76 | | |
77 | | /* Test whether a font is a CharString font. */ |
78 | | static bool |
79 | | font_uses_charstrings(const gs_font *pfont) |
80 | 0 | { |
81 | 0 | return (pfont->FontType == ft_encrypted || |
82 | 0 | pfont->FontType == ft_encrypted2 || |
83 | 0 | pfont->FontType == ft_disk_based); |
84 | 0 | } |
85 | | |
86 | | /* Initialize a Type 1 interpreter. */ |
87 | | static int |
88 | | type1_exec_init(gs_type1_state *pcis, gs_text_enum_t *penum, |
89 | | gs_gstate *pgs, gs_font_type1 *pfont1) |
90 | 0 | { |
91 | | /* |
92 | | * We have to disregard penum->pis and penum->path, and render to |
93 | | * the current gstate and path. This is a design bug that we will |
94 | | * have to address someday! |
95 | | */ |
96 | |
|
97 | 0 | int alpha_bits = 1; |
98 | 0 | gs_log2_scale_point log2_subpixels; |
99 | |
|
100 | 0 | if (color_is_pure(gs_currentdevicecolor_inline(pgs))) /* Keep consistency with alpha_buffer_bits() */ |
101 | 0 | alpha_bits = (*dev_proc(pgs->device, get_alpha_bits)) (pgs->device, go_text); |
102 | 0 | if (alpha_bits <= 1) { |
103 | | /* We render to cache device or the target device has no alpha bits. */ |
104 | 0 | log2_subpixels = penum->log2_scale; |
105 | 0 | } else { |
106 | | /* We'll render to target device through alpha buffer. */ |
107 | | /* Keep consistency with alpha_buffer_init() */ |
108 | 0 | log2_subpixels.x = log2_subpixels.y = ilog2(alpha_bits); |
109 | 0 | } |
110 | 0 | return gs_type1_interp_init(pcis, pgs, pgs->path, |
111 | 0 | &penum->log2_scale, &log2_subpixels, |
112 | 0 | (penum->text.operation & TEXT_DO_ANY_CHARPATH) != 0 || |
113 | 0 | penum->device_disabled_grid_fitting, |
114 | 0 | pfont1->PaintType, pfont1); |
115 | 0 | } |
116 | | |
117 | | /* ---------------- .type1execchar ---------------- */ |
118 | | |
119 | | /* |
120 | | * This is the workhorse for %Type1/2BuildChar, %Type1/2BuildGlyph, |
121 | | * CCRun, and CID fonts. Eventually this will appear in the C API; |
122 | | * even now, its normal control path doesn't use any continuations. |
123 | | */ |
124 | | |
125 | | /* |
126 | | * Define the state record for this operator, which must save the metrics |
127 | | * separately as well as the Type 1 interpreter state. |
128 | | */ |
129 | | typedef struct gs_type1exec_state_s { |
130 | | gs_type1_state cis; /* must be first */ |
131 | | i_ctx_t *i_ctx_p; /* so push/pop can access o-stack */ |
132 | | double sbw[4]; |
133 | | int /*metrics_present */ present; |
134 | | gs_rect char_bbox; |
135 | | bool use_FontBBox_as_Metrics2; |
136 | | /* |
137 | | * The following elements are only used locally to make the stack clean |
138 | | * for OtherSubrs: they don't need to be declared for the garbage |
139 | | * collector. |
140 | | */ |
141 | | ref save_args[6]; |
142 | | int num_args; |
143 | | bool AlignToPixels; |
144 | | } gs_type1exec_state; |
145 | | |
146 | | gs_private_st_suffix_add1(st_gs_type1exec_state, gs_type1exec_state, |
147 | | "gs_type1exec_state", gs_type1exec_state_enum_ptrs, |
148 | | gs_type1exec_state_reloc_ptrs, st_gs_type1_state, |
149 | | i_ctx_p); |
150 | | |
151 | | /* Forward references */ |
152 | | static int bbox_continue(i_ctx_t *); |
153 | | static int nobbox_continue(i_ctx_t *); |
154 | | static int type1_push_OtherSubr(i_ctx_t *, const gs_type1exec_state *, |
155 | | int (*)(i_ctx_t *), const ref *); |
156 | | static int type1_call_OtherSubr(i_ctx_t *, const gs_type1exec_state *, |
157 | | int (*)(i_ctx_t *), const ref *); |
158 | | static int type1_callout_dispatch(i_ctx_t *, int (*)(i_ctx_t *), int); |
159 | | static int type1_continue_dispatch(i_ctx_t *, gs_type1exec_state *, |
160 | | const ref *, ref *, int); |
161 | | static int op_type1_cleanup(i_ctx_t *); |
162 | | static void op_type1_free(i_ctx_t *); |
163 | | static int bbox_getsbw_continue(i_ctx_t *); |
164 | | static int type1exec_bbox(i_ctx_t *, gs_text_enum_t *, gs_type1exec_state *, gs_font *, op_proc_t *exec_cont); |
165 | | static int bbox_finish_fill(i_ctx_t *); |
166 | | static int bbox_finish_stroke(i_ctx_t *); |
167 | | static int bbox_fill(i_ctx_t *); |
168 | | static int bbox_stroke(i_ctx_t *); |
169 | | static int nobbox_finish(i_ctx_t *, gs_type1exec_state *); |
170 | | static int nobbox_draw(i_ctx_t *, int (*)(gs_gstate *)); |
171 | | static int nobbox_fill(i_ctx_t *); |
172 | | static int nobbox_stroke(i_ctx_t *); |
173 | | |
174 | | /* <font> <code|name> <name> <charstring> .type1execchar - */ |
175 | | static int |
176 | | ztype1execchar(i_ctx_t *i_ctx_p) |
177 | 0 | { |
178 | 0 | return charstring_execchar(i_ctx_p, (1 << (int)ft_encrypted) | |
179 | 0 | (1 << (int)ft_disk_based)); |
180 | 0 | } |
181 | | static int |
182 | | charstring_execchar_aux(i_ctx_t *i_ctx_p, gs_text_enum_t *penum, gs_font *pfont) |
183 | 0 | { |
184 | 0 | os_ptr op = osp; |
185 | 0 | gs_font_base *const pbfont = (gs_font_base *) pfont; |
186 | 0 | gs_font_type1 *const pfont1 = (gs_font_type1 *) pfont; |
187 | 0 | const gs_type1_data *pdata; |
188 | 0 | gs_type1exec_state cxs; |
189 | 0 | gs_type1_state *const pcis = &cxs.cis; |
190 | 0 | gs_rect FontBBox = pfont1->FontBBox; |
191 | 0 | int code; |
192 | |
|
193 | 0 | if (penum->current_font->FontType == ft_CID_encrypted) { |
194 | 0 | if (FontBBox.q.x <= FontBBox.p.x && FontBBox.q.y <= FontBBox.p.y) { |
195 | 0 | gs_font_cid0 *pfcid0 = (gs_font_cid0 *)penum->current_font; |
196 | |
|
197 | 0 | FontBBox = pfcid0->FontBBox; |
198 | 0 | } |
199 | 0 | } |
200 | |
|
201 | 0 | pdata = &pfont1->data; |
202 | | /* |
203 | | * Any reasonable implementation would execute something like |
204 | | * 1 setmiterlimit 0 setlinejoin 0 setlinecap |
205 | | * here, but the Adobe implementations don't. |
206 | | * |
207 | | * If this is a stroked font, set the stroke width. |
208 | | */ |
209 | 0 | if (pfont->PaintType) |
210 | 0 | gs_setlinewidth(igs, pfont->StrokeWidth); |
211 | 0 | check_estack(3); /* for continuations */ |
212 | | /* |
213 | | * Execute the definition of the character. |
214 | | */ |
215 | 0 | if (r_is_proc(op)) |
216 | 0 | return zchar_exec_char_proc(i_ctx_p); |
217 | | /* |
218 | | * The definition must be a Type 1 CharString. |
219 | | * Note that we do not require read access: this is deliberate. |
220 | | */ |
221 | 0 | check_type(*op, t_string); |
222 | 0 | if (r_size(op) <= max(pdata->lenIV, 0)) |
223 | 0 | return_error(gs_error_invalidfont); |
224 | | /* |
225 | | * In order to make character oversampling work, we must |
226 | | * set up the cache before calling .type1addpath. |
227 | | * To do this, we must get the bounding box from the FontBBox, |
228 | | * and the width from the CharString or the Metrics. |
229 | | * If the FontBBox isn't valid, we can't do any of this. |
230 | | */ |
231 | | |
232 | 0 | if ((penum->FontBBox_as_Metrics2.x == 0 && |
233 | 0 | penum->FontBBox_as_Metrics2.y == 0) || |
234 | 0 | gs_rootfont(igs)->WMode == 0 ) { |
235 | 0 | code = zchar_get_metrics(pbfont, op - 1, cxs.sbw); |
236 | 0 | if (code < 0) |
237 | 0 | return code; |
238 | 0 | cxs.present = code; |
239 | 0 | cxs.use_FontBBox_as_Metrics2 = false; |
240 | 0 | } else { /* pass here if FontType==9,11 && WMode==1*/ |
241 | 0 | cxs.sbw[0] = penum->FontBBox_as_Metrics2.x / 2; |
242 | 0 | cxs.sbw[1] = penum->FontBBox_as_Metrics2.y; |
243 | 0 | cxs.sbw[2] = 0; |
244 | 0 | cxs.sbw[3] = -penum->FontBBox_as_Metrics2.x; /* Sic! */ |
245 | 0 | cxs.use_FontBBox_as_Metrics2 = true; |
246 | 0 | cxs.present = metricsNone; |
247 | 0 | } |
248 | | /* Establish a current point. */ |
249 | 0 | code = gs_moveto(igs, 0.0, 0.0); |
250 | 0 | if (code < 0) |
251 | 0 | return code; |
252 | 0 | code = type1_exec_init(pcis, penum, igs, pfont1); |
253 | 0 | if (code < 0) |
254 | 0 | return code; |
255 | 0 | gs_type1_set_callback_data(pcis, &cxs); |
256 | 0 | if (FontBBox.q.x > FontBBox.p.x && |
257 | 0 | FontBBox.q.y > FontBBox.p.y |
258 | 0 | ) { |
259 | | /* The FontBBox appears to be valid. */ |
260 | 0 | op_proc_t exec_cont = 0; |
261 | |
|
262 | 0 | cxs.char_bbox = pfont1->FontBBox; |
263 | 0 | code = type1exec_bbox(i_ctx_p, penum, &cxs, pfont, &exec_cont); |
264 | 0 | if (code >= 0 && exec_cont != 0) |
265 | 0 | code = (*exec_cont)(i_ctx_p); |
266 | 0 | return code; |
267 | 0 | } else { |
268 | | /* The FontBBox is not valid */ |
269 | 0 | const ref *opstr = op; |
270 | 0 | ref other_subr; |
271 | 0 | const gs_matrix * pctm = &ctm_only(igs); |
272 | | |
273 | | /* First, check for singular CTM */ |
274 | 0 | if (pctm->xx * pctm->yy == pctm->xy * pctm->yx) { |
275 | | /* The code below won't be able to find the FontBBox but we |
276 | | * don't need it anyway. Set an empty box and consider it valid. |
277 | | */ |
278 | 0 | op_proc_t exec_cont = 0; |
279 | |
|
280 | 0 | cxs.char_bbox.p.x = 0; |
281 | 0 | cxs.char_bbox.p.y = 0; |
282 | 0 | cxs.char_bbox.q.x = 0; |
283 | 0 | cxs.char_bbox.q.y = 0; |
284 | 0 | code = type1exec_bbox(i_ctx_p, penum, &cxs, pfont, &exec_cont); |
285 | 0 | if (code >= 0 && exec_cont != 0) |
286 | 0 | code = (*exec_cont)(i_ctx_p); |
287 | 0 | return code; |
288 | 0 | } |
289 | | /* Now we create the path first, then do the setcachedevice. |
290 | | * If we are oversampling (in this case, only for anti- |
291 | | * aliasing, not just to improve quality), we have to |
292 | | * create the path twice, since we can't know the |
293 | | * oversampling factor until after setcachedevice. |
294 | | */ |
295 | 0 | switch (cxs.present) { |
296 | 0 | case metricsSideBearingAndWidth: { |
297 | 0 | gs_point pt; |
298 | |
|
299 | 0 | pt.x = cxs.sbw[0], pt.y = cxs.sbw[1]; |
300 | 0 | gs_type1_set_lsb(pcis, &pt); |
301 | 0 | } |
302 | | /* fall through */ |
303 | 0 | case metricsWidthOnly: { |
304 | 0 | gs_point pt; |
305 | |
|
306 | 0 | pt.x = cxs.sbw[2], pt.y = cxs.sbw[3]; |
307 | 0 | gs_type1_set_width(pcis, &pt); |
308 | 0 | } |
309 | 0 | } |
310 | | |
311 | | /* Continue interpreting. */ |
312 | 0 | icont: |
313 | 0 | code = type1_continue_dispatch(i_ctx_p, &cxs, opstr, &other_subr, 4); |
314 | 0 | op = osp; /* OtherSubrs might change it */ |
315 | 0 | switch (code) { |
316 | 0 | case 0: /* all done */ |
317 | 0 | return nobbox_finish(i_ctx_p, &cxs); |
318 | 0 | default: /* code < 0, error */ |
319 | 0 | return code; |
320 | 0 | case type1_result_callothersubr: /* unknown OtherSubr */ |
321 | 0 | return type1_call_OtherSubr(i_ctx_p, &cxs, nobbox_continue, |
322 | 0 | &other_subr); |
323 | 0 | case type1_result_sbw: /* [h]sbw, just continue */ |
324 | 0 | switch (cxs.present) { |
325 | 0 | case metricsNone: |
326 | 0 | cxs.sbw[0] = fixed2float(pcis->lsb.x); |
327 | 0 | cxs.sbw[1] = fixed2float(pcis->lsb.y); |
328 | | /* fall through */ |
329 | 0 | case metricsWidthOnly: |
330 | 0 | cxs.sbw[2] = fixed2float(pcis->width.x); |
331 | 0 | cxs.sbw[3] = fixed2float(pcis->width.y); |
332 | 0 | } |
333 | 0 | opstr = 0; |
334 | 0 | goto icont; |
335 | 0 | } |
336 | 0 | } |
337 | 0 | } |
338 | | |
339 | | int |
340 | | charstring_execchar(i_ctx_t *i_ctx_p, int font_type_mask) |
341 | 0 | { |
342 | 0 | gs_text_enum_t *penum = op_show_find(i_ctx_p); |
343 | 0 | gs_font *pfont; |
344 | 0 | os_ptr op = osp; |
345 | 0 | int code = font_param(op - 3, &pfont); |
346 | |
|
347 | 0 | if (code < 0) |
348 | 0 | return code; |
349 | 0 | if (penum == 0 || |
350 | 0 | pfont->FontType >= sizeof(font_type_mask) * 8 || |
351 | 0 | !(font_type_mask & (1 << (int)pfont->FontType))) |
352 | 0 | return_error(gs_error_undefined); |
353 | 0 | code = charstring_execchar_aux(i_ctx_p, penum, pfont); |
354 | 0 | if (code < 0 && igs->in_cachedevice == CACHE_DEVICE_CACHING) { |
355 | | /* Perform the cache cleanup, when the cached character data |
356 | | has been allocated (gx_alloc_char_bits) but |
357 | | the character has not been added to the cache (gx_add_cached_char) |
358 | | due to a falure in the character renderer. |
359 | | */ |
360 | 0 | gs_show_enum *const penum_s = (gs_show_enum *)penum; |
361 | |
|
362 | 0 | if (penum_s->cc != NULL) { |
363 | 0 | gx_free_cached_char(pfont->dir, penum_s->cc); |
364 | 0 | penum_s->cc = NULL; |
365 | 0 | } |
366 | 0 | } |
367 | 0 | return code; |
368 | 0 | } |
369 | | |
370 | | /* -------- bbox case -------- */ |
371 | | |
372 | | /* Do all the work for the case where we have a bounding box. */ |
373 | | /* Returns exec_cont - a function, which must be called by caller after this function. */ |
374 | | static int |
375 | | type1exec_bbox(i_ctx_t *i_ctx_p, gs_text_enum_t *penum, gs_type1exec_state * pcxs, |
376 | | gs_font * pfont, op_proc_t *exec_cont) |
377 | 0 | { |
378 | 0 | os_ptr op = osp; |
379 | 0 | gs_type1_state *const pcis = &pcxs->cis; |
380 | 0 | gs_font_base *const pbfont = (gs_font_base *) pfont; |
381 | 0 | op_proc_t cont = (pbfont->PaintType == 0 && penum->orig_font->PaintType == 0 |
382 | 0 | ? bbox_finish_fill : bbox_finish_stroke); |
383 | 0 | ref *pcdevproc; |
384 | | |
385 | | /* |
386 | | * We appear to have a valid bounding box. If we don't have Metrics for |
387 | | * this character, start interpreting the CharString; do the |
388 | | * setcachedevice as soon as we know the (side bearing and) width. |
389 | | */ |
390 | 0 | if ((pcxs->present == metricsNone && !pcxs->use_FontBBox_as_Metrics2) || |
391 | 0 | (penum->orig_font->WMode && zchar_get_CDevProc(pbfont, &pcdevproc))) { |
392 | | /* Get the width from the CharString, |
393 | | * then set the cache device. */ |
394 | | /* We pass here when WMode==1 and the font has CDevProc, |
395 | | * because we do need sbw as CDevProc's argument. |
396 | | * A more natural way would be not setting pcxs->use_FontBBox_as_Metrics2 |
397 | | * when the font has CDevProc, except for missing sbw in the glyph. |
398 | | * We prefer to pass here because we've got examples |
399 | | * of Tyoe 1 fonts with empty glyphs, i.e. with no sbw, |
400 | | * so we don't want to assume that they'll never appear in a CID font. |
401 | | * In that case penum->FontBBox_as_Metrics2 will go here to zchar_set_cache. */ |
402 | 0 | ref cnref; |
403 | 0 | ref other_subr; |
404 | 0 | int code; |
405 | | |
406 | | /* Since an OtherSubr callout might change osp, */ |
407 | | /* save the character name now. */ |
408 | 0 | ref_assign(&cnref, op - 1); |
409 | 0 | code = type1_continue_dispatch(i_ctx_p, pcxs, op, &other_subr, 4); |
410 | 0 | op = osp; /* OtherSubrs might change it */ |
411 | 0 | switch (code) { |
412 | 0 | default: /* code < 0 or done, error */ |
413 | 0 | return ((code < 0 ? code : |
414 | 0 | gs_note_error(gs_error_invalidfont))); |
415 | 0 | case type1_result_callothersubr: /* unknown OtherSubr */ |
416 | 0 | return type1_call_OtherSubr(i_ctx_p, pcxs, |
417 | 0 | bbox_getsbw_continue, |
418 | 0 | &other_subr); |
419 | 0 | case type1_result_sbw: /* [h]sbw, done */ |
420 | 0 | break; |
421 | 0 | } |
422 | 0 | type1_cis_get_metrics(pcis, pcxs->sbw); |
423 | 0 | return zchar_set_cache(i_ctx_p, pbfont, &cnref, |
424 | 0 | NULL, pcxs->sbw + 2, |
425 | 0 | &pcxs->char_bbox, |
426 | 0 | cont, exec_cont, NULL); |
427 | 0 | } else { |
428 | | /* We have the width and bounding box: */ |
429 | | /* set up the cache device now. */ |
430 | 0 | return zchar_set_cache(i_ctx_p, pbfont, op - 1, |
431 | 0 | (pcxs->present == metricsSideBearingAndWidth |
432 | 0 | && !pcxs->use_FontBBox_as_Metrics2 ? |
433 | 0 | pcxs->sbw : NULL), |
434 | 0 | pcxs->sbw + 2, |
435 | 0 | &pcxs->char_bbox, |
436 | 0 | cont, exec_cont, |
437 | 0 | (pcxs->use_FontBBox_as_Metrics2 ? pcxs->sbw : NULL)); |
438 | 0 | } |
439 | 0 | } |
440 | | |
441 | | /* Continue from an OtherSubr callout while getting metrics. */ |
442 | | static int |
443 | | bbox_getsbw_continue(i_ctx_t *i_ctx_p) |
444 | 0 | { |
445 | 0 | os_ptr op = osp; |
446 | 0 | ref other_subr; |
447 | 0 | gs_type1exec_state *pcxs = r_ptr(esp, gs_type1exec_state); |
448 | 0 | gs_type1_state *const pcis = &pcxs->cis; |
449 | 0 | int code; |
450 | |
|
451 | 0 | code = type1_continue_dispatch(i_ctx_p, pcxs, NULL, &other_subr, 4); |
452 | 0 | op = osp; /* in case z1_push/pop_proc was called */ |
453 | 0 | switch (code) { |
454 | 0 | default: /* code < 0 or done, error */ |
455 | 0 | op_type1_free(i_ctx_p); |
456 | 0 | return ((code < 0 ? code : gs_note_error(gs_error_invalidfont))); |
457 | 0 | case type1_result_callothersubr: /* unknown OtherSubr */ |
458 | 0 | return type1_push_OtherSubr(i_ctx_p, pcxs, bbox_getsbw_continue, |
459 | 0 | &other_subr); |
460 | 0 | case type1_result_sbw: { /* [h]sbw, done */ |
461 | 0 | double sbw[4]; |
462 | 0 | const gs_font_base *const pbfont = |
463 | 0 | (const gs_font_base *)pcis->pfont; |
464 | 0 | gs_rect bbox; |
465 | 0 | op_proc_t cont = (pbfont->PaintType == 0 ? bbox_finish_fill : bbox_finish_stroke), exec_cont = 0; |
466 | | |
467 | | /* Get the metrics before freeing the state. */ |
468 | 0 | type1_cis_get_metrics(pcis, sbw); |
469 | 0 | bbox = pcxs->char_bbox; |
470 | 0 | op_type1_free(i_ctx_p); |
471 | 0 | code = zchar_set_cache(i_ctx_p, pbfont, op - 1, sbw, sbw + 2, &bbox, |
472 | 0 | cont, &exec_cont, NULL); |
473 | 0 | if (code >= 0 && exec_cont != 0) |
474 | 0 | code = (*exec_cont)(i_ctx_p); |
475 | 0 | return code; |
476 | 0 | } |
477 | 0 | } |
478 | 0 | } |
479 | | |
480 | | /* <font> <code|name> <name> <charstring> <sbx> <sby> %bbox_{fill|stroke} - */ |
481 | | /* <font> <code|name> <name> <charstring> %bbox_{fill|stroke} - */ |
482 | | static int bbox_finish(i_ctx_t *i_ctx_p, op_proc_t cont, op_proc_t *exec_cont); |
483 | | static int |
484 | | bbox_finish_fill(i_ctx_t *i_ctx_p) |
485 | 0 | { |
486 | 0 | op_proc_t exec_cont = 0; |
487 | 0 | int code; |
488 | |
|
489 | 0 | code = bbox_finish(i_ctx_p, bbox_fill, &exec_cont); |
490 | 0 | if (code >= 0 && exec_cont != 0) |
491 | 0 | code = exec_cont(i_ctx_p); |
492 | 0 | return code; |
493 | 0 | } |
494 | | static int |
495 | | bbox_finish_stroke(i_ctx_t *i_ctx_p) |
496 | 0 | { |
497 | 0 | op_proc_t exec_cont = 0; |
498 | 0 | int code; |
499 | |
|
500 | 0 | code = bbox_finish(i_ctx_p, bbox_stroke, &exec_cont); |
501 | 0 | if (code >= 0 && exec_cont != 0) |
502 | 0 | code = exec_cont(i_ctx_p); |
503 | 0 | return code; |
504 | 0 | } |
505 | | |
506 | | static int |
507 | | bbox_finish(i_ctx_t *i_ctx_p, op_proc_t cont, op_proc_t *exec_cont) |
508 | 0 | { /* Returns exec_cont - a function, which must be called by caller after this function. */ |
509 | 0 | os_ptr op = osp; |
510 | 0 | gs_font *pfont; |
511 | 0 | int code; |
512 | 0 | gs_text_enum_t *penum = op_show_find(i_ctx_p); |
513 | 0 | gs_type1exec_state cxs; /* stack allocate to avoid sandbars */ |
514 | 0 | gs_type1_state *const pcis = &cxs.cis; |
515 | 0 | double sbxy[2]; |
516 | 0 | gs_point sbpt; |
517 | 0 | gs_point *psbpt = 0; |
518 | 0 | os_ptr opc = op; |
519 | 0 | const ref *opstr; |
520 | 0 | ref other_subr; |
521 | |
|
522 | 0 | if (!r_has_type(opc, t_string)) { |
523 | 0 | check_op(3); |
524 | 0 | code = num_params(op, 2, sbxy); |
525 | 0 | if (code < 0) |
526 | 0 | return code; |
527 | 0 | sbpt.x = sbxy[0]; |
528 | 0 | sbpt.y = sbxy[1]; |
529 | 0 | psbpt = &sbpt; |
530 | 0 | opc -= 2; |
531 | 0 | check_type(*opc, t_string); |
532 | 0 | } |
533 | 0 | code = font_param(opc - 3, &pfont); |
534 | 0 | if (code < 0) |
535 | 0 | return code; |
536 | 0 | if (penum == 0 || !font_uses_charstrings(pfont)) |
537 | 0 | return_error(gs_error_undefined); |
538 | 0 | { |
539 | 0 | gs_font_type1 *const pfont1 = (gs_font_type1 *) pfont; |
540 | 0 | int lenIV = pfont1->data.lenIV; |
541 | |
|
542 | 0 | if (lenIV > 0 && r_size(opc) <= lenIV) |
543 | 0 | return_error(gs_error_invalidfont); |
544 | 0 | check_estack(5); /* in case we need to do a callout */ |
545 | 0 | code = type1_exec_init(pcis, penum, igs, pfont1); |
546 | 0 | if (code < 0) |
547 | 0 | return code; |
548 | 0 | if (psbpt) |
549 | 0 | gs_type1_set_lsb(pcis, psbpt); |
550 | 0 | } |
551 | 0 | opstr = opc; |
552 | 0 | icont: |
553 | 0 | code = type1_continue_dispatch(i_ctx_p, &cxs, opstr, &other_subr, |
554 | 0 | (psbpt ? 6 : 4)); |
555 | 0 | op = osp; /* OtherSubrs might have altered it */ |
556 | 0 | switch (code) { |
557 | 0 | case 0: /* all done */ |
558 | | /* Call the continuation now. */ |
559 | 0 | if (psbpt) |
560 | 0 | ref_stack_pop(&o_stack, 2); |
561 | 0 | *exec_cont = cont; |
562 | 0 | return 0; |
563 | 0 | case type1_result_callothersubr: /* unknown OtherSubr */ |
564 | 0 | push_op_estack(cont); /* call later */ |
565 | 0 | return type1_call_OtherSubr(i_ctx_p, &cxs, bbox_continue, |
566 | 0 | &other_subr); |
567 | 0 | case type1_result_sbw: /* [h]sbw, just continue */ |
568 | 0 | opstr = 0; |
569 | 0 | goto icont; |
570 | 0 | default: /* code < 0, error */ |
571 | 0 | return code; |
572 | 0 | } |
573 | 0 | } |
574 | | |
575 | | static int |
576 | | bbox_continue(i_ctx_t *i_ctx_p) |
577 | 0 | { |
578 | 0 | os_ptr op = osp; |
579 | 0 | int npop = (r_has_type(op, t_string) ? 4 : 6); |
580 | 0 | int code = type1_callout_dispatch(i_ctx_p, bbox_continue, npop); |
581 | |
|
582 | 0 | if (code == 0) { |
583 | 0 | op = osp; /* OtherSubrs might have altered it */ |
584 | 0 | npop -= 4; /* nobbox_fill/stroke handles the rest */ |
585 | 0 | pop(npop); |
586 | 0 | op -= npop; |
587 | 0 | op_type1_free(i_ctx_p); |
588 | 0 | } |
589 | 0 | return code; |
590 | 0 | } |
591 | | |
592 | | /* |
593 | | * Check the path against FontBBox before drawing. The original operands |
594 | | * of type1execchar are still on the o-stack. |
595 | | * Returns exec_cont - a function, which must be called by caller after this function. |
596 | | */ |
597 | | static int |
598 | | bbox_draw(i_ctx_t *i_ctx_p, int (*draw)(gs_gstate *), op_proc_t *exec_cont) |
599 | 0 | { |
600 | 0 | os_ptr op = osp; |
601 | 0 | gs_rect bbox; |
602 | 0 | gs_font *pfont; |
603 | 0 | gs_text_enum_t *penum; |
604 | 0 | gs_font_base * pbfont; |
605 | 0 | gs_font_type1 * pfont1; |
606 | 0 | gs_type1exec_state cxs; |
607 | 0 | int code; |
608 | |
|
609 | 0 | if (igs->in_cachedevice < 2) /* not caching */ |
610 | 0 | return nobbox_draw(i_ctx_p, draw); |
611 | 0 | if ((code = font_param(op - 3, &pfont)) < 0) |
612 | 0 | return code; |
613 | 0 | penum = op_show_find(i_ctx_p); |
614 | 0 | if (penum == 0 || !font_uses_charstrings(pfont)) |
615 | 0 | return_error(gs_error_undefined); |
616 | 0 | if ((code = gs_pathbbox(igs, &bbox)) < 0) { |
617 | | /* |
618 | | * If the matrix is singular, all user coordinates map onto a |
619 | | * straight line. Don't bother rendering the character at all. |
620 | | */ |
621 | 0 | if (code == gs_error_undefinedresult) { |
622 | 0 | pop(4); |
623 | 0 | gs_newpath(igs); |
624 | 0 | return 0; |
625 | 0 | } |
626 | 0 | return code; |
627 | 0 | } |
628 | 0 | if (draw == gs_stroke) { |
629 | | /* Expand the bounding box by the line width. */ |
630 | 0 | float width = gs_currentlinewidth(igs) * 1.41422; |
631 | |
|
632 | 0 | bbox.p.x -= width, bbox.p.y -= width; |
633 | 0 | bbox.q.x += width, bbox.q.y += width; |
634 | 0 | } |
635 | 0 | pbfont = (gs_font_base *)pfont; |
636 | 0 | if (rect_within(bbox, pbfont->FontBBox)) /* within bounds */ |
637 | 0 | return nobbox_draw(i_ctx_p, draw); |
638 | | /* Enlarge the FontBBox to save work in the future. */ |
639 | 0 | rect_merge(pbfont->FontBBox, bbox); |
640 | | /* Dismantle everything we've done, and start over. */ |
641 | 0 | gs_text_retry(penum); |
642 | 0 | pfont1 = (gs_font_type1 *) pfont; |
643 | 0 | if ((penum->FontBBox_as_Metrics2.x == 0 && |
644 | 0 | penum->FontBBox_as_Metrics2.y == 0) || |
645 | 0 | gs_rootfont(igs)->WMode == 0 ) { |
646 | 0 | code = zchar_get_metrics(pbfont, op - 1, cxs.sbw); |
647 | 0 | if (code < 0) |
648 | 0 | return code; |
649 | 0 | cxs.present = code; |
650 | 0 | cxs.use_FontBBox_as_Metrics2 = false; |
651 | 0 | } else { |
652 | 0 | cxs.sbw[0] = penum->FontBBox_as_Metrics2.x / 2; |
653 | 0 | cxs.sbw[1] = penum->FontBBox_as_Metrics2.y; |
654 | 0 | cxs.sbw[2] = 0; |
655 | 0 | cxs.sbw[3] = -penum->FontBBox_as_Metrics2.x; /* Sic! */ |
656 | 0 | cxs.use_FontBBox_as_Metrics2 = true; |
657 | 0 | cxs.present = metricsSideBearingAndWidth; |
658 | 0 | } |
659 | 0 | code = type1_exec_init(&cxs.cis, penum, igs, pfont1); |
660 | 0 | if (code < 0) |
661 | 0 | return code; |
662 | 0 | cxs.char_bbox = pfont1->FontBBox; |
663 | 0 | code = type1exec_bbox(i_ctx_p, penum, &cxs, pfont, exec_cont); |
664 | 0 | return code; |
665 | 0 | } |
666 | | static int |
667 | | bbox_fill(i_ctx_t *i_ctx_p) |
668 | 0 | { |
669 | 0 | op_proc_t exec_cont = 0; |
670 | 0 | int code; |
671 | | |
672 | | /* See above re GS_CHAR_FILL. */ |
673 | 0 | code = bbox_draw(i_ctx_p, GS_CHAR_FILL, &exec_cont); |
674 | 0 | if (code >= 0 && exec_cont != 0) |
675 | 0 | code = (*exec_cont)(i_ctx_p); |
676 | 0 | return code; |
677 | 0 | } |
678 | | static int |
679 | | bbox_stroke(i_ctx_t *i_ctx_p) |
680 | 0 | { |
681 | 0 | op_proc_t exec_cont = 0; |
682 | 0 | int code; |
683 | |
|
684 | 0 | code = bbox_draw(i_ctx_p, gs_stroke, &exec_cont); |
685 | 0 | if (code >= 0 && exec_cont != 0) |
686 | 0 | code = (*exec_cont)(i_ctx_p); |
687 | 0 | return code; |
688 | 0 | } |
689 | | |
690 | | /* -------- Common code -------- */ |
691 | | |
692 | | /* Handle the results of interpreting the CharString. */ |
693 | | /* pcref points to a t_string ref. */ |
694 | | static int |
695 | | type1_continue_dispatch(i_ctx_t *i_ctx_p, gs_type1exec_state *pcxs, |
696 | | const ref * pcref, ref *pos, int num_args) |
697 | 0 | { |
698 | 0 | int value; |
699 | 0 | int code; |
700 | 0 | gs_glyph_data_t cs_data; |
701 | 0 | gs_glyph_data_t *pcsd; |
702 | |
|
703 | 0 | cs_data.memory = imemory; |
704 | 0 | if (pcref == 0) { |
705 | 0 | pcsd = 0; |
706 | 0 | } else { |
707 | 0 | gs_glyph_data_from_string(&cs_data, pcref->value.const_bytes, |
708 | 0 | r_size(pcref), NULL); |
709 | 0 | pcsd = &cs_data; |
710 | 0 | } |
711 | | /* |
712 | | * Since OtherSubrs may push or pop values on the PostScript operand |
713 | | * stack, remove the arguments of .type1execchar before calling the |
714 | | * Type 1 interpreter, and put them back afterwards unless we're |
715 | | * about to execute an OtherSubr procedure. Also, we must set up |
716 | | * the callback data for pushing OtherSubrs arguments. |
717 | | */ |
718 | 0 | pcxs->i_ctx_p = i_ctx_p; |
719 | 0 | pcxs->num_args = num_args; |
720 | 0 | memcpy(pcxs->save_args, osp - (num_args - 1), num_args * sizeof(ref)); |
721 | 0 | osp -= num_args; |
722 | 0 | gs_type1_set_callback_data(&pcxs->cis, pcxs); |
723 | 0 | code = pcxs->cis.pfont->data.interpret(&pcxs->cis, pcsd, &value); |
724 | 0 | switch (code) { |
725 | 0 | case type1_result_callothersubr: { |
726 | | /* |
727 | | * The Type 1 interpreter handles all known OtherSubrs, |
728 | | * so this must be an unknown one. |
729 | | */ |
730 | 0 | const font_data *pfdata = pfont_data(gs_currentfont(igs)); |
731 | |
|
732 | 0 | code = array_get(imemory, &pfdata->u.type1.OtherSubrs, (long)value, pos); |
733 | 0 | if (code >= 0) |
734 | 0 | return type1_result_callothersubr; |
735 | 0 | } |
736 | 0 | } |
737 | | /* Put back the arguments removed above. */ |
738 | 0 | memcpy(osp + 1, pcxs->save_args, num_args * sizeof(ref)); |
739 | 0 | osp += num_args; |
740 | 0 | return code; |
741 | 0 | } |
742 | | |
743 | | /* |
744 | | * Push a continuation, the arguments removed for the OtherSubr, and |
745 | | * the OtherSubr procedure. |
746 | | */ |
747 | | static int |
748 | | type1_push_OtherSubr(i_ctx_t *i_ctx_p, const gs_type1exec_state *pcxs, |
749 | | int (*cont)(i_ctx_t *), const ref *pos) |
750 | 0 | { |
751 | 0 | int i, n = pcxs->num_args; |
752 | |
|
753 | 0 | push_op_estack(cont); |
754 | | /* |
755 | | * Push the saved arguments (in reverse order, so they will get put |
756 | | * back on the operand stack in the correct order) on the e-stack. |
757 | | */ |
758 | 0 | for (i = n; --i >= 0; ) { |
759 | 0 | *++esp = pcxs->save_args[i]; |
760 | 0 | r_clear_attrs(esp, a_executable); /* just in case */ |
761 | 0 | } |
762 | 0 | ++esp; |
763 | 0 | *esp = *pos; |
764 | 0 | return o_push_estack; |
765 | 0 | } |
766 | | |
767 | | /* |
768 | | * Do a callout to an OtherSubr implemented in PostScript. |
769 | | * The caller must have done a check_estack(4 + num_args). |
770 | | */ |
771 | | static int |
772 | | type1_call_OtherSubr(i_ctx_t *i_ctx_p, const gs_type1exec_state * pcxs, |
773 | | int (*cont) (i_ctx_t *), |
774 | | const ref * pos) |
775 | 0 | { |
776 | | /* Move the Type 1 interpreter state to the heap. */ |
777 | 0 | gs_type1exec_state *hpcxs = |
778 | 0 | ialloc_struct(gs_type1exec_state, &st_gs_type1exec_state, |
779 | 0 | "type1_call_OtherSubr"); |
780 | |
|
781 | 0 | if (hpcxs == 0) |
782 | 0 | return_error(gs_error_VMerror); |
783 | 0 | *hpcxs = *pcxs; |
784 | 0 | gs_type1_set_callback_data(&hpcxs->cis, hpcxs); |
785 | 0 | push_mark_estack(es_show, op_type1_cleanup); |
786 | 0 | ++esp; |
787 | 0 | make_istruct(esp, 0, hpcxs); |
788 | 0 | return type1_push_OtherSubr(i_ctx_p, pcxs, cont, pos); |
789 | 0 | } |
790 | | |
791 | | /* Continue from an OtherSubr callout while building the path. */ |
792 | | static int |
793 | | type1_callout_dispatch(i_ctx_t *i_ctx_p, int (*cont)(i_ctx_t *), |
794 | | int num_args) |
795 | 0 | { |
796 | 0 | ref other_subr; |
797 | 0 | gs_type1exec_state *pcxs = r_ptr(esp, gs_type1exec_state); |
798 | 0 | int code; |
799 | |
|
800 | 0 | icont: |
801 | 0 | code = type1_continue_dispatch(i_ctx_p, pcxs, NULL, &other_subr, |
802 | 0 | num_args); |
803 | 0 | switch (code) { |
804 | 0 | case 0: /* callout done, cont is on e-stack */ |
805 | 0 | return 0; |
806 | 0 | default: /* code < 0 or done, error */ |
807 | 0 | op_type1_free(i_ctx_p); |
808 | 0 | return ((code < 0 ? code : gs_note_error(gs_error_invalidfont))); |
809 | 0 | case type1_result_callothersubr: /* unknown OtherSubr */ |
810 | 0 | return type1_push_OtherSubr(i_ctx_p, pcxs, cont, &other_subr); |
811 | 0 | case type1_result_sbw: /* [h]sbw, just continue */ |
812 | 0 | goto icont; |
813 | 0 | } |
814 | 0 | } |
815 | | |
816 | | /* Clean up after a Type 1 callout. */ |
817 | | static int |
818 | | op_type1_cleanup(i_ctx_t *i_ctx_p) |
819 | 0 | { |
820 | 0 | ifree_object(r_ptr(esp + 2, void), "op_type1_cleanup"); |
821 | 0 | return 0; |
822 | 0 | } |
823 | | static void |
824 | | op_type1_free(i_ctx_t *i_ctx_p) |
825 | 0 | { |
826 | 0 | ifree_object(r_ptr(esp, void), "op_type1_free"); |
827 | | /* |
828 | | * In order to avoid popping from the e-stack and then pushing onto |
829 | | * it, which would violate an interpreter invariant, we simply |
830 | | * overwrite the two e-stack items being discarded (hpcxs and the |
831 | | * cleanup operator) with empty procedures. |
832 | | */ |
833 | 0 | make_empty_const_array(esp - 1, a_readonly + a_executable); |
834 | 0 | make_empty_const_array(esp, a_readonly + a_executable); |
835 | 0 | } |
836 | | |
837 | | /* -------- no-bbox case -------- */ |
838 | | |
839 | | static int |
840 | | nobbox_continue(i_ctx_t *i_ctx_p) |
841 | 0 | { |
842 | 0 | int code = type1_callout_dispatch(i_ctx_p, nobbox_continue, 4); |
843 | |
|
844 | 0 | if (code) |
845 | 0 | return code; |
846 | 0 | { |
847 | 0 | gs_type1exec_state *pcxs = r_ptr(esp, gs_type1exec_state); |
848 | 0 | gs_type1exec_state cxs; |
849 | |
|
850 | 0 | cxs = *pcxs; |
851 | 0 | gs_type1_set_callback_data(&cxs.cis, &cxs); |
852 | 0 | op_type1_free(i_ctx_p); |
853 | 0 | return nobbox_finish(i_ctx_p, &cxs); |
854 | 0 | } |
855 | 0 | } |
856 | | |
857 | | /* Finish the no-FontBBox case after constructing the path. */ |
858 | | /* If we are oversampling for anti-aliasing, we have to go around again. */ |
859 | | /* <font> <code|name> <name> <charstring> %nobbox_continue - */ |
860 | | static int |
861 | | nobbox_finish(i_ctx_t *i_ctx_p, gs_type1exec_state * pcxs) |
862 | 0 | { |
863 | 0 | os_ptr op = osp; |
864 | 0 | int code; |
865 | 0 | gs_text_enum_t *penum = op_show_find(i_ctx_p); |
866 | 0 | gs_font *pfont; |
867 | |
|
868 | 0 | if ((code = gs_pathbbox(igs, &pcxs->char_bbox)) < 0 || |
869 | 0 | (code = font_param(op - 3, &pfont)) < 0 |
870 | 0 | ) |
871 | 0 | return code; |
872 | 0 | if (penum == 0 || !font_uses_charstrings(pfont)) |
873 | 0 | return_error(gs_error_undefined); |
874 | 0 | { |
875 | 0 | gs_font_base *const pbfont = (gs_font_base *) pfont; |
876 | 0 | gs_font_type1 *const pfont1 = (gs_font_type1 *) pfont; |
877 | 0 | op_proc_t cont, exec_cont = 0; |
878 | |
|
879 | 0 | if (pcxs->present == metricsNone) { |
880 | 0 | gs_point endpt; |
881 | |
|
882 | 0 | if ((code = gs_currentpoint(igs, &endpt)) < 0) |
883 | 0 | return code; |
884 | 0 | pcxs->sbw[2] = endpt.x, pcxs->sbw[3] = endpt.y; |
885 | 0 | pcxs->present = metricsSideBearingAndWidth; |
886 | 0 | } |
887 | | /* |
888 | | * We only need to rebuild the path from scratch if we might |
889 | | * oversample for anti-aliasing. |
890 | | */ |
891 | 0 | if ((*dev_proc(igs->device, get_alpha_bits))(igs->device, go_text) > 1 |
892 | 0 | ) { |
893 | 0 | gs_newpath(igs); |
894 | 0 | gs_moveto(igs, 0.0, 0.0); |
895 | 0 | code = type1_exec_init(&pcxs->cis, penum, igs, pfont1); |
896 | 0 | if (code < 0) |
897 | 0 | return code; |
898 | 0 | code = type1exec_bbox(i_ctx_p, penum, pcxs, pfont, &exec_cont); |
899 | 0 | } else { |
900 | 0 | cont = (pbfont->PaintType == 0 && penum->orig_font->PaintType == 0 |
901 | 0 | ? nobbox_fill : nobbox_stroke); |
902 | 0 | exec_cont = 0; |
903 | 0 | code = zchar_set_cache(i_ctx_p, pbfont, op - 1, NULL, |
904 | 0 | pcxs->sbw + 2, |
905 | 0 | &pcxs->char_bbox, |
906 | 0 | cont, &exec_cont, |
907 | 0 | (pcxs->use_FontBBox_as_Metrics2 ? pcxs->sbw : NULL)); |
908 | 0 | } |
909 | 0 | if (code >= 0 && exec_cont != 0) |
910 | 0 | code = (*exec_cont)(i_ctx_p); |
911 | 0 | return code; |
912 | 0 | } |
913 | 0 | } |
914 | | /* Finish by popping the operands and filling or stroking. */ |
915 | | static int |
916 | | nobbox_draw(i_ctx_t *i_ctx_p, int (*draw)(gs_gstate *)) |
917 | 0 | { |
918 | 0 | int code = draw(igs); |
919 | |
|
920 | 0 | if (code >= 0) |
921 | 0 | pop(4); |
922 | 0 | return code; |
923 | 0 | } |
924 | | static int |
925 | | nobbox_fill(i_ctx_t *i_ctx_p) |
926 | 0 | { |
927 | | /* See above re GS_CHAR_FILL. */ |
928 | 0 | return nobbox_draw(i_ctx_p, GS_CHAR_FILL); |
929 | 0 | } |
930 | | static int |
931 | | nobbox_stroke(i_ctx_t *i_ctx_p) |
932 | 0 | { |
933 | | /* As a compatibility to Adobe, use the exact "StrokeWidth". |
934 | | Reset fill_adjust for that. */ |
935 | 0 | int code; |
936 | 0 | gs_fixed_point fa = i_ctx_p->pgs->fill_adjust; |
937 | |
|
938 | 0 | i_ctx_p->pgs->fill_adjust.x = i_ctx_p->pgs->fill_adjust.y = 0; |
939 | 0 | code = nobbox_draw(i_ctx_p, gs_stroke); |
940 | 0 | i_ctx_p->pgs->fill_adjust = fa; |
941 | 0 | return code; |
942 | 0 | } |
943 | | |
944 | | /* <font> <array> .setweightvector - */ |
945 | | static int |
946 | | zsetweightvector(i_ctx_t *i_ctx_p) |
947 | 0 | { |
948 | 0 | os_ptr op = osp; |
949 | 0 | gs_font *pfont; |
950 | 0 | int code = font_param(op - 1, &pfont); |
951 | 0 | gs_font_type1 *pfont1; |
952 | 0 | int size; |
953 | 0 | float wv[max_WeightVector]; |
954 | |
|
955 | 0 | if (code < 0) { |
956 | | /* The font was not defined yet. Just ignore. See lib/gs_type1.ps . */ |
957 | 0 | pop(2); |
958 | 0 | return 0; |
959 | 0 | } |
960 | 0 | if (pfont->FontType != ft_encrypted && pfont->FontType != ft_encrypted2) |
961 | 0 | return_error(gs_error_invalidfont); |
962 | 0 | pfont1 = (gs_font_type1 *)pfont; |
963 | 0 | size = r_size(op); |
964 | 0 | if (size != pfont1->data.WeightVector.count) |
965 | 0 | return_error(gs_error_invalidfont); |
966 | 0 | code = process_float_array(imemory, op, size, wv); |
967 | 0 | if (code < 0) |
968 | 0 | return code; |
969 | 0 | if (memcmp(wv, pfont1->data.WeightVector.values, |
970 | 0 | sizeof(pfont1->data.WeightVector.values[0]) * size) != 0) { |
971 | 0 | memcpy(pfont1->data.WeightVector.values, wv, size); |
972 | 0 | gs_purge_font_from_char_caches_completely(pfont); |
973 | 0 | } |
974 | 0 | pop(2); |
975 | 0 | return 0; |
976 | 0 | } |
977 | | |
978 | | /* ------ Initialization procedure ------ */ |
979 | | |
980 | | const op_def zchar1_op_defs[] = |
981 | | { |
982 | | {"4.type1execchar", ztype1execchar}, |
983 | | /* Internal operators */ |
984 | | {"4%bbox_getsbw_continue", bbox_getsbw_continue}, |
985 | | {"4%bbox_continue", bbox_continue}, |
986 | | {"4%bbox_finish_fill", bbox_finish_fill}, |
987 | | {"4%bbox_finish_stroke", bbox_finish_stroke}, |
988 | | {"4%nobbox_continue", nobbox_continue}, |
989 | | {"4%nobbox_fill", nobbox_fill}, |
990 | | {"4%nobbox_stroke", nobbox_stroke}, |
991 | | {"4.setweightvector", zsetweightvector}, |
992 | | op_def_end(0) |
993 | | }; |
994 | | |
995 | | /* ------ Auxiliary procedures for type 1 fonts ------ */ |
996 | | |
997 | | static int |
998 | | z1_glyph_data(gs_font_type1 * pfont, gs_glyph glyph, gs_glyph_data_t *pgd) |
999 | 7.29M | { |
1000 | 7.29M | ref gref; |
1001 | | |
1002 | 7.29M | glyph_ref(pfont->memory, glyph, &gref); |
1003 | 7.29M | return zchar_charstring_data((gs_font *)pfont, &gref, pgd); |
1004 | 7.29M | } |
1005 | | |
1006 | | static int |
1007 | | z1_subr_data(gs_font_type1 * pfont, int index, bool global, |
1008 | | gs_glyph_data_t *pgd) |
1009 | 2.70M | { |
1010 | 2.70M | const font_data *pfdata = pfont_data(pfont); |
1011 | 2.70M | ref subr; |
1012 | 2.70M | int code; |
1013 | | |
1014 | 2.70M | code = array_get(pfont->memory, (global ? &pfdata->u.type1.GlobalSubrs : |
1015 | 2.70M | &pfdata->u.type1.Subrs), |
1016 | 2.70M | index, &subr); |
1017 | 2.70M | if (code < 0) |
1018 | 10.4k | return code; |
1019 | 2.70M | check_type_only(subr, t_string); |
1020 | 2.69M | gs_glyph_data_from_string(pgd, subr.value.const_bytes, r_size(&subr), |
1021 | 2.69M | NULL); |
1022 | 2.69M | return 0; |
1023 | 2.69M | } |
1024 | | |
1025 | | static int |
1026 | | z1_seac_data(gs_font_type1 *pfont, int ccode, gs_glyph *pglyph, |
1027 | | gs_const_string *gstr, gs_glyph_data_t *pgd) |
1028 | 0 | { |
1029 | 0 | gs_glyph glyph = gs_c_known_encode((gs_char)ccode, |
1030 | 0 | ENCODING_INDEX_STANDARD); |
1031 | 0 | int code; |
1032 | 0 | ref rglyph; |
1033 | |
|
1034 | 0 | if (glyph == GS_NO_GLYPH) |
1035 | 0 | return_error(gs_error_rangecheck); |
1036 | 0 | if ((code = gs_c_glyph_name(glyph, gstr)) < 0 || |
1037 | 0 | (code = name_ref(pfont->memory, gstr->data, gstr->size, &rglyph, 0)) < 0 |
1038 | 0 | ) |
1039 | 0 | return code; |
1040 | 0 | if (pglyph) |
1041 | 0 | *pglyph = name_index(pfont->memory, &rglyph); |
1042 | 0 | if (pgd) |
1043 | 0 | code = zchar_charstring_data((gs_font *)pfont, &rglyph, pgd); |
1044 | 0 | return code; |
1045 | 0 | } |
1046 | | |
1047 | | static int |
1048 | | z1_push(void *callback_data, const fixed * pf, int count) |
1049 | 0 | { |
1050 | 0 | gs_type1exec_state *pcxs = callback_data; |
1051 | 0 | i_ctx_t *i_ctx_p = pcxs->i_ctx_p; |
1052 | 0 | const fixed *p = pf + count - 1; |
1053 | 0 | int i; |
1054 | |
|
1055 | 0 | check_ostack(count); |
1056 | 0 | for (i = 0; i < count; i++, p--) { |
1057 | 0 | osp++; |
1058 | 0 | make_real(osp, fixed2float(*p)); |
1059 | 0 | } |
1060 | 0 | return 0; |
1061 | 0 | } |
1062 | | |
1063 | | static int |
1064 | | z1_pop(void *callback_data, fixed * pf) |
1065 | 0 | { |
1066 | 0 | gs_type1exec_state *pcxs = callback_data; |
1067 | 0 | i_ctx_t *i_ctx_p = pcxs->i_ctx_p; |
1068 | 0 | double val; |
1069 | 0 | int code = real_param(osp, &val); |
1070 | |
|
1071 | 0 | if (code < 0) |
1072 | 0 | return code; |
1073 | 0 | *pf = float2fixed(val); |
1074 | 0 | osp--; |
1075 | 0 | return 0; |
1076 | 0 | } |
1077 | | |
1078 | | /* Define the Type 1 procedure vector. */ |
1079 | | const gs_type1_data_procs_t z1_data_procs = { |
1080 | | z1_glyph_data, z1_subr_data, z1_seac_data, z1_push, z1_pop |
1081 | | }; |
1082 | | |
1083 | | /* ------ Font procedures for Type 1 fonts ------ */ |
1084 | | |
1085 | | /* |
1086 | | * Get a Type 1 or Type 2 glyph outline. This is the glyph_outline |
1087 | | * procedure for the font. |
1088 | | */ |
1089 | | int |
1090 | | zchar1_glyph_outline(gs_font *font, int WMode, gs_glyph glyph, const gs_matrix *pmat, |
1091 | | gx_path *ppath, double sbw[4]) |
1092 | 32 | { |
1093 | 32 | gs_font_type1 *const pfont1 = (gs_font_type1 *)font; |
1094 | 32 | ref gref; |
1095 | 32 | gs_glyph_data_t gdata; |
1096 | 32 | int code; |
1097 | | |
1098 | 32 | glyph_ref(font->memory, glyph, &gref); |
1099 | 32 | gdata.memory = font->memory; |
1100 | 32 | code = zchar_charstring_data(font, &gref, &gdata); |
1101 | 32 | if (code < 0) |
1102 | 0 | return code; |
1103 | 32 | return zcharstring_outline(pfont1, WMode, &gref, &gdata, pmat, ppath, sbw); |
1104 | 32 | } |
1105 | | /* |
1106 | | * Get a glyph outline given a CharString. The glyph_outline procedure |
1107 | | * for CIDFontType 0 fonts uses this. |
1108 | | */ |
1109 | | int |
1110 | | zcharstring_outline(gs_font_type1 *pfont1, int WMode, const ref *pgref, |
1111 | | const gs_glyph_data_t *pgd_orig, |
1112 | | const gs_matrix *pmat, gx_path *ppath, double sbw[4]) |
1113 | 32 | { |
1114 | 32 | const gs_glyph_data_t *pgd = pgd_orig; |
1115 | 32 | int code; |
1116 | 32 | gs_type1exec_state cxs; |
1117 | 32 | gs_type1_state *const pcis = &cxs.cis; |
1118 | 32 | const gs_type1_data *pdata; |
1119 | 32 | int value; |
1120 | 32 | gs_gstate gs; |
1121 | 32 | double wv[4]; |
1122 | 32 | gs_point mpt; |
1123 | | |
1124 | 32 | pdata = &pfont1->data; |
1125 | 32 | if (pgd->bits.size <= max(pdata->lenIV, 0)) |
1126 | 0 | return_error(gs_error_invalidfont); |
1127 | | #if 0 /* Ignore CDevProc for now. */ |
1128 | | if (zchar_get_CDevProc((const gs_font_base *)pfont1, &pcdevproc)) |
1129 | | return_error(gs_error_rangecheck); /* can't call CDevProc from here */ |
1130 | | #endif |
1131 | 32 | switch (WMode) { |
1132 | 0 | default: |
1133 | 0 | code = zchar_get_metrics2((gs_font_base *)pfont1, pgref, wv); |
1134 | 0 | if (code) { |
1135 | 0 | sbw[0] = wv[2]; |
1136 | 0 | sbw[1] = wv[3]; |
1137 | 0 | sbw[2] = wv[0]; |
1138 | 0 | sbw[3] = wv[1]; |
1139 | 0 | break; |
1140 | 0 | } |
1141 | | /* falls through */ |
1142 | 32 | case 0: |
1143 | 32 | code = zchar_get_metrics((gs_font_base *)pfont1, pgref, sbw); |
1144 | 32 | } |
1145 | 32 | if (code < 0) |
1146 | 0 | return code; |
1147 | 32 | cxs.present = code; |
1148 | | /* Initialize just enough of the imager state. */ |
1149 | 32 | if (pmat) |
1150 | 0 | gs_matrix_fixed_from_matrix(&gs.ctm, pmat); |
1151 | 32 | else { |
1152 | 32 | gs_matrix imat; |
1153 | | |
1154 | 32 | gs_make_identity(&imat); |
1155 | 32 | gs_matrix_fixed_from_matrix(&gs.ctm, &imat); |
1156 | 32 | } |
1157 | 32 | gs.flatness = 0; |
1158 | 32 | code = gs_type1_interp_init(&cxs.cis, &gs, ppath, NULL, NULL, true, 0, |
1159 | 32 | pfont1); |
1160 | 32 | if (code < 0) |
1161 | 0 | return code; |
1162 | 32 | cxs.cis.no_grid_fitting = true; |
1163 | 32 | gs_type1_set_callback_data(pcis, &cxs); |
1164 | 32 | switch (cxs.present) { |
1165 | 0 | case metricsSideBearingAndWidth: |
1166 | 0 | mpt.x = sbw[0], mpt.y = sbw[1]; |
1167 | 0 | gs_type1_set_lsb(pcis, &mpt); |
1168 | | /* falls through */ |
1169 | 0 | case metricsWidthOnly: |
1170 | 0 | mpt.x = sbw[2], mpt.y = sbw[3]; |
1171 | 0 | gs_type1_set_width(pcis, &mpt); |
1172 | 32 | case metricsNone: |
1173 | 32 | ; |
1174 | 32 | } |
1175 | | /* Continue interpreting. */ |
1176 | 64 | icont: |
1177 | 64 | code = pfont1->data.interpret(pcis, pgd, &value); |
1178 | 64 | switch (code) { |
1179 | 32 | case 0: /* all done */ |
1180 | | /* falls through */ |
1181 | 32 | default: /* code < 0, error */ |
1182 | 32 | return code; |
1183 | 0 | case type1_result_callothersubr: /* unknown OtherSubr */ |
1184 | 0 | return_error(gs_error_rangecheck); /* can't handle it */ |
1185 | 32 | case type1_result_sbw: /* [h]sbw, just continue */ |
1186 | 32 | type1_cis_get_metrics(pcis, cxs.sbw); |
1187 | 32 | type1_cis_get_metrics(pcis, sbw); |
1188 | 32 | pgd = 0; |
1189 | 32 | goto icont; |
1190 | 64 | } |
1191 | 64 | } |
1192 | | |
1193 | | /* |
1194 | | * Redefine glyph_info to take Metrics[2] and CDevProc into account (unless |
1195 | | * GLYPH_INFO_OUTLINE_WIDTHS is set). If CDevProc is present, return |
1196 | | * gs_error_rangecheck, since we can't call the interpreter from here. |
1197 | | */ |
1198 | | int |
1199 | | z1_glyph_info_generic(gs_font *font, gs_glyph glyph, const gs_matrix *pmat, |
1200 | | int members, gs_glyph_info_t *info, font_proc_glyph_info((*proc)), int wmode) |
1201 | 5.96M | { |
1202 | 5.96M | ref gref; |
1203 | 5.96M | ref *pcdevproc; |
1204 | 5.96M | gs_font_base *const pbfont = (gs_font_base *)font; |
1205 | 5.96M | int width_members = members & (GLYPH_INFO_WIDTH0 << wmode); |
1206 | 5.96M | int outline_widths = members & GLYPH_INFO_OUTLINE_WIDTHS; |
1207 | 5.96M | bool modified_widths = false; |
1208 | 5.96M | int default_members = members & ~(width_members + outline_widths + |
1209 | 5.96M | GLYPH_INFO_VVECTOR0 + GLYPH_INFO_VVECTOR1 + |
1210 | 5.96M | GLYPH_INFO_CDEVPROC); |
1211 | 5.96M | int done_members = 0; |
1212 | 5.96M | int code; |
1213 | | |
1214 | 5.96M | if (!width_members) |
1215 | 2.51M | return (*proc)(font, glyph, pmat, members, info); |
1216 | 3.44M | if (!outline_widths && zchar_get_CDevProc(pbfont, &pcdevproc)) { |
1217 | 0 | done_members |= GLYPH_INFO_CDEVPROC; |
1218 | 0 | if (members & GLYPH_INFO_CDEVPROC) { |
1219 | 0 | info->members = done_members; |
1220 | 0 | return_error(gs_error_rangecheck); |
1221 | 0 | } else { |
1222 | | /* Ignore CDevProc. Used to compure MissingWidth.*/ |
1223 | 0 | } |
1224 | 0 | } |
1225 | 3.44M | glyph_ref(pbfont->memory, glyph, &gref); |
1226 | 3.44M | if (width_members == GLYPH_INFO_WIDTH1) { |
1227 | 0 | double wv[4]; |
1228 | 0 | code = zchar_get_metrics2(pbfont, &gref, wv); |
1229 | 0 | if (code > 0) { |
1230 | 0 | modified_widths = true; |
1231 | 0 | info->width[1].x = wv[0]; |
1232 | 0 | info->width[1].y = wv[1]; |
1233 | 0 | info->v.x = wv[2]; |
1234 | 0 | info->v.y = wv[3]; |
1235 | 0 | done_members = width_members | GLYPH_INFO_VVECTOR1; |
1236 | 0 | width_members = 0; |
1237 | 0 | } |
1238 | 0 | } |
1239 | 3.44M | if (width_members) { |
1240 | 3.44M | double sbw[4]; |
1241 | 3.44M | code = zchar_get_metrics(pbfont, &gref, sbw); |
1242 | 3.44M | if (code > 0) { |
1243 | 0 | modified_widths = true; |
1244 | 0 | info->width[wmode].x = sbw[2]; |
1245 | 0 | info->width[wmode].y = sbw[3]; |
1246 | 0 | if (code == metricsSideBearingAndWidth) { |
1247 | 0 | info->v.x = sbw[0]; |
1248 | 0 | info->v.y = sbw[1]; |
1249 | 0 | width_members |= GLYPH_INFO_VVECTOR0; |
1250 | 0 | } else { |
1251 | 0 | info->v.x = 0; |
1252 | 0 | info->v.y = 0; |
1253 | 0 | } |
1254 | 0 | done_members = width_members; |
1255 | 0 | width_members = 0; |
1256 | 0 | } |
1257 | 3.44M | } |
1258 | | |
1259 | 3.44M | if (outline_widths) { |
1260 | 48.8k | if (modified_widths || zchar_get_CDevProc(pbfont, &pcdevproc)) { |
1261 | | /* Discard the modified widths, but indicate they exist. */ |
1262 | 0 | width_members |= done_members; |
1263 | 0 | done_members = outline_widths; |
1264 | 0 | } |
1265 | 48.8k | } |
1266 | 3.44M | default_members |= width_members; |
1267 | 3.44M | if (default_members) { |
1268 | 3.44M | code = (*proc)(font, glyph, pmat, default_members, info); |
1269 | | |
1270 | 3.44M | if (code < 0) |
1271 | 0 | return code; |
1272 | 3.44M | } else |
1273 | 0 | info->members = 0; |
1274 | 3.44M | info->members |= done_members; |
1275 | 3.44M | return 0; |
1276 | 3.44M | } |
1277 | | |
1278 | | int |
1279 | | z1_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat, |
1280 | | int members, gs_glyph_info_t *info) |
1281 | 5.96M | { |
1282 | 5.96M | int wmode = font->WMode; |
1283 | | |
1284 | 5.96M | return z1_glyph_info_generic(font, glyph, pmat, members, info, |
1285 | 5.96M | &gs_type1_glyph_info, wmode); |
1286 | 5.96M | } |
1287 | | |
1288 | | /* Get a Type 1 or Type 9 character metrics and set the cache device. */ |
1289 | | int |
1290 | | z1_set_cache(i_ctx_t *i_ctx_p, gs_font_base *pbfont, ref *cnref, |
1291 | | gs_glyph glyph, op_proc_t cont, op_proc_t *exec_cont) |
1292 | 0 | { /* This function is similar to zchar42_set_cache. */ |
1293 | 0 | double sbw[4]; |
1294 | 0 | gs_glyph_info_t info; |
1295 | 0 | int wmode = gs_rootfont(igs)->WMode; |
1296 | 0 | int code; |
1297 | 0 | gs_matrix id_matrix = { identity_matrix_body }; |
1298 | |
|
1299 | 0 | code = gs_default_glyph_info((gs_font *)pbfont, glyph, &id_matrix, |
1300 | 0 | ((GLYPH_INFO_WIDTH0 | GLYPH_INFO_VVECTOR0) << wmode) | GLYPH_INFO_BBOX, |
1301 | 0 | &info); |
1302 | 0 | if (code < 0) |
1303 | 0 | return code; |
1304 | 0 | sbw[0] = info.v.x; |
1305 | 0 | sbw[1] = info.v.y; |
1306 | 0 | sbw[2] = info.width[wmode].x; |
1307 | 0 | sbw[3] = info.width[wmode].y; |
1308 | 0 | return zchar_set_cache(i_ctx_p, pbfont, cnref, NULL, |
1309 | 0 | sbw + 2, &info.bbox, |
1310 | 0 | cont, exec_cont, |
1311 | 0 | wmode ? sbw : NULL); |
1312 | 0 | } |