OSDN Git Service

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