Debian Codegraph @

texk/dvipdfm-x/t1_char.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 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <math.h>
29
30 #include "system.h"
31 #include "mem.h"
32 #include "error.h"
33 #include "numbers.h"
34
35 #include "pdfobj.h"
36 #include "pdffont.h"
37
38 #include "pdfencoding.h"
39 #include "unicode.h"
40
41 #include "dpxutil.h"
42
43 #include "pst_obj.h"
44 #include "pst.h"
45
46 #include "cff_limits.h"
47 #include "cff_types.h"
48 #include "cff_dict.h"
49 #include "cff.h"
50
51 #include "t1_char.h"
52
53 /*
54 * Type 1 -> Type 1C
55 */
56
57 /* Charstring decoder/encoder status codes */
58 #define CS_OP_NOSUPPORT -4
59 #define CS_BUFFER_ERROR -3
60 #define CS_STACK_ERROR -2
61 #define CS_PARSE_ERROR -1
62 #define CS_PARSE_OK 0
63 #define CS_PARSE_END 1
64 #define CS_SUBR_RETURN 2
65 #define CS_CHAR_END 3
66
67 static int status = CS_PARSE_ERROR;
68
69 #define DST_NEED(a,b) {if ((a) < (b)) { status = CS_BUFFER_ERROR ; return ; }}
70 #define SRC_NEED(a,b) {if ((a) < (b)) { status = CS_PARSE_ERROR ; return ; }}
71 #define NEED(a,b) {if ((a) < (b)) { status = CS_STACK_ERROR ; return ; }}
72
73 #define T1_CS_PHASE_INIT 0
74 #define T1_CS_PHASE_HINT 1
75 #define T1_CS_PHASE_PATH 2
76 #define T1_CS_PHASE_FLEX 3
77
78 static int phase = -1;
79 static int nest = -1;
80
81 #ifndef CS_STEM_ZONE_MAX
82 #define CS_STEM_ZONE_MAX 96
83 #endif
84 #ifndef CS_STEM_GROUP_MAX
85 #define CS_STEM_GROUP_MAX CS_STEM_ZONE_MAX
86 #endif
87 #ifndef CS_ARG_STACK_MAX
88 #define CS_ARG_STACK_MAX 48
89 #endif
90 #ifndef PS_ARG_STACK_MAX
91 /*
92 * Counter control may have CS_STEM_ZONE_MAX*2+2 arguments.
93 */
94 #define PS_ARG_STACK_MAX (CS_STEM_ZONE_MAX*2+2)
95 #endif
96
97 typedef struct t1_cpath {
98 int type;
99 int num_args;
100 double args[CS_ARG_STACK_MAX];
101 struct t1_cpath *next;
102 } t1_cpath;
103
104 #define HSTEM 0
105 #define VSTEM 1
106 typedef struct {
107 int id;
108 int dir;
109 double pos, del;
110 } t1_stem;
111
112 typedef struct {
113 int num_stems;
114 double stems[CS_STEM_ZONE_MAX]; /* int */
115 } t1_stemgroup;
116
117 #define T1_CS_FLAG_NONE 0
118 #define T1_CS_FLAG_USE_HINTMASK (1 << 0)
119 #define T1_CS_FLAG_USE_CNTRMASK (1 << 1)
120 #define T1_CS_FLAG_USE_SEAC (1 << 2)
121
122 typedef struct {
123 int flags;
124 struct {
125 double sbx, sby, wx, wy;
126 } sbw;
127 struct {
128 double llx, lly, urx, ury;
129 } bbox;
130 struct {
131 double asb, adx, ady;
132 card8 bchar, achar;
133 } seac;
134 int num_stems;
135 t1_stem stems[CS_STEM_ZONE_MAX];
136 t1_cpath *charpath;
137 t1_cpath *lastpath;
138 } t1_chardesc;
139
140 static int cs_stack_top = 0;
141 static int ps_stack_top = 0;
142
143 /* [vh]stem support require one more stack size. */
144 static double cs_arg_stack[CS_ARG_STACK_MAX+1];
145 static double ps_arg_stack[PS_ARG_STACK_MAX];
146
147 #define CS_HINT_DECL -1
148 #define CS_FLEX_CTRL -2
149 #define CS_CNTR_CTRL -3
150
151 /*
152 * Type 1/2 CharString encoding
153 */
154
155 /*
156 * 1-byte CharString operaotrs:
157 * cs_escape is first byte of two-byte operator
158 */
159
160 /* RESERVED 0 */
161 #define cs_hstem 1
162 /* RESERVED 2 */
163 #define cs_vstem 3
164 #define cs_vmoveto 4
165 #define cs_rlineto 5
166 #define cs_hlineto 6
167 #define cs_vlineto 7
168 #define cs_rrcurveto 8
169 #define cs_closepath 9
170 #define cs_callsubr 10
171 #define cs_return 11
172 #define cs_escape 12
173 #define cs_hsbw 13
174 #define cs_endchar 14
175 /* RESERVED 15 */
176 /* RESERVED 16 */
177 /* RESERVED 17 */
178 #define cs_hstemhm 18
179 #define cs_hintmask 19
180 #define cs_cntrmask 20
181 #define cs_rmoveto 21
182 #define cs_hmoveto 22
183 #define cs_vstemhm 23
184 #define cs_rcurveline 24
185 #define cs_rlinecurve 25
186 #define cs_vvcurveto 26
187 #define cs_hhcurveto 27
188 /* SHORTINT 28 : first byte of shortint*/
189 #define cs_callgsubr 29
190 #define cs_vhcurveto 30
191 #define cs_hvcurveto 31
192
193 /* 2-byte op. flex 34-37 used. */
194
195 /*
196 * 2-byte CharString operaotrs:
197 * "dotsection" is obsoleted in Type 2 charstring.
198 */
199
200 #define cs_dotsection 0
201 #define cs_vstem3 1
202 #define cs_hstem3 2
203 #define cs_and 3
204 #define cs_or 4
205 #define cs_not 5
206 #define cs_seac 6
207 #define cs_sbw 7
208 /* RESERVED 8 */
209 #define cs_abs 9
210 #define cs_add 10
211 #define cs_sub 11
212 #define cs_div 12
213 /* RESERVED 13 */
214 #define cs_neg 14
215 #define cs_eq 15
216 #define cs_callothersubr 16
217 #define cs_pop 17
218 #define cs_drop 18
219 /* RESERVED 19 */
220 #define cs_put 20
221 #define cs_get 21
222 #define cs_ifelse 22
223 #define cs_random 23
224 #define cs_mul 24
225 /* RESERVED 25 */
226 #define cs_sqrt 26
227 #define cs_dup 27
228 #define cs_exch 28
229 #define cs_index 29
230 #define cs_roll 30
231 /* RESERVED 31 */
232 /* RESERVED 32 */
233 #define cs_setcurrentpoint 33
234 #define cs_hflex 34
235 #define cs_flex 35
236 #define cs_hflex1 36
237 #define cs_flex1 37
238
239 #define IS_PATH_OPERATOR(o) (((o) >= cs_vmoveto && (o) <= cs_closepath) || \
240 ((o) >= cs_rmoveto && (o) <= cs_hvcurveto && \
241 (o) != cs_vstemhm && (o) != cs_callgsubr && (o) != 28)\
242 )
243
244 /*
245 * Stem:
246 *
247 * 1. Stems must be sorted in the increasing bottom/left edge order.
248 * 2. The encoded values are all relative; The value x(y) of the first
249 * stem is relative to 0 for Type 2 charstring and is relative to
250 * the left(bottom) side-bearing for Type 1 charstring.
251 * 3. A width of -20(-21) specifies the top/right(bottom/left) edge
252 * of an edge hint in Type 2 charstring. But the width of 'ghost'
253 * hint in Type 1 charstring is positive with value 20 or 21.
254 * 4. The h(v)stemhm MUST be used instead of h(v)stem if charstring
255 * contains hintmask operator.
256 *
257 * TODO:
258 *
259 * Convert ghost hint to edge hint, Counter control for hstem3/vstem3.
260 */
261
262 static inline int
263 stem_compare (const void *v1, const void *v2)
264 {
265 int cmp = 0;
266 const t1_stem *s1, *s2;
267
268 s1 = (const t1_stem *) v1;
269 s2 = (const t1_stem *) v2;
270 if (s1->dir == s2->dir) {
271 if (s1->pos == s2->pos) {
272 if (s1->del == s2->del)
273 cmp = 0;
274 else
275 cmp = (s1->del < s2->del) ? -1 : 1;
276 } else {
277 cmp = (s1->pos < s2->pos) ? -1 : 1;
278 }
279 } else {
280 cmp = (s1->dir == HSTEM) ? -1 : 1;
281 }
282
283 return cmp;
284 }
285
286 #define SORT_STEMS(cd) qsort((cd)->stems,(cd)->num_stems,sizeof(t1_stem),stem_compare)
287
288 static int
289 get_stem (t1_chardesc *cd, int stem_id)
290 {
291 int i;
292
293 for (i = 0; i < cd->num_stems; i++) {
294 if (cd->stems[i].id == stem_id)
295 break;
296 }
297
298 return ((i < cd->num_stems) ? i : -1);
299 }
300
301 static int
302 add_stem (t1_chardesc *cd, double pos, double del, int dir)
303 {
304 int i;
305
306 ASSERT(cd);
307
308 pos += (dir == HSTEM) ? cd->sbw.sby : cd->sbw.sbx;
309 for (i = 0; i < cd->num_stems; i++) {
310 if (cd->stems[i].dir == dir &&
311 cd->stems[i].pos == pos &&
312 cd->stems[i].del == del)
313 break;
314 }
315 if (i == cd->num_stems) {
316 if (cd->num_stems == CS_STEM_ZONE_MAX)
317 return -1;
318 cd->stems[i].dir = dir;
319 cd->stems[i].pos = pos;
320 cd->stems[i].del = del;
321 cd->stems[i].id = cd->num_stems;
322 (cd->num_stems)++;
323 }
324
325 return cd->stems[i].id;
326 }
327
328
329 static void
330 copy_args (double *args1, double *args2, int count)
331 {
332 while (count-- > 0) {
333 *args1 = *args2;
334 args1++; args2++;
335 }
336 }
337
338 /*
339 * Stack:
340 */
341 #define LIMITCHECK(n) do {\
342 if (cs_stack_top+(n) > CS_ARG_STACK_MAX) {\
343 status = CS_STACK_ERROR;\
344 return;\
345 }\
346 } while (0)
347 #define CHECKSTACK(n) do {\
348 if (cs_stack_top < (n)) {\
349 status = CS_STACK_ERROR;\
350 return;\
351 }\
352 } while (0)
353 #define CLEARSTACK() do {\
354 cs_stack_top = 0;\
355 } while (0)
356
357 /*
358 * Path construction:
359 */
360 /* Get operands from cs_arg_stack[] */
361 static void
362 add_charpath (t1_chardesc *cd, int type, double *argv, int argn)
363 {
364 t1_cpath *p;
365
366 ASSERT(cd);
367 ASSERT(argn <= CS_ARG_STACK_MAX);
368
369 p = NEW(1, t1_cpath);
370 p->type = type;
371 p->num_args = argn;
372 p->next = NULL;
373
374 while (argn-- > 0)
375 p->args[argn] = argv[argn];
376
377 if (!cd->charpath)
378 cd->charpath = p;
379 if (cd->lastpath)
380 cd->lastpath->next = p;
381 cd->lastpath = p;
382
383 if (type >= 0 &&
384 phase != T1_CS_PHASE_FLEX && IS_PATH_OPERATOR(type))
385 phase = T1_CS_PHASE_PATH;
386 }
387
388 static void
389 init_charpath (t1_chardesc *cd)
390 {
391 cd->flags = T1_CS_FLAG_NONE;
392 cd->num_stems = 0;
393 cd->sbw.wx = cd->sbw.wy = 0.0;
394 cd->sbw.sbx = cd->sbw.sby = 0.0;
395 cd->bbox.llx = cd->bbox.lly = cd->bbox.urx = cd->bbox.ury = 0.0;
396 cd->charpath = cd->lastpath = NULL;
397 }
398
399 static void
400 release_charpath (t1_chardesc *cd)
401 {
402 t1_cpath *curr, *next;
403
404 ASSERT(cd);
405
406 curr = cd->charpath;
407 while (curr != NULL) {
408 next = curr->next;
409 RELEASE(curr);
410 curr = next;
411 }
412
413 cd->charpath = cd->lastpath = NULL;
414 }
415
416 /*
417 * Type 1 charstring operators:
418 */
419 #define ADD_PATH(p,t,n) add_charpath((p),(t),&(cs_arg_stack[cs_stack_top-(n)]),(n))
420
421 /*
422 * Single byte operators:
423 */
424 static void
425 do_operator1 (t1_chardesc *cd, card8 **data)
426 {
427 card8 op = **data;
428
429 *data += 1;
430
431 switch (op) {
432 case cs_closepath:
433 /*
434 * From T1 spec.:
435 * Note that, unlike the closepath command in the PostScript language,
436 * this command does not reposition the current point. Any subsequent
437 * rmoveto must be relative to the current point in force before the
438 * Type 1 font format closepath command was given.
439 */
440 /* noop */
441 CLEARSTACK();
442 break;
443 case cs_hsbw:
444 CHECKSTACK(2);
445 cd->sbw.wx = cs_arg_stack[--cs_stack_top];
446 cd->sbw.wy = 0;
447 cd->sbw.sbx = cs_arg_stack[--cs_stack_top];
448 cd->sbw.sby = 0;
449 CLEARSTACK();
450 /* hsbw does NOT set currentpoint. */
451 break;
452 case cs_hstem:
453 case cs_vstem:
454 CHECKSTACK(2);
455 {
456 int stem_id;
457 stem_id = add_stem(cd,
458 cs_arg_stack[cs_stack_top-2],
459 cs_arg_stack[cs_stack_top-1],
460 ((op == cs_hstem) ? HSTEM : VSTEM));
461 if (stem_id < 0) {
462 WARN("Too many hints...");
463 status = CS_PARSE_ERROR;
464 return;
465 }
466 /* Put stem_id onto the stack... */
467 cs_arg_stack[cs_stack_top++] = stem_id;
468 ADD_PATH(cd, CS_HINT_DECL, 1);
469 }
470 CLEARSTACK();
471 break;
472 case cs_rmoveto:
473 /*
474 * Reference point is (0, 0) in Type 2 charstring.
475 */
476 CHECKSTACK(2);
477 {
478 if (phase < T1_CS_PHASE_PATH) {
479 cs_arg_stack[cs_stack_top-2] += cd->sbw.sbx;
480 cs_arg_stack[cs_stack_top-1] += cd->sbw.sby;
481 }
482 ADD_PATH(cd, op, 2);
483 }
484 CLEARSTACK();
485 break;
486 case cs_hmoveto:
487 case cs_vmoveto:
488 CHECKSTACK(1);
489 {
490 int argn = 1;
491 if (phase < T1_CS_PHASE_PATH) {
492 /*
493 * The reference point for the first moveto operator is diferrent
494 * between Type 1 charstring and Type 2 charstring. We compensate it.
495 */
496 if (op == cs_hmoveto) {
497 cs_arg_stack[cs_stack_top-1] += cd->sbw.sbx;
498 if (cd->sbw.sby != 0.0) {
499 cs_arg_stack[cs_stack_top++] = cd->sbw.sby;
500 argn = 2;
501 op = cs_rmoveto;
502 }
503 } else {
504 cs_arg_stack[cs_stack_top-1] += cd->sbw.sby;
505 if (cd->sbw.sbx != 0.0) {
506 cs_arg_stack[cs_stack_top] = cs_arg_stack[cs_stack_top-1];
507 cs_arg_stack[cs_stack_top-1] = cd->sbw.sbx;
508 cs_stack_top++;
509 argn = 2;
510 op = cs_rmoveto;
511 }
512 }
513 }
514 ADD_PATH(cd, op, argn);
515 }
516 CLEARSTACK();
517 break;
518 case cs_endchar:
519 status = CS_CHAR_END;
520 CLEARSTACK();
521 break;
522 /* above oprators are candidate for first stack-clearing operator */
523 case cs_rlineto:
524 CHECKSTACK(2);
525 ADD_PATH(cd, op, 2);
526 CLEARSTACK();
527 break;
528 case cs_hlineto:
529 case cs_vlineto:
530 CHECKSTACK(1);
531 ADD_PATH(cd, op, 1);
532 CLEARSTACK();
533 break;
534 case cs_rrcurveto:
535 CHECKSTACK(6);
536 ADD_PATH(cd, op, 6);
537 CLEARSTACK();
538 break;
539 case cs_vhcurveto:
540 case cs_hvcurveto:
541 CHECKSTACK(4);
542 ADD_PATH(cd, op, 4);
543 CLEARSTACK();
544 break;
545 /* all operotors above are stack-clearing operator */
546 /* no output */
547 case cs_return:
548 break;
549 case cs_callsubr:
550 ERROR("Unexpected callsubr.");
551 break;
552 default:
553 /* no-op ? */
554 WARN("Unknown charstring operator: 0x%02x", op);
555 status = CS_PARSE_ERROR;
556 break;
557 }
558
559 return;
560 }
561
562 /*
563 * OtherSubrs:
564 *
565 * arg0 arg1 ... argn n othersubr# callothersubr
566 *
567 * 0: Build flex:
568 * fd x y 3 0 callothersubr
569 * Othersubr #0 pushes x and y to PostScript interpreter operand stack.
570 * 1: Start flex
571 * 0 1 callothersubr
572 * 2: Mark flex control points
573 * 0 2 callothersubr
574 * 3: Discard hint
575 * subr# 1 3 callothersubr pop callsubr
576 * Subroutine subr# (only) contains stem declaration.
577 * Othersubr #3 pushes subr# to PostScript interpreter operand stack.
578 * 12: Counter control
579 * A subr to avoid stack overflow.
580 * 13: Counter control
581 */
582
583 /*
584 * Convert six control points marked as CS_FLEX_CTRL to a flex path.
585 */
586 static void
587 do_othersubr0 (t1_chardesc *cd)
588 {
589 t1_cpath *flex, *cur, *next;
590
591 if (ps_stack_top < 1) {
592 status = CS_PARSE_ERROR;
593 return;
594 }
595
596 /* Seek first CS_FLEX_CTRL mark */
597 for (cur = cd->charpath; cur != NULL && cur->type != CS_FLEX_CTRL; cur = cur->next);
598 flex = cur;
599 {
600 int i;
601 cur = cur->next;
602 for (i = 1; i < 7; i++) {
603 if (cur == NULL || cur->type != CS_FLEX_CTRL ||
604 cur->num_args != 2) {
605 status = CS_PARSE_ERROR;
606 return;
607 }
608 if (i == 1) {
609 flex->args[0] += cur->args[0];
610 flex->args[1] += cur->args[1];
611 } else {
612 copy_args(&(flex->args[2*i-2]), cur->args, 2);
613 }
614 next = cur->next;
615 RELEASE(cur);
616 cur = next;
617 }
618 }
619 if (cur != NULL) {
620 status = CS_PARSE_ERROR;
621 return;
622 }
623 /*
624 * Now 'flex' have all six control points, the first pair is relative
625 * from starting point.
626 */
627 flex->type = cs_flex;
628 flex->args[12] = ps_arg_stack[--ps_stack_top]; /* flex depth */
629 flex->num_args = 13;
630 flex->next = NULL;
631 cd->lastpath = flex;
632
633 phase = T1_CS_PHASE_PATH;
634 }
635
636 /* Start flex */
637 static void
638 do_othersubr1 (void)
639 {
640 phase = T1_CS_PHASE_FLEX;
641 }
642
643 /* Mark flex control point */
644 static void
645 do_othersubr2 (t1_chardesc *cd)
646 {
647 if (phase != T1_CS_PHASE_FLEX || !cd->lastpath) {
648 status = CS_PARSE_ERROR;
649 return;
650 }
651
652 switch (cd->lastpath->type) {
653 case cs_rmoveto:
654 break;
655 case cs_hmoveto:
656 cd->lastpath->num_args = 2;
657 cd->lastpath->args[1] = 0.0;
658 break;
659 case cs_vmoveto:
660 cd->lastpath->num_args = 2;
661 cd->lastpath->args[1] = cd->lastpath->args[0];
662 cd->lastpath->args[0] = 0.0;
663 break;
664 default:
665 status = CS_PARSE_ERROR;
666 return;
667 }
668 cd->lastpath->type = CS_FLEX_CTRL;
669 }
670
671 /*
672 * Hint Replacement:
673 * "Adobe Type 1 Font Format", Chapter 8.
674 */
675 static void
676 do_othersubr3 (t1_chardesc *cd)
677 {
678 cd->flags |= T1_CS_FLAG_USE_HINTMASK;
679 }
680
681 static void
682 do_othersubr12 (void)
683 {
684 /* Othersubr12 call must immediately follow the hsbw or sbw. */
685 if (phase != T1_CS_PHASE_INIT) {
686 status = CS_PARSE_ERROR;
687 return;
688 }
689 /* noop */
690 }
691
692 static void
693 do_othersubr13 (t1_chardesc *cd)
694 {
695 t1_stemgroup stemgroups[CS_STEM_GROUP_MAX];
696 int num_hgroups, num_vgroups, n, stem_id;
697 double pos, del;
698
699 /* After #12 callothersubr or hsbw or sbw. */
700 if (phase != T1_CS_PHASE_INIT) {
701 status = CS_PARSE_ERROR;
702 return;
703 }
704 for (n = 0; n < CS_STEM_GROUP_MAX; n++) {
705 stemgroups[n].num_stems = 0;
706 }
707
708 num_hgroups = (int) ps_arg_stack[--ps_stack_top];
709 if (num_hgroups < 0 || num_hgroups > CS_STEM_GROUP_MAX) {
710 status = CS_PARSE_ERROR;
711 return;
712 }
713 n = 0; pos = 0.0;
714 while (ps_stack_top >= 2 && n < num_hgroups) {
715 /* add_stem() add sidebearing */
716 pos += ps_arg_stack[--ps_stack_top];
717 del = ps_arg_stack[--ps_stack_top];
718 stem_id = add_stem(cd,
719 (del < 0.0) ? pos + del : pos,
720 (del < 0.0) ? -del : del,
721 HSTEM);
722 stemgroups[n].stems[stemgroups[n].num_stems] = stem_id;
723 stemgroups[n].num_stems += 1;
724 pos += del;
725 if (del < 0.0) {
726 pos = 0.0;
727 n++;
728 }
729 }
730 if (n != num_hgroups) {
731 status = CS_STACK_ERROR;
732 return;
733 }
734
735 num_vgroups = (int) ps_arg_stack[--ps_stack_top];
736 if (num_vgroups < 0 || num_vgroups > CS_STEM_GROUP_MAX) {
737 status = CS_PARSE_ERROR;
738 return;
739 }
740 n = 0; pos = 0.0;
741 while (ps_stack_top >= 2 && n < num_vgroups) {
742 /* add_stem() add sidebearing */
743 pos += ps_arg_stack[--ps_stack_top];
744 del = ps_arg_stack[--ps_stack_top];
745 stem_id = add_stem(cd,
746 (del < 0.0) ? pos + del : pos,
747 (del < 0.0) ? -del : del,
748 VSTEM);
749 stemgroups[n].stems[stemgroups[n].num_stems] = stem_id;
750 stemgroups[n].num_stems += 1;
751 pos += del;
752 if (del < 0.0) {
753 pos = 0.0;
754 n++;
755 }
756 }
757 if (n != num_vgroups) {
758 status = CS_STACK_ERROR;
759 return;
760 }
761
762 for (n = 0; n < MAX(num_hgroups, num_vgroups); n++) {
763 add_charpath(cd, cs_cntrmask,
764 stemgroups[n].stems, stemgroups[n].num_stems);
765 }
766
767 cd->flags |= T1_CS_FLAG_USE_CNTRMASK;
768 }
769
770 static void
771 do_callothersubr (t1_chardesc *cd)
772 {
773 int argn, subrno;
774
775 CHECKSTACK(2);
776 subrno = (int) cs_arg_stack[--cs_stack_top];
777 argn = (int) cs_arg_stack[--cs_stack_top];
778
779 CHECKSTACK(argn);
780 if (ps_stack_top+argn > PS_ARG_STACK_MAX) {
781 status = CS_PARSE_ERROR;
782 return;
783 }
784 while (argn-- > 0)
785 ps_arg_stack[ps_stack_top++] = cs_arg_stack[--cs_stack_top];
786
787 switch (subrno) {
788 case 0: do_othersubr0(cd) ; break;
789 case 1: do_othersubr1() ; break;
790 case 2: do_othersubr2(cd) ; break;
791 case 3: do_othersubr3(cd) ; break;
792 case 12: do_othersubr12() ; break;
793 case 13: do_othersubr13(cd); break;
794 default:
795 ERROR("Unknown othersubr #%ld.", subrno);
796 break;
797 }
798 }
799
800 /*
801 * Double byte operators:
802 */
803 static void
804 do_operator2 (t1_chardesc *cd, card8 **data, card8 *endptr)
805 {
806 card8 op;
807
808 *data += 1;
809
810 SRC_NEED(endptr, *data + 1);
811
812 op = **data;
813 *data += 1;
814
815 switch(op) {
816 case cs_sbw:
817 CHECKSTACK(4);
818 cd->sbw.wy = cs_arg_stack[--cs_stack_top];
819 cd->sbw.wx = cs_arg_stack[--cs_stack_top];
820 cd->sbw.sby = cs_arg_stack[--cs_stack_top];
821 cd->sbw.sbx = cs_arg_stack[--cs_stack_top];
822 CLEARSTACK();
823 break;
824 case cs_hstem3:
825 case cs_vstem3:
826 /*
827 * TODO:
828 * The counter control can be used for hstem3 and vstem3
829 * operator if LanguageGroup is not equal to 1.
830 */
831 CHECKSTACK(6);
832 {
833 int i;
834 for (i = 2; i >= 0; i--) {
835 int stem_id;
836 stem_id = add_stem(cd,
837 cs_arg_stack[cs_stack_top-2*i-2],
838 cs_arg_stack[cs_stack_top-2*i-1],
839 ((op == cs_hstem3) ? HSTEM : VSTEM));
840 if (stem_id < 0) {
841 WARN("Too many hints...");
842 status = CS_PARSE_ERROR;
843 return;
844 }
845 /* Put stem_id onto the stack... */
846 cs_arg_stack[cs_stack_top++] = stem_id;
847 ADD_PATH(cd, CS_HINT_DECL, 1);
848 cs_stack_top--;
849 }
850 }
851 CLEARSTACK();
852 break;
853 case cs_setcurrentpoint:
854 CHECKSTACK(2);
855 /* noop */
856 CLEARSTACK();
857 break;
858 /* all operator above are stack-clearing */
859 case cs_pop:
860 /*
861 * Transfer a operand from PS interpreter operand stack to BuildChar
862 * operand stack.
863 */
864 if (ps_stack_top < 1) {
865 status = CS_PARSE_ERROR;
866 return;
867 }
868 LIMITCHECK(1);
869 cs_arg_stack[cs_stack_top++] = ps_arg_stack[--ps_stack_top];
870 break;
871 case cs_dotsection:
872 #if 0
873 /*
874 * If the hint replacement feature is used in the font, the
875 * "dotsection" operator exist only for compatibility to older
876 * (more than 10 years old) Type 1 font rasterizer which can't
877 * perform hint replacement. In this case, we silently ignore
878 * the "dotsection" operator.
879 *
880 * The following code will wrongly warn about "dotsection" when
881 * the charstring only contains dot (e.g., "bullet") where the
882 * hint replacement is not necessary.
883 *
884 * Adobe ATM renderers always treat this operator as a no-op.
885 * (See, Adobe Technical Note #5177, Appendix C)
886 */
887 if (!(cd->flags & T1_CS_FLAG_USE_HINTMASK)) {
888 if (dpx_conf.verbose_level > 1)
889 WARN("Obsolete Type 1 charstring operator \"dotsection\" not supported.");
890 }
891 #endif
892 /* noop */
893 break;
894 case cs_div: /* TODO: check overflow */
895 CHECKSTACK(2);
896 cs_arg_stack[cs_stack_top-2] /= cs_arg_stack[cs_stack_top-1];
897 cs_stack_top--;
898 break;
899 case cs_callothersubr:
900 do_callothersubr(cd);
901 break;
902 case cs_seac:
903 CHECKSTACK(5);
904 cd->flags |= T1_CS_FLAG_USE_SEAC;
905 cd->seac.achar = (card8) cs_arg_stack[--cs_stack_top];
906 cd->seac.bchar = (card8) cs_arg_stack[--cs_stack_top];
907 cd->seac.ady = cs_arg_stack[--cs_stack_top];
908 cd->seac.adx = cs_arg_stack[--cs_stack_top];
909 /* We must compensate the difference of the glyph origin. */
910 cd->seac.ady += cd->sbw.sby;
911 cd->seac.adx += cd->sbw.sbx - cs_arg_stack[--cs_stack_top];
912 CLEARSTACK();
913 break;
914 default:
915 /* no-op ? */
916 WARN("Unknown charstring operator: 0x0c%02x", op);
917 status = CS_PARSE_ERROR;
918 break;
919 }
920
921 return;
922 }
923
924 /*
925 * Charstring encoding:
926 * Copied from cs_type2.c
927 * Note:
928 * The Type 2 interpretation of a number encoded in five-bytes (those with
929 * an initial byte value of 255) differs from how it is interpreted in the
930 * Type 1 format.
931 */
932
933 /* Type 2 5-bytes encoding used. */
934 static void
935 put_numbers (double *argv, int argn, card8 **dest, card8 *limit)
936 {
937 int i;
938
939 for (i = 0; i < argn; i++) {
940 double value;
941 int ivalue;
942 value = argv[i];
943 /* Nearest integer value */
944 ivalue = (int) floor(value+0.5);
945 if (value >= 0x8000L || value <= (-0x8000L - 1)) {
946 /*
947 * This number cannot be represented as a single operand.
948 * We must use `a b mul ...' or `a c div' to represent large values.
949 */
950 ERROR("Argument value too large. (This is bug)");
951 } else if (fabs(value - ivalue) > 3.0e-5) {
952 /* 16.16-bit signed fixed value */
953 DST_NEED(limit, *dest + 5);
954 *(*dest)++ = 255;
955 ivalue = (int) floor(value); /* mantissa */
956 *(*dest)++ = (ivalue >> 8) & 0xff;
957 *(*dest)++ = ivalue & 0xff;
958 ivalue = (int)((value - ivalue) * 0x10000l); /* fraction */
959 *(*dest)++ = (ivalue >> 8) & 0xff;
960 *(*dest)++ = ivalue & 0xff;
961 /* Everything else are integers. */
962 } else if (ivalue >= -107 && ivalue <= 107) {
963 DST_NEED(limit, *dest + 1);
964 *(*dest)++ = ivalue + 139;
965 } else if (ivalue >= 108 && ivalue <= 1131) {
966 DST_NEED(limit, *dest + 2);
967 ivalue = 0xf700u + ivalue - 108;
968 *(*dest)++ = (ivalue >> 8) & 0xff;
969 *(*dest)++ = ivalue & 0xff;
970 } else if (ivalue >= -1131 && ivalue <= -108) {
971 DST_NEED(limit, *dest + 2);
972 ivalue = 0xfb00u - ivalue - 108;
973 *(*dest)++ = (ivalue >> 8) & 0xff;
974 *(*dest)++ = ivalue & 0xff;
975 } else if (ivalue >= -32768 && ivalue <= 32767) { /* shortint */
976 DST_NEED(limit, *dest + 3);
977 *(*dest)++ = 28;
978 *(*dest)++ = (ivalue >> 8) & 0xff;
979 *(*dest)++ = (ivalue) & 0xff;
980 } else { /* Shouldn't come here */
981 ERROR("Unexpected error.");
982 }
983 }
984
985 return;
986 }
987
988 static void
989 get_integer (card8 **data, card8 *endptr)
990 {
991 int result = 0;
992 card8 b0 = **data, b1, b2;
993
994 *data += 1;
995
996 if (b0 == 28) { /* shortint */
997 SRC_NEED(endptr, *data + 2);
998 b1 = **data;
999 b2 = *(*data+1);
1000 result = b1*256+b2;
1001 if (result > 0x7fff)
1002 result -= 0x10000L;
1003 *data += 2;
1004 } else if (b0 >= 32 && b0 <= 246) { /* int (1) */
1005 result = b0 - 139;
1006 } else if (b0 >= 247 && b0 <= 250) { /* int (2) */
1007 SRC_NEED(endptr, *data + 1);
1008 b1 = **data;
1009 result = (b0-247)*256+b1+108;
1010 *data += 1;
1011 } else if (b0 >= 251 && b0 <= 254) {
1012 SRC_NEED(endptr, *data + 1);
1013 b1 = **data;
1014 result = -(b0-251)*256-b1-108;
1015 *data += 1;
1016 } else {
1017 status = CS_PARSE_ERROR;
1018 return;
1019 }
1020
1021 LIMITCHECK(1);
1022 cs_arg_stack[cs_stack_top++] = (double) result;
1023
1024 return;
1025 }
1026
1027 /* Type 1 */
1028 static void
1029 get_longint (card8 **data, card8 *endptr)
1030 {
1031 int result = 0;
1032 int i;
1033
1034 *data += 1;
1035 SRC_NEED(endptr, *data + 4);
1036 result = **data;
1037 if (result >= 0x80L)
1038 result -= 0x100L;
1039 *data += 1;
1040 for (i = 1; i < 4; i++) {
1041 result = result*256 + (**data);
1042 *data += 1;
1043 }
1044
1045 LIMITCHECK(1);
1046 cs_arg_stack[cs_stack_top++] = (double) result;
1047
1048 return;
1049 }
1050
1051 /*
1052 * TODO:
1053 * Check "seac"
1054 * We cannot do backword parsing due to subroutine, div etc.
1055 */
1056
1057 /* Parse charstring and build charpath. */
1058 static void
1059 t1char_build_charpath (t1_chardesc *cd,
1060 card8 **data, card8 *endptr, cff_index *subrs)
1061 {
1062 card8 b0 = 0, *subr;
1063 int len;
1064
1065 if (nest > CS_SUBR_NEST_MAX)
1066 ERROR("Subroutine nested too deeply.");
1067
1068 nest++;
1069 while (*data < endptr && status == CS_PARSE_OK) {
1070 b0 = **data;
1071 if (b0 == 255) {
1072 get_longint(data, endptr); /* Type 1 */
1073 } else if (b0 == cs_return) {
1074 status = CS_SUBR_RETURN;
1075 } else if (b0 == cs_callsubr) {
1076 if (cs_stack_top < 1) {
1077 status = CS_STACK_ERROR;
1078 } else {
1079 int idx;
1080
1081 idx = cs_arg_stack[--cs_stack_top];
1082 if (!subrs || idx >= subrs->count)
1083 ERROR("Invalid Subr#.");
1084 subr = subrs->data + subrs->offset[idx] - 1;
1085 len = subrs->offset[idx+1] - subrs->offset[idx];
1086 t1char_build_charpath(cd, &subr, subr+len, subrs);
1087 *data += 1;
1088 }
1089 } else if (b0 == cs_escape) {
1090 do_operator2(cd, data, endptr);
1091 } else if (b0 < 32 && b0 != 28) { /* 19, 20 need mask */
1092 do_operator1(cd, data);
1093 } else if ((b0 <= 22 && b0 >= 27) || b0 == 31) { /* reserved */
1094 status = CS_PARSE_ERROR; /* not an error ? */
1095 } else { /* integer */
1096 get_integer(data, endptr);
1097 }
1098 }
1099
1100 if (status == CS_SUBR_RETURN) {
1101 status = CS_PARSE_OK;
1102 } else if (status == CS_CHAR_END && *data < endptr) {
1103 if (!(*data == endptr - 1 && **data == cs_return))
1104 WARN("Garbage after endchar. (%d bytes)", (int) (endptr - *data));
1105 } else if (status < CS_PARSE_OK) { /* error */
1106 ERROR("Parsing charstring failed: (status=%d, stack=%d)", status, cs_stack_top);
1107 }
1108
1109 nest--;
1110
1111 return;
1112 }
1113
1114 /*
1115 * Calculate BoundingBox and compress path.
1116 * The essentials of PDF size reduction is not Type 2 charstring compression
1117 * but Type 1 charstring encryption. Encryption makes lossless compression
1118 * useless. We will only do very simple charstring compression.
1119 */
1120 static void
1121 do_postproc (t1_chardesc *cd)
1122 {
1123 int i;
1124 t1_cpath *cur, *prev, *next;
1125 double x, y;
1126
1127 if (!cd->charpath)
1128 return;
1129
1130 /* Set dummy large value. */
1131 cd->bbox.llx = cd->bbox.lly = 100000.0;
1132 cd->bbox.urx = cd->bbox.ury = -100000.0;
1133
1134 cur = cd->charpath;
1135 prev = NULL;
1136 x = y = 0.0;
1137
1138 #define UPDATE_BBOX(b,x,y) do {\
1139 if ((b).llx > (x)) (b).llx = (x);\
1140 if ((b).urx < (x)) (b).urx = (x);\
1141 if ((b).lly > (y)) (b).lly = (y);\
1142 if ((b).ury < (y)) (b).ury = (y);\
1143 } while (0)
1144 #define TRY_COMPACT (prev && cur && ((prev->num_args + cur->num_args) < CS_ARG_STACK_MAX))
1145
1146 while (cur != NULL) {
1147 next = cur->next;
1148 switch (cur->type) {
1149 case cs_rmoveto:
1150 x += cur->args[0]; y += cur->args[1];
1151 UPDATE_BBOX(cd->bbox, x, y);
1152 break;
1153 case cs_rlineto:
1154 x += cur->args[0]; y += cur->args[1];
1155 UPDATE_BBOX(cd->bbox, x, y);
1156 if (TRY_COMPACT) {
1157 if (prev->type == cs_rlineto) {
1158 copy_args(prev->args+prev->num_args, cur->args, cur->num_args);
1159 prev->num_args += cur->num_args;
1160 prev->next = next;
1161 RELEASE(cur); cur = NULL;
1162 } else if (prev->type == cs_rrcurveto) {
1163 copy_args(prev->args+prev->num_args, cur->args, cur->num_args);
1164 prev->num_args += cur->num_args;
1165 prev->type = cs_rcurveline;
1166 prev->next = next;
1167 RELEASE(cur); cur = NULL;
1168 }
1169 }
1170 break;
1171 case cs_hmoveto:
1172 x += cur->args[0];
1173 UPDATE_BBOX(cd->bbox, x, y);
1174 break;
1175 case cs_hlineto:
1176 x += cur->args[0];
1177 UPDATE_BBOX(cd->bbox, x, y);
1178 if (TRY_COMPACT) {
1179 if ((prev->type == cs_vlineto && (prev->num_args % 2) == 1) ||
1180 (prev->type == cs_hlineto && (prev->num_args % 2) == 0)) {
1181 copy_args(prev->args+prev->num_args, cur->args, cur->num_args);
1182 prev->num_args += cur->num_args;
1183 prev->next = next;
1184 RELEASE(cur); cur = NULL;
1185 }
1186 }
1187 break;
1188 case cs_vmoveto:
1189 y += cur->args[0];
1190 UPDATE_BBOX(cd->bbox, x, y);
1191 break;
1192 case cs_vlineto:
1193 y += cur->args[0];
1194 UPDATE_BBOX(cd->bbox, x, y);
1195 if (TRY_COMPACT) {
1196 if ((prev->type == cs_hlineto && (prev->num_args % 2) == 1) ||
1197 (prev->type == cs_vlineto && (prev->num_args % 2) == 0)) {
1198 copy_args(prev->args+prev->num_args, cur->args, cur->num_args);
1199 prev->num_args += cur->num_args;
1200 prev->next = next;
1201 RELEASE(cur); cur = NULL;
1202 }
1203 }
1204 break;
1205 case cs_rrcurveto:
1206 for (i = 0; i < 3; i++) {
1207 x += cur->args[2*i]; y += cur->args[2*i+1];
1208 UPDATE_BBOX(cd->bbox, x, y);
1209 }
1210 if (TRY_COMPACT) {
1211 if (prev->type == cs_rrcurveto) {
1212 copy_args(prev->args+prev->num_args, cur->args, cur->num_args);
1213 prev->num_args += cur->num_args;
1214 prev->next = next;
1215 RELEASE(cur); cur = NULL;
1216 } else if (prev->type == cs_rlineto) {
1217 copy_args(prev->args+prev->num_args, cur->args, cur->num_args);
1218 prev->num_args += cur->num_args;
1219 prev->type = cs_rlinecurve;
1220 prev->next = next;
1221 RELEASE(cur); cur = NULL;
1222 }
1223 }
1224 break;
1225 case cs_vhcurveto:
1226 y += cur->args[0];
1227 UPDATE_BBOX(cd->bbox, x, y);
1228 x += cur->args[1]; y += cur->args[2];
1229 UPDATE_BBOX(cd->bbox, x, y);
1230 x += cur->args[3];
1231 UPDATE_BBOX(cd->bbox, x, y);
1232 if (TRY_COMPACT) {
1233 if ((prev->type == cs_hvcurveto && ((prev->num_args / 4) % 2) == 1) ||
1234 (prev->type == cs_vhcurveto && ((prev->num_args / 4) % 2) == 0)) {
1235 copy_args(prev->args+prev->num_args, cur->args, cur->num_args);
1236 prev->num_args += cur->num_args;
1237 prev->next = next;
1238 RELEASE(cur); cur = NULL;
1239 }
1240 }
1241 break;
1242 case cs_hvcurveto:
1243 x += cur->args[0];
1244 UPDATE_BBOX(cd->bbox, x, y);
1245 x += cur->args[1]; y += cur->args[2];
1246 UPDATE_BBOX(cd->bbox, x, y);
1247 y += cur->args[3];
1248 UPDATE_BBOX(cd->bbox, x, y);
1249 if (TRY_COMPACT) {
1250 if ((prev->type == cs_vhcurveto && ((prev->num_args / 4) % 2) == 1) ||
1251 (prev->type == cs_hvcurveto && ((prev->num_args / 4) % 2) == 0)) {
1252 copy_args(prev->args+prev->num_args, cur->args, cur->num_args);
1253 prev->num_args += cur->num_args;
1254 prev->next = next;
1255 RELEASE(cur); cur = NULL;
1256 }
1257 }
1258 break;
1259 case cs_flex:
1260 for (i = 0; i < 6; i++) {
1261 x += cur->args[2*i]; y += cur->args[2*1+1];
1262 UPDATE_BBOX(cd->bbox, x, y);
1263 }
1264 if (cur->args[12] == 50.0) {
1265 if (cur->args[1] == 0.0 && cur->args[11] == 0.0 &&
1266 cur->args[5] == 0.0 && cur->args[7] == 0.0 &&
1267 cur->args[3] + cur->args[9] == 0.0) {
1268 /* cur->args[0] = cur->args[0]; dx1 */
1269 cur->args[1] = cur->args[2]; /* dx2 */
1270 cur->args[2] = cur->args[3]; /* dy2 */
1271 cur->args[3] = cur->args[4]; /* dx3 */
1272 cur->args[4] = cur->args[6]; /* dx4 */
1273 cur->args[5] = cur->args[8]; /* dx5 */
1274 cur->args[6] = cur->args[10]; /* dx6 */
1275 cur->num_args = 7;
1276 cur->type = cs_hflex;
1277 } else if (cur->args[5] == 0.0 && cur->args[7] == 0.0 &&
1278 (cur->args[1] + cur->args[3] +
1279 cur->args[9] + cur->args[11]) == 0) {
1280 /* cur->args[0] = cur->args[0]; dx1 */
1281 /* cur->args[1] = cur->args[1]; dy1 */
1282 /* cur->args[2] = cur->args[2]; dx2 */
1283 /* cur->args[3] = cur->args[3]; dy2 */
1284 /* cur->args[4] = cur->args[4]; dx3 */
1285 cur->args[5] = cur->args[6]; /* dx4 */
1286 cur->args[6] = cur->args[8]; /* dx5 */
1287 cur->args[7] = cur->args[9]; /* dy5 */
1288 cur->args[8] = cur->args[10]; /* dx6 */
1289 cur->num_args = 9;
1290 cur->type = cs_hflex1;
1291 }
1292 }
1293 break;
1294 case CS_HINT_DECL:
1295 case cs_cntrmask:
1296 /* noop */
1297 break;
1298 default:
1299 ERROR("Unexpected Type 2 charstring command %d.", cur->type);
1300 break;
1301 }
1302 if (cur != NULL)
1303 prev = cur;
1304 cur = next;
1305 }
1306
1307 /* Had no path. Fix lower-left point. */
1308 if (cd->bbox.llx > cd->bbox.urx)
1309 cd->bbox.llx = cd->bbox.urx = cd->sbw.wx;
1310 if (cd->bbox.lly > cd->bbox.ury)
1311 cd->bbox.lly = cd->bbox.ury = cd->sbw.wy;
1312
1313 return;
1314 }
1315
1316 #define RESET_STATE() do {\
1317 status = CS_PARSE_OK;\
1318 phase = T1_CS_PHASE_INIT;\
1319 nest = 0;\
1320 ps_stack_top = 0;\
1321 } while (0)
1322
1323 int
1324 t1char_get_metrics (card8 *src, int srclen, cff_index *subrs, t1_ginfo *ginfo)
1325 {
1326 t1_chardesc t1char, *cd;
1327
1328 cd = &t1char;
1329 init_charpath(cd);
1330 RESET_STATE();
1331 CLEARSTACK();
1332 t1char_build_charpath(cd, &src, src+srclen, subrs);
1333 if (cs_stack_top != 0 || ps_stack_top != 0)
1334 WARN("Stack not empty. (%d, %d)", cs_stack_top, ps_stack_top);
1335 do_postproc(cd);
1336 if (ginfo) {
1337 ginfo->wx = cd->sbw.wx;
1338 ginfo->wy = cd->sbw.wy;
1339 ginfo->bbox.llx = cd->bbox.llx;
1340 ginfo->bbox.lly = cd->bbox.lly;
1341 ginfo->bbox.urx = cd->bbox.urx;
1342 ginfo->bbox.ury = cd->bbox.ury;
1343 if (cd->flags & T1_CS_FLAG_USE_SEAC) {
1344 ginfo->use_seac = 1;
1345 ginfo->seac.adx = cd->seac.adx;
1346 ginfo->seac.ady = cd->seac.ady;
1347 ginfo->seac.bchar = cd->seac.bchar;
1348 ginfo->seac.achar = cd->seac.achar;
1349 } else {
1350 ginfo->use_seac = 0;
1351 }
1352 }
1353 release_charpath(cd);
1354
1355 return 0;
1356 }
1357
1358 #define CHECK_BUFFER(n) if (dst+(n) >= endptr) {\
1359 ERROR("Buffer overflow.");\
1360 }
1361 #define CHECK_STATUS() if (status != CS_PARSE_OK) {\
1362 ERROR("Charstring encoder error: %d", status);\
1363 }
1364
1365 /*
1366 * Encode Charpath as a Type 2 Charstring
1367 */
1368 static int
1369 t1char_encode_charpath (t1_chardesc *cd,
1370 double default_width, double nominal_width,
1371 card8 *dst, card8 *endptr)
1372 {
1373 card8 *save;
1374 t1_cpath *curr;
1375
1376 ASSERT(cd);
1377
1378 save = dst;
1379 curr = cd->charpath;
1380
1381 RESET_STATE();
1382 CLEARSTACK();
1383 /*
1384 * Advance Width
1385 */
1386 if (cd->sbw.wx != default_width) {
1387 double wx = cd->sbw.wx - nominal_width;
1388 put_numbers(&wx, 1, &dst, endptr);
1389 CHECK_STATUS();
1390 }
1391 /*
1392 * Hint Declaration
1393 */
1394 {
1395 int num_hstems = 0, num_vstems = 0;
1396 int i, reset = 1;
1397 double stem[2];
1398
1399 for (i = 0; i < cd->num_stems && cd->stems[i].dir == HSTEM; i++) {
1400 num_hstems++;
1401 stem[0] = (reset ?
1402 (cd->stems[i].pos) :
1403 (cd->stems[i].pos - (cd->stems[i-1].pos + cd->stems[i-1].del)));
1404 stem[1] = cd->stems[i].del;
1405 put_numbers(stem, 2, &dst, endptr);
1406 CHECK_STATUS();
1407 reset = 0;
1408 if (2*num_hstems > CS_ARG_STACK_MAX - 3) {
1409 CHECK_BUFFER(1);
1410 *dst++ = (card8) ((cd->flags & T1_CS_FLAG_USE_HINTMASK) ? cs_hstemhm : cs_hstem);
1411 reset = 1;
1412 }
1413 }
1414 if (reset == 0) {
1415 CHECK_BUFFER(1);
1416 *dst++ = (card8) ((cd->flags & T1_CS_FLAG_USE_HINTMASK) ? cs_hstemhm : cs_hstem);
1417 }
1418 reset = 1;
1419 if (cd->num_stems - num_hstems > 0) {
1420 for (i = num_hstems; i < cd->num_stems; i++) {
1421 num_vstems++;
1422 stem[0] = (reset ?
1423 (cd->stems[i].pos) :
1424 (cd->stems[i].pos - (cd->stems[i-1].pos + cd->stems[i-1].del)));
1425 stem[1] = cd->stems[i].del;
1426 put_numbers(stem, 2, &dst, endptr);
1427 CHECK_STATUS();
1428 reset = 0;
1429 if (2*num_vstems > CS_ARG_STACK_MAX - 3) {
1430 CHECK_BUFFER(1);
1431 *dst++ = (card8) ((cd->flags & T1_CS_FLAG_USE_HINTMASK) ? cs_vstemhm : cs_vstem);
1432 reset = 1;
1433 }
1434 }
1435 if (reset == 0) {
1436 CHECK_BUFFER(1);
1437 if ((cd->flags & T1_CS_FLAG_USE_HINTMASK) ||
1438 (cd->flags & T1_CS_FLAG_USE_CNTRMASK)) {
1439 /*
1440 * The vstem hint operator can be ommited if hstem and vstem hints
1441 * are both declared at the beginning of a charstring, and is
1442 * followed directly by the hintmask or cntrmask operators.
1443 */
1444 if (curr->type != CS_HINT_DECL &&
1445 curr->type != cs_cntrmask) {
1446 *dst++ = (card8) cs_vstemhm;
1447 }
1448 } else {
1449 *dst++ = (card8) cs_vstem;
1450 }
1451 }
1452 }
1453 }
1454 /*
1455 * Path Construction and Hint Replacement
1456 */
1457 while (curr != NULL && curr->type != cs_endchar) {
1458 switch (curr->type) {
1459 case CS_HINT_DECL:
1460 {
1461 card8 hintmask[(CS_STEM_ZONE_MAX+7)/8];
1462
1463 memset(hintmask, 0, (cd->num_stems+7)/8);
1464 while (curr != NULL && curr->type == CS_HINT_DECL) {
1465 int stem_idx;
1466
1467 stem_idx = get_stem(cd, (int) curr->args[0]);
1468 ASSERT(stem_idx < cd->num_stems);
1469 hintmask[stem_idx/8] |= (1 << (7 - (stem_idx % 8)));
1470 curr = curr->next;
1471 }
1472 if (cd->flags & T1_CS_FLAG_USE_HINTMASK) {
1473 CHECK_BUFFER((cd->num_stems+7)/8 + 1);
1474 *dst++ = (card8) cs_hintmask;
1475 memcpy(dst, hintmask, (cd->num_stems+7)/8);
1476 dst += (cd->num_stems+7)/8;
1477 }
1478 }
1479 break;
1480 case cs_cntrmask:
1481 {
1482 card8 cntrmask[(CS_STEM_ZONE_MAX+7)/8];
1483 int stem_idx, i;
1484
1485 memset(cntrmask, 0, (cd->num_stems+7)/8);
1486 for (i = 0; i < curr->num_args; i++) {
1487 stem_idx = get_stem(cd, (int) curr->args[i]);
1488 ASSERT(stem_idx < cd->num_stems);
1489 cntrmask[stem_idx/8] |= (1 << (7 - (stem_idx % 8)));
1490 }
1491 CHECK_BUFFER((cd->num_stems+7)/8 + 1);
1492 *dst++ = (card8) cs_cntrmask;
1493 memcpy(dst, cntrmask, (cd->num_stems+7)/8);
1494 dst += (cd->num_stems+7)/8;
1495 curr = curr->next;
1496 }
1497 break;
1498 case cs_rmoveto: case cs_hmoveto: case cs_vmoveto:
1499 case cs_rlineto: case cs_hlineto: case cs_vlineto:
1500 case cs_rrcurveto: case cs_hvcurveto: case cs_vhcurveto:
1501 case cs_rlinecurve: case cs_rcurveline:
1502 {
1503 put_numbers(curr->args, curr->num_args, &dst, endptr);
1504 CHECK_STATUS();
1505 CHECK_BUFFER(1);
1506 *dst++ = (card8) curr->type;
1507 curr = curr->next;
1508 }
1509 break;
1510 case cs_flex: case cs_hflex:
1511 case cs_hflex1:
1512 {
1513 put_numbers(curr->args, curr->num_args, &dst, endptr);
1514 CHECK_STATUS();
1515 CHECK_BUFFER(2);
1516 *dst++ = (card8) cs_escape;
1517 *dst++ = (card8) curr->type;
1518 curr = curr->next;
1519 }
1520 break;
1521 default:
1522 ERROR("Unknown Type 2 charstring command: %d", curr->type);
1523 break;
1524 }
1525 }
1526
1527 /*
1528 * (adx ady bchar achar) endchar
1529 */
1530 if (cd->flags & T1_CS_FLAG_USE_SEAC) {
1531 double seac[4];
1532 seac[0] = cd->seac.adx;
1533 seac[1] = cd->seac.ady;
1534 seac[2] = cd->seac.bchar;
1535 seac[3] = cd->seac.achar;
1536 put_numbers(seac, 4, &dst, endptr);
1537 CHECK_STATUS();
1538 CHECK_BUFFER(2);
1539 WARN("Obsolete four arguments of \"endchar\" will be used for Type 1 \"seac\" operator.");
1540 }
1541 CHECK_BUFFER(1);
1542 *dst++ = (card8) cs_endchar;
1543
1544 return (int) (dst - save);
1545 }
1546
1547 int
1548 t1char_convert_charstring (card8 *dst, int dstlen,
1549 card8 *src, int srclen, cff_index *subrs,
1550 double default_width, double nominal_width,
1551 t1_ginfo *ginfo)
1552 {
1553 int length;
1554 t1_chardesc t1char, *cd;
1555
1556 cd = &t1char;
1557 init_charpath(cd);
1558 RESET_STATE();
1559 CLEARSTACK();
1560 t1char_build_charpath(cd, &src, src+srclen, subrs);
1561 if (cs_stack_top != 0 || ps_stack_top != 0)
1562 WARN("Stack not empty. (%d, %d)", cs_stack_top, ps_stack_top);
1563 do_postproc(cd);
1564 SORT_STEMS(cd);
1565
1566 length = t1char_encode_charpath(cd, default_width, nominal_width, dst, dst+dstlen);
1567
1568 if (ginfo) {
1569 ginfo->wx = cd->sbw.wx;
1570 ginfo->wy = cd->sbw.wy;
1571 ginfo->bbox.llx = cd->bbox.llx;
1572 ginfo->bbox.lly = cd->bbox.lly;
1573 ginfo->bbox.urx = cd->bbox.urx;
1574 ginfo->bbox.ury = cd->bbox.ury;
1575 if (cd->flags & T1_CS_FLAG_USE_SEAC) {
1576 ginfo->use_seac = 1;
1577 ginfo->seac.adx = cd->seac.adx;
1578 ginfo->seac.ady = cd->seac.ady;
1579 ginfo->seac.bchar = cd->seac.bchar;
1580 ginfo->seac.achar = cd->seac.achar;
1581 } else {
1582 ginfo->use_seac = 0;
1583 }
1584 }
1585 release_charpath(cd);
1586
1587 return length;
1588 }