allsorts-azul 0.16.2

Azul's fork of the allsorts font parser / shaping engine / subsetter. Adds pixel-snap hinting fixes + assorted bug fixes to YesLogic's upstream. Intended to be upstreamed — use the official `allsorts` crate if you can.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
//! TrueType bytecode interpreter — executes font programs (fpgm, prep) and
//! per-glyph instructions to grid-fit outlines.
//!
//! # Specification references
//!
//! - **MS OpenType**: <https://learn.microsoft.com/en-us/typography/opentype/spec/tt_instructions>
//! - **Apple TrueType RM**: <https://developer.apple.com/fonts/TrueType-Reference-Manual/RM05/Chap5.html>
//!
//! # Key instructions
//!
//! | Opcode | Name   | Description |
//! |--------|--------|-------------|
//! | 0x2E   | MDAP   | Move Direct Absolute Point (touch + optional round) |
//! | 0x3E   | MIAP   | Move Indirect Absolute Point (CVT-based, key for twilight zone init) |
//! | 0xC0+  | MDRP   | Move Direct Relative Point (measured original distance) |
//! | 0xE0+  | MIRP   | Move Indirect Relative Point (CVT-based distance from reference) |
//! | 0x39   | IP     | Interpolate Point (preserve relative position between rp1/rp2) |
//! | 0x30   | IUP    | Interpolate Untouched Points (final pass, per-contour) |
//! | 0x5D+  | DELTAP | Delta Exception Point (ppem-specific pixel tuning) |
//! | 0x73+  | DELTAC | Delta Exception CVT (ppem-specific CVT modification) |
//!
//! # Twilight zone (zone 0)
//!
//! The `prep` program uses MIAP on zone 0 to create reference points that
//! encode key font measurements (cap height, x-height, stem widths, etc.).
//! These points must be properly initialized (original + current coordinates)
//! so that glyph programs can reference them via MIRP/IP for grid-fitting.

use std::fmt;

use super::f26dot6::{compute_scale, F2Dot14, F26Dot6};
use super::graphics_state::{GraphicsState, RoundState};

// ── Error type ───────────────────────────────────────────────────────

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum HintError {
    StackOverflow,
    StackUnderflow,
    InvalidOpcode(u8),
    UndefinedFunction(u32),
    CallStackOverflow,
    InvalidPointIndex(u32),
    InvalidCvtIndex(u32),
    InvalidStorageIndex(u32),
    InvalidZone(u32),
    DivideByZero,
    UnexpectedEndOfBytecode,
    InvalidJump,
    ExceededMaxInstructions,
    FontNotReady,
}

impl fmt::Display for HintError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            HintError::StackOverflow => write!(f, "hinting: stack overflow"),
            HintError::StackUnderflow => write!(f, "hinting: stack underflow"),
            HintError::InvalidOpcode(op) => write!(f, "hinting: invalid opcode 0x{:02X}", op),
            HintError::UndefinedFunction(id) => {
                write!(f, "hinting: undefined function {}", id)
            }
            HintError::CallStackOverflow => write!(f, "hinting: call stack overflow"),
            HintError::InvalidPointIndex(i) => {
                write!(f, "hinting: invalid point index {}", i)
            }
            HintError::InvalidCvtIndex(i) => write!(f, "hinting: invalid CVT index {}", i),
            HintError::InvalidStorageIndex(i) => {
                write!(f, "hinting: invalid storage index {}", i)
            }
            HintError::InvalidZone(z) => write!(f, "hinting: invalid zone {}", z),
            HintError::DivideByZero => write!(f, "hinting: divide by zero"),
            HintError::UnexpectedEndOfBytecode => {
                write!(f, "hinting: unexpected end of bytecode")
            }
            HintError::InvalidJump => write!(f, "hinting: invalid jump target"),
            HintError::ExceededMaxInstructions => {
                write!(f, "hinting: exceeded maximum instruction count")
            }
            HintError::FontNotReady => write!(f, "hinting: font not ready (fpgm not executed)"),
        }
    }
}

impl std::error::Error for HintError {}

// ── FreeType-compatible signed multiply-divide ──────────────────────
//
// Matches FreeType's `FT_MulDiv`: compute `a * b / c` with correct
// rounding for any sign combination.  The trick is to take absolute
// values, add a half-divisor for rounding, divide, then reapply sign.

#[inline]
fn ft_muldiv(a: i64, b: i64, c: i64) -> i64 {
    if c == 0 {
        return 0;
    }
    let mut s: i64 = 1;
    let mut aa = a;
    let mut bb = b;
    let mut cc = c;
    if aa < 0 { aa = -aa; s = -s; }
    if bb < 0 { bb = -bb; s = -s; }
    if cc < 0 { cc = -cc; s = -s; }
    let d = (aa * bb + (cc >> 1)) / cc;
    if s > 0 { d } else { -d }
}

/// FreeType's FT_DivFix: compute `(a << 16) / b` with signed rounding.
/// Returns a 16.16 fixed-point scale factor.
#[inline]
fn ft_divfix(a: i64, b: i64) -> i64 {
    if b == 0 {
        return 0x7FFFFFFF;
    }
    let mut s: i64 = 1;
    let mut aa = a;
    let mut bb = b;
    if aa < 0 { aa = -aa; s = -s; }
    if bb < 0 { bb = -bb; s = -s; }
    let q = ((aa << 16) + (bb >> 1)) / bb;
    if s > 0 { q } else { -q }
}

/// FreeType's FT_MulFix: compute `(a * b + 0x8000) >> 16` with signed rounding.
/// Multiplies a value by a 16.16 fixed-point factor.
#[inline]
fn ft_mulfix(a: i64, b: i64) -> i64 {
    let mut s: i64 = 1;
    let mut aa = a;
    let mut bb = b;
    if aa < 0 { aa = -aa; s = -s; }
    if bb < 0 { bb = -bb; s = -s; }
    let c = (aa * bb + 0x8000) >> 16;
    if s > 0 { c } else { -c }
}

// ── Point / Zone types ───────────────────────────────────────────────

#[derive(Copy, Clone, Debug, Default)]
pub struct Point {
    pub x: i32, // F26Dot6
    pub y: i32, // F26Dot6
}

bitflags::bitflags! {
    #[derive(Copy, Clone, Debug, Default)]
    pub struct PointFlags: u8 {
        const TOUCHED_X = 0x01;
        const TOUCHED_Y = 0x02;
        const ON_CURVE  = 0x04;
    }
}

#[derive(Clone, Debug)]
pub struct Zone {
    pub original: Vec<Point>,
    pub current: Vec<Point>,
    /// Unscaled original coordinates in font units (FreeType's "orus").
    /// Used by IUP for more precise interpolation — integer font-unit
    /// coordinates avoid the rounding errors present in scaled F26Dot6.
    pub orus: Vec<Point>,
    pub flags: Vec<PointFlags>,
    pub contour_ends: Vec<u16>,
}

impl Zone {
    pub fn new(n_points: usize) -> Self {
        Zone {
            original: vec![Point::default(); n_points],
            current: vec![Point::default(); n_points],
            orus: vec![Point::default(); n_points],
            flags: vec![PointFlags::empty(); n_points],
            contour_ends: Vec::new(),
        }
    }

    pub fn len(&self) -> usize {
        self.current.len()
    }

    pub fn resize(&mut self, n: usize) {
        self.original.resize(n, Point::default());
        self.current.resize(n, Point::default());
        self.orus.resize(n, Point::default());
        self.flags.resize(n, PointFlags::empty());
    }

    /// Ensure the zone can hold at least `n` points, growing if necessary.
    #[inline]
    pub fn ensure_capacity(&mut self, n: usize) {
        if n > self.current.len() && n <= 10_000 {
            self.resize(n);
        }
    }
}

// ── Function definition ──────────────────────────────────────────────

#[derive(Clone, Debug)]
pub struct FuncDef {
    pub bytecode: Vec<u8>,
}

// ── Maximum instruction count to prevent infinite loops ──────────────

const MAX_INSTRUCTIONS: u64 = 1_000_000;
const MAX_CALL_DEPTH: u32 = 64;

// ── Interpreter ──────────────────────────────────────────────────────

#[derive(Clone, Debug)]
pub struct Interpreter {
    // Stack
    pub(crate) stack: Vec<i32>,
    max_stack: usize,

    // CVT (F26Dot6 values stored as i32)
    pub(crate) cvt: Vec<i32>,
    // Original scaled CVT values (before prep/WCVTP modifications).
    // DELTAC applies adjustments to these originals, not WCVTP-modified values.
    pub(crate) cvt_original: Vec<i32>,
    // Accumulated DELTAC adjustments per CVT entry. Multiple DELTACs
    // targeting the same CVT at the same ppem accumulate their deltas.
    cvt_deltac_accum: Vec<i32>,

    // Storage area
    pub(crate) storage: Vec<i32>,

    // Function/instruction definitions
    pub(crate) fdefs: Vec<Option<FuncDef>>,
    pub(crate) idefs: Vec<Option<FuncDef>>,

    // Graphics state
    pub(crate) gs: GraphicsState,
    pub(crate) default_gs: GraphicsState,

    // Zones: 0 = twilight, 1 = glyph
    pub(crate) zones: [Zone; 2],

    // Font metrics
    pub(crate) ppem: u16,
    pub(crate) point_size: i32, // F26Dot6
    pub(crate) units_per_em: u16,
    pub(crate) scale: i64, // 16.16 fixed-point

    // Execution state
    instruction_count: u64,
    call_depth: u32,

    /// When true, dump every instruction to stderr (for debugging)
    pub trace_mode: bool,
    /// When true, log move_point calls on glyph zone to stderr
    pub debug_trace_points: bool,

    // ── Hinting mode flags (toggle to find correct behavior) ────────

    /// Flag A: Reset X coordinates to original after glyph program.
    /// Matches FreeType v40 / Chrome which only applies Y-axis hinting.
    /// Without this: full X+Y hinting (v35 mode), causes SHPIX spacing issues.
    /// With this: Y-only hinting, but curves that depend on X/Y coordination
    /// (e.g., 'u' bottom curve at ppem=20) may distort.
    pub suppress_x_axis: bool,

    /// Flag B: Snapshot Y after IUP[Y], discard post-IUP Y modifications.
    /// Some glyph programs modify Y after IUP via function calls (e.g.,
    /// 'o' at ppem=12: pt5 moves from 150→77). FreeType v40 may suppress these.
    /// Without this: post-IUP moves apply, causing diamond shapes at small ppem.
    /// With this: clean IUP interpolation preserved, but some legitimate
    /// post-IUP adjustments are also discarded.
    pub snapshot_iup_y: bool,

    /// Flag C: Use orig_dist sign for minimum_distance when dist rounds to zero.
    /// Without this: small negative distances get +min_dist (wrong direction).
    /// With this: preserves original direction, matching FreeType behavior.
    pub fix_min_distance_sign: bool,

    /// Snapshot of Y coordinates taken right after IUP[Y] runs.
    iup_y_snapshot: Option<Vec<i32>>,
}

