Debian Codegraph @

texk/xdvipsk/otflib/cs_type2.c

raw
1 /* This is xdvipsk, an eXtended version of dvips(k) by Tomas Rokicki.
2
3 Copyright (C) 2016 by VTeX Ltd (www.vtex.lt),
4 the xdvipsk project team - Sigitas Tolusis and Arunas Povilaitis.
5
6 Program original code copyright (C) 2007-2014 by Jin-Hwan Cho and
7 Shunsaku Hirata, the dvipdfmx project team.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 */
23
24 /*
25 * Type 2 Charstring support:
26 * Decode and encode Type 2 charstring
27 *
28 * All local/global subroutine calls in a given charstring is replace by the
29 * content of subroutine charstrings. We do this because some PostScript RIP
30 * may have problems with sparse subroutine array. Workaround for this is to
31 * re-order subroutine array so that no gap appears in the subroutine array,
32 * or put dummy charstrings that contains only `return' in the gap. However,
33 * re-ordering of subroutine is rather difficult for Type 2 charstrings due
34 * to the bias which depends on the total number of subroutines. Replacing
35 * callgsubr/callsubr calls with the content of the corresponding subroutine
36 * charstring may be more efficient than putting dummy subroutines in the
37 * case of subsetted font. Adobe distiller seems doing same thing.
38 *
39 * And also note that subroutine numbers within subroutines can depend on the
40 * content of operand stack as follows:
41 *
42 * ... l m callsubr << subr #(m+bias): n add callsubr >> ...
43 *
44 * I've not implemented the `random' operator which generates a pseudo-random
45 * number in the range (0, 1] and push them into argument stack.
46 * How pseudo-random sequences are generated is not documented in the Type 2
47 * charstring spec..
48 */
49
50 #include <config.h>
51
52 #include <stdio.h>
53
54 /* fabs, sqrt ... */
55 #include <math.h>
56
57 #include "mem.h"
58 #include "error.h"
59
60 /* data types, limits */
61 #include "cff_types.h"
62 #include "cff_limits.h"
63
64 #include "cs_type2.h"
65
66 #define CS_TYPE2_DEBUG_STR "Type2 Charstring Parser"
67 #define CS_TYPE2_DEBUG 5
68
69 static UT_icd double_icd = {sizeof(double), NULL, NULL, NULL };
70
71 #define STACK_SIZE 48
72 #define PS_STACK_SIZE 24
73 #define MAX_SUBR_DEPTH 10
74 #define SCRATCH_SIZE 32
75
76 /* Type1CharstringGenInterp status codes */
77 #define S_CGI_INITIAL 0
78 #define S_CGI_OPEN 1
79 #define S_CGI_CLOSED 2
80 #define S_CGI_SEAC 3
81
82 /* Type1CharstringGen status codes */
83 #define S_CHG_INITIAL 0
84 #define S_CHG_GEN 1
85
86 /* CharstringInterp status codes */
87 #define S_CCI_INITIAL 0
88 #define S_CCI_SEAC 1
89 #define S_CCI_SBW 2
90 #define S_CCI_HSTEM 3
91 #define S_CCI_VSTEM 4
92 #define S_CCI_HINTMASK 5
93 #define S_CCI_IPATH 6
94 #define S_CCI_PATH 7
95
96 /* CharstringInterp error codes */
97 #define CCI_errOK 0
98 #define CCI_errInternal -1
99 #define CCI_errRunoff -2
100 #define CCI_errUnimplemented -3
101 #define CCI_errOverflow -4
102 #define CCI_errUnderflow -5
103 #define CCI_errVector -6
104 #define CCI_errValue -7
105 #define CCI_errSubr -8
106 #define CCI_errGlyph -9
107 #define CCI_errCurrentPoint -10
108 #define CCI_errFlex -11
109 #define CCI_errMultipleMaster -12
110 #define CCI_errOpenStroke -13
111 #define CCI_errLateSidebearing -14
112 #define CCI_errOthersubr -15
113 #define CCI_errOrdering -16
114 #define CCI_errHintmask -17
115 #define CCI_errSubrDepth -18
116 #define CCI_errLastError -18
117
118
119 /* Global variables */
120 static double cs_f_precision = 5.0;
121 static int cs_precision = 5;
122 static boolean direct_hr = false;
123 static UT_array *ut_glyphs = NULL;
124 static UT_array *ut_subrs = NULL;
125 static double default_width_x;
126 static double nominal_width_x;
127 static int hr_firstsubr = 5;
128 static cff_index *gsubr_idx;
129 static cff_index *subr_idx;
130 static card8 *t1_dst;
131 static card8 *t1_limit;
132 static long t1_dstlen;
133 static card8 *curr_t1_dst;
134 static card8 *curr_t1_limit;
135 static long curr_t1_dstlen;
136
137
138 /* Type1CharstringGenInterp variables */
139 static int status_cgi = S_CGI_INITIAL;
140 static cs_point width = {0.0, 0.0};
141 static boolean in_hr = false;
142 static double max_flex_height = 0;
143 static boolean bad_flex = false;
144 // hints and hint replacement
145 static UT_array *stem_pos = NULL;
146 static UT_array *stem_width = NULL;
147 static UT_array *stem_hstem = NULL;
148 static card8 *last_hints = NULL;
149
150
151 /* Type1CharstringGen variables */
152 static int status_chg = S_CHG_INITIAL;
153 static cs_point cs_true = {0.0, 0.0};
154 static cs_point cs_false = {0.0, 0.0};
155
156
157 /* CharstringInterp variables */
158 static int status_cci = S_CCI_INITIAL;
159 static int cs_error;
160 static int cs_error_data;
161 static boolean done;
162 static boolean careful;
163 static double cs_s[STACK_SIZE];
164 static int cs_sp;
165 static int subr_depth;
166 static double cs_trn[SCRATCH_SIZE];
167 static cs_point lsb = {0.0, 0.0};
168 static cs_point cp = {0.0, 0.0};
169 static cs_point seac_origin = {0.0, 0.0};
170 static boolean flex;
171 static int t2nhints;
172 static double double_for_error;
173
174
175 #ifdef WIN32
176 # define random() rand()
177 #endif
178
179 #define CHECK_STACK(numargs) do { if (cs_sp < numargs) ERROR("Type2 CharString Parser Error: stack underflow"); } while (0)
180 #define CHECK_STATE() do { if (status_cci < S_CCI_IPATH) ERROR("Type2 CharString Parser Error: ordering constraints violated"); } while (0)
181 #define CHECK_PATH_START() do { status_cci = S_CCI_PATH; } while (0)
182 #define CHECK_PATH_END() do { if (status_cci == S_CCI_PATH) { act_closepath(cmd); } status_cci = S_CCI_IPATH; } while (0)
183 #define CHECK_SCRATCH(idx) do { if (idx >= SCRATCH_SIZE ) ERROR("Type2 CharString Parser Error: transient array underflow"); } while (0)
184
185 #define UNKDOUBLE -9.79797e97
186 #define MIN_KNOWN_DOUBLE -9.69696e97
187 #define KNOWN(d) ((d) >= MIN_KNOWN_DOUBLE)
188
189
190 /*
191 * Type 1/2 CharString encoding
192 */
193
194 /*
195 * 1-byte CharString operators:
196 * cs_escape is first byte of two-byte operator
197 */
198
199 /* RESERVED 0 */
200 #define cs_hstem 1
201 /* RESERVED 2 */
202 #define cs_vstem 3
203 #define cs_vmoveto 4
204 #define cs_rlineto 5
205 #define cs_hlineto 6
206 #define cs_vlineto 7
207 #define cs_rrcurveto 8
208 #define cs_closepath 9 /* TYPE1 */
209 #define cs_callsubr 10
210 #define cs_return 11
211 #define cs_escape 12
212 #define cs_hsbw 13 /* TYPE1 */
213 #define cs_endchar 14
214 /* RESERVED 15 */
215 #define cs_blend 16
216 /* RESERVED 17 */
217 #define cs_hstemhm 18
218 #define cs_hintmask 19
219 #define cs_cntrmask 20
220 #define cs_rmoveto 21
221 #define cs_hmoveto 22
222 #define cs_vstemhm 23
223 #define cs_rcurveline 24
224 #define cs_rlinecurve 25
225 #define cs_vvcurveto 26
226 #define cs_hhcurveto 27
227 #define cs_shortint 28
228 #define cs_callgsubr 29
229 #define cs_vhcurveto 30
230 #define cs_hvcurveto 31
231 #define cs_escapedelta 32
232
233 /*
234 * 2-byte CharString operators:
235 * "dotsection" is obsoleted in Type 2 charstring.
236 */
237
238 #define cs_dotsection 32 + 0
239 #define cs_vstem3 32 + 1 /* TYPE1 */
240 #define cs_hstem3 32 + 2 /* TYPE1 */
241 #define cs_and 32 + 3
242 #define cs_or 32 + 4
243 #define cs_not 32 + 5
244 #define cs_seac 32 + 6 /* TYPE1 */
245 #define cs_sbw 32 + 7 /* TYPE1 */
246 #define cs_store 32 + 8
247 #define cs_abs 32 + 9
248 #define cs_add 32 + 10
249 #define cs_sub 32 + 11
250 #define cs_div 32 + 12
251 #define cs_load 32 + 13
252 #define cs_neg 32 + 14
253 #define cs_eq 32 + 15
254 #define cs_callothersubr 32 + 16 /* TYPE1 */
255 #define cs_pop 32 + 17 /* TYPE1 */
256 #define cs_drop 32 + 18
257 /* RESERVED 19 */
258 #define cs_put 32 + 20
259 #define cs_get 32 + 21
260 #define cs_ifelse 32 + 22
261 #define cs_random 32 + 23
262 #define cs_mul 32 + 24
263 /* RESERVED 25 */
264 #define cs_sqrt 32 + 26
265 #define cs_dup 32 + 27
266 #define cs_exch 32 + 28
267 #define cs_index 32 + 29
268 #define cs_roll 32 + 30
269 #define cs_setcurrentpoint 32 + 31 /* TYPE1 */
270 /* RESERVED 32 */
271 /* RESERVED 33 */
272 #define cs_hflex 32 + 34
273 #define cs_flex 32 + 35
274 #define cs_hflex1 32 + 36
275 #define cs_flex1 32 + 37
276
277
278 static void act_hintmask(int cmd, card8 *data, int nhints);
279 static boolean do_charstring (card8 **t2_data, long length);
280
281
282 static void
283 shift_point(cs_point *p, double dx, double dy)
284 {
285 p->x += dx;
286 p->y += dy;
287 }
288
289 static void
290 calc_shifted_point(cs_point *src, cs_point *dst, double dx, double dy)
291 {
292 dst->x = src->x + dx;
293 dst->y = src->y + dy;
294 }
295
296 static void
297 append_t1chr(card8 val)
298 {
299 if (curr_t1_limit >= curr_t1_dst + 1)
300 curr_t1_dst[curr_t1_dstlen++] = val;
301 else
302 ERROR("Type2 CharString Parser Error: Max possible charstring length exceeded");
303 }
304
305 static boolean s_push(double d)
306 {
307 if (cs_sp < STACK_SIZE)
308 cs_s[cs_sp++] = d;
309 else
310 ERROR("Type2 CharString Parser Error: Stack overflow");
311 return true;
312 }
313
314 static double s_pop(int n)
315 {
316 cs_sp -= n;
317 return cs_s[cs_sp];
318 }
319
320 static double s_top(int n)
321 {
322 return cs_s[cs_sp - n - 1];
323 }
324
325 static void
326 gen_rational(int big_val, int divisor)
327 {
328 int base, w;
329 long l;
330
331 int frac = big_val % divisor;
332 int val = (frac == 0 ? big_val / divisor : big_val);
333
334 if (val >= -107 && val <= 107)
335 append_t1chr((card8)(val + 139));
336
337 else if (val >= -1131 && val <= 1131) {
338 base = val < 0 ? 251 : 247;
339 if (val < 0) val = -val;
340 val -= 108;
341 w = val % 256;
342 val = (val - w) / 256;
343 append_t1chr((card8)(val + base));
344 append_t1chr((card8)w);
345
346 } else {
347 append_t1chr('\377');
348 l = val;
349 append_t1chr((card8)((l >> 24) & 0xFF));
350 append_t1chr((card8)((l >> 16) & 0xFF));
351 append_t1chr((card8)((l >> 8) & 0xFF));
352 append_t1chr((card8)((l >> 0) & 0xFF));
353 }
354
355 if (frac != 0) {
356 append_t1chr((card8)(divisor + 139));
357 append_t1chr((card8)cs_escape);
358 append_t1chr((card8)(cs_div - cs_escapedelta));
359 }
360 }
361
362 static void
363 gen_number(double float_val, int kind)
364 {
365 int big_val;
366
367 switch (kind) {
368 case 'x':
369 cs_true.x += float_val;
370 float_val = cs_true.x - cs_false.x;
371 break;
372 case 'y':
373 cs_true.y += float_val;
374 float_val = cs_true.y - cs_false.y;
375 break;
376 case 'X':
377 cs_true.x = float_val;
378 break;
379 case 'Y':
380 cs_true.y = float_val;
381 break;
382 }
383
384 // 30.Jul.2003 - Avoid rounding differences between platforms with the
385 // extra 0.00001.
386 big_val = (int)floor(float_val * cs_f_precision + 0.50001);
387
388 gen_rational(big_val, cs_precision);
389
390 float_val = big_val / cs_f_precision;
391 switch (kind) {
392 case 'x':
393 cs_false.x += float_val;
394 break;
395 case 'y':
396 cs_false.y += float_val;
397 break;
398 case 'X':
399 cs_false.x = float_val;
400 break;
401 case 'Y':
402 cs_false.y = float_val;
403 break;
404 }
405 }
406
407
408 static void
409 gen_command(int command)
410 {
411 if (command >= cs_escapedelta) {
412 append_t1chr((card8)cs_escape);
413 append_t1chr((card8)(command - cs_escapedelta));
414 if (command != cs_sbw)
415 status_chg = S_CHG_GEN;
416 } else {
417 append_t1chr((card8)command);
418 if (command > cs_vmoveto && command != cs_hsbw)
419 status_chg = S_CHG_GEN;
420 }
421 }
422
423 static void
424 gen_moveto(cs_point *p, boolean closepath, boolean always)
425 {
426 cs_point d;
427 int big_dx, big_dy;
428
429 // make sure we generate some moveto on the first command
430
431 d.x = p->x - cs_true.x;
432 d.y = p->y - cs_true.y;
433 big_dx = (int)floor(d.x * cs_f_precision + 0.50001);
434 big_dy = (int)floor(d.y * cs_f_precision + 0.50001);
435
436 if (big_dx == 0 && big_dy == 0 && status_chg != S_CHG_INITIAL && !always)
437 /* do nothing */;
438 else {
439 if (closepath)
440 gen_command(cs_closepath);
441 if (big_dy == 0) {
442 gen_number(d.x, 'x');
443 gen_command(cs_hmoveto);
444 } else if (big_dx == 0) {
445 gen_number(d.y, 'y');
446 gen_command(cs_vmoveto);
447 } else {
448 gen_number(d.x, 'x');
449 gen_number(d.y, 'y');
450 gen_command(cs_rmoveto);
451 }
452 }
453
454 cs_true = *p;
455 }
456
457
458 /*
459 * Subroutines:
460 * The bias for subroutine number is introduced in type 2 charstrings.
461 *
462 * subr: set to a pointer to the subroutine charstring.
463 * len: set to the length of subroutine charstring.
464 * subr_idx: CFF INDEX data that contains subroutines.
465 * id: biased subroutine number.
466 */
467 static void
468 get_subr (card8 **subr, long *len, cff_index *subr_idx, long id)
469 {
470 card16 count;
471
472 if (subr_idx == NULL)
473 ERROR("%s: Subroutine called but no subroutine found.", CS_TYPE2_DEBUG_STR);
474
475 count = subr_idx->count;
476
477 /* Adding bias number */
478 if (count < 1240) {
479 id += 107;
480 } else if (count < 33900) {
481 id += 1131;
482 } else {
483 id += 32768;
484 }
485
486 if (id > count)
487 ERROR("%s: Invalid Subr index: %ld (max=%u)", CS_TYPE2_DEBUG_STR, id, count);
488
489 *len = (subr_idx->offset)[id + 1] - (subr_idx->offset)[id];
490 *subr = subr_idx->data + (subr_idx->offset)[id] - 1;
491
492 return;
493 }
494
495 static void
496 act_width(int cmd, const cs_point *p)
497 {
498 width.x = p->x;
499 width.y = p->y;
500 }
501
502 static void
503 act_default_width(int cmd)
504 {
505 cs_point p;
506 double d = default_width_x;
507 if (KNOWN(d)) {
508 p.x = d;
509 p.y = 0;
510 act_width(cmd, &p);
511 }
512 }
513
514 static void
515 act_nominal_width_delta(int cmd, double delta)
516 {
517 cs_point p;
518 double d = nominal_width_x;
519 if (KNOWN(d)) {
520 p.x = d + delta;
521 p.y = 0;
522 act_width(cmd, &p);
523 }
524 }
525
526 static void
527 gen_sbw(boolean hints_follow)
528 {
529 if (!hints_follow && utarray_len(stem_hstem))
530 act_hintmask(cs_hintmask, 0, utarray_len(stem_hstem));
531 else if (lsb.y == 0 && width.y == 0) {
532 gen_number(lsb.x, 'X');
533 gen_number(width.x, 0);
534 gen_command(cs_hsbw);
535 } else {
536 gen_number(lsb.x, 'X');
537 gen_number(lsb.y, 'Y');
538 gen_number(width.x, 0);
539 gen_number(width.y, 0);
540 gen_command(cs_sbw);
541 }
542 status_cgi = S_CGI_CLOSED;
543 }
544
545
546 static long
547 gen_hints(const card8 *data, int nhints, card8 **dst)
548 {
549 card8 *stem_dst, *tmp_dst;
550 card8 *stem_limit, *tmp_limit;
551 long stem_dstlen, tmp_dstlen;
552 unsigned char mask = 0x80;
553 double offset, *d;
554 int i, *p;
555
556 tmp_dst = curr_t1_dst;
557 tmp_limit = curr_t1_limit;
558 tmp_dstlen = curr_t1_dstlen;
559
560 stem_dst = curr_t1_dst = NEW(CS_STR_LEN_MAX, card8);
561 stem_limit = curr_t1_limit = stem_dst + CS_STR_LEN_MAX;
562 stem_dstlen = curr_t1_dstlen = 0;
563
564 for (i = 0; i < nhints; i++) {
565 if (*data & mask) {
566 p = (int *)utarray_eltptr(stem_hstem,i);
567 offset = (*p ? lsb.y : lsb.x);
568 d = (double *)utarray_eltptr(stem_pos,i);
569 gen_number(*d - offset, 0);
570 d = (double *)utarray_eltptr(stem_width,i);
571 gen_number(*d, 0);
572 gen_command(*p ? cs_hstem : cs_vstem);
573 }
574 if ((mask >>= 1) == 0)
575 data++, mask = 0x80;
576 }
577
578 stem_dstlen = curr_t1_dstlen;
579 *dst = NEW(stem_dstlen,card8);
580 memcpy(*dst,stem_dst,stem_dstlen);
581 free(stem_dst);
582
583 curr_t1_dst = tmp_dst;
584 curr_t1_limit = tmp_limit;
585 curr_t1_dstlen = tmp_dstlen;
586
587 return stem_dstlen;
588 }
589
590 static void
591 act_hintmask(int cmd, card8 *data, int nhints)
592 {
593 card8 *data_holder, *hints;
594 long hints_len, j;
595 int subrno, nsubrs, i;
596 cs_type1subr *p, sb;
597
598 if (cmd == cs_cntrmask || nhints > utarray_len(stem_hstem))
599 return;
600
601 if (!data) {
602 data_holder = NEW((((nhints - 1) >> 3) + 1),card8);
603 memset(data_holder,'\377',(((nhints - 1) >> 3) + 1));
604 data = data_holder;
605 }
606 else
607 data_holder = NULL;
608
609 hints_len = gen_hints(data, nhints, &hints);
610 in_hr = false;
611
612 if (status_cgi == S_CGI_INITIAL || direct_hr) {
613 if (status_cgi == S_CGI_INITIAL)
614 gen_sbw(true);
615 for ( j=0; j<hints_len; j++ )
616 append_t1chr(hints[j]);
617 free(hints);
618 } else if (ut_subrs) {
619 hints_len++;
620 hints = RENEW(hints,hints_len,card8);
621 hints[hints_len - 1] = (card8)(cs_return);
622
623 subrno = -1, nsubrs = utarray_len(ut_subrs);
624 for (i = hr_firstsubr; i < nsubrs; i++) {
625 p = (cs_type1subr *)utarray_eltptr(ut_subrs,i);
626 if ( !p ) break;
627 if ( p->size == hints_len ) {
628 if ( memcmp(p->data,hints,hints_len) == 0 ) {
629 subrno = i;
630 break;
631 }
632 }
633 }
634
635 if (subrno < 0) {
636 sb.size = hints_len;
637 sb.data = NEW(hints_len,card8);
638 memcpy(sb.data,hints,hints_len);
639 utarray_push_back(ut_subrs,&sb);
640 subrno = nsubrs;
641 }
642 free(hints);
643
644 if (subrno >= 0) {
645 gen_number(subrno, 0);
646 gen_number(4, 0);
647 gen_command(cs_callsubr);
648 }
649 }
650 if ( data_holder )
651 free(data_holder);
652 }
653 static void
654 act_seac(int cmd, double asb, double adx, double ady, int bchar, int achar)
655 {
656 if (status_cgi == S_CGI_INITIAL)
657 gen_sbw(false);
658 gen_number(asb, 0);
659 gen_number(adx, 0);
660 gen_number(ady, 0);
661 gen_number(bchar, 0);
662 gen_number(achar, 0);
663 gen_command(cs_seac);
664 status_cgi = S_CGI_SEAC;
665 }
666
667 static void
668 swap_stem_hints()
669 {
670 utarray_clear(stem_pos);
671 utarray_clear(stem_width);
672 utarray_clear(stem_hstem);
673 in_hr = true;
674 }
675
676 static void
677 act_hstem(int cmd, double pos, double width)
678 {
679 int kind = 1;
680 if (status_cgi != S_CGI_INITIAL && !in_hr)
681 swap_stem_hints();
682 utarray_push_back(stem_pos,&pos);
683 utarray_push_back(stem_width,&width);
684 utarray_push_back(stem_hstem,&kind);
685 }
686
687 static void
688 act_vstem(int cmd, double pos, double width)
689 {
690 int kind = 0;
691 if (status_cgi != S_CGI_INITIAL && !in_hr)
692 swap_stem_hints();
693 utarray_push_back(stem_pos,&pos);
694 utarray_push_back(stem_width,&width);
695 utarray_push_back(stem_hstem,&kind);
696 }
697
698 static int
699 type2_handle_width(int cmd, boolean have_width)
700 {
701 cp.x = lsb.x = seac_origin.x;
702 cp.y = lsb.y = seac_origin.y;
703 if (status_cci != S_CCI_INITIAL)
704 /* ignore width */;
705 else if (have_width)
706 act_nominal_width_delta(cmd, cs_s[0]);
707 else
708 act_default_width(cmd);
709 return (have_width ? 1 : 0);
710 }
711
712 static void
713 act_line(int cmd, cs_point *a, cs_point *b)
714 {
715 if (status_cgi == S_CGI_INITIAL)
716 gen_sbw(false);
717 else if (in_hr)
718 act_hintmask(cmd, 0, utarray_len(stem_hstem));
719 gen_moveto(a, status_cgi == S_CGI_OPEN, false);
720 status_cgi = S_CGI_OPEN;
721 if (a->x == b->x) {
722 gen_number(b->y - a->y, 'y');
723 gen_command(cs_vlineto);
724 } else if (a->y == b->y) {
725 gen_number(b->x - a->x, 'x');
726 gen_command(cs_hlineto);
727 } else {
728 gen_number(b->x - a->x, 'x');
729 gen_number(b->y - a->y, 'y');
730 gen_command(cs_rlineto);
731 }
732 }
733
734 static void
735 act_curve(int cmd, cs_point *a, cs_point *b, cs_point *c, cs_point *d)
736 {
737 if (status_cgi == S_CGI_INITIAL)
738 gen_sbw(false);
739 else if (in_hr)
740 act_hintmask(cmd, 0,utarray_len(stem_hstem));
741 gen_moveto(a, status_cgi == S_CGI_OPEN, false);
742 status_cgi = S_CGI_OPEN;
743 if (b->y == a->y && d->x == c->x) {
744 gen_number(b->x - a->x, 'x');
745 gen_number(c->x - b->x, 'x');
746 gen_number(c->y - b->y, 'y');
747 gen_number(d->y - c->y, 'y');
748 gen_command(cs_hvcurveto);
749 } else if (b->x == a->x && d->y == c->y) {
750 gen_number(b->y - a->y, 'y');
751 gen_number(c->x - a->x, 'x');
752 gen_number(c->y - b->y, 'y');
753 gen_number(d->x - c->x, 'x');
754 gen_command(cs_vhcurveto);
755 } else {
756 gen_number(b->x - a->x, 'x');
757 gen_number(b->y - a->y, 'y');
758 gen_number(c->x - b->x, 'x');
759 gen_number(c->y - b->y, 'y');
760 gen_number(d->x - c->x, 'x');
761 gen_number(d->y - c->y, 'y');
762 gen_command(cs_rrcurveto);
763 }
764 }
765
766 static void
767 act_flex(int cmd, cs_point *p0, cs_point *p1, cs_point *p2, cs_point *p3_4, cs_point *p5,cs_point *p6, cs_point *p7, double flex_depth)
768 {
769 boolean v_ok, h_ok;
770 double distance;
771 int sign;
772 cs_point p_reference;
773 double flex_height;
774
775 if (status_cgi == S_CGI_INITIAL)
776 gen_sbw(false);
777 else if (in_hr)
778 act_hintmask(cmd, 0, utarray_len(stem_hstem));
779 gen_moveto(p0, status_cgi == S_CGI_OPEN, false);
780 status_cgi = S_CGI_OPEN;
781
782 // 1. Outer endpoints must have same x (or y) coordinate
783 v_ok = (p0->x == p7->x);
784 h_ok = (p0->y == p7->y);
785
786 // 2. Join point and its neighboring controls must be at an extreme
787 if (v_ok && p2->x == p3_4->x && p3_4->x == p5->x) {
788 distance = fabs(p3_4->x - p0->x);
789 sign = (p3_4->x < p0->x ? -1 : 1);
790 if (sign * (p1->x - p0->x) < 0 || sign * (p1->x - p0->x) > distance
791 || sign * (p6->x - p0->x) < 0 || sign * (p6->x - p0->x) > distance)
792 v_ok = false;
793 } else
794 v_ok = false;
795
796 if (h_ok && p2->y == p3_4->y && p3_4->y == p5->y) {
797 distance = fabs(p3_4->y - p0->y);
798 sign = (p3_4->y < p0->y ? -1 : 1);
799 if (sign * (p1->y - p0->y) < 0 || sign * (p1->y - p0->y) > distance
800 || sign * (p6->y - p0->y) < 0 || sign * (p6->y - p0->y) > distance)
801 h_ok = false;
802 } else
803 h_ok = false;
804
805 // 3. Flex height <= 20
806 if (v_ok && fabs(p3_4->x - p0->x) > 20)
807 v_ok = false;
808 if (h_ok && fabs(p3_4->y - p0->y) > 20)
809 h_ok = false;
810
811 // generate flex commands
812 if (v_ok || h_ok) {
813 if ( h_ok ) {
814 p_reference.x = p3_4->x;
815 p_reference.y = p0->y;
816 }
817 else {
818 p_reference.x = p0->x;
819 p_reference.y = p3_4->y;
820 }
821
822 gen_number(1, 0);
823 gen_command(cs_callsubr);
824
825 gen_moveto(&p_reference, false, true);
826 gen_number(2, 0);
827 gen_command(cs_callsubr);
828
829 gen_moveto(p1, false, true);
830 gen_number(2, 0);
831 gen_command(cs_callsubr);
832
833 gen_moveto(p2, false, true);
834 gen_number(2, 0);
835 gen_command(cs_callsubr);
836
837 gen_moveto(p3_4, false, true);
838 gen_number(2, 0);
839 gen_command(cs_callsubr);
840
841 gen_moveto(p5, false, true);
842 gen_number(2, 0);
843 gen_command(cs_callsubr);
844
845 gen_moveto(p6, false, true);
846 gen_number(2, 0);
847 gen_command(cs_callsubr);
848
849 gen_moveto(p7, false, true);
850 gen_number(2, 0);
851 gen_command(cs_callsubr);
852
853 gen_number(flex_depth, 0);
854 gen_number(p7->x, 'X');
855 gen_number(p7->y, 'Y');
856 gen_number(0, 0);
857 gen_command(cs_callsubr);
858
859 flex_height = fabs(h_ok ? p3_4->y - p0->y : p3_4->x - p0->x);
860 if (flex_height > max_flex_height)
861 max_flex_height = flex_height;
862 } else {
863 bad_flex = true;
864 act_curve(cmd, p0, p1, p2, p3_4);
865 act_curve(cmd, p3_4, p5, p6, p7);
866 }
867 }
868
869 static void
870 act_closepath(int cmd)
871 {
872 if (in_hr)
873 act_hintmask(cmd, 0, utarray_len(stem_hstem));
874 gen_command(cs_closepath);
875 status_cgi = S_CGI_CLOSED;
876 }
877
878 static void
879 actp_rmoveto(int cmd, double dx, double dy)
880 {
881 shift_point(&cp, dx, dy);
882 }
883
884 static void
885 actp_rlineto(int cmd, double dx, double dy)
886 {
887 cs_point p0 = cp;
888 shift_point(&cp, dx, dy);
889 act_line(cmd, &p0, &cp);
890 }
891
892 static void
893 actp_rrcurveto(int cmd, double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
894 {
895 cs_point p1, p2;
896 cs_point p0 = cp;
897 p1.x = p0.x + dx1;
898 p1.y = p0.y + dy1;
899 p2.x = p1.x + dx2;
900 p2.y = p1.y + dy2;
901 calc_shifted_point(&p2, &cp, dx3, dy3);
902 act_curve(cmd, &p0, &p1, &p2, &cp);
903 }
904
905 static void
906 actp_rrflex(int cmd, double dx1, double dy1, double dx2, double dy2, double dx3, double dy3, double dx4, double dy4, double dx5, double dy5, double dx6, double dy6, double flex_depth)
907 {
908 cs_point p1, p2, p3_4, p5, p6;
909 cs_point p0 = cp;
910 p1.x = p0.x + dx1;
911 p1.y = p0.y + dy1;
912 p2.x = p1.x + dx2;
913 p2.y = p1.y + dy2;
914 p3_4.x = p2.x + dx3;
915 p3_4.y = p2.y + dy3;
916 p5.x = p3_4.x + dx4;
917 p5.y = p3_4.y + dy4;
918 p6.x = p5.x + dx5;
919 p6.y = p5.y + dy5;
920 calc_shifted_point(&p6, &cp, dx6, dy6);
921 act_flex(cmd, &p0, &p1, &p2, &p3_4, &p5, &p6, &cp, flex_depth);
922 }
923
924 static boolean
925 vector_command(int cmd)
926 {
927 int vectoroff, offset, num, i;
928 double v;
929
930 switch (cmd) {
931
932 case cs_put:
933 CHECK_STACK(2);
934 offset = (int)cs_s[cs_sp - 1];
935 CHECK_SCRATCH(offset);
936 cs_trn[offset] = cs_s[cs_sp - 2];
937 cs_sp -= 2;
938 break;
939
940 case cs_get:
941 CHECK_STACK(1);
942 offset = (int)cs_s[cs_sp - 1];
943 CHECK_SCRATCH(offset);
944 cs_s[cs_sp - 1] = cs_trn[offset];
945 break;
946
947 default:
948 ERROR("Type2 CharString Parser Error: unimplemented command found");
949
950 }
951
952 return true;
953 }
954
955 static boolean
956 roll_command()
957 {
958 int amount, n, i, base;
959 double copy_stack[STACK_SIZE];
960
961 CHECK_STACK(2);
962 amount = (int)s_pop(1);
963 n = (int)s_pop(1);
964 if (n <= 0)
965 ERROR("Type2 CharString Parser Error: stack underflow");
966 CHECK_STACK(n);
967
968 base = cs_sp - n;
969 while (amount < 0)
970 amount += n;
971
972 for (i = 0; i < n; i++)
973 copy_stack[i] = cs_s[ base + (i+amount) % n ];
974 for (i = 0; i < n; i++)
975 cs_s[base + i] = copy_stack[i];
976
977 return true;
978 }
979
980 static boolean
981 arith_command(int cmd)
982 {
983 int i;
984 double d;
985
986 switch (cmd) {
987
988 case cs_abs:
989 CHECK_STACK(1);
990 if (s_top(0) < 0)
991 cs_s[cs_sp - 1] = -s_top(0);
992 break;
993
994 case cs_add:
995 CHECK_STACK(1);
996 d = s_pop(1);
997 cs_s[cs_sp - 1] += d;
998 break;
999
1000 case cs_sub:
1001 CHECK_STACK(1);
1002 d = s_pop(1);
1003 cs_s[cs_sp - 1] -= d;
1004 break;
1005
1006 case cs_div:
1007 CHECK_STACK(2);
1008 d = s_pop(1);
1009 cs_s[cs_sp - 1] /= d;
1010 break;
1011
1012 case cs_neg:
1013 CHECK_STACK(1);
1014 cs_s[cs_sp - 1] = -s_top(0);
1015 break;
1016
1017 case cs_random: {
1018 double d;
1019 do {
1020 d = random() / ((double)RAND_MAX);
1021 } while (d == 0);
1022 s_push(d);
1023 break;
1024 }
1025
1026 case cs_mul:
1027 CHECK_STACK(2);
1028 d = s_pop(1);
1029 cs_s[cs_sp - 1] *= d;
1030 break;
1031
1032 case cs_sqrt:
1033 CHECK_STACK(1);
1034 if (s_top(0) < 0)
1035 ERROR("Type2 CharString Parser Error: bad value");
1036 cs_s[cs_sp - 1] = sqrt(s_top(0));
1037 break;
1038
1039 case cs_drop:
1040 CHECK_STACK(1);
1041 s_pop(1);
1042 break;
1043
1044 case cs_exch:
1045 CHECK_STACK(2);
1046 d = s_top(0);
1047 cs_s[cs_sp - 1] = s_top(1);
1048 cs_s[cs_sp - 2] = d;
1049 break;
1050
1051 case cs_index:
1052 CHECK_STACK(1);
1053 i = (int)s_top(0);
1054 if (i < 0)
1055 ERROR("Type2 CharString Parser Error: bad value");
1056 CHECK_STACK(i + 2);
1057 cs_s[cs_sp - 1] = s_top(i+1);
1058 break;
1059
1060 case cs_roll:
1061 return roll_command();
1062
1063 case cs_dup:
1064 CHECK_STACK(1);
1065 s_push(s_top(0));
1066 break;
1067
1068 case cs_and:
1069 CHECK_STACK(2);
1070 d = s_pop(1);
1071 cs_s[cs_sp - 1] = (s_top(0) != 0) && (d != 0);
1072 break;
1073
1074 case cs_or:
1075 CHECK_STACK(2);
1076 d = s_pop(1);
1077 cs_s[cs_sp - 1] = (s_top(0) != 0) || (d != 0);
1078 break;
1079
1080 case cs_not:
1081 CHECK_STACK(1);
1082 cs_s[cs_sp - 1] = (s_top(0) == 0);
1083 break;
1084
1085 case cs_eq:
1086 CHECK_STACK(2);
1087 d = s_pop(1);
1088 cs_s[cs_sp - 1] = (s_top(0) == d);
1089 break;
1090
1091 case cs_ifelse:
1092 CHECK_STACK(4);
1093 if (s_top(1) > s_top(0))
1094 cs_s[cs_sp - 4] = cs_s[cs_sp - 3];
1095 s_pop(3);
1096 break;
1097
1098 default:
1099 ERROR("Type2 CharString Parser Error: unimplemented command found");
1100
1101 }
1102
1103 return true;
1104 }
1105
1106 static boolean
1107 callsubr_command()
1108 {
1109 int which;
1110 card8 *subr_cs;
1111 long subr_len;
1112 boolean tmp_done, loc_done;
1113
1114 CHECK_STACK(1);
1115 which = (int)s_pop(1);
1116
1117 subr_cs = NULL;
1118 get_subr(&subr_cs, &subr_len, subr_idx, which);
1119 if (!subr_cs)
1120 ERROR("Type2 CharString Parser Error: bad subroutine number %d",which);
1121
1122 if (subr_depth >= MAX_SUBR_DEPTH)
1123 ERROR("Type2 CharString Parser Error: subrs nested too deep at '%d'",which);
1124 subr_depth++;
1125
1126 tmp_done = done;
1127 done = false;
1128 do_charstring(&subr_cs,subr_len);
1129 loc_done = done;
1130 done = tmp_done;
1131
1132 subr_depth--;
1133 if (cs_error != CCI_errOK)
1134 return false;
1135 return !loc_done;
1136 }
1137
1138 static boolean
1139 callgsubr_command()
1140 {
1141 int which;
1142 card8 *subr_cs;
1143 long subr_len;
1144 boolean tmp_done, loc_done;
1145
1146 CHECK_STACK(1);
1147 which = (int)s_pop(1);
1148
1149 subr_cs = NULL;
1150 get_subr(&subr_cs, &subr_len, gsubr_idx, which);
1151 if (!subr_cs)
1152 ERROR("Type2 CharString Parser Error: bad subroutine number %d",which);
1153
1154 if (subr_depth >= MAX_SUBR_DEPTH)
1155 ERROR("Type2 CharString Parser Error: subrs nested too deep at '%d'",which);
1156 subr_depth++;
1157
1158 tmp_done = done;
1159 done = false;
1160 do_charstring(&subr_cs,subr_len);
1161 loc_done = done;
1162 done = tmp_done;
1163
1164 subr_depth--;
1165 if (cs_error != CCI_errOK)
1166 return false;
1167 return !loc_done;
1168 }
1169
1170 static boolean
1171 type2_command(int cmd, card8 *data, int *left)
1172 {
1173 double pos, dx, dy;
1174 int bottom = 0;
1175
1176 switch (cmd) {
1177
1178 case cs_hstem:
1179 case cs_hstemhm:
1180 CHECK_STACK(2);
1181 if (status_cci <= S_CCI_SEAC)
1182 bottom = type2_handle_width(cmd, (cs_sp % 2) == 1);
1183 if (status_cci > S_CCI_HSTEM)
1184 ERROR("Type2 CharString Parser Error: ordering constraints violated");
1185 status_cci = S_CCI_HSTEM;
1186 for (pos = 0; bottom + 1 < cs_sp; bottom += 2) {
1187 t2nhints++;
1188 act_hstem(cmd, pos + cs_s[bottom], cs_s[bottom + 1]);
1189 pos += cs_s[bottom] + cs_s[bottom + 1];
1190 }
1191 break;
1192
1193 case cs_vstem:
1194 case cs_vstemhm:
1195 CHECK_STACK(2);
1196 if (status_cci <= S_CCI_SEAC)
1197 bottom = type2_handle_width(cmd, (cs_sp % 2) == 1);
1198 if (status_cci > S_CCI_VSTEM)
1199 ERROR("Type2 CharString Parser Error: ordering constraints violated");
1200 status_cci = S_CCI_VSTEM;
1201 for (pos = 0; bottom + 1 < cs_sp; bottom += 2) {
1202 t2nhints++;
1203 act_vstem(cmd, pos + cs_s[bottom], cs_s[bottom + 1]);
1204 pos += cs_s[bottom] + cs_s[bottom + 1];
1205 }
1206 break;
1207
1208 case cs_hintmask:
1209 case cs_cntrmask:
1210 if (status_cci <= S_CCI_SEAC && cs_sp >= 1) {
1211 bottom = type2_handle_width(cmd, (cs_sp % 2) == 1);
1212 for (pos = 0; bottom + 1 < cs_sp; bottom += 2) {
1213 t2nhints++;
1214 act_hstem(cmd, pos + cs_s[bottom], cs_s[bottom + 1]);
1215 pos += cs_s[bottom] + cs_s[bottom + 1];
1216 }
1217 }
1218 if ((status_cci == S_CCI_HSTEM || status_cci == S_CCI_VSTEM) && cs_sp >= 2)
1219 for (pos = 0; bottom + 1 < cs_sp; bottom += 2) {
1220 t2nhints++;
1221 act_vstem(cmd, pos + cs_s[bottom], cs_s[bottom + 1]);
1222 pos += cs_s[bottom] + cs_s[bottom + 1];
1223 }
1224 if (status_cci < S_CCI_HINTMASK)
1225 status_cci = S_CCI_HINTMASK;
1226 if (t2nhints == 0)
1227 ERROR("Type2 CharString Parser Error: inappropriate hintmask");
1228 if (!data || !left)
1229 ERROR("Type2 CharString Parser Error: internal error");
1230 if (((t2nhints - 1) >> 3) + 1 > *left)
1231 ERROR("Type2 CharString Parser Error: commands past end");
1232 act_hintmask(cmd, data, t2nhints);
1233 *left -= ((t2nhints - 1) >> 3) + 1;
1234 break;
1235
1236 case cs_rmoveto:
1237 CHECK_STACK(2);
1238 if (status_cci <= S_CCI_SEAC)
1239 bottom = type2_handle_width(cmd, cs_sp > 2);
1240 CHECK_PATH_END();
1241 actp_rmoveto(cmd, cs_s[bottom], cs_s[bottom + 1]);
1242 break;
1243
1244 case cs_hmoveto:
1245 CHECK_STACK(1);
1246 if (status_cci <= S_CCI_SEAC)
1247 bottom = type2_handle_width(cmd, cs_sp > 1);
1248 CHECK_PATH_END();
1249 actp_rmoveto(cmd, cs_s[bottom], 0);
1250 break;
1251
1252 case cs_vmoveto:
1253 CHECK_STACK(1);
1254 if (status_cci <= S_CCI_SEAC)
1255 bottom = type2_handle_width(cmd, cs_sp > 1);
1256 CHECK_PATH_END();
1257 actp_rmoveto(cmd, 0, cs_s[bottom]);
1258 break;
1259
1260 case cs_rlineto:
1261 CHECK_STACK(2);
1262 CHECK_STATE();
1263 CHECK_PATH_START();
1264 for (; bottom + 1 < cs_sp; bottom += 2)
1265 actp_rlineto(cmd, cs_s[bottom], cs_s[bottom + 1]);
1266 break;
1267
1268 case cs_hlineto:
1269 CHECK_STACK(1);
1270 CHECK_STATE();
1271 CHECK_PATH_START();
1272 while (bottom < cs_sp) {
1273 actp_rlineto(cmd, cs_s[bottom++], 0);
1274 if (bottom < cs_sp)
1275 actp_rlineto(cmd, 0, cs_s[bottom++]);
1276 }
1277 break;
1278
1279 case cs_vlineto:
1280 CHECK_STACK(1);
1281 CHECK_STATE();
1282 CHECK_PATH_START();
1283 while (bottom < cs_sp) {
1284 actp_rlineto(cmd, 0, cs_s[bottom++]);
1285 if (bottom < cs_sp)
1286 actp_rlineto(cmd, cs_s[bottom++], 0);
1287 }
1288 break;
1289
1290 case cs_rrcurveto:
1291 CHECK_STACK(6);
1292 CHECK_STATE();
1293 CHECK_PATH_START();
1294 for (; bottom + 5 < cs_sp; bottom += 6)
1295 actp_rrcurveto(cmd, cs_s[bottom], cs_s[bottom + 1], cs_s[bottom + 2], cs_s[bottom + 3], cs_s[bottom + 4], cs_s[bottom + 5]);
1296 break;
1297
1298 case cs_hhcurveto:
1299 CHECK_STACK(4);
1300 CHECK_STATE();
1301 CHECK_PATH_START();
1302 if (cs_sp % 2 == 1) {
1303 actp_rrcurveto(cmd, cs_s[bottom + 1], cs_s[bottom], cs_s[bottom + 2], cs_s[bottom + 3], cs_s[bottom + 4], 0);
1304 bottom += 5;
1305 }
1306 for (; bottom + 3 < cs_sp; bottom += 4)
1307 actp_rrcurveto(cmd, cs_s[bottom], 0, cs_s[bottom + 1], cs_s[bottom + 2], cs_s[bottom + 3], 0);
1308 break;
1309
1310 case cs_hvcurveto:
1311 CHECK_STACK(4);
1312 CHECK_STATE();
1313 CHECK_PATH_START();
1314 while (bottom + 3 < cs_sp) {
1315 double dx3 = (bottom + 5 == cs_sp ? cs_s[bottom + 4] : 0);
1316 actp_rrcurveto(cmd, cs_s[bottom], 0, cs_s[bottom + 1], cs_s[bottom + 2], dx3, cs_s[bottom + 3]);
1317 bottom += 4;
1318 if (bottom + 3 < cs_sp) {
1319 double dy3 = (bottom + 5 == cs_sp ? cs_s[bottom + 4] : 0);
1320 actp_rrcurveto(cmd, 0, cs_s[bottom], cs_s[bottom + 1], cs_s[bottom + 2], cs_s[bottom + 3], dy3);
1321 bottom += 4;
1322 }
1323 }
1324 break;
1325
1326 case cs_rcurveline:
1327 CHECK_STACK(8);
1328 CHECK_STATE();
1329 CHECK_PATH_START();
1330 for (; bottom + 7 < cs_sp; bottom += 6)
1331 actp_rrcurveto(cmd, cs_s[bottom], cs_s[bottom + 1], cs_s[bottom + 2], cs_s[bottom + 3], cs_s[bottom + 4], cs_s[bottom + 5]);
1332 actp_rlineto(cmd, cs_s[bottom], cs_s[bottom + 1]);
1333 break;
1334
1335 case cs_rlinecurve:
1336 CHECK_STACK(8);
1337 CHECK_STATE();
1338 CHECK_PATH_START();
1339 for (; bottom + 7 < cs_sp; bottom += 2)
1340 actp_rlineto(cmd, cs_s[bottom], cs_s[bottom + 1]);
1341 actp_rrcurveto(cmd, cs_s[bottom], cs_s[bottom + 1], cs_s[bottom + 2], cs_s[bottom + 3], cs_s[bottom + 4], cs_s[bottom + 5]);
1342 break;
1343
1344 case cs_vhcurveto:
1345 CHECK_STACK(4);
1346 CHECK_STATE();
1347 CHECK_PATH_START();
1348 while (bottom + 3 < cs_sp) {
1349 double dy3 = (bottom + 5 == cs_sp ? cs_s[bottom + 4] : 0);
1350 actp_rrcurveto(cmd, 0, cs_s[bottom], cs_s[bottom + 1], cs_s[bottom + 2], cs_s[bottom + 3], dy3);
1351 bottom += 4;
1352 if (bottom + 3 < cs_sp) {
1353 double dx3 = (bottom + 5 == cs_sp ? cs_s[bottom + 4] : 0);
1354 actp_rrcurveto(cmd, cs_s[bottom], 0, cs_s[bottom + 1], cs_s[bottom + 2], dx3, cs_s[bottom + 3]);
1355 bottom += 4;
1356 }
1357 }
1358 break;
1359
1360 case cs_vvcurveto:
1361 CHECK_STACK(4);
1362 CHECK_STATE();
1363 CHECK_PATH_START();
1364 if (cs_sp % 2 == 1) {
1365 actp_rrcurveto(cmd, cs_s[bottom], cs_s[bottom + 1], cs_s[bottom + 2], cs_s[bottom + 3], 0, cs_s[bottom + 4]);
1366 bottom += 5;
1367 }
1368 for (; bottom + 3 < cs_sp; bottom += 4)
1369 actp_rrcurveto(cmd, 0, cs_s[bottom], cs_s[bottom + 1], cs_s[bottom + 2], 0, cs_s[bottom + 3]);
1370 break;
1371
1372 case cs_flex:
1373 CHECK_STACK(13);
1374 CHECK_STATE();
1375 CHECK_PATH_START();
1376 assert(bottom == 0);
1377 actp_rrflex(cmd,
1378 cs_s[0], cs_s[1], cs_s[2], cs_s[3], cs_s[4], cs_s[5],
1379 cs_s[6], cs_s[7], cs_s[8], cs_s[9], cs_s[10], cs_s[11],
1380 cs_s[12]);
1381 break;
1382
1383 case cs_hflex:
1384 CHECK_STACK(7);
1385 CHECK_STATE();
1386 CHECK_PATH_START();
1387 assert(bottom == 0);
1388 actp_rrflex(cmd,
1389 cs_s[0], 0, cs_s[1], cs_s[2], cs_s[3], 0,
1390 cs_s[4], 0, cs_s[5], -cs_s[2], cs_s[6], 0,
1391 50);
1392 break;
1393
1394 case cs_hflex1:
1395 CHECK_STACK(9);
1396 CHECK_STATE();
1397 CHECK_PATH_START();
1398 assert(bottom == 0);
1399 actp_rrflex(cmd,
1400 cs_s[0], cs_s[1], cs_s[2], cs_s[3], cs_s[4], 0,
1401 cs_s[5], 0, cs_s[6], cs_s[7], cs_s[8], -(cs_s[1] + cs_s[3] + cs_s[7]),
1402 50);
1403 break;
1404
1405 case cs_flex1: {
1406 CHECK_STACK(11);
1407 CHECK_STATE();
1408 CHECK_PATH_START();
1409 assert(bottom == 0);
1410 dx = cs_s[0] + cs_s[2] + cs_s[4] + cs_s[6] + cs_s[8];
1411 dy = cs_s[1] + cs_s[3] + cs_s[5] + cs_s[7] + cs_s[9];
1412 if (fabs(dx) > fabs(dy))
1413 actp_rrflex(cmd,
1414 cs_s[0], cs_s[1], cs_s[2], cs_s[3], cs_s[4], cs_s[5],
1415 cs_s[6], cs_s[7], cs_s[8], cs_s[9], cs_s[10], -dy,
1416 50);
1417 else
1418 actp_rrflex(cmd,
1419 cs_s[0], cs_s[1], cs_s[2], cs_s[3], cs_s[4], cs_s[5],
1420 cs_s[6], cs_s[7], cs_s[8], cs_s[9], -dx, cs_s[10],
1421 50);
1422 break;
1423 }
1424
1425 case cs_endchar:
1426 if (status_cci <= S_CCI_SEAC)
1427 bottom = type2_handle_width(cmd, cs_sp > 0 && cs_sp != 4);
1428 if (bottom + 3 < cs_sp && status_cci == S_CCI_INITIAL)
1429 act_seac(cmd, 0, cs_s[bottom], cs_s[bottom + 1], (int)cs_s[bottom + 2], (int)cs_s[bottom + 3]);
1430 CHECK_PATH_END();
1431 done = true;
1432 cs_sp = 0;
1433 return false;
1434
1435 case cs_return:
1436 return false;
1437
1438 case cs_callsubr:
1439 return callsubr_command();
1440
1441 case cs_callgsubr:
1442 return callgsubr_command();
1443
1444 case cs_put:
1445 case cs_get:
1446 return vector_command(cmd);
1447
1448 case cs_abs:
1449 case cs_add:
1450 case cs_sub:
1451 case cs_div:
1452 case cs_neg:
1453 case cs_random:
1454 case cs_mul:
1455 case cs_sqrt:
1456 case cs_drop:
1457 case cs_exch:
1458 case cs_index:
1459 case cs_roll:
1460 case cs_dup:
1461 case cs_and:
1462 case cs_or:
1463 case cs_not:
1464 case cs_eq:
1465 case cs_ifelse:
1466 return arith_command(cmd);
1467
1468 case cs_dotsection:
1469 break;
1470
1471 default:
1472 ERROR("Type2 CharString Parser Error: unimplemented command found");
1473
1474 }
1475
1476 cs_sp = 0;
1477 return cs_error >= 0;
1478 }
1479
1480 static boolean
1481 do_charstring (card8 **t2_data, long length)
1482 {
1483 card8 *data = *t2_data;
1484 int left = length;
1485
1486 while (left > 0) {
1487 boolean more;
1488 int ahead, val;
1489 int16_t val16;
1490 int32_t val32;
1491 int left_ptr;
1492
1493 if (*data >= 32 && *data <= 246) { // push small number
1494 more = s_push(data[0] - 139);
1495 ahead = 1;
1496
1497 } else if (*data < 32) { // a command
1498 if (*data == cs_escape) {
1499 if (left < 2)
1500 goto runoff_error;
1501 more = type2_command(cs_escapedelta + data[1], 0, 0);
1502 ahead = 2;
1503 if (more && (left - ahead <= 0))
1504 more = 0;
1505 } else if (*data == cs_shortint) { // short integer
1506 if (left < 3)
1507 goto runoff_error;
1508 val16 = (data[1] << 8) | data[2];
1509 more = s_push(val16);
1510 ahead = 3;
1511 } else if (*data == cs_hintmask || *data == cs_cntrmask) {
1512 left_ptr = left - 1;
1513 more = type2_command(data[0], data + 1, &left_ptr);
1514 ahead = 1 + (left - 1) - left_ptr;
1515 if (more && (left - ahead <= 0))
1516 more = 0;
1517 }
1518 else {
1519 more = type2_command(data[0], 0, 0);
1520 ahead = 1;
1521 if (more && (left - ahead <= 0))
1522 more = 0;
1523 }
1524
1525 } else if (*data >= 247 && *data <= 250) { // push medium number
1526 if (left < 2)
1527 goto runoff_error;
1528 val = + ((data[0] - 247) << 8) + 108 + data[1];
1529 more = s_push(val);
1530 ahead = 2;
1531
1532 } else if (*data >= 251 && *data <= 254) { // push negative medium number
1533 if (left < 2)
1534 goto runoff_error;
1535 val = - ((data[0] - 251) << 8) - 108 - data[1];
1536 more = s_push(val);
1537 ahead = 2;
1538
1539 } else { // 255: push huge number
1540 if (left < 5)
1541 goto runoff_error;
1542 val32 = (data[1] << 24) | (data[2] << 16) | (data[3] << 8) | data[4];
1543 more = s_push(val32 / 65536.);
1544 ahead = 5;
1545 }
1546
1547 if (!more)
1548 return true;
1549
1550 data += ahead;
1551 left -= ahead;
1552 }
1553
1554 runoff_error:
1555 ERROR("Type2 CharString Parser Error: charstring commands past end");
1556 return false;
1557 }
1558
1559 static void
1560 cs_parse_init (void)
1561 {
1562 cs_true.x = cs_false.x = 0;
1563 cs_true.y = cs_false.y = 0;
1564 status_chg = S_CHG_INITIAL;
1565
1566 cs_sp = 0;
1567 lsb.x = cp.x = seac_origin.x = 0;
1568 lsb.y = cp.y = seac_origin.y = 0;
1569 status_cci = S_CCI_INITIAL;
1570 flex = false;
1571 t2nhints = 0;
1572 subr_depth = 0;
1573 done = false;
1574 cs_error = CCI_errOK;
1575 }
1576
1577 long
1578 cs_convert_charstring (card8 *src, long srclen,
1579 cff_index *gsubr, cff_index *subr,
1580 double default_width, double nominal_width)
1581 {
1582 cs_type1subr chr;
1583
1584 t1_dst = curr_t1_dst = NEW(CS_STR_LEN_MAX, card8);
1585 t1_limit = curr_t1_limit = t1_dst + CS_STR_LEN_MAX;
1586 t1_dstlen = curr_t1_dstlen = 0;
1587
1588 gsubr_idx = gsubr;
1589 subr_idx = subr;
1590 default_width_x = default_width;
1591 nominal_width_x = nominal_width;
1592
1593 cs_parse_init();
1594
1595 width.x = 0.0;
1596 width.y = 0.0;
1597 swap_stem_hints();
1598 status_cgi = S_CGI_INITIAL;
1599 in_hr = false;
1600
1601 /* expand call(g)subrs */
1602 do_charstring(&src, srclen);
1603
1604 if (status_cgi == S_CGI_INITIAL)
1605 gen_sbw(false);
1606 else if (in_hr)
1607 act_hintmask(cs_endchar, 0, utarray_len(stem_hstem));
1608 if (status_cgi != S_CGI_SEAC)
1609 gen_command(cs_endchar);
1610
1611 t1_dstlen = curr_t1_dstlen;
1612 chr.size = t1_dstlen;
1613 chr.data = NEW(t1_dstlen,card8);
1614 memcpy(chr.data,t1_dst,t1_dstlen);
1615 utarray_push_back(ut_glyphs,&chr);
1616 free(t1_dst);
1617
1618 return t1_dstlen;
1619 }
1620
1621 void
1622 cs_start_conversion(int precision, boolean makeSubrs,
1623 UT_array *t1_glyphs, UT_array *t1_subrs)
1624 {
1625 if (precision >= 1 && precision <= 107)
1626 cs_precision = precision;
1627 else
1628 cs_precision = 5;
1629 cs_f_precision = cs_precision;
1630 hr_firstsubr = utarray_len(t1_subrs);
1631 direct_hr = !makeSubrs;
1632 ut_glyphs = t1_glyphs;
1633 ut_subrs = t1_subrs;
1634
1635 in_hr = false;
1636 max_flex_height = 0;
1637 bad_flex = false;
1638 last_hints = NULL;
1639 utarray_new(stem_pos,&double_icd);
1640 utarray_new(stem_width,&double_icd);
1641 utarray_new(stem_hstem,&ut_int_icd);
1642 }
1643
1644 void
1645 cs_end_conversion()
1646 {
1647 utarray_free(stem_pos);
1648 utarray_free(stem_width);
1649 utarray_free(stem_hstem);
1650 }