home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2003 #12 / K-CD-12-2003.ISO / pdf995 / res / convert / gs_setpd.ps < prev    next >
Encoding:
Text File  |  2002-02-23  |  24.7 KB  |  775 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.8.2.2 2002/02/22 19:45:55 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.       DEBUG { (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 define InputAttributes and OutputAttributes with a single
  180. % dummy media type that handles pages of any size.
  181. % Devices that care will override this.
  182.   /InputAttributes {
  183.     mark 0
  184.     % Since sizes match within 5 user units, we need to set the smallest
  185.     % PageSize to 6 units so that [0 0] will fail.
  186.     mark /PageSize [6 dup 16#7ffff dup] .dicttomark
  187.     .dicttomark
  188.   }
  189.   (%MediaSource) 0
  190.   /OutputAttributes {
  191.     mark 0 mark .dicttomark readonly .dicttomark
  192.   }
  193.   (%MediaDestination) 0
  194.   /Install {{.callinstall}} bind
  195.   /BeginPage {{.callbeginpage}} bind
  196.   /EndPage {{.callendpage}} bind
  197.   /Policies .defaultpolicies
  198. .dicttomark readonly def
  199.  
  200. % Define currentpagedevice so it creates the dictionary on demand if needed,
  201. % adding all the required entries defined just above.
  202. % We have to deal specially with entries that the driver may change
  203. % on its own.
  204. /.dynamicppkeys mark
  205.   /.MediaSize dup        % because it changes when PageSize is set
  206.   /PageCount dup
  207. .dicttomark readonly def
  208. /.makecurrentpagedevice {    % - .makecurrentpagedevice <dict>
  209.   currentdevice null .getdeviceparams
  210.     % Make the dictionary large enough to add defaulted entries.
  211.   counttomark 2 idiv .requiredattrs length add dict
  212.   counttomark 2 idiv { dup 4 2 roll put } repeat exch pop
  213.     % Add any missing required attributes.
  214.     % Make a writable and (if possible) local copy of any default
  215.     % dictionaries, to work around a bug in the output of WordPerfect,
  216.     % which assumes that these dictionaries are writable and local.
  217.   .currentglobal exch dup gcheck .setglobal
  218.   .requiredattrs {
  219.     2 index 2 index known {
  220.       pop pop
  221.     } {
  222.       exec 2 index 3 1 roll put
  223.     } ifelse
  224.   } forall exch .setglobal
  225.   dup .setpagedevice
  226. } bind def
  227. /currentpagedevice {
  228.   .currentpagedevice {
  229.     dup length 0 eq {
  230.       pop .makecurrentpagedevice
  231.     } {
  232.         % If any of the dynamic keys have changed,
  233.         % we must update the page device dictionary.
  234.       currentdevice //.dynamicppkeys .getdeviceparams .dicttomark {
  235.         % Stack: current key value
  236.         2 index 2 index .knownget { 1 index ne } { true } ifelse
  237.          { 2 index wcheck not
  238.         {    % This is the first entry being updated.
  239.             % Copy the dictionary to make it writable.
  240.           3 -1 roll dup length dict .copydict
  241.           3 1 roll
  242.         }
  243.            if
  244.            2 index 3 1 roll put
  245.          }
  246.          { pop pop
  247.          }
  248.         ifelse
  249.       } forall
  250.         % If the dictionary was global and is now local, copy
  251.         % any global subsidiary dictionaries to local VM.  This
  252.         % too is to work around the Word Perfect bug (see above).
  253.       dup gcheck not {
  254.     dup {
  255.       dup type /dicttype eq { dup gcheck } { false } ifelse {
  256.         % Copy-on-write, see above.
  257.         2 index wcheck not {
  258.           3 -1 roll dup length dict .copydict
  259.           3 1 roll
  260.         } if
  261.         .copytree 2 index 3 1 roll put
  262.       } {
  263.         pop pop
  264.       } ifelse
  265.     } forall
  266.       } if
  267.         % We would like to do a .setpagedevice so we don't keep
  268.         % re-creating the dictionary.  Unfortunately, the effect
  269.         % of this is that if any dynamic key changes (PageCount
  270.         % in particular), we will do the equivalent of a
  271.         % setpagedevice at the next restore or grestore.
  272.         % Therefore, we make the dictionary read-only, but
  273.         % we don't store it away.  I.e., NOT:
  274.         % dup wcheck { .setpagedevice .currentpagedevice pop } if
  275.       readonly
  276.     } ifelse
  277.   } if
  278. } bind odef
  279.  
  280. % Copy a dictionary recursively.
  281. /.copytree {    % <dict> .copytree <dict'>
  282.   dup length dict exch {
  283.     dup type /dicttype eq { .copytree } if 2 index 3 1 roll put
  284.   } forall
  285. } bind def
  286.  
  287. % The implementation of setpagedevice is quite complex.  Currently,
  288. % everything but the media matching algorithm is implemented here.
  289.  
  290. % By default, we only present the requested changes to the device,
  291. % but there are some parameters that require special merging action.
  292. % Define those parameters here, with the procedures that do the merging.
  293. % The procedures are called as follows:
  294. %    <merged> <key> <new_value> -proc- <merged> <key> <new_value'>
  295. /.mergespecial mark
  296.   /InputAttributes
  297.    { dup null eq
  298.       { pop null
  299.       }
  300.       { 3 copy pop .knownget
  301.      { dup null eq
  302.         { pop dup length dict }
  303.         { dup length 2 index length add dict .copydict }
  304.        ifelse
  305.      }
  306.      { dup length dict
  307.      }
  308.         ifelse .copydict readonly
  309.       }
  310.      ifelse
  311.    } bind
  312.   /OutputAttributes 1 index
  313.   /Policies
  314.     { 3 copy pop .knownget
  315.        { dup length 2 index length add dict .copydict }
  316.        { dup length dict }
  317.       ifelse copy readonly
  318.     } bind
  319. .dicttomark readonly def
  320.  
  321. % Define the keys used in input attribute matching.
  322. /.inputattrkeys [
  323.   /PageSize /MediaColor /MediaWeight /MediaType /InsertSheet
  324.     % The following are documented in Adobe's supplement for v2017.
  325.   /LeadingEdge /MediaClass
  326. ] readonly def
  327. % Define other keys used in media selection.
  328. /.inputselectionkeys [
  329.   /MediaPosition /Orientation
  330. ] readonly def
  331.  
  332. % Define the keys used in output attribute matching.
  333. /.outputattrkeys [
  334.   /OutputType
  335. ] readonly def
  336.  
  337. % Define all the parameters that should always be copied to the merged
  338. % dictionary.
  339. /.copiedkeys [
  340.   /OutputDevice
  341.   .mergespecial { pop } forall
  342.   .inputattrkeys aload pop
  343.   .inputselectionkeys aload pop
  344.   .outputattrkeys aload pop
  345. ] readonly def
  346.  
  347. % Define the parameters that should not be presented to the device.
  348. % The procedures are called as follows:
  349. %    <merged> <key> <value> -proc-
  350. % The procedure leaves all its operands on the stack and returns
  351. % true iff the key/value pair should be presented to .putdeviceparams.
  352. /.presentspecial mark
  353.   .dynamicppkeys { pop false } forall
  354.             % We must ignore an explicit request for .MediaSize,
  355.             % because media matching always handles this.
  356.   /.MediaSize false
  357.   /Name false
  358.   /OutputDevice false
  359.   /PageDeviceName false
  360.   /PageOffset false
  361.   /PageSize false        % obsolete alias for .MediaSize
  362.   /InputAttributes false
  363.   .inputattrkeys
  364.     { dup /PageSize eq
  365.        { pop }
  366.        { { 2 index /InputAttributes .knownget { null eq } { true } ifelse } }
  367.       ifelse
  368.     }
  369.   forall
  370.   .inputselectionkeys { false } forall
  371.   /OutputAttributes false
  372.   .outputattrkeys
  373.     { { 2 index /OutputAttributes .knownget { null eq } { true } ifelse } }
  374.   forall
  375.   /Install false
  376.   /BeginPage false
  377.   /EndPage false
  378.   /Policies false
  379.     % Our extensions:
  380.   /HWColorMap
  381.     {            % HACK: don't transmit the color map, because
  382.             % window systems can change the color map on their own
  383.             % incrementally.  Someday we'll have a better
  384.             % solution for this....
  385.       false
  386.     }
  387.   /ViewerPreProcess false
  388. .dicttomark readonly def
  389.  
  390. % Define access to device defaults.
  391. /.defaultdeviceparams
  392.  { finddevice null .getdeviceparams
  393.  } bind def
  394.  
  395. % Select media (input or output).  The hard work is done in an operator:
  396. %    <pagedict> <attrdict> <policydict> <keys> .matchmedia <key> true
  397. %    <pagedict> <attrdict> <policydict> <keys> .matchmedia false
  398. %    <pagedict> null <policydict> <keys> .matchmedia null true
  399. /.selectmedia        % <orig> <request> <merged> <failed>     <-- retained
  400.             %   <attrdict> <policydict> <attrkeys> <mediakey>
  401.             %   .selectmedia
  402.  { 5 index 5 -2 roll 4 index .matchmedia
  403.         % Stack: orig request merged failed attrkeys mediakey
  404.         %   (key true | false)
  405.     { 4 index 3 1 roll put pop
  406.     }
  407.     {    % Adobe's implementations have a "big hairy heuristic"
  408.     % to choose the set of keys to report as having failed the match.
  409.     % For the moment, we report any keys that are in the request
  410.     % and don't have the same value as in the original dictionary.
  411.       5 index 1 index .knownget
  412.        { 4 index 3 1 roll put }
  413.        { 3 index exch .undef }
  414.       ifelse
  415.        {    % Stack: <orig> <request> <merged> <failed> <attrkey>
  416.      3 index 1 index .knownget
  417.       { 5 index 2 index .knownget { ne } { pop true } ifelse }
  418.       { true }
  419.      ifelse        % Stack: ... <failed> <attrkey> <report>
  420.       { 2 copy /rangecheck put }
  421.      if pop
  422.        }
  423.       forall
  424.     }
  425.    ifelse
  426.  } bind def
  427.  
  428. % Apply Policies to any unprocessed failed requests.
  429. % As we process each request entry, we replace the error name
  430. % in the <failed> dictionary with the policy value,
  431. % and we replace the key in the <merged> dictionary with its prior value
  432. % (or remove it if it had no prior value).
  433. /.policyprocs mark
  434. % These procedures are called with the following on the stack:
  435. %   <orig> <merged> <failed> <Policies> <key> <policy>
  436. % They are expected to consume the top 2 operands.
  437. % NOTE: we currently treat all values other than 0, 1, or 7 (for PageSize)
  438. % the same as 0, i.e., we signal an error.
  439.   0 {        % Set errorinfo and signal a configurationerror.
  440.     pop dup 4 index exch get 2 array astore
  441.     $error /errorinfo 3 -1 roll put
  442.     cleartomark
  443.     /setpagedevice load /configurationerror signalerror
  444.   } bind
  445.   1 {        % Roll back the failed request to its previous status.
  446. DEBUG { (Rolling back.) = pstack flush } if
  447.     3 index 2 index 3 -1 roll put
  448.     4 index 1 index .knownget
  449.      { 4 index 3 1 roll put }
  450.      { 3 index exch .undef }
  451.     ifelse
  452.   } bind
  453.   7 {        % For PageSize only, just impose the request.
  454.     1 index /PageSize eq
  455.      { pop pop 1 index /PageSize 7 put }
  456.      { .policyprocs 0 get exec }
  457.     ifelse
  458.   } bind
  459. .dicttomark readonly def
  460. /.applypolicies        % <orig> <merged> <failed> .applypolicies
  461.             %   <orig> <merged'> <failed'>
  462.  { 1 index /Policies get 1 index
  463.     { type /integertype eq
  464.        { pop        % already processed
  465.        }
  466.        { 2 copy .knownget not { 1 index /PolicyNotFound get } if
  467.             % Stack: <orig> <merged> <failed> <Policies> <key>
  468.             %   <policy>
  469.      .policyprocs 1 index .knownget not { .policyprocs 0 get } if exec
  470.        }
  471.       ifelse
  472.     }
  473.    forall pop
  474.  } bind def
  475.  
  476. % Prepare to present parameters to the device, by spreading them onto the
  477. % operand stack and removing any that shouldn't be presented.
  478. /.prepareparams        % <params> .prepareparams -mark- <key1> <value1> ...
  479.  { mark exch dup
  480.     {            % Stack: -mark- key1 value1 ... merged key value
  481.       .presentspecial 2 index .knownget
  482.        { exec { 3 -1 roll } { pop pop } ifelse }
  483.        { 3 -1 roll }
  484.       ifelse
  485.     }
  486.    forall pop
  487.  } bind def
  488.  
  489. % Put device parameters without resetting currentpagedevice.
  490. % (.putdeviceparams clears the current page device.)
  491. /.putdeviceparamsonly    % <device> <Policies|null> <require_all> -mark-
  492.             %   <key1> <value1> ... .putdeviceparamsonly
  493.             % On success: <device> <eraseflag>
  494.             % On failure: <device> <Policies|null> <req_all> -mark-
  495.             %   <key1> <error1> ...
  496.  { .currentpagedevice
  497.     { counttomark 4 add 1 roll .putdeviceparams
  498.       dup type /booleantype eq { 3 } { counttomark 5 add } ifelse -1 roll
  499.       .setpagedevice
  500.     }
  501.     { pop .putdeviceparams
  502.     }
  503.    ifelse
  504.  } bind def
  505.  
  506. % Try setting the device parameters from the merged request.
  507. /.trysetparams        % <merged> <(ignored)> <device> <Policies>
  508.             %   .trysetparams
  509.  { true 4 index .prepareparams
  510.             % Add the computed .MediaSize.
  511.             % Stack: merged (ignored) device Policies -true-
  512.             %   -mark- key1 value1 ...
  513.    counttomark 5 add index .computemediasize
  514.    exch pop exch pop /.MediaSize exch
  515. DEBUG { (Putting.) = pstack flush } if
  516.    .putdeviceparamsonly
  517. DEBUG { (Result of putting.) = pstack flush } if
  518.  } bind def
  519.  
  520. % Compute the media size and initial matrix from a merged request (after
  521. % media selection).
  522. /.computemediasize    % <request> .computemediasize
  523.             %   <request> <matrix> <[width height]>
  524.  { dup /PageSize get                    % requested page size
  525.    1 index /InputAttributes get
  526.      2 index (%MediaSource) get get /PageSize get    % media size
  527.                             % (may be a range)
  528.    2 index /Policies get
  529.      dup /PageSize .knownget
  530.       { exch pop } { /PolicyNotFound get } ifelse    % PageSize policy,
  531.                             % affects scaling
  532.    3 index /Orientation .knownget not { null } if
  533.    4 index /RollFedMedia .knownget not { false } if
  534.    matrix .matchpagesize not {
  535.         % This is a "can't happen" condition!
  536.      /setpagedevice load /rangecheck signalerror
  537.    } if
  538.    2 array astore
  539.  } bind def
  540.  
  541. % ---------------- setpagedevice itself ---------------- %
  542.  
  543. /setpagedevice
  544.  {        % We mustn't pop the argument until the very end,
  545.         % so that the pseudo-operator machinery can restore the stack
  546.         % if an error occurs.
  547.    mark 1 index currentpagedevice
  548.  
  549.         % Check whether we are changing OutputDevice;
  550.         % also handle the case where the current device
  551.         % is not a page device.
  552.         % Stack: mark <request> <current>
  553. DEBUG { (Checking.) = pstack flush } if
  554.  
  555.    dup /OutputDevice .knownget
  556.     {        % Current device is a page device.
  557.       2 index /OutputDevice .knownget
  558.        {    % A specific OutputDevice was requested.
  559.      2 copy eq
  560.       { pop pop null }
  561.       { exch pop }
  562.      ifelse
  563.        }
  564.        { pop null
  565.        }
  566.       ifelse
  567.     }
  568.     {        % Current device is not a page device.
  569.         % Use the default device.
  570.       1 index /OutputDevice .knownget not { .defaultdevicename } if
  571.     }
  572.    ifelse
  573.    dup null eq
  574.     { pop
  575.     }
  576.     { exch pop .defaultdeviceparams
  577.         % In case of duplicate keys, .dicttomark takes the entry
  578.         % lower on the stack, so we can just append the defaults here.
  579.       .requiredattrs { exec } forall .dicttomark
  580.     }
  581.    ifelse
  582.  
  583.         % Check whether a viewer wants to intervene.
  584.         % We must check both the request (which takes precedence)
  585.         % and the current dictionary.
  586.         % Stack: mark <request> <orig>
  587.    exch dup /ViewerPreProcess .knownget
  588.     { exec }
  589.     { 1 index /ViewerPreProcess .knownget { exec } if }
  590.    ifelse exch
  591.  
  592.         % Construct a merged request from the actual request plus
  593.         % any keys that should always be propagated.
  594.         % Stack: mark <request> <orig>
  595. DEBUG { (Merging.) = pstack flush } if
  596.  
  597.    exch 1 index length 1 index length add dict
  598.    .copiedkeys
  599.     {        % Stack: <orig> <request> <merged> <key>
  600.       3 index 1 index .knownget { 3 copy put pop } if pop
  601.     }
  602.    forall
  603.         % Stack: <orig> <request> <merged>
  604.    dup 2 index
  605.     {        % stack: <orig> <request> <merged> <merged> <rkey> <rvalue>
  606.       .mergespecial 2 index .knownget { exec } if
  607.       put dup
  608.     }
  609.    forall pop
  610.         % Hack: if FIXEDRESOLUTION is true, discard any attempt to
  611.         % change HWResolution.
  612.    FIXEDRESOLUTION { dup /HWResolution .undef } if
  613.         % Hack: if FIXEDMEDIA is true, discard any attempt to change
  614.         % PageSize or HWSize.
  615.    FIXEDMEDIA
  616.     { dup /PageSize 4 index /PageSize get put
  617.       dup /HWSize 4 index /HWSize get put
  618.     } if
  619.         % Hack: to work around some files that take a PageSize
  620.         % from InputAttributes and impose it, discard any attempt
  621.         % to set PageSize to a 4-element value.
  622.         % Stack: mark <orig> <request> <merged>
  623.     dup /PageSize .knownget {
  624.       length 2 ne {
  625.     dup /PageSize 4 index /PageSize get put
  626.       } if
  627.     } if
  628.  
  629.         % Select input and output media.
  630.         % Stack: mark <orig> <request> <merged>
  631. DEBUG { (Selecting.) = pstack flush } if
  632.  
  633.    0 dict    % <failed>
  634.    1 index /InputAttributes .knownget
  635.     { 2 index /Policies get
  636.       .inputattrkeys (%MediaSource) cvn .selectmedia
  637.     } if
  638.    1 index /OutputAttributes .knownget
  639.     { 2 index /Policies get
  640.       .outputattrkeys (%MediaDestination) cvn .selectmedia
  641.      } if
  642.    3 -1 roll 4 1 roll        % temporarily swap orig & request
  643.    .applypolicies
  644.    3 -1 roll 4 1 roll        % swap back
  645.  
  646.         % Construct the new device, and attempt to set its attributes.
  647.         % Stack: mark <orig> <request> <merged> <failed>
  648. DEBUG { (Constructing.) = pstack flush } if
  649.  
  650.    currentdevice .devicename 2 index /OutputDevice get eq
  651.     { currentdevice }
  652.     { 1 index /OutputDevice get finddevice }
  653.    ifelse
  654.         %**************** We should copy the device here,
  655.         %**************** but since we can't close the old device,
  656.         %**************** we don't.  This is WRONG.
  657.     %****************copydevice
  658.    2 index /Policies get
  659.    .trysetparams
  660.    dup type /booleantype ne
  661.     {        % The request failed.
  662.         % Stack: ... <orig> <request> <merged> <failed> <device>
  663.         %   <Policies> true mark <name> <errorname> ...
  664. DEBUG { (Recovering.) = pstack flush } if
  665.       counttomark 4 add index
  666.       counttomark 2 idiv { dup 4 -2 roll put } repeat
  667.       pop pop pop
  668.         % Stack: mark ... <orig> <request> <merged> <failed> <device>
  669.         %   <Policies>
  670.       6 2 roll 3 -1 roll 4 1 roll
  671.       .applypolicies
  672.       3 -1 roll 4 1 roll 6 -2 roll
  673.       .trysetparams        % shouldn't fail!
  674.       dup type /booleantype ne
  675.        { 2 { counttomark 1 add 1 roll cleartomark } repeat
  676.          /setpagedevice load exch signalerror
  677.        }
  678.       if
  679.     }
  680.    if
  681.  
  682.         % The attempt succeeded.  Install the new device.
  683.         % Stack: mark ... <merged> <failed> <device> <eraseflag>
  684. DEBUG { (Installing.) = pstack flush } if
  685.  
  686.    pop 2 .endpage
  687.     { 1 true .outputpage
  688.       (>>setpagedevice, press <return> to continue<<\n) .confirm
  689.     }
  690.    if
  691.         % .setdevice clears the current page device!
  692.    .currentpagedevice pop exch
  693.    .setdevice pop
  694.    .setpagedevice
  695.  
  696.         % Merge the request into the current page device,
  697.         % unless we're changing the OutputDevice.
  698.         % Stack: mark ... <merged> <failed>
  699.    exch currentpagedevice dup length 2 index length add dict
  700.         % Stack: mark ... <failed> <merged> <current> <newdict>
  701.    2 index /OutputDevice .knownget {
  702.      2 index /OutputDevice .knownget not { null } if eq
  703.    } {
  704.      true
  705.    } ifelse {
  706.         % Same OutputDevice, merge the dictionaries.
  707.      .copydict
  708.    } {
  709.         % Different OutputDevice, discard the old dictionary.
  710.      exch pop
  711.    } ifelse .copydict
  712.         % Initialize the default matrix, taking media matching
  713.         % into account.
  714.    .computemediasize pop initmatrix concat
  715.    dup /PageOffset .knownget
  716.     {        % Translate by the given number of 1/72" units in device X/Y.
  717.       dup 0 get exch 1 get
  718.       2 index /HWResolution get dup 1 get exch 0 get
  719.       4 -1 roll mul 72 div   3 1 roll mul 72 div
  720.       idtransform translate
  721.     }
  722.    if
  723.         % We must install the new page device dictionary
  724.         % before calling the Install procedure.
  725.   dup .setpagedevice
  726.   .setdefaultscreen    % Set the default screen before calling Install.
  727.   dup /Install .knownget {
  728.     { .execinstall } stopped { .postinstall stop } { .postinstall } ifelse
  729.   } {
  730.     .postinstall
  731.   } ifelse
  732. } odef
  733.  
  734. % We break out the code after calling the Install procedure into a
  735. % separate procedure, since it is executed even if Install causes an error.
  736. % By making .execinstall a separate operator procedure, we get the stacks
  737. % restored if it fails.
  738.  
  739. /.execinstall {        % <proc> .execinstall -
  740.     % Because the interpreter optimizes tail calls, we can't just let
  741.     % the body of this procedure be 'exec', because that would lose
  742.     % the stack protection that is the whole reason for having the
  743.     % procedure in the first place.  We hack this by adding a couple
  744.     % of extra tokens to ensure that the operator procedure is still
  745.     % on the stack during the exec.
  746.   exec
  747.   0 pop    % See above.
  748. } odef
  749. /.postinstall {        % mark ... <failed> <merged> .postinstall -
  750.    matrix currentmatrix .setdefaultmatrix
  751.         % Erase and initialize the page.
  752.    erasepage initgraphics
  753.    .beginpage
  754.  
  755.         % Clean up, calling PolicyReport if needed.
  756.         % Stack: mark ... <failed> <merged>
  757. DEBUG { (Finishing.) = pstack flush } if
  758.  
  759.    exch dup length 0 ne
  760.     { 1 index /Policies get /PolicyReport get
  761.       counttomark 1 add 2 roll cleartomark
  762.       exec
  763.     }
  764.     { cleartomark
  765.     }
  766.    ifelse pop
  767.  
  768. } odef
  769.  
  770. end                % level2dict
  771. .setlanguagelevel
  772.