7 typedef struct Hint Hint;
11 #pragma varargck type "π" TTPoint
13 #define dprint(...) {if(debug) fprint(2, __VA_ARGS__);}
15 static TTGState defstate = {
41 u8int *shint, *ip, *ehint;
50 rounddiv(int a, int b)
52 if(b < 0){ a = -a; b = -b; }
60 vrounddiv(vlong a, int b)
62 if(b < 0){ a = -a; b = -b; }
70 herror(Hint *h, char *fmt, ...)
75 vsnprint(h->err, sizeof(h->err), fmt, va);
77 dprint("error: %s\n", h->err);
82 push(Hint *h, u32int w)
84 assert(h->sp < h->nstack);
85 h->stack[h->sp++] = w;
92 return h->stack[--h->sp];
99 herror(h, "missing byte");
115 getpoint(Hint *h, int n, int pi)
118 pi = h->f->rp[(n >> 4 & 3) - 1];
119 if((h->f->zp >> (n >> 8 & 3) & 1) != 0){
121 herror(h, "access to glyph zone from FPGM/CVT");
122 if((uint)pi >= h->g->npt)
123 herror(h, "glyph zone point index %d out of range", n);
124 dprint("G%s%d: %+π\n", n&ORIG?"O":"", pi, (n & ORIG) != 0 ? h->g->ptorg[pi] : h->g->pt[pi]);
125 return (n & ORIG) != 0 ? h->g->ptorg[pi] : h->g->pt[pi];
127 if((uint)pi >= h->f->u->maxTwilightPoints)
128 herror(h, "twilight zone point index %d out of range", pi);
129 return (n & ORIG) != 0 ? h->f->twiorg[pi] : h->f->twilight[pi];
134 setpoint(Hint *h, int n, int pi, TTPoint p)
137 pi = h->f->rp[(n >> 4 & 3) - 1];
138 if((n & NOTOUCH) == 0){
139 if(h->f->fvx != 0) p.flags |= 2;
140 if(h->f->fvy != 0) p.flags |= 4;
142 if((h->f->zp >> (n >> 8 & 3) & 1) != 0){
144 herror(h, "access to glyph zone from FPGM/CVT");
145 if((uint)pi >= h->g->npt)
146 herror(h, "glyph zone point index %d out of range", n);
147 dprint("G%d: %+π -> %+π\n", pi, h->g->pt[pi], p);
150 if((uint)pi >= h->f->u->maxTwilightPoints)
151 herror(h, "twilight zone point index %d out of range", n);
152 dprint("T%d: %+π -> %+π\n", pi, h->f->twilight[pi], p);
153 h->f->twilight[pi] = p;
158 debugprint(Hint *h, int skip)
163 static char *opcnames[256] = {
164 [0x00] "SVTCA", "SVTCA", "SPVTCA", "SPVTCA", "SFVTCA", "SFVTCA", "SPVTL", "SPVTL",
165 [0x08] "SFVTL", "SFVTL", "SPVFS", "SFVFS", "GPV", "GFV", "SFVTPV", "ISECT",
166 [0x10] "SRP0", "SRP1", "SRP2", "SZP0", "SZP1", "SZP2", "SZPS", "SLOOP",
167 [0x18] "RTG", "RTHG", "SMD", "ELSE", "JMPR", "SCVTCI", "SSWCI", "SSW",
168 [0x20] "DUP", "POP", "CLEAR", "SWAP", "DEPTH", "CINDEX", "MINDEX", "ALIGNPTS",
169 [0x28] nil, "UTP", "LOOPCALL", "CALL", "FDEF", "ENDF", "MDAP", "MDAP",
170 [0x30] "IUP", "IUP", "SHP", "SHP", "SHC", "SHC", "SHZ", "SHZ",
171 [0x38] "SHPIX", "IP", "MSIRP", "MSIRP", "ALIGNRP", "RTDG", "MIAP", "MIAP",
172 [0x40] "NPUSHB", "NPUSHW", "WS", "RS", "WCVTP", "RCVT", "GC", "GC",
173 [0x48] "SCFS", "MD", "MD", "MPPEM", "MPS", "FLIPON", "FLIPOFF", "DEBUG",
174 [0x50] "LT", "LTEQ", "GT", "GTEQ", "EQ", "NEQ", "ODD", "EVEN",
175 [0x58] "IF", "EIF", "AND", "OR", "NOT", "DELTAP1", "SDB", "SDS",
176 [0x60] "ADD", "SUB", "DIV", "MUL", "ABS", "NEG", "FLOOR", "CEILING",
177 [0x68] "ROUND", "ROUND", "ROUND", "ROUND", "NROUND", "NROUND", "NROUND", "NROUND",
178 [0x70] "WCVTF", "DELTAP2", "DELTAP3", "DELTAC1", "DELTAC2", "DELTAC3", "SROUND", "S45ROUND",
179 [0x78] "JROT", "JROF", "ROFF", nil, "RUTG", "RDTG", "SANGW", "AA",
180 [0x80] "FLIPPT", "FLIPRGON", "FLIPRGOFF", [0x85] "SCANCTRL", "SDPVTL", "SDPVTL",
181 [0x88] "GETINFO", "IDEF", "ROLL", "MAX", "MIN", "SCANTYPE", "INSTCTRL", nil,
182 [0xB0] "PUSHB", "PUSHB", "PUSHB", "PUSHB", "PUSHB", "PUSHB", "PUSHB", "PUSHB",
183 [0xB8] "PUSHW", "PUSHW", "PUSHW", "PUSHW", "PUSHW", "PUSHW", "PUSHW", "PUSHW",
185 static u8int argb[256] = {
186 [0x00] 1, 1, 1, 1, 1, 1, 1, 1,
187 [0x08] 1, 1, 1, 1, 1, 1,
189 [0x30] 1, 1, 1, 1, 1, 1, 1, 1,
190 [0x38] 0, 0, 1, 1, 0, 0, 1, 1,
191 [0x46] 1, 1, 0, 1, 1,
192 [0x68] 2, 2, 2, 2, 2, 2, 2, 2,
197 fmtfdinit(&f, 2, buf, sizeof(buf));
199 if(skip) fmtprint(&f, "** ");
200 fmtprint(&f, "%d %d ", h->level, (int)(h->ip - h->shint));
202 fmtprint(&f, "%s[%d]", op >= 0xe0 ? "MIRP" : "MDRP", op & 0x1f);
203 else if(opcnames[op] == nil)
206 fmtprint(&f, argb[op] != 0 ? "%s[%d]" : "%s[]", opcnames[op], op & (1<<argb[op]) - 1);
208 fmtprint(&f, " :: ");
209 for(i = 0; i < 8 && i < h->sp; i++)
210 fmtprint(&f, "%d ", h->stack[h->sp - 1 - i]);
248 n = (h->ip[-1] & 7) + 1;
261 n = (h->ip[-1] & 7) + 1;
270 skip(Hint *h, int mode)
276 if(h->ip >= h->ehint)
277 herror(h, "reached end of stream during skip()");
278 if(debug) debugprint(h, 1);
285 if(level == 0 && (*h->ip == 0x1b || *h->ip == 0x59))
294 case 0x58: level++; break;
295 case 0x59: level--; break;
296 case 0xb0: case 0xb1: case 0xb2: case 0xb3:
297 case 0xb4: case 0xb5: case 0xb6: case 0xb7:
298 h->ip += (h->ip[-1] & 7) + 1;
300 case 0xb8: case 0xb9: case 0xba: case 0xbb:
301 case 0xbc: case 0xbd: case 0xbe: case 0xbf:
302 h->ip += 2 * ((h->ip[-1] & 7) + 1);
317 if((uint)i >= h->f->u->maxFunctionDefs)
318 herror(h, "function identifier out of range");
321 f->func[i].npgm = h->ip - sp;
322 f->func[i].pgm = mallocz(f->func[i].npgm, 1);
323 if(f->func[i].pgm == nil)
324 herror(h, "malloc: %r");
325 memcpy(f->func[i].pgm, sp, f->func[i].npgm);
329 static void run(Hint *);
335 u8int *lip, *lshint, *lehint;
338 if((uint)i >= h->f->u->maxFunctionDefs || h->f->func[i].npgm == 0)
339 herror(h, "undefined funcion %d", i);
343 h->ip = h->shint = h->f->func[i].pgm;
344 h->ehint = h->ip + h->f->func[i].npgm;
357 u8int *lip, *lshint, *lehint;
361 if((uint)i >= h->f->u->maxFunctionDefs || h->f->func[i].npgm == 0)
362 herror(h, "undefined funcion %d", i);
367 h->ip = h->shint = h->f->func[i].pgm;
368 h->ehint = h->ip + h->f->func[i].npgm;
405 if(n <= 0 || n > h->sp)
406 herror(h, "CINDEX[%d] out of range", n);
407 push(h, h->stack[h->sp - n]);
416 if(n <= 0 || n > h->sp)
417 herror(h, "MINDEX[%d] out of range", n);
418 x = h->stack[h->sp - n];
419 memmove(&h->stack[h->sp - n], &h->stack[h->sp - n + 1], (n - 1) * sizeof(u32int));
420 h->stack[h->sp - 1] = x;
430 h->f->fvx = 16384 * (a & 1);
431 h->f->fvy = 16384 * (~a & 1);
434 h->f->dpvx = h->f->pvx = 16384 * (a & 1);
435 h->f->dpvy = h->f->pvy = 16384 * (~a & 1);
447 h->f->instctrl |= 1<<s;
449 h->f->instctrl &= ~(1<<s);
459 ttround(Hint *h, int x)
463 if(h->f->rperiod == 0) return x;
465 y = x - h->f->rphase + h->f->rthold;
466 y -= y % h->f->rperiod;
468 if(y < 0) y = h->f->rphase;
470 y = x + h->f->rphase - h->f->rthold;
471 y -= y % h->f->rperiod;
473 if(y > 0) y = -h->f->rphase;
486 case 0x50: r = a < b; break;
487 case 0x51: r = a <= b; break;
488 case 0x52: r = a > b; break;
489 case 0x53: r = a >= b; break;
490 case 0x54: r = a == b; break;
491 case 0x55: r = a != b; break;
492 case 0x5a: r = a && b; break;
493 case 0x5b: r = a || b; break;
494 case 0x60: r = a + b; break;
495 case 0x61: r = a - b; break;
496 case 0x62: if(b == 0) herror(h, "division by zero"); r = (vlong)(int)a * 64 / (int)b; break;
497 case 0x63: r = (vlong)(int)a * (vlong)(int)b >> 6; break;
498 case 0x8b: r = a < b ? b : a; break;
499 case 0x8c: r = a < b ? a : b; break;
500 default: SET(r); abort();
512 case 0x56: r = (ttround(h, a) / 64 & 1) != 0; break;
513 case 0x57: r = (ttround(h, a) / 64 & 1) == 0; break;
514 case 0x5c: r = !a; break;
515 case 0x64: r = (int)a < 0 ? -a : a; break;
516 case 0x65: r = -a; break;
517 case 0x66: r = a & -64; break;
518 case 0x67: r = -(-a & -64); break;
519 case 0x68: case 0x69: case 0x6a: case 0x6b: r = ttround(h, a); break;
520 default: SET(r); abort();
531 if((uint)n >= h->f->u->maxStorage)
532 herror(h, "RS[%d] out of bounds");
533 push(h, h->f->storage[n]);
544 if((uint)n >= h->f->u->maxStorage)
545 herror(h, "WS[%d] out of bounds");
546 h->f->storage[n] = v;
581 if((s & 1) != 0) r |= 3;
588 h->f->scanctrl = pop(h);
594 h->f->scantype = pop(h);
604 case 0x19: /* RTHG */
607 case 0x3D: /* RTDG */
611 case 0x7C: /* RUTG */
614 case 0x7D: /* RDTG */
617 case 0x7A: /* ROFF */
629 if((n >> 6 & 3) == 3)
630 herror(h, "(S)ROUND: period set to reserved value 3");
631 if(h->ip[-1] == 0x77)
632 h->f->rperiod = 181 >> (2 - (n >> 6 & 3));
634 h->f->rperiod = 32 << (n >> 6 & 3);
635 h->f->rphase = h->f->rperiod * (n >> 4 & 3) / 4;
637 h->f->rthold = h->f->rperiod - 1;
639 h->f->rthold = h->f->rperiod * ((int)(n & 15) - 4) / 8;
645 h->f->rp[h->ip[-1] & 3] = pop(h);
654 if(n>>1 != 0) herror(h, "SZP invalid argument %d", n);
655 t = h->ip[-1] - 0x13;
656 if(t == 3) h->f->zp = 7 * n;
657 else h->f->zp = h->f->zp & ~(1<<t) | n<<t;
661 project(Hint *h, TTPoint *p, TTPoint *q)
664 return rounddiv(h->f->pvx * p->x + h->f->pvy * p->y, 16384);
665 return rounddiv(h->f->pvx * (p->x - q->x) + h->f->pvy * (p->y - q->y), 16384);
669 dualproject(Hint *h, TTPoint *p, TTPoint *q)
672 return rounddiv(h->f->dpvx * p->x + h->f->dpvy * p->y, 16384);
673 return rounddiv(h->f->dpvx * (p->x - q->x) + h->f->dpvy * (p->y - q->y), 16384);
677 forceproject(Hint *h, TTPoint p, int d)
685 den = f->pvx * f->fvx + f->pvy * f->fvy;
686 if(den == 0) herror(h, "FV and PV orthogonal");
687 k = f->fvx * p.y - f->fvy * p.x;
688 n.x = vrounddiv(16384LL * d * f->fvx - k * f->pvy, den);
689 n.y = vrounddiv(16384LL * d * f->fvy + k * f->pvx, den);
697 int a, pi, di, d, d0, d1;
703 if((uint)di >= h->f->ncvt) herror(h, "MIAP out of range");
704 p = getpoint(h, ZP0, pi);
706 dprint("cvt %d\n", d0);
707 d1 = project(h, &p, nil);
708 dprint("old %d\n", d1);
710 if((h->f->zp & 1) != 0){
711 if(a && abs(d1 - d) > h->f->cvci)
714 /* fuck you microsoft */
715 h->f->twiorg[pi].x = rounddiv(d0 * h->f->pvx, 16384);
716 h->f->twiorg[pi].y = rounddiv(d0 * h->f->pvy, 16384);
718 if(a) d = ttround(h, d);
719 n = forceproject(h, p, d);
720 setpoint(h, 0x80, pi, n);
721 h->f->rp[0] = h->f->rp[1] = pi;
731 p = getpoint(h, ZP0, pi);
732 if((h->ip[-1] & 1) != 0)
733 p = forceproject(h, p, ttround(h, project(h, &p, nil)));
734 setpoint(h, ZP0, pi, p);
735 h->f->rp[0] = h->f->rp[1] = pi;
743 TTPoint p1, op1, p2, op2, p, op, n;
744 int dp1, dp2, do12, d;
746 p1 = getpoint(h, RP1 | ZP0, 0);
747 op1 = getpoint(h, RP1 | ZP0 | ORIG, 0);
748 p2 = getpoint(h, RP2 | ZP1, 0);
749 op2 = getpoint(h, RP2 | ZP1 | ORIG, 0);
750 dp1 = project(h, &p1, nil);
751 dp2 = project(h, &p2, nil);
752 do12 = dualproject(h, &op1, &op2);
754 herror(h, "invalid IP[] call");
755 for(i = 0; i < h->f->loop; i++){
757 p = getpoint(h, ZP2, pi);
758 op = getpoint(h, ZP2 | ORIG, pi);
759 d = ttfvrounddiv((vlong)dp1 * dualproject(h, &op, &op2) - (vlong)dp2 * dualproject(h, &op, &op1), do12);
760 n = forceproject(h, p, d);
761 setpoint(h, 0x82, pi, n);
762 dprint("(%d,%d) -> (%d,%d)\n", p.x, p.y, n.x, n.y);
774 p = getpoint(h, ZP2, pi);
775 push(h, project(h, &p, nil));
785 p = getpoint(h, ZP2|ORIG, pi);
786 push(h, dualproject(h, &p, nil));
796 if(l >= h->f->ncvt) herror(h, "WCVTP out of range");
807 if(l >= h->f->ncvt) herror(h, "WCVTF out of range");
808 h->f->cvt[l] = rounddiv(v * h->f->ppem * 64, h->f->u->emsize);
817 if(l >= h->f->ncvt) herror(h, "RCVT out of range");
818 push(h, h->f->cvt[l]);
824 push(h, ttround(h, pop(h)));
869 TTPoint n, p, p0, op, op0;
875 if(cvti >= h->f->ncvt)
876 herror(h, "MIRP out of bounds");
878 dprint("cvt %d\n", d);
879 if(abs(d - h->f->singlewval) < h->f->singlewci)
880 d = d < 0 ? -h->f->singlewci : h->f->singlewci;
881 dprint("single %d\n", d);
882 p = getpoint(h, ZP1, pi);
883 p0 = getpoint(h, ZP0 | RP0, 0);
884 op = getpoint(h, ZP1 | ORIG, pi);
885 op0 = getpoint(h, ZP0 | RP0 | ORIG, 0);
886 d0 = dualproject(h, &op, &op0);
887 if(h->f->autoflip && (d0 ^ d) < 0)
890 if((h->f->zp + 1 & 3) <= 1 && abs(d - d0) > h->f->cvci)
892 dprint("cutin %d (%d)\n", d, h->f->cvci);
895 dprint("round %d\n", d);
898 if(d < h->f->mindist)
901 if(d > -h->f->mindist)
904 dprint("mindist %d (%d)\n", d, h->f->mindist);
905 d += project(h, &p0, nil);
906 dprint("total %d\n", d);
907 n = forceproject(h, p, d);
908 setpoint(h, ZP1, pi, n);
909 h->f->rp[1] = h->f->rp[0];
926 if(abs(d - h->f->singlewval) < h->f->singlewci)
927 d = d < 0 ? -h->f->singlewci : h->f->singlewci;
928 p = getpoint(h, ZP1, pi);
929 p0 = getpoint(h, ZP0 | RP0, 0);
930 d += project(h, &p0, nil);
931 n = forceproject(h, p, d);
932 setpoint(h, ZP1, pi, n);
933 h->f->rp[1] = h->f->rp[0];
945 b = (h->ip[-1] - 0x73) * 16 + h->f->deltabase;
949 if(h->f->ppem != b + (arg >> 4)) continue;
951 arg = arg + (arg >> 3) - 8 << h->f->deltashift;
952 if((uint)c >= h->f->ncvt) herror(h, "DELTAC argument out of range");
964 b = (h->ip[-1] == 0x5d ? 0 : h->ip[-1] - 0x70) * 16 + h->f->deltabase;
968 if(h->f->ppem != b + (arg >> 4)) continue;
970 arg = arg + (arg >> 3) - 8 << h->f->deltashift;
971 p = getpoint(h, ZP0, pi);
972 n = forceproject(h, p, project(h, &p, nil) + arg);
973 setpoint(h, ZP0, pi, n);
980 h->ip += (int)pop(h) - 1;
981 if(h->ip < h->shint || h->ip > h->ehint)
982 herror(h, "JMPR out of bounds");
993 if((e == 0) == (h->ip[-1] & 1)){
995 if(h->ip < h->shint || h->ip > h->ehint)
996 herror(h, "JROT/JROF out of bounds");
1003 h->f->mindist = pop(h);
1013 q = getpoint(h, ZP0 | RP0, 0);
1014 dq = project(h, &q, nil);
1015 for(i = 0; i < h->f->loop; i++){
1017 p = getpoint(h, ZP1, pi);
1018 n = forceproject(h, p, dq);
1019 setpoint(h, ZP1, pi, n);
1025 dirvec(TTPoint a, TTPoint b)
1032 if(r.x == 0 && r.y == 0) r.x = 1<<14;
1034 d = hypot(r.x, r.y);
1035 r.x = r.x / d * 16384;
1036 r.y = r.y / d * 16384;
1051 p1 = getpoint(h, ZP1, pi1);
1052 p2 = getpoint(h, ZP2, pi2);
1054 if((h->ip[-1] & 1) != 0){
1063 h->f->dpvx = h->f->pvx = p.x;
1064 h->f->dpvy = h->f->pvy = p.y;
1078 h->f->dpvy = h->f->pvy = pop(h);
1079 h->f->dpvx = h->f->pvx = pop(h);
1100 TTPoint p, p0, op, op0, n;
1104 p = getpoint(h, ZP1, pi);
1105 p0 = getpoint(h, ZP0 | RP0, 0);
1106 op = getpoint(h, ZP1 | ORIG, pi);
1107 op0 = getpoint(h, ZP0 | RP0 | ORIG, 0);
1108 d = d0 = dualproject(h, &op, &op0);
1109 if(abs(d - h->f->singlewval) < h->f->singlewci)
1110 d = d >= 0 ? -h->f->singlewci : h->f->singlewci;
1111 if((h->ip[-1] & 4) != 0)
1113 if((h->ip[-1] & 8) != 0)
1115 if(d < h->f->mindist)
1118 if(d > -h->f->mindist)
1121 n = forceproject(h, p, d + project(h, &p0, nil));
1122 setpoint(h, ZP1, pi, n);
1123 h->f->rp[1] = h->f->rp[0];
1125 if((h->ip[-1] & 16) != 0)
1139 p1 = getpoint(h, ZP1, pi1);
1140 p2 = getpoint(h, ZP2, pi2);
1141 op1 = getpoint(h, ZP1 | ORIG, pi1);
1142 op2 = getpoint(h, ZP2 | ORIG, pi2);
1144 if((h->ip[-1] & 1) != 0){
1151 p = dirvec(op1, op2);
1152 if((h->ip[-1] & 1) != 0){
1164 h->f->fvx = h->f->pvx;
1165 h->f->fvy = h->f->pvy;
1171 h->f->deltabase = pop(h);
1177 h->f->deltashift = pop(h);
1183 h->f->singlewval = pop(h);
1189 h->f->singlewci = pop(h);
1193 h_fliponoff(Hint *h)
1195 h->f->autoflip = h->ip[-1] & 1;
1203 p1 = getpoint(h, ZP1, pop(h));
1204 p0 = getpoint(h, ZP0, pop(h));
1205 push(h, project(h, &p0, &p1));
1213 p1 = getpoint(h, ZP1 | ORIG, pop(h));
1214 p0 = getpoint(h, ZP0 | ORIG, pop(h));
1215 push(h, dualproject(h, &p0, &p1));
1221 int i, d, pi, dx, dy;
1225 dx = vrounddiv((vlong)h->f->fvx * d, 16384);
1226 dy = vrounddiv((vlong)h->f->fvy * d, 16384);
1227 for(i = 0; i < h->f->loop; i++){
1229 p = getpoint(h, ZP2, pi);
1232 setpoint(h, ZP2, pi, p);
1238 iup1(Hint *h, int ip, int iq, int i, int e)
1244 if(g->ptorg[ip].x == g->ptorg[iq].x)
1246 g->pt[i].x = g->ptorg[i].x + g->pt[iq].x - g->ptorg[iq].x;
1249 z = (g->ptorg[i].x - g->ptorg[iq].x) * 64 / (g->ptorg[ip].x - g->ptorg[iq].x);
1251 else if(z > 64) z = 64;
1252 g->pt[i].x = g->ptorg[i].x + (((g->pt[ip].x - g->ptorg[ip].x) * z + (g->pt[iq].x - g->ptorg[iq].x) * (64 - z)) / 64);
1257 iup0(Hint *h, int ip, int iq, int i, int e)
1263 if(g->ptorg[ip].y == g->ptorg[iq].y)
1265 g->pt[i].y = g->ptorg[i].y + g->pt[iq].y - g->ptorg[iq].y;
1268 z = (g->ptorg[i].y - g->ptorg[iq].y) * 64 / (g->ptorg[ip].y - g->ptorg[iq].y);
1270 else if(z > 64) z = 64;
1271 g->pt[i].y = g->ptorg[i].y + (((g->pt[ip].y - g->ptorg[ip].y) * z + (g->pt[iq].y - g->ptorg[iq].y) * (64 - z)) / 64);
1280 void (*iupp)(Hint *, int, int, int, int);
1282 iupp = (h->ip[-1] & 1) != 0 ? iup1 : iup0;
1283 for(i = 0; i < h->g->ncon; i++){
1285 for(j = h->g->confst[i]; j < h->g->confst[i+1]; j++){
1287 if((p->flags & TOUCHY>>(h->ip[-1]&1)) != 0){
1291 iupp(h, t1, j, t1 + 1, j - 1);
1296 iupp(h, t1, t0, h->g->confst[i], t0 - 1);
1297 iupp(h, t1, t0, t1 + 1, h->g->confst[i+1]-1);
1299 iupp(h, t0, t0, h->g->confst[i], h->g->confst[i+1]-1);
1302 for(i = 0; i < h->g->npt; i++)
1303 dprint("%d: %+π\n", i, h->g->pt[i]);
1313 herror(h, "SLOOP invalid argument %d", n);
1325 p = getpoint(h, ZP2, pi);
1326 n = forceproject(h, p, d);
1327 setpoint(h, ZP2, pi, n);
1338 herror(h, "FLIPRG without glyph");
1340 if((int)i < h->g->npt)
1341 h->g->pt[i].flags = h->g->pt[i].flags & ~1 | h->ip[-1] & 1;
1347 int a0i, a1i, b0i, b1i, pi;
1348 TTPoint a0, a1, b0, b1, p;
1360 a0 = getpoint(h, ZP0, a0i);
1361 a1 = getpoint(h, ZP0, a1i);
1362 b0 = getpoint(h, ZP1, b0i);
1363 b1 = getpoint(h, ZP1, b1i);
1364 p = getpoint(h, ZP2, pi);
1367 n0c = (vlong)n0x * a0.x + (vlong)n0y * a0.y;
1370 n1c = (vlong)n1x * b0.x + (vlong)n1y * b0.y;
1371 Δ = (vlong)n1x * n0y - (vlong)n0x * n1y;
1373 p.x = ((a0.x + a1.x) / 2 + (b0.x + b1.x) / 2) / 2;
1374 p.y = ((a0.y + a1.y) / 2 + (b0.y + b1.y) / 2) / 2;
1376 p.x = vrounddiv(n0y * n1c - n1y * n0c, Δ);
1377 p.y = vrounddiv(n1x * n0c - n0x * n1c, Δ);
1380 setpoint(h, ZP2, pi, p);
1392 if((h->ip[-1] & 1) != 0){
1393 rp = getpoint(h, RP1|ZP0, 0);
1394 orp = getpoint(h, RP1|ZP0|ORIG, 0);
1396 rp = getpoint(h, RP2|ZP1, 0);
1397 orp = getpoint(h, RP2|ZP1|ORIG, 0);
1400 d = project(h, &rp, &orp);
1401 for(i = 0; i < h->f->loop; i++){
1403 p = getpoint(h, ZP2, pi);
1404 dp = project(h, &p, nil);
1405 n = forceproject(h, p, dp + d);
1406 setpoint(h, ZP2, pi, n);
1420 if((h->ip[-1] & 1) != 0){
1422 if(((h->f->zp ^ h->f->zp >> 2) & 1) != 0)
1424 rp = getpoint(h, RP1|ZP0, 0);
1425 orp = getpoint(h, RP1|ZP0|ORIG, 0);
1428 if(((h->f->zp ^ h->f->zp >> 1) & 1) != 0)
1430 rp = getpoint(h, RP2|ZP1, 0);
1431 orp = getpoint(h, RP2|ZP1|ORIG, 0);
1435 herror(h, "SHC[] outside of glyf program");
1436 if((uint)c >= h->g->ncon)
1437 herror(h, "contour %d out of range", c);
1438 d = project(h, &rp, &orp);
1439 for(i = h->g->confst[c]; i < h->g->confst[c+1]; i++){
1440 if(i == rpi) continue;
1441 p = getpoint(h, ZP2, i);
1442 dp = project(h, &p, nil);
1443 n = forceproject(h, p, dp + d);
1444 setpoint(h, ZP2, i, n);
1449 static void (*itable[256])(Hint *) = {
1450 [0x00] h_svtca, h_svtca, h_svtca, h_svtca, h_svtca, h_svtca,
1451 [0x06] h_sxvtl, h_sxvtl, h_sxvtl, h_sxvtl,
1458 [0x10] h_srp, h_srp, h_srp,
1459 [0x13] h_szp, h_szp, h_szp, h_szp,
1461 [0x18] h_roundst, h_roundst,
1478 [0x2e] h_mdap, h_mdap,
1479 [0x30] h_iup, h_iup,
1480 [0x32] h_shp, h_shp,
1481 [0x34] h_shc, h_shc,
1484 [0x3a] h_msirp, h_msirp,
1487 [0x3e] h_miap, h_miap,
1494 [0x46] h_gc0, h_gc1,
1496 [0x49] h_md0, h_md1,
1498 [0x4d] h_fliponoff, h_fliponoff,
1500 [0x50] h_binop, h_binop, h_binop, h_binop, h_binop, h_binop,
1501 [0x56] h_unop, h_unop,
1503 [0x59] h_nop, /* endif */
1504 [0x5a] h_binop, h_binop,
1509 [0x60] h_binop, h_binop, h_binop, h_binop, h_unop, h_unop, h_unop, h_unop,
1510 [0x68] h_unop, h_unop, h_unop, h_unop, h_nop, h_nop, h_nop, h_nop,
1512 [0x71] h_deltap, h_deltap,
1513 [0x73] h_deltac, h_deltac, h_deltac,
1514 [0x76] h_sround, h_sround,
1515 [0x78] h_jrcond, h_jrcond,
1517 [0x7c] h_roundst, h_roundst,
1520 [0x81] h_fliprg, h_fliprg,
1522 [0x86] h_sdpvtl, h_sdpvtl,
1525 [0x8b] h_binop, h_binop,
1528 [0xb0] h_pushb, h_pushb, h_pushb, h_pushb,
1529 h_pushb, h_pushb, h_pushb, h_pushb,
1530 [0xb8] h_pushw, h_pushw, h_pushw, h_pushw,
1531 h_pushw, h_pushw, h_pushw, h_pushw,
1532 [0xc0] h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp,
1533 h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp,
1534 h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp,
1535 h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp, h_mdrp,
1536 [0xe0] h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp,
1537 h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp,
1538 h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp,
1539 h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp, h_mirp,
1547 p = va_arg(f->args, TTPoint);
1548 if((f->flags & FmtSign) != 0)
1549 return fmtprint(f, "(%.2f,%.2f,%d)", (float)p.x/64, (float)p.y/64, p.flags);
1551 return fmtprint(f, "(%d,%d,%d)", p.x, p.y, p.flags);
1557 while(h->ip < h->ehint){
1558 if(debug) debugprint(h, 0);
1559 if(itable[*h->ip] == nil)
1560 sysfatal("unknown hint instruction %#.2x", *h->ip);
1562 itable[*h->ip++](h);
1567 runpg(TTFont *f, TTGlyph *g, uchar *buf, int n)
1572 if(debug && !didfmt){
1573 fmtinstall(L'π', pointfmt);
1576 memset(&h, 0, sizeof(Hint));
1577 if(setjmp(h.jmp) != 0){
1578 errstr(h.err, sizeof(h.err));
1583 h.stack = f->hintstack;
1584 h.nstack = f->u->maxStackElements;
1585 h.ip = h.shint = buf;
1596 if((g->font->defstate.instctrl & 1<<1) != 0)
1599 if((g->font->defstate.instctrl & 1<<2) != 0)
1600 g->font->TTGState = defstate;
1602 g->font->TTGState = g->font->defstate;
1603 rc = runpg(g->font, g, g->hint, g->nhint);
1604 if(debug && rc >= 0){
1605 for(i = 0; i < g->npt; i++)
1606 dprint("%d: %+π\n", i, g->pt[i]);
1612 ttfrunfpgm(TTFont *f)
1617 f->TTGState = defstate;
1618 f->defstate = defstate;
1619 len = ttfgototable(f->u, "fpgm");
1622 buf = mallocz(len, 1);
1625 Bread(f->u->bin, buf, len);
1627 rc = runpg(f, nil, buf, len);
1633 ttfruncvt(TTFont *f)
1638 f->TTGState = defstate;
1639 f->defstate = defstate;
1640 len = ttfgototable(f->u, "prep");
1643 buf = mallocz(len, 1);
1646 Bread(f->u->bin, buf, len);
1648 rc = runpg(f, nil, buf, len);
1665 f->defstate = f->TTGState;