3 This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
5 Copyright (C) 2002-2012 by Jin-Hwan Cho and Shunsaku Hirata,
6 the dvipdfmx project team.
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.
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.
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.
24 * Type 2 Charstring support:
25 * Decode and encode Type 2 charstring
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.
38 * And also note that subroutine numbers within subroutines can depend on the
39 * content of operand stack as follows:
41 * ... l m callsubr << subr #(m+bias): n add callsubr >> ...
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
56 /* data types, limits */
57 #include "cff_types.h"
58 #include "cff_limits.h"
62 #define CS_TYPE2_DEBUG_STR "Type2 Charstring Parser"
63 #define CS_TYPE2_DEBUG 5
65 /* decoder/encoder status codes */
66 #define CS_BUFFER_ERROR -3
67 #define CS_STACK_ERROR -2
68 #define CS_PARSE_ERROR -1
70 #define CS_PARSE_END 1
71 #define CS_SUBR_RETURN 2
74 static int status = CS_PARSE_ERROR;
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 ; }}
80 /* hintmask and cntrmask need number of stem zones */
81 static int num_stems = 0;
84 /* subroutine nesting */
88 static int have_width = 0;
89 static double width = 0.0;
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.
97 /* adx ady bchar achar endchar */
98 static double seac[4] = {0.0, 0.0, 0.0, 0.0};
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];
107 * Type 2 CharString encoding
111 * 1-byte CharString operaotrs:
112 * cs_escape is first byte of two-byte operator
123 #define cs_rrcurveto 8
124 /* cs_closepath 9 : TYPE1 */
125 #define cs_callsubr 10
128 /* cs_hsbw 13 : TYPE1 */
129 #define cs_endchar 14
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
149 * 2-byte CharString operaotrs:
150 * "dotsection" is obsoleted in Type 2 charstring.
153 #define cs_dotsection 0
154 /* cs_vstem3 1 : TYPE1 */
155 /* cs_hstem3 2 : TYPE1 */
159 /* cs_seac 6 : TYPE1 */
160 /* cs_sbw 7 : TYPE1 */
169 /* cs_callothersubr 16 : TYPE1 */
170 /* cs_pop 17 : TYPE1 */
184 /* cs_setcurrentpoint 31 : TYPE1 */
193 * clear_stack() put all operands sotred in operand stack to dest.
196 clear_stack (card8 **dest, card8 *limit)
200 for (i = 0; i < stack_top; i++) {
203 value = arg_stack[i];
204 /* Nearest integer value */
205 ivalue = (long) floor(value+0.5);
206 if (value >= 0x8000L || value <= (-0x8000L - 1)) {
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.
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);
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);
239 *(*dest)++ = (ivalue >> 8) & 0xff;
240 *(*dest)++ = (ivalue) & 0xff;
241 } else { /* Shouldn't come here */
242 ERROR("%s: Unexpected error.", CS_TYPE2_DEBUG_STR);
246 stack_top = 0; /* clear stack */
252 * Single byte operators:
253 * Path construction, Operator for finishing a path, Hint operators.
257 * 1: hint declaration, first stack-clearing operator appeared
258 * 2: in path construction
262 do_operator1 (card8 **dest, card8 *limit, card8 **data, card8 *endptr)
271 /* charstring may have hintmask if above operator have seen */
274 if (phase == 0 && (stack_top % 2)) {
276 width = arg_stack[0];
278 num_stems += stack_top/2;
279 clear_stack(dest, limit);
280 DST_NEED(limit, *dest + 1);
287 if (phase == 0 && (stack_top % 2)) {
289 width = arg_stack[0];
291 num_stems += stack_top/2;
293 clear_stack(dest, limit);
294 DST_NEED(limit, *dest + 1);
297 int masklen = (num_stems + 7) / 8;
298 DST_NEED(limit, *dest + masklen);
299 SRC_NEED(endptr, *data + masklen);
300 memmove(*dest, *data, masklen);
307 if (phase == 0 && (stack_top % 2)) {
309 width = arg_stack[0];
311 clear_stack(dest, limit);
312 DST_NEED(limit, *dest + 1);
318 if (phase == 0 && (stack_top % 2) == 0) {
320 width = arg_stack[0];
322 clear_stack(dest, limit);
323 DST_NEED(limit, *dest + 1);
328 if (stack_top == 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;
336 } else if (stack_top > 0) {
337 WARN("%s: Operand stack not empty.", CS_TYPE2_DEBUG_STR);
339 DST_NEED(limit, *dest + 1);
341 status = CS_CHAR_END;
343 /* above oprators are candidate for first stack-clearing operator */
355 WARN("%s: Broken Type 2 charstring.", CS_TYPE2_DEBUG_STR);
356 status = CS_PARSE_ERROR;
359 clear_stack(dest, limit);
360 DST_NEED(limit, *dest + 1);
363 /* all operotors above are stack-clearing operator */
368 ERROR("%s: Unexpected call(g)subr/return", CS_TYPE2_DEBUG_STR);
372 WARN("%s: Unknown charstring operator: 0x%02x", CS_TYPE2_DEBUG_STR, op);
373 status = CS_PARSE_ERROR;
381 * Double byte operators:
382 * Flex, arithmetic, conditional, and storage operators.
384 * Following operators are not supported:
385 * random: How random ?
388 do_operator2 (card8 **dest, card8 *limit, card8 **data, card8 *endptr)
394 SRC_NEED(endptr, *data + 1);
400 case cs_dotsection: /* deprecated */
401 WARN("Operator \"dotsection\" deprecated in Type 2 charstring.");
402 status = CS_PARSE_ERROR;
410 WARN("%s: Broken Type 2 charstring.", CS_TYPE2_DEBUG_STR);
411 status = CS_PARSE_ERROR;
414 clear_stack(dest, limit);
415 DST_NEED(limit, *dest + 2);
416 *(*dest)++ = cs_escape;
419 /* all operator above are stack-clearing */
424 if (arg_stack[stack_top] && arg_stack[stack_top-1]) {
425 arg_stack[stack_top-1] = 1.0;
427 arg_stack[stack_top-1] = 0.0;
433 if (arg_stack[stack_top] || arg_stack[stack_top-1]) {
434 arg_stack[stack_top-1] = 1.0;
436 arg_stack[stack_top-1] = 0.0;
441 if (arg_stack[stack_top-1]) {
442 arg_stack[stack_top-1] = 0.0;
444 arg_stack[stack_top-1] = 1.0;
449 arg_stack[stack_top-1] = fabs(arg_stack[stack_top-1]);
453 arg_stack[stack_top-2] += arg_stack[stack_top-1];
458 arg_stack[stack_top-2] -= arg_stack[stack_top-1];
461 case cs_div: /* doesn't check overflow */
463 arg_stack[stack_top-2] /= arg_stack[stack_top-1];
468 arg_stack[stack_top-1] *= -1.0;
473 if (arg_stack[stack_top] == arg_stack[stack_top-1]) {
474 arg_stack[stack_top-1] = 1.0;
476 arg_stack[stack_top-1] = 0.0;
486 int idx = (int)arg_stack[--stack_top];
487 NEED(CS_TRANS_ARRAY_MAX, idx);
488 trn_array[idx] = arg_stack[--stack_top];
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];
502 if (arg_stack[stack_top+1] > arg_stack[stack_top+2]) {
503 arg_stack[stack_top-1] = arg_stack[stack_top];
508 arg_stack[stack_top-2] = arg_stack[stack_top-2] * arg_stack[stack_top-1];
513 arg_stack[stack_top-1] = sqrt(arg_stack[stack_top-1]);
517 NEED(CS_ARG_STACK_MAX, stack_top+1);
518 arg_stack[stack_top] = arg_stack[stack_top-1];
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;
530 NEED(stack_top, 2); /* need two arguments at least */
532 int idx = (int)arg_stack[stack_top-1];
534 arg_stack[stack_top-1] = arg_stack[stack_top-2];
536 NEED(stack_top, idx+2);
537 arg_stack[stack_top-1] = arg_stack[stack_top-idx-2];
545 J = (int)arg_stack[--stack_top];
546 N = (int)arg_stack[--stack_top];
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];
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];
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;
580 WARN("%s: Unknown charstring operator: 0x0c%02x", CS_TYPE2_DEBUG_STR, op);
581 status = CS_PARSE_ERROR;
590 * exactly the same as the DICT encoding (except 29)
593 get_integer (card8 **data, card8 *endptr)
596 card8 b0 = **data, b1, b2;
600 if (b0 == 28) { /* shortint */
601 SRC_NEED(endptr, *data + 2);
608 } else if (b0 >= 32 && b0 <= 246) { /* int (1) */
610 } else if (b0 >= 247 && b0 <= 250) { /* int (2) */
611 SRC_NEED(endptr, *data + 1);
613 result = (b0-247)*256+b1+108;
615 } else if (b0 >= 251 && b0 <= 254) {
616 SRC_NEED(endptr, *data + 1);
618 result = -(b0-251)*256-b1-108;
621 status = CS_PARSE_ERROR;
625 NEED(CS_ARG_STACK_MAX, stack_top+1);
626 arg_stack[stack_top++] = (double) result;
632 * Signed 16.16-bits fixed number for Type 2 charstring encoding
635 get_fixed (card8 **data, card8 *endptr)
642 SRC_NEED(endptr, *data + 4);
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;
649 NEED(CS_ARG_STACK_MAX, stack_top+1);
650 arg_stack[stack_top++] = rvalue;
658 * The bias for subroutine number is introduced in type 2 charstrings.
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.
666 get_subr (card8 **subr, long *len, cff_index *subr_idx, long id)
670 if (subr_idx == NULL)
671 ERROR("%s: Subroutine called but no subroutine found.", CS_TYPE2_DEBUG_STR);
673 count = subr_idx->count;
675 /* Adding bias number */
678 } else if (count < 33900) {
685 ERROR("%s: Invalid Subr index: %ld (max=%u)", CS_TYPE2_DEBUG_STR, id, count);
687 *len = (subr_idx->offset)[id + 1] - (subr_idx->offset)[id];
688 *subr = subr_idx->data + (subr_idx->offset)[id] - 1;
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
701 do_charstring (card8 **dest, card8 *limit,
702 card8 **data, card8 *endptr,
703 cff_index *gsubr_idx, cff_index *subr_idx)
708 if (nest > CS_SUBR_NEST_MAX)
709 ERROR("%s: Subroutine nested too deeply.", CS_TYPE2_DEBUG_STR);
713 while (*data < endptr && status == CS_PARSE_OK) {
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) {
721 status = CS_STACK_ERROR;
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);
731 } else if (b0 == cs_callsubr) {
733 status = CS_STACK_ERROR;
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);
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);
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);
771 status = CS_PARSE_OK;
779 * Not just copying...
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)
794 /* expand call(g)subrs */
795 do_charstring(&dst, dst + dstlen, &src, src + srclen, gsubr, subr);
798 ginfo->flags = 0; /* not used */
800 ginfo->wx = nominal_width + width;
802 ginfo->wx = default_width;
806 return (long)(dst - save);