home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 October / Chip_2002-10_cd1.bin / zkuste / pdf / download / gs704w32.exe / gs7.04 / lib / gs_lev2.ps < prev    next >
Encoding:
Text File  |  2002-01-31  |  31.5 KB  |  925 lines

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