impl Interpreter {
    /// Create a new interpreter from maxp table values.
    pub fn new(
        max_stack_elements: u16,
        max_storage: u16,
        max_function_defs: u16,
        max_instruction_defs: u16,
        max_twilight_points: u16,
        units_per_em: u16,
    ) -> Self {
        Interpreter {
            stack: Vec::with_capacity(max_stack_elements as usize),
            max_stack: max_stack_elements as usize,
            cvt: Vec::new(),
            cvt_original: Vec::new(),
            cvt_deltac_accum: Vec::new(),
            storage: vec![0i32; max_storage as usize],
            fdefs: vec![None; max_function_defs as usize],
            idefs: vec![None; max_instruction_defs as usize],
            gs: GraphicsState::default(),
            default_gs: GraphicsState::default(),
            zones: [
                Zone::new(max_twilight_points as usize), // twilight
                Zone::new(0),                            // glyph (resized per glyph)
            ],
            ppem: 0,
            point_size: 0,
            units_per_em,
            scale: 0,
            instruction_count: 0,
            call_depth: 0,
            trace_mode: false,
            debug_trace_points: false,
            suppress_x_axis: false,    // Flag A: OFF — full X+Y hinting
            snapshot_iup_y: false,    // Flag B: OFF
            fix_min_distance_sign: true, // Flag C: preserve direction on zero-round
            iup_y_snapshot: None,
        }
    }

    /// Returns the current CVT values (F26Dot6 stored as i32).
    pub fn cvt(&self) -> &[i32] {
        &self.cvt
    }

    /// Returns the max stack size.
    pub fn max_stack(&self) -> usize {
        self.max_stack
    }

    /// Returns the graphics state (for debugging).
    pub fn graphics_state(&self) -> &GraphicsState {
        &self.gs
    }

    /// Returns the storage area size.
    pub fn storage_len(&self) -> usize {
        self.storage.len()
    }

    /// Read a storage area value (for debugging).
    pub fn read_storage(&self, idx: usize) -> Option<i32> {
        self.storage.get(idx).copied()
    }

    /// Returns the number of function definitions.
    pub fn fdef_count(&self) -> usize {
        self.fdefs.len()
    }

    /// Returns the number of twilight zone points.
    pub fn twilight_point_count(&self) -> usize {
        self.zones[0].original.len()
    }

    /// Returns the ppem value.
    pub fn ppem(&self) -> u16 {
        self.ppem
    }

    /// Returns the scale value (16.16 fixed-point).
    pub fn scale(&self) -> i64 {
        self.scale
    }

    /// Returns units_per_em.
    pub fn units_per_em(&self) -> u16 {
        self.units_per_em
    }

    /// Execute the font program (`fpgm`) to populate function definitions.
    pub fn execute_fpgm(&mut self, fpgm: &[u8]) -> Result<(), HintError> {
        self.stack.clear();
        self.gs = GraphicsState::default();
        self.instruction_count = 0;
        self.call_depth = 0;
        self.execute(fpgm)
    }

    /// Set the ppem size and execute the prep program.
    pub fn execute_prep(&mut self, prep: &[u8], ppem: u16, point_size: f64) -> Result<(), HintError> {
        self.ppem = ppem;
        self.point_size = F26Dot6::from_f64(point_size).to_bits();
        self.scale = compute_scale(ppem, self.units_per_em);

        self.stack.clear();
        self.gs = GraphicsState::default();
        // Note: storage is NOT cleared between prep runs.
        // The prep program is responsible for setting all values it needs.
        self.instruction_count = 0;
        self.call_depth = 0;
        self.execute(prep)?;

        // Save the modified graphics state as the default for glyph programs
        self.default_gs = self.gs.clone();
        Ok(())
    }

    /// Scale CVT values from FUnits to F26Dot6 pixels.
    pub fn scale_cvt(&mut self, cvt_funits: &[i16]) {
        self.cvt.clear();
        self.cvt.reserve(cvt_funits.len());
        for &funit in cvt_funits {
            self.cvt
                .push(F26Dot6::from_funits(funit as i32, self.scale).to_bits());
        }
        // Save original scaled CVT values. DELTAC applies adjustments
        // to these originals, not to WCVTP-modified values. Without this,
        // the prep's WCVTP rounds CVT[0] from 1422→1408, then DELTAC(-40)
        // gives 1368 (rounds to 1344=21px). With originals, DELTAC(-40)
        // applies to 1422 giving 1382 (rounds to 1408=22px, correct).
        self.cvt_original = self.cvt.clone();
        self.cvt_deltac_accum = vec![0i32; self.cvt.len()];
    }

    /// Hint a glyph outline by executing its bytecode instructions.
    ///
    /// `points` are pre-scaled to F26Dot6 coordinates.
    /// `on_curve` flags indicate whether each point is on-curve.
    /// `contour_ends` gives the index of the last point in each contour.
    /// `instructions` is the per-glyph bytecode from the glyf table.
    ///
    /// After execution, the hinted point positions can be read from
    /// `self.zones[1].current`.
    pub fn hint_glyph(
        &mut self,
        points: &[Point],
        on_curve: &[bool],
        contour_ends: &[u16],
        instructions: &[u8],
    ) -> Result<(), HintError> {
        // Set up the glyph zone with empty orus (no unscaled data)
        self.hint_glyph_with_orus(points, None, on_curve, contour_ends, instructions)
    }

    /// Hint a glyph with optional unscaled original coordinates (orus).
    ///
    /// If `orus` is provided, IUP interpolation uses these exact integer
    /// coordinates instead of the scaled F26Dot6 `points`. This matches
    /// FreeType's approach and avoids ±1 F26Dot6 rounding errors in IUP.
    pub fn hint_glyph_with_orus(
        &mut self,
        points: &[Point],
        orus: Option<&[Point]>,
        on_curve: &[bool],
        contour_ends: &[u16],
        instructions: &[u8],
    ) -> Result<(), HintError> {
        // Set up the glyph zone
        let n = points.len();
        let zone = &mut self.zones[1];
        zone.resize(n);
        for i in 0..n {
            zone.original[i] = points[i];
            zone.current[i] = points[i];
            // Store unscaled coordinates if provided, otherwise use scaled
            zone.orus[i] = if let Some(orus) = orus {
                orus.get(i).copied().unwrap_or(points[i])
            } else {
                points[i]
            };
            let mut flags = PointFlags::empty();
            if on_curve.get(i).copied().unwrap_or(false) {
                flags |= PointFlags::ON_CURVE;
            }
            zone.flags[i] = flags;
        }
        zone.contour_ends = contour_ends.to_vec();

        // Reset graphics state to defaults (set by prep), then override
        // per TrueType spec: projection/freedom vectors reset to x-axis,
        // reference points to 0, loop to 1, zone pointers to 1.
        self.gs = self.default_gs.clone();
        self.gs.projection_vector = (F2Dot14::ONE, F2Dot14::ZERO);
        self.gs.freedom_vector = (F2Dot14::ONE, F2Dot14::ZERO);
        self.gs.dual_projection_vector = (F2Dot14::ONE, F2Dot14::ZERO);
        self.gs.rp0 = 0;
        self.gs.rp1 = 0;
        self.gs.rp2 = 0;
        self.gs.loop_value = 1;
        self.gs.zp0 = 1;
        self.gs.zp1 = 1;
        self.gs.zp2 = 1;

        self.stack.clear();
        self.instruction_count = 0;
        self.call_depth = 0;

        self.execute(instructions)?;

        // v40 backward compatibility: undo X-axis movements AND post-IUP
        // Y-axis modifications.
        //
        // FreeType v40 (DEFAULT mode) and Chrome/Core Text on macOS:
        // - Suppress all X-axis grid-fitting (subpixel rendering handles X)
        // - After IUP[Y], some glyph programs apply post-IUP function calls
        //   that modify Y coordinates (e.g., Times New Roman 'o' at ppem=12
        //   moves pt5.y from 150→77 via a called function). FreeType v40
        //   suppresses these post-IUP modifications.
        //
        // We handle this by saving Y values right after IUP[Y] runs
        // (stored in `iup_y_snapshot`) and restoring them after execution.
        // Flag A: suppress X-axis movements
        if self.suppress_x_axis {
            let zone = &mut self.zones[1];
            for i in 0..zone.current.len() {
                zone.current[i].x = zone.original[i].x;
            }
        }

        // Flag B: restore Y from IUP snapshot (discard post-IUP Y mods)
        if self.snapshot_iup_y {
            if let Some(snap) = &self.iup_y_snapshot {
                let zone = &mut self.zones[1];
                for i in 0..zone.current.len().min(snap.len()) {
                    zone.current[i].y = snap[i];
                }
            }
            self.iup_y_snapshot = None;
        }

        Ok(())
    }

    // ── Core execution loop ──────────────────────────────────────────

    fn execute(&mut self, bytecode: &[u8]) -> Result<(), HintError> {
        let mut ip: usize = 0;
        while ip < bytecode.len() {
            self.instruction_count += 1;
            if self.instruction_count > MAX_INSTRUCTIONS {
                return Err(HintError::ExceededMaxInstructions);
            }

            let opcode = bytecode[ip];
            ip += 1;

            self.dispatch(opcode, bytecode, &mut ip)?;
        }
        Ok(())
    }

