summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/meta-pdf.mkii
blob: 0ba3fb9799d92c501f0832545607c4b7c32b23e2 (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
%D \module
%D   [       file=meta-pdf,
%D        version=2006.06.07,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Conversion to \PDF,
%D         author=Hans Hagen \& others (see text),
%D           date=\currentdate,
%D      copyright=\PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D Formerly known as supp-pdf.tex and supp-mpe.tex.

%D We will clean up the color mess later.

%D These macros are written as generic as possible. Some
%D general support macro's are loaded from a small module
%D especially made for non \CONTEXT\ use. In this module I
%D use a matrix transformation macro written by Tanmoy
%D Bhattacharya. Thanks to extensive testing by Sebastian
%D Ratz I was able to complete this module within reasonable
%D time. This module has support for \METAPOST\ extensions
%D built in.
%D
%D Daniel H. Luecking came up with a better (more precise)
%D transformation method. You can recognize his comment by
%D his initials. (We keep the old code around because it's a
%D nice illustration on how a module like this evolves.)

% Beware, we cannot use 0pt here by defaukt since it may be
% defined in the range \dimen 0 - 20 which we happen to use
% as scratch registers; for this reason we start allocating
% scratch registers > 20

%D This module handles some \PDF\ conversion and insertions
%D topics. By default, the macros use the \PDFTEX\ primitive
%D \type{\pdfliteral} when available. Since \PDFTEX\ is now the
%D default engine for \TEX\ distributions, we need a more complex
%D test.

\writestatus{loading}{MetaPost Graphics / MPS to PDF}

\unprotect

\ifx\PDFcode   \undefined \let\PDFcode      \gobbleoneargument                \fi
\ifx\PDFcomment\undefined \def\PDFcomment#1{\PDFcode{\letterpercent\space#1}} \fi

%D First we define a handy constant:

\bgroup \catcode`\%=\@@other \xdef\letterpercent{\string%} \egroup

%D \macros
%D   {pdfimage,pdfimages,pdfclippedimage}
%D
%D Starting with pdftex version 14, images are included more
%D natural to the form embedding. This enables alternative
%D images to be embedded.
%D
%D \starttyping
%D \pdfimage  <optional dimensions> {file}
%D \pdfimages <optional dimensions> {high res file} {low res file}
%D \stoptyping
%D
%D The first one replaces the pre||version||14 original,
%D while the latter provides alternative images.
%D
%D The next macro is dedicated to Maarten Gelderman, who
%D needed to paste prepared \PDF\ pages into conference
%D proceedings.
%D
%D \starttyping
%D \pdfclippedimage <optional dimensions> {file} {l} {r} {t} {b}
%D \stoptyping

\ifx\pdftexversion\undefined \else \ifnum\pdftexversion>13 % still relevant?

  \def\pdfimage#1#%
    {\dopdfimage{#1}}

  \def\dopdfimage#1#2%
    {\immediate\pdfximage#1{#2}%
     \pdfrefximage\pdflastximage}

  \def\pdfimages#1#%
    {\dopdfimages{#1}}

  \def\dopdfimages#1#2#3%
    {\immediate\pdfximage#1{#2}%
     \immediate\pdfobj{[ << /Image \the\pdflastximage\space0 R /DefaultForPrinting true >> ]}%
     \immediate\pdfximage#1 attr {/Alternates \the\pdflastobj\space0 R}{#3}%
     \pdfrefximage\pdflastximage}

  \def\pdfclippedimage#1#% specs {file}{left}{right}{top}{bottom}
    {\dopdfclippedimage{#1}}

  \def\dopdfclippedimage#1#2#3#4#5#6%
    {\bgroup
     \pdfximage#1{#2}%
     \setbox\scratchbox\hbox{\pdfrefximage\pdflastximage}%
     \hsize\dimexpr\wd\scratchbox-#3-#4\relax
     \vsize\dimexpr\ht\scratchbox-#5-#6\relax
     \setbox\scratchbox\vbox to \vsize
       {\vskip-#5\hbox to \hsize{\hskip-#3\box\scratchbox\hss}}%
     \pdfxform\scratchbox
     \pdfrefxform\pdflastxform
     \egroup}

\fi \fi

%D \macros
%D   {convertMPtoPDF}
%D
%D The next set of macros implements \METAPOST\ to \PDF\
%D conversion. The traditional method is in the MkII file.

%D The main conversion command is:
%D
%D \starttyping
%D \convertMPtoPDF {filename} {x scale} {y scale}
%D \stoptyping
%D
%D The dimensions are derived from the bounding box. So we
%D only have to say:
%D
%D \starttyping
%D \convertMPtoPDF{mp-pra-1.eps}{1}{1}
%D \convertMPtoPDF{mp-pra-1.eps}{.5}{.5}
%D \stoptyping

%D \macros
%D   {makeMPintoPDFobject,lastPDFMPobject}
%D
%D For experts there are a few more options. When attributes
%D are to be added, the code must be embedded in an object
%D accompanied with the appropriate directives. One can
%D influence this process with \type {\makeMPintoPDFobject}.
%D
%D This option defaults to~0, because \CONTEXT\ takes care
%D of objects at another level, which saves some bytes.
%D
%D \starttabulate[|l|l|p|]
%D \NC 0 \NC never    \NC don't use an object    \NC\NR
%D \NC 1 \NC always   \NC always use an object   \NC\NR
%D \NC 2 \NC optional \NC use object when needed \NC\NR
%D \stoptabulate
%D
%D The last object number used is avaliable in the macro
%D \type {\lastPDFMPobject}.

\ifx\makeMPintoPDFobject   \undefined \newcount\makeMPintoPDFobject      \fi
\ifx\blackoutMPgraphic     \undefined \chardef\blackoutMPgraphic\plusone \fi
\ifx\everyMPtoPDFconversion\undefined \newtoks\everyMPtoPDFconversion    \fi

\let\lastPDFMPobject    \!!zerocount
\let\currentPDFresources\empty
\let\setMPextensions    \relax

\def\PDFMPformoffset
  {\ifx\objectoffset\undefined\zeropoint\else\objectoffset\fi}

\def\resetMPvariables#1#2#3%
  {\global\let\MPwidth \!!zeropoint
   \global\let\MPheight\!!zeropoint
   \global\let\MPllx   \!!zerocount
   \global\let\MPlly   \!!zerocount
   \global\let\MPurx   \!!zerocount
   \global\let\MPury   \!!zerocount
   \xdef\MPxscale      {#2}\ifx\MPxscale\empty\let\MPxscale\!!plusone\fi
   \xdef\MPyscale      {#3}\ifx\MPyscale\empty\let\MPyscale\!!plusone\fi
   \xdef\MPfilename    {#1}}

%D The main macro:

\def\convertMPtoPDF#1#2#3%
  {\resetMPvariables{#1}{#2}{#3}%
   \vbox\bgroup
   \forgetall
   \offinterlineskip
   \ifx\pdfdecimaldigits\undefined\else \pdfdecimaldigits=5 \fi % new
   \global\let\MPheight\!!zeropoint
   \global\let\MPwidth \!!zeropoint
   \setbox\scratchbox\vbox\bgroup
     \message{[MP to PDF]}%
     \startMPresources
     \PDFcomment{mps begin}%
     \PDFcode{q}%
     \PDFcode{1 0 0 1 0 0 cm}%
     \ifcase\blackoutMPgraphic\or\PDFcode{0 g 0 G}\fi
     \doprocessMPtoPDFfile}

\def\processMPtoPDFfile#1#2#3% obsolete
  {\resetMPvariables{#1}{#2}{#3}%
   \bgroup
   \let\finishMPgraphic\egroup
   \doprocessMPtoPDFfile}

\def\doprocessMPtoPDFfile
  {\setMPspecials
   \setMPextensions
   \the\everyMPtoPDFconversion
   \catcode`\^^M=\@@endofline
   \startMPscanning
   \let\do\empty
   \donefalse
   \let\handleMPsequence\dohandleMPsequence
   \input\MPfilename\relax}

\def\finishMPgraphic
  {\PDFcode{Q}%
   \PDFcomment{mps end}%
   \stopMPresources
   \egroup
   \setbox\scratchbox\hbox\bgroup
      \hskip-\MPllx\onebasepoint
      \raise-\MPlly\onebasepoint
      \box\scratchbox
   \egroup
   \setbox\scratchbox\vbox to \MPheight\bgroup
     \vfill
     \hsize\MPwidth
     \smashbox\scratchbox
     \box\scratchbox
   \egroup
   \wd\scratchbox\MPwidth
   \ht\scratchbox\MPheight
   \dopackageMPgraphic\scratchbox
   \egroup
   \endinput}

%D A common hook.

\let\MPfshowcommand\empty

%D Objects.

\def\dopackageMPgraphic#1% #1 = boxregister
  {\ifcase\makeMPintoPDFobject\or\or\ifx\currentPDFresources\empty\else
     % an existing value of 2 signals object support (set elsewhere)
     \makeMPintoPDFobject\plusone
   \fi\fi
   \ifcase\makeMPintoPDFobject
     \box#1%
   \or
     \scratchdimen\PDFMPformoffset\relax
     \ifdim\scratchdimen>\zeropoint % compensate for error
       \setbox#1\vbox spread 2\scratchdimen
         {\forgetall\vss\hbox spread 2\scratchdimen{\hss\box#1\hss}\vss}%
     \fi
     \setMPPDFobject{\currentPDFresources}{#1}%
     \ifdim\scratchdimen>\zeropoint % compensate for error
       \vbox to \MPheight
         {\forgetall\vss\hbox to \MPwidth{\hss\getMPPDFobject\hss}\vss}%
     \else
       \getMPPDFobject
     \fi
     \global\let\currentPDFresources\empty
   \else
     \box#1%
   \fi}

\def\setMPPDFobject#1#2% resources boxnumber
  {\ifx\pdfxform\undefined
     \def\getMPPDFobject{\box#2}%
   \else\ifx\pdftexversion\undefined
     \def\getMPPDFobject{\box#2}%
   \else\ifnum\pdftexversion<14
     \def\getMPPDFobject{\box#2}%
   \else
     \ifx\everyPDFxform\undefined\else\the\everyPDFxform\fi
     \immediate\pdfxform resources{#1}#2%
     \edef\getMPPDFobject{\noexpand\pdfrefxform\the\pdflastxform}%
   \fi\fi\fi}

\let\getMPPDFobject\relax

%D \macros
%D   {deleteMPgraphic,
%D    startMPresources,
%D    stopMPresources}

\ifx\deleteMPgraphic\undefined
  \def\deleteMPgraphic#1{}
\fi

\ifx\startMPresources\undefined
  \let\startMPresources\relax
  \let\stopMPresources\relax
\fi

%D We implement extensions by using the \METAPOST\ special
%D mechanism. Opposite to \TEX's specials, the \METAPOST\ ones
%D are flushed before or after the graphic data, but thereby
%D are no longer connected to a position.
%D
%D We implement specials by overloading the \type {fill}
%D operator. By counting the fills, we can let the converter
%D treat the appropriate fill in a special way. The
%D specification of the speciality can have two forms,
%D determined by the setting of a boolean variable:
%D
%D \starttyping
%D _inline_specials_ := false ; % comment like code (default)
%D _inline_specials_ := true  ; % command like code
%D \stoptyping
%D
%D When the specification is embedded as comment, it looks
%D like:
%D
%D \starttyping
%D %%MetaPostSpecial <size> <data> <number> <identifier>
%D \stoptyping
%D
%D The in||line alternative is more tuned for \POSTSCRIPT,
%D since it permits us to define a macro \type {special}.
%D
%D \starttyping
%D inline  : <data> <number> <identifier> <size> special
%D \stoptyping
%D
%D The \type {identifier} determines what to do, and the data
%D can be used to accomplish this. A type~2 shading function
%D has identifier~2. Alltogether, the number of parameters is
%D specified in \type {size}. The \type {number} is the number
%D of the fill that needs the special treatment. For a type~2
%D and~3 shaded fill, the datablock contains the following

%D data:
%D
%D \starttyping
%D from to n inner_r g b x y        outer_r g b x y
%D from to n inner_r g b x y radius outer_r g b x y radius
%D \stoptyping

\newconditional\manyMPspecials \settrue\manyMPspecials

%D In case of \PDF, we need to prepare resourcs.

\newtoks\MPstartresources
\newtoks\MPstopresources

\def\startMPresources
  {\the\MPstartresources}

\def\stopMPresources
  {\the\MPstopresources}

%D Some day we may consider collecting local resources.

\appendtoks
  \global\let\currentPDFresources\empty % kind of redundant
\to \MPstartresources

% \appendtoks
%    \collectPDFresources
%    \global\let\currentPDFresources\collectedPDFresources
% \to \MPstopresources

\appendtoksonce
   \the\everyPDFxform
\to \MPstopresources

%D Since colors are not subjected to transformations, we can
%D only use colors as signal. In our case, we use a dummy colored
%D path with a red color component of \type {0.n}, so \type
%D {0.001} is the first path and \type {0.010} the tenth. Since
%D \METAPOST strips trailing zeros, we have to padd the string.

\newif\ifMPcmykcolors
\newif\ifMPspotcolors

\def\dohandleMPrgb   #1#2#3{\revokeMPtransparencyspecial\execcolorR   #1:#2:#3:0:0\od}
\def\dohandleMPcmyk#1#2#3#4{\revokeMPtransparencyspecial\execcolorC#1:#2:#3:#4:0:0\od}
\def\dohandleMPgray      #1{\revokeMPtransparencyspecial\execcolorS         #1:0:0\od}
\def\dohandleMPspot#1#2#3#4{\revokeMPtransparencyspecial\execcolorP#1:#2:#3:#4:0:0\od}

%D Specials:

\settrue \manyMPspecials \newcount\nofMParguments \let\extraMPpathcode\empty

\def\@@MP  {@@MP}
\def\@@MPSK{@MPSK@}

\def\MPspecial{\@@MPSK\@@MPSK\gMPs\nofMParguments}

\def\defineMPspecial#1#2%
  {\setvalue{\@@MPSK\@@MPSK#1}{#2}}

%D Special number~1 is dedicated to \CMYK\ support. If you
%D want to know why: look at this:
%D
%D \startbuffer[mp]
%D   fill fullcircle xyscaled (3cm,1cm) withcolor \MPcolor{test} ;
%D \stopbuffer
%D
%D \startbuffer[cmyk]
%D \startcombination[4*1]
%D   {\definecolor[test][c=1,y=.3,k=.3] \processMPbuffer[mp]} {c=1 y=.3 k=.3}
%D   {\definecolor[test][c=.9,y=.15]    \processMPbuffer[mp]} {c=.9 y=.15}
%D   {\definecolor[test][c=.25,y=.8]    \processMPbuffer[mp]} {c=.25 y=.8}
%D   {\definecolor[test][c=.45,y=.1]    \processMPbuffer[mp]} {c=.45 y=.1}
%D \stopcombination
%D \stopbuffer
%D
%D \placefigure
%D   {\CMYK\ support disabled,
%D    conversion to \RGB.}
%D   {\setupcolors[cmyk=nee,state=start]\getbuffer[cmyk]}
%D
%D \placefigure
%D   {\CMYK\ support enabled,
%D    no support in \METAPOST.}
%D   {\setupcolors[cmyk=ja,mpcmyk=nee,state=start]\getbuffer[cmyk]}
%D
%D \placefigure
%D   {\CMYK\ support enabled,
%D    no conversion to \RGB,
%D    support in \METAPOST}
%D   {\setupcolors[cmyk=ja,state=start]\getbuffer[cmyk]}

\defineMPspecial{1}
  {\ifMPcmykcolors
     \setxvalue{\@@MPSK\gMPs6}{\noexpand\dohandleMPcmykcolor{\gMPs2}{\gMPs3}{\gMPs4}{\gMPs5}}%
   \fi}

\defineMPspecial{2}
  {\ifMPspotcolors
     \setxvalue{\@@MPSK\gMPs6}{\noexpand\dohandleMPspotcolor{\gMPs2}{\gMPs3}{\gMPs4}{\gMPs5}}%
%      \checkMPspot{\gMPs2}{\gMPs3}{\gMPs4}{\gMPs5}%
   \fi}

% \def\checkMPspot#1#2#3#4%
%   {\expanded{\resolveMPspotcolor#1 #2 #3 #4}\end
%    \ifx\MPspotspace\MPresolvedspace
%      \edef\MPspotspacespec{/\MPspotspace\space}%
%      \doifinstringelse\MPspotspacespec\currentMPcolorspaces
%        \donothing\registerMPcolorspace
%    \fi}

\let\revokeMPtransparencyspecial\relax

\def\dohandleMPrgbcolor   #1#2#3{\revokeMPtransparencyspecial\execcolorR   #1:#2:#3:0:0\od}
\def\dohandleMPcmykcolor#1#2#3#4{\revokeMPtransparencyspecial\execcolorC#1:#2:#3:#4:0:0\od}
\def\dohandleMPgraycolor      #1{\revokeMPtransparencyspecial\execcolorS         #1:0:0\od}
\def\dohandleMPspotcolor#1#2#3#4{\revokeMPtransparencyspecial\execcolorP#1:#2:#3:#4:0:0\od}

%D Transparency support used specials 60 (rgb) and 61
%D (cmyk).
%D
%D \startbufferFshade

%D u := 2cm ; path p ; p := fullcircle scaled u shifted (u/4,0);
%D
%D fill p rotated  90 withcolor transparent(1,.5,yellow) ;
%D fill p rotated 210 withcolor transparent(1,.5,green) ;
%D fill p rotated 330 withcolor transparent(1,.5,blue) ;
%D \stopbuffer
%D
%D \typebuffer
%D
%D \startlinecorrection \processMPbuffer \stoplinecorrection
%D
%D One can also communicate colors between \CONTEXT\ and
%D \METAPOST:
%D
%D \startbuffer
%D \definecolor[tcyan]   [c=1,k=.2,t=.5]
%D \definecolor[tmagenta][m=1,k=.2,t=.5]
%D \definecolor[tyellow] [y=1,k=.2,t=.5]
%D \stopbuffer
%D
%D \typebuffer \getbuffer
%D
%D \startbuffer
%D u := 2cm ; path p ; p := fullcircle scaled u shifted (u/4,0);
%D
%D fill p rotated  90 withcolor \MPcolor{tcyan} ;
%D fill p rotated 210 withcolor \MPcolor{tmagenta} ;
%D fill p rotated 330 withcolor \MPcolor{tyellow} ;
%D \stopbuffer
%D
%D \startlinecorrection \processMPbuffer \stoplinecorrection
%D
%D We save all the three components needed in one macro,
%D just to save hash space.

\def\dohandleMPrgbtransparency   #1#2#3#4#5{\execcolorR   #1:#2:#3:#4:#5\od\let\revokeMPtransparencyspecial\dorevokeMPtransparencyspecial}
\def\dohandleMPcmyktransparency#1#2#3#4#5#6{\execcolorC#1:#2:#3:#4:#5:#6\od\let\revokeMPtransparencyspecial\dorevokeMPtransparencyspecial}
\def\dohandleMPgraytransparency      #1#2#3{\execcolorS         #1:#2:#3\od\let\revokeMPtransparencyspecial\dorevokeMPtransparencyspecial}
\def\dohandleMPspottransparency#1#2#3#4#5#6{\execcolorP#1:#2:#3:#4:#5:#6\od\let\revokeMPtransparencyspecial\dorevokeMPtransparencyspecial}

\def\dorevokeMPtransparencyspecial
  {\PDFcode{\PDFtransparencyresetidentifier\space gs}%
   \let\revokeMPtransparencyspecial\relax}

\defineMPspecial{3} % rgb
  {\setxvalue{\@@MPSK\gMPs6}{\noexpand\dohandleMPrgbtransparency{\gMPs3}{\gMPs4}{\gMPs5}{\gMPs1}{\gMPs2}}}

\defineMPspecial{4} % cmyk
  {\setxvalue{\@@MPSK\gMPs7}{\noexpand\dohandleMPcmyktransparency{\gMPs3}{\gMPs4}{\gMPs5}{\gMPs6}{\gMPs1}{\gMPs2}}}

\defineMPspecial{5} % spot
  {\setxvalue{\@@MPSK\gMPs7}{\noexpand\dohandleMPspottransparency{\gMPs3}{\gMPs4}{\gMPs5}{\gMPs6}{\gMPs1}{\gMPs2}}%
   }%\checkMPspot{\gMPs3}{\gMPs4}{\gMPs5}{\gMPs6}}

%D Shading is an example of a more advanced graphic feature,
%D but users will seldom encounter those complications. Here
%D we only show a few simple examples, but many other
%D alternatives are possible by setting up the functions built
%D in \PDF\ in the appropriate way.
%D
%D Shading has to do with interpolation between two or more
%D points or user supplied ranges. In \PDF, the specifications
%D of a shade has to be encapsulated in objects and passed on
%D as resources. This is a \PDF\ level 1.3. feature. One can
%D simulate three dimensional shades as well and define simple
%D functions using a limited set of \POSTSCRIPT\ primitives.
%D Given the power of \METAPOST\ and these \PDF\ features, we
%D can achieve superb graphic effects.
%D
%D Since everything is hidden in \TEX\ and \METAPOST\ graphics,
%D we can stick to high level \CONTEXT\ command, as shown in
%D the following exmples.
%D
%D \startbuffer
%D \startuniqueMPgraphic{CircularShade}
%D   path  p ; p := unitsquare xscaled \overlaywidth yscaled \overlayheight ;
%D   circular_shade(p,0,.2red,.9red) ;
%D \stopuniqueMPgraphic
%D
%D \startuniqueMPgraphic{LinearShade}
%D   path  p ; p := unitsquare xscaled \overlaywidth yscaled \overlayheight ;
%D   linear_shade(p,0,.2blue,.9blue) ;
%D \stopuniqueMPgraphic
%D
%D \startuniqueMPgraphic{DuotoneShade}
%D   path  p ; p := unitsquare xscaled \overlaywidth yscaled \overlayheight ;
%D   linear_shade(p,2,.5green,.5red) ;
%D \stopuniqueMPgraphic
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer
%D
%D These graphics can be hooked into the overlay mechanism,
%D which is available in many commands.
%D
%D \startbuffer
%D \defineoverlay[demo 1][\uniqueMPgraphic{CircularShade}]
%D \defineoverlay[demo 2][\uniqueMPgraphic  {LinearShade}]
%D \defineoverlay[demo 3][\uniqueMPgraphic {DuotoneShade}]
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer
%D
%D These backgrounds can for instance be applied to \type
%D {\framed}:
%D
%D \startbuffer
%D \setupframed[width=3cm,height=2cm,frame=off]
%D \startcombination[3*1]
%D   {\framed[backgroundachtergrond=demo 1]{\bfd \white Demo 1}} {}
%D   {\framed[backgroundachtergrond=demo 2]{\bfd \white Demo 2}} {}
%D   {\framed[backgroundachtergrond=demo 3]{\bfd \white Demo 3}} {}
%D \stopcombination
%D \stopbuffer
%D
%D \typebuffer
%D
%D \startlinecorrection
%D \getbuffer
%D \stoplinecorrection
%D
%D There are a few more alternatives, determined by the second
%D parameter passed to \type {circular_shade} and alike.
%D
%D \def\SomeShade#1#2#3#4#5%
%D   {\startuniqueMPgraphic{Shade-#1}
%D      width := \overlaywidth ;
%D      height := \overlayheight ;
%D      path p ; p := unitsquare xscaled width yscaled height ;
%D      #2_shade(p,#3,#4,#5) ;
%D    \stopuniqueMPgraphic
%D    \defineoverlay[Shade-#1][\uniqueMPgraphic{Shade-#1}]%
%D    \framed[backgroundachtergrond=Shade-#1,width=2cm,height=2cm,frame=off]{}}
%D
%D \startlinecorrection
%D \startcombination[5*1]
%D   {\SomeShade{10}{circular}{0}{.3blue}{.9blue}} {circular 0}
%D   {\SomeShade{11}{circular}{1}{.3blue}{.9blue}} {circular 1}
%D   {\SomeShade{12}{circular}{2}{.3blue}{.9blue}} {circular 2}
%D   {\SomeShade{13}{circular}{3}{.3blue}{.9blue}} {circular 3}
%D   {\SomeShade{14}{circular}{4}{.3blue}{.9blue}} {circular 4}
%D \stopcombination
%D \stoplinecorrection
%D
%D \blank
%D
%D \startlinecorrection
%D \startcombination[5*1]
%D   {\SomeShade{20}{circular}{0}{.9green}{.3green}} {circular 0}
%D   {\SomeShade{21}{circular}{1}{.9green}{.3green}} {circular 1}
%D   {\SomeShade{22}{circular}{2}{.9green}{.3green}} {circular 2}
%D   {\SomeShade{23}{circular}{3}{.9green}{.3green}} {circular 3}
%D   {\SomeShade{24}{circular}{4}{.9green}{.3green}} {circular 4}
%D \stopcombination
%D \stoplinecorrection
%D
%D \blank
%D
%D \startlinecorrection
%D \startcombination[4*1]
%D   {\SomeShade{30}{linear}{0}{.3red}{.9red}} {linear 0}
%D   {\SomeShade{31}{linear}{1}{.3red}{.9red}} {linear 1}
%D   {\SomeShade{32}{linear}{2}{.3red}{.9red}} {linear 2}
%D   {\SomeShade{33}{linear}{3}{.3red}{.9red}} {linear 3}
%D \stopcombination
%D \stoplinecorrection
%D
%D These macros closely cooperate with the \METAPOST\ module
%D \type {mp-spec.mp}, which is part of the \CONTEXT\
%D distribution.
%D
%D The low level (\PDF) implementation is based on the \TEX\
%D based \METAPOST\ to \PDF\ converter. Shading is supported
%D by overloading the \type {fill} operator as implemented
%D earlier. In \PDF\ type~2 and~3 shading functions are
%D specified in terms of:
%D
%D \starttabulate[|Tl|l|]
%D \NC /Domain \NC sort of meeting range \NC \NR
%D \NC /C0     \NC inner shade \NC \NR
%D \NC /C1     \NC outer shade \NC \NR
%D \NC /N      \NC smaller values, bigger inner circles \NC \NR
%D \stoptabulate

\newcount\currentPDFshade  % 0  % global (document wide) counter

% \def\dosetMPsomePDFshade#1#2% generic but needs refs
%   {\global\advance\currentPDFshade \plusone
%    \doPDFdictionaryobject{FDF}{ftn:Sh:\the\currentPDFshade}
%      {/FunctionType 2
%       /Domain [\gMPs1 \gMPs2]
%       /C0 [\MPshadeA]
%       /C1 [\MPshadeB]
%       /N \gMPs3}%
%    \doPDFgetobjectreference{FDF}{ftn:Sh:\the\currentPDFshade}\PDFobjectreference
%    \doPDFdictionaryobject{FDF}{obj:Sh:\the\currentPDFshade}
%      {/ShadingType #1
%       /ColorSpace /\MPresolvedspace
%       /Function \PDFobjectreference\space
%       /Coords [\MPshadeC]
%       /Extend [true true]}%
%    \doPDFgetobjectreference{FDF}{obj:Sh:\the\currentPDFshade}\PDFobjectreference
%    \appendtoPDFdocumentshades{/Sh\the\currentPDFshade\space\PDFobjectreference}%
%    \setxvalue{\@@MPSK#2}{\noexpand\dohandleMPshade{\the\currentPDFshade}}}

\def\dosetMPsomePDFshade#1#2%
  {\immediate\pdfobj
     {<</FunctionType 2
        /Domain [\gMPs1 \gMPs2]
        /C0 [\MPshadeA]
        /C1 [\MPshadeB]
        /N \gMPs3>>}%
   \immediate\pdfobj
     {<</ShadingType #1
        /ColorSpace /\MPresolvedspace
        /Function \the\pdflastobj\space 0 R
        /Coords [\MPshadeC]
        /Extend [true true]>>}%
   \global\advance\currentPDFshade \plusone
   \appendtoPDFdocumentshades{/Sh\the\currentPDFshade\space\the\pdflastobj\space0 R }%
   \setxvalue{\@@MPSK#2}{\noexpand\dohandleMPshade{\the\currentPDFshade}}}

\def\dosetMPlinearshade  {\dosetMPsomePDFshade2}% #1
\def\dosetMPcircularshade{\dosetMPsomePDFshade3}% #1

\defineMPspecial{30}
  {\expanded{\resolveMPrgbcolor{\gMPs4}{\gMPs5}{\gMPs6}}\to\MPshadeA
   \expanded{\resolveMPrgbcolor{\gMPs{9}}{\gMPs{10}}{\gMPs{11}}}\to\MPshadeB
   \edef\MPshadeC{\gMPs7 \gMPs8 \gMPs{12} \gMPs{13}}%
   \dosetMPlinearshade{\gMPs{14}}}

\defineMPspecial{31}
  {\expanded{\resolveMPrgbcolor{\gMPs4}{\gMPs5}{\gMPs6}}\to\MPshadeA
   \expanded{\resolveMPrgbcolor{\gMPs{10}}{\gMPs{11}}{\gMPs{12}}}\to\MPshadeB
   \edef\MPshadeC{\gMPs7 \gMPs8 \gMPs9 \gMPs{13} \gMPs{14} \gMPs{15}}%
   \dosetMPcircularshade{\gMPs{16}}}

\defineMPspecial{32}
  {\expanded{\resolveMPcmykcolor{\gMPs4}{\gMPs5}{\gMPs6}{\gMPs7}}\to\MPshadeA
   \expanded{\resolveMPcmykcolor{\gMPs{10}}{\gMPs{11}}{\gMPs{12}}{\gMPs{13}}}\to\MPshadeB
   \edef\MPshadeC{\gMPs8 \gMPs9 \gMPs{14} \gMPs{15}}%
   \dosetMPlinearshade{\gMPs{16}}}

\defineMPspecial{33}
  {\expanded{\resolveMPcmykcolor{\gMPs4}{\gMPs5}{\gMPs6}{\gMPs7}}\to\MPshadeA
   \expanded{\resolveMPcmykcolor{\gMPs{11}}{\gMPs{12}}{\gMPs{13}}{\gMPs{14}}}\to\MPshadeB
   \edef\MPshadeC{\gMPs8 \gMPs9 \gMPs{10} \gMPs{15} \gMPs{16} \gMPs{17}}%
   \dosetMPcircularshade{\gMPs{18}}}

\defineMPspecial{34}
  {\expanded{\resolveMPspotcolor{\gMPs4}{\gMPs5}{\gMPs6}{\gMPs7}}\to\MPshadeA
   \expanded{\resolveMPspotcolor{\gMPs{10}}{\gMPs{11}}{\gMPs{12}}{\gMPs{13}}}\to\MPshadeB
   \edef\MPshadeC{\gMPs8 \gMPs9 \gMPs{14} \gMPs{15}}%
   \dosetMPlinearshade{\gMPs{16}}}

\defineMPspecial{35}
  {\expanded{\resolveMPcmykcolor{\gMPs4}{\gMPs5}{\gMPs6}{\gMPs7}}\to\MPshadeA
   \expanded{\resolveMPcmykcolor{\gMPs{11}}{\gMPs{12}}{\gMPs{13}}{\gMPs{14}}}\to\MPshadeB
   \edef\MPshadeC{\gMPs8 \gMPs9 \gMPs{10} \gMPs{15} \gMPs{16} \gMPs{17}}%
   \dosetMPcircularshade{\gMPs{18}}}


\newconditional\ignoreMPpath

\def\dohandleMPshade#1%
  {\revokeMPtransparencyspecial
   \settrue\ignoreMPpath
   \def\extraMPpathcode{/Sh#1 sh Q}%
   \chardef\finiMPpath\zerocount
   \PDFcode{q /Pattern cs}}

%D Figure inclusion is kind of strange to \METAPOST, but when
%D Santiago Muelas started discussing this with me, I was able
%D to cook up a solution using specials.

\defineMPspecial{10}
  {\setxvalue{\@@MPSK\gMPs8}%
     {\noexpand\handleMPfigurespecial{\gMPs1}{\gMPs2}{\gMPs3}{\gMPs4}{\gMPs5}{\gMPs6}{\gMPs7}{\gMPs8}}}

\def\handleMPfigurespecial#1#2#3#4#5#6#7#8% todo : combine with ext fig
  {\global\letvalue{\@@MPSK#8}\empty
   \vbox to \zeropoint
     {\vss
      \hbox to \zeropoint
        {\ifcase\pdfoutput\or % will be hooked into the special driver
           \doiffileelse{#7}
             {\doifundefinedelse{mps:x:#7}
                {\immediate\pdfximage\!!width\onebasepoint\!!height\onebasepoint{#7}%
                 \setxvalue{mps:x:#7}{\pdfrefximage\the\pdflastximage}}%
                {\message{[reusing figure #7]}}%
              \PDFcode{q #1 #2 #3 #4 #5 #6 cm}%
              \rlap{\getvalue{mps:x:#7}}%
              \PDFcode{Q}}
             {\message{[unknown figure #7]}}%
         \fi
         \hss}}}

%D An example of using both special features is the
%D following.
%D
%D \starttyping
%D \startMPpage
%D   externalfigure "hakker1b.png" scaled 22cm rotated  10 shifted (-2cm,0cm);
%D   externalfigure "hakker1b.png" scaled 10cm rotated -10 ;
%D   externalfigure "hakker1b.png" scaled  7cm rotated  45 shifted (8cm,12cm) ;
%D   path p ; p := unitcircle xscaled 15cm yscaled 20cm;
%D   path q ; q := p rotatedaround(center p,90) ;
%D   path r ; r := buildcycle(p,q) ; clip currentpicture to r ;
%D   path s ; s := boundingbox currentpicture enlarged 5mm ;
%D   picture c ; c := currentpicture ; currentpicture := nullpicture ;
%D   circular_shade(s,0,.2red,.9red) ;
%D   addto currentpicture also c ;
%D \stopMPpage
%D \stoptyping

%D This is some experimental hyperlink driver that I wrote
%D for Mark Wicks.

\defineMPspecial{20}
  {\setxvalue{\@@MPSK\gMPs6}%
     {\noexpand\handleMPhyperlink{\gMPs1}{\gMPs2}{\gMPs3}{\gMPs4}{\gMPs5}{\gMPs6}}}

\def\handleMPhyperlink#1#2#3#4#5#6%
  {\global\letvalue{\@@MPSK#6}\empty
   \setbox\scratchbox\hbox
     {\setbox\scratchbox\null
      \wd\scratchbox\dimexpr-#1\onebasepoint+#3\onebasepoint\relax
      \ht\scratchbox\dimexpr-#2\onebasepoint+#4\onebasepoint\relax
      \incolorfalse
      \gotobox{\box\scratchbox}[#5]}%
   \setbox\scratchbox\hbox
     {\hskip\dimexpr\MPxoffset\onebasepoint+#1\onebasepoint\relax
      \raise\dimexpr\MPyoffset\onebasepoint+#2\onebasepoint\relax
      \box\scratchbox}%
   \smashbox\scratchbox
   \box\scratchbox}

%D This special (number 50) passes positions to a tex file.
%D This method uses a two||pass approach an (mis|)|used the
%D context positioning macros. In \type {core-pos} we will
%D implement the low level submacro needed.
%D
%D \startbuffer
%D \definelayer[test]
%D
%D \setlayer
%D   [test]
%D   [x=\MPx{somepos-1},y=\MPy{somepos-1}]
%D   {Whatever we want here!}
%D
%D \setlayer
%D   [test]
%D   [x=\MPx{somepos-2},y=\MPy{somepos-2}]
%D   {Whatever we need there!}
%D
%D \startuseMPgraphic{oeps}
%D   draw fullcircle scaled 6cm withcolor red ;
%D   register ("somepos-1",1cm,2cm,center currentpicture) ;
%D   register ("somepos-2",4cm,3cm,(-1cm,-2cm)) ;
%D \stopuseMPgraphic
%D
%D \framed[background=test,offset=overlay]{\useMPgraphic{oeps}}
%D \stopbuffer
%D
%D \typebuffer
%D
%D Here the width and height are not realy used, but one can
%D imagine situations where tex has to work with values
%D calculated by \METAPOST.
%D
%D \startlinecorrection
%D \getbuffer
%D \stoplinecorrection
%D
%D Later we will implement a more convenient macro:
%D
%D \starttyping
%D \setMPlayer [test] [somepos-1] {Whatever we want here!}
%D \setMPlayer [test] [somepos-2] {Whatever we need there!}
%D \stoptyping

\defineMPspecial{50} % x y width height label
  {\dosavepositionwhd
     {\gMPs5}%
     {0}%
     {\the\dimexpr-\MPllx\onebasepoint+\gMPs1\onebasepoint\relax}
     {\the\dimexpr\gMPs2\onebasepoint-\scratchdimen+\MPury\onebasepoint\relax}%
     {\the\dimexpr\gMPs3\onebasepoint\relax}%
     {\the\dimexpr\gMPs4\onebasepoint\relax}%
     {0pt}}

%D A few auxiliary macros. This will move to colo-ini.

\def\MPgrayspace{DeviceGray}
\def\MPrgbspace {DeviceRGB}
\def\MPcmykspace{DeviceCMYK}
\let\MPspotspace\MPgrayspace

\def\MPcmykBlack{0 0 0 0}
\def\MPcmykWhite{0 0 0 1}

\def\startMPcolorresolve
  {\bgroup
   \def\dostartgraycolormode##1%
     {\global\let\MPresolvedspace\MPgrayspace
      \xdef\MPresolvedcolor{##1}}%
   \def\dostartrgbcolormode ##1##2##3%
     {\global\let\MPresolvedspace\MPrgbspace
      \xdef\MPresolvedcolor{##1 ##2 ##3}}%
   \def\dostartcmykcolormode##1##2##3##4%
     {\global\let\MPresolvedspace\MPcmykspace
      \xdef\MPresolvedcolor{##1 ##2 ##3 ##4}}%
   \def\dostartspotcolormode##1##2%
     {\global\let\MPspotspace\empty % left over ?
      \xdef\MPresolvedspace{##1}%
      \xdef\MPresolvedcolor{##2}%
      \global\let\MPspotspace\MPresolvedspace}% signal
   \dostartgraycolormode\!!zerocount} % kind of hackery initialization

\let\stopMPcolorresolve\egroup

\def\resolveMPrgbcolor#1#2#3\to#4%
  {\startMPcolorresolve
   \execcolorR#1:#2:#3:0:0\od
   \stopMPcolorresolve
   \let#4\MPresolvedcolor}

\def\resolveMPcmykcolor#1#2#3#4\to#5%
  {\startMPcolorresolve
   \execcolorC#1:#2:#3:#4:0:0\od
   \stopMPcolorresolve
   \let#5\MPresolvedcolor}

\def\resolveMPgraycolor#1\end\to#2%
  {\startMPcolorresolve
   \execcolorS#1:0:0\od
   \stopMPcolorresolve
   \let#2\MPresolvedcolor}

\def\resolveMPspotcolor#1#2#3#4\end\to#5%
  {\startMPcolorresolve
   \ifnum#2>\plusone
     \checkmultitonecolor{#1}%
   \fi
   \execcolorP#1:#2:#3:#4:0:0\od
   \stopMPcolorresolve
   \let#5\MPresolvedcolor}

%D \macros
%D   {dogetPDFmediabox}
%D
%D The next macro can be used to find the mediabox of a \PDF\
%D illustration.
%D
%D \starttyping
%D \dogetPDFmediabox
%D   {filename}
%D   {new dimen}{new dimen}{new dimen}{new dimen}
%D \stoptyping
%D
%D Beware of dimen clashes: this macro uses the 5~default
%D scratch registers! When no file or mediabox is found, the
%D dimensions are zeroed.

\def\dogetPDFmediabox#1#2#3#4#5%
  {\bgroup
   \def\PDFxscale{1}%
   \def\PDFyscale{1}%
   \uncatcodespecials
   \endlinechar\minusone
   \def\checkPDFtypepage##1/Type /Page##2##3\done%
     {\ifx##2\relax
      \else\if##2s% accept /Page and /Pages
        \let\doprocessPDFline\findPDFmediabox
      \else
        \let\doprocessPDFline\findPDFmediabox
      \fi\fi}%
   \def\findPDFtypepage
     {\expandafter\checkPDFtypepage\fileline/Type /Page\relax\done}%
   \def\checkPDFmediabox##1/MediaBox##2##3\done%
     {\ifx##2\relax \else
        \setPDFmediabox##2##3\done
        \fileprocessedtrue
      \fi}%
   \def\findPDFmediabox
     {\expandafter\checkPDFmediabox\fileline/MediaBox\relax\done}%
   \let\doprocessPDFline\findPDFtypepage
   \doprocessfile\scratchread{#1}\doprocessPDFline
   \egroup
   \ifx\PDFxoffset\undefined
     #2=\zeropoint
     #3=\zeropoint
     #4=\zeropoint
     #5=\zeropoint
   \else
     #2=\PDFxoffset\onebasepoint
     #3=\PDFyoffset\onebasepoint
     #4=\PDFwidth
     #5=\PDFheight
   \fi}

\def\setPDFboundingbox#1#2#3#4#5#6%
  {\dimen0=#1\dimen0=#5\dimen0
   \ScaledPointsToBigPoints{\number\dimen0}\PDFxoffset
   \dimen0=#3\dimen0=#5\dimen0
   \xdef\PDFwidth{\the\dimen0}%
   \dimen0=#2\dimen0=#6\dimen0
   \ScaledPointsToBigPoints{\number\dimen0}\PDFyoffset
   \dimen0=#4\dimen0=#6\dimen0
   \xdef\PDFheight{\the\dimen0}%
   \global\let\PDFxoffset\PDFxoffset
   \global\let\PDFyoffset\PDFyoffset}

\def\setPDFmediabox#1[#2 #3 #4 #5]#6\done
  {\dimen2=#2\onebasepoint\dimen2=-\dimen2 % \dimen2=-#2\onebasepoint also works since tex handles --
   \dimen4=#3\onebasepoint\dimen4=-\dimen4 % \dimen4=-#3\onebasepoint also works since tex handles --
   \dimen6=#4\onebasepoint\advance\dimen6 \dimen2
   \dimen8=#5\onebasepoint\advance\dimen8 \dimen4
   \setPDFboundingbox{\dimen2}{\dimen4}{\dimen6}{\dimen8}\PDFxscale\PDFyscale}

%D End of soon obsolete code.

\startMPinitializations
  mp_shade_version := 2 ;
\stopMPinitializations

%D Here comes the traditional \MKII\ converter.
%D
%D Because we want to test as fast as possible, we first
%D define the \POSTSCRIPT\ operators that \METAPOST\ uses.
%D We don't define irrelevant ones, because these are
%D skipped anyway.
%D
%D The converter can be made a bit faster by replacing the
%D two test macros (the ones with the many \type {\if's}) by
%D a call to named branch macros (something \typ {\getvalue
%D {xPSmoveto}}. For everyday documents with relatively
%D small graphics the gain in speed can be neglected.

\def \PScurveto          {curveto}
\def \PSlineto           {lineto}
\def \PSmoveto           {moveto}
\def \PSshowpage         {showpage}
\def \PSnewpath          {newpath}
\def \PSfshow            {fshow}
\def \PSclosepath        {closepath}
\def \PSfill             {fill}
\def \PSstroke           {stroke}
\def \PSclip             {clip}
\def \PSrlineto          {rlineto}
\def \PSsetlinejoin      {setlinejoin}
\def \PSsetlinecap       {setlinecap}
\def \PSsetmiterlimit    {setmiterlimit}
\def \PSsetgray          {setgray}
\def \PSsetrgbcolor      {setrgbcolor}
\def \PSsetcmykcolor     {setcmykcolor}
\def \PSsetdash          {setdash}
\def \PSgsave            {gsave}
\def \PSgrestore         {grestore}
\def \PStranslate        {translate}
\def \PSscale            {scale}
\def \PSconcat           {concat}
\def \PSdtransform       {dtransform}
\def \PSsetlinewidth     {setlinewidth}
\def \PSpop              {pop}

\def \PSnfont            {nfont}    % was needed for TUG98 proceedings
\def \PSspecial          {special}  % extensions to MetaPost

%D A previous version set \type {%} to ignore, which
%D simplified the following definitions. At the start of
%D conversion the percent character was made active again.
%D Because the whole graphic is one paragraph (there are no
%D empty lines) this does not give the desired effect. This
%D went unnoticed untill Scott Pakin sent me a test file
%D percent characters in a string. So, from now on we have
%D to prefix the following strings with percentages.

%D Some day I'll figure out a better solution (line by line reading
%D using \ETEX).

\edef \PSBoundingBox      {\letterpercent\letterpercent BoundingBox:}
\edef \PSHiResBoundingBox {\letterpercent\letterpercent HiResBoundingBox:}
\edef \PSExactBoundingBox {\letterpercent\letterpercent ExactBoundingBox:}
\edef \PSMetaPostSpecial  {\letterpercent\letterpercent MetaPostSpecial:}
\edef \PSMetaPostSpecials {\letterpercent\letterpercent MetaPostSpecials:}
\edef \PSPage             {\letterpercent\letterpercent Page:}
\edef \PSBeginProlog      {\letterpercent\letterpercent BeginProlog}
\edef \PSEndProlog        {\letterpercent\letterpercent EndProlog}
\edef \PSEof              {\letterpercent\letterpercent EOF}

%D By the way, the \type {setcmykcolor} operator is not
%D output by \METAPOST\ but can result from converting the
%D \cap{RGB} color specifications, as implemented in
%D \type{supp-mps}.

%D In \POSTSCRIPT\ arguments precede the operators. Due to the
%D fact that in some translations we need access to those
%D arguments, and also because sometimes we have to skip them,
%D we stack them up. The stack is one||dimensional for non path
%D operators and two||dimensional for operators inside a path.
%D This is because we have to save the whole path for
%D (optional) postprocessing. Values are pushed onto the stack
%D by:
%D
%D \starttyping
%D \setMPargument {value}
%D \stoptyping
%D
%D They can be retrieved by the short named macros:
%D
%D \starttyping
%D \gMPa {number}
%D \gMPs {number}
%D \stoptyping
%D
%D When scanning a path specification, we also save the
%D operator, using
%D
%D \starttyping
%D \setMPkeyword {n}
%D \stoptyping
%D
%D The path drawing operators are coded for speed: \type{clip},
%D \type{stroke}, \type{fill} and \type{fillstroke} become
%D 1, 2, 3 and~4.
%D
%D When processing the path this code can be retrieved
%D using
%D
%D \starttyping
%D \getMPkeyword % {n}
%D \stoptyping
%D
%D When setting an argument, the exact position on the stack
%D depends on the current value of the \COUNTERS\
%D \type{\nofMPsegments} and \type{\nofMParguments}.

\newcount\nofMPsegments
\newcount\nofMParguments

%D These variables hold the coordinates. The argument part of
%D the stack is reset by:
%D
%D \starttyping
%D \resetMPstack
%D \stoptyping
%D
%D We use the prefix \type{@@MP} to keep the stack from
%D conflicting with existing macros. To speed up things a bit
%D more, we use the constant \type{\@@MP}.

\def\@@MP{@@MP}

\def\setMPargument% #1%
  {\advance\nofMParguments \plusone
   \expandafter\def\csname\@@MP\the\nofMPsegments\the\nofMParguments\endcsname} % {#1}

\def\letMPargument
  {\advance\nofMParguments \plusone
   \expandafter\let\csname\@@MP\the\nofMPsegments\the\nofMParguments\endcsname}

\def\setMPsequence#1 %
  {\advance\nofMParguments \plusone
   \expandafter\def\csname\@@MP\the\nofMPsegments\the\nofMParguments\endcsname{#1}%
   \handleMPsequence}

\def\gMPa#1%
  {\csname\@@MP0\number#1\endcsname}

\def\gMPs#1%
  {\csname\@@MP\the\nofMPsegments\number#1\endcsname}

\def\dogMPa#1%
  {\@EAEAEA\do\csname\@@MP0\number#1\endcsname}

\def\setMPkeyword#1 %
  {\expandafter\def\csname\@@MP\the\nofMPsegments0\endcsname{#1}%
   \advance\nofMPsegments \plusone
   \nofMParguments\zerocount}

\def\getMPkeyword% #1%
  {\csname\@@MP\the\nofMPsegments0\endcsname} % {\csname\@@MP#10\endcsname}

\def\docleanupMPargument#1% we need this because args can have [ or ] pre/appended
  {\expandafter\edef\csname\@@MP\the\nofMPsegments\number#1\endcsname
     {\csname\@@MP\the\nofMPsegments\number#1\endcsname}}

%D When we reset the stack, we can assume that all further
%D comment is to be ignored and handled in strings.
%D By redefining the reset macro after the first call, we
%D save some run time. Only use this macro after all
%D comments are processed and use the simple alternative
%D when dealing with comments.

\def\doresetMPstack
  {\nofMParguments\zerocount}

\def\resetMPstack
  {\let\handleMPgraphic\handleMPendgraphic
   \let\resetMPstack\doresetMPstack
   \resetMPstack}

%D The arguments are saved with the preceding command
%D \type{\do}. By default this command expands to nothing, but
%D when we deal with strings it's used to strip off the
%D \type{(} and \type{)}.
%D
%D Strings are kind of tricky, because characters can be
%D passed verbatim \type{(hello)}, by octal number
%D \type{(\005)} or as command \type{(\()}. We therefore
%D cannot simply ignore \type{(} and \type{)}, the way we do
%D with \type{[} and \type{]}. Another complication is that
%D strings may contain characters that normally have a
%D special meaning in \TEX, like \type{$} and \type{{}}.
%D
%D A previous solution made \type{\} an active character and
%D let it look ahead for a number or characters. We had to
%D abandon this scheme because of the need for verbatim
%D support. The next solution involved some \CATCODE\
%D trickery but works well.

\def\octalMPcharacter#1#2#3%
  {\char'#1#2#3\relax}

%D curly braces and squarly brackets are stored in the argument stack
%D as part  of strings, for instance in:
%D
%D \starttyping
%D /fshow {exch findfont exch scalefont setfont show}bind def
%D [3 3 ] 0 setdash
%D \stoptyping
%D
%D but we need to keep them in situation like
%D
%D \starttyping
%D ([bla bla] bla bla) ec-lmr10 9.96265 fshow
%D ({bla bla} bla bla) ec-lmr10 9.96265 fshow
%D \stoptyping
%D
%D So, when we store the snippets, we keep the special tokens, and
%D when needed we either ignore or obey them

%D We could use a catcodetable here.

\bgroup
\catcode`\|=\@@comment
\catcode`\%=\@@active
\catcode`\[=\@@active
\catcode`\]=\@@active
\catcode`\{=\@@active
\catcode`\}=\@@active
\catcode`B=\@@begingroup
\catcode`E=\@@endgroup
\gdef\keepMPspecials|
  B\let%\letterpercent|
   \def[B\noexpand[E|
   \def]B\noexpand]E|
   \def{B\noexpand{E|
   \def}B\noexpand}EE
\gdef\ignoreMPspecials|
  B\let%\letterpercent|
   \def[BE|
   \def]BE|
   \def{BE|
   \def}BEE
\gdef\obeyMPspecials|
  B\def%B\char 37\relax E|
   \def[B\char 91\relax E|
   \def]B\char 93\relax E|
   \def{B\char123\relax E|
   \def}B\char125\relax EE
\gdef\setMPspecials|
  B\setnaturalcatcodes
   \catcode`\\=\@@escape
   \catcode`\%=\@@active
   \catcode`\[=\@@active
   \catcode`\]=\@@active
   \catcode`\{=\@@active
   \catcode`\}=\@@active
   \lccode`\-=0 | latex sets this to `\-
   \lccode`\%=`\%| otherwise it's seen as a number
   \def\(B\char40\relax     E|
   \def\)B\char41\relax     E|
   \def\\B\char92\relax     E|
   \def\0B\octalMPcharacter0E|
   \def\1B\octalMPcharacter1E|
   \def\2B\octalMPcharacter2E|
   \def\3B\octalMPcharacter3E|
   \def\4B\octalMPcharacter4E|
   \def\5B\octalMPcharacter5E|
   \def\6B\octalMPcharacter6E|
   \def\7B\octalMPcharacter7E|
   \def\8B\octalMPcharacter8E|
   \def\9B\octalMPcharacter9EE
\egroup

%D We use the comment symbol as a sort of trigger. Beware!
%D The whole graphic is seen as on eparagraph, which means
%D that we cannot change the catcodes in between.

\bgroup
\catcode`\%=\@@active
\gdef\startMPscanning{\let%=\startMPconversion}
\egroup

%D In earlier versions we used the sequence
%D
%D \starttyping
%D \expandafter\handleMPsequence\input filename\relax
%D \stoptyping
%D
%D Persistent problems in \LATEX\ however forced us to use a
%D different scheme. Every \POSTSCRIPT\ file starts with a
%D \type{%}, so we temporary make this an active character
%D that starts the scanning and redefines itself. (The problem
%D originates in the redefinition by \LATEX\ of the
%D \type{\input} primitive.)

\def\startMPconversion
  {\keepMPspecials
   \handleMPsequence}

%D Here comes the main loop. Most arguments are numbers. This
%D means that they can be recognized by their \type{\lccode}.
%D This method saves a lot of processing time. We could
%D speed up the conversion by handling the \type{path}
%D seperately.

\def\dohandleMPsequence#1%
  {\ifdone
     \ifcase\lccode`#1\relax
       \@EAEAEA\dohandleMPsequenceA
     \else
       \@EAEAEA\dohandleMPsequenceB
     \fi
   \else
     \@EA\dohandleMPsequenceC
   \fi#1}

\let\dohandleMPsequenceA\setMPsequence

\def\installMPSkeywordN#1#2%
  {\expandafter\def\csname\@@MP:N:#1\endcsname{#2}}

\def\installMPSshortcutN#1#2% todo: \let
  {\expandafter\let\csname\@@MP:N:#1\expandafter\endcsname\csname\@@MP:N:#2\endcsname}

\def\dohandleMPsequenceB#1 %
  {\edef\somestring{#1}%
   \executeifdefined{\@@MP:N:\somestring}\handleMPgraphic
   \handleMPsequence}

\installMPSkeywordN \PSmoveto
  {\edef\lastMPmoveX{\gMPa1}%
   \edef\lastMPmoveY{\gMPa2}%
   \resetMPstack}
\installMPSkeywordN \PSnewpath
  {\let\handleMPsequence\handleMPpath}
\installMPSkeywordN \PSgsave
  {\PDFcode{q}%
   \resetMPstack}
\installMPSkeywordN \PSgrestore
  {\PDFcode{Q}%
   \resetMPstack}
\installMPSkeywordN \PSdtransform  % == setlinewidth
  {\let\handleMPsequence\handleMPdtransform}
   % after that we will encounter more tokens until setlinewidth+pop
   % or pop+setlinewidth which we catch next; we explicitly need to
   % reset the stack since [] n setdash may follow; a more clever
   % approach would be to read on till the condition is met, but it's
   % the only pop / setlinewidth we will encounter so ...
\installMPSkeywordN \PSsetlinewidth
  {% already handled in dtransform
   \resetMPstack}
\installMPSkeywordN \PSpop
  {% already handled in dtransform
   \resetMPstack}
\installMPSkeywordN \PSconcat
  {\cleanupMPconcat
   \PDFcode{\gMPa1 \gMPa2 \gMPa3 \gMPa4 \gMPa5 \gMPa6 cm}%
   \resetMPstack}
\installMPSkeywordN \PSsetrgbcolor
  {\handleMPrgbcolor
   \resetMPstack}
\installMPSkeywordN \PSsetcmykcolor
  {\handleMPcmykcolor
   \resetMPstack}
\installMPSkeywordN \PSsetgray
  {\handleMPgraycolor
   \resetMPstack}
\installMPSkeywordN \PStranslate
  {\PDFcode{1 0 0 1 \gMPa1 \gMPa2 cm}%
   \resetMPstack}
\installMPSkeywordN \PSsetdash
  {\handleMPsetdash
   \resetMPstack}
\installMPSkeywordN \PSsetlinejoin
  {\PDFcode{\gMPa1 j}%
   \resetMPstack}
\installMPSkeywordN \PSsetmiterlimit
  {\PDFcode{\gMPa1 M}%
   \resetMPstack}
\installMPSkeywordN \PSfshow
  {%\PDFcode{n}% removed !
   \handleMPfshow
   \resetMPstack}
\installMPSkeywordN \PSsetlinecap
  {\PDFcode{\gMPa1 J}%
   \resetMPstack}
\installMPSkeywordN \PSrlineto
  {\flushMPmoveto
   \PDFcode{\!MP\lastMPmoveX\space\!MP\lastMPmoveY\space l S}%
   \resetMPmoveto
   \resetMPstack}
\installMPSkeywordN \PSscale
  {\PDFcode{\gMPa1 0 0 \gMPa2 0 0 cm}%
   \resetMPstack}
\installMPSkeywordN \PSspecial
  {\handleMPspecialcommand
   \resetMPstack}

\installMPSshortcutN {n}  \PSnewpath
\installMPSshortcutN {p}  \PSclosepath
\installMPSshortcutN {l}  \PSlineto
\installMPSshortcutN {r}  \PSrlineto
\installMPSshortcutN {m}  \PSmoveto
\installMPSshortcutN {c}  \PScurveto
\installMPSshortcutN {C}  \PSsetcmykcolor
\installMPSshortcutN {G}  \PSsetgray
\installMPSshortcutN {R}  \PSsetrgbcolor
\installMPSshortcutN {lj} \PSsetlinejoin
\installMPSshortcutN {ml} \PSsetmiterlimit
\installMPSshortcutN {lc} \PSsetlinecap
\installMPSshortcutN {sd} \PSsetdash
\installMPSshortcutN {S}  \PSstroke
\installMPSshortcutN {F}  \PSfill
\installMPSshortcutN {W}  \PSclip

\installMPSshortcutN {q}  \PSgsave
\installMPSshortcutN {Q}  \PSgrestore

\installMPSshortcutN {s}  \PSscale
\installMPSshortcutN {t}  \PSconcat
\installMPSshortcutN {P}  \PSshowpage

\installMPSkeywordN {hlw} {\PDFcode{\gMPa1 w}\resetMPstack}
\installMPSkeywordN {vlw} {\PDFcode{\gMPa1 w}\resetMPstack}
\installMPSkeywordN {rd}  {\PDFcode{[] 0   d}\resetMPstack}

\def\dohandleMPsequenceC#1 %
  {\edef\somestring{#1}%
   \handleMPgraphic
   \handleMPsequence}

%D Since colors are not sensitive to transformations, they
%D are sometimes used for signaling. Therefore, we handle them
%D separately. The next macro can be redefined if needed.

\def\handleMPrgbcolor
  {\PDFcode{\!MPgMPa1 \!MPgMPa2 \!MPgMPa3 rg
            \!MPgMPa1 \!MPgMPa2 \!MPgMPa3 RG}}

\def\handleMPcmykcolor
  {\PDFcode{\!MPgMPa1 \!MPgMPa2 \!MPgMPa3 \!MPgMPa4 k
            \!MPgMPa1 \!MPgMPa2 \!MPgMPa3 \!MPgMPa4 K}}

\def\handleMPgraycolor
  {\PDFcode{\!MPgMPa1 g
            \!MPgMPa1 G}}

\def\handleMPspotcolor
  {\PDFcode{0 g
            0 G}}

%D Beginning and ending the graphics is taken care of by the
%D macro \type{\handleMPgraphic}, which is redefined when
%D the first graphics operator is met.

\def\handleMPendgraphic % #1%
  {\ifx\somestring\PSshowpage
     \let\handleMPsequence\finishMPgraphic
   \else\ifx\somestring\PSEof
     \let\handleMPsequence\finishMPgraphic
   \else
     \letMPargument\somestring % {#1}%
   \fi\fi}

\def\handleMPbegingraphic % #1%
  {\ifx\somestring\PSBoundingBox
     \def\handleMPsequence{\handleMPboundingbox1}%
   \else\ifx\somestring\PSHiResBoundingBox
     \def\handleMPsequence{\handleMPboundingbox2}%
   \else\ifx\somestring\PSExactBoundingBox
     \def\handleMPsequence{\handleMPboundingbox3}%
   \else\ifx\somestring\PSshowpage
     \let\handleMPsequence\finishMPgraphic
   \else\ifx\somestring\PSEof
     \let\handleMPsequence\finishMPgraphic
   \else\ifx\somestring\PSPage
     \let\handleMPsequence\handleMPpage
   \else\ifx\somestring\PSMetaPostSpecials
     \let\handleMPsequence\handleMPspecialscomment
   \else\ifx\somestring\PSMetaPostSpecial
     \let\handleMPsequence\handleMPspecialcomment
   \else\ifx\somestring\PSBeginProlog
     \let\handleMPsequence\handleMPprolog
   \else
     \letMPargument\somestring % {#1}%
   \fi\fi\fi\fi\fi\fi\fi\fi\fi}

\let\handleMPgraphic=\handleMPbegingraphic

%D New: we can best filter the prolog because nowdays it can contain
%D quite some code.

% hm, catcode mess, so we need to tweak %'s catcode here
% \long\expandafter\def\expandafter\handleMPprolog\expandafter#\expandafter1\PSEndProlog%
% but today i'm not in the mood for ugly stuff

\long\def\handleMPprolog#1EndProlog %
  {\doresetMPstack
   \let\handleMPsequence\dohandleMPsequence
   \handleMPsequence}

%D We check for three kind of bounding boxes: the normal one
%D and two high precision ones:
%D
%D \starttyping
%D BoundingBox: llx lly ucx ucy
%D HiResBoundingBox: llx lly ucx ucy
%D ExactBoundingBox: llx lly ucx ucy
%D \stoptyping
%D
%D The original as well as the recalculated dimensions are
%D saved for later use.

\newif\ifskipemptyMPgraphic \skipemptyMPgraphicfalse

\chardef\currentMPboundingbox=0

\def\handleMPboundingbox#1#2 #3 #4 #5
  {\ifnum#1>\currentMPboundingbox
     \chardef\currentMPboundingbox#1\relax
     \xdef\MPllx   {#2}%
     \xdef\MPlly   {#3}%
     \xdef\MPurx   {#4}%
     \xdef\MPury   {#5}%
     \xdef\MPwidth {\the\dimexpr\MPurx\onebasepoint-\MPllx\onebasepoint\relax}%
     \xdef\MPheight{\the\dimexpr\MPury\onebasepoint-\MPlly\onebasepoint\relax}%
   \fi
   \doresetMPstack
   \let\handleMPsequence\dohandleMPsequence
   \let\next\handleMPsequence
   \ifskipemptyMPgraphic
     \ifdim\MPheight=\zeropoint\ifdim\MPwidth=\zeropoint
       \def\next{\endinput\finishMPgraphic}%
     \fi\fi
   \fi
   \next}

%D Unless defined otherwise, we simply ignore specialcomments.

\def\handleMPspecialcomment
  {\doresetMPstack
   \let\handleMPsequence\dohandleMPsequence
   \handleMPsequence}

\let\handleMPspecialscomment\handleMPspecialcomment

%D We use the \type{page} comment as a signal that
%D stackbuilding can be started.

\def\handleMPpage #1 #2
  {\doresetMPstack
   \donetrue
   \let\handleMPsequence\dohandleMPsequence
   \handleMPsequence}

%D The same applies to the special extensions.

\def\handleMPspecialcommand
  {\doresetMPstack
   \let\handleMPsequence\dohandleMPsequence
   \handleMPsequence}

%D \METAPOST\ draws its dots by moving to a location and
%D invoking \type{0 0 rlineto}. This operator is not
%D available in \PDF. Our solution is straightforward: we draw
%D a line from $(current\_x, current\_y)$ to itself. This
%D means that the arguments of the preceding \type{moveto} have
%D to be saved.

%D These saved coordinates are also used when we handle the
%D texts. Text handling proved to be a bit of a nuisance, but
%D finally I saw the light. It proved that we also had to
%D take care of \type{(split arguments)}.

% \startMPcode
%     draw btex Ga toch effe f\kern0ptietsen?{}` etex ;
% \stopMPcode

\newtoks \everyMPshowfont

\def\setMPfshowfont#1#2%
  {\font\temp=#1\space at #2\relax\temp
   \the\everyMPshowfont}

\let\MPfshowcommand\empty

\def\dohandleMPfshow
  {\setbox\scratchbox\hbox
     {\obeyMPspecials
      \edef\MPtextsize{\gMPa\nofMParguments}%
      \def\do(##1){##1}% only works in latest mp
      \edef\MPtextdata{\dogMPa1}% beware, stack can have more
      \handleMPtext}%
   \setbox\scratchbox\hbox
     {\hskip\lastMPmoveX\onebasepoint
      \raise\lastMPmoveY\onebasepoint
      \box\scratchbox}%
   \smashbox\scratchbox
   \box\scratchbox}

\def\handleMPtext {\handleMPtextnormal} % so we can overload this one later
\def\handleMPfshow{\dohandleMPfshow   } % so we can overload this one later

\def\handleMPtext
  {\ifnum\nofMParguments>\plusthree
     \handleMPtextnormal
   \else
     \defconvertedcommand\MPtextdata\MPtextdata
     \expanded{\splitstring\MPtextdata}\at::::\to\MPtexttag\and\MPtextnumber
     \executeifdefined{handleMPtext\MPtexttag}\handleMPtextnormal
   \fi}

% elsewhere we will implement \handleMPtextmptxt

\def\doflushMPtext#1%
  {\edef\!!stringa{#1}%
   \@EA\dodoflushMPtext\!!stringa\relax}

\def\dodoflushMPtext
  {\afterassignment\dododoflushMPtext\let\nexttoken=}

\def\dododoflushMPtext
  {\ifx\nexttoken\relax
     % done
   \else\ifx\nexttoken\char
     \@EA\@EA\@EA\dodododoflushMPtext
   \else
     {\nexttoken}%
     \@EA\@EA\@EA\dodoflushMPtext
   \fi\fi}

\def\dodododoflushMPtext
  {\afterassignment\dododododoflushMPtext\scratchcounter}

\def\dododododoflushMPtext
 {{\char\scratchcounter}\let\next\dodoflushMPtext}

\def\handleMPtextnormal
  {\let\ \relax % mp breaks long lines and appends a \
   \ifx\MPtextsize\PSnfont % round font size (to pt)
     \advance\nofMParguments \minusone
     \expandafter\scratchdimen\gMPa\nofMParguments\onepoint\relax
     \ifdim\scratchdimen<\onepoint
       \def\MPtextsize{1pt}%
     \else
       \advance\scratchdimen .5\onepoint
       \def\MPtextsize##1.##2\relax{\def\MPtextsize{##1pt}}%
       \expandafter\MPtextsize\the\scratchdimen\relax
     \fi
   \else
     \edef\MPtextsize{\MPtextsize bp}%
   \fi
   \advance\nofMParguments \minusone
   \setMPfshowfont{\gMPa\nofMParguments}\MPtextsize
   \advance\nofMParguments \minusone
   \temp
   \MPfshowcommand
     {\ifnum\nofMParguments=\plusone
        \def\do(##1){##1}%
        \doflushMPtext{\dogMPa1}%
      \else % can't happen anymore in mp version 1+
        % we need to catch ( a ) (a a a) (\123 \123 \123) etc
        \scratchcounter\plusone
        \def\dodo##1% Andreas Fieger's bug: (\304...)
          {\edef\!!stringa{##1\empty\empty}% and another one: ( 11) -> \ifx 11
           \ifx\!!stringa\MPspacechar\MPspacechar\else\expandafter##1\fi}%
        \def\do(##1{\dodo{##1}}%
        \dogMPa\scratchcounter\MPspacechar
        \let\do\relax
        \loop
          \advance\scratchcounter \plusone
          \ifnum\scratchcounter<\nofMParguments\relax
            \gMPa\scratchcounter\MPspacechar
        \repeat
        \def\do##1){\dodo{##1}}%
        \dogMPa\scratchcounter
      \fi
      \unskip}}

%D You could consider the following definition to be the most
%D natural one.

% \def\MPspacechar{\space} % normal case

\def\MPspacechar{\char32\relax} % old solution does not work with math

%D However, the following implementation is more robust, since
%D some fonts have funny visible spaces in the space slot. This
%D gives a mismatch between the space that \METAPOST\ took into
%D account and the \quote {natural} space. This only happens in
%D labels, since \type {btex}||\type {etex} thingies don't have
%D spaces. This phenomena showed up when preparing the
%D \METAFUN\ manual, where Palatino fonts are used. We can
%D safely assume that \METAPOST\ considers \type {\char32} to
%D be the space.

\def\MPspacechar{\setbox\scratchbox\hbox{\char32}\kern\wd\scratchbox}

%D Well, this does not work with math fonts, so:

\def\MPspacechar{\char32\relax}

%D Most operators are just converted and keep their
%D arguments. Dashes however need a bit different treatment,
%D otherwise \PDF\ viewers complain loudly. Another
%D complication is that one argument comes after the \type{]}.
%D When reading the data, we simply ignore the array boundary
%D characters. We save ourselves some redundant newlines and
%D at the same time keep the output readable by packing the
%D literals.

\def\handleMPsetdash
  {\bgroup
   \ignoreMPspecials
   \let\somestring\empty
   \scratchcounter\plusone
   \loop
     \ifnum\scratchcounter<\nofMParguments
       \edef\somestring{\somestring\space\gMPa\scratchcounter}%
       \advance\scratchcounter \plusone
   \repeat
   \edef\somestring{[\somestring]\space\gMPa\scratchcounter\space d}%
   \PDFcode{\somestring}%
   \egroup}

%D The \type{setlinewidth} commands looks a bit complicated. There are
%D two alternatives, that result in a similar look in both
%D $x$- and $y$-dorection. As John Hobby says:
%D
%D \startnarrower \switchtobodyfont[ss]
%D \starttyping
%D x 0 dtransform exch truncate exch idtransform pop setlinewidth
%D 0 y dtransform truncate idtransform setlinewidth pop
%D \stoptyping
%D
%D These are just fancy versions of \type{x setlinewidth} and
%D \type{y setlinewidth}. The \type{x 0 ...} form is used if
%D the path is {\em primarily vertical}. It rounds the width
%D so that vertical lines come out an integer number of pixels
%D wide in device space. The \type{0 y ...} form does the same
%D for paths that are {\em primarily horizontal}. The reason
%D why I did this is Knuth insists on getting exactly the
%D widths \TEX\ intends for the horizontal and vertical rules
%D in \type{btex...etex} output. (Note that PostScript scan
%D conversion rules cause a horizontal or vertical line of
%D integer width $n$ in device space to come out $n+1$ pixels
%D wide, regardless of the phase relative to the pixel grid.)
%D \stopnarrower
%D
%D The common operator in these sequences is \type{dtransform},
%D so we can use this one to trigger setting the linewidth.

\def\handleMPdtransform
  {\ifdim\gMPa1\onepoint>\zeropoint
     \PDFcode{\gMPa1 w}%
     \def\next##1 ##2 ##3 ##4 ##5 ##6 {\handleMPsequence}%
   \else
     \PDFcode{\gMPa2 w}%
     \def\next##1 ##2 ##3 ##4 {\handleMPsequence}%
   \fi
   \let\handleMPsequence\dohandleMPsequence
   \resetMPstack
   \next}

%D The most complicated command is \type{concat}. \METAPOST\
%D applies this operator to \type{stroke}. At that moment the
%D points set by \type{curveto} and \type{moveto}, are already
%D fixed. In \PDF\ however the \type{cm} operator affects the
%D points as well as the pen (stroke). Like more \PDF\
%D operators, \type{cm} is defined in a bit ambiguous way.
%D The only save route for non||circular penshapes, is saving
%D the path, recalculating the points and applying the
%D transformation matrix in such a way that we can be sure
%D that its behavior is well defined. This comes down to
%D inverting the path and applying \type{cm} to that path as
%D well as the pen. This all means that we have to save the
%D path.

%D In \METAPOST\ there are three ways to handle a path $p$:
%D
%D \starttyping
%D draw p;  fill p;  filldraw p;
%D \stoptyping
%D
%D The last case outputs a \type{gsave fill grestore} before
%D \type{stroke}. Handling the path outside the main loops
%D saves about 40\% run time.\footnote{We can save some more by
%D following the \METAPOST\ output routine, but for the moment
%D we keep things simple.} Switching between the main loop and
%D the path loop is done by means of the recursely called
%D macro \type{\handleMPsequence}.

\def\handleMPpath
  {\chardef\finiMPpath\zerocount
   \let\closeMPpath\relax
   \let\flushMPpath\flushnormalMPpath
   \resetMPstack
   \nofMPsegments\plusone
   \let\handleMPsequence\dohandleMPpath
   \dohandleMPpath}

%D Most paths are drawn with simple round pens. Therefore we've
%D split up the routine in two.

\def\resetMPmoveto
  {\let\lastMPmoveX\empty
   \let\lastMPmoveY\empty}

\resetMPmoveto

\def\flushMPmoveto
  {\ifx\lastMPmoveX\empty \else
     \PDFcode{\!MP\lastMPmoveX\space \!MP\lastMPmoveY\space m}%
   \fi}

\def\flushnormalMPsegment
  {\ifcase\getMPkeyword\relax
     \flushMPmoveto
     \resetMPmoveto
     \PDFcode{\!MPgMPs1 \!MPgMPs2 l}%
   \or
     \flushMPmoveto
     \resetMPmoveto
     \PDFcode{\!MPgMPs1 \!MPgMPs2 \!MPgMPs3 \!MPgMPs4 \!MPgMPs5 \!MPgMPs6 c}%
   \or
     \ifx\lastMPmoveX\empty \else % we assume 0,0 rlineto
       \flushMPmoveto
       \PDFcode{\!MP\lastMPmoveX\space \!MP\lastMPmoveY\space l}%
       \resetMPmoveto
     \fi
   \or
     % \flushMPmoveto
     % \resetMPmoveto
   \fi}

\def\flushMPconcatmoveto
   {\ifx\lastMPmoveX\empty\else
      \doMPconcat\lastMPmoveX\lastMPmoveX\lastMPmoveY\lastMPmoveY
      \flushMPmoveto
    \fi}

\def\flushconcatMPsegment
  {\ifcase\getMPkeyword\relax
     \flushMPconcatmoveto
     \resetMPmoveto
     \doMPconcat{\gMPs1}\a{\gMPs2}\b%
     \PDFcode{\!MP\a\space\!MP\b\space l}%
   \or
     \flushMPconcatmoveto
     \resetMPmoveto
     \doMPconcat{\gMPs1}\a{\gMPs2}\b%
     \doMPconcat{\gMPs3}\c{\gMPs4}\d%
     \doMPconcat{\gMPs5}\e{\gMPs6}\f%
     \PDFcode{\!MP\a\space\!MP\b\space
              \!MP\c\space\!MP\d\space
              \!MP\e\space\!MP\f\space c}%
   \or % rather mp specific ... rline always has 0,0
     \bgroup
     \noMPtranslate
     \flushMPconcatmoveto
     % next should be \lastMPmoveX+\a,\lastMPmoveY+\b but we know it's 0,0
     \PDFcode{\!MP\lastMPmoveX\space\!MP\lastMPmoveY\space l S}%
     \resetMPmoveto
     \egroup
   \or
%      \flushMPconcatmoveto
%      \resetMPmoveto
   \fi}

\def\doflushsomeMPpath
  {\dodoflushsomeMPpath
   \advance\nofMPsegments \plusone
   \ifnum\nofMPsegments<\scratchcounter
     \expandafter\doflushsomeMPpath
   \fi}

\def\flushsomeMPpath
  {\scratchcounter\nofMPsegments
   \nofMPsegments\plusone
   \doflushsomeMPpath}

\def\flushnormalMPpath{\let\dodoflushsomeMPpath\flushnormalMPsegment\flushsomeMPpath}

%OLD \def\flushconcatMPpath{\let\dodoflushsomeMPpath\flushconcatMPsegment\flushsomeMPpath}

%NEW pre-calculate 1/D so it needn't be repeated for each control point.

\def\flushconcatMPpath
  {\MPreciprocaldeterminant
   \let\dodoflushsomeMPpath\flushconcatMPsegment\flushsomeMPpath}

%D The transformation of the coordinates is handled by one of
%D the macros Tanmoy posted to the \PDFTEX\ mailing list.
%D I rewrote and optimized the original macro to suit the other
%D macros in this module.
%D
%D \starttyping
%D \doMPconcat {x position} \xresult {y position} \yresult
%D \stoptyping
%D
%D By setting the auxiliary \DIMENSIONS\ \type{\dimen0} upto
%D \type{\dimen10} only once per path, we save over 20\% run
%D time. Some more speed was gained by removing some parameter
%D passing. These macros can be optimized a bit more by using
%D more constants. There is however not much need for further
%D optimization because penshapes usually are round and
%D therefore need no transformation. Nevertheless we move the
%D factor to the outer level and use a bit different \type{pt}
%D removal macro. Although the values represent base points,
%D we converted them to pure points, simply because those can
%D be converted back.

%OLD \mathchardef\MPconcatfactor=256 % beware don't remove spaces before it

%OLD \def\doMPreducedimen#1
%OLD   {\count0\MPconcatfactor
%OLD    \advance\dimen#1 \ifdim\dimen#1>\zeropoint .5\else -.5\fi\count0
%OLD    \divide\dimen#1 \count0\relax}

%OLD % too inaccurate (see old pragma logo)
%OLD
%OLD \def\doMPreducedimen#1
%OLD   {\count0=\MPconcatfactor
%OLD    \divide\dimen#1 \count0\relax}

%OLD \def\doMPreducedimen#1
%OLD   {\advance\dimen#1 \ifdim\dimen#1>\zeropoint .5\else -.5\fi\MPconcatfactor
%OLD    \divide\dimen#1 \MPconcatfactor}

%D The transformation code is rewritten by Daniel H. Luecking who
%D describes his patch as follows:
%D
%D We would like to divide 1 by $X$, but all divisions are integer so
%D for accuracy we want to convert to large integers and make sure the
%D integer quotient has as many significant digits as possible. Thus we
%D need to replace $1/X$ with $M/N$ where $N$ is as large as possible
%D and $M/N$ is as large as possible. Also for simplicity $M$ should be
%D a power of 2. So we make $M = 2^{30}$ \footnote{$2^{31} - 1$ is the
%D largest legal integer. Using it (and simply ignoring the inaccuracy
%D caused by $-1$) turns out to be at least as accurate in all cases,
%D and more accurate in some.} (largest legal power of 2) and adjust
%D $X$ downward (if necessary) to the the range $1-2^{16}$. This gives
%D at least 15 significant binary digits, (almost as accurate as
%D \METAPOST\ for numbers near 1) or almost 5 significant figures
%D (decimal).

\newcount\MPscratchCnt
\newdimen\MPscratchDim % will be assigned global

\def\MPadjustdimen   % sets \MPscratchDim and \MPscratchCnt
  {\MPscratchCnt\zerocount
   \doMPadjustdimen}

\def\doMPadjustdimen
  {\ifdim\MPscratchDim>\onepoint
     \divide \MPscratchDim\plustwo
     \advance\MPscratchCnt\plusone
     \expandafter\doMPadjustdimen
   \fi}

%OLD \def\doMPexpanddimen#1
%OLD   {\multiply\dimen#1 \MPconcatfactor\relax}

%D DHL: When viewed as an integer, $1 \hbox{pt}=2^{16}$ so $2^{32}/X$
%D is  the right way to do $(1 \hbox{pt})/(X \hbox{pt})$ and get the
%D answer  in points. But we are limited to $2^{30}/X$. However, we
%D actually do  $[ 2^{30} / (X/2^K) ]*2^{2-K}$ where $K$ is the number
%D of halvings it takes to bring $X$ below $1 \hbox{pt}$. If $K$ is 0
%D or 1 we readjust by multiplying by 4 or 2, otherwise by halving
%D $(K-2)$ times \type {\MPscratchCnt} holds the value of $K$ from
%D \type {\MPadjustdimen}.

\def\MPreadjustdimen  % acts on \MPscratchDim and MPscratchCnt
  {\ifcase\MPscratchCnt
     \multiply\scratchdimen \plusfour
   \or
     \multiply\scratchdimen \plustwo
   \else
     \expandafter\doMPreadjustdimen
   \fi}

\def\doMPreadjustdimen
  {\ifnum\MPscratchCnt>\plustwo
     \divide \scratchdimen\plustwo
     \advance\MPscratchCnt\minusone
     \expandafter\doMPreadjustdimen
   \fi}

\def\MPreciprocaldeterminant
  {\scratchdimen\withoutpt\the\dimen0 \dimen6           % s_x*s_y
   \advance\scratchdimen -\withoutpt\the\dimen2 \dimen4 % s_x*s_y - r_x*r_y
   \ifdim\scratchdimen<\zeropoint  % we need a positive dimension
     \scratchdimen-\scratchdimen   % for \MPadjustdimen (?)
     \doMPreciprocal
     \scratchdimen-\scratchdimen
   \else
     \doMPreciprocal
   \fi
   \edef\MPreciprocal{\withoutpt\the\scratchdimen}}

\newcount\MPnumerator \MPnumerator = 1073741824 % 2^{30}

% todo: dimexpr

\def\doMPreciprocal % replace \scratchdimen with its reciprocal
  {\ifdim\scratchdimen=\onepoint \else
     \MPadjustdimen
     \scratchcounter\MPnumerator
     \divide\scratchcounter\scratchdimen
     \scratchdimen1\scratchcounter % 1 needed
     \MPreadjustdimen
  \fi}

%OLD \def\presetMPconcat
%OLD  {\dimen 0=\gMPs1\onepoint \doMPreducedimen 0    % r_x
%OLD   \dimen 2=\gMPs2\onepoint \doMPreducedimen 2    % s_x
%OLD   \dimen 4=\gMPs3\onepoint \doMPreducedimen 4    % s_y
%OLD   \dimen 6=\gMPs4\onepoint \doMPreducedimen 6    % r_y
%OLD   \dimen 8=\gMPs5\onepoint \doMPreducedimen 8    % t_x
%OLD   \dimen10=\gMPs6\onepoint \doMPreducedimen10 }  % t_y
%OLD
%OLD \def\presetMPscale
%OLD   {\dimen 0=\gMPs1\onepoint \doMPreducedimen 0
%OLD    \dimen 2 \zeropoint
%OLD    \dimen 4 \zeropoint
%OLD    \dimen 6=\gMPs2\onepoint \doMPreducedimen 6
%OLD    \dimen 8 \zeropoint
%OLD    \dimen10 \zeropoint}

\def\cleanupMPconcat
  {\ignoreMPspecials
   \docleanupMPargument1%
   \docleanupMPargument6%
   \keepMPspecials}

\def\presetMPconcat
  {\dimen 0=\gMPs1\onepoint   % s_x
   \dimen 2=\gMPs2\onepoint   % r_x
   \dimen 4=\gMPs3\onepoint   % r_y
   \dimen 6=\gMPs4\onepoint   % s_y
   \dimen 8=\gMPs5\onepoint   % t_x
   \dimen10=\gMPs6\onepoint}  % t_y

\def\presetMPscale
  {\dimen 0=\gMPs1\onepoint
   \dimen 2 \zeropoint
   \dimen 4 \zeropoint
   \dimen 6=\gMPs2\onepoint
   \dimen 8 \zeropoint
   \dimen10 \zeropoint}

\def\noMPtranslate % use this one grouped
  {\dimen 8 \zeropoint        % t_x
   \dimen10 \zeropoint}       % t_y

%D \starttyping
%D \def\doMPconcat#1#2#3#4%
%D   {\dimen12=#1 pt \doMPreducedimen12 % p_x
%D    \dimen14=#3 pt \doMPreducedimen14 % p_y
%D    %
%D    \dimen16  \dimen 0
%D    \multiply \dimen16  \dimen 6
%D    \dimen20  \dimen 2
%D    \multiply \dimen20  \dimen 4
%D    \advance  \dimen16 -\dimen20
%D    %
%D    \dimen18  \dimen12
%D    \multiply \dimen18  \dimen 6
%D    \dimen20  \dimen14
%D    \multiply \dimen20  \dimen 4
%D    \advance  \dimen18 -\dimen20
%D    \dimen20  \dimen 4
%D    \multiply \dimen20  \dimen10
%D    \advance  \dimen18  \dimen20
%D    \dimen20  \dimen 6
%D    \multiply \dimen20  \dimen 8
%D    \advance  \dimen18 -\dimen20
%D    %
%D    \multiply \dimen12 -\dimen 2
%D    \multiply \dimen14  \dimen 0
%D    \advance  \dimen12  \dimen14
%D    \dimen20  \dimen 2
%D    \multiply \dimen20  \dimen 8
%D    \advance  \dimen12  \dimen20
%D    \dimen20  \dimen 0
%D    \multiply \dimen20  \dimen10
%D    \advance  \dimen12 -\dimen20
%D    %
%D    \doMPreducedimen16
%D    \divide   \dimen18  \dimen16 \doMPexpanddimen18
%D    \divide   \dimen12  \dimen16 \doMPexpanddimen12
%D    %
%D    \edef#2{\withoutpt\the\dimen18}% % p_x^\prime
%D    \edef#4{\withoutpt\the\dimen12}} % p_y^\prime
%D \stoptyping

%D The following optimization resulted from some tests by
%D and email exchanges with Sanjoy Mahajan.
%D
%D \starttyping
%D \def\doMPconcat#1#2#3#4%
%D   {\dimen12=#1 pt \doMPreducedimen12 % p_x
%D    \dimen14=#3 pt \doMPreducedimen14 % p_y
%D    %
%D    \dimen16  \dimen 0
%D    \multiply \dimen16  \dimen 6
%D    \dimen20  \dimen 2
%D    \multiply \dimen20  \dimen 4
%D    \advance  \dimen16 -\dimen20
%D    %
%D    \dimen18  \dimen12
%D    \multiply \dimen18  \dimen 6
%D    \dimen20  \dimen14
%D    \multiply \dimen20  \dimen 4
%D    \advance  \dimen18 -\dimen20
%D    \dimen20  \dimen 4
%D    \multiply \dimen20  \dimen10
%D    \advance  \dimen18  \dimen20
%D    \dimen20  \dimen 6
%D    \multiply \dimen20  \dimen 8
%D    \advance  \dimen18 -\dimen20
%D    %
%D    \multiply \dimen12 -\dimen 2
%D    \multiply \dimen14  \dimen 0
%D    \advance  \dimen12  \dimen14
%D    \dimen20  \dimen 2
%D    \multiply \dimen20  \dimen 8
%D    \advance  \dimen12  \dimen20
%D    \dimen20  \dimen 0
%D    \multiply \dimen20  \dimen10
%D    \advance  \dimen12 -\dimen20
%D    %
%D   %\ifdim\dimen16>\onepoint % oeps, can be < 1pt too
%D    \ifdim\dimen16=\onepoint \else
%D      \ifdim\dimen16>\MPconcatfactor pt
%D        \doMPreducedimen16
%D        \divide \dimen18 \dimen16 \doMPexpanddimen18
%D        \divide \dimen12 \dimen16 \doMPexpanddimen12
%D      \else
%D        \divide \dimen18 \dimen16 \doMPexpanddimen18 \doMPexpanddimen18
%D        \divide \dimen12 \dimen16 \doMPexpanddimen12 \doMPexpanddimen12
%D      \fi
%D    \fi
%D    %
%D    \edef#2{\withoutpt\the\dimen18}% % p_x^\prime
%D    \edef#4{\withoutpt\the\dimen12}} % p_y^\prime
%D \stoptyping
%D
%D But, this one is still too inaccurate, so we now have:

%D DHL: Ideally, $r_x$, $r_y$, $s_x$, $s_y$ should be in macros, not
%D dimensions (they are scalar quantities after all, not lengths). I
%D suppose the authors decided to do calculations with integer
%D arithmetic instead of using real factors because it's faster.
%D However, the actual macros test slower, possibly because I've
%D omitted three nested loops. In my test files, my approach is more
%D accurate. It is also far simpler and overflow does not seem to be a
%D significant concern. The scale factors written by Metapost are (?)
%D always $<=1$ (it scales coordinates internally) and coordinates are
%D always likely to be less than \type {\maxdimen}.
%D
%D If this should ever cause problems, the scale factors can be reduced.

% the original:
%
% \def\doMPconcat#1#2#3#4%
%   {\dimen12=#1\onepoint% p_x % #1\onepoint
%    \dimen14=#3\onepoint% p_y % #3\onepoint
%    \advance\dimen12 -\dimen8  % p_x - t_x
%    \advance\dimen14 -\dimen10 % p_y - t_y
%    \dimen18=\withoutpt\the\dimen6 \dimen12          % s_y(p_x - t_x)
%    \advance\dimen18 -\withoutpt\the\dimen4 \dimen14 %   - r_y(p_y-t_y)
%    \dimen14=\withoutpt\the\dimen0 \dimen14          % s_x(p_y-t_y)
%    \advance\dimen14 -\withoutpt\the\dimen2 \dimen12 %   - r_x(p_x-t_x)
%    % \MPreciprocal contains precomputed 1/D:
%    \dimen18=\MPreciprocal\dimen18
%    \dimen14=\MPreciprocal\dimen14
%    \edef#2{\withoutpt\the\dimen18}% % p_x^\prime
%    \edef#4{\withoutpt\the\dimen14}} % p_y^\prime
%
% faster but not that often used

\def\doMPconcat#1#2#3#4%
  {\dimen12\dimexpr#1\points-\dimen 8\relax % p_x-t_x
   \dimen14\dimexpr#3\points-\dimen10\relax % p_y-t_y
   \dimen18\dimexpr\withoutpt\the\dimen6\dimen12-\withoutpt\the\dimen4\dimen14\relax % s_y(p_x-t_x)-r_y(p_y-t_y)
   \dimen14\dimexpr\withoutpt\the\dimen0\dimen14-\withoutpt\the\dimen2\dimen12\relax % s_x(p_y-t_y)-r_x(p_x-t_x)
   \edef#2{\withoutpt\the\dimexpr\MPreciprocal\dimen18\relax}% % p_x^\prime
   \edef#4{\withoutpt\the\dimexpr\MPreciprocal\dimen14\relax}} % p_y^\prime

%D One reason for Daniel to write this patch was that at small sizes
%D the accuracy was less than optimal. Here is a test that demonstrates
%D that his alternative is pretty good:
%D
%D \startlinecorrection
%D \startMPcode
%D for i = 5cm,1cm,5mm,1mm,.5mm,.1mm,.01mm :
%D     draw fullcircle scaled i withpen pencircle xscaled (i/10) yscaled (i/20) rotated 45 ;
%D endfor ;
%D \stopMPcode
%D \stoplinecorrection

%D The following explanation of the conversion process was
%D posted to the \PDFTEX\ mailing list by Tanmoy. The original
%D macro was part of a set of macro's that included sinus and
%D cosinus calculations as well as scaling and translating. The
%D \METAPOST\ to \PDF\ conversion however only needs
%D transformation.

%M \start \switchtobodyfont [ss]

%D Given a point $(U_x, U_y)$ in user coordinates, the business
%D of \POSTSCRIPT\ is to convert it to device space. Let us say
%D that the device space coordinates are $(D_x, D_y)$. Then, in
%D \POSTSCRIPT\ $(D_x, D_y)$ can be written in terms of
%D $(U_x, U_y)$ in matrix notation, either as
%D
%D \placeformula
%D   \startformula
%D   \pmatrix{D_x&D_y&1\cr} = \pmatrix{U_x&U_y&1\cr}
%D                            \pmatrix{s_x&r_x&0\cr
%D                                     r_y&s_y&0\cr
%D                                     t_x&t_y&1\cr}
%D   \stopformula
%D
%D or
%D
%D \placeformula
%D   \startformula
%D    \pmatrix{D_x\cr D_y\cr 1} = \pmatrix{s_x&r_y&t_x\cr
%D                                         r_x&s_y&t_y\cr
%D                                         0  &0  &1  \cr}
%D                                \pmatrix{U_x\cr
%D                                         U_y\cr
%D                                         1  \cr}
%D   \stopformula
%D
%D both of which is a shorthand for the same set of equations:
%D
%D \placeformula
%D   \startformula
%D      D_x = s_x U_x + r_y U_y + t_x
%D   \stopformula
%D
%D \placeformula
%D   \startformula
%D      D_y = r_x U_x + s_y U_y + t_y
%D   \stopformula
%D
%D which define what is called an `affine transformation'.
%D
%D \POSTSCRIPT\ represents the `transformation matrix' as a
%D six element matrix instead of a $3\times 3$ array because
%D three of the elements are always~0, 0 and~1. Thus the above
%D transformation is written in postscript as $[s_x\, r_x\,
%D r_y\, s_y\, t_x\, t_y]$. However, when doing any
%D calculations, it is useful to go back to the original
%D matrix notation (whichever: I will use the second) and
%D continue from there.
%D
%D As an example, if the current transformation matrix is
%D $[s_x\, r_x\, r_y\, s_y\, t_x\, t_y]$ and you say \typ{[a b
%D c d e f] concat}, this means:
%D
%D \startnarrower
%D Take the user space coordinates and transform them to an
%D intermediate set of coordinates using array $[a\, b\, c\, d\,
%D e\, f]$ as the transformation matrix.
%D
%D Take the intermediate set of coordinates and change them to
%D device coordinates using array $[s_x\, r_x\, r_y\, s_y\, t_x\, t_y]$
%D as the transformation matrix.
%D \stopnarrower
%D
%D Well, what is the net effect? In matrix notation, it is
%D
%D \placeformula
%D   \startformula
%D     \pmatrix{I_x\cr I_y\cr 1\cr} = \pmatrix{a&c&e\cr
%D                                             b&d&f\cr
%D                                             0&0&1\cr}
%D                                    \pmatrix{U_x\cr
%D                                             U_y\cr
%D                                             1  \cr}
%D   \stopformula
%D
%D \placeformula
%D   \startformula
%D     \pmatrix{D_y\cr D_y\cr 1\cr} = \pmatrix{s_x&r_y&t_x\cr
%D                                             r_x&s_y&t_y\cr
%D                                             0  &0  &1  \cr}
%D                                    \pmatrix{I_x\cr
%D                                             I_y\cr
%D                                             1  \cr}
%D   \stopformula
%D
%D where $(I_x, I_y)$ is the intermediate coordinate.
%D
%D Now, the beauty of the matrix notation is that when there is
%D a chain of such matrix equations, one can always compose
%D them into one matrix equation using the standard matrix
%D composition law. The composite matrix from two matrices can
%D be derived very easily: the element in the $i$\high{th}
%D horizontal row and $j$\high{th} vertical column is
%D calculated by`multiplying' the $i$\high{th} row of the first
%D matrix and the $j$\high{th} column of the second matrix (and
%D summing over the elements). Thus, in the above:
%D
%D \placeformula
%D   \startformula
%D   \pmatrix{D_x\cr D_y\cr 1} = \pmatrix{s_x^\prime&r_y^\prime&t_x^\prime\cr
%D                                        r_x^\prime&s_y^\prime&t_y^\prime\cr
%D                                        0         &0         &0        \cr}
%D                               \pmatrix{U_x\cr
%D                                        U_y\cr
%D                                        1  \cr}
%D   \stopformula
%D
%D with
%D
%D \placeformula
%D   \startformula
%D      \eqalign
%D        {s_x^\prime & = s_x a + r_y b       \cr
%D         r_x^\prime & = r_x a + s_y b       \cr
%D         r_y^\prime & = s_x c + r_y d       \cr
%D         s_y^\prime & = r_x c + s_y d       \cr
%D         t_x^\prime & = s_x e + r_y f + t_x \cr
%D         t_y^\prime & = r_x e + s_y f + t_y \cr}
%D   \stopformula

%D In fact, the same rule is true not only when one is going
%D from user coordinates to device coordinates, but whenever
%D one is composing two `transformations' together
%D (transformations are `associative'). Note that the formula
%D is not symmetric: you have to keep track of which
%D transformation existed before (i.e.\ the equivalent of
%D $[s_x\, r_x\, r_y\, s_y\, t_x\, t_y]$) and which was
%D specified later (i.e.\ the equivalent of $[a\, b\, c\, d\,
%D e\, f]$). Note also that the language can be rather
%D confusing: the one specified later `acts earlier',
%D converting the user space coordinates to intermediate
%D coordinates, which are then acted upon by the pre||existing
%D transformation. The important point is that order of
%D transformation matrices cannot be flipped (transformations
%D are not `commutative').
%D
%D Now what does it mean to move a transformation matrix
%D before a drawing? What it means is that given a point
%D $(P_x, P_y)$ we need a different set of coordinates
%D $(P_x^\prime, P_y^\prime)$ such that if the transformation
%D acts on $(P_x^\prime, P_y^\prime)$, they produce $(P_x,
%D P_y)$. That is we need to solve the set of equations:
%D
%D \placeformula
%D   \startformula
%D     \pmatrix{P_x\cr P_y\cr 1\cr} = \pmatrix{s_x&r_y&t_x\cr
%D                                             r_x&s_y&t_y\cr
%D                                             0  &0  &1  \cr}
%D                                    \pmatrix{P_x^\prime\cr
%D                                             P_y^\prime\cr
%D                                             1         \cr}
%D   \stopformula
%D
%D Again matrix notation comes in handy (i.e. someone has
%D already solved the problem for us): we need the inverse
%D transformation matrix. The inverse transformation matrix can
%D be calculated very easily:
%D
%D \placeformula
%D   \startformula
%D     \pmatrix{P_x^\prime\cr P_y^\prime\cr 1\cr} =
%D        \pmatrix{s_x^\prime&r_y^\prime&t_x^\prime\cr
%D                 r_x^\prime&s_y^\prime&t_y^\prime\cr
%D                 0  &0  &1  \cr}
%D        \pmatrix{P_x\cr
%D                 P_y\cr
%D                 1         \cr}
%D   \stopformula
%D
%D where, the inverse transformation matrix is given by
%D
%D \placeformula
%D   \startformula
%D     \eqalign
%D       {D          & = s_x s_y - r_x r_y           \cr
%D        s_x^\prime & = s_y / D                     \cr
%D        s_y^\prime & = s_x / D                     \cr
%D        r_x^\prime & = - r_x / D                   \cr
%D        r_y^\prime & = - r_y / D                   \cr
%D        t_x^\prime & = ( - s_y t_x + r_y t_y ) / D \cr
%D        t_y^\prime & = (   r_x t_x - s_x t_y ) / D \cr}
%D   \stopformula
%D
%D And you can see that when expanded out, this does
%D give the formulas:
%D
%D \placeformula
%D   \startformula
%D     P_x^\prime = { { s_y(p_x-t_x) + r_y(t_y-p_y) } \over
%D                    { s_x s_y-r_x r_y } }
%D   \stopformula
%D
%D \placeformula
%D   \startformula
%D     P_y^\prime = { { s_x(p_y-t_y) + r_x(t_x-p_x) } \over
%D                    { s_x*s_y-r_x*r_y } }
%D   \stopformula
%D
%D The code works by representing a real number by converting
%D it to a dimension to be put into a \DIMENSION\ register: 2.3 would
%D be represented as 2.3pt for example. In this scheme,
%D multiplying two numbers involves multiplying the \DIMENSION\
%D registers and dividing by 65536. Accuracy demands that the
%D division be done as late as possible, but overflow
%D considerations need early division.
%D
%D Division involves dividing the two \DIMENSION\ registers and
%D multiplying the result by 65536. Again, accuracy would
%D demand that the numerator be multiplied (and|/|or the
%D denominator divided) early: but that can lead to overflow
%D which needs to be avoided.
%D
%D If nothing is known about the numbers to start with (in
%D concat), I have chosen to divide the 65536 as a 256 in each
%D operand. However, in the series calculating the sine and
%D cosine, I know that the terms are small (because I never
%D have an angle greater than 45 degrees), so I chose to
%D apportion the factor in a different way.

%M \stop

%D The path is output using the values saved on the stack. If
%D needed, all coordinates are recalculated.

\def\finishMPpath
  {\PDFcode{\ifcase\finiMPpath W n\or S\or f\or B\fi}}

\def\processMPpath
  {\checkMPpath
   \ifcase\nofMPsegments\else
     \flushMPpath
     \closeMPpath
     \finishMPpath
   \fi
   \let\handleMPsequence\dohandleMPsequence
   \resetMPstack
   \nofMPsegments\zerocount
   \handleMPsequence}

%D The following \METAPOST\ code is quite valid but, when
%D processed and converted to \PDF, will make a file
%D unprintable on a Hewlett Packard printer (from Acrobat
%D $v<=5$). Who is to blame, the driver of the OS layer in
%D between, is hard to determine, so we add an additional
%D check.
%D
%D \starttyping
%D clip currentpicture to origin -- cycle ;
%D setbounds currentpicture to fullsquare scaled 5cm ;
%D \stoptyping

\def\checkMPpath
  {\ifcase\finiMPpath
     \ifnum\nofMPsegments<\plusthree % n is one ahead
       \message{omitting zero clip path}%
       \nofMPsegments\zerocount
     \fi
   \fi}

%D In \PDF\ the \type{cm} operator must precede the path
%D specification. We therefore can output the \type{cm} at
%D the moment we encounter it.

\def\handleMPpathconcat
  {\presetMPconcat
   \PDFcode{\gMPs1 \gMPs2 \gMPs3 \gMPs4 \gMPs5 \gMPs6 cm}%
   \resetMPstack}

\def\handleMPpathscale
  {\presetMPscale
   \PDFcode{\gMPs1 0 0 \gMPs2 0 0 cm}%
   \resetMPstack}

%D This macro interprets the path and saves it as compact as
%D possible.

\def\dohandleMPpath#1%
  {\ifcase\lccode`#1\relax
     \@EA\dohandleMPpathA
   \else
     \@EA\dohandleMPpathB
   \fi#1}

\let\dohandleMPpathA\setMPsequence

\def\installMPSkeywordP#1#2%
  {\expandafter\def\csname\@@MP:P:#1\endcsname{#2}}

\def\installMPSshortcutP#1#2% todo: \let
  {\expandafter\let\csname\@@MP:P:#1\expandafter\endcsname\csname\@@MP:P:#2\endcsname}

\def\dohandleMPpathB#1 %
  {\def\somestring{#1}%
   \executeifdefined{\@@MP:P:\somestring}\relax
   \handleMPsequence}

\installMPSkeywordP \PSlineto
  {\setMPkeyword0 }
\installMPSkeywordP \PScurveto
  {\setMPkeyword1 }
\installMPSkeywordP \PSrlineto
  {\setMPkeyword2 }
\installMPSkeywordP \PSmoveto
  {\edef\lastMPmoveX{\gMPs1}%
   \edef\lastMPmoveY{\gMPs2}%
   \resetMPstack
   \setMPkeyword3 }
\installMPSkeywordP \PSclip
  {% \chardef\finiMPpath\zerocount % already
   \let\handleMPsequence\processMPpath}
\installMPSkeywordP \PSgsave
  {\chardef\finiMPpath\plusthree}
\installMPSkeywordP \PSgrestore
  {}
\installMPSkeywordP \PSfill
  {\ifcase\finiMPpath
     \chardef\finiMPpath\plustwo
     \let\handleMPsequence\processMPpath
   \fi}
\installMPSkeywordP \PSstroke
  {\ifcase\finiMPpath
    \chardef\finiMPpath\plusone
   \fi
   \let\handleMPsequence\processMPpath}
\installMPSkeywordP \PSclosepath
  {\def\closeMPpath{\PDFcode{h}}}
\installMPSkeywordP \PSconcat
  {\cleanupMPconcat
   \let\flushMPpath\flushconcatMPpath
   \handleMPpathconcat}
\installMPSkeywordP \PSscale
  {\let\flushMPpath\flushconcatMPpath
   \handleMPpathscale}

\installMPSshortcutP {l} \PSlineto
\installMPSshortcutP {r} \PSrlineto
\installMPSshortcutP {m} \PSmoveto
\installMPSshortcutP {c} \PScurveto

\installMPSshortcutP {q} \PSgsave
\installMPSshortcutP {Q} \PSgrestore
\installMPSshortcutP {S} \PSstroke
\installMPSshortcutP {F} \PSfill
\installMPSshortcutP {B} \PSgsave
\installMPSshortcutP {W} \PSclip
\installMPSshortcutP {p} \PSclosepath

\installMPSshortcutP {s} \PSscale
\installMPSshortcutP {t} \PSconcat

%D \macros
%D   {twodigitMPoutput}
%D
%D We can limit the precision to two digits after the comma
%D by saying:
%D
%D \starttyping
%D \twodigitMPoutput
%D \stoptyping
%D
%D This option only works in \CONTEXT\ combined with \ETEX.

\def\twodigitMPoutput
  {\let\!MP        \twodigitrounding
   \def\!MPgMPs##1{\twodigitrounding{\gMPs##1}}%
   \def\!MPgMPa##1{\twodigitrounding{\gMPa##1}}}

\let\!MP    \empty
\let\!MPgMPa\gMPa
\let\!MPgMPs\gMPs

%D Here comes the special-specific code:

\def\setMPextensions
  {\ifconditional\manyMPspecials
     \def\MPrgbnumber##1{\expandafter\doMPrgbnumber##10000.00000\relax}%
     \def\doMPrgbnumber##1.##2##3##4##5##6\relax{##2##3##4##5}%
   \else
     \def\MPrgbnumber##1{\expandafter\doMPrgbnumber##1000.0000\relax}%
     \def\doMPrgbnumber##1.##2##3##4##5\relax{##2##3##4}%
  \fi}

% \settrue\manyMPspecials \setMPextensions

%D This macro handles the special definitions that are
%D passed as comment.

%D The implementation below saves the data on the stack in
%D a way similar to the macros in \type {supp-pdf.tex}, and
%D just overload a few already defined handlers. That way,
%D the existing macros are still generic. \footnote {Actually,
%D the macros here are just as generic.}
%D
%D Currently the only extension concerns shading, which is
%D accomplished by handling yet another value of \type
%D {\finiMPpath}. The recource disctionary is stored and
%D later picked up by the general \CONTEXT\ figure inclusion
%D macros.

%D The \type {%%MetaPostSpecials: version.revision signal} line
%D triggers this module into handling color specifications kind
%D of special. We need this safeguard for non||special
%D usage.

%D When defined inline, we use another macro to handle the
%D definitions. Actually, this macro is called by the
%D previous ones.

\chardef\MPspecialversion  = 0  % specials when >1
\chardef\MPspecialrevision = 0  % specials when >1
\chardef\MPspecialsignal   = 0  % passed on by graphic

\chardef\inlineMPspecials  = 1  % only needed for stack resetting

\def\dohandleMPspecialcomment#1
  {\setMPargument{#1}%
   \advance\scratchcounter \minusone
   \ifcase\scratchcounter
     \handleMPspecialcommand
     \donetrue
     \doresetMPstack
     \let\handleMPsequence\dohandleMPsequence
     \expandafter\handleMPsequence
   \else
     \expandafter\dohandleMPspecialcomment
   \fi}

\def\handleMPspecialcomment #1 % number of arguments
  {\doresetMPstack
   \scratchcounter#1\relax
   \ifcase\scratchcounter % when zero, inline shading is used
     \chardef\inlineMPspecials\plusone
     \let\handleMPsequence\dohandleMPsequence
     \expandafter\handleMPsequence
   \else
     \chardef\inlineMPspecials\zerocount
     \expandafter\dohandleMPspecialcomment
   \fi}

%D When defined inline, we use another macro to handle the
%D definitions. Actually, this macro is called by the
%D previous ones.

\def\handleMPspecialcommand
  {\ifcase\inlineMPspecials\or
     \advance\nofMParguments \minusone % pop the size
   \fi
   \ifundefined\MPspecial % beware, no real \if
     \message{[unknown \MPspecial]}%
   \else
     \csname\MPspecial\endcsname
   \fi
   \ifcase\inlineMPspecials
     \doresetMPstack % 0
   \else
     \resetMPstack   % 1
   \fi}

\def\handleMPspecialscomment #1.#2 #3 % version.revision signal #4=div=1000|10000
  {\doresetMPstack
   \chardef\MPspecialversion #1%
   \chardef\MPspecialrevision#2%
   \chardef\MPspecialsignal  #3%
   \let\handleMPsequence\dohandleMPsequence
   \ifnum#1=\plusone
     \expandafter\handleMPsequence
   \else
     \expandafter\handleMPspecialscommentx
   \fi}

\def\handleMPspecialscommentx #1 % version 2
  {\ifnum10000=0#1\relax
     \settrue \manyMPspecials
   \else
     \setfalse\manyMPspecials
   \fi
   \setMPextensions
   \handleMPsequence}

\def\handleMPrgbcolor
  {\edef\lastMPrvalue{\csname\@@MP01\endcsname}%{\gMPs1}%
   \edef\lastMPgvalue{\csname\@@MP02\endcsname}%{\gMPs2}%
   \edef\lastMPbvalue{\csname\@@MP03\endcsname}%{\gMPs3}%
   \ifnum\MPrgbnumber\lastMPrvalue=123\relax
     \csname\@@MPSK\number\MPrgbnumber\lastMPbvalue\endcsname
   \else
     \dohandleMPrgb\lastMPrvalue\lastMPgvalue\lastMPbvalue
   \fi}

\def\handleMPgraycolor{\dohandleMPgray{\gMPs1}}
\def\handleMPcmykcolor{\dohandleMPcmyk{\gMPs1}{\gMPs2}{\gMPs3}{\gMPs4}}
\def\handleMPspotcolor{\dohandleMPspot{\gMPs1}{\gMPs2}{\gMPs3}{\gMPs4}}

% \newcontitional\ignoreMPpath

\def\finishMPpath
  {\ifconditional\ignoreMPpath
     \PDFcode{W n\space}%
   \else
     \PDFcode{\ifcase\finiMPpath W n\or S\or f\or B\else W n\fi}%
   \fi
   \ifx\extraMPpathcode\empty\else
     \PDFcode{\extraMPpathcode}%
     \let\extraMPpathcode\empty
   \fi
   \setfalse\ignoreMPpath}

\def\processMPpath
  {\checkMPpath % !
   \flushMPpath
   \closeMPpath
   \finishMPpath
   \let\handleMPsequence\dohandleMPsequence
   \resetMPstack
   \nofMPsegments\zerocount
   \handleMPsequence}

\protect \endinput

% When i'm bored ...

% \newcatcodetable\mpscatcodes

% \startcatcodetable \mpscatcodes
%     \catcode`\| \@@comment
%     \catcode`\% \@@active
%     \catcode`\[ \@@active
%     \catcode`\] \@@active
%     \catcode`\{ \@@active
%     \catcode`\} \@@active
% \stopcatcodetable

% \def\keepMPspecials
%   {\setcatcodecommand \mpscatcodes `\% \letterpercent
%    \setcatcodecommand \mpscatcodes `\[ \letterleftbracket
%    \setcatcodecommand \mpscatcodes `\] \letterrightbracket
%    \setcatcodecommand \mpscatcodes `\{ \letterleftbrace
%    \setcatcodecommand \mpscatcodes `\} \letterrightbrace}

% \def\ignoreMPspecials
%   {\setcatcodecommand \mpscatcodes `\% \letterpercent
%    \setcatcodecommand \mpscatcodes `\[ \empty
%    \setcatcodecommand \mpscatcodes `\] \empty
%    \setcatcodecommand \mpscatcodes `\{ \empty
%    \setcatcodecommand \mpscatcodes `\} \empty}

% \def\obeyMPspecials
%   {\setcatcodecommand \mpscatcodes `\% \letterpercent
%    \setcatcodecommand \mpscatcodes `\[ \letterleftbracket
%    \setcatcodecommand \mpscatcodes `\] \letterrightbracket
%    \setcatcodecommand \mpscatcodes `\{ \letterleftbrace
%    \setcatcodecommand \mpscatcodes `\} \letterrightbrace}

% \gdef\setMPspecials|
%   {\setcatcodetable\mpscatcodes
%    \lccode`\-=\zerocount % to be sure, it could be a letter
%    \lccode`\%=`\%%       % otherwise it's seen as a number
%    \def\({\char40\relax     }%
%    \def\){\char41\relax     }%
%    \def\\{\char92\relax     }%
%    \def\0{\octalMPcharacter0}%
%    \def\1{\octalMPcharacter1}%
%    \def\2{\octalMPcharacter2}%
%    \def\3{\octalMPcharacter3}%
%    \def\4{\octalMPcharacter4}%
%    \def\5{\octalMPcharacter5}%
%    \def\6{\octalMPcharacter6}%
%    \def\7{\octalMPcharacter7}%
%    \def\8{\octalMPcharacter8}%
%    \def\9{\octalMPcharacter9}}