File: ttinterp.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (4971 lines) | stat: -rw-r--r-- 144,468 bytes parent folder | download | duplicates (4)
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
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
(*******************************************************************
 *
 *  TTInterp.pas                                              2.1
 *
 *  TrueType bytecode intepreter.
 *
 *  Copyright 1996 David Turner, Robert Wilhelm and Werner Lemberg
 *
 *  This file is part of the FreeType project, and may only be used
 *  modified and distributed under the terms of the FreeType project
 *  license, LICENSE.TXT. By continuing to use, modify or distribute
 *  this file you indicate that you have read the license and
 *  understand and accept it fully.
 *
 *  Changes between 2.1 and 2.0 :
 *
 *  - Moved into TInterpreter class
 *  - Multithreading should be possible
 *  - Error log
 *  - Dynamic stack size
 *
 *  Changes between 2.0 and 1.2 :
 *
 *  - Lots, lots, of changes : This version is not re-entrant,
 *    but much faster.
 *
 *
 ******************************************************************)

unit TTInterp;

interface

{$R-} // TODO: Fix out-of-bounds accesses.
{$mode Delphi}

uses TTTypes,
     TTObjs;

  function Run_Ins( exec : PExec_Context ; AErrorLog: boolean = false) : TError;
  (* Run the interpreter with the current code range and IP *)

implementation
uses
  TTCalc, SysUtils, Classes, TTMemory;

const
  maxStackSizeAllowed = 16000;

const
  TT_Round_Off            = 5;
  TT_Round_To_Half_Grid   = 0;
  TT_Round_To_Grid        = 1;
  TT_Round_To_Double_Grid = 2;
  TT_Round_Up_To_Grid     = 4;
  TT_Round_Down_To_Grid   = 3;
  TT_Round_Super          = 6;
  TT_ROund_Super_45       = 7;

  TT_Flag_Touched_X    = $02;  (* X touched flag *)
  TT_Flag_Touched_Y    = $04;  (* Y touched flag *)

  TT_Flag_Touched_Both = TT_Flag_Touched_X or TT_FLag_Touched_Y;

type
  TInstruction_Function = procedure( args : PStorage ) of object;

const
  Null_Vector : TT_Vector = (x:0;y:0);

type

  { TInterpreter }

  TInterpreter = class
  private
    pEC       : PExec_Context;
    opcode    : Byte; (* current opcode              *)
    oplength  : Int;  (* length of current opcode    *)
    opargs    : Int;  (* number of arguments in opcode *)

    top       : Int;  (* top of instance stack  *)
    new_top   : Int;  (* new stack top after opc. exec *)
    callTop   : Int;  (* top of call stack  *)

    enableLog: boolean;
    instructionLog: TStringList;

    Instruct_Dispatch : array[0..255] of record
      name: string;
      func: TInstruction_Function;
    end;
    function GetLastInstruction: string;
  public
    constructor Create(AContext: PExec_Context; AEnableLog: boolean);
    destructor Destroy; override;
    function Run: TError;
    property Context: PExec_Context read pEC;
    property LastInstruction: string read GetLastInstruction;

  private
    function NeedStackSize(AValue: integer): TError; overload;
    function NeedStackSize(AValue: integer; var APointerInStack : PStorage): TError; overload;
    function Calc_Length: boolean;
    procedure Compute_Funcs;
    function Compute_Point_Displacement(out x: TT_F26dot6; out y: TT_F26dot6;
      out zone: PGlyph_Zone; out refp: Int): TError;
    procedure Compute_Round(round_mode: Byte);
    procedure Direct_Move(zone: PGlyph_Zone; point: Int; distance: TT_F26dot6);
    procedure Direct_Move_X(zone: PGlyph_Zone; point: Int; distance: TT_F26dot6
      );
    procedure Direct_Move_Y(zone: PGlyph_Zone; point: Int; distance: TT_F26dot6
      );
    function Dual_Project(var P1, P2: TT_Vector): TT_F26dot6;
    function Free_Project(var P1, P2: TT_Vector): TT_F26dot6;
    function GetShort: Short;
    function Get_Current_Ratio: Long;
    function Get_Ppem: Long;
    function Goto_CodeRange(aRange, aIP: Int): boolean;
    procedure Ins_AA({%H-}args: PStorage);
    procedure Ins_ABS(args: PStorage);
    procedure Ins_ADD(args: PStorage);
    procedure Ins_ALIGNPTS(args: PStorage);
    procedure Ins_ALIGNRP({%H-}args: PStorage);
    procedure Ins_AND(args: PStorage);
    procedure Ins_CALL(args: PStorage);
    procedure Ins_CEILING(args: PStorage);
    procedure Ins_CINDEX(args: PStorage);
    procedure Ins_CLEAR({%H-}args: PStorage);
    procedure Ins_DEBUG({%H-}args: PStorage);
    procedure Ins_DELTAC(args: PStorage);
    procedure Ins_DELTAP(args: PStorage);
    procedure Ins_DEPTH(args: PStorage);
    procedure Ins_DIV(args: PStorage);
    procedure Ins_DUP(args: PStorage);
    procedure Ins_EIF({%H-}args: PStorage);
    procedure Ins_ELSE({%H-}args: PStorage);
    procedure Ins_ENDF({%H-}args: PStorage);
    procedure Ins_EQ(args: PStorage);
    procedure Ins_EVEN(args: PStorage);
    procedure Ins_FDEF(args: PStorage);
    procedure Ins_FLIPOFF({%H-}args: PStorage);
    procedure Ins_FLIPON({%H-}args: PStorage);
    procedure Ins_FLIPPT({%H-}args: PStorage);
    procedure Ins_FLIPRGOFF(args: PStorage);
    procedure Ins_FLIPRGON(args: PStorage);
    procedure Ins_FLOOR(args: PStorage);
    procedure Ins_GC(args: PStorage);
    procedure Ins_GETINFO(args: PStorage);
    procedure Ins_GFV(args: PStorage);
    procedure Ins_GPV(args: PStorage);
    procedure Ins_GT(args: PStorage);
    procedure Ins_GTEQ(args: PStorage);
    procedure Ins_IDEF(args: PStorage);
    procedure Ins_IF(args: PStorage);
    procedure Ins_INSTCTRL(args: PStorage);
    procedure Ins_IP({%H-}args: PStorage);
    procedure Ins_ISECT(args: PStorage);
    procedure Ins_IUP({%H-}args: PStorage);
    procedure Ins_JMPR(args: PStorage);
    procedure Ins_JROF(args: PStorage);
    procedure Ins_JROT(args: PStorage);
    procedure Ins_LOOPCALL(args: PStorage);
    procedure Ins_LT(args: PStorage);
    procedure Ins_LTEQ(args: PStorage);
    procedure Ins_MAX(args: PStorage);
    procedure Ins_MD(args: PStorage);
    procedure Ins_MDAP(args: PStorage);
    procedure Ins_MDRP(args: PStorage);
    procedure Ins_MIAP(args: PStorage);
    procedure Ins_MIN(args: PStorage);
    procedure Ins_MINDEX(args: PStorage);
    procedure Ins_MIRP(args: PStorage);
    procedure Ins_MPPEM(args: PStorage);
    procedure Ins_MPS(args: PStorage);
    procedure Ins_MSIRP(args: PStorage);
    procedure Ins_MUL(args: PStorage);
    procedure Ins_NEG(args: PStorage);
    procedure Ins_NEQ(args: PStorage);
    procedure Ins_NOT(args: PStorage);
    procedure Ins_NPUSHB(args: PStorage);
    procedure Ins_NPUSHW(args: PStorage);
    procedure Ins_NROUND(args: PStorage);
    procedure Ins_ODD(args: PStorage);
    procedure Ins_OR(args: PStorage);
    procedure Ins_POP({%H-}args: PStorage);
    procedure Ins_PUSHB(args: PStorage);
    procedure Ins_PUSHW(args: PStorage);
    procedure Ins_RCVT(args: PStorage);
    procedure Ins_RDTG({%H-}args: PStorage);
    procedure Ins_ROFF({%H-}args: PStorage);
    procedure Ins_ROLL(args: PStorage);
    procedure Ins_ROUND(args: PStorage);
    procedure Ins_RS(args: PStorage);
    procedure Ins_RTDG({%H-}args: PStorage);
    procedure Ins_RTG({%H-}args: PStorage);
    procedure Ins_RTHG({%H-}args: PStorage);
    procedure Ins_RUTG({%H-}args: PStorage);
    procedure Ins_S45ROUND(args: PStorage);
    procedure Ins_SANGW({%H-}args: PStorage);
    procedure Ins_SCANCTRL(args: PStorage);
    procedure Ins_SCANTYPE(args: PStorage);
    procedure Ins_SCFS(args: PStorage);
    procedure Ins_SCVTCI(args: PStorage);
    procedure Ins_SDB(args: PStorage);
    procedure Ins_SDPVTL(args: PStorage);
    procedure Ins_SDS(args: PStorage);
    procedure Ins_SFVFS(args: PStorage);
    procedure Ins_SFVTCA({%H-}args: PStorage);
    procedure Ins_SFVTL(args: PStorage);
    procedure Ins_SFVTPV({%H-}args: PStorage);
    procedure Ins_SHC(args: PStorage);
    procedure Ins_SHP({%H-}args: PStorage);
    procedure Ins_SHPIX(args: PStorage);
    procedure Ins_SHZ(args: PStorage);
    procedure Ins_SLOOP(args: PStorage);
    procedure Ins_SMD(args: PStorage);
    procedure Ins_SPVFS(args: PStorage);
    procedure Ins_SPVTCA({%H-}args: PStorage);
    procedure Ins_SPVTL(args: PStorage);
    procedure Ins_SROUND(args: PStorage);
    procedure Ins_SRP0(args: PStorage);
    procedure Ins_SRP1(args: PStorage);
    procedure Ins_SRP2(args: PStorage);
    procedure Ins_SSW(args: PStorage);
    procedure Ins_SSWCI(args: PStorage);
    procedure Ins_SUB(args: PStorage);
    procedure Ins_SVTCA({%H-}args: PStorage);
    procedure Ins_SWAP(args: PStorage);
    function Ins_SxVTL(aIdx1: Int; aIdx2: Int; aOpc: Int; var Vec: TT_UnitVector
      ): boolean;
    procedure Ins_SZP0(args: PStorage);
    procedure Ins_SZP1(args: PStorage);
    procedure Ins_SZP2(args: PStorage);
    procedure Ins_SZPS(args: PStorage);
    procedure Ins_UNKNOWN({%H-}args: PStorage);
    procedure Ins_UTP(args: PStorage);
    procedure Ins_WCVTF(args: PStorage);
    procedure Ins_WCVTP(args: PStorage);
    procedure Ins_WS(args: PStorage);
    procedure Move_CVT(index: Int; value: TT_F26Dot6);
    procedure Move_CVT_Stretched(index: Int; value: TT_F26dot6);
    procedure Move_Zp2_Point(point: Int; dx: TT_F26dot6; dy: TT_F26dot6);
    function Norm(X, Y: TT_F26dot6): TT_F26dot6;
    function Normalize(U, V: TT_F26dot6; var R: TT_UnitVector): boolean;
    function Project(var P1, P2: TT_Vector): TT_F26dot6;
    function Project_x(var P1, P2: TT_Vector): TT_F26dot6;
    function Project_y(var P1, P2: TT_Vector): TT_F26dot6;
    function Read_CVT(index: Int): TT_F26Dot6;
    function Read_CVT_Stretched(index: Int): TT_F26Dot6;
    function Round_Down_To_Grid(distance: TT_F26dot6; compensation: TT_F26dot6
      ): TT_F26dot6;
    function Round_None(distance: TT_F26dot6; compensation: TT_F26dot6
      ): TT_F26dot6;
    function Round_Super(distance: TT_F26dot6; compensation: TT_F26dot6
      ): TT_F26dot6;
    function Round_Super_45(distance: TT_F26dot6; compensation: TT_F26dot6
      ): TT_F26dot6;
    function Round_To_Double_Grid(distance: TT_F26dot6; compensation: TT_F26dot6
      ): TT_F26dot6;
    function Round_To_Grid(distance: TT_F26dot6; compensation: TT_F26dot6
      ): TT_F26dot6;
    function Round_To_Half_Grid(distance: TT_F26dot6; compensation: TT_F26dot6
      ): TT_F26dot6;
    function Round_Up_To_Grid(distance: TT_F26dot6; compensation: TT_F26dot6
      ): TT_F26dot6;
    function Scale_Pixels(value: long): TT_F26Dot6;
    procedure SetSuperRound(GridPeriod: TT_F26dot6; selector: Long);
    function SkipCode: boolean;
    procedure Write_CVT(index: Int; value: TT_F26Dot6);
    procedure Write_CVT_Stretched(index: Int; value: TT_F26Dot6);
  end;