    fn dispatch(
        &mut self,
        opcode: u8,
        bytecode: &[u8],
        ip: &mut usize,
    ) -> Result<(), HintError> {
        match opcode {
            // ── Vector setting ───────────────────────────────────
            0x00 => {
                // SVTCA[y] - set both vectors to y-axis
                self.gs.projection_vector = (F2Dot14::ZERO, F2Dot14::ONE);
                self.gs.freedom_vector = (F2Dot14::ZERO, F2Dot14::ONE);
                self.gs.dual_projection_vector = (F2Dot14::ZERO, F2Dot14::ONE);
            }
            0x01 => {
                // SVTCA[x] - set both vectors to x-axis
                self.gs.projection_vector = (F2Dot14::ONE, F2Dot14::ZERO);
                self.gs.freedom_vector = (F2Dot14::ONE, F2Dot14::ZERO);
                self.gs.dual_projection_vector = (F2Dot14::ONE, F2Dot14::ZERO);
            }
            0x02 => {
                // SPVTCA[y] - set projection vector to y-axis
                self.gs.projection_vector = (F2Dot14::ZERO, F2Dot14::ONE);
                self.gs.dual_projection_vector = (F2Dot14::ZERO, F2Dot14::ONE);
            }
            0x03 => {
                // SPVTCA[x] - set projection vector to x-axis
                self.gs.projection_vector = (F2Dot14::ONE, F2Dot14::ZERO);
                self.gs.dual_projection_vector = (F2Dot14::ONE, F2Dot14::ZERO);
            }
            0x04 => {
                // SFVTCA[y] - set freedom vector to y-axis
                self.gs.freedom_vector = (F2Dot14::ZERO, F2Dot14::ONE);
            }
            0x05 => {
                // SFVTCA[x] - set freedom vector to x-axis
                self.gs.freedom_vector = (F2Dot14::ONE, F2Dot14::ZERO);
            }
            0x06..=0x07 => {
                // SPVTL[a] - set projection vector to line
                let a = opcode & 1;
                self.op_spvtl(a != 0)?;
            }
            0x08..=0x09 => {
                // SFVTL[a] - set freedom vector to line
                let a = opcode & 1;
                self.op_sfvtl(a != 0)?;
            }
            0x0A => {
                // SPVFS - set projection vector from stack
                let y = self.pop()? as i16 as i32;
                let x = self.pop()? as i16 as i32;
                self.gs.projection_vector = (F2Dot14::from_bits(x), F2Dot14::from_bits(y));
                self.gs.dual_projection_vector = self.gs.projection_vector;
            }
            0x0B => {
                // SFVFS - set freedom vector from stack
                let y = self.pop()? as i16 as i32;
                let x = self.pop()? as i16 as i32;
                self.gs.freedom_vector = (F2Dot14::from_bits(x), F2Dot14::from_bits(y));
            }
            0x0C => {
                // GPV - get projection vector
                self.push(self.gs.projection_vector.0.to_bits())?;
                self.push(self.gs.projection_vector.1.to_bits())?;
            }
            0x0D => {
                // GFV - get freedom vector
                self.push(self.gs.freedom_vector.0.to_bits())?;
                self.push(self.gs.freedom_vector.1.to_bits())?;
            }
            0x0E => {
                // SFVTPV - set freedom vector to projection vector
                self.gs.freedom_vector = self.gs.projection_vector;
            }
            0x0F => {
                // ISECT - move point to intersection
                self.op_isect()?;
            }

            // ── Reference point / zone setting ───────────────────
            0x10 => {
                // SRP0
                self.gs.rp0 = self.pop()? as u32;
            }
            0x11 => {
                // SRP1
                self.gs.rp1 = self.pop()? as u32;
            }
            0x12 => {
                // SRP2
                self.gs.rp2 = self.pop()? as u32;
            }
            0x13 => {
                // SZP0
                let zone = self.pop()? as u32;
                if zone > 1 {
                    return Err(HintError::InvalidZone(zone));
                }
                self.gs.zp0 = zone;
            }
            0x14 => {
                // SZP1
                let zone = self.pop()? as u32;
                if zone > 1 {
                    return Err(HintError::InvalidZone(zone));
                }
                self.gs.zp1 = zone;
            }
            0x15 => {
                // SZP2
                let zone = self.pop()? as u32;
                if zone > 1 {
                    return Err(HintError::InvalidZone(zone));
                }
                self.gs.zp2 = zone;
            }
            0x16 => {
                // SZPS - set all zone pointers
                let zone = self.pop()? as u32;
                if zone > 1 {
                    return Err(HintError::InvalidZone(zone));
                }
                self.gs.zp0 = zone;
                self.gs.zp1 = zone;
                self.gs.zp2 = zone;
            }
            0x17 => {
                // SLOOP
                let n = self.pop()?;
                self.gs.loop_value = n.max(1) as u32;
            }

            // ── Rounding mode ────────────────────────────────────
            0x18 => {
                // RTG - round to grid
                self.gs.round_state = RoundState::Grid;
            }
            0x19 => {
                // RTHG - round to half grid
                self.gs.round_state = RoundState::HalfGrid;
            }

            // ── Distances ────────────────────────────────────────
            0x1A => {
                // SMD - set minimum distance
                let d = self.pop()?;
                self.gs.minimum_distance = F26Dot6::from_bits(d);
            }

            // ── Control flow ─────────────────────────────────────
            0x1B => {
                // ELSE - skip to EIF
                self.skip_else(bytecode, ip)?;
            }
            0x1C => {
                // JMPR - jump relative
                let offset = self.pop()?;
                // offset is relative to the JMPR opcode position (*ip - 1)
                let opcode_pos = *ip as i64 - 1;
                let new_ip = opcode_pos + offset as i64;
                if new_ip < 0 || new_ip > bytecode.len() as i64 {
                    return Err(HintError::InvalidJump);
                }
                *ip = new_ip as usize;
            }
            0x1D => {
                // SCVTCI - set CVT cut-in
                let v = self.pop()?;
                self.gs.control_value_cut_in = F26Dot6::from_bits(v);
            }
            0x1E => {
                // SSWCI - set single width cut-in
                let v = self.pop()?;
                self.gs.single_width_cut_in = F26Dot6::from_bits(v);
            }
            0x1F => {
                // SSW - set single width value (FUnits -> F26Dot6)
                let v = self.pop()?;
                self.gs.single_width_value =
                    F26Dot6::from_funits(v, self.scale);
            }

            // ── Stack manipulation ───────────────────────────────
            0x20 => {
                // DUP
                let v = self.peek()?;
                self.push(v)?;
            }
            0x21 => {
                // POP
                self.pop()?;
            }
            0x22 => {
                // CLEAR
                self.stack.clear();
            }
            0x23 => {
                // SWAP
                let len = self.stack.len();
                if len < 2 {
                    return Err(HintError::StackUnderflow);
                }
                self.stack.swap(len - 1, len - 2);
            }
            0x24 => {
                // DEPTH
                let d = self.stack.len() as i32;
                self.push(d)?;
            }
            0x25 => {
                // CINDEX - copy indexed element
                let idx = self.pop()? as usize;
                let len = self.stack.len();
                if idx == 0 || idx > len {
                    return Err(HintError::StackUnderflow);
                }
                let v = self.stack[len - idx];
                self.push(v)?;
            }
            0x26 => {
                // MINDEX - move indexed element to top
                let idx = self.pop()? as usize;
                let len = self.stack.len();
                if idx == 0 || idx > len {
                    return Err(HintError::StackUnderflow);
                }
                let pos = len - idx;
                let v = self.stack.remove(pos);
                self.stack.push(v);
            }

            // ── Point alignment ──────────────────────────────────
            0x27 => {
                // ALIGNPTS
                self.op_alignpts()?;
            }

            0x29 => {
                // UTP - untouch point
                let p = self.pop()? as u32;
                let zp0 = self.gs.zp0 as usize;
                if let Some(flags) = self.zones[zp0].flags.get_mut(p as usize) {
                    // Clear touched flags based on freedom vector
                    if self.gs.freedom_vector.0.to_bits() != 0 {
                        flags.remove(PointFlags::TOUCHED_X);
                    }
                    if self.gs.freedom_vector.1.to_bits() != 0 {
                        flags.remove(PointFlags::TOUCHED_Y);
                    }
                }
            }

            // ── Function calls ───────────────────────────────────
            0x2A => {
                // LOOPCALL
                let fn_id = self.pop()? as u32;
                let count = self.pop()? as u32;
                if self.call_depth >= MAX_CALL_DEPTH {
                    return Err(HintError::CallStackOverflow);
                }
                if count > 10000 {
                    return Err(HintError::ExceededMaxInstructions);
                }
                let func = self
                    .fdefs
                    .get(fn_id as usize)
                    .and_then(|f| f.as_ref())
                    .ok_or(HintError::UndefinedFunction(fn_id))?
                    .bytecode
                    .clone();
                for _ in 0..count {
                    self.call_depth += 1;
                    self.execute(&func)?;
                    self.call_depth -= 1;
                }
            }
            0x2B => {
                // CALL
                let fn_id = self.pop()? as u32;
                if self.call_depth >= MAX_CALL_DEPTH {
                    return Err(HintError::CallStackOverflow);
                }
                let func = self
                    .fdefs
                    .get(fn_id as usize)
                    .and_then(|f| f.as_ref())
                    .ok_or(HintError::UndefinedFunction(fn_id))?
                    .bytecode
                    .clone();
                self.call_depth += 1;
                self.execute(&func)?;
                self.call_depth -= 1;
            }
            0x2C => {
                // FDEF - function definition
                let fn_id = self.pop()? as u32;
                let start = *ip;
                // Scan forward to find ENDF (0x2D), handling nested FDEF/ENDF
                let end = self.find_endf(bytecode, ip)?;
                let func_bytecode = bytecode[start..end].to_vec();
                if (fn_id as usize) < self.fdefs.len() {
                    self.fdefs[fn_id as usize] = Some(FuncDef {
                        bytecode: func_bytecode,
                    });
                }
                // ip now points past ENDF
            }
            0x2D => {
                // ENDF - end of function
                // In normal execution flow (inside CALL), this returns from execute()
                return Ok(());
            }

            // ── Point movement ───────────────────────────────────
            0x2E..=0x2F => {
                // MDAP[r] - move direct absolute point
                let round = opcode & 1 != 0;
                self.op_mdap(round)?;
            }
            0x30..=0x31 => {
                // IUP[a] - interpolate untouched points
                let axis = opcode & 1; // 0 = y, 1 = x
                self.op_iup(axis)?;
                // In v40 mode, snapshot Y values right after IUP[Y] runs
                // so post-IUP function calls can't modify them.
                if axis == 0 && self.snapshot_iup_y {
                    self.iup_y_snapshot = Some(
                        self.zones[1].current.iter().map(|p| p.y).collect()
                    );
                }
            }
            0x32..=0x33 => {
                // SHP[a] - shift point
                // SHP[0] (0x32) uses rp2/zp1, SHP[1] (0x33) uses rp1/zp0
                let use_rp1 = opcode & 1 != 0;
                self.op_shp(use_rp1)?;
            }
            0x34..=0x35 => {
                // SHC[a] - shift contour
                // SHC[0] (0x34) uses rp2/zp1, SHC[1] (0x35) uses rp1/zp0
                let use_rp1 = opcode & 1 != 0;
                self.op_shc(use_rp1)?;
            }
            0x36..=0x37 => {
                // SHZ[a] - shift zone
                // SHZ[0] (0x36) uses rp2/zp1, SHZ[1] (0x37) uses rp1/zp0
                let use_rp1 = opcode & 1 != 0;
                self.op_shz(use_rp1)?;
            }
            0x38 => {
                // SHPIX - shift point by pixel amount
                self.op_shpix()?;
            }
            0x39 => {
                // IP - interpolate point
                self.op_ip()?;
            }
            0x3A..=0x3B => {
                // MSIRP[a] - move stack indirect relative point
                let set_rp0 = opcode & 1 != 0;
                self.op_msirp(set_rp0)?;
            }
            0x3C => {
                // ALIGNRP - align relative point
                self.op_alignrp()?;
            }
            0x3D => {
                // RTDG - round to double grid
                self.gs.round_state = RoundState::DoubleGrid;
            }
            0x3E..=0x3F => {
                // MIAP[r] - move indirect absolute point
                let round = opcode & 1 != 0;
                self.op_miap(round)?;
            }

            // ── Push instructions ────────────────────────────────
            0x40 => {
                // NPUSHB - push n bytes
                if *ip >= bytecode.len() {
                    return Err(HintError::UnexpectedEndOfBytecode);
                }
                let n = bytecode[*ip] as usize;
                *ip += 1;
                if *ip + n > bytecode.len() {
                    return Err(HintError::UnexpectedEndOfBytecode);
                }
                for i in 0..n {
                    self.push(bytecode[*ip + i] as i32)?;
                }
                *ip += n;
            }
            0x41 => {
                // NPUSHW - push n words
                if *ip >= bytecode.len() {
                    return Err(HintError::UnexpectedEndOfBytecode);
                }
                let n = bytecode[*ip] as usize;
                *ip += 1;
                if *ip + n * 2 > bytecode.len() {
                    return Err(HintError::UnexpectedEndOfBytecode);
                }
                for i in 0..n {
                    // Cast high byte to i8 for proper sign extension.
                    let hi = bytecode[*ip + i * 2] as i8;
                    let lo = bytecode[*ip + i * 2 + 1] as u8;
                    let val = ((hi as i32) << 8) | (lo as i32);
                    self.push(val)?;
                }
                *ip += n * 2;
            }

            // ── Storage / CVT ────────────────────────────────────
            0x42 => {
                // WS - write storage
                let val = self.pop()?;
                let idx = self.pop()? as u32;
                let i = idx as usize;
                // Dynamically grow storage if needed (fonts may exceed maxp limits)
                if i >= self.storage.len() {
                    if i > 10_000 {
                        return Err(HintError::InvalidStorageIndex(idx));
                    }
                    self.storage.resize(i + 1, 0);
                }
                self.storage[i] = val;
            }
            0x43 => {
                // RS - read storage
                let idx = self.pop()? as u32;
                let i = idx as usize;
                // Dynamically grow storage if needed (reads default to 0)
                if i >= self.storage.len() {
                    if i > 10_000 {
                        return Err(HintError::InvalidStorageIndex(idx));
                    }
                    self.storage.resize(i + 1, 0);
                }
                self.push(self.storage[i])?;
            }
            0x44 => {
                // WCVTP - write CVT in pixel units (F26Dot6)
                let val = self.pop()?;
                let idx = self.pop()? as u32;
                let i = idx as usize;
                if i >= self.cvt.len() {
                    self.cvt.resize(i + 1, 0);
                }
                self.cvt[i] = val;
            }
            0x45 => {
                // RCVT - read CVT
                let idx = self.pop()? as u32;
                let val = self.read_cvt(idx)?;
                self.push(val)?;
            }
            0x46..=0x47 => {
                // GC[a] - get coordinate (0=current, 1=original)
                let use_original = opcode & 1 != 0;
                self.op_gc(use_original)?;
            }
            0x48 => {
                // SCFS - set coordinate from stack
                self.op_scfs()?;
            }
            0x49..=0x4A => {
                // MD[a] - measure distance
                // 0x49 = MD[0]: use current (grid-fitted) positions
                // 0x4A = MD[1]: use original (unhinted) positions
                // 0x49 = MD[0]: use current (grid-fitted) positions
                // 0x4A = MD[1]: use original (unhinted) positions
                let use_original = opcode == 0x4A;
                self.op_md(use_original)?;
            }
            0x4B => {
                // MPPEM - measure pixels per em
                self.push(self.ppem as i32)?;
            }
            0x4C => {
                // MPS - measure point size (F26Dot6)
                self.push(self.point_size)?;
            }

            // ── Boolean / flip ───────────────────────────────────
            0x4D => {
                // FLIPON
                self.gs.auto_flip = true;
            }
            0x4E => {
                // FLIPOFF
                self.gs.auto_flip = false;
            }
            0x4F => {
                // DEBUG - no-op
            }

            // ── Comparison ───────────────────────────────────────
            0x50 => {
                // LT
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(if a < b { 1 } else { 0 })?;
            }
            0x51 => {
                // LTEQ
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(if a <= b { 1 } else { 0 })?;
            }
            0x52 => {
                // GT
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(if a > b { 1 } else { 0 })?;
            }
            0x53 => {
                // GTEQ
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(if a >= b { 1 } else { 0 })?;
            }
            0x54 => {
                // EQ
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(if a == b { 1 } else { 0 })?;
            }
            0x55 => {
                // NEQ
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(if a != b { 1 } else { 0 })?;
            }
            0x56 => {
                // ODD
                let v = self.pop()?;
                let rounded = self.gs.round(F26Dot6::from_bits(v));
                self.push(if (rounded.to_i32() & 1) != 0 { 1 } else { 0 })?;
            }
            0x57 => {
                // EVEN
                let v = self.pop()?;
                let rounded = self.gs.round(F26Dot6::from_bits(v));
                self.push(if (rounded.to_i32() & 1) == 0 { 1 } else { 0 })?;
            }

            // ── IF / ELSE / EIF ──────────────────────────────────
            0x58 => {
                // IF
                let cond = self.pop()?;
                if cond == 0 {
                    self.skip_to_else_or_eif(bytecode, ip)?;
                }
            }
            0x59 => {
                // EIF - end if (no-op when reached normally)
            }

            // ── Logic ────────────────────────────────────────────
            0x5A => {
                // AND
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(if a != 0 && b != 0 { 1 } else { 0 })?;
            }
            0x5B => {
                // OR
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(if a != 0 || b != 0 { 1 } else { 0 })?;
            }
            0x5C => {
                // NOT
                let v = self.pop()?;
                self.push(if v == 0 { 1 } else { 0 })?;
            }

            // ── Delta instructions ───────────────────────────────
            0x5D => self.op_deltap(1, bytecode, ip)?, // DELTAP1
            0x5E => {
                // SDB - set delta base
                let new_base = self.pop()? as u16;
                self.gs.delta_base = new_base;
            }
            0x5F => {
                // SDS - set delta shift
                self.gs.delta_shift = self.pop()? as u16;
            }

            // ── Arithmetic ───────────────────────────────────────
            0x60 => {
                // ADD
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(a.wrapping_add(b))?;
            }
            0x61 => {
                // SUB
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(a.wrapping_sub(b))?;
            }
            0x62 => {
                // DIV
                let b = self.pop()?;
                if b == 0 {
                    return Err(HintError::DivideByZero);
                }
                let a = self.pop()?;
                // F26Dot6 division: a * 64 / b with FreeType-compatible rounding
                let result = ft_muldiv(a as i64, 64, b as i64);
                self.push(result as i32)?;
            }
            0x63 => {
                // MUL
                let b = self.pop()?;
                let a = self.pop()?;
                // F26Dot6 multiplication: a * b / 64 with FreeType-compatible rounding
                let result = ft_muldiv(a as i64, b as i64, 64);
                self.push(result as i32)?;
            }
            0x64 => {
                // ABS
                let v = self.pop()?;
                self.push(v.abs())?;
            }
            0x65 => {
                // NEG
                let v = self.pop()?;
                self.push(-v)?;
            }
            0x66 => {
                // FLOOR
                let v = self.pop()?;
                self.push(v & !63)?;
            }
            0x67 => {
                // CEILING
                let v = self.pop()?;
                self.push((v + 63) & !63)?;
            }

            // ── ROUND / NROUND ───────────────────────────────────
            0x68..=0x6B => {
                // ROUND[ab] - round value
                let v = self.pop()?;
                let result = self.gs.round(F26Dot6::from_bits(v));
                self.push(result.to_bits())?;
            }
            0x6C..=0x6F => {
                // NROUND[ab] - no-round (pass through)
                // Value stays on stack as-is
            }

            // ── More CVT / Delta ─────────────────────────────────
            0x70 => {
                // WCVTF - write CVT in FUnits
                let val = self.pop()?; // value in FUnits
                let idx = self.pop()? as u32;
                let scaled = F26Dot6::from_funits(val, self.scale).to_bits();
                let i = idx as usize;
                if i >= self.cvt.len() {
                    self.cvt.resize(i + 1, 0);
                }
                self.cvt[i] = scaled;
            }
            0x71 => self.op_deltap(2, bytecode, ip)?, // DELTAP2
            0x72 => self.op_deltap(3, bytecode, ip)?, // DELTAP3
            0x73 => self.op_deltac(1)?,               // DELTAC1
            0x74 => self.op_deltac(2)?,               // DELTAC2
            0x75 => self.op_deltac(3)?,               // DELTAC3

            // ── Super rounding ───────────────────────────────────
            0x76 => {
                // SROUND
                let n = self.pop()? as u32;
                self.gs.set_super_round(n, false);
                self.gs.round_state = RoundState::Super;
            }
            0x77 => {
                // S45ROUND
                let n = self.pop()? as u32;
                self.gs.set_super_round(n, true);
                self.gs.round_state = RoundState::Super45;
            }

            // ── Conditional jumps ────────────────────────────────
            0x78 => {
                // JROT - jump relative on true
                let cond = self.pop()?;
                let offset = self.pop()?;
                if cond != 0 {
                    // offset is relative to the JROT opcode position (*ip - 1)
                    let opcode_pos = *ip as i64 - 1;
                    let new_ip = opcode_pos + offset as i64;
                    if new_ip < 0 || new_ip > bytecode.len() as i64 {
                        return Err(HintError::InvalidJump);
                    }
                    *ip = new_ip as usize;
                }
            }
            0x79 => {
                // JROF - jump relative on false
                let cond = self.pop()?;
                let offset = self.pop()?;
                if cond == 0 {
                    // offset is relative to the JROF opcode position (*ip - 1)
                    let opcode_pos = *ip as i64 - 1;
                    let new_ip = opcode_pos + offset as i64;
                    if new_ip < 0 || new_ip > bytecode.len() as i64 {
                        return Err(HintError::InvalidJump);
                    }
                    *ip = new_ip as usize;
                }
            }

            0x7A => {
                // ROFF - round off
                self.gs.round_state = RoundState::Off;
            }

            0x7C => {
                // RUTG - round up to grid
                self.gs.round_state = RoundState::UpToGrid;
            }
            0x7D => {
                // RDTG - round down to grid
                self.gs.round_state = RoundState::DownToGrid;
            }
            0x7E => {
                // SANGW (obsolete) - no-op
                self.pop()?;
            }
            0x7F => {
                // AA (obsolete) - no-op
                self.pop()?;
            }

            // ── Flip instructions ────────────────────────────────
            0x80 => {
                // FLIPPT
                self.op_flippt()?;
            }
            0x81 => {
                // FLIPRGON
                let hi = self.pop()? as usize;
                let lo = self.pop()? as usize;
                for i in lo..=hi {
                    if let Some(flags) = self.zones[1].flags.get_mut(i) {
                        flags.insert(PointFlags::ON_CURVE);
                    }
                }
            }
            0x82 => {
                // FLIPRGOFF
                let hi = self.pop()? as usize;
                let lo = self.pop()? as usize;
                for i in lo..=hi {
                    if let Some(flags) = self.zones[1].flags.get_mut(i) {
                        flags.remove(PointFlags::ON_CURVE);
                    }
                }
            }

            0x85 => {
                // SCANCTRL
                self.gs.scan_control = self.pop()? as u32;
            }
            0x86..=0x87 => {
                // SDPVTL[a] - set dual projection vector to line
                let a = opcode & 1;
                self.op_sdpvtl(a != 0)?;
            }
            0x88 => {
                // GETINFO
                self.op_getinfo()?;
            }
            0x89 => {
                // IDEF - instruction definition
                let instr_id = self.pop()? as u32;
                let start = *ip;
                let end = self.find_endf(bytecode, ip)?;
                let func_bytecode = bytecode[start..end].to_vec();
                if (instr_id as usize) < self.idefs.len() {
                    self.idefs[instr_id as usize] = Some(FuncDef {
                        bytecode: func_bytecode,
                    });
                }
            }
            0x8A => {
                // ROLL - roll top 3 stack elements
                let len = self.stack.len();
                if len < 3 {
                    return Err(HintError::StackUnderflow);
                }
                let a = self.stack[len - 1]; // top
                let b = self.stack[len - 2]; // second
                let c = self.stack[len - 3]; // third
                // Move third to top: [a,b,c] → [c,a,b]
                self.stack[len - 1] = c;
                self.stack[len - 2] = a;
                self.stack[len - 3] = b;
            }
            0x8B => {
                // MAX
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(a.max(b))?;
            }
            0x8C => {
                // MIN
                let b = self.pop()?;
                let a = self.pop()?;
                self.push(a.min(b))?;
            }
            0x8D => {
                // SCANTYPE
                self.gs.scan_type = self.pop()?;
            }
            0x8E => {
                // INSTCTRL - pops selector (s) then value (v)
                let s = self.pop()? as u32;
                let v = self.pop()? as u32;
                if s >= 1 && s <= 3 {
                    let mask = 1u8 << (s - 1);
                    if v != 0 {
                        self.gs.instruct_control |= mask;
                    } else {
                        self.gs.instruct_control &= !mask;
                    }
                }
            }

            // ── PUSHB / PUSHW ────────────────────────────────────
            0xB0..=0xB7 => {
                // PUSHB[n] - push n+1 bytes
                let count = (opcode - 0xB0 + 1) as usize;
                if *ip + count > bytecode.len() {
                    return Err(HintError::UnexpectedEndOfBytecode);
                }
                for i in 0..count {
                    self.push(bytecode[*ip + i] as i32)?;
                }
                *ip += count;
            }
            0xB8..=0xBF => {
                // PUSHW[n] - push n+1 words (signed 16-bit)
                let count = (opcode - 0xB8 + 1) as usize;
                if *ip + count * 2 > bytecode.len() {
                    return Err(HintError::UnexpectedEndOfBytecode);
                }
                for i in 0..count {
                    let hi = bytecode[*ip + i * 2] as i8;
                    let lo = bytecode[*ip + i * 2 + 1];
                    let val = ((hi as i32) << 8) | (lo as i32);
                    self.push(val)?;
                }
                *ip += count * 2;
            }

            // ── MDRP ─────────────────────────────────────────────
            0xC0..=0xDF => {
                // MDRP[abcde] - move direct relative point
                self.op_mdrp(opcode)?;
            }

            // ── MIRP ─────────────────────────────────────────────
            0xE0..=0xFF => {
                // MIRP[abcde] - move indirect relative point
                self.op_mirp(opcode)?;
            }

            // Unused / reserved opcodes
            _ => {
                // Check IDEFs for this opcode
                if let Some(Some(idef)) = self.idefs.get(opcode as usize) {
                    let func = idef.bytecode.clone();
                    self.call_depth += 1;
                    self.execute(&func)?;
                    self.call_depth -= 1;
                }
                // Otherwise silently ignore (compatibility)
            }
        }
        Ok(())
    }

