summaryrefslogtreecommitdiff
path: root/graphics/pstricks/contrib/luapstricks/luapstricks.lua
blob: 5482582d1532bbcb3fd075f272be1fc682d6818a (plain)
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
---- luapstricks.lua
-- Copyright 2021--2022 Marcel Krüger <tex@2krueger.de>
--
-- This work may be distributed and/or modified under the
-- conditions of the LaTeX Project Public License, either version 1.3
-- of this license or (at your option) any later version.
-- The latest version of this license is in
--   http://www.latex-project.org/lppl.txt
-- and version 1.3 or later is part of all distributions of LaTeX
-- version 2005/12/01 or later.
--
-- This work has the LPPL maintenance status `maintained'.
-- 
-- The Current Maintainer of this work is M. Krüger
--
-- This work consists of the file luapstricks.lua.

if luatexbase then
  luatexbase.provides_module {
    name = 'luapstricks',
    version = 'v0.6',
    date = '2022-09-10',
    description = 'PSTricks backend for LuaLaTeX',
  }
end

local setwhatsitfield = node.setwhatsitfield or node.setfield
local late_lua_sub = node.subtype'late_lua'

local pdfprint = vf.pdf -- Set later to have the right mode
local function gobble() end
local function no_pdfprint_allowed()
  pdfprint = gobble -- Don't warn more than once for each code block
  tex.error("luapstricks: Graphics in immediate code segment", {
      "There was an attempt to trigger drawing commands in an immediate code block. \z
      This isn't allowed and will therefore be ignored."
  })
end

local pi = math.pi
local two_pi = 2*pi
local pi2_inv = 2/pi
local pi3_inv = 3/pi

local sin_table = {0, 1, 0, -1}

local l = lpeg

local whitespace = (l.S'\0\t\n\r\f ' + '%' * (1-l.P'\n')^0 * (l.P'\n' + -1))^1

local regular = 1 - l.S'\0\t\n\r\f %()<>[]{}/'

local exitmarker = {}
local lookup

-- local integer = l.S'+-'^-1 * l.R'09'^1 / tonumber
local real = l.S'+-'^-1 * (l.R'09'^1 * ('.' * l.R'09'^0)^-1 + '.' * l.R'09'^1) * (l.S'Ee' * l.S'+-'^-1 * l.R'09'^1)^-1 / tonumber
local radix_scanner = setmetatable({}, {__index = function(t, b)
  local digit
  if b < 10 then
    digit = l.R('0' .. string.char(string.byte'0' + b - 1))
  else
    digit = l.R'09'
    if b > 10 then
      digit = digit + l.R('A' .. string.char(string.byte'A' + b - 11))
      digit = digit + l.R('a' .. string.char(string.byte'a' + b - 11))
    end
  end
  digit = l.C(digit^1) * l.Cp()
  t[b] = digit
  return digit
end})
local radix = l.Cmt(l.R'09' * l.R'09'^-1 / tonumber * '#', function(subj, pos, radix)
  if radix < 2 or radix > 36 then return end
  local digits, pos = radix_scanner[radix]:match(subj, pos)
  if not digits then return end
  digits = tonumber(digits, radix)
  return pos, digits
end)
local number = radix + real -- + integer -- Every integer is also a real

local str_view do
  local meta = {
    __index = function(s, k)
      if k == 'value' then
        return string.sub(s.base.value, s.offset, s.last)
      end
    end,
    __newindex = function(s, k, v)
      if k == 'value' then
        s.base.value = string.sub(s.base.value, 1, s.offset-1) .. v .. string.sub(s.base.value, s.last+1)
        return
      end
      -- We could do rawset here, but there is no reason for setting keys anyway
      assert(false)
    end,
  }
  function str_view(base, offset, length)
    if getmetatable(base) == meta then
      offset = offset + base.offset - 1
      base = base.base
    end
    return setmetatable({
      kind = 'string',
      base = base,
      offset = offset,
      last = offset + length - 1,
    }, meta)
  end
end

local string_patt do
  local literal = '(' * l.Cs(l.P{(
      l.Cg('\\' * (
          'n' * l.Cc'\n'
        + 'r' * l.Cc'\r'
        + 't' * l.Cc'\t'
        + 'b' * l.Cc'\b'
        + 'f' * l.Cc'\f'
        + '\\' * l.Cc'\\'
        + '(' * l.Cc'('
        + ')' * l.Cc')'
        + l.R'07' * l.R'07'^-2 / function(s) return string.char(tonumber(s, 8) % 0x100) end
        + ('\r' * l.P'\n'^-1 + '\n')^-1 * l.Cc''
      ))
    + l.Cg('\r' * l.P'\n'^-1 * l.Cc'\n')
    + (1-l.S'()')
    + '(' * l.V(1) * ')'
  )^0}) * ')'
  local hexchar = l.R('09', 'af', 'AF')
  local hexbyte = hexchar * hexchar^-1 / function(s)
    local b = tonumber(s, 16)
    return #s == 1 and 16*b or b
  end
  local hex = '<' * (hexbyte^0 / string.char) * '>'
  string_patt = literal + hex -- TODO: Base85 is not implemented
end

local name = l.C(regular^1 + l.S'[]' + '<<' + '>>')
local literal_name = '/' * l.C(regular^0)
local imm_name = '//' * l.C(regular^0)

-- All objects are literal by default, except names represented as direct strings and operators
local any_object = l.P{whitespace^-1 * (
    number * -regular
  + l.Ct(l.Cg(string_patt, 'value') * l.Cg(l.Cc'string', 'kind'))
  + imm_name / function(name) return lookup(name) end
  + l.Ct(l.Cg(literal_name, 'value') * l.Cg(l.Cc'name', 'kind'))
  + name
  + l.Ct(l.Cg(l.Ct(l.Cg('{' * l.Ct(l.V(1)^0) * whitespace^-1 * '}', 'value') * l.Cg(l.Cc'array', 'kind')), 'value') * l.Cg(l.Cc'executable', 'kind'))
)}
local object_list = l.Ct(any_object^0) * whitespace^-1 * (-1 + l.Cp())

local function parse_ps(s)
  local tokens, fail_offset = object_list:match(s)
  if fail_offset then
    error(string.format('Failed to parse PS tokens at `%s\'', s:sub(fail_offset)))
  end
  return tokens
end

