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_res.ps < prev    next >
Encoding:
Text File  |  2006-07-06  |  34.0 KB  |  1,056 lines

  1. %    Copyright (C) 1994, 1996, 1997, 1998, 1999, 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_res.ps,v 1.33.2.2 2004/08/31 20:22:03 ray Exp $
  14. % Initialization file for Level 2 resource machinery.
  15. % When this is run, systemdict is still writable,
  16. % but (almost) everything defined here goes into level2dict.
  17.  
  18. level2dict begin
  19.  
  20. (BEGIN RESOURCES) VMDEBUG
  21.  
  22. % We keep track of (global) instances with another entry in the resource
  23. % dictionary, an .Instances dictionary.  For categories with implicit
  24. % instances, the values in .Instances are the same as the keys;
  25. % for other categories, the values are [instance status size].
  26.  
  27. % Note that the dictionary that defines a resource category is stored
  28. % in global VM.  The PostScript manual says that each category must
  29. % manage global and local instances separately.  However, objects in
  30. % global VM other than systemdict can't reference objects in local VM.
  31. % This means that the resource category dictionary, which would otherwise be
  32. % the obvious place to keep track of the instances, can't be used to keep
  33. % track of local instances.  Instead, we define a dictionary in local VM
  34. % called localinstancedict, in which the key is the category name and
  35. % the value is the analogue of .Instances for local instances.
  36.  
  37. % We don't currently implement automatic resource unloading.
  38. % When and if we do, it should be hooked to the garbage collector.
  39. % However, Ed Taft of Adobe says their interpreters don't implement this
  40. % either, so we aren't going to worry about it for a while.
  41.  
  42. currentglobal false setglobal systemdict begin
  43.   /localinstancedict 5 dict
  44.   .forcedef    % localinstancedict is local, systemdict is global
  45. end true setglobal
  46. /.emptydict 0 dict readonly def
  47. setglobal
  48.  
  49. % Resource category dictionaries have the following keys (those marked with
  50. % * are optional):
  51. %    Standard, defined in the Red Book:
  52. %        Category (name)
  53. %        *InstanceType (name)
  54. %        DefineResource
  55. %            <key> <instance> DefineResource <instance>
  56. %        UndefineResource
  57. %            <key> UndefineResource -
  58. %        FindResource
  59. %            <key> FindResource <instance>
  60. %        ResourceStatus
  61. %            <key> ResourceStatus <status> <size> true
  62. %            <key> ResourceStatus false
  63. %        ResourceForAll
  64. %            <template> <proc> <scratch> ResourceForAll -
  65. %        *ResourceFileName
  66. %            <key> <scratch> ResourceFileName <filename>
  67. %    Additional, specific to our implementation:
  68. %        .Instances (dictionary)
  69. %        .LocalInstances
  70. %            - .LocalInstances <dict>
  71. %        .GetInstance
  72. %            <key> .GetInstance <instance> -true-
  73. %            <key> .GetInstance -false-
  74. %        .CheckResource
  75. %            <key> <value> .CheckResource <key> <value> <ok>
  76. %              (or may give an error if not OK)
  77. %        .DoLoadResource
  78. %            <key> .DoLoadResource <key> (may give an error)
  79. %        .LoadResource
  80. %            <key> .LoadResource - (may give an error)
  81. %        .ResourceFile
  82. %            <key> .ResourceFile <file> -true-
  83. %            <key> .ResourceFile <key> -false-
  84. %        .ResourceFileStatus
  85. %            <key> .ResourceFileStatus 2 <vmusage> -true-
  86. %            <key> .ResourceFileStatus -false-
  87. % All the above procedures expect that the top dictionary on the d-stack
  88. % is the resource dictionary.
  89.  
  90. % Define enough of the Category category so we can define other categories.
  91. % The dictionary we're about to create will become the Category
  92. % category definition dictionary.
  93.  
  94. % .findcategory and .resourceexec are only called from within the
  95. % implementation of the resource 'operators', so they doesn't have to worry
  96. % about cleaning up the stack if they fail (the interpreter's stack
  97. % protection machinery for pseudo-operators takes care of this).
  98. /.findcategory {    % <name> .findcategory -
  99.             %   (pushes the category on the dstack)
  100.   /Category findresource begin
  101. } bind def
  102.  
  103. /.resourceexec {    % <key> /xxxResource .resourceexec -
  104.             %   (also pops the category from the dstack)
  105.   load exec end
  106. } bind def
  107.  
  108. % .getvminstance treats instances on disk as undefined.
  109. /.getvminstance {    % <key> .getvminstance <instance> -true-
  110.             % <key> .getvminstance -false-
  111.   .GetInstance {
  112.     dup 1 get 2 ne { true } { pop false } ifelse
  113.   } {
  114.     false
  115.   } ifelse
  116. } bind def
  117.  
  118. 20 dict begin
  119.  
  120.         % Standard entries
  121.  
  122. /Category /Category def
  123. /InstanceType /dicttype def
  124.  
  125. /DefineResource {
  126.     .CheckResource {
  127.       dup /Category 3 index cvlit .growput
  128.       dup [ exch 0 -1 ] exch
  129.       .Instances 4 2 roll put
  130.         % Make the Category dictionary read-only.  We will have to
  131.         % use .forceput / .forcedef later to replace the dummy,
  132.         % empty .Instances dictionary with the real one later.
  133.       readonly
  134.     } {
  135.       /defineresource load /typecheck signalerror
  136.     } ifelse
  137. } bind def
  138. /FindResource        % (redefined below)
  139.     { .Instances exch get 0 get
  140.     } bind def
  141.  
  142.         % Additional entries
  143.  
  144. /.Instances 30 dict def
  145. .Instances /Category [currentdict 0 -1] put
  146.  
  147. /.LocalInstances 0 dict def
  148. /.GetInstance
  149.     { .Instances exch .knownget
  150.     } bind def
  151. /.CheckResource
  152.     { dup gcheck currentglobal and
  153.        { /DefineResource /FindResource /ResourceForAll /ResourceStatus
  154.          /UndefineResource }
  155.        { 2 index exch known and }
  156.       forall
  157.       not { /defineresource load /invalidaccess signalerror } if
  158.       true
  159.     } bind def
  160.  
  161. .Instances end begin    % for the base case of findresource
  162.  
  163. (END CATEGORY) VMDEBUG
  164.  
  165. % Define the resource operators.  We use the "stack protection" feature of
  166. % odef to make sure the stacks are restored properly on an error.
  167. % This requires that the operators not pop anything from the stack until
  168. % they have executed their logic successfully.  We can't make this
  169. % work for resourceforall, because the procedure it executes mustn't see
  170. % the operands of resourceforall on the stack, but we can make it work for
  171. % the others.
  172.  
  173. % findresource is the only operator that needs to bind //Category.
  174. /findresource {        % <key> <category> findresource <instance>
  175.     2 copy dup /Category eq
  176.       { pop //Category 0 get begin } { .findcategory } ifelse
  177.     /FindResource .resourceexec exch pop exch pop
  178. } bind
  179. end        % .Instances of Category
  180. odef
  181.  
  182. /defineresource {    % <key> <instance> <category> defineresource <instance>
  183.     3 copy .findcategory
  184.     currentdict /InstanceType known {
  185.       dup type InstanceType ne {
  186.         dup type /packedarraytype eq InstanceType /arraytype eq and
  187.         not { /defineresource load /typecheck signalerror } if
  188.       } if
  189.     } if
  190.     /DefineResource .resourceexec
  191.     4 1 roll pop pop pop
  192. } bind odef
  193. % We must prevent resourceforall from automatically restoring the stacks,
  194. % because we don't want the stacks restored if proc causes an error or
  195. % executes a 'stop'. On the other hand, resourceforall is defined in the
  196. % PLRM as an operator, so it must have type /operatortype.  We hack this
  197. % by taking advantage of the fact that the interpreter optimizes tail
  198. % calls, so stack protection doesn't apply to the very last token of an
  199. % operator procedure.
  200. /resourceforall1 {    % <template> <proc> <scratch> <category> resourceforall1 -
  201.     dup /Category findresource begin
  202.     /ResourceForAll load
  203.     % Stack: <template> <proc> <scratch> <category> proc
  204.     exch pop        % pop the category
  205.     exec end
  206. } bind def
  207. /resourceforall {    % <template> <proc> <scratch> <category> resourceforall1 -
  208.     //resourceforall1 exec        % see above
  209. } bind odef
  210. /resourcestatus {    % <key> <category> resourcestatus <status> <size> true
  211.             % <key> <category> resourcestatus false
  212.     2 copy .findcategory /ResourceStatus .resourceexec
  213.      { 4 2 roll pop pop true } { pop pop false } ifelse
  214. } bind odef
  215. /undefineresource {    % <key> <category> undefineresource -
  216.     2 copy .findcategory /UndefineResource .resourceexec pop pop
  217. } bind odef
  218.  
  219. % Define the system parameters used for the Generic implementation of
  220. % ResourceFileName.
  221. systemdict begin
  222.  
  223. %     - .default_resource_dir <string>
  224. /.default_resource_dir {
  225.   (CMap) findlibfile {
  226.   closefile (CMap) search {
  227.     exch pop exch pop
  228.     } if
  229.   } {
  230.     pop        % (CMap)
  231.     .file_name_parent .file_name_directory_separator concatstrings
  232.     (Resource) concatstrings
  233.     /LIBPATH .systemvar {
  234.       dup .file_name_current eq {
  235.         pop
  236.       } {
  237.         1 index false .file_name_combine {
  238.           exch pop exit
  239.         } {
  240.           pop pop
  241.         } ifelse
  242.       } ifelse
  243.     } forall
  244.   } ifelse
  245. } bind def
  246.  
  247. %  <path> <name> <string> .resource_dir_name <path> <name> <string>
  248. /.resource_dir_name
  249. {  systemdict 2 index .knownget {
  250.      exch pop
  251.      systemdict 1 index undef
  252.    } {
  253.      dup () ne {
  254.      .file_name_directory_separator concatstrings
  255.     } if
  256.     2 index exch false .file_name_combine not {
  257.       (ERROR: .default_resource_dir returned ) print exch print ( that can't combine with ) print =
  258.       /.default_resource_dir /configurationerror .signalerror
  259.     } if
  260.   } ifelse
  261. } bind def
  262.  
  263. currentdict /pssystemparams known not {
  264.   /pssystemparams 10 dict readonly def
  265. } if
  266. pssystemparams begin
  267.   .default_resource_dir
  268.   /FontResourceDir (Font) .resource_dir_name
  269.      readonly .forcedef    % pssys'params is r-o
  270.   /GenericResourceDir () .resource_dir_name
  271.      readonly .forcedef    % pssys'params is r-o
  272.   pop % .default_resource_dir
  273.   /GenericResourcePathSep
  274.       .file_name_separator readonly .forcedef        % pssys'params is r-o
  275.   (%diskFontResourceDir) cvn (/Resource/Font/) readonly .forcedef    % pssys'params is r-o
  276.   (%diskGenericResourceDir) cvn (/Resource/) readonly .forcedef    % pssys'params is r-o
  277. end
  278. end
  279.  
  280. % Check if GenericResourceDir presents in LIBPATH.
  281.  
  282. % The value of GenericResourceDir must end with directory separator.
  283. % We use .file_name_combine to check it. 
  284. % Comments use OpenVMS syntax, because it is the most complicated case.
  285. (x) pssystemparams /GenericResourcePathSep get
  286. (y) concatstrings concatstrings dup length              % (x]y) l1
  287. pssystemparams /GenericResourceDir get dup length exch  % (x]y) l1 l2 (dir)
  288. 3 index true .file_name_combine not {
  289.   exch
  290.   (File name ) print print ( cant combine with ) print =
  291.   /GenericResourceDir cvx /configurationerror signalerror
  292. } if
  293. dup length                                              % (x]y) l1 l2 (dir.x]y) l
  294. 4 2 roll add                                            % (x]y) (dir.x]y) l ll
  295. ne {
  296.   (GenericResourceDir value does not end with directory separator.\n) =
  297.   /GenericResourceDir cvx /configurationerror signalerror
  298. } if
  299. pop pop
  300.  
  301. % Define the generic algorithm for computing resource file names.
  302. /.rfnstring 8192 string def
  303. /.genericrfn        % <key> <scratch> <prefix> .genericrfn <filename>
  304.  { 3 -1 roll //.rfnstring cvs concatstrings exch copy
  305.  } bind def
  306.  
  307. % Define a procedure for making a packed array in local VM.
  308. /.localpackedarray {    % <obj1> ... <objn> <n> .localpackedarray <packedarray>
  309.   .currentglobal false .setglobal 1 index 2 add 1 roll
  310.   packedarray exch .setglobal
  311. } bind def
  312.  
  313. % Define the Generic category.
  314.  
  315. /Generic mark
  316.  
  317.         % Standard entries
  318.  
  319. % We're still running in Level 1 mode, so dictionaries won't expand.
  320. % Leave room for the /Category entry.
  321. /Category null
  322.  
  323. % Implement the body of Generic resourceforall for local, global, and
  324. % external cases.  'args' is [template proc scratch resdict].
  325. /.enumerateresource {    % <key> [- <proc> <scratch>] .enumerateresource -
  326.   1 index type dup /stringtype eq exch /nametype eq or {
  327.     exch 1 index 2 get cvs exch
  328.   } if
  329.     % Use .setstackprotect to prevent the stacks from being restored if
  330.     % an error occurs during execution of proc.
  331.   1 get false .setstackprotect exec true .setstackprotect
  332. } bind def
  333. /.localresourceforall {        % <key> <value> <args> .localr'forall -
  334.   exch pop
  335.   2 copy 0 get .stringmatch { .enumerateresource } { pop pop } ifelse
  336. } bind def
  337. /.globalresourceforall {    % <key> <value> <args> .globalr'forall -
  338.   exch pop
  339.   2 copy 0 get .stringmatch {
  340.     dup 3 get begin .LocalInstances end 2 index known not {
  341.       .enumerateresource
  342.     } {
  343.       pop pop
  344.     } ifelse
  345.   } {
  346.     pop pop
  347.   } ifelse
  348. } bind def
  349. /.externalresourceforall {    % <filename> <len> <args> .externalr'forall -
  350.   3 1 roll 1 index length 1 index sub getinterval exch
  351.   dup 3 get begin .Instances .LocalInstances end
  352.         % Stack: key args insts localinsts
  353.   3 index known {
  354.     pop pop pop
  355.   } {
  356.     2 index known { pop pop } { .enumerateresource } ifelse
  357.   } ifelse
  358. } bind def
  359.  
  360. /DefineResource {
  361.     .CheckResource
  362.        { dup [ exch 0 -1 ]
  363.             % Stack: key value instance
  364.          currentglobal
  365.           { false setglobal 2 index UndefineResource    % remove local def if any
  366.         true setglobal
  367.         .Instances dup //.emptydict eq {
  368.           pop 3 dict
  369.             % As noted above, Category dictionaries are read-only,
  370.             % so we have to use .forcedef here.
  371.           /.Instances 1 index .forcedef    % Category dict is read-only
  372.         } if
  373.           }
  374.           { .LocalInstances dup //.emptydict eq
  375.              { pop 3 dict localinstancedict Category 2 index put
  376.          }
  377.         if
  378.           }
  379.          ifelse
  380.             % Stack: key value instance instancedict
  381.          3 index 2 index .growput
  382.             % Now make the resource value read-only.
  383.          0 2 copy get { readonly } .internalstopped pop
  384.          dup 4 1 roll put exch pop exch pop
  385.        }
  386.        { /defineresource load /typecheck signalerror
  387.        }
  388.     ifelse
  389. } .bind executeonly        % executeonly to prevent access to .forcedef
  390. /UndefineResource
  391.     {  { dup 2 index .knownget
  392.           { dup 1 get 1 ge
  393.          { dup 0 null put 1 2 put pop pop }
  394.          { pop exch .undef }
  395.         ifelse
  396.           }
  397.           { pop pop
  398.           }
  399.          ifelse
  400.        }
  401.       currentglobal
  402.        { 2 copy .Instances exch exec
  403.        }
  404.       if .LocalInstances exch exec
  405.     } bind
  406. % Because of some badly designed code in Adobe's CID font downloader that
  407. % makes findresource and resourcestatus deliberately inconsistent with each
  408. % other, the default FindResource must not call ResourceStatus if there is
  409. % an instance of the desired name already defined in VM.
  410. /FindResource {
  411.     dup .getvminstance {
  412.       exch pop 0 get
  413.     } {
  414.       dup ResourceStatus {
  415.         pop 1 gt {
  416.           .DoLoadResource .getvminstance not {
  417.         /findresource load /undefinedresource signalerror
  418.           } if 0 get
  419.         } {
  420.           .GetInstance pop 0 get
  421.         } ifelse
  422.       } {
  423.        /findresource load /undefinedresource signalerror
  424.       } ifelse
  425.     } ifelse
  426. } bind
  427. % Because of some badly designed code in Adobe's CID font downloader, the
  428. % definition of ResourceStatus for Generic and Font must be the same (!).
  429. % We patch around this by using an intermediate .ResourceFileStatus procedure.
  430. /ResourceStatus {
  431.     dup .GetInstance {
  432.       exch pop dup 1 get exch 2 get true
  433.     } {
  434.       .ResourceFileStatus
  435.     } ifelse
  436. } bind
  437. /.ResourceFileStatus {
  438.     .ResourceFile { closefile 2 -1 true } { pop false } ifelse
  439. } bind
  440. /ResourceForAll {
  441.         % Construct a new procedure to hold the arguments.
  442.         % All objects constructed here must be in local VM to avoid
  443.         % a possible invalidaccess.
  444.     currentdict 4 .localpackedarray    % [template proc scratch resdict]
  445.         % We must pop the resource dictionary off the dict stack
  446.         % when doing the actual iteration, and restore it afterwards.
  447.     .currentglobal not {
  448.       .LocalInstances length 0 ne {
  449.         % We must do local instances, and do them first.
  450.         //.localresourceforall {exec} 0 get 3 .localpackedarray cvx
  451.         .LocalInstances exch {forall} 0 get 1 index 0 get
  452.         currentdict end 3 .execn begin
  453.       } if
  454.     } if
  455.         % Do global instances next.
  456.     //.globalresourceforall {exec} 0 get 3 .localpackedarray cvx
  457.     .Instances exch cvx {forall} 0 get 1 index 0 get
  458.     currentdict end 3 .execn begin
  459.         mark                                             % args [
  460.         Category .namestring .file_name_separator concatstrings
  461.         2 index 0 get                                    % args [ (c/) (t)
  462.         dup length 3 1 roll                              % args [ l (c/) (t)
  463.         concatstrings                                    % args [ l (c/t)
  464.     [ 
  465.       /LIBPATH .systemvar 2 index
  466.       .generate_dir_list_templates                   % args (t) [ l [(pt)]
  467.         % also add on the Resources as specified by the GenericResourceDir
  468.           [ currentsystemparams /GenericResourceDir get]
  469.       counttomark 1 add index .generate_dir_list_templates
  470.             % Resource files on OpenVMS requires a separate template (gs:[dir.*]*)
  471.           [ currentsystemparams /GenericResourceDir get]
  472.       counttomark 1 add index .file_name_separator (*)
  473.           concatstrings concatstrings .generate_dir_list_templates
  474.       ] exch pop
  475.     {                                                % args [ l (pt)
  476.       dup length 2 index sub exch                    % args [ l Lp (pt)
  477.  
  478.       {                                              % args [ l Lp (pf)
  479.         dup length                                   % args [ l Lp (pf) Lpf
  480.         2 index sub                                  % args [ l Lp (pf) Lf
  481.         2 index exch                                 % args [ l Lp (pf) Lp Lf
  482.         getinterval cvn dup                          % args [ l Lp /n /n
  483.         4 2 roll                                     % args [ /n /n l Lp
  484.       } //.rfnstring filenameforall
  485.       pop                                            % args [ l /n1 /n1 ... /nN /nN l
  486.     } forall                                         % args [ l /n1 /n1 ... /nN /nN
  487.     pop
  488.     .dicttomark % An easy way to exclude duplicates. % args <</n/n>>
  489.       % {
  490.       { pop } 0 get
  491.           2 index 2 get { cvs 0 } aload pop 5 index
  492.           //.externalresourceforall {exec} 0 get
  493.           % }
  494.         7 .localpackedarray cvx
  495.         3 2 roll pop % args
  496.     { forall } 0 get
  497.       currentdict end 2 .execn begin
  498. } bind
  499. /ResourceFileName
  500.       {                                             % /in (scr)
  501.         exch //.rfnstring cvs                       % (scr) (n)
  502.             /GenericResourcePathSep getsystemparam exch % (scr) (/) (n)
  503.             Category .namestring                        % (scr) (/) (n) (c)
  504.             3 1 roll                                    % (scr) (c) (/) (n)
  505.         concatstrings concatstrings                 % (scr) (c/n)
  506.             /GenericResourceDir getsystemparam .file_name_is_absolute not {
  507.               /GenericResourceDir getsystemparam exch concatstrings
  508.           findlibfile
  509.           {                                         % (scr) (p/c/n) file
  510.             pop exch copy true                      % (p/c/n) true
  511.           } {                                       % (scr) (c/n)
  512.             false                                   % (scr) (c/n) false
  513.           } ifelse
  514.         } {                                         % (scr) (c/n)
  515.           false                                     % (scr) (c/n) false
  516.         } ifelse
  517.         not {                                       % (scr) (c/n)
  518.               /GenericResourceDir getsystemparam        % (scr) (c/n) (d/)
  519.               dup length exch                           % (scr) (c/n) Ld (d/)
  520.               3 index copy pop                          % (scr') (c/n) Ld
  521.               1 index length                            % (scr') (c/n) Ld Lcn
  522.               3 index 3 copy pop                        % (scr') (c/n) Ld Lcn (scr') Ld Lcn
  523.               getinterval                               % (scr') (c/n) Ld Lcn (scr[Ld:Lcn])
  524.               4 3 roll exch                             % (scr') Ld Lcn (c/n) (scr[Ld:Lcn])
  525.               copy pop                                  % (scr'') Ld Lcn
  526.               add 0 exch getinterval                    % (scr''[0:Ld+Lcn])
  527.         } if
  528.       } bind
  529.  
  530.         % Additional entries
  531.  
  532. % Unfortunately, we can't create the real .Instances dictionary now,
  533. % because if someone copies the Generic category (which pp. 95-96 of the
  534. % 2nd Edition Red Book says is legitimate), they'll wind up sharing
  535. % the .Instances.  Instead, we have to create .Instances on demand,
  536. % just like the entry in localinstancedict.
  537. % We also have to prevent anyone from creating instances of Generic itself.
  538. /.Instances //.emptydict
  539.  
  540. /.LocalInstances
  541.     { localinstancedict Category .knownget not { //.emptydict } if
  542.     } bind
  543. /.GetInstance
  544.     { currentglobal
  545.        { .Instances exch .knownget }
  546.        { .LocalInstances 1 index .knownget
  547.           { exch pop true }
  548.           { .Instances exch .knownget }
  549.          ifelse
  550.        }
  551.       ifelse
  552.     } bind
  553. /.CheckResource
  554.     { true
  555.     } bind
  556. /.vmused {
  557.         % - .vmused <usedvalue>
  558.         % usedvalue = vmstatus in global + vmstatus in local.
  559.   0 2 {
  560.     .currentglobal not .setglobal
  561.     vmstatus pop exch pop add
  562.   } repeat
  563. } bind def
  564. /.DoLoadResource {
  565.         % .LoadResource may push entries on the operand stack.
  566.         % It is an undocumented feature of Adobe implementations,
  567.         % which we must match for the sake of some badly written
  568.         % font downloading code, that such entries are popped
  569.         % automatically.
  570.     count 1 index cvlit .vmused
  571.         % Stack: key count litkey memused
  572.     {.LoadResource} 4 1 roll 4 .execn
  573.         % Stack: ... count key memused
  574.     .vmused exch sub
  575.     1 index .getvminstance not {
  576.       pop dup /undefinedresource signalerror    % didn't load
  577.     } if
  578.     dup 1 1 put
  579.     2 3 -1 roll put
  580.         % Stack: ... count key
  581.     exch count 1 sub exch sub {exch pop} repeat
  582. } bind
  583. /.LoadResource
  584.     { dup .ResourceFile
  585.        { exch pop currentglobal
  586.           { .runresource }
  587.           { true setglobal { .runresource } stopped false setglobal { stop } if }
  588.          ifelse
  589.        }
  590.        { dup /undefinedresource signalerror
  591.        }
  592.      ifelse
  593.     } bind
  594. /.ResourceFile
  595.         {
  596.           Category //.rfnstring cvs length                      % key l
  597.           dup //.rfnstring dup length 2 index sub               % key l l (buf) L-l
  598.           3 2 roll exch getinterval                             % key l ()
  599.           .file_name_directory_separator exch copy length add   % key l1
  600.           dup //.rfnstring dup length 2 index sub               % key l1 l1 (buf) L-l
  601.           3 2 roll exch getinterval                             % key l1 ()
  602.           2 index exch cvs length add                           % key l2
  603.           //.rfnstring exch 0 exch getinterval                  % key (relative_path)
  604.           .libfile {
  605.             exch pop true
  606.           } {
  607.             pop
  608.             currentdict /ResourceFileName known {
  609.           mark 1 index //.rfnstring { ResourceFileName } .internalstopped {
  610.             cleartomark false
  611.           } {
  612.             dup status {
  613.               pop pop pop pop
  614.               (r) file
  615.               exch pop exch pop true
  616.             } {
  617.               cleartomark false
  618.             } ifelse
  619.           } ifelse
  620.          } {
  621.            pop false
  622.          } ifelse
  623.           } ifelse
  624.     } bind
  625.  
  626.  
  627.  
  628. .dicttomark
  629. /Category defineresource pop
  630.  
  631. % Fill in the rest of the Category category.
  632. /Category /Category findresource dup
  633. /Generic /Category findresource begin {
  634.   /FindResource /ResourceForAll /ResourceStatus /.ResourceFileStatus
  635.   /UndefineResource /ResourceFileName
  636.   /.ResourceFile /.LoadResource /.DoLoadResource
  637. } { dup load put dup } forall
  638. pop readonly pop end
  639.  
  640. (END GENERIC) VMDEBUG
  641.  
  642. % Define the fixed categories.
  643.  
  644. mark
  645.     % Non-Type categories with existing entries.
  646.  /ColorSpaceFamily
  647.    { }    % These must be deferred, because optional features may add some.
  648.  /Emulator
  649.    mark EMULATORS { cvn } forall .packtomark
  650.  /Filter
  651.    { }    % These must be deferred, because optional features may add some.
  652.  /IODevice
  653.     % Loop until the .getiodevice gets a rangecheck.
  654.    errordict /rangecheck 2 copy get
  655.    errordict /rangecheck { pop stop } put    % pop the command
  656.    mark 0 { {
  657.     dup .getiodevice dup null eq { pop } { exch } ifelse 1 add
  658.    } loop} .internalstopped
  659.    pop pop pop .packtomark
  660.    4 1 roll put
  661.    .clearerror
  662.     % Type categories listed in the Red Book.
  663.  /ColorRenderingType
  664.    { }    % These must be deferred, because optional features may add some.
  665.  /FMapType
  666.    { }    % These must be deferred, because optional features may add some.
  667.  /FontType
  668.    { }    % These must be deferred, because optional features may add some.
  669.  /FormType
  670.    { }    % These must be deferred, because optional features may add some.
  671.  /HalftoneType
  672.    { }    % These must be deferred, because optional features may add some.
  673.  /ImageType
  674.    { }    % Deferred, optional features may add some.
  675.  /PatternType
  676.    { }  % Deferred, optional features may add some.
  677.     % Type categories added since the Red Book.
  678.  /setsmoothness where {
  679.    pop /ShadingType { }    % Deferred, optional features may add some.
  680.  } if
  681. counttomark 2 idiv
  682.  { mark
  683.  
  684.         % Standard entries
  685.  
  686.         % We'd like to prohibit defineresource,
  687.         % but because optional features may add entries, we can't.
  688.         % We can at least require that the key and value match.
  689.    /DefineResource
  690.     { currentglobal not
  691.        { /defineresource load /invalidaccess signalerror }
  692.        { 2 copy ne
  693.           { /defineresource load /rangecheck signalerror }
  694.           { dup .Instances 4 -2 roll .growput }
  695.          ifelse
  696.        }
  697.       ifelse
  698.     } bind
  699.    /UndefineResource
  700.     { /undefineresource load /invalidaccess signalerror } bind
  701.    /FindResource
  702.     { .Instances 1 index .knownget
  703.        { exch pop }
  704.        { /findresource load /undefinedresource signalerror }
  705.       ifelse
  706.     } bind
  707.    /ResourceStatus
  708.     { .Instances exch known { 0 0 true } { false } ifelse } bind
  709.    /ResourceForAll
  710.     /Generic /Category findresource /ResourceForAll get
  711.  
  712.         % Additional entries
  713.  
  714.    counttomark 2 add -1 roll
  715.    dup length dict dup begin exch { dup def } forall end
  716.         % We'd like to make the .Instances readonly here,
  717.         % but because optional features may add entries, we can't.
  718.    /.Instances exch
  719.    /.LocalInstances    % used by ResourceForAll
  720.     0 dict def
  721.  
  722.    .dicttomark /Category defineresource pop
  723.  } repeat pop
  724.  
  725. (END FIXED) VMDEBUG
  726.  
  727. % Define the other built-in categories.
  728.  
  729. /.definecategory    % <name> -mark- <key1> ... <valuen> .definecategory -
  730.  { counttomark 2 idiv 2 add        % .Instances, Category
  731.    /Generic /Category findresource dup maxlength 3 -1 roll add
  732.    dict .copydict begin
  733.    counttomark 2 idiv { def } repeat pop    % pop the mark
  734.    currentdict end /Category defineresource pop
  735.  } bind def
  736.  
  737. /ColorRendering mark /InstanceType /dicttype .definecategory
  738. % ColorSpace is defined below
  739. % Encoding is defined below
  740. % Font is defined below
  741. /Form mark /InstanceType /dicttype .definecategory
  742. /Halftone mark /InstanceType /dicttype .definecategory
  743. /Pattern mark /InstanceType /dicttype .definecategory
  744. /ProcSet mark /InstanceType /dicttype .definecategory
  745. % Added since the Red Book:
  746. /ControlLanguage mark /InstanceType /dicttype .definecategory
  747. /HWOptions mark /InstanceType /dicttype .definecategory
  748. /Localization mark /InstanceType /dicttype .definecategory
  749. /OutputDevice mark /InstanceType /dicttype .definecategory
  750. /PDL mark /InstanceType /dicttype .definecategory
  751. % CIDFont, CIDMap, and CMap are defined in gs_cidfn.ps
  752. % FontSet is defined in gs_cff.ps
  753. % IdiomSet is defined in gs_ll3.ps
  754. % InkParams and TrapParams are defined in gs_trap.ps
  755.  
  756. (END MISC) VMDEBUG
  757.  
  758. % Define the ColorSpace category.
  759.  
  760. /.defaultcsnames mark
  761.   /DefaultGray 0
  762.   /DefaultRGB 1
  763.   /DefaultCMYK 2
  764. .dicttomark readonly def
  765.  
  766. % The "hooks" are no-ops here, redefined in LL3.
  767. /.definedefaultcs {    % <index> <value> .definedefaultcs -
  768.   pop pop
  769. } bind def
  770. /.undefinedefaultcs {    % <index> .undefinedefaultcs -
  771.   pop
  772. } bind def
  773.  
  774. /ColorSpace mark
  775.  
  776. /InstanceType /arraytype
  777.  
  778. % We keep track of whether there are any local definitions for any of
  779. % the Default keys.  This information must get saved and restored in
  780. % parallel with the local instance dictionary, so it must be stored in
  781. % local VM.
  782. userdict /.localcsdefaults false put
  783.  
  784. /DefineResource {
  785.   2 copy /Generic /Category findresource /DefineResource get exec
  786.   exch pop
  787.   exch //.defaultcsnames exch .knownget {
  788.     1 index .definedefaultcs
  789.     currentglobal not { .userdict /.localcsdefaults true put } if
  790.   } if
  791. } bind
  792.  
  793. /UndefineResource {
  794.   dup /Generic /Category findresource /UndefineResource get exec
  795.   //.defaultcsnames 1 index .knownget {
  796.     % Stack: resname index
  797.     currentglobal {
  798.       .undefinedefaultcs pop
  799.     } {
  800.     % We removed the local definition, but there might be a global one.
  801.       exch .GetInstance {
  802.     0 get .definedefaultcs
  803.       } {
  804.     .undefinedefaultcs
  805.       } ifelse
  806.     % Recompute .localcsdefaults by scanning.  This is rarely needed.
  807.       .userdict /.localcsdefaults false //.defaultcsnames {
  808.     pop .LocalInstances exch known { pop true exit } if
  809.       } forall put
  810.     } ifelse
  811.   } {
  812.     pop
  813.   } ifelse
  814. } bind
  815.  
  816. .definecategory            % ColorSpace
  817.  
  818. % Define the Encoding category.
  819.  
  820. /Encoding mark
  821.  
  822. /InstanceType /arraytype
  823.  
  824. % Handle already-registered encodings, including lazily loaded encodings
  825. % that aren't loaded yet.
  826.  
  827. /.Instances mark
  828.   EncodingDirectory
  829.    { dup length 256 eq { [ exch readonly 0 -1 ] } { pop [null 2 -1] } ifelse
  830.    } forall
  831. .dicttomark
  832.  
  833. /.ResourceFileDict mark
  834.   EncodingDirectory
  835.    { dup length 256 eq { pop pop } { 0 get } ifelse
  836.    } forall
  837. .dicttomark
  838.  
  839. /ResourceFileName
  840.  { .ResourceFileDict 2 index .knownget
  841.     { exch copy exch pop }
  842.     { /Generic /Category findresource /ResourceFileName get exec }
  843.    ifelse
  844.  } bind
  845.  
  846. .definecategory            % Encoding
  847.  
  848. % Make placeholders in level2dict for the redefined Encoding operators,
  849. % so that they will be swapped properly when we switch language levels.
  850.  
  851. /.findencoding /.findencoding load def
  852. /findencoding /findencoding load def
  853. /.defineencoding /.defineencoding load def
  854.  
  855. (END ENCODING) VMDEBUG
  856.  
  857. % Define the Font category.
  858.  
  859. /.fontstatus {        % <fontname> .fontstatus <fontname> <found>
  860.   {        % Create a loop context just so we can exit it early.
  861.         % Check Fontmap.
  862.     Fontmap 1 index .knownget {
  863.       {
  864.     dup type /nametype eq {
  865.       .fontstatus { pop null exit } if
  866.     } {
  867.       dup type /stringtype eq {
  868.         findlibfile { closefile pop null exit } if pop
  869.       } {
  870.         % Procedure, assume success.
  871.         pop null exit
  872.       } ifelse
  873.     } ifelse
  874.       } forall dup null eq { pop true exit } if
  875.     } if
  876.         % Convert names to strings; give up on other types.
  877.     dup type /nametype eq { .namestring } if
  878.     dup type /stringtype ne { false exit } if
  879.         % Check the resource directory.
  880.     dup .fonttempstring /FontResourceDir getsystemparam .genericrfn
  881.     status {
  882.       pop pop pop pop true exit
  883.     } if
  884.         % Check for a file on the search path with the same name
  885.         % as the font.
  886.     findlibfile { closefile true exit } if
  887.         % Scan a FONTPATH directory and try again.
  888.     .scannextfontdir not { false exit } if
  889.   } loop
  890. } bind def
  891.  
  892. /Font mark
  893.  
  894. /InstanceType /dicttype
  895.  
  896. /DefineResource
  897.     { 2 copy //definefont exch pop
  898.       /Generic /Category findresource /DefineResource get exec
  899.     } bind
  900. /UndefineResource
  901.     { dup //undefinefont
  902.       /Generic /Category findresource /UndefineResource get exec
  903.     } bind
  904. /FindResource {
  905.     dup .getvminstance {
  906.       exch pop 0 get
  907.     } {
  908.       dup ResourceStatus {
  909.         pop 1 gt { .loadfontresource } { .GetInstance pop 0 get } ifelse
  910.       } {
  911.         .loadfontresource
  912.       } ifelse
  913.     } ifelse
  914. } bind
  915. /ResourceForAll {
  916.     { .scannextfontdir not { exit } if } loop
  917.     /Generic /Category findresource /ResourceForAll get exec
  918. } bind
  919. /.ResourceFileStatus {
  920.     .fontstatus { pop 2 -1 true } { pop false } ifelse
  921. } bind
  922.  
  923. /.loadfontresource {
  924.     dup vmstatus pop exch pop exch
  925.         % Hack: rebind .currentresourcefile so that all calls of
  926.         % definefont will know these are built-in fonts.
  927.     currentfile {pop //findfont exec} .execasresource  % (findfont is a procedure)
  928.     exch vmstatus pop exch pop exch sub
  929.         % stack: name font vmused
  930.         % findfont has the prerogative of not calling definefont
  931.         % in certain obscure cases of font substitution.
  932.     2 index .getvminstance {
  933.       dup 1 1 put
  934.       2 3 -1 roll put
  935.     } {
  936.       pop
  937.     } ifelse exch pop
  938. } bind
  939.  
  940. /.Instances FontDirectory length 2 mul dict
  941.  
  942. .definecategory            % Font
  943.  
  944. % Redefine font "operators".
  945. /.definefontmap
  946.  { /Font /Category findresource /.Instances get
  947.    dup 3 index known
  948.     { pop
  949.     }
  950.     { 2 index
  951.         % Make sure we create the array in global VM.
  952.       .currentglobal true .setglobal
  953.       [null 2 -1] exch .setglobal
  954.       .growput
  955.     }
  956.    ifelse
  957.    //.definefontmap exec
  958.  } bind def
  959.  
  960. % Make sure the old definitions are still in systemdict so that
  961. % they will get bound properly.
  962. systemdict begin
  963.   /.origdefinefont /definefont load def
  964.   /.origundefinefont /undefinefont load def
  965.   /.origfindfont /findfont load def
  966. end
  967. /definefont {
  968.   /Font defineresource
  969. } bind odef
  970. /undefinefont {
  971.   /Font undefineresource
  972. } bind odef
  973. % The Red Book requires that findfont be a procedure, not an operator,
  974. % but it still needs to restore the stacks reliably if it fails.
  975. /.findfontop {
  976.   /Font findresource
  977. } bind odef
  978. /findfont {
  979.   .findfontop
  980. } bind def    % Must be a procedure, not an operator
  981.  
  982. % Remove initialization utilities.
  983. currentdict /.definecategory .undef
  984. currentdict /.emptydict .undef
  985.  
  986. end                % level2dict
  987.  
  988. % Convert deferred resources after we finally switch to Level 2.
  989.  
  990. /.fixresources {
  991.     % Encoding resources
  992.   EncodingDirectory
  993.    { dup length 256 eq
  994.       { /Encoding defineresource pop }
  995.       { pop pop }
  996.      ifelse
  997.    } forall
  998.   /.findencoding { /Encoding findresource } bind def
  999.   /findencoding /.findencoding load def        % must be a procedure
  1000.   /.defineencoding { /Encoding defineresource pop } bind def
  1001.     % ColorRendering resources and ProcSet
  1002.   systemdict /ColorRendering .knownget {
  1003.     /ColorRendering exch /ProcSet defineresource pop
  1004.     systemdict /ColorRendering undef
  1005.     /DefaultColorRendering currentcolorrendering /ColorRendering defineresource pop
  1006.   } if
  1007.     % ColorSpace resources
  1008.   systemdict /CIEsRGB .knownget {
  1009.     /sRGB exch /ColorSpace defineresource pop
  1010.     systemdict /CIEsRGB undef
  1011.   } if
  1012.     % ColorSpaceFamily resources
  1013.   colorspacedict { pop dup /ColorSpaceFamily defineresource pop } forall
  1014.     % Filter resources
  1015.   filterdict { pop dup /Filter defineresource pop } forall
  1016.     % FontType and FMapType resources
  1017.   buildfontdict { pop dup /FontType defineresource pop } forall
  1018.   mark
  1019.     buildfontdict 0 known { 2 3 4 5 6 7 8 } if
  1020.     buildfontdict 9 known { 9 } if
  1021.   counttomark { dup /FMapType defineresource pop } repeat pop
  1022.     % FormType resources
  1023.   .formtypes { pop dup /FormType defineresource pop } forall
  1024.     % HalftoneType resources
  1025.   .halftonetypes { pop dup /HalftoneType defineresource pop } forall
  1026.     % ColorRenderingType resources
  1027.   .colorrenderingtypes {pop dup /ColorRenderingType defineresource pop} forall
  1028.     % ImageType resources
  1029.   .imagetypes { pop dup /ImageType defineresource pop } forall
  1030.     % PatternType resources
  1031.   .patterntypes { pop dup /PatternType defineresource pop } forall
  1032.     % Make the fixed resource categories immutable.
  1033.   /.shadingtypes where {
  1034.     pop .shadingtypes { pop dup /ShadingType defineresource pop } forall
  1035.   } if
  1036.   [ /ColorSpaceFamily /Emulator /Filter /IODevice /ColorRenderingType
  1037.     /FMapType /FontType /FormType /HalftoneType /ImageType /PatternType
  1038.     /.shadingtypes where { pop /ShadingType } if
  1039.   ] {
  1040.     /Category findresource
  1041.     dup /.Instances get readonly pop
  1042.     .LocalInstances readonly pop
  1043.     readonly pop
  1044.   } forall
  1045.     % clean up
  1046.   systemdict /.fixresources undef
  1047. } bind def
  1048.  
  1049. %% Replace 1 (gs_resmp.ps)
  1050. (gs_resmp.ps)  dup runlibfile VMDEBUG
  1051. %% Replace 1 (gs_resst.ps)
  1052. (gs_resst.ps)  dup runlibfile VMDEBUG
  1053.