    // ── Stack helpers ────────────────────────────────────────────────

    fn push(&mut self, val: i32) -> Result<(), HintError> {
        if self.stack.len() >= self.max_stack {
            return Err(HintError::StackOverflow);
        }
        self.stack.push(val);
        Ok(())
    }

    fn pop(&mut self) -> Result<i32, HintError> {
        self.stack.pop().ok_or(HintError::StackUnderflow)
    }

    fn peek(&self) -> Result<i32, HintError> {
        self.stack.last().copied().ok_or(HintError::StackUnderflow)
    }

    fn read_cvt(&self, idx: u32) -> Result<i32, HintError> {
        self.cvt
            .get(idx as usize)
            .copied()
            .ok_or(HintError::InvalidCvtIndex(idx))
    }

    // ── Control flow helpers ─────────────────────────────────────────

    /// Skip bytecode until matching ELSE or EIF for an IF whose condition was false.
    fn skip_to_else_or_eif(
        &self,
        bytecode: &[u8],
        ip: &mut usize,
    ) -> Result<(), HintError> {
        let mut depth = 1u32;
        while *ip < bytecode.len() {
            let op = bytecode[*ip];
            *ip += 1;
            match op {
                0x58 => depth += 1, // nested IF
                0x59 => {
                    // EIF
                    depth -= 1;
                    if depth == 0 {
                        return Ok(());
                    }
                }
                0x1B => {
                    // ELSE
                    if depth == 1 {
                        return Ok(());
                    }
                }
                // Skip inline data for push instructions
                0x40 => {
                    // NPUSHB
                    if *ip >= bytecode.len() {
                        return Err(HintError::UnexpectedEndOfBytecode);
                    }
                    let n = bytecode[*ip] as usize;
                    *ip += 1 + n;
                }
                0x41 => {
                    // NPUSHW
                    if *ip >= bytecode.len() {
                        return Err(HintError::UnexpectedEndOfBytecode);
                    }
                    let n = bytecode[*ip] as usize;
                    *ip += 1 + n * 2;
                }
                0xB0..=0xB7 => *ip += (op - 0xB0 + 1) as usize,
                0xB8..=0xBF => *ip += ((op - 0xB8 + 1) * 2) as usize,
                _ => {}
            }
        }
        Err(HintError::UnexpectedEndOfBytecode)
    }

