Debian Codegraph @

texk/dvipdfm-x/cs_type2.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21 /*
22 * Type 2 Charstring support:
23 * Decode and encode Type 2 charstring
24 *
25 * All local/global subroutine calls in a given charstring is replace by the
26 * content of subroutine charstrings. We do this because some PostScript RIP
27 * may have problems with sparse subroutine array. Workaround for this is to
28 * re-order subroutine array so that no gap appears in the subroutine array,
29 * or put dummy charstrings that contains only `return' in the gap. However,
30 * re-ordering of subroutine is rather difficult for Type 2 charstrings due
31 * to the bias which depends on the total number of subroutines. Replacing
32 * callgsubr/callsubr calls with the content of the corresponding subroutine
33 * charstring may be more efficient than putting dummy subroutines in the
34 * case of subsetted font. Adobe distiller seems doing same thing.
35 *
36 * And also note that subroutine numbers within subroutines can depend on the
37 * content of operand stack as follows:
38 *
39 * ... l m callsubr << subr #(m+bias): n add callsubr >> ...
40 *
41 * I've not implemented the `random' operator which generates a pseudo-random
42 * number in the range (0, 1] and push them into argument stack.
43 * How pseudo-random sequences are generated is not documented in the Type 2
44 * charstring spec..
45 */
46
47 #ifdef HAVE_CONFIG_H
48 #include <config.h>
49 #endif
50
51 #include <stdio.h>
52
53 /* fabs, sqrt ... */
54 #include <math.h>
55
56 #include "error.h"
57
58 /* data types, limits */
59 #include "cff_types.h"
60 #include "cff_limits.h"
61
62 #include "cs_type2.h"
63
64 #define CS_TYPE2_DEBUG_STR "Type2 Charstring Parser"
65 #define CS_TYPE2_DEBUG 5
66
67 /* decoder/encoder status codes */
68 #define CS_BUFFER_ERROR -3
69 #define CS_STACK_ERROR -2
70 #define CS_PARSE_ERROR -1
71 #define CS_PARSE_OK 0
72 #define CS_PARSE_END 1
73 #define CS_SUBR_RETURN 2
74 #define CS_CHAR_END 3
75
76 static int status = CS_PARSE_ERROR;
77
78 #define DST_NEED(a,b) {if ((a) < (b)) { status = CS_BUFFER_ERROR ; return ; }}
79 #define SRC_NEED(a,b) {if ((a) < (b)) { status = CS_PARSE_ERROR ; return ; }}
80 #define NEED(a,b) {if ((a) < (b)) { status = CS_STACK_ERROR ; return ; }}
81
82 /* hintmask and cntrmask need number of stem zones */
83 static int num_stems = 0;
84 static int phase = 0;
85
86 /* subroutine nesting */
87 static int nest = 0;
88
89 /* advance width */
90 static int have_width = 0;
91 static double width = 0.0;
92
93 /*
94 * Standard Encoding Accented Characters:
95 * Optional four arguments for endchar. See, CFF spec., p.35.
96 * This is obsolete feature and is no longer supported.
97 */
98 #if 0
99 /* adx ady bchar achar endchar */
100 static double seac[4] = {0.0, 0.0, 0.0, 0.0};
101 #endif
102
103 /* Operand stack and Transient array */
104 static int stack_top = 0;
105 static double arg_stack[CS_ARG_STACK_MAX];
106 static double trn_array[CS_TRANS_ARRAY_MAX];
107
108 /*
109 * Type 2 CharString encoding
110 */
111
112 /*
113 * 1-byte CharString operaotrs:
114 * cs_escape is first byte of two-byte operator
115 */
116
117 /* RESERVED 0 */
118 #define cs_hstem 1
119 /* RESERVED 2 */
120 #define cs_vstem 3
121 #define cs_vmoveto 4
122 #define cs_rlineto 5
123 #define cs_hlineto 6
124 #define cs_vlineto 7
125 #define cs_rrcurveto 8
126 /* cs_closepath 9 : TYPE1 */
127 #define cs_callsubr 10
128 #define cs_return 11
129 #define cs_escape 12
130 /* cs_hsbw 13 : TYPE1 */
131 #define cs_endchar 14
132 /* RESERVED 15 */
133 /* RESERVED 16 */
134 /* RESERVED 17 */
135 #define cs_hstemhm 18
136 #define cs_hintmask 19
137 #define cs_cntrmask 20
138 #define cs_rmoveto 21
139 #define cs_hmoveto 22
140 #define cs_vstemhm 23
141 #define cs_rcurveline 24
142 #define cs_rlinecurve 25
143 #define cs_vvcurveto 26
144 #define cs_hhcurveto 27
145 /* SHORTINT 28 : first byte of shortint*/
146 #define cs_callgsubr 29
147 #define cs_vhcurveto 30
148 #define cs_hvcurveto 31
149
150 /*
151 * 2-byte CharString operaotrs:
152 * "dotsection" is obsoleted in Type 2 charstring.
153 */
154
155 #define cs_dotsection 0
156 /* cs_vstem3 1 : TYPE1 */
157 /* cs_hstem3 2 : TYPE1 */
158 #define cs_and 3
159 #define cs_or 4
160 #define cs_not 5
161 /* cs_seac 6 : TYPE1 */
162 /* cs_sbw 7 : TYPE1 */
163 /* RESERVED 8 */
164 #define cs_abs 9
165 #define cs_add 10
166 #define cs_sub 11
167 #define cs_div 12
168 /* RESERVED 13 */
169 #define cs_neg 14
170 #define cs_eq 15
171 /* cs_callothersubr 16 : TYPE1 */
172 /* cs_pop 17 : TYPE1 */
173 #define cs_drop 18
174 /* RESERVED 19 */
175 #define cs_put 20
176 #define cs_get 21
177 #define cs_ifelse 22
178 #define cs_random 23
179 #define cs_mul 24
180 /* RESERVED 25 */
181 #define cs_sqrt 26
182 #define cs_dup 27
183 #define cs_exch 28
184 #define cs_index 29
185 #define cs_roll 30
186 /* cs_setcurrentpoint 31 : TYPE1 */
187 /* RESERVED 32 */
188 /* RESERVED 33 */
189 #define cs_hflex 34
190 #define cs_flex 35
191 #define cs_hflex1 36
192 #define cs_flex1 37
193
194 /*
195 * clear_stack() put all operands sotred in operand stack to dest.
196 */
197 static void
198 clear_stack (card8 **dest, card8 *limit)
199 {
200 int i;
201
202 for (i = 0; i < stack_top; i++) {
203 double value;
204 int ivalue;
205 value = arg_stack[i];
206 /* Nearest integer value */
207 ivalue = (int) floor(value+0.5);
208 if (value >= 0x8000L || value <= (-0x8000L - 1)) {
209 /*
210 * This number cannot be represented as a single operand.
211 * We must use `a b mul ...' or `a c div' to represent large values.
212 */
213 ERROR("%s: Argument value too large. (This is bug)", CS_TYPE2_DEBUG_STR);
214 } else if (fabs(value - ivalue) > 3.0e-5) {
215 /* 16.16-bit signed fixed value */
216 DST_NEED(limit, *dest + 5);
217 *(*dest)++ = 255;
218 ivalue = (int) floor(value); /* mantissa */
219 *(*dest)++ = (ivalue >> 8) & 0xff;
220 *(*dest)++ = ivalue & 0xff;
221 ivalue = (int)((value - ivalue) * 0x10000l); /* fraction */
222 *(*dest)++ = (ivalue >> 8) & 0xff;
223 *(*dest)++ = ivalue & 0xff;
224 /* Everything else are integers. */
225 } else if (ivalue >= -107 && ivalue <= 107) {
226 DST_NEED(limit, *dest + 1);
227 *(*dest)++ = ivalue + 139;
228 } else if (ivalue >= 108 && ivalue <= 1131) {
229 DST_NEED(limit, *dest + 2);
230 ivalue = 0xf700u + ivalue - 108;
231 *(*dest)++ = (ivalue >> 8) & 0xff;
232 *(*dest)++ = ivalue & 0xff;
233 } else if (ivalue >= -1131 && ivalue <= -108) {
234 DST_NEED(limit, *dest + 2);
235 ivalue = 0xfb00u - ivalue - 108;
236 *(*dest)++ = (ivalue >> 8) & 0xff;
237 *(*dest)++ = ivalue & 0xff;
238 } else if (ivalue >= -32768 && ivalue <= 32767) { /* shortint */
239 DST_NEED(limit, *dest + 3);
240 *(*dest)++ = 28;
241 *(*dest)++ = (ivalue >> 8) & 0xff;
242 *(*dest)++ = (ivalue) & 0xff;
243 } else { /* Shouldn't come here */
244 ERROR("%s: Unexpected error.", CS_TYPE2_DEBUG_STR);
245 }
246 }
247
248 stack_top = 0; /* clear stack */
249
250 return;
251 }
252
253 /*
254 * Single byte operators:
255 * Path construction, Operator for finishing a path, Hint operators.
256 *
257 * phase:
258 * 0: inital state
259 * 1: hint declaration, first stack-clearing operator appeared
260 * 2: in path construction
261 */
262
263 static void
264 do_operator1 (card8 **dest, card8 *limit, card8 **data, card8 *endptr)
265 {
266 card8 op = **data;
267
268 *data += 1;
269
270 switch (op) {
271 case cs_hstemhm:
272 case cs_vstemhm:
273 /* charstring may have hintmask if above operator have seen */
274 case cs_hstem:
275 case cs_vstem:
276 if (phase == 0 && (stack_top % 2)) {
277 have_width = 1;
278 width = arg_stack[0];
279 }
280 num_stems += stack_top/2;
281 clear_stack(dest, limit);
282 DST_NEED(limit, *dest + 1);
283 *(*dest)++ = op;
284 phase = 1;
285 break;
286 case cs_hintmask:
287 case cs_cntrmask:
288 if (phase < 2) {
289 if (phase == 0 && (stack_top % 2)) {
290 have_width = 1;
291 width = arg_stack[0];
292 }
293 num_stems += stack_top/2;
294 }
295 clear_stack(dest, limit);
296 DST_NEED(limit, *dest + 1);
297 *(*dest)++ = op;
298 if (num_stems > 0) {
299 int masklen = (num_stems + 7) / 8;
300 DST_NEED(limit, *dest + masklen);
301 SRC_NEED(endptr, *data + masklen);
302 memmove(*dest, *data, masklen);
303 *data += masklen;
304 *dest += masklen;
305 }
306 phase = 2;
307 break;
308 case cs_rmoveto:
309 if (phase == 0 && (stack_top % 2)) {
310 have_width = 1;
311 width = arg_stack[0];
312 }
313 clear_stack(dest, limit);
314 DST_NEED(limit, *dest + 1);
315 *(*dest)++ = op;
316 phase = 2;
317 break;
318 case cs_hmoveto:
319 case cs_vmoveto:
320 if (phase == 0 && (stack_top % 2) == 0) {
321 have_width = 1;
322 width = arg_stack[0];
323 }
324 clear_stack(dest, limit);
325 DST_NEED(limit, *dest + 1);
326 *(*dest)++ = op;
327 phase = 2;
328 break;
329 case cs_endchar:
330 if (stack_top == 1) {
331 have_width = 1;
332 width = arg_stack[0];
333 clear_stack(dest, limit);
334 } else if (stack_top == 4 || stack_top == 5) {
335 WARN("\"seac\" character deprecated in Type 2 charstring.");
336 status = CS_PARSE_ERROR;
337 return;
338 } else if (stack_top > 0) {
339 WARN("%s: Operand stack not empty.", CS_TYPE2_DEBUG_STR);
340 }
341 DST_NEED(limit, *dest + 1);
342 *(*dest)++ = op;
343 status = CS_CHAR_END;
344 break;
345 /* above oprators are candidate for first stack-clearing operator */
346 case cs_rlineto:
347 case cs_hlineto:
348 case cs_vlineto:
349 case cs_rrcurveto:
350 case cs_rcurveline:
351 case cs_rlinecurve:
352 case cs_vvcurveto:
353 case cs_hhcurveto:
354 case cs_vhcurveto:
355 case cs_hvcurveto:
356 if (phase < 2) {
357 WARN("%s: Broken Type 2 charstring.", CS_TYPE2_DEBUG_STR);
358 status = CS_PARSE_ERROR;
359 return;
360 }
361 clear_stack(dest, limit);
362 DST_NEED(limit, *dest + 1);
363 *(*dest)++ = op;
364 break;
365 /* all operotors above are stack-clearing operator */
366 /* no output */
367 case cs_return:
368 case cs_callgsubr:
369 case cs_callsubr:
370 ERROR("%s: Unexpected call(g)subr/return", CS_TYPE2_DEBUG_STR);
371 break;
372 default:
373 /* no-op ? */
374 WARN("%s: Unknown charstring operator: 0x%02x", CS_TYPE2_DEBUG_STR, op);
375 status = CS_PARSE_ERROR;
376 break;
377 }
378
379 return;
380 }
381
382 /*
383 * Double byte operators:
384 * Flex, arithmetic, conditional, and storage operators.
385 *
386 * Following operators are not supported:
387 * random: How random ?
388 */
389 static void
390 do_operator2 (card8 **dest, card8 *limit, card8 **data, card8 *endptr)
391 {
392 card8 op;
393
394 *data += 1;
395
396 SRC_NEED(endptr, *data + 1);
397
398 op = **data;
399 *data += 1;
400
401 switch(op) {
402 case cs_dotsection: /* deprecated */
403 WARN("Operator \"dotsection\" deprecated in Type 2 charstring.");
404 status = CS_PARSE_ERROR;
405 return;
406 case cs_hflex:
407 case cs_flex:
408 case cs_hflex1:
409 case cs_flex1:
410 if (phase < 2) {
411 WARN("%s: Broken Type 2 charstring.", CS_TYPE2_DEBUG_STR);
412 status = CS_PARSE_ERROR;
413 return;
414 }
415 clear_stack(dest, limit);
416 DST_NEED(limit, *dest + 2);
417 *(*dest)++ = cs_escape;
418 *(*dest)++ = op;
419 break;
420 /* all operator above are stack-clearing */
421 /* no output */
422 case cs_and:
423 NEED(stack_top, 2);
424 stack_top--;
425 if (arg_stack[stack_top] && arg_stack[stack_top-1]) {
426 arg_stack[stack_top-1] = 1.0;
427 } else {
428 arg_stack[stack_top-1] = 0.0;
429 }
430 break;
431 case cs_or:
432 NEED(stack_top, 2);
433 stack_top--;
434 if (arg_stack[stack_top] || arg_stack[stack_top-1]) {
435 arg_stack[stack_top-1] = 1.0;
436 } else {
437 arg_stack[stack_top-1] = 0.0;
438 }
439 break;
440 case cs_not:
441 NEED(stack_top, 1);
442 if (arg_stack[stack_top-1]) {
443 arg_stack[stack_top-1] = 0.0;
444 } else {
445 arg_stack[stack_top-1] = 1.0;
446 }
447 break;
448 case cs_abs:
449 NEED(stack_top, 1);
450 arg_stack[stack_top-1] = fabs(arg_stack[stack_top-1]);
451 break;
452 case cs_add:
453 NEED(stack_top, 2);
454 arg_stack[stack_top-2] += arg_stack[stack_top-1];
455 stack_top--;
456 break;
457 case cs_sub:
458 NEED(stack_top, 2);
459 arg_stack[stack_top-2] -= arg_stack[stack_top-1];
460 stack_top--;
461 break;
462 case cs_div: /* doesn't check overflow */
463 NEED(stack_top, 2);
464 arg_stack[stack_top-2] /= arg_stack[stack_top-1];
465 stack_top--;
466 break;
467 case cs_neg:
468 NEED(stack_top, 1);
469 arg_stack[stack_top-1] *= -1.0;
470 break;
471 case cs_eq:
472 NEED(stack_top, 2);
473 stack_top--;
474 if (arg_stack[stack_top] == arg_stack[stack_top-1]) {
475 arg_stack[stack_top-1] = 1.0;
476 } else {
477 arg_stack[stack_top-1] = 0.0;
478 }
479 break;
480 case cs_drop:
481 NEED(stack_top, 1);
482 stack_top--;
483 break;
484 case cs_put:
485 NEED(stack_top, 2);
486 {
487 int idx = (int)arg_stack[--stack_top];
488 NEED(CS_TRANS_ARRAY_MAX, idx);
489 trn_array[idx] = arg_stack[--stack_top];
490 }
491 break;
492 case cs_get:
493 NEED(stack_top, 1);
494 {
495 int idx = (int)arg_stack[stack_top-1];
496 NEED(CS_TRANS_ARRAY_MAX, idx);
497 arg_stack[stack_top-1] = trn_array[idx];
498 }
499 break;
500 case cs_ifelse:
501 NEED(stack_top, 4);
502 stack_top -= 3;
503 if (arg_stack[stack_top+1] > arg_stack[stack_top+2]) {
504 arg_stack[stack_top-1] = arg_stack[stack_top];
505 }
506 break;
507 case cs_mul:
508 NEED(stack_top, 2);
509 arg_stack[stack_top-2] = arg_stack[stack_top-2] * arg_stack[stack_top-1];
510 stack_top--;
511 break;
512 case cs_sqrt:
513 NEED(stack_top, 1);
514 arg_stack[stack_top-1] = sqrt(arg_stack[stack_top-1]);
515 break;
516 case cs_dup:
517 NEED(stack_top, 1);
518 NEED(CS_ARG_STACK_MAX, stack_top+1);
519 arg_stack[stack_top] = arg_stack[stack_top-1];
520 stack_top++;
521 break;
522 case cs_exch:
523 NEED(stack_top, 2);
524 {
525 double save = arg_stack[stack_top-2];
526 arg_stack[stack_top-2] = arg_stack[stack_top-1];
527 arg_stack[stack_top-1] = save;
528 }
529 break;
530 case cs_index:
531 NEED(stack_top, 2); /* need two arguments at least */
532 {
533 int idx = (int)arg_stack[stack_top-1];
534 if (idx < 0) {
535 arg_stack[stack_top-1] = arg_stack[stack_top-2];
536 } else {
537 NEED(stack_top, idx+2);
538 arg_stack[stack_top-1] = arg_stack[stack_top-idx-2];
539 }
540 }
541 break;
542 case cs_roll:
543 NEED(stack_top, 2);
544 {
545 int N, J;
546 J = (int)arg_stack[--stack_top];
547 N = (int)arg_stack[--stack_top];
548 NEED(stack_top, N);
549 if (J > 0) {
550 J = J % N;
551 while (J-- > 0) {
552 double save = arg_stack[stack_top-1];
553 int i = stack_top - 1;
554 while (i > stack_top-N) {
555 arg_stack[i] = arg_stack[i-1];
556 i--;
557 }
558 arg_stack[i] = save;
559 }
560 } else {
561 J = (-J) % N;
562 while (J-- > 0) {
563 double save = arg_stack[stack_top-N];
564 int i = stack_top - N;
565 while (i < stack_top-1) {
566 arg_stack[i] = arg_stack[i+1];
567 i++;
568 }
569 arg_stack[i] = save;
570 }
571 }
572 }
573 break;
574 case cs_random:
575 WARN("%s: Charstring operator \"random\" found.", CS_TYPE2_DEBUG_STR);
576 NEED(CS_ARG_STACK_MAX, stack_top+1);
577 arg_stack[stack_top++] = 1.0;
578 break;
579 default:
580 /* no-op ? */
581 WARN("%s: Unknown charstring operator: 0x0c%02x", CS_TYPE2_DEBUG_STR, op);
582 status = CS_PARSE_ERROR;
583 break;
584 }
585
586 return;
587 }
588
589 /*
590 * integer:
591 * exactly the same as the DICT encoding (except 29)
592 */
593 static void
594 get_integer (card8 **data, card8 *endptr)
595 {
596 int result = 0;
597 card8 b0 = **data, b1, b2;
598
599 *data += 1;
600
601 if (b0 == 28) { /* shortint */
602 SRC_NEED(endptr, *data + 2);
603 b1 = **data;
604 b2 = *(*data+1);
605 result = b1*256+b2;
606 if (result > 0x7fff)
607 result -= 0x10000L;
608 *data += 2;
609 } else if (b0 >= 32 && b0 <= 246) { /* int (1) */
610 result = b0 - 139;
611 } else if (b0 >= 247 && b0 <= 250) { /* int (2) */
612 SRC_NEED(endptr, *data + 1);
613 b1 = **data;
614 result = (b0-247)*256+b1+108;
615 *data += 1;
616 } else if (b0 >= 251 && b0 <= 254) {
617 SRC_NEED(endptr, *data + 1);
618 b1 = **data;
619 result = -(b0-251)*256-b1-108;
620 *data += 1;
621 } else {
622 status = CS_PARSE_ERROR;
623 return;
624 }
625
626 NEED(CS_ARG_STACK_MAX, stack_top+1);
627 arg_stack[stack_top++] = (double) result;
628
629 return;
630 }
631
632 /*
633 * Signed 16.16-bits fixed number for Type 2 charstring encoding
634 */
635 static void
636 get_fixed (card8 **data, card8 *endptr)
637 {
638 int ivalue;
639 double rvalue;
640
641 *data += 1;
642
643 SRC_NEED(endptr, *data + 4);
644
645 ivalue = *(*data) * 0x100 + *(*data+1);
646 rvalue = (ivalue > 0x7fffL) ? (ivalue - 0x10000L) : ivalue;
647 ivalue = *(*data+2) * 0x100 + *(*data+3);
648 rvalue += ((double) ivalue) / 0x10000L;
649
650 NEED(CS_ARG_STACK_MAX, stack_top+1);
651 arg_stack[stack_top++] = rvalue;
652 *data += 4;
653
654 return;
655 }
656
657 /*
658 * Subroutines:
659 * The bias for subroutine number is introduced in type 2 charstrings.
660 *
661 * subr: set to a pointer to the subroutine charstring.
662 * len: set to the length of subroutine charstring.
663 * subr_idx: CFF INDEX data that contains subroutines.
664 * id: biased subroutine number.
665 */
666 static void
667 get_subr (card8 **subr, int *len, cff_index *subr_idx, int id)
668 {
669 card16 count;
670
671 if (subr_idx == NULL)
672 ERROR("%s: Subroutine called but no subroutine found.", CS_TYPE2_DEBUG_STR);
673
674 count = subr_idx->count;
675
676 /* Adding bias number */
677 if (count < 1240) {
678 id += 107;
679 } else if (count < 33900) {
680 id += 1131;
681 } else {
682 id += 32768;
683 }
684
685 if (id > count)
686 ERROR("%s: Invalid Subr index: %ld (max=%u)", CS_TYPE2_DEBUG_STR, id, count);
687
688 *len = (subr_idx->offset)[id + 1] - (subr_idx->offset)[id];
689 *subr = subr_idx->data + (subr_idx->offset)[id] - 1;
690
691 return;
692 }
693
694 /*
695 * NOTE:
696 * The Type 2 interpretation of a number encoded in five-bytes (those with
697 * an initial byte value of 255) differs from how it is interpreted in the
698 * Type 1 format.
699 */
700
701 static void
702 do_charstring (card8 **dest, card8 *limit,
703 card8 **data, card8 *endptr,
704 cff_index *gsubr_idx, cff_index *subr_idx)
705 {
706 card8 b0 = 0, *subr;
707 int len;
708
709 if (nest > CS_SUBR_NEST_MAX)
710 ERROR("%s: Subroutine nested too deeply.", CS_TYPE2_DEBUG_STR);
711
712 nest++;
713
714 while (*data < endptr && status == CS_PARSE_OK) {
715 b0 = **data;
716 if (b0 == 255) { /* 16-bit.16-bit fixed signed number */
717 get_fixed(data, endptr);
718 } else if (b0 == cs_return) {
719 status = CS_SUBR_RETURN;
720 } else if (b0 == cs_callgsubr) {
721 if (stack_top < 1) {
722 status = CS_STACK_ERROR;
723 } else {
724 stack_top--;
725 get_subr(&subr, &len, gsubr_idx, (int) arg_stack[stack_top]);
726 if (*dest + len > limit)
727 ERROR("%s: Possible buffer overflow.", CS_TYPE2_DEBUG_STR);
728 do_charstring(dest, limit, &subr, subr + len,
729 gsubr_idx, subr_idx);
730 *data += 1;
731 }
732 } else if (b0 == cs_callsubr) {
733 if (stack_top < 1) {
734 status = CS_STACK_ERROR;
735 } else {
736 stack_top--;
737 get_subr(&subr, &len, subr_idx, (int) arg_stack[stack_top]);
738 if (limit < *dest + len)
739 ERROR("%s: Possible buffer overflow.", CS_TYPE2_DEBUG_STR);
740 do_charstring(dest, limit, &subr, subr + len,
741 gsubr_idx, subr_idx);
742 *data += 1;
743 }
744 } else if (b0 == cs_escape) {
745 do_operator2(dest, limit, data, endptr);
746 } else if (b0 < 32 && b0 != 28) { /* 19, 20 need mask */
747 do_operator1(dest, limit, data, endptr);
748 } else if ((b0 <= 22 && b0 >= 27) || b0 == 31) { /* reserved */
749 status = CS_PARSE_ERROR; /* not an error ? */
750 } else { /* integer */
751 get_integer(data, endptr);
752 }
753 }
754
755 if (status == CS_SUBR_RETURN) {
756 status = CS_PARSE_OK;
757 } else if (status == CS_CHAR_END && *data < endptr) {
758 WARN("%s: Garbage after endchar.", CS_TYPE2_DEBUG_STR);
759 } else if (status < CS_PARSE_OK) { /* error */
760 ERROR("%s: Parsing charstring failed: (status=%d, stack=%d)",
761 CS_TYPE2_DEBUG_STR, status, stack_top);
762 }
763
764 nest--;
765
766 return;
767 }
768
769 static void
770 cs_parse_init (void)
771 {
772 status = CS_PARSE_OK;
773 nest = 0;
774 phase = 0;
775 num_stems = 0;
776 stack_top = 0;
777 }
778
779 /*
780 * Not just copying...
781 */
782 int
783 cs_copy_charstring (card8 *dst, int dstlen,
784 card8 *src, int srclen,
785 cff_index *gsubr, cff_index *subr,
786 double default_width, double nominal_width, cs_ginfo *ginfo)
787 {
788 card8 *save = dst;
789
790 cs_parse_init();
791
792 width = 0.0;
793 have_width = 0;
794
795 /* expand call(g)subrs */
796 do_charstring(&dst, dst + dstlen, &src, src + srclen, gsubr, subr);
797
798 if (ginfo) {
799 ginfo->flags = 0; /* not used */
800 if (have_width) {
801 ginfo->wx = nominal_width + width;
802 } else {
803 ginfo->wx = default_width;
804 }
805 }
806
807 return (int)(dst - save);
808 }