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

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