    /// Skip from ELSE to matching EIF.
    fn skip_else(
        &self,
        bytecode: &[u8],
        ip: &mut usize,
    ) -> Result<(), HintError> {
        let mut depth = 1u32;
        while *ip < bytecode.len() {
            let op = bytecode[*ip];
            *ip += 1;
            match op {
                0x58 => depth += 1, // nested IF
                0x59 => {
                    // EIF
                    depth -= 1;
                    if depth == 0 {
                        return Ok(());
                    }
                }
                // Skip inline data
                0x40 => {
                    if *ip >= bytecode.len() {
                        return Err(HintError::UnexpectedEndOfBytecode);
                    }
                    let n = bytecode[*ip] as usize;
                    *ip += 1 + n;
                }
                0x41 => {
                    if *ip >= bytecode.len() {
                        return Err(HintError::UnexpectedEndOfBytecode);
                    }
                    let n = bytecode[*ip] as usize;
                    *ip += 1 + n * 2;
                }
                0xB0..=0xB7 => *ip += (op - 0xB0 + 1) as usize,
                0xB8..=0xBF => *ip += ((op - 0xB8 + 1) * 2) as usize,
                _ => {}
            }
        }
        Err(HintError::UnexpectedEndOfBytecode)
    }

    /// Find matching ENDF for a FDEF/IDEF, advancing ip past it.
    fn find_endf(&self, bytecode: &[u8], ip: &mut usize) -> Result<usize, HintError> {
        let mut depth = 1u32;
        while *ip < bytecode.len() {
            let op = bytecode[*ip];
            *ip += 1;
            match op {
                0x2C | 0x89 => depth += 1, // nested FDEF or IDEF
                0x2D => {
                    // ENDF
                    depth -= 1;
                    if depth == 0 {
                        return Ok(*ip - 1); // position of ENDF
                    }
                }
                // Skip inline data
                0x40 => {
                    if *ip >= bytecode.len() {
                        return Err(HintError::UnexpectedEndOfBytecode);
                    }
                    let n = bytecode[*ip] as usize;
                    *ip += 1 + n;
                }
                0x41 => {
                    if *ip >= bytecode.len() {
                        return Err(HintError::UnexpectedEndOfBytecode);
                    }
                    let n = bytecode[*ip] as usize;
                    *ip += 1 + n * 2;
                }
                0xB0..=0xB7 => *ip += (op - 0xB0 + 1) as usize,
                0xB8..=0xBF => *ip += ((op - 0xB8 + 1) * 2) as usize,
                _ => {}
            }
        }
        Err(HintError::UnexpectedEndOfBytecode)
    }

    // ── Projection / freedom vector helpers ──────────────────────────

    /// Project a point onto the projection vector, returning F26Dot6 distance.
    fn project(&self, p: Point) -> i32 {
        let (px, py) = self.gs.projection_vector;
        ((p.x as i64 * px.to_bits() as i64 + p.y as i64 * py.to_bits() as i64 + 0x2000) >> 14)
            as i32
    }

    /// Project using the dual projection vector (for measuring original distances).
    fn dual_project(&self, p: Point) -> i32 {
        let (px, py) = self.gs.dual_projection_vector;
        ((p.x as i64 * px.to_bits() as i64 + p.y as i64 * py.to_bits() as i64 + 0x2000) >> 14)
            as i32
    }

    /// Move a point along the freedom vector by a given F26Dot6 distance.
    fn move_point(&mut self, zone: usize, point: usize, distance: i32) {
        // Dynamically grow zone if needed, with cap to prevent OOM
        if point >= self.zones[zone].current.len() {
            if point > 10_000 {
                return; // silently ignore bogus point indices
            }
            self.zones[zone].resize(point + 1);
        }

        let (fx, fy) = self.gs.freedom_vector;
        let (px, py) = self.gs.projection_vector;

        // Compute the dot product of freedom and projection vectors
        let dot = (fx.to_bits() as i64 * px.to_bits() as i64
            + fy.to_bits() as i64 * py.to_bits() as i64
            + 0x2000)
            >> 14;

        if dot == 0 {
            return;
        }

        // displacement = distance * freedom_vector / (freedom_vector · projection_vector)
        // Use FreeType-compatible signed division: convert to absolute values,
        // round with positive bias, then re-apply sign. This prevents
        // truncation-toward-zero errors that cause negative displacements
        // to under-apply by 1 F26Dot6 unit.
        let dx = ft_muldiv(distance as i64, fx.to_bits() as i64, dot) as i32;
        let dy = ft_muldiv(distance as i64, fy.to_bits() as i64, dot) as i32;

        self.zones[zone].current[point].x += dx;
        self.zones[zone].current[point].y += dy;

        // Set touched flags
        if fx.to_bits() != 0 {
            self.zones[zone].flags[point].insert(PointFlags::TOUCHED_X);
        }
        if fy.to_bits() != 0 {
            self.zones[zone].flags[point].insert(PointFlags::TOUCHED_Y);
        }
    }

    // ── Point / zone access helpers ──────────────────────────────────

    fn get_point(&mut self, zone: usize, index: u32) -> Result<Point, HintError> {
        let z = self.zones.get_mut(zone).ok_or(HintError::InvalidPointIndex(index))?;
        let i = index as usize;
        // Dynamically grow zone if needed (twilight zone may exceed maxp limits)
        // Cap at reasonable limit to prevent OOM from corrupted indices
        if i >= z.current.len() {
            if i > 10_000 {
                return Err(HintError::InvalidPointIndex(index));
            }
            z.resize(i + 1);
        }
        Ok(z.current[i])
    }

    fn get_original_point(&mut self, zone: usize, index: u32) -> Result<Point, HintError> {
        let z = self.zones.get_mut(zone).ok_or(HintError::InvalidPointIndex(index))?;
        let i = index as usize;
        if i >= z.original.len() {
            if i > 10_000 {
                return Err(HintError::InvalidPointIndex(index));
            }
            z.resize(i + 1);
        }
        Ok(z.original[i])
    }

    // ── Vector-from-line instructions ────────────────────────────────

    fn op_spvtl(&mut self, perpendicular: bool) -> Result<(), HintError> {
        // FreeType convention: top→p1 with zp2, lower→p2 with zp1
        // Vector direction: zp1[p2] - zp2[p1] (matching FreeType's DO_SPVTL)
        let p1_idx = self.pop()? as u32; // top of stack
        let p2_idx = self.pop()? as u32; // second
        let p2 = self.get_point(self.gs.zp1 as usize, p2_idx)?;
        let p1 = self.get_point(self.gs.zp2 as usize, p1_idx)?;
        let v = self.compute_vector_from_line(p1, p2, perpendicular);
        self.gs.projection_vector = v;
        self.gs.dual_projection_vector = v;
        Ok(())
    }

    fn op_sfvtl(&mut self, perpendicular: bool) -> Result<(), HintError> {
        // Same convention as SPVTL: top→p1/zp2, lower→p2/zp1
        let p1_idx = self.pop()? as u32;
        let p2_idx = self.pop()? as u32;
        let p2 = self.get_point(self.gs.zp1 as usize, p2_idx)?;
        let p1 = self.get_point(self.gs.zp2 as usize, p1_idx)?;
        self.gs.freedom_vector = self.compute_vector_from_line(p1, p2, perpendicular);
        Ok(())
    }

    fn op_sdpvtl(&mut self, perpendicular: bool) -> Result<(), HintError> {
        // Same convention as SPVTL: top→p1/zp2, lower→p2/zp1
        let p1_idx = self.pop()? as u32;
        let p2_idx = self.pop()? as u32;

        // Use current points for projection vector
        let p2 = self.get_point(self.gs.zp1 as usize, p2_idx)?;
        let p1 = self.get_point(self.gs.zp2 as usize, p1_idx)?;
        self.gs.projection_vector = self.compute_vector_from_line(p1, p2, perpendicular);

        // Use original points for dual projection vector
        let op2 = self.get_original_point(self.gs.zp1 as usize, p2_idx)?;
        let op1 = self.get_original_point(self.gs.zp2 as usize, p1_idx)?;
        self.gs.dual_projection_vector = self.compute_vector_from_line(op1, op2, perpendicular);

        Ok(())
    }

