summaryrefslogtreecommitdiff
path: root/tex/context/base/supp-pdf.tex
blob: caf9d72e441b659167cbd76161b2a02f3e9bd512 (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
%D \module
%D   [       file=supp-pdf,
%D        version=1997.05.21,
%D          title=\CONTEXT\ Support Macros,
%D       subtitle=\METAPOST\ to \PDF\ conversion,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. Non||commercial use is 
%C granted. 

%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 of Sebastian 
%D Ratz I was able to complete this module within reasonable
%D time. First we take care of non||\CONTEXT\ use: 

\ifx \undefined \writestatus \input supp-mis.tex \relax \fi

%D This module handles some \PDF\ conversion and insertions
%D topics. The macros use the \PDFTEX\ primitive
%D \type{\pdfliteral}.

\writestatus{loading}{Context Support Macros / PDF}

\unprotect

\ifx\pdfliteral\undefined
  \def\pdfliteral#1{\message{[ignored pdfliteral: #1]}}
\fi

%D \macros
%D   {convertPDFtoPDF}
%D   {}
%D 
%D \PDFTEX\ supports verbatim inclusion of \PDF\ code. The
%D following macro takes care of inserting externally defined
%D illustrations in \PDF\ format. According to a suggestion
%D Tanmoy Bhattacharya posted to the \PDFTEX\ mailing list, we
%D first skip lines until \type{stream} is reached and then
%D copy lines until \type{endstream} is encountered. This
%D scheme only works with vectorized graphics in which no
%D indirect references to objects are used. Bitmaps also don't
%D work. Interpreting their specifications is beyond the
%D current implementation. 
%D 
%D \starttypen
%D \convertPDFtoPDF 
%D   {filename} 
%D   {x scale} {y scale} 
%D   {x offset } {y offset} 
%D   {width} {height} 
%D \stoptypen
%D 
%D When the scales are set to~1, the last last four values 
%D are the same as the bounding box, e.g.  
%D
%D \starttypen
%D \convertPDFtoPDF{mp-pra-1.pdf} {1} {1}{-1bp}{-1bp}{398bp}{398bp}
%D \convertPDFtoPDF{mp-pra-1.pdf}{.5}{.5} {0bp} {0bp}{199bp}{199bp}
%D \stoptypen
%D
%D Keep in mind, that this kind of copying only works for 
%D pure and valid pdf code (without fonts). 

%D The scanning and copying is straightforward and quite fast.
%D To speed up things we use two constants. 

\def\@@PDFstream@@    {stream}
\def\@@PDFendstream@@ {endstream}

%D \macros
%D   {PDFmediaboxprefered}
%D   {}
%D  
%D If needed, the macros can scan for the mediabox that 
%D specifies the dimensions and offsets of the graphic. When
%D we say: 
%D
%D \starttypen
%D \PDFmediaboxpreferedtrue
%D \stoptypen
%D
%D the mediabox present in the file superseded the user 
%D specified, already scaled and calculated offset and 
%D dimensions. Beware: the user supplied values are not the 
%D bounding box ones! 

\newif\ifPDFmediaboxprefered

\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=#2bp\dimen2=-\dimen2
   \dimen4=#3bp\dimen4=-\dimen4
   \dimen6=#4bp\advance\dimen6 by \dimen2
   \dimen8=#5bp\advance\dimen8 by \dimen4
   \setPDFboundingbox{\dimen2}{\dimen4}{\dimen6}{\dimen8}\PDFxscale\PDFyscale}

\def\checkPDFmediabox#1/MediaBox#2#3\done%
  {\ifx#2\relax \else
     \message{mediabox}%
     \setPDFmediabox#2#3\done
   \fi}   

%D We use the general macro \type{\doprocessfile} and feed this 
%D with a line handling macro that changed it's behavior when 
%D the stream operators are encountered. 

\def\handlePDFline%
  {\ifx\@@PDFstream@@\fileline
     \let\doprocessPDFline=\copyPDFobject
     \startPDFtoPDF
   \else\ifPDFmediaboxprefered
     \expandafter\checkPDFmediabox\fileline/MediaBox\relax\done
   \fi\fi}

\def\copyPDFobject%
  {\ifx\@@PDFendstream@@\fileline
     \ifPDFmediaboxprefered
       \let\doprocessPDFline=\findPDFmediabox
     \else
       \let\doprocessPDFline=\relax
     \fi
   \else
     \advance\scratchcounter by 1
     \pdfliteral{\fileline}%
   \fi}

\def\findPDFmediabox%
  {\expandafter\checkPDFmediabox\fileline/MediaBox\relax\done}

%D The main conversion macro wraps the \PDF\ codes in a box 
%D that is output as an object. The graphics are embedded 
%D in~\type{q} and~\type{Q} and are scaled and positioned using 
%D one transform call (\type{cm}). This saves some additional 
%D scaling.   

\def\startPDFtoPDF%
  {\setbox0=\vbox\bgroup
     \message{[PDF to PDF \PDFfilename}%
     \forgetall
     \scratchcounter=0    
     \let\stopPDFtoPDF=\dostopPDFtoPDF}

\def\dostopPDFtoPDF%
  {\ifnum\scratchcounter<0 \scratchcounter=1 \fi
   \message{(\the\scratchcounter\space lines)]}%
   \egroup
   \wd0=\PDFwidth
   \vbox to \PDFheight
     {\forgetall
      \vfill
      \pdfliteral{q}%
      \pdfliteral{1 0 0 1 \PDFxoffset\space \PDFyoffset\space cm}%
      \pdfliteral{\PDFxscale\space 0 0 \PDFyscale\space 0 0 cm}%
      \box0
      \pdfliteral{Q}}}
 
\def\stopPDFtoPDF%
   {\message{[PDF to PDF \PDFfilename\space not found]}}

\def\convertPDFtoPDF#1#2#3#4#5#6#7% 
  {\bgroup
   \def\PDFfilename{#1}%
   \def\PDFxscale  {#2}%
   \def\PDFyscale  {#3}%
   \setPDFboundingbox{#4}{#5}{#6}{#7}{1}{1}%
   \uncatcodespecials
   \endlinechar=-1
   \let\doprocessPDFline=\handlePDFline
   \doprocessfile\scratchread\PDFfilename\doprocessPDFline
   \stopPDFtoPDF
   \egroup}

%D \macros
%D   {convertMPtoPDF}
%D   {}
%D
%D The next set of macros implements \METAPOST\ to \PDF\
%D conversion. Because we want to test as fast as possible, we
%D first define the \POSTSCRIPT\ operators that \METAPOST\
%D uses. We don't define irrelevant ones, because these are
%D skipped anyway. 

\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 \PSBoundingBox      {BoundingBox:}
\def \PSHiResBoundingBox {HiResBoundingBox:}
\def \PSExactBoundingBox {ExactBoundingBox:}
\def \PSPage             {Page:}

%D By the way, the \type {setcmykcolor} operator is not 
%D output by \METAPOST\ but can result from converting the 
%D \kap{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, as well as that 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 \starttypen 
%D \setMPargument {value}
%D \stoptypen 
%D 
%D They can be retrieved by the short named macros:  
%D 
%D \starttypen 
%D \gMPa {number}
%D \sMPa {number}
%D \stoptypen 
%D
%D When scanning a path specification, we also save the 
%D operator, using 
%D 
%D \starttypen 
%D \setMPkeyword {n}
%D \stoptypen 
%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 \starttypen 
%D \getMPkeyword{n}
%D \stoptypen 
%D 
%D When setting an argument, the exact position on the stack
%D depend 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 \starttypen 
%D \resetMPstack
%D \stoptypen 
%D 
%D We use the prefix \type{@@MP} to keep the stack from
%D conflicting with existing macros. To speed up things bit
%D more, we use the constant \type{\@@MP}. 

\def\@@MP{@@MP}

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

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

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

\def\setMPkeyword#1
  {\expandafter\def\csname\@@MP\the\nofMPsegments0\endcsname{#1}%
   \advance\nofMPsegments by 1
   \nofMParguments=0\relax}

\def\getMPkeyword#1%
  {\csname\@@MP#10\endcsname}
 
%D When we reset the stack, we can assume that all further 
%D comment is to be ignored as well as handled in strings. 
%D By redefining the reset macro after the first call, we 
%D save some run time. 

\def\resetMPstack%
  {\catcode`\%=\@@active
   \let\handleMPgraphic=\handleMPendgraphic
   \def\resetMPstack{\nofMParguments=0\relax}%
   \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 character. W ehad 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}

\bgroup
\catcode`\|=\@@comment
\catcode`\%=\@@active
\catcode`\[=\@@active
\catcode`\]=\@@active
\catcode`\{=\@@active
\catcode`\}=\@@active
\catcode`B=\@@begingroup
\catcode`E=\@@endgroup
\gdef\ignoreMPspecials|
  B\def%BE|
   \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\catcode`\%=\@@active
   \catcode`\[=\@@active
   \catcode`\]=\@@active
   \catcode`\{=\@@active
   \catcode`\}=\@@active
   \catcode`\$=\@@letter
   \catcode`\_=\@@letter
   \catcode`\#=\@@letter
   \catcode`\^=\@@letter
   \catcode`\&=\@@letter
   \catcode`\|=\@@letter
   \catcode`\~=\@@letter
   \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: 

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

%D In earlier versions we used the sequence 
%D
%D \starttypen
%D \expandafter\handleMPsequence\input filename\relax
%D \stoptypen
%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%
  {\catcode`\%=\@@ignore
   \ignoreMPspecials
   \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#2 %
  {\ifnum\lccode`#1=0
     \setMPargument{#1#2}%
   \else
     \edef\somestring{#1#2}%
     \ifx\somestring\PSmoveto
       \edef\lastMPmoveX{\gMPa1}%
       \edef\lastMPmoveY{\gMPa2}%
       \pdfliteral{\gMPa1 \gMPa2 m}%
       \resetMPstack
     \else\ifx\somestring\PSnewpath
       \let\handleMPsequence=\handleMPpath
     \else\ifx\somestring\PSgsave
       \pdfliteral{q}%
       \resetMPstack
     \else\ifx\somestring\PSgrestore
       \pdfliteral{Q}%
       \resetMPstack
     \else\ifx\somestring\PSdtransform  % == setlinewidth
       \let\handleMPsequence=\handleMPdtransform 
     \else\ifx\somestring\PSconcat
       \pdfliteral{\gMPa1 \gMPa2 \gMPa3 \gMPa4 \gMPa5 \gMPa6 cm}%
       \resetMPstack
     \else\ifx\somestring\PSsetrgbcolor
       \pdfliteral{\gMPa1 \gMPa2 \gMPa3 rg \gMPa1 \gMPa2 \gMPa3 RG}%
       \resetMPstack
     \else\ifx\somestring\PSsetcmykcolor
       \pdfliteral{\gMPa1 \gMPa2 \gMPa3 \gMPa4 k \gMPa1 \gMPa2 \gMPa3 \gMPa4 K}%
       \resetMPstack
     \else\ifx\somestring\PSsetgray
       \pdfliteral{\gMPa1 g \gMPa1 G}%
       \resetMPstack
     \else\ifx\somestring\PStranslate
       \pdfliteral{1 0 0 1 \gMPa1 \gMPa2 cm}%
       \resetMPstack
     \else\ifx\somestring\PSsetdash
       \handleMPsetdash
       \resetMPstack
     \else\ifx\somestring\PSsetlinejoin
       \pdfliteral{\gMPa1 j}%
       \resetMPstack
     \else\ifx\somestring\PSsetmiterlimit
       \pdfliteral{\gMPa1 M}%
       \resetMPstack
     \else\ifx\somestring\PSfshow
       \handleMPfshow
       \resetMPstack
     \else\ifx\somestring\PSsetlinecap
       \pdfliteral{\gMPa1 J}%
       \resetMPstack
     \else\ifx\somestring\PSrlineto
       \pdfliteral{\lastMPmoveX\space \lastMPmoveY\space l S}%
       \resetMPstack
     \else\ifx\somestring\PSscale
       \pdfliteral{\gMPa1 0 0 \gMPa2 0 0 cm}%
       \resetMPstack
     \else
       \handleMPgraphic{#1#2}%
     \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
   \fi
   \handleMPsequence}

%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
     \setMPargument{#1}%
   \fi}

\def\handleMPbegingraphic#1%
  {\ifx\somestring\PSBoundingBox
     \let\handleMPsequence=\handleMPboundingbox
   \else\ifx\somestring\PSHiResBoundingBox
     \let\handleMPsequence=\handleMPboundingbox
   \else\ifx\somestring\PSExactBoundingBox
     \let\handleMPsequence=\handleMPboundingbox
   \else\ifx\somestring\PSPage
     \let\handleMPsequence=\handleMPpage
   \else
     \setMPargument{#1}%
   \fi\fi\fi\fi}

\let\handleMPgraphic=\handleMPbegingraphic

%D We check for three kind of bounding boxes: the normal one
%D and two high precission ones: 
%D 
%D \starttypen
%D BoundingBox: llx lly ucx ucy 
%D HiResBoundingBox: llx lly ucx ucy 
%D ExactBoundingBox: llx lly ucx ucy 
%D \stoptypen
%D 
%D The dimensions are saved for later use. 

\def\handleMPboundingbox #1 #2 #3 #4
  {\dimen0=#1pt\dimen0=-\MPxscale\dimen0
   \dimen2=#2pt\dimen2=-\MPyscale\dimen2
   \xdef\MPxoffset{\withoutpt{\the\dimen0}}%
   \xdef\MPyoffset{\withoutpt{\the\dimen2}}%
   \dimen0=#1bp\dimen0=-\dimen0
   \dimen2=#2bp\dimen2=-\dimen2
   \advance\dimen0 by #3bp
   \dimen0=\MPxscale\dimen0
   \xdef\MPwidth{\the\dimen0}%
   \advance\dimen2 by #4bp
   \dimen2=\MPyscale\dimen2
   \xdef\MPheight{\the\dimen2}%
   \nofMParguments=0
   \let\handleMPsequence=\dohandleMPsequence
   \handleMPsequence}

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

\def\handleMPpage #1 #2 
  {\nofMParguments=0
   \let\handleMPsequence=\dohandleMPsequence
   \handleMPsequence}

%D \METAPOST\ draws it 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. 

\def\lastMPmoveX{0}
\def\lastMPmoveY{0}

%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 finaly I saw the light. It proved that we also had to 
%D take care of \type{(split arguments)}.

\def\handleMPfshow%
  {\setbox0=\hbox
     {\obeyMPspecials
      \edef\size{\gMPa{\the\nofMParguments} }%
      \advance\nofMParguments by -1
      \font\temp=\gMPa{\the\nofMParguments} at \size bp
      \advance\nofMParguments by -1
      \temp
      \ifnum\nofMParguments=1
        \def\do(##1){##1}%
        \gMPa1%
      \else
        \scratchcounter=1
        \def\do(##1{##1}%
        \gMPa{\the\scratchcounter}\space
        \def\do{}%
        \loop
          \advance\scratchcounter by 1
          \ifnum\scratchcounter<\nofMParguments
            \gMPa{\the\scratchcounter}\space 
        \repeat
        \def\do##1){##1}%
        \gMPa{\the\scratchcounter}%
      \fi
      \unskip}%
   \dimen0=\lastMPmoveY bp
   \advance\dimen0 by \ht0
   \ScaledPointsToBigPoints{\number\dimen0}\lastMPmoveY
   \pdfliteral{n q 1 0 0 1 \lastMPmoveX\space\lastMPmoveY\space cm}%
   \dimen0=\ht0
   \advance\dimen0 by \dp0
   \box0
   \vskip-\dimen0
   \pdfliteral{Q}}

%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 simple 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
   \def\somestring{[}%
   \scratchcounter=1 
   \loop
   \ifnum\scratchcounter<\nofMParguments
     \edef\somestring{\somestring\space\gMPa{\the\scratchcounter}}%
     \advance\scratchcounter by 1
   \repeat
   \edef\somestring{\somestring]\gMPa{\the\scratchcounter} d}%
   \pdfliteral{\somestring}%
   \egroup}

%D The \type{setlinewidth} commands look a bit complicated. There are 
%D two alternatives, that alsways look the same. As John Hobby 
%D says: 
%D 
%D \startsmaller
%D \starttypen 
%D x 0 dtransform exch truncate exch idtransform pop setlinewidth
%D 0 y dtransform truncate idtransform setlinewidth pop
%D \stoptypen
%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 \stopsmaller
%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\gMPa1pt>\!!zeropoint
     \pdfliteral{\gMPa1 w}%
     \def\next##1 ##2 ##3 ##4 ##5 ##6 {\handleMPsequence}%
   \else
     \pdfliteral{\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{stoke}. 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 a defined in a bit ambiguous way.
%D The only save route for non||circular penshapes, is saving
%D teh 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 \starttypen
%D draw p;  fill p;  filldraw p;
%D \stoptypen
%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.\voetnoot{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=0
   \let\closeMPpath=\relax
   \let\flushMPpath=\flushnormalMPpath
   \resetMPstack
   \nofMPsegments=1
   \let\handleMPsequence=\dohandleMPpath
   \dohandleMPpath}

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

\def\flushnormalMPpath%
  {\scratchcounter=\nofMPsegments
   \nofMPsegments=1
   \loop
     \expandafter\ifcase\getMPkeyword{\the\nofMPsegments}\relax
       \pdfliteral{\gMPs1 \gMPs2 l}%
     \or
       \pdfliteral{\gMPs1 \gMPs2 \gMPs3 \gMPs4 \gMPs5 \gMPs6 c}%
     \or
       \pdfliteral{\lastMPmoveX\space \lastMPmoveY\space l S}%
     \or
       \edef\lastMPmoveX{\gMPs1}%
       \edef\lastMPmoveY{\gMPs2}%
       \pdfliteral{\lastMPmoveX\space \lastMPmoveY\space m}%
     \fi
     \advance\nofMPsegments by 1\relax
   \ifnum\nofMPsegments<\scratchcounter
   \repeat}

\def\flushconcatMPpath%
  {\scratchcounter=\nofMPsegments
   \nofMPsegments=1   
   \loop
     \expandafter\ifcase\getMPkeyword{\the\nofMPsegments}\relax
       \doMPconcat{\gMPs1}\a{\gMPs2}\b
       \pdfliteral{\a\space \b\space l}%
     \or
       \doMPconcat{\gMPs1}\a{\gMPs2}\b
       \doMPconcat{\gMPs3}\c{\gMPs4}\d
       \doMPconcat{\gMPs5}\e{\gMPs6}\f
       \pdfliteral{\a\space \b\space \c\space \d\space \e\space \f\space c}%
     \or
       \bgroup
       \noMPtranslate
       \doMPconcat\lastMPmoveX\a\lastMPmoveY\b
       \pdfliteral{\a\space \b\space l S}%
       \egroup
     \or
       \edef\lastMPmoveX{\gMPs1}%
       \edef\lastMPmoveY{\gMPs2}%
       \doMPconcat\lastMPmoveX\a\lastMPmoveY\b
       \pdfliteral{\a\space \b\space m}%
     \fi
     \advance\nofMPsegments by 1\relax
   \ifnum\nofMPsegments<\scratchcounter
   \repeat}

%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 \starttypen
%D \doMPconcat {x position} \xresult {y position} \yresult
%D \stoptypen
%D 
%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 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. 

\def\MPconcatfactor{256}

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

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

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

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

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

%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 calculation as well as scaling and translating. The
%D \METAPOST\ to \PDF\ conversion however only needs
%D transformation. 

%D \start \switchnaarkorps [ss]
%D
%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 \plaatsformule
%D   \startformule
%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   \stopformule
%D 
%D or
%D 
%D \plaatsformule
%D   \startformule
%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   \stopformule
%D 
%D both of which is a shorthand for the same set of equations:
%D 
%D \plaatsformule
%D   \startformule
%D      D_x = s_x U_x + r_y U_y + t_x
%D   \stopformule
%D 
%D \plaatsformule
%D   \startformule
%D      D_y = r_x U_x + s_y U_y + t_y
%D   \stopformule
%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 \startsmaller
%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 \stopsmaller
%D 
%D Well, what is the net effect? In matrix notation, it is
%D 
%D \plaatsformule
%D   \startformule
%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   \stopformule
%D 
%D \plaatsformule
%D   \startformule
%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   \stopformule
%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$\hoog{th}
%D horizontal row and $j$\hoog{th} vertical column is
%D calculated by`multiplying' the $i$\hoog{th} row of the first
%D matrix and the $j$\hoog{th} column of the second matrix (and
%D summing over the elements). Thus, in the above: 
%D 
%D \plaatsformule
%D   \startformule
%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   \stopformule
%D 
%D with
%D 
%D \plaatsformule
%D   \startformule
%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   \stopformule

%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 \plaatsformule
%D   \startformule
%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   \stopformule
%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: it is 
%D 
%D \plaatsformule
%D   \startformule
%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   \stopformule
%D 
%D where, the inverse transformation matrix is given by
%D 
%D \plaatsformule
%D   \startformule
%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   \stopformule  
%D 
%D And you can see that when expanded out, this does 
%D give the formulas:  
%D 
%D \plaatsformule
%D   \startformule 
%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   \stopformule
%D 
%D \plaatsformule
%D   \startformule  
%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   \stopformule
%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. 
%D 
%D \stop
%D 
%D The path is output using the values saved on the stack. If 
%D needed, all coordinates are recalculated. 

\def\processMPpath%
  {\flushMPpath
   \closeMPpath
   \pdfliteral{\ifcase\finiMPpath W n\or S\or f\or B\fi}%
   \let\handleMPsequence=\dohandleMPsequence
   \resetMPstack
   \nofMPsegments=0
   \handleMPsequence}

%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
   \pdfliteral{\gMPs1 \gMPs2 \gMPs3 \gMPs4 \gMPs5 \gMPs6 cm}%
   \resetMPstack}

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

\def\dohandleMPpath#1#2 %
  {\ifnum\lccode`#1=0
     \setMPargument{#1#2}%
   \else
     \def\somestring{#1#2}%
     \ifx\somestring\PSlineto
       \setMPkeyword0
     \else\ifx\somestring\PScurveto
       \setMPkeyword1
     \else\ifx\somestring\PSrlineto
       \setMPkeyword2
     \else\ifx\somestring\PSmoveto
       \setMPkeyword3
     \else\ifx\somestring\PSclip
       \let\handleMPsequence=\processMPpath
     \else\ifx\somestring\PSgsave
       \chardef\finiMPpath=3 
     \else\ifx\somestring\PSgrestore
     \else\ifx\somestring\PSfill
       \ifnum\finiMPpath=0 
         \chardef\finiMPpath=2 
         \let\handleMPsequence=\processMPpath
       \fi
     \else\ifx\somestring\PSstroke
       \ifnum\finiMPpath=0 
         \chardef\finiMPpath=1 
       \fi
       \let\handleMPsequence=\processMPpath
     \else\ifx\somestring\PSclosepath
       \def\closeMPpath{\pdfliteral{h}}%
     \else\ifx\somestring\PSconcat
       \let\flushMPpath=\flushconcatMPpath
       \handleMPpathconcat
     \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
   \fi
   \handleMPsequence}

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

\def\convertMPtoPDF#1#2#3% 
  {\bgroup
   \message{[MP to PDF #1]}%
   \setMPspecials
   \startMPscanning
   \def\do{}%
   \edef\MPxscale{#2}%
   \edef\MPyscale{#3}%
   \setbox0=\vbox\bgroup
     \forgetall 
     \offinterlineskip
     \pdfliteral{q}%
     \let\handleMPsequence=\dohandleMPsequence
     \catcode`\^^M=\@@endofline
     \input #1\relax}

\def\finishMPgraphic%
  {\pdfliteral{Q}%
   \egroup
   \wd0=\MPwidth
   \vbox to \MPheight
     {\forgetall
      \vfill
      \pdfliteral{q \MPxscale\space 0 0 \MPyscale\space 
        \MPxoffset\space \MPyoffset\space cm}%
      \box0
      \pdfliteral{Q}}%
   \egroup}

%D This kind of conversion is possible because \METAPOST\ 
%D does all the calculations. Converting other \POSTSCRIPT\ 
%D files would drive both me and \TEX\ crazy. 

\protect 

\endinput