Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/cffwrite/cffwrite_t2cstr.c

raw
1 /* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
2 This software is licensed as OpenSource, under the Apache License, Version 2.0.
3 This license is available at: http://opensource.org/licenses/Apache-2.0. */
4
5 /*
6 * Charstring support.
7 */
8
9 #include "cffwrite_t2cstr.h"
10 #include "cffwrite_subr.h"
11 #include "txops.h"
12 #include "ctutil.h"
13
14 #include <string.h>
15 #include <stdlib.h>
16 #include <math.h>
17
18 /* Make up operators for internal use */
19 #define tx_noop tx_reserved0
20 #define t2_cntroff t2_reservedESC33
21
22 /* Push value on stack */
23 #define PUSH(v) (h->stack.array[h->stack.cnt++] = (float)(v))
24 #define PUSH_DELTA(v) (h->deltaStack.array[h->deltaStack.cnt++] = (float)(v))
25
26 #define TC_MAX_WARNINGS 5
27
28 /* ----------------------------- Module Context ---------------------------- */
29
30 typedef struct /* Stem hint */
31 {
32 abfBlendArg edge0v;
33 abfBlendArg edge1v;
34 unsigned char id; /* Unique identifier */
35 char flags;
36 #define STEM_VERT (1 << 0) /* Flags vertical stem */
37 #define STEM_CNTR (1 << 1) /* Flags counter stem */
38 } Stem;
39
40 typedef struct /* Hint record */
41 {
42 long iCstr; /* Hint/cntrmask operator index */
43 long iMask; /* Index of first mask byte */
44 long size; /* Size of mask */
45 } Hint;
46
47 typedef struct /* Cntr record */
48 {
49 long iMask; /* Index of first mask byte */
50 long size; /* Size of mask */
51 } Cntr;
52
53 enum /* Warning types */
54 {
55 warn_move0,
56 warn_move1,
57 warn_move2,
58 warn_hint0,
59 warn_hint1,
60 warn_hint2,
61 warn_hint3,
62 warn_hint4,
63 warn_hint5,
64 warn_hint6,
65 warn_hint7,
66 warn_flex0,
67 warn_flex1,
68 warn_dup0,
69 warn_dup1,
70 warn_cnt
71 };
72
73 /* Hintmask */
74 #define MAX_MASK_BYTES ((T2_MAX_STEMS + 7) / 8)
75 typedef char HintMask[MAX_MASK_BYTES];
76 #define SET_HINT_BIT(a, i) ((a)[(i) / 8] |= 1 << (7 - (i) % 8))
77 #define CLEAR_MASK(a) memset(a, 0, MAX_MASK_BYTES)
78
79 /* Stem id mask */
80 #define SET_ID_BIT(a, i) ((a)[(i) / 8] |= 1 << ((i) % 8))
81 #define TEST_ID_BIT(a, i) ((a)[(i) / 8] & 1 << ((i) % 8))
82
83 struct cstrCtx_ {
84 long flags; /* Control flags */
85 #define SEEN_MOVETO (1 << 0) /* Seen moveto operator */
86 #define SEEN_HINT (1 << 1) /* Seen hint subs */
87 #define SEEN_CNTR (1 << 2) /* Seen counter */
88 #define SEEN_WARN (1 << 3) /* Seen cstr warning */
89 #define SEEN_BLEND (1 << 4) /* Seen blend */
90 int pendop; /* Pending op */
91 int seqop; /* Pending sequence op */
92 struct /* Operand stack */
93 {
94 int cnt;
95 float array[CFF2_MAX_OP_STACK];
96 } stack;
97 struct /* Operand stack */
98 {
99 int cnt;
100 float array[CFF2_MAX_OP_STACK];
101 } deltaStack;
102 int numBlends;
103 unsigned short maxstack;
104 int flushed_cnt; /* Number of values flushed out and stored on the client's stack */
105 float x; /* Current x-coordinate */
106 float y; /* Current y-coordinate */
107 float start_x; /* x-coordinate of current path initial moveto */
108 float start_y; /* y-coordinate of current path initial moveto */
109 dnaDCL(char, cstr); /* Charstring accumulator */
110 struct /* Hint stems */
111 {
112 int cnt;
113 Stem array[T2_MAX_STEMS];
114 } stems;
115 long iCstr; /* Current hintmask charstring index */
116 dnaDCL(char, masks); /* Idmasks */
117 dnaDCL(Hint, hints); /* Hint records */
118 dnaDCL(Cntr, cntrs); /* Counter records */
119 HintMask lastmask; /* Last hintmask */
120 HintMask initmask; /* Initial idmask */
121 HintMask hintmask; /* Hint idmask */
122 HintMask cntrmask; /* Counter idmask */
123 int masksize; /* Final hint/cntr mask size (bytes) */
124 struct /* Glyph data */
125 {
126 abfGlyphInfo *info;
127 float hAdv;
128 } glyph;
129 long tmpoff; /* Temporary file offset */
130 unsigned long unique; /* Unique subroutinizer separator value */
131 unsigned short warning[warn_cnt]; /* Warning accumulator */
132 cfwCtx g; /* Package context */
133 long glyphwarning; /* Warning per glyph */
134 };
135
136 static void flushBlends(cstrCtx h);
137 static void tmp_savefixed(cfwCtx g, Fixed f);
138 static Fixed float2Fixed(float r);
139 static void tmp_saveop(cfwCtx g, int op);
140
141 /* Initialize module. */
142 void cfwCstrNew(cfwCtx g) {
143 cstrCtx h = (cstrCtx)cfwMemNew(g, sizeof(struct cstrCtx_));
144
145 memset(h, 0, sizeof(*h));
146
147 /* Review with Ariza */
148 DURING_EX(g->err.env)
149 /* Link contexts */
150 h->g = g;
151 g->ctx.cstr = h;
152
153 dnaINIT(g->ctx.dnaFail, h->cstr, 500, 5000);
154 dnaINIT(g->ctx.dnaFail, h->masks, 30, 60);
155 dnaINIT(g->ctx.dnaFail, h->hints, 10, 40);
156 dnaINIT(g->ctx.dnaFail, h->cntrs, 1, 10);
157
158 /* Open tmp stream */
159 g->stm.tmp = g->cb.stm.open(&g->cb.stm, CFW_TMP_STREAM_ID, 0);
160 if (g->stm.tmp == NULL) {
161 cfwFatal(g, cfwErrTmpStream, NULL);
162 }
163 h->tmpoff = 0;
164
165 /* Initialize warning accumulator */
166 memset(h->warning, 0, sizeof(h->warning));
167
168 /* Used as charstring separator for subroutinizer */
169 h->unique = 0;
170 HANDLER
171 cfwMemFree(g, h);
172 RERAISE;
173 END_HANDLER
174 }
175
176 /* Prepare module for reuse. */
177 void cfwCstrReuse(cfwCtx g) {
178 cstrCtx h = g->ctx.cstr;
179 if (g->cb.stm.seek(&g->cb.stm, g->stm.tmp, 0)) {
180 cfwFatal(g, cfwErrTmpStream, NULL);
181 }
182 h->tmpoff = 0;
183 }
184
185 /* Free resources. */
186 void cfwCstrFree(cfwCtx g) {
187 cstrCtx h = g->ctx.cstr;
188
189 if (h == NULL) {
190 return;
191 }
192 dnaFREE(h->cstr);
193 dnaFREE(h->masks);
194 dnaFREE(h->hints);
195 dnaFREE(h->cntrs);
196
197 /* Close tmp stream */
198 if (g->cb.stm.close(&g->cb.stm, g->stm.tmp)) {
199 cfwFatal(g, cfwErrTmpStream, NULL);
200 }
201
202 cfwMemFree(g, g->ctx.cstr);
203 g->ctx.cstr = NULL;
204 }
205
206 /* Print charstring warning. */
207 static void addWarn(cstrCtx h, int type) {
208 h->warning[type]++;
209 h->flags |= SEEN_WARN;
210 h->glyphwarning |= 1 << type;
211 }
212
213 /* Begin new glyph definition. */
214 static int glyphBeg(abfGlyphCallbacks *cb, abfGlyphInfo *info) {
215 cfwCtx g = (cfwCtx)cb->direct_ctx;
216 cstrCtx h = g->ctx.cstr;
217
218 cb->info = info;
219 if (g->err.code != 0) {
220 if (g->flags & CFW_CHECK_IF_GLYPHS_DIFFER) {
221 if (g->err.code & cfwErrGlyphPresent) {
222 g->err.code = g->err.code & ~cfwErrGlyphPresent;
223 }
224
225 if (g->err.code & cfwErrGlyphDiffers) {
226 g->err.code = g->err.code & ~cfwErrGlyphDiffers;
227 }
228
229 if (g->err.code != 0) {
230 return ABF_FAIL_RET; /* Error on last glyph */
231 }
232 } else {
233 return ABF_FAIL_RET; /* Error on last glyph */
234 }
235 } else if ((info->flags & ABF_GLYPH_SEEN) && (!(g->flags & CFW_CHECK_IF_GLYPHS_DIFFER))) {
236 return ABF_SKIP_RET; /* Ignore duplicate glyph */
237 } else if (!(info->flags & ABF_GLYPH_CID) &&
238 (info->gname.ptr == NULL || info->gname.ptr[0] == '\0')) {
239 return ABF_FAIL_RET; /* No glyph name in name-keyed font */
240 }
241 /* Initialize */
242 h->flags = 0;
243 h->pendop = tx_noop;
244 h->seqop = tx_noop;
245 h->x = 0;
246 h->y = 0;
247 if (g->flags & CFW_WRITE_CFF2)
248 h->maxstack = CFF2_MAX_OP_STACK;
249 else
250 h->maxstack = T2_MAX_OP_STACK;
251 h->flushed_cnt = 0;
252 h->stack.cnt = 0;
253 h->deltaStack.cnt = 0;
254 h->numBlends = 0;
255 h->cstr.cnt = 0;
256 h->stems.cnt = 0;
257 h->iCstr = -1;
258 h->masks.cnt = 0;
259 h->hints.cnt = 0;
260 h->cntrs.cnt = 0;
261 CLEAR_MASK(h->initmask);
262 CLEAR_MASK(h->hintmask);
263 CLEAR_MASK(h->cntrmask);
264 h->glyph.info = info;
265 h->glyphwarning = 0;
266
267 g->glyph_metrics.cb.beg(&g->glyph_metrics.cb, info);
268
269 return ABF_CONT_RET;
270 }
271
272 /* Add horizontal advance width. */
273 static void glyphWidth(abfGlyphCallbacks *cb, float hAdv) {
274 cfwCtx g = (cfwCtx)cb->direct_ctx;
275 cstrCtx h = g->ctx.cstr;
276 h->glyph.hAdv = roundf(hAdv);
277
278 g->glyph_metrics.cb.width(&g->glyph_metrics.cb, hAdv);
279 }
280
281 /* Save number in charstring. */
282 static void cstr_savenum(cstrCtx h, float r) {
283 long i;
284 unsigned char *t;
285 if (h->cstr.cnt + 4 >= h->cstr.size) {
286 /* Grow buffer to accommodate maximum number length */
287 if (dnaGrow(&h->cstr, 1, h->cstr.cnt + 4)) {
288 h->g->err.code = cfwErrNoMemory;
289 return;
290 }
291 }
292 t = (unsigned char *)&h->cstr.array[h->cstr.cnt];
293 i = (long)r;
294 h->cstr.cnt += ((i == r) ? cfwEncInt(i, t) : cfwEncReal(r, t));
295 }
296
297 /* Save op code in charstring. */
298 static void cstr_saveop(cstrCtx h, int op) {
299 if (h->cstr.cnt + 1 >= h->cstr.size) {
300 /* Grow buffer to accommodate maximum op size */
301 if (dnaGrow(&h->cstr, 1, h->cstr.cnt + 1)) {
302 h->g->err.code = cfwErrNoMemory;
303 return;
304 }
305 }
306 if (op & 0xff00) {
307 h->cstr.array[h->cstr.cnt++] = tx_escape;
308 }
309 h->cstr.array[h->cstr.cnt++] = (unsigned char)op;
310 }
311
312 /* Save operator and args to charstring. Return 1 on error else 0. */
313 static void saveop(cstrCtx h, int op) {
314 if (h->numBlends > 0)
315 flushBlends(h);
316
317 else if (h->stack.cnt != 0) {
318 /* Save args */
319 int i;
320 for (i = 0; i < h->stack.cnt; i++) {
321 cstr_savenum(h, h->stack.array[i]);
322 }
323 h->stack.cnt = 0;
324 }
325 h->flushed_cnt = 0;
326
327 /* Save op */
328 switch (op) {
329 case t2_blend:
330 cfwMessage(h->g, "CFF2 error - unexpected blend op <%s>",
331 h->glyph.info->gname.ptr);
332 break;
333
334 case tx_hlineto:
335 case tx_vlineto:
336 case tx_hvcurveto:
337 case tx_vhcurveto:
338 cstr_saveop(h, h->seqop);
339 h->pendop = tx_noop; /* Clear pending op */
340 break;
341
342 default:
343 cstr_saveop(h, op);
344 h->pendop = tx_noop; /* Clear pending op */
345 break;
346 }
347 }
348
349 static void saveopDirect(cstrCtx h, int op) {
350 if (h->numBlends > 0)
351 flushBlends(h);
352
353 else if (h->stack.cnt != 0) {
354 /* Save args */
355 int i;
356 for (i = 0; i < h->stack.cnt; i++) {
357 cstr_savenum(h, h->stack.array[i]);
358 }
359 h->stack.cnt = 0;
360 }
361
362 h->flushed_cnt = 0;
363 cstr_saveop(h, op);
364
365 h->pendop = tx_noop; /* Clear pending op */
366 }
367
368 /* Returns 1 if argcnt plus blended args may overflow the operand stack */
369 static int checkOverflowByArg(cstrCtx h, int argcnt)
370 {
371 int blend_arg_cnt = h->glyph.info->blendInfo.numRegions;
372 if (blend_arg_cnt > 0)
373 {
374 blend_arg_cnt *= argcnt;
375 blend_arg_cnt++; /* number of args */
376 }
377 return (h->stack.cnt + h->flushed_cnt + argcnt + h->deltaStack.cnt + blend_arg_cnt
378 + ((h->g->flags & CFW_SUBRIZE) ? 1 : 0)) > h->maxstack;
379 }
380
381 /* Returns 1 if a new set of default and delta values pushed to the delta stack will overflow the operand stack */
382 static int checkOverflow(cstrCtx h)
383 {
384 return (h->stack.cnt + h->flushed_cnt + h->deltaStack.cnt + 1 + h->glyph.info->blendInfo.numRegions + 1
385 + ((h->g->flags & CFW_SUBRIZE) ? 1 : 0)) > h->maxstack;
386 }
387
388 /* Check stack headroom and flush pending op if next op causes overflow.
389 When subroutinizer is enabled, reserve one extra space for a subr number.
390 Return 1 on error else 0. */
391 static void flushop(cstrCtx h, int argcnt) {
392 if (checkOverflowByArg(h, argcnt)) {
393 saveop(h, h->pendop);
394 }
395 }
396
397 /* Discard pending operator. */
398 static void clearop(cstrCtx h) {
399 h->stack.cnt = 0;
400 h->pendop = tx_noop;
401 }
402
403 /* Discard pending moveto operator. */
404 static void clearmoveto(cstrCtx h, float dx, float dy) {
405 /* Restore current point */
406 h->x -= dx;
407 h->y -= dy;
408 h->x = (float)RND_ON_WRITE(h->x);
409 h->y = (float)RND_ON_WRITE(h->y);
410
411 addWarn(h, warn_move2);
412 clearop(h);
413 }
414
415 /* Add move to path. */
416 static void glyphMove(abfGlyphCallbacks *cb, float x0, float y0) {
417 cfwCtx g = (cfwCtx)cb->direct_ctx;
418 cstrCtx h = g->ctx.cstr;
419
420 float dx0;
421 float dy0;
422 int doOptimize = !(g->flags & CFW_NO_OPTIMIZATION);
423
424 x0 = (float)RND_ON_WRITE(x0); // need to round to 2 decimal places, else get cumulative error when reading the relative coords.
425 y0 = (float)RND_ON_WRITE(y0);
426 dx0 = x0 - h->x;
427 dy0 = y0 - h->y;
428
429 /* Check pending op */
430 switch (h->pendop) {
431 case tx_vmoveto:
432 clearmoveto(h, 0, h->stack.array[0]);
433 break;
434
435 case tx_hmoveto:
436 clearmoveto(h, h->stack.array[0], 0);
437 break;
438
439 case tx_rmoveto:
440 clearmoveto(h, h->stack.array[0], h->stack.array[1]);
441 break;
442
443 case tx_dotsection:
444 /* Discard dotsection before closepath */
445 clearop(h);
446 break;
447
448 case tx_noop:
449 break;
450
451 default:
452 saveop(h, h->pendop);
453 break;
454 }
455
456 if ((!doOptimize) && (h->flags & SEEN_MOVETO)) /* add closing line-to, if last op is not the same as the starting moveto. */
457 {
458 if ((h->x != h->start_x) || (h->y != h->start_y)) {
459 dx0 = h->start_x - h->x;
460 dy0 = h->start_y - h->y;
461 PUSH(dx0);
462 PUSH(dy0);
463 h->pendop = tx_rlineto;
464 saveop(h, h->pendop);
465 h->x = h->start_x;
466 h->y = h->start_y;
467 }
468 }
469
470 /* Compute delta and update current point */
471 dx0 = x0 - h->x;
472 dy0 = y0 - h->y;
473 h->x = h->start_x = x0;
474 h->y = h->start_y = y0;
475
476 g->glyph_metrics.cb.move(&g->glyph_metrics.cb, x0, y0);
477
478 /* Choose format */
479 if ((dx0 == 0.0) && doOptimize) {
480 /* - dy0 vmoveto */
481 /* 0 */ PUSH(dy0);
482 h->pendop = tx_vmoveto;
483 } else if ((dy0 == 0.0) && doOptimize) {
484 /* dx0 - hmoveto */
485 PUSH(dx0); /* 0 */
486 h->pendop = tx_hmoveto;
487 } else {
488 /* dx0 dy0 rmoveto */
489 PUSH(dx0);
490 PUSH(dy0);
491 h->pendop = tx_rmoveto;
492 }
493 h->flags |= SEEN_MOVETO;
494 }
495
496 static void pushBlendDeltas(cstrCtx h, abfBlendArg *blendArg) {
497 int j;
498 for (j = 0; j < h->glyph.info->blendInfo.numRegions; j++) {
499 float val = blendArg->blendValues[j];
500 PUSH_DELTA(val);
501 }
502 }
503
504 static void flushBlends(cstrCtx h) {
505 int i;
506 if ((h->deltaStack.cnt + 1 + h->stack.cnt + h->flushed_cnt
507 + ((h->g->flags & CFW_SUBRIZE) ? 1 : 0)) > CFF2_MAX_OP_STACK) {
508 cfwCtx g = h->g;
509 cfwFatal(g, cfwErrStackOverflow, "Blend overflow");
510 }
511
512 if (!(h->flags & SEEN_BLEND)) {
513 h->flags |= SEEN_BLEND;
514 if (h->glyph.info->blendInfo.vsindex) {
515 cstr_savenum(h, h->glyph.info->blendInfo.vsindex);
516 cstr_saveop(h, t2_vsindex);
517 }
518 }
519
520 /* Note that even after numbers are flushed out of our stack, non-blended values and blended values
521 * will still occupy space on the client's stack so need to be taken into account during overflow check
522 * until a stack-clearing operator is issued.
523 */
524 h->flushed_cnt += h->stack.cnt + h->numBlends;
525
526 for (i = 0; i < h->deltaStack.cnt; i++)
527 PUSH(h->deltaStack.array[i]);
528 PUSH(h->numBlends);
529 h->deltaStack.cnt = 0;
530 h->numBlends = 0;
531
532 if (h->stack.cnt != 0) {
533 /* Save args */
534 int i;
535 for (i = 0; i < h->stack.cnt; i++) {
536 cstr_savenum(h, h->stack.array[i]);
537 }
538 h->stack.cnt = 0;
539 }
540 cstr_saveop(h, t2_blend);
541 }
542
543 static void flushStemBlends(cstrCtx h) {
544 cfwCtx g = h->g;
545 int i;
546 if ((h->deltaStack.cnt + h->stack.cnt) > CFF2_MAX_OP_STACK) {
547 cfwCtx g = h->g;
548 cfwFatal(g, cfwErrStackOverflow, "Blend overflow");
549 }
550
551 for (i = 0; i < h->deltaStack.cnt; i++)
552 tmp_savefixed(g, float2Fixed(h->deltaStack.array[i]));
553
554 tmp_savefixed(g, float2Fixed((float)h->numBlends));
555 h->deltaStack.cnt = 0;
556 h->numBlends = 0;
557
558 tmp_saveop(g, t2_blend);
559 }
560
561 static void pushBlend(cstrCtx h, abfBlendArg *arg1) {
562 if (h->glyph.info->blendInfo.numRegions == 0)
563 return;
564 if ((arg1 == NULL) || (!arg1->hasBlend)) {
565 if (h->numBlends > 0)
566 flushBlends(h);
567 } else {
568 if (checkOverflow(h)) {
569 flushBlends(h);
570 }
571 pushBlendDeltas(h, arg1);
572 h->numBlends++;
573 }
574 return;
575 }
576
577 static void pushStemBlends(cstrCtx h, abfBlendArg *arg1) {
578 if (h->glyph.info->blendInfo.numRegions == 0)
579 return;
580 if ((arg1 == NULL) || (!arg1->hasBlend)) {
581 if (h->numBlends > 0)
582 flushStemBlends(h);
583 } else {
584 if (checkOverflow(h)) {
585 flushStemBlends(h);
586 }
587 pushBlendDeltas(h, arg1);
588 h->numBlends++;
589 }
590 return;
591 }
592
593 static int blendIsZero(cstrCtx h, float dv, abfBlendArg *blendArg) {
594 /* Validate that all the blend args are zero. */
595 int i;
596 if (dv != 0)
597 return 0;
598
599 if (!blendArg->hasBlend)
600 return 1;
601
602 i = 0;
603 while (i < h->glyph.info->blendInfo.numRegions) {
604 float delta = (float)RND_ON_WRITE(blendArg->blendValues[i] - blendArg->value);
605 if (delta != 0)
606 return 0;
607 i++;
608 }
609 return 1;
610 }
611
612 static void glyphMoveVF(abfGlyphCallbacks *cb, abfBlendArg *argX, abfBlendArg *argY) {
613 cfwCtx g = (cfwCtx)cb->direct_ctx;
614 cstrCtx h = g->ctx.cstr;
615 float x0, dx0;
616 float y0, dy0;
617 int doOptimize = !(g->flags & CFW_NO_OPTIMIZATION);
618
619 x0 = (float)RND_ON_WRITE(argX->value); // need to round to 2 decimal places, else get cumulative error when reading the relative coords.
620 y0 = (float)RND_ON_WRITE(argY->value);
621 dx0 = x0 - h->x;
622 dy0 = y0 - h->y;
623
624 /* Check pending op */
625 switch (h->pendop) {
626 case tx_vmoveto:
627 clearmoveto(h, 0, h->stack.array[0]);
628 break;
629
630 case tx_hmoveto:
631 clearmoveto(h, h->stack.array[0], 0);
632 break;
633
634 case tx_rmoveto:
635 clearmoveto(h, h->stack.array[0], h->stack.array[1]);
636 break;
637
638 case tx_dotsection:
639 /* Discard dotsection before closepath */
640 clearop(h);
641 break;
642
643 case tx_noop:
644 break;
645
646 default:
647 saveop(h, h->pendop);
648 break;
649 }
650
651 /* h->pendop is now tx_noop */
652 h->x = x0;
653 h->y = y0;
654
655 if (doOptimize && blendIsZero(h, dx0, argX)) {
656 pushBlend(h, argY);
657 PUSH(dy0);
658 h->pendop = tx_vmoveto;
659 } else if (doOptimize && blendIsZero(h, dy0, argY)) {
660 pushBlend(h, argX);
661 PUSH(dx0);
662 h->pendop = tx_hmoveto;
663 } else {
664 pushBlend(h, argX);
665 PUSH(dx0);
666 pushBlend(h, argY);
667 PUSH(dy0);
668 h->pendop = tx_rmoveto;
669 }
670 h->flags |= SEEN_MOVETO;
671 }
672
673 /* Insert missing initial moveto op. */
674 static void insertMove(abfGlyphCallbacks *cb) {
675 cfwCtx g = (cfwCtx)cb->direct_ctx;
676 cstrCtx h = g->ctx.cstr;
677 addWarn(h, warn_move0);
678 glyphMove(cb, 0, 0);
679 }
680
681 /* Add line to path. */
682 static void glyphLine(abfGlyphCallbacks *cb, float x1, float y1) {
683 cfwCtx g = (cfwCtx)cb->direct_ctx;
684 cstrCtx h = g->ctx.cstr;
685
686 float dx1;
687 float dy1;
688 int doOptimize = !(g->flags & CFW_NO_OPTIMIZATION);
689
690 x1 = (float)RND_ON_WRITE(x1); // need to round to 2 decimal places, else get cumulative error when reading the relative coords.
691 y1 = (float)RND_ON_WRITE(y1);
692 dx1 = x1 - h->x;
693 dy1 = y1 - h->y;
694
695 h->x = x1;
696 h->y = y1;
697
698 if (!(h->flags & SEEN_MOVETO)) {
699 insertMove(cb);
700 }
701
702 g->glyph_metrics.cb.line(&g->glyph_metrics.cb, x1, y1);
703
704 /* Choose format */
705 if ((dx1 == 0.0) && doOptimize) {
706 /* - dy1 vlineto */
707 flushop(h, 1);
708 switch (h->pendop) {
709 case tx_hlineto:
710 /* 0 */ PUSH(dy1);
711 h->pendop = tx_vlineto;
712 break;
713
714 default:
715 saveop(h, h->pendop);
716
717 /* Fall through */
718 case tx_noop:
719 /* 0 */ PUSH(dy1);
720 h->pendop = h->seqop = tx_vlineto;
721 }
722 } else if ((dy1 == 0.0) && doOptimize) {
723 /* dx1 - hlineto */
724 flushop(h, 1);
725 switch (h->pendop) {
726 case tx_vlineto:
727 PUSH(dx1); /* 0 */
728 h->pendop = tx_hlineto;
729 break;
730
731 default:
732 saveop(h, h->pendop);
733
734 /* Fall through */
735 case tx_noop:
736 PUSH(dx1); /* 0 */
737 h->pendop = h->seqop = tx_hlineto;
738 break;
739 }
740 } else {
741 /* dx1 dy1 rlineto */
742 flushop(h, 2);
743 if (doOptimize) {
744 switch (h->pendop) {
745 case tx_rlineto:
746 PUSH(dx1);
747 PUSH(dy1);
748 break;
749
750 case tx_rrcurveto:
751 PUSH(dx1);
752 PUSH(dy1);
753 saveop(h, t2_rcurveline);
754 break;
755
756 default:
757 saveop(h, h->pendop);
758
759 /* Fall through */
760 case tx_noop:
761 PUSH(dx1);
762 PUSH(dy1);
763 h->pendop = tx_rlineto;
764 break;
765 }
766 } else {
767 if (h->pendop != tx_noop)
768 saveop(h, h->pendop);
769 PUSH(dx1);
770 PUSH(dy1);
771 h->pendop = tx_rlineto;
772 }
773 }
774 }
775
776 static void glyphLineVF(abfGlyphCallbacks *cb, abfBlendArg *argX, abfBlendArg *argY) {
777 cfwCtx g = (cfwCtx)cb->direct_ctx;
778 cstrCtx h = g->ctx.cstr;
779 float x0, dx0;
780 float y0, dy0;
781 int doOptimize = !(g->flags & CFW_NO_OPTIMIZATION);
782
783 x0 = (float)RND_ON_WRITE(argX->value); // need to round to 2 decimal places, else get cumulative error when reading the relative coords.
784 y0 = (float)RND_ON_WRITE(argY->value);
785 dx0 = x0 - h->x;
786 dy0 = y0 - h->y;
787
788 h->x = x0;
789 h->y = y0;
790
791 if (!(h->flags & SEEN_MOVETO)) {
792 insertMove(cb);
793 }
794
795 if (doOptimize && blendIsZero(h, dx0, argX)) {
796 flushop(h, 1);
797 switch (h->pendop) {
798 case tx_hlineto:
799 pushBlend(h, argY);
800 PUSH(dy0);
801 h->pendop = tx_vlineto;
802 break;
803
804 default:
805 saveop(h, h->pendop);
806 /* Fall through */
807
808 case tx_noop:
809 pushBlend(h, argY);
810 PUSH(dy0);
811 h->pendop = h->seqop = tx_vlineto;
812 }
813 } else if (doOptimize && blendIsZero(h, dy0, argY)) {
814 flushop(h, 1);
815 switch (h->pendop) {
816 case tx_vlineto:
817 pushBlend(h, argX);
818 PUSH(dx0);
819 h->pendop = tx_hlineto;
820 break;
821
822 default:
823 saveop(h, h->pendop);
824 /* Fall through */
825
826 case tx_noop:
827 pushBlend(h, argX);
828 PUSH(dx0);
829 h->pendop = h->seqop = tx_hlineto;
830 }
831 } else {
832 flushop(h, 2);
833 if (doOptimize) {
834 switch (h->pendop) {
835 case tx_rlineto:
836 pushBlend(h, argX);
837 PUSH(dx0);
838 pushBlend(h, argY);
839 PUSH(dy0);
840 break;
841
842 case tx_rrcurveto:
843 pushBlend(h, argX);
844 PUSH(dx0);
845 pushBlend(h, argY);
846 PUSH(dy0);
847 saveop(h, t2_rcurveline);
848 break;
849
850 default:
851 saveop(h, h->pendop);
852
853 /* Fall through */
854 case tx_noop:
855 pushBlend(h, argX);
856 PUSH(dx0);
857 pushBlend(h, argY);
858 PUSH(dy0);
859 h->pendop = tx_rlineto;
860 break;
861 }
862 } else {
863 if (h->pendop != tx_noop)
864 saveop(h, h->pendop);
865 pushBlend(h, argX);
866 PUSH(dx0);
867 pushBlend(h, argY);
868 PUSH(dy0);
869 h->pendop = tx_rlineto;
870 }
871 }
872 }
873
874 /* Add curve to path. */
875 static void glyphCurve(abfGlyphCallbacks *cb,
876 float x1, float y1,
877 float x2, float y2,
878 float x3, float y3) {
879 cfwCtx g = (cfwCtx)cb->direct_ctx;
880 cstrCtx h = g->ctx.cstr;
881
882 float dx1;
883 float dy1;
884 float dx2;
885 float dy2;
886 float dx3;
887 float dy3;
888 int doOptimize = !(g->flags & CFW_NO_OPTIMIZATION);
889
890 x1 = (float)RND_ON_WRITE(x1); // need to round to 2 decimal places, else get cumulative error when reading the relative coords.
891 y1 = (float)RND_ON_WRITE(y1);
892 x2 = (float)RND_ON_WRITE(x2);
893 y2 = (float)RND_ON_WRITE(y2);
894 x3 = (float)RND_ON_WRITE(x3);
895 y3 = (float)RND_ON_WRITE(y3);
896
897 dx1 = x1 - h->x;
898 dy1 = y1 - h->y;
899 dx2 = x2 - x1;
900 dy2 = y2 - y1;
901 dx3 = x3 - x2;
902 dy3 = y3 - y2;
903
904 h->x = x3;
905 h->y = y3;
906
907 if (!(h->flags & SEEN_MOVETO)) {
908 insertMove(cb);
909 }
910
911 g->glyph_metrics.cb.curve(&g->glyph_metrics.cb, x1, y1, x2, y2, x3, y3);
912
913 /* Choose format */
914 if ((dx1 == 0.0) && doOptimize) {
915 if (dy3 == 0.0) {
916 /* - dy1 dx2 dy2 dx3 - vhcurveto */
917 flushop(h, 4);
918 switch (h->pendop) {
919 case tx_hvcurveto:
920 /* 0 */ PUSH(dy1);
921 PUSH(dx2);
922 PUSH(dy2);
923 PUSH(dx3); /* 0 */
924 h->pendop = tx_vhcurveto;
925 break;
926
927 default:
928 saveop(h, h->pendop);
929
930 /* Fall through */
931 case tx_noop:
932 /* 0 */ PUSH(dy1);
933 PUSH(dx2);
934 PUSH(dy2);
935 PUSH(dx3); /* 0 */
936 h->pendop = h->seqop = tx_vhcurveto;
937 }
938 } else if (dx3 == 0.0) {
939 /* - dy1 dx2 dy2 - dy3 vvcurveto */
940 flushop(h, 4);
941 switch (h->pendop) {
942 case t2_vvcurveto:
943 /* 0 */ PUSH(dy1);
944 PUSH(dx2);
945 PUSH(dy2);
946 /* 0 */ PUSH(dy3);
947 break;
948
949 default:
950 saveop(h, h->pendop);
951
952 /* Fall through */
953 case tx_noop:
954 /* 0 */ PUSH(dy1);
955 PUSH(dx2);
956 PUSH(dy2);
957 /* 0 */ PUSH(dy3);
958 h->pendop = t2_vvcurveto;
959 break;
960 }
961 } else {
962 /* - dy1 dx2 dy2 dx3 dy3 vhcurveto (odd args) */
963 flushop(h, 5);
964 switch (h->pendop) {
965 default:
966 saveop(h, h->pendop);
967
968 /* Fall through */
969 case tx_noop:
970 h->seqop = tx_vhcurveto;
971
972 /* Fall through */
973 case tx_hvcurveto:
974 /* 0 */ PUSH(dy1);
975 PUSH(dx2);
976 PUSH(dy2);
977 PUSH(dx3);
978 PUSH(dy3);
979 saveop(h, tx_vhcurveto);
980 break;
981 }
982 }
983 } else if ((dy1 == 0.0) && doOptimize) {
984 if (dx3 == 0.0) {
985 /* dx1 - dx2 dy2 - dy3 hvcurveto */
986 flushop(h, 4);
987 switch (h->pendop) {
988 case tx_vhcurveto:
989 PUSH(dx1); /* 0 */
990 PUSH(dx2);
991 PUSH(dy2);
992 /* 0 */ PUSH(dy3);
993 h->pendop = tx_hvcurveto;
994 break;
995
996 default:
997 saveop(h, h->pendop);
998
999 /* Fall through */
1000 case tx_noop:
1001 PUSH(dx1); /* 0 */
1002 PUSH(dx2);
1003 PUSH(dy2);
1004 /* 0 */ PUSH(dy3);
1005 h->pendop = h->seqop = tx_hvcurveto;
1006 break;
1007 }
1008 } else if (dy3 == 0.0) {
1009 /* dx1 - dx2 dy2 dx3 - hhcurveto */
1010 flushop(h, 4);
1011 switch (h->pendop) {
1012 case t2_hhcurveto:
1013 PUSH(dx1); /* 0 */
1014 PUSH(dx2);
1015 PUSH(dy2);
1016 PUSH(dx3); /* 0 */
1017 break;
1018
1019 default:
1020 saveop(h, h->pendop);
1021
1022 /* Fall through */
1023 case tx_noop:
1024 PUSH(dx1); /* 0 */
1025 PUSH(dx2);
1026 PUSH(dy2);
1027 PUSH(dx3); /* 0 */
1028 h->pendop = t2_hhcurveto;
1029 break;
1030 }
1031 } else {
1032 /* dx1 - dx2 dy2 dy3 dx3 hvcurveto (odd args) */
1033 flushop(h, 5);
1034 switch (h->pendop) {
1035 default:
1036 saveop(h, h->pendop);
1037
1038 /* Fall through */
1039 case tx_noop:
1040 h->seqop = tx_hvcurveto;
1041
1042 /* Fall through */
1043 case tx_vhcurveto:
1044 PUSH(dx1); /* 0 */
1045 PUSH(dx2);
1046 PUSH(dy2);
1047 PUSH(dy3);
1048 PUSH(dx3); /* Note arg swap */
1049 saveop(h, tx_hvcurveto);
1050 break;
1051 }
1052 }
1053 } else {
1054 if ((dx3 == 0.0) && doOptimize) {
1055 /* dx1 dy1 dx2 dy2 - dy3 vvcurveto (odd args) */
1056 flushop(h, 5);
1057 switch (h->pendop) {
1058 default:
1059 saveop(h, h->pendop);
1060
1061 /* Fall through */
1062 case tx_noop:
1063 PUSH(dx1);
1064 PUSH(dy1);
1065 PUSH(dx2);
1066 PUSH(dy2);
1067 PUSH(dy3); /* 0 */
1068 h->pendop = t2_vvcurveto;
1069 break;
1070 }
1071 } else if ((dy3 == 0.0) && doOptimize) {
1072 /* dx1 dy1 dx2 dy2 dx3 - hhcurveto (odd args) */
1073 flushop(h, 5);
1074 switch (h->pendop) {
1075 default:
1076 saveop(h, h->pendop);
1077
1078 /* Fall through */
1079 case tx_noop:
1080 PUSH(dy1);
1081 PUSH(dx1); /* Note arg swap */
1082 PUSH(dx2);
1083 PUSH(dy2);
1084 PUSH(dx3); /* 0 */
1085 h->pendop = t2_hhcurveto;
1086 }
1087 } else {
1088 /* dx1 dy1 dx2 dy2 dx3 dy3 rrcurveto */
1089 flushop(h, 6);
1090 if (doOptimize) {
1091 switch (h->pendop) {
1092 case tx_rlineto:
1093 PUSH(dx1);
1094 PUSH(dy1);
1095 PUSH(dx2);
1096 PUSH(dy2);
1097 PUSH(dx3);
1098 PUSH(dy3);
1099 saveop(h, t2_rlinecurve);
1100 break;
1101
1102 case tx_rrcurveto:
1103 PUSH(dx1);
1104 PUSH(dy1);
1105 PUSH(dx2);
1106 PUSH(dy2);
1107 PUSH(dx3);
1108 PUSH(dy3);
1109 break;
1110
1111 default:
1112 saveop(h, h->pendop);
1113
1114 /* Fall through */
1115 case tx_noop:
1116 PUSH(dx1);
1117 PUSH(dy1);
1118 PUSH(dx2);
1119 PUSH(dy2);
1120 PUSH(dx3);
1121 PUSH(dy3);
1122 h->pendop = tx_rrcurveto;
1123 break;
1124 }
1125 } else {
1126 if (h->pendop != tx_noop)
1127 saveop(h, h->pendop);
1128 PUSH(dx1);
1129 PUSH(dy1);
1130 PUSH(dx2);
1131 PUSH(dy2);
1132 PUSH(dx3);
1133 PUSH(dy3);
1134 h->pendop = tx_rrcurveto;
1135 }
1136 }
1137 }
1138 }
1139
1140 static void glyphCurveVF(abfGlyphCallbacks *cb,
1141 abfBlendArg *arg_x1, abfBlendArg *arg_y1,
1142 abfBlendArg *arg_x2, abfBlendArg *arg_y2,
1143 abfBlendArg *arg_x3, abfBlendArg *arg_y3) {
1144 cfwCtx g = (cfwCtx)cb->direct_ctx;
1145 cstrCtx h = g->ctx.cstr;
1146
1147 float x1, dx1;
1148 float y1, dy1;
1149 float x2, dx2;
1150 float y2, dy2;
1151 float x3, dx3;
1152 float y3, dy3;
1153 int doOptimize = !(g->flags & CFW_NO_OPTIMIZATION);
1154
1155 x1 = (float)RND_ON_WRITE(arg_x1->value); // need to round to 2 decimal places, else get cumulative error when reading the relative coords.
1156 y1 = (float)RND_ON_WRITE(arg_y1->value);
1157 x2 = (float)RND_ON_WRITE(arg_x2->value);
1158 y2 = (float)RND_ON_WRITE(arg_y2->value);
1159 x3 = (float)RND_ON_WRITE(arg_x3->value);
1160 y3 = (float)RND_ON_WRITE(arg_y3->value);
1161
1162 dx1 = x1 - h->x;
1163 dy1 = y1 - h->y;
1164 dx2 = x2 - x1;
1165 dy2 = y2 - y1;
1166 dx3 = x3 - x2;
1167 dy3 = y3 - y2;
1168
1169 h->x = x3;
1170 h->y = y3;
1171
1172 if (!(h->flags & SEEN_MOVETO)) {
1173 insertMove(cb);
1174 }
1175 /* Choose format */
1176 if (blendIsZero(h, dx1, arg_x1) && doOptimize) {
1177 if (blendIsZero(h, dy3, arg_y3)) {
1178 /* - dy1 dx2 dy2 dx3 - vhcurveto */
1179 flushop(h, 4);
1180 switch (h->pendop) {
1181 case tx_hvcurveto:
1182 pushBlend(h, arg_y1);
1183 /* 0 */ PUSH(dy1);
1184 pushBlend(h, arg_x2);
1185 PUSH(dx2);
1186 pushBlend(h, arg_y2);
1187 PUSH(dy2);
1188 pushBlend(h, arg_x3);
1189 PUSH(dx3); /* 0 */
1190 h->pendop = tx_vhcurveto;
1191 break;
1192
1193 default:
1194 saveop(h, h->pendop);
1195
1196 /* Fall through */
1197 case tx_noop:
1198 pushBlend(h, arg_y1);
1199 /* 0 */ PUSH(dy1);
1200 pushBlend(h, arg_x2);
1201 PUSH(dx2);
1202 pushBlend(h, arg_y2);
1203 PUSH(dy2);
1204 pushBlend(h, arg_x3);
1205 PUSH(dx3); /* 0 */
1206 h->pendop = h->seqop = tx_vhcurveto;
1207 }
1208 } else if (blendIsZero(h, dx3, arg_x3)) {
1209 /* - dy1 dx2 dy2 - dy3 vvcurveto */
1210 flushop(h, 4);
1211 switch (h->pendop) {
1212 case t2_vvcurveto:
1213 pushBlend(h, arg_y1);
1214 /* 0 */ PUSH(dy1);
1215 pushBlend(h, arg_x2);
1216 PUSH(dx2);
1217 pushBlend(h, arg_y2);
1218 PUSH(dy2);
1219 pushBlend(h, arg_y3);
1220 /* 0 */ PUSH(dy3);
1221 break;
1222
1223 default:
1224 saveop(h, h->pendop);
1225
1226 /* Fall through */
1227 case tx_noop:
1228 pushBlend(h, arg_y1);
1229 /* 0 */ PUSH(dy1);
1230 pushBlend(h, arg_x2);
1231 PUSH(dx2);
1232 pushBlend(h, arg_y2);
1233 PUSH(dy2);
1234 pushBlend(h, arg_y3);
1235 /* 0 */ PUSH(dy3);
1236 h->pendop = t2_vvcurveto;
1237 break;
1238 }
1239 } else {
1240 /* - dy1 dx2 dy2 dx3 dy3 vhcurveto (odd args) */
1241 flushop(h, 5);
1242 switch (h->pendop) {
1243 default:
1244 saveop(h, h->pendop);
1245
1246 /* Fall through */
1247 case tx_noop:
1248 h->seqop = tx_vhcurveto;
1249
1250 /* Fall through */
1251 case tx_hvcurveto:
1252 pushBlend(h, arg_y1);
1253 /* 0 */ PUSH(dy1);
1254 pushBlend(h, arg_x2);
1255 PUSH(dx2);
1256 pushBlend(h, arg_y2);
1257 PUSH(dy2);
1258 pushBlend(h, arg_x3);
1259 PUSH(dx3);
1260 pushBlend(h, arg_y3);
1261 PUSH(dy3);
1262 saveop(h, tx_vhcurveto);
1263 break;
1264 }
1265 }
1266 } else if (blendIsZero(h, dy1, arg_y1) && doOptimize) {
1267 if (blendIsZero(h, dx3, arg_x3)) {
1268 /* dx1 - dx2 dy2 - dy3 hvcurveto */
1269 flushop(h, 4);
1270 switch (h->pendop) {
1271 case tx_vhcurveto:
1272 pushBlend(h, arg_x1);
1273 PUSH(dx1); /* 0 */
1274 pushBlend(h, arg_x2);
1275 PUSH(dx2);
1276 pushBlend(h, arg_y2);
1277 PUSH(dy2);
1278 pushBlend(h, arg_y3);
1279 /* 0 */ PUSH(dy3);
1280 h->pendop = tx_hvcurveto;
1281 break;
1282
1283 default:
1284 saveop(h, h->pendop);
1285
1286 /* Fall through */
1287 case tx_noop:
1288 pushBlend(h, arg_x1);
1289 PUSH(dx1); /* 0 */
1290 pushBlend(h, arg_x2);
1291 PUSH(dx2);
1292 pushBlend(h, arg_y2);
1293 PUSH(dy2);
1294 pushBlend(h, arg_y3);
1295 /* 0 */ PUSH(dy3);
1296 h->pendop = h->seqop = tx_hvcurveto;
1297 break;
1298 }
1299 } else if (blendIsZero(h, dy3, arg_y3)) {
1300 /* dx1 - dx2 dy2 dx3 - hhcurveto */
1301 flushop(h, 4);
1302 switch (h->pendop) {
1303 case t2_hhcurveto:
1304 pushBlend(h, arg_x1);
1305 PUSH(dx1); /* 0 */
1306 pushBlend(h, arg_x2);
1307 PUSH(dx2);
1308 pushBlend(h, arg_y2);
1309 PUSH(dy2);
1310 pushBlend(h, arg_x3);
1311 PUSH(dx3); /* 0 */
1312 break;
1313
1314 default:
1315 saveop(h, h->pendop);
1316
1317 /* Fall through */
1318 case tx_noop:
1319 pushBlend(h, arg_x1);
1320 PUSH(dx1); /* 0 */
1321 pushBlend(h, arg_x2);
1322 PUSH(dx2);
1323 pushBlend(h, arg_y2);
1324 PUSH(dy2);
1325 pushBlend(h, arg_x3);
1326 PUSH(dx3); /* 0 */
1327 h->pendop = t2_hhcurveto;
1328 break;
1329 }
1330 } else {
1331 /* dx1 - dx2 dy2 dy3 dx3 hvcurveto (odd args) */
1332 flushop(h, 5);
1333 switch (h->pendop) {
1334 default:
1335 saveop(h, h->pendop);
1336
1337 /* Fall through */
1338 case tx_noop:
1339 h->seqop = tx_hvcurveto;
1340
1341 /* Fall through */
1342 case tx_vhcurveto:
1343 pushBlend(h, arg_x1);
1344 PUSH(dx1); /* 0 */
1345 pushBlend(h, arg_x2);
1346 PUSH(dx2);
1347 pushBlend(h, arg_y2);
1348 PUSH(dy2);
1349 pushBlend(h, arg_y3);
1350 PUSH(dy3);
1351 pushBlend(h, arg_x3);
1352 PUSH(dx3); /* Note arg swap */
1353 saveop(h, tx_hvcurveto);
1354 break;
1355 }
1356 }
1357 } else {
1358 if (blendIsZero(h, dx3, arg_x3) && doOptimize) {
1359 /* dx1 dy1 dx2 dy2 - dy3 vvcurveto (odd args) */
1360 flushop(h, 5);
1361 switch (h->pendop) {
1362 default:
1363 saveop(h, h->pendop);
1364
1365 /* Fall through */
1366 case tx_noop:
1367 pushBlend(h, arg_x1);
1368 PUSH(dx1);
1369 pushBlend(h, arg_y1);
1370 PUSH(dy1);
1371 pushBlend(h, arg_x2);
1372 PUSH(dx2);
1373 pushBlend(h, arg_y2);
1374 PUSH(dy2);
1375 pushBlend(h, arg_y3);
1376 PUSH(dy3); /* 0 */
1377 h->pendop = t2_vvcurveto;
1378 break;
1379 }
1380 } else if (blendIsZero(h, dy3, arg_y3) && doOptimize) {
1381 /* dx1 dy1 dx2 dy2 dx3 - hhcurveto (odd args) */
1382 flushop(h, 5);
1383 switch (h->pendop) {
1384 default:
1385 saveop(h, h->pendop);
1386
1387 /* Fall through */
1388 case tx_noop:
1389 pushBlend(h, arg_y1);
1390 PUSH(dy1);
1391 pushBlend(h, arg_x1);
1392 PUSH(dx1); /* Note arg swap */
1393 pushBlend(h, arg_x2);
1394 PUSH(dx2);
1395 pushBlend(h, arg_y2);
1396 PUSH(dy2);
1397 pushBlend(h, arg_x3);
1398 PUSH(dx3); /* 0 */
1399 h->pendop = t2_hhcurveto;
1400 }
1401 } else {
1402 /* dx1 dy1 dx2 dy2 dx3 dy3 rrcurveto */
1403 flushop(h, 6);
1404 if (doOptimize) {
1405 switch (h->pendop) {
1406 case tx_rlineto:
1407 pushBlend(h, arg_x1);
1408 PUSH(dx1);
1409 pushBlend(h, arg_y1);
1410 PUSH(dy1);
1411 pushBlend(h, arg_x2);
1412 PUSH(dx2);
1413 pushBlend(h, arg_y2);
1414 PUSH(dy2);
1415 pushBlend(h, arg_x3);
1416 PUSH(dx3);
1417 pushBlend(h, arg_y3);
1418 PUSH(dy3);
1419 saveop(h, t2_rlinecurve);
1420 break;
1421
1422 case tx_rrcurveto:
1423 pushBlend(h, arg_x1);
1424 PUSH(dx1);
1425 pushBlend(h, arg_y1);
1426 PUSH(dy1);
1427 pushBlend(h, arg_x2);
1428 PUSH(dx2);
1429 pushBlend(h, arg_y2);
1430 PUSH(dy2);
1431 pushBlend(h, arg_x3);
1432 PUSH(dx3);
1433 pushBlend(h, arg_y3);
1434 PUSH(dy3);
1435 break;
1436
1437 default:
1438 saveop(h, h->pendop);
1439
1440 /* Fall through */
1441 case tx_noop:
1442 pushBlend(h, arg_x1);
1443 PUSH(dx1);
1444 pushBlend(h, arg_y1);
1445 PUSH(dy1);
1446 pushBlend(h, arg_x2);
1447 PUSH(dx2);
1448 pushBlend(h, arg_y2);
1449 PUSH(dy2);
1450 pushBlend(h, arg_x3);
1451 PUSH(dx3);
1452 pushBlend(h, arg_y3);
1453 PUSH(dy3);
1454 h->pendop = tx_rrcurveto;
1455 break;
1456 }
1457 } else {
1458 if (h->pendop != tx_noop)
1459 saveop(h, h->pendop);
1460 pushBlend(h, arg_x1);
1461 PUSH(dx1);
1462 pushBlend(h, arg_y1);
1463 PUSH(dy1);
1464 pushBlend(h, arg_x2);
1465 PUSH(dx2);
1466 pushBlend(h, arg_y2);
1467 PUSH(dy2);
1468 pushBlend(h, arg_x3);
1469 PUSH(dx3);
1470 pushBlend(h, arg_y3);
1471 PUSH(dy3);
1472 h->pendop = tx_rrcurveto;
1473 }
1474 }
1475 }
1476 }
1477
1478 static void glyphCurveVF2(abfGlyphCallbacks *cb,
1479 abfBlendArg *arg_x1, abfBlendArg *arg_y1,
1480 abfBlendArg *arg_x2, abfBlendArg *arg_y2,
1481 abfBlendArg *arg_x3, abfBlendArg *arg_y3) {
1482 cfwCtx g = (cfwCtx)cb->direct_ctx;
1483 cstrCtx h = g->ctx.cstr;
1484 int doOptimize = !(g->flags & CFW_NO_OPTIMIZATION);
1485 float x1, dx1;
1486 float y1, dy1;
1487 float x2, dx2;
1488 float y2, dy2;
1489 float x3, dx3;
1490 float y3, dy3;
1491
1492 x1 = (float)RND_ON_WRITE(arg_x1->value); // need to round to 2 decimal places, else get cumulative error when reading the relative coords.
1493 y1 = (float)RND_ON_WRITE(arg_y1->value);
1494 x2 = (float)RND_ON_WRITE(arg_x2->value);
1495 y2 = (float)RND_ON_WRITE(arg_y2->value);
1496 x3 = (float)RND_ON_WRITE(arg_x3->value);
1497 y3 = (float)RND_ON_WRITE(arg_y3->value);
1498
1499 dx1 = x1 - h->x;
1500 dy1 = y1 - h->y;
1501 dx2 = x2 - x1;
1502 dy2 = y2 - y1;
1503 dx3 = x3 - x2;
1504 dy3 = y3 - y2;
1505
1506 if (!(h->flags & SEEN_MOVETO)) {
1507 insertMove(cb);
1508 }
1509
1510 h->x = x3;
1511 h->y = y3;
1512
1513 if (1) {
1514 if (h->pendop != tx_noop)
1515 saveop(h, h->pendop);
1516 pushBlend(h, arg_x1);
1517 PUSH(dx1);
1518 pushBlend(h, arg_y1);
1519 PUSH(dy1);
1520 pushBlend(h, arg_x2);
1521 PUSH(dx2);
1522 pushBlend(h, arg_y2);
1523 PUSH(dy2);
1524 pushBlend(h, arg_x3);
1525 PUSH(dx3);
1526 pushBlend(h, arg_y3);
1527 PUSH(dy3);
1528 h->pendop = tx_rrcurveto;
1529 }
1530 }
1531
1532 /* Save hint/cntr mask. */
1533 static int saveMask(cstrCtx h, HintMask mask) {
1534 int size = (h->stems.cnt + 7) / 8;
1535 long index = dnaExtend(&h->masks, 1, size);
1536 if (index == -1) {
1537 h->g->err.code = cfwErrNoMemory;
1538 return 0;
1539 }
1540 memcpy(&h->masks.array[index], mask, size);
1541 memset(mask, 0, size);
1542 return size;
1543 }
1544
1545 /* Save hint. */
1546 static void saveHint(cstrCtx h) {
1547 Hint *hint;
1548 long index = dnaNext(&h->hints, sizeof(Hint));
1549 if (index == -1) {
1550 h->g->err.code = cfwErrNoMemory;
1551 return;
1552 }
1553 hint = &h->hints.array[index];
1554 hint->iCstr = h->iCstr;
1555 hint->iMask = h->masks.cnt;
1556 hint->size = saveMask(h, h->hintmask);
1557 }
1558
1559 /* Start new hint. */
1560 static void newHint(cstrCtx h) {
1561 if (h->pendop == tx_noop && h->iCstr == h->cstr.cnt) {
1562 /* Consecutive hintsubs; clear mask */
1563 addWarn(h, warn_hint6);
1564 CLEAR_MASK(h->hintmask);
1565 } else {
1566 if (h->pendop != tx_noop) {
1567 saveop(h, h->pendop); /* Save pending op */
1568 }
1569 if (h->flags & SEEN_HINT) {
1570 saveHint(h);
1571 } else {
1572 h->flags |= SEEN_HINT;
1573 }
1574
1575 h->iCstr = h->cstr.cnt;
1576 }
1577 }
1578
1579 /* Save counter. */
1580 static void saveCntr(cstrCtx h) {
1581 Cntr *cntr;
1582 long index = dnaNext(&h->cntrs, sizeof(Cntr));
1583 if (index == -1) {
1584 h->g->err.code = cfwErrNoMemory;
1585 return;
1586 }
1587 cntr = &h->cntrs.array[index];
1588 cntr->iMask = h->masks.cnt;
1589 cntr->size = saveMask(h, h->cntrmask);
1590 }
1591
1592 /* Start new counter. */
1593 static void newCntr(cstrCtx h) {
1594 if (h->pendop != tx_noop) {
1595 saveop(h, h->pendop); /* Save pending op */
1596 }
1597 if (h->flags & SEEN_CNTR) {
1598 saveCntr(h);
1599 } else {
1600 h->flags |= SEEN_CNTR;
1601 }
1602 }
1603
1604 /* Match stems.
1605 XXX For now this will work even with blends because the default
1606 master can't have duplicate stem values, but going forward
1607 this may need to be enhanced to match blends. */
1608 static int CTL_CDECL matchStems(const void *key, const void *value, void *ctx) {
1609 Stem *a = (Stem *)key;
1610 Stem *b = (Stem *)value;
1611 int cmp = (a->flags & STEM_VERT) - (b->flags & STEM_VERT);
1612 if (cmp != 0) {
1613 return cmp;
1614 } else if (a->edge0v.value < b->edge0v.value) {
1615 return -1;
1616 } else if (a->edge0v.value > b->edge0v.value) {
1617 return 1;
1618 } else if (a->edge1v.value < b->edge1v.value) {
1619 return -1;
1620 } else if (a->edge1v.value > b->edge1v.value) {
1621 return 1;
1622 } else {
1623 return 0;
1624 }
1625 }
1626
1627 /* Match stems if both edges are within 2 units. */
1628 static int approxMatch(Stem *_new, Stem *old) {
1629 float delta0 = _new->edge0v.value - old->edge0v.value;
1630 float delta1 = _new->edge1v.value - old->edge1v.value;
1631 return ((old->flags & STEM_CNTR) &&
1632 (_new->flags & STEM_VERT) == (old->flags & STEM_VERT) &&
1633 -2.0f < delta0 && delta0 < 2.0f &&
1634 -2.0f < delta1 && delta1 < 2.0f);
1635 }
1636
1637 /* Add stem to list. Return stem id. */
1638 static unsigned char addStem(cstrCtx h, int flags, float edge0, float edge1) {
1639 int found;
1640 size_t index;
1641 Stem _new;
1642 Stem *stem;
1643 #if PLAT_WIN || PLAT_LINUX
1644 /* On the Pentium architecture in an optimized build, float variables
1645 are kept in 80-bit registers which is more precision than is needed and
1646 in fact causes the comparisons against the ABF_EDGE_HINT_LO and
1647 ABF_EDGE_HINT_HI integers to fail. The "volatile" qualifier forces
1648 intermediate stores and loads which circumvents this problem. */
1649 volatile float delta;
1650 #else
1651 float delta;
1652 #endif
1653
1654 delta = edge1 - edge0;
1655 if (delta < 0 && delta != ABF_EDGE_HINT_LO && delta != ABF_EDGE_HINT_HI) {
1656 addWarn(h, warn_hint0);
1657 _new.edge0v.value = edge1;
1658 _new.edge1v.value = edge0;
1659 } else {
1660 _new.edge0v.value = edge0;
1661 _new.edge1v.value = edge1;
1662 }
1663 _new.edge0v.hasBlend = 0;
1664 _new.edge1v.hasBlend = 0;
1665 _new.flags = (flags & ABF_VERT_STEM) ? STEM_VERT : 0;
1666 if (flags & ABF_CNTR_STEM) {
1667 _new.flags |= STEM_CNTR;
1668 }
1669
1670 /* Check if stem already in list */
1671 found = ctuLookup(&_new, h->stems.array, h->stems.cnt, sizeof(Stem),
1672 matchStems, &index, NULL);
1673 stem = &h->stems.array[index];
1674 if (!found) {
1675 if (!(h->g->flags & CFW_ENABLE_CMP_TEST) &&
1676 (h->flags & SEEN_CNTR) && !(flags & ABF_CNTR_STEM) &&
1677 h->stems.cnt != (int)index) {
1678 /* We are trying to match a stem against a stem list containing
1679 global coloring data. The global coloring stems can have a
1680 2-unit error on each edge so we have to perform approximate
1681 match. This is the correct conversion behavior but can cause
1682 rendering differences between the source font and the converted
1683 font so we disable this if are trying to compare bitmaps. */
1684 if (approxMatch(&_new, stem)) {
1685 return stem->id;
1686 }
1687 if (index > 0 && approxMatch(&_new, stem - 1)) {
1688 return (stem - 1)->id;
1689 }
1690 }
1691
1692 /* New stem */
1693 if (h->stems.cnt == T2_MAX_STEMS) {
1694 addWarn(h, warn_hint3);
1695 } else {
1696 /* Insert new stem in list */
1697 memmove(stem + 1, stem, (h->stems.cnt - index) * sizeof(Stem));
1698 _new.id = (unsigned char)h->stems.cnt++;
1699 *stem = _new;
1700 }
1701 }
1702
1703 return stem->id;
1704 }
1705
1706 static unsigned char addStemVF(cstrCtx h, int flags, abfBlendArg *edge0v, abfBlendArg *edge1v) {
1707 int found;
1708 size_t index;
1709 Stem _new;
1710 Stem *stem;
1711 float edge0 = edge0v->value;
1712 float edge1 = edge1v->value;
1713
1714 #if PLAT_WIN || PLAT_LINUX
1715 /* On the Pentium architecture in an optimized build, float variables
1716 are kept in 80-bit registers which is more precision than is needed and
1717 in fact causes the comparisons against the ABF_EDGE_HINT_LO and
1718 ABF_EDGE_HINT_HI integers to fail. The "volatile" qualifier forces
1719 intermediate stores and loads which circumvents this problem. */
1720 volatile float delta;
1721 #else
1722 float delta;
1723 #endif
1724
1725 delta = edge1 - edge0;
1726 if (delta < 0 && delta != ABF_EDGE_HINT_LO && delta != ABF_EDGE_HINT_HI) {
1727 addWarn(h, warn_hint0);
1728 _new.edge0v = *edge1v;
1729 _new.edge1v = *edge0v;
1730 } else {
1731 _new.edge0v = *edge0v;
1732 _new.edge1v = *edge1v;
1733 }
1734 _new.flags = (flags & ABF_VERT_STEM) ? STEM_VERT : 0;
1735 if (flags & ABF_CNTR_STEM) {
1736 _new.flags |= STEM_CNTR;
1737 }
1738
1739 /* Check if stem already in list */
1740 found = ctuLookup(&_new, h->stems.array, h->stems.cnt, sizeof(Stem),
1741 matchStems, &index, NULL);
1742 stem = &h->stems.array[index];
1743 if (!found) {
1744 if (!(h->g->flags & CFW_ENABLE_CMP_TEST) &&
1745 (h->flags & SEEN_CNTR) && !(flags & ABF_CNTR_STEM) &&
1746 h->stems.cnt != (int)index) {
1747 /* We are trying to match a stem against a stem list containing
1748 global coloring data. The global coloring stems can have a
1749 2-unit error on each edge so we have to perform approximate
1750 match. This is the correct conversion behavior but can cause
1751 rendering differences between the source font and the converted
1752 font so we disable this if are trying to compare bitmaps. */
1753 if (approxMatch(&_new, stem)) {
1754 return stem->id;
1755 }
1756 if (index > 0 && approxMatch(&_new, stem - 1)) {
1757 return (stem - 1)->id;
1758 }
1759 }
1760
1761 /* New stem */
1762 if (h->stems.cnt == T2_MAX_STEMS) {
1763 addWarn(h, warn_hint3);
1764 } else {
1765 /* Insert new stem in list */
1766 memmove(stem + 1, stem, (h->stems.cnt - index) * sizeof(Stem));
1767 _new.id = (unsigned char)h->stems.cnt++;
1768 *stem = _new;
1769 }
1770 }
1771
1772 return stem->id;
1773 }
1774
1775 /* Add stem hint. */
1776 static void glyphStem(abfGlyphCallbacks *cb,
1777 int flags, float edge0, float edge1) {
1778 cfwCtx g = (cfwCtx)cb->direct_ctx;
1779 cstrCtx h = g->ctx.cstr;
1780 unsigned char stemid;
1781
1782 if (h->flags & SEEN_MOVETO) {
1783 if (flags & ABF_NEW_HINTS || h->stems.cnt == 0) {
1784 newHint(h);
1785 }
1786 } else {
1787 if (flags & ABF_NEW_HINTS && h->stems.cnt > 0) {
1788 newHint(h);
1789 }
1790 }
1791
1792 stemid = addStem(h, flags, edge0, edge1);
1793
1794 if (flags & (ABF_CNTR_STEM | ABF_STEM3_STEM)) {
1795 /* Add counter-controlled stem */
1796 if (flags & ABF_NEW_GROUP || !(h->flags & SEEN_CNTR)) {
1797 newCntr(h);
1798 }
1799 SET_ID_BIT(h->cntrmask, stemid);
1800 }
1801
1802 if (!(flags & ABF_CNTR_STEM)) {
1803 /* Add stem or stem3 hint */
1804 if (h->flags & SEEN_HINT) {
1805 SET_ID_BIT(h->hintmask, stemid);
1806 } else {
1807 SET_ID_BIT(h->initmask, stemid);
1808 }
1809 }
1810 }
1811
1812 static void glyphStemVF(abfGlyphCallbacks *cb,
1813 int flags, abfBlendArg *edge0v, abfBlendArg *edge1v) {
1814 cfwCtx g = (cfwCtx)cb->direct_ctx;
1815 cstrCtx h = g->ctx.cstr;
1816 unsigned char stemid;
1817
1818 if (h->flags & SEEN_MOVETO) {
1819 if (flags & ABF_NEW_HINTS || h->stems.cnt == 0) {
1820 newHint(h);
1821 }
1822 } else {
1823 if (flags & ABF_NEW_HINTS && h->stems.cnt > 0) {
1824 newHint(h);
1825 }
1826 }
1827
1828 stemid = addStemVF(h, flags, edge0v, edge1v);
1829
1830 if (flags & (ABF_CNTR_STEM | ABF_STEM3_STEM)) {
1831 /* Add counter-controlled stem */
1832 if (flags & ABF_NEW_GROUP || !(h->flags & SEEN_CNTR)) {
1833 newCntr(h);
1834 }
1835 SET_ID_BIT(h->cntrmask, stemid);
1836 }
1837
1838 if (!(flags & ABF_CNTR_STEM)) {
1839 /* Add stem or stem3 hint */
1840 if (h->flags & SEEN_HINT) {
1841 SET_ID_BIT(h->hintmask, stemid);
1842 } else {
1843 SET_ID_BIT(h->initmask, stemid);
1844 }
1845 }
1846 }
1847
1848 /* Add flex hint. */
1849 static void glyphFlex(abfGlyphCallbacks *cb, float depth,
1850 float x1, float y1,
1851 float x2, float y2,
1852 float x3, float y3,
1853 float x4, float y4,
1854 float x5, float y5,
1855 float x6, float y6) {
1856 cfwCtx g = (cfwCtx)cb->direct_ctx;
1857 cstrCtx h = g->ctx.cstr;
1858 float x0 = h->x;
1859 float y0 = h->y;
1860 h->x = x6;
1861 h->y = y6;
1862
1863 if (h->pendop != tx_noop) {
1864 saveop(h, h->pendop);
1865 }
1866
1867 x1 = (float)RND_ON_WRITE(x1); // need to round to 2 decimal places, else get cumulative error when reading the relative coords.
1868 y1 = (float)RND_ON_WRITE(y1);
1869 x2 = (float)RND_ON_WRITE(x2);
1870 y2 = (float)RND_ON_WRITE(y2);
1871 x3 = (float)RND_ON_WRITE(x3);
1872 y3 = (float)RND_ON_WRITE(y3);
1873 x4 = (float)RND_ON_WRITE(x4);
1874 y4 = (float)RND_ON_WRITE(y4);
1875 x5 = (float)RND_ON_WRITE(x5);
1876 y5 = (float)RND_ON_WRITE(y5);
1877 x6 = (float)RND_ON_WRITE(x6);
1878 y6 = (float)RND_ON_WRITE(y6);
1879
1880 if (depth != TX_STD_FLEX_DEPTH) {
1881 /* dx1 dy1 dx2 dy2 dx3 dy3 dx4 dy4 dx5 dy5 dx6 dy6 depth flex */
1882 flex:
1883 PUSH(x1 - x0);
1884 PUSH(y1 - y0);
1885 PUSH(x2 - x1);
1886 PUSH(y2 - y1);
1887 PUSH(x3 - x2);
1888 PUSH(y3 - y2);
1889 PUSH(x4 - x3);
1890 PUSH(y4 - y3);
1891 PUSH(x5 - x4);
1892 PUSH(y5 - y4);
1893 PUSH(x6 - x5);
1894 PUSH(y6 - y5);
1895 PUSH(depth);
1896 saveop(h, t2_flex);
1897 } else {
1898 int horiz;
1899 if (y0 == y6) {
1900 if (y2 == y3 && y3 == y4) {
1901 if (y0 == y1 && y5 == y6) {
1902 /* dx1 - dx2 dy2 dx3 - dx4 - dx5 - dx6 - - hflex */
1903 PUSH(x1 - x0); /* 0 */
1904 PUSH(x2 - x1);
1905 PUSH(y2 - y1);
1906 PUSH(x3 - x2); /* 0 */
1907 PUSH(x4 - x3); /* 0 */
1908 PUSH(x5 - x4); /* 0 */
1909 PUSH(x6 - x5); /* 0 */
1910 /* 50 */
1911 saveop(h, t2_hflex);
1912 } else {
1913 /* dx1 dy1 dx2 dy2 dx3 - dx4 - dx5 dy5 dx6 - - hflex1 */
1914 PUSH(x1 - x0);
1915 PUSH(y1 - y0);
1916 PUSH(x2 - x1);
1917 PUSH(y2 - y1);
1918 PUSH(x3 - x2); /* 0 */
1919 PUSH(x4 - x3); /* 0 */
1920 PUSH(x5 - x4);
1921 PUSH(y5 - y4);
1922 PUSH(x6 - x5); /* 0 */
1923 /* 50 */
1924 saveop(h, t2_hflex1);
1925 }
1926 return;
1927 }
1928 horiz = 1;
1929 } else if (x0 == x6) {
1930 horiz = 0;
1931 } else {
1932 addWarn(h, warn_flex0);
1933 goto flex;
1934 }
1935
1936 if ((fabs(x5 - x0) > fabs(y5 - y0)) != horiz) {
1937 addWarn(h, warn_flex1);
1938 goto flex;
1939 }
1940
1941 /* dx1 dy1 dx2 dy2 dx3 dy3 dx4 dy4 dx5 dy5 dx6 - - flex1 (h) */
1942 /* dx1 dy1 dx2 dy2 dx3 dy3 dx4 dy4 dx5 dy5 - dy6 - flex1 (v) */
1943 PUSH(x1 - x0);
1944 PUSH(y1 - y0);
1945 PUSH(x2 - x1);
1946 PUSH(y2 - y1);
1947 PUSH(x3 - x2);
1948 PUSH(y3 - y2);
1949 PUSH(x4 - x3);
1950 PUSH(y4 - y3);
1951 PUSH(x5 - x4);
1952 PUSH(y5 - y4);
1953 PUSH(horiz ? x6 - x5 : y6 - y5);
1954 saveop(h, t2_flex1);
1955 }
1956 }
1957
1958 /* Add generic operator. */
1959 static void glyphGenop(abfGlyphCallbacks *cb, int cnt, float *args, int op) {
1960 cfwCtx g = (cfwCtx)cb->direct_ctx;
1961 cstrCtx h = g->ctx.cstr;
1962
1963 if (h->pendop != tx_noop) {
1964 saveop(h, h->pendop);
1965 }
1966
1967 switch (op) {
1968 case t2_cntron:
1969 h->pendop = t2_cntron;
1970 break;
1971
1972 case t2_cntroff:
1973 /* Create all clear cntrmask */
1974 newCntr(h);
1975 break;
1976
1977 default:
1978 if (cnt) {
1979 memmove(h->stack.array, args, cnt * sizeof(float));
1980 h->stack.cnt = cnt;
1981 }
1982 if (op == tx_dotsection) {
1983 h->pendop = tx_dotsection;
1984 } else {
1985 saveop(h, op);
1986 }
1987 break;
1988 }
1989 }
1990
1991 /* Add seac operator. */
1992 static void glyphSeac(abfGlyphCallbacks *cb,
1993 float adx, float ady, int bchar, int achar) {
1994 cfwCtx g = (cfwCtx)cb->direct_ctx;
1995 cstrCtx h = g->ctx.cstr;
1996 if (h->pendop != tx_noop) {
1997 saveop(h, h->pendop);
1998 }
1999 PUSH(adx);
2000 PUSH(ady);
2001 PUSH(bchar);
2002 PUSH(achar);
2003 }
2004
2005 /* Save op code in tmp file. */
2006 static void tmp_saveop(cfwCtx g, int op) {
2007 unsigned char t[2];
2008 size_t count;
2009 if (op & 0xff00) {
2010 t[0] = tx_escape;
2011 t[1] = (unsigned char)op;
2012 count = 2;
2013 } else {
2014 t[0] = (unsigned char)op;
2015 count = 1;
2016 }
2017 if (g->cb.stm.write(&g->cb.stm, g->stm.tmp, count, (char *)t) == 0) {
2018 g->err.code = cfwErrTmpStream;
2019 }
2020 }
2021
2022 /* Save fixed-point in tmp file. */
2023 static void tmp_savefixed(cfwCtx g, Fixed f) {
2024 unsigned char t[5];
2025 size_t count;
2026 if (f & 0xffff) {
2027 t[0] = (unsigned char)255;
2028 t[1] = (unsigned char)(f >> 24);
2029 t[2] = (unsigned char)(f >> 16);
2030 t[3] = (unsigned char)(f >> 8);
2031 t[4] = (unsigned char)f;
2032 count = 5;
2033 } else {
2034 count = cfwEncInt(f >> 16, t);
2035 }
2036 if (g->cb.stm.write(&g->cb.stm, g->stm.tmp, count, (char *)t) == 0) {
2037 g->err.code = cfwErrTmpStream;
2038 }
2039 }
2040
2041 /* This function converts a real number to a fixed point number. However, the
2042 precision of the conversion is restricted to 2 decimal digits of fraction.
2043 This permits round-tripping of fractional numbers in Type 1 charstrings
2044 that were implemented by division by 10 or 100. */
2045 static Fixed float2Fixed(float r) {
2046 float half = (r < 0) ? -0.5f : 0.5f;
2047 Fixed x = (Fixed)(r * 100 + half);
2048 return (x / 100) * 65536 + (Fixed)((x % 100) * 655.36f + half);
2049 }
2050
2051 /* Save stem args and ops observing stack limit and possible width arg. The
2052 arithmetic is performed in fixed point notation so that cumulative rounding
2053 errors in the delta stem list can be eliminated. */
2054 static void saveStemOp(cstrCtx h, int iBeg, int iEnd, int op, int optimize) {
2055 unsigned short MAX_STEMS = (h->maxstack - 1) / 2;
2056 cfwCtx g = h->g;
2057 int i = iBeg;
2058 int stems = iEnd - iBeg;
2059 int ops = (stems + MAX_STEMS - 1) / MAX_STEMS;
2060 stems = stems - (ops - 1) * MAX_STEMS;
2061 if (g->flags & CFW_ENABLE_CMP_TEST) {
2062 /* For testing; produces result compatible with typecomp CFF */
2063 while (ops--) {
2064 Fixed last = 0;
2065 while (stems--) {
2066 Stem *stem = &h->stems.array[i++];
2067 Fixed edge0 = float2Fixed(stem->edge0v.value);
2068 Fixed edge1 = float2Fixed(stem->edge1v.value);
2069 tmp_savefixed(g, edge0 - last);
2070 tmp_savefixed(g, edge1 - edge0);
2071 last = edge1;
2072 }
2073 if (ops > 0 || !optimize) {
2074 tmp_saveop(g, op);
2075 }
2076 stems = MAX_STEMS;
2077 }
2078 } else {
2079 /* For production; produces "correct" result */
2080 while (ops--) {
2081 if (g->flags & CFW_WRITE_CFF2) {
2082 float lastf = 0;
2083 /* First, write the default values */
2084 while (stems--) {
2085 Stem *stem = &h->stems.array[i++];
2086 pushStemBlends(h, &stem->edge0v);
2087 tmp_savefixed(g, float2Fixed(stem->edge0v.value - lastf));
2088 pushStemBlends(h, &stem->edge1v);
2089 tmp_savefixed(g, float2Fixed(stem->edge1v.value - stem->edge0v.value));
2090 lastf = stem->edge1v.value;
2091 }
2092 if (h->numBlends > 0)
2093 flushStemBlends(h);
2094 } else {
2095 float last = 0;
2096 while (stems--) {
2097 Stem *stem = &h->stems.array[i++];
2098 tmp_savefixed(g, float2Fixed(stem->edge0v.value - last));
2099 tmp_savefixed(g, float2Fixed(stem->edge1v.value - stem->edge0v.value));
2100 last = stem->edge1v.value;
2101 }
2102 }
2103 if (ops > 0 || !optimize) {
2104 tmp_saveop(g, op);
2105 }
2106 stems = MAX_STEMS;
2107 }
2108 }
2109 }
2110
2111 /* Test for hint overlap */
2112 static void checkOverlap(cstrCtx h, HintMask hintmask) {
2113 int i;
2114 Stem *last;
2115
2116 if (h->g->stm.dbg == NULL) {
2117 return; /* Debug stream deactivated */
2118 }
2119 /* Find bits set and compare corresponding stems */
2120 last = NULL;
2121 for (i = 0; i < h->masksize; i++) {
2122 int j;
2123 unsigned char byte = hintmask[i];
2124 for (j = i * 8; byte != 0; j++) {
2125 if (byte & 0x80) {
2126 Stem *curr = &h->stems.array[j];
2127 if (last != NULL &&
2128 ((last->flags ^ curr->flags) & STEM_VERT) == 0 &&
2129 last->edge1v.value >= curr->edge0v.value) {
2130 addWarn(h, warn_hint4);
2131 }
2132 last = curr;
2133 }
2134 byte <<= 1;
2135 }
2136 }
2137 }
2138
2139 /* Save hint/cntrmask op in tmp stream. */
2140 static void saveHintMaskOp(cstrCtx h, unsigned char *map,
2141 int idsize, char *idmask, int op) {
2142 cfwCtx g = h->g;
2143 int i;
2144 char hintop[MAX_MASK_BYTES + 2]; /* One extra length byte consumed by subroutinizer */
2145 char *hintmask = &hintop[1];
2146
2147 hintop[0] = (char)op;
2148
2149 /* Add length byte for subroutinizer */
2150 if (g->flags & CFW_SUBRIZE) {
2151 *hintmask++ = (char)h->masksize;
2152 }
2153
2154 /* Construct hintmask from stem ids */
2155 memset(hintmask, 0, h->masksize);
2156 for (i = 0; i < idsize * 8; i++) {
2157 if (TEST_ID_BIT(idmask, i)) {
2158 SET_HINT_BIT(hintmask, map[i]);
2159 }
2160 }
2161
2162 if (op == t2_hintmask) {
2163 if (memcmp(h->lastmask, hintmask, h->masksize) == 0) {
2164 if (!(g->flags & CFW_ENABLE_CMP_TEST)) {
2165 /* This hintmask is a duplicate of the initial mask or is a
2166 duplicate of the last hintmask. Normally we can remove it
2167 without causing rendering differences between the source
2168 font and the converted font, but if all the hintmasks are
2169 duplicates of the initial mask the rendering behavior can
2170 change because this causes fixupmap code to be executed by
2171 the render, hence the above test. */
2172 addWarn(h, warn_hint1);
2173 return;
2174 }
2175 }
2176 memcpy(h->lastmask, hintmask, h->masksize);
2177 }
2178
2179 /* Save mask */
2180 if (g->flags & CFW_SUBRIZE) {
2181 /* Save mask op size in second byte for subroutinizer */
2182 char sizebyte = (char)(h->masksize + 2);
2183 if ((g->cb.stm.write(&g->cb.stm, g->stm.tmp, 1, (char *)hintop) == 0)
2184 || (g->cb.stm.write(&g->cb.stm, g->stm.tmp, 1, &sizebyte) == 0)
2185 || (g->cb.stm.write(&g->cb.stm, g->stm.tmp, h->masksize, hintmask) == 0)) {
2186 g->err.code = cfwErrTmpStream;
2187 }
2188 } else {
2189 if (g->cb.stm.write(&g->cb.stm, g->stm.tmp,
2190 1 + h->masksize, (char *)hintop) == 0) {
2191 g->err.code = cfwErrTmpStream;
2192 }
2193 }
2194
2195 /* Counter masks (counter control list) aren't checked because it yields
2196 too many warnings due to stem merging and integerization of list */
2197 if (op == t2_hintmask) {
2198 checkOverlap(h, hintmask);
2199 }
2200 }
2201
2202 /* Save stem hints. */
2203 static void saveStems(cstrCtx h, unsigned char *map) {
2204 cfwCtx g = h->g;
2205 int i;
2206 int ivstem; /* Index of first vertical stem */
2207 int hasinit; /* Charstring has initial mask? */
2208 int hasmask; /* Charstring contains a hintmask? */
2209 int maskfirst; /* Maskop first? */
2210 char *last;
2211
2212 if (h->stems.cnt == 0) {
2213 if (h->flags & SEEN_MOVETO) {
2214 /* Marking glyph has no stem hints */
2215 addWarn(h, warn_hint2);
2216 }
2217 return;
2218 }
2219
2220 /* Set final hintmask size */
2221 h->masksize = (h->stems.cnt + 7) / 8;
2222
2223 /* Initialize lastmask to all stems */
2224 memset(h->lastmask, 0xff, h->masksize - 1);
2225 h->lastmask[h->masksize - 1] = 0xff >> (8 - h->stems.cnt % 8) % 8;
2226
2227 /* Determine if charstring has initial mask */
2228 hasinit = memcmp(h->lastmask, h->initmask, h->masksize) != 0;
2229
2230 /* Check for all duplicate hintmasks */
2231 last = h->initmask;
2232 for (i = 0; i < h->hints.cnt; i++) {
2233 Hint *hint = &h->hints.array[i];
2234 char *curr = &h->masks.array[hint->iMask];
2235 if (hint->size != h->masksize || memcmp(last, curr, h->masksize) != 0) {
2236 maskfirst = hint->iCstr == 0;
2237 hasmask = 1;
2238 goto check_opt;
2239 }
2240 last = curr;
2241 }
2242
2243 if (!hasinit) {
2244 if (h->hints.cnt > 0) {
2245 /* All hintsubs removed! */
2246 addWarn(h, warn_hint5);
2247 }
2248 } else {
2249 /* Unused stems */
2250 addWarn(h, warn_hint7);
2251 }
2252
2253 maskfirst = 0;
2254 hasmask = 0;
2255
2256 /* Apply ROM optimization */
2257 check_opt:
2258 if (maskfirst && (g->flags & CFW_ROM_OPT)) {
2259 hasinit = 0;
2260 }
2261
2262 if (hasinit || h->cntrs.cnt > 0) {
2263 maskfirst = 1; /* The charstring will begin with a hint/cntrmask */
2264 }
2265 /* Will a hintmask exist in the charstring? */
2266 hasmask |= hasinit;
2267
2268 /* Set vstem index */
2269 ivstem = h->stems.cnt;
2270 for (i = 0; i < h->stems.cnt; i++) {
2271 if (h->stems.array[i].flags & STEM_VERT) {
2272 ivstem = i;
2273 break;
2274 }
2275 }
2276
2277 if (ivstem > 0) {
2278 /* Save hstems */
2279 saveStemOp(h, 0, ivstem, hasmask ? t2_hstemhm : tx_hstem, 0);
2280 }
2281
2282 if (ivstem < h->stems.cnt) {
2283 /* Save vstems */
2284 saveStemOp(h, ivstem, h->stems.cnt, hasmask ? t2_vstemhm : tx_vstem,
2285 maskfirst);
2286 }
2287
2288 /* Construct stem id to stem order mapping */
2289 for (i = 0; i < h->stems.cnt; i++) {
2290 map[h->stems.array[i].id] = (unsigned char)i;
2291 }
2292
2293 /* Save counter masks */
2294 for (i = 0; i < h->cntrs.cnt; i++) {
2295 Cntr *cntr = &h->cntrs.array[i];
2296 saveHintMaskOp(h, map,
2297 cntr->size, &h->masks.array[cntr->iMask], t2_cntrmask);
2298 }
2299
2300 /* Initialize lastmask to all stems */
2301 memset(h->lastmask, 0xff, h->masksize - 1);
2302 h->lastmask[h->masksize - 1] = 0xff << (8 - h->stems.cnt % 8) % 8;
2303
2304 if (hasinit) {
2305 /* Save initial hints */
2306 saveHintMaskOp(h, map, h->masksize, h->initmask, t2_hintmask);
2307 } else {
2308 /* No initial hints so check all hints for overlap */
2309 checkOverlap(h, h->lastmask);
2310 }
2311 }
2312
2313 /* Convert warning number to string. */
2314 static char *warnMsg(int warning) {
2315 switch (warning) {
2316 case warn_move0:
2317 return "no initial moveto (inserted)";
2318
2319 case warn_move1:
2320 return "moveto preceeds closepath (discarded)";
2321
2322 case warn_move2:
2323 return "moveto sequence (collapsed)";
2324
2325 case warn_hint0:
2326 return "negative hint (reversed)";
2327
2328 case warn_hint1:
2329 return "duplicate hintsubs (discarded)";
2330
2331 case warn_hint2:
2332 return "unhinted";
2333
2334 case warn_hint3:
2335 return "stem list overflow (discarded)";
2336
2337 case warn_hint4:
2338 return "hint overlap";
2339
2340 case warn_hint5:
2341 return "all hintsubs removed (fixupmap enabled)";
2342
2343 case warn_hint6:
2344 return "consecutive hintsubs (discarded)";
2345
2346 case warn_hint7:
2347 return "unused hints";
2348
2349 case warn_flex0:
2350 return "non-perpendicular flex";
2351
2352 case warn_flex1:
2353 return "suspect flex args";
2354
2355 case warn_dup0:
2356 return "glyph skipped - duplicate of glyph in font";
2357
2358 case warn_dup1:
2359 return "glyph skipped - same name, different charstring as glyph in font";
2360
2361 default:
2362 return "unknown warning!";
2363 }
2364 }
2365
2366 /* Print charstring warning. */
2367 static void printWarn(cstrCtx h) {
2368 cfwCtx g = h->g;
2369 int i;
2370
2371 if (g->stm.dbg == NULL) {
2372 return;
2373 }
2374
2375 for (i = 0; i < warn_cnt; i++) {
2376 if ((h->glyphwarning & (1 << i)) &&
2377 ((g->flags & CFW_WARN_DUP_HINTSUBS) || i != warn_hint1)) {
2378 char *msg = warnMsg(i);
2379
2380 if (h->warning[i] > 1) {
2381 if (h->warning[i] > TC_MAX_WARNINGS)
2382 continue;
2383 }
2384
2385 if (h->glyph.info->flags & ABF_GLYPH_CID) {
2386 cfwMessage(h->g,
2387 "%s <cid-%hu>", msg, h->glyph.info->cid);
2388 } else {
2389 cfwMessage(h->g, "%s <%s>",
2390 msg, h->glyph.info->gname.ptr);
2391 }
2392 }
2393 }
2394 }
2395
2396 /* Print charstring warning. */
2397 void printFinalWarn(cfwCtx g) {
2398 cstrCtx h = g->ctx.cstr;
2399 int i;
2400
2401 if (g->stm.dbg == NULL) {
2402 return;
2403 }
2404
2405 for (i = 0; i < warn_cnt; i++) {
2406 if (h->warning[i] > TC_MAX_WARNINGS) {
2407 char *msg = warnMsg(i);
2408 cfwMessage(h->g, "There are %d additional reports of '%s'.\n", h->warning[i] - TC_MAX_WARNINGS, msg);
2409 }
2410 h->warning[i] = 0;
2411 }
2412 }
2413
2414 static void updateFontBoundingBox(cfwCtx g) {
2415 /* ignore empty glyphs */
2416 if ( (g->glyph_metrics.ctx.int_mtx.left == 0)
2417 && (g->glyph_metrics.ctx.int_mtx.right == 0)
2418 && (g->glyph_metrics.ctx.int_mtx.top == 0)
2419 && (g->glyph_metrics.ctx.int_mtx.bottom == 0))
2420 return;
2421
2422 if (g->glyph_metrics.ctx.int_mtx.left < g->font_bbox.left)
2423 g->font_bbox.left = g->glyph_metrics.ctx.int_mtx.left;
2424
2425 if (g->glyph_metrics.ctx.int_mtx.right > g->font_bbox.right)
2426 g->font_bbox.right = g->glyph_metrics.ctx.int_mtx.right;
2427
2428 if (g->glyph_metrics.ctx.int_mtx.top > g->font_bbox.top)
2429 g->font_bbox.top = g->glyph_metrics.ctx.int_mtx.top;
2430
2431 if (g->glyph_metrics.ctx.int_mtx.bottom < g->font_bbox.bottom)
2432 g->font_bbox.bottom = g->glyph_metrics.ctx.int_mtx.bottom;
2433 }
2434
2435 /* End glyph definition. */
2436 static void glyphEnd(abfGlyphCallbacks *cb) {
2437 cfwCtx g = (cfwCtx)cb->direct_ctx;
2438 cstrCtx h = g->ctx.cstr;
2439 int i;
2440 long iCstr;
2441 unsigned char map[T2_MAX_STEMS];
2442 long cstroff = h->tmpoff;
2443 int doOptimize = !(g->flags & CFW_NO_OPTIMIZATION);
2444
2445 switch (h->pendop) {
2446 case tx_vmoveto:
2447 case tx_hmoveto:
2448 case tx_rmoveto:
2449 /* Path finished with moveto; discard it */
2450 addWarn(h, warn_move1);
2451
2452 /* Fall through */
2453 case tx_dotsection:
2454 /* Discard dotsection before closepath */
2455 clearop(h);
2456 break;
2457
2458 case tx_noop:
2459 break;
2460
2461 default:
2462 saveop(h, h->pendop);
2463 break;
2464 }
2465
2466 if ((!doOptimize) && (h->flags & SEEN_MOVETO)) /* add closing line-to, if last op is not the same as the starting moveto. */
2467 {
2468 if ((h->x != h->start_x) || (h->y != h->start_y)) {
2469 float dx0;
2470 float dy0;
2471 dx0 = h->start_x - h->x;
2472 dy0 = h->start_y - h->y;
2473 PUSH(dx0);
2474 PUSH(dy0);
2475 h->pendop = tx_rlineto;
2476 saveop(h, h->pendop);
2477 h->x = h->start_x;
2478 h->y = h->start_y;
2479 }
2480 }
2481
2482 if (g->flags & CFW_WRITE_CFF2) {
2483 clearop(h);
2484 } else {
2485 saveop(h, tx_endchar);
2486 }
2487
2488 /* Save last hint and counter */
2489 newHint(h);
2490 newCntr(h);
2491
2492 /* Save stems, counters, and initial hintmask at head of charstring */
2493 saveStems(h, map);
2494
2495 /* Write rest of charstring */
2496 iCstr = 0;
2497 for (i = 0; i < h->hints.cnt; i++) {
2498 Hint *hint = &h->hints.array[i];
2499 size_t count;
2500
2501 /* Write charstring segment */
2502 count = hint->iCstr - iCstr;
2503 if (g->cb.stm.write(&g->cb.stm, g->stm.tmp,
2504 count, &h->cstr.array[iCstr]) != count) {
2505 g->err.code = cfwErrTmpStream;
2506 }
2507
2508 /* Save hintmask */
2509 saveHintMaskOp(h, map,
2510 hint->size, &h->masks.array[hint->iMask], t2_hintmask);
2511 iCstr = hint->iCstr;
2512 }
2513
2514 if (g->flags & CFW_SUBRIZE) {
2515 /* Add unique separator to charstring for subroutinizer */
2516 cstr_saveop(h, t2_separator);
2517 cstr_saveop(h, (h->unique >> 16) & 0xff);
2518 cstr_saveop(h, (h->unique >> 8) & 0xff);
2519 cstr_saveop(h, (h->unique++) & 0xff);
2520 }
2521
2522 /* Write last segment */
2523 if ((h->cstr.cnt - iCstr) > 0) {
2524 if (g->cb.stm.write(&g->cb.stm, g->stm.tmp,
2525 h->cstr.cnt - iCstr, &h->cstr.array[iCstr]) == 0) {
2526 g->err.code = cfwErrTmpStream;
2527 }
2528 }
2529
2530 h->tmpoff = g->cb.stm.tell(&g->cb.stm, g->stm.tmp);
2531 if (h->tmpoff == -1) {
2532 g->err.code = cfwErrTmpStream;
2533 }
2534
2535 {
2536 /* Check if new glyph is same as old, if merging fonts */
2537 long seen_index = 0;
2538 int errorCode = 0;
2539 if (g->flags & CFW_CHECK_IF_GLYPHS_DIFFER) {
2540 /* check and see if glyph has been already seen */
2541 seen_index = cfwSeenGlyph(g, h->glyph.info, &errorCode, cstroff, h->tmpoff);
2542 if (errorCode) {
2543 /* set the cfwCtx error code.*/
2544 g->err.code |= errorCode;
2545 if (errorCode == cfwErrGlyphPresent) {
2546 addWarn(h, warn_dup0);
2547 } else if (errorCode == cfwErrGlyphDiffers) {
2548 addWarn(h, warn_dup1);
2549 }
2550
2551 /* rewind temp stream to start of last glyph */
2552 g->cb.stm.seek(&g->cb.stm, g->stm.tmp, cstroff);
2553 h->tmpoff = g->cb.stm.tell(&g->cb.stm, g->stm.tmp);
2554 }
2555 }
2556 if (errorCode == 0) {
2557 cfwAddGlyph(g, h->glyph.info, h->glyph.hAdv, h->tmpoff - cstroff, cstroff, seen_index);
2558 }
2559 }
2560
2561 if (h->flags & SEEN_WARN) {
2562 printWarn(h);
2563 }
2564
2565 g->glyph_metrics.cb.end(&g->glyph_metrics.cb);
2566
2567 updateFontBoundingBox(g);
2568 }
2569
2570 /* Public callback set */
2571 const abfGlyphCallbacks cfwGlyphCallbacks = {
2572 NULL,
2573 NULL,
2574 0,
2575 glyphBeg,
2576 glyphWidth,
2577 glyphMove,
2578 glyphLine,
2579 glyphCurve,
2580 glyphStem,
2581 glyphFlex,
2582 glyphGenop,
2583 glyphSeac,
2584 glyphEnd,
2585 glyphMoveVF,
2586 glyphLineVF,
2587 glyphCurveVF,
2588 glyphStemVF,
2589 };
2590
2591 /* ----------------------------- Debug Support ----------------------------- */
2592
2593 #if CFW_DEBUG
2594
2595 /* Debug mask. */
2596 static void dbmask(int size, char *mask) {
2597 if (size == 0) {
2598 printf("empty");
2599 } else {
2600 int j;
2601 char *sep = "";
2602 for (j = 0; j < size * 8; j++) {
2603 if (TEST_ID_BIT(mask, j)) {
2604 printf("%s%d", sep, j);
2605 sep = "+";
2606 }
2607 }
2608 }
2609 }
2610
2611 /* Debug hints. */
2612 static void dbhints(cstrCtx h) {
2613 long i;
2614 printf("--- stems[index]={edge0,edge1,id,flags}\n");
2615 if (h->stems.cnt == 0) {
2616 printf("empty\n");
2617 } else {
2618 long i;
2619 for (i = 0; i < h->stems.cnt; i++) {
2620 Stem *stem = &h->stems.array[i];
2621 printf("[%ld]={%g,%g,%u,%c}\n", i,
2622 stem->edge0v.value, stem->edge1v.value, stem->id,
2623 (stem->flags & STEM_VERT) ? 'v' : 'h');
2624 }
2625 }
2626 printf("--- initial mask\n");
2627 dbmask((h->stems.cnt + 7) / 8, h->initmask);
2628 printf("\n");
2629 printf("--- hints[index]={iCstr,iMask,size}\n");
2630 if (h->hints.cnt == 0) {
2631 printf("empty\n");
2632 } else {
2633 for (i = 0; i < h->hints.cnt; i++) {
2634 Hint *hint = &h->hints.array[i];
2635 printf("[%ld]={%ld,", i, hint->iCstr);
2636 dbmask(hint->size, &h->masks.array[hint->iMask]);
2637 printf(",%ld}\n", hint->size);
2638 }
2639 }
2640 printf("--- cntrs[index]={iMask,size}\n");
2641 if (h->cntrs.cnt == 0) {
2642 printf("empty\n");
2643 } else {
2644 for (i = 0; i < h->cntrs.cnt; i++) {
2645 Cntr *cntr = &h->cntrs.array[i];
2646 printf("[%ld]={", i);
2647 dbmask(cntr->size, &h->masks.array[cntr->iMask]);
2648 printf(",%ld}\n", cntr->size);
2649 }
2650 }
2651 }
2652
2653 /* Debug stack. */
2654 static void dbstack(cstrCtx h) {
2655 printf("--- stack[index]=value\n");
2656 if (h->stack.cnt == 0) {
2657 printf("empty\n");
2658 } else {
2659 int i;
2660 for (i = 0; i < h->stack.cnt; i++) {
2661 printf("[%d]=%g ", i, h->stack.array[i]);
2662 }
2663 printf("\n");
2664 }
2665 }
2666
2667 /* Debug Type 2 charstring. */
2668 static void dbt2cstr(long length, unsigned char *cstr) {
2669 static char *opname[32] = {
2670 /* 0 */ "reserved0",
2671 /* 1 */ "hstem",
2672 /* 2 */ "reserved2",
2673 /* 3 */ "vstem",
2674 /* 4 */ "vmoveto",
2675 /* 5 */ "rlineto",
2676 /* 6 */ "hlineto",
2677 /* 7 */ "vlineto",
2678 /* 8 */ "rrcurveto",
2679 /* 9 */ "reserved9",
2680 /* 10 */ "callsubr",
2681 /* 11 */ "return",
2682 /* 12 */ "escape",
2683 /* 13 */ "reserved13",
2684 /* 14 */ "endchar",
2685 /* 15 */ "vsindex",
2686 /* 16 */ "blend",
2687 /* 17 */ "reserved17",
2688 /* 18 */ "hstemhm",
2689 /* 19 */ "hintmask",
2690 /* 20 */ "cntrmask",
2691 /* 21 */ "rmoveto",
2692 /* 22 */ "hmoveto",
2693 /* 23 */ "vstemhm",
2694 /* 24 */ "rcurveline",
2695 /* 25 */ "rlinecurve",
2696 /* 26 */ "vvcurveto",
2697 /* 27 */ "hhcurveto",
2698 /* 28 */ "shortint",
2699 /* 29 */ "callgsubr",
2700 /* 30 */ "vhcurveto",
2701 /* 31 */ "hvcurveto",
2702 };
2703 static char *escopname[] = {
2704 /* 0 */ "dotsection",
2705 /* 1 */ "reservedESC1",
2706 /* 2 */ "reservedESC2",
2707 /* 3 */ "and",
2708 /* 4 */ "or",
2709 /* 5 */ "not",
2710 /* 6 */ "reservedESC6",
2711 /* 7 */ "reservedESC7",
2712 /* 8 */ "reservedESC8",
2713 /* 9 */ "abs",
2714 /* 10 */ "add",
2715 /* 11 */ "sub",
2716 /* 12 */ "div",
2717 /* 13 */ "reservedESC13",
2718 /* 14 */ "neg",
2719 /* 15 */ "eq",
2720 /* 16 */ "reservedESC16",
2721 /* 17 */ "reservedESC17",
2722 /* 18 */ "drop",
2723 /* 19 */ "reservedESC19",
2724 /* 20 */ "put",
2725 /* 21 */ "get",
2726 /* 22 */ "ifelse",
2727 /* 23 */ "random",
2728 /* 24 */ "mul",
2729 /* 25 */ "reservedESC25",
2730 /* 26 */ "sqrt",
2731 /* 27 */ "dup",
2732 /* 28 */ "exch",
2733 /* 29 */ "index",
2734 /* 30 */ "roll",
2735 /* 31 */ "reservedESC31",
2736 /* 32 */ "reservedESC32",
2737 /* 33 */ "reservedESC33",
2738 /* 34 */ "hflex",
2739 /* 35 */ "flex",
2740 /* 36 */ "hflex1",
2741 /* 37 */ "flex1",
2742 /* 38 */ "cntron",
2743 };
2744 int single = 0; /* Suppress optimizer warning */
2745 int stems = 0;
2746 int args = 0;
2747 long i = 0;
2748
2749 while (i < length) {
2750 int op = cstr[i];
2751 switch (op) {
2752 case tx_endchar:
2753 printf("%s ", opname[op]);
2754 i++;
2755 break;
2756
2757 case tx_reserved0:
2758 case tx_reserved2:
2759 case tx_vmoveto:
2760 case tx_rlineto:
2761 case tx_hlineto:
2762 case tx_vlineto:
2763 case tx_rrcurveto:
2764 case t2_reserved9:
2765 case tx_callsubr:
2766 case tx_return:
2767 case t2_reserved13:
2768 case t2_vsindex:
2769 case t2_blend:
2770 case tx_reserved17:
2771 case tx_rmoveto:
2772 case tx_hmoveto:
2773 case t2_rcurveline:
2774 case t2_rlinecurve:
2775 case t2_vvcurveto:
2776 case t2_hhcurveto:
2777 case t2_callgsubr:
2778 case tx_vhcurveto:
2779 case tx_hvcurveto:
2780 printf("%s ", opname[op]);
2781 args = 0;
2782 i++;
2783 break;
2784
2785 case tx_hstem:
2786 case tx_vstem:
2787 case t2_hstemhm:
2788 case t2_vstemhm:
2789 printf("%s ", opname[op]);
2790 stems += args / 2;
2791 args = 0;
2792 i++;
2793 break;
2794
2795 case t2_hintmask:
2796 case t2_cntrmask: {
2797 int bytes;
2798 if (args > 0) {
2799 stems += args / 2;
2800 }
2801 bytes = (stems + 7) / 8;
2802 printf("%s+", opname[op]);
2803 i++;
2804 while (bytes--) {
2805 printf("%02X", cstr[i++]);
2806 }
2807 printf(" ");
2808 args = 0;
2809 break;
2810 }
2811
2812 case tx_escape: {
2813 /* Process escaped operator */
2814 int escop = cstr[i + 1];
2815 if (escop > (int)ARRAY_LEN(escopname) - 1) {
2816 printf("reservedESC%d ", escop);
2817 } else {
2818 printf("%s ", escopname[escop]);
2819 }
2820 args = 0;
2821 i += 2;
2822 break;
2823 }
2824
2825 case t2_shortint:
2826 printf("%d ", cstr[i + 1] << 8 | cstr[i + 2]);
2827 args++;
2828 i += 3;
2829 break;
2830
2831 case 247:
2832 case 248:
2833 case 249:
2834 case 250:
2835 /* +ve 2 byte number */
2836 printf("%d ", 108 + 256 * (cstr[i] - 247) + cstr[i + 1]);
2837 args++;
2838 i += 2;
2839 break;
2840
2841 case 251:
2842 case 252:
2843 case 253:
2844 case 254:
2845 /* -ve 2 byte number */
2846 printf("%d ", -108 - 256 * (cstr[i] - 251) - cstr[i + 1]);
2847 i += 2;
2848 args++;
2849 break;
2850
2851 case 255: {
2852 /* 5 byte number */
2853 long value = (long)cstr[i + 1] << 24 | (long)cstr[i + 2] << 16 |
2854 (long)cstr[i + 3] << 8 | (long)cstr[i + 4];
2855 printf("%g ", value / 65536.0);
2856 args++;
2857 i += 5;
2858 break;
2859 }
2860
2861 default:
2862 /* 1 byte number */
2863 single = cstr[i] - 139;
2864 printf("%d ", single);
2865 args++;
2866 i++;
2867 break;
2868 }
2869 }
2870 printf("\n");
2871 }
2872
2873 /* This function just serves to suppress annoying "defined but not used"
2874 compiler messages when debugging */
2875 static void CTL_CDECL dbuse(int arg, ...) {
2876 dbuse(0, dbhints, dbstack, dbt2cstr);
2877 }
2878
2879 #endif /* CFW_DEBUG */