    fn compute_vector_from_line(
        &self,
        p1: Point,
        p2: Point,
        perpendicular: bool,
    ) -> (F2Dot14, F2Dot14) {
        let dx = (p2.x - p1.x) as f64;
        let dy = (p2.y - p1.y) as f64;
        let len = (dx * dx + dy * dy).sqrt();
        if len < 1.0e-10 {
            return (F2Dot14::ONE, F2Dot14::ZERO);
        }
        let (nx, ny) = if perpendicular {
            (-dy / len, dx / len)
        } else {
            (dx / len, dy / len)
        };
        (F2Dot14::from_f64(nx), F2Dot14::from_f64(ny))
    }

    // ── Point movement instructions ──────────────────────────────────

    /// MDAP[a] — Move Direct Absolute Point.
    /// Spec: MS OpenType §tt_instructions, Apple TrueType RM §5.
    /// Touches point `p` in zp0; if `round` is set, rounds the projected
    /// coordinate to grid.  Sets rp0 = rp1 = p.
    ///
    /// **Twilight zone**: if zp0 == 0, the original coordinate is copied
    /// from the current coordinate *before* any movement so that later
    /// instructions (MIRP, IP) that read the original get a meaningful
    /// value instead of the initial zero.
    fn op_mdap(&mut self, round: bool) -> Result<(), HintError> {
        let p = self.pop()? as u32;
        let zp0 = self.gs.zp0 as usize;

        // Twilight zone: set original = current before projecting.
        // TODO: investigate correct twilight zone initialization
        if zp0 == 0 {
            let i = p as usize;
            self.zones[0].ensure_capacity(i + 1);
            self.zones[0].original[i] = self.zones[0].current[i];
        }

        let point = self.get_point(zp0, p)?;
        let cur_dist = self.project(point);

        let distance = if round {
            let rounded = self.gs.round(F26Dot6::from_bits(cur_dist));
            rounded.to_bits() - cur_dist
        } else {
            0
        };

        self.move_point(zp0, p as usize, distance);
        self.gs.rp0 = p;
        self.gs.rp1 = p;
        Ok(())
    }

    /// MIAP[a] — Move Indirect Absolute Point.
    /// Spec: MS OpenType §tt_instructions, Apple TrueType RM §5.
    /// Moves point `p` in zp0 to the CVT value (rounded if `a` bit set,
    /// subject to control_value_cut_in).  Sets rp0 = rp1 = p.
    ///
    /// **Twilight zone**: if zp0 == 0, the point's original *and* current
    /// coordinates are initialized from the CVT value along the freedom
    /// vector.  This is how the `prep` program creates twilight reference
    /// points that encode key font measurements (cap height, x-height,
    /// ascender, etc.).  Without this, twilight points stay at (0,0) and
    /// glyph programs that reference them via MIRP/IP get wrong distances.
    fn op_miap(&mut self, round: bool) -> Result<(), HintError> {
        let cvt_idx = self.pop()? as u32;
        let p = self.pop()? as u32;
        let zp0 = self.gs.zp0 as usize;

        let cvt_val = self.read_cvt(cvt_idx)?;

        // Twilight zone: initialize point from CVT value along freedom vector.
        // Per TrueType spec, MIAP in twilight zone sets original and current
        // coordinates from the CVT value. Disabled pending further investigation
        // as it regresses HelveticaNeue hinting (the prep program's twilight zone
        // setup interacts differently than expected).
        // TODO: investigate correct twilight zone initialization
        if zp0 == 0 {
            let (fx, fy) = self.gs.freedom_vector;
            let i = p as usize;
            self.zones[0].ensure_capacity(i + 1);
            let ox = ft_muldiv(cvt_val as i64, fx.to_bits() as i64, 0x4000) as i32;
            let oy = ft_muldiv(cvt_val as i64, fy.to_bits() as i64, 0x4000) as i32;
            self.zones[0].original[i] = Point { x: ox, y: oy };
            self.zones[0].current[i] = Point { x: ox, y: oy };
        }

        let point = self.get_point(zp0, p)?;
        let cur_dist = self.project(point);

        let distance = if round {
            let diff = (cvt_val - cur_dist).abs();
            let target = if diff <= self.gs.control_value_cut_in.to_bits() {
                cvt_val
            } else {
                cur_dist
            };
            let rounded = self.gs.round(F26Dot6::from_bits(target));
            rounded.to_bits() - cur_dist
        } else {
            cvt_val - cur_dist
        };

        self.move_point(zp0, p as usize, distance);
        self.gs.rp0 = p;
        self.gs.rp1 = p;
        Ok(())
    }

    /// MDRP[abcde] — Move Direct Relative Point.
    /// Spec: MS OpenType §tt_instructions, Apple TrueType RM §5.
    /// Moves point `p` (in zp1) relative to rp0 (in zp0) by the *measured*
    /// original distance (no CVT lookup), with optional rounding, minimum
    /// distance, and single-width overrides.
    fn op_mdrp(&mut self, opcode: u8) -> Result<(), HintError> {
        let p = self.pop()? as u32;
        let zp0 = self.gs.zp0 as usize;
        let zp1 = self.gs.zp1 as usize;

        // Flags from opcode bits: 0xC0 + [set_rp0, respect_min_dist, round, _, _]
        let set_rp0 = (opcode >> 4) & 1 != 0;
        let respect_min_dist = (opcode >> 3) & 1 != 0;
        let do_round = (opcode >> 2) & 1 != 0;

        // Measure original distance between rp0 and point (before any adjustments)
        let rp0_orig = self.get_original_point(zp0, self.gs.rp0)?;
        let p_orig = self.get_original_point(zp1, p)?;
        let orig_dist = self.dual_project(Point {
            x: p_orig.x - rp0_orig.x,
            y: p_orig.y - rp0_orig.y,
        });
        let mut dist = orig_dist;

        // Apply single width
        let swv = self.gs.single_width_value.to_bits();
        if swv != 0 {
            let swci = self.gs.single_width_cut_in.to_bits();
            if (dist - swv).abs() < swci {
                dist = if dist >= 0 { swv } else { -swv };
            }
        }

        if do_round {
            dist = self.gs.round(F26Dot6::from_bits(dist)).to_bits();
        }

        if respect_min_dist {
            let min_dist = self.gs.minimum_distance.to_bits();
            if dist >= 0 {
                if dist < min_dist {
                    // When dist rounds to zero from a negative original distance,
                    // apply minimum in the ORIGINAL direction to avoid sign flip.
                    dist = if self.fix_min_distance_sign && dist == 0 && orig_dist < 0 { -min_dist } else { min_dist };
                }
            } else if dist > -min_dist {
                dist = -min_dist;
            }
        }

        // Current position of reference point
        let rp0_cur = self.get_point(zp0, self.gs.rp0)?;
        let p_cur = self.get_point(zp1, p)?;
        let cur_dist = self.project(Point {
            x: p_cur.x - rp0_cur.x,
            y: p_cur.y - rp0_cur.y,
        });

        self.move_point(zp1, p as usize, dist - cur_dist);

        self.gs.rp1 = self.gs.rp0;
        self.gs.rp2 = p;
        if set_rp0 {
            self.gs.rp0 = p;
        }
        Ok(())
    }

    /// MIRP[abcde] — Move Indirect Relative Point.
    /// Spec: MS OpenType §tt_instructions, Apple TrueType RM §5.
    /// Moves point `p` (in zp1) relative to rp0 (in zp0) by a CVT distance,
    /// with optional rounding, minimum distance, and auto-flip.
    ///
    /// **Twilight zone**: if zp1 == 0, the target point's original and current
    /// coordinates are set to rp0_orig + CVT distance along the freedom vector.
    /// This initializes the twilight point so that orig_dist calculations work.
    fn op_mirp(&mut self, opcode: u8) -> Result<(), HintError> {
        let cvt_idx = self.pop()? as u32;
        let p = self.pop()? as u32;
        let zp0 = self.gs.zp0 as usize;
        let zp1 = self.gs.zp1 as usize;

        let set_rp0 = (opcode >> 4) & 1 != 0;
        let respect_min_dist = (opcode >> 3) & 1 != 0;
        let do_round = (opcode >> 2) & 1 != 0;

        let cvt_val = if cvt_idx < self.cvt.len() as u32 {
            self.cvt[cvt_idx as usize]
        } else {
            0
        };

        // Twilight zone: initialize point from rp0_orig + CVT along freedom vector.
        // TODO: investigate correct twilight zone initialization
        if zp1 == 0 {
            let (fx, fy) = self.gs.freedom_vector;
            let rp0_orig = self.get_original_point(zp0, self.gs.rp0)?;
            let i = p as usize;
            self.zones[0].ensure_capacity(i + 1);
            let ox = rp0_orig.x + ft_muldiv(cvt_val as i64, fx.to_bits() as i64, 0x4000) as i32;
            let oy = rp0_orig.y + ft_muldiv(cvt_val as i64, fy.to_bits() as i64, 0x4000) as i32;
            self.zones[0].original[i] = Point { x: ox, y: oy };
            self.zones[0].current[i] = Point { x: ox, y: oy };
        }

        // Measure original distance
        let rp0_orig = self.get_original_point(zp0, self.gs.rp0)?;
        let p_orig = self.get_original_point(zp1, p)?;
        let orig_dist = self.dual_project(Point {
            x: p_orig.x - rp0_orig.x,
            y: p_orig.y - rp0_orig.y,
        });

        let mut dist = cvt_val;

        // Auto flip
        if self.gs.auto_flip {
            if (orig_dist >= 0) != (dist >= 0) {
                dist = -dist;
            }
        }

        // Apply single width
        let swv = self.gs.single_width_value.to_bits();
        if swv != 0 {
            let swci = self.gs.single_width_cut_in.to_bits();
            if (dist - swv).abs() < swci {
                dist = if dist >= 0 { swv } else { -swv };
            }
        }

        // CVT cut-in: if actual distance is too far from CVT value, use actual
        let cvt_ci = self.gs.control_value_cut_in.to_bits();
        if do_round && (dist - orig_dist).abs() > cvt_ci {
            dist = orig_dist;
        }

        if do_round {
            dist = self.gs.round(F26Dot6::from_bits(dist)).to_bits();
        }

        if respect_min_dist {
            let min_dist = self.gs.minimum_distance.to_bits();
            if dist >= 0 {
                if dist < min_dist {
                    // When dist rounds to zero from a negative original distance,
                    // apply minimum in the ORIGINAL direction to avoid sign flip.
                    dist = if self.fix_min_distance_sign && dist == 0 && orig_dist < 0 { -min_dist } else { min_dist };
                }
            } else if dist > -min_dist {
                dist = -min_dist;
            }
        }

        // Move point
        let rp0_cur = self.get_point(zp0, self.gs.rp0)?;
        let p_cur = self.get_point(zp1, p)?;
        let cur_dist = self.project(Point {
            x: p_cur.x - rp0_cur.x,
            y: p_cur.y - rp0_cur.y,
        });

        self.move_point(zp1, p as usize, dist - cur_dist);

        self.gs.rp1 = self.gs.rp0;
        self.gs.rp2 = p;
        if set_rp0 {
            self.gs.rp0 = p;
        }
        Ok(())
    }

