home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 September / PCWorld_2001-09_cd.bin / Software / Vyzkuste / rychlokurz / gs700w32.exe / gs7.00 / lib / pdf_main.ps < prev    next >
Text File  |  2001-04-06  |  24KB  |  769 lines

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