home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / gs-esp / 8.15 / lib / gs_setpd.ps < prev    next >
Encoding:
Text File  |  2006-07-06  |  29.1 KB  |  920 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.   % M. Sweet, Easy Software Products
  165.   %
  166.   % Due to the fact that it is not possible to properly implement
  167.   % the selection policies from a Ghostscript driver, we have changed
  168.   % the default policy to "7" (impose) to avoid numerous problems with
  169.   % printing within CUPS...
  170.   %
  171.   % /PolicyNotFound 1
  172.   % /PageSize 0
  173.   /PolicyNotFound 7
  174.   /PageSize 7
  175.   /PolicyReport {
  176.     dup /.LockSafetyParams known {
  177.     % Only possible error is invalidaccess
  178.       /setpagedevice .systemvar /invalidaccess signalerror
  179.     }
  180.     if
  181.     pop
  182.   } bind
  183. .dicttomark readonly def
  184. % Note that the values of .requiredattrs are executed, not just fetched.
  185. /.requiredattrs mark
  186.   /PageDeviceName //null
  187.   /PageOffset [0 0] readonly
  188. % We populate InputAttributes with all of the known page sizes 
  189. % followed by a dummy media type that handles pages of any size.
  190. % This will create some duplicates, but that only slightly slows
  191. % down the media selection (loop is in zmedia2.c).
  192. %
  193. % Some PostScript creators assume that slot 0 is the default media
  194. % size and some can't handle a non-standard 4-element array which
  195. % is a 'range' type page size (always put last).
  196. %
  197. % Real Devices that can only handle specific page sizes will override this.
  198.   /InputAttributes {
  199.     mark
  200.     % First put the device's default page size in slot 0
  201.     % This satifies those that have devices built with a4 as the default
  202.     0 mark /PageSize currentdevice /PageSize gsgetdeviceprop .dicttomark
  203.     statusdict /.pagetypenames get {
  204.       counttomark 1 sub 2 idiv exch mark exch /PageSize exch
  205.       % stack: mark --dict-- --dict-- ... key mark /PageSize pagetypename
  206.       % see note above about pagetype executable array contents.
  207.       load dup 0 get exch 1 get 2 array astore .dicttomark
  208.     } forall
  209.     % If NORANGEPAGESIZE is defined, (-dNORANGEPAGESIZE), then don't add
  210.     % the 'match any' PageSize entry
  211.     systemdict /NORANGEPAGESIZE known not {
  212.     % Add one last entry which is the 4 element range array (non-standard)
  213.     counttomark 2 idiv 
  214.     % PageSize with either dimension 0 will be detected in
  215.     % match_page_size, so we can allow it here
  216.     mark /PageSize [0 dup 16#7ffff dup] .dicttomark
  217.     } if
  218.     .dicttomark
  219.   }
  220.   (%MediaSource) 0
  221.   /OutputAttributes {
  222.     mark 0 mark .dicttomark readonly .dicttomark
  223.   }
  224.   (%MediaDestination) 0
  225.   /Install {{.callinstall}} bind
  226.   /BeginPage {{.callbeginpage}} bind
  227.   /EndPage {{.callendpage}} bind
  228.   /Policies .defaultpolicies
  229.   /ImagingBBox //null        % default value
  230.   /UseCIEColor /.getuseciecolor load
  231. .dicttomark readonly def
  232.  
  233. % Define currentpagedevice so it creates the dictionary on demand if needed,
  234. % adding all the required entries defined just above.
  235. % We have to deal specially with entries that the driver may change
  236. % on its own.
  237. /.dynamicppkeys mark
  238.   /.MediaSize dup        % because it changes when PageSize is set
  239.   /PageCount dup
  240.   /Colors dup
  241.   /BitsPerPixel dup
  242.   /ColorValues dup
  243.   % M. Sweet, Easy Software Products
  244.   %
  245.   % The following keys are commonly set by applications and should never
  246.   % fail...
  247.   /ManualFeed dup
  248.   /Duplex dup
  249.   /Tumble dup
  250. .dicttomark readonly def
  251. /.makecurrentpagedevice {    % - .makecurrentpagedevice <dict>
  252.   currentdevice //null .getdeviceparams
  253.     % Make the dictionary large enough to add defaulted entries.
  254.   counttomark 2 idiv .requiredattrs length add dict
  255.   counttomark 2 idiv { dup 4 2 roll put } repeat exch pop
  256.     % Add any missing required attributes.
  257.     % Make a writable and (if possible) local copy of any default
  258.     % dictionaries, to work around a bug in the output of WordPerfect,
  259.     % which assumes that these dictionaries are writable and local.
  260.   .currentglobal exch dup gcheck .setglobal
  261.   .requiredattrs {
  262.     2 index 2 index known {
  263.       1 index /Policies eq {
  264.     % Merge policies from the device driver with defaults
  265.     2 index             % <<>> /key value <<>>
  266.     3 2 roll get        % <<>> value <<policies>>
  267.     exch {                
  268.       2 index 2 index known { 
  269.         pop pop
  270.       } { 
  271.         2 index 3 1 roll put
  272.       } ifelse
  273.     } forall
  274.     pop
  275.       } {
  276.     pop pop
  277.       } ifelse
  278.     } {
  279.       exec 2 index 3 1 roll put
  280.     } ifelse
  281.   } forall exch .setglobal
  282.   dup .setpagedevice
  283. } bind def
  284. /currentpagedevice {
  285.   .currentpagedevice {
  286.     dup length 0 eq {
  287.       pop .makecurrentpagedevice
  288.     } {
  289.         % If any of the dynamic keys have changed,
  290.         % we must update the page device dictionary.
  291.       currentdevice //.dynamicppkeys .getdeviceparams .dicttomark {
  292.         % Stack: current key value
  293.         2 index 2 index .knownget { 1 index ne } { //true } ifelse
  294.          { 2 index wcheck not
  295.         {    % This is the first entry being updated.
  296.             % Copy the dictionary to make it writable.
  297.           3 -1 roll
  298.           currentglobal 1 index dup gcheck currentglobal and setglobal
  299.           length dict
  300.           exch setglobal
  301.           .copydict
  302.           3 1 roll
  303.         }
  304.            if
  305.            2 index 3 1 roll put
  306.          }
  307.          { pop pop
  308.          }
  309.         ifelse
  310.       } forall
  311.       % If the device is the distiller device, update distillerparams that
  312.       % may have been changed by setdistillerparams
  313.       currentdevice .devicename /pdfwrite eq {
  314.     currentdistillerparams {
  315.           % Stack: current key value
  316.       2 index 2 index .knownget { 1 index ne } { //true } ifelse
  317.       { 2 index 3 1 roll put } { pop pop } ifelse
  318.     } forall
  319.       } if
  320.         % If the dictionary was global and is now local, copy
  321.         % any global subsidiary dictionaries to local VM.  This
  322.         % too is to work around the Word Perfect bug (see above).
  323.       dup gcheck not {
  324.     dup {
  325.       dup type /dicttype eq { dup gcheck } { //false } ifelse {
  326.         % Copy-on-write, see above.
  327.         2 index wcheck not {
  328.           3 -1 roll dup length dict .copydict
  329.           3 1 roll
  330.         } if
  331.         .copytree 2 index 3 1 roll put
  332.       } {
  333.         pop pop
  334.       } ifelse
  335.     } forall
  336.       } if
  337.         % We would like to do a .setpagedevice so we don't keep
  338.         % re-creating the dictionary.  Unfortunately, the effect
  339.         % of this is that if any dynamic key changes (PageCount
  340.         % in particular), we will do the equivalent of a
  341.         % setpagedevice at the next restore or grestore.
  342.         % Therefore, we make the dictionary read-only, but
  343.         % we don't store it away.  I.e., NOT:
  344.         % dup wcheck { .setpagedevice .currentpagedevice pop } if
  345.       readonly
  346.     } ifelse
  347.   } if
  348. } bind odef
  349.  
  350. % Copy a dictionary recursively.
  351. /.copytree {    % <dict> .copytree <dict'>
  352.   dup length dict exch {
  353.     dup type /dicttype eq { .copytree } if 2 index 3 1 roll put
  354.   } forall
  355. } bind def
  356.  
  357. % The implementation of setpagedevice is quite complex.  Currently,
  358. % everything but the media matching algorithm is implemented here.
  359.  
  360. % By default, we only present the requested changes to the device,
  361. % but there are some parameters that require special merging action.
  362. % Define those parameters here, with the procedures that do the merging.
  363. % The procedures are called as follows:
  364. %    <merged> <key> <new_value> -proc- <merged> <key> <new_value'>
  365. /.mergespecial mark
  366.   /InputAttributes
  367.    { dup //null eq
  368.       { pop //null
  369.       }
  370.       { 3 copy pop .knownget
  371.      { dup //null eq
  372.         { pop dup length dict }
  373.         { dup length 2 index length add dict .copydict }
  374.        ifelse
  375.      }
  376.      { dup length dict
  377.      }
  378.         ifelse .copydict readonly
  379.       }
  380.      ifelse
  381.    } bind
  382.   /OutputAttributes 1 index
  383.   /Policies
  384.     { 3 copy pop .knownget
  385.        { dup length 2 index length add dict .copydict }
  386.        { dup length dict }
  387.       ifelse copy readonly
  388.     } bind
  389.   % Ignore MediaPostition when merging (Adobe CPSI does).
  390.   /MediaPosition { dup //null ne { pop //null } if } bind
  391. .dicttomark readonly def
  392.  
  393. % M. Sweet, Easy Software Products:
  394. %
  395. % Define NOMEDIAATTRS to turn off the default (but unimplementable) media
  396. % selection policies for setpagedevice.  This is used by CUPS to support
  397. % the standard Adobe media attributes.
  398. NOMEDIAATTRS {
  399.   % Define only PageSize for input attribute matching.
  400.   /.inputattrkeys [
  401.     /PageSize
  402.   ] readonly def
  403.   % Define no other keys used in media selection.
  404.   /.inputselectionkeys [
  405.     /noInputSelectionsKeys
  406.   ] readonly def
  407.  
  408.   % Define no keys used in output attribute matching.
  409.   /.outputattrkeys [
  410.     /noOutputAttrKeys
  411.   ] readonly def
  412. } {
  413.   % Define the keys used in input attribute matching.
  414.   /.inputattrkeys [
  415.     /PageSize /MediaColor /MediaWeight /MediaType /InsertSheet
  416.       % The following are documented in Adobe's supplement for v2017.
  417.     /LeadingEdge /MediaClass
  418.   ] readonly def
  419.   % Define other keys used in media selection.
  420.   /.inputselectionkeys [
  421.     /MediaPosition /Orientation
  422.   ] readonly def
  423.  
  424.   % Define the keys used in output attribute matching.
  425.   /.outputattrkeys [
  426.     /OutputType
  427.   ] readonly def
  428. } ifelse
  429.  
  430. % Define all the parameters that should always be copied to the merged
  431. % dictionary.
  432. /.copiedkeys [
  433.   /OutputDevice
  434.   .mergespecial { pop } forall
  435.   .inputattrkeys aload pop
  436.   .inputselectionkeys aload pop
  437.   .outputattrkeys aload pop
  438. ] readonly def
  439.  
  440. % Define the parameters that should not be presented to the device.
  441. % The procedures are called as follows:
  442. %    <merged> <key> <value> -proc-
  443. % The procedure leaves all its operands on the stack and returns
  444. % true iff the key/value pair should be presented to .putdeviceparams.
  445. /.presentspecial mark
  446.   .dynamicppkeys { pop //false } forall
  447.             % We must ignore an explicit request for .MediaSize,
  448.             % because media matching always handles this.
  449.   /.MediaSize //false
  450.   /Name //false
  451.   /OutputDevice //false
  452.   /PageDeviceName //false
  453.   /PageOffset //false
  454.   /PageSize //false        % obsolete alias for .MediaSize
  455.   /InputAttributes //false
  456.   .inputattrkeys
  457.     % M. Sweet, Easy Software Products:
  458.     %
  459.     % Treat LeadingEdge like PageSize so that a common Ghostscript driver
  460.     % doesn't need the NOMEDIAATTRS definition.
  461.     %
  462.     % { dup /PageSize eq
  463.     { dup dup /PageSize eq exch /LeadingEdge eq or
  464.        { pop }
  465.        { { 2 index /InputAttributes .knownget { //null eq } { //true } ifelse } }
  466.       ifelse
  467.     }
  468.   forall
  469.   .inputselectionkeys { //false } forall
  470.   /OutputAttributes //false
  471.   .outputattrkeys
  472.     { { 2 index /OutputAttributes .knownget { //null eq } { //true } ifelse } }
  473.   forall
  474.   /Install //false
  475.   /BeginPage //false
  476.   /EndPage //false
  477.   /Policies //false
  478.     % Our extensions:
  479.   /HWColorMap
  480.     {            % HACK: don't transmit the color map, because
  481.             % window systems can change the color map on their own
  482.             % incrementally.  Someday we'll have a better
  483.             % solution for this....
  484.       //false
  485.     }
  486.   /ViewerPreProcess //false
  487.   /ImagingBBox //false    % This prevents the ImagingBBox value in the setpagedevice
  488.             % from affecting the device's ImagingBBox parameter, but
  489.             % does retain a 'shadow' copy at the PostScript level.
  490.             % This is done for Adobe compatibility since Adobe does
  491.             % render marks outside the ImagingBBox (and QuarkXpress
  492.             % relies on it).
  493. .dicttomark readonly def
  494.  
  495. % Define access to device defaults.
  496. /.defaultdeviceparams
  497.  { finddevice //null .getdeviceparams
  498.  } bind def
  499.  
  500. % Select media (input or output).  The hard work is done in an operator:
  501. %    <pagedict> <attrdict> <policydict> <keys> .matchmedia <key> true
  502. %    <pagedict> <attrdict> <policydict> <keys> .matchmedia false
  503. %    <pagedict> null <policydict> <keys> .matchmedia null true
  504. /.selectmedia        % <orig> <request> <merged> <failed>     <-- retained
  505.             %   <attrdict> <policydict> <attrkeys> <mediakey>
  506.             %   .selectmedia
  507.  { 5 index 5 -2 roll 4 index .matchmedia
  508.         % Stack: orig request merged failed attrkeys mediakey
  509.         %   (key true | false)
  510.     { 4 index 3 1 roll put pop
  511.     }
  512.     {    % Adobe's implementations have a "big hairy heuristic"
  513.     % to choose the set of keys to report as having failed the match.
  514.     % For the moment, we report any keys that are in the request
  515.     % and don't have the same value as in the original dictionary.
  516.       5 index 1 index .knownget
  517.        { 4 index 3 1 roll put }
  518.        { 3 index exch .undef }
  519.       ifelse
  520.        {    % Stack: <orig> <request> <merged> <failed> <attrkey>
  521.      3 index 1 index .knownget
  522.       { 5 index 2 index .knownget { ne } { pop //true } ifelse }
  523.       { //true }
  524.      ifelse        % Stack: ... <failed> <attrkey> <report>
  525.       { 2 copy /rangecheck put }
  526.      if pop
  527.        }
  528.       forall
  529.     }
  530.    ifelse
  531.  } bind def
  532.  
  533. % Apply Policies to any unprocessed failed requests.
  534. % As we process each request entry, we replace the error name
  535. % in the <failed> dictionary with the policy value,
  536. % and we replace the key in the <merged> dictionary with its prior value
  537. % (or remove it if it had no prior value).
  538. /.policyprocs mark
  539. % These procedures are called with the following on the stack:
  540. %   <orig> <merged> <failed> <Policies> <key> <policy>
  541. % They are expected to consume the top 2 operands.
  542. % NOTE: we currently treat all values other than 0, 1, or 7 (for PageSize)
  543. % the same as 0, i.e., we signal an error.
  544. %
  545. % M. Sweet, Easy Software Products:
  546. %
  547. % Define NOMEDIAATTRS to turn off the default (but unimplementable) media
  548. % selection policies for setpagedevice.  This is used by CUPS to support
  549. % the standard Adobe media attributes.
  550.   0 {        % Set errorinfo and signal a configurationerror.
  551.       NOMEDIAATTRS {
  552.         % NOMEDIAATTRS means that the default policy is 7...
  553.         pop 2 index exch 7 put
  554.       } {
  555.     pop dup 4 index exch get 2 array astore
  556.     $error /errorinfo 3 -1 roll put
  557.     cleartomark
  558.     /setpagedevice load /configurationerror signalerror
  559.       } ifelse
  560.   } bind
  561.   1 {        % Roll back the failed request to its previous status.
  562. SETPDDEBUG { (Rolling back.) = pstack flush } if
  563.     3 index 2 index 3 -1 roll put
  564.     4 index 1 index .knownget
  565.      { 4 index 3 1 roll put }
  566.      { 3 index exch .undef }
  567.     ifelse
  568.   } bind
  569.   7 {        % For PageSize only, just impose the request.
  570.     1 index /PageSize eq
  571.      { pop pop 1 index /PageSize 7 put }
  572.      { .policyprocs 0 get exec }
  573.     ifelse
  574.   } bind
  575. .dicttomark readonly def
  576. /.applypolicies        % <orig> <merged> <failed> .applypolicies
  577.             %   <orig> <merged'> <failed'>
  578.  { 1 index /Policies get 1 index
  579.     { type /integertype eq
  580.        { pop        % already processed
  581.        }
  582.        { 2 copy .knownget not { 1 index /PolicyNotFound get } if
  583.             % Stack: <orig> <merged> <failed> <Policies> <key>
  584.             %   <policy>
  585.      .policyprocs 1 index .knownget not { .policyprocs 0 get } if exec
  586.        }
  587.       ifelse
  588.     }
  589.    forall pop
  590.  } bind def
  591.  
  592. % Prepare to present parameters to the device, by spreading them onto the
  593. % operand stack and removing any that shouldn't be presented.
  594. /.prepareparams        % <params> .prepareparams -mark- <key1> <value1> ...
  595.  { mark exch dup
  596.     {            % Stack: -mark- key1 value1 ... merged key value
  597.       .presentspecial 2 index .knownget
  598.        { exec { 3 -1 roll } { pop pop } ifelse }
  599.        { 3 -1 roll }
  600.       ifelse
  601.     }
  602.    forall pop
  603.  } bind def
  604.  
  605. % Put device parameters without resetting currentpagedevice.
  606. % (.putdeviceparams clears the current page device.)
  607. /.putdeviceparamsonly    % <device> <Policies|null> <require_all> -mark-
  608.             %   <key1> <value1> ... .putdeviceparamsonly
  609.             % On success: <device> <eraseflag>
  610.             % On failure: <device> <Policies|null> <req_all> -mark-
  611.             %   <key1> <error1> ...
  612.  { .currentpagedevice
  613.     { counttomark 4 add 1 roll .putdeviceparams
  614.       dup type /booleantype eq { 3 } { counttomark 5 add } ifelse -1 roll
  615.       .setpagedevice
  616.     }
  617.     { pop .putdeviceparams
  618.     }
  619.    ifelse
  620.  } bind def
  621.  
  622. % Try setting the device parameters from the merged request.
  623. /.trysetparams        % <merged> <(ignored)> <device> <Policies>
  624.             %   .trysetparams
  625.  { //true 4 index .prepareparams
  626.             % Add the computed .MediaSize.
  627.             % Stack: merged (ignored) device Policies -true-
  628.             %   -mark- key1 value1 ...
  629.    counttomark 5 add index .computemediasize
  630.    exch pop exch pop /.MediaSize exch
  631. SETPDDEBUG { (Putting.) = pstack flush } if
  632.    .putdeviceparamsonly
  633. SETPDDEBUG { (Result of putting.) = pstack flush } if
  634.  } bind def
  635.  
  636. % Compute the media size and initial matrix from a merged request (after
  637. % media selection).
  638. /.computemediasize    % <request> .computemediasize
  639.             %   <request> <matrix> <[width height]>
  640.  { dup /PageSize get                    % requested page size
  641.    1 index /InputAttributes get
  642.      2 index (%MediaSource) get get /PageSize get    % media size
  643.                             % (may be a range)
  644.    2 index /Policies get
  645.      dup /PageSize .knownget
  646.       { exch pop } { /PolicyNotFound get } ifelse    % PageSize policy,
  647.                             % affects scaling
  648.    3 index /Orientation .knownget not { //null } if
  649.    4 index /RollFedMedia .knownget not { //false } if
  650.    matrix .matchpagesize not {
  651.         % This is a "can't happen" condition!
  652.      /setpagedevice load /rangecheck signalerror
  653.    } if
  654.    2 array astore
  655.  } bind def
  656.  
  657. % ---------------- setpagedevice itself ---------------- %
  658.  
  659. /setpagedevice
  660.  {        % We mustn't pop the argument until the very end,
  661.         % so that the pseudo-operator machinery can restore the stack
  662.         % if an error occurs.
  663.    mark 1 index currentpagedevice
  664.  
  665.         % Check whether we are changing OutputDevice;
  666.         % also handle the case where the current device
  667.         % is not a page device.
  668.         % Stack: mark <request> <current>
  669. SETPDDEBUG { (Checking.) = pstack flush } if
  670.  
  671.    dup /OutputDevice .knownget
  672.     {        % Current device is a page device.
  673.       2 index /OutputDevice .knownget
  674.        {    % A specific OutputDevice was requested.
  675.      2 copy eq
  676.       { pop pop //null }
  677.       { exch pop }
  678.      ifelse
  679.        }
  680.        { pop //null
  681.        }
  682.       ifelse
  683.     }
  684.     {        % Current device is not a page device.
  685.         % Use the default device.
  686.       1 index /OutputDevice .knownget not { .defaultdevicename } if
  687.     }
  688.    ifelse
  689.    dup //null eq
  690.     { pop
  691.     }
  692.     { exch pop .defaultdeviceparams
  693.         % In case of duplicate keys, .dicttomark takes the entry
  694.         % lower on the stack, so we can just append the defaults here.
  695.       .requiredattrs { exec } forall .dicttomark
  696.     }
  697.    ifelse
  698.  
  699.         % Check whether a viewer wants to intervene.
  700.         % We must check both the request (which takes precedence)
  701.         % and the current dictionary.
  702.         % Stack: mark <request> <orig>
  703.    exch dup /ViewerPreProcess .knownget
  704.     { exec }
  705.     { 1 index /ViewerPreProcess .knownget { exec } if }
  706.    ifelse exch
  707.  
  708.         % Construct a merged request from the actual request plus
  709.         % any keys that should always be propagated.
  710.         % Stack: mark <request> <orig>
  711. SETPDDEBUG { (Merging.) = pstack flush } if
  712.  
  713.    exch 1 index length 1 index length add dict
  714.    .copiedkeys
  715.     {        % Stack: <orig> <request> <merged> <key>
  716.       3 index 1 index .knownget { 3 copy put pop } if pop
  717.     }
  718.    forall
  719.         % Stack: <orig> <request> <merged>
  720.    dup 2 index
  721.     {        % stack: <orig> <request> <merged> <merged> <rkey> <rvalue>
  722.       .mergespecial 2 index .knownget { exec } if
  723.       put dup
  724.     }
  725.    forall pop
  726.         % Hack: if FIXEDRESOLUTION is true, discard any attempt to
  727.         % change HWResolution.
  728.    FIXEDRESOLUTION { dup /HWResolution .undef } if
  729.         % Hack: if FIXEDMEDIA is true, discard any attempt to change
  730.         % PageSize or HWSize.
  731.    FIXEDMEDIA
  732.     { dup /PageSize 4 index /PageSize get put
  733.       dup /HWSize 4 index /HWSize get put
  734.     } if
  735.         % Hack: to work around some files that take a PageSize
  736.         % from InputAttributes and impose it, discard any attempt
  737.         % to set PageSize to a 4-element value.
  738.         % Stack: mark <orig> <request> <merged>
  739.     dup /PageSize .knownget {
  740.       length 2 ne {
  741.     dup /PageSize 4 index /PageSize get put
  742.       } if
  743.     } if
  744.  
  745.         % Select input and output media.
  746.         % Stack: mark <orig> <request> <merged>
  747. SETPDDEBUG { (Selecting.) = pstack flush } if
  748.  
  749.    0 dict    % <failed>
  750.    1 index /InputAttributes .knownget
  751.     { 2 index /Policies get
  752.       .inputattrkeys (%MediaSource) cvn .selectmedia
  753.     } if
  754.    1 index /OutputAttributes .knownget
  755.     { 2 index /Policies get
  756.       .outputattrkeys (%MediaDestination) cvn .selectmedia
  757.      } if
  758.    3 -1 roll 4 1 roll        % temporarily swap orig & request
  759.    .applypolicies
  760.    3 -1 roll 4 1 roll        % swap back
  761.  
  762.         % Construct the new device, and attempt to set its attributes.
  763.         % Stack: mark <orig> <request> <merged> <failed>
  764. SETPDDEBUG { (Constructing.) = pstack flush } if
  765.  
  766.    currentdevice .devicename 2 index /OutputDevice get eq
  767.     { currentdevice }
  768.     { 1 index /OutputDevice get finddevice }
  769.    ifelse
  770.         %**************** We should copy the device here,
  771.         %**************** but since we can't close the old device,
  772.         %**************** we don't.  This is WRONG.
  773.     %****************copydevice
  774.    2 index /Policies get
  775.    .trysetparams
  776.    dup type /booleantype ne
  777.     {        % The request failed.
  778.         % Stack: ... <orig> <request> <merged> <failed> <device>
  779.         %   <Policies> true mark <name> <errorname> ...
  780. SETPDDEBUG { (Recovering.) = pstack flush } if
  781.       counttomark 4 add index
  782.       counttomark 2 idiv { dup 4 -2 roll put } repeat
  783.       pop pop pop
  784.         % Stack: mark ... <orig> <request> <merged> <failed> <device>
  785.         %   <Policies>
  786.       6 2 roll 3 -1 roll 4 1 roll
  787.       .applypolicies
  788.       3 -1 roll 4 1 roll 6 -2 roll
  789.       .trysetparams        % shouldn't fail!
  790.       dup type /booleantype ne
  791.        { 2 { counttomark 1 add 1 roll cleartomark } repeat
  792.          /setpagedevice load exch signalerror
  793.        }
  794.       if
  795.     }
  796.    if
  797.  
  798.         % The attempt succeeded.  Install the new device.
  799.         % Stack: mark ... <merged> <failed> <device> <eraseflag>
  800. SETPDDEBUG { (Installing.) = pstack flush } if
  801.  
  802.    pop 2 .endpage
  803.     { 1 //true .outputpage
  804.       (>>setpagedevice, press <return> to continue<<\n) .confirm
  805.     }
  806.    if
  807.         % .setdevice clears the current page device!
  808.    .currentpagedevice pop exch
  809.    .setdevice pop
  810.    .setpagedevice
  811.  
  812.         % Implement UseCIEColor directly if this is a LL3 system.
  813.         % The color substitution feature is now implemented in
  814.         % the interpreter, and this is used as an optimization.
  815.         %
  816.         % NB: This shoud be the only use of the .setuseciecolor
  817.         %     operator anywhere.
  818.         %
  819.         % If UseCIEColor is transitioned to false, set some
  820.         % color space other than /DeviceGray, to insure that
  821.         % initgraphics will actually perform a setcolorspace
  822.         % operation (there is an optimization in setcolorspace
  823.         % that does nothing if the operand and current color
  824.         % spaces are the same, and UseCIEColor is false).
  825.  
  826.     /.setuseciecolor where
  827.       {
  828.         pop 1 index /UseCIEColor .knownget
  829.           {
  830.             dup .setuseciecolor not
  831.               { /DeviceRGB setcolorspace }
  832.             if
  833.           }
  834.         if
  835.       }
  836.     if
  837.  
  838.         % Merge the request into the current page device,
  839.         % unless we're changing the OutputDevice.
  840.         % Stack: mark ... <merged> <failed>
  841.    exch currentpagedevice dup length 2 index length add dict
  842.         % Stack: mark ... <failed> <merged> <current> <newdict>
  843.    2 index /OutputDevice .knownget {
  844.      2 index /OutputDevice .knownget not { //null } if eq
  845.    } {
  846.      //true
  847.    } ifelse {
  848.         % Same OutputDevice, merge the dictionaries.
  849.      .copydict
  850.    } {
  851.         % Different OutputDevice, discard the old dictionary.
  852.      exch pop
  853.    } ifelse .copydict
  854.         % Initialize the default matrix, taking media matching
  855.         % into account.
  856.    .computemediasize pop initmatrix concat
  857.    dup /PageOffset .knownget
  858.     {        % Translate by the given number of 1/72" units in device X/Y.
  859.       dup 0 get exch 1 get
  860.       2 index /HWResolution get dup 1 get exch 0 get
  861.       4 -1 roll mul 72 div   3 1 roll mul 72 div
  862.       idtransform translate
  863.     }
  864.    if
  865.         % We must install the new page device dictionary
  866.         % before calling the Install procedure.
  867.   dup .setpagedevice
  868.   .setdefaulthalftone    % Set the default screen before calling Install.
  869.   dup /Install .knownget {
  870.     { .execinstall } stopped { .postinstall stop } { .postinstall } ifelse
  871.   } {
  872.     .postinstall
  873.   } ifelse
  874. } odef
  875.  
  876. % We break out the code after calling the Install procedure into a
  877. % separate procedure, since it is executed even if Install causes an error.
  878. % By making .execinstall a separate operator procedure, we get the stacks
  879. % restored if it fails.
  880.  
  881. /.execinstall {        % <proc> .execinstall -
  882.     % Because the interpreter optimizes tail calls, we can't just let
  883.     % the body of this procedure be 'exec', because that would lose
  884.     % the stack protection that is the whole reason for having the
  885.     % procedure in the first place.  We hack this by adding a couple
  886.     % of extra tokens to ensure that the operator procedure is still
  887.     % on the stack during the exec.
  888.   exec
  889.   0 pop    % See above.
  890. } odef
  891. /.postinstall {        % mark ... <failed> <merged> .postinstall -
  892.    matrix currentmatrix .setdefaultmatrix
  893.         % Erase and initialize the page.
  894.    initgraphics
  895.    currentoverprint //false setoverprint 1 setcolor
  896.    .fillpage
  897.    0 setcolor setoverprint
  898.    .beginpage
  899.  
  900.         % Clean up, calling PolicyReport if needed.
  901.         % Stack: mark ... <failed> <merged>
  902. SETPDDEBUG { (Finishing.) = pstack flush } if
  903.  
  904.    exch dup length 0 ne
  905.     { 1 index /Policies get /PolicyReport get
  906.       counttomark 1 add 2 roll cleartomark
  907.       exec
  908.     }
  909.     { cleartomark
  910.     }
  911.    ifelse pop
  912.  
  913. } odef
  914.  
  915. end                % level2dict
  916. .setlanguagelevel
  917.