    /// MSIRP[a] — Move Stack Indirect Relative Point.
    /// Spec: MS OpenType §tt_instructions.
    /// Moves point `p` (in zp1) so its distance from rp0 (in zp0) along
    /// the projection vector equals `dist` (popped from stack in F26Dot6).
    ///
    /// **Twilight zone**: if zp1 == 0, initialize point from rp0_orig.
    fn op_msirp(&mut self, set_rp0: bool) -> Result<(), HintError> {
        let dist = self.pop()?; // F26Dot6
        let p = self.pop()? as u32;
        let zp0 = self.gs.zp0 as usize;
        let zp1 = self.gs.zp1 as usize;

        // Twilight zone: initialize point from rp0 original position
        // TODO: investigate correct twilight zone initialization
        if zp1 == 0 {
            let rp0_orig = self.get_original_point(zp0, self.gs.rp0)?;
            let i = p as usize;
            self.zones[0].ensure_capacity(i + 1);
            self.zones[0].original[i] = rp0_orig;
            self.zones[0].current[i] = rp0_orig;
        }

        let rp0_cur = self.get_point(zp0, self.gs.rp0)?;
        let p_cur = self.get_point(zp1, p)?;
        let cur_dist = self.project(Point {
            x: p_cur.x - rp0_cur.x,
            y: p_cur.y - rp0_cur.y,
        });

        self.move_point(zp1, p as usize, dist - cur_dist);

        self.gs.rp1 = self.gs.rp0;
        self.gs.rp2 = p;
        if set_rp0 {
            self.gs.rp0 = p;
        }
        Ok(())
    }

    fn op_alignrp(&mut self) -> Result<(), HintError> {
        let loop_count = self.gs.loop_value;
        self.gs.loop_value = 1;

        let zp0 = self.gs.zp0 as usize;
        let zp1 = self.gs.zp1 as usize;
        let rp0 = self.gs.rp0;

        for _ in 0..loop_count {
            let p = self.pop()? as u32;
            let rp0_cur = self.get_point(zp0, rp0)?;
            let p_cur = self.get_point(zp1, p)?;
            let cur_dist = self.project(Point {
                x: p_cur.x - rp0_cur.x,
                y: p_cur.y - rp0_cur.y,
            });
            self.move_point(zp1, p as usize, -cur_dist);
        }
        Ok(())
    }

    fn op_alignpts(&mut self) -> Result<(), HintError> {
        let p2 = self.pop()? as u32;
        let p1 = self.pop()? as u32;
        let zp0 = self.gs.zp0 as usize;
        let zp1 = self.gs.zp1 as usize;

        let p1_cur = self.get_point(zp1, p1)?;
        let p2_cur = self.get_point(zp0, p2)?;
        let dist = self.project(Point {
            x: p2_cur.x - p1_cur.x,
            y: p2_cur.y - p1_cur.y,
        });

        let half = dist / 2;
        self.move_point(zp1, p1 as usize, half);
        self.move_point(zp0, p2 as usize, -half);
        Ok(())
    }

    fn op_shp(&mut self, use_rp1: bool) -> Result<(), HintError> {
        let loop_count = self.gs.loop_value;
        self.gs.loop_value = 1;

        let (rp, rp_zone) = if use_rp1 {
            (self.gs.rp1, self.gs.zp0 as usize)
        } else {
            (self.gs.rp2, self.gs.zp1 as usize)
        };
        let zp2 = self.gs.zp2 as usize;

        // Compute displacement: difference between current and original of rp
        let rp_cur = self.get_point(rp_zone, rp)?;
        let rp_orig = self.get_original_point(rp_zone, rp)?;
        let displacement = self.project(Point {
            x: rp_cur.x - rp_orig.x,
            y: rp_cur.y - rp_orig.y,
        });

        for _ in 0..loop_count {
            let p = self.pop()? as u32;
            self.move_point(zp2, p as usize, displacement);
        }
        Ok(())
    }

    fn op_shc(&mut self, use_rp1: bool) -> Result<(), HintError> {
        let contour = self.pop()? as usize;

        let (rp, rp_zone) = if use_rp1 {
            (self.gs.rp1, self.gs.zp0 as usize)
        } else {
            (self.gs.rp2, self.gs.zp1 as usize)
        };
        let zp2 = self.gs.zp2 as usize;

        let rp_cur = self.get_point(rp_zone, rp)?;
        let rp_orig = self.get_original_point(rp_zone, rp)?;
        let displacement = self.project(Point {
            x: rp_cur.x - rp_orig.x,
            y: rp_cur.y - rp_orig.y,
        });

        // Get contour point range
        let start = if contour == 0 {
            0
        } else {
            self.zones[zp2]
                .contour_ends
                .get(contour - 1)
                .map(|&e| e as usize + 1)
                .unwrap_or(0)
        };
        let end = self.zones[zp2]
            .contour_ends
            .get(contour)
            .map(|&e| e as usize + 1)
            .unwrap_or(self.zones[zp2].len());

        for i in start..end {
            if i as u32 != rp {
                self.move_point(zp2, i, displacement);
            }
        }
        Ok(())
    }

    fn op_shz(&mut self, use_rp1: bool) -> Result<(), HintError> {
        let zone_idx = self.pop()? as u32;
        if zone_idx > 1 {
            return Err(HintError::InvalidZone(zone_idx));
        }

        let (rp, rp_zone) = if use_rp1 {
            (self.gs.rp1, self.gs.zp0 as usize)
        } else {
            (self.gs.rp2, self.gs.zp1 as usize)
        };

        let rp_cur = self.get_point(rp_zone, rp)?;
        let rp_orig = self.get_original_point(rp_zone, rp)?;
        let displacement = self.project(Point {
            x: rp_cur.x - rp_orig.x,
            y: rp_cur.y - rp_orig.y,
        });

        let z = zone_idx as usize;
        let n = self.zones[z].len();
        for i in 0..n {
            self.move_point(z, i, displacement);
        }
        Ok(())
    }

    fn op_shpix(&mut self) -> Result<(), HintError> {
        let dist = self.pop()?; // F26Dot6 pixels
        let loop_count = self.gs.loop_value;
        self.gs.loop_value = 1;

        let zp2 = self.gs.zp2 as usize;
        let (fx, fy) = self.gs.freedom_vector;

        for _ in 0..loop_count {
            let p = self.pop()? as u32;
            let i = p as usize;
            self.zones[zp2].ensure_capacity(i + 1);
            // Move directly along freedom vector (no projection)
            // Use ft_muldiv for correct signed rounding (FreeType's TT_MulFix14)
            let dx = ft_muldiv(dist as i64, fx.to_bits() as i64, 0x4000) as i32;
            let dy = ft_muldiv(dist as i64, fy.to_bits() as i64, 0x4000) as i32;
            self.zones[zp2].current[i].x += dx;
            self.zones[zp2].current[i].y += dy;

            if fx.to_bits() != 0 {
                self.zones[zp2].flags[i].insert(PointFlags::TOUCHED_X);
            }
            if fy.to_bits() != 0 {
                self.zones[zp2].flags[i].insert(PointFlags::TOUCHED_Y);
            }
        }
        Ok(())
    }

    /// IP — Interpolate Point.
    /// Spec: MS OpenType §tt_instructions, Apple TrueType RM §5.
    /// For each point popped from the stack, interpolates its position
    /// along the projection vector so that its relative placement between
    /// rp1 and rp2 is preserved from the original outline to the current
    /// (hinted) outline.  The interpolation factor is computed from
    /// original distances (using dual_project), applied to the current
    /// range, then the point is moved along the freedom vector.
    fn op_ip(&mut self) -> Result<(), HintError> {
        let loop_count = self.gs.loop_value;
        self.gs.loop_value = 1;

        let zp0 = self.gs.zp0 as usize;
        let zp1 = self.gs.zp1 as usize;
        let zp2 = self.gs.zp2 as usize;

        // Get reference points (original and current)
        let rp1_orig = self.get_original_point(zp0, self.gs.rp1)?;
        let rp2_orig = self.get_original_point(zp1, self.gs.rp2)?;
        let rp1_cur = self.get_point(zp0, self.gs.rp1)?;
        let rp2_cur = self.get_point(zp1, self.gs.rp2)?;

        let orig_range = self.dual_project(Point {
            x: rp2_orig.x - rp1_orig.x,
            y: rp2_orig.y - rp1_orig.y,
        });
        let cur_range = self.project(Point {
            x: rp2_cur.x - rp1_cur.x,
            y: rp2_cur.y - rp1_cur.y,
        });

        for _ in 0..loop_count {
            let p = self.pop()? as u32;
            let p_orig = self.get_original_point(zp2, p)?;
            let p_cur = self.get_point(zp2, p)?;

            let orig_dist = self.dual_project(Point {
                x: p_orig.x - rp1_orig.x,
                y: p_orig.y - rp1_orig.y,
            });

            let new_dist = if orig_range != 0 {
                ft_muldiv(cur_range as i64, orig_dist as i64, orig_range as i64) as i32
            } else {
                orig_dist
            };

            let cur_dist = self.project(Point {
                x: p_cur.x - rp1_cur.x,
                y: p_cur.y - rp1_cur.y,
            });

            self.move_point(zp2, p as usize, new_dist - cur_dist);
        }
        Ok(())
    }

    /// IUP[a] — Interpolate Untouched Points.
    /// Spec: MS OpenType §tt_instructions, Apple TrueType RM §5.
    /// Final pass: for each contour, walks between consecutive touched
    /// points, interpolating untouched points to preserve their relative
    /// position in the original outline.  Uses `orus` (unscaled font-unit
    /// coordinates) for interpolation factors to avoid F26Dot6 rounding
    /// errors, matching FreeType's approach.
    ///
    /// Points outside the touched range are shifted by the nearest touched
    /// point's delta; points between two touched points are linearly
    /// interpolated using FreeType's FT_DivFix/FT_MulFix for precision.
    fn op_iup(&mut self, axis: u8) -> Result<(), HintError> {
        let n_points = self.zones[1].len();
        if n_points == 0 {
            return Ok(());
        }

        let touched_flag = if axis == 1 {
            PointFlags::TOUCHED_X
        } else {
            PointFlags::TOUCHED_Y
        };

        // Collect all (contour_start, contour_end, touched_points) first
        // to avoid borrowing self.zones[1] while calling self.iup_interp.
        let mut work: Vec<(usize, usize, Vec<usize>)> = Vec::new();
        let mut contour_start = 0usize;
        let contour_ends: Vec<u16> = self.zones[1].contour_ends.clone();

        for &contour_end_u16 in &contour_ends {
            let contour_end = contour_end_u16 as usize;
            if contour_end >= n_points {
                break;
            }

            let mut touched_points: Vec<usize> = Vec::new();
            for i in contour_start..=contour_end {
                if self.zones[1].flags[i].contains(touched_flag) {
                    touched_points.push(i);
                }
            }

            if !touched_points.is_empty() {
                work.push((contour_start, contour_end, touched_points));
            }
            contour_start = contour_end + 1;
        }

        // Now perform interpolation
        for (contour_start, contour_end, touched_points) in &work {
            let n_touched = touched_points.len();
            for t in 0..n_touched {
                let t1_idx = touched_points[t];
                let t2_idx = touched_points[(t + 1) % n_touched];
                self.iup_interp(*contour_start, *contour_end, t1_idx, t2_idx, axis);
            }
        }
        Ok(())
    }

