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

  1. %    Copyright (C) 1990, 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_lev2.ps,v 1.29.2.5 2004/09/09 20:56:59 ray Exp $
  14. % Initialization file for Level 2 functions.
  15. % When this is run, systemdict is still writable,
  16. % but (almost) everything defined here goes into level2dict.
  17.  
  18. level2dict begin
  19.  
  20. % ------ System and user parameters ------ %
  21.  
  22. % User parameters must obey save/restore, and must also be maintained
  23. % per-context.  We implement the former, and some of the latter, here
  24. % with PostScript code.  NOTE: our implementation assumes that user
  25. % parameters change only as a result of setuserparams -- that there are
  26. % no user parameters that are ever changed dynamically by the interpreter
  27. % (although the interpreter may adjust the value presented to setuserparams)
  28. %
  29. % There are two types of user parameters: those which are actually
  30. % maintained in the interpreter, and those which exist only at the
  31. % PostScript level.  We maintain the current state of both types in
  32. % a read-only local dictionary named userparams, defined in systemdict.
  33. % In a multi-context system, each context has its own copy of this
  34. % dictionary.  In addition, there is a constant dictionary named
  35. % psuserparams where each key is the name of a user parameter that exists
  36. % only in PostScript and the value is a procedure to check that the value
  37. % is legal: setuserparams uses this for checking the values.
  38. % setuserparams updates userparams explicitly, in addition to setting
  39. % any user parameters in the interpreter; thus we can use userparams
  40. % to reset those parameters after a restore or a context switch.
  41. % NOTE: the name userparams is known to the interpreter, and in fact
  42. % the interpreter creates the userparams dictionary.
  43.  
  44. % Check parameters that are managed at the PostScript level.
  45. /.checkparamtype {        % <newvalue> <type> .checkparamtype <bool>
  46.   exch type eq
  47. } .bind def
  48. /.checksetparams {        % <newdict> <opname> <checkdict>
  49.                 %   .checksetparams <newdict>
  50.   2 index {
  51.         % Stack: newdict opname checkdict key newvalue
  52.     3 copy 3 1 roll .knownget {
  53.       exec not {
  54.     pop pop pop load /typecheck signalerror
  55.       } if
  56.       dup type /stringtype eq {
  57.     dup rcheck not {
  58.       pop pop pop load /invalidaccess signalerror
  59.     } if
  60.       } if
  61.     } {
  62.       pop
  63.     } ifelse pop pop
  64.   } forall pop pop
  65. } .bind def    % not odef, shouldn't reset stacks
  66.  
  67. % currentuser/systemparams creates and returns a dictionary in the
  68. % current VM.  The easiest way to make this work is to copy any composite
  69. % PostScript-level parameters to global VM.  Currently we have strings
  70. % as well as arrays. For arrays, we also need to copy any contents that
  71. % are in VM. Also copying string parameters insures the contents won't
  72. % be changed. Also be careful to preserve 'executable' state.
  73. /.copyparam {            % <value> .copyparam <value'>
  74.   dup type /arraytype eq {
  75.     .currentglobal true .setglobal exch 
  76.     dup wcheck exch dup xcheck exch        % original attributes
  77.     dup length array exch dup {    % stack: destination_array original_array original_array
  78.       dup type /arraytype eq {
  79.     dup 2 index ne {    % avoid recursion
  80.         .copyparam    % recurse to handle composite array elements
  81.         } {
  82.       % this array self referenced, do it again (yuk!)
  83.       pop 1 index        % get copy of destination array
  84.     } ifelse
  85.       } {
  86.         dup type /stringtype eq {
  87.       .copyparam
  88.         } if 
  89.       }
  90.       ifelse 3 1 roll        % keep arrays on top
  91.     } forall pop astore
  92.     exch { cvx } if        % set executable state
  93.     exch not { readonly } if    % set readonly attribute as original
  94.     exch .setglobal
  95.   } if
  96.   dup type /stringtype eq {
  97.     dup wcheck exch    % save attr for setting readonly
  98.     .currentglobal true .setglobal
  99.     1 index length string exch .setglobal
  100.     copy exch not { readonly } if
  101.   } if
  102. } .bind def
  103.  
  104. % Some user parameters are managed entirely at the PostScript level.
  105. % We take care of that here.
  106. systemdict begin
  107. /psuserparams 48 dict def
  108. /getuserparam {            % <name> getuserparam <value>
  109.   /userparams .systemvar 1 index get exch pop
  110. } odef
  111. % Fill in userparams (created by the interpreter) with current values.
  112. mark .currentuserparams
  113. counttomark 2 idiv {
  114.   userparams 3 1 roll put
  115. } repeat pop
  116. /.definepsuserparam {        % <name> <value> .definepsuserparam -
  117.   psuserparams 3 copy pop
  118.   type cvlit /.checkparamtype cvx 2 packedarray cvx put
  119.   userparams 3 1 roll put
  120. } .bind def
  121. end
  122. /currentuserparams {        % - currentuserparams <dict>
  123.   /userparams .systemvar dup length dict .copydict
  124. } odef
  125. /setuserparams {        % <dict> setuserparams -
  126.     % Check that we will be able to set the PostScript-level
  127.     % user parameters.
  128.   /setuserparams /psuserparams .systemvar .checksetparams
  129.     % Set the C-level user params.  If this succeeds, we know that
  130.     % the password check succeeded.
  131.   dup .setuserparams
  132.     % Now set the PostScript-level params.
  133.     % The interpreter may have adjusted the values of some of the
  134.     % parameters, so we have to read them back.
  135.   dup {
  136.     /userparams .systemvar 2 index known {
  137.       psuserparams 2 index known not {
  138.     pop dup .getuserparam
  139.       } if
  140.       .copyparam
  141.       % special protection for the security related parameters
  142.       [ /PermitFileReading /PermitFileWriting /PermitFileControl ]
  143.       { 2 index eq { % force all strings to readonly
  144.       dup length array exch { readonly exch } forall astore
  145.         } if
  146.       } forall
  147.       % protect top level of parameters that we copied
  148.       dup type dup /arraytype eq exch /stringtype eq or { readonly } if
  149.       /userparams .systemvar 3 1 roll .forceput  % userparams is read-only
  150.     } {
  151.       pop pop
  152.     } ifelse
  153.   } forall
  154.     % A context switch might have occurred during the above loop,
  155.     % causing the interpreter-level parameters to be reset.
  156.     % Set them again to the new values.  From here on, we are safe,
  157.     % since a context switch will consult userparams.
  158.   .setuserparams
  159. } .bind odef
  160. % Initialize user parameters managed here.
  161. /JobName () .definepsuserparam
  162.  
  163. % Restore must restore the user parameters.
  164. % (Since userparams is in local VM, save takes care of saving them.)
  165. /restore {        % <save> restore -
  166.   //restore /userparams .systemvar .setuserparams
  167. } .bind odef
  168.  
  169. % The pssystemparams dictionary holds some system parameters that
  170. % are managed entirely at the PostScript level.
  171. systemdict begin
  172. currentdict /pssystemparams known not {
  173.   /pssystemparams 40 dict readonly def
  174. } if
  175. /getsystemparam {        % <name> getsystemparam <value>
  176.   //pssystemparams 1 index .knownget { exch pop } { .getsystemparam } ifelse
  177. } odef
  178. end
  179. /currentsystemparams {        % - currentsystemparams <dict>
  180.   mark .currentsystemparams //pssystemparams { } forall .dicttomark
  181. } odef
  182. /setsystemparams {        % <dict> setsystemparams -
  183.     % Check that we will be able to set the PostScript-level
  184.     % system parameters.
  185.    /SAFETY .systemvar /safe get {
  186.      % SAFER mode disallows some changes
  187.      [ /GenericResourceDir /FontResourceDir /GenericResourcePathSep ] {
  188.        2 copy .knownget {
  189.      exch //pssystemparams exch .knownget {
  190.            ne { /setsystemparams /invalidaccess signalerror } if
  191.          } {
  192.            pop
  193.          } ifelse
  194.        } {
  195.          pop
  196.        } ifelse
  197.      } forall
  198.    } if
  199.    /setsystemparams //pssystemparams mark exch {
  200.      type cvlit /.checkparamtype cvx 2 packedarray cvx
  201.    } forall .dicttomark .checksetparams
  202.     % Set the C-level system params.  If this succeeds, we know that
  203.     % the password check succeeded.
  204.    dup .setsystemparams
  205.     % Now set the PostScript-level params.  We must copy local strings
  206.     % into global VM.
  207.    dup
  208.     { //pssystemparams 2 index known
  209.        {        % Stack: key newvalue
  210.      .copyparam
  211.      % protect top level parameters that we copied
  212.          dup type dup /arraytype eq exch /stringtype eq or { readonly } if
  213.      //pssystemparams 3 1 roll .forceput    % pssystemparams is read-only
  214.        }
  215.        { pop pop
  216.        }
  217.       ifelse
  218.     }
  219.    forall pop
  220. } .bind odef
  221.  
  222. % Initialize the passwords.
  223. % NOTE: the names StartJobPassword and SystemParamsPassword are known to
  224. % the interpreter, and must be bound to noaccess strings.
  225. % The length of these strings must be max_password (iutil2.h) + 1.
  226. /StartJobPassword 65 string noaccess def
  227. /SystemParamsPassword 65 string noaccess def
  228.  
  229. % Redefine cache parameter setting to interact properly with userparams.
  230. /setcachelimit {
  231.   mark /MaxFontItem 2 index .dicttomark setuserparams pop
  232. } .bind odef
  233. /setcacheparams {
  234.     % The MaxFontCache parameter is a system parameter, which we might
  235.     % not be able to set.  Fortunately, this doesn't matter, because
  236.     % system parameters don't have to be synchronized between this code
  237.     % and the VM.
  238.   counttomark 1 add copy setcacheparams
  239.   currentcacheparams    % mark size lower upper
  240.     3 -1 roll pop
  241.     /MinFontCompress 3 1 roll
  242.     /MaxFontItem exch
  243.   .dicttomark setuserparams
  244.   cleartomark
  245. } .bind odef
  246.  
  247. % Add bogus user and system parameters to satisfy badly written PostScript
  248. % programs that incorrectly assume the existence of all the parameters
  249. % listed in Appendix C of the Red Book.  Note that some of these may become
  250. % real parameters later: code near the end of gs_init.ps takes care of
  251. % removing any such parameters from ps{user,system}params.
  252.  
  253. % psuserparams
  254.   /MaxFormItem 100000 .definepsuserparam
  255.   /MaxPatternItem 20000 .definepsuserparam
  256.   /MaxScreenItem 48000 .definepsuserparam
  257.   /MaxUPathItem 5000 .definepsuserparam
  258.  
  259. % File Access Permission parameters
  260.   .currentglobal true .setglobal
  261.   /.checkFilePermitparams {
  262.     type /arraytype eq {
  263.       currentuserparams /LockFilePermissions get {
  264.         5 { pop } repeat /setuserparams /invalidaccess signalerror
  265.       }{
  266.         % in addition to validating the value, ensure the value is read/only
  267.         dup { readonly exch } forall
  268.         .currentglobal exch dup gcheck .setglobal length array exch .setglobal
  269.     astore readonly
  270.       }
  271.       ifelse
  272.     } {
  273.       5 { pop } repeat /setuserparams /typecheck signalerror
  274.     }
  275.     ifelse
  276.     true
  277.   } .bind def
  278. % Initialize the File Permission access control to wide open
  279. % These will only be accessed via current/set userparams.
  280. % Values are a string containing multiple nul terminated path strings
  281.   /PermitFileReading dup [ (*) ] .definepsuserparam
  282.     psuserparams exch /.checkFilePermitparams load put
  283.   /PermitFileWriting dup [ (*) ] .definepsuserparam
  284.     psuserparams exch /.checkFilePermitparams load put
  285.   /PermitFileControl dup [ (*) ] .definepsuserparam
  286.     psuserparams exch /.checkFilePermitparams load put
  287.   .setglobal
  288.  
  289. pssystemparams begin
  290.   /CurDisplayList 0 .forcedef
  291.   /CurFormCache 0 .forcedef
  292.   /CurOutlineCache 0 .forcedef
  293.   /CurPatternCache 0 .forcedef
  294.   /CurUPathCache 0 .forcedef
  295.   /CurScreenStorage 0 .forcedef
  296.   /CurSourceList 0 .forcedef
  297.   /DoPrintErrors false .forcedef
  298.   /MaxDisplayList 140000 .forcedef
  299.   /MaxFormCache 100000 .forcedef
  300.   /MaxOutlineCache 65000 .forcedef
  301.   /MaxPatternCache 100000 .forcedef
  302.   /MaxUPathCache 300000 .forcedef
  303.   /MaxScreenStorage 84000 .forcedef
  304.   /MaxSourceList 25000 .forcedef
  305.   /RamSize 4194304 .forcedef
  306. end
  307.  
  308. % Define the procedures for handling comment scanning.  The names
  309. % %ProcessComment and %ProcessDSCComment are known to the interpreter.
  310. % These procedures take the file and comment string and file as operands.
  311. /.checkprocesscomment {
  312.   dup null eq {
  313.     pop true
  314.   } {
  315.     dup xcheck {
  316.       type dup /arraytype eq exch /packedarraytype eq or
  317.     } {
  318.       pop false
  319.     } ifelse
  320.   } ifelse
  321. } .bind def
  322. /ProcessComment null .definepsuserparam
  323. psuserparams /ProcessComment {.checkprocesscomment} put
  324. (%ProcessComment) cvn {
  325.   /ProcessComment getuserparam
  326.   dup null eq { pop pop pop } { exec } ifelse
  327. } bind def
  328. /ProcessDSCComment null .definepsuserparam
  329. psuserparams /ProcessDSCComment {.checkprocesscomment} put
  330. /.loadingfont false def
  331. (%ProcessDSCComment) cvn {
  332.   /ProcessDSCComment getuserparam
  333.   dup null eq .loadingfont or { pop pop pop } { exec } ifelse
  334. } bind def
  335.  
  336. % ------ Miscellaneous ------ %
  337.  
  338. (<<) cvn            % - << -mark-
  339.   /mark load def
  340. (>>) cvn            % -mark- <key1> <value1> ... >> <dict>
  341.   /.dicttomark load def
  342. /languagelevel 2 def
  343. % When running in Level 2 mode, this interpreter is supposed to be
  344. % compatible with Adobe version 2017.
  345. /version (2017) readonly def
  346.  
  347. % If binary tokens are supported by this interpreter,
  348. % set an appropriate default binary object format.
  349. /setobjectformat where
  350.  { pop
  351.    /RealFormat getsystemparam (IEEE) eq { 1 } { 3 } ifelse
  352.    /ByteOrder getsystemparam { 1 add } if
  353.    setobjectformat
  354.  } if
  355.  
  356. % Aldus Freehand versions 2.x check for the presence of the
  357. % setcolor operator, and if it is missing, substitute a procedure.
  358. % Unfortunately, the procedure takes different parameters from
  359. % the operator.  As a result, files produced by this application
  360. % cause an error if the setcolor operator is actually defined
  361. % and 'bind' is ever used.  Aldus fixed this bug in Freehand 3.0,
  362. % but there are a lot of files created by the older versions
  363. % still floating around.  Therefore, at Adobe's suggestion,
  364. % we implement the following dreadful hack in the 'where' operator:
  365. %      If the key is /setcolor, and
  366. %        there is a dictionary named FreeHandDict, and
  367. %        currentdict is that dictionary,
  368. %      then "where" consults only that dictionary and not any other
  369. %        dictionaries on the dictionary stack.
  370. .wheredict /setcolor {
  371.   /FreeHandDict .where {
  372.     /FreeHandDict get currentdict eq {
  373.       pop currentdict /setcolor known { currentdict true } { false } ifelse
  374.     } {
  375.       .where
  376.     } ifelse
  377.   } {
  378.     .where
  379.   } ifelse
  380. } bind put
  381.  
  382. % ------ Virtual memory ------ %
  383.  
  384. /currentglobal            % - currentglobal <bool>
  385.   /currentshared load def
  386. /gcheck                % <obj> gcheck <bool>
  387.   /scheck load def
  388. /setglobal            % <bool> setglobal -
  389.   /setshared load def
  390. % We can make the global dictionaries very small, because they auto-expand.
  391. /globaldict currentdict /shareddict .knownget not { 4 dict } if def
  392. /GlobalFontDirectory SharedFontDirectory def
  393.  
  394. % VMReclaim and VMThreshold are user parameters.
  395. /setvmthreshold {        % <int> setvmthreshold -
  396.   mark /VMThreshold 2 index .dicttomark setuserparams pop
  397. } odef
  398. /vmreclaim {            % <int> vmreclaim -
  399.   dup 0 gt {
  400.     .vmreclaim
  401.   } {
  402.     mark /VMReclaim 2 index .dicttomark setuserparams pop
  403.   } ifelse
  404. } odef
  405. -1 setvmthreshold
  406.  
  407. % ------ IODevices ------ %
  408.  
  409. /.getdevparams where {
  410.   pop /currentdevparams {    % <iodevice> currentdevparams <dict>
  411.     .getdevparams .dicttomark
  412.   } odef
  413. } if
  414. /.putdevparams where {
  415.   pop /setdevparams {        % <iodevice> <dict> setdevparams -
  416.     mark 1 index { } forall counttomark 2 add index
  417.     .putdevparams pop pop
  418.   } odef
  419. } if
  420.  
  421. % ------ Job control ------ %
  422.  
  423. serverdict begin
  424.  
  425. % We could protect the job information better, but we aren't attempting
  426. % (currently) to protect ourselves against maliciousness.
  427.  
  428. /.jobsave null def        % top-level save object
  429. /.jobsavelevel 0 def        % save depth of job (0 if .jobsave is null,
  430.                 % 1 otherwise)
  431. /.adminjob true def        % status of current unencapsulated job
  432.  
  433. end        % serverdict
  434.  
  435. % Because there may be objects on the e-stack created since the job save,
  436. % we have to clear the e-stack before doing the end-of-job restore.
  437. % We do this by executing a 2 .stop, which is caught by the 2 .stopped
  438. % in .runexec; we leave on the o-stack a procedure to execute aftewards.
  439. %
  440. %**************** The definition of startjob is not complete yet, since
  441. % it doesn't reset stdin/stdout.
  442. /.startnewjob {            % <exit_bool> <password_level>
  443.                 %   .startnewjob -
  444.     serverdict /.jobsave get dup null eq { pop } { restore } ifelse
  445.     exch {
  446.             % Unencapsulated job
  447.       serverdict /.jobsave null put
  448.       serverdict /.jobsavelevel 0 put
  449.       serverdict /.adminjob 3 -1 roll 1 gt put
  450.         % The Adobe documentation doesn't say what happens to the
  451.         % graphics state stack in this case, but an experiment
  452.         % produced results suggesting that a grestoreall occurs.
  453.       grestoreall
  454.     } {
  455.             % Encapsulated job
  456.       pop
  457.       serverdict /.jobsave save put
  458.       serverdict /.jobsavelevel 1 put
  459.       .userdict /quit /stop load put
  460.     } ifelse
  461.         % Reset the interpreter state.
  462.   clear cleardictstack
  463.   initgraphics
  464.   false setglobal
  465.   2 vmreclaim    % Make sure GC'ed memory is reclaimed and freed.
  466. } bind def
  467. /.startjob {            % <exit_bool> <password> <finish_proc>
  468.                 %   .startjob <ok_bool>
  469.   vmstatus pop pop serverdict /.jobsavelevel get eq
  470.   2 index .checkpassword 0 gt and {
  471.     exch .checkpassword exch count 3 roll count 3 sub { pop } repeat
  472.     cleardictstack
  473.         % Reset the e-stack back to the 2 .stopped in .runexec,
  474.         % passing the finish_proc to be executed afterwards.
  475.     2 .stop
  476.   } {        % Password check failed
  477.     pop pop pop false
  478.   } ifelse
  479. } odef
  480. /startjob {            % <exit_bool> <password> startjob <ok_bool>
  481.     % This is a hack.  We really need some way to indicate explicitly
  482.     % to the interpreter that we are under control of a job server.
  483.   { .startnewjob true } .startjob
  484. } odef
  485.  
  486. % The procedure to undo the job encapsulation 
  487. /.endjob {
  488.   clear cleardictstack
  489.   serverdict /.jobsave get dup null eq { pop } { restore } ifelse
  490.   serverdict /.jobsave null put
  491.   2 vmreclaim   % recover local and global VM
  492. } odef
  493.  
  494. systemdict begin
  495. /quit {                % - quit -
  496.   //systemdict begin serverdict /.jobsave get null eq
  497.    { end //quit }
  498.    { /quit load /invalidaccess /signalerror load end exec }
  499.   ifelse
  500. } bind odef
  501. end
  502.  
  503. % We would like to define exitserver as a procedure, using the code
  504. % that the Red Book says is equivalent to it.  However, since startjob
  505. % resets the exec stack, we can't do this, because control would never
  506. % proceed past the call on startjob if the exitserver is successful.
  507. % Instead, we need to construct exitserver out of pieces of startjob.
  508.  
  509. serverdict begin
  510.  
  511. /exitserver {            % <password> exitserver -
  512.   true exch { .startnewjob } .startjob not {
  513.     /exitserver /invalidaccess signalerror
  514.   } if
  515. } bind def
  516.  
  517. end        % serverdict
  518.  
  519. % ------ Compatibility ------ %
  520.  
  521. % In Level 2 mode, the following replace the definitions that gs_statd.ps
  522. % installs in statusdict and serverdict.
  523. % Note that statusdict must be allocated in local VM.
  524. % We don't bother with many of these yet.
  525.  
  526. /.dict1 { exch mark 3 1 roll .dicttomark } bind def
  527.  
  528. currentglobal false setglobal 25 dict exch setglobal begin
  529. currentsystemparams
  530.  
  531. % The following do not depend on the presence of setpagedevice.
  532. /buildtime 1 index /BuildTime get def
  533. % Also define /buildtime in systemdict because Adobe does so and some fonts use it as ID
  534. systemdict /buildtime dup load put
  535. /byteorder 1 index /ByteOrder get def
  536. /checkpassword { .checkpassword 0 gt } bind def
  537. dup /DoStartPage known
  538.  { /dostartpage { /DoStartPage getsystemparam } bind def
  539.    /setdostartpage { /DoStartPage .dict1 setsystemparams } bind def
  540.  } if
  541. dup /StartupMode known
  542.  { /dosysstart { /StartupMode getsystemparam 0 ne } bind def
  543.    /setdosysstart { { 1 } { 0 } ifelse /StartupMode .dict1 setsystemparams } bind def
  544.  } if
  545. %****** Setting jobname is supposed to set userparams.JobName, too.
  546. /jobname { /JobName getuserparam } bind def
  547. /jobtimeout { /JobTimeout getuserparam } bind def
  548. /ramsize { /RamSize getsystemparam } bind def
  549. /realformat 1 index /RealFormat get def
  550. dup /PrinterName known
  551.  { /setprintername { /PrinterName .dict1 setsystemparams } bind def
  552.  } if
  553. /printername
  554.  { currentsystemparams /PrinterName .knownget not { () } if exch copy
  555.  } bind def
  556. currentuserparams /WaitTimeout known
  557.  { /waittimeout { /WaitTimeout getuserparam } bind def
  558.  } if
  559.  
  560. % The following do require setpagedevice.
  561. /.setpagedevice where { pop } { (%END PAGEDEVICE) .skipeof } ifelse
  562. /defaulttimeouts
  563.  { currentsystemparams dup
  564.    /JobTimeout .knownget not { 0 } if
  565.    exch /WaitTimeout .knownget not { 0 } if
  566.    currentpagedevice /ManualFeedTimeout .knownget not { 0 } if
  567.  } bind def
  568. /margins
  569.  { currentpagedevice /Margins .knownget { exch } { [0 0] } ifelse
  570.  } bind def
  571. /pagemargin
  572.  { currentpagedevice /PageOffset .knownget { 0 get } { 0 } ifelse
  573.  } bind def
  574. /pageparams
  575.  { currentpagedevice
  576.    dup /Orientation .knownget { 1 and ORIENT1 { 1 xor } if } { 0 } ifelse exch
  577.    dup /PageSize get aload pop 3 index 0 ne { exch } if 3 2 roll
  578.    /PageOffset .knownget { 0 get } { 0 } ifelse 4 -1 roll
  579.  } bind def
  580. /setdefaulttimeouts
  581.  { exch mark /ManualFeedTimeout 3 -1 roll
  582.    /Policies mark /ManualFeedTimeout 1 .dicttomark
  583.    .dicttomark setpagedevice
  584.    /WaitTimeout exch mark /JobTimeout 5 2 roll .dicttomark setsystemparams
  585.  } bind def
  586. /.setpagesize { 2 array astore /PageSize .dict1 setpagedevice } bind def
  587. /setduplexmode { /Duplex .dict1 setpagedevice } bind def
  588. /setmargins
  589.  { exch 2 array astore /Margins .dict1 setpagedevice
  590.  } bind def
  591. /setpagemargin { 0 2 array astore /PageOffset .dict1 setpagedevice } bind def
  592. /setpageparams
  593.  { mark /PageSize 6 -2 roll
  594.    4 index 1 and ORIENT1 { 1 } { 0 } ifelse ne { exch } if 2 array astore
  595.    /Orientation 5 -1 roll ORIENT1 { 1 xor } if
  596.    /PageOffset counttomark 2 add -1 roll 0 2 array astore
  597.    .dicttomark setpagedevice
  598.  } bind def
  599. /setresolution
  600.  { dup 2 array astore /HWResolution .dict1 setpagedevice
  601.  } bind def
  602. %END PAGEDEVICE
  603.  
  604. % The following are not implemented yet.
  605. %manualfeed
  606. %manualfeedtimeout
  607. %pagecount
  608. %pagestackorder
  609. %setpagestackorder
  610.  
  611. pop        % currentsystemparams
  612.  
  613. % Flag the current dictionary so it will be swapped when we
  614. % change language levels.  (See zmisc2.c for more information.)
  615. /statusdict currentdict def
  616.  
  617. currentdict end
  618. /statusdict exch .forcedef    % statusdict is local, systemdict is global
  619.  
  620. % The following compatibility operators are in systemdict.  They are
  621. % defined here, rather than in gs_init.ps, because they require the
  622. % resource machinery.
  623.  
  624. /devforall {        % <proc> <scratch> devforall -
  625.   exch {
  626.     1 index currentdevparams
  627.     /Type .knownget { /FileSystem eq } { false } ifelse
  628.     { exec } { pop pop } ifelse
  629.   } /exec load 3 packedarray cvx exch
  630.   (*) 3 1 roll /IODevice resourceforall
  631. } odef
  632. /devstatus {        % <(%disk*%)> devstatus <searchable> <writable>
  633.             %   <hasNames> <mounted> <removable> <searchOrder>
  634.             %   <freePages> <size> true
  635.             % <string> devstatus false
  636.   dup length 5 ge {
  637.     dup 0 5 getinterval (%disk) eq {
  638.       dup /IODevice resourcestatus {
  639.     pop pop dup currentdevparams
  640.     dup /Searchable get
  641.     exch dup /Writeable get
  642.     exch dup /HasNames get
  643.     exch dup /Mounted get
  644.     exch dup /Removable get
  645.     exch dup /SearchOrder get
  646.     exch dup /Free get
  647.     exch /LogicalSize get
  648.     9 -1 roll pop true
  649.       } {
  650.     pop false
  651.       } ifelse
  652.     } {
  653.       pop false
  654.     } ifelse
  655.   } {
  656.     pop false
  657.   } ifelse
  658. } odef
  659.  
  660. % ------ Color spaces ------ %
  661.  
  662. % Move setcolorsapce, setcolor, and colorspacedict to level2dict
  663. level2dict /setcolorspace .cspace_util 1 index get put
  664. level2dict /setcolor .cspace_util 1 index get put
  665. level2dict /colorspacedict .cspace_util 1 index get put
  666.  
  667. % Add the level 2 color spaces
  668. % DevicePixel is actually a LanguageLevel 3 feature; it is here for
  669. % historical reasons.
  670. %% Replace 1 (gs_devpxl.ps) 
  671. (gs_devpxl.ps) runlibfile
  672.  
  673. %% Replace 1 (gs_ciecs2.ps)
  674. (gs_ciecs2.ps) runlibfile
  675.  
  676. %% Replace 1 (gs_indxd.ps)
  677. (gs_indxd.ps) runlibfile
  678.  
  679. %% Replace 1 (gs_sepr.ps)
  680. (gs_sepr.ps) runlibfile
  681.  
  682. %% Replace 1 (gs_patrn.ps)
  683. (gs_patrn.ps) runlibfile
  684.  
  685.  
  686.  
  687. % ------ CIE color rendering ------ %
  688.  
  689. % Define findcolorrendering and a default ColorRendering ProcSet.
  690.  
  691. /findcolorrendering {        % <intentname> findcolorrendering
  692.                 %   <crdname> <found>
  693.   /ColorRendering /ProcSet findresource
  694.   1 index .namestring (.) concatstrings
  695.   1 index /GetPageDeviceName get exec .namestring (.) concatstrings
  696.   2 index /GetHalftoneName get exec .namestring
  697.   concatstrings concatstrings
  698.   dup /ColorRendering resourcestatus {
  699.     pop pop exch pop exch pop true
  700.   } {
  701.     pop /GetSubstituteCRD get exec false
  702.   } ifelse
  703. } odef
  704.  
  705. 5 dict dup begin
  706.  
  707. /GetPageDeviceName {        % - GetPageDeviceName <name>
  708.   currentpagedevice dup /PageDeviceName .knownget {
  709.     exch pop dup null eq { pop /none } if
  710.   } {
  711.     pop /none
  712.   } ifelse
  713. } bind def
  714.  
  715. /GetHalftoneName {        % - GetHalftoneName <name>
  716.   currenthalftone /HalftoneName .knownget not { /none } if
  717. } bind def
  718.  
  719. /GetSubstituteCRD {        % <intentname> GetSubstituteCRD <crdname>
  720.   pop /DefaultColorRendering
  721. } bind def
  722.  
  723. end
  724. % The resource machinery hasn't been activated, so just save the ProcSet
  725. % and let .fixresources finish the installation process.
  726. /ColorRendering exch def
  727.  
  728. % Define setcolorrendering.
  729.  
  730. /.colorrenderingtypes 5 dict def
  731.  
  732. /setcolorrendering {        % <crd> setcolorrendering -
  733.   dup /ColorRenderingType get //.colorrenderingtypes exch get exec
  734. } odef
  735.  
  736. /.setcolorrendering1 where { pop } { (%END CRD) .skipeof } ifelse
  737.  
  738. .colorrenderingtypes 1 {
  739.   dup .buildcolorrendering1 .setcolorrendering1
  740. } .bind put
  741.  
  742. % Note: the value 101 in the next line must be the same as the value of
  743. % GX_DEVICE_CRD1_TYPE in gscrdp.h.
  744. .colorrenderingtypes 101 {
  745.   dup .builddevicecolorrendering1 .setdevicecolorrendering1
  746. } .bind put
  747.  
  748. % sRGB output CRD, D65 white point
  749. mark
  750. /ColorRenderingType 1
  751. /RangePQR [ -0.5 2 -0.5 2 -0.5 2 ] readonly
  752.  
  753. % Bradford Cone Space
  754. /MatrixPQR [ 0.8951 -0.7502  0.0389
  755.          0.2664  1.7135 -0.0685
  756.         -0.1614  0.0367  1.0296] readonly
  757.  
  758. /MatrixLMN [ 3.240449 -0.969265  0.055643
  759.         -1.537136  1.876011 -0.204026
  760.         -0.498531  0.041556  1.057229 ] readonly
  761.  
  762. % Inverse sRGB gamma transform
  763. /EncodeABC [ { dup 0.00304 le
  764.                 { 12.92321 mul }
  765.                 { 1 2.4 div exp 1.055 mul 0.055 sub }
  766.                ifelse
  767.              } bind dup dup
  768.            ] readonly
  769.  
  770. /WhitePoint [ 0.9505 1 1.0890 ] readonly % D65
  771. /BlackPoint [ 0 0 0 ] readonly
  772.  
  773. % VonKries-like transform in Bradford Cone Space
  774.    /TransformPQR
  775.      % The implementations have been moved to C for performance.
  776.      [ { .TransformPQR_scale_WB0 } bind
  777.        { .TransformPQR_scale_WB1 } bind 
  778.        { .TransformPQR_scale_WB2 } bind
  779.      ] readonly
  780. .dicttomark setcolorrendering
  781.  
  782. %END CRD
  783.  
  784. % Initialize a CIEBased color space for sRGB.
  785. /CIEsRGB [ /CIEBasedABC
  786.   mark
  787.     /DecodeLMN [ {
  788.       dup 0.03928 le { 12.92321 div } { 0.055 add 1.055 div 2.4 exp } ifelse
  789.     } bind dup dup ] readonly
  790.     /MatrixLMN [
  791.       0.412457 0.212673 0.019334
  792.       0.357576 0.715152 0.119192
  793.       0.180437 0.072175 0.950301
  794.     ] readonly
  795.     /WhitePoint [0.9505 1.0 1.0890] readonly
  796.   .dicttomark readonly
  797. ] readonly def
  798.  
  799. % ------ Painting ------ %
  800.  
  801. % A straightforward definition of execform that doesn't actually
  802. % do any caching.
  803. /.execform1 {
  804.     % This is a separate operator so that the stacks will be restored
  805.     % properly if an error occurs.
  806.   dup /Matrix get concat
  807.   dup /BBox get aload pop
  808.   exch 3 index sub exch 2 index sub rectclip
  809.   dup /PaintProc get
  810.   1 index /Implementation known not {
  811.     1 index dup /Implementation null .forceput readonly pop
  812.   } if
  813.   exec
  814. } .bind odef    % must bind .forceput
  815.  
  816. /.formtypes 5 dict
  817.   dup 1 /.execform1 load put
  818. def
  819.  
  820. /execform {            % <form> execform -
  821.   gsave {
  822.     dup /FormType get //.formtypes exch get exec
  823.   } stopped grestore { stop } if
  824. } odef
  825.  
  826. /.patterntypes 5 dict
  827.   dup 1 /.buildpattern1 load put
  828. def
  829.  
  830. /makepattern {            % <proto_dict> <matrix> makepattern <pattern>
  831.   //.patterntypes 2 index /PatternType get get
  832.   .currentglobal false .setglobal exch
  833.         % Stack: proto matrix global buildproc
  834.   3 index dup length 1 add dict .copydict
  835.   3 index 3 -1 roll exec 3 -1 roll .setglobal
  836.   1 index /Implementation 3 -1 roll put
  837.   readonly exch pop exch pop
  838. } odef
  839.  
  840. /setpattern {            % [<comp1> ...] <pattern> setpattern -
  841.   currentcolorspace 0 get /Pattern ne {
  842.     [ /Pattern currentcolorspace ] setcolorspace
  843.   } if setcolor
  844. } odef
  845.  
  846. % The following functions emulate the actions of findcmykcustomcolor and
  847. % setcustomcolor.  These functions are described in Adobe's TN 5044.  That
  848. % same document also says "The following ôoperatorsö are not defined in the
  849. % PostScript Language Reference Manual, but should be used as pseudo-operators
  850. % in your PostScript language output. Separation applications from Adobe
  851. % Systems and other vendors will redefine these convention operators to
  852. % separate your documents.  Your application should conditionally define
  853. % procedures with these special names, as shown later in this document."
  854. %
  855. % We are providing these functions because we have found files created by
  856. % "QuarkXPress: pictwpstops filter 1.0" which produce bad shading dictionaries
  857. % if these operators are not defined. 
  858.  
  859. % TN 5044 does not define the contents of the array.  We are simply putting
  860. % the values given into an array.  This is consistent with what we see when
  861. % testing with Adobe Distiller 6.0.
  862. %   <cyan> <magenta> <yellow> <black> <key> findcmykcustomcolor <array>
  863. /findcmykcustomcolor { 5 array astore } bind def
  864.  
  865. % Build a tint transform function for use by setcustomcolor.  This function
  866. % is for a Separation color space which has a DeviceCMYK base color space
  867. % (i.e. 1 input and 4 outputs).  The input to buildcustomtinttransform is the
  868. % array created by findcmykcustomcolor.  The resulting function is:
  869. %   { dup cyan mul exch dup magenta mul exch dup yellow mul exch black mul }
  870. %   Where cyan, magenta, yellow, and black are values from the array.
  871. /buildcustomtinttransform    % <array> buildcustomtinttransform <function>
  872. { [ /dup load 2 index 0 get /mul load
  873.   /exch load /dup load 6 index 1 get /mul load
  874.   /exch load /dup load 10 index 2 get /mul load
  875.   /exch load 13 index 3 get /mul load
  876.   ] cvx bind
  877.   exch pop            % Remove the input array
  878. } bind def
  879.  
  880. % Set a custom color based upon a tint and array which describes the custom
  881. % color.  See findcmykcustomcolor.  First we create and then set a Separation
  882. % colorspace.  Then we set the specified color.
  883. /setcustomcolor            % <array> <tint> setcustomcolor -
  884. { % Start building Separation colorspace
  885.   [ /Separation 3 index 4 get    % Get separation name from array's key
  886.   /DeviceCMYK
  887.   5 index buildcustomtinttransform ]    % build the tint transform function
  888.   setcolorspace            % Set the Separation color space as current
  889.   setcolor            % Set the tint as the current color
  890.   pop                % Remove the input array
  891. } bind def
  892.  
  893. % This proc is supposed to implement a version of overprinting. TN 5044 says
  894. % that this proc is not used by any shipping host-based application. We have
  895. % only found it being used in a proc set in files by Canvas from Deneba Systems.
  896. % Even their proc set does not actually do any overprinting.  However their
  897. % files crash if this is not defined.  Thus we have a copy of this proc but
  898. % we are simply checking for inputs being -1 and if so then we set the value
  899. % to 0.
  900. /setcmykoverprint {
  901.   4 { dup -1 eq { pop 0 } if 4 1 roll } repeat setcmykcolor
  902. } bind def
  903.  
  904. end                % level2dict
  905.