Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/libefont/t1interp.cc

raw
1 // -*- related-file-name: "../include/efont/t1interp.hh" -*-
2
3 /* t1interp.{cc,hh} -- Type 1/2 charstring interpretation
4 *
5 * Copyright (c) 1998-2019 Eddie Kohler
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version. This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <efont/t1interp.hh>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <math.h>
23
24 #ifdef WIN32
25 # define random() rand()
26 #endif
27
28 #define CHECK_STACK(numargs) do { if (size() < numargs) return error(errUnderflow, cmd); } while (0)
29 #define CHECK_STATE() do { if (_state < S_IPATH) return error(errOrdering, cmd); } while (0)
30 #define CHECK_PATH_START() do { _state = S_PATH; } while (0)
31 #define CHECK_PATH_END() do { if (_state == S_PATH) { act_closepath(cmd); } _state = S_IPATH; } while (0)
32
33 #ifndef static_assert
34 # define static_assert(c, msg) switch ((int) (c)) case 0: case (c):
35 #endif
36
37 namespace Efont {
38
39 static const char * const error_formats[] = {
40 "charstring OK", // errOK
41 "charstring internal error in '%C'", // errInternal
42 "charstring commands past end", // errRunoff
43 "charstring command '%C' unimplemented", // errUnimplemented
44 "charstring stack overflow", // errOverflow
45 "charstring stack underflow in '%C'", // errUnderflow
46 "charstring bad vector operation in '%C'", // errVector
47 "charstring bad value in '%C'", // errValue
48 "charstring bad subroutine number %d", // errSubr
49 "charstring bad glyph number '%d'", // errGlyph
50 "charstring no current point in '%C'", // errCurrentPoint
51 "charstring flex error", // errFlex
52 "charstring multiple master error in '%C'", // errMultipleMaster
53 "charstring open stroke", // errOpenStroke
54 "charstring late sidebearing command '%C'", // errLateSidebearing
55 "charstring bad othersubr number %d", // errOthersubr
56 "charstring ordering constraints violated at '%C'", // errOrdering
57 "charstring inappropriate hintmask", // errHintmask
58 "charstring subrs nested too deep at '%d'" // errSubrDepth
59 };
60
61 double CharstringInterp::double_for_error;
62
63
64 CharstringInterp::CharstringInterp()
65 : _error(errOK), _careful(false), _sp(0), _ps_sp(0),
66 _scratch_vector(SCRATCH_SIZE, 0), _program(0)
67 {
68 }
69
70 CharstringInterp::CharstringInterp(const Vector<double> &weight_vector)
71 : _error(errOK), _careful(false), _sp(0), _ps_sp(0),
72 _weight_vector(weight_vector),
73 _scratch_vector(SCRATCH_SIZE, 0), _program(0)
74 {
75 }
76
77 void
78 CharstringInterp::initialize()
79 {
80 clear();
81 ps_clear();
82 _lsb = _cp = _seac_origin = Point(0, 0);
83 _state = S_INITIAL;
84 _flex = false;
85 _t2nhints = 0;
86 _subr_depth = 0;
87 _done = false;
88 _error = errOK;
89 }
90
91 bool
92 CharstringInterp::interpret(const CharstringProgram *program, const Charstring *cs)
93 {
94 if (cs) {
95 initialize();
96 _program = program;
97 cs->process(*this);
98 return _error == errOK;
99 } else
100 return error(errGlyph, 0);
101 }
102
103 bool
104 CharstringInterp::error(int err, int error_data)
105 {
106 _error = err;
107 _error_data = error_data;
108 return false;
109 }
110
111 String
112 CharstringInterp::error_string(int error, int error_data)
113 {
114 static_assert(-errLastError == (sizeof(error_formats) / sizeof(error_formats[0])) - 1,
115 "errLastError defined incorrectly");
116 if (error >= 0)
117 return error_formats[0];
118 else if (error < errLastError)
119 return "charstring unknown error number " + String(error);
120 String format = error_formats[-error];
121 int percent = format.find_left('%');
122 if (percent >= 0 && format[percent + 1] == 'C')
123 return format.substring(0, percent) + Charstring::command_name(error_data) + format.substring(percent + 2);
124 else if (percent >= 0 && format[percent + 1] == 'd')
125 return format.substring(0, percent) + String(error_data) + format.substring(percent + 2);
126 else
127 return format;
128 }
129
130 bool
131 CharstringInterp::number(double v)
132 {
133 push(v);
134 return true;
135 }
136
137 void
138 CharstringInterp::fetch_weight_vector()
139 {
140 if (_program)
141 if (Vector<double> *wv = _program->mm_vector(CharstringProgram::VEC_WEIGHT, false))
142 _weight_vector = *wv;
143 }
144
145 bool
146 CharstringInterp::vector_command(int cmd)
147 {
148 CharstringProgram::VectorType which_vector;
149 int vectoroff, offset, num, i;
150 Vector<double> *v = 0;
151
152 switch (cmd) {
153
154 case Cs::cPut:
155 CHECK_STACK(2);
156 offset = (int)top(0);
157 vec(&_scratch_vector, offset) = top(1);
158 pop(2);
159 break;
160
161 case Cs::cGet:
162 CHECK_STACK(1);
163 offset = (int)top();
164 top() = vec(&_scratch_vector, offset);
165 break;
166
167 case Cs::cStore:
168 CHECK_STACK(4);
169 which_vector = (CharstringProgram::VectorType)((int)top(3));
170 vectoroff = (int)top(2);
171 offset = (int)top(1);
172 num = (int)top(0);
173 pop(4);
174
175 if (!_program)
176 return error(errVector, cmd);
177 v = _program->mm_vector(which_vector, true);
178 if (!v)
179 return error(errVector, cmd);
180
181 for (i = 0; i < num; i++, offset++, vectoroff++)
182 vec(v, vectoroff) = vec(&_scratch_vector, offset);
183 // erase our weight vector if the global weight vector has changed
184 if (which_vector == CharstringProgram::VEC_WEIGHT)
185 _weight_vector.clear();
186 break;
187
188 case Cs::cLoad:
189 CHECK_STACK(3);
190 which_vector = (CharstringProgram::VectorType)((int)top(2));
191 offset = (int)top(1);
192 num = (int)top(0);
193 pop(3);
194
195 if (!_program)
196 return error(errVector, cmd);
197 v = _program->mm_vector(which_vector, false);
198 // use our weight vector if appropriate
199 if (!v && which_vector == CharstringProgram::VEC_WEIGHT && _weight_vector.size())
200 v = &_weight_vector;
201 if (!v)
202 return error(errVector, cmd);
203
204 for (i = 0; i < num; i++, offset++)
205 vec(&_scratch_vector, offset) = vec(v, i);
206 break;
207
208 default:
209 return error(errUnimplemented, cmd);
210
211 }
212
213 return true;
214 }
215
216 bool
217 CharstringInterp::blend_command()
218 {
219 const int cmd = Cs::cBlend;
220 CHECK_STACK(1);
221 int nargs = (int)pop();
222
223 ensure_weight_vector();
224 if (!_weight_vector.size())
225 return error(errVector, cmd);
226
227 int nmasters = _weight_vector.size();
228 CHECK_STACK(nargs * nmasters);
229
230 int base = _sp - nargs * nmasters;
231 int off = base + nargs;
232 for (int j = 0; j < nargs; j++) {
233 double &val = _s[base + j];
234 for (int i = 1; i < nmasters; i++, off++)
235 val += _weight_vector.at_u(i) * _s[off];
236 }
237
238 pop(nargs * (nmasters - 1));
239 return true;
240 }
241
242 bool
243 CharstringInterp::roll_command()
244 {
245 const int cmd = Cs::cRoll;
246 CHECK_STACK(2);
247 int amount = (int)pop();
248 int n = (int)pop();
249 if (n <= 0)
250 return error(errValue, cmd);
251 CHECK_STACK(n);
252
253 int base = _sp - n;
254 while (amount < 0)
255 amount += n;
256
257 int i;
258 double copy_stack[STACK_SIZE];
259 for (i = 0; i < n; i++)
260 copy_stack[i] = _s[ base + (i+amount) % n ];
261 for (i = 0; i < n; i++)
262 _s[base + i] = copy_stack[i];
263
264 return true;
265 }
266
267 bool
268 CharstringInterp::arith_command(int cmd)
269 {
270 int i;
271 double d;
272
273 switch (cmd) {
274
275 case Cs::cBlend:
276 return blend_command();
277
278 case Cs::cAbs:
279 CHECK_STACK(1);
280 if (top() < 0)
281 top() = -top();
282 break;
283
284 case Cs::cAdd:
285 CHECK_STACK(1);
286 d = pop();
287 top() += d;
288 break;
289
290 case Cs::cSub:
291 CHECK_STACK(1);
292 d = pop();
293 top() -= d;
294 break;
295
296 case Cs::cDiv:
297 CHECK_STACK(2);
298 d = pop();
299 top() /= d;
300 break;
301
302 case Cs::cNeg:
303 CHECK_STACK(1);
304 top() = -top();
305 break;
306
307 case Cs::cRandom: {
308 double d;
309 do {
310 d = random() / ((double)RAND_MAX);
311 } while (d == 0);
312 push(d);
313 break;
314 }
315
316 case Cs::cMul:
317 CHECK_STACK(2);
318 d = pop();
319 top() *= d;
320 break;
321
322 case Cs::cSqrt:
323 CHECK_STACK(1);
324 if (top() < 0)
325 return error(errValue, cmd);
326 top() = sqrt(top());
327 break;
328
329 case Cs::cDrop:
330 CHECK_STACK(1);
331 pop();
332 break;
333
334 case Cs::cExch:
335 CHECK_STACK(2);
336 d = top(0);
337 top(0) = top(1);
338 top(1) = d;
339 break;
340
341 case Cs::cIndex:
342 CHECK_STACK(1);
343 i = (int)top();
344 if (i < 0)
345 return error(errValue, cmd);
346 CHECK_STACK(i + 2);
347 top() = top(i+1);
348 break;
349
350 case Cs::cRoll:
351 return roll_command();
352
353 case Cs::cDup:
354 CHECK_STACK(1);
355 push(top());
356 break;
357
358 case Cs::cAnd:
359 CHECK_STACK(2);
360 d = pop();
361 top() = (top() != 0) && (d != 0);
362 break;
363
364 case Cs::cOr:
365 CHECK_STACK(2);
366 d = pop();
367 top() = (top() != 0) || (d != 0);
368 break;
369
370 case Cs::cNot:
371 CHECK_STACK(1);
372 top() = (top() == 0);
373 break;
374
375 case Cs::cEq:
376 CHECK_STACK(2);
377 d = pop();
378 top() = (top() == d);
379 break;
380
381 case Cs::cIfelse:
382 CHECK_STACK(4);
383 if (top(1) > top(0))
384 top(3) = top(2);
385 pop(3);
386 break;
387
388 case Cs::cPop:
389 if (ps_size() < 1)
390 return error(errUnderflow, cmd);
391 push(ps_pop());
392 break;
393
394 case 15:
395 // this command is found with no explanation in JansonText-Roman
396 CHECK_STACK(2);
397 pop(2);
398 return true;
399
400 default:
401 return error(errUnimplemented, cmd);
402
403 }
404
405 return true;
406 }
407
408 bool
409 CharstringInterp::callsubr_command()
410 {
411 const int cmd = Cs::cCallsubr;
412 CHECK_STACK(1);
413 int which = (int)pop();
414
415 Charstring *subr_cs = get_subr(which);
416 if (!subr_cs)
417 return error(errSubr, which);
418
419 if (_subr_depth >= MAX_SUBR_DEPTH)
420 return error(errSubrDepth, which);
421 _subr_depth++;
422
423 subr_cs->process(*this);
424
425 _subr_depth--;
426 if (_error != errOK)
427 return false;
428 return !done();
429 }
430
431 bool
432 CharstringInterp::callgsubr_command()
433 {
434 const int cmd = Cs::cCallgsubr;
435 CHECK_STACK(1);
436 int which = (int)pop();
437
438 Charstring *subr_cs = get_gsubr(which);
439 if (!subr_cs)
440 return error(errSubr, which);
441
442 if (_subr_depth >= MAX_SUBR_DEPTH)
443 return error(errSubrDepth, which);
444 _subr_depth++;
445
446 subr_cs->process(*this);
447
448 _subr_depth--;
449 if (_error != errOK)
450 return false;
451 return !done();
452 }
453
454 bool
455 CharstringInterp::mm_command(int command, int on_stack)
456 {
457 ensure_weight_vector();
458 if (!_weight_vector.size())
459 return error(errVector, command);
460
461 int nargs;
462 switch (command) {
463 case Cs::othcMM1: nargs = 1; break;
464 case Cs::othcMM2: nargs = 2; break;
465 case Cs::othcMM3: nargs = 3; break;
466 case Cs::othcMM4: nargs = 4; break;
467 case Cs::othcMM6: nargs = 6; break;
468 default: return error(errInternal, command);
469 }
470
471 int nmasters = _weight_vector.size();
472 if (size() < nargs * nmasters
473 || on_stack != nargs * nmasters)
474 return error(errMultipleMaster, command);
475
476 int base = size() - on_stack;
477
478 int off = base + nargs;
479 for (int j = 0; j < nargs; j++) {
480 double &val = at(base + j);
481 for (int i = 1; i < nmasters; i++, off++)
482 val += _weight_vector.at_u(i) * at(off);
483 }
484
485 for (int i = nargs - 1; i >= 0; i--)
486 ps_push(at(base + i));
487
488 pop(on_stack);
489 return true;
490 }
491
492 bool
493 CharstringInterp::itc_command(int command, int on_stack)
494 {
495 ensure_weight_vector();
496 if (!_weight_vector.size())
497 return error(errVector, command);
498
499 int base = size() - on_stack;
500 switch (command) {
501
502 case Cs::othcITC_load: {
503 if (on_stack != 1)
504 return error(errOthersubr, command);
505 int offset = (int)at(base);
506 for (int i = 0; i < _weight_vector.size(); i++)
507 vec(&_scratch_vector, offset+i) = _weight_vector.at_u(i);
508 break;
509 }
510
511 case Cs::othcITC_put: {
512 if (on_stack != 2)
513 return error(errOthersubr, command);
514 int offset = (int)at(base+1);
515 vec(&_scratch_vector, offset) = at(base);
516 break;
517 }
518
519 case Cs::othcITC_get: {
520 if (on_stack != 1)
521 return error(errOthersubr, command);
522 int offset = (int)at(base);
523 ps_push(vec(&_scratch_vector, offset));
524 break;
525 }
526
527 case Cs::othcITC_add: {
528 if (on_stack != 2)
529 return error(errOthersubr, command);
530 ps_push(at(base) + at(base+1));
531 break;
532 }
533
534 case Cs::othcITC_sub: {
535 if (on_stack != 2)
536 return error(errOthersubr, command);
537 ps_push(at(base) - at(base+1));
538 break;
539 }
540
541 case Cs::othcITC_mul: {
542 if (on_stack != 2)
543 return error(errOthersubr, command);
544 ps_push(at(base) * at(base+1));
545 break;
546 }
547
548 case Cs::othcITC_div: {
549 if (on_stack != 2)
550 return error(errOthersubr, command);
551 ps_push(at(base) / at(base+1));
552 break;
553 }
554
555 case Cs::othcITC_ifelse: {
556 if (on_stack != 4)
557 return error(errOthersubr, command);
558 if (at(base+2) <= at(base+3))
559 ps_push(at(base));
560 else
561 ps_push(at(base+1));
562 break;
563 }
564
565 default:
566 return error(errOthersubr, command);
567
568 }
569
570 pop(on_stack);
571 return true;
572 }
573
574
575 inline void
576 CharstringInterp::actp_rmoveto(int /*cmd*/, double dx, double dy)
577 {
578 _cp.shift(dx, dy);
579 }
580
581 inline void
582 CharstringInterp::actp_rlineto(int cmd, double dx, double dy)
583 {
584 Point p0(_cp);
585 _cp.shift(dx, dy);
586 act_line(cmd, p0, _cp);
587 }
588
589 void
590 CharstringInterp::actp_rrcurveto(int cmd, double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
591 {
592 Point p0(_cp);
593 Point p1(p0, dx1, dy1);
594 Point p2(p1, dx2, dy2);
595 _cp = p2.shifted(dx3, dy3);
596 act_curve(cmd, p0, p1, p2, _cp);
597 }
598
599 void
600 CharstringInterp::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)
601 {
602 Point p0(_cp);
603 Point p1(p0, dx1, dy1);
604 Point p2(p1, dx2, dy2);
605 Point p3_4(p2, dx3, dy3);
606 Point p5(p3_4, dx4, dy4);
607 Point p6(p5, dx5, dy5);
608 _cp = p6.shifted(dx6, dy6);
609 act_flex(cmd, p0, p1, p2, p3_4, p5, p6, _cp, flex_depth);
610 }
611
612
613 bool
614 CharstringInterp::callothersubr_command(int othersubrnum, int n)
615 {
616 switch (othersubrnum) {
617
618 case Cs::othcFlexend:
619 if (n != 3)
620 goto unknown;
621 if (!_flex || ps_size() != 16)
622 return error(errFlex);
623 CHECK_PATH_START();
624 act_flex(Cs::cCallothersubr, Point(ps_at(0), ps_at(1)),
625 Point(ps_at(4), ps_at(5)), Point(ps_at(6), ps_at(7)),
626 Point(ps_at(8), ps_at(9)), Point(ps_at(10), ps_at(11)),
627 Point(ps_at(12), ps_at(13)), Point(ps_at(14), ps_at(15)),
628 top(2));
629 ps_clear();
630 ps_push(top(0));
631 ps_push(top(1));
632 _flex = false;
633 _state = S_PATH;
634 break;
635
636 case Cs::othcFlexbegin:
637 if (n != 0)
638 goto unknown;
639 if (_flex)
640 return error(errFlex);
641 ps_clear();
642 ps_push(_cp.x);
643 ps_push(_cp.y);
644 _flex = true;
645 _state = S_IPATH;
646 break;
647
648 case Cs::othcFlexmiddle:
649 if (n != 0)
650 goto unknown;
651 if (!_flex)
652 return error(errFlex);
653 ps_push(_cp.x);
654 ps_push(_cp.y);
655 break;
656
657 case Cs::othcReplacehints:
658 if (n != 1)
659 goto unknown;
660 ps_clear();
661 ps_push(top());
662 break;
663
664 case Cs::othcMM1:
665 case Cs::othcMM2:
666 case Cs::othcMM3:
667 case Cs::othcMM4:
668 case Cs::othcMM6:
669 return mm_command(othersubrnum, n);
670
671 case Cs::othcITC_load:
672 case Cs::othcITC_add:
673 case Cs::othcITC_sub:
674 case Cs::othcITC_mul:
675 case Cs::othcITC_div:
676 case Cs::othcITC_put:
677 case Cs::othcITC_get:
678 case Cs::othcITC_unknown:
679 case Cs::othcITC_ifelse:
680 case Cs::othcITC_random:
681 return itc_command(othersubrnum, n);
682
683 default: // unknown
684 unknown:
685 ps_clear();
686 for (int i = 0; i < n; i++)
687 ps_push(top(i));
688 break;
689
690 }
691
692 pop(n);
693 return true;
694 }
695
696 bool
697 CharstringInterp::type1_command(int cmd)
698 {
699 switch (cmd) {
700
701 case Cs::cReturn:
702 return false;
703
704 case Cs::cHsbw:
705 CHECK_STACK(2);
706 if (_state > S_SEAC && _careful)
707 return error(errOrdering, cmd);
708 else {
709 _lsb = _cp = _seac_origin.shifted(at(0), 0);
710 if (_state == S_INITIAL) {
711 act_sidebearing(cmd, _lsb);
712 act_width(cmd, Point(at(1), 0));
713 }
714 if (_state <= S_SEAC)
715 _state = S_SBW;
716 }
717 break;
718
719 case Cs::cSbw:
720 CHECK_STACK(4);
721 if (_state > S_SEAC && _careful)
722 return error(errOrdering, cmd);
723 else {
724 _lsb = _cp = _seac_origin.shifted(at(0), at(1));
725 if (_state == S_INITIAL) {
726 act_sidebearing(cmd, _lsb);
727 act_width(cmd, Point(at(2), at(3)));
728 }
729 if (_state <= S_SEAC)
730 _state = S_SBW;
731 }
732 break;
733
734 case Cs::cSeac:
735 CHECK_STACK(5);
736 if (_state > S_SBW)
737 return error(errOrdering, cmd);
738 act_seac(cmd, at(0), at(1), at(2), (int)at(3), (int)at(4));
739 clear();
740 return false;
741
742 case Cs::cCallsubr:
743 return callsubr_command();
744
745 case Cs::cCallothersubr: {
746 CHECK_STACK(2);
747 int othersubrnum = (int)top(0);
748 int n = (int)top(1);
749 pop(2);
750 if (othersubrnum < 0 || size() < n)
751 return error(errOthersubr, cmd);
752 return callothersubr_command(othersubrnum, n);
753 }
754
755 case Cs::cPut:
756 case Cs::cGet:
757 case Cs::cStore:
758 case Cs::cLoad:
759 return vector_command(cmd);
760
761 case Cs::cBlend:
762 case Cs::cAbs:
763 case Cs::cAdd:
764 case Cs::cSub:
765 case Cs::cDiv:
766 case Cs::cNeg:
767 case Cs::cRandom:
768 case Cs::cMul:
769 case Cs::cSqrt:
770 case Cs::cDrop:
771 case Cs::cExch:
772 case Cs::cIndex:
773 case Cs::cRoll:
774 case Cs::cDup:
775 case Cs::cAnd:
776 case Cs::cOr:
777 case Cs::cNot:
778 case Cs::cEq:
779 case Cs::cIfelse:
780 case Cs::cPop:
781 return arith_command(cmd);
782
783 case Cs::cHlineto:
784 CHECK_STACK(1);
785 CHECK_PATH_START();
786 actp_rlineto(cmd, at(0), 0);
787 break;
788
789 case Cs::cHmoveto:
790 CHECK_STACK(1);
791 CHECK_PATH_END();
792 actp_rmoveto(cmd, at(0), 0);
793 break;
794
795 case Cs::cHvcurveto:
796 CHECK_STACK(4);
797 CHECK_PATH_START();
798 actp_rrcurveto(cmd, at(0), 0, at(1), at(2), 0, at(3));
799 break;
800
801 case Cs::cRlineto:
802 CHECK_STACK(2);
803 CHECK_PATH_START();
804 actp_rlineto(cmd, at(0), at(1));
805 break;
806
807 case Cs::cRmoveto:
808 CHECK_STACK(2);
809 CHECK_PATH_END();
810 actp_rmoveto(cmd, at(0), at(1));
811 break;
812
813 case Cs::cRrcurveto:
814 CHECK_STACK(6);
815 CHECK_PATH_START();
816 actp_rrcurveto(cmd, at(0), at(1), at(2), at(3), at(4), at(5));
817 break;
818
819 case Cs::cVhcurveto:
820 CHECK_STACK(4);
821 CHECK_PATH_START();
822 actp_rrcurveto(cmd, 0, at(0), at(1), at(2), at(3), 0);
823 break;
824
825 case Cs::cVlineto:
826 CHECK_STACK(1);
827 CHECK_PATH_START();
828 actp_rlineto(cmd, 0, at(0));
829 break;
830
831 case Cs::cVmoveto:
832 CHECK_STACK(1);
833 CHECK_PATH_END();
834 actp_rmoveto(cmd, 0, at(0));
835 break;
836
837 case Cs::cDotsection:
838 break;
839
840 case Cs::cHstem:
841 CHECK_STACK(2);
842 act_hstem(cmd, _lsb.y + at(0), at(1));
843 break;
844
845 case Cs::cHstem3:
846 CHECK_STACK(6);
847 act_hstem3(cmd, _lsb.y + at(0), at(1), _lsb.y + at(2), at(3), _lsb.y + at(4), at(5));
848 break;
849
850 case Cs::cVstem:
851 CHECK_STACK(2);
852 act_vstem(cmd, _lsb.x + at(0), at(1));
853 break;
854
855 case Cs::cVstem3:
856 CHECK_STACK(6);
857 act_vstem3(cmd, _lsb.x + at(0), at(1), _lsb.x + at(2), at(3), _lsb.x + at(4), at(5));
858 break;
859
860 case Cs::cSetcurrentpoint:
861 CHECK_STACK(2);
862 _cp = Point(at(0), at(1));
863 break;
864
865 case Cs::cClosepath:
866 CHECK_PATH_END();
867 break;
868
869 case Cs::cEndchar:
870 CHECK_PATH_END();
871 set_done();
872 return false;
873
874 case Cs::cError:
875 default:
876 return error(errUnimplemented, cmd);
877
878 }
879
880 clear();
881 return error() >= 0;
882 }
883
884
885 #undef DEBUG_TYPE2
886
887 int
888 CharstringInterp::type2_handle_width(int cmd, bool have_width)
889 {
890 _cp = _lsb = _seac_origin;
891 if (_state != S_INITIAL)
892 /* ignore width */;
893 else if (have_width)
894 act_nominal_width_delta(cmd, at(0));
895 else
896 act_default_width(cmd);
897 return (have_width ? 1 : 0);
898 }
899
900 bool
901 CharstringInterp::type2_command(int cmd, const uint8_t *data, int *left)
902 {
903 int bottom = 0;
904
905 #ifdef DEBUG_TYPE2
906 fprintf(stderr, "%s [%d/%d]\n", Charstring::command_name(cmd).c_str(), _t2nhints, size());
907 #endif
908
909 switch (cmd) {
910
911 case Cs::cHstem:
912 case Cs::cHstemhm:
913 CHECK_STACK(2);
914 if (_state <= S_SEAC)
915 bottom = type2_handle_width(cmd, (size() % 2) == 1);
916 if (_state > S_HSTEM)
917 return error(errOrdering, cmd);
918 _state = S_HSTEM;
919 for (double pos = 0; bottom + 1 < size(); bottom += 2) {
920 _t2nhints++;
921 act_hstem(cmd, pos + at(bottom), at(bottom + 1));
922 pos += at(bottom) + at(bottom + 1);
923 }
924 break;
925
926 case Cs::cVstem:
927 case Cs::cVstemhm:
928 CHECK_STACK(2);
929 if (_state <= S_SEAC)
930 bottom = type2_handle_width(cmd, (size() % 2) == 1);
931 if (_state > S_VSTEM)
932 return error(errOrdering, cmd);
933 _state = S_VSTEM;
934 for (double pos = 0; bottom + 1 < size(); bottom += 2) {
935 _t2nhints++;
936 act_vstem(cmd, pos + at(bottom), at(bottom + 1));
937 pos += at(bottom) + at(bottom + 1);
938 }
939 break;
940
941 case Cs::cHintmask:
942 case Cs::cCntrmask:
943 if (_state <= S_SEAC && size() >= 1) {
944 bottom = type2_handle_width(cmd, (size() % 2) == 1);
945 for (double pos = 0; bottom + 1 < size(); bottom += 2) {
946 _t2nhints++;
947 act_hstem(cmd, pos + at(bottom), at(bottom + 1));
948 pos += at(bottom) + at(bottom + 1);
949 }
950 }
951 if ((_state == S_HSTEM || _state == S_VSTEM) && size() >= 2)
952 for (double pos = 0; bottom + 1 < size(); bottom += 2) {
953 _t2nhints++;
954 act_vstem(cmd, pos + at(bottom), at(bottom + 1));
955 pos += at(bottom) + at(bottom + 1);
956 }
957 if (_state < S_HINTMASK)
958 _state = S_HINTMASK;
959 if (_t2nhints == 0)
960 return error(errHintmask, cmd);
961 if (!data || !left)
962 return error(errInternal, cmd);
963 if (((_t2nhints - 1) >> 3) + 1 > *left)
964 return error(errRunoff, cmd);
965 act_hintmask(cmd, data, _t2nhints);
966 *left -= ((_t2nhints - 1) >> 3) + 1;
967 break;
968
969 case Cs::cRmoveto:
970 CHECK_STACK(2);
971 if (_state <= S_SEAC)
972 bottom = type2_handle_width(cmd, size() > 2);
973 CHECK_PATH_END();
974 actp_rmoveto(cmd, at(bottom), at(bottom + 1));
975 #if DEBUG_TYPE2
976 bottom += 2;
977 #endif
978 break;
979
980 case Cs::cHmoveto:
981 CHECK_STACK(1);
982 if (_state <= S_SEAC)
983 bottom = type2_handle_width(cmd, size() > 1);
984 CHECK_PATH_END();
985 actp_rmoveto(cmd, at(bottom), 0);
986 #if DEBUG_TYPE2
987 bottom++;
988 #endif
989 break;
990
991 case Cs::cVmoveto:
992 CHECK_STACK(1);
993 if (_state <= S_SEAC)
994 bottom = type2_handle_width(cmd, size() > 1);
995 CHECK_PATH_END();
996 actp_rmoveto(cmd, 0, at(bottom));
997 #if DEBUG_TYPE2
998 bottom++;
999 #endif
1000 break;
1001
1002 case Cs::cRlineto:
1003 CHECK_STACK(2);
1004 CHECK_STATE();
1005 CHECK_PATH_START();
1006 for (; bottom + 1 < size(); bottom += 2)
1007 actp_rlineto(cmd, at(bottom), at(bottom + 1));
1008 break;
1009
1010 case Cs::cHlineto:
1011 CHECK_STACK(1);
1012 CHECK_STATE();
1013 CHECK_PATH_START();
1014 while (bottom < size()) {
1015 actp_rlineto(cmd, at(bottom++), 0);
1016 if (bottom < size())
1017 actp_rlineto(cmd, 0, at(bottom++));
1018 }
1019 break;
1020
1021 case Cs::cVlineto:
1022 CHECK_STACK(1);
1023 CHECK_STATE();
1024 CHECK_PATH_START();
1025 while (bottom < size()) {
1026 actp_rlineto(cmd, 0, at(bottom++));
1027 if (bottom < size())
1028 actp_rlineto(cmd, at(bottom++), 0);
1029 }
1030 break;
1031
1032 case Cs::cRrcurveto:
1033 CHECK_STACK(6);
1034 CHECK_STATE();
1035 CHECK_PATH_START();
1036 for (; bottom + 5 < size(); bottom += 6)
1037 actp_rrcurveto(cmd, at(bottom), at(bottom + 1), at(bottom + 2), at(bottom + 3), at(bottom + 4), at(bottom + 5));
1038 break;
1039
1040 case Cs::cHhcurveto:
1041 CHECK_STACK(4);
1042 CHECK_STATE();
1043 CHECK_PATH_START();
1044 if (size() % 2 == 1) {
1045 actp_rrcurveto(cmd, at(bottom + 1), at(bottom), at(bottom + 2), at(bottom + 3), at(bottom + 4), 0);
1046 bottom += 5;
1047 }
1048 for (; bottom + 3 < size(); bottom += 4)
1049 actp_rrcurveto(cmd, at(bottom), 0, at(bottom + 1), at(bottom + 2), at(bottom + 3), 0);
1050 break;
1051
1052 case Cs::cHvcurveto:
1053 CHECK_STACK(4);
1054 CHECK_STATE();
1055 CHECK_PATH_START();
1056 while (bottom + 3 < size()) {
1057 double dx3 = (bottom + 5 == size() ? at(bottom + 4) : 0);
1058 actp_rrcurveto(cmd, at(bottom), 0, at(bottom + 1), at(bottom + 2), dx3, at(bottom + 3));
1059 bottom += 4;
1060 if (bottom + 3 < size()) {
1061 double dy3 = (bottom + 5 == size() ? at(bottom + 4) : 0);
1062 actp_rrcurveto(cmd, 0, at(bottom), at(bottom + 1), at(bottom + 2), at(bottom + 3), dy3);
1063 bottom += 4;
1064 }
1065 }
1066 #if DEBUG_TYPE2
1067 if (bottom + 1 == size())
1068 bottom++;
1069 #endif
1070 break;
1071
1072 case Cs::cRcurveline:
1073 CHECK_STACK(8);
1074 CHECK_STATE();
1075 CHECK_PATH_START();
1076 for (; bottom + 7 < size(); bottom += 6)
1077 actp_rrcurveto(cmd, at(bottom), at(bottom + 1), at(bottom + 2), at(bottom + 3), at(bottom + 4), at(bottom + 5));
1078 actp_rlineto(cmd, at(bottom), at(bottom + 1));
1079 #if DEBUG_TYPE2
1080 bottom += 2;
1081 #endif
1082 break;
1083
1084 case Cs::cRlinecurve:
1085 CHECK_STACK(8);
1086 CHECK_STATE();
1087 CHECK_PATH_START();
1088 for (; bottom + 7 < size(); bottom += 2)
1089 actp_rlineto(cmd, at(bottom), at(bottom + 1));
1090 actp_rrcurveto(cmd, at(bottom), at(bottom + 1), at(bottom + 2), at(bottom + 3), at(bottom + 4), at(bottom + 5));
1091 #if DEBUG_TYPE2
1092 bottom += 6;
1093 #endif
1094 break;
1095
1096 case Cs::cVhcurveto:
1097 CHECK_STACK(4);
1098 CHECK_STATE();
1099 CHECK_PATH_START();
1100 while (bottom + 3 < size()) {
1101 double dy3 = (bottom + 5 == size() ? at(bottom + 4) : 0);
1102 actp_rrcurveto(cmd, 0, at(bottom), at(bottom + 1), at(bottom + 2), at(bottom + 3), dy3);
1103 bottom += 4;
1104 if (bottom + 3 < size()) {
1105 double dx3 = (bottom + 5 == size() ? at(bottom + 4) : 0);
1106 actp_rrcurveto(cmd, at(bottom), 0, at(bottom + 1), at(bottom + 2), dx3, at(bottom + 3));
1107 bottom += 4;
1108 }
1109 }
1110 #if DEBUG_TYPE2
1111 if (bottom + 1 == size())
1112 bottom++;
1113 #endif
1114 break;
1115
1116 case Cs::cVvcurveto:
1117 CHECK_STACK(4);
1118 CHECK_STATE();
1119 CHECK_PATH_START();
1120 if (size() % 2 == 1) {
1121 actp_rrcurveto(cmd, at(bottom), at(bottom + 1), at(bottom + 2), at(bottom + 3), 0, at(bottom + 4));
1122 bottom += 5;
1123 }
1124 for (; bottom + 3 < size(); bottom += 4)
1125 actp_rrcurveto(cmd, 0, at(bottom), at(bottom + 1), at(bottom + 2), 0, at(bottom + 3));
1126 break;
1127
1128 case Cs::cFlex:
1129 CHECK_STACK(13);
1130 CHECK_STATE();
1131 CHECK_PATH_START();
1132 assert(bottom == 0);
1133 actp_rrflex(cmd,
1134 at(0), at(1), at(2), at(3), at(4), at(5),
1135 at(6), at(7), at(8), at(9), at(10), at(11),
1136 at(12));
1137 #if DEBUG_TYPE2
1138 bottom += 13;
1139 #endif
1140 break;
1141
1142 case Cs::cHflex:
1143 CHECK_STACK(7);
1144 CHECK_STATE();
1145 CHECK_PATH_START();
1146 assert(bottom == 0);
1147 actp_rrflex(cmd,
1148 at(0), 0, at(1), at(2), at(3), 0,
1149 at(4), 0, at(5), -at(2), at(6), 0,
1150 50);
1151 #if DEBUG_TYPE2
1152 bottom += 7;
1153 #endif
1154 break;
1155
1156 case Cs::cHflex1:
1157 CHECK_STACK(9);
1158 CHECK_STATE();
1159 CHECK_PATH_START();
1160 assert(bottom == 0);
1161 actp_rrflex(cmd,
1162 at(0), at(1), at(2), at(3), at(4), 0,
1163 at(5), 0, at(6), at(7), at(8), -(at(1) + at(3) + at(7)),
1164 50);
1165 #if DEBUG_TYPE2
1166 bottom += 9;
1167 #endif
1168 break;
1169
1170 case Cs::cFlex1: {
1171 CHECK_STACK(11);
1172 CHECK_STATE();
1173 CHECK_PATH_START();
1174 assert(bottom == 0);
1175 double dx = at(0) + at(2) + at(4) + at(6) + at(8);
1176 double dy = at(1) + at(3) + at(5) + at(7) + at(9);
1177 if (fabs(dx) > fabs(dy))
1178 actp_rrflex(cmd,
1179 at(0), at(1), at(2), at(3), at(4), at(5),
1180 at(6), at(7), at(8), at(9), at(10), -dy,
1181 50);
1182 else
1183 actp_rrflex(cmd,
1184 at(0), at(1), at(2), at(3), at(4), at(5),
1185 at(6), at(7), at(8), at(9), -dx, at(10),
1186 50);
1187 break;
1188 #if DEBUG_TYPE2
1189 bottom += 11;
1190 #endif
1191 }
1192
1193 case Cs::cEndchar:
1194 if (_state <= S_SEAC)
1195 bottom = type2_handle_width(cmd, size() > 0 && size() != 4);
1196 if (bottom + 3 < size() && _state == S_INITIAL)
1197 act_seac(cmd, 0, at(bottom), at(bottom + 1), (int)at(bottom + 2), (int)at(bottom + 3));
1198 CHECK_PATH_END();
1199 set_done();
1200 clear();
1201 return false;
1202
1203 case Cs::cReturn:
1204 return false;
1205
1206 case Cs::cCallsubr:
1207 return callsubr_command();
1208
1209 case Cs::cCallgsubr:
1210 return callgsubr_command();
1211
1212 case Cs::cPut:
1213 case Cs::cGet:
1214 case Cs::cStore:
1215 case Cs::cLoad:
1216 return vector_command(cmd);
1217
1218 case Cs::cBlend:
1219 case Cs::cAbs:
1220 case Cs::cAdd:
1221 case Cs::cSub:
1222 case Cs::cDiv:
1223 case Cs::cNeg:
1224 case Cs::cRandom:
1225 case Cs::cMul:
1226 case Cs::cSqrt:
1227 case Cs::cDrop:
1228 case Cs::cExch:
1229 case Cs::cIndex:
1230 case Cs::cRoll:
1231 case Cs::cDup:
1232 case Cs::cAnd:
1233 case Cs::cOr:
1234 case Cs::cNot:
1235 case Cs::cEq:
1236 case Cs::cIfelse:
1237 case Cs::cPop:
1238 return arith_command(cmd);
1239
1240 case Cs::cDotsection:
1241 break;
1242
1243 case Cs::cError:
1244 default:
1245 return error(errUnimplemented, cmd);
1246
1247 }
1248
1249 #if DEBUG_TYPE2
1250 if (bottom != size())
1251 fprintf(stderr, "[left %d on stack] ", size() - bottom);
1252 #endif
1253
1254 clear();
1255 return error() >= 0;
1256 }
1257
1258
1259 void
1260 CharstringInterp::act_sidebearing(int, const Point &)
1261 {
1262 /* do nothing */
1263 }
1264
1265 void
1266 CharstringInterp::act_width(int, const Point &)
1267 {
1268 /* do nothing */
1269 }
1270
1271 void
1272 CharstringInterp::act_default_width(int cmd)
1273 {
1274 double d = (_program ? _program->global_width_x(false) : UNKDOUBLE);
1275 if (KNOWN(d))
1276 act_width(cmd, Point(d, 0));
1277 }
1278
1279 void
1280 CharstringInterp::act_nominal_width_delta(int cmd, double delta)
1281 {
1282 double d = (_program ? _program->global_width_x(true) : UNKDOUBLE);
1283 if (KNOWN(d))
1284 act_width(cmd, Point(d + delta, 0));
1285 }
1286
1287 void
1288 CharstringInterp::act_seac(int cmd, double asb, double adx, double ady, int bchar, int achar)
1289 {
1290 Charstring *acs = 0, *bcs = 0;
1291 if (achar < 0 || achar >= 256 || bchar < 0 || bchar >= 256
1292 || !_program || _program->parent_program()
1293 || !(acs = get_glyph(Charstring::standard_encoding[achar]))
1294 || !(bcs = get_glyph(Charstring::standard_encoding[bchar]))) {
1295 error(errGlyph, cmd);
1296 return;
1297 }
1298
1299 Point apos = Point(adx + _lsb.x - asb, ady + _lsb.y);
1300 Point save_lsb = _lsb;
1301 Point save_seac_origin = _seac_origin;
1302
1303 CharstringInterp::initialize();
1304 _seac_origin = apos;
1305 _state = S_SEAC;
1306 acs->process(*this);
1307 if (error() == errOK) {
1308 CharstringInterp::initialize();
1309 _seac_origin = save_seac_origin;
1310 _state = S_SEAC;
1311 bcs->process(*this);
1312 }
1313
1314 _lsb = save_lsb;
1315 }
1316
1317 void
1318 CharstringInterp::act_line(int cmd, const Point &p0, const Point &p1)
1319 {
1320 act_curve(cmd, p0, p0, p1, p1);
1321 }
1322
1323 void
1324 CharstringInterp::act_curve(int cmd, const Point &, const Point &, const Point &, const Point &)
1325 {
1326 error(errUnimplemented, cmd);
1327 }
1328
1329 void
1330 CharstringInterp::act_flex(int cmd, const Point &p0, const Point &p1, const Point &p2, const Point &p3_4, const Point &p5, const Point &p6, const Point &p7, double flex_depth)
1331 {
1332 (void) flex_depth;
1333 act_curve(cmd, p0, p1, p2, p3_4);
1334 act_curve(cmd, p3_4, p5, p6, p7);
1335 }
1336
1337 void
1338 CharstringInterp::act_closepath(int)
1339 {
1340 /* do nothing */
1341 }
1342
1343 void
1344 CharstringInterp::act_hstem(int, double, double)
1345 {
1346 /* do nothing */
1347 }
1348
1349 void
1350 CharstringInterp::act_vstem(int, double, double)
1351 {
1352 /* do nothing */
1353 }
1354
1355 void
1356 CharstringInterp::act_hstem3(int cmd, double y0, double dy0, double y1, double dy1, double y2, double dy2)
1357 {
1358 act_hstem(cmd, y0, dy0);
1359 act_hstem(cmd, y1, dy1);
1360 act_hstem(cmd, y2, dy2);
1361 }
1362
1363 void
1364 CharstringInterp::act_vstem3(int cmd, double x0, double dx0, double x1, double dx1, double x2, double dx2)
1365 {
1366 act_vstem(cmd, x0, dx0);
1367 act_vstem(cmd, x1, dx1);
1368 act_vstem(cmd, x2, dx2);
1369 }
1370
1371 void
1372 CharstringInterp::act_hintmask(int, const uint8_t *, int)
1373 {
1374 /* do nothing */
1375 }
1376
1377 }