home *** CD-ROM | disk | FTP | other *** search
/ jppd.dyndns.org / jppd.dyndns.org.tar / jppd.dyndns.org / QUERYPRO / Impressora_PDF / converter.exe / GPLGS / gs_setpd.ps < prev    next >
Text File  |  2004-09-14  |  29KB  |  866 lines

  1. %    Copyright (C) 1994, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: gs_setpd.ps,v 1.18.2.3 2004/09/13 22:32:19 ray Exp $
  14. % The current implementation of setpagedevice has the following limitations:
  15. %    - It doesn't attempt to "interact with the user" for Policy = 2.
  16.  
  17. languagelevel 1 .setlanguagelevel
  18. level2dict begin
  19.  
  20. % ---------------- Redefinitions ---------------- %
  21.  
  22. % Redefine .beginpage and .endpage so that they call BeginPage and
  23. % EndPage respectively if appropriate.
  24.  
  25. % We have to guard against the BeginPage procedure not popping its operand.
  26. % This is really stupid, but the Genoa CET does it.
  27. /.beginpage {        % - .beginpage -
  28.   .currentshowpagecount {
  29.     .currentpagedevice pop
  30.     dup //null ne { /BeginPage .knownget } { pop //false } ifelse {
  31.         % Stack: ... pagecount proc
  32.        count 2 .execn
  33.         % Stack: ... ..???.. oldcount
  34.        count 1 add exch sub { pop } repeat
  35.     } {
  36.       pop
  37.     } ifelse
  38.   } if
  39. } bind odef
  40.  
  41. % Guard similarly against EndPage not popping its operand.
  42. /.endpage {        % <reason> .endpage <print_bool>
  43.   .currentshowpagecount {
  44.     1 index .currentpagedevice pop
  45.     dup //null ne { /EndPage .knownget } { pop //false } ifelse {
  46.         % Stack: ... reason pagecount reason proc
  47.       count 2 .execn
  48.         % Stack: ... ..???.. print oldcount
  49.       count 2 add exch sub { exch pop } repeat
  50.     } {
  51.       pop pop 2 ne
  52.     } ifelse
  53.   } {
  54.     2 ne
  55.   } ifelse
  56. } bind odef
  57.  
  58. % Define interpreter callouts for handling gstate-saving operators,
  59. % to make sure that they create a page device dictionary for use by
  60. % the corresponding gstate-restoring operator.
  61. % We'd really like to avoid the cost of doing this, but we don't see how.
  62. % The names %gsavepagedevice, %savepagedevice, %gstatepagedevice,
  63. % %copygstatepagedevice, and %currentgstatepagedevice are known to the
  64. % interpreter.
  65.  
  66. (%gsavepagedevice) cvn
  67.  { currentpagedevice pop gsave
  68.  } bind def
  69.  
  70. (%savepagedevice) cvn
  71.  { currentpagedevice pop save
  72.  } bind def
  73.  
  74. (%gstatepagedevice) cvn
  75.  { currentpagedevice pop gstate
  76.  } bind def
  77.  
  78. (%copygstatepagedevice) cvn
  79.  { currentpagedevice pop copy
  80.  } bind def
  81.  
  82. (%currentgstatepagedevice) cvn
  83.  { currentpagedevice pop currentgstate
  84.  } bind def
  85.  
  86. % Define interpreter callouts for handling gstate-restoring operators
  87. % when the current page device needs to be changed.
  88. % The names %grestorepagedevice, %grestoreallpagedevice,
  89. % %restorepagedevice, %restore1pagedevice, and %setgstatepagedevice
  90. % are known to the interpreter.
  91.  
  92. /.installpagedevice
  93.  {    % Since setpagedevice doesn't create new device objects,
  94.     % we must (carefully) reinstall the old parameters in
  95.     % the same device.
  96.    .currentpagedevice pop //null currentdevice //null .trysetparams
  97.    dup type /booleantype eq
  98.     { pop pop }
  99.     {        % This should never happen!
  100.       SETPDDEBUG { (Error in .trysetparams!) = pstack flush } if
  101.       cleartomark pop pop pop
  102.       /.installpagedevice cvx /rangecheck signalerror
  103.     }
  104.    ifelse pop pop
  105.     % A careful reading of the Red Book reveals that an erasepage
  106.     % should occur, but *not* an initgraphics.
  107.    erasepage .beginpage
  108.  } bind def
  109.  
  110. /.uninstallpagedevice
  111.  { 2 .endpage { .currentnumcopies //false .outputpage } if
  112.    nulldevice
  113.  } bind def
  114.  
  115. (%grestorepagedevice) cvn
  116.  { .uninstallpagedevice grestore .installpagedevice
  117.  } bind def
  118.  
  119. (%grestoreallpagedevice) cvn
  120.  { .uninstallpagedevice grestore .installpagedevice grestoreall
  121.  } bind def
  122.  
  123. (%restore1pagedevice) cvn
  124.  { .uninstallpagedevice grestore .installpagedevice restore
  125.  } bind def
  126.  
  127. (%restorepagedevice) cvn
  128.  { .uninstallpagedevice restore .installpagedevice
  129.  } bind def
  130.  
  131. (%setgstatepagedevice) cvn
  132.  { .uninstallpagedevice setgstate .installpagedevice
  133.  } bind def
  134.  
  135. % Redefine .currentnumcopies so it consults the NumCopies device parameter.
  136. /.numcopiesdict mark
  137.   /NumCopies dup
  138. .dicttomark readonly def
  139.  
  140. /.currentnumcopies
  141.  { currentdevice //.numcopiesdict .getdeviceparams
  142.    dup type /integertype eq
  143.     { exch pop exch pop }
  144.     { cleartomark #copies }
  145.    ifelse
  146.  } bind odef
  147.  
  148. % Redefine .currentpagedevice and .setpagedevice so they convert between
  149. % null and a fixed empty directionary.
  150. /.nullpagedevice 0 dict readonly def
  151. /.currentpagedevice {
  152.   //.currentpagedevice exch dup //null eq { pop //.nullpagedevice } if exch
  153. } bind odef
  154. /.setpagedevice {
  155.   dup //.nullpagedevice eq { pop //null } if //.setpagedevice
  156. } bind odef
  157.  
  158. % ---------------- Auxiliary definitions ---------------- %
  159.  
  160. % Define the required attributes of all page devices, and their default values.
  161. % We don't include attributes such as .MediaSize, which all devices
  162. % are guaranteed to supply on their own.
  163. /.defaultpolicies mark
  164.   /PolicyNotFound 1
  165.   /PageSize 0
  166.   /PolicyReport {
  167.     dup /.LockSafetyParams known {
  168.     % Only possible error is invalidaccess
  169.       /setpagedevice .systemvar /invalidaccess signalerror
  170.     }
  171.     if
  172.     pop
  173.   } bind
  174. .dicttomark readonly def
  175. % Note that the values of .requiredattrs are executed, not just fetched.
  176. /.requiredattrs mark
  177.   /PageDeviceName //null
  178.   /PageOffset [0 0] readonly
  179. % We populate InputAttributes with all of the known page sizes 
  180. % followed by a dummy media type that handles pages of any size.
  181. % This will create some duplicates, but that only slightly slows
  182. % down the media selection (loop is in zmedia2.c).
  183. %
  184. % Some PostScript creators assume that slot 0 is the default media
  185. % size and some can't handle a non-standard 4-element array which
  186. % is a 'range' type page size (always put last).
  187. %
  188. % Real Devices that can only handle specific page sizes will override this.
  189.   /InputAttributes {
  190.     mark
  191.     % First put the device's default page size in slot 0
  192.     % This satifies those that have devices built with a4 as the default
  193.     0 mark /PageSize currentdevice /PageSize gsgetdeviceprop .dicttomark
  194.     statusdict /.pagetypenames get {
  195.       counttomark 1 sub 2 idiv exch mark exch /PageSize exch
  196.       % stack: mark --dict-- --dict-- ... key mark /PageSize pagetypename
  197.       % see note above about pagetype executable array contents.
  198.       load dup 0 get exch 1 get 2 array astore .dicttomark
  199.     } forall
  200.     % If NORANGEPAGESIZE is defined, (-dNORANGEPAGESIZE), then don't add
  201.     % the 'match any' PageSize entry
  202.     systemdict /NORANGEPAGESIZE known not {
  203.     % Add one last entry which is the 4 element range array (non-standard)
  204.     counttomark 2 idiv 
  205.     % PageSize with either dimension 0 will be detected in
  206.     % match_page_size, so we can allow it here
  207.     mark /PageSize [0 dup 16#7ffff dup] .dicttomark
  208.     } if
  209.     .dicttomark
  210.   }
  211.   (%MediaSource) 0
  212.   /OutputAttributes {
  213.     mark 0 mark .dicttomark readonly .dicttomark
  214.   }
  215.   (%MediaDestination) 0
  216.   /Install {{.callinstall}} bind
  217.   /BeginPage {{.callbeginpage}} bind
  218.   /EndPage {{.callendpage}} bind
  219.   /Policies .defaultpolicies
  220.   /ImagingBBox //null        % default value
  221.   /UseCIEColor /.getuseciecolor load
  222. .dicttomark readonly def
  223.  
  224. % Define currentpagedevice so it creates the dictionary on demand if needed,
  225. % adding all the required entries defined just above.
  226. % We have to deal specially with entries that the driver may change
  227. % on its own.
  228. /.dynamicppkeys mark
  229.   /.MediaSize dup        % because it changes when PageSize is set
  230.   /PageCount dup
  231.   /Colors dup
  232.   /BitsPerPixel dup
  233.   /ColorValues dup
  234. .dicttomark readonly def
  235. /.makecurrentpagedevice {    % - .makecurrentpagedevice <dict>
  236.   currentdevice //null .getdeviceparams
  237.     % Make the dictionary large enough to add defaulted entries.
  238.   counttomark 2 idiv .requiredattrs length add dict
  239.   counttomark 2 idiv { dup 4 2 roll put } repeat exch pop
  240.     % Add any missing required attributes.
  241.     % Make a writable and (if possible) local copy of any default
  242.     % dictionaries, to work around a bug in the output of WordPerfect,
  243.     % which assumes that these dictionaries are writable and local.
  244.   .currentglobal exch dup gcheck .setglobal
  245.   .requiredattrs {
  246.     2 index 2 index known {
  247.       1 index /Policies eq {
  248.     % Merge policies from the device driver with defaults
  249.     2 index             % <<>> /key value <<>>
  250.     3 2 roll get        % <<>> value <<policies>>
  251.     exch {                
  252.       2 index 2 index known { 
  253.         pop pop
  254.       } { 
  255.         2 index 3 1 roll put
  256.       } ifelse
  257.     } forall
  258.     pop
  259.       } {
  260.     pop pop
  261.       } ifelse
  262.     } {
  263.       exec 2 index 3 1 roll put
  264.     } ifelse
  265.   } forall exch .setglobal
  266.   dup .setpagedevice
  267. } bind def
  268. /currentpagedevice {
  269.   .currentpagedevice {
  270.     dup length 0 eq {
  271.       pop .makecurrentpagedevice
  272.     } {
  273.         % If any of the dynamic keys have changed,
  274.         % we must update the page device dictionary.
  275.       currentdevice //.dynamicppkeys .getdeviceparams .dicttomark {
  276.         % Stack: current key value
  277.         2 index 2 index .knownget { 1 index ne } { //true } ifelse
  278.          { 2 index wcheck not
  279.         {    % This is the first entry being updated.
  280.             % Copy the dictionary to make it writable.
  281.           3 -1 roll
  282.           currentglobal 1 index dup gcheck currentglobal and setglobal
  283.           length dict
  284.           exch setglobal
  285.           .copydict
  286.           3 1 roll
  287.         }
  288.            if
  289.            2 index 3 1 roll put
  290.          }
  291.          { pop pop
  292.          }
  293.         ifelse
  294.       } forall
  295.       % If the device is the distiller device, update distillerparams that
  296.       % may have been changed by setdistillerparams
  297.       currentdevice .devicename /pdfwrite eq {
  298.     currentdistillerparams {
  299.           % Stack: current key value
  300.       2 index 2 index .knownget { 1 index ne } { //true } ifelse
  301.       { 2 index 3 1 roll put } { pop pop } ifelse
  302.     } forall
  303.       } if
  304.         % If the dictionary was global and is now local, copy
  305.         % any global subsidiary dictionaries to local VM.  This
  306.         % too is to work around the Word Perfect bug (see above).
  307.       dup gcheck not {
  308.     dup {
  309.       dup type /dicttype eq { dup gcheck } { //false } ifelse {
  310.         % Copy-on-write, see above.
  311.         2 index wcheck not {
  312.           3 -1 roll dup length dict .copydict
  313.           3 1 roll
  314.         } if
  315.         .copytree 2 index 3 1 roll put
  316.       } {
  317.         pop pop
  318.       } ifelse
  319.     } forall
  320.       } if
  321.         % We would like to do a .setpagedevice so we don't keep
  322.         % re-creating the dictionary.  Unfortunately, the effect
  323.         % of this is that if any dynamic key changes (PageCount
  324.         % in particular), we will do the equivalent of a
  325.         % setpagedevice at the next restore or grestore.
  326.         % Therefore, we make the dictionary read-only, but
  327.         % we don't store it away.  I.e., NOT:
  328.         % dup wcheck { .setpagedevice .currentpagedevice pop } if
  329.       readonly
  330.     } ifelse
  331.   } if
  332. } bind odef
  333.  
  334. % Copy a dictionary recursively.
  335. /.copytree {    % <dict> .copytree <dict'>
  336.   dup length dict exch {
  337.     dup type /dicttype eq { .copytree } if 2 index 3 1 roll put
  338.   } forall
  339. } bind def
  340.  
  341. % The implementation of setpagedevice is quite complex.  Currently,
  342. % everything but the media matching algorithm is implemented here.
  343.  
  344. % By default, we only present the requested changes to the device,
  345. % but there are some parameters that require special merging action.
  346. % Define those parameters here, with the procedures that do the merging.
  347. % The procedures are called as follows:
  348. %    <merged> <key> <new_value> -proc- <merged> <key> <new_value'>
  349. /.mergespecial mark
  350.   /InputAttributes
  351.    { dup //null eq
  352.       { pop //null
  353.       }
  354.       { 3 copy pop .knownget
  355.      { dup //null eq
  356.         { pop dup length dict }
  357.         { dup length 2 index length add dict .copydict }
  358.        ifelse
  359.      }
  360.      { dup length dict
  361.      }
  362.         ifelse .copydict readonly
  363.       }
  364.      ifelse
  365.    } bind
  366.   /OutputAttributes 1 index
  367.   /Policies
  368.     { 3 copy pop .knownget
  369.        { dup length 2 index length add dict .copydict }
  370.        { dup length dict }
  371.       ifelse copy readonly
  372.     } bind
  373.   % Ignore MediaPostition when merging (Adobe CPSI does).
  374.   /MediaPosition { dup //null ne { pop //null } if } bind
  375. .dicttomark readonly def
  376.  
  377. % Define the keys used in input attribute matching.
  378. /.inputattrkeys [
  379.   /PageSize /MediaColor /MediaWeight /MediaType /InsertSheet
  380.     % The following are documented in Adobe's supplement for v2017.
  381.   /LeadingEdge /MediaClass
  382. ] readonly def
  383. % Define other keys used in media selection.
  384. /.inputselectionkeys [
  385.   /MediaPosition /Orientation
  386. ] readonly def
  387.  
  388. % Define the keys used in output attribute matching.
  389. /.outputattrkeys [
  390.   /OutputType
  391. ] readonly def
  392.  
  393. % Define all the parameters that should always be copied to the merged
  394. % dictionary.
  395. /.copiedkeys [
  396.   /OutputDevice
  397.   .mergespecial { pop } forall
  398.   .inputattrkeys aload pop
  399.   .inputselectionkeys aload pop
  400.   .outputattrkeys aload pop
  401. ] readonly def
  402.  
  403. % Define the parameters that should not be presented to the device.
  404. % The procedures are called as follows:
  405. %    <merged> <key> <value> -proc-
  406. % The procedure leaves all its operands on the stack and returns
  407. % true iff the key/value pair should be presented to .putdeviceparams.
  408. /.presentspecial mark
  409.   .dynamicppkeys { pop //false } forall
  410.             % We must ignore an explicit request for .MediaSize,
  411.             % because media matching always handles this.
  412.   /.MediaSize //false
  413.   /Name //false
  414.   /OutputDevice //false
  415.   /PageDeviceName //false
  416.   /PageOffset //false
  417.   /PageSize //false        % obsolete alias for .MediaSize
  418.   /InputAttributes //false
  419.   .inputattrkeys
  420.     { dup /PageSize eq
  421.        { pop }
  422.        { { 2 index /InputAttributes .knownget { //null eq } { //true } ifelse } }
  423.       ifelse
  424.     }
  425.   forall
  426.   .inputselectionkeys { //false } forall
  427.   /OutputAttributes //false
  428.   .outputattrkeys
  429.     { { 2 index /OutputAttributes .knownget { //null eq } { //true } ifelse } }
  430.   forall
  431.   /Install //false
  432.   /BeginPage //false
  433.   /EndPage //false
  434.   /Policies //false
  435.     % Our extensions:
  436.   /HWColorMap
  437.     {            % HACK: don't transmit the color map, because
  438.             % window systems can change the color map on their own
  439.             % incrementally.  Someday we'll have a better
  440.             % solution for this....
  441.       //false
  442.     }
  443.   /ViewerPreProcess //false
  444.   /ImagingBBox //false    % This prevents the ImagingBBox value in the setpagedevice
  445.             % from affecting the device's ImagingBBox parameter, but
  446.             % does retain a 'shadow' copy at the PostScript level.
  447.             % This is done for Adobe compatibility since Adobe does
  448.             % render marks outside the ImagingBBox (and QuarkXpress
  449.             % relies on it).
  450. .dicttomark readonly def
  451.  
  452. % Define access to device defaults.
  453. /.defaultdeviceparams
  454.  { finddevice //null .getdeviceparams
  455.  } bind def
  456.  
  457. % Select media (input or output).  The hard work is done in an operator:
  458. %    <pagedict> <attrdict> <policydict> <keys> .matchmedia <key> true
  459. %    <pagedict> <attrdict> <policydict> <keys> .matchmedia false
  460. %    <pagedict> null <policydict> <keys> .matchmedia null true
  461. /.selectmedia        % <orig> <request> <merged> <failed>     <-- retained
  462.             %   <attrdict> <policydict> <attrkeys> <mediakey>
  463.             %   .selectmedia
  464.  { 5 index 5 -2 roll 4 index .matchmedia
  465.         % Stack: orig request merged failed attrkeys mediakey
  466.         %   (key true | false)
  467.     { 4 index 3 1 roll put pop
  468.     }
  469.     {    % Adobe's implementations have a "big hairy heuristic"
  470.     % to choose the set of keys to report as having failed the match.
  471.     % For the moment, we report any keys that are in the request
  472.     % and don't have the same value as in the original dictionary.
  473.       5 index 1 index .knownget
  474.        { 4 index 3 1 roll put }
  475.        { 3 index exch .undef }
  476.       ifelse
  477.        {    % Stack: <orig> <request> <merged> <failed> <attrkey>
  478.      3 index 1 index .knownget
  479.       { 5 index 2 index .knownget { ne } { pop //true } ifelse }
  480.       { //true }
  481.      ifelse        % Stack: ... <failed> <attrkey> <report>
  482.       { 2 copy /rangecheck put }
  483.      if pop
  484.        }
  485.       forall
  486.     }
  487.    ifelse
  488.  } bind def
  489.  
  490. % Apply Policies to any unprocessed failed requests.
  491. % As we process each request entry, we replace the error name
  492. % in the <failed> dictionary with the policy value,
  493. % and we replace the key in the <merged> dictionary with its prior value
  494. % (or remove it if it had no prior value).
  495. /.policyprocs mark
  496. % These procedures are called with the following on the stack:
  497. %   <orig> <merged> <failed> <Policies> <key> <policy>
  498. % They are expected to consume the top 2 operands.
  499. % NOTE: we currently treat all values other than 0, 1, or 7 (for PageSize)
  500. % the same as 0, i.e., we signal an error.
  501.   0 {        % Set errorinfo and signal a configurationerror.
  502.     pop dup 4 index exch get 2 array astore
  503.     $error /errorinfo 3 -1 roll put
  504.     cleartomark
  505.     /setpagedevice load /configurationerror signalerror
  506.   } bind
  507.   1 {        % Roll back the failed request to its previous status.
  508. SETPDDEBUG { (Rolling back.) = pstack flush } if
  509.     3 index 2 index 3 -1 roll put
  510.     4 index 1 index .knownget
  511.      { 4 index 3 1 roll put }
  512.      { 3 index exch .undef }
  513.     ifelse
  514.   } bind
  515.   7 {        % For PageSize only, just impose the request.
  516.     1 index /PageSize eq
  517.      { pop pop 1 index /PageSize 7 put }
  518.      { .policyprocs 0 get exec }
  519.     ifelse
  520.   } bind
  521. .dicttomark readonly def
  522. /.applypolicies        % <orig> <merged> <failed> .applypolicies
  523.             %   <orig> <merged'> <failed'>
  524.  { 1 index /Policies get 1 index
  525.     { type /integertype eq
  526.        { pop        % already processed
  527.        }
  528.        { 2 copy .knownget not { 1 index /PolicyNotFound get } if
  529.             % Stack: <orig> <merged> <failed> <Policies> <key>
  530.             %   <policy>
  531.      .policyprocs 1 index .knownget not { .policyprocs 0 get } if exec
  532.        }
  533.       ifelse
  534.     }
  535.    forall pop
  536.  } bind def
  537.  
  538. % Prepare to present parameters to the device, by spreading them onto the
  539. % operand stack and removing any that shouldn't be presented.
  540. /.prepareparams        % <params> .prepareparams -mark- <key1> <value1> ...
  541.  { mark exch dup
  542.     {            % Stack: -mark- key1 value1 ... merged key value
  543.       .presentspecial 2 index .knownget
  544.        { exec { 3 -1 roll } { pop pop } ifelse }
  545.        { 3 -1 roll }
  546.       ifelse
  547.     }
  548.    forall pop
  549.  } bind def
  550.  
  551. % Put device parameters without resetting currentpagedevice.
  552. % (.putdeviceparams clears the current page device.)
  553. /.putdeviceparamsonly    % <device> <Policies|null> <require_all> -mark-
  554.             %   <key1> <value1> ... .putdeviceparamsonly
  555.             % On success: <device> <eraseflag>
  556.             % On failure: <device> <Policies|null> <req_all> -mark-
  557.             %   <key1> <error1> ...
  558.  { .currentpagedevice
  559.     { counttomark 4 add 1 roll .putdeviceparams
  560.       dup type /booleantype eq { 3 } { counttomark 5 add } ifelse -1 roll
  561.       .setpagedevice
  562.     }
  563.     { pop .putdeviceparams
  564.     }
  565.    ifelse
  566.  } bind def
  567.  
  568. % Try setting the device parameters from the merged request.
  569. /.trysetparams        % <merged> <(ignored)> <device> <Policies>
  570.             %   .trysetparams
  571.  { //true 4 index .prepareparams
  572.             % Add the computed .MediaSize.
  573.             % Stack: merged (ignored) device Policies -true-
  574.             %   -mark- key1 value1 ...
  575.    counttomark 5 add index .computemediasize
  576.    exch pop exch pop /.MediaSize exch
  577. SETPDDEBUG { (Putting.) = pstack flush } if
  578.    .putdeviceparamsonly
  579. SETPDDEBUG { (Result of putting.) = pstack flush } if
  580.  } bind def
  581.  
  582. % Compute the media size and initial matrix from a merged request (after
  583. % media selection).
  584. /.computemediasize    % <request> .computemediasize
  585.             %   <request> <matrix> <[width height]>
  586.  { dup /PageSize get                    % requested page size
  587.    1 index /InputAttributes get
  588.      2 index (%MediaSource) get get /PageSize get    % media size
  589.                             % (may be a range)
  590.    2 index /Policies get
  591.      dup /PageSize .knownget
  592.       { exch pop } { /PolicyNotFound get } ifelse    % PageSize policy,
  593.                             % affects scaling
  594.    3 index /Orientation .knownget not { //null } if
  595.    4 index /RollFedMedia .knownget not { //false } if
  596.    matrix .matchpagesize not {
  597.         % This is a "can't happen" condition!
  598.      /setpagedevice load /rangecheck signalerror
  599.    } if
  600.    2 array astore
  601.  } bind def
  602.  
  603. % ---------------- setpagedevice itself ---------------- %
  604.  
  605. /setpagedevice
  606.  {        % We mustn't pop the argument until the very end,
  607.         % so that the pseudo-operator machinery can restore the stack
  608.         % if an error occurs.
  609.    mark 1 index currentpagedevice
  610.  
  611.         % Check whether we are changing OutputDevice;
  612.         % also handle the case where the current device
  613.         % is not a page device.
  614.         % Stack: mark <request> <current>
  615. SETPDDEBUG { (Checking.) = pstack flush } if
  616.  
  617.    dup /OutputDevice .knownget
  618.     {        % Current device is a page device.
  619.       2 index /OutputDevice .knownget
  620.        {    % A specific OutputDevice was requested.
  621.      2 copy eq
  622.       { pop pop //null }
  623.       { exch pop }
  624.      ifelse
  625.        }
  626.        { pop //null
  627.        }
  628.       ifelse
  629.     }
  630.     {        % Current device is not a page device.
  631.         % Use the default device.
  632.       1 index /OutputDevice .knownget not { .defaultdevicename } if
  633.     }
  634.    ifelse
  635.    dup //null eq
  636.     { pop
  637.     }
  638.     { exch pop .defaultdeviceparams
  639.         % In case of duplicate keys, .dicttomark takes the entry
  640.         % lower on the stack, so we can just append the defaults here.
  641.       .requiredattrs { exec } forall .dicttomark
  642.     }
  643.    ifelse
  644.  
  645.         % Check whether a viewer wants to intervene.
  646.         % We must check both the request (which takes precedence)
  647.         % and the current dictionary.
  648.         % Stack: mark <request> <orig>
  649.    exch dup /ViewerPreProcess .knownget
  650.     { exec }
  651.     { 1 index /ViewerPreProcess .knownget { exec } if }
  652.    ifelse exch
  653.  
  654.         % Construct a merged request from the actual request plus
  655.         % any keys that should always be propagated.
  656.         % Stack: mark <request> <orig>
  657. SETPDDEBUG { (Merging.) = pstack flush } if
  658.  
  659.    exch 1 index length 1 index length add dict
  660.    .copiedkeys
  661.     {        % Stack: <orig> <request> <merged> <key>
  662.       3 index 1 index .knownget { 3 copy put pop } if pop
  663.     }
  664.    forall
  665.         % Stack: <orig> <request> <merged>
  666.    dup 2 index
  667.     {        % stack: <orig> <request> <merged> <merged> <rkey> <rvalue>
  668.       .mergespecial 2 index .knownget { exec } if
  669.       put dup
  670.     }
  671.    forall pop
  672.         % Hack: if FIXEDRESOLUTION is true, discard any attempt to
  673.         % change HWResolution.
  674.    FIXEDRESOLUTION { dup /HWResolution .undef } if
  675.         % Hack: if FIXEDMEDIA is true, discard any attempt to change
  676.         % PageSize or HWSize.
  677.    FIXEDMEDIA
  678.     { dup /PageSize 4 index /PageSize get put
  679.       dup /HWSize 4 index /HWSize get put
  680.     } if
  681.         % Hack: to work around some files that take a PageSize
  682.         % from InputAttributes and impose it, discard any attempt
  683.         % to set PageSize to a 4-element value.
  684.         % Stack: mark <orig> <request> <merged>
  685.     dup /PageSize .knownget {
  686.       length 2 ne {
  687.     dup /PageSize 4 index /PageSize get put
  688.       } if
  689.     } if
  690.  
  691.         % Select input and output media.
  692.         % Stack: mark <orig> <request> <merged>
  693. SETPDDEBUG { (Selecting.) = pstack flush } if
  694.  
  695.    0 dict    % <failed>
  696.    1 index /InputAttributes .knownget
  697.     { 2 index /Policies get
  698.       .inputattrkeys (%MediaSource) cvn .selectmedia
  699.     } if
  700.    1 index /OutputAttributes .knownget
  701.     { 2 index /Policies get
  702.       .outputattrkeys (%MediaDestination) cvn .selectmedia
  703.      } if
  704.    3 -1 roll 4 1 roll        % temporarily swap orig & request
  705.    .applypolicies
  706.    3 -1 roll 4 1 roll        % swap back
  707.  
  708.         % Construct the new device, and attempt to set its attributes.
  709.         % Stack: mark <orig> <request> <merged> <failed>
  710. SETPDDEBUG { (Constructing.) = pstack flush } if
  711.  
  712.    currentdevice .devicename 2 index /OutputDevice get eq
  713.     { currentdevice }
  714.     { 1 index /OutputDevice get finddevice }
  715.    ifelse
  716.         %**************** We should copy the device here,
  717.         %**************** but since we can't close the old device,
  718.         %**************** we don't.  This is WRONG.
  719.     %****************copydevice
  720.    2 index /Policies get
  721.    .trysetparams
  722.    dup type /booleantype ne
  723.     {        % The request failed.
  724.         % Stack: ... <orig> <request> <merged> <failed> <device>
  725.         %   <Policies> true mark <name> <errorname> ...
  726. SETPDDEBUG { (Recovering.) = pstack flush } if
  727.       counttomark 4 add index
  728.       counttomark 2 idiv { dup 4 -2 roll put } repeat
  729.       pop pop pop
  730.         % Stack: mark ... <orig> <request> <merged> <failed> <device>
  731.         %   <Policies>
  732.       6 2 roll 3 -1 roll 4 1 roll
  733.       .applypolicies
  734.       3 -1 roll 4 1 roll 6 -2 roll
  735.       .trysetparams        % shouldn't fail!
  736.       dup type /booleantype ne
  737.        { 2 { counttomark 1 add 1 roll cleartomark } repeat
  738.          /setpagedevice load exch signalerror
  739.        }
  740.       if
  741.     }
  742.    if
  743.  
  744.         % The attempt succeeded.  Install the new device.
  745.         % Stack: mark ... <merged> <failed> <device> <eraseflag>
  746. SETPDDEBUG { (Installing.) = pstack flush } if
  747.  
  748.    pop 2 .endpage
  749.     { 1 //true .outputpage
  750.       (>>setpagedevice, press <return> to continue<<\n) .confirm
  751.     }
  752.    if
  753.         % .setdevice clears the current page device!
  754.    .currentpagedevice pop exch
  755.    .setdevice pop
  756.    .setpagedevice
  757.  
  758.         % Implement UseCIEColor directly if this is a LL3 system.
  759.         % The color substitution feature is now implemented in
  760.         % the interpreter, and this is used as an optimization.
  761.         %
  762.         % NB: This shoud be the only use of the .setuseciecolor
  763.         %     operator anywhere.
  764.         %
  765.         % If UseCIEColor is transitioned to false, set some
  766.         % color space other than /DeviceGray, to insure that
  767.         % initgraphics will actually perform a setcolorspace
  768.         % operation (there is an optimization in setcolorspace
  769.         % that does nothing if the operand and current color
  770.         % spaces are the same, and UseCIEColor is false).
  771.  
  772.     /.setuseciecolor where
  773.       {
  774.         pop 1 index /UseCIEColor .knownget
  775.           {
  776.             dup .setuseciecolor not
  777.               { /DeviceRGB setcolorspace }
  778.             if
  779.           }
  780.         if
  781.       }
  782.     if
  783.  
  784.         % Merge the request into the current page device,
  785.         % unless we're changing the OutputDevice.
  786.         % Stack: mark ... <merged> <failed>
  787.    exch currentpagedevice dup length 2 index length add dict
  788.         % Stack: mark ... <failed> <merged> <current> <newdict>
  789.    2 index /OutputDevice .knownget {
  790.      2 index /OutputDevice .knownget not { //null } if eq
  791.    } {
  792.      //true
  793.    } ifelse {
  794.         % Same OutputDevice, merge the dictionaries.
  795.      .copydict
  796.    } {
  797.         % Different OutputDevice, discard the old dictionary.
  798.      exch pop
  799.    } ifelse .copydict
  800.         % Initialize the default matrix, taking media matching
  801.         % into account.
  802.    .computemediasize pop initmatrix concat
  803.    dup /PageOffset .knownget
  804.     {        % Translate by the given number of 1/72" units in device X/Y.
  805.       dup 0 get exch 1 get
  806.       2 index /HWResolution get dup 1 get exch 0 get
  807.       4 -1 roll mul 72 div   3 1 roll mul 72 div
  808.       idtransform translate
  809.     }
  810.    if
  811.         % We must install the new page device dictionary
  812.         % before calling the Install procedure.
  813.   dup .setpagedevice
  814.   .setdefaulthalftone    % Set the default screen before calling Install.
  815.   dup /Install .knownget {
  816.     { .execinstall } stopped { .postinstall stop } { .postinstall } ifelse
  817.   } {
  818.     .postinstall
  819.   } ifelse
  820. } odef
  821.  
  822. % We break out the code after calling the Install procedure into a
  823. % separate procedure, since it is executed even if Install causes an error.
  824. % By making .execinstall a separate operator procedure, we get the stacks
  825. % restored if it fails.
  826.  
  827. /.execinstall {        % <proc> .execinstall -
  828.     % Because the interpreter optimizes tail calls, we can't just let
  829.     % the body of this procedure be 'exec', because that would lose
  830.     % the stack protection that is the whole reason for having the
  831.     % procedure in the first place.  We hack this by adding a couple
  832.     % of extra tokens to ensure that the operator procedure is still
  833.     % on the stack during the exec.
  834.   exec
  835.   0 pop    % See above.
  836. } odef
  837. /.postinstall {        % mark ... <failed> <merged> .postinstall -
  838.    matrix currentmatrix .setdefaultmatrix
  839.         % Erase and initialize the page.
  840.    initgraphics
  841.    currentoverprint //false setoverprint 1 setcolor
  842.    .fillpage
  843.    0 setcolor setoverprint
  844.    .beginpage
  845.  
  846.         % Clean up, calling PolicyReport if needed.
  847.         % Stack: mark ... <failed> <merged>
  848. SETPDDEBUG { (Finishing.) = pstack flush } if
  849.  
  850.    exch dup length 0 ne
  851.     { 1 index /Policies get /PolicyReport get
  852.       counttomark 1 add 2 roll cleartomark
  853.       exec
  854.     }
  855.     { cleartomark
  856.     }
  857.    ifelse pop
  858.  
  859. } odef
  860.  
  861. end                % level2dict
  862. .setlanguagelevel
  863.