    fn iup_interp(
        &mut self,
        contour_start: usize,
        contour_end: usize,
        t1_idx: usize,
        t2_idx: usize,
        axis: u8,
    ) {
        let touched_flag = if axis == 1 {
            PointFlags::TOUCHED_X
        } else {
            PointFlags::TOUCHED_Y
        };

        // Walk from t1 to t2 (wrapping around contour), interpolating untouched points
        let contour_len = contour_end - contour_start + 1;

        let get_coord = |p: &Point| -> i32 {
            if axis == 1 { p.x } else { p.y }
        };

        // Use unscaled coordinates (orus) for interpolation factors — matches FreeType.
        // Unscaled integers avoid F26Dot6 rounding errors in range/factor computation.
        let t1_orus = get_coord(&self.zones[1].orus[t1_idx]);
        let t1_cur = get_coord(&self.zones[1].current[t1_idx]);
        let t2_orus = get_coord(&self.zones[1].orus[t2_idx]);
        let t2_cur = get_coord(&self.zones[1].current[t2_idx]);
        let t1_orig = get_coord(&self.zones[1].original[t1_idx]);
        let t2_orig = get_coord(&self.zones[1].original[t2_idx]);

        let delta1 = t1_cur - t1_orig;
        let delta2 = t2_cur - t2_orig;

        // Walk from t1+1 to t2-1 (wrapping)
        if contour_len <= 1 {
            return;
        }

        let mut i = t1_idx;
        loop {
            // Advance to next point in contour (wrapping)
            i = if i == contour_end {
                contour_start
            } else {
                i + 1
            };

            if i == t2_idx {
                break;
            }

            if self.zones[1].flags[i].contains(touched_flag) {
                continue;
            }

            // Use unscaled coordinates for interpolation (FreeType uses orus)
            let orus_i = get_coord(&self.zones[1].orus[i]);

            let new_coord = if t1_orus == t2_orus {
                // Both reference points are at the same position: shift
                let cur = get_coord(&self.zones[1].current[i]);
                cur + delta1
            } else {
                // Interpolate using unscaled coordinates for range/factor
                let lo_orus = t1_orus.min(t2_orus);
                let hi_orus = t1_orus.max(t2_orus);
                let lo_cur = if t1_orus < t2_orus { t1_cur } else { t2_cur };
                let hi_cur = if t1_orus < t2_orus { t2_cur } else { t1_cur };
                let lo_delta = if t1_orus < t2_orus { delta1 } else { delta2 };
                let hi_delta = if t1_orus < t2_orus { delta2 } else { delta1 };

                if orus_i <= lo_orus {
                    let orig = get_coord(&self.zones[1].original[i]);
                    orig + lo_delta
                } else if orus_i >= hi_orus {
                    let orig = get_coord(&self.zones[1].original[i]);
                    orig + hi_delta
                } else {
                    let range = (hi_orus - lo_orus) as i64;
                    let factor = (orus_i - lo_orus) as i64;
                    let scale = ft_divfix((hi_cur - lo_cur) as i64, range);
                    lo_cur + ft_mulfix(factor, scale) as i32
                }
            };

            if axis == 1 {
                self.zones[1].current[i].x = new_coord;
            } else {
                self.zones[1].current[i].y = new_coord;
            }
        }
    }

    // ── Coordinate / measurement ─────────────────────────────────────

    fn op_gc(&mut self, use_original: bool) -> Result<(), HintError> {
        let p = self.pop()? as u32;
        let zp2 = self.gs.zp2 as usize;
        let point = if use_original {
            self.get_original_point(zp2, p)?
        } else {
            self.get_point(zp2, p)?
        };
        let val = self.project(point);
        self.push(val)?;
        Ok(())
    }

    fn op_scfs(&mut self) -> Result<(), HintError> {
        let val = self.pop()?; // F26Dot6
        let p = self.pop()? as u32;
        let zp2 = self.gs.zp2 as usize;

        let point = self.get_point(zp2, p)?;
        let cur = self.project(point);
        self.move_point(zp2, p as usize, val - cur);
        Ok(())
    }

    fn op_md(&mut self, use_original: bool) -> Result<(), HintError> {
        let p2 = self.pop()? as u32;
        let p1 = self.pop()? as u32;

        let dist = if use_original {
            let p1_pt = self.get_original_point(self.gs.zp0 as usize, p1)?;
            let p2_pt = self.get_original_point(self.gs.zp1 as usize, p2)?;
            self.dual_project(Point {
                x: p2_pt.x - p1_pt.x,
                y: p2_pt.y - p1_pt.y,
            })
        } else {
            let p1_pt = self.get_point(self.gs.zp0 as usize, p1)?;
            let p2_pt = self.get_point(self.gs.zp1 as usize, p2)?;
            self.project(Point {
                x: p2_pt.x - p1_pt.x,
                y: p2_pt.y - p1_pt.y,
            })
        };

        self.push(dist)?;
        Ok(())
    }

    // ── Intersection ─────────────────────────────────────────────────

    fn op_isect(&mut self) -> Result<(), HintError> {
        let b1 = self.pop()? as u32;
        let b0 = self.pop()? as u32;
        let a1 = self.pop()? as u32;
        let a0 = self.pop()? as u32;
        let p = self.pop()? as u32;

        // Per TrueType spec: a0,a1 define line A using zp0; b0,b1 define line B using zp1
        let pa0 = self.get_point(self.gs.zp0 as usize, a0)?;
        let pa1 = self.get_point(self.gs.zp0 as usize, a1)?;
        let pb0 = self.get_point(self.gs.zp1 as usize, b0)?;
        let pb1 = self.get_point(self.gs.zp1 as usize, b1)?;

        // Line A: pa0 to pa1, Line B: pb0 to pb1
        let dax = (pa1.x - pa0.x) as i64;
        let day = (pa1.y - pa0.y) as i64;
        let dbx = (pb1.x - pb0.x) as i64;
        let dby = (pb1.y - pb0.y) as i64;

        let denom = dax * dby - day * dbx;

        let zp2 = self.gs.zp2 as usize;
        let i = p as usize;
        self.zones[zp2].ensure_capacity(i + 1);

        if denom.abs() < 1 {
            // Lines are parallel; use midpoint of endpoints
            self.zones[zp2].current[i].x = (pa0.x + pa1.x + pb0.x + pb1.x) / 4;
            self.zones[zp2].current[i].y = (pa0.y + pa1.y + pb0.y + pb1.y) / 4;
        } else {
            let dpx = (pb0.x - pa0.x) as i64;
            let dpy = (pb0.y - pa0.y) as i64;
            let numer = dpx * dby - dpy * dbx;
            let t = ft_muldiv(numer, 64, denom);

            self.zones[zp2].current[i].x = pa0.x + ft_muldiv(dax, t, 64) as i32;
            self.zones[zp2].current[i].y = pa0.y + ft_muldiv(day, t, 64) as i32;
        }

        self.zones[zp2].flags[i].insert(PointFlags::TOUCHED_X | PointFlags::TOUCHED_Y);
        Ok(())
    }

    // ── Flip ─────────────────────────────────────────────────────────

    fn op_flippt(&mut self) -> Result<(), HintError> {
        let loop_count = self.gs.loop_value;
        self.gs.loop_value = 1;

        for _ in 0..loop_count {
            let p = self.pop()? as usize;
            // FLIPPT uses the glyph zone (zone 1), not zp0 - per TrueType spec
            // "Flips a point to on-curve or off-curve" in the glyph outline zone
            if let Some(flags) = self.zones[1].flags.get_mut(p) {
                flags.toggle(PointFlags::ON_CURVE);
            }
        }
        Ok(())
    }

    // ── Delta instructions ───────────────────────────────────────────
    //
    // Spec: Apple TrueType Reference Manual §5, Table 5 (magnitude encoding).
    // Stack pop order: n, then n pairs of (arg, point/cvt) — arg is on top.
    // Magnitude encoding: bits 3-0 of arg:
    //   0=-8 steps, 1=-7, ..., 7=-1, 8=+1, 9=+2, ..., 15=+8
    // Step size: 1 / (1 << delta_shift) pixels (typically 1/8 px at shift=3).
    // DELTAP1/2/3 differ by range offset: 0, +16, +32 added to delta_base.
    // DELTAC1/2/3 modify CVT entries instead of moving points.

    /// DELTAP[n] — Delta Exception P.
    /// Moves point by a small amount at a specific ppem, for pixel-level tuning.
    fn op_deltap(
        &mut self,
        range: u8,
        _bytecode: &[u8],
        _ip: &mut usize,
    ) -> Result<(), HintError> {
        let n = self.pop()? as u32;
        let zp0 = self.gs.zp0 as usize;
        let delta_base = self.gs.delta_base as i32;
        let delta_shift = self.gs.delta_shift as i32;

        let range_offset = match range {
            1 => 0,
            2 => 16,
            3 => 32,
            _ => 0,
        };

        for _ in 0..n {
            let arg = self.pop()? as u32;
            let point_idx = self.pop()? as u32;

            let ppem_offset = ((arg >> 4) & 0x0F) as i32;
            let target_ppem = delta_base + range_offset + ppem_offset;

            if target_ppem == self.ppem as i32 {
                let magnitude = (arg & 0x0F) as i32;
                let delta = if magnitude < 8 {
                    // Spec: selector 0=-8, 1=-7, ..., 7=-1
                    -(8 - magnitude)
                } else {
                    // Spec: selector 8=+1, 9=+2, ..., 15=+8
                    magnitude - 7
                };
                // Scale by 1 / (1 << delta_shift)
                let scaled = if delta_shift > 0 {
                    delta * 64 / (1 << delta_shift)
                } else {
                    delta * 64
                };
                self.move_point(zp0, point_idx as usize, scaled);
            }
        }
        Ok(())
    }

    fn op_deltac(&mut self, range: u8) -> Result<(), HintError> {
        let n = self.pop()? as u32;
        let delta_base = self.gs.delta_base as i32;
        let delta_shift = self.gs.delta_shift as i32;

        let range_offset = match range {
            1 => 0,
            2 => 16,
            3 => 32,
            _ => 0,
        };

        for _ in 0..n {
            // TEST: try reversed pop order (cvt_idx first, then arg)
            let cvt_idx = self.pop()? as u32;
            let arg = self.pop()? as u32;

            let ppem_offset = ((arg >> 4) & 0x0F) as i32;
            let target_ppem = delta_base + range_offset + ppem_offset;

            if target_ppem == self.ppem as i32 {
                let magnitude = (arg & 0x0F) as i32;
                let delta = if magnitude < 8 {
                    // Spec: selector 0=-8, 1=-7, ..., 7=-1
                    -(8 - magnitude)
                } else {
                    // Spec: selector 8=+1, 9=+2, ..., 15=+8
                    magnitude - 7
                };
                let scaled = if delta_shift > 0 {
                    delta * 64 / (1 << delta_shift)
                } else {
                    delta * 64
                };

                let i = cvt_idx as usize;
                if i < self.cvt.len() {
                    self.cvt[i] += scaled;
                }
            }
        }
        Ok(())
    }

    // ── GETINFO ──────────────────────────────────────────────────────

    fn op_getinfo(&mut self) -> Result<(), HintError> {
        let selector = self.pop()? as u32;
        let mut result = 0u32;

        // Bit 0: engine version
        if selector & 1 != 0 {
            // Return version 40 (Windows DirectWrite / modern rasterizer).
            result |= 40;
        }

        // Bit 1: glyph rotated (we don't rotate, so false)
        // Bit 2: glyph stretched (we don't stretch, so false)

        // Bit 3: font variations active
        // (not currently supported in our interpreter)

        // Bit 5: grayscale rendering — we DO grayscale AA
        if selector & (1 << 5) != 0 {
            result |= 1 << 12; // grayscale bit
        }

        // Bit 6: ClearType enabled
        if selector & (1 << 6) != 0 {
            result |= 1 << 13; // ClearType enabled
        }

        self.push(result as i32)?;
        Ok(())
    }
}