home *** CD-ROM | disk | FTP | other *** search
/ Mixa 155: Dogs / MIXA 155: Dogs.iso / pc / Viewer / BROWSER(W) / フィルタ / PDF / LIB / pdf_main.ps < prev    next >
Encoding:
Text File  |  2002-10-29  |  22.4 KB  |  757 lines

  1. %    Copyright (C) 1994, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is licensed to a single customer by Artifex Software Inc.
  3. % under the terms of a specific OEM agreement.
  4.  
  5. % $RCSfile: pdf_main.ps,v $ $Revision: 1.21 $
  6. % pdf_main.ps
  7. % PDF file- and page-level operations.
  8.  
  9. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  10. .currentglobal true .setglobal
  11. /pdfdict where { pop } { /pdfdict 100 dict def } ifelse
  12. pdfdict begin
  13.  
  14. % Patch in an obsolete variable used by some third-party software.
  15. /#? false def
  16.  
  17. % Test whether the current output device handles pdfmark.
  18. /.writepdfmarkdict 1 dict dup /pdfmark null put readonly def
  19. /.writepdfmarks {    % - .writepdfmarks <bool>
  20.   currentdevice //.writepdfmarkdict .getdeviceparams
  21.   mark eq { false } { pop pop true } ifelse
  22.   systemdict /DOPDFMARKS known or 
  23. } bind def
  24.  
  25. % For simplicity, we use a single interpretation dictionary for all
  26. % PDF graphics execution, even though this is too liberal.
  27. /pdfopdict mark
  28.   objopdict { } forall
  29.   drawopdict { } forall
  30.   /endstream { exit } bind
  31.   (%%EOF) cvn { exit } bind        % for filters
  32.     % PDF 1.1 operators
  33.   /BX { /BXlevel BXlevel 1 add store } bind
  34.   /EX { /BXlevel BXlevel 1 sub store } bind
  35.   /PS { cvx exec } bind
  36.     % PDF 1.2 operators
  37.   /BMC { pop } bind
  38.   /BDC { pop pop } bind
  39.   /EMC { }
  40.   /MP { pop } bind
  41.   /DP { pop pop } bind
  42. .dicttomark readonly def
  43.  
  44. % ======================== Main program ======================== %
  45.  
  46. end            % pdfdict
  47. userdict begin
  48.  
  49. /defaultfontname /Times-Roman def
  50.  
  51. % Make sure the registered encodings are loaded, so we don't run the risk
  52. % that some of the indices for their names will overflow the packed
  53. % representation.  (Yes, this is a hack.)
  54. SymbolEncoding pop
  55. DingbatsEncoding pop
  56.  
  57. % Redefine 'run' so it recognizes PDF files.
  58. systemdict begin
  59. /.runps /run load def
  60. /runpdfstring 50 string def        % length is arbitrary
  61. /run {
  62.   dup type /filetype ne { (r) file } if
  63.   dup ( ) .peekstring {
  64.     (%) eq {
  65.       dup (  ) .peekstring {
  66.     (%P) eq {
  67.       dup //runpdfstring {
  68.         % Some invalid files might have extra-long first lines....
  69.         { readline } .internalstopped not { pop pop exit } if
  70.         pop =string
  71.       } loop
  72.       //runpdfstring (%PDF-) anchorsearch {
  73.         pop pop runpdf
  74.       } {
  75.         pop cvx .runexec
  76.       } ifelse
  77.     } {
  78.       cvx .runps
  79.     } ifelse
  80.       } {
  81.     closefile
  82.       } ifelse
  83.     } {
  84.       cvx .runps
  85.     } ifelse
  86.   } {
  87.     closefile
  88.   } ifelse
  89. } bind odef
  90. currentdict /runpdfstring .undef
  91.  
  92. /runpdf            % <file> runpdf -
  93.  { userdict begin
  94.    /Page# null def
  95.    /Page null def
  96.    /DSCPageCount 0 def
  97.    /PDFSave null def
  98.    GS_PDF_ProcSet begin
  99.    pdfdict begin
  100.    pdfopen begin
  101.    Trailer /Root oget /Pages oget /CropBox knownoget
  102.     { mark /CropBox 3 -1 roll /PAGES pdfmark
  103.     }
  104.    if
  105.    /FirstPage where { pop FirstPage } { 1 } ifelse
  106.    1
  107.    /LastPage where { pop LastPage } { pdfpagecount } ifelse
  108.    QUIET not
  109.     { (Processing pages ) print 2 index =only ( through ) print dup =only
  110.       (.) = flush
  111.     }
  112.    if
  113.     { dup /Page# exch store
  114.       QUIET not { (Page ) print dup == flush } if
  115.       pdfgetpage pdfshowpage
  116.     } for
  117.    currentdict pdfclose
  118.    end            % temporary dict
  119.    end            % pdfdict
  120.    end            % userdict
  121.  } bind def
  122. end            % systemdict
  123. % Redefine the procedure that the C code uses for running piped input.
  124. % It is OK to use { (%stdin) run } here, because a startjob cannot occur.
  125. /.runstdin {
  126.   { (%stdin) run } execute0
  127. } bind def
  128.  
  129. end            % userdict
  130. pdfdict begin
  131.  
  132. % ======================== File parsing ======================== %
  133.  
  134. % Read the cross-reference and trailer sections.
  135.  
  136. /traileropdict mark
  137.   (<<) cvn { mark } bind
  138.   (>>) cvn /.dicttomark load
  139.   ([) cvn { mark } bind        % ditto
  140.   (]) cvn dup load
  141. %  /true true        % see .pdfexectoken in pdf_base.ps
  142. %  /false false        % ibid.
  143. %  /null null        % ibid.
  144.   /R { /resolveR cvx 3 packedarray cvx } bind    % see Objects below
  145.   /startxref /exit load
  146. .dicttomark readonly def
  147.  
  148. % Because of EOL conversion, lines with fixed contents might be followed
  149. % by one or more blanks.
  150. /lineeq            % <filestr> <conststr> lineeq <bool>
  151.  { anchorsearch
  152.     { pop { ( ) anchorsearch not { () eq exit } if pop } loop }
  153.     { pop false }
  154.    ifelse
  155.  } bind def
  156. /linene { lineeq not } bind def
  157.  
  158. % Read (mostly scan) the cross-reference table.
  159. /readxref        % <pos> readxref <trailerdict>
  160.  { PDFoffset add PDFfile exch setfileposition
  161.         % In some PDF files, this position actually points to
  162.         % white space before the xref line.  Skip over this here.
  163.    { PDFfile fileposition PDFfile read pop 32 gt { exit } if pop
  164.    } loop
  165.    PDFfile exch setfileposition
  166.         % The PDF specification says that the 'xref' must be on a line
  167.         % by itself. The code here formerly used readline and linene to
  168.         % check this. However, Acrobat Reader only requires the line to
  169.         % begin with 'xref', and there are enough applications producing
  170.         % non-compliant PDF files that we have to do this too.
  171.    PDFfile pdfstring 0 4 getinterval readstring pop
  172.    (xref) ne { /readxref cvx /syntaxerror signalerror } if
  173.         % Store the xref table entry position for each object.
  174.         % We only need to read the run headers, not every entry.
  175.     { PDFfile token pop        % first object # or trailer
  176.       dup /trailer eq { pop exit } if
  177.       PDFfile pdfstring readline pop
  178.       token pop            % entry count
  179.       exch pop exch
  180.         % This section might be adding new objects:
  181.         % ensure that Objects and Generations are big enough.
  182.         % Stack: count obj#
  183.       2 copy add growPDFobjects
  184.       PDFfile fileposition 3 -1 roll
  185.        { Objects 2 index lget null eq    % later update might have set it
  186.       { Objects 2 index 2 index cvx lput }
  187.          if exch 1 add exch 20 add
  188.        }
  189.       repeat PDFfile exch setfileposition pop
  190.     } loop
  191.    count /pdfemptycount exch def
  192.    PDFfile traileropdict .pdfrun
  193.  } bind def
  194.  
  195. % Open a PDF file and read the header, trailer, and cross-reference.
  196. /pdfopen {        % <file> pdfopen <dict>
  197.   pdfopenfile begin
  198.   pdfopencache
  199.   .writepdfmarks {
  200.     % Copy bookmarks (outline) to the output.
  201.     Trailer /Root oget /Outlines knownoget {
  202.       /First knownoget {
  203.     { dup writeoutline /Next knownoget not { exit } if } loop
  204.       } if
  205.     } if
  206.   } if        % end .writepdfmarks
  207.   currentdict end
  208. } bind def
  209. /pdfopencache {        % - pdfopencache -
  210.     % Create and initialize some caches.
  211.   /PageCount pdfpagecount def
  212.   /PageNumbers PageCount dict def
  213.   /PageIndex PageCount array def
  214. } bind def
  215. /pdfopenfile {        % <file> pdfopenfile <dict>
  216.    pdfdict readonly pop        % can't do it any earlier than this
  217.    15 dict begin
  218.    /LocalResources 0 dict def
  219.    /DefaultMatrix null def    % establish binding
  220.    /Printed where { pop } {
  221.         % Guess whether the output device is a printer.
  222.      /Printed currentpagedevice /OutputFile known def
  223.    } ifelse
  224.    /PSLevel1 where { pop } { /PSLevel1 false def } ifelse
  225.    cvlit /PDFfile exch def
  226.    /PDFsource PDFfile def
  227.    /Repaired false def
  228.    PDFfile dup 0 setfileposition pdfstring readstring 
  229.    not {/pdfopen cvx /syntaxerror signalerror} if
  230.    (%PDF-) search not {/pdfopen cvx /syntaxerror signalerror} if
  231.    length /PDFoffset exch def pop cvr /PDFversion exch def
  232.    findxref
  233.    initPDFobjects
  234.     % Read the last cross-reference table.
  235.    readxref /Trailer exch def
  236.    Trailer /Encrypt known
  237.     { pdf_process_Encrypt    % signal error
  238.     }
  239.    if
  240.     % Read any previous cross-reference tables.
  241.    Trailer { /Prev .knownget not { exit } if readxref } loop
  242.    Repaired { printrepaired } if
  243.    currentdict end
  244.  } bind def
  245.  
  246. % Skip backward over the %%EOF at the end of the PDF file, and read
  247. % the preceding startxref line.  The PDF specification unambiguously
  248. % requires that the %%EOF appear on a line by itself, and that the
  249. % startxref and the following position value appear on separate lines;
  250. % however, some applications (including, apparently, Acrobat Distiller
  251. % on the Macintosh) may add up to 2K of garbage after the %%EOF, and some
  252. % other applications also truncate the %%EOF to %%EO, and/or put the
  253. % startxref and the following value on the same line.
  254. % A file reported from Distiller 3.02b for AIX 4.1.1 has 2076 bytes
  255. % of garbage. The tolerance is increased to 4K to cover future
  256. % applications and bigger allocation units.
  257. /findxref {        % - findxref <xrefpos>
  258.   PDFfile dup dup 0 setfileposition bytesavailable
  259.   dup /PDFfilelen exch def
  260.     % Find the last %%EOF string (within 4096 bytes)
  261.   4096 sub PDFoffset .max
  262.   2 copy setfileposition
  263.   PDFfilelen exch sub string 1 index exch readstring pop {
  264.     (\015%%EO) search {        % Adobe can handle truncated key string
  265.       pop pop            % if found, keep searching 'post' string
  266.     } {
  267.       (\012%%EO) search
  268.     { pop pop } { exit } ifelse    % exit if neither string found
  269.     } ifelse
  270.   } loop
  271.   PDFfilelen exch length sub 4 sub PDFoffset .max exch 1 index setfileposition
  272.     % Stack: eofpos
  273.     % Check for whether this is, in fact, a valid PDF file.
  274.   dup PDFfilelen exch sub dup dup 7 gt exch 6 lt or {
  275.     pop true
  276.   } {
  277.     string PDFfile exch readstring pop
  278.     dup (%%EOF\n) eq exch dup (%%EOF\r) eq
  279.     exch (%%EOF\r\n) eq or or not
  280.   } ifelse {
  281.     (
  282.    **** This file has a corrupted %%EOF marker, or garbage after the %%EOF.
  283. ) pdfformaterror
  284.   } if
  285.   PDFfile exch setfileposition
  286.     % Now read the startxref and xref start position.
  287.   prevline token not { null } if dup type /integertype eq {
  288.     exch pop cvi        % xref start position
  289.     exch PDFfile exch setfileposition
  290.     prevline (startxref) linene { /findxref cvx /syntaxerror signalerror } if
  291.     pop
  292.   } {    % else, this file has 'startxref #####' format
  293.     (startxref) ne { /findxref cvx /syntaxerror signalerror } if
  294.     cvi        % xref start position
  295.     (
  296.    **** The format of the startxref line in this file is invalid.
  297. ) pdfformaterror
  298.     exch PDFfile exch setfileposition
  299.   } ifelse
  300. } bind def
  301. /stderrprint {        % <string> stderrprint -
  302.   (%stderr) (w) file dup dup 4 -1 roll writestring flushfile closefile
  303. } bind def
  304. /pdfformaterror {    % <string> pdfformaterror -
  305.   stderrprint
  306.   /Repaired true store
  307. } bind def
  308. /printrepaired {
  309.   Trailer /Info knownoget {
  310.     /Producer knownoget not { null } if
  311.   } {
  312.     null
  313.   } ifelse
  314.   dup null eq {
  315.     pop (\
  316.    **** Please notify the author of the software that produced this file
  317. )
  318.   } {
  319.     (   **** The file was produced by ) stderrprint
  320.     % Handle a Unicode Producer.
  321.     (\376\377) anchorsearch {
  322.       pop dup length 2 idiv string 0 1 2 index length 1 sub {
  323.         % Stack: origstr newstr i
  324.     1 index exch 3 index 1 index 2 mul 1 add get put
  325.       } for exch pop
  326.     } if
  327.     stderrprint
  328.     (:
  329.    **** please notify the author of this software
  330. )
  331.   } ifelse stderrprint
  332. (\
  333.    **** that the file does not conform to Adobe's published PDF
  334.    **** specification.  Processing of the file will continue normally.
  335.  
  336. ) stderrprint
  337. } bind def
  338.  
  339. % Write the outline structure for a file.  Uses linkdest (below).
  340. /writeoutline        % <outlinedict> writeoutline -
  341.  { mark
  342.    0 2 index /First knownoget
  343.     { { exch 1 add exch /Next knownoget not { exit } if } loop }
  344.    if
  345.         % stack: dict mark count
  346.    dup 0 eq
  347.     { pop 1 index
  348.     }
  349.     { 2 index /Count knownoget { 0 lt { neg } if } if
  350.       /Count exch 3 index
  351.     }
  352.    ifelse linkdest /Title oget /Title exch /OUT pdfmark
  353.    /First knownoget
  354.     { { dup writeoutline /Next knownoget not { exit } if } loop }
  355.    if
  356.  } bind def
  357.  
  358. % Close a PDF file.
  359. /pdfclose        % <dict> pdfclose -
  360.  { begin
  361.    PDFfile closefile
  362.    end
  363.  } bind def
  364.  
  365. % ======================== Page accessing ======================== %
  366.  
  367. % Get a (possibly inherited) attribute of a page.
  368. /pget            % <pagedict> <key> pget <value> -true-
  369.             % <pagedict> <key> pget -false-
  370.  { 2 copy knownoget
  371.     { exch pop exch pop true
  372.     }
  373.     { exch /Parent knownoget
  374.        { exch pget }
  375.        { pop false }
  376.       ifelse
  377.     }
  378.    ifelse
  379.  } bind def
  380.  
  381. % Get the value of a resource on a given page.
  382. /rget {            % <resname> <pagedict> <restype> rget <value> -true-
  383.             % <resname> <pagedict> <restype> rget -false-
  384.   LocalResources 1 index knownoget {
  385.      3 index knownoget
  386.   } {
  387.     false
  388.   } ifelse {
  389.     exch pop exch pop exch pop true
  390.   } {
  391.     exch /Resources pget {
  392.       exch knownoget { exch knownoget } { pop false } ifelse
  393.     } {
  394.       pop pop false
  395.     } ifelse
  396.   } ifelse
  397. } bind def
  398.  
  399. % Get the total number of pages in the document.
  400. /pdfpagecount        % - pdfpagecount <int>
  401.  { Trailer /Root oget /Pages oget /Count oget
  402.  } bind def
  403.  
  404. % Find the N'th page of the document by iterating through the Pages tree.
  405. % The first page is numbered 1.
  406. /pdffindpageref {        % <int> pdffindpage <objref>
  407.   dup Trailer /Root oget /Pages get
  408.     {        % We should be able to tell when we reach a leaf
  409.         % by finding a Type unequal to /Pages.  Unfortunately,
  410.         % some files distributed by Adobe lack the Type key
  411.         % in some of the Pages nodes!  Instead, we check for Kids.
  412.       dup oforce /Kids knownoget not { exit } if
  413.       exch pop null
  414.       0 1 3 index length 1 sub {
  415.          2 index exch get
  416.      dup oforce dup /Kids known { /Count oget } { pop 1 } ifelse
  417.         % Stack: index kids null noderef count
  418.      dup 5 index ge { pop exch pop exit } if
  419.      5 -1 roll exch sub 4 1 roll pop
  420.       } for exch pop
  421.         % Stack: index null|noderef
  422.       dup null eq { pop pop 1 null exit } if
  423.     } loop
  424.         % Stack: index countleft noderef
  425.    1 index 1 ne { pop pop /pdffindpage cvx /rangecheck signalerror } if
  426.    exch pop
  427.    PageIndex 2 index 1 sub 2 index oforce put
  428.    PageNumbers 1 index oforce 3 index put
  429.    exch pop
  430. } bind def
  431. /pdffindpage {        % <int> pdffindpage <pagedict>
  432.   pdffindpageref oforce
  433. } bind def
  434.  
  435. % Find the N'th page of the document.
  436. % The first page is numbered 1.
  437. /pdfgetpage        % <int> pdfgetpage <pagedict>
  438.  { PageIndex 1 index 1 sub get dup null ne
  439.     { exch pop oforce }
  440.     { pop pdffindpage }
  441.    ifelse
  442.  } bind def
  443.  
  444. % Find the page number of a page object (inverse of pdfgetpage).
  445. /pdfpagenumber        % <pagedict> pdfpagenumber <int>
  446.  {    % We use the simplest and stupidest of all possible algorithms....
  447.    PageNumbers 1 index .knownget
  448.     { exch pop
  449.     }
  450.     { 1 1 PageCount 1 add    % will give a rangecheck if not found
  451.        { dup pdfgetpage oforce 2 index eq { exit } if pop
  452.        }
  453.       for exch pop
  454.     }
  455.    ifelse
  456.  } bind def
  457.  
  458. % Display a given page.
  459. /boxrect        % [<llx> <lly> <urx> <ury>] boxrect <x> <y> <w> <h>
  460.  { aload pop exch 3 index sub exch 2 index sub
  461.  } bind def
  462. /resolvedest {        % <name|string|other> resolvedest <other|null>
  463.   dup type /nametype eq {
  464.     Trailer /Root oget /Dests knownoget {
  465.       exch knownoget not { null } if
  466.     } {
  467.       null
  468.     } ifelse
  469.   } {
  470.     dup type /stringtype eq {
  471.       Trailer /Root oget /Names knownoget {
  472.     /Dests knownoget {
  473.       exch nameoget
  474.     } {
  475.       pop null
  476.     } ifelse
  477.       } {
  478.     pop null
  479.       } ifelse
  480.     } if
  481.   } ifelse
  482. } bind def
  483. /linkdest {        % <link|outline> linkdest
  484.             %   ([/Page <n>] /View <view> | ) <link|outline>
  485.   dup /Dest knownoget
  486.     { resolvedest
  487.       dup type /dicttype eq { /D knownoget not { null } if } if
  488.       dup null eq
  489.        { pop }
  490.        { dup 0 oget
  491.      dup null eq
  492.       { pop }
  493.       { dup type /integertype ne { pdfpagenumber } if
  494.         /Page exch 4 -2 roll
  495.       }
  496.      ifelse
  497.      dup length 1 sub 1 exch getinterval /View exch 3 -1 roll
  498.        }
  499.       ifelse
  500.     }
  501.    if
  502. } bind def
  503. % <pagedict> mark ... -proc- -
  504. /namedactions 8 dict dup begin
  505.   /FirstPage {
  506.     /Page 1 3 -1 roll
  507.   } def
  508.   /LastPage {
  509.     counttomark 2 add index pdfpagecount /Page exch 3 -1 roll
  510.   } def
  511.   /NextPage {
  512.     counttomark 2 add index pdfpagenumber 1 add /Page exch 3 -1 roll
  513.   } def
  514.   /PrevPage {
  515.     counttomark 2 add index pdfpagenumber 1 sub /Page exch 3 -1 roll
  516.   } def
  517. end readonly def
  518. % <pagedict> <annotdict> -proc- -
  519. /annottypes 5 dict dup begin
  520.   /Text {
  521.     mark exch
  522.      { /Rect /Open /Contents }
  523.      { 2 copy knownoget { 3 -1 roll } { pop } ifelse }
  524.     forall pop /ANN pdfmark
  525.   } bind def
  526.   /Link {
  527.     mark exch
  528.      { /Rect /Border }
  529.      { 2 copy knownoget { 3 -1 roll } { pop } ifelse }
  530.     forall dup /A knownoget {
  531.       dup /URI known {
  532.         /A mark 3 2 roll    % <<>> /A [ <<action>>
  533.         { oforce } forall
  534.         .dicttomark
  535.         3 2 roll
  536.       } {
  537.         dup /D knownoget {
  538.       exch pop exch dup length dict copy dup /Dest 4 -1 roll put
  539.         } {
  540.       /N knownoget {        % Assume /S /Named
  541.          namedactions exch .knownget { exec } if
  542.       } if
  543.         } ifelse
  544.       } ifelse
  545.     } if
  546.     linkdest pop /LNK pdfmark
  547.   } bind def
  548. end readonly def
  549.  
  550. /pdfshowpage        % <pagedict> pdfshowpage -
  551.  { dup /Page exch store
  552.    pdfshowpage_init 
  553.    pdfshowpage_setpage 
  554.    save /PDFSave exch store
  555.    (before exec) VMDEBUG
  556.      pdfshowpage_finish
  557.    (after exec) VMDEBUG
  558.    PDFSave restore
  559.  } bind def
  560.  
  561. /pdfpagecontents    % <pagedict> pdfpagecontents <contents>
  562.  { } bind def
  563.  
  564. /pdfshowpage_init     % <pagedict> pdfshowpage_init <pagedict>
  565.  { /DSCPageCount DSCPageCount 1 add store
  566.  } bind def
  567.  
  568. /.pdfshowpage_Install {    % <pagedict> [<prevproc>] .pdfshowpage_Install -
  569.   exch
  570.     % We would like to clip to the CropBox here, but the subsequent
  571.     % initgraphics would override it.  Instead, we have to handle it
  572.     % in graphicsbeginpage.
  573.   dup /MediaBox pget {
  574.     dup 0 get neg exch 1 get neg translate
  575.   } if
  576.   pop 0 get exec
  577. } bind def
  578.  
  579. /pdfshowpage_setpage {    % <pagedict> pdfshowpage_setpage <pagedict>
  580.   4 dict begin        % for setpagedevice
  581.     % Stack: pagedict
  582.   currentpagedevice /Orientation 2 index /Rotate pget not { 0 } if 90 idiv
  583.     % Rotate specifies *clockwise* rotation!
  584.     neg 3 and def
  585.     % Stack: pagedict currentpagedict
  586.   1 index /MediaBox pget {
  587.             % Set the page size.
  588.     boxrect 2 array astore /PageSize exch def pop pop
  589.   } if
  590.   dup /Install .knownget {
  591.             % Don't let the Install procedure get more deeply
  592.             % nested after every page.
  593.       dup type dup /arraytype eq exch /packedarraytype eq or {
  594.     dup length 4 eq {
  595.       dup 2 get /.pdfshowpage_Install load eq {
  596.         1 get 0 get    % previous procedure
  597.       } if
  598.     } if
  599.       } if
  600.   } {
  601.     { }
  602.   } ifelse 1 array astore
  603.   2 index exch /.pdfshowpage_Install load /exec load
  604.   4 packedarray cvx
  605.     % Stack: pagedict currentpagedict installproc
  606.   /Install exch def
  607.     % Stack: pagedict currentpagedict
  608.   pop currentdict end setpagedevice
  609. } bind def
  610.  
  611. /pdfshowpage_finish {    % <pagedict> pdfshowpage_finish -
  612.  
  613.   .writepdfmarks {
  614.  
  615.     % Copy the crop box.
  616.     dup /CropBox knownoget {
  617.     % If the page has been rotated, rotate the CropBox.
  618.       mark /CropBox 3 -1 roll
  619.       3 index /Rotate pget {
  620.     90 idiv 1 and 0 ne {
  621.       aload pop 4 -2 roll exch 4 2 roll exch 4 array astore
  622.     } if
  623.       } if
  624.       /PAGE pdfmark
  625.     } if
  626.  
  627.     % Copy annotations and links.
  628.     dup /Annots knownoget {
  629.       0 1 2 index length 1 sub
  630.        { 1 index exch oget
  631.          dup /Subtype oget annottypes exch .knownget { exec } { pop } ifelse
  632.        }
  633.       for pop
  634.     } if
  635.  
  636.   } if        % end .writepdfmarks
  637.  
  638.     % Display the actual page contents.
  639.    6 dict begin
  640.    /BXlevel 0 def
  641.    /BGDefault currentblackgeneration def
  642.    /UCRDefault currentundercolorremoval def
  643.     %****** DOESN'T HANDLE COLOR TRANSFER YET ******
  644.    /TRDefault currenttransfer def
  645.   matrix currentmatrix 2 dict
  646.   2 index /CropBox knownoget {
  647.     boxrect 4 array astore 1 index /ClipRect 3 -1 roll put
  648.   } if
  649.   dictbeginpage /DefaultMatrix 1 index store setmatrix
  650.   dup          % for showing annotations below
  651.   count /pdfemptycount exch store
  652.   gsave
  653.     % If the page uses any transparency features, show it within
  654.     % a transparency group.
  655.   PDFversion 1.4 lt {
  656.     showpagecontents
  657.   } {
  658.     dup pageusestransparency {
  659.       % Show the page within a PDF 1.4 device filter.
  660.       0 .pushpdf14devicefilter {
  661.     % If the page has a Group, enclose contents in transparency group.
  662.         % (Adobe Tech Note 5407, sec 9.2)
  663.         dup /Group knownoget {
  664.       1 index /CropBox knownoget not {
  665.         1 index /MediaBox oget
  666.       } if .beginformgroup {
  667.         showpagecontents
  668.       } .internalstopped {
  669.         .discardtransparencygroup stop
  670.       } if .endtransparencygroup
  671.         } {
  672.       showpagecontents
  673.         } ifelse
  674.       } .internalstopped {
  675.     % todo: discard
  676.     .popdevicefilter stop
  677.       } if .popdevicefilter
  678.     } {
  679.       showpagecontents
  680.     } ifelse
  681.   } ifelse
  682.   grestore
  683.    % todo: mixing drawing ops outside the device filter could cause
  684.    % problems, for example with the pnga device.
  685.    /Annots knownoget { { oforce drawannot } forall } if
  686.    endpage
  687.    end            % scratch dict
  688. } bind def
  689. /showpagecontents {    % <pagedict> showpagecontents -
  690.   /Contents knownoget not { 0 array } if
  691.   dup type /arraytype ne { 1 array astore } if {
  692.     oforce false resolvestream pdfopdict .pdfrun
  693.   } forall
  694. } bind def
  695. /processcolorspace {    % - processcolorspace <colorspace>
  696.     % The following is per the PLRM3.
  697.   currentdevice 1 dict dup /ProcessColorModel dup put .getdeviceparams
  698.   exch pop exch pop
  699.   dup type /nametype ne { cvn } if
  700.   dup { setcolorspace } .internalstopped { pop /DeviceRGB } if
  701. } bind def
  702.  
  703. % ------ Transparency support ------ %
  704.  
  705. % Determine whether a page might invoke any transparency features:
  706. %    - Group in the page dictionary
  707. %    - Non-default ca, CA, or SMask in an ExtGState
  708. %    - Form XObject with Group
  709. %    - Image XObject with SMask
  710. % ****** DO WE NEED TO LOOK IN RESOURCE DICTS OF OTHER CONTENT STREAMS? ******
  711. /pageusestransparency {        % <pagedict> pageusestransparency <bool>
  712.   PDFversion 1.4 lt {
  713.     pop false
  714.   } {
  715.     dup /Group known {
  716.       pop true
  717.     } {
  718.       false exch {
  719.     dup resourceusestransparency { pop not exit } if
  720.     /Parent knownoget not { exit } if
  721.       } loop
  722.     } ifelse
  723.   } ifelse
  724. } bind def
  725. % Check the Resources of a page or Form.
  726. /resourceusestransparency {    % <dict> resourceusestransparency <bool>
  727.   {    % Use loop to provide an exitable context.
  728.     /Resources knownoget not { 0 dict } if
  729.     dup /ExtGState knownoget {
  730.       false exch {
  731.     exch pop oforce
  732.     dup /ca knownoget { 1 ne { pop not exit } if } if
  733.     dup /CA knownoget { 1 ne { pop not exit } if } if
  734.     dup /SMask knownoget { /None ne { pop not exit } if } if
  735.     pop
  736.       } forall { pop true exit } if
  737.     } if
  738.     dup /XObject knownoget {
  739.       false exch {
  740.     exch pop oforce dup /Subtype get
  741.     dup /Image eq { 1 index /SMask known { pop pop not exit } if } if
  742.     /Form eq {
  743.       dup /Group known { pop not exit } if
  744.       resourceusestransparency { not exit } if
  745.     } {
  746.       pop
  747.     } ifelse
  748.       } forall { pop true exit } if
  749.     } if
  750.     pop false exit
  751.   } loop
  752. } bind def
  753.  
  754. end            % pdfdict
  755. .setglobal
  756.