summaryrefslogtreecommitdiff
path: root/tex/context/base/mkii/spec-tpd.mkii
blob: 042d5cdfe36203d9bd28145d16f99b1905b1396d (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
%D \module
%D   [       file=spec-tpd,
%D        version=1996.01.25,
%D          title=\CONTEXT\ Special Macros,
%D       subtitle=\PDFTEX,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% once we can be sure that the latest versions of pdftex are
% available we can use:
%
% \pdfobj reserveobjnum \edef\one{\the\pdflastobj}
% \pdfobj reserveobjnum \edef\two{\the\pdflastobj}
%
% \pdfobj useobjnum \one {x}
% \pdfobj useobjnum \two {x}
%
% we then can rewrite part of spec-fdf because the other drivers
% already support symbolic references

% layers and links don't work while the spec says they should

% method = auto
%
% \definefileinsertion{tpd}{auto}%
%   {\executesystemcommand{t:/ruby/pstopdf.rb #2}%
%    \dofileinsertion{tpd}{pdf}}

% \let\PDFdestvoffset\zeropoint
% \newbox\pdfdestbox
%
% \def\doPDFdestination#1%
%   {\ifdim\PDFdestvoffset=\zeropoint\relax
%      \pdfdest name {#1}\PDFpageviewkey
%    \else
%      \setbox\pdfdestbox\hbox to \zeropoint
%        {\raise\PDFdestvoffset\hbox{\pdfdest name {#1}\PDFpageviewkey}}%
%      \ht\pdfdestbox\zeropoint
%      \box\pdfdestbox
%    \fi}
%
% \def\PDFdestvoffset{2\lineheight} % {1.5\strutht}

%D \macros
%D   {jobsuffix}
%D
%D Being one of the first typographical systems able to support
%D advances \PDF\ support, \TEX\ is also one of the first
%D systems to produce high quality \PDF\ code directly. Thanks
%D to Han The Thanh c.s. the \TEX\ community can leap forward
%D once again.
%D
%D One important characteristic of \PDFTEX\ is that is can
%D produce standard \DVI\ code as well as \PDF\ code. This
%D enables us to use one format file to support both output
%D formats.

%D All modules in this group use specials to tell drivers what
%D non||\TEX\ actions to take. Because from the \TEX\ point of
%D view, there is no difference between \DVI\ and \PDF, we
%D therefore only have to bend the \DVI\ driver support into
%D \PDF\ support. Technically spoken, specials no longer serve
%D a purpose, except from ending up as comment in the \PDF\
%D file.
%D
%D Before we continue we need to make sure if indeed those
%D \PDFTEX\ primitives are permitted. If no primitives are
%D available, we just stop reading any further.

\unprotect

\ifx\pdftexversion\undefined
  \writestatus\m!systems{you should use pdfTeX binaries}\wait
  \protect\expandafter\endinput
\fi

\ifnum\pdftexversion<13
  \writestatus\m!systems{your pdfTeX version is much too old}\wait
  \protect\expandafter\endinput
\fi

\ifnum\pdftexversion<14
  \writestatus\m!systems{please update your pdfTeX binaries}
\fi

\ifnum\pdftexversion>19
  \pdfhorigin=1 true in
  \pdfvorigin=1 true in
\fi

%D We default to 300 dots per inch image resolution and 600 dpi
%D bitmap fonts (when asked for).

\ifx\pdfimageresolution\undefined \newcount\pdfimageresolution \fi
\ifx\pdfpkresolution   \undefined \newcount\pdfpkresolution    \fi

\pdfimageresolution=300
\pdfpkresolution   =600

%D Another downward compatible hack:

\ifx\pdflastximagepages\undefined
  \newcount\pdflastximagepages \pdflastximagepages=1
\fi

%D And:

\ifx\pdfpageresources\undefined
  \newtoks\pdfpageresources
\fi

%D In order to get high quality \METAPOST\ inclusion, we set
%D the number of digits to~5 (prevents rounding errors).

\ifx\pdfdecimaldigits\undefined
  \newcount\pdfdecimaldigits
\fi

\pdfdecimaldigits=10

\ifx\pdfinclusionerrorlevel\undefined
  \ifx\pdfoptionpdfinclusionerrorlevel\undefined
    \newcount\pdfinclusionerrorlevel
  \else
    \let\pdfinclusionerrorlevel\pdfoptionpdfinclusionerrorlevel
  \fi
\fi

\pdfinclusionerrorlevel=0

\ifx\pdfminorversion\undefined
  \ifx\pdfoptionpdfminorversion\undefined
    \newcount\pdfminorversion
  \else
    \let\pdfminorversion\pdfoptionpdfminorversion
  \fi
\fi

\pdfminorversion=5

\def\PDFversion{1.\number\pdfminorversion}

% %D Why are the Acrobat viewers so buggy? To prevent font cache
% %D mismatches, we say:
%
% \ifx\pdfuniqueresname\undefined \else
%   \pdfuniqueresname=1
% \fi

%D Once we are sure that we're indeed supporting \PDFTEX, we
%D force \PDF\ output at the highest compression. For debugging
%D purposes one can set the compresslevel to~0. We also have to
%D make sure no other specials are supported, else \PDFTEX\
%D will keep on telling us that we're wrong. We also load the
%D general \PDF\ macros that are shared between this driver and
%D the \ACROBAT\ one.

\startspecials[tpd][reset,fdf]

%D This means that by saying
%D
%D \starttyping
%D \usespecials[tpd]
%D \stoptyping
%D
%D we get ourselves full \PDF\ output.

%D For some internal testing we need to know the output
%D suffix.

\setjobsuffix{pdf}

%D We don't use specials here, which means that we must flush
%D settings before the page is shipped out.

\specialbasedsettingsfalse

%D Some more internal settings.

\appendtoksonce
  \pdfoutput\zerocount
\to \everyresetspecials

\pdfoutput=1 % we reset that one with \everyresetspecials

%D Just in case we mimmick specials, we have to make sure no
%D default specials end up in the process.

\let\defaultspecial=\gobbleoneargument

\appendtoksonce
  \let\defaultspecial\normalspecial
\to \everyresetspecials

\let\PDFcode        \pdfliteral
\def\PDFcontentcode{\pdfliteral}
\def\PDFdirectcode {\pdfliteral direct}

%D \macros
%D   {everyPDFximage}
%D
%D This token register is flushed before an ximage is loaded.

\ifdefined\everyPDFximage \else \newtoks\everyPDFximage \fi
\ifdefined\everyPDFxform  \else \newtoks\everyPDFxform  \fi

%D \macros
%D   {dosetuppaper}
%D
%D If we don't set the paper size, \PDFTEX\ will certainly do
%D it in a way we don't want, therefore we need:

\definespecial\dosetuppaper#1#2#3%
  {\global\pdfpagewidth #2\relax
   \global\pdfpageheight#3\relax}

%D \macros
%D   {doloadmapfile,doloadmapline,doresetmapfilelist}

\ifx\pdfmapfile\undefined \let\pdfmapfile\gobbletwoarguments \fi
\ifx\pdfmapline\undefined \let\pdfmapline\gobbletwoarguments \fi

\definespecial\doresetmapfilelist
  {\global\let\doresetmapfilelist\relax
   \pdfmapfile{original-empty.map}}

\ifnum\pdftexversion<120

  \definespecial\doloadmapfile #1#2{\pdfmapfile{+#2}}
  \definespecial\doloadmapline #1#2{}

\else

  \definespecial\doloadmapfile #1#2{\pdfmapfile{#1#2}}
  \definespecial\doloadmapline #1#2{\pdfmapline{#1#2}}

\fi

%D nasty but needed

\appendtoksonce \loadallfontmapfiles \to \everyPDFximage
\appendtoksonce \loadallfontmapfiles \to \everyPDFxform

%D \macros
%D   {doinsertfile,dogetnofinsertpages}
%D
%D Graphics are not part of \TEX\ and therefore not part of the
%D \DVI\ standard. \PDF\ on the other hand has several graphic
%D primitives. During the multi||step process \TEX\
%D $\rightarrow$ \DVI\ $\rightarrow$ \POSTSCRIPT\ $\rightarrow$
%D \PDF\ one can insert graphics using specials. In \PDFTEX\
%D however there is only one step! This means that \PDFTEX\
%D itself has to do the inclusion.
%D
%D At the moment \PDFTEX\ supports inclusion of bitmap \PNG\
%D graphics as well as not too complicated \PDF\ code. Using
%D this last option, we are able to include both \METAPOST\ and
%D \PDF\ output produced by \GHOSTSCRIPT.
%D
%D We fall back on the generic \CONTEXT\ module supp-pdf to
%D accomplish \PDF\ inclusion. The methods implemented there
%D are hooked into both the figure placement mechanisms of
%D \CONTEXT\ and the specials inclusion mechanism.

\definespecial\doinsertfile
  {\dofileinsertion{tpd}{\truegraphictype\@@DriverImageType}}

%D The number of pages in (for instance an \PDF) insert
%D file, can be asked for using:

\definespecial\dogetnofinsertpages#1%
  {\xdef\nofinsertpages{1}% global
   \doifvalidpdfimagefileelse{#1}%
     {\pdfximage{#1}\xdef\nofinsertpages{\the\pdflastximagepages}}
     {}}

%D Currently we support \type{pdf} for \PDF\ files, \type{mps}
%D for \METAPOST\ graphics, \type{png} and \type{jpg} for
%D bitmap graphics.

\definefileinsertion{tpd}{mps}
  {\hbox
     {\convertMPtoPDF\@@DriverImageFile{1}{1}%
      \global\let\PDFimagereference\empty}}

\definefileinsertion{tpd}{pdf}{\handlepdfimage}
\definefileinsertion{tpd}{png}{\handlepdfimage}
\definefileinsertion{tpd}{jpg}{\handlepdfimage}
\definefileinsertion{tpd}{jb2}{\handlepdfimage}

% \definefileinsertion{tpd}{jpeg} {\handlepdfimage}
% \definefileinsertion{tpd}{jbig2}{\handlepdfimage}

%D Experimental:

%D The main file insertion macro is as follows. Because
%D \PDFTEX\ does not support arbitrary suffixes, we double
%D check on a user supplied filename, because \PDFTEX\ chokes
%D on unknown suffixes.

\def\doifvalidpdfimagefileelse#1% todo: greedy split
  {\doiffileelse{#1}
     {\edef\filesuffix{#1}%
      \doloop
        {\@EA\aftersplitstring\filesuffix\at.\to\temp
         \ifx\temp\empty
           \exitloop
         \else
           \lowercasestring\temp\to\filesuffix % insertion check also needs lowercase
           % a temporary hack
           \doif\filesuffix{pdf}{\pdfimageresolution72}%
           % because pdfTeX scales back
         \fi}}
      {\let\filesuffix\s!unknown}%
   \doiffileinsertionsupportedelse\filesuffix}

\ifx\pdflastximagepages\undefined \chardef\pdflastximagepages=1 \fi

\def\checkpdfimagepagenumber
  {\edef\pdfimagepagenumber
     {\ifx\@@DriverImagePage\empty\else\ifnum\@@DriverImagePage>\zerocount
        \space page\space\@@DriverImagePage\space
      \fi\fi}}

\ifnum\pdftexversion>13

  \def\handlepdfimage
    {\bgroup
     \the\everyPDFximage
     \doifvalidpdfimagefileelse\@@DriverImageFile
       {\checkpdfimagepagenumber
        \ifx\pdfimagepagenumber\empty\@EA\pdfimmediateximage\else\@EA\pdfximage\fi
          \ifdim\@@DriverImageWidth >\zeropoint \!!width \@@DriverImageWidth \fi
          \ifdim\@@DriverImageHeight>\zeropoint \!!height\@@DriverImageHeight\fi
          \pdfimagepagenumber
          \@@DriverImageBox
          {\@@DriverImageFile}
        \xdef\PDFimagereference{\the\pdflastximage}%
        \xdef\nofinsertpages{\the\pdflastximagepages}%
        \pdfrefximage\pdflastximage}
       {\framed[\c!width=\@@DriverImageWidth,\c!height=\@@DriverImageHeight]{\@@DriverImageFile}}%
     \egroup}

\else

  \def\handlepdfimage
    {\bgroup
     \doifvalidpdfimagefileelse\@@DriverImageFile
       {\pdfimage
          \ifdim\@@DriverImageWidth >\zeropoint \!!width  \@@DriverImageWidth \fi
          \ifdim\@@DriverImageHeight>\zeropoint \!!height \@@DriverImageHeight\fi
          {\@@DriverImageFile}}%
       {\framed[\c!width=\@@DriverImageWidth,\c!height=\@@DriverImageHeight]{\@@DriverImageFile}}%
     \egroup}

\fi

%D As we will see now, \PDFTEX\ not only directly supports
%D \type{mps}, \type{png}, \type{pdf}, \type{jpg} but also
%D \type{mov}. In \CONTEXT\ we support movie inserts in a way
%D similar to figure inclusion. The next macro calls the
%D general \PDF\ one.

\definefileinsertion{tpd}{mov}{\doPDFinsertmov}
\definefileinsertion{tpd}{avi}{\doPDFinsertmov}

%D \macros
%D   {doinsertsoundtrack}
%D
%D We use numbers instead of labels to keep track of sounds.

\definespecial\doinsertsoundtrack{\doPDFinsertsoundtrack}

%D For the moment we don't test for alternatives that
%D themselves have alternatives, especially cylcic
%D dependencies.

% some day we will do a proper check on bitmap depth,
%
% \pdfobj reserveobjnum\relax
% \pdfximage colorspace \pdflastobj {some file}
% ... \pdflastximagecolordepth ...
% \immediate\pdfobj useobjnum \pdflastobj {some spec}

\ifx\@@DriverImageBox\undefined \def\@@DriverImageBox{artbox} \fi

\def\pdfimmediateximage{\immediate\pdfximage}

\ifnum\pdftexversion>13

  \def\checkpdfimageattributes
    {\ifx\PDFfigurereference\empty
       \global\let\pdfimageattributes\empty
     \else
       \immediate\pdfobj
         {[ << /Image \PDFobjref\PDFfigurereference
               /DefaultForPrinting true >> ]}%
       \xdef\pdfimageattributes
         {attr {/Alternates \PDFobjref\pdflastobj}}%
     \fi}

  \global\let\PDFimagecolorreference\empty

  \def\checkpdfimagecolorspecs
    {\ifx\pdflastximagecolordepth \undefined
       \global\let\pdfimagecolorspecs\empty
     \else\ifx\PDFimagecolorreference\empty
       \global\let\pdfimagecolorspecs\empty
     \else
       \xdef\pdfimagecolorspecs{colorspace \PDFimagecolorreference\space}%
     \fi\fi
     \global\let\PDFimagecolorreference\empty}

  \def\dogetTPDfiguresize
    {\doifvalidpdfimagefileelse\@@DriverImageFile
       {\ifvoid\foundexternalfigure
          \donetrue
        \else\ifx\PDFfigurereference\empty
          \donetrue
        \else
          % hm, is this still needed
          \doifinstringelse\filesuffix{\c!png,\c!jpg}\donetrue\donefalse
        \fi\fi}
       {\donefalse}%
     \ifdone
       \the\everyPDFximage
       \checkpdfimageattributes
       \checkpdfimagecolorspecs
       \checkpdfimagepagenumber
       \global\setbox\foundexternalfigure\hbox
         {\ifx\pdfimagepagenumber\empty\@EA\pdfimmediateximage\else\@EA\pdfximage\fi
            \pdfimageattributes
            \pdfimagecolorspecs
            \pdfimagepagenumber
            \@@DriverImageBox
            {\@@DriverImageFile}%
          \xdef\PDFimagereference{\the\pdflastximage}%
          \xdef\nofinsertpages{\the\pdflastximagepages}%
          \pdfrefximage\pdflastximage}%
       \xdef\analyzedfigurewidth {\the\wd\foundexternalfigure}%
       \xdef\analyzedfigureheight{\the\ht\foundexternalfigure}%
       \global\let\PDFfigurereference\empty
       \global\let\PDFimageattributes\empty
     \else
       \global\let\analyzedfigurewidth \!!zeropoint
       \global\let\analyzedfigureheight\!!zeropoint
     \fi}

\else

  \def\dogetTPDfiguresize
    {\doifvalidpdfimagefileelse\@@DriverImageFile
       {\global\setbox\foundexternalfigure\vbox{\pdfimage{\@@DriverImageFile}}%
        \xdef\analyzedfigurewidth {\the\wd\foundexternalfigure}%
        \xdef\analyzedfigureheight{\the\ht\foundexternalfigure}}
       {\global\let\analyzedfigurewidth \!!zeropoint
        \global\let\analyzedfigureheight\!!zeropoint}}

\fi

\definefilechecker{tpd}{pdf}{\dogetTPDfiguresize}
\definefilechecker{tpd}{png}{\dogetTPDfiguresize}
\definefilechecker{tpd}{jpg}{\dogetTPDfiguresize}
\definefilechecker{tpd}{jb2}{\dogetTPDfiguresize}

\definespecial\dogetfiguresize
  {\dofilechecker{tpd}{\truegraphictype\@@DriverImageType}}

%D \macros
%D   {doregisterfigure}
%D
%D Here is the fuzzy, very special dependant figure
%D registration special. We need to refer to the innermost
%D object (ximage).

\ifnum\pdftexversion>13

  \definespecial\doregisterfigure#1#2%
    {\doifundefined{IM::#1::#2}
       {\setxvalue{IM::#1::#2}{\the\pdflastximage}}%
     \xdef\PDFfigurereference{\getvalue{IM::#1::#2}}}

\fi

%D \macros
%D  {doovalbox}
%D
%D Drawing frames with round corners is inherited from the
%D main module.

\definespecial\doovalbox {\doPDFovalbox}

%D \macros
%D   {dostartgraymode,dostopgraymode,
%D    dostartrgbcolormode,dostartcmykcolormode,dostartgraycolormode,
%D    dostopcolormode,
%D    dostartrotation,dostoprotation,
%D    dostartscaling,dostopscaling,
%D    dostartmirroring,dostopmirroring,
%D    dostartnegative,dostopnegative,
%D    dostartoverprint,dostopoverprint}
%D
%D These are implemented in the main \PDF\ module.

\definespecial\dostartgraymode     {\doPDFstartgraymode}
\definespecial\dostopgraymode      {\doPDFstopgraymode}
\definespecial\dostartrgbcolormode {\doPDFstartrgbcolormode}
\definespecial\dostartcmykcolormode{\doPDFstartcmykcolormode}
\definespecial\dostartgraycolormode{\doPDFstartgraycolormode}
\definespecial\dostartspotcolormode{\doPDFstartspotcolormode}
\definespecial\dostopcolormode     {\doPDFstopcolormode}
\definespecial\dostartrotation     {\doPDFstartrotation}
\definespecial\dostoprotation      {\doPDFstoprotation}
\definespecial\dostartscaling      {\doPDFstartscaling}
\definespecial\dostopscaling       {\doPDFstopscaling}
\definespecial\dostartmirroring    {\doPDFstartmirroring}
\definespecial\dostopmirroring     {\doPDFstopmirroring}
\definespecial\dostartnegative     {\doPDFstartnegative}
\definespecial\dostopnegative      {\doPDFstopnegative}
\definespecial\dostartoverprint    {\doPDFstartoverprint}
\definespecial\dostopoverprint     {\doPDFstopoverprint}

\definespecial\doregistercmykspotcolor{\doPDFregistercmykspotcolor}
\definespecial\doregisterrgbspotcolor {\doPDFregisterrgbspotcolor}
\definespecial\doregistergrayspotcolor{\doPDFregistergrayspotcolor}

\definespecial\doregistercmykindexcolor{\doPDFregistercmykindexcolor}
\definespecial\doregisterrgbindexcolor {\doPDFregisterrgbindexcolor}
\definespecial\doregistergrayindexcolor{\doPDFregistergrayindexcolor}

\definespecial\doregisterfigurecolor{\doPDFregisterfigurecolor}

\definespecial\dostartnonecolormode{\doPDFstartnonecolormode}
\definespecial\doregisternonecolor {\doPDFregisternonecolor}

\def\doPDFregisterspotcolorname#1#2% no need for escape in luatex
  {\bgroup
   \let\ascii\empty
   \def\docommand##1%
     {\edef\ascii{\ascii
      \ifx\nexthandledtoken\space
        \letterhash20%
      \else\ifx\nexthandledtoken\blankspace
        \letterhash20%
      \else
        ##1%
      \fi\fi}}%
   \expanded{\handletokens#2}\with\docommand
   \letgvalue{@@pdf@@scn@@#1}\ascii
   \egroup}

\def\doPDFregistersomespotcolor#1#2#3#4#5#6#7% name fractions names p's space domain function
  {\bgroup
   \let\spotpops\empty
   \ifcase#2\or
     %def\PDFspotcolornames{/Separation /#1}%
     \edef\PDFspotcolornames{/Separation /\executeifdefined{@@pdf@@scn@@#1}{#1}}%
     \def\PDFspotcolordomain{0.0 1.0}%
   \else
     \dorecurse{#2}{\edef\spotpops{\spotpops pop }}%
     \let\PDFspotcolornames \empty
     \let\PDFspotcolordomain\empty
     \def\dospotcolorcommand##1%
       {\edef\PDFspotcolornames {\PDFspotcolornames/\executeifdefined{@@pdf@@scn@@##1}{##1}\space}%
        \edef\PDFspotcolordomain{\PDFspotcolordomain 0.0 1.0\space}}%
     \processcommacommand[#3]\dospotcolorcommand
     \edef\PDFspotcolornames{/DeviceN [\PDFspotcolornames]}%
   \fi
   \immediate \pdfobj stream attr
     {/FunctionType 4 /Domain [\PDFspotcolordomain] /Range [#6]}{{\spotpops#7}}%
   \immediate \pdfobj
     {[\PDFspotcolornames\space /Device#5 \PDFobjref\pdflastobj]}%
   \dosetobjectreference{PDFCS}{#1}{\the\pdflastobj}%
   \appendtoPDFdocumentcolorspaces{/#1 \PDFobjref\pdflastobj}%
   \egroup}

%D New and very experimental.

% \def\doPDFregistersomeindexcolor#1#2#3#4#5#6#7% name fractions names p's space domain function
%   {\bgroup
%    \let\spotpops\empty
%    \dorecurse{#2}{\edef\spotpops{\spotpops exch pop\space}}%
%    \let\PDFspotcolornames \empty
%    \let\PDFspotcolordomain\empty
%    \def\docommand##1%
%      {\edef\PDFspotcolornames{\PDFspotcolornames/\executeifdefined{@@pdf@@scn@@##1}{##1}\space}%
%       \edef\PDFspotcolordomain{\PDFspotcolordomain 0.0 1.0\space}}%
%    \processcommacommand[#3,None]\docommand
%    \let\PDFcolorindexvector\empty
%    \def\docommand##1%
%      {\scratchdimen##1\points
%       \scratchdimen\recurselevel\scratchdimen
%       \scratchcounter\scratchdimen
%       \divide\scratchcounter \maxcard
%       \edef\PDFcolorindexvector{\PDFcolorindexvector\uchexnumbers\scratchcounter}}%
%    \dostepwiserecurse\zerocount{255}\plusone
%      {\rawprocesscommacommand[#4,1]\docommand
%       \xdef\PDFcolorindexvector{\PDFcolorindexvector\space}}%
%    \immediate \pdfobj stream attr
%      {/FunctionType 4 /Domain [\PDFspotcolordomain] /Range [#6]}{{\spotpops#7}}%
%    \immediate \pdfobj
%      {[/Indexed
%          [/DeviceN [\PDFspotcolornames] /Device#5 \PDFobjref\pdflastobj] %
%          255 <\PDFcolorindexvector>]}%
%    \dosetobjectreference{PDFIX}{#1}{\the\pdflastobj}%
%    \appendtoPDFdocumentcolorspaces{/#1_INDEXED \PDFobjref\pdflastobj}%
%    \egroup}

% we reverse the index; an alternative is to negate the graphic itself (\start/stop negative)

\def\doPDFregistersomeindexcolor#1#2#3#4#5#6#7% name fractions names p's space domain function
  {\bgroup
   \let\spotpops\empty
   \dorecurse{#2}{\edef\spotpops{\spotpops exch pop\space}}%
   \let\PDFspotcolornames \empty
   \let\PDFspotcolordomain\empty
   \def\docommand##1%
     {%\edef\PDFspotcolornames {\PDFspotcolornames/##1\space}%
      \edef\PDFspotcolornames{\PDFspotcolornames/\executeifdefined{@@pdf@@scn@@##1}{##1}\space}%
      \edef\PDFspotcolordomain{\PDFspotcolordomain 0.0 1.0\space}}%
   \processcommacommand[#3,None]\docommand
   \let\PDFcolorindexvector\empty
   \def\docommand##1%
     {\scratchdimen##1\points
      \scratchdimen\recurselevel\scratchdimen
      \scratchcounter\scratchdimen
      \divide\scratchcounter \maxcard
      \edef\PDFcolorindexvector{\PDFcolorindexvector\uchexnumbers\scratchcounter}}%
   %\dostepwiserecurse\zerocount{255}\plusone
   \dostepwiserecurse{255}\zerocount\minusone % we need to negate
     {\rawprocesscommacommand[#4,1]\docommand
      \xdef\PDFcolorindexvector{\PDFcolorindexvector\space}}%
   \immediate \pdfobj stream attr
     {/FunctionType 4 /Domain [\PDFspotcolordomain] /Range [#6]}{{\spotpops#7}}%
   \immediate \pdfobj
     {[/Indexed
         [/DeviceN [\PDFspotcolornames] /Device#5 \the\pdflastobj\space0 R] %
         255 <\PDFcolorindexvector>]}%
   \dosetobjectreference{PDFIX}{#1}{\the\pdflastobj}%
   \appendtoPDFdocumentcolorspaces{/#1_INDEXED \the\pdflastobj\space0 R}%
   \egroup}

%D \macros
%D   {dostarttransparency,dostoptransparency}
%D
%D For transparency, we need to implement a couple of
%D auxiliary macros. If needed, we will generalize them later.

\definespecial\dostarttransparency{\doPDFstarttransparency}
\definespecial\dostoptransparency {\doPDFstoptransparency}

\PDFtransparencysupportedtrue

\def\@@PDT{@PDT@}

\ifx\PDFcurrenttransparency\undefined
  \newcount\PDFcurrenttransparency \PDFcurrenttransparency=0 % -1
\fi

\def\assignPDFtransparency#1#2%
  {\edef\PDFtransparencyidentifier{/Tr#1}%
   \edef\PDFtransparencyreference{\PDFobjref{#2}}}

\def\presetPDFtransparency#1#2%
  {\initializePDFtransparency
   \executeifdefined{\@@PDT#1:#2}{\dopresetPDFtransparency{#1}{#2}}}

\def\dopresetPDFtransparency#1#2%
  {\global\advance\PDFcurrenttransparency \plusone
   \immediate\pdfobj{\PDFtransparancydictionary{#1}{#2}{}}%
   \edef\PDFtransparencyidentifier{/Tr\the\PDFcurrenttransparency}%
   \edef\PDFtransparencyreference {\PDFobjref\pdflastobj}%
   \setxvalue{\@@PDT#1:#2}%
     {\noexpand\assignPDFtransparency{\the\PDFcurrenttransparency}{\the\pdflastobj}}%
   \appendtoPDFdocumentextgstates
     {\PDFtransparencyidentifier\space
      \PDFtransparencyreference\space}}

\def\initializePDFtransparency
  {\immediate\pdfobj{\PDFtransparancydictionary{1}{1}{/AIS false}}%
   \xdef\PDFtransparencyresetidentifier{/Tr0}%
   \xdef\PDFtransparencyresetreference{\PDFobjref\pdflastobj}%
   \setxvalue{\@@PDT0:0}%
     {\noexpand\assignPDFtransparency{0}{\the\pdflastobj}}%
   \appendtoPDFdocumentextgstates
     {\PDFtransparencyresetidentifier\space
      \PDFtransparencyresetreference\space}%
   \global\let\initializePDFtransparency\relax}

%D \macros
%D   {dostartclipping,dostopclipping}
%D
%D Clipping in \PDFTEX\ is rather trivial. We can even hook
%D in \METAPOST\ without problems.

\definespecial\dostartclipping#1#2#3%
  {\PointsToBigPoints{#2}\width
   \PointsToBigPoints{#3}\height
   \grabMPclippath{#1}{1}\width\height
     {0 0 m \width\space 0 l \width \height l 0 \height l}%
   \pdfliteral % PDFcode ?
     {q 0 w \MPclippath\space W n}}

\definespecial\dostopclipping
  {\pdfliteral{Q n}} % PDFcode

%D \macros
%D   {dosetupinteraction}
%D
%D Nothing special is needed to enable \PDF\ commands and
%D interaction. We stick with a message.

\definespecial\dosetupinteraction
  {\showmessage\m!interactions{21}{pdftex}}

%D \macros
%D   {doresetgotowhereever,
%D    dostartthisisrealpage,dostartthisislocation,
%D    dostartgotorealpage,dostartgotolocation,dostartgotoJS}
%D
%D The interactions macros are the core of this module. We
%D support both page destinations and named ones. We don't
%D need the \type{\stop}||alternatives. We also don't need
%D to set the special that sets the real page number.

\definespecial\doresetgotowhereever {\doPDFresetgotowhereever}
\definespecial\dostartthisislocation{\doPDFstartthisislocation}

%D When going to a location, we obey the time and space saving
%D boolean \type{\ifusepagedestination}. Named destinations are
%D stripped and made robust. This all happens in the macros
%D called for.

\definespecial\dostartgotolocation{\doPDFstartgotolocation}
\definespecial\dostartgotorealpage{\doPDFstartgotorealpage}
\definespecial\dostartgotoJS      {\doPDFstartgotoJS}

\let\PDFpagexyzspec\empty % pdftex does not accept spec

%D \macros
%D   {doflushJSpreamble}
%D
%D It does not make sense to duplicate common \JAVASCRIPT\
%D functions, and therefore they can be predefined and must be
%D output separately. Currently this special is not shared
%D with the \ACROBAT\ one, simply because \DISTILLER\ does not
%D yet support something \type{\pdfnames}.

% \oneJSpreamblefalse  % buggy in acrobat

\definespecial\doflushJSpreamble#1%
  {\bgroup
   \let\compositeJScode\empty
   \def\docommand##1%
     {\edef\sanitizedJScode{\getJSpreamble{##1}}%
      \@EA\doPSsanitizeJScode\sanitizedJScode\to\sanitizedJScode
      \immediate\pdfobj {<< /S /JavaScript /JS (\sanitizedJScode) >>}%
      \edef\compositeJScode
        {\compositeJScode\space (##1) \PDFobjref\pdflastobj}}%
   \processcommalist[#1]\docommand
   \immediate\pdfobj{<< /Names [ \compositeJScode ] >>}%
   \pdfnames{/JavaScript \PDFobjref\pdflastobj}%
   \egroup}

%D \macros
%D   {dostarthide,dostophide}
%D
%D Hiding parts of the document for printing is not yet
%D supported by \PDF\ and therefore \PDFTEX.

\definespecial\dostarthide{}
\definespecial\dostophide {}

%D \macros
%D   {dosetupscreen}
%D
%D Setting of the screen boundingbox involves some
%D calculations. Here we also take care of (non) full screen
%D startup. The dimensions are rounded. Because \PDFTEX\ and
%D \ACROBAT\ handle setting the page dimensions in a
%D different way, we do not share this special.

\definespecial\dosetupscreen  {\doPDFsetupscreen  \pdfpageheight}

\definespecial\dosetupartbox  {\doPDFsetupartbox  \pdfpageheight}
\definespecial\dosetupcropbox {\doPDFsetupcropbox \pdfpageheight}
\definespecial\dosetupbleedbox{\doPDFsetupbleedbox\pdfpageheight}
\definespecial\dosetuptrimbox {\doPDFsetuptrimbox \pdfpageheight}

%D \macros
%D   {dostartexecutecommand}
%D
%D \PDF\ viewers enable us to navigate using menus and shortcut
%D keys. These navigational tools can also be accessed by using
%D annotations. The next special takes care of inserting them.

\definespecial\dostartexecutecommand{\doPDFstartexecutecommand}

%D \macros
%D   {dosetupidentity}
%D
%D Documents can be tagged with an application accessible title
%D and subtitle, the authorname, a date, the creator, keywords
%D etc. For the moment \PDFTEX\ only supports the first three
%D of these.

\definespecial\dosetupidentity{\doPDFsetupidentity}

%D \macros
%D   {dostartrunprogam}
%D
%D We can run a program form within a document, although this
%D feature is rather weak, due to path problems and buggy
%D argument passing.

\definespecial\dostartrunprogram{\doPDFstartrunprogram}

%D \macros
%D   {dostartgotoprofile, dostopgotoprofile,
%D    dobeginofprofile, doendofprofile}
%D
%D \CONTEXT\ user profiles and version control fall back on
%D \PDF\ article threads. Unfortunately one cannot influence
%D the view yet in an (for me) acceptable way.

\definespecial\dostartgotoprofile{\doPDFstartgotoprofile}

%D Some day, I'll reimplement threading in a useful way.
%D Currently the viewers handle threads rather diffuse.

\ifnum\pdftexversion>13

\definespecial\dobeginofprofile#1#2#3#4%
  {\setPDFdestination{#1}%
   \doifsomething\PDFdestination
     {\pdfthread
        width #2 height #3
        attr {/Title (\PDFdestination)} % can be omitted
        name {\PDFdestination}}}

\definespecial\doendofprofile%
  {}

\fi

%D \macros
%D  {doinsertbookmark}
%D
%D In \PDF\ bookmarks are the building blocks of a viewer
%D provided sort of table of contents. \TEX\ has to provide
%D the entry as well as the number of child entries. Strings
%D need to be sanatized as good as possible to suit the default
%D encoding. In \CONTEXT\ users can overrule this string by
%D supplying an alternative one. Look at the macro called for
%D to see how funny these bookmarks are defined.

\definespecial\doinsertbookmark{\doPDFinsertbookmark}

%D \macros
%D  {dostartobject,dostopobject,doinsertobject}
%D
%D Due to \PDF's object oriented character, we can include and
%D reuse objects. These can be compared with \TEX's boxes. The
%D \TEX\ counterpart is defined in the module \type{spec-dvi}.
%D We don't use the dimensions here.
%D
%D The next solution is not that beautiful. Because objects are
%D containers for whatever kind of content, graphics can be
%D part of this content, and a graphic object can be part of
%D the more general type. In practice this means that an ximage
%D would be embedded in an xform, which in itself is not that
%D big a problem, apart from a few bytes overhead. However, for
%D reasons unknown to me alternative images must be pure
%D ximages |<|indeed, somehow one cannot use a vector graphic
%D as alternative|>| that are not embedded into forms, so this
%D is why the object handler treats them different. This
%D implies knowledge of the calling routines, especially the
%D \type{FIG} trigger, that signals that we just embedded an
%D image. Alternatively I could have introduced a dual object
%D system, but the overhead in duplicate specials is currently
%D not what we want. I'd rather implement a more mature
%D object support system from scratch.

\let\currentPDFresources\empty
\let\PDFimageattributes \empty
\let\PDFfigurereference \empty
\let\PDFimagereference  \empty

\ifnum\pdftexversion>13

  \definespecial\dostartobject#1#2#3#4#5%
    {\bgroup
     \setbox\nextbox\vbox\bgroup
     \def\dodostopobject
       {\egroup
        \ifx\PDFimagereference\empty
          % We also flush page resources, since shared
          % resources end up there; otherwise transparencies
          % won't work in xforms; some day I will optimize
          % this.
          \the\everyPDFxform
          \finalizeobjectbox\nextbox
          \immediate\pdfxform
            resources {\currentPDFresources\the\pdfpageresources}%
            \nextbox
          \global\let\currentPDFresources\empty
          \dosetobjectreference{#1}{#2}{\the\pdflastxform}%
        \else
          \dosetobjectreference{#1}{#2}{-\PDFimagereference}%
          \global\let\PDFimagereference\empty
        \fi}}

  \definespecial\dostopobject
    {\dodostopobject
     \egroup}

  \definespecial\doresetobjects
    {\global\let\PDFimagereference\empty}

  \definespecial\doinsertobject#1#2%
    {\bgroup
     \doifobjectreferencefoundelse{#1}{#2}
       {\dogetobjectreference{#1}{#2}\PDFobjectreference
        \ifnum\PDFobjectreference<0
          \@EA\@EA\@EA\pdfrefximage\@EA\gobbleoneargument\PDFobjectreference
        \else
          \pdfrefxform\PDFobjectreference
        \fi}%
       {}%
     \egroup}

\else

  \definespecial\dostartobject#1#2#3#4#5%
    {\bgroup
     \setbox\nextbox\vbox\bgroup
     \def\dodostopobject
       {\egroup
        \the\everyPDFxform
        \pdfform\nextbox
        \dosetobjectreference{#1}{#2}{\the\pdflastform}}}

  \definespecial\dostopobject
    {\dodostopobject
     \egroup}

  \definespecial\doinsertobject#1#2%
    {\bgroup
     \dogetobjectreference{#1}{#2}\PDFobjectreference
     \pdfrefform\PDFobjectreference
     \egroup}

\fi

\appendtoksonce
    \collectPDFresources
    \global\let\currentPDFresources\collectedPDFresources
\to \everyPDFxform

%D \macros
%D   {dosetpagetransition}
%D
%D Page transitions only make sence in presentations. They are
%D passed as raw \PDF\ code to the page object. Take a look
%D at the implementation to get an impression of the rubish
%D passed on.

\definespecial\dosetpagetransition{\doPDFsetpagetransition}

%D The expansion is needed because else the \type{\pdfpageattr}
%D token list flushes an unexpanded \type{\csname}. The
%D \type{\global} is needed because the assignment can take
%D place deeply buried (for instance in the \type{\shipout}
%D box.

%D \macros
%D   {doinsertcomment, doflushcomments}
%D
%D Text annotation, or comments, are provided too:

\definespecial\doinsertcomment{\doPDFinsertcomment}
\definespecial\doflushcomments{\doPDFflushcomments}

%D \macros
%D   {dopresetlinefield,dopresettextfield,
%D    dopresetchoicefield,dopresetpopupfield,dopresetcombofield,
%D    dopresetpushfield,dopresetcheckfield,
%D    dopresetradiofield,dopresetradiorecord}
%D
%D \PDF\ offers extensive field support. The next bunch of
%D definitions map the specials.

\definespecial\dopresetlinefield  {\doFDFpresetlinefield}
\definespecial\dopresettextfield  {\doFDFpresettextfield}
\definespecial\dopresetchoicefield{\doFDFpresetchoicefield}
\definespecial\dopresetpopupfield {\doFDFpresetpopupfield}
\definespecial\dopresetcombofield {\doFDFpresetcombofield}
\definespecial\dopresetpushfield  {\doFDFpresetpushfield}
\definespecial\dopresetcheckfield {\doFDFpresetcheckfield}
\definespecial\dopresetradiofield {\doFDFpresetradiofield}
\definespecial\dopresetradiorecord{\doFDFpresetradiorecord}

%D \macros
%D   {dodefinefieldset,dogetfieldset,doiffieldset}
%D
%D Field sets, needed for reset and submit handling, are
%D taken care of by:

\definespecial\dodefinefieldset{\doFDFdefinefieldset}
\definespecial\dogetfieldset   {\doFDFgetfieldset}
\definespecial\doiffieldset    {\doFDFiffieldset}

%D \macros
%D   {doregistercalculationset}
%D
%D The calculation order is defined using:

\definespecial\doregistercalculationset{\doFDFregistercalculationset}

%D \macros
%D   {doPDFdestination}
%D
%D Finally we implement some low level macros to deal with
%D flushing \PDF\ code. First we handle the named destinations.

\def\doPDFdestination#1%
  {\pdfdest name {#1}\PDFpageviewkey}

%D \macros
%D   {doPDFaction,doPDFannotation,ifsharePDFactions}
%D
%D Next we handle annotations. All link annotations are
%D implemented using the action dictionary. This enables us to
%D use multiple actions. The second macro is for instance
%D used for movie inclusion.

\newif\ifsharePDFactions \sharePDFactionstrue

% hm, due to some stupid optimization this feature has been
% disabled for some time, watch out \lastPDFaction is to be
% persistent

\ifnum\pdftexversion>13

  \def\doPDFaction#1#2#3%
    {\xdef\lastPDFcontent{#3}%
     \ifcollectreferenceactions
       \global\let\lastPDFaction\lastPDFcontent
     \else
       \ifsharePDFactions
         \ifcase\similarreference\relax
           \xdef\lastPDFaction{<<\lastPDFcontent>>}%
         \or
           \immediate\pdfobj{<<\lastPDFcontent>>}%
           \xdef\lastPDFaction{\PDFobjref\pdflastobj}%
         \else
           % leave \lastPDFaction untouched
         \fi
       \else
         \xdef\lastPDFaction{<<\lastPDFcontent>>}%
       \fi
       \pdfannot
         width #1 height #2 depth \zeropoint
           {/Subtype /Link
            /Border [0 0 0]
            \ifhighlighthyperlinks \else /H /N \fi
            /A \lastPDFaction}%
     \fi}

\else

  \def\doPDFaction#1#2#3%
    {\ifcollectreferenceactions
       \xdef\lastPDFaction{#3}%
     \else
       \ifsharePDFactions
         \ifcase\similarreference\relax
           \xdef\lastPDFaction{<<#3>>}%
         \or
           \immediate\pdfobj{<<#3>>}%
           \xdef\lastPDFaction{\PDFobjref\pdflastobj}%
         \else
           % leave \lastPDFaction untouched
         \fi
       \else
         \xdef\lastPDFaction{<<#3>>}%
       \fi
       \pdfannotlink % could be \pdfannot if not the - problem was there
         width #1 height #2 depth \zeropoint
         user {/Subtype /Link
               /Border [0 0 0]
               \ifhighlighthyperlinks \else /H /N \fi
               /A \lastPDFaction}%
       \pdfendlink
     \fi}

\fi

% pdftex and viewers give problems with this one (printing forms)
%
%\def\doPDFannotation#1#2#3%
%  {\pdfannot width #1sp height -#2sp depth \zeropoint{#3}}
%
% This is corrected in version 14. When this version is wide
% spread, this will be cleaned up.

\ifnum\pdftexversion>13

  \def\doPDFannotation#1#2#3%
    {\pdfannot width #1 height #2 depth \zeropoint{#3}}

\else

  \def\doPDFannotation#1#2#3%
    {\hbox{\raise#2\hbox{\pdfannot width #1 height #2 depth \zeropoint{#3}}}}

\fi

%D \macros
%D   {doPDFannotationobject,doPDFactionobject}
%D
%D For field support we need annotation objects. Although in
%D many cases we can do without indirect references (and use
%D the last annotation object number directly), we take the
%D save route.

\def\doPDFannotationobject#1#2#3#4#5%
  {\doPDFannotation{#3}{#4}{#5}%
   \dosetobjectreference{#1}{#2}{\the\pdflastannot}}

\def\doPDFactionobject#1#2#3#4#5%
  {\doPDFaction{#3}{#4}{#5}%
   \dosetobjectreference{#1}{#2}{\the\pdflastannot}}

%D \macros
%D   {doPDFaddtocatalog,doPDFaddtoinfo,
%D    doPDFpageattribute,doPDFpagesattribute}
%D
%D Next some simple ones. Watch the global directive and the
%D expansion in the page attribute macro.

\def\doPDFaddtocatalog
  {\pdfcatalog}

\def\doPDFaddtoinfo
  {\pdfinfo}

\def\doPDFpageattribute#1%
  {\expanded{\global\pdfpageattr{#1\the\pdfpageattr}}}

\def\doPDFpageresource#1%
  {\expanded{\global\pdfpageresources{#1\the\pdfpageresources}}}

\def\doPDFpagesattribute#1%
  {\expanded{\global\pdfpagesattr{#1\the\pdfpagesattr}}}

\def\doPDFresetpageattributes
  {\global\pdfpageattr\emptytoks}

\def\doPDFresetpageresources
  {\global\pdfpageresources\emptytoks}

%D \macros
%D   {doPDFbookmark}
%D
%D Well, isn't the next one ugly? Thanks to the \PDF\
%D standard.

\def\doPDFbookmark#1#2#3#4#5%
  {\doPDFgetpagereference{#4}\PDFobjectreference
   \pdfoutline
     user {<</S /GoTo /D [\PDFobjectreference\space\PDFpageviewwrd]>>}%
     \ifcase#2 \else count \ifcase#5-\fi#2 \fi
     {\ifPDFunicode<#3>\else#3\fi}}

%D \macros
%D   {doPDFdictionaryobject,doPDFarrayobject}
%D
%D Where \PDFTEX\ has only one object primitive, optionally a
%D stream one, \ACROBAT\ has several operators.

\def\doPDFdictionaryobject#1#2#3%
  {\flushatshipout
     {\immediate\pdfobj{<< #3 >>}%
      \dosetobjectreference{#1}{#2}{\the\pdflastobj}}}

\def\doPDFarrayobject#1#2#3%
  {\flushatshipout
     {\immediate\pdfobj{[ #3 ]}%
      \dosetobjectreference{#1}{#2}{\the\pdflastobj}}}

% tricky .. too many xforms now
%
% \def\doreservePDFobject#1#2%
%   {\pdfobj reserveobjnum{}%
%    \driverreferenced\dosetobjectreference{#1}{#2}{\the\pdflastobj}}
%
% \def\doPDFreserveddictionaryobject#1#2#3%
%   {\doPDFgetobjectnumber{#1}{#2}\PDFobjectnumber
%    \immediate\pdfobj useobjnum \PDFobjectnumber {<< #3 >>}}
%
% \def\doPDFreservedarrayobject#1#2#3%
%   {\doPDFgetobjectnumber{#1}{#2}\PDFobjectnumber
%    \immediate\pdfobj useobjnum \PDFobjectnumber {[ #3 ]}}

% \doreservePDFobject{FDF}{docuextgstates}
% \doreservePDFobject{FDF}{colorspaces}
% \doreservePDFobject{FDF}{docushades}

%D \macros
%D   {defaultobjectreference,doPDFgetobjectreference}
%D
%D Because in \PDFTEX\ we have to construct the object
%D references \type{N 0 R}, we can default to the non existing
%D zero object number.

\def\defaultobjectreference#1#2%
  {0}

\def\doPDFgetobjectreference#1#2#3%
  {\dogetobjectreference{#1}{#2}#3%
   \edef#3{\ifx#3\empty null\else\PDFobjref{#3}\fi}}

\def\doPDFgetobjectnumber#1#2#3%
  {\dogetobjectreference{#1}{#2}#3%
   \edef#3{\ifx#3\empty 0\else#3\fi}}

% \def\doPDFgetobjectpage         #1#2#3{..}
% \def\doPDFgetobjectpagereference#1#2#3{..}

\ifx\pdfpageref\undefined

  \def\doPDFgetpagereference % number macro
    {\installprogram{texutil --filter \jobname}%
     \gdef\doPDFgetpagereference##1{\doPDFgetobjectreference{PDFP}{\number##1}}% {##2}
     \doPDFgetpagereference}

\else

  \def\doPDFgetpagereference#1#2% number macro
    {\edef#2{\ifnum#1>\zerocount\PDFobjref{\pdfpageref#1}\else null\fi}}

\fi

%D \macros
%D   {initializePDFnegative,initializePDFoverprint}
%D
%D Here follow some rather obscure macros. They will only
%D come into action when one wants negated output.

%D Todo: move code to fdf module

\def\initializePDFnegative
  {\immediate\pdfobj stream attr {/FunctionType 4 /Range [0 1] /Domain [0 1]} {{1 exch sub}}%
   \immediate\pdfobj{<</Type /ExtGState /TR \PDFobjref\pdflastobj>>}%
   \appendtoPDFdocumentextgstates{/GSnegative \PDFobjref\pdflastobj}%
   \immediate\pdfobj{<</Type /ExtGState /TR /Identity>>}%
   \appendtoPDFdocumentextgstates{/GSpositive \PDFobjref\pdflastobj}%
   \global\let\initializePDFnegative\relax}

\def\initializePDFoverprint
  {\immediate\pdfobj{<</Type /ExtGState /OP false /OPM 0>>}% /op defaults to /OP
   \appendtoPDFdocumentextgstates{/GSknockout \PDFobjref\pdflastobj}%
   \immediate\pdfobj{<</Type /ExtGState /OP true /OPM 1>>}% /op defaults to /OP
   \edef\PDFobjectreferenceB{\the\pdflastobj}%
   \appendtoPDFdocumentextgstates{/GSoverprint \PDFobjref\pdflastobj}%
   \global\let\initializePDFoverprint\relax}

%D File embedding. Storing the stream identifier is needed
%D to get access to the number. When typeset, the user can
%D feed this number to \type {pdftosrc} and filter the
%D file from the \PDF\ file.

\let\PDFlaststreamobject   \s!unknown
%def\PDFlaststreamreference{0 0 R}

\def\doPDFfilestreamobject#1#2#3#4%
  {\immediate\pdfobj stream file{#4}%
   \edef\PDFlaststreamobject{\the\pdflastobj}%
   \dosetobjectreference{PDFFS}{#2}{\PDFlaststreamobject}%
   \doPDFdictionaryobject{#1}{#2}{/Type /Filespec /F (#3) /EF <</F \PDFobjref\PDFlaststreamobject>>}}

\def\doPDFgetfilestreamreference#1#2%
  {\doPDFgetobjectreference{PDFFS}{#1}#2}

\def\doPDFfilestreamidentifier#1%
  {\doifsomething{#1}
     {\doPDFgetfilestreamreference{#1}\PDFobjectreference
      \@EA\beforesplitstring\PDFobjectreference\at{ }\to\PDFlaststreamobject
      \PDFlaststreamobject}}

% %D We can set \METAPOST\ prologues to~1:

\def\MPprologues{1}

%D Experimental:

\definespecial\dostartfonteffect{\doPDFstartfonteffect}
\definespecial\dostopfonteffect {\doPDFstopfonteffect}

%D Some MP stuff:

\let\handleMPfshow\dohandleMPfshow % default anyway

\ifnum\pdftexversion<14

  \def\setMPPDFobject#1#2% resources boxnumber
    {\def\getMPPDFobject{\box#2}}

\else

  \def\setMPPDFobject#1#2% resources boxnumber
    {\the\everyPDFxform
     \finalizeobjectbox{#2}%
     \immediate\pdfxform resources{#1}#2%
     \edef\getMPPDFobject{\noexpand\pdfrefxform\the\pdflastxform}}

\fi

\let\getMPPDFobject\relax

\definespecial\doinsertMPfile#1%
  {\doiffileelse{./#1}{\includeMPasPDF{./#1}}{\message{[MP #1]}}}

%D Now we can finish this module.

\stopspecials

\protect \endinput