local serialize_pdf do
  function serialize_pdf(obj)
    local t = type(obj)
    if t == 'number' then
      return string.format(math.type(obj) == 'float' and '%.5f' or '%i', obj)
    elseif t == 'boolean' then
      return obj and 'true' or 'false'
    elseif t == 'string' then
      return '/' .. obj
    elseif t == 'table' then
      t = obj.kind
      if t == 'name' then
        return '/' .. obj.value
      elseif t == 'string' then
        return '(' .. obj.value .. ')' -- TODO: Escaping
      elseif t == 'dict' then
        local helper = {}
        for k, v in next, obj.value do
          helper[#helper+1] = serialize_pdf(k)
          helper[#helper+1] = serialize_pdf(v)
        end
        return '<<' .. table.concat(helper, ' ') .. '>>'
      elseif t == 'array' then
        local helper = {}
        for i, v in ipairs(obj.value) do
          helper[i] = serialize_pdf(v)
        end
        return '[' .. table.concat(helper, ' ') .. ']'
      else
        error'Unable to serialize object'
      end
    end
    error'Unable to serialize object'
  end
end

local srand, rrand, rand do
  local state
  function srand(s)
    state = s//1
    if state < 1 then
      state = -(state % 0x7ffffffe) + 1
    elseif state > 0x7ffffffe then
      state = 0x7ffffffe
    end
  end
  function rrand()
    return state
  end
  function rand()
    state = (16807 * state) % 0x7fffffff
    -- if state <= 0 then
    --   state = state + 0x7fffffff
    -- end
    return state
  end
  srand(math.random(1, 0x7ffffffe))
end

local maybe_decompress do
  local compressed_pattern = '%!PS\n\z
    currentfile<</Predictor 1' * l.R'05' * '/Columns ' * (l.R'09'^1/tonumber) * '>>/FlateDecode filter cvx exec\n'
    * l.C(l.P(1)^1)

  local stacklimit = 999000

  function maybe_decompress(data)
    local columns, compressed = compressed_pattern:match(data)
    if not columns then return data end

    data = zlib.decompress(compressed)
    local bytes do
      local size = #data
      if size < stacklimit then
        bytes = {data:byte(1, -1)}
      else
        bytes = {}
        local off = 1
        for i = 1, size, stacklimit do
          table.move({data:byte(i, i+stacklimit-1)}, 1, stacklimit, i, bytes)
        end
      end
    end
    local new_data = {}
    local start_row = 1
    local out_row = 1
    while true do
      local control = bytes[start_row]
      if not control then break end
      if control == 0 or (control == 2 and start_row == 1) then
        table.move(bytes, start_row + 1, start_row + columns, out_row, new_data)
      elseif control == 1 then
        local last = bytes[start_row + 1]
        new_data[out_row] = last
        for i = 2, columns do
          last = (bytes[start_row + i] + last) & 0xFF
          new_data[out_row + i - 1] = last
        end
      elseif control == 2 then
        for i = 1, columns do
          new_data[out_row + i - 1] = (bytes[start_row + i] + new_data[out_row - columns - 1 + i]) & 0xFF
        end
      else
        error'Unimplemented'
      end
      start_row = start_row + columns + 1
      out_row = out_row + columns
    end
    local result = ''
    local size = #new_data
    for i = 1, size, stacklimit do
      result = result .. string.char(table.unpack(new_data, i, i + stacklimit > size and size or i + stacklimit - 1))
    end
    return result
  end
end

local font_aliases = {
  -- First add some help to find the TeX Gyre names under the corresponding URW font names
  ['NimbusRoman-Regular'] = 'kpse:texgyretermes-regular.otf',
  ['NimbusRoman-Italic'] = 'kpse:texgyretermes-italic.otf',
  ['NimbusRoman-Bold'] = 'kpse:texgyretermes-bold.otf',
  ['NimbusRoman-BoldItalic'] = 'kpse:texgyretermes-bolditalic.otf',

  ['NimbusSans-Regular'] = 'kpse:texgyreheros-regular.otf',
  ['NimbusSans-Italic'] = 'kpse:texgyreheros-italic.otf',
  ['NimbusSans-Bold'] = 'kpse:texgyreheros-bold.otf',
  ['NimbusSans-BoldItalic'] = 'kpse:texgyreheros-bolditalic.otf',

  ['NimbusSansNarrow-Regular'] = 'kpse:texgyreheroscn-regular.otf',
  ['NimbusSansNarrow-Oblique'] = 'kpse:texgyreheroscn-italic.otf',
  ['NimbusSansNarrow-Bold'] = 'kpse:texgyreheroscn-bold.otf',
  ['NimbusSansNarrow-BoldOblique'] = 'kpse:texgyreheroscn-bolditalic.otf',

  ['NimbusMonoPS-Regular'] = 'kpse:texgyrecursor-regular.otf',
  ['NimbusMonoPS-Italic'] = 'kpse:texgyrecursor-italic.otf',
  ['NimbusMonoPS-Bold'] = 'kpse:texgyrecursor-bold.otf',
  ['NimbusMonoPS-BoldItalic'] = 'kpse:texgyrecursor-bolditalic.otf',

  ['URWBookman-Light'] = 'kpse:texgyrebonum-regular.otf',
  ['URWBookman-LightItalic'] = 'kpse:texgyrebonum-italic.otf',
  ['URWBookman-Demi'] = 'kpse:texgyrebonum-bold.otf',
  ['URWBookman-DemiItalic'] = 'kpse:texgyrebonum-bolditalic.otf',

  ['URWGothic-Book'] = 'kpse:texgyreadventor-regular.otf',
  ['URWGothic-BookOblique'] = 'kpse:texgyreadventor-italic.otf',
  ['URWGothic-Demi'] = 'kpse:texgyreadventor-bold.otf',
  ['URWGothic-DemiOblique'] = 'kpse:texgyreadventor-bolditalic.otf',

  -- These fonts have weird names in their URW variant, so we use the standard font names directly instead.
  ['NewCenturySchlbk-Roman'] = 'kpse:texgyreschola-regular.otf',
  ['NewCenturySchlbk-Italic'] = 'kpse:texgyreschola-italic.otf',
  ['NewCenturySchlbk-Bold'] = 'kpse:texgyreschola-bold.otf',
  ['NewCenturySchlbk-BoldItalic'] = 'kpse:texgyreschola-bolditalic.otf',

  ['Palatino-Roman'] = 'kpse:texgyrepagella-regular.otf',
  ['Palatino-Italic'] = 'kpse:texgyrepagella-italic.otf',
  ['Palatino-Bold'] = 'kpse:texgyrepagella-bold.otf',
  ['Palatino-BoldItalic'] = 'kpse:texgyrepagella-bolditalic.otf',

  ['ZapfChancery-MediumItalic'] = 'kpse:texgyrechorus-mediumitalic.otf',

  -- The two symbol fonts don't have OpenType equivalents in TeX Live
  -- so we use TFM based fonts instead
  ['StandardSymbolsPS'] = 'usyr',
  ['Dingbats'] = 'uzdr',
}
-- Then map the standard 35 font names to the URW names as done by GhostScript
-- (Except for New Century Schoolbook which got mapped directly before.
for psname, remapped in next, {
  ['Times-Roman'] = 'NimbusRoman-Regular',
  ['Times-Italic'] = 'NimbusRoman-Italic',
  ['Times-Bold'] = 'NimbusRoman-Bold',
  ['Times-BoldItalic'] = 'NimbusRoman-BoldItalic',

  ['Helvetica'] = 'NimbusSans-Regular',
  ['Helvetica-Oblique'] = 'NimbusSans-Italic',
  ['Helvetica-Bold'] = 'NimbusSans-Bold',
  ['Helvetica-BoldOblique'] = 'NimbusSans-BoldItalic',

  ['Helvetica-Narrow'] = 'NimbusSansNarrow-Regular',
  ['Helvetica-Narrow-Oblique'] = 'NimbusSansNarrow-Oblique',
  ['Helvetica-Narrow-Bold'] = 'NimbusSansNarrow-Bold',
  ['Helvetica-Narrow-BoldOblique'] = 'NimbusSansNarrow-BoldOblique',

  ['Courier'] = 'NimbusMonoPS-Regular',
  ['Courier-Oblique'] = 'NimbusMonoPS-Italic',
  ['Courier-Bold'] = 'NimbusMonoPS-Bold',
  ['Courier-BoldOblique'] = 'NimbusMonoPS-BoldItalic',

  ['Bookman-Light'] = 'URWBookman-Light',
  ['Bookman-LightItalic'] = 'URWBookman-LightItalic',
  ['Bookman-Demi'] = 'URWBookman-Demi',
  ['Bookman-DemiItalic'] = 'URWBookman-DemiItalic',

  ['AvantGarde-Book'] = 'URWGothic-Book',
  ['AvantGarde-BookOblique'] = 'URWGothic-BookOblique',
  ['AvantGarde-Demi'] = 'URWGothic-Demi',
  ['AvantGarde-DemiOblique'] = 'URWGothic-DemiOblique',

  ['Symbol'] = 'StandardSymbolsPS',
  ['StandardSymL'] = 'StandardSymbolsPS',

  ['ZapfDingbats'] = 'Dingbats',

  -- Some additional names needed for PSTricks
  ['NimbusRomNo9L-Regu'] = 'NimbusRoman-Regular',
  ['NimbusRomNo9L-ReguItal'] = 'NimbusRoman-Italic',
  ['NimbusRomNo9L-Medi'] = 'NimbusRoman-Bold',
  ['NimbusRomNo9L-MediItal'] = 'NimbusRoman-BoldItalic',
  ['NimbusRomNo9L-Bold'] = 'NimbusRoman-Bold',

  ['NimbusSanL-Regu'] = 'NimbusSans-Regular',
  ['NimbusSanL-ReguItal'] = 'NimbusSans-Italic',
  ['NimbusSanL-Bold'] = 'NimbusSans-Bold',
  ['NimbusSanL-BoldItal'] = 'NimbusSans-BoldItalic',

  ['NimbusSanL-BoldCond'] = 'NimbusSansNarrow-Bold',
  ['NimbusSanL-BoldCondItal'] = 'NimbusSansNarrow-BoldOblique',
  ['NimbusSanL-ReguCond'] = 'NimbusSansNarrow-Regular',
  ['NimbusSanL-ReguCondItal'] = 'NimbusSansNarrow-Oblique',

  ['NimbusMonL-Regu'] = 'NimbusMonoPS-Regular',
  ['NimbusMonL-ReguObli'] = 'NimbusMonoPS-Italic',
  ['NimbusMonL-Bold'] = 'NimbusMonoPS-Bold',
  ['NimbusMonL-BoldObli'] = 'NimbusMonoPS-BoldItalic',

  ['URWBookmanL-DemiBoldItal'] = 'URWBookman-DemiItalic',
  ['URWBookmanL-DemiBold'] = 'URWBookman-Demi',
  ['URWBookmanL-LighItal'] = 'URWBookman-LightItalic',
  ['URWBookmanL-Ligh'] = 'URWBookman-Light',

  ['URWGothicL-BookObli'] = 'URWGothic-BookOblique',
  ['URWGothicL-Book'] = 'URWGothic-Book',
  ['URWGothicL-DemiObli'] = 'URWGothic-DemiOblique',
  ['URWGothicL-Demi'] = 'URWGothic-Demi',

  ['CenturySchL-Roma'] = 'NewCenturySchlbk-Roman',
  ['CenturySchL-Ital'] = 'NewCenturySchlbk-Italic',
  ['CenturySchL-Bold'] = 'NewCenturySchlbk-Bold',
  ['CenturySchL-BoldItal'] = 'NewCenturySchlbk-BoldItalic',

  ['URWPalladioL-Roma'] = 'Palatino-Roman',
  ['URWPalladioL-Ital'] = 'Palatino-Italic',
  ['URWPalladioL-Bold'] = 'Palatino-Bold',
  ['URWPalladioL-BoldItal'] = 'Palatino-BoldItalic',

  ['URWChanceryL-MediItal'] = 'ZapfChancery-MediumItalic',
} do
  font_aliases[psname] = font_aliases[remapped] or remapped
end

local operand_stack = {}

local pushs do
  local function helper(height, args, arg, ...)
    if args == 0 then return end
    height = height + 1
    operand_stack[height] = arg
    return helper(height, args - 1, ...)
  end
  function pushs(...)
    return helper(#operand_stack, select('#', ...), ...)
  end
end
local function push(value)
  operand_stack[#operand_stack+1] = value
end

local function ps_error(kind, ...)
  pushs(...)
  return error{pserror = kind, trace = debug.traceback()}
end

local function pop(...)
  local height = #operand_stack
  if height == 0 then
    return ps_error('stackunderflow', ...)
  end
  local v = operand_stack[height]
  operand_stack[height] = nil
  return v, v
end
local function pop_num(...)
  local raw = pop(...)
  local n = raw
  local tn = type(n)
  if tn == 'table' and n.kind == 'executable' then
    n = n.value
    tn = type(n)
  end
  if tn ~= 'number' then
    ps_error('typecheck', raw, ...)
  end
  return n, raw
end
local pop_int = pop_num
local function pop_proc(...)
  local v = pop()
  if type(v) ~= 'table' or v.kind ~= 'executable' or type(v.value) ~= 'table' or v.value.kind ~= 'array' then
    ps_error('typecheck', v, ...)
  end
  return v.value.value, v
end
local pop_bool = pop
local function pop_dict()
  local orig = pop()
  local dict = orig
  if type(dict) ~= 'table' then
    ps_error('typecheck', orig)
  end
  if dict.kind == 'executable' then
    dict = dict.value
    if type(dict) ~= 'table' then
      ps_error('typecheck', orig)
    end
  end
  if dict.kind ~= 'dict' then
    ps_error('typecheck', orig)
  end
  return dict.value, orig, dict
end
local function pop_array()
  local orig = pop()
  local arr = orig
  if type(arr) == 'table' and arr.kind == 'executable' then
    arr = arr.value
  end
  if type(arr) ~= 'table' or arr.kind ~= 'array' then
    ps_error('typecheck', orig)
  end
  return arr
end
local pop_string = pop
local function pop_key()
  local key = pop()
  if type(key) == 'table' then
    local kind = key.kind
    if kind == 'executable' then
      key = key.value
      if type(key) ~= 'table' then return key end
      kind = key.kind
    end
    if kind == 'string' or kind == 'name' or kind == 'operator' then
      key = key.value
    end
  end
  return key
end

local execute_ps, execute_tok

local dictionary_stack

-- About the bbox entry:
--   - If the bounding box is not currently tracked, it is set to nil
--   - Otherwise it's a linked list linked with the .next field. Every entry is a "matrix level"
--   - if .bbox[1] is nil, the current matrix level does not have a set bounding box yet
--   - Otherside it's {min_x, min_y, max_x, max_y}
--   - If a .bbox.matrix entry is present then it describes the matrix which should be applied before the bbox gets added to the next "matrix level"
local graphics_stack = {{
  matrix = {10, 0, 0, 10, 0, 0}, -- Chosen for consistency with GhostScript's pdfwrite. Must be the same as defaultmatrix
  bbox = nil,
  linewidth = nil,
  current_path = nil,
  current_point = nil,
  color = {},
  fillconstantalpha = 1,
  strokeconstantalpha = 1,
  alphaisshape = nil,
  blendmode = nil,
  linejoin = nil,
  linecap = nil,
  strokeadjust = nil,
  font = nil,
  dash = nil,
  saved_delayed = nil, -- nil if the `gsave` of this graphic state is not delayed
  flatness = 1,
  miterlimit = nil,
}}

local lua_node_lookup = setmetatable({}, {__mode = 'k'})
local char_width_storage -- Non nil only at the beginning of a Type 3 glyph. Used to export the width.
local ExtGStateCount = 0
local pdfdict_gput = token.create'pdfdict_gput:nnn'
if pdfdict_gput.cmdname == 'undefined_cs' then
  pdfdict_gput = nil
end
local lbrace = token.create(string.byte'{')
local rbrace = token.create(string.byte'}')
local ExtGState = setmetatable({}, {__index = pdfdict_gput and function(t, k)
  ExtGStateCount = ExtGStateCount + 1
  local name = 'PSExtG' .. ExtGStateCount
  tex.runtoks(function()
    tex.write(pdfdict_gput, lbrace, 'g__pdf_Core/Page/Resources/ExtGState', rbrace, lbrace, name, rbrace, lbrace, k, rbrace)
  end)
  ltx.__pdf.Page.Resources.ExtGState = true
  ltx.pdf.Page_Resources_gpush(tex.count.g_shipout_readonly_int)
  name = '/' .. name .. ' gs'
  t[k] = name
  return name
end or function()
  texio.write_nl"Extended graphic state modifications dropped since `pdfmanagement-testphase' is not loaded."
  return ''
end})

local write_shading do
  local ShadingCount = 0
  if pdfdict_gput then
    function write_shading(attr, data)
      local obj = pdf.obj{
        type = 'stream',
        immediate = false,
        attr = attr,
        string = data,
      }
      pdf.refobj(obj)
      ShadingCount = ShadingCount + 1
      local name = 'PSShad' .. ShadingCount
      local k = obj .. ' 0 R'
      tex.runtoks(function()
        tex.write(pdfdict_gput, lbrace, 'g__pdf_Core/Page/Resources/Shading', rbrace, lbrace, name, rbrace, lbrace, k, rbrace)
      end)
      ltx.__pdf.Page.Resources.Shading = true
      ltx.pdf.Page_Resources_gpush(tex.count.g_shipout_readonly_int)
      name = '/' .. name
      return name
    end
  else
    function write_shading()
      texio.write_nl"Extended graphic state modifications dropped since `pdfmanagement-testphase' is not loaded."
      return ''
    end
  end
end

local function matrix_transform(x, y, xx, xy, yx, yy, dx, dy)
  return x * xx + y * yx + dx, x * xy + y * yy + dy
end
local function matrix_invert(xx, xy, yx, yy, dx, dy)
  local determinante = xx*yy - xy*yx
  xx, xy, yx, yy = yy/determinante, -xy/determinante, -yx/determinante, xx/determinante
  dx, dy = - dx * xx - dy * yx, - dx * xy - dy * yy
  return xx, xy, yx, yy, dx, dy
end
local delayed = {
  text = {},
  matrix = {1, 0, 0, 1, 0, 0},
}
local function update_matrix(xx, xy, yx, yy, dx, dy)
  local matrix = graphics_stack[#graphics_stack].matrix
  matrix[1], matrix[2],
  matrix[3], matrix[4],
  matrix[5], matrix[6]
    = xx * matrix[1] + xy * matrix[3],             xx * matrix[2] + xy * matrix[4],
      yx * matrix[1] + yy * matrix[3],             yx * matrix[2] + yy * matrix[4],
      dx * matrix[1] + dy * matrix[3] + matrix[5], dx * matrix[2] + dy * matrix[4] + matrix[6]

  local delayed_matrix = delayed.matrix
  delayed_matrix[1], delayed_matrix[2],
  delayed_matrix[3], delayed_matrix[4],
  delayed_matrix[5], delayed_matrix[6]
    = xx * delayed_matrix[1] + xy * delayed_matrix[3],                     xx * delayed_matrix[2] + xy * delayed_matrix[4],
      yx * delayed_matrix[1] + yy * delayed_matrix[3],                     yx * delayed_matrix[2] + yy * delayed_matrix[4],
      dx * delayed_matrix[1] + dy * delayed_matrix[3] + delayed_matrix[5], dx * delayed_matrix[2] + dy * delayed_matrix[4] + delayed_matrix[6]

  local current_path = graphics_stack[#graphics_stack].current_path
  if not current_path then return end

  local determinante = xx*yy - xy*yx
  xx, xy, yx, yy, dx, dy = matrix_invert(xx, xy, yx, yy, dx, dy)
  local i=1
  while current_path[i] do
    local entry = current_path[i]
    if type(entry) == 'number' then
      local after = current_path[i+1]
      assert(type(after) == 'number')
      current_path[i], current_path[i+1] = xx * entry + yx * after + dx, xy * entry + yy * after + dy
      i = i+2
    else
      i = i+1
    end
  end
  local current_point = graphics_stack[#graphics_stack].current_point
  local x, y = current_point[1], current_point[2]
  current_point[1], current_point[2] = xx * x + yx * y + dx, xy * x + yy * y + dy
end

local function delayed_print(str)
  local delayed_text = delayed.text
  delayed_text[#delayed_text + 1] = str
end

local function reset_delayed(delayed)
  local delayed_matrix = delayed.matrix
  local delayed_text = delayed.text
  for i=1, #delayed_text do
    delayed_text[i] = nil
  end
  delayed_matrix[1], delayed_matrix[2],
  delayed_matrix[3], delayed_matrix[4],
  delayed_matrix[5], delayed_matrix[6] = 1, 0, 0, 1, 0, 0
end

local function flush_delayed_table(delayed, state, force_start)
  local delayed_matrix = delayed.matrix
  local delayed_text = delayed.text

  local cm_string = string.format('%.5f %.5f %.5f %.5f %.5f %.5f cm', delayed_matrix[1], delayed_matrix[2],
                                                                      delayed_matrix[3], delayed_matrix[4],
                                                                      delayed_matrix[5], delayed_matrix[6])
  if cm_string == "1.00000 0.00000 0.00000 1.00000 0.00000 0.00000 cm" then
    cm_string = nil
  else
    local bbox = state.bbox
    if bbox then
      state.bbox = { matrix = delayed_matrix, next = bbox }
      delayed.matrix = {} -- Will be initialized in reset_delayed
    end
  end

  -- Before flushing, make sure that the current graphics state has started.
  graphics_stack_height = graphics_stack_height or #graphics_stack
  local saved_delayed = state.saved_delayed
  if saved_delayed and(cm_string or delayed_text[1] or force_start) then 
    state.saved_delayed = nil
    pdfprint'q'
  end
  for i=1, #delayed_text do
    pdfprint(delayed_text[i])
  end
  if cm_string then
    pdfprint((cm_string:gsub('%.?0+ ', ' ')))
  end
  return reset_delayed(delayed)
end

local function flush_delayed(force_start)
  local pre_first_delayed_group
  for i = #graphics_stack, 1, -1 do
    if not graphics_stack[i].saved_delayed then
      pre_first_delayed_group = i
      break
    end
  end
  for i = pre_first_delayed_group, #graphics_stack-1 do
    flush_delayed_table(graphics_stack[i+1].saved_delayed, graphics_stack[i]) -- No need for force_start here
  end
  return flush_delayed_table(delayed, graphics_stack[#graphics_stack], force_start)
end

local function register_point_bbox(bbox, x, y)
  local min_x, min_y, max_x, max_y = bbox[1], bbox[2], bbox[3], bbox[4]
  if min_x then
    if x < min_x then
      bbox[1] = x
    elseif x > max_x then
      bbox[3] = x
    end
    if y < min_y then
      bbox[2] = y
    elseif y > max_y then
      bbox[4] = y
    end
  else
    bbox[1], bbox[2], bbox[3], bbox[4] = x, y, x, y
  end
end

-- Only call after flush_delayed
local function register_point(state, x, y)
  local bbox = state.bbox
  if not bbox then return end
  return register_point_bbox(bbox, x, y)
end

local function merge_bbox(bbox, after)
  if bbox[1] then
    local matrix = bbox.matrix
    if matrix then
      register_point_bbox(after, matrix_transform(bbox[1], bbox[2], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6]))
      register_point_bbox(after, matrix_transform(bbox[1], bbox[4], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6]))
      register_point_bbox(after, matrix_transform(bbox[3], bbox[2], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6]))
      register_point_bbox(after, matrix_transform(bbox[3], bbox[4], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6]))
    else
      register_point_bbox(after, bbox[1], bbox[2])
      register_point_bbox(after, bbox[3], bbox[4])
    end
  end
  return after
end

function drawarc(xc, yc, r, a1, a2)
  a1, a2 = math.rad(a1), math.rad(a2)
  local dx, dy = r*math.cos(a1), r*math.sin(a1)
  local x, y = xc + dx, yc + dy
  local segments = math.ceil(math.abs(a2-a1)*pi2_inv)
  local da = (a2-a1)/segments
  local state = graphics_stack[#graphics_stack]
  local current_path = state.current_path
  local i
  if current_path then
    i = #current_path + 1
    current_path[i], current_path[i+1], current_path[i+2] = x, y, 'l'
    i = i + 3
  else
    current_path = {x, y, 'm'}
    i = 4
    state.current_path = current_path
    state.current_point = {}
  end
  local factor = 4*math.tan(da/4)/3
  dx, dy = factor*dy, -factor*dx
  for _=1, segments do
    current_path[i], current_path[i+1] = x - dx, y - dy
    a1 = a1 + da
    dx, dy = r*math.cos(a1), r*math.sin(a1)
    x, y = xc + dx, yc + dy
    dx, dy = factor*dy, -factor*dx
    current_path[i+2], current_path[i+3] = x + dx, y + dy
    current_path[i+4], current_path[i+5] = x, y
    current_path[i+6] = 'c'
    i = i + 7
  end
  state.current_point[1], state.current_point[2] = x, y
end

local function try_lookup(name)
  for i = #dictionary_stack, 1, -1 do
    local dict = dictionary_stack[i]
    local value = dict.value[name]
    if value ~= nil then
      return value
    end
  end
end
function lookup(name)
  local result = try_lookup(name)
  if result == nil then
    return error(string.format('Unknown name %q', name))
  end
  return result
end
local function bind(proc)
  for i=1, #proc do
    local entry = proc[i]
    local tentry = type(entry)
    if tentry == 'table' and entry.kind == 'executable' and type(entry.value) == 'table' and entry.value.kind == 'array' then
      bind(entry.value.value)
    elseif tentry == 'string' then
      local res = try_lookup(entry)
      if type(res) == 'function' then
        proc[i] = res
      end
    end
  end
end

local subdivide, flatten do
  function subdivide(t, x0, y0, x1, y1, x2, y2, x3, y3)
    local mt = 1-t
    local x01, y01 = mt * x0 + t * x1, mt * y0 + t * y1
    local x12, y12 = mt * x1 + t * x2, mt * y1 + t * y2
    local x23, y23 = mt * x2 + t * x3, mt * y2 + t * y3
    local x012, y012 = mt * x01 + t * x12, mt * y01 + t * y12
    local x123, y123 = mt * x12 + t * x23, mt * y12 + t * y23
    local x0123, y0123 = mt * x012 + t * x123, mt * y012 + t * y123
    return x01, y01, x012, y012, x0123, y0123, x123, y123, x23, y23, x3, y3
  end
  local function flatness(x0, y0, x1, y1, x2, y2, x3, y3)
    local dx, dy = x3-x0, y3-y0
    local dist = math.sqrt(dx*dx + dy*dy)
    local d1 = math.abs(dx * (x0-x1) - dy * (y0-y1)) / dist
    local d2 = math.abs(dx * (x0-x2) - dy * (y0-y2)) / dist
    return d1 > d2 and d1 or d2
  end
  function flatten(out, target, x0, y0, x1, y1, x2, y2, x3, y3)
    local current = flatness(x0, y0, x1, y1, x2, y2, x3, y3)
    if current <= target then
      local i = #out
      -- out[i+1], out[i+2],
      -- out[i+3], out[i+4],
      -- out[i+5], out[i+6], out[i+7]
      --   = x1, y1, x2, y2, x3, y3, 'c'
      out[i+1], out[i+2], out[i+3]
        = x3, y3, 'l'
      return
    end
    local a, b, c, d, e, f, g, h, i, j, k, l = subdivide(.5, x0, y0, x1, y1, x2, y2, x3, y3)
    flatten(out, target, x0, y0, a, b, c, d, e, f)
    return flatten(out, target, e, f, g, h, i, j, k, l)
  end
end

local function ps_to_string(a)
  local ta = type(a)
  if ta == 'table' and a.kind == 'executable' then
    a = a.value
    ta = type(a)
  end
  if ta == 'string' then
  elseif ta == 'boolean' then
    a = a and 'true' or 'false'
  elseif ta == 'number' then
    a = string.format(math.type(a) == 'float' and '%.6g' or '%i', a)
    -- a = tostring(a)
  elseif ta == 'function' then
    texio.write_nl'Warning: cvs on operators is unsupported. Replaced by dummy.'
    a = '--nostringval--'
  elseif ta == 'table' then
    local kind = a.kind
    if kind == 'string' or kind == 'name' then
      a = a.value
    elseif kind == 'operator' then
      texio.write_nl'Warning: cvs on operators is unsupported. Replaced by dummy.'
      a = '--nostringval--'
    else
      a = '--nostringval--'
    end
  elseif ta == 'userdata' and a.read then
    a = 'file'
  else
    assert(false)
  end
  return a
end

local mark = {kind = 'mark'}
local null = {kind = 'null'}
local statusdict = {kind = 'dict', value = {}}
local globaldict = {kind = 'dict', value = {}}
local userdict = {kind = 'dict', value = {
  SDict = {kind = 'dict', value = {
    normalscale = {kind = 'executable', value = {kind = 'array', value = {}}},
  }},
  TeXDict = {kind = 'dict', value = {
    Resolution = function() push((pdf.getpkresolution())) end,
  }},
  ['@beginspecial'] = {kind = 'executable', value = {kind = 'array', value = {}}},
  ['@setspecial'] = {kind = 'executable', value = {kind = 'array', value = {}}},
  ['@endspecial'] = {kind = 'executable', value = {kind = 'array', value = {}}},
}}
userdict.value.TeXDict.value.VResolution = userdict.value.TeXDict.value.Resolution
local FontDirectory = {kind = 'dict', value = {}}
local ResourceCategories = {kind = 'dict', value = {}}

local function num_to_base(num, base, ...)
  if num == 0 then return string.char(...) end
  local remaining = num // base
  local digit = num - base * remaining
  if digit < 10 then
    digit = digit + 0x30
  else
    digit = digit + 0x37
  end
  return num_to_base(remaining, base, digit, ...)
end

local systemdict
local function generic_show(str, ax, ay)
  local state = graphics_stack[#graphics_stack]
  local current_point = state.current_point
  if not current_point then return nil, 'nocurrentpoint' end
  local rawpsfont = state.font
  if not rawpsfont then return nil, 'invalidfont' end
  local str = str.value
  local psfont = rawpsfont.value
  local fid = psfont.FID
  local matrix = psfont.FontMatrix.value
  local fonttype = psfont.FontType
  if fonttype ~= 0x1CA and fonttype ~= 3 then
    texio.write_nl'luapstricks: Attempt to use unsupported font type.'
    return nil, 'invalidfont'
  end
  local x0, y0 = current_point[1], current_point[2]
  update_matrix(
    matrix[1],      matrix[2],
    matrix[3],      matrix[4],
    matrix[5] + x0, matrix[6] + y0)
  local w = 0
  if fonttype == 0x1CA then
    local characters = assert(font.getfont(fid)).characters
    local max_d, max_h = 0, 0
    flush_delayed()
    if pdfprint ~= gobble then
      vf.push()
      vf.fontid(fid)
    end
    for b in string.bytes(str) do
      if pdfprint ~= gobble then
        vf.char(b)
        if ax then
          vf.right(ax)
          vf.down(-ay)
        end
      end
      local char = characters[b]
      if char then
        w = w + (char.width or 0)
        if char.depth and char.depth > max_d then
          max_d = char.depth
        end
        if char.height and char.height > max_h then
          max_h = char.height
        end
      end
    end
    w = w/65781.76
    if pdfprint ~= gobble then
      max_d = max_d/65781.76
      max_h = max_h/65781.76
      register_point(state, 0, -max_d)
      if ax then
        local count = #str
        register_point(state, w + count * ax, max_h + count * ay)
      else
        register_point(state, w, max_h)
      end
      vf.pop()
    end
  elseif fonttype == 3 then
    for b in string.bytes(str) do
      systemdict.value.gsave()
      local state = graphics_stack[#graphics_stack]
      state.current_point, state.current_path = nil
      push(rawpsfont)
      push(b)
      local this_w
      char_width_storage = function(width)
        this_w = width
      end
      execute_tok(psfont.BuildChar) -- FIXME(maybe): Switch to BuildGlyph?
      systemdict.value.grestore()
      w = w + assert(this_w, 'Type 3 character failed to set width')
      update_matrix(1, 0, 0, 1, this_w, 0)
      if ax then
        update_matrix(1, 0, 0, 1, ax, ay)
      end
    end
    update_matrix(1, 0, 0, 1, -w, 0)
    if ax then
      local count = #str
      update_matrix(1, 0, 0, 1, count * -ax, count * -ay)
    end
  else
    assert(false)
  end
  if ax then
    local count = #str
    push(w + count * ax)
    push(count * ay)
  else
    push(w)
    push(0)
  end
  systemdict.value.rmoveto()
  update_matrix(matrix_invert(
    matrix[1],      matrix[2],
    matrix[3],      matrix[4],
    matrix[5] + x0, matrix[6] + y0))
  return true
end

systemdict = {kind = 'dict', value = {
  dup = function()
    local v = pop()
    push(v)
    push(v)
  end,
  exch = function()
    local b = pop()
    local a = pop(b)
    push(b)
    push(a)
  end,
  pop = function()
    pop()
  end,
  clear = function()
    for i = 1, #operand_stack do
      operand_stack[i] = nil
    end
  end,
  copy = function()
    local arg, orig = pop()
    local exec
    if type(arg) == 'table' and arg.kind == 'executable' then
      exec = true
      arg = arg.value
    end
    if type(arg) == 'number' then
      local height = #operand_stack
      if arg > height then
        error'copy argument larger then stack'
      end
      table.move(operand_stack, height-arg+1, height, height+1)
    elseif type(arg) == 'table' then
      -- See remarks in getinterval about missing functionality
      local kind = arg.kind
      if kind == 'array' then
        local src = pop_array().value
        if #src ~= #arg.value then
          error'copy with different sized arrays is not implemented yet'
        end
        table.move(src, 1, #src, 1, arg.value)
      elseif kind == 'string' then
        local src = pop_string().value
        if #src == #arg.value then
        elseif #src < #arg.value then
          arg = str_view(arg, 1, #src)
        else
          ps_error'rangecheck'
        end
        arg.value = src
      elseif kind == 'dict' then
        local src = pop_dict()
        if next(arg.value) then
          error'Target dictionary must be empty'
        end
        for k, v in next, src do
          arg.value[k] = v
        end
      else
        ps_error'typecheck'
      end
      push(exec and {kind = 'executable', value = arg} or arg)
    else
      ps_error('typecheck', orig)
    end
  end,
  roll = function()
    local j, arg2 = pop_int()
    local n, arg1 = pop_int(arg2)
    if n < 0 then
      ps_error('rangecheck', arg1, arg2)
    end
    if n == 0 or j == 0 then return end
    local height = #operand_stack
    if j < 0 then
      j = (-j) % n
      local temp = table.move(operand_stack, height-n+1, height-n+j, 1, {})
      table.move(operand_stack, height-n+j+1, height, height-n+1)
      table.move(temp, 1, j, height-j+1, operand_stack)
    else
      j = j % n
      local temp = table.move(operand_stack, height-j+1, height, 1, {})
      table.move(operand_stack, height-n+1, height-j, height-n+j+1)
      table.move(temp, 1, j, height-n+1, operand_stack)
    end
  end,
  index = function()
    local i, arg1 = pop_int()
    local height = #operand_stack
    if i < 0 or height <= i then
      ps_error('rangecheck', arg1)
    end
    push(operand_stack[height - i])
  end,
  null = function()
    push(null)
  end,
  mark = function()
    push(mark)
  end,
  ['['] = function()
    push(mark)
  end,
  [']'] = function()
    systemdict.value.counttomark()
    systemdict.value.array()
    systemdict.value.astore()
    systemdict.value.exch()
    systemdict.value.pop()
  end,
  ['<<'] = function()
    push(mark)
  end,
  ['>>'] = function()
    local mark_pos
    for i = #operand_stack, 1, -1 do
      if operand_stack[i] == mark then
        mark_pos = i
        break
      end
    end
    if not mark_pos then error'unmatchedmark' end
    local dict = lua.newtable(0, (#operand_stack-mark_pos) // 2)
    for i = mark_pos + 1, #operand_stack - 1, 2 do
      push(operand_stack[i])
      local key = pop_key()
      dict[key] = operand_stack[i+1]
    end
    for i = mark_pos, #operand_stack do
      operand_stack[i] = nil
    end
    push{kind = 'dict', value = dict}
  end,
  count = function()
    push(#operand_stack)
  end,
  counttomark = function()
    local height = #operand_stack
    for i=height, 1, -1 do
      local entry = operand_stack[i]
      if type(entry) == 'table' and entry.kind == 'mark' then
        return push(height-i)
      end
    end
    error'unmatchedmark'
  end,
  cleartomark = function()
    local entry
    repeat
      entry = pop()
    until (not entry) or type(entry) == 'table' and entry.kind == 'mark'
    if not entry then error'unmatchedmark' end
  end,

  ['if'] = function()
    local proc, arg2 = pop_proc()
    local cond = pop_bool(arg2)
    if cond then
      execute_ps(proc)
    end
  end,
  ifelse = function()
    local proc_else, arg3 = pop_proc()
    local proc_then, arg2 = pop_proc(arg3)
    local cond = pop_bool(arg2, arg3)
    if cond then
      execute_ps(proc_then)
    else
      execute_ps(proc_else)
    end
  end,
  ['for'] = function()
    local proc, arg4 = pop_proc()
    local limit, arg3 = pop_num(arg4)
    local step, arg2 = pop_num(arg3, arg4)
    local initial = pop_num(arg2, arg3, arg4)
    local success, err = pcall(function()
      for i=initial, limit, step do
        push(i)
        execute_ps(proc)
      end
    end)
    if not success and err ~= exitmarker then
      error(err, 0)
    end
  end,
  forall = function()
    local proc, arg2 = pop_proc()
    local obj, arg1 = pop()
    if type(obj) ~= 'table' then
      ps_error('typecheck', arg1, arg2)
    end
    if obj.kind == 'executable' then
      obj = obj.value
      if type(obj) ~= 'table' then
        ps_error('typecheck', arg1, arg2)
      end
    end
    local success, err = pcall(
         obj.kind == 'array' and function()
           for i=1, #obj.value do
             push(obj.value[i])
             execute_ps(proc)
           end
         end
      or obj.kind == 'string' and function()
           for b in string.bytes(obj.value) do
             push(b)
             execute_ps(proc)
           end
         end
      or obj.kind == 'dict' and function()
           for k, v in next, obj.value do
             pushs(k, v)
             execute_ps(proc)
           end
         end
      or ps_error('typecheck', arg1, arg2))
    if not success and err ~= exitmarker then
      error(err, 0)
    end
  end,
  ['repeat'] = function()
    local proc, arg2 = pop_proc()
    local count = pop_int(arg2)
    local success, err = pcall(function()
      for i=1, count do
        execute_ps(proc)
      end
    end)
    if not success and err ~= exitmarker then
      error(err, 0)
    end
  end,
  loop = function()
    local proc = pop_proc()
    local success, err = pcall(function()
      while true do
        execute_ps(proc)
      end
    end)
    if not success and err ~= exitmarker then
      error(err, 0)
    end
  end,

  reversepath = function()
    local state = graphics_stack[#graphics_stack]
    local path = state.current_path
    if not path then return end
    local newpath = lua.newtable(#path, 0)
    local i = 1
    local out_ptr = 1
    -- Iterate over groups starting with "x y m". These can contain multiple subpaths separated with `h`.
    while path[i+2] == 'm' do
      local x0, y0 = path[i], path[i + 1]
      local after = i + 3
      while path[after] and path[after + 2] ~= 'm' do
        after = after + 1
      end
      local j = after
      out_ptr = out_ptr + 3 -- Leave space for the initial `x y m`
      newpath[out_ptr - 1] = 'm'
      local drop_closepath = true -- If this is true we do not end with a closepath and therefore have to remove the first one.
      while true do
        j = j - 1
        local cmd = path[j]
        if cmd == 'h' then
          if j ~= after - 1 then
            newpath[out_ptr - 3], newpath[out_ptr - 2] = x0, y0
            if not drop_closepath then
              newpath[out_ptr] = 'h'
              out_ptr = out_ptr + 1
            end
            out_ptr = out_ptr + 3 -- Leave space for the initial `x y m`
            newpath[out_ptr - 1] = 'm'
          end
          drop_closepath = false
        elseif cmd == 'm' then
          newpath[out_ptr - 3], newpath[out_ptr - 2] = path[j - 2], path[j - 1]
          break
        else
          if cmd == 'c' then
            newpath[out_ptr - 3], newpath[out_ptr - 2] = path[j - 2], path[j - 1]
            newpath[out_ptr], newpath[out_ptr + 1], newpath[out_ptr + 2], newpath[out_ptr + 3] = path[j - 4], path[j - 3], path[j - 6], path[j - 5]
            out_ptr = out_ptr + 6
            newpath[out_ptr] = 'c'
            j = j - 6
          elseif cmd == 'l' then
            newpath[out_ptr - 3], newpath[out_ptr - 2] = path[j - 2], path[j - 1]
            out_ptr = out_ptr + 2
            j = j - 2
          else
            assert(false)
          end
          newpath[out_ptr] = cmd
          out_ptr = out_ptr + 1
        end
      end
      if not drop_closepath then
        newpath[out_ptr] = 'h'
        out_ptr = out_ptr + 1
      end
      i = after
    end
    state.current_path = newpath
    local last_cmd = #newpath
    if newpath[last_cmd] == 'h' then
      last_cmd = last_cmd - 1
    end
    state.current_point[1], state.current_point[2] = newpath[last_cmd - 2], newpath[last_cmd - 1]
  end,

  pathforall = function()
    local close = pop_proc()
    local curve = pop_proc()
    local line = pop_proc()
    local move = pop_proc()
    local state = graphics_stack[#graphics_stack]
    local path = state.current_path
    if not path then return end
    path = table.move(path, 1, #path, 1, {}) -- We don't want to be affected by modifications
    local success, err = pcall( function()
      local i = 1
      while true do
        local entry = path[i]
        if type(entry) == 'string' then
          execute_ps(entry == 'm' and move or entry == 'l' and line or entry == 'c' and curve or entry == 'h' and close or error'Unexpected path operator')
        elseif entry then
          push(entry)
        else
          break
        end
        i = i + 1
      end
    end)
    if not success and err ~= exitmarker then
      error(err, 0)
    end
  end,

  ['.texboxforall'] = function()
    local proc, arg2 = pop_proc()
    local boxop = pop()
    local box = lua_node_lookup[boxop]
    if not box then
      -- push(boxop)
      -- -- push(proc)
      ps_error('typecheck', boxop, arg2)
    end
    if node.direct.getid(box.box) ~= node.id'hlist' then
      -- push(boxop)
      -- push(proc)
      error'.texboxforall is currently only supported for hboxes'
    end

    local head = node.direct.getlist(box.box)
    head = node.direct.flatten_discretionaries(head)
    node.direct.setlist(box.box, head)
    local success, err = pcall(function()
      local x, y = 0, 0
      local n = head
      while n do
        local after = node.direct.getnext(n)
        local width = node.direct.rangedimensions(box.box, n, after)/65781.76
        push(mark)
        local id = node.type(node.direct.getid(n))
        local subbox = {box = n, parent = box} -- parent is needed for lifetime reasons
        local function op()
          flush_delayed()
          vf.push()
          local n = subbox.box -- Same as the outer box, but this preserves the lifetime of subbox
          local parent = subbox.parent.box
          local after = node.direct.getnext(n)
          local head = node.direct.getlist(parent)
          node.direct.setnext(n, nil)
          node.direct.setlist(parent, n)
          local state = graphics_stack[#graphics_stack]
          local w, h, d = node.direct.dimensions(n)
          register_point(state, 0, -d/65781.76)
          register_point(state, w/65781.76, h/65781.76)
          vf.node(parent)
          node.direct.setnext(n, after)
          node.direct.setlist(parent, head)
          vf.pop()
        end
        lua_node_lookup[subbox] = op
        push(op)
        push(x)
        push(y)
        push(width)
        push(0)
        push(id)
        execute_ps(proc)
        if width ~= 0 then
          x = x + width
        end
        n = after
      end
    end)
    if not success and err ~= exitmarker then
      error(err, 0)
    end
  end,
  pathbbox = function()
    local current_path = assert(graphics_stack[#graphics_stack].current_path, 'nocurrentpoint')
    local i=1
    local llx, lly, urx, ury
    while current_path[i] do
      local entry = current_path[i]
      if type(entry) == 'number' then
        local after = current_path[i+1]
        assert(type(after) == 'number')
        llx = llx and llx < entry and llx or entry
        lly = lly and lly < after and lly or after
        urx = urx and urx > entry and urx or entry
        ury = ury and ury > after and ury or after
        i = i+2
      else
        i = i+1
      end
    end
    push(llx)
    push(lly)
    push(urx)
    push(ury)
  end,

  ['not'] = function()
    local val, orig = pop()
    local tval = type(val)
    if tval == 'table' and val.kind == 'executable' then
      val = val.value
      local tval = type(val)
    end
    if tval == 'boolean' then
      push(not val)
    elseif tval == 'number' then
      push(~val)
    else
      ps_error('typecheck', orig)
    end
  end,
  ['and'] = function()
    local val, orig = pop()
    local tval = type(val)
    if tval == 'table' and val.kind == 'executable' then
      val = val.value
      local tval = type(val)
    end
    if tval == 'boolean' then
      push(pop_bool() and val)
    elseif tval == 'number' then
      push(val & pop_int())
    else
      ps_error('typecheck', orig)
    end
  end,
  ['or'] = function()
    local val, orig = pop()
    local tval = type(val)
    if tval == 'table' and val.kind == 'executable' then
      val = val.value
      local tval = type(val)
    end
    if tval == 'boolean' then
      push(pop_bool() or val)
    elseif tval == 'number' then
      push(val | pop_int())
    else
      ps_error('typecheck', orig)
    end
  end,
  ['xor'] = function()
    local val, orig = pop()
    local tval = type(val)
    if tval == 'table' and val.kind == 'executable' then
      val = val.value
      local tval = type(val)
    end
    if tval == 'boolean' then
      push(val ~= pop_bool())
    elseif tval == 'number' then
      push(val ~ pop_int())
    else
      ps_error('typecheck', orig)
    end
  end,
  bitshift = function()
    local shift, arg2 = pop_num()
    local val = pop_num(arg2)
    push(val << shift)
  end,

  eq = function()
    local b = pop()
    local a = pop(b)
    if type(a) == 'table' and (a.kind == 'executable' or a.kind == 'name' or a.kind == 'operator') then
      a = a.value
    end
    if type(a) == 'table' and a.kind == 'string' then
      a = a.value
    end
    if type(b) == 'table' and (b.kind == 'executable' or b.kind == 'name' or b.kind == 'operator') then
      b = b.value
    end
    if type(b) == 'table' and b.kind == 'string' then
      b = b.value
    end
    push(a==b)
  end,
  ne = function()
    local b = pop()
    local a = pop(b)
    if type(a) == 'table' and (a.kind == 'executable' or a.kind == 'name' or a.kind == 'operator') then
      a = a.value
    end
    if type(a) == 'table' and a.kind == 'string' then
      a = a.value
    end
    if type(b) == 'table' and (b.kind == 'executable' or b.kind == 'name' or b.kind == 'operator') then
      b = b.value
    end
    if type(b) == 'table' and b.kind == 'string' then
      b = b.value
    end
    push(a~=b)
  end,
  gt = function()
    local b, arg2 = pop()
    local a, arg1 = pop(arg2)
    local ta, tb = type(a), type(b)
    if ta == 'table' and a.kind == 'executable' then
      a = a.value ta = type(a)
    end
    if tb == 'table' and b.kind == 'executable' then
      b = b.value tb = type(b)
    end
    if ta == 'number' then
      if tb ~= 'number' then
        ps_error('typecheck', arg1, arg2)
      end
    elseif ta == 'table' and ta.kind == 'string' then
      if tb ~= 'table' or tb.kind ~= 'string' then
        ps_error('typecheck', arg1, arg2)
      end
      a, b = a.value, b.value
    else
      ps_error('typecheck', arg1, arg2)
    end
    push(a>b)
  end,
  ge = function()
    local b, arg2 = pop()
    local a, arg1 = pop(arg2)
    local ta, tb = type(a), type(b)
    if ta == 'table' and a.kind == 'executable' then
      a = a.value ta = type(a)
    end
    if tb == 'table' and b.kind == 'executable' then
      b = b.value tb = type(b)
    end
    if ta == 'number' then
      if tb ~= 'number' then
        ps_error('typecheck', arg1, arg2)
      end
    elseif ta == 'table' and ta.kind == 'string' then
      if tb ~= 'table' or tb.kind ~= 'string' then
        ps_error('typecheck', arg1, arg2)
      end
      a, b = a.value, b.value
    else
      ps_error('typecheck', arg1, arg2)
    end
    push(a>=b)
  end,
  le = function()
    local b, arg2 = pop()
    local a, arg1 = pop(arg2)
    local ta, tb = type(a), type(b)
    if ta == 'table' and a.kind == 'executable' then
      a = a.value ta = type(a)
    end
    if tb == 'table' and b.kind == 'executable' then
      b = b.value tb = type(b)
    end
    if ta == 'number' then
      if tb ~= 'number' then
        ps_error('typecheck', arg1, arg2)
      end
    elseif ta == 'table' and ta.kind == 'string' then
      if tb ~= 'table' or tb.kind ~= 'string' then
        ps_error('typecheck', arg1, arg2)
      end
      a, b = a.value, b.value
    else
      ps_error('typecheck', arg1, arg2)
    end
    push(a<=b)
  end,
  lt = function()
    local b, arg2 = pop()
    local a, arg1 = pop(arg2)
    local ta, tb = type(a), type(b)
    if ta == 'table' and a.kind == 'executable' then
      a = a.value ta = type(a)
    end
    if tb == 'table' and b.kind == 'executable' then
      b = b.value tb = type(b)
    end
    if ta == 'number' then
      if tb ~= 'number' then
        ps_error('typecheck', arg1, arg2)
      end
    elseif ta == 'table' and a.kind == 'string' then
      if tb ~= 'table' or b.kind ~= 'string' then
        ps_error('typecheck', arg1, arg2)
      end
      a, b = a.value, b.value
    else
      ps_error('typecheck', arg1, arg2)
    end
    push(a<b)
  end,

  -- The following two are GhostScript extensions
  max = function()
    local b, arg2 = pop()
    local a, arg1 = pop(arg2)
    local ta, tb = type(a), type(b)
    if ta == 'table' and a.kind == 'executable' then
      a = a.value ta = type(a)
    end
    if tb == 'table' and b.kind == 'executable' then
      b = b.value tb = type(b)
    end
    if ta == 'number' then
      if tb ~= 'number' then
        ps_error('typecheck', arg1, arg2)
      end
    elseif ta == 'table' and ta.kind == 'string' then
      if tb ~= 'table' or tb.kind ~= 'string' then
        ps_error('typecheck', arg1, arg2)
      end
      a, b = a.value, b.value
    else
      ps_error('typecheck', arg1, arg2)
    end
    push(a > b and a or b)
  end,
  min = function()
    local b, arg2 = pop()
    local a, arg1 = pop(arg2)
    local ta, tb = type(a), type(b)
    if ta == 'table' and a.kind == 'executable' then
      a = a.value ta = type(a)
    end
    if tb == 'table' and b.kind == 'executable' then
      b = b.value tb = type(b)
    end
    if ta == 'number' then
      if tb ~= 'number' then
        ps_error('typecheck', arg1, arg2)
      end
    elseif ta == 'table' and ta.kind == 'string' then
      if tb ~= 'table' or tb.kind ~= 'string' then
        ps_error('typecheck', arg1, arg2)
      end
      a, b = a.value, b.value
    else
      ps_error('typecheck', arg1, arg2)
    end
    push(a < b and a or b)
  end,

  add = function()
    local b, arg2 = pop_num()
    local a = pop_num(arg2)
    push(a+b)
  end,
  sub = function()
    local b, arg2 = pop_num()
    local a = pop_num(arg2)
    push(a-b)
  end,
  mul = function()
    local b, arg2 = pop_num()
    local a = pop_num(arg2)
    push(a*b)
  end,
  div = function()
    local b, arg2 = pop_num()
    local a = pop_num(arg2)
    push(a/b)
  end,
  idiv = function()
    local b, arg2 = pop_num()
    local a = pop_num(arg2)
    push(a//b)
  end,
  mod = function()
    local b, arg2 = pop_num()
    local a = pop_num(arg2)
    push(a%b)
  end,
  exp = function()
    local b, arg2 = pop_num()
    local a = pop_num(arg2)
    push(a^b)
  end,
  sqrt = function()
    push(math.sqrt(pop_num()))
  end,
  sin = function()
    local x = pop_num()
    local i, f = math.modf(x/90)
    if f == 0 then
      push(sin_table[i % 4 + 1])
    else
      push(math.sin(math.rad(x)))
    end
  end,
  cos = function()
    local x = pop_num()
    local i, f = math.modf(x/90)
    if f == 0 then
      push(sin_table[(i+1) % 4 + 1])
    else
      push(math.cos(math.rad(x)))
    end
  end,
  atan = function()
    local b, arg2 = pop_num()
    local a = pop_num(arg2)
    local res = math.deg(math.atan(a, b))
    if res < 0 then res = res + 360 end
    push(res)
  end,
  arccos = function()
    push(math.deg(math.acos(pop_num())))
  end,
  arcsin = function()
    push(math.deg(math.asin(pop_num())))
  end,
  abs = function()
    push(math.abs(pop_num()))
  end,
  neg = function()
    push(-pop_num())
  end,
  round = function()
    return push(math.floor(pop_num()+.5))
  end,
  ceiling = function()
    return push(math.ceil(pop_num()))
  end,
  floor = function()
    return push(math.floor(pop_num()))
  end,
  ln = function()
    push(math.log((pop_num())))
  end,
  log = function()
    push(math.log(pop_num(), 10))
  end,
  truncate = function()
    push((math.modf(pop_num())))
  end,
  cvn = function()
    local a, raw = pop()
    if type(a) == 'table' and a.kind == 'executable' then
      local val = a.value
      if type(val) ~= 'table' or val.kind ~= 'string' then
        ps_error('typecheck', raw)
      end
      push(val.value)
    end
    if type(a) ~= 'table' or a.kind ~= 'string' then
      ps_error('typecheck', raw)
    end
    return push{kind = 'name', value = a.value}
  end,
  cvi = function()
    local a, raw = pop()
    if type(a) == 'table' and a.kind == 'executable' then
      a = a.value
    end
    if type(a) == 'table' and a.kind == 'string' then
      a = (number * -1):match(a.value)
      if not a then
        ps_error('syntaxerror', raw)
      end
    end
    if type(a) ~= 'number' then ps_error('typecheck', raw) end
    push(a//1)
  end,
  cvr = function()
    local a, raw = pop()
    if type(a) == 'table' and a.kind == 'executable' then
      a = a.value
    end
    if type(a) == 'table' and a.kind == 'string' then
      a = (number * -1):match(a.value)
      if not a then
        ps_error('syntaxerror', raw)
      end
    end
    if type(a) ~= 'number' then ps_error('typecheck', raw) end
    push(a*1.)
  end,
  cvs = function()
    local old_str, arg2 = pop_string()
    local a, arg1 = pop()
    a = ps_to_string(a)
    if #old_str.value < #a then ps_error('rangecheck', arg1, arg2) end
    old_str.value = a .. string.sub(old_str.value, #a+1, -1)
    return push{kind = 'string', value = a}
  end,
  cvrs = function()
    local old_str, arg3 = pop_string()
    local radix, arg2 = pop_num()
    local num, arg1 = pop_num()
    if radix == 10 then
      num = string.format(math.type(num) == 'float' and '%.6g' or '%i', num)
    else
      num = num//1
      if num < 0 and num >= -0x80000000 then
        num = num + 0x100000000
      end
      if num < 0 then
        ps_error('rangecheck', arg1, arg2, arg3)
      end
      num = num == 0 and '0' or num_to_base(num, radix)
    end
    if #old_str.value < #num then ps_error('rangecheck', arg1, arg2, arg3) end
    old_str.value = num .. string.sub(old_str.value, #num+1, -1)
    return push{kind = 'string', value = num}
  end,

  string = function()
    push{kind = 'string', value = string.rep('\0', (pop_int()))}
  end,
  search = function()
    local seek = pop_string()
    local str = pop_string()
    local start, stop = string.find(str.value, seek.value, 1, true)
    if start then
      push(str_view(str, stop + 1, #str.value - stop))
      push(str_view(str, start, stop - start + 1))
      push(str_view(str, 1, start - 1))
      push(true)
    else
      push(str)
      push(false)
    end
  end,

  array = function()
    local size = pop_int()
    local arr = lua.newtable(size, 0)
    for i=1, size do arr[i] = null end
    push{kind = 'array', value = arr}
  end,
  astore = function()
    local arr = pop_array()
    local size = #arr.value
    for i=size, 1, -1 do
      arr.value[i] = pop()
    end
    push(arr)
  end,
  aload = function()
    local arr = pop_array()
    table.move(arr.value, 1, #arr.value, #operand_stack + 1, operand_stack)
    push(arr)
  end,
  getinterval = function()
    local count, arg3 = pop_int()
    local index, arg2 = pop_int()
    local arr, arg1 = pop()
    if type(arr) ~= 'table' then ps_error('typecheck', arg1, arg2, arg3) end
    if arr.kind == 'executable' then
      arr = arr.value
      if type(arr) ~= 'table' then ps_error('typecheck', arg1, arg2, arg3) end
    end
    if arr.kind == 'string' then
      push(str_view(arr, index + 1, count))
    elseif arr.kind == 'array' then
      -- TODO: At least for the array case, we could use metamethods to make get element sharing behavior
      push{kind = 'array', value = table.move(arr.value, index + 1, index + count, 1, {})}
    else
      ps_error('typecheck', arg1, arg2, arg3)
    end
  end,
  putinterval = function()
    local from, arg2 = pop()
    local index, arg1 = pop_int()
    if type(from) ~= 'table' then ps_error('typecheck', arg1, arg2) end
    if from.kind == 'executable' then
      from = from.value
      if type(from) ~= 'table' then ps_error('typecheck', arg1, arg2) end
    end
    if from.kind == 'string' then
      local to = pop_string()
      from = from.value
      to.value = string.sub(to.value, 1, index) .. from .. string.sub(to.value, index + 1 + #from)
    elseif from.kind == 'array' then
      local to = pop_array()
      table.move(from.value, 1, #from.value, index + 1, to.value)
    else
      ps_error('typecheck', arg1, arg2)
    end
  end,

  dict = function()
    local size = pop_int()
    push{kind = 'dict', value = lua.newtable(0, size)}
  end,
  begin = function()
    local _
    _, _, dictionary_stack[#dictionary_stack + 1] = pop_dict()
  end,
  ['end'] = function()
    if #dictionary_stack <= 3 then
      ps_error'dictstackunderflow'
    end
    dictionary_stack[#dictionary_stack] = nil
  end,
  currentdict = function()
    push(dictionary_stack[#dictionary_stack])
  end,
  bind = function()
    local d = pop()
    push(d)
    if type(d) ~= 'table' then ps_error'typecheck' end
    if d.kind == 'executable' then
      d = d.value
      if type(d) ~= 'table' then ps_error'typecheck' end
    end
    if d.kind ~= 'array' then ps_error'typecheck' end
    bind(d.value)
  end,
  def = function()
    local value = pop()
    local key = pop_key()
    dictionary_stack[#dictionary_stack].value[key] = value
  end,
  store = function()
    local value = pop()
    local key = pop_key()
    for i=#dictionary_stack, 1, -1 do
      if dictionary_stack[i].value[key] ~= nil then
        dictionary_stack[i].value[key] = value
        return
      end
    end
    dictionary_stack[#dictionary_stack].value[key] = value
  end,
  known = function()
    local key = pop_key()
    local dict = pop()
    push(dict.value[key] ~= nil)
  end,
  where = function()
    local key = pop_key()
    for i = #dictionary_stack, 1, -1 do
      local dict = dictionary_stack[i]
      local value = dict.value[key]
      if value ~= nil then
        push(dict)
        return push(true)
      end
    end
    return push(false)
  end,
  load = function()
    push(lookup(pop_key()))
  end,
  get = function()
    local key = pop()
    local obj = pop()
    if type(obj) ~= 'table' then ps_error'typecheck' end
    if obj.kind == 'executable' then
      obj = obj.value
      if type(obj) ~= 'table' then ps_error'typecheck' end
    end
    local val = obj.value
    if obj.kind == 'string' then
      push(key) key = pop_int()
      if key < 0 or key >= #val then ps_error'rangecheck' end
      push(string.byte(val, key+1))
    elseif obj.kind == 'array' then
      push(key) key = pop_int()
      if key < 0 or key >= #val then ps_error'rangecheck' end
      push(val[key+1])
    elseif obj.kind == 'dict' then
      push(key) key = pop_key()
      push(val[key])
    else
      ps_error'typecheck'
    end
  end,
  put = function()
    local value = pop()
    local key = pop()
    local obj = pop()
    if type(obj) ~= 'table' then ps_error'typecheck' end
    if obj.kind == 'executable' then
      obj = obj.value
      if type(obj) ~= 'table' then ps_error'typecheck' end
    end
    local val = obj.value
    if obj.kind == 'string' then
      push(key) key = pop_int()
      if key < 0 or key >= #val then ps_error'rangecheck' end
      push(value) value = pop_int()
      obj.value = string.sub(val, 1, key) .. string.char(value) .. string.sub(val, key+2, #val)
    elseif obj.kind == 'array' then
      push(key) key = pop_int()
      if key < 0 or key >= #val then ps_error'rangecheck' end
      val[key+1] = value
    elseif obj.kind == 'dict' then
      push(key) key = pop_key()
      val[key] = value
    else
      ps_error'typecheck'
    end
  end,
  undef = function()
    local key = pop_key()
    local dict = pop_dict()
    dict[key] = nil
  end,
  length = function()
    local obj = pop()
    if type(obj) == 'string' then
      return push(#obj)
    elseif type(obj) ~= 'table' then
      ps_error'typecheck'
    end
    if obj.kind == 'executable' then
      obj = obj.value
      if type(obj) ~= 'table' then ps_error'typecheck' end
    end
    local val = obj.value
    if obj.kind == 'string' then
      push(#val)
    elseif obj.kind == 'name' then
      push(#val)
    elseif obj.kind == 'array' then
      push(#val)
    elseif obj.kind == 'dict' then
      local length = 0
      for _ in next, val do
        length = length + 1
      end
      push(length)
    else
      ps_error'typecheck'
    end
  end,

  matrix = function()
    push{kind = 'array', value = {1, 0, 0, 1, 0, 0}}
  end,
  defaultmatrix = function()
    local m = pop_array()
    local mm = m.value
    assert(#mm == 6)
    mm[1], mm[2], mm[3], mm[4], mm[5], mm[6] = 10, 0, 0, 10, 0, 0
    push(m)
  end,
  currentmatrix = function()
    local m = pop_array()
    assert(#m.value == 6)
    table.move(graphics_stack[#graphics_stack].matrix, 1, 6, 1, m.value)
    push(m)
  end,
  currentlinewidth = function()
    push(assert(graphics_stack[#graphics_stack].linewidth, 'linewidth has to be set before it is queried'))
  end,
  currentmiterlimit = function()
    push(assert(graphics_stack[#graphics_stack].miterlimit, 'miterlimit has to be set before it is queried'))
  end,
  currentflat = function()
    push(graphics_stack[#graphics_stack].flatness)
  end,
  setlinewidth = function()
    local lw = pop_num()
    graphics_stack[#graphics_stack].linewidth = lw
    delayed_print(string.format('%.3f w', lw))
  end,
  setlinejoin = function()
    local linejoin = pop_int()
    graphics_stack[#graphics_stack].linejoin = linejoin
    delayed_print(string.format('%i j', linejoin))
  end,
  setlinecap = function()
    local linecap = pop_int()
    graphics_stack[#graphics_stack].linecap = linecap
    delayed_print(string.format('%i J', linecap))
  end,
  setmiterlimit = function()
    local ml = pop_int()
    graphics_stack[#graphics_stack].miterlimit = ml
    delayed_print(string.format('%.3f M', ml))
  end,
  setstrokeadjust = function()
    local sa = pop_bool()
    graphics_stack[#graphics_stack].strokeadjust = sa
    delayed_print(ExtGState[sa and '<</SA true>>' or '<</SA false>>'])
  end,
  setdash = function()
    local offset = pop_num()
    local patt = pop_array().value
    graphics_stack[#graphics_stack].dash = {offset = offset, pattern = patt}
    local mypatt = {}
    for i=1, #patt do
      mypatt[i] = string.format('%.3f', patt[i])
    end
    delayed_print(string.format('[%s] %.3f d', table.concat(mypatt, ' '), offset))
  end,
  setflat = function()
    local flatness = pop_num()
    graphics_stack[#graphics_stack].flatness = flatness
    delayed_print(string.format('%.3f i', flatness))
  end,
  currentpoint = function()
    local current_point = assert(graphics_stack[#graphics_stack].current_point, 'nocurrentpoint')
    push(current_point[1])
    push(current_point[2])
  end,

  moveto = function()
    local y = pop_num()
    local x = pop_num()
    local state = graphics_stack[#graphics_stack]
    local current_path = state.current_path
    if current_path then
      local i = #current_path
      if i ~= 1 and current_path[i] == 'm' then
        current_path[i-2], current_path[i-1] = x, y
      else
        current_path[i+1], current_path[i+2], current_path[i+3] = x, y, 'm'
      end
      local current_point = state.current_point
      current_point[1], current_point[2] = x, y
    else
      state.current_path = {x, y, 'm'}
      state.current_point = {x, y}
    end
  end,
  rmoveto = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = assert(state.current_path, 'nocurrentpoint')
    local y = pop_num()
    local x = pop_num()
    local current_point = state.current_point
    x, y = current_point[1] + x, current_point[2] + y
    local i = #current_path
    if i ~= 1 and current_path[i] == 'm' then
      current_path[i-2], current_path[i-1] = x, y
    else
      current_path[i+1], current_path[i+2], current_path[i+3] = x, y, 'm'
    end
    current_point[1], current_point[2] = x, y
  end,
  lineto = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = assert(state.current_path, 'nocurrentpoint')
    local y = pop_num()
    local x = pop_num()
    local i = #current_path + 1
    current_path[i], current_path[i+1], current_path[i+2] = x, y, 'l'
    local current_point = state.current_point
    current_point[1], current_point[2] = x, y
  end,
  rlineto = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = assert(state.current_path, 'nocurrentpoint')
    local y = pop_num()
    local x = pop_num()
    local current_point = state.current_point
    x, y = x + current_point[1], y + current_point[2]
    local i = #current_path + 1
    current_path[i], current_path[i+1], current_path[i+2] = x, y, 'l'
    current_point[1], current_point[2] = x, y
  end,
  curveto = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = assert(state.current_path, 'nocurrentpoint')
    local y3 = pop_num()
    local x3 = pop_num()
    local y2 = pop_num()
    local x2 = pop_num()
    local y1 = pop_num()
    local x1 = pop_num()
    local i = #current_path + 1
    current_path[i], current_path[i+1], current_path[i+2], current_path[i+3], current_path[i+4], current_path[i+5], current_path[i+6] = x1, y1, x2, y2, x3, y3, 'c'
    local current_point = state.current_point
    current_point[1], current_point[2] = x3, y3
  end,
  rcurveto = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = assert(state.current_path, 'nocurrentpoint')
    local current_point = state.current_point
    local x0, y0 = current_point[1], current_point[2]
    local y3 = pop_num() + y0
    local x3 = pop_num() + x0
    local y2 = pop_num() + y0
    local x2 = pop_num() + x0
    local y1 = pop_num() + y0
    local x1 = pop_num() + x0
    local i = #current_path + 1
    current_path[i], current_path[i+1], current_path[i+2], current_path[i+3], current_path[i+4], current_path[i+5], current_path[i+6] = x1, y1, x2, y2, x3, y3, 'c'
    local current_point = state.current_point
    current_point[1], current_point[2] = x3, y3
  end,
  closepath = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = state.current_path
    local current_point = state.current_point
    if not current_path then return end
    if current_path[#current_path] == 'h' then return end
    local x, y
    for i=#current_path, 1, -1 do
      if current_path[i] == 'm' then
        x, y = assert(tonumber(current_path[i-2])), assert(tonumber(current_path[i-1]))
      end
    end
    current_point[1], current_point[2] = assert(x), y
    current_path[#current_path + 1] = 'h'
  end,

  arc = function()
    local a2 = pop_num()
    local a1 = pop_num()
    local r = pop_num()
    local yc = pop_num()
    local xc = pop_num()
    while a2 < a1 do
      a2 = a2 + 360
    end
    drawarc(xc, yc, r, a1, a2)
  end,
  arcn = function()
    local a2 = pop_num()
    local a1 = pop_num()
    local r = pop_num()
    local yc = pop_num()
    local xc = pop_num()
    while a1 < a2 do
      a1 = a1 + 360
    end
    drawarc(xc, yc, r, a1, a2)
  end,
  arcto = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = assert(state.current_path, 'nocurrentpoint')
    local current_point = state.current_point
    local x0, y0 = current_point[1], current_point[2]

    local r = pop_num()
    local y2 = pop_num()
    local x2 = pop_num()
    local y1 = pop_num()
    local x1 = pop_num()

    local dx1, dy1 = x1 - x0, y1 - y0
    local dx2, dy2 = x2 - x1, y2 - y1

    local a1 = math.atan(dy1, dx1)
    local a2 = math.atan(dy2, dx2)

    if a1 - pi > a2 then
      a1 = a1 - two_pi
    elseif a2 - pi > a1 then
      a2 = a2 - two_pi
    end

    if a1 > a2 then
      a1 = a1 + math.pi/2
      a2 = a2 + math.pi/2
    else
      a1 = a1 - math.pi/2
      a2 = a2 - math.pi/2
    end

    local ox1, oy1 = r * math.cos(a1), r * math.sin(a1)
    local ox2, oy2 = r * math.cos(a2), r * math.sin(a2)
    -- Now we need to calculate the intersection of the lines offset by o1/o2
    -- to determine the center. We inlin eth ematix inverse for performance and better handling of edge cases.
    -- local t1, t2 = matrix_transform(0, 0, matrix_invert(dx1, dy1, dx2, dy2, ox2-ox1, oy2-oy1))
    local det = dx1*dy2 - dy1*dx2
    if math.abs(det) < 0.0000001 then
      -- Just draw a line
      push(x1)
      push(y1)
      systemdict.value.lineto()
      push(x1)
      push(y1)
      push(x1)
      push(y1)
      return
    end
    local t1 = (ox1 - ox2) * dy2/det + (oy2 - oy1) * dx2/det
    local cx, cy = x1 - ox1 + t1 * dx1, y1 - oy1 + t1 * dy1
    -- local ccx, ccy = x1 - ox2 - t2 * dx2, y1 - oy2 + t2 * dy2
    drawarc(cx, cy, r, a1*180/pi, a2*180/pi)

    push(cx + ox1)
    push(cy + oy1)
    push(cx + ox2)
    push(cy + oy2)
  end,
  arct = function()
    systemdict.value.arcto()
    pop()
    pop()
    pop()
    pop()
  end,

  eoclip = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = state.current_path
    if not current_path then return end
    flush_delayed(true)
    for i = 1, #current_path do
      if type(current_path[i]) == 'number' then
        pdfprint(string.format('%.5f', current_path[i]))
      else
        pdfprint(current_path[i])
      end
    end
    pdfprint'W* n'
  end,
  clip = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = state.current_path
    if not current_path then return end
    flush_delayed(true)
    for i = 1, #current_path do
      if type(current_path[i]) == 'number' then
        pdfprint(string.format('%.5f', current_path[i]))
      else
        pdfprint(current_path[i])
      end
    end
    pdfprint'W n'
  end,
  eofill = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = state.current_path
    if not current_path then return end
    current_path[#current_path+1] = 'f*'
    flush_delayed()
    local x
    for i = 1, #current_path do
      local value = current_path[i]
      if type(value) == 'number' then
        current_path[i] = string.format('%.5f', value)
        if x then
          register_point(state, x, value)
          x = nil
        else
          x = value
        end
      end
    end
    pdfprint((table.concat(current_path, ' '):gsub('%.?0+ ', ' ')))
    state.current_path, state.current_point = nil
  end,
  fill = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = state.current_path
    if not current_path then return end
    current_path[#current_path+1] = 'f'
    flush_delayed()
    local x
    for i = 1, #current_path do
      local value = current_path[i]
      if type(value) == 'number' then
        current_path[i] = string.format('%.5f', value)
        if x then
          register_point(state, x, value)
          x = nil
        else
          x = value
        end
      end
    end
    pdfprint((table.concat(current_path, ' '):gsub('%.?0+ ', ' ')))
    state.current_path, state.current_point = nil
  end,
  stroke = function()
    local state = graphics_stack[#graphics_stack]
    local current_path = state.current_path
    if not current_path then return end
    current_path[#current_path+1] = 'S'
    flush_delayed()
    local x
    for i = 1, #current_path do
      local value = current_path[i]
      if type(value) == 'number' then
        current_path[i] = string.format('%.5f', value)
        if x then
          register_point(state, x, value)
          x = nil
        else
          x = value
        end
      end
    end
    pdfprint((table.concat(current_path, ' '):gsub('%.?0+ ', ' ')))
    state.current_path, state.current_point = nil
  end,
  flattenpath = function()
    local state = graphics_stack[#graphics_stack]
    local old_path = state.current_path
    if not old_path then return end
    local new_path = {}
    local last_x, last_y = nil, 0
    local saved_x, saved_y
    local subpath_x, subpath_y
    local last_op = 1
    local matrix = state.matrix
    local tolerance = state.flatness / math.sqrt(matrix[1]*matrix[4]-matrix[2]*matrix[3])
    for i=1, #old_path do
      local entry = old_path[i]
      if type(entry) == 'string' then
        if entry == 'c' then
          assert(i - last_op == 6)
          flatten(new_path, tolerance, saved_x, saved_y, table.unpack(old_path, last_op, i-1))
          table.move(old_path, last_op + 4, last_op + 5, #new_path + 1, new_path)
          new_path[#new_path+1] = 'l'
        else
          if entry == 'm' then
            subpath_x, subpath_y = last_x, last_y
          elseif entry == 'h' then
            last_x, last_y = subpath_x, subpath_y
          end
          table.move(old_path, last_op, i, #new_path + 1, new_path)
        end
        saved_x, saved_y = last_x, last_y
        last_op = i + 1
      else
        if last_y then
          last_x, last_y = entry
        else
          last_y = entry
        end
      end
    end
    assert(last_op == #old_path + 1)
    state.current_path = new_path
  end,

  rectclip = function()
    flush_delayed()
    local top = pop()
    if type(top) == 'table' and top.kind == 'executable' then
      top = top.value
    end
    if type(top) == 'number' then
      local h = top
      local w = pop_num()
      local y = pop_num()
      local x = pop_num()
      pdfprint((string.format('%.5f %.5f %.5f %.5f re W n', x, y, w, h):gsub('%.?0+ ', ' ')))
    else
      error'Unsupported rectclip variant'
    end
  end,
  rectstroke = function()
    flush_delayed()
    local top = pop()
    if type(top) == 'table' and top.kind == 'executable' then
      top = top.value
    end
    if type(top) == 'number' then
      local h = top
      local w = pop_num()
      local y = pop_num()
      local x = pop_num()
      pdfprint((string.format('%.5f %.5f %.5f %.5f re S', x, y, w, h):gsub('%.?0+ ', ' ')))
      local state = graphics_stack[#graphics_stack]
      register_point(state, x, y)
      register_point(state, x + w, y + h)
    else
      error'Unsupported rectstroke variant'
    end
  end,
  rectfill = function()
    flush_delayed()
    local top = pop()
    if type(top) == 'table' and top.kind == 'executable' then
      top = top.value
    end
    if type(top) == 'number' then
      local h = top
      local w = pop_num()
      local y = pop_num()
      local x = pop_num()
      pdfprint((string.format('%.5f %.5f %.5f %.5f re f', x, y, w, h):gsub('%.?0+ ', ' ')))
      local state = graphics_stack[#graphics_stack]
      register_point(state, x, y)
      register_point(state, x + w, y + h)
    else
      error'Unsupported rectfill variant'
    end
  end,

  shfill = function()
    local shading_dict, arg1 = pop_dict()
    flush_delayed()
    local data_src
    local pdf_dict = ''
    for k, v in next, shading_dict do
      if k == 'DataSource' then
        data_src = v
      else
        pdf_dict = pdf_dict .. serialize_pdf(k) .. ' ' .. serialize_pdf(v)
      end
    end
    if shading_dict.ShadingType == 4 then
      assert(data_src)
      if type(data_src) ~= 'table' then
        push(arg1)
        ps_error'typecheck'
      end
      if data_src.kind == 'string' then
        data_src = data_src.value
      elseif data_src.kind == 'array' then
        data_src = data_src.value
        local color_model = shading_dict.ColorSpace.value[1]
        if type(color_model) == 'table' and color_model.kind == 'name' then
          color_model = color_model.value
        end
        if color_model == 'DeviceRGB' then
          color_model = 3
        elseif color_model == 'DeviceCMYK' then
          color_model = 4
        elseif color_model == 'DeviceGray' then
          color_model = 1
        else
          error'Unsupported color model in Shading dictionary'
        end
        local components = color_model + 3
        pdf_dict = pdf_dict .. '/BitsPerCoordinate 24/BitsPerComponent 8/BitsPerFlag 8/Decode[-8192 8191 -8192 8191' .. string.rep(' 0 1', color_model) .. ']'
        local data = ''
        for i = 1, #data_src-components+1, components do
          data = data .. string.pack('>BI3I3', data_src[i], (data_src[i+1]*1024+.5)//1 + 8388608, (data_src[i+2]*1024+.5)//1 + 8388608)
          for j = i + 3, i + 2 + color_model do
            data = data .. string.pack('B', (data_src[j]*255+.5)//1)
          end
        end
        data_src = data
      else
        error'Unsupported DataSource variant'
      end
      local obj = write_shading(pdf_dict, data_src)
      pdfprint(string.format('%s sh', write_shading(pdf_dict, data_src)))
    end
  end,

  scale = function()
    local m = pop()
    if type(m) == 'table' and m.kind == 'array' then
      local mv = m.value
      if #mv ~= 6 then error'Unexpected size of matrix' end
      local y = pop_num()
      local x = pop_num()
      mv[1], mv[2], mv[3], mv[4], mv[5], mv[6] = x, 0, 0, y, 0, 0
      push(m)
    else
      push(m)
      local y = pop_num()
      local x = pop_num()
      update_matrix(x, 0, 0, y, 0, 0)
    end
  end,
  translate = function()
    local m = pop()
    if type(m) == 'table' and m.kind == 'array' then
      local mv = m.value
      if #mv ~= 6 then error'Unexpected size of matrix' end
      local y = pop_num()
      local x = pop_num()
      mv[1], mv[2], mv[3], mv[4], mv[5], mv[6] = 1, 0, 0, 1, x, y
      push(m)
    else
      push(m)
      local y = pop_num()
      local x = pop_num()
      update_matrix(1, 0, 0, 1, x, y)
    end
  end,
  rotate = function()
    local m = pop()
    if type(m) == 'table' and m.kind == 'array' then
      local mv = m.value
      if #mv ~= 6 then error'Unexpected size of matrix' end
      local angle = math.rad(pop_num())
      local s, c = math.sin(angle), math.cos(angle)
      mv[1], mv[2], mv[3], mv[4], mv[5], mv[6] = c, s, -s, c, 0, 0
      push(m)
    else
      push(m)
      local angle = math.rad(pop_num())
      local s, c = math.sin(angle), math.cos(angle)
      update_matrix(c, s, -s, c, 0, 0)
    end
  end,
  transform = function()
    local m = pop()
    if type(m) == 'table' and m.kind == 'array' then
      m = m.value
      if #m ~= 6 then error'Unexpected size of matrix' end
    else
      push(m)
      m = graphics_stack[#graphics_stack].matrix
    end
    local y = pop_num()
    local x = pop_num()
    x, y = matrix_transform(x, y, m[1], m[2], m[3], m[4], m[5], m[6])
    push(x)
    push(y)
  end,
  itransform = function()
    local m = pop()
    if type(m) == 'table' and m.kind == 'array' then
      m = m.value
      if #m ~= 6 then error'Unexpected size of matrix' end
    else
      push(m)
      m = graphics_stack[#graphics_stack].matrix
    end
    local y = pop_num()
    local x = pop_num()
    x, y = matrix_transform(x, y, matrix_invert(m[1], m[2], m[3], m[4], m[5], m[6]))
    push(x)
    push(y)
  end,
  dtransform = function()
    local m = pop()
    if type(m) == 'table' and m.kind == 'array' then
      m = m.value
      if #m ~= 6 then error'Unexpected size of matrix' end
    else
      push(m)
      m = graphics_stack[#graphics_stack].matrix
    end
    local y = pop_num()
    local x = pop_num()
    x, y = matrix_transform(x, y, m[1], m[2], m[3], m[4], 0, 0)
    push(x)
    push(y)
  end,
  idtransform = function()
    local m = pop()
    if type(m) == 'table' and m.kind == 'array' then
      m = m.value
      if #m ~= 6 then error'Unexpected size of matrix' end
    else
      push(m)
      m = graphics_stack[#graphics_stack].matrix
    end
    local y = pop_num()
    local x = pop_num()
    x, y = matrix_transform(x, y, matrix_invert(m[1], m[2], m[3], m[4], 0, 0))
    push(x)
    push(y)
  end,
  concatmatrix = function()
    local m3a = pop_array()
    local m3 = m3a.value
    if #m3 ~= 6 then error'Unexpected size of matrix' end
    local m2 = pop_array().value
    if #m2 ~= 6 then error'Unexpected size of matrix' end
    local m1 = pop_array().value
    if #m1 ~= 6 then error'Unexpected size of matrix' end
    m3[1], m3[2],
    m3[3], m3[4],
    m3[5], m3[6]
      = m1[1] * m2[1] + m1[2] * m2[3],         m1[1] * m2[2] + m1[2] * m2[4],
        m1[3] * m2[1] + m1[4] * m2[3],         m1[3] * m2[2] + m1[4] * m2[4],
        m1[5] * m2[1] + m1[6] * m2[3] + m2[5], m1[5] * m2[2] + m1[6] * m2[4] + m2[6]
    push(m3a)
  end,
  invertmatrix = function()
    local target = pop_array()
    local T = target.value
    assert(#T == 6)
    local M = pop_array().value
    assert(#M == 6)
    T[1], T[2], T[3], T[4], T[5], T[6]
      = matrix_invert(M[1], M[2], M[3], M[4], M[5], M[6])
    push(target)
  end,
  concat = function()
    local m = pop_array().value
    if #m ~= 6 then error'Unexpected size of matrix' end
    update_matrix(m[1], m[2], m[3], m[4], m[5], m[6])
  end,
  -- setmatrix is not supported in PDF, so we invert the old matrix first
  setmatrix = function()
    local m = pop()
    if type(m) ~= 'table' or m.kind ~= 'array' then
      ps_error'typecheck'
    end
    local m = m.value
    if #m ~= 6 then ps_error'rangecheck' end
    local old = graphics_stack[#graphics_stack].matrix
    local pt = graphics_stack[#graphics_stack].current_point
    local a, b, c, d, e, f = matrix_invert(old[1], old[2], old[3], old[4], old[5], old[6])
    update_matrix(a, b, c, d, e, f)
    update_matrix(m[1], m[2], m[3], m[4], m[5], m[6])
  end,
  setpdfcolor = function()
    local pdf = pop_string().value
    local color = graphics_stack[#graphics_stack].color
    delayed_print(pdf)
    color.space = {kind = 'array', value = {{kind = 'name', value = 'PDF'}}}
    for i=2, #color do color[i] = nil end
    color[1] = pdf
  end,
  setgray = function()
    local g = pop_num()
    local color = graphics_stack[#graphics_stack].color
    color.space = {kind = 'array', value = {{kind = 'name', value = 'DeviceGray'}}}
    for i=2, #color do color[i] = nil end
    color[1] = g
    delayed_print(string.format('%.3f g %.3f G', g, g))
  end,
  setrgbcolor = function()
    local b = pop_num()
    local g = pop_num()
    local r = pop_num()
    local color = graphics_stack[#graphics_stack].color
    color.space = {kind = 'array', value = {{kind = 'name', value = 'DeviceRGB'}}}
    for i=4, #color do color[i] = nil end
    color[1], color[2], color[3] = r, g, b
    delayed_print(string.format('%.3f %.3f %.3f rg %.3f %.3f %.3f RG', r, g, b, r, g, b))
  end,
  -- Conversion based on Wikipedia article about HSB colorspace
  sethsbcolor = function()
    local b = pop_num()
    local s = pop_num()
    local h = pop_num()
    if b < 0 then b = 0 elseif b > 1 then b = 1 end
    if s < 0 then s = 0 elseif s > 1 then s = 1 end
    if h < 0 then h = 0 elseif h > 1 then h = 1 end
    local hi, hf = math.modf(6 * h)
    local p, q, t = b * (1 - s), b * (1 - s*hf), b * (1 - s * (1-hf))
    if hi == 0 or hi == 6 then
      push(b) push(t) push(p)
    elseif hi == 1 then
      push(q) push(b) push(p)
    elseif hi == 2 then
      push(p) push(b) push(t)
    elseif hi == 3 then
      push(p) push(q) push(b)
    elseif hi == 4 then
      push(t) push(p) push(b)
    elseif hi == 5 then
      push(b) push(p) push(q)
    end
    return systemdict.value.setrgbcolor()
  end,
  setcmykcolor = function()
    local k = pop_num()
    local y = pop_num()
    local m = pop_num()
    local c = pop_num()
    local color = graphics_stack[#graphics_stack].color
    color.space = {kind = 'array', value = {{kind = 'name', value = 'DeviceCMYK'}}}
    for i=5, #color do color[i] = nil end
    color[1], color[2], color[3], color[4] = c, m, y, k
    delayed_print(string.format('%.3f %.3f %.3f %.3f k %.3f %.3f %.3f %.3f K', c, m, y, k, c, m, y, k))
  end,
  ['.setopacityalpha'] = function()
    error'Unsupported, use .setfillconstantalpha instead'
  end,
  ['.setfillconstantalpha'] = function()
    local alpha = pop_num()
    graphics_stack[#graphics_stack].fillconstantalpha = alpha
    delayed_print(ExtGState['<</ca ' .. alpha .. '>>'])
  end,
  ['.setstrokeconstantalpha'] = function()
    local alpha = pop_num()
    graphics_stack[#graphics_stack].strokeconstantalpha = alpha
    delayed_print(ExtGState['<</CA ' .. alpha .. '>>'])
  end,
  ['.currentalphaisshape'] = function()
    local ais = graphics_stack[#graphics_stack].alphaisshape
    if ais == nil then error'alphaisshape has to be set before it is queried' end
    push(ais)
  end,
  ['.setalphaisshape'] = function()
    local ais = pop_bool()
    graphics_stack[#graphics_stack].alphaisshape = ais
    delayed_print(ExtGState['<</AIS ' .. (ais and 'true' or 'false') .. '>>'])
  end,
  ['.currentblendmode'] = function()
    local blendmode = graphics_stack[#graphics_stack].blendmode
    if blendmode == nil then error'blendmode has to be set before it is queried' end
    push{kind = 'name', value = blendmode}
  end,
  ['.setblendmode'] = function()
    local blendmode = pop()
    if type(blendmode) == 'string' then
    elseif type(blendmode) == 'table' and blendmode.kind == 'name' then
      blendmode = blendmode.value
    else
      push(blendmode)
      ps_error'typecheck'
    end
    graphics_stack[#graphics_stack].blendmode = blendmode
    delayed_print(ExtGState['<</BM /' .. blendmode .. '>>'])
  end,
  newpath = function()
    local state = graphics_stack[#graphics_stack]
    state.current_point = nil
    state.current_path = nil
  end,

  currentcolorspace = function()
    local color = graphics_stack[#graphics_stack].color
    if not color then error'Color has to be set before it is queried' end
    push(color.space)
  end,
  currentcolor = function()
    local color = graphics_stack[#graphics_stack].color
    if not color then error'Color has to be set before it is queried' end
    for i = 1, #color do
      push(color[i])
    end
  end,
  currentcmykcolor = function()
    local c, m, y, k
    local color = graphics_stack[#graphics_stack].color
    if not color then error'Color has to be set before it is queried' end
    local space = color.space.value[1]
    if type(space) == 'table' and space.kind == 'name' then space = space.value end
    if space == 'DeviceRGB' then
      c, m, y = 1 - color[1], 1 - color[2], 1 - color[3]
      -- k = math.min(c, m, y)
      -- TODO: Undercolor removal/black generation
      -- local undercolor = undercolorremoval(k)
      -- local undercolor = 0
      -- k = blackgeneration(k)
      k = 0
      -- c, m, y = c - undercolor, y - undercolor, k - undercolor
    elseif space == 'DeviceGray' then
      c, m, y, k = 0, 0, 0, 1 - color[1]
    elseif space == 'DeviceCMYK' then
      c, m, y, k = color[1], color[2], color[3], color[4]
    elseif space == 'PDF' then
      c, m, y, k = 0, 0, 0, 1
      print('???', 'tocmyk', color[1])
    else
      r, g, b, k = 0, 0, 0, 1
    end
    push(r)
    push(g)
    push(b)
  end,
  currentgraycolor = function()
    local g
    local color = graphics_stack[#graphics_stack].color
    if not color then error'Color has to be set before it is queried' end
    local space = color.space.value[1]
    if type(space) == 'table' and space.kind == 'name' then space = space.value end
    if space == 'DeviceRGB' then
      g = 0.3 * color[1] + 0.59 * color[2], 0.11 * color[3]
    elseif space == 'DeviceGray' then
      g = color[1]
    elseif space == 'DeviceCMYK' then
      g = math.min(1, math.max(0, 0.3 * color[1] + 0.59 * color[2] + 0.11 * color[3] + color[4]))
    elseif space == 'PDF' then
      g = 1
      print('???', 'togray', color[1])
    else
      g = 1
    end
    push(g)
  end,
  currentrgbcolor = function()
    local r, g, b
    local color = graphics_stack[#graphics_stack].color
    if not color then error'Color has to be set before it is queried' end
    local space = color.space.value[1]
    if type(space) == 'table' and space.kind == 'name' then space = space.value end
    if space == 'DeviceRGB' then
      r, g, b = color[1], color[2], color[3]
    elseif space == 'DeviceGray' then
      r = color[1]
      g, b = r, r
    elseif space == 'DeviceCMYK' then
      local c, m, y, k = color[1], color[2], color[3], color[4]
      c, m, y = c+k, m+k, y+k
      r, g, b = c >= 1 and 0 or 1-c, m >= 1 and 0 or 1-m, y >= 1 and 0 or 1-y
    elseif space == 'PDF' then
      r, g, b = 0, 0, 0
      print('???', 'torgb', color[1])
    else
      r, g, b = 0, 0, 0
    end
    push(r)
    push(g)
    push(b)
  end,
  currenthsbcolor = function()
    systemdict.value.currentrgbcolor()
    local b = pop_num()
    local g = pop_num()
    local r = pop_num()
    local M, m = math.max(r, g, b), math.min(r, g, b)
    local H
    if M == m then
      H = 0
    elseif M == r then
      H = (g-b)/(M-m) / 6
      if H < 0 then H = H + 1 end
    elseif M == g then
      H = (b-r)/(M-m) / 6 + 1/3
    elseif assert(M == b) then
      H = (r-g)/(M-m) / 6 + 2/3
    end
    local S = M == 0 and 0 or (M-m)/M
    local B = M
    push(H)
    push(S)
    push(B)
  end,
  currentfont = function()
    local f = graphics_stack[#graphics_stack].font
    if f then
      push(f)
    else
      push{kind = 'dict', value = {
         FID = font.current(),
         FontMatrix = {kind = 'array', value = {1, 0, 0, 1, 0, 0}},
         FontName = {kind = 'name', value = tex.fontname(font.current())},
         FontType = 0x1CA,
       }}
    end
  end,

  gsave = function()
    local bbox = graphics_stack[#graphics_stack].bbox
    graphics_stack[#graphics_stack+1] = table.copy(graphics_stack[#graphics_stack], bbox and {[bbox] = {}})
    graphics_stack[#graphics_stack].saved_delayed = delayed
    delayed = {
      text = {},
      matrix = {1, 0, 0, 1, 0, 0},
    }
  end,
  grestore = function()
    local state = graphics_stack[#graphics_stack]
    local saved_delayed = state.saved_delayed
    if saved_delayed then
      delayed = saved_delayed
    else
      pdfprint'Q'
      reset_delayed(delayed)
    end
    local upper_state = graphics_stack[#graphics_stack-1]
    local upper_bbox = upper_state.bbox
    if upper_bbox then
      local bbox = assert(state.bbox)
      while upper_bbox ~= bbox do
        bbox = merge_bbox(bbox, bbox.next or upper_bbox)
      end
    end
    graphics_stack[#graphics_stack] = nil
  end,

  setglobal = pop_bool,

  flush = function()
    io.stdout:flush()
  end,
  print = function()
    local msg = pop_string()
    io.stdout:write(msg.value)
  end,
  stack = function()
    for i=#operand_stack, 1, -1 do
      texio.write_nl('term and log', ps_to_string(operand_stack[i]))
    end
  end,
  ['='] = function()
    texio.write_nl('term and log', ps_to_string(pop()))
  end,
  ['=='] = function() -- FIXME: Should give a better representation
    texio.write_nl('term and log', ps_to_string(pop()))
  end,

  stringwidth = function()
    local state = graphics_stack[#graphics_stack]
    local rawpsfont = assert(state.font, 'invalidfont')
    local str = pop_string().value
    local psfont = rawpsfont.value
    local fid = psfont.FID
    local matrix = psfont.FontMatrix.value
    local fonttype = psfont.FontType
    if fonttype ~= 0x1CA and fonttype ~= 3 then
      texio.write_nl'luapstricks: Attempt to use unsupported font type.'
      ps_error('invalidfont')
    end
    local w = 0
    if fonttype == 0x1CA then
      local characters = assert(font.getfont(fid)).characters
      for b in string.bytes(str) do
        local char = characters[b]
        w = w + (char and char.width or 0)
      end
      w = w/65781.76
    elseif fonttype == 3 then
      local saved_delayed = delayed
      delayed = {
        text = {},
        matrix = {1, 0, 0, 1, 0, 0},
      }
      local saved_saved_delayed = state.saved_delayed
      state.saved_delayed = nil
      local saved_pdfprint = pdfprint
      pdfprint = gobble
      for b in string.bytes(str) do
        systemdict.value.gsave()
        local state = graphics_stack[#graphics_stack]
        state.current_point, state.current_path = nil
        push(rawpsfont)
        push(b)
        local this_w
        char_width_storage = function(width)
          this_w = width
        end
        execute_tok(psfont.BuildChar) -- FIXME(maybe): Switch to BuildGlyph?
        systemdict.value.grestore()
        w = w + assert(this_w, 'Type 3 character failed to set width')
        update_matrix(1, 0, 0, 1, this_w, 0)
      end
      update_matrix(1, 0, 0, 1, -w, 0)
      pdfprint = saved_pdfprint
      state.saved_delayed = saved_saved_delayed
      delayed = saved_delayed
    end
    local x, y = matrix_transform(w, 0,
      matrix[1], matrix[2],
      matrix[3], matrix[4],
      0, 0)
    push(x)
    push(y)
  end,
  ashow = function()
    local str, arg3 = pop_string()
    local ay, arg2 = pop_num(arg3)
    local ax, arg1 = pop_num(arg2, arg3)
    local res, err = generic_show(str, ax, ay)
    if not res then
      ps_error(err, arg1, arg2, arg3)
    end
  end,
  show = function()
    local str, orig = pop_string()
    local res, err = generic_show(str)
    if not res then
      ps_error(err, orig)
    end
  end,
  definefont = function()
    local fontdict, raw_fontdict = pop_dict()
    local fontkey = pop_key()
    fontdict.FontMatrix = fontdict.FontMatrix or {kind = 'array', value = {1, 0, 0, 1, 0, 0}}
    if assert(fontdict.FontType) == 0x1CA then
      local fontname = fontdict.FontName
      if type(fontname) == 'table' and fontname.kind == 'name' then
        fontname = fontname.value
      elseif type(fontname) ~= 'string' then
        pushs(fontkey, raw_fontdict)
        ps_error'typecheck'
      end
      local fid = fonts.definers.read(fontname, 65782)
      if not fid then ps_error'invalidfont' end
      if not tonumber(fid) then
        local data = fid
        fid = font.define(data)
        fonts.definers.register(data, fid)
      end
      fontdict.FID = fid
    elseif fontdict.FontType == 3 then
    else
      texio.write_nl'luapstricks: definefont has been called with a font type which is not supported by luapstricks. I will continue, but attempts to use this font will fail.'
    end
    FontDirectory[fontkey] = raw_fontdict
    push(raw_fontdict)
  end,
  makefont = function()
    local m = pop_array().value
    if #m ~= 6 then error'Unexpected size of matrix' end
    local fontdict = pop_dict()
    local new_fontdict = {}
    for k,v in next, fontdict do
      new_fontdict[k] = v
    end
    local old_m = assert(fontdict.FontMatrix, 'invalidfont').value
    new_fontdict.FontMatrix = {kind = 'array', value = {
      old_m[1] * m[1] + old_m[2] * m[3],        old_m[1] * m[2] + old_m[2] * m[4],
      old_m[3] * m[1] + old_m[4] * m[3],        old_m[3] * m[2] + old_m[4] * m[4],
      old_m[5] * m[1] + old_m[6] * m[3] + m[5], old_m[5] * m[2] + old_m[6] * m[4] + m[6],
    }}
    push{kind = 'dict', value = new_fontdict}
  end,
  scalefont = function()
    local factor = pop_num()
    local fontdict = pop_dict()
    local new_fontdict = {}
    for k,v in next, fontdict do
      new_fontdict[k] = v
    end
    local old_m = assert(fontdict.FontMatrix, 'invalidfont').value
    new_fontdict.FontMatrix = {kind = 'array', value = {
      factor * old_m[1], factor * old_m[2],
      factor * old_m[3], factor * old_m[4],
      factor * old_m[5], factor * old_m[6],
    }}
    push{kind = 'dict', value = new_fontdict}
  end,
  setfont = function()
    local _, _, fontdict = pop_dict()
    local state = graphics_stack[#graphics_stack]
    state.font = fontdict
  end,
  ['.findfontid'] = function()
    local fid = pop_int()

    if font.frozen(fid) == nil then
      push(fid)
      ps_error'invalidfont'
    end
    local fontsize_inv = 65782/pdf.getfontsize(fid)
    local fontname = tex.fontname(fid)
    return push{kind = 'dict', value = {
      FID = fid,
      FontMatrix = {kind = 'array', value = {fontsize_inv, 0, 0, fontsize_inv, 0, 0}},
      FontName = {kind = 'name', value = fontname},
      FontType = 0x1CA,
    }}
  end,
  findfont = function()
    local fontname = pop_key()
    local fontdict = FontDirectory[fontname]
    if fontdict then push(fontdict) return end

    fontname = font_aliases[fontname] or fontname
    local fid = fonts.definers.read(fontname, 65782)
    if not fid then ps_error'invalidfont' end
    if not tonumber(fid) then
      local data = fid
      fid = font.define(data)
      fonts.definers.register(data, fid)
    end
    return push{kind = 'dict', value = {
      FID = fid,
      FontMatrix = {kind = 'array', value = {1, 0, 0, 1, 0, 0}},
      FontName = {kind = 'name', value = fontname},
      FontType = 0x1CA,
    }}
  end,
  selectfont = function()
    systemdict.value.exch()
    systemdict.value.findfont()
    systemdict.value.exch()
    if type(operand_stack[#operand_stack]) == 'number' then
      systemdict.value.scalefont()
    else
      systemdict.value.makefont()
    end
    systemdict.value.setfont()
  end,

  setcharwidth = function()
    -- Pop and ignore the advance height -- FIXME(maybe)
    pop_num()
    assert(char_width_storage, 'undefined')(pop_num())
    char_width_storage = nil
  end,
  setcachedevice = function()
    -- First pop and ignore the bounding box
    pop_num()
    pop_num()
    pop_num()
    pop_num()
    -- Fallback to setcharwidth
    systemdict.value.setcharwidth()
  end,
  setcachedevice2 = function()
    -- First pop additional entries for setccachedevice2 -- TODO: Implement other writing modes
    pop_num()
    pop_num()
    pop_num()
    pop_num()
    -- Fallback to setcachedevice
    systemdict.value.setcachedevice()
  end,

  findresource = function()
    local category = pop_key()
    local catdict = ResourceCategories.value[category]
    if not catdict then
      push(category)
      print('undefined resource category', category)
      ps_error'undefined'
    end
    local dict_height = #dictionary_stack + 1
    dictionary_stack[dict_height] = catdict
    execute_tok'FindResource'
    if #dictionary_stack ~= dict_height or dictionary_stack[dict_height] ~= catdict then
      error'Messed up dictionary stack in custom resource'
    end
    dictionary_stack[dict_height] = nil
  end,
  resourcestatus = function()
    local category = pop_key()
    local catdict = ResourceCategories.value[category]
    if not catdict then
      push(category)
      print('undefined resource category', category)
      ps_error'undefined'
    end
    local dict_height = #dictionary_stack + 1
    dictionary_stack[dict_height] = catdict
    execute_tok'ResourceStatus'
    if #dictionary_stack ~= dict_height or dictionary_stack[dict_height] ~= catdict then
      error'Messed up dictionary stack in custom resource'
    end
    dictionary_stack[dict_height] = nil
  end,
  resourceforall = function()
    local category = pop_key()
    local catdict = ResourceCategories.value[category]
    if not catdict then
      push(category)
      print('undefined resource category', category)
      ps_error'undefined'
    end
    local dict_height = #dictionary_stack + 1
    dictionary_stack[dict_height] = catdict
    execute_tok'ResourceForAll'
    if #dictionary_stack ~= dict_height or dictionary_stack[dict_height] ~= catdict then
      error'Messed up dictionary stack in custom resource'
    end
    dictionary_stack[dict_height] = nil
  end,
  defineresource = function()
    local category = pop_key()
    local catdict = ResourceCategories.value[category]
    if not catdict then
      push(category)
      print('undefined resource category', category)
      ps_error'undefined'
    end
    local dict_height = #dictionary_stack + 1
    dictionary_stack[dict_height] = catdict
    execute_tok'DefineResource'
    if #dictionary_stack ~= dict_height or dictionary_stack[dict_height] ~= catdict then
      error'Messed up dictionary stack in custom resource'
    end
    dictionary_stack[dict_height] = nil
  end,
  undefineresource = function()
    local category = pop_key()
    local catdict = ResourceCategories.value[category]
    if not catdict then
      push(category)
      print('undefined resource category', category)
      ps_error'undefined'
    end
    local dict_height = #dictionary_stack + 1
    dictionary_stack[dict_height] = catdict
    execute_tok'UndefineResource'
    if #dictionary_stack ~= dict_height or dictionary_stack[dict_height] ~= catdict then
      error'Messed up dictionary stack in custom resource'
    end
    dictionary_stack[dict_height] = nil
  end,

  realtime = function()
    push(os.gettimeofday() * 1000 // 1)
  end,

  rrand = function()
    push(rrand())
  end,
  srand = function()
    srand(pop_int())
  end,
  rand = function()
    push(rand())
  end,

  readonly = function() end, -- Concept not implemented
  type = function()
    local val = pop()
    local tval = type(val)
    if tval == 'table' and val.kind == 'executable' then
      val = val.value
      tval = type(val)
    end
    local tname
    if tval == 'string' then
      tname = 'nametype'
    elseif tval == 'number' then
      tname = math.type(val) == 'integer' and 'integertype' or 'realtype' 
    elseif tval == 'boolean' then
      tname = 'booleantype'
    elseif tval == 'function' then
      tname = 'operatortype'
    elseif tval == 'table' then
      local kind = val.kind
      if kind == 'name' then
        tname = 'nametype'
      elseif kind == 'operator' then
        tname = 'operatortype'
      elseif kind == 'array' then
        tname = 'arraytype'
      elseif kind == 'dict' then
        tname = 'dicttype'
      elseif kind == 'dict' then
        tname = 'dicttype'
      elseif kind == 'null' then
        tname = 'nulltype'
      elseif kind == 'mark' then
        tname = 'nulltype'
      elseif kind == 'string' then
        tname = 'stringtype'
      else
        assert(false, 'Unexpected type')
      end
    else
      assert(false, 'Unexpected type')
    end
    push(tname)
    -- filetype
    -- fonttype
    -- gstatetype (LanguageLevel 2)
    -- packedarraytype (LanguageLevel 2)
    -- savetype
  end,
  xcheck = function()
    local a = pop()
    local ta = type(a)
    push(ta == 'function' or ta == 'name' or (ta == 'table' and a.kind == 'executable'))
  end,
  cvlit = function()
    local a = pop()
    local ta = type(a)
    if (ta == 'table' and a.kind == 'executable') or ta == 'string' or ta == 'function' then
      return push(a.value)
    end
    if ta == 'string' then
      return push{kind = 'name', value = a}
    end
    if ta == 'function' then
      return push{kind = 'operator', value = a}
    end
    return push(a)
  end,
  cvx = function()
    local a = pop()
    local ta = type(a)
    if (ta == 'table' and a.kind == 'executable') or ta == 'string' or ta == 'function' then
      return push(a)
    elseif ta == 'table' and (a.kind == 'operator' or a.kind == 'name') then
      return push(a.value)
    else
      return push{kind = 'executable', value = a}
    end
  end,
  exec = function()
    return execute_tok((pop()))
  end,
  stopped = function()
    local proc = pop()
    local success, err = pcall(execute_tok, proc)
    if success then
      push(false)
    elseif err == 'stop' or true then -- Since we don implement error handlers, all errors act like their error handler included "stop"
      push(true)
    end
  end,
  stop = function()
    error'stop'
  end,
  exit = function()
    error(exitmarker)
  end,
  quit = function()
    os.exit()
  end,
  run = function()
    local filename = pop_string().value
    local resolved = kpse.find_file(filename, 'PostScript header')
    if not resolved then
      error(string.format('Unable to find file %q.', filename))
    end
    local f = assert(io.open(resolved, 'rb'))
    local data = maybe_decompress(f:read'a')
    f:close()
    return execute_tok{kind = 'executable', value = {kind = 'string', value = data}}
  end,

  -- We don't implement local/global separation, so we ignore setglobal and always report currentglobal as true
  setglobal = function()
    pop()
  end,
  currentglobal = function()
    push(true)
  end,

  closefile = function()
    local f = pop()
    f:close()
  end,
  file = function()
    local access = pop_string()
    local orig_filename = pop_string()
    local filename = orig_filename.value
    if access.value:sub(1, 1) == 'a' then
      filename = kpse.find_file(filename)
      if not filename then
        push(orig_filename)
        push(access)
        ps_error'undefinedfilename'
      end
    end
    if access.value == '' then
      push(orig_filename)
      push(access)
      ps_error'invalidfileaccess'
    end
    local f = io.open(filename, access.value)
    if not f then
      push(orig_filename)
      push(access)
      ps_error'invalidfileaccess'
    end
    push(f)
  end,
  write = function()
    local data = pop_num()
    local f = pop()
    data = data % 256
    f:write(string.char(data))
  end,
  writestring = function()
    local data = pop_string().value
    local f = pop()
    f:write(data)
  end,
  readstring = function()
    local target = pop_string()
    local f = pop()
    local data = f:read(#target.value)
    if #target.value == #data then
      target.value = data
      push(target)
      push(true)
      systemdict.value.stack()
    else
      target = str_view(target, 1, #data)
      target.value = data
      push(target)
      push(false)
      systemdict.value.stack()
    end
  end,
  readline = function()
    local target = pop_string()
    local f = pop()
    local data = f:read'L' -- TODO: \r should be accepted as EOL marker too
    if data then
      if #data > #target.value then
        push(f)
        push(target)
        ps_error'rangecheck'
      end
      target = str_view(target, 1, #data)
      target.value = data
      push(target)
      push(true)
    else
      push{kind = 'string', value = ''}
      push(false)
    end
  end,

  token = function()
    local arg = pop()
    if type(arg) ~= 'table' or arg.kind ~= 'string' then
      push(arg)
      if type(arg) == 'userdata' and arg.read then
        error'token applied to file arguments is no yet implemented'
      else
        ps_error'typecheck'
      end
    end
    local str = arg.value
    local tok, after = l.match(any_object * l.Cp(), str)
    if after == nil then
      if l.match(whitespace^-1 * -1, str) then
        push(false)
      else
        push(arg)
        ps_error'syntaxerror'
      end
    else
      push(str_view(arg, after, #str - after + 1))
      push(tok)
      push(true)
    end
  end,

  ['.trackbbox'] = function()
    local state = graphics_stack[#graphics_stack]
    flush_delayed()
    state.bbox = { next = state.bbox, start = true }
  end,
  -- Trackedbbox should only be invoked if the current matrix is essentially the same
  -- as in the corresponding .trackbbox, otherwise everything gets messed up.
  -- This isn't checked, mostly because we don't want a check to be too sensitive.
  ['.trackedbbox'] = function()
    local state = graphics_stack[#graphics_stack]
    local bbox = state.bbox
    if not bbox then
      error'trackedbbox without matching trackbbox'
    end
    while not bbox.start do
      if not bbox.next then
        error'Illegal nesting of trackbbox/trackedbbox and gsave/grestore'
      end
      bbox = merge_bbox(bbox, bbox.next)
    end
    state.bbox = bbox.next and merge_bbox(bbox, bbox.next)
    push(bbox[1] or 0)
    push(bbox[2] or 0)
    push(bbox[3] or 0)
    push(bbox[4] or 0)
  end,

  revision = 1000,
  ['true'] = true,
  ['false'] = false,
  systemdict = systemdict,
  statusdict = statusdict,
  globaldict = globaldict,
  FontDirectory = FontDirectory,

  ISOLatin1Encoding = {kind = 'array', value = {
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = 'space'},
    {kind = 'name', value = 'exclam'},
    {kind = 'name', value = 'quotedbl'},
    {kind = 'name', value = 'numbersign'},
    {kind = 'name', value = 'dollar'},
    {kind = 'name', value = 'percent'},
    {kind = 'name', value = 'ampersand'},
    {kind = 'name', value = 'quoteright'},
    {kind = 'name', value = 'parenleft'},
    {kind = 'name', value = 'parenright'},
    {kind = 'name', value = 'asterisk'},
    {kind = 'name', value = 'plus'},
    {kind = 'name', value = 'comma'},
    {kind = 'name', value = 'minus'},
    {kind = 'name', value = 'period'},
    {kind = 'name', value = 'slash'},
    {kind = 'name', value = 'zero'},
    {kind = 'name', value = 'one'},
    {kind = 'name', value = 'two'},
    {kind = 'name', value = 'three'},
    {kind = 'name', value = 'four'},
    {kind = 'name', value = 'five'},
    {kind = 'name', value = 'six'},
    {kind = 'name', value = 'seven'},
    {kind = 'name', value = 'eight'},
    {kind = 'name', value = 'nine'},
    {kind = 'name', value = 'colon'},
    {kind = 'name', value = 'semicolon'},
    {kind = 'name', value = 'less'},
    {kind = 'name', value = 'equal'},
    {kind = 'name', value = 'greater'},
    {kind = 'name', value = 'question'},
    {kind = 'name', value = 'at'},
    {kind = 'name', value = 'A'},
    {kind = 'name', value = 'B'},
    {kind = 'name', value = 'C'},
    {kind = 'name', value = 'D'},
    {kind = 'name', value = 'E'},
    {kind = 'name', value = 'F'},
    {kind = 'name', value = 'G'},
    {kind = 'name', value = 'H'},
    {kind = 'name', value = 'I'},
    {kind = 'name', value = 'J'},
    {kind = 'name', value = 'K'},
    {kind = 'name', value = 'L'},
    {kind = 'name', value = 'M'},
    {kind = 'name', value = 'N'},
    {kind = 'name', value = 'O'},
    {kind = 'name', value = 'P'},
    {kind = 'name', value = 'Q'},
    {kind = 'name', value = 'R'},
    {kind = 'name', value = 'S'},
    {kind = 'name', value = 'T'},
    {kind = 'name', value = 'U'},
    {kind = 'name', value = 'V'},
    {kind = 'name', value = 'W'},
    {kind = 'name', value = 'X'},
    {kind = 'name', value = 'Y'},
    {kind = 'name', value = 'Z'},
    {kind = 'name', value = 'bracketleft'},
    {kind = 'name', value = 'backslash'},
    {kind = 'name', value = 'bracketright'},
    {kind = 'name', value = 'asciicircum'},
    {kind = 'name', value = 'underscore'},
    {kind = 'name', value = 'quoteleft'},
    {kind = 'name', value = 'a'},
    {kind = 'name', value = 'b'},
    {kind = 'name', value = 'c'},
    {kind = 'name', value = 'd'},
    {kind = 'name', value = 'e'},
    {kind = 'name', value = 'f'},
    {kind = 'name', value = 'g'},
    {kind = 'name', value = 'h'},
    {kind = 'name', value = 'i'},
    {kind = 'name', value = 'j'},
    {kind = 'name', value = 'k'},
    {kind = 'name', value = 'l'},
    {kind = 'name', value = 'm'},
    {kind = 'name', value = 'n'},
    {kind = 'name', value = 'o'},
    {kind = 'name', value = 'p'},
    {kind = 'name', value = 'q'},
    {kind = 'name', value = 'r'},
    {kind = 'name', value = 's'},
    {kind = 'name', value = 't'},
    {kind = 'name', value = 'u'},
    {kind = 'name', value = 'v'},
    {kind = 'name', value = 'w'},
    {kind = 'name', value = 'x'},
    {kind = 'name', value = 'y'},
    {kind = 'name', value = 'z'},
    {kind = 'name', value = 'braceleft'},
    {kind = 'name', value = 'bar'},
    {kind = 'name', value = 'braceright'},
    {kind = 'name', value = 'asciitilde'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = 'dotlessi'},
    {kind = 'name', value = 'grave'},
    {kind = 'name', value = 'acute'},
    {kind = 'name', value = 'circumflex'},
    {kind = 'name', value = 'tilde'},
    {kind = 'name', value = 'macron'},
    {kind = 'name', value = 'breve'},
    {kind = 'name', value = 'dotaccent'},
    {kind = 'name', value = 'dieresis'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = 'ring'},
    {kind = 'name', value = 'cedilla'},
    {kind = 'name', value = '.notdef'},
    {kind = 'name', value = 'hungarumlaut'},
    {kind = 'name', value = 'ogonek'},
    {kind = 'name', value = 'caron'},
    {kind = 'name', value = 'space'},
    {kind = 'name', value = 'exclamdown'},
    {kind = 'name', value = 'cent'},
    {kind = 'name', value = 'sterling'},
    {kind = 'name', value = 'currency'},
    {kind = 'name', value = 'yen'},
    {kind = 'name', value = 'brokenbar'},
    {kind = 'name', value = 'section'},
    {kind = 'name', value = 'dieresis'},
    {kind = 'name', value = 'copyright'},
    {kind = 'name', value = 'ordfeminine'},
    {kind = 'name', value = 'guillemotleft'},
    {kind = 'name', value = 'logicalnot'},
    {kind = 'name', value = 'hyphen'},
    {kind = 'name', value = 'registered'},
    {kind = 'name', value = 'macron'},
    {kind = 'name', value = 'degree'},
    {kind = 'name', value = 'plusminus'},
    {kind = 'name', value = 'twosuperior'},
    {kind = 'name', value = 'threesuperior'},
    {kind = 'name', value = 'acute'},
    {kind = 'name', value = 'mu'},
    {kind = 'name', value = 'paragraph'},
    {kind = 'name', value = 'periodcentered'},
    {kind = 'name', value = 'cedilla'},
    {kind = 'name', value = 'onesuperior'},
    {kind = 'name', value = 'ordmasculine'},
    {kind = 'name', value = 'guillemotright'},
    {kind = 'name', value = 'onequarter'},
    {kind = 'name', value = 'onehalf'},
    {kind = 'name', value = 'threequarters'},
    {kind = 'name', value = 'questiondown'},
    {kind = 'name', value = 'Agrave'},
    {kind = 'name', value = 'Aacute'},
    {kind = 'name', value = 'Acircumflex'},
    {kind = 'name', value = 'Atilde'},
    {kind = 'name', value = 'Adieresis'},
    {kind = 'name', value = 'Aring'},
    {kind = 'name', value = 'AE'},
    {kind = 'name', value = 'Ccedilla'},
    {kind = 'name', value = 'Egrave'},
    {kind = 'name', value = 'Eacute'},
    {kind = 'name', value = 'Ecircumflex'},
    {kind = 'name', value = 'Edieresis'},
    {kind = 'name', value = 'Igrave'},
    {kind = 'name', value = 'Iacute'},
    {kind = 'name', value = 'Icircumflex'},
    {kind = 'name', value = 'Idieresis'},
    {kind = 'name', value = 'Eth'},
    {kind = 'name', value = 'Ntilde'},
    {kind = 'name', value = 'Ograve'},
    {kind = 'name', value = 'Oacute'},
    {kind = 'name', value = 'Ocircumflex'},
    {kind = 'name', value = 'Otilde'},
    {kind = 'name', value = 'Odieresis'},
    {kind = 'name', value = 'multiply'},
    {kind = 'name', value = 'Oslash'},
    {kind = 'name', value = 'Ugrave'},
    {kind = 'name', value = 'Uacute'},
    {kind = 'name', value = 'Ucircumflex'},
    {kind = 'name', value = 'Udieresis'},
    {kind = 'name', value = 'Yacute'},
    {kind = 'name', value = 'Thorn'},
    {kind = 'name', value = 'germandbls'},
    {kind = 'name', value = 'agrave'},
    {kind = 'name', value = 'aacute'},
    {kind = 'name', value = 'acircumflex'},
    {kind = 'name', value = 'atilde'},
    {kind = 'name', value = 'adieresis'},
    {kind = 'name', value = 'aring'},
    {kind = 'name', value = 'ae'},
    {kind = 'name', value = 'ccedilla'},
    {kind = 'name', value = 'egrave'},
    {kind = 'name', value = 'eacute'},
    {kind = 'name', value = 'ecircumflex'},
    {kind = 'name', value = 'edieresis'},
    {kind = 'name', value = 'igrave'},
    {kind = 'name', value = 'iacute'},
    {kind = 'name', value = 'icircumflex'},
    {kind = 'name', value = 'idieresis'},
    {kind = 'name', value = 'eth'},
    {kind = 'name', value = 'ntilde'},
    {kind = 'name', value = 'ograve'},
    {kind = 'name', value = 'oacute'},
    {kind = 'name', value = 'ocircumflex'},
    {kind = 'name', value = 'otilde'},
    {kind = 'name', value = 'odieresis'},
    {kind = 'name', value = 'divide'},
    {kind = 'name', value = 'oslash'},
    {kind = 'name', value = 'ugrave'},
    {kind = 'name', value = 'uacute'},
    {kind = 'name', value = 'ucircumflex'},
    {kind = 'name', value = 'udieresis'},
    {kind = 'name', value = 'yacute'},
    {kind = 'name', value = 'thorn'},
    {kind = 'name', value = 'ydieresis'},
  }}
}}
systemdict.value.systemdict = systemdict
dictionary_stack = {systemdict, globaldict, userdict, userdict.value.TeXDict}
-- local execution_stack = {} -- Currently not implemented

-- Quite some stuff is missing here since these aren't implemented yet. Anyway mostly useful for testing.
ResourceCategories.value.Font = {kind = 'dict', value = {
  Category = {kind = 'name', value = 'Font'},
  InstanceType = 'dicttype',
  DefineResource = systemdict.value.definefont,
  FindResource = systemdict.value.findfont,
}}

ResourceCategories.value.Generic = {kind = 'dict', value = {
  Category = {kind = 'name', value = 'Generic'},
  DefineResource = function()
    local instance = pop()
    local key = pop_key()
    execute_tok'.Instances'
    local instances = pop_dict()
    instances[key] = instance
    push(instance)
  end,
  UndefineResource = function()
    local key = pop_key()
    execute_tok'.Instances'
    local instances = pop_dict()
    instances[key] = nil
  end,
  FindResource = function()
    local key = pop_key()
    execute_tok'.Instances'
    local instances = pop_dict()
    local instance = instances[key]
    if instance then
      push(instance)
      return
    end
    push(key)
    ps_error'undefinedresource'
  end,
  -- ResourceStatus = function()
  --   local key = pop_key()
  --   execute_tok'.Instances'
  --   local instances = pop_dict()
  --   local instance = instances[key]
  --   if instance then
  --     push(instance)
  --     return
  --   end
  --   push(key)
  --   ps_error'undefinedresource'
  -- end,
  -- ResourceForAll = function()
  --   local key = pop_key()
  --   execute_tok'.Instances'
  --   local instances = pop_dict()
  --   local instance = instances[key]
  --   if instance then
  --     push(instance)
  --     return
  --   end
  --   push(key)
  --   ps_error'undefinedresource'
  -- end,
  ['.Instances'] = {kind = 'dict', value = {}},
}}

local register_texbox do
  local meta = {__gc = function(t) node.direct.free(t.box) end}
  local dict = {}
  ResourceCategories.value['.TeXBox'] = {kind = 'dict', value = {
    Category = {kind = 'name', value = '.TeXBox'},
    DefineResource = function()
      push{kind = 'name', value = '.TeXBox'}
      ps_error'undefined'
    end,
    UndefineResource = function()
      local key = pop_key()
      dict[key] = nil
    end,
    FindResource = function()
      local key = pop_key()
      local instance = dict[key]
      if instance then
        push(instance)
        return
      end
      push(key)
      ps_error'undefinedresource'
    end,
  }}
  local id = 0
  function register_texbox(box)
    id = id + 1
    box = setmetatable({box = node.direct.todirect(box)}, meta)
    local op = function()
      flush_delayed()
      local state = graphics_stack[#graphics_stack]
      local w, h, d = node.direct.dimensions(box.box)
      register_point(state, 0, -d/65781.76)
      register_point(state, w/65781.76, h/65781.76)
      vf.push()
      vf.node(box.box)
      vf.pop()
    end
    lua_node_lookup[op] = box
    dict[id] = op
    return id
  end
end

ResourceCategories.value.Category = {kind = 'dict', value = {
  Category = {kind = 'name', value = 'Generic'},
  InstanceType = 'dicttype',
  DefineResource = function()
    local instance = pop()
    local key = pop_key()
    ResourceCategories.value[key] = instance
    push(instance)
  end,
  UndefineResource = function()
    local key = pop_key()
    ResourceCategories.value[key] = nil
  end,
  FindResource = function()
    local key = pop_key()
    local instance = ResourceCategories.value[key]
    if instance then
      push(instance)
      return
    end
    push(key)
    ps_error'undefinedresource'
  end,
  -- ResourceStatus = function()
  --   local key = pop_key()
  --   execute_tok'.Instances'
  --   local instances = pop_dict()
  --   local instance = instances[key]
  --   if instance then
  --     push(instance)
  --     return
  --   end
  --   push(key)
  --   ps_error'undefinedresource'
  -- end,
  -- ResourceForAll = function()
  --   local key = pop_key()
  --   execute_tok'.Instances'
  --   local instances = pop_dict()
  --   local instance = instances[key]
  --   if instance then
  --     push(instance)
  --     return
  --   end
  --   push(key)
  --   ps_error'undefinedresource'
  -- end,
}}

function execute_tok(tok, suppress_proc)
  local ttok = type(tok)
  if ttok == 'string' then
    return execute_tok(lookup(tok))
  elseif ttok == 'function' then
    return tok()
  elseif ttok == 'table' and tok.kind == 'executable' then
    local vtok = tok.value
    ttok = type(vtok)
    if suppress_proc and ttok == 'table' and tok.value.kind == 'array' then
      return push(tok)
    end
    if ttok == 'table' then
      local kind = vtok.kind
      if kind == 'array' then
        return execute_ps(vtok.value)
      elseif kind == 'string' then
        return execute_ps(assert(parse_ps(vtok.value), 'syntaxerror'))
      else
        error'Unimplemented'
      end
    elseif ttok == 'number' then
      return push(tok)
    else
      error'Unimplemented'
    end
  else
    return push(tok)
  end
end

function execute_ps(tokens)
  for i=1, #tokens do
    execute_tok(tokens[i], true)
  end
end
local any_object_or_end = any_object * l.Cp() + whitespace^-1 * -1 * l.Cc(nil) + l.Cp() * l.Cc(false)
function execute_string(str, context)
  local pos = 1
  while true do
    local tok
    tok, pos = any_object_or_end:match(str, pos)
    if pos then
      local success, err = pcall(execute_tok, tok, true)
      if not success then
        if context and type(err) == 'table' and err.pserror and not err.context then
          err.tok = tok
          err.context = context
        end
        error(err)
      end
    elseif pos == false then
      ps_error'syntaxerror'
    else
      break
    end
  end
end

-- If x, y shall be present iff direct ~= 'immediate'
local function outer_execute(tokens, direct, context, x, y)
  local TeXDict = userdict.value.TeXDict.value
  local saved_ocount = TeXDict.ocount
  local height = #operand_stack
  TeXDict.ocount = height
  local graphics_height
  if direct ~= 'immediate' then
    operand_stack[height + 1], operand_stack[height + 2] = x/65781.76, y/65781.76
    if direct then
      systemdict.value.moveto()
    else
      graphics_height = #graphics_stack
      systemdict.value.gsave()
      systemdict.value.translate()
    end
  end
  local success, err = pcall(execute_string, tokens, context)
  if not success then
    if type(err) == 'table' and err.pserror then
      tex.error(string.format('luapstricks: %q error occured while executing PS code from %q', err.pserror, err.context), {
        string.format('The error occured while executing the PS command %q.\n%s', err.tok, err.trace)
      })
    else
      error(err, 0)
    end
  end
  flush_delayed()
  if not direct then
    systemdict.value.grestore()
    if graphics_height ~= #graphics_stack then
      if graphics_height < #graphics_stack then
        texio.write_nl"luapstricks: PS block contains unbalanced gsave. grestore will be executed to compensate."
        repeat
          systemdict.value.grestore()
        until graphics_height == #graphics_stack
      else
        texio.write_nl"luapstricks: PS block contains unbalanced grestore."
      end
    end
    height = TeXDict.ocount or height
    local new_height = #operand_stack
    assert(new_height >= height)
    for k = height + 1, new_height do
      operand_stack[k] = nil
    end
    TeXDict.ocount = saved_ocount
  end
end

local ps_tokens, ps_direct, ps_context, ps_pos_x, ps_pos_y
local fid = font.define{
  name = 'dummy virtual font for PS rendering',
  -- type = 'virtual',
  characters = {
    [0x1F3A8] = {
      commands = {
        {'lua', function(fid)
          local n = node.new('glyph', 256)
          n.font = fid
          n.char = 1
          assert(not ps_pos_x)
          ps_pos_x, ps_pos_y = pdf.getpos()
          n.xoffset = -ps_pos_x
          n.yoffset = -ps_pos_y
          n = node.hpack(n)
          vf.node(node.direct.todirect(n))
          node.free(n)
        end}
      }
    },
    [1] = {
      commands = {
        {'lua', function()
          local tokens, x, y = assert(ps_tokens), ps_pos_x, ps_pos_y
          ps_tokens, ps_pos_x, ps_pos_y = nil
          return outer_execute(tokens, ps_direct, ps_context, x, y)
        end}
      }
    },
  },
}

local func = luatexbase.new_luafunction'luaPST'
token.set_lua('luaPST', func, 'protected')
lua.get_functions_table()[func] = function()
  local readstate = status.readstate or status
  local context = string.format('%s:%i', readstate.filename, readstate.linenumber)
  local direct = token.scan_keyword'immediate' and 'immediate' or token.scan_keyword'direct' and 'direct'
  local file = token.scan_keyword'file'
  local tokens = token.scan_argument(true)
  if file then
    context = tokens
    local f = io.open(kpse.find_file(tokens, 'PostScript header'), 'r')
    tokens = f:read'a'
    f:close()
  end
  if direct == 'immediate' then
    local saved_pdfprint = pdfprint
    pdfprint = no_pdfprint_allowed
    outer_execute(tokens, direct, context)
    pdfprint = saved_pdfprint
  else
    local n = node.new('whatsit', late_lua_sub)
    setwhatsitfield(n, 'data', function(n)
      assert(not ps_tokens)
      ps_tokens = tokens
      ps_direct = direct
      ps_context = context

      local nn = node.new('glyph')
      nn.subtype = 256
      nn.font, nn.char = fid, 0x1F3A8
      local list = node.new('hlist')
      list.head = nn
      list.direction = 0
      node.insert_after(n, n, list)
    end)
    node.write(n)
  end
end
tex.runtoks(function()
  tex.sprint[[\protected\def\luaPSTheader{\luaPST direct file}]]
end)

do
  func = luatexbase.new_luafunction'luaPSTcolor'
  token.set_lua('luaPSTcolor', func)
  local ps_rgb = 'rgb ' * l.C(l.P(1)^0) * l.Cc' setrgbcolor' * l.Cc'rgb '
  local ps_cmyk = 'cmyk ' * l.C(l.P(1)^0) * l.Cc' setcmykcolor' * l.Cc'cmyk '
  local ps_gray = 'gray ' * l.C(l.P(1)^0) * l.Cc' setgray' * l.Cc'gray '
  local ps_hsb = 'hsb ' * l.C(l.P(1)^0) * l.Cc' sethsbcolor' * l.Cc'hsb '
  local pscolor = ps_rgb + ps_gray + ps_gray + ps_hsb
  local pdf_rgb = l.Cmt(l.C(number * whitespace * number * whitespace * number / 0) * whitespace * 'rg'
                * whitespace * l.C(number * whitespace * number * whitespace * number / 0) * whitespace * 'RG' * -1, function(s, p, a, b)
                  if a == b then
                    return true, a, ' setrgbcolor', 'rgb '
                  else
                    return false
                  end
                end)
  local pdf_cmyk = l.Cmt(l.C(number * whitespace * number * whitespace * number * whitespace * number / 0) * whitespace * 'k'
                 * whitespace * l.C(number * whitespace * number * whitespace * number * whitespace * number / 0) * whitespace * 'K' * -1, function(s, p, a, b)
                  if a == b then
                    return true, a, ' setcmykcolor', 'cmyk '
                  else
                    return false
                  end
                end)
  local pdf_gray = l.Cmt(l.C(number / 0) * whitespace * 'g'
                 * whitespace * l.C(number / 0) * whitespace * 'G' * -1, function(s, p, a, b)
                  if a == b then
                    return true, a, ' setgray', 'gray '
                  else
                    return false
                  end
                end)
  local pdf_other = l.Cs(l.Cc'(' * l.P(1)^0 * l.Cc')') * l.C' setpdfcolor' * l.C'gray '
  local pdfcolor = pdf_rgb + pdf_cmyk + pdf_gray + pdf_other
  local anycolor = pscolor + pdfcolor
  lua.get_functions_table()[func] = function()
    local dvips_format = token.scan_keyword'dvips'
    local result, suffix, prefix = anycolor:match(token.scan_argument())
    tex.sprint(-2, dvips_format and prefix .. result or result .. suffix)
  end
end

func = luatexbase.new_luafunction'luaPSTbox'
token.set_lua('luaPSTbox', func)
lua.get_functions_table()[func] = function()
  local box = register_texbox(token.scan_list())
  tex.sprint(-2, tostring(box))
end