const

  (*********************************************************************)
  (*                                                                   *)
  (*  Before an opcode is executed, the interpreter verifies that      *)
  (*  there are enough arguments on the stack, with the help of        *)
  (*  the Pop_Push_Count table.                                        *)
  (*                                                                   *)
  (*  Note that for opcodes with a varying numbre of parameters,       *)
  (*  either 0 or 1 arg is verified before execution, depending        *)
  (*  on the nature of the instruction :                               *)
  (*                                                                   *)
  (*   - if the number of arguments is given by the bytecode           *)
  (*     stream or the loop variable, 0 is chosen.                     *)
  (*                                                                   *)
  (*   - if the first argument is a count n that is followed           *)
  (*     by arguments a1..an, then 1 is chosen.                        *)
  (*                                                                   *)
  (*********************************************************************)

  Pop_Push_Count : array[0..511] of byte
                 = (
                     (* SVTCA  y *)  0, 0,
                     (* SVTCA  x *)  0, 0,
                     (* SPvTCA y *)  0, 0,
                     (* SPvTCA x *)  0, 0,
                     (* SFvTCA y *)  0, 0,
                     (* SFvTCA x *)  0, 0,
                     (* SPvTL // *)  2, 0,
                     (* SPvTL +  *)  2, 0,
                     (* SFvTL // *)  2, 0,
                     (* SFvTL +  *)  2, 0,
                     (* SPvFS    *)  2, 0,
                     (* SFvFS    *)  2, 0,
                     (* GPV      *)  0, 2,
                     (* GFV      *)  0, 2,
                     (* SFvTPv   *)  0, 0,
                     (* ISECT    *)  5, 0,

                     (* SRP0     *)  1, 0,
                     (* SRP1     *)  1, 0,
                     (* SRP2     *)  1, 0,
                     (* SZP0     *)  1, 0,
                     (* SZP1     *)  1, 0,
                     (* SZP2     *)  1, 0,
                     (* SZPS     *)  1, 0,
                     (* SLOOP    *)  1, 0,
                     (* RTG      *)  0, 0,
                     (* RTHG     *)  0, 0,
                     (* SMD      *)  1, 0,
                     (* ELSE     *)  0, 0,
                     (* JMPR     *)  1, 0,
                     (* SCvTCi   *)  1, 0,
                     (* SSwCi    *)  1, 0,
                     (* SSW      *)  1, 0,

                     (* DUP      *)  1, 2,
                     (* POP      *)  1, 0,
                     (* CLEAR    *)  0, 0,
                     (* SWAP     *)  2, 2,
                     (* DEPTH    *)  0, 1,
                     (* CINDEX   *)  1, 1,
                     (* MINDEX   *)  1, 0, (* first arg *)
                     (* AlignPTS *)  2, 0,
                     (* INS_$28  *)  0, 0,
                     (* UTP      *)  1, 0,
                     (* LOOPCALL *)  2, 0,
                     (* CALL     *)  1, 0,
                     (* FDEF     *)  1, 0,
                     (* ENDF     *)  0, 0,
                     (* MDAP[0]  *)  1, 0,
                     (* MDAP[1]  *)  1, 0,

                     (* IUP[0]   *)  0, 0,
                     (* IUP[1]   *)  0, 0,
                     (* SHP[0]   *)  0, 0,  (* no args *)
                     (* SHP[1]   *)  0, 0,  (* no args *)
                     (* SHC[0]   *)  1, 0,
                     (* SHC[1]   *)  1, 0,
                     (* SHZ[0]   *)  1, 0,
                     (* SHZ[1]   *)  1, 0,
                     (* SHPIX    *)  1, 0,  (* first arg *)
                     (* IP       *)  0, 0,  (* no args   *)
                     (* MSIRP[0] *)  2, 0,
                     (* MSIRP[1] *)  2, 0,
                     (* AlignRP  *)  0, 0,  (* no args *)
                     (* RTDG     *)  0, 0,
                     (* MIAP[0]  *)  2, 0,
                     (* MIAP[1]  *)  2, 0,

                     (* NPushB   *)  0, 0,
                     (* NPushW   *)  0, 0,
                     (* WS       *)  2, 0,
                     (* RS       *)  1, 1,
                     (* WCvtP    *)  2, 0,
                     (* RCvt     *)  1, 1,
                     (* GC[0]    *)  1, 1,
                     (* GC[1]    *)  1, 1,
                     (* SCFS     *)  2, 0,
                     (* MD[0]    *)  2, 1,
                     (* MD[1]    *)  2, 1,
                     (* MPPEM    *)  0, 1,
                     (* MPS      *)  0, 1,
                     (* FlipON   *)  0, 0,
                     (* FlipOFF  *)  0, 0,
                     (* DEBUG    *)  1, 0,

                     (* LT       *)  2, 1,
                     (* LTEQ     *)  2, 1,
                     (* GT       *)  2, 1,
                     (* GTEQ     *)  2, 1,
                     (* EQ       *)  2, 1,
                     (* NEQ      *)  2, 1,
                     (* ODD      *)  1, 1,
                     (* EVEN     *)  1, 1,
                     (* IF       *)  1, 0,
                     (* EIF      *)  0, 0,
                     (* AND      *)  2, 1,
                     (* OR       *)  2, 1,
                     (* NOT      *)  1, 1,
                     (* DeltaP1  *)  1, 0, (* first arg *)
                     (* SDB      *)  1, 0,
                     (* SDS      *)  1, 0,

                     (* ADD      *)  2, 1,
                     (* SUB      *)  2, 1,
                     (* DIV      *)  2, 1,
                     (* MUL      *)  2, 1,
                     (* ABS      *)  1, 1,
                     (* NEG      *)  1, 1,
                     (* FLOOR    *)  1, 1,
                     (* CEILING  *)  1, 1,
                     (* ROUND[0] *)  1, 1,
                     (* ROUND[1] *)  1, 1,
                     (* ROUND[2] *)  1, 1,
                     (* ROUND[3] *)  1, 1,
                     (* NROUND[0]*)  1, 1,
                     (* NROUND[1]*)  1, 1,
                     (* NROUND[2]*)  1, 1,
                     (* NROUND[3]*)  1, 1,

                     (* WCvtF    *)  2, 0,
                     (* DeltaP2  *)  1, 0,  (* first arg *)
                     (* DeltaP3  *)  1, 0,  (* first arg *)
                     (* DeltaCn[0]*) 1, 0,  (* first arg *)
                     (* DeltaCn[1]*) 1, 0,  (* first arg *)
                     (* DeltaCn[2]*) 1, 0,  (* first arg *)
                     (* SROUND   *)  1, 0,
                     (* S45Round *)  1, 0,
                     (* JROT     *)  2, 0,
                     (* JROF     *)  2, 0,
                     (* ROFF     *)  0, 0,
                     (* INS_$7B  *)  0, 0,
                     (* RUTG     *)  0, 0,
                     (* RDTG     *)  0, 0,
                     (* SANGW    *)  1, 0,
                     (* AA       *)  1, 0,

                     (* FlipPT   *)  0, 0,  (* no args *)
                     (* FlipRgON *)  2, 0,
                     (* FlipRgOFF*)  2, 0,
                     (* INS_$83  *)  0, 0,
                     (* INS_$84  *)  0, 0,
                     (* ScanCTRL *)  1, 0,
                     (* SDVPTL[0]*)  2, 0,
                     (* SDVPTL[1]*)  2, 0,
                     (* GetINFO  *)  1, 1,
                     (* IDEF     *)  1, 0,
                     (* ROLL     *)  3, 3,  (* pops 3 args/push 3 args *)
                     (* MAX      *)  2, 1,
                     (* MIN      *)  2, 1,
                     (* ScanTYPE *)  1, 0,
                     (* InstCTRL *)  2, 0,
                     (* INS_$8F  *)  0, 0,

                     (* INS_$90 *)   0, 0,
                     (* INS_$91 *)   0, 0,
                     (* INS_$92 *)   0, 0,
                     (* INS_$93 *)   0, 0,
                     (* INS_$94 *)   0, 0,
                     (* INS_$95 *)   0, 0,
                     (* INS_$96 *)   0, 0,
                     (* INS_$97 *)   0, 0,
                     (* INS_$98 *)   0, 0,
                     (* INS_$99 *)   0, 0,
                     (* INS_$9A *)   0, 0,
                     (* INS_$9B *)   0, 0,
                     (* INS_$9C *)   0, 0,
                     (* INS_$9D *)   0, 0,
                     (* INS_$9E *)   0, 0,
                     (* INS_$9F *)   0, 0,

                     (* INS_$A0 *)   0, 0,
                     (* INS_$A1 *)   0, 0,
                     (* INS_$A2 *)   0, 0,
                     (* INS_$A3 *)   0, 0,
                     (* INS_$A4 *)   0, 0,
                     (* INS_$A5 *)   0, 0,
                     (* INS_$A6 *)   0, 0,
                     (* INS_$A7 *)   0, 0,
                     (* INS_$A8 *)   0, 0,
                     (* INS_$A9 *)   0, 0,
                     (* INS_$AA *)   0, 0,
                     (* INS_$AB *)   0, 0,
                     (* INS_$AC *)   0, 0,
                     (* INS_$AD *)   0, 0,
                     (* INS_$AE *)   0, 0,
                     (* INS_$AF *)   0, 0,

                     (* PushB[0] *)  0, 1,
                     (* PushB[1] *)  0, 2,
                     (* PushB[2] *)  0, 3,
                     (* PushB[3] *)  0, 4,
                     (* PushB[4] *)  0, 5,
                     (* PushB[5] *)  0, 6,
                     (* PushB[6] *)  0, 7,
                     (* PushB[7] *)  0, 8,
                     (* PushW[0] *)  0, 1,
                     (* PushW[1] *)  0, 2,
                     (* PushW[2] *)  0, 3,
                     (* PushW[3] *)  0, 4,
                     (* PushW[4] *)  0, 5,
                     (* PushW[5] *)  0, 6,
                     (* PushW[6] *)  0, 7,
                     (* PushW[7] *)  0, 8,

                     (* MDRP[00] *)  1, 0,
                     (* MDRP[01] *)  1, 0,
                     (* MDRP[02] *)  1, 0,
                     (* MDRP[03] *)  1, 0,
                     (* MDRP[04] *)  1, 0,
                     (* MDRP[05] *)  1, 0,
                     (* MDRP[06] *)  1, 0,
                     (* MDRP[07] *)  1, 0,
                     (* MDRP[08] *)  1, 0,
                     (* MDRP[09] *)  1, 0,
                     (* MDRP[10] *)  1, 0,
                     (* MDRP[11] *)  1, 0,
                     (* MDRP[12] *)  1, 0,
                     (* MDRP[13] *)  1, 0,
                     (* MDRP[14] *)  1, 0,
                     (* MDRP[15] *)  1, 0,
                     (* MDRP[16] *)  1, 0,
                     (* MDRP[17] *)  1, 0,

                     (* MDRP[18] *)  1, 0,
                     (* MDRP[19] *)  1, 0,
                     (* MDRP[20] *)  1, 0,
                     (* MDRP[21] *)  1, 0,
                     (* MDRP[22] *)  1, 0,
                     (* MDRP[23] *)  1, 0,
                     (* MDRP[24] *)  1, 0,
                     (* MDRP[25] *)  1, 0,
                     (* MDRP[26] *)  1, 0,
                     (* MDRP[27] *)  1, 0,
                     (* MDRP[28] *)  1, 0,
                     (* MDRP[29] *)  1, 0,
                     (* MDRP[30] *)  1, 0,
                     (* MDRP[31] *)  1, 0,

                     (* MIRP[00] *)  2, 0,
                     (* MIRP[01] *)  2, 0,
                     (* MIRP[02] *)  2, 0,
                     (* MIRP[03] *)  2, 0,
                     (* MIRP[04] *)  2, 0,
                     (* MIRP[05] *)  2, 0,
                     (* MIRP[06] *)  2, 0,
                     (* MIRP[07] *)  2, 0,
                     (* MIRP[08] *)  2, 0,
                     (* MIRP[09] *)  2, 0,
                     (* MIRP[10] *)  2, 0,
                     (* MIRP[11] *)  2, 0,
                     (* MIRP[12] *)  2, 0,
                     (* MIRP[13] *)  2, 0,
                     (* MIRP[14] *)  2, 0,
                     (* MIRP[15] *)  2, 0,
                     (* MIRP[16] *)  2, 0,
                     (* MIRP[17] *)  2, 0,

                     (* MIRP[18] *)  2, 0,
                     (* MIRP[19] *)  2, 0,
                     (* MIRP[20] *)  2, 0,
                     (* MIRP[21] *)  2, 0,
                     (* MIRP[22] *)  2, 0,
                     (* MIRP[23] *)  2, 0,
                     (* MIRP[24] *)  2, 0,
                     (* MIRP[25] *)  2, 0,
                     (* MIRP[26] *)  2, 0,
                     (* MIRP[27] *)  2, 0,
                     (* MIRP[28] *)  2, 0,
                     (* MIRP[29] *)  2, 0,
                     (* MIRP[30] *)  2, 0,
                     (* MIRP[31] *)  2, 0
                   );

(*******************************************************************
 *
 *  Function    :  Norm
 *
 *  Description :  returns the norm (length) of a vector
 *
 *  Input  :  X, Y   vector
 *
 *  Output :  returns length in F26dot6
 *
 *****************************************************************)

 function TInterpreter.Norm( X, Y : TT_F26dot6 ): TT_F26dot6;
 begin
   result := sqrt64(int64(X)*int64(X)+int64(Y)*int64(Y));
 end;

(*******************************************************************
 *
 *  Function    :  Scale_Pixels
 *
 *  Description :  Converts from FUnits to Fractional pixels
 *                 coordinates.
 *
 *****************************************************************)

  function TInterpreter.Scale_Pixels( value : long ) : TT_F26Dot6;
  {$IFDEF INLINE} inline; {$ENDIF}
  begin
    Scale_Pixels := MulDiv_Round( value,
                                  pEC^.metrics.scale1,
                                  pEC^.metrics.scale2 );
  end;

  function TInterpreter.Get_Current_Ratio : Long;
  var
    x, y : Long;
  begin
    if pEC^.metrics.ratio <> 0 then
      Get_Current_Ratio := pEC^.metrics.ratio
    else
    begin
      if pEC^.GS.projVector.y = 0 then
        pEC^.metrics.ratio := pEC^.metrics.x_ratio

      else if pEC^.GS.projVector.x = 0 then
        pEC^.metrics.ratio := pEC^.metrics.y_ratio

      else
        begin
          x := MulDiv_Round( pEC^.GS.projVector.x,
                             pEC^.metrics.x_ratio,
                             $4000 );

          y := MulDiv_Round( pEC^.GS.projVector.y,
                             pEC^.metrics.y_ratio,
                             $4000 );

          pEC^.metrics.ratio := Norm( x, y );
        end;

      Get_Current_Ratio := pEC^.metrics.ratio;
    end
  end;

  function TInterpreter.Get_Ppem : Long;
  {$IFDEF INLINE} inline; {$ENDIF}
  begin
    Get_Ppem := MulDiv_Round( pEC^.metrics.ppem, Get_Current_Ratio, $10000 );
  end;


  function TInterpreter.Read_CVT( index : Int ) : TT_F26Dot6;
  begin
    Read_CVT := pEC^.cvt^[index];
  end;

  function TInterpreter.Read_CVT_Stretched( index : Int ) : TT_F26Dot6;
  begin
    Read_CVT_Stretched := MulDiv_Round( pEC^.cvt^[index],
                                        Get_Current_Ratio,
                                        $10000 );
  end;


  procedure TInterpreter.Write_CVT( index : Int; value : TT_F26Dot6 );
  begin
    pEC^.cvt^[index] := value;
  end;

  procedure TInterpreter.Write_CVT_Stretched( index : Int; value : TT_F26Dot6 );
  begin
    pEC^.cvt^[index] := MulDiv_Round( value,
                                     $10000,
                                     Get_Current_Ratio );
  end;


  procedure TInterpreter.Move_CVT( index : Int; value : TT_F26Dot6 );
  begin
    inc( pEC^.cvt^[index], value );
  end;

  procedure TInterpreter.Move_CVT_Stretched( index : Int; value : TT_F26dot6 );
  begin
    inc( pEC^.cvt^[index], MulDiv_Round( value,
                                        $10000,
                                        Get_Current_Ratio ));
  end;

(*******************************************************************
 *
 *  Function    :  Calc_Length
 *
 *  Description :  Computes the length in bytes of current opcode
 *
 *****************************************************************)

 function TInterpreter.Calc_Length : boolean;
 begin
   Calc_Length := false;

   opcode := pEC^.Code^[pEC^.IP];

   case opcode of

     $40 : if pEC^.IP+1 >= pEC^.codeSize
             then exit
           else
             oplength := pEC^.code^[pEC^.IP+1]   + 2;

     $41 : if pEC^.IP+1 >= pEC^.codeSize
             then exit
           else
             oplength := pEC^.code^[pEC^.IP+1]*2 + 2;

     $B0..$B7 : oplength :=  opcode-$B0    + 2;
     $B8..$BF : oplength := (opcode-$B8)*2 + 3;
   else
     oplength := 1;
   end;

   Calc_Length := pEC^.IP+oplength <= pEC^.codeSize;
 end;

(*******************************************************************
 *
 *  Function    :  Get_Short
 *
 *  Description :  Return a short integer taken from the instruction
 *                 stream at address IP.
 *
 *  Input  :  None
 *
 *  Output :  Short read at Code^[IP..IP+1]
 *
 *  Notes  :  This one could become a Macro in the C version
 *
 *****************************************************************)

 function TInterpreter.GetShort : Short;
 var
   L1,L0        : Byte;
 begin
   L1     := pEC^.code^[pEC^.IP]; inc(pEC^.IP);
   L0     := pEC^.code^[pEC^.IP]; inc(pEC^.IP);
   if L1 >= 128 then
     result := Short(-32768) + (Short(L1 and 127) shl 8) + L0
   else
     result := (L1 shl 8) + L0;
 end;


 function TInterpreter.Goto_CodeRange( aRange,
                          aIP     : Int ): boolean;
 begin

   Goto_CodeRange := False;

   with pEC^ do
   begin
     if (aRange<1) or (aRange>3) then
       begin
         pEC^.error := TT_Err_Bad_Argument;
         exit;
       end;

     with CodeRangeTable[ARange] do
       begin

         if Base = nil then  (* invalid coderange *)
         begin
           error := TT_Err_Invalid_Coderange;
           exit;
         end;

         (* NOTE : Because the last instruction of a program may be a CALL *)
         (*        which will return to the first byte *after* the code    *)
         (*        range, we test for AIP <= Size, instead of AIP < Size   *)

         if AIP > Size then
           begin
             error          := TT_Err_Code_Overflow;
             Goto_CodeRange := False;
             exit;
           end;

         Code     := PByte(Base);
         CodeSize := Size;
         IP       := AIP;
       end;

     curRange := ARange;
   end;

   Goto_CodeRange := True;
 end;


(*******************************************************************
 *
 *  Function    :  Direct_Move
 *
 *  Description :  Moves a point by a given distance along the
 *                 freedom vector.
 *
 *  Input  : Vx, Vy      point coordinates to move
 *           touch       touch flag to modify
 *           distance
 *
 *  Output :  None
 *
 *****************************************************************)

 procedure TInterpreter.Direct_Move( zone     : PGlyph_Zone;
                        point    : Int;
                        distance : TT_F26dot6 );
 var
   v : TT_F26dot6;
 begin
   v := pEC^.GS.freeVector.x;
   if v <> 0 then
   begin
     inc( zone^.cur^[point].x, MulDiv_Round( distance,
                                             Long(v)*$10000,
                                             pEC^.F_dot_P ));

     zone^.flags^[point] := zone^.flags^[point] or TT_Flag_Touched_X;
   end;

   v := pEC^.GS.freeVector.y;
   if v <> 0 then
   begin
     inc( zone^.cur^[point].y, MulDiv_Round( distance,
                                             Long(v)*$10000,
                                             pEC^.F_dot_P ));

     zone^.flags^[point] := zone^.flags^[point] or TT_Flag_Touched_Y;
   end;
 end;

 (* The following versions are used whenever both vectors are both *)
 (* along one of the coordinate unit vectors, i.e. in 90% cases    *)

 procedure TInterpreter.Direct_Move_X( zone     : PGlyph_Zone;
                          point    : Int;
                          distance : TT_F26dot6 );
 begin
   inc( zone^.cur^[point].x, distance );
   zone^.flags^[point] := zone^.flags^[point] or TT_Flag_Touched_X;
 end;

 procedure TInterpreter.Direct_Move_Y( zone     : PGlyph_Zone;
                          point    : Int;
                          distance : TT_F26dot6 );
 begin
   inc( zone^.cur^[point].y, distance );
   zone^.flags^[point] := zone^.flags^[point] or TT_Flag_Touched_Y;
 end;

(*******************************************************************
 *
 *  Function    :  Round_None
 *
 *  Description :  Do not round, but add engine compensation
 *
 *  Input  :  distance      : distance to round
 *            compensation  : engine compensation
 *
 *  Output :  rounded distance
 *
 *  NOTE : The spec says very few about the relationship between
 *         rounding and engine compensation. However, it seems
 *         from the description of super round that we should
 *         should add the compensation before rounding
 *
 *****************************************************************)

 function TInterpreter.Round_None( distance     : TT_F26dot6;
                      compensation : TT_F26dot6 ) : TT_F26dot6;
 var
   val : TT_F26dot6;
 begin
   if distance >= 0 then
     begin
       val := distance + compensation;
       if val < 0 then val := 0;
     end
   else
     begin
       val := distance - compensation;
       if val > 0 then val := 0;
     end;

   Round_None := val;
 end;

(*******************************************************************
 *
 *  Function    :  Round_To_Grid
 *
 *  Description :  round value to grid after adding engine
 *                 compensation
 *
 *  Input  :  distance      : distance to round
 *            compensation  : engine compensation
 *
 *  Output :  rounded distance
 *
 *****************************************************************)

 function TInterpreter.Round_To_Grid( distance     : TT_F26dot6;
                         compensation : TT_F26dot6 ) : TT_F26dot6;
 var
   val : TT_F26dot6;
 begin
   if distance >= 0 then
     begin
       val := (distance + 32 + compensation) and -64;
       if val < 0 then val := 0;
     end
   else
     begin
       val := - ((compensation - distance + 32) and -64);
       if val > 0 then val := 0;
     end;

   Round_To_Grid := val;
 end;

(*******************************************************************
 *
 *  Function    :  Round_To_Half_Grid
 *
 *  Description :  round value to half grid after adding engine
 *                 compensation
 *
 *  Input  :  distance      : distance to round
 *            compensation  : engine compensation
 *
 *  Output :  rounded distance
 *
 *****************************************************************)

 function TInterpreter.Round_To_Half_Grid( distance     : TT_F26dot6;
                         compensation : TT_F26dot6 ) : TT_F26dot6;
 var
   val : TT_F26dot6;
 begin
   if distance >= 0 then
     begin
       val := (distance + compensation) and -64 + 32;
       if val < 0 then val := 0;
     end
   else
     begin
       val := - ((-distance + compensation) and -64 + 32);
       if val > 0 then val := 0;
     end;

   Round_To_Half_Grid := val;
 end;


(*******************************************************************
 *
 *  Function    :  Round_Down_To_Grid
 *
 *  Description :  round value down to grid after adding engine
 *                 compensation
 *
 *  Input  :  distance      : distance to round
 *            compensation  : engine compensation
 *
 *  Output :  rounded distance
 *
 *****************************************************************)

 function TInterpreter.Round_Down_To_Grid( distance     : TT_F26dot6;
                              compensation : TT_F26dot6 ) : TT_F26dot6;
 var
   val : TT_F26dot6;
 begin
   if distance >= 0 then
     begin
       val := (distance + compensation) and -64;
       if val < 0 then val := 0;
     end
   else
     begin
       val := - ((-distance + compensation) and -64);
       if val > 0 then val := 0;
     end;

   Round_Down_To_Grid := val;
 end;

(*******************************************************************
 *
 *  Function    :  Round_Up_To_Grid
 *
 *  Description :  round value up to grid after adding engine
 *                 compensation
 *
 *  Input  :  distance      : distance to round
 *            compensation  : engine compensation
 *
 *  Output :  rounded distance
 *
 *****************************************************************)

 function TInterpreter.Round_Up_To_Grid( distance     : TT_F26dot6;
                            compensation : TT_F26dot6 ) : TT_F26dot6;
 var
   val : TT_F26dot6;
 begin
   if distance >= 0 then
     begin
       val := (distance + 63 + compensation) and -64;
       if val < 0 then val := 0;
     end
   else
     begin
       val := - ((-distance + 63 + compensation) and -64);
       if val > 0 then val := 0;
     end;

   Round_Up_To_Grid := val;
 end;

(*******************************************************************
 *
 *  Function    :  Round_To_Double_Grid
 *
 *  Description :  round value to double grid after adding engine
 *                 compensation
 *
 *  Input  :  distance      : distance to round
 *            compensation  : engine compensation
 *
 *  Output :  rounded distance
 *
 *****************************************************************)

 function TInterpreter.Round_To_Double_Grid( distance     : TT_F26dot6;
                                compensation : TT_F26dot6 ) : TT_F26dot6;
 var
   val : TT_F26dot6;
 begin
   if distance >= 0 then
     begin
       val := (distance + 16 + compensation) and -32;
       if val < 0 then val := 0;
     end
   else
     begin
       val := - ((-distance + 16 + compensation) and -32);
       if val > 0 then val := 0;
     end;

   Round_To_Double_Grid := val;
 end;

(*******************************************************************
 *
 *  Function    :  Round_Super
 *
 *  Description :  super round value to grid after adding engine
 *                 compensation
 *
 *  Input  :  distance      : distance to round
 *            compensation  : engine compensation
 *
 *  Output :  rounded distance
 *
 *  NOTE : The spec says very few about the relationship between
 *         rounding and engine compensation. However, it seems
 *         from the description of super round that we should
 *         should add the compensation before rounding
 *
 *****************************************************************)

 function TInterpreter.Round_Super( distance     : TT_F26dot6;
                       compensation : TT_F26dot6 ) : TT_F26dot6;
 var
   val : TT_F26dot6;
 begin
   with pEC^ do

     if distance >= 0 then
       begin
         val := (distance - phase + threshold + compensation) and -period;
         if val < 0 then val := 0;
         val := val + phase;
       end
     else
       begin
         val := -((-distance - phase + threshold + compensation) and -period);
         if val > 0 then val := 0;
         val := val - phase;
       end;

   Round_Super := val;
 end;

(*******************************************************************
 *
 *  Function    :  Round_Super_45
 *
 *  Description :  super round value to grid after adding engine
 *                 compensation
 *
 *  Input  :  distance      : distance to round
 *            compensation  : engine compensation
 *
 *  Output :  rounded distance
 *
 *  NOTE : There is a separate function for Round_Super_45 as we
 *         may need a greater precision.
 *
 *****************************************************************)

 function TInterpreter.Round_Super_45( distance     : TT_F26dot6;
                          compensation : TT_F26dot6 ) : TT_F26dot6;
 var
   val : TT_F26dot6;
 begin
   with pEC^ do

     if distance >= 0 then
       begin
         val := ((distance - phase + threshold + compensation) div period)
                * period;
         if val < 0 then val := 0;
         val := val + phase;
       end
     else
       begin
         val := -((-distance - phase + threshold + compensation) div period
                   * period );
         if val > 0 then val := 0;
         val := val - phase;
       end;

   Round_Super_45 := val;
 end;

 procedure TInterpreter.Compute_Round( round_mode : Byte );
 begin
   case Round_Mode of

     TT_Round_Off            : pEC^.func_round := Round_None;
     TT_Round_To_Grid        : pEC^.func_round := Round_To_Grid;
     TT_Round_Up_To_Grid     : pEC^.func_round := Round_Up_To_Grid;
     TT_Round_Down_To_Grid   : pEC^.func_round := Round_Down_To_Grid;
     TT_Round_To_Half_Grid   : pEC^.func_round := Round_To_Half_Grid;
     TT_Round_To_Double_Grid : pEC^.func_round := Round_To_Double_Grid;
     TT_Round_Super          : pEC^.func_round := Round_Super;
     TT_Round_Super_45       : pEC^.func_round := Round_Super_45;
   end;
 end;


(*******************************************************************
 *
 *  Function    :  SetSuperRound
 *
 *  Description :  Set Super Round parameters
 *
 *  Input  :  GridPeriod   Grid period
 *            OpCode       SROUND opcode
 *
 *  Output :  None
 *
 *  Notes  :
 *
 *****************************************************************)

 procedure TInterpreter.SetSuperRound( GridPeriod : TT_F26dot6; selector : Long );

 begin
   with pEC^ do
   begin

     Case selector and $C0 of

      $00 : period := GridPeriod div 2;
      $40 : period := GridPeriod;
      $80 : period := GridPeriod * 2;

      (* This opcode is reserved, but ... *)

      $C0 : period := GridPeriod;
     end;

     Case selector and $30 of

      $00 : phase := 0;
      $10 : phase := period div 4;
      $20 : phase := period div 2;
      $30 : phase := gridPeriod*3 div 4;
     end;

     if selector and $F = 0 then

        Threshold := Period-1
      else
        Threshold := (Integer( selector and $F )-4)*period div 8;

     period    := period div 256;
     phase     := phase div 256;
     threshold := threshold div 256;

   end
 end;

(*******************************************************************
 *
 *  Function    :  Project
 *
 *  Description :  Computes the projection of (Vx,Vy) along the
 *                 current projection vector
 *
 *  Input  :  Vx, Vy    input vector
 *
 *  Output :  return distance in F26dot6
 *
 *****************************************************************)

 function TInterpreter.Project( var P1, P2 : TT_Vector ) : TT_F26dot6;
 var
   T1, T2 : Int64;
 begin
   with pEC^.GS.projVector do
   begin
     MulTo64( P1.x - P2.x, x, T1 );
     MulTo64( P1.y - P2.y, y, T2 );
   end;

   Project := Div64by32( T1+T2, $4000 );
 end;


 function TInterpreter.Dual_Project( var P1, P2 : TT_Vector ) : TT_F26dot6;
 var
   T1, T2 : Int64;
 begin
   with pEC^.GS.dualVector do
   begin
     MulTo64( P1.x - P2.x, x, T1 );
     MulTo64( P1.y - P2.y, y, T2 );
   end;

   Dual_Project := Div64by32( T1+T2, $4000 );
 end;


 function TInterpreter.Free_Project( var P1, P2 : TT_Vector ) : TT_F26dot6;
 var
   T1, T2 : Int64;
 begin
   with pEC^.GS.freeVector do
   begin
     MulTo64( P1.x - P2.x, x, T1 );
     MulTo64( P1.y - P2.y, y, T2 );
   end;

   Free_Project := Div64by32( T1+T2, $4000 );
 end;


 function TInterpreter.Project_x( var P1, P2 : TT_Vector ) : TT_F26dot6;
 begin
   Project_x := P1.x - P2.x;
 end;

 function TInterpreter.Project_y( var P1, P2 : TT_Vector ) : TT_F26dot6;
 begin
   Project_y := P1.y - P2.y;
 end;

(*******************************************************************
 *
 *  Function    :  Compute_Funcs
 *
 *  Description :  Computes the projections and movement function
 *                 pointers according to the current graphics state
 *
 *  Input  :  None
 *
 *****************************************************************)

 procedure TInterpreter.Compute_Funcs;
 begin
   with pEC^, GS do
   begin

     if (freeVector.x = $4000) then
       begin
         func_freeProj := Project_x;
         F_dot_P       := Long(projVector.x) * $10000;
       end
     else
     if (freeVector.y = $4000) then
       begin
         func_freeProj := Project_y;
         F_dot_P       := Long(projVector.y) * $10000;
       end
     else
       begin
         func_move     := Direct_Move;
         func_freeProj := Free_Project;
         F_dot_P       := Long(projVector.x) * freeVector.x * 4 +
                          Long(projVector.y) * freeVector.y * 4;
       end;

     if (projVector.x = $4000) then func_Project := Project_x
     else
     if (projVector.y = $4000) then func_Project := Project_y
     else
                                    func_Project := Project;

     if (dualVector.x = $4000) then func_dualproj := Project_x
     else
     if (dualVector.y = $4000) then func_dualproj := Project_y
     else
                                    func_dualproj := Dual_Project;

     func_move := Direct_Move;

     if F_dot_P = $40000000 then

       if freeVector.x = $4000 then func_move := Direct_Move_x
       else
       if freeVector.y = $4000 then func_move := Direct_Move_y;

     (* at small sizes, F_dot_P can become too small, resulting *)
     (* in overflows and 'spikes' in a number of glyfs like 'w' *)

     if abs( F_dot_P ) < $4000000 then F_dot_P := $40000000;

     (* set aspect ratio to 0 to force recomputation by Get_Current_Ratio *)
     metrics.ratio := 0;
   end;
 end;


(**************************************************)
(*                                                *)
(* Normalize :  Normer un vecteur ( U, V )        *)
(*              rsultat dans     ( X, Y )        *)
(*              False si vecteur paramtre nul    *)
(*                                                *)
(**************************************************)

function TInterpreter.Normalize( U, V : TT_F26dot6; var R : TT_UnitVector ): boolean;
var
  W       : TT_F26dot6;
  S1, S2  : Boolean;
begin

  if (Abs(U) < $10000) and (Abs(V) < $10000) then
    begin
      U := U*$100;
      V := V*$100;

      W := Norm( U, V );
      if W = 0 then
        begin
          (* XXX : Undocumented. Apparently, it is possible to try *)
          (*       to normalize the vector (0,0). Return success   *)
          (*       in this case                                    *)
          Normalize := SUCCESS;
          exit;
        end;

      R.x := MulDiv( U, $4000, W );
      R.y := MulDiv( V, $4000, W );

    end
  else
    begin

      W := Norm( U, V );

      if W > 0 then
       begin
        U := MulDiv( U, $4000, W );
        V := MulDiv( V, $4000, W );

        W := U*U + V*V;

        (* Now, we want that Sqrt( W ) = $4000 *)
        (* Or $1000000 <= W < $1004000         *)

        if U < 0 then begin U := -U; S1 := True; end else S1 := False;
        if V < 0 then begin V := -V; S2 := True; end else S2 := False;

        while W < $1000000 do
         begin
           (* We need to increase W, by a minimal amount *)
           if U < V then inc( U )
                    else inc( V );
           W := U*U + V*V;
         end;

        while W >= $1004000 do
         begin
           (* We need to decrease W, by a minimal amount *)
           if U < V then dec( U )
                    else dec( V );
           W := U*U + V*V;
         end;

        (* Note that in various cases, we can only *)
        (* compute a Sqrt(W) of $3FFF, eg. U=V     *)

        if S1 then U := -U;
        if S2 then V := -V;

        R.x := U; (* Type conversion *)
        R.y := V; (* Type conversion *)

      end
     else
      begin
       Normalize := False;
       pEC^.error := TT_Err_Divide_By_Zero;
      end;
  end;

  Normalize := True;
end;


(****************************************************************)
(*                                                              *)
(* MANAGING THE STACK                                           *)
(*                                                              *)
(*  Instructions appear in the specs' order                     *)
(*                                                              *)
(****************************************************************)

(*******************************************)
(* DUP[]     : Duplicate top stack element *)
(* CodeRange : $20                         *)

   procedure TInterpreter.Ins_DUP( args : PStorage );
   begin
     args^[1] := args^[0];
   end;

(*******************************************)
(* POP[]     : POPs the stack's top elt.   *)
(* CodeRange : $21                         *)

   procedure TInterpreter.Ins_POP( args : PStorage );
   begin
     (* nothing to do *)
   end;

(*******************************************)
(* CLEAR[]   : Clear the entire stack      *)
(* CodeRange : $22                         *)

   procedure TInterpreter.Ins_CLEAR( args : PStorage );
   begin
     new_top := 0;
   end;

(*******************************************)
(* SWAP[]    : Swap the top two elements   *)
(* CodeRange : $23                         *)

   procedure TInterpreter.Ins_SWAP( args : PStorage );
   var L : Long;
   begin
     L        := args^[0];
     args^[0] := args^[1];
     args^[1] := L;
   end;

(*******************************************)
(* DEPTH[]   : return the stack depth      *)
(* CodeRange : $24                         *)

   procedure TInterpreter.Ins_DEPTH( args : PStorage );
   begin
     args^[0] := top;
   end;

(*******************************************)
(* CINDEX[]  : copy indexed element        *)
(* CodeRange : $25                         *)

   procedure TInterpreter.Ins_CINDEX( args : PStorage );
   var
     L : Long;
   begin
     L := args^[0];
     if (L <= 0) or (L > opargs) then
       pEC^.error := TT_Err_Invalid_Reference
     else
       args^[0] := pEC^.stack^[opargs-l];
   end;

(*******************************************)
(* MINDEX[]  : move indexed element        *)
(* CodeRange : $26                         *)

   procedure TInterpreter.Ins_MINDEX( args : PStorage );
   var
     L, K : Long;
   begin
     L := args^[0];
     if (L <= 0) or (L > opargs) then
       pEC^.Error := TT_Err_Invalid_Reference
     else
       begin
         K := pEC^.stack^[opargs-L];

         move( pEC^.stack^[opargs-L+1],
               pEC^.stack^[opargs-L],
               (L-1)*sizeof(Long) );

         pEC^.stack^[opargs-1] := K;
       end;
   end;

(*******************************************)
(* ROLL[]    : roll top three elements     *)
(* CodeRange : $8A                         *)

   procedure TInterpreter.Ins_ROLL( args : PStorage );
   var
     A, B, C : Long;
   begin
     A := args^[2];
     B := args^[1];
     C := args^[0];

     args^[2] := C;
     args^[1] := A;
     args^[0] := B;
   end;

(****************************************************************)
(*                                                              *)
(* MANAGING THE FLOW OF CONTROL                                 *)
(*                                                              *)
(*  Instructions appear in the specs' order                     *)
(*                                                              *)
(****************************************************************)

   function TInterpreter.SkipCode : boolean;
   var
     b : Boolean;
   begin
     b := False;

     inc( pEC^.IP, oplength );

     b := pEC^.IP < pEC^.codeSize;

     if b then b := Calc_Length;

     if not b then
       pEC^.error := TT_Err_Code_Overflow;

     SkipCode := b;
   end;


(*******************************************)
(* IF[]      : IF test                     *)
(* CodeRange : $58                         *)

   procedure TInterpreter.Ins_IF( args : PStorage );
   var
     nIfs : Int;
     Out  : Boolean;
   begin
     if args^[0] <> 0 then exit;

     nIfs := 1;
     Out  := False;

     Repeat

      if not SkipCode then exit;

      Case opcode of

      (* IF *)
       $58 : inc( nIfs );

      (* ELSE *)
       $1B : out:= nIfs=1;

      (* EIF *)
       $59 : begin
              dec( nIfs );
              out:= nIfs=0;
             end;
      end;

     until Out;
   end;


(*******************************************)
(* ELSE[]    : ELSE                        *)
(* CodeRange : $1B                         *)

   procedure TInterpreter.Ins_ELSE( args : PStorage );
   var
     nIfs : Int;
   begin
     nIfs     := 1;

     Repeat

      if not SkipCode then exit;

      case opcode of

      (* IF *)
       $58 : inc( nIfs );

      (* EIF *)
       $59 : dec( nIfs );
      end;

     until nIfs=0;
   end;

(*******************************************)
(* EIF[]     : End IF                      *)
(* CodeRange : $59                         *)

   procedure TInterpreter.Ins_EIF( args : PStorage );
   begin
     (* nothing to do *)
   end;

(*******************************************)
(* JROT[]    : Jump Relative On True       *)
(* CodeRange : $78                         *)

   procedure TInterpreter.Ins_JROT( args : PStorage );
   begin
     if args^[1] <> 0 then
     begin
       inc( pEC^.IP, args^[0] );
       pEC^.step_ins := false;
     end;
   end;

(*******************************************)
(* JMPR[]    : JuMP Relative               *)
(* CodeRange : $1C                         *)

   procedure TInterpreter.Ins_JMPR( args : PStorage );
   begin
     inc( pEC^.IP, args^[0] );
     pEC^.step_ins := false;
   end;

(*******************************************)
(* JROF[]    : Jump Relative On False      *)
(* CodeRange : $79                         *)

   procedure TInterpreter.Ins_JROF( args : PStorage );
   begin
     if args^[1] = 0 then
     begin
       inc( pEC^.IP, args^[0] );
       pEC^.step_ins := false;
     end;
   end;

(****************************************************************)
(*                                                              *)
(* LOGICAL FUNCTIONS                                            *)
(*                                                              *)
(*  Instructions appear in the specs' order                     *)
(*                                                              *)
(****************************************************************)

(*******************************************)
(* LT[]      : Less Than                   *)
(* CodeRange : $50                         *)

   procedure TInterpreter.Ins_LT( args : PStorage );
   begin
     if args^[0] < args^[1] then args^[0] := 1
                            else args^[0] := 0;
   end;

(*******************************************)
(* LTEQ[]    : Less Than or EQual          *)
(* CodeRange : $51                         *)

   procedure TInterpreter.Ins_LTEQ( args : PStorage );
   begin
     if args^[0] <= args^[1] then args^[0] := 1
                             else args^[0] := 0;
   end;

(*******************************************)
(* GT[]      : Greater Than                *)
(* CodeRange : $52                         *)

   procedure TInterpreter.Ins_GT( args : PStorage );
   begin
     if args^[0] > args^[1] then args^[0] := 1
                            else args^[0] := 0;
   end;

(*******************************************)
(* GTEQ[]    : Greater Than or EQual       *)
(* CodeRange : $53                         *)

   procedure TInterpreter.Ins_GTEQ( args : PStorage );
   begin
     if args^[0] >= args^[1] then args^[0] := 1
                             else args^[0] := 0;
   end;

(*******************************************)
(* EQ[]      : EQual                       *)
(* CodeRange : $54                         *)

   procedure TInterpreter.Ins_EQ( args : PStorage );
   begin
     if args^[0] = args^[1] then args^[0] := 1
                            else args^[0] := 0;
   end;

(*******************************************)
(* NEQ[]     : Not EQual                   *)
(* CodeRange : $55                         *)

   procedure TInterpreter.Ins_NEQ( args : PStorage );
   begin
     if args^[0] <> args^[1] then args^[0] := 1
                             else args^[0] := 0;
   end;

(*******************************************)
(* ODD[]     : Odd                         *)
(* CodeRange : $56                         *)

   procedure TInterpreter.Ins_ODD( args : PStorage );
   begin
     if pEC^.func_round( args^[0], 0 ) and 127 = 64 then args^[0] := 1
                                                   else args^[0] := 0;
   end;

(*******************************************)
(* EVEN[]    : Even                        *)
(* CodeRange : $57                         *)

   procedure TInterpreter.Ins_EVEN( args : PStorage );
   begin
     if pEC^.func_round( args^[0], 0 ) and 127 = 0 then args^[0] := 1
                                                  else args^[0] := 0;
   end;

(*******************************************)
(* AND[]     : logical AND                 *)
(* CodeRange : $5A                         *)

   procedure TInterpreter.Ins_AND( args : PStorage );
   begin
     if ( args^[0] <> 0 ) and
        ( args^[1] <> 0 ) then args^[0] := 1
                          else args^[0] := 0;
   end;

(*******************************************)
(* OR[]      : logical OR                  *)
(* CodeRange : $5B                         *)

   procedure TInterpreter.Ins_OR( args : PStorage );
   begin
     if ( args^[0] <> 0 ) or
        ( args^[1] <> 0 ) then args^[0] := 1
                          else args^[0] := 0;
   end;

(*******************************************)
(* NOT[]     : logical NOT                 *)
(* CodeRange : $5C                         *)

   procedure TInterpreter.Ins_NOT( args : PStorage );
   begin
     if args^[0] <> 0 then args^[0] := 0
                      else args^[0] := 1;
   end;

(****************************************************************)
(*                                                              *)
(* ARITHMETIC AND MATH INSTRUCTIONS                             *)
(*                                                              *)
(*  Instructions appear in the specs' order                     *)
(*                                                              *)
(****************************************************************)

(*******************************************)
(* ADD[]     : ADD                         *)
(* CodeRange : $60                         *)

   procedure TInterpreter.Ins_ADD( args : PStorage );
   begin
     inc( args^[0], args^[1] );
   end;

(*******************************************)
(* SUB[]     : SUBstract                   *)
(* CodeRange : $61                         *)

   procedure TInterpreter.Ins_SUB( args : PStorage );
   begin
     dec( args^[0], args^[1] );
   end;

(*******************************************)
(* DIV[]     : DIVide                      *)
(* CodeRange : $62                         *)

   procedure TInterpreter.Ins_DIV( args : PStorage );
   begin
    if args^[1] = 0 then
    begin
      pEC^.error := TT_Err_Divide_By_Zero;
      exit;
    end;

    args^[0] := MulDiv_Round( args^[0], 64, args^[1] );
   end;

(*******************************************)
(* MUL[]     : MULtiply                    *)
(* CodeRange : $63                         *)

   procedure TInterpreter.Ins_MUL( args : PStorage );
   begin
     args^[0] := MulDiv_Round( args^[0], args^[1], 64 );
   end;

(*******************************************)
(* ABS[]     : ABSolute value              *)
(* CodeRange : $64                         *)

   procedure TInterpreter.Ins_ABS( args : PStorage );
   begin
     args^[0] := abs( args^[0] );
   end;

(*******************************************)
(* NEG[]     : NEGate                      *)
(* CodeRange : $65                         *)

   procedure TInterpreter.Ins_NEG( args : PStorage );
   begin
     args^[0] := -args^[0];
   end;

(*******************************************)
(* FLOOR[]   : FLOOR                       *)
(* CodeRange : $66                         *)

   procedure TInterpreter.Ins_FLOOR( args : PStorage );
   begin
     args^[0] := args^[0] and -64;
   end;

(*******************************************)
(* CEILING[] : CEILING                     *)
(* CodeRange : $67                         *)

   procedure TInterpreter.Ins_CEILING( args : PStorage );
   begin
     args^[0] := ( args^[0]+63 ) and -64;
   end;

(*******************************************)
(* MAX[]     : MAXimum                     *)
(* CodeRange : $68                         *)

   procedure TInterpreter.Ins_MAX( args : PStorage );
   begin
     if args^[1] > args^[0] then args^[0] := args^[1];
   end;

(*******************************************)
(* MIN[]     : MINimum                     *)
(* CodeRange : $69                         *)

   procedure TInterpreter.Ins_MIN( args : PStorage );
   begin
     if args^[1] < args^[0] then args^[0] := args^[1];
   end;

(****************************************************************)
(*                                                              *)
(* COMPENSATING FOR THE ENGINE CHARACTERISTICS                  *)
(*                                                              *)
(*  Instructions appear in the specs' order                     *)
(*                                                              *)
(****************************************************************)

(*******************************************)
(* ROUND[ab] : ROUND value                 *)
(* CodeRange : $68-$6B                     *)

   procedure TInterpreter.Ins_ROUND( args : PStorage );
   begin
     args^[0] := pEC^.func_round( args^[0],
                                 pEC^.metrics.compensations[ opcode-$68 ] );
   end;

(*******************************************)
(* NROUND[ab]: No ROUNDing of value        *)
(* CodeRange : $6C-$6F                     *)

   procedure TInterpreter.Ins_NROUND( args : PStorage );
   begin
     args^[0] := Round_None( args^[0],
                             pEC^.metrics.compensations[ opcode-$6C ] );
   end;

(****************************************************************)
(*                                                              *)
(* DEFINING AND USING FUNCTIONS AND INSTRUCTIONS                *)
(*                                                              *)
(*  Instructions appear in the specs' order                     *)
(*                                                              *)
(****************************************************************)

(*******************************************)
(* FDEF[]    : Function DEFinition         *)
(* CodeRange : $2C                         *)

   procedure TInterpreter.Ins_FDEF( args : PStorage );
   var
     func : int;
   begin

     (* check space *)
     if pEC^.numFDefs >= pEC^.maxFDefs then begin
       pEC^.error := TT_Err_Too_Many_FuncDefs;
       exit;
     end;

     func := Int(args^[0]);
     with pEC^.FDefs^[pEC^.numFDefs] do
       begin
         Range  := pEC^.curRange;
         Opc    := func;
         Start  := pEC^.IP+1;
         Active := True;
       end;

     if func > pEC^.maxFunc then
       pEC^.maxFunc := func;

     inc(pEC^.numFDefs);

     (* now skip the whole function definition *)
     (* we don't allow nested IDEFS & FDEFs    *)

     while SkipCode do

       case opcode of

         $89,  (* IDEF *)
         $2C : (* FDEF *)
               begin
                 pEC^.error := TT_Err_Nested_Defs;
                 exit;
               end;

         $2D : (* ENDF *)
               exit;
       end;
   end;

(*******************************************)
(* ENDF[]    : END Function definition     *)
(* CodeRange : $2D                         *)

   procedure TInterpreter.Ins_ENDF( args : PStorage );
   begin

     if callTop <= 0 then   (* We encountered an ENDF without a call *)
     begin
       pEC^.error := TT_Err_ENDF_in_Exec_Stream;
       exit;
     end;

     dec( callTop );

     with pEC^.Callstack^[callTop] do
      begin
       dec( Cur_Count );

       pEC^.step_ins := false;

       if Cur_Count > 0 then

         begin
           (* Loop the current function *)
           inc( callTop );
           pEC^.IP := Cur_Restart;
         end

       else
         (* exit the current call frame                      *)
         (* NOTE : When the last intruction of a program     *)
         (*        is a CALL or LOOPCALL, the return address *)
         (*        is always out of the code range. This is  *)
         (*        valid address, and  is why we do not test *)
         (*        the result of Goto_CodeRange here !!      *)

         Goto_CodeRange( Caller_Range, Caller_IP )
      end;

    end;

(*******************************************)
(* CALL[]    : CALL function               *)
(* CodeRange : $2B                         *)

   procedure TInterpreter.Ins_CALL( args : PStorage );
   var
     ii, nn : Int;
     def    : PDefRecord;
   label
     Fail;
   begin

     (* First of all, check index *)
     if (args^[0] < 0) or (args^[0] > pEC^.maxFunc) then
       goto Fail;

     (* Except for some old Apple fonts, all functions in a TrueType *)
     (* fonts are defined in increasing order, starting from 0.      *)
     (*                                                              *)
     (* This mean that, normally, we have :                          *)
     (*                                                              *)
     (*    pEC^.maxFunc+1 = pEC^.numFDefs                              *)
     (*    pEC^.FDefs[n].opc = n for n in 0..pEC^.maxFunc              *)
     (*                                                              *)

     nn  := Int(args^[0]);
     def := @pEC^.FDefs^[nn];

     if ( pEC^.maxFunc+1 <> pEC^.numFDefs ) or ( def^.opc <> nn ) then begin
       (* lookup the FDefs table *)
       ii  := 0;
       def := @pEC^.FDefs^[0];
       while (ii < pEC^.numFDefs) and (def^.opc <> nn) do begin
         inc(ii);
         inc(def);
       end;

       (* Fail if the function isn't listed *)
       if ii >= pEC^.numFDefs then
         goto Fail;
     end;

     (* check that the function is active *)
     if not def^.active then
       goto Fail;

     (* check call stack *)
     if callTop >= pEC^.callSize then
       begin
         pEC^.error := TT_Err_Stack_Overflow;
         exit;
       end;

     with pEC^.callstack^[callTop] do
       begin
         Caller_Range := pEC^.curRange;
         Caller_IP    := pEC^.IP+1;
         Cur_Count    := 1;
         Cur_Restart  := def^.Start;
       end;

     inc( callTop );

     with def^ do Goto_CodeRange( Range, Start );

     pEC^.step_ins := false;
     exit;

    Fail:
      pEC^.error := TT_Err_Invalid_Reference;
      exit;
    end;

(*******************************************)
(* LOOPCALL[]: LOOP and CALL function      *)
(* CodeRange : $2A                         *)

   procedure TInterpreter.Ins_LOOPCALL( args : PStorage );
   begin

     if ( args^[1] < 0 ) or ( args^[1] >= pEC^.numFDefs ) or
        ( not pEC^.FDefs^[args^[1]].Active ) then
       begin
         pEC^.error := TT_Err_Invalid_Reference;
         exit;
       end;

     if callTop >= pEC^.callSize then
       begin
         pEC^.error := TT_Err_Stack_Overflow;
         exit;
       end;

     if args^[0] > 0 then
       begin
         with pEC^.callstack^[callTop] do
           begin
             Caller_Range := pEC^.curRange;
             Caller_IP    := pEC^.IP+1;
             Cur_Count    := args^[0];
             Cur_Restart  := pEC^.FDefs^[args^[1]].Start;
           end;

         inc( callTop );

         with pEC^.FDefs^[args^[1]] do Goto_CodeRange( Range, Start );

         pEC^.step_ins := false;
       end;

   end;

(*******************************************)
(* IDEF[]    : Instruction DEFinition      *)
(* CodeRange : $89                         *)

   procedure TInterpreter.Ins_IDEF( args : PStorage );
   var
     A : Int;
   begin

     A := 0;

     while ( A < pEC^.numIDefs ) do
       with pEC^.IDefs^[A] do
         begin

           if not Active then
             begin
               Opc    := args^[0];
               Start  := pEC^.IP+1;
               Range  := pEC^.curRange;
               Active := True;

               A := pEC^.numIDefs;

                (* now skip the whole function definition *)
                (* we don't allow nested IDEFS & FDEFs    *)

               while SkipCode do
                 case opcode of

                   $89,  (* IDEF *)
                   $2C : (* FDEF *)
                         begin
                           pEC^.error := TT_Err_Nested_Defs;
                           exit;
                         end;

                   $2D : (* ENDF *)
                         exit;
                 end;
             end
           else
             inc( A );
         end;
     end;

(****************************************************************)
(*                                                              *)
(* PUSHING DATA ONTO THE INTERPRETER STACK                      *)
(*                                                              *)
(*  Instructions appear in the specs' order                     *)
(*                                                              *)
(****************************************************************)

(*******************************************)
(* NPUSHB[]  : PUSH N Bytes                *)
(* CodeRange : $40                         *)

   procedure TInterpreter.Ins_NPUSHB( args : PStorage );
   var
    L, K : Long;
   begin
     L := pEC^.code^[pEC^.IP+1];

     if NeedStackSize(top + L, args) then exit;

     for K := 1 to L do
       args^[k-1] := pEC^.code^[pEC^.IP+1+k];

     inc( new_top, L );
   end;

(*******************************************)
(* NPUSHW[]  : PUSH N Words                *)
(* CodeRange : $41                         *)

   procedure TInterpreter.Ins_NPUSHW( args : PStorage );
   var
    L, K : Long;
   begin
     L := pEC^.code^[pEC^.IP+1];

     if NeedStackSize(top + L, args) then exit;

     inc( pEC^.IP, 2 );

     for K := 1 to L do
       args^[k-1] := GetShort;

     pEC^.step_ins := false;

     inc( new_top, L );
   end;

(*******************************************)
(* PUSHB[abc]: PUSH Bytes                  *)
(* CodeRange : $B0-$B7                     *)

   procedure TInterpreter.Ins_PUSHB( args : PStorage );
   var
    L, K : Long;
   begin
     L := opcode - $B0+1;

     if NeedStackSize(top + L + 1, args) then exit;

     for k := 1 to L do
       args^[k-1] := pEC^.code^[pEC^.ip+k];

   end;

(*******************************************)
(* PUSHW[abc]: PUSH Words                  *)
(* CodeRange : $B8-$BF                     *)

   procedure TInterpreter.Ins_PUSHW( args : PStorage );
   var
     L, K : Long;
   begin
     L := opcode - $B8+1;

     if NeedStackSize(top + L + 1, args) then exit;

     inc( pEC^.IP );

     for k := 1 to L do
       args^[k-1] := GetShort;

     pEC^.step_ins := false;

   end;

(****************************************************************)
(*                                                              *)
(* MANAGING THE STORAGE AREA                                    *)
(*                                                              *)
(*  Instructions appear in the specs' order                     *)
(*                                                              *)
(****************************************************************)

(*******************************************)
(* RS[]      : Read Store                  *)
(* CodeRange : $43                         *)

   procedure TInterpreter.Ins_RS( args : PStorage );
   begin
     if (args^[0] < 0) or (args^[0] >= pEC^.storeSize) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     args^[0] := pEC^.storage^[args^[0]];
   end;

(*******************************************)
(* WS[]      : Write Store                 *)
(* CodeRange : $42                         *)

   procedure TInterpreter.Ins_WS( args : PStorage );
   begin
     if (args^[0] < 0) or (args^[0] >= pEC^.storeSize) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     pEC^.storage^[args^[0]] := args^[1];
   end;

(*******************************************)
(* WCVTP[]   : Write CVT in Pixel units    *)
(* CodeRange : $44                         *)

   procedure TInterpreter.Ins_WCVTP( args : PStorage );
   begin
     if (args^[0] < 0) or (args^[0] >= pEC^.cvtSize) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     pEC^.func_write_cvt( args^[0], args^[1] );
   end;

(*******************************************)
(* WCVTF[]   : Write CVT in FUnits         *)
(* CodeRange : $70                         *)

   procedure TInterpreter.Ins_WCVTF( args : PStorage );
   begin
     if (args^[0] < 0) or (args^[0] >= pEC^.cvtSize) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     pEC^.cvt^[args^[0]] := Scale_Pixels(args^[1]);
   end;

(*******************************************)
(* RCVT[]    : Read CVT                    *)
(* CodeRange : $45                         *)

   procedure TInterpreter.Ins_RCVT( args : PStorage );
   begin
     if (args^[0] < 0) or (args^[0] >= pEC^.cvtSize) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     args^[0] := pEC^.func_read_cvt(args^[0]);
   end;

(****************************************************************)
(*                                                              *)
(* MANAGING THE GRAPHICS STATE                                  *)
(*                                                              *)
(*  Instructions appear in the specs' order                     *)
(*                                                              *)
(****************************************************************)

(*******************************************)
(* SVTCA[a]  : Set F and P vectors to axis *)
(* CodeRange : $00-$01                     *)

   procedure TInterpreter.Ins_SVTCA( args : PStorage );
   var A, B : Short;
   begin
     case (opcode and 1) of
       0 : A := $0000;
       1 : A := $4000;
     end;
     B := A xor $4000;

     pEC^.GS.freeVector.x := A;
     pEC^.GS.projVector.x := A;
     pEC^.GS.dualVector.x := A;

     pEC^.GS.freeVector.y := B;
     pEC^.GS.projVector.y := B;
     pEC^.GS.dualVector.y := B;

     Compute_Funcs;
   end;

(*******************************************)
(* SPVTCA[a] : Set PVector to Axis         *)
(* CodeRange : $02-$03                     *)

   procedure TInterpreter.Ins_SPVTCA( args : PStorage );
   var A, B : Short;
   begin
     case (opcode and 1) of
       0 : A := $0000;
       1 : A := $4000;
     end;
     B := A xor $4000;

     pEC^.GS.projVector.x := A;
     pEC^.GS.dualVector.x := A;

     pEC^.GS.projVector.y := B;
     pEC^.GS.dualVector.y := B;

     Compute_Funcs;
   end;

(*******************************************)
(* SFVTCA[a] : Set FVector to Axis         *)
(* CodeRange : $04-$05                     *)

   procedure TInterpreter.Ins_SFVTCA( args : PStorage );
   var A, B : Short;
   begin
     case (opcode and 1) of
       0 : A := $0000;
       1 : A := $4000;
     end;
     B := A xor $4000;

     pEC^.GS.freeVector.x := A;
     pEC^.GS.freeVector.y := B;

     Compute_Funcs;
   end;



   function TInterpreter.Ins_SxVTL( aIdx1     : Int;
                       aIdx2     : Int;
                       aOpc      : Int;
                       var Vec   : TT_UnitVector ) : boolean;
   var
     A, B, C : Long;
   begin
     Ins_SxVTL := False;

     with pEC^ do
     begin

       if (aIdx2 >= zp1.n_points) or (aIdx1 >= zp2.n_points) then
         begin
           Error := TT_Err_Invalid_Reference;
           exit;
         end;

       with zp1.Cur^[aIdx2] do
       begin
         A := x;
         B := y;
       end;

       with zp2.Cur^[aIdx1] do
       begin
         dec( A, x );
         dec( B, y );
       end;

       if aOpc and 1 <> 0 then
        begin
         C :=  B;  (* CounterClockwise rotation *)
         B :=  A;
         A := -C;
        end;

       if not Normalize( A, B, Vec ) then
       begin
         pEC^.error := TT_Err_Ok;
         Vec.x     := $4000;
         Vec.y     := $0000;
       end;

       Ins_SxVTL := True;
     end;
   end;


(*******************************************)
(* SPVTL[a]  : Set PVector to Line         *)
(* CodeRange : $06-$07                     *)

   procedure TInterpreter.Ins_SPVTL( args : PStorage );
   begin
     if not INS_SxVTL( args^[1],
                       args^[0],
                       opcode,
                       pEC^.GS.projVector ) then exit;

     pEC^.GS.dualVector := pEC^.GS.projVector;
     Compute_Funcs;
   end;

(*******************************************)
(* SFVTL[a]  : Set FVector to Line         *)
(* CodeRange : $08-$09                     *)

   procedure TInterpreter.Ins_SFVTL( args : PStorage );
   begin
     if not INS_SxVTL( args^[1],
                       args^[0],
                       opcode,
                       pEC^.GS.freeVector ) then exit;

     Compute_Funcs;
   end;

(*******************************************)
(* SFVTPV[]  : Set FVector to PVector      *)
(* CodeRange : $0E                         *)

   procedure TInterpreter.Ins_SFVTPV( args : PStorage );
   begin
     pEC^.GS.freeVector := pEC^.GS.projVector;
     Compute_Funcs;
   end;

(*******************************************)
(* SDPVTL[a] : Set Dual PVector to Line    *)
(* CodeRange : $86-$87                     *)

   procedure TInterpreter.Ins_SDPVTL( args : PStorage );
   var
     A, B, C : Long;
     p1, p2  : Int;
   begin

     p1 := args^[1];
     p2 := args^[0];

     if (args^[0] < 0) or (args^[0] >= pEC^.zp1.n_points) or
        (args^[1] < 0) or (args^[1] >= pEC^.zp2.n_points) then
       begin
         pEC^.error := TT_Err_Invalid_Reference;
         exit;
       end;

     A := pEC^.zp1.org^[p2].x - pEC^.zp2.org^[p1].x;
     B := pEC^.zp1.org^[p2].y - pEC^.zp2.org^[p1].y;

     if opcode and 1 <> 0 then
      begin
       C :=  B;  (* CounterClockwise rotation *)
       B :=  A;
       A := -C;
      end;

     Normalize( A, B, pEC^.GS.dualVector );

     A := pEC^.zp1.cur^[p2].x - pEC^.zp2.cur^[p1].x;
     B := pEC^.zp1.cur^[p2].y - pEC^.zp2.cur^[p1].y;

     if opcode and 1 <> 0 then
      begin
       C :=  B;  (* CounterClockwise rotation *)
       B :=  A;
       A := -C;
      end;

     Normalize( A, B, pEC^.GS.projVector );

     Compute_Funcs;
     pEC^.error := TT_Err_Ok;
   end;

(*******************************************)
(* SPVFS[]   : Set PVector From Stack      *)
(* CodeRange : $0A                         *)

   procedure TInterpreter.Ins_SPVFS( args : PStorage );
   var
     S    : Short;
     X, Y : Long;
   begin
     S := args^[1]; Y := S;  (* type conversion; extends sign *)
     S := args^[0]; X := S;  (* type conversion; extends sign *)

     if not Normalize( X, Y, pEC^.GS.projVector ) then exit;

     pEC^.GS.dualVector := pEC^.GS.projVector;

     Compute_Funcs;
   end;

(*******************************************)
(* SFVFS[]   : Set FVector From Stack      *)
(* CodeRange : $0B                         *)

   procedure TInterpreter.Ins_SFVFS( args : PStorage );
   var
     S    : Short;
     X, Y : Long;
   begin
     S := args^[1]; Y := S;  (* type conversion; extends sign *)
     S := args^[0]; X := S;  (* type conversion; extends sign *)

     if not Normalize( X, Y, pEC^.GS.freeVector ) then exit;

     Compute_Funcs;
   end;

(*******************************************)
(* GPV[]     : Get Projection Vector       *)
(* CodeRange : $0C                         *)

   procedure TInterpreter.Ins_GPV( args : PStorage );
   begin
     args^[0] := pEC^.GS.projVector.x;
     args^[1] := pEC^.GS.projVector.y;
   end;

(*******************************************)
(* GFV[]     : Get Freedom Vector          *)
(* CodeRange : $0D                         *)

   procedure TInterpreter.Ins_GFV( args : PStorage );
   begin
     args^[0] := pEC^.GS.freeVector.x;
     args^[1] := pEC^.GS.freeVector.y;
   end;

(*******************************************)
(* SRP0[]    : Set Reference Point 0       *)
(* CodeRange : $10                         *)

   procedure TInterpreter.Ins_SRP0( args : PStorage );
   begin
     pEC^.GS.rp0 := args^[0];
   end;

(*******************************************)
(* SRP1[]    : Set Reference Point 1       *)
(* CodeRange : $11                         *)

   procedure TInterpreter.Ins_SRP1( args : PStorage );
   begin
     pEC^.GS.rp1 := args^[0];
   end;

(*******************************************)
(* SRP2[]    : Set Reference Point 2       *)
(* CodeRange : $12                         *)

   procedure TInterpreter.Ins_SRP2( args : PStorage );
   begin
     pEC^.GS.rp2 := args^[0];
   end;

(*******************************************)
(* SZP0[]    : Set Zone Pointer 0          *)
(* CodeRange : $13                         *)

   procedure TInterpreter.Ins_SZP0( args : PStorage );
   begin
     case args^[0] of

       0 : pEC^.zp0 := pEC^.Twilight;
       1 : pEC^.zp0 := pEC^.Pts;
     else
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     pEC^.GS.gep0 := args^[0];
   end;

(*******************************************)
(* SZP1[]    : Set Zone Pointer 1          *)
(* CodeRange : $14                         *)

   procedure TInterpreter.Ins_SZP1( args : PStorage );
   begin
     case args^[0] of

       0 : pEC^.zp1 := pEC^.Twilight;
       1 : pEC^.zp1 := pEC^.Pts;
     else
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     pEC^.GS.gep1 := args^[0];
   end;

(*******************************************)
(* SZP2[]    : Set Zone Pointer 2          *)
(* CodeRange : $15                         *)

   procedure TInterpreter.Ins_SZP2( args : PStorage );
   begin
     case args^[0] of

       0 : pEC^.zp2 := pEC^.Twilight;
       1 : pEC^.zp2 := pEC^.Pts;
     else
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     pEC^.GS.gep2 := args^[0];
   end;

(*******************************************)
(* SZPS[]    : Set Zone Pointers           *)
(* CodeRange : $16                         *)

   procedure TInterpreter.Ins_SZPS( args : PStorage );
   begin
     case args^[0] of

       0 : pEC^.zp0 := pEC^.Twilight;
       1 : pEC^.zp0 := pEC^.Pts;
     else
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     pEC^.zp1 := pEC^.zp0;
     pEC^.zp2 := pEC^.zp0;

     pEC^.GS.gep0 := args^[0];
     pEC^.GS.gep1 := args^[0];
     pEC^.GS.gep2 := args^[0];
   end;

(*******************************************)
(* RTHG[]    : Round To Half Grid          *)
(* CodeRange : $19                         *)

   procedure TInterpreter.Ins_RTHG( args : PStorage );
   begin
     pEC^.GS.round_state := TT_Round_To_Half_Grid;
     pEC^.func_round := Round_To_Half_Grid;
   end;

(*******************************************)
(* RTG[]     : Round To Grid               *)
(* CodeRange : $18                         *)

   procedure TInterpreter.Ins_RTG( args : PStorage );
   begin
     pEC^.GS.round_state := TT_Round_To_Grid;
     pEC^.func_round := Round_To_Grid;
   end;

(*******************************************)
(* RTDG[]    : Round To Double Grid        *)
(* CodeRange : $3D                         *)

   procedure TInterpreter.Ins_RTDG( args : PStorage );
   begin
     pEC^.GS.round_state := TT_Round_To_Double_Grid;
     pEC^.func_round := Round_To_Double_Grid;
   end;

(*******************************************)
(* RUTG[]    : Round Up To Grid            *)
(* CodeRange : $7C                         *)

   procedure TInterpreter.Ins_RUTG( args : PStorage );
   begin
     pEC^.GS.round_state := TT_Round_Up_To_Grid;
     pEC^.func_round := Round_Up_To_Grid;
   end;

(*******************************************)
(* RDTG[]    : Round Down To Grid          *)
(* CodeRange : $7D                         *)

   procedure TInterpreter.Ins_RDTG( args : PStorage );
   begin
     pEC^.GS.round_state := TT_Round_Down_To_Grid;
     pEC^.func_round := Round_Down_To_Grid;
   end;

(*******************************************)
(* ROFF[]    : Round OFF                   *)
(* CodeRange : $7A                         *)

   procedure TInterpreter.Ins_ROFF( args : PStorage );
   begin
     pEC^.GS.round_state := TT_Round_Off;
     pEC^.func_round := Round_None;
   end;

(*******************************************)
(* SROUND[]  : Super ROUND                 *)
(* CodeRange : $76                         *)

   procedure TInterpreter.Ins_SROUND( args : PStorage );
   begin
     SetSuperRound( $4000, args^[0] );
     pEC^.GS.round_state := TT_Round_Super;
     pEC^.func_round := Round_Super;
   end;

(*******************************************)
(* S45ROUND[]: Super ROUND 45 degrees      *)
(* CodeRange : $77                         *)

   procedure TInterpreter.Ins_S45ROUND( args : PStorage );
   begin
     SetSuperRound( $2D41, args^[0] );
     pEC^.GS.round_state := TT_Round_Super_45;
     pEC^.func_round := Round_Super_45;
   end;


(*******************************************)
(* SLOOP[]   : Set LOOP variable           *)
(* CodeRange : $17                         *)

   procedure TInterpreter.Ins_SLOOP( args : PStorage );
   begin
     pEC^.GS.Loop := args^[0];
   end;

(*******************************************)
(* SMD[]     : Set Minimum Distance        *)
(* CodeRange : $1A                         *)

   procedure TInterpreter.Ins_SMD( args : PStorage );
   begin
     pEC^.GS.minimum_distance := args^[0];
   end;

(*******************************************)
(* INSTCTRL[]: INSTruction ConTRol         *)
(* CodeRange : $8e                         *)

   procedure TInterpreter.Ins_INSTCTRL( args : PStorage );
   var
     K, L : Int;
   begin
     K := args^[1];
     L := args^[0];

     if ( K < 1 ) or ( K > 2 ) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     if L <> 0 then L := K;

     pEC^.GS.instruct_control := ( pEC^.GS.instruct_control and not K ) or L;
   end;

(*******************************************)
(* SCANCTRL[]: SCAN ConTRol                *)
(* CodeRange : $85                         *)

   procedure TInterpreter.Ins_SCANCTRL( args : PStorage );
   var
     A : Int;
   begin

     (* Get Threshold *)
     A := args^[0] and $FF;

     if A = $FF then
       pEC^.GS.scan_Control := True
     else
       if A = 0 then
         pEC^.GS.scan_Control := False
     else
       begin

         A := A * 64;

         (* XXX TODO : Add rotation and stretch cases *)

         if ( args^[0] and $100 <> 0 ) and
            ( pEC^.metrics.pointSize <= A ) then pEC^.GS.scan_Control := True;

         //if ( args^[0] and $200 <> 0 ) and
         //   ( false ) then pEC^.GS.scan_Control := True;

         //if ( args^[0] and $400 <> 0 ) and
         //   ( false ) then pEC^.GS.scan_Control := True;

         if ( args^[0] and $800 <> 0 ) and
            ( pEC^.metrics.pointSize > A ) then pEC^.GS.scan_Control := False;

         if ( args^[0] and $1000 <> 0 ) and
            ( not False ) then pEC^.GS.scan_Control := False;

         if ( args^[0] and $2000 <> 0 ) and
            ( not False ) then pEC^.GS.scan_Control := False;
       end;
   end;

(*******************************************)
(* SCANTYPE[]: SCAN TYPE                   *)
(* CodeRange : $8D                         *)

   procedure TInterpreter.Ins_SCANTYPE( args : PStorage );
   begin
     (* For compatibility with future enhancements, *)
     (* we must ignore new modes                    *)

     if (args^[0] >= 0 ) and (args^[0] <= 5) then
     begin
       if args^[0] = 3 then args^[0] := 2;

       pEC^.GS.scan_type := args^[0];
     end;
   end;

(**********************************************)
(* SCVTCI[]  : Set Control Value Table Cut In *)
(* CodeRange : $1D                            *)

   procedure TInterpreter.Ins_SCVTCI( args : PStorage );
   begin
     pEC^.GS.control_value_cutin := args^[0];
   end;

(**********************************************)
(* SSWCI[]   : Set Single Width Cut In        *)
(* CodeRange : $1E                            *)

   procedure TInterpreter.Ins_SSWCI( args : PStorage );
   begin
     pEC^.GS.single_width_cutin := args^[0];
   end;

(**********************************************)
(* SSW[]     : Set Single Width               *)
(* CodeRange : $1F                            *)

   procedure TInterpreter.Ins_SSW( args : PStorage );
   begin
     pEC^.GS.single_width_value := args^[0] div $400;
   end;

(**********************************************)
(* FLIPON[]  : Set Auto_flip to On            *)
(* CodeRange : $4D                            *)

   procedure TInterpreter.Ins_FLIPON( args : PStorage );
   begin
     pEC^.GS.auto_flip := True;
   end;

(**********************************************)
(* FLIPOFF[] : Set Auto_flip to Off           *)
(* CodeRange : $4E                            *)

   procedure TInterpreter.Ins_FLIPOFF( args : PStorage );
   begin
     pEC^.GS.auto_flip := False;
   end;

(**********************************************)
(* SANGW[]   : Set Angle Weigth               *)
(* CodeRange : $7E                            *)

   procedure TInterpreter.Ins_SANGW( args : PStorage );
   begin
     (* instruction not supported anymore *)
   end;

(**********************************************)
(* SDB[]     : Set Delta Base                 *)
(* CodeRange : $5E                            *)

   procedure TInterpreter.Ins_SDB( args : PStorage );
   begin
     pEC^.GS.delta_base := args^[0]
   end;

(**********************************************)
(* SDS[]     : Set Delta Shift                *)
(* CodeRange : $5F                            *)

   procedure TInterpreter.Ins_SDS( args : PStorage );
   begin
     pEC^.GS.delta_shift := args^[0]
   end;

(**********************************************)
(* GC[a]     : Get Coordinate projected onto  *)
(* CodeRange : $46-$47                        *)

(* BULLSHIT : Measures from the original glyph must to be taken *)
(*            along the dual projection vector !!               *)

   procedure TInterpreter.Ins_GC( args : PStorage );
   var
     L : Int;
   begin
     L := args^[0];

     if (L < 0) or (L >= pEC^.zp2.n_points) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     case opcode and 1 of

        0 : L := pEC^.func_project ( pEC^.zp2.cur^[L], Null_Vector );
        1 : L := pEC^.func_dualProj( pEC^.zp2.org^[L], Null_Vector );
       end;

     args^[0] := L;
    end;

(**********************************************)
(* SCFS[]    : Set Coordinate From Stack      *)
(* CodeRange : $48                            *)
(*                                            *)
(* Formule :                                  *)
(*                                            *)
(*   OA := OA + ( value - OA.p )/( f.p ) x f  *)
(*                                            *)

   procedure TInterpreter.Ins_SCFS( args : PStorage );
   var
     K, L : Int;
   begin
     L := args^[0];

     if (args^[0] < 0) or (args^[0] >= pEC^.zp2.n_points) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     K := pEC^.func_project( pEC^.zp2.cur^[L], Null_Vector );

     pEC^.func_move( @pEC^.zp2, L, args^[1] - K );

     (* not part of the specs, but here for safety *)

     if pEC^.GS.gep2 = 0 then
       pEC^.zp2.org^[L] := pEC^.zp2.cur^[L];

   end;

(**********************************************)
(* MD[a]     : Measure Distance               *)
(* CodeRange : $49-$4A                        *)

(* BULLSHIT : Measure taken in the original glyph must be along *)
(*            the dual projection vector                        *)

(* Second BULLSHIT : Flag attributions are inverted !!            *)
(*                   0 => measure distance in original outline    *)
(*                   1 => measure distance in grid-fitted outline *)

   procedure TInterpreter.Ins_MD( args : PStorage );
   var
     K, L : Int;
     D    : TT_F26dot6;
   begin
     K := args^[1];
     L := args^[0];

     if (args^[0] < 0) or (args^[0] >= pEC^.zp0.n_points) or
        (args^[1] < 0) or (args^[1] >= pEC^.zp1.n_points) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     case opcode and 1 of

       0 : D := pEC^.func_dualProj( pEC^.zp0.org^[L], pEC^.zp1.org^[K] );
       1 : D := pEC^.func_project ( pEC^.zp0.cur^[L], pEC^.zp1.cur^[K] );
     end;

     args^[0] := D;
   end;

(**********************************************)
(* MPPEM[]   : Measure Pixel Per EM           *)
(* CodeRange : $4B                            *)

  procedure TInterpreter.Ins_MPPEM( args : PStorage );
  begin
    args^[0] := Get_Ppem;
  end;

(**********************************************)
(* MPS[]     : Measure PointSize              *)
(* CodeRange : $4C                            *)

   procedure TInterpreter.Ins_MPS( args : PStorage );
   begin
     args^[0] := pEC^.metrics.pointSize;
   end;

(****************************************************************)
(*                                                              *)
(* MANAGING OUTLINES                                            *)
(*                                                              *)
(*  Instructions appear in the specs' order                     *)
(*                                                              *)
(****************************************************************)


(**********************************************)
(* FLIPPT[]  : FLIP PoinT                     *)
(* CodeRange : $80                            *)

   procedure TInterpreter.Ins_FLIPPT( args : PStorage );
   var
     point : Int;
   begin
     if top < pEC^.GS.loop then
     begin
       pEC^.error := TT_Err_Too_Few_Arguments;
       exit;
     end;

     while pEC^.GS.loop > 0 do
     begin
       dec( opargs );

       point := pEC^.stack^[ opargs ];

       if (point < 0) or (point >= pEC^.pts.n_points) then
       begin
         pEC^.error := TT_Err_Invalid_Reference;
         exit;
       end;

       pEC^.pts.flags^[point] := pEC^.pts.flags^[point] xor TT_Flag_On_Curve;

       dec( pEC^.GS.loop );
     end;

     pEC^.GS.loop := 1;
     new_top := opargs;
   end;

(**********************************************)
(* FLIPRGON[]: FLIP RanGe ON                  *)
(* CodeRange : $81                            *)

   procedure TInterpreter.Ins_FLIPRGON( args : PStorage );
   var
     I, K, L : Int;
   begin
     K := args^[1];
     L := args^[0];

     if (K < 0) or (K >= pEC^.pts.n_points) or
        (L < 0) or (L >= pEC^.pts.n_points) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     for I := L to K do
       pEC^.pts.flags^[I] := pEC^.pts.flags^[I] or TT_Flag_On_Curve;
   end;

(**********************************************)
(* FLIPRGOFF : FLIP RanGe OFF                 *)
(* CodeRange : $82                            *)

   procedure TInterpreter.Ins_FLIPRGOFF( args : PStorage );
   var
     I, K, L : Int;
   begin
     K := args^[1];
     L := args^[0];

     if (K < 0) or (K >= pEC^.pts.n_points) or
        (L < 0) or (L >= pEC^.pts.n_points) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     for I := L to K do
       pEC^.pts.flags^[I] := pEC^.pts.flags^[I] and not TT_Flag_On_Curve;
   end;



  function TInterpreter.Compute_Point_Displacement( out x    : TT_F26dot6;
                                       out y    : TT_F26dot6;
                                       out zone : PGlyph_Zone;
                                       out refp : Int ) : TError;
  var
    zp   : PGlyph_Zone;
    p    : Int;
    d    : TT_F26dot6;
  begin

    Compute_Point_Displacement := Success;

    case opcode and 1 of
      0 : begin zp := @pEC^.zp1; p := pEC^.GS.rp2; end;
      1 : begin zp := @pEC^.zp0; p := pEC^.GS.rp1; end;
    end;

    if (p < 0) or (p >= zp^.n_points) then
    begin
      pEC^.error := TT_Err_Invalid_Displacement;
      Compute_Point_Displacement := Failure;
      exit;
    end;

    zone := zp;
    refp := p;

    d := pEC^.func_project( zp^.cur^[p], zp^.org^[p] );

    x := MulDiv_Round( d, Long(pEC^.GS.freeVector.x)*$10000, pEC^.F_dot_P );
    y := MulDiv_Round( d, Long(pEC^.GS.freeVector.y)*$10000, pEC^.F_dot_P );

  end;


  procedure TInterpreter.Move_Zp2_Point( point : Int;
                            dx    : TT_F26dot6;
                            dy    : TT_F26dot6 );
  begin
    if pEC^.GS.freeVector.x <> 0 then
    begin
      inc( pEC^.zp2.cur^[point].x, dx );
      pEC^.zp2.flags^[point] := pEC^.zp2.flags^[point] or TT_Flag_Touched_X;
    end;

    if pEC^.GS.freeVector.y <> 0 then
    begin
      inc( pEC^.zp2.cur^[point].y, dy );
      pEC^.zp2.flags^[point] := pEC^.zp2.flags^[point] or TT_Flag_Touched_Y;
    end;
  end;

(**********************************************)
(* SHP[a]    : SHift Point by the last point  *)
(* CodeRange : $32-33                         *)

   procedure TInterpreter.Ins_SHP( args : PStorage );
   var
     zp   : PGlyph_Zone;
     refp : Int;

     dx   : TT_F26dot6;
     dy   : TT_F26dot6;
     point: Int;
   begin

     if Compute_Point_Displacement( dx, dy, zp, refp ) then
       exit;

     if top < pEC^.GS.loop then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     while pEC^.GS.loop > 0 do
     begin

       dec( opargs );

       point := pEC^.stack^[ opargs ];

       if (point < 0) or (point >= pEC^.zp2.n_points) then
       begin
         pEC^.error := TT_Err_Invalid_Reference;
         exit;
       end;

       Move_Zp2_Point( point, dx, dy );

       dec( pEC^.GS.loop );

     end;

     pEC^.GS.loop := 1;
     new_top := opargs;
   end;

(**********************************************)
(* SHC[a]    : SHift Contour                  *)
(* CodeRange : $34-35                         *)

   procedure TInterpreter.Ins_SHC( args : PStorage );
   var
     zp   : PGlyph_Zone;
     refp : Int;
     dx   : TT_F26dot6;
     dy   : TT_F26dot6;

     contour, i : Int;

     first_point, last_point : Int;
   begin

     contour := args^[0];

     if (args^[0] < 0) or (args^[0] >= pEC^.pts.n_contours ) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     if Compute_Point_Displacement( dx, dy, zp, refp ) then
       exit;

     if contour = 0 then first_point := 0 else
                         first_point := pEC^.pts.conEnds^[contour-1]+1;

     last_point := pEC^.pts.conEnds^[contour];

     for i := first_point to last_point do
     begin
       if (zp^.cur <> pEC^.zp2.cur) or
          (refp <> i ) then

         Move_Zp2_Point( i, dx, dy );
     end;

   end;

(**********************************************)
(* SHZ[a]    : SHift Zone                     *)
(* CodeRange : $36-37                         *)

   procedure TInterpreter.Ins_SHZ( args : PStorage );
   var
     zp   : PGlyph_Zone;
     refp : Int;
     dx   : TT_F26dot6;
     dy   : TT_F26dot6;

     i : Int;

     last_point : Int;
   begin

     //zone := args^[0];

     if (args^[0] < 0) or (args^[0] > 1) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     if Compute_Point_Displacement( dx, dy, zp, refp ) then
       exit;

     last_point := zp^.n_points-1;

     for i := 0 to last_point do
     begin
       if (zp^.cur <> pEC^.zp2.cur) or
          (refp <> i ) then

         Move_Zp2_Point( i, dx, dy );
     end;

   end;

(**********************************************)
(* SHPIX[]   : SHift points by a PIXel amount *)
(* CodeRange : $38                            *)

   procedure TInterpreter.Ins_SHPIX( args : PStorage );
   var
     dx   : TT_F26dot6;
     dy   : TT_F26dot6;
     point: Int;
   begin

     if top < pEC^.GS.loop then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     dx := MulDiv_Round( args^[0],
                         pEC^.GS.freeVector.x,
                         $4000 );

     dy := MulDiv_Round( args^[0],
                         pEC^.GS.freeVector.y,
                         $4000 );

     while pEC^.GS.loop > 0 do
     begin

       dec( opargs );

       point := pEC^.stack^[ opargs ];

       if (point < 0) or (point >= pEC^.zp2.n_points) then
       begin
         pEC^.error := TT_Err_Invalid_Reference;
         exit;
       end;

       Move_Zp2_Point( point, dx, dy );

       dec( pEC^.GS.loop );

     end;

     pEC^.GS.loop := 1;
     new_top := opargs;
   end;

(**********************************************)
(* MSIRP[a]  : Move Stack Indirect Relative   *)
(* CodeRange : $3A-$3B                        *)

   procedure TInterpreter.Ins_MSIRP( args : PStorage );
   var
     point    : Int;
     distance : TT_F26dot6;
   begin

     point := args^[0];

     if (args^[0] < 0) or (args^[0] >= pEC^.zp1.n_points) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     (* XXX : UNDOCUMENTED - Twilight Zone *)

     (* Again, one stupid undocumented feature found in the *)
     (* twilight zone. What did these guys had in mind when *)
     (* they wrote the spec ? There _must_ be another       *)
     (* specification than the published one !! #@%$& !!    *)

     if pEC^.GS.gep0 = 0 then   (* if in twilight zone *)
     begin
       pEC^.zp1.org^[point] := pEC^.zp0.org^[pEC^.GS.rp0];
       pEC^.zp1.cur^[point] := pEC^.zp1.org^[point];
     end;

     distance := pEC^.func_project( pEC^.zp1.cur^[point],
                                   pEC^.zp0.cur^[pEC^.GS.rp0] );

     pEC^.func_move( @pEC^.zp1, point, args^[1] - distance );

     pEC^.GS.rp1 := pEC^.GS.rp0;
     pEC^.GS.rp2 := point;

     if opcode and 1 <> 0 then pEC^.GS.rp0 := point;
   end;

(**********************************************)
(* MDAP[a]   : Move Direct Absolute Point     *)
(* CodeRange : $2E-$2F                        *)

   procedure TInterpreter.Ins_MDAP( args : PStorage );
   var
     point    : Int;
     cur_dist : TT_F26dot6;
     distance : TT_F26dot6;
   begin
     point := args^[0];

     if (args^[0] < 0) or (args^[0] >= pEC^.zp0.n_points) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     (* XXXX Is there some undocumented feature while in the *)
     (*      twilight zone ??                                *)

     if opcode and 1 <> 0 then
       begin

         cur_dist := pEC^.func_project( pEC^.zp0.cur^[point], Null_Vector );

         distance := pEC^.func_round( cur_dist,
                                     pEC^.metrics.compensations[0] ) -
                     cur_dist;
       end
     else
       distance := 0;

     pEC^.func_move( @pEC^.zp0, point, distance );

     pEC^.GS.rp0 := point;
     pEC^.GS.rp1 := point;
   end;

(**********************************************)
(* MIAP[a]   : Move Indirect Absolute Point   *)
(* CodeRange : $3E-$3F                        *)

   procedure TInterpreter.Ins_MIAP( args : PStorage );
   var
     cvtEntry : Int;
     point    : Int;
     distance : TT_F26dot6;
     org_dist : TT_F26dot6;
   begin
     cvtEntry := args^[1];
     point    := args^[0];

     if (args^[0] < 0) or (args^[0] >= pEC^.zp0.n_points  ) or
        (args^[1] < 0) or (args^[1] >= pEC^.cvtSize) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     (* Undocumented :                                    *)
     (*                                                   *)
     (* The behaviour of an MIAP instruction is quite     *)
     (* different when used in the twilight zone^.        *)
     (*                                                   *)
     (* First, no control value cutin test is performed   *)
     (* as it would fail anyway. Second, the original     *)
     (* point, i.e. (org_x,org_y) of zp0.point, is set   *)
     (* to the absolute, unrounded, distance found in     *)
     (* the CVT.                                          *)
     (*                                                   *)
     (* This is used in the CVT programs of the Microsoft *)
     (* fonts Arial, Times, etc.., in order to re-adjust  *)
     (* some key font heights. It allows the use of the   *)
     (* IP instruction in the twilight zone, which        *)
     (* otherwise would be "illegal" per se the specs :)  *)
     (*                                                   *)
     (* We implement it with a special sequence for the   *)
     (* twilight zone. This is a bad hack, but it seems   *)
     (* to work..                                         *)
     (*                                         - David   *)

     distance := pEC^.func_read_cvt(cvtEntry);

     if pEC^.GS.gep0 = 0 then  (* If in twilight zone *)
     begin
       pEC^.zp0.org^[point].y := MulDiv_Round( pEC^.GS.freeVector.x,
                                              distance,
                                              $4000 );

       pEC^.zp0.org^[point].y := MulDiv_Round( pEC^.GS.freeVector.y,
                                              distance,
                                              $4000 );

       pEC^.zp0.cur^[point] := pEC^.zp0.org^[point];
     end;

     org_dist := pEC^.func_project( pEC^.zp0.cur^[point], Null_Vector );

     if opcode and 1 <> 0 then  (* rounding and control cutin flag *)
     begin

       if abs( distance-org_dist ) > pEC^.GS.control_value_cutin then
         distance := org_dist;

       distance := pEC^.func_round( distance,
                                   pEC^.metrics.compensations[0] );
     end;

     pEC^.func_move( @pEC^.zp0, point, distance - org_dist );

     pEC^.GS.rp0 := point;
     pEC^.GS.rp1 := point;

   end;

(**********************************************)
(* MDRP[abcde] : Move Direct Relative Point   *)
(* CodeRange   : $C0-$DF                      *)

   procedure TInterpreter.Ins_MDRP( args : PStorage );
   var
     point      : Int;
     distance   : TT_F26dot6;
     org_dist   : TT_F26dot6;
   begin
     point := args^[0];

     if (args^[0] < 0) or (args^[0] >= pEC^.zp1.n_points) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     (* XXXX Is there some undocumented feature while in the *)
     (*      twilight zone ??                                *)

     org_dist := pEC^.func_dualProj( pEC^.zp1.org^[point],
                                    pEC^.zp0.org^[pEC^.GS.rp0] );
     (* single width cutin test *)

     if abs(org_dist) < pEC^.GS.single_width_cutin then

       if org_dist >= 0 then org_dist :=  pEC^.GS.single_width_value
                        else org_dist := -pEC^.GS.single_width_value;

     (* round flag *)

     if opcode and 4 <> 0 then

       distance := pEC^.func_round( org_dist,
                                   pEC^.metrics.compensations[ opcode and 3 ] )
     else
       distance := Round_None( org_dist,
                               pEC^.metrics.compensations[ opcode and 3 ] );

     (* minimum distance flag *)

     if opcode and 8 <> 0 then
     begin

       if org_dist >= 0 then

         if distance < pEC^.GS.minimum_distance then
           distance := pEC^.GS.minimum_distance
         else
       else
         if distance > -pEC^.GS.minimum_distance then
           distance := -pEC^.GS.minimum_distance;
     end;

     (* now move the point *)

     org_dist := pEC^.func_project( pEC^.zp1.cur^[point],
                                   pEC^.zp0.cur^[pEC^.GS.rp0] );

     pEC^.func_move( @pEC^.zp1, point, distance - org_dist );

     pEC^.GS.rp1 := pEC^.GS.rp0;
     pEC^.GS.rp2 := point;

     if opcode and 16 <> 0 then pEC^.GS.rp0 := point;
   end;

(**********************************************)
(* MIRP[abcde] : Move Indirect Relative Point *)
(* CodeRange   : $E0-$FF                      *)

   procedure TInterpreter.Ins_MIRP( args : PStorage );
   var
     point    : Int;
     cvtEntry : Int;
     cvt_dist : TT_F26dot6;
     distance : TT_F26dot6;
     cur_dist : TT_F26dot6;
     org_dist : TT_F26dot6;
   begin

     point    := args^[0];
     cvtEntry := args^[1];

     (* XXX : UNDOCUMENTED => cvt[-1] = 0 ???? *)

     if (args^[0] < 0 ) or (args^[0] >= pEC^.zp1.n_points) or
        (args^[1] < -1) or (args^[1] >= pEC^.cvtSize) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     if cvtEntry < 0 then
       cvt_dist := 0
     else
       cvt_dist := pEC^.func_read_cvt(cvtEntry);

     (* single width test *)

     if abs(cvt_dist) < pEC^.GS.single_width_cutin then

       if cvt_dist >= 0 then cvt_dist :=  pEC^.GS.single_width_value
                        else cvt_dist := -pEC^.GS.single_width_value;

     (* XXX : Undocumented - twilight zone *)

     if pEC^.GS.gep1 = 0 then   (* if in twilight zone *)
     begin
       pEC^.zp1.org^[point].x := pEC^.zp0.org^[pEC^.GS.rp0].x +
                                MulDiv_Round( cvt_dist,
                                              pEC^.GS.freeVector.x,
                                              $4000 );

       pEC^.zp1.org^[point].x := pEC^.zp0.org^[pEC^.GS.rp0].y +
                                MulDiv_Round( cvt_dist,
                                              pEC^.GS.freeVector.y,
                                              $4000 );

       pEC^.zp1.cur^[point] := pEC^.zp1.org^[point];
     end;


     org_dist := pEC^.func_dualProj( pEC^.zp1.org^[point],
                                    pEC^.zp0.org^[pEC^.GS.rp0] );

     cur_dist := pEC^.func_Project( pEC^.zp1.cur^[point],
                                   pEC^.zp0.cur^[pEC^.GS.rp0] );

     (* auto-flip test *)

     if pEC^.GS.auto_flip then
       if (org_dist xor cvt_dist < 0) then
         cvt_dist := -cvt_dist;

     (* control value cutin and round *)

     if opcode and 4 <> 0 then
       begin
         (* XXX : UNDOCUMENTED : only perform cut-in test when both *)
         (*       zone pointers refer to the points zone            *)

         if pEC^.GS.gep0 = pEC^.GS.gep1 then
           if abs( cvt_dist - org_dist ) >= pEC^.GS.control_value_cutin then
             cvt_dist := org_dist;

         distance := pEC^.func_round( cvt_dist,
                                     pEC^.metrics.compensations[ opcode and 3 ] );
       end
     else
       distance := Round_None( cvt_dist,
                               pEC^.metrics.compensations[ opcode and 3 ] );

     (* minimum distance test *)

     if opcode and 8 <> 0 then
     begin
       if org_dist >= 0 then

         if distance < pEC^.GS.minimum_distance then
           distance := pEC^.GS.minimum_distance
         else
       else
         if distance > -pEC^.GS.minimum_distance then
           distance := -pEC^.GS.minimum_distance;
     end;

     pEC^.func_move( @pEC^.zp1, point, distance - cur_dist );

     pEC^.GS.rp1 := pEC^.GS.rp0;

     if opcode and 16 <> 0 then pEC^.GS.rp0 := point;

    (* UNDOCUMENTED !! *)

     pEC^.GS.rp2 := point;
   end;

(**********************************************)
(* ALIGNRP[]   : ALIGN Relative Point         *)
(* CodeRange   : $3C                          *)

   procedure TInterpreter.Ins_ALIGNRP(args : PStorage );
   var
     point    : Int;
     distance : TT_F26dot6;
   begin
     if top < pEC^.GS.loop then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     while pEC^.GS.loop > 0 do
     begin

       dec( opargs );

       point := pEC^.stack^[ opargs ];

       if (point < 0) or (point >= pEC^.zp1.n_points) then
       begin
         pEC^.error := TT_Err_Invalid_Reference;
         exit;
       end;

       distance := pEC^.func_project( pEC^.zp1.cur^[point],
                                     pEC^.zp0.cur^[pEC^.GS.rp0] );

       pEC^.func_move( @pEC^.zp1, point, -distance );

       dec( pEC^.GS.loop );
     end;

     pEC^.GS.loop := 1;
     new_top := opargs;
   end;

(**********************************************)
(* AA[]        : Adjust Angle                 *)
(* CodeRange   : $7F                          *)

   procedure TInterpreter.Ins_AA( args : PStorage );
   begin
     (* Intentional - no longer supported *)
   end;

(**********************************************)
(* ISECT[]     : moves point to InterSECTion  *)
(* CodeRange   : $0F                          *)

   procedure TInterpreter.Ins_ISECT( args : PStorage );
   var
     point  : Int;
     a0, a1 : Int;
     b0, b1 : Int;

     discriminant : TT_F26dot6;
     dx,  dy,
     dax, day,
     dbx, dby     : TT_F26dot6;

     val : TT_F26dot6;

     R : TT_Vector;

   begin

     point := args^[0];
     a0    := args^[1];
     a1    := args^[2];
     b0    := args^[3];
     b1    := args^[4];

     if (b0 >= pEC^.zp0.n_points) or (b1 >= pEC^.zp0.n_points) or
        (a0 >= pEC^.zp1.n_points) or (a1 >= pEC^.zp1.n_points) or
        (point >= pEC^.zp0.n_points) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;
(*
     if   Normalize( pEC^.zp1.cur_x^[a1] - pEC^.zp1.cur_x^[a0],
                     pEC^.zp1.cur_y^[a1] - pEC^.zp1.cur_y^[a0],
                     U )
        and
          Normalize( - pEC^.zp0.cur_x^[b1] - pEC^.zp0.cur_x^[b0],
                       pEC^.zp0.cur_y^[b1] - pEC^.zp0.cur_y^[b0],
                       V )
       then
         begin

           dx := MulDiv_Round( pEC^.zp0.cur_x^[b0] -
                               pEC^.zp1.cur_x^[a0],
                               V.x,
                               $4000 ) +

                 MulDiv_Round( pEC^.zp0.cur_y^[b0] -
                               pEC^.zp1.cur_y^[a0],
                               V.y,
                               $4000 );

           dy := MulDiv_Round( U.x, V.x, $4000 ) +
                 MulDiv_Round( U.y, V.y, $4000 );

           if dy <> 0 then
           begin
             dx := MulDiv_Round( dx, $4000, dy );

             pEC^.zp2.flags^[point] := pEC^.zp2.flags^[point] or
                                      TT_Flag_Touched_Both;

             pEC^.zp2.cur_x^[point] := pEC^.zp1.cur_x^[a0] +

                      MulDiv_Round( dx, U.x, $4000 );

             pEC^.zp2.cur_y^[point] := pEC^.zp1.cur_y^[a0] +

                      MulDiv_Round( dx, U.y, $4000 );

             exit;
           end;
        end;
 *)
     dbx := pEC^.zp0.cur^[b1].x - pEC^.zp0.cur^[b0].x;
     dby := pEC^.zp0.cur^[b1].y - pEC^.zp0.cur^[b0].y;

     dax := pEC^.zp1.cur^[a1].x - pEC^.zp1.cur^[a0].x;
     day := pEC^.zp1.cur^[a1].y - pEC^.zp1.cur^[a0].y;

     dx := pEC^.zp0.cur^[b0].x - pEC^.zp1.cur^[a0].x;
     dy := pEC^.zp0.cur^[b0].y - pEC^.zp1.cur^[a0].y;

     pEC^.zp2.flags^[point] := pEC^.zp2.flags^[point] or
                              TT_Flag_Touched_Both;

     discriminant := MulDiv( dax, -dby, $40 ) +
                     MulDiv( day,  dbx, $40 );

     if abs(discriminant) >= $40 then
       begin

         val := MulDiv( dx, -dby, $40 ) +
                MulDiv( dy,  dbx, $40 );

         R.x := MulDiv( val, dax, discriminant );
         R.y := MulDiv( val, day, discriminant );

         pEC^.zp2.cur^[point].x := pEC^.zp1.cur^[a0].x + R.x;
         pEC^.zp2.cur^[point].y := pEC^.zp1.cur^[a0].y + R.y;
       end
     else
       begin

         (* else, take the middle of the middles of A and B *)

         pEC^.zp2.cur^[point].x := ( pEC^.zp1.cur^[a0].x +
                                    pEC^.zp1.cur^[a1].x +
                                    pEC^.zp0.cur^[b0].x +
                                    pEC^.zp0.cur^[b1].x ) div 4;

         pEC^.zp2.cur^[point].y := ( pEC^.zp1.cur^[a0].y +
                                    pEC^.zp1.cur^[a1].y +
                                    pEC^.zp0.cur^[b0].y +
                                    pEC^.zp0.cur^[b1].y ) div 4;
       end;
   end;

(**********************************************)
(* ALIGNPTS[]  : ALIGN PoinTS                 *)
(* CodeRange   : $27                          *)

   procedure TInterpreter.Ins_ALIGNPTS( args : PStorage );
   var
     p1, p2   : Int;
     distance : TT_F26dot6;
   begin
     p1 := args^[0];
     p2 := args^[1];

     if (args^[0] < 0) or (args^[0] >= pEC^.zp1.n_points) or
        (args^[1] < 0) or (args^[1] >= pEC^.zp0.n_points) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     distance := pEC^.func_project( pEC^.zp0.cur^[p2],
                                   pEC^.zp1.cur^[p1] ) div 2;

     pEC^.func_move( @pEC^.zp1, p1, distance );
     pEC^.func_move( @pEC^.zp0, p2, -distance );
   end;

(**********************************************)
(* IP[]        : Interpolate Point            *)
(* CodeRange   : $39                          *)

   procedure TInterpreter.Ins_IP( args : PStorage );
   var
     org_a : TT_F26dot6;
     org_b : TT_F26dot6;
     org_x : TT_F26dot6;
     cur_a : TT_F26dot6;
     cur_b : TT_F26dot6;
     cur_x : TT_F26dot6;

     distance : TT_F26dot6;

     point     : Int;
   begin

     if top < pEC^.GS.loop then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     org_a := pEC^.func_dualProj( pEC^.zp0.org^[pEC^.GS.rp1], Null_Vector );

     org_b := pEC^.func_dualProj( pEC^.zp1.org^[pEC^.GS.rp2], Null_Vector );

     cur_a := pEC^.func_project( pEC^.zp0.cur^[pEC^.GS.rp1], Null_Vector );

     cur_b := pEC^.func_project( pEC^.zp1.cur^[pEC^.GS.rp2], Null_Vector );

     while pEC^.GS.loop > 0 do
     begin

       dec( opargs );

       point := pEC^.stack^[ opargs ];

       org_x := pEC^.func_dualProj( pEC^.zp2.org^[point], Null_Vector );

       cur_x := pEC^.func_project( pEC^.zp2.cur^[point], Null_Vector );

       if (( org_a <= org_b ) and ( org_x <= org_a )) or
          (( org_a >  org_b ) and ( org_x >= org_a )) then
         begin
           distance := ( cur_a - org_a ) + ( org_x - cur_x );
         end
       else
       if (( org_a <= org_b ) and ( org_x >= org_b )) or
          (( org_a >  org_b ) and ( org_x <  org_b )) then
         begin
           distance := ( cur_b - org_b ) + ( org_x - cur_x );
         end
       else
         begin
           (* note : it seems that rounding this value isn't a good *)
           (*        idea ( width of capital 'S' in Times           *)

           distance := MulDiv( cur_b - cur_a,
                               org_x - org_a,
                               org_b - org_a ) + ( cur_a - cur_x );
         end;

       pEC^.func_move( @pEC^.zp2, point, distance );

       dec( pEC^.GS.loop );
     end;

     pEC^.GS.loop := 1;
     new_top := opargs;
   end;

(**********************************************)
(* UTP[a]      : UnTouch Point                *)
(* CodeRange   : $29                          *)

   procedure TInterpreter.Ins_UTP( args : PStorage );
   var
     mask : Byte;
   begin
     if (args^[0] < 0) or (args^[0] >= pEC^.zp0.n_points) then
     begin
       pEC^.error := TT_Err_Invalid_Reference;
       exit;
     end;

     mask := $FF;

     if pEC^.GS.freeVector.x <> 0 then mask := mask and not TT_Flag_Touched_X;
     if pEC^.GS.freeVector.y <> 0 then mask := mask and not TT_Flag_Touched_Y;

     pEC^.zp0.flags^[args^[0]] := pEC^.zp0.flags^[args^[0]] and mask;
   end;

(**********************************************)
(* IUP[a]      : Interpolate Untouched Points *)
(* CodeRange   : $30-$31                      *)

   procedure TInterpreter.Ins_IUP( args : PStorage );
   var
     mask : byte;

     first_point,    (* first point of contour        *)
     end_point,      (* end point (last+1) of contour *)

     first_touched,  (* first touched point in contour   *)
     cur_touched,    (* current touched point in contour *)

     point,          (* current point   *)
     contour : Int;  (* current contour *)

     orgs,              (* original and current coordinate *)
     curs  : TT_Points; (* arrays                          *)

     procedure Shift_X( p1, p2, p : Int );
     var
       i : Int;
       x : TT_F26dot6;
     begin
       x := curs^[p].x - orgs^[p].x;

       for i := p1 to p-1 do inc( curs^[i].x, x );
       for i := p+1 to p2 do inc( curs^[i].x, x );
     end;

     procedure Shift_Y( p1, p2, p : Int );
     var
       i : Int;
       y : TT_F26dot6;
     begin
       y := curs^[p].y - orgs^[p].y;

       for i := p1 to p-1 do inc( curs^[i].y, y );
       for i := p+1 to p2 do inc( curs^[i].y, y );
     end;


     procedure Interp_X( p1, p2, ref1, ref2 : Int );
     var
       i                 : Int;
       x, x1, x2, d1, d2 : TT_F26dot6;
     begin

       if p1 > p2 then exit;

       x1 := orgs^[ref1].x;  d1 := curs^[ref1].x - orgs^[ref1].x;
       x2 := orgs^[ref2].x;  d2 := curs^[ref2].x - orgs^[ref2].x;

       if x1 = x2 then
         for i := p1 to p2 do
         begin
           x := orgs^[i].x;
           if x <= x1 then x := x + d1
                      else x := x + d2;

           curs^[i].x := x;
         end

       else
       if x1 < x2 then

         for i := p1 to p2 do
         begin
           x := orgs^[i].x;

           if (x <= x1) then x := x + d1
           else
           if (x >= x2) then x := x + d2
           else
             x := curs^[ref1].x +
                  MulDiv( x-x1, curs^[ref2].x-curs^[ref1].x, x2-x1 );

           curs^[i].x := x;
         end
       else

         (* x2 < x1 *)

         for i := p1 to p2 do
         begin
           x := orgs^[i].x;

           if ( x <= x2 ) then x := x + d2
           else
           if ( x >= x1 ) then x := x + d1
           else
             x := curs^[ref1].x +
                  MulDiv( x-x1, curs^[ref2].x-curs^[ref1].x, x2-x1 );

           curs^[i].x := x;
         end;
     end;

     procedure Interp_Y( p1, p2, ref1, ref2 : Int );
     var
       i                 : Int;
       y, y1, y2, d1, d2 : TT_F26dot6;
     begin

       if p1 > p2 then exit;

       y1 := orgs^[ref1].y;  d1 := curs^[ref1].y - orgs^[ref1].y;
       y2 := orgs^[ref2].y;  d2 := curs^[ref2].y - orgs^[ref2].y;

       if y1 = y2 then
         for i := p1 to p2 do
         begin
           y := orgs^[i].y;
           if y <= y1 then y := y + d1
                      else y := y + d2;

           curs^[i].y := y;
         end

       else
       if y1 < y2 then

         for i := p1 to p2 do
         begin
           y := orgs^[i].y;

           if (y <= y1) then y := y + d1
           else
           if (y >= y2) then y := y + d2
           else
             y := curs^[ref1].y +
                  MulDiv( y-y1, curs^[ref2].y-curs^[ref1].y, y2-y1 );

           curs^[i].y := y;
         end
       else

         (* y2 < y1 *)

         for i := p1 to p2 do
         begin
           y := orgs^[i].y;

           if ( y <= y2 ) then y := y + d2
           else
           if ( y >= y1 ) then y := y + d1
           else
             y := curs^[ref1].y +
                  MulDiv( y-y1, curs^[ref2].y-curs^[ref1].y, y2-y1 );

           curs^[i].y := y;
         end;
     end;

   begin
     orgs := pEC^.pts.org;
     curs := pEC^.pts.cur;

     case opcode and 1 of
       0 : mask := TT_Flag_Touched_Y;
       1 : mask := TT_Flag_Touched_X;
     end;

     with pEC^ do
     begin

       contour := 0;
       point   := 0;

       repeat

         end_point   := pts.conEnds^[contour];
         first_point := point;

         while ( point <= end_point          ) and
               ( pts.flags^[point] and mask = 0 ) do  inc(point);

         if point <= end_point then
         begin

           first_touched := point;
           cur_touched   := point;

           inc( point );

           while ( point <= end_point ) do
           begin
             if pts.flags^[point] and mask <> 0 then
             begin
               if opcode and 1 <> 0 then
                 Interp_X( cur_touched+1, point-1, cur_touched, point )
               else
                 Interp_Y( cur_touched+1, point-1, cur_touched, point );

               cur_touched := point;
             end;

             inc( point );
           end;

           if cur_touched = first_touched then
             if opcode and 1 <> 0 then
               Shift_X( first_point, end_point, cur_touched )
             else
               Shift_Y( first_point, end_point, cur_touched )
           else
             begin
               if opcode and 1 <> 0 then
               begin
                 interp_x( cur_touched+1, end_point,   cur_touched, first_touched );
                 interp_x( first_point, first_touched-1, cur_touched, first_touched );
               end
               else
               begin
                 interp_y( cur_touched+1, end_point,   cur_touched, first_touched );
                 interp_y( first_point, first_touched-1, cur_touched, first_touched );
               end;
             end;

         end;

         inc( contour );

       until contour >= pts.n_contours;

     end;

   end;

(**********************************************)
(* DELTAPn[]   : DELTA Exceptions P1, P2, P3  *)
(* CodeRange   : $5D,$71,$72                  *)

   procedure TInterpreter.Ins_DELTAP( args : PStorage );
   var
     nump    : Int;
     k       : Int;
     A, B, C :Int;
   begin

     nump := args^[0];

     for K := 1 to nump do
       begin
         if opargs < 2 then
         begin
           pEC^.error := TT_Err_Too_Few_Arguments;
           exit;
         end;

         dec( opargs, 2 );

         A := pEC^.stack^[opargs+1];
         B := pEC^.stack^[ opargs ];

         (* XXX :                                              *)
         (* some commonly fonts have broke programs where the  *)
         (* the point reference has an invalid value. Here, we *)
         (* simply ignore them, because a DeltaP won't change  *)
         (* a glyph shape dramatically..                       *)
         (*                                                    *)

         if A < pEC^.zp0.n_points then
         begin
           C := ( B and $F0 ) shr 4;

           Case opcode of
             $5D : ;
             $71 : C := C+16;
             $72 : C := C+32;
            end;

           C := C + pEC^.GS.delta_Base;

           if GET_Ppem = C then
             begin
               B := (B and $F) - 8;
               if B >= 0 then B := B+1;
               B := ( B*64 ) div ( 1 shl pEC^.GS.delta_Shift );

               pEC^.func_move( @pEC^.zp0, A, B );
             end;
         end;

       end;

     new_top := opargs;
   end;


(**********************************************)
(* DELTACn[]   : DELTA Exceptions C1, C2, C3  *)
(* CodeRange   : $73,$74,$75                  *)

   procedure TInterpreter.Ins_DELTAC( args : PStorage );
   var
     nump    : Int;
     k       : Int;
     A, B, C :Int;
   begin

     nump := args^[0];

     for K := 1 to nump do
       begin
         if opargs < 2 then
         begin
           pEC^.error := TT_Err_Too_Few_Arguments;
           exit;
         end;

         dec( opargs, 2 );

         A := pEC^.stack^[opargs+1];
         B := pEC^.stack^[ opargs ];

         if A >= pEC^.cvtSize then
         begin
           pEC^.error := TT_Err_Invalid_Reference;
           exit;
         end;

         C := ( B and $F0 ) shr 4;

         Case opcode of
           $73 : ;
           $74 : C := C+16;
           $75 : C := C+32;
          end;

         C := C + pEC^.GS.delta_Base;

         if GET_Ppem = C then
           begin
             B := (B and $F) - 8;
             if B >= 0 then B := B+1;
             B := ( B*64 ) div ( 1 shl pEC^.GS.delta_Shift );

             pEC^.func_move_cvt( A, B );
           end;
       end;

     new_top := opargs;
   end;

(****************************************************************)
(*                                                              *)
(* MISC. INSTRUCTIONS                                           *)
(*                                                              *)
(****************************************************************)

(***********************************************************)
(* DEBUG[]     : DEBUG. Unsupported                        *)
(* CodeRange   : $4F                                       *)

(* NOTE : The original instruction pops a value from the stack *)

   procedure TInterpreter.Ins_DEBUG( args : PStorage );
   begin
     pEC^.error := TT_Err_Debug_Opcode;
   end;

(**********************************************)
(* GETINFO[]   : GET INFOrmation              *)
(* CodeRange   : $88                          *)

   procedure TInterpreter.Ins_GETINFO( args : PStorage );
   var
     K : Int;
   begin
     K := 0;

     if args^[0] and 1 <> 0 then K := 3;
     (* We return then Windows 3.1 version number *)
     (* for the font scaler                       *)

     if false then {%H-}K := K or $80;
     (* Has the glyph been rotated ? *)
     (* XXXX TO DO *)

     if false then {%H-}K := K or $100;
     (* Has the glyph been stretched ? *)
     (* XXXX TO DO *)

     args^[0] := K;
   end;


   procedure TInterpreter.Ins_UNKNOWN( args : PStorage );
   begin
     pEC^.error := TT_Err_Invalid_Opcode;
   end;

function TInterpreter.GetLastInstruction: string;
begin
  result := Instruct_Dispatch[opcode].name;
end;

  constructor TInterpreter.Create(AContext: PExec_Context; AEnableLog: boolean);
  var numIns: integer;
    procedure addIns(AName: string; AFunc: TInstruction_Function);
    begin
      if numIns < high(Instruct_Dispatch)+1 then
      begin
        with Instruct_Dispatch[numIns] do
        begin
          name := AName;
          func := AFunc;
        end;
        inc(numIns);
      end else
        raise exception.Create('Too much instructions');
    end;

  begin
    pEC := AContext;

    enableLog:= AEnableLog;
    if enableLog then instructionLog := TStringList.Create;

    numIns := low(Instruct_Dispatch);
    addIns('SVTCA  y', Ins_SVTCA);
    addIns('SVTCA  x', Ins_SVTCA);
    addIns('SPvTCA y', Ins_SPVTCA);
    addIns('SPvTCA x', Ins_SPVTCA);
    addIns('SFvTCA y', Ins_SFVTCA);
    addIns('SFvTCA x', Ins_SFVTCA);
    addIns('SPvTL //', Ins_SPVTL);
    addIns('SPvTL +', Ins_SPVTL);
    addIns('SFvTL //', Ins_SFVTL);
    addIns('SFvTL +', Ins_SFVTL);
    addIns('SPvFS', Ins_SPVFS);
    addIns('SFvFS', Ins_SFVFS);
    addIns('GPV', Ins_GPV);
    addIns('GFV', Ins_GFV);
    addIns('SFvTPv', Ins_SFVTPV);
    addIns('ISECT', Ins_ISECT);

    addIns('SRP0', Ins_SRP0);
    addIns('SRP1', Ins_SRP1);
    addIns('SRP2', Ins_SRP2);
    addIns('SZP0', Ins_SZP0);
    addIns('SZP1', Ins_SZP1);
    addIns('SZP2', Ins_SZP2);
    addIns('SZPS', Ins_SZPS);
    addIns('SLOOP', Ins_SLOOP);
    addIns('RTG', Ins_RTG);
    addIns('RTHG', Ins_RTHG);
    addIns('SMD', Ins_SMD);
    addIns('ELSE', Ins_ELSE);
    addIns('JMPR', Ins_JMPR);
    addIns('SCvTCi', Ins_SCVTCI);
    addIns('SSwCi', Ins_SSWCI);
    addIns('SSW', Ins_SSW);

    addIns('DUP', Ins_DUP);
    addIns('POP', Ins_POP);
    addIns('CLEAR', Ins_CLEAR);
    addIns('SWAP', Ins_SWAP);
    addIns('DEPTH', Ins_DEPTH);
    addIns('CINDEX', Ins_CINDEX);
    addIns('MINDEX', Ins_MINDEX);
    addIns('AlignPTS', Ins_ALIGNPTS);
    addIns('INS_$28', Ins_UNKNOWN);
    addIns('UTP', Ins_UTP);
    addIns('LOOPCALL', Ins_LOOPCALL);
    addIns('CALL', Ins_CALL);
    addIns('FDEF', Ins_FDEF);
    addIns('ENDF', Ins_ENDF);
    addIns('MDAP[0]', Ins_MDAP);
    addIns('MDAP[1]', Ins_MDAP);

    addIns('IUP[0]', Ins_IUP);
    addIns('IUP[1]', Ins_IUP);
    addIns('SHP[0]', Ins_SHP);
    addIns('SHP[1]', Ins_SHP);
    addIns('SHC[0]', Ins_SHC);
    addIns('SHC[1]', Ins_SHC);
    addIns('SHZ[0]', Ins_SHZ);
    addIns('SHZ[1]', Ins_SHZ);
    addIns('SHPIX', Ins_SHPIX);
    addIns('IP', Ins_IP);
    addIns('MSIRP[0]', Ins_MSIRP);
    addIns('MSIRP[1]', Ins_MSIRP);
    addIns('AlignRP', Ins_ALIGNRP);
    addIns('RTDG', Ins_RTDG);
    addIns('MIAP[0]', Ins_MIAP);
    addIns('MIAP[1]', Ins_MIAP);

    addIns('NPushB', Ins_NPUSHB);
    addIns('NPushW', Ins_NPUSHW);
    addIns('WS', Ins_WS);
    addIns('RS', Ins_RS);
    addIns('WCvtP', Ins_WCVTP);
    addIns('RCvt', Ins_RCVT);
    addIns('GC[0]', Ins_GC);
    addIns('GC[1]', Ins_GC);
    addIns('SCFS', Ins_SCFS);
    addIns('MD[0]', Ins_MD);
    addIns('MD[1]', Ins_MD);
    addIns('MPPEM', Ins_MPPEM);
    addIns('MPS', Ins_MPS);
    addIns('FlipON', Ins_FLIPON);
    addIns('FlipOFF', Ins_FLIPOFF);
    addIns('DEBUG', Ins_DEBUG);

    addIns('LT', Ins_LT);
    addIns('LTEQ', Ins_LTEQ);
    addIns('GT', Ins_GT);
    addIns('GTEQ', Ins_GTEQ);
    addIns('EQ', Ins_EQ);
    addIns('NEQ', Ins_NEQ);
    addIns('ODD', Ins_ODD);
    addIns('EVEN', Ins_EVEN);
    addIns('IF', Ins_IF);
    addIns('EIF', Ins_EIF);
    addIns('AND', Ins_AND);
    addIns('OR', Ins_OR);
    addIns('NOT', Ins_NOT);
    addIns('DeltaP1', Ins_DELTAP);
    addIns('SDB', Ins_SDB);
    addIns('SDS', Ins_SDS);

    addIns('ADD', Ins_ADD);
    addIns('SUB', Ins_SUB);
    addIns('DIV', Ins_DIV);
    addIns('MUL', Ins_MUL);
    addIns('ABS', Ins_ABS);
    addIns('NEG', Ins_NEG);
    addIns('FLOOR', Ins_FLOOR);
    addIns('CEILING', Ins_CEILING);
    addIns('ROUND[0]', Ins_ROUND);
    addIns('ROUND[1]', Ins_ROUND);
    addIns('ROUND[2]', Ins_ROUND);
    addIns('ROUND[3]', Ins_ROUND);
    addIns('NROUND[0]', Ins_ROUND);
    addIns('NROUND[1]', Ins_ROUND);
    addIns('NROUND[2]', Ins_ROUND);
    addIns('NROUND[3]', Ins_ROUND);

    addIns('WCvtF', Ins_WCVTF);
    addIns('DeltaP2', Ins_DELTAP);
    addIns('DeltaP3', Ins_DELTAP);
    addIns('DeltaCn[0]', Ins_DELTAC);
    addIns('DeltaCn[1]', Ins_DELTAC);
    addIns('DeltaCn[2]', Ins_DELTAC);
    addIns('SROUND', Ins_SROUND);
    addIns('S45Round', Ins_S45ROUND);
    addIns('JROT', Ins_JROT);
    addIns('JROF', Ins_JROF);
    addIns('ROFF', Ins_ROFF);
    addIns('INS_$7B', Ins_UNKNOWN);
    addIns('RUTG', Ins_RUTG);
    addIns('RDTG', Ins_RDTG);
    addIns('SANGW', Ins_SANGW);
    addIns('AA', Ins_AA);

    addIns('FlipPT', Ins_FLIPPT);
    addIns('FlipRgON', Ins_FLIPRGON);
    addIns('FlipRgOFF', Ins_FLIPRGOFF);
    addIns('INS_$83', Ins_UNKNOWN);
    addIns('INS_$84', Ins_UNKNOWN);
    addIns('ScanCTRL', Ins_SCANCTRL);
    addIns('SDPVTL[0]', Ins_SDPVTL);
    addIns('SDPVTL[1]', Ins_SDPVTL);
    addIns('GetINFO', Ins_GETINFO);
    addIns('IDEF', Ins_IDEF);
    addIns('ROLL', Ins_ROLL);
    addIns('MAX', Ins_MAX);
    addIns('MIN', Ins_MIN);
    addIns('ScanTYPE', Ins_SCANTYPE);
    addIns('InstCTRL', Ins_INSTCTRL);
    addIns('INS_$8F', Ins_UNKNOWN);

    addIns('INS_$90', Ins_UNKNOWN);
    addIns('INS_$91', Ins_UNKNOWN);
    addIns('INS_$92', Ins_UNKNOWN);
    addIns('INS_$93', Ins_UNKNOWN);
    addIns('INS_$94', Ins_UNKNOWN);
    addIns('INS_$95', Ins_UNKNOWN);
    addIns('INS_$96', Ins_UNKNOWN);
    addIns('INS_$97', Ins_UNKNOWN);
    addIns('INS_$98', Ins_UNKNOWN);
    addIns('INS_$99', Ins_UNKNOWN);
    addIns('INS_$9A', Ins_UNKNOWN);
    addIns('INS_$9B', Ins_UNKNOWN);
    addIns('INS_$9C', Ins_UNKNOWN);
    addIns('INS_$9D', Ins_UNKNOWN);
    addIns('INS_$9E', Ins_UNKNOWN);
    addIns('INS_$9F', Ins_UNKNOWN);

    addIns('INS_$A0', Ins_UNKNOWN);
    addIns('INS_$A1', Ins_UNKNOWN);
    addIns('INS_$A2', Ins_UNKNOWN);
    addIns('INS_$A3', Ins_UNKNOWN);
    addIns('INS_$A4', Ins_UNKNOWN);
    addIns('INS_$A5', Ins_UNKNOWN);
    addIns('INS_$A6', Ins_UNKNOWN);
    addIns('INS_$A7', Ins_UNKNOWN);
    addIns('INS_$A8', Ins_UNKNOWN);
    addIns('INS_$A9', Ins_UNKNOWN);
    addIns('INS_$AA', Ins_UNKNOWN);
    addIns('INS_$AB', Ins_UNKNOWN);
    addIns('INS_$AC', Ins_UNKNOWN);
    addIns('INS_$AD', Ins_UNKNOWN);
    addIns('INS_$AE', Ins_UNKNOWN);
    addIns('INS_$AF', Ins_UNKNOWN);

    addIns('PushB[0]', Ins_PUSHB);
    addIns('PushB[1]', Ins_PUSHB);
    addIns('PushB[2]', Ins_PUSHB);
    addIns('PushB[3]', Ins_PUSHB);
    addIns('PushB[4]', Ins_PUSHB);
    addIns('PushB[5]', Ins_PUSHB);
    addIns('PushB[6]', Ins_PUSHB);
    addIns('PushB[7]', Ins_PUSHB);
    addIns('PushW[0]', Ins_PUSHW);
    addIns('PushW[1]', Ins_PUSHW);
    addIns('PushW[2]', Ins_PUSHW);
    addIns('PushW[3]', Ins_PUSHW);
    addIns('PushW[4]', Ins_PUSHW);
    addIns('PushW[5]', Ins_PUSHW);
    addIns('PushW[6]', Ins_PUSHW);
    addIns('PushW[7]', Ins_PUSHW);

    addIns('MDRP[00]', Ins_MDRP);
    addIns('MDRP[01]', Ins_MDRP);
    addIns('MDRP[02]', Ins_MDRP);
    addIns('MDRP[03]', Ins_MDRP);
    addIns('MDRP[04]', Ins_MDRP);
    addIns('MDRP[05]', Ins_MDRP);
    addIns('MDRP[06]', Ins_MDRP);
    addIns('MDRP[07]', Ins_MDRP);
    addIns('MDRP[08]', Ins_MDRP);
    addIns('MDRP[09]', Ins_MDRP);
    addIns('MDRP[10]', Ins_MDRP);
    addIns('MDRP[11]', Ins_MDRP);
    addIns('MDRP[12]', Ins_MDRP);
    addIns('MDRP[13]', Ins_MDRP);
    addIns('MDRP[14]', Ins_MDRP);
    addIns('MDRP[15]', Ins_MDRP);
    addIns('MDRP[16]', Ins_MDRP);
    addIns('MDRP[17]', Ins_MDRP);

    addIns('MDRP[18]', Ins_MDRP);
    addIns('MDRP[19]', Ins_MDRP);
    addIns('MDRP[20]', Ins_MDRP);
    addIns('MDRP[21]', Ins_MDRP);
    addIns('MDRP[22]', Ins_MDRP);
    addIns('MDRP[23]', Ins_MDRP);
    addIns('MDRP[24]', Ins_MDRP);
    addIns('MDRP[25]', Ins_MDRP);
    addIns('MDRP[26]', Ins_MDRP);
    addIns('MDRP[27]', Ins_MDRP);
    addIns('MDRP[28]', Ins_MDRP);
    addIns('MDRP[29]', Ins_MDRP);
    addIns('MDRP[30]', Ins_MDRP);
    addIns('MDRP[31]', Ins_MDRP);

    addIns('MIRP[00]', Ins_MIRP);
    addIns('MIRP[01]', Ins_MIRP);
    addIns('MIRP[02]', Ins_MIRP);
    addIns('MIRP[03]', Ins_MIRP);
    addIns('MIRP[04]', Ins_MIRP);
    addIns('MIRP[05]', Ins_MIRP);
    addIns('MIRP[06]', Ins_MIRP);
    addIns('MIRP[07]', Ins_MIRP);
    addIns('MIRP[08]', Ins_MIRP);
    addIns('MIRP[09]', Ins_MIRP);
    addIns('MIRP[10]', Ins_MIRP);
    addIns('MIRP[11]', Ins_MIRP);
    addIns('MIRP[12]', Ins_MIRP);
    addIns('MIRP[13]', Ins_MIRP);
    addIns('MIRP[14]', Ins_MIRP);
    addIns('MIRP[15]', Ins_MIRP);

    addIns('MIRP[16]', Ins_MIRP);
    addIns('MIRP[17]', Ins_MIRP);
    addIns('MIRP[18]', Ins_MIRP);
    addIns('MIRP[19]', Ins_MIRP);
    addIns('MIRP[20]', Ins_MIRP);
    addIns('MIRP[21]', Ins_MIRP);
    addIns('MIRP[22]', Ins_MIRP);
    addIns('MIRP[23]', Ins_MIRP);
    addIns('MIRP[24]', Ins_MIRP);
    addIns('MIRP[25]', Ins_MIRP);
    addIns('MIRP[26]', Ins_MIRP);
    addIns('MIRP[27]', Ins_MIRP);
    addIns('MIRP[28]', Ins_MIRP);
    addIns('MIRP[29]', Ins_MIRP);
    addIns('MIRP[30]', Ins_MIRP);
    addIns('MIRP[31]', Ins_MIRP);

    if numIns <> high(Instruct_Dispatch)+1 then
      raise exception.Create('Missing instruction');
  end;

  destructor TInterpreter.Destroy;
begin
  instructionLog.Free;
  inherited Destroy;
end;

  function TInterpreter.Run: TError;
  label
    SuiteLabel, ErrorLabel, No_Error;
  var
    A : Int;
  begin
    top     := 0;
    callTop := 0;
    if enableLog then instructionLog.Clear;

    (* set cvt functions *)

    pEC^.metrics.ratio := 0;
    if pEC^.instance^.metrics.x_ppem <> pEC^.instance^.metrics.y_ppem then
      begin
        pEC^.func_read_cvt  := Read_CVT_Stretched;
        pEC^.func_write_cvt := Write_CVT_Stretched;
        pEC^.func_move_cvt  := Move_CVT_Stretched;
      end
    else
      begin
        pEC^.func_read_cvt  := Read_CVT;
        pEC^.func_write_cvt := Write_CVT;
        pEC^.func_move_cvt  := Move_CVT;
      end;
    Compute_Funcs;
    Compute_Round( pEC^.GS.round_state );

    repeat
      Calc_Length;

     (* First, let's check for empty stack and overflow *)

      opargs := top - Pop_Push_Count[ opcode*2 ];

     (* args is the top of the stack once arguments have been popped *)
     (* one can also see it as the index of the last argument        *)

      if opargs < 0 then
      begin
        pEC^.error := TT_Err_Too_Few_Arguments;
        goto ErrorLabel;
      end;

      new_top := opargs + Pop_Push_Count[ opcode*2+1 ];

     (* new_top  is the new top of the stack, after the instruction's *)
     (* execution. top will be set to new_top after the 'case'        *)

      if NeedStackSize(new_top) then goto ErrorLabel;

      pEC^.step_ins := true;
      pEC^.error    := TT_Err_Ok;

      if enableLog then instructionLog.Add('0x'+IntToHex(pEC^.IP,4)+': '+Instruct_Dispatch[ opcode ].name + ' (SP=' + IntToStr(top)+')');
      Instruct_Dispatch[ opcode ].func( PStorage(@pEC^.stack^[opargs]) );

      if pEC^.error <> TT_Err_Ok then
      begin

        case pEC^.error of

          TT_Err_Invalid_Opcode:  (* looking for redefined instructions *)

            begin
              A := 0;
              while ( A < pEC^.numIDefs ) do
                with pEC^.IDefs^[A] do

                  if Active and ( opcode = Opc ) then
                    begin
                      if callTop >= pEC^.callSize then
                        begin
                          pEC^.error := TT_Err_Invalid_Reference;
                          goto ErrorLabel;
                        end;

                      with pEC^.callstack^[callTop] do
                        begin
                          Caller_Range := pEC^.curRange;
                          Caller_IP    := pEC^.IP+1;
                          Cur_Count    := 1;
                          Cur_Restart  := Start;
                        end;

                      if not Goto_CodeRange( Range, Start ) then
                        goto ErrorLabel;

                      goto SuiteLabel;
                    end
                  else
                    inc(A);

                pEC^.error := TT_Err_Invalid_Opcode;
                goto ErrorLabel;

            end;
        else
          pEC^.error := pEC^.error;
          goto ErrorLabel;
        end;

      end;

      top := new_top;

      if pEC^.step_ins then inc( pEC^.IP, oplength );

  SuiteLabel:

      if (pEC^.IP >= pEC^.codeSize) then

       if callTop > 0 then
         begin
           pEC^.error := TT_Err_Code_Overflow;
           goto ErrorLabel;
         end
       else
         goto No_Error;

    until pEC^.instruction_trap;

  No_Error:
    result  := Success;
    exit;

  ErrorLabel:
    result  := Failure;

  end;

  function TInterpreter.NeedStackSize(AValue: integer): TError;
  var newSize: integer;
    newStack: PStorage;
  begin
    if AValue > pEC^.stackSize then
    begin
      if pEC^.stackSize < maxStackSizeAllowed then
      begin
        newSize := pEC^.stackSize*2+1;
        if newSize > maxStackSizeAllowed then newSize := maxStackSizeAllowed;
        newStack := nil;
        if Alloc( newStack, newSize*sizeof(Long) ) then
        begin //cannot allocate
          pEC^.error := TT_Err_Stack_Overflow;
          result := Failure;
        end;
        move(pEC^.stack^[0], newStack^[0], pEC^.stackSize*sizeof(Long) );
        TTMemory.Free( pEC^.stack );
        pEC^.stack := newStack;
        pEC^.stackSize := newSize;
        result := Success; //stack expanded
      end else
      begin
        //maximum allowed reached
        pEC^.error := TT_Err_Stack_Overflow;
        result := Failure;
      end;
    end else
      result := Success;
  end;

  function TInterpreter.NeedStackSize(AValue: integer;
    var APointerInStack: PStorage): TError;
  var APosInStack: integer;
  begin
    if (APointerInStack <> nil) then
    begin
      APosInStack:= System.PByte(APointerInStack) - System.PByte(pEC^.stack);
      result := NeedStackSize(AValue);
      APointerInStack := PStorage(System.PByte(pEC^.stack) + APosInStack);
    end else
      result := NeedStackSize(AValue);
  end;

  (****************************************************************)
  (*                                                              *)
  (*                    RUN                                       *)
  (*                                                              *)
  (*  This function executes a run of opcodes. It will exit       *)
  (*  in the following cases :                                    *)
  (*                                                              *)
  (*   - Errors ( in which case it returns FALSE )                *)
  (*                                                              *)
  (*   - Reaching the end of the main code range  (returns TRUE)  *)
  (*      reaching the end of a code range within a function      *)
  (*      call is an error.                                       *)
  (*                                                              *)
  (*   - After executing one single opcode, if the flag           *)
  (*     'Instruction_Trap' is set to TRUE. (returns TRUE)        *)
  (*                                                              *)
  (*  On exit whith TRUE, test IP < CodeSize to know wether it    *)
  (*  comes from a instruction trap or a normal termination       *)
  (*                                                              *)
  (*                                                              *)
  (*     Note : The documented DEBUG opcode pops a value from     *)
  (*            the stack. This behaviour is unsupported, here    *)
  (*            a DEBUG opcode is always an error.                *)
  (*                                                              *)
  (*                                                              *)
  (* THIS IS THE INTERPRETER'S MAIN LOOP                          *)
  (*                                                              *)
  (*  Instructions appear in the specs' order                     *)
  (*                                                              *)
  (****************************************************************)

  function Run_Ins( exec : PExec_Context; AErrorLog: boolean ) : TError;
  var interpreter: TInterpreter;
    logfile: TFileStream;
    procedure writelnLog(s: string);
    begin
      s+= LineEnding;
      logfile.WriteBuffer(s[1],length(s));
    end;

  begin
    if exec.interpreter = nil then
    begin
      interpreter := TInterpreter.Create(exec,AErrorLog);
      exec.interpreter := interpreter;
    end else
      interpreter := TInterpreter(exec.interpreter);
    result := interpreter.Run;
    if AErrorLog and result then
    begin
      logfile := TFileStream.Create('ttinterp.log',fmOpenWrite or fmCreate);
      writelnLog('----------------------- '+DateTimeToStr(Now));
      writelnLog('Error ' + IntToHex(exec.error,4) + ' on ' + interpreter.LastInstruction);
      writelnLog('Program:');
      interpreter.instructionLog.SaveToStream(logfile);
      writelnLog('-----------------------');
      logfile.Free;
    end;
  end;

end.