summaryrefslogtreecommitdiff
path: root/gs/Resource/Init/pdf_main.ps
blob: 06f4eb7686b4c78fb0ff1775e1434df770392e94 (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
% Copyright (C) 2001-2012 Artifex Software, Inc.
% All Rights Reserved.
%
% This software is provided AS-IS with no warranty, either express or
% implied.
%
% This software is distributed under license and may not be copied,
% modified or distributed except as expressly authorized under the terms
% of the license contained in the file LICENSE in this distribution.
%
% Refer to licensing information at http://www.artifex.com or contact
% Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
% CA  94903, U.S.A., +1(415)492-9861, for further information.
%

% pdf_main.ps
% PDF file- and page-level operations.

/.setlanguagelevel where { pop 2 .setlanguagelevel } if
.currentglobal //true .setglobal
/pdfdict where { pop } { /pdfdict 100 dict def } ifelse
pdfdict begin

% Patch in an obsolete variable used by some third-party software.
/#? //false def

% Test whether the current output device handles pdfmark.
/.writepdfmarkdict 1 dict dup /pdfmark //null put readonly def
/.writepdfmarks {	% - .writepdfmarks <bool>
  currentdevice //.writepdfmarkdict .getdeviceparams
  mark eq { //false } { pop pop //true } ifelse
  systemdict /DOPDFMARKS known or
} bind def

% For simplicity, we use a single interpretation dictionary for all
% PDF graphics execution, even though this is too liberal.
/pdfopdict mark
  objopdict { } forall
  drawopdict { } forall
  /endstream { exit } bind
  (%%EOF) cvn { exit } bind		% for filters
  /obj { (   **** Warning: Content stream is not terminated by 'endstream'.\n)
         pdfformaterror pop pop exit
       } bind
        % PDF 1.1 operators
  /BX { /BXlevel BXlevel 1 add store } bind
  /EX { /BXlevel BXlevel 1 sub store } bind
  /PS { cvx exec } bind
        % PDF 1.2 operators
  /BMC {
    /BMClevel BMClevel 1 add store
    pop
  } bind
  /BDC {
    /BMClevel BMClevel 1 add store
    exch /OC eq {
      dup type /nametype eq {
        PDFfile fileposition exch                      % pos /Name
        Page /Properties rget {
          ocg-is-visible not {
            OFFlevels BMClevel dup put
          } if
        } if
        PDFfile exch setfileposition
      } {
        pop
      } ifelse
    } {
      pop
    } ifelse
  } bind
  /EMC {
    OFFlevels BMClevel
    2 copy known {
       2 copy undef
    } if
    1 sub /BMClevel exch store
    pop
  } bind
  /MP { pop } bind
  /DP { pop pop } bind
  /- { 0  % Bug 690016
       (   **** Warning: Invalid operator '-' is assumed to be a number 0.\n)
       pdfformaterror
     } bind
.dicttomark readonly def

% ======================== Main program ======================== %

end			% pdfdict
userdict begin

/defaultfontname /Times-Roman def

% Make sure the registered encodings are loaded, so we don't run the risk
% that some of the indices for their names will overflow the packed
% representation.  (Yes, this is a hack.)
SymbolEncoding pop
DingbatsEncoding pop

% Redefine 'run' so it recognizes PDF files.
systemdict begin
/.runps /run load def
/run {
  dup type /filetype ne { (r) file } if
  % skip leading whitespace characters (actually anything less than or equal to <sp>)
  { dup ( ) .peekstring not { //false exit } if
    dup 0 get 32 le { pop dup read pop pop } { //true exit } ifelse
  } loop
  exch pop
  {
    % Appletalk PAP sends short strings with %! header expecting a response.
    % 'gv' swallows the %!PS line, then sends DSC comments beginning with %%
    % and also waits for a response. The following avoids those hangs.
    dup 2 string .peekstring pop dup (%!) eq exch (%%) eq or {
      cvx .runps
    } {
      dup 1023 string .peekstring pop
      % "1024 string" exceeds current %stdin buffer
      % Valid PDF file cannot be smaller than 400 bytes.
      (%PDF-) search {
        3 1 roll pop pop
        dup (%!PS) search not {
          length 0 ne {
            1 index exch readstring pop pop
            (%stderr) (w) file dup
            (   **** Warning: File has some garbage before %PDF- .\n)
            writestring flushfile
          } {
            pop
          } ifelse
          dup (%stdin) (r) file eq {
            % Copy PDF from stdin to temporary file then run it.
            //null (w+) /.tempfile .systemvar exec exch 3 1 roll
            % stack: tempname stdin tempfile
            64000 string
            {
              % stack: tempname stdin tempfile string
              2 index 1 index readstring
              exch 3 index exch writestring
              not { exit } if
            }
            loop
            pop exch closefile
            % stack: tempname tempfile
            dup 0 setfileposition
            dup
            PDFSTOPONERROR {
              runpdf
            } {
              {runpdf} stopped
              {(**** ERROR ****\nThe PDF interpreter encountered an error, this file cannot be processed.\n) print flush count 1 1 3 -1 roll {pop pop} for} if
            } ifelse
            closefile deletefile
          } {
            PDFSTOPONERROR {
              runpdf
            } {
              {runpdf} stopped
              {(**** ERROR ****\nThe PDF interpreter encountered an error, this file cannot be processed.\n) print flush count 1 1 3 -1 roll {pop pop} for} if
            } ifelse
          } ifelse
        } {
          pop pop pop pop cvx .runps % (%!PS) found first
        } ifelse
      } {
        pop cvx .runps % (%PDF-) not found
      } ifelse
    } ifelse
  } {
    closefile % file was empty
  } ifelse
} bind odef
currentdict /runpdfstring .undef

/runpdfbegin {		% <file> runpdfbegin -
   userdict begin
        % It turns out that the PDF interpreter uses memory more
        % effectively if it is run under at least one level of save.
        % This is counter-intuitive, and we don't understand why it happens,
        % but the improvement is significant.
   /PDFTopSave save def
   0 setobjectformat
   /Page# //null def
   /Page //null def
   /DSCPageCount 0 def
   /PDFSave //null def
   GS_PDF_ProcSet begin
   pdfdict begin
   pdfopen begin
   /CumulativePageCount currentpagedevice /PageCount get def
} bind def

/runpdfpagerange {	% - runpdfpagerange <firstpage#> <lastpage#>
   /PortfolioPage where {
     pop
     PortfolioPage cvi dup pdfpagecount add                  % a b+1
     /PortfolioPage dup load                                 % a b+1 /P ()
     (          ) 1 index  copy pop
     dup 3 index exch cvs pop                                % a b+1 /P (b+1)
     store
     1 sub                                                   % a b
     /FirstPage where { pop FirstPage } { 1 } ifelse
     /LastPage  where { pop LastPage  } {2000000000} ifelse  % a b fp lp
     2 index 2 index lt { % b < fp
       1e10
     } {
       3 index 2 index gt { % a > fp
         1
       } {
         1 index 4 index sub 1 add  
       } ifelse
     } ifelse                                                % a b fp lp f
     3 index 2 index lt { % b < bp
       3 index 5 index sub 1 add  % b-a+1=count
     } {
       4 index 2 index gt {
         0
       } {
         1 index 5 index sub 1 add
       } ifelse
     } ifelse                                                % a b fp lp f l
     6 2 roll pop pop pop pop

     QUIET not { 
       1 index 1 index gt { 
         (Skipping the subfuile.) = flush 
       } { 
           (Processing pages ) print 1 index =only ( through ) print dup =only
           (.) = flush
       } ifelse
     } if
   } {
     /FirstPage where { 
       pop FirstPage dup pdfpagecount gt {
         (\nRequested FirstPage is greater than the number of pages in the file: ) print
          pdfpagecount = flush
       } if
     } {
        1
     } ifelse
     /LastPage where { pop LastPage pdfpagecount .min } { pdfpagecount } ifelse
     1 index 1 index gt { 
       (   No pages will be processed \(FirstPage > LastPage\).) = flush 
     } { 
       QUIET not { 
         (Processing pages ) print 1 index =only ( through ) print dup =only
         (.) = flush
       } if
     } ifelse
   } ifelse
} bind def

/dopdfpages {   % firstpage# lastpage# dopdfpages -
  << /PDFScanRules //true >> setuserparams	% set scanning rules for PDF vs. PS
  << /RenderTTNotdef systemdict
     /RENDERTTNOTDEF get >> setuserparams	% Should we render TT /.notdef
  1 exch
    { dup /Page# exch store
      QUIET not { (Page ) print dup //== exec flush } if
      pdfgetpage pdfshowpage
    } for
  % Indicate that the number of spot colors is unknown in case the next page
  % imaged is a PS file.
  << /PageSpotColors -1 >> setpagedevice
  << /PDFScanRules //null >> setuserparams	% restore scanning rules for PS
} bind def

/runpdfend {
   Repaired { printrepaired } if
   currentdict pdfclose
   end			% temporary dict
   end			% pdfdict
   end			% GS_PDF_ProcSet
   PDFTopSave restore
   end			% userdict
   2 vmreclaim		% couldn't hurt
} bind def

% Copy stream to an external temporary file and
% return the file name as PS name.
/copy_embedded_file {
  //true resolvestream      % strm
  dup 1023 string .peekstring pop % "1024 string" exceeds current %stdin buffer
  dup length 400 ge {             % Valid PDF file cannot be smaller than 400 bytes.
     (%PDF-) search {
       pop pop pop //true
     } {
       pop //false
     } ifelse
   } {
     pop //false
   } ifelse {
    //null (w) /.tempfile   % strm (name) null (w) /.tempfile
    .systemvar exec         % strm (name) file
    3 -1 roll               % (name) file strm
    32768 string            % (name) file strm (buf)
    { 3 copy readstring     % (name) file strm (buf) file (data) bool
      3 1 roll              % (name) file strm (buf) bool file (data)
      writestring           % (name) file strm (buf) bool
      not { exit } if
    } loop
    pop closefile           % (name) file
    closefile               % (name)
    cvn                     % /name
  } {
    closefile
  } ifelse
} bind def

% Recursively enumerate /Names entries
% <node> pdf_collection_names /temp_file_name ...
/pdf_collection_names {
  dup /Names knownoget {
    exch pop
    { oforce
      dup type /dicttype eq {
        /EF knownoget {
          /F knownoget {
            copy_embedded_file
          } if
        } if
      } {
        pop
      } ifelse
    } forall
  } {
    /Kids knownoget {
      { oforce 
        dup //null ne {
          pdf_collection_names
        } {
          pop
        } ifelse
      } forall
    } if
  } ifelse
} bind def

% Copy selected subfiles to temporary files and return the file names
% as a PostScript names to protect them from restore.
% Currently, all PDF files in the Portfolio are extracted and returned.
%
% - pdf_collection_files [ /temp_file_name ... /temp_file_name
/pdf_collection_files {
  mark
  Trailer /Root knownoget {
    dup /Collection oknown {
      /Names knownoget {
        /EmbeddedFiles knownoget {
          pdf_collection_names
        } if
      } if
    } {
      pop
    } ifelse
  } if
} bind def

/runpdf {		% <file> runpdf -
  //runpdfbegin exec
  //pdf_collection_files exec
  dup mark eq {
    pop
    process_trailer_attrs
    //runpdfpagerange exec
    //dopdfpages exec
    //runpdfend exec
  } {
    //runpdfend exec
    ] 
    (1         ) 10 string copy exch
    {
        dup type /filetype eq {
          //runpdfbegin exec
          dup /PortfolioPage exch def
          process_trailer_attrs
          //runpdfpagerange exec
          //dopdfpages exec
          //runpdfend exec
          closefile
        } {
          .namestring dup (r) file
          //runpdfbegin exec
          /PortfolioPage 2 index def
          process_trailer_attrs
          //runpdfpagerange exec
          //dopdfpages exec
          //runpdfend exec
          deletefile
        } ifelse
    } forall
    pop
  } ifelse
} bind def

currentdict /pdf_collection_files .undef

end			% systemdict
% Redefine the procedure that the C code uses for running piped input.
% It is OK to use { (%stdin) run } here, because a startjob cannot occur.
/.runstdin {
  { (%stdin) run } execute0
} bind def

end			% userdict
pdfdict begin

% ======================== File parsing ======================== %

% Read the cross-reference and trailer sections.

/traileropdict mark
  (<<) cvn { /dictlevelcount dictlevelcount 1 add store mark } bind
  (>>) cvn { { .dicttomark } stopped {
              (   **** File has unbalanced >> in trailer.\n) pdfformaterror
             } if
             /dictlevelcount dictlevelcount 1 sub def
             dictlevelcount 0 eq { exit } if
           } bind
  ([) cvn { mark } bind		% ditto
  (]) cvn dup load
%  /true true		% see .pdfexectoken in pdf_base.ps
%  /false false		% ibid.
%  /null null		% ibid.
  /R { /resolveR cvx 3 packedarray cvx } bind	% see Objects below
.dicttomark readonly def

% After reading entry count: skip whitespace, exit on a digit
% (presumably the object offset, otherwise throw /syntaxerror
/xref-char-dict <<
   0  {}        % \000
   9  1 index   % \t
  10  1 index   % \r
  12  1 index   % \f
  13  1 index   % \n
  32  1 index   % ' '
  
  48  { exit } bind % '0'
  49  1 index   % '1'
  50  1 index   % '2'
  51  1 index   % '3'
  52  1 index   % '4'
  53  1 index   % '5'
  54  1 index   % '6'
  55  1 index   % '7'
  56  1 index   % '8'
  57  1 index   % '9'
 116  {         % 't'railer
        PDFfile 7 string .peekstring not { () } if
        dup /trailer eq {
          % Empty xref section
          pop exit
        } {
          (   **** Warning: bad string \() exch concatstrings
          (\) follows xref section header.\n) concatstrings
          pdfformaterror
          /setxrefentry cvx /syntaxerror signalerror
        } ifelse
      } bind
>> readonly def

 %  Read original version (pre PDF 1.5) of the xref table.
 %  Note:  The position is the location of 'xref'.  The current PDFfile
 %  position is just after the 'XREF'.
/readorigxref		% <pos> readorigxref <trailerdict>
 {
   pop				% We do not need the position.
   0				% Initialize xref table error counter
   { PDFfile token pop		% first object # or trailer
     dup /trailer eq { pop Objects llength 0 eq {/setxrefentry cvx /syntaxerror signalerror} {exit} ifelse } if
     PDFfile token pop		% entry count
 
     % remaining must be whitespace only (otherwise this xref Size was invalid.
     { PDFfile (0) .peekstring not { pop exit } if
       0 get //xref-char-dict exch .knownget {
         exec
       } {
         (   **** Warning: xref subsection header has extra characters.\n)
         pdfformaterror
         /setxrefentry cvx /syntaxerror signalerror
       } ifelse
       PDFfile read { pop } { exit } ifelse
     } loop
                % This section might be adding new objects:
                % ensure that Objects and Generations are big enough.
                % stack: <err count> <first obj> <entry count>
     2 copy add growPDFobjects
     {				% stack: <err count> <obj num>
                % Read xref line
       PDFfile 20 string readstring pop  % always read 20 chars.
       token pop		% object position
       exch token pop		% generation #
       exch token pop		% n or f
       exch			% stack: <err count> <obj#> <loc> <gen#> <tag> <remainder of line>
       % check to make sure trailing garbage is just white space
       //false 1 index { 32 gt or } forall {
         6 -1 roll 1 add 6 1 roll  % bump error count on garbage
         dup (\n) search {
           exch pop exch pop
         } {
           (\r) search {
             exch pop exch pop
           } if
         } ifelse
         length
         PDFfile fileposition exch sub PDFfile exch setfileposition
       } if
       pop			% Stack: <err count> <obj#> <loc> <gen#> <tag>
       dup /n eq {		% xref line tag is /n
         pop			% pop dup of line tag
         1 index 0 eq {
           (   **** Warning: considering '0000000000 XXXXX n' as a free entry.\n)
           pdfformaterror
         } {
           0 3 1 roll		% Set ObjectStream object number = 0
           //false setxrefentry	% Save xref entry, don't change existing entries
           3 -1 roll pop	% Remove ObjectStream object onumber
         } ifelse
       }
       {			% xref line tag was not /n
         /f ne			% verify that the tag was /f
         { /setxrefentry cvx /syntaxerror signalerror
         } if
       } ifelse
       pop pop			% pop <obj location> and <gen num>
       % stack: <err count> <obj num>
       1 add			% increment object number
     } repeat
     pop			% pop <obj #>
   } loop
   0 ne {
     (   **** Warning:  length of some xref entries is not equal to 20 bytes.\n)
     pdfformaterror
   } if
   /dictlevelcount 0 def
   PDFfile traileropdict .pdfrun
 } bind def

currentdict /xref-char-dict undef

 % This dicitonary is used to read the xref dictionary.  It should work for
 % reading any dictionary.  dictlevelcount must contain 0.
/xrefopdict mark
  (<<) cvn { /dictlevelcount dictlevelcount 1 add def mark } bind
  (>>) cvn { .dicttomark /dictlevelcount dictlevelcount 1 sub def
             dictlevelcount 0 eq { exit} if } bind
  ([) cvn { mark } bind		% ditto
  (]) cvn dup load
%  /true true		% see .pdfexectoken in pdf_base.ps
%  /false false		% ibid.
%  /null null		% ibid.
  /R { /resolveR cvx 3 packedarray cvx } bind	% see Objects below
.dicttomark readonly def

% Get a variable length positive integer value from a stream.  A value
% of zero is returned if the count is zero.
/getintn {	% <stream> <count> getintn int
  0 exch { 256 mul 1 index read pop add } repeat
  exch pop			% Discard stream
} bind def

% This array contains handlers for processing the different types of
% entries in the XRef stream.
% Stack: <Xrefdict> <xref stream> <Index array> <pair loc> <obj num>
% 	 <field 2> <field 3>
% The handlers leave the stack unchanged.
/xref15entryhandlers [
  {	% XRef entry type 0 - free or f type xref entry
%    (free ) print
%    (obj num: ) print 2 index pdfstring cvs print ( ) print
%    (loc: ) print 1 index pdfstring cvs print ( ) print
%    (gen: ) print dup === flush
  } bind		% Do nothing for free xref entries
        % XRef entry type 1 - normal or n type xref entry
  {	% field 2 = obj loc, field 3 = gen num
%    (normal ) print
%    (obj num: ) print 2 index pdfstring cvs print ( ) print
%    (loc: ) print 1 index pdfstring cvs print ( ) print
%    (gen: ) print dup === flush
    0 3 1 roll			% set stream number = 0
    //false setxrefentry
    3 -1 roll pop		% remove stream number
  } bind
        % XRef entry type 2 - compressed object type xref entry
  {	% field 2 = object stream num, field 3 = index into object stream
%    (Compressed objects: ) print
%    (obj num: ) print 2 index pdfstring cvs print ( ) print
%    (field 2: ) print 1 index pdfstring cvs print ( ) print
%    (field 3: ) print dup === flush
    0 //false setxrefentry pop	% set generation number = 0
  } bind
] def

 %  Read the PDF 1.5 version of the xref table.
 %  Note:  The position is the location of the start of the dictionary object
 %  In PDF 1.5, the XRef dictionary also serves as the trailer dictionary
/readpdf15xref		% <pos> readpdf15xref <trailerdict>
 {
   /Classic-xref //false store
   PDFfile exch setfileposition		% move to start of object
        % Get object number, revision, and 'obj' and discard
   //false PDFfile token {
     type /integertype eq {
       PDFfile token {
         type /integertype eq {
           PDFfile token {
             /obj eq or
           } if
         } if
       } if
     } if
   } if
   not {
     /readpdf15xref cvx /syntaxerror signalerror
   } if
        % Get the XRef dicitionary
   /dictlevelcount 0 def PDFfile xrefopdict .pdfrun
        % Verify that we have an XRef dictionary
   dup /Type get /XRef ne {
     /readpdf15xref cvx /syntaxerror signalerror
   } if
        % Ensure that we we have room in the objects array, etc.
   dup /Size get growPDFobjects
        % Create a stream for the XRef data
   PDFfile token pop pop		% Skip over 'stream'
   dup stream //false resolvestream
        % Stack: <XRefdict> <xref stream>
        % The Index array defines the ranges of object numbers in the
        % XRef stream.  Each value pair is consists of starting object
        % number and the count of consecutive objects.
        % Get the Index array, if present
   1 index /Index .knownget not {	% If no Index array ...
     [ 0 3 index /Size get ]		% Default = [ 0 Size ]
   } if
        % Loop through the Index array
   0 2 2 index length 1 sub {
        % Get start and end of object range
     2 copy get				% Start of the range
     dup 3 index 3 index 1 add get 	% Number of entries in range
        % Loop through the range of object numbers
     add 1 sub 1 exch {			% Form end of range, set increment = 1
        % Stack: <Xrefdict> <xref stream> <Index array> <pair loc> <obj num>
        % Get xref parameters.  Note:  The number of bytes for each parameter
        % is defined by the entries in the W array.
       4 index /W get aload pop		% Get W array values
        % The first field indicates type of entry.  Get first field value.
        % If the num. of bytes for field 1 is 0 then default field value is 1
       3 -1 roll dup 0 eq { pop 1 } { 6 index exch getintn } ifelse
        % Get the handler for the xref entry type.  We will execute the
        % handler after we get the other two field values.
       xref15entryhandlers exch get
       3 -1 roll 6 index exch getintn	% Get second field
       3 -1 roll 6 index exch getintn	% Get third field
       3 -1 roll exec			% Execute Xref entry handler
       pop pop pop			% Remove field values and obj num
     } for				% Loop through Xref entries
     pop				% Remove Index array pair loc
   } for				% Loop through Index array entries
   pop pop				% Remove Index array and xref stream
 } bind def

% Read the cross-reference table.
% <pos> is the position either from the startxref statement or the /Prev
% entry in the prior trailer dictionary.
/readxref		% <pos> readxref <trailerdict>
 {
   PDFoffset add PDFfile exch
   % Check that the given location is within the file.
   dup PDFfilelen gt {
       (   **** Warning:  Specified xref location is beyond end of file.\n)
       pdfformaterror
       /readxref cvx /invalidaccess signalerror
   } if
   setfileposition
                % In some PDF files, this position actually points to
                % white space before the xref line.  Skip over this here.
   {
     PDFfile fileposition PDFfile read pop 32 gt { exit } if pop
   } loop
   dup		% Make copy of the file position (before last char was read).
   PDFfile exch setfileposition
                % The PDF specification says that the 'xref' must be on a line
                % by itself. The code here formerly used readline and linene to
                % check this. However, Acrobat Reader only requires the line to
                % begin with 'xref', and there are enough applications producing
                % non-compliant PDF files that we have to do this too.
   PDFfile pdfstring 0 4 getinterval readstring pop
   (xref) eq
   {
     readorigxref	% 'xref' -> original xref table
     % if hybrid-reference PDF, also fetch the entries
     % found in the XRef stream pointed by /XRefStm
     dup /XRefStm knownoget {
       readpdf15xref pop
     } if
   }
   { readpdf15xref }	% otherwise assume PDF 1.5 xref stream
   ifelse
 } bind def

% Open a PDF file and read the header, trailer, and cross-reference.
/pdfopen {		% <file> pdfopen <dict>
        % Color space substitution in PDF is handled somewhat differently
        % than in PostScript. A given device color space will be substituted
        % if the corresponding "Default..." entry exists in the Page's
        % Resource dictionary (which might be inhereted); there is no
        % UseCIEColor to enable/disable color mapping.
        %
        % This behavior is achieved by always setting UseCIEColor to true
        % in the page device dictionary. If the value of this parameter was
        % originally false (i.e.: the output device does not perform color
        % space substitution by default), the instances DefaultGray,
        % DefaultRGB, and DefaultCMYK of the (local) ColorSpace category
        % are redefined to be DeviceGray, DeviceRGB, and DeviceCMYK,
        % respectively. This is not done if UseCIEColor is true by default,
        % as in that case color substitution is presumably desired even
        % if the file does not request it.
   currentpagedevice /UseCIEColor .knownget dup { pop } if not
    { .currentglobal //false .setglobal
      /DefaultGray { /DeviceGray } cvlit /ColorSpace defineresource pop
      /DefaultRGB { /DeviceRGB } cvlit /ColorSpace defineresource pop
      /DefaultCMYK { /DeviceCMYK } cvlit /ColorSpace defineresource pop
      .setglobal
    }
   if
  pdfopenfile begin
  pdfopencache
  currentdict end
} bind def

/process_trailer_attrs {  % - process_trailer_attrs -
  writeoutputintents
  .writepdfmarks {
        % Copy bookmarks (outline) to the output.
    Trailer /Root knownoget {
    /Outlines knownoget {
      /First knownoget {
          dup type /dicttype eq {
            { dup writeoutline /Next knownoget not { exit } if
              dup type /dicttype eq not {exit} if
            } loop
          }
          {pop} ifelse
        }if
      } if
    } if
    Trailer /Info knownoget {
      mark exch
      { /Author /Creator /Title /Subject /Keywords
        % /Producer is Ghostscript; /CreationDate is now; /ModDate is also now.
      } {
        2 copy knownoget { 3 -1 roll } { pop } ifelse
      } forall
      pop
      dup mark ne { /DOCINFO pdfmark } { pop } ifelse
    } if
  } if
  % Initialize OC groups
  Trailer /Root knownoget {
    /OCProperties knownoget {
    % By default, OCGs are 'on'; mark only 'off' OCGs.
    % If, contrary to the spec, BaseState is OFF, mark all OCGs
    % and unmark ON ones. Bug 691491.
    dup /OCGs knownoget not { {} } if exch
    /D knownoget {
      dup /BaseState knownoget { /OFF eq } { //false } ifelse {
        (   **** Warning:  Default viewing OC config dictionary has /BaseState /OFF\n)
        pdfformaterror
        1 index {
          oforce dup type /dicttype eq {
            /OFF 0 put
          } {
            pop
          } ifelse
        } forall
        /ON knownoget {
          { oforce dup type /dicttype eq {
              /OFF undef
            } {
              pop
            } ifelse
          } forall
        } if
      } {
        /OFF knownoget {
          { oforce dup type /dicttype eq {
              /OFF 0 put
            } {
              pop
            } ifelse
          } forall
        } if
      } ifelse
    } if
    pop
  } if
  } if

  % Enumerate all AcroForm fielda and link all separate widgets
  % to the parent field.
  Trailer /Root knownoget {
    /AcroForm knownoget {
    dup /NeedAppearances knownoget not { //false } if {
      /NeedAppearances //true def
      dup
      /Fields knownoget {
        { oforce
          link_widget_annots
          pop
        } forall
      } if
      pop
    } {
      pop
    } ifelse
  } if
  } if
  
  % Use OutputIntent ICC profile
  systemdict /UsePDFX3Profile .knownget {
    dup //false eq {
      pop
    } {
      dup //true eq { pop 0 } if
      dup type /integertype eq {
        Trailer /Root oget /OutputIntents knownoget {
          dup length 2 index le  % i [] bool
          2 index 0 lt or {
            pop pop
            (Source file has no OutputIntent with this number.\n)
          } {
            exch oget dup type /dicttype ne { pop << >> } if
            /DestOutputProfile knownoget {
              [ /ICCBased 3 -1 roll ] ICCBased-resolve
              1 get .set_outputintent
              ()
            } {
              (This OutputputIntents dictionary has no profile.\n)
            } ifelse
          } ifelse
        } {
          pop (File has no /OutputIntents attribute\n)
        } ifelse
      } {
        pop (UsePDFX3Profile must be a boolean or an integer.\n)
      } ifelse
      dup () eq {
        pop
      } {
        (   **** ) stderrprint stderrprint
      } ifelse
    } ifelse
  } if
  Trailer /Root knownoget { /PageLabels knownoget {
    %% Currenlty only the pdfwrite device can handle PageLabels
    %% because we deal with these using putparams, not pdfmarks
    %% This is because the pdfmark syntax can't handle all the
    %% possible page label formats in a PDF file!
    currentdevice .devicename /pdfwrite eq {
      .pdfpagelabels
    } {
      pop
    } ifelse
  } if
  } if
} bind def

% Verify that each entry in the xref table is pointing at an object with
% the correct object number and generation number.
/verify_xref				% - verify_xref -
{ PDFfilelen
  1 1 Objects llength 1 sub	% stack: filesize 1 1 <number of objects - 1>
  {	% Check if the object is free (i.e. not used).  The values in
        % Generations is the generation number plus 1.  If the value in
        % Generations is zero then the object is free.
        % Stack:  <filesize> <obj num>
    Generations 1 index lget 		% Get the genration number
    0 ne {				% Skip if object number is free
      ObjectStream 1 index lget 	% Check if object is in objectstream
      0 eq {	% We only check objects not in an objectstream
        {	% Use stop context since we may get an error if object is invalid
          dup Objects exch lget 	% Get the object location
          PDFoffset add dup 3 index ge	% Compare object location to file size
          { pop //true }			% Rebuild if location not in file
          { PDFfile exch setfileposition % Go to the object location
            //true			% Stack:  <filesize> <obj num> <true>
            PDFfile token pop		% Read object number from file
            2 index eq {		% Verify object number
              PDFfile token pop		% Read generation number from file
              Generations 3 index	% Get specified generaton number
              lget 1 sub		% Gen numbs are stored with 1 added.
              eq { 			% Verify generation number
                PDFfile token pop
                /obj eq { % Verify 'obj' text
                  pop //false		% We have valid object, do not rebuild
                } if
              } if
            } if
          } ifelse
        } .internalstopped
        { //true } if			% If we stop then we need to rebuild
        % Stack:  <filesize> <obj num> <need rebuild flag>
        {
          (   **** Warning:  File has an invalid xref entry:  )
          pdfformaterror
          pdfstring cvs pdfformaterror
          (.  Rebuilding xref table.\n) pdfformaterror
          search_objects
          exit
        } if				% If the entry is invalid
      } {
              % The object is in an object stream.  We currently do not rebuild
              % objects in an object stream.  So If we find one, then abort the
              % verification of the xref table entries.
        pop exit			% Pop object number and then exit loop
      } ifelse				% If not in an object stream
    } if				% If object entry is not free
    pop					% Remove object number
  } for
  pop					% Remove the size of the file
} bind odef

/pdfopencache {		% - pdfopencache -
        % Create and initialize some caches.
  /PageCount pdfpagecount def
  /PageNumbers PageCount 65534 .min dict def
  /PageIndex PageCount 65534 .min array def
} bind def

/pdfopenfile {		% <file> pdfopenfile <dict>
   pdfdict readonly pop		% can't do it any earlier than this
   32 dict begin
   /LocalResources 0 dict def
   /DefaultQstate //null def	% establish binding
   /Printed where { pop } {
                % Guess whether the output device is a printer.
     /Printed currentpagedevice /OutputFile known def
   } ifelse
   /PSLevel1 where { pop } { /PSLevel1 //false def } ifelse
   % NB: PDFfile is used outside of the PDF code to determine that a
   % PDF job is being processed; to not change or hide this key.
   cvlit /PDFfile exch def
   /PDFsource PDFfile def
   /Repaired //false def
   /StreamRunAborted //false def
   /NeedAppearances //false def
   currentglobal //true .setglobal globaldict begin
   /UndefProcList 0 dict def
   end .setglobal
   PDFfile dup 0 setfileposition
   0 () /SubFileDecode filter   % to avoid file closure
   pdfstring readstring pop
   (%PDF-) search not {/pdfopen cvx /syntaxerror signalerror} if
   length /PDFoffset exch def pop
   % some badly formed PDF's (Visioneer) have something other than EOL
   % after the version number. If we get an error, shorten the string
   % and try again.
   //false exch		% error encountered
   { { cvr } stopped
     { exch pop //true exch 0 1 index length 1 sub dup 0 eq
       { pop 0 exit } if	% exit if string now empty
       getinterval		% trim character from right end and retry
     }
     { exch {
         (   **** Warning: PDF version number not followed by EOL.\n)
         pdfformaterror
       }
       if exit
     }
     ifelse
   } loop

   /Classic-xref //true def
   /PDFversion exch def
        % Read the last cross-reference table.
   count /pdfemptycount exch def
   /Trailer << >> def		% Initialize to an emptry dict.
   { initPDFobjects findxref readxref } .internalstopped {
     recover_xref_data		% Read failed.  Attempt to recover xref data.
     search_trailer		% Search for the primary trailer
   } {
     /Trailer exch def		% Save trailer dict after first xref table
        % Read any previous cross-reference tables.  When we are done,
        % verify that the entries in the xref tables are valid if NoVerifyXref
        % is not defined.
     Trailer
     { /Prev knownoget not {	% If no previous xref table then ...
         Classic-xref {
           /NoVerifyXref where { pop } { verify_xref } ifelse 
         } if  
         exit
       } if
       { readxref } .internalstopped {
         recover_xref_data	% Read failed.  Attempt to recover xref data.
         exit			% Exit loop since recover gets all obj data.
       } if  			% If readxref stopped
       % The PDF spec. says that each trailer dict should contain the required
       % entries.  However we have seen a PDF file that only has a Prev entry in
       % the initial trailer dict.  Acrobat complains but it accepts these files.
       % To work with these files, we are copying any entries which we find in
       % a previous trailer dict which are not present in the initial dict.
       dup {
         Trailer 2 index known {
           pop pop              % discard if key already present
         } {
           Trailer 3 1 roll put % add key if not present
         } ifelse
       } forall
     } loop			% Loop to previous trailer
   } ifelse			% Ifelse readxref stopped

   /NumObjects Objects llength def  % To check that obj# < NumObjects

   % Scan numbers in the range 2147483648..4294967295 in Encrypt dictionary
   % as unsigned integers for compatibility with Acrobat Reader. Bug 689010.
   << /PDFScanUnsigned //true >> setuserparams
   { Trailer /Encrypt knownoget {
       pop
       pdf_process_Encrypt	% signal error
     } if
   } stopped
   << /PDFScanUnsigned //false >> setuserparams
   { stop } if

   % Check for recursion in the page tree. Bug 689954, MOAB-06-01-2007
   verify_page_tree

   currentdict end
 } bind def

% Look for the last (startxref) from the current position
% of the file. Return the position after (startxref) if found or -1 .
/find-startxref {  % <file> find_eof <file> <position>
  -1
  { 1 index dup  0 (startxref) /SubFileDecode filter flushfile
    bytesavailable 9 lt { exit } if
    pop dup fileposition
  } loop
} bind def

% Search for the last 'startxfer' and read a following token, which
% must be a number. Don't pay any attention to %%EOF because it's
% often mangled.
% There seems to be no limit on the amount of garbage that can be
% appended to the PDF file. Current record (60K) belongs to
% PDF-Out (v 2.0 - 35). We start the search from the last 1024
% bytes and continue from the beginning of the file.
/findxref {		% - findxref <xrefpos>
  PDFfile dup dup dup 0 setfileposition bytesavailable
  dup /PDFfilelen exch def
        % Find the last %%EOF string (within 1024 bytes)
  1024 sub PDFoffset .max
  setfileposition find-startxref   % file pos|-1  % search the last 1024 bytes
  dup 0 le {
    pop
    dup PDFoffset setfileposition find-startxref  % search from the beginnibg
    dup 0 le {
       (   **** Error: Cannot find a 'startxref' anywhere in the file.\n)
       pdfformaterror
       /findxref cvx /syntaxerror signalerror
    } if
  } if
  2 copy setfileposition
  pop token not { //null } if
  dup type /integertype ne {
    (   **** Error: invalid token after startxref.\n)  pdfformaterror
        /findxref cvx /syntaxerror signalerror
  } if
} bind def

/stderrfile (%stderr) (w) file def

/stderrprint {                % <string> stderrprint -
  //stderrfile dup 3 -1 roll writestring flushfile
} bind def

/pdfformaterror {	% <string> pdfformaterror -
  stderrprint
  /Repaired //true store
} bind def

/knownoget_safe
{ 2 copy knownoget { 3 1 roll pop pop //true } { pop pop //false } ifelse
} odef

/printProducer {
  Trailer /Info { knownoget_safe } stopped { pop pop //false } if {
    /Producer knownoget not { //null } if
  } {
    //null
  } ifelse
  dup //null eq {
    pop
  } {
    (   **** The file was produced by: \n   **** >>>> ) stderrprint
        % Handle a Unicode Producer.
    (\376\377) anchorsearch {
      pop dup length 2 idiv string 0 1 2 index length 1 sub {
                % Stack: origstr newstr i
        1 index exch 3 index 1 index 2 mul 1 add get put
      } for exch pop
    } if
    stderrprint
    ( <<<<\n) stderrprint
  } ifelse
} bind def
% The UndefProcList collects noisy warnings.
% This gets rid of many multiple warnings from pdf_font.ps
/printCollectedWarnings {
   UndefProcList length 0 gt {
      (\n   **** Embedded font uses undefined procedure\(s\):  ) stderrprint
      UndefProcList {
         exch .namestring stderrprint ( ) stderrprint
         =string cvs stderrprint ( times, ) stderrprint
      } forall
      (\n) stderrprint
   } if
} bind def
/printrepaired {
   printCollectedWarnings
   (\n   **** This file had errors that were repaired or ignored.\n)
  stderrprint
  printProducer
  (   **** Please notify the author of the software that produced this\n)
  stderrprint
  (   **** file that it does not conform to Adobe's published PDF\n)
  stderrprint
  (   **** specification.\n\n)
  stderrprint
} bind def

% Write the outline structure for a file.  Uses linkdest (below).
% omit links to pages that don't exist.
/writeoutline		% <outlinedict> writeoutline -
 { mark
   0 2 index /First knownoget
    { { exch 1 add exch /Next knownoget not { exit } if } loop }
   if
                % stack: dict mark count
   dup 0 eq
    { pop 1 index }
    { 2 index /Count knownoget { 0 lt { neg } if } if
      /Count exch 3 index
    }
    ifelse
    {
      dup /A knownoget {
        dup /URI known {
          /A mark 3 2 roll    % <<>> /A [ <<action>>
          { oforce } forall
          .dicttomark
          3 2 roll
        } {
          dup /D knownoget {
            exch pop exch dup length dict copy dup /Dest 4 -1 roll put
          } {
            /N knownoget {                % Assume /S /Named
               namedactions exch .knownget { exec } if
             } if
          } ifelse
        } ifelse
      } if
      linkdest
    } stopped
    {
      cleartomark	% ignore this link
      (   **** Warning: Outline has invalid link that was discarded.\n)
      pdfformaterror
    } {
      /Title knownoget { 
        /Title exch /OUT pdfmark
      } {
        cleartomark
        (   **** Warning: Outline without /Title attribute was discarded.\n)
        pdfformaterror
      } ifelse
    }
   ifelse
   /First knownoget
    { { dup writeoutline /Next knownoget not { exit } if } loop }
   if
 } bind def

% Close a PDF file.
/pdfclose		% <dict> pdfclose -
 { begin
   PDFfile closefile
   end
 } bind def

% ======================== Page accessing ======================== %

% Get a (possibly inherited) attribute of a page.
/pget			% <pagedict> <key> pget <value> -true-
                        % <pagedict> <key> pget -false-
 {
   2 copy knownoget
    { exch pop exch pop //true }
    { exch /Parent knownoget
      { exch pget }
        % finally see if the key is (misplaced) in the Root Catalog dict
      { dup Trailer /Root oget exch knownoget dup {
          3 -1 roll (   **** Warning:  The /) pdfformaterror 50 string cvs pdfformaterror
          ( key is missing from the Page tree.\n) pdfformaterror
        }
        { exch pop }
        ifelse
      }
      ifelse
    }
   ifelse
 } bind def

% Get the value of a resource on a given page.
/rget {			% <resname> <pagedict> <restype> rget <value> -true-
                        % <resname> <pagedict> <restype> rget -false-
  LocalResources 1 index knownoget {
     3 index knownoget
  } {
    //false
  } ifelse {
    exch pop exch pop exch pop //true
  } {
    1 index /Resources pget {
      1 index  knownoget { 3 index knownoget } { //false } ifelse
    } {
      //false
    } ifelse {
      4 1 roll pop pop pop //true
    } {
      countdictstack array dictstack
      //false 5 1 roll
      dup length 1 sub -1 4 {       % false /Name <<>> /Type [<<gstate>>] i
        1 index exch get            % false /Name <<>> /Type [] <<gstate>>
        /FormResDict .knownget {    % false /Name <<>> /Type [] <<res>>
          2 index knownoget {       % false /Name <<>> /Type [] <<type_dict>>
            4 index knownoget {     % false /Name <<>> /type [] <<res_dict>>
              (   **** Warning: resource was found only in the external context.\n) pdfformaterror
              //true 7 2 roll       % <<res_dict>> true false /Name <<>> /Type []
              pop exit
            } if
          } if
        } if
      } for
      pop pop pop pop
    } ifelse
  } ifelse
} bind def

% Get the total number of pages in the document.
/pdfpagecount		% - pdfpagecount <int>
 { Trailer /Root knownoget {
    /Pages knownoget {
     dup /Count knownoget {
       dup type /integertype eq { dup 0 le } { //true } ifelse {
         pop
         dup /Kids knownoget {
           pop
           (   **** Warning:  Invalid Page count.\n) pdfformaterror
           % find the last page and use that as the Count
           1 1 999999999 {
             dup pdffindpage?
             exch pop
             //null eq { exit } { pop } ifelse
           } for
           1 sub		% decrement to last page that we were able to find
           2 copy /Count exch put
         } {
           0 % return 0 and keep 0 page count.
           (   **** Warning:  PDF document has no pages.\n) pdfformaterror
         } ifelse
       } if
       exch pop
     } {
         dup /Type oget /Page eq {
           << exch 1 array astore /Kids exch /Count 1 /Type /Pages >>
           Trailer /Root oget /Pages 3 -1 roll put
           1
           (   **** Warning:  No /Pages node. The document root directly point a page.\n)
           pdfformaterror
       } {
         (   **** Warning:  Page count not found; assuming 1.\n)
         pdfformaterror
         pop 1
       } ifelse
     } ifelse
     } {
     0
     }ifelse
  }{
   0
  } ifelse
 } bind def

% Check for loops in the 'page tree' but accept an acyclic graph.
% - verify_page_tree -
/verify_page_tree {
  Trailer /Root knownoget {
    /Pages knownoget {
      10 dict begin
      /verify_page_tree_recursive {
        dup 1 def
        dup /Kids knownoget {
          { oforce
            dup //null ne {
              currentdict 1 index known {
                (   **** Error: there's a loop in the page tree. Giving up.\n) pdfformaterror
                /verify_page_tree cvx /syntaxerror signalerror
              } if
              verify_page_tree_recursive
            } {
              pop
            } ifelse
          } forall
        } if
        currentdict exch undef
      } def
      verify_page_tree_recursive
      end
    } if
  } if
} bind def

/pdffindpage? {		% <int> pdffindpage? 1 null 	(page not found)
                        %  <int> pdffindpage? 1 noderef (page found)
                        %  <int> pdffindpage? 0 null	(Error: page not found)
  Trailer /Root oget /Pages get
    {		% We should be able to tell when we reach a leaf
                % by finding a Type unequal to /Pages.  Unfortunately,
                % some files distributed by Adobe lack the Type key
                % in some of the Pages nodes!  Instead, we check for Kids.
      dup oforce /Kids knownoget not { exit } if
      exch pop //null
      0 1 3 index length 1 sub {
         2 index exch get
         dup oforce dup /Kids known { /Count oget } { pop 1 } ifelse
                % Stack: index kids null noderef count
         dup 5 index ge { pop exch pop exit } if
         5 -1 roll exch sub 4 1 roll pop
      } for exch pop
                % Stack: index null|noderef
      dup //null eq { pop pop 1 //null exit } if
    } loop
} bind def

% Find the N'th page of the document by iterating through the Pages tree.
% The first page is numbered 1.
/pdffindpageref {		% <int> pdffindpage <objref>
  dup pdffindpage?
                % Stack: index countleft noderef
   1 index 1 ne { pop pop /pdffindpage cvx /rangecheck signalerror } if
   exch pop
   PageIndex 2 index 1 sub 65533 .min 2 index oforce put
   PageNumbers 1 index oforce 3 index dup 65534 le
    { put }
    { pop pop pop }	% don't store more than 65534 pagenumbers
   ifelse
   exch pop
} bind def
/pdffindpage {		% <int> pdffindpage <pagedict>
  pdffindpageref oforce
} bind def

% Find the N'th page of the document.
% The first page is numbered 1.
/pdfgetpage		% <int> pdfgetpage <pagedict>
 { PageIndex 1 index 1 sub dup 65533 lt
    { get }
    { pop pop //null }
   ifelse
   dup //null ne
    { exch pop oforce }
    { pop pdffindpage }
   ifelse
 } bind def

% Find the page number of a page object (inverse of pdfgetpage).
/pdfpagenumber		% <pagedict> pdfpagenumber <int>
 {	% We use the simplest and stupidest of all possible algorithms....
   PageNumbers 1 index .knownget
    { exch pop
    }
    { 1 1 PageCount 1 add	% will give a rangecheck if not found
       { dup pdfgetpage oforce 2 index eq { exit } if pop
       }
      for exch pop
    }
   ifelse
 } bind def

% Arrange the four elements that define a rectangle into a 'normal' order.
/normrect_elems   % <x1> <y1> <x2> <y2> normrect_elems <llx> <lly> <urx> <ury>
{
    4 { % Bug 693919
      dup type dup /integertype ne exch /realtype ne and {
         (   **** Warning: replacing malformed number ') pdfformaterror pdfstring cvs pdfformaterror
         (' with 0.\n) pdfformaterror
         0
      } if
      4 1 roll
    } repeat
    exch 4 1 roll			% <x2> <x1> <y1> <y2>
    2 copy gt { exch } if		% <x2> <x1> <lly> <ury>
    4 2 roll 2 copy lt { exch } if	% <lly> <ury> <urx> <llx>
    4 1 roll exch			% <llx> <lly> <urx> <ury>
} bind def

% Arrange a rectangle into a 'normal' order.  I.e the lower left corner
% followed by the upper right corner.
/normrect 	% <rect> normrect <rect>
{
    aload pop normrect_elems 4 array astore
} bind def

/fix_empty_rect_elems % </Name> <x1> <y1> <x2> <y2> fix_empty_rect_elems <x1> <y1> <x2'> <y2'>
{  dup 3 index eq { //true } { 1 index 4 index eq } ifelse {
     pop pop pop pop
     (   **** Warning:  File has an empty ) pdfformaterror pdfstring cvs pdfformaterror
     (. Using the current page size instead.\n) pdfformaterror
     0 0 currentpagedevice /PageSize get aload pop
   } {
     5 -1 roll pop
   } ifelse
} bind def

/boxrect		% <llx> <lly> <urx> <ury> boxrect <x> <y> <w> <h>
 { exch 3 index sub exch 2 index sub
 } bind def
/resolvedest {		% <name|string|other> resolvedest <other|null>
  dup type /nametype eq {
    Trailer /Root oget /Dests knownoget {
      exch knownoget not { //null } if
    } {
      pop //null
    } ifelse
  } {
    dup type /stringtype eq {
      Trailer /Root oget /Names knownoget {
        /Dests knownoget {
          exch nameoget
        } {
          pop //null
        } ifelse
      } {
        pop //null
      } ifelse
    } if
  } ifelse
} bind def

% Procedures to do the necessary transformations of view destinations
% <PDF2PS_matrix> <rot> <view> -- <view'>
/viewdestprocs 8 dict dup begin
    /Fit  { exch pop exch pop } bind def
    /FitH {
        aload pop
        0 4 -1 roll 1 and 0 eq { exch } if
        4 -1 roll transform exch pop
        2 array astore
    } bind def
    /FitV {
        aload pop
        0 4 -1 roll 1 and 0 ne { exch } if
        4 -1 roll transform pop
        2 array astore
    } bind def
    /FitB  /Fit  load def
    /FitBH /FitH load def
    /FitBV /FitV load def
    /XYZ  {
        aload pop
        3 1 roll
        2 copy 7 -1 roll 1 and 0 ne { exch } if	4 2 roll    % odd rotation switches x<->y
        2 { dup //null eq { pop 0 } if exch } repeat	    % replace nulls with 0
        7 -1 roll transform				    % transform coordinates
        2 { 3 -1 roll //null eq { pop //null } if exch } repeat % put the nulls back
        3 -1 roll
        4 array astore
    } bind def
    /FitR {
        exch pop
        aload pop
        2 { 5 index transform 4 2 roll } repeat normrect_elems
        5 array astore
        exch pop
    } bind def
end readonly def

/linkdest {		% <link|outline> linkdest
                        %   ([/Page <n>] /View <view> | ) <link|outline>
  dup /Dest knownoget
    { resolvedest
      dup type /dicttype eq { /D knownoget not { //null } if } if
      dup //null eq
       { pop }
       { dup 0 oget
         //false % don't have a page# and transformation matrix (yet)
         1 index type /dicttype eq {
           1 index /Type knownoget {
             /Page eq {
               pop % the "false" flag
               dup pdf_cached_PDF2PS_matrix exch
               dup /Rotate pget not { 0 } if 90 idiv exch
               pdfpagenumber CumulativePageCount add
               //true % now we have a page# and a transformation matrix
             } if
           } if
         } if
         % stack: <link|outline> <dest>	( <PDF2PS_matrix> <rot>	<page#>	true  |	 <page> false )
         {
           /Page exch 6	2 roll
           % stack: [/Page <page#>] <link|outline> <dest> <PDF2PS_matrix> <rot>
           3 -1 roll dup length 1	sub 1 exch getinterval /View 4 1 roll
           % stack: [/Page <page#>] <link|outline> /View <PDF2PS_matrix> <rot> <view>
           //viewdestprocs 1 index 0 get get exec
           3 -1 roll
         } {
           dup type /integertype eq {
             CumulativePageCount add
             /Page exch 1 add 4 2 roll
           } {
             pop
           } ifelse
           dup length 1 sub 1 exch getinterval /View exch 3 -1 roll
         } ifelse
       }
      ifelse
    }
   if
} bind def
% <pagedict> mark ... -proc- <page#> <error>
/namedactions 8 dict dup begin
  /FirstPage { 1 CumulativePageCount add //false } def
  /LastPage { pdfpagecount CumulativePageCount add //false } def
  /NextPage { counttomark 2 add index pdfpagenumber 1 add dup pdfpagecount gt exch CumulativePageCount add exch} bind def
  /PrevPage { counttomark 2 add index pdfpagenumber 1 sub dup 1 lt exch CumulativePageCount add exch} bind def
end readonly def
% <pagedict> <annotdict> -proc- -
/annottypes 5 dict dup begin
  /Text {
    mark exch
     { /Rect /Open /Contents /F}
     { 2 copy knownoget { 3 -1 roll } { pop } ifelse }
    forall pop /ANN pdfmark
  } bind def
  /Link {
    mark exch
    dup /BS knownoget { << exch { oforce } forall >> /BS exch 3 -1 roll } if
    dup /F knownoget { /F exch 3 -1 roll } if
    dup /C knownoget { /Color exch 3 -1 roll } if
    dup /Rect knownoget { /Rect exch 3 -1 roll } if
    dup /Border knownoget {
      dup type /arraytype eq {
        dup length 3 lt
      } {
        //true
      } ifelse {
        pop [ 0 0 0 ] % Following AR5 use invisible border.
      } if
      /Border exch 3 -1 roll
    } if
    dup /A knownoget {
      dup /URI known {
        /A mark 3 2 roll    % <<>> /A [ <<action>>
        { oforce } forall
        .dicttomark
        3 2 roll
      } {
        dup /D knownoget {
          exch pop exch dup length dict copy dup /Dest 4 -1 roll put
        } {
          /N knownoget {		% Assume /S /Named
             namedactions exch .knownget {
               exec {
                 pop
                 (   **** Warning: Ignoring a named action pointing out of the document page range.\n)
                 pdfformaterror
               } {
                 /Page exch 3 -1 roll
               } ifelse
             } if
          } if
        } ifelse
      } ifelse
    } if
    { linkdest } stopped {
      cleartomark
      (   **** Warning: Link annotation points out of the document page range.\n)
      pdfformaterror
    } {
      pop /LNK pdfmark
    } ifelse
  } bind def
end readonly def

% **** The following procedure should not be changed to allow clients
% **** to directly interface with the constituent procedures. GSview
% **** and some Artifex customers rely on the pdfshowpage_init,
% **** pdfshowpage_setpage, pdfshowpage_finish so all logic should be
% **** implemented in one of those three procedures.
/pdfshowpage		% <pagedict> pdfshowpage -
 { dup /Page exch store
   pdfshowpage_init     % <pagedict>
   pdfshowpage_setpage  % <pagedict>
   pdfshowpage_finish   % -
 } bind def

/pdfpagecontents	% <pagedict> pdfpagecontents <contents>
 { } bind def

/pdfshowpage_init 	% <pagedict> pdfshowpage_init <pagedict>
 { /DSCPageCount DSCPageCount 1 add store
 } bind def

/get_media_box { % <pagedict> get_media_box <box> <bool>
  /MediaBox pget {
    oforce_array //true
  } {
    (   **** Page has no /MediaBox attribute. Using the current page size.\n)
    pdfformaterror
    [ 0 0 currentpagedevice /PageSize get aload pop ] //false
  } ifelse
} bind def

/get_any_box { % <pagedict> get_any_box <box name> <box>
  //systemdict /UseTrimBox .knownget dup { and } if {
    dup /TrimBox pget {
      /TrimBox exch
    } if
  } if
  dup type /arraytype ne {
    //systemdict /UseArtBox .knownget dup { and } if {
      dup /ArtBox pget {
        /ArtBox exch
      } if
    } if
  } if
  dup type /arraytype ne {
    //systemdict /UseCropBox .knownget dup { and } if {
      dup /CropBox pget {
        /CropBox exch
      } if
    } if
  } if
  dup type /arraytype ne {
    /MediaBox exch get_media_box pop
  } {
    oforce_elems              % <<pdict>> /SomeBox x0 y0 x1 y1
    6 -1 roll get_media_box { % /SomeBox x0 y0 x1 y1 [MediaBox]
      aload pop               % /SomeBox x0 y0 x1 y1 X0 Y0 X1 Y1
      5 -1 roll .min exch     % /SomeBox x0 y0 x1 X0 Y0 mY1 X1
      5 -1 roll .min exch     % /SomeBox x0 y0 X0 Y0 mX1 mY1
      4 2 roll                % /SomeBox x0 y0 mX1 mY1 X0 Y0
      5 -1 roll .max exch     % /SomeBox x0 mX1 mY1 mY0 X0
      5 -1 roll .max exch     % /SomeBox mX1 mY1 mX0 mY0
      4 2 roll                % /SomeBox mX0 mY0 mX1 mY1
    } {
      pop
    } ifelse
    4 array astore            % /SomeBox [mX0 mY0 mX1 mY1]
  } ifelse
} bind def

% Compute the matrix that transforms the PDF->PS "default" user space
/pdf_PDF2PS_matrix {	% <pdfpagedict> -- matrix
  matrix currentmatrix matrix setmatrix exch
  % stack: savedCTM <pdfpagedict>
  dup get_any_box
  % stack: savedCTM <pdfpagedict> /Trim|Crop|Art|MediaBox <Trim|Crop|Art|Media Box>
  oforce_elems normrect_elems fix_empty_rect_elems 4 array astore
  //systemdict /PDFFitPage known {
    PDFDEBUG { (Fiting PDF to imageable area of the page.) = flush } if
    currentpagedevice /.HWMargins get aload pop
    currentpagedevice /PageSize get aload pop
    % Adjust PageSize and .HWMargins for the page portrait/landscape orientation
    2 copy gt	% PageSize_is_landscape
    7 index aload pop 3 -1 roll sub 3 1 roll exch sub exch
    10 index /Rotate pget not { 0 } if 90 idiv 1 and 0 ne { exch } if
    gt	% Box_is_landscape
    ne {
      1 index 0 translate 90 rotate	% add in a rotation
      2 copy ne {
        % rotate the .HWMargins
        2 copy lt {
          6 2 roll 4 -1 roll 6 -2 roll
        } {
          6 2 roll 4  1 roll 6 -2 roll
        } ifelse
        % rotate the page dimensions
        exch
      } if
    } if
    3 -1 roll sub 3 1 roll exch sub exch
    % stack: savedCTM <pdfpagedict> <Crop|Media Box> Xmin Ymin Xmax Ymax
    PDFDEBUG { (    Translate up by [ ) print 3 index =print (, ) print 2 index =print ( ]) = flush } if
    3 index 3 index translate		% move origin up to imageable area
    2 index sub exch 3 index sub exch 4 2 roll pop pop
            % stack: savedCTM <pdfpagedict> [Box] XImageable YImageable
    2 index aload pop 2 index sub exch 3 index sub exch 4 2 roll pop pop
    5 index /Rotate pget not { 0 } if 90 idiv 1 and 0 ne { exch } if
            % stack: savedCTM <pdfpagedict> [Box] XImageable YImageable XBox YBox
    4 copy
    3 -1 roll exch div 3 1 roll div .min
            % stack: savedCTM <pdfpagedict> [Box] XImageable YImageable XBox YBox scale
    PDFDEBUG { (    Scale by ) print dup = flush } if
    5 -4 roll
            % stack: savedCTM <pdfpagedict> [Box] scale XImageable YImageable XBox YBox
    3 index 2 index 6 index mul sub 2 div 3 index 2 index 7 index mul sub 2 div
    PDFDEBUG { (    Centering translate by [ ) print 1 index =print (, ) print dup =print ( ]) = flush } if
    translate pop pop pop pop
  } {
    //systemdict /NoUserUnit .knownget not { //false } if {
      1
    } {
      1 index /UserUnit knownoget {
        PDFDEBUG { (Scaling due to UserUnit by ) print dup = flush } if
      } {
        1
      } ifelse
    } ifelse
  } ifelse
  % stack: savedCTM <pdfpagedict> [Box] scale
  dup scale
  % Rotate according to /Rotate
  aload pop boxrect
  {
    {     pop pop }
    { -90 rotate pop neg 0 translate }
    { 180 rotate neg exch neg exch translate }
    {  90 rotate neg 0 exch translate pop }
  }
  5 index /Rotate pget not { 0 } if
  PDFDEBUG { dup 0 ne { (Rotating by ) print dup =print ( degrees.) = flush } if } if
  90 idiv 3 and get exec
  % Now translate to the origin given in the Crop|Media Box
  exch neg exch neg translate
  % stack: savedCTM <pdfpagedict>
  pop
  matrix currentmatrix exch setmatrix
} bind def

% Cache the matrix that transforms the PDF->PS "default" user space
% into <pdfpagedict> under the key //PDF2PS_matrix_key, then return it
/PDF2PS_matrix_key (PDF->PS matrix) cvn def
/pdf_cached_PDF2PS_matrix {  % <pdfpagedict> -- <PDF2PS_matrix>
  dup //PDF2PS_matrix_key .knownget {
    exch pop
  } {
    dup dup pdf_PDF2PS_matrix //PDF2PS_matrix_key exch put
    //PDF2PS_matrix_key get
  } ifelse
} bind def
currentdict /PDF2PS_matrix_key undef

/.pdfshowpage_Install {	% <pagedict> [<prevproc>] .pdfshowpage_Install -
  0 get exec
  pdf_cached_PDF2PS_matrix concat
} bind def

/pdfshowpage_setpage {	% <pagedict> pdfshowpage_setpage <pagedict>
  6 dict begin		% for setpagedevice
        % Stack: pdfpagedict
  % UseCIEColor is always true for PDF; see the comment in runpdf above
  /UseCIEColor //true def
  /Orientation 0 def
  currentpagedevice
        % Stack: pdfpagedict currentpagedevicedict
  1 index get_any_box
        % Stack: pdfpagedict currentpagedevicedict /BoxName [box]
  oforce_elems normrect_elems fix_empty_rect_elems boxrect 4 2 roll pop pop
  3 index /Rotate pget not { 0 } if 90 idiv 1 and 0 ne { exch } if
  % stack: pdfpagedict currentpagedevicedict boxwidth boxheight
  //systemdict /PDFFitPage known {
    % Preserve page size,
    % but choose portrait/landscape depending on box width:height ratio
    % (if box width == height, select portrait orientation)
    gt
    1 index /PageSize get aload pop
    2 copy gt
    4 -1 roll ne { exch } if
  } {
    % Set the page size.
    //systemdict /NoUserUnit .knownget not { //false } if not {
      3 index /UserUnit knownoget {
        dup 4 -1 roll mul 3 1 roll mul
      } if
    } if
  } ifelse
  2 array astore /PageSize exch def
  % Determine the number of spot colors used on the page.  Note: This searches
  % the pages resources.  It may be high if a spot color is in a resource but
  % is not actually used on the page.
  /PageSpotColors 2 index countspotcolors def

  % Let the device know if we will be using PDF 1.4 transparency.
  % The clist logic may need to adjust the size of bands.
  1 index pageusestransparency /PageUsesTransparency exch def
  dup /Install .knownget {
                        % Don't let the Install procedure get more deeply
                        % nested after every page.
      dup type dup /arraytype eq exch /packedarraytype eq or {
        dup length 4 eq {
          dup 2 get /.pdfshowpage_Install load eq {
            1 get 0 get	% previous procedure
          } if
        } if
      } if
  } {
    { }
  } ifelse 1 array astore
  2 index exch /.pdfshowpage_Install load /exec load
  4 packedarray cvx
        % Stack: pagedict currentpagedict installproc
  /Install exch def
        % Stack: pagedict currentpagedict
  pop currentdict end setpagedevice
} bind def

/.free_page_resources {   % - .free_page_resources -
  Page /Resources pget {
    /Shading knownoget {
      { dup type /dicttype eq {
          dup /.shading_dict known {
            dup /.shading_dict undef
          } if
        } if
        pop pop
      } forall
    } if
  } if
} bind def

/pdfshowpage_finish {	% <pagedict> pdfshowpage_finish -
   save /PDFSave exch store
   /PDFdictstackcount countdictstack store
   /PDFexecstackcount count 2 sub store
   (before exec) VMDEBUG

   % set up color space substitution (this must be inside the page save)
   pdfshowpage_setcspacesub

  .writepdfmarks {

        % Copy the boxes.
    { /CropBox /BleedBox /TrimBox /ArtBox } {
      2 copy pget {
        % .pdfshowpage_Install transforms the user space do same here with the boxes
        oforce_elems
        2 { Page pdf_cached_PDF2PS_matrix transform 4 2 roll } repeat
        normrect_elems 4 index 5 1 roll fix_empty_rect_elems 4 array astore
        mark 3 1 roll /PAGE pdfmark
      } {
        pop
      } ifelse
    } forall

        % Copy annotations and links.
    dup /Annots knownoget {
      0 1 2 index length 1 sub
       { 1 index exch oget
         dup type /dicttype eq {
           dup /Subtype oget annottypes exch .knownget { exec } { pop } ifelse
         } {
           pop
         } ifelse
       }
      for pop
    } if

  } if		% end .writepdfmarks

        % Display the actual page contents.
   8 dict begin
   /BXlevel 0 def
   /BMClevel 0 def
   /OFFlevels 0 dict def
   /BGDefault currentblackgeneration def
   /UCRDefault currentundercolorremoval def
        %****** DOESN'T HANDLE COLOR TRANSFER YET ******
   /TRDefault currenttransfer def
  matrix currentmatrix 2 dict
  2 index /CropBox pget {
    oforce_elems normrect_elems boxrect
    4 array astore 1 index /ClipRect 3 -1 roll put
  } if
  dictbeginpage setmatrix
  /DefaultQstate qstate store

  count 1 sub /pdfemptycount exch store
        % If the page uses any transparency features, show it within
        % a transparency group.
  dup pageusestransparency dup /PDFusingtransparency exch def {
    % Show the page within a PDF 1.4 device filter.
    0 .pushpdf14devicefilter {
      /DefaultQstate qstate store		% device has changed -- reset DefaultQstate
      % If the page has a Group, enclose contents in transparency group.
      % (Adobe Tech Note 5407, sec 9.2)
      dup /Group knownoget {
        1 index /CropBox pget {
          /CropBox exch
        } {
          1 index get_media_box pop /MediaBox exch
        } ifelse
        oforce_elems normrect_elems fix_empty_rect_elems 4 array astore .beginformgroup 
        showpagecontents
        .endtransparencygroup
      } {
        showpagecontents
      } ifelse
    } stopped {
      % todo: discard
      .poppdf14devicefilter
      /DefaultQstate qstate store	% device has changed -- reset DefaultQstate
      stop
    } if .poppdf14devicefilter
    /DefaultQstate qstate store	% device has changed -- reset DefaultQstate
  } {
    showpagecontents
  } ifelse
  .free_page_resources
  % todo: mixing drawing ops outside the device filter could cause
  % problems, for example with the pnga device.
  endpage
  end			% scratch dict
  % Some PDF files don't have matching q/Q (gsave/grestore) so we need
  % to clean up any left over dicts from the dictstack
  
  PDFdictstackcount //false
  { countdictstack 2 index le { exit } if
    currentdict /n known not or
    end
  } loop {
    StreamRunAborted not {
      (   **** Warning: File has unbalanced q/Q operators \(too many q's\)\n)
      pdfformaterror
    } if
  } if
  pop
  count PDFexecstackcount sub { pop } repeat
  (after exec) VMDEBUG
  Repaired		% pass Repaired state around the restore
  PDFSave restore
  /Repaired exch def
} bind def

% Display the contents of a page (including annotations).
/showpagecontents {	% <pagedict> showpagecontents -
  dup		% Save the pagedict for the Annotations
  count 1 sub  /pdfemptycount exch store
  gsave		% preserve gstate for Annotations later
  /Contents knownoget not { 0 array } if
  dup type /arraytype ne { 1 array astore } if {
    oforce //false resolvestream pdfopdict .pdfrun
  } forall
  % check for extra garbage on the ostack and clean it up
  count pdfemptycount sub dup 0 ne {
    (   **** File did not complete the page properly and may be damaged.\n)
    pdfformaterror
    { pop } repeat
  } {
    pop
  } ifelse
  grestore
  % Draw the annotations
  //systemdict /ShowAnnots .knownget not { //true } if {
    /Annots knownoget {
      { oforce
        dup //null ne {
          drawannot
        } {
          pop
        } ifelse
      } forall
    } if
  } if
  //systemdict /ShowAcroForm .knownget { //true eq } { //false } ifelse {
    Trailer /Root oget /AcroForm knownoget { draw_acro_form } if
  } if
} bind def

/processcolorspace {	% - processcolorspace <colorspace>
        % The following is per the PLRM3.
  currentdevice 1 dict dup /ProcessColorModel dup put .getdeviceparams
  exch pop exch pop
  dup type /nametype ne { cvn } if
  dup { setcolorspace } .internalstopped { pop /DeviceRGB } if
} bind def

% ------ Transparency support ------ %

% Determine whether a page might invoke any transparency features:
%	- Non-default BM, ca, CA, or SMask in an ExtGState
%	- Image XObject with SMask
% Note: we deliberately don't check to see whether a Group is defined,
% because Adobe Illustrator 10 (and possibly other applications) define
% a page-level group whether transparency is actually used or not.
% Ignoring the presence of Group is justified because, in the absence
% of any other transparency features, they have no effect.
%
% Transparency is a 1.4 feature however we have seen files that claimed
% to be PDF 1.1 with transparency features. Bugs 689288, 691273.
/pageusestransparency {		% <pagedict> pageusestransparency <bool>
  NOTRANSPARENCY {
    pop //false
  } {
    dup //false exch {
      4 dict 1 index resourceusestransparency { pop not exit } if
      /Parent knownoget not { exit } if
    } loop
    % Also check for transparency in the annotation (if not in resources).
    { pop //true } { annotsusetransparency } ifelse
  } ifelse
} bind def

% Check if transparency is specified in an ExtGState dict
/extgstateusestransparency {	% <gstate dict> extgstateusestransparency <bool>
  //false exch		% Assume no transparency
  dup //null eq {
    pop % bug 692050
  } {
    {			% establish loop context
      dup /BM knownoget {
        dup /Normal ne exch /Compatible ne and { pop not exit } if
      } if
      dup /ca knownoget { 1 ne { pop not exit } if } if
      dup /CA knownoget { 1 ne { pop not exit } if } if
      dup /SMask knownoget { /None ne { pop not exit } if } if
      pop exit
    } loop
  } ifelse
} bind def

% Check if transparency is used in a Pattern
/patternusestransparency {	% <Pattern dict> patternusestransparency <bool>
  dup //null eq NOTRANSPARENCY or
  { pop //false }
  { //false exch		% Assume no transparency
    {
      4 dict 1 index resourceusestransparency { pop not exit } if
      dup /ExtGState knownoget { extgstateusestransparency { pop not exit } if } if
      pop exit
    } loop
  }
  ifelse
} bind def

% Check the Resources of a page or Form. Check for loops in the resource chain.
/resourceusestransparency {	% <dict> <dict> resourceusestransparency <bool>
  {	% Use loop to provide an exitable context.
    /Resources knownoget not { 0 dict } if
    2 copy .knownget {
      { % Some circular references may be missed because scanning stops
        % when the 1st transparency is found.
        (   **** File has circular references in resource dictionaries.\n)
        pdfformaterror
      } if
      pop //false exit
    } if
    2 copy //true put              % In the current chain.
    dup /ExtGState knownoget {
      //false exch
      { exch pop oforce extgstateusestransparency { pop //true exit } if
      } forall
      { pop //true exit } if
    } if
    dup /Pattern knownoget {
      //false exch
      { exch pop oforce patternusestransparency { pop //true exit } if
      } forall
      { pop //true exit } if
    } if
    dup /XObject knownoget {
      dup type /dicttype eq {
        //false exch
        {
          exch pop oforce 
          dup //null ne {
            dup /Subtype get
            dup /Image eq {
              1 index /SMask oknown { pop pop not exit } if
              1 index /SMaskInData knownoget {
                0 ne { pop pop not exit } if
              } if
            } if
            /Form eq {
              3 index exch resourceusestransparency { not exit } if
            } {
              pop
            } ifelse
          } {
            pop
          } ifelse
        } forall
        { pop //true exit } if
      } {
        (   **** Ignoring non-dictionary /XObject attribute.\n)
        pdfformaterror
        pop
      } ifelse
    } if
    2 copy //false put             % Visited but not in the current chain.
    pop //false exit
  } loop
  exch pop
} bind def

% Check if the annotations on a page use transparency
/annotsusetransparency {	% <page dict> annotsusetransparency <bool>
   //false exch			% Assume no transparency
   /Annots knownoget {		% Get Annots array
     {
       oforce
       dup //null ne {
         dup /Subtype knownoget {
           /Highlight eq {      % Highlight annotation is always implemented
             pop pop //true exit  % as transparency.
           } if
         } if
         /AP knownoget {	% Get appearance dict for the annoation
           /N knownogetdict { 	% Get the /N (i.e. normal) appearance stream
             4 dict exch resourceusestransparency { pop //true exit } if
           } if
         } if  			% If AP dict known
       } {
         pop
       } ifelse
     } forall			% For all annots on the page
   } if
} bind def

% Add a color name to our spot color list.  Ignore /All and /None
/putspotcolor {			% <name> <spotcolordict> putspotcolor -
  % The 'name' could be a string.  If so then convert to a name.
  exch dup type /stringtype eq { cvn } if
  dup dup /None eq exch /All eq or { pop pop } { 0 put } ifelse
} bind def

% Determine which spot colors are used within a color space  Note: This
% dict will include all colors used in Separation or DeviceN color spaces.
% Thus it may include Cyan, Magenta, Yellow, and Black.
%	<colorspace> <spotcolordict> colorspacespotcolors -
/colorspacespotcolors {
  exch dup type /arraytype eq {
    % If we have an Indexed color space then get the base space.
    dup 0 oget                    % <<>> [csp] /Type
    dup /Indexed eq {
      pop 1 oget                  % <<>> [base]
      2 copy exch colorspacespotcolors
    } {
      % Stack:  <spotcolordict> <colorspace> <colorspacetype>
      dup /Separation eq exch /DeviceN eq or {
        dup 1 oget dup type /arraytype eq {
          { oforce 2 index putspotcolor } forall
        } {
          2 index putspotcolor
        } ifelse
      } if
    } ifelse
  } if
  pop pop
} bind def

% Check the Resources of a page, form, or annotation.  Determine which spot
% colors are used within the resource  Note: The spot color dict will include
% all colors used in Separation or DeviceN color spaces.  Thus it may include
% Cyan, Magenta, Yellow, and Black.  We also pass a dict that is used to check
% for loops in the resource list.
%	<spotcolordict> <loopdict> <page/form/annot dict>
% 		 resourcespotcolors <spotcolordict> <loopdict>
/resourcespotcolors {
  {	% Use loop to provide an exitable context.
    % Exit if no Resources entry
    /Resources knownoget not { exit } if
    % Exit if we have already seen this dict
    2 copy known { pop exit } if

    % Save the Resources dict into our loop checking dict.
    2 copy 0 put

    % Scan resources that might contain a color space.
    dup /ColorSpace knownoget {
      { exch pop oforce 3 index colorspacespotcolors } forall
    } if
    dup /Pattern knownoget {
      { exch pop oforce 
        dup /Shading knownoget {
          exch pop
          /ColorSpace oget 3 index colorspacespotcolors
        } {
          4 copy exch pop resourcespotcolors pop pop pop 
        } ifelse
      } forall
    } if
    dup /Shading knownoget {
      { exch pop oforce /ColorSpace oget 3 index colorspacespotcolors } forall
    } if
    /XObject knownoget {
      dup type /dicttype eq {
        { exch pop oforce
          dup //null ne {
            dup /Subtype oget 
            dup /Form eq { 
              pop resourcespotcolors 
            } { 
              /Image eq {
                /ColorSpace knownoget {
                  2 index colorspacespotcolors
                } if
              } {
                pop
              } ifelse 
            } ifelse
          } {
            pop
          } ifelse
        } forall
      } {
        pop % Just ignore here, already reported by resourceusestransparency.
      } ifelse
    } if
    exit
  } loop
} bind def

% Determine which spot colors are used within the annotations.  Note: This
% dict will include all colors used in Separation or DeviceN color spaces.
% Thus it may include Cyan, Magenta, Yellow, and Black.
%	<spotcolordict> <loopdict> <annotsarray>
% 		 annotsspotcolors <spotcolordict> <loopdict>
/annotsspotcolors {
  { oforce
    dup //null ne {
      /AP knownoget {	% Get appearance dict for the annoation
        /N knownogetdict {		% Get the /N (i.e. normal) appearance stream
          resourcespotcolors
        } if			% If normal appearance streamknown
      } if			% If AP dict known
    } {
      pop
    } ifelse
 } forall
} bind def

% Determine spot colors are used within a page.  We are creating a dict to
% hold the spot color names as keys.  Using a dict avoids having to worry
% about duplicate entries.  The keys in the dict contain the spot color
% names.  However the values have no meaning.  Note: This dict will include
% all colors used in Separation or DeviceN color spaces specified in the
% page's resources.  Thus it may include Cyan, Magenta, Yellow, and Black.
% There is no attempt to verify that these color spaces are actually used
% within the object streams for the page.
/pagespotcolors {		% <pagedict> pagespotcolors <spotcolordict>
  dup
  % Create a dict to hold spot color names.
  0 dict exch
  % Create a dict to be used to check for reference loops.
  4 dict exch
  % Check for color spaces in the Resources  
  resourcespotcolors
  % now check upwards in the object hierarchy in case of inherited resources
  2 index
  {
    /Parent knownoget
    {
      dup 4 1 roll
      resourcespotcolors
      3 -1 roll
    }
    {
      exit
    } ifelse
  } loop
  % Also check for color spaces in the annotations.
  3 -1 roll
  /Annots knownoget { annotsspotcolors } if
  pop				% Discard reference loop dict
} bind def

% Determine how many (if any) spot colors are used by a page.
% Note:  This count does not include Cyan, Magenta, Yellow, or Black
/countspotcolors {		% <pagedict> countspotcolors <count>
  pagespotcolors		% Get dict with all spot colors
  dup length			% spot color dict length
  % Remove CMYK from the spot color count.
  [ /Cyan /Magenta /Yellow /Black ]
  { 2 index exch known { 1 sub } if } forall
  exch pop			% Remove spot color dict
} bind def

% ------ ColorSpace substitution support ------ %

%
%  <pagedict>   pdfshowpage_setcspacesub   <pagedict>
%
% Set up color space substitution for a page. Invocations of this procedure
% must be bracketed by the save/restore operation for the page, to avoid
% unintended effects on other pages.
%
% If any color space substitution is used, and the current color space is a
% device dependent color space, make sure the current color space is updated.
% There is an optimization in the setcolorspace operator that does
% nothing if both the current and operand color spaces are the same. For
% PostScript this optimization is disabled if the UseCIEColor page device
% parameter is true. This is not the case for PDF, as performance suffers
% significantly on some PDF files if color spaces are set repeatedly. Hence,
% if color space substitution is to be used, and the current color space
% is a device dependent color space, we must make sure to "transition" the
% current color space.
%
/pdfshowpage_setcspacesub  {
  //false
  /DefaultGray 2 index /ColorSpace //rget exec {
    resolvecolorspace dup csncomp 1 eq {
      dup type /nametype eq { 1 array astore } if
      /DefaultGray exch /ColorSpace defineresource pop
      pop //true
    } {
      pop 
      (   **** Warning: ignoring invalid /DefaultGray color space.\n)
      pdfformaterror
    } ifelse
  } if

  /DefaultRGB 2 index /ColorSpace //rget exec {
    resolvecolorspace dup csncomp 3 eq {
      dup type /nametype eq { 1 array astore } if
      /DefaultRGB exch /ColorSpace defineresource pop
      pop //true
    } {
      pop
      (   **** Warning: ignoring invalid /DefaultRGB color space.\n)
      pdfformaterror
    } ifelse
  } if

  /DefaultCMYK 2 index /ColorSpace //rget exec {
    resolvecolorspace dup csncomp 4 eq {
      dup type /nametype eq { 1 array astore } if
      /DefaultCMYK exch /ColorSpace defineresource pop
      pop //true
    } {
      pop
      (   **** Warning: ignoring invalid /DefaultCMYK color space.\n)
      pdfformaterror
    } ifelse
  } if

  { % if using color space substitution, "transition" the current color space
    currentcolorspace dup length 1 eq {  % always an array
      0 get
      dup /DeviceGray eq 1 index /DeviceRGB eq or 1 index /DeviceCMYK eq or { 
        /Pattern setcolorspace setcolorspace 
      } { 
        pop 
      } ifelse
    } { 
      pop 
    } ifelse
  } if
} bind def

% Write OutputIntents to device if the device handles it
/writeoutputintents {
  currentdevice 1 dict dup /OutputIntent //null put readonly
  .getdeviceparams
  mark ne { pop pop
    % device supports OutputIntent parameter
    Trailer /Root oget /OutputIntents knownoget {
      dup type /arraytype eq {
        {	% process all output profiles present
          oforce
          dup length dict .copydict
          dup /DestOutputProfile knownoget {
            PDFfile fileposition exch
            mark exch { oforce } forall .dicttomark
            //true resolvestream
            [ { counttomark 1 add index
                64000 string readstring
                not { exit } if
              } loop
            ] exch closefile
            0 1 index { length add } forall .bytestring
            0 3 2 roll {
              3 copy putinterval
              length add
            } forall pop
            exch PDFfile exch setfileposition
            1 index /DestOutputProfile 3 2 roll put
          } if
          % Convert to string array because it's easier for the device
          [ 1 index /OutputCondition knownoget not { () } if
            2 index /OutputConditionIdentifier knownoget not { () } if
            3 index /RegistryName knownoget not { () } if
            4 index /Info knownoget not { () } if
            5 index /DestOutputProfile knownoget not { () } if
          ]
          [ /OutputIntent 3 2 roll .pdfputparams pop pop
          pop	  % done with this OutputIntent dictionary
        } forall
      } {
        pop
        (   **** Warning: OutputIntent attribute of a wrong type is ignored.\n)
        pdfformaterror
      } ifelse
    } if	% OutputIntents known
    % tell device there are no more OutputIntents
    [ /OutputIntent [ ] .pdfputparams pop pop
  } if
} bind def

end			% pdfdict
.setglobal