home *** CD-ROM | disk | FTP | other *** search
/ jppd.dyndns.org / jppd.dyndns.org.tar / jppd.dyndns.org / QUERYPRO / Impressora_PDF / converter.exe / GPLGS / gs_res.ps < prev    next >
Text File  |  2004-09-01  |  36KB  |  1,048 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.   .file_name_parent .file_name_directory_separator concatstrings
  226.   (Resource) concatstrings
  227.   /LIBPATH .systemvar {
  228.     dup .file_name_current eq {
  229.       pop
  230.     } {
  231.       1 index false .file_name_combine {
  232.         exch pop exit
  233.       } {
  234.         pop pop
  235.       } ifelse
  236.     } ifelse
  237.   } forall
  238. } bind def
  239.  
  240. %  <path> <name> <string> .resource_dir_name <path> <name> <string>
  241. /.resource_dir_name
  242. {  systemdict 2 index .knownget {
  243.      exch pop
  244.      systemdict 1 index undef
  245.    } {
  246.      dup () ne {
  247.      .file_name_directory_separator concatstrings
  248.     } if
  249.     2 index exch false .file_name_combine not {
  250.       (Error: .default_resource_dir returned ) print exch print ( that can't combine with ) print =
  251.       /.default_resource_dir /configurationerror .signalerror
  252.     } if
  253.   } ifelse
  254. } bind def
  255.  
  256. currentdict /pssystemparams known not {
  257.   /pssystemparams 10 dict readonly def
  258. } if
  259. pssystemparams begin
  260.   .default_resource_dir
  261.   /FontResourceDir (Font) .resource_dir_name
  262.      readonly .forcedef    % pssys'params is r-o
  263.   /GenericResourceDir () .resource_dir_name
  264.      readonly .forcedef    % pssys'params is r-o
  265.   pop % .default_resource_dir
  266.   /GenericResourcePathSep
  267.       .file_name_separator readonly .forcedef        % pssys'params is r-o
  268.   (%diskFontResourceDir) cvn (/Resource/Font/) readonly .forcedef    % pssys'params is r-o
  269.   (%diskGenericResourceDir) cvn (/Resource/) readonly .forcedef    % pssys'params is r-o
  270. end
  271. end
  272.  
  273. % Check if GenericResourceDir presents in LIBPATH.
  274.  
  275. % The value of GenericResourceDir must end with directory separator.
  276. % We use .file_name_combine to check it. 
  277. % Comments use OpenVMS syntax, because it is the most complicated case.
  278. (x) pssystemparams /GenericResourcePathSep get
  279. (y) concatstrings concatstrings dup length              % (x]y) l1
  280. pssystemparams /GenericResourceDir get dup length exch  % (x]y) l1 l2 (dir)
  281. 3 index true .file_name_combine not {
  282.   exch
  283.   (File name ) print print ( cant combine with ) print =
  284.   /GenericResourceDir cvx /configurationerror signalerror
  285. } if
  286. dup length                                              % (x]y) l1 l2 (dir.x]y) l
  287. 4 2 roll add                                            % (x]y) (dir.x]y) l ll
  288. ne {
  289.   (GenericResourceDir value does not end with directory separator.\n) =
  290.   /GenericResourceDir cvx /configurationerror signalerror
  291. } if
  292. pop pop
  293.  
  294. % Define the generic algorithm for computing resource file names.
  295. /.rfnstring 8192 string def
  296. /.genericrfn        % <key> <scratch> <prefix> .genericrfn <filename>
  297.  { 3 -1 roll //.rfnstring cvs concatstrings exch copy
  298.  } bind def
  299.  
  300. % Define a procedure for making a packed array in local VM.
  301. /.localpackedarray {    % <obj1> ... <objn> <n> .localpackedarray <packedarray>
  302.   .currentglobal false .setglobal 1 index 2 add 1 roll
  303.   packedarray exch .setglobal
  304. } bind def
  305.  
  306. % Define the Generic category.
  307.  
  308. /Generic mark
  309.  
  310.         % Standard entries
  311.  
  312. % We're still running in Level 1 mode, so dictionaries won't expand.
  313. % Leave room for the /Category entry.
  314. /Category null
  315.  
  316. % Implement the body of Generic resourceforall for local, global, and
  317. % external cases.  'args' is [template proc scratch resdict].
  318. /.enumerateresource {    % <key> [- <proc> <scratch>] .enumerateresource -
  319.   1 index type dup /stringtype eq exch /nametype eq or {
  320.     exch 1 index 2 get cvs exch
  321.   } if
  322.     % Use .setstackprotect to prevent the stacks from being restored if
  323.     % an error occurs during execution of proc.
  324.   1 get false .setstackprotect exec true .setstackprotect
  325. } bind def
  326. /.localresourceforall {        % <key> <value> <args> .localr'forall -
  327.   exch pop
  328.   2 copy 0 get .stringmatch { .enumerateresource } { pop pop } ifelse
  329. } bind def
  330. /.globalresourceforall {    % <key> <value> <args> .globalr'forall -
  331.   exch pop
  332.   2 copy 0 get .stringmatch {
  333.     dup 3 get begin .LocalInstances end 2 index known not {
  334.       .enumerateresource
  335.     } {
  336.       pop pop
  337.     } ifelse
  338.   } {
  339.     pop pop
  340.   } ifelse
  341. } bind def
  342. /.externalresourceforall {    % <filename> <len> <args> .externalr'forall -
  343.   3 1 roll 1 index length 1 index sub getinterval exch
  344.   dup 3 get begin .Instances .LocalInstances end
  345.         % Stack: key args insts localinsts
  346.   3 index known {
  347.     pop pop pop
  348.   } {
  349.     2 index known { pop pop } { .enumerateresource } ifelse
  350.   } ifelse
  351. } bind def
  352.  
  353. /DefineResource {
  354.     .CheckResource
  355.        { dup [ exch 0 -1 ]
  356.             % Stack: key value instance
  357.          currentglobal
  358.           { false setglobal 2 index UndefineResource    % remove local def if any
  359.         true setglobal
  360.         .Instances dup //.emptydict eq {
  361.           pop 3 dict
  362.             % As noted above, Category dictionaries are read-only,
  363.             % so we have to use .forcedef here.
  364.           /.Instances 1 index .forcedef    % Category dict is read-only
  365.         } if
  366.           }
  367.           { .LocalInstances dup //.emptydict eq
  368.              { pop 3 dict localinstancedict Category 2 index put
  369.          }
  370.         if
  371.           }
  372.          ifelse
  373.             % Stack: key value instance instancedict
  374.          3 index 2 index .growput
  375.             % Now make the resource value read-only.
  376.          0 2 copy get { readonly } .internalstopped pop
  377.          dup 4 1 roll put exch pop exch pop
  378.        }
  379.        { /defineresource load /typecheck signalerror
  380.        }
  381.     ifelse
  382. } .bind executeonly        % executeonly to prevent access to .forcedef
  383. /UndefineResource
  384.     {  { dup 2 index .knownget
  385.           { dup 1 get 1 ge
  386.          { dup 0 null put 1 2 put pop pop }
  387.          { pop exch .undef }
  388.         ifelse
  389.           }
  390.           { pop pop
  391.           }
  392.          ifelse
  393.        }
  394.       currentglobal
  395.        { 2 copy .Instances exch exec
  396.        }
  397.       if .LocalInstances exch exec
  398.     } bind
  399. % Because of some badly designed code in Adobe's CID font downloader that
  400. % makes findresource and resourcestatus deliberately inconsistent with each
  401. % other, the default FindResource must not call ResourceStatus if there is
  402. % an instance of the desired name already defined in VM.
  403. /FindResource {
  404.     dup .getvminstance {
  405.       exch pop 0 get
  406.     } {
  407.       dup ResourceStatus {
  408.         pop 1 gt {
  409.           .DoLoadResource .getvminstance not {
  410.         /findresource load /undefinedresource signalerror
  411.           } if 0 get
  412.         } {
  413.           .GetInstance pop 0 get
  414.         } ifelse
  415.       } {
  416.        /findresource load /undefinedresource signalerror
  417.       } ifelse
  418.     } ifelse
  419. } bind
  420. % Because of some badly designed code in Adobe's CID font downloader, the
  421. % definition of ResourceStatus for Generic and Font must be the same (!).
  422. % We patch around this by using an intermediate .ResourceFileStatus procedure.
  423. /ResourceStatus {
  424.     dup .GetInstance {
  425.       exch pop dup 1 get exch 2 get true
  426.     } {
  427.       .ResourceFileStatus
  428.     } ifelse
  429. } bind
  430. /.ResourceFileStatus {
  431.     .ResourceFile { closefile 2 -1 true } { pop false } ifelse
  432. } bind
  433. /ResourceForAll {
  434.         % Construct a new procedure to hold the arguments.
  435.         % All objects constructed here must be in local VM to avoid
  436.         % a possible invalidaccess.
  437.     currentdict 4 .localpackedarray    % [template proc scratch resdict]
  438.         % We must pop the resource dictionary off the dict stack
  439.         % when doing the actual iteration, and restore it afterwards.
  440.     .currentglobal not {
  441.       .LocalInstances length 0 ne {
  442.         % We must do local instances, and do them first.
  443.         //.localresourceforall {exec} 0 get 3 .localpackedarray cvx
  444.         .LocalInstances exch {forall} 0 get 1 index 0 get
  445.         currentdict end 3 .execn begin
  446.       } if
  447.     } if
  448.         % Do global instances next.
  449.     //.globalresourceforall {exec} 0 get 3 .localpackedarray cvx
  450.     .Instances exch cvx {forall} 0 get 1 index 0 get
  451.     currentdict end 3 .execn begin
  452.         mark                                             % args [
  453.         Category .namestring .file_name_separator concatstrings
  454.         2 index 0 get                                    % args [ (c/) (t)
  455.         dup length 3 1 roll                              % args [ l (c/) (t)
  456.         concatstrings                                    % args [ l (c/t)
  457.     [ 
  458.       /LIBPATH .systemvar 2 index
  459.       .generate_dir_list_templates                   % args (t) [ l [(pt)]
  460.         % also add on the Resources as specified by the GenericResourceDir
  461.           [ currentsystemparams /GenericResourceDir get]
  462.       counttomark 1 add index .generate_dir_list_templates
  463.             % Resource files on OpenVMS requires a separate template (gs:[dir.*]*)
  464.           [ currentsystemparams /GenericResourceDir get]
  465.       counttomark 1 add index .file_name_separator (*)
  466.           concatstrings concatstrings .generate_dir_list_templates
  467.       ] exch pop
  468.     {                                                % args [ l (pt)
  469.       dup length 2 index sub exch                    % args [ l Lp (pt)
  470.  
  471.       {                                              % args [ l Lp (pf)
  472.         dup length                                   % args [ l Lp (pf) Lpf
  473.         2 index sub                                  % args [ l Lp (pf) Lf
  474.         2 index exch                                 % args [ l Lp (pf) Lp Lf
  475.         getinterval cvn dup                          % args [ l Lp /n /n
  476.         4 2 roll                                     % args [ /n /n l Lp
  477.       } //.rfnstring filenameforall
  478.       pop                                            % args [ l /n1 /n1 ... /nN /nN l
  479.     } forall                                         % args [ l /n1 /n1 ... /nN /nN
  480.     pop
  481.     .dicttomark % An easy way to exclude duplicates. % args <</n/n>>
  482.       % {
  483.       { pop } 0 get
  484.           2 index 2 get { cvs 0 } aload pop 5 index
  485.           //.externalresourceforall {exec} 0 get
  486.           % }
  487.         7 .localpackedarray cvx
  488.         3 2 roll pop % args
  489.     { forall } 0 get
  490.       currentdict end 2 .execn begin
  491. } bind
  492. /ResourceFileName
  493.       {                                             % /in (scr)
  494.         exch //.rfnstring cvs                       % (scr) (n)
  495.             /GenericResourcePathSep getsystemparam exch % (scr) (/) (n)
  496.             Category .namestring                        % (scr) (/) (n) (c)
  497.             3 1 roll                                    % (scr) (c) (/) (n)
  498.         concatstrings concatstrings                 % (scr) (c/n)
  499.             /GenericResourceDir getsystemparam .file_name_is_absolute not {
  500.           findlibfile
  501.           {                                         % (scr) (p/c/n) file
  502.             pop exch copy true                      % (p/c/n) true
  503.           } {                                       % (scr) (c/n)
  504.             false                                   % (scr) (c/n) false
  505.           } ifelse
  506.         } {                                         % (scr) (c/n)
  507.           false                                     % (scr) (c/n) false
  508.         } ifelse
  509.         not {                                       % (scr) (c/n)
  510.               /GenericResourceDir getsystemparam        % (scr) (c/n) (d/)
  511.               dup length exch                           % (scr) (c/n) Ld (d/)
  512.               3 index copy pop                          % (scr') (c/n) Ld
  513.               1 index length                            % (scr') (c/n) Ld Lcn
  514.               3 index 3 copy pop                        % (scr') (c/n) Ld Lcn (scr') Ld Lcn
  515.               getinterval                               % (scr') (c/n) Ld Lcn (scr[Ld:Lcn])
  516.               4 3 roll exch                             % (scr') Ld Lcn (c/n) (scr[Ld:Lcn])
  517.               copy pop                                  % (scr'') Ld Lcn
  518.               add 0 exch getinterval                    % (scr''[0:Ld+Lcn])
  519.         } if
  520.       } bind
  521.  
  522.         % Additional entries
  523.  
  524. % Unfortunately, we can't create the real .Instances dictionary now,
  525. % because if someone copies the Generic category (which pp. 95-96 of the
  526. % 2nd Edition Red Book says is legitimate), they'll wind up sharing
  527. % the .Instances.  Instead, we have to create .Instances on demand,
  528. % just like the entry in localinstancedict.
  529. % We also have to prevent anyone from creating instances of Generic itself.
  530. /.Instances //.emptydict
  531.  
  532. /.LocalInstances
  533.     { localinstancedict Category .knownget not { //.emptydict } if
  534.     } bind
  535. /.GetInstance
  536.     { currentglobal
  537.        { .Instances exch .knownget }
  538.        { .LocalInstances 1 index .knownget
  539.           { exch pop true }
  540.           { .Instances exch .knownget }
  541.          ifelse
  542.        }
  543.       ifelse
  544.     } bind
  545. /.CheckResource
  546.     { true
  547.     } bind
  548. /.vmused {
  549.         % - .vmused <usedvalue>
  550.         % usedvalue = vmstatus in global + vmstatus in local.
  551.   0 2 {
  552.     .currentglobal not .setglobal
  553.     vmstatus pop exch pop add
  554.   } repeat
  555. } bind def
  556. /.DoLoadResource {
  557.         % .LoadResource may push entries on the operand stack.
  558.         % It is an undocumented feature of Adobe implementations,
  559.         % which we must match for the sake of some badly written
  560.         % font downloading code, that such entries are popped
  561.         % automatically.
  562.     count 1 index cvlit .vmused
  563.         % Stack: key count litkey memused
  564.     {.LoadResource} 4 1 roll 4 .execn
  565.         % Stack: ... count key memused
  566.     .vmused exch sub
  567.     1 index .getvminstance not {
  568.       pop dup /undefinedresource signalerror    % didn't load
  569.     } if
  570.     dup 1 1 put
  571.     2 3 -1 roll put
  572.         % Stack: ... count key
  573.     exch count 1 sub exch sub {exch pop} repeat
  574. } bind
  575. /.LoadResource
  576.     { dup .ResourceFile
  577.        { exch pop currentglobal
  578.           { .runresource }
  579.           { true setglobal { .runresource } stopped false setglobal { stop } if }
  580.          ifelse
  581.        }
  582.        { dup /undefinedresource signalerror
  583.        }
  584.      ifelse
  585.     } bind
  586. /.ResourceFile
  587.         {
  588.           Category //.rfnstring cvs length                      % key l
  589.           dup //.rfnstring dup length 2 index sub               % key l l (buf) L-l
  590.           3 2 roll exch getinterval                             % key l ()
  591.           .file_name_directory_separator exch copy length add   % key l1
  592.           dup //.rfnstring dup length 2 index sub               % key l1 l1 (buf) L-l
  593.           3 2 roll exch getinterval                             % key l1 ()
  594.           2 index exch cvs length add                           % key l2
  595.           //.rfnstring exch 0 exch getinterval                  % key (relative_path)
  596.           .libfile {
  597.             exch pop true
  598.           } {
  599.             pop
  600.             currentdict /ResourceFileName known {
  601.           mark 1 index //.rfnstring { ResourceFileName } .internalstopped {
  602.             cleartomark false
  603.           } {
  604.             dup status {
  605.               pop pop pop pop
  606.               (r) file
  607.               exch pop exch pop true
  608.             } {
  609.               cleartomark false
  610.             } ifelse
  611.           } ifelse
  612.          } {
  613.            pop false
  614.          } ifelse
  615.           } ifelse
  616.     } bind
  617.  
  618.  
  619.  
  620. .dicttomark
  621. /Category defineresource pop
  622.  
  623. % Fill in the rest of the Category category.
  624. /Category /Category findresource dup
  625. /Generic /Category findresource begin {
  626.   /FindResource /ResourceForAll /ResourceStatus /.ResourceFileStatus
  627.   /UndefineResource /ResourceFileName
  628.   /.ResourceFile /.LoadResource /.DoLoadResource
  629. } { dup load put dup } forall
  630. pop readonly pop end
  631.  
  632. (END GENERIC) VMDEBUG
  633.  
  634. % Define the fixed categories.
  635.  
  636. mark
  637.     % Non-Type categories with existing entries.
  638.  /ColorSpaceFamily
  639.    { }    % These must be deferred, because optional features may add some.
  640.  /Emulator
  641.    mark EMULATORS { cvn } forall .packtomark
  642.  /Filter
  643.    { }    % These must be deferred, because optional features may add some.
  644.  /IODevice
  645.     % Loop until the .getiodevice gets a rangecheck.
  646.    errordict /rangecheck 2 copy get
  647.    errordict /rangecheck { pop stop } put    % pop the command
  648.    mark 0 { {
  649.     dup .getiodevice dup null eq { pop } { exch } ifelse 1 add
  650.    } loop} .internalstopped
  651.    pop pop pop .packtomark
  652.    4 1 roll put
  653.    .clearerror
  654.     % Type categories listed in the Red Book.
  655.  /ColorRenderingType
  656.    { }    % These must be deferred, because optional features may add some.
  657.  /FMapType
  658.    { }    % These must be deferred, because optional features may add some.
  659.  /FontType
  660.    { }    % These must be deferred, because optional features may add some.
  661.  /FormType
  662.    { }    % These must be deferred, because optional features may add some.
  663.  /HalftoneType
  664.    { }    % These must be deferred, because optional features may add some.
  665.  /ImageType
  666.    { }    % Deferred, optional features may add some.
  667.  /PatternType
  668.    { }  % Deferred, optional features may add some.
  669.     % Type categories added since the Red Book.
  670.  /setsmoothness where {
  671.    pop /ShadingType { }    % Deferred, optional features may add some.
  672.  } if
  673. counttomark 2 idiv
  674.  { mark
  675.  
  676.         % Standard entries
  677.  
  678.         % We'd like to prohibit defineresource,
  679.         % but because optional features may add entries, we can't.
  680.         % We can at least require that the key and value match.
  681.    /DefineResource
  682.     { currentglobal not
  683.        { /defineresource load /invalidaccess signalerror }
  684.        { 2 copy ne
  685.           { /defineresource load /rangecheck signalerror }
  686.           { dup .Instances 4 -2 roll .growput }
  687.          ifelse
  688.        }
  689.       ifelse
  690.     } bind
  691.    /UndefineResource
  692.     { /undefineresource load /invalidaccess signalerror } bind
  693.    /FindResource
  694.     { .Instances 1 index .knownget
  695.        { exch pop }
  696.        { /findresource load /undefinedresource signalerror }
  697.       ifelse
  698.     } bind
  699.    /ResourceStatus
  700.     { .Instances exch known { 0 0 true } { false } ifelse } bind
  701.    /ResourceForAll
  702.     /Generic /Category findresource /ResourceForAll get
  703.  
  704.         % Additional entries
  705.  
  706.    counttomark 2 add -1 roll
  707.    dup length dict dup begin exch { dup def } forall end
  708.         % We'd like to make the .Instances readonly here,
  709.         % but because optional features may add entries, we can't.
  710.    /.Instances exch
  711.    /.LocalInstances    % used by ResourceForAll
  712.     0 dict def
  713.  
  714.    .dicttomark /Category defineresource pop
  715.  } repeat pop
  716.  
  717. (END FIXED) VMDEBUG
  718.  
  719. % Define the other built-in categories.
  720.  
  721. /.definecategory    % <name> -mark- <key1> ... <valuen> .definecategory -
  722.  { counttomark 2 idiv 2 add        % .Instances, Category
  723.    /Generic /Category findresource dup maxlength 3 -1 roll add
  724.    dict .copydict begin
  725.    counttomark 2 idiv { def } repeat pop    % pop the mark
  726.    currentdict end /Category defineresource pop
  727.  } bind def
  728.  
  729. /ColorRendering mark /InstanceType /dicttype .definecategory
  730. % ColorSpace is defined below
  731. % Encoding is defined below
  732. % Font is defined below
  733. /Form mark /InstanceType /dicttype .definecategory
  734. /Halftone mark /InstanceType /dicttype .definecategory
  735. /Pattern mark /InstanceType /dicttype .definecategory
  736. /ProcSet mark /InstanceType /dicttype .definecategory
  737. % Added since the Red Book:
  738. /ControlLanguage mark /InstanceType /dicttype .definecategory
  739. /HWOptions mark /InstanceType /dicttype .definecategory
  740. /Localization mark /InstanceType /dicttype .definecategory
  741. /OutputDevice mark /InstanceType /dicttype .definecategory
  742. /PDL mark /InstanceType /dicttype .definecategory
  743. % CIDFont, CIDMap, and CMap are defined in gs_cidfn.ps
  744. % FontSet is defined in gs_cff.ps
  745. % IdiomSet is defined in gs_ll3.ps
  746. % InkParams and TrapParams are defined in gs_trap.ps
  747.  
  748. (END MISC) VMDEBUG
  749.  
  750. % Define the ColorSpace category.
  751.  
  752. /.defaultcsnames mark
  753.   /DefaultGray 0
  754.   /DefaultRGB 1
  755.   /DefaultCMYK 2
  756. .dicttomark readonly def
  757.  
  758. % The "hooks" are no-ops here, redefined in LL3.
  759. /.definedefaultcs {    % <index> <value> .definedefaultcs -
  760.   pop pop
  761. } bind def
  762. /.undefinedefaultcs {    % <index> .undefinedefaultcs -
  763.   pop
  764. } bind def
  765.  
  766. /ColorSpace mark
  767.  
  768. /InstanceType /arraytype
  769.  
  770. % We keep track of whether there are any local definitions for any of
  771. % the Default keys.  This information must get saved and restored in
  772. % parallel with the local instance dictionary, so it must be stored in
  773. % local VM.
  774. userdict /.localcsdefaults false put
  775.  
  776. /DefineResource {
  777.   2 copy /Generic /Category findresource /DefineResource get exec
  778.   exch pop
  779.   exch //.defaultcsnames exch .knownget {
  780.     1 index .definedefaultcs
  781.     currentglobal not { .userdict /.localcsdefaults true put } if
  782.   } if
  783. } bind
  784.  
  785. /UndefineResource {
  786.   dup /Generic /Category findresource /UndefineResource get exec
  787.   //.defaultcsnames 1 index .knownget {
  788.     % Stack: resname index
  789.     currentglobal {
  790.       .undefinedefaultcs pop
  791.     } {
  792.     % We removed the local definition, but there might be a global one.
  793.       exch .GetInstance {
  794.     0 get .definedefaultcs
  795.       } {
  796.     .undefinedefaultcs
  797.       } ifelse
  798.     % Recompute .localcsdefaults by scanning.  This is rarely needed.
  799.       .userdict /.localcsdefaults false //.defaultcsnames {
  800.     pop .LocalInstances exch known { pop true exit } if
  801.       } forall put
  802.     } ifelse
  803.   } {
  804.     pop
  805.   } ifelse
  806. } bind
  807.  
  808. .definecategory            % ColorSpace
  809.  
  810. % Define the Encoding category.
  811.  
  812. /Encoding mark
  813.  
  814. /InstanceType /arraytype
  815.  
  816. % Handle already-registered encodings, including lazily loaded encodings
  817. % that aren't loaded yet.
  818.  
  819. /.Instances mark
  820.   EncodingDirectory
  821.    { dup length 256 eq { [ exch readonly 0 -1 ] } { pop [null 2 -1] } ifelse
  822.    } forall
  823. .dicttomark
  824.  
  825. /.ResourceFileDict mark
  826.   EncodingDirectory
  827.    { dup length 256 eq { pop pop } { 0 get } ifelse
  828.    } forall
  829. .dicttomark
  830.  
  831. /ResourceFileName
  832.  { .ResourceFileDict 2 index .knownget
  833.     { exch copy exch pop }
  834.     { /Generic /Category findresource /ResourceFileName get exec }
  835.    ifelse
  836.  } bind
  837.  
  838. .definecategory            % Encoding
  839.  
  840. % Make placeholders in level2dict for the redefined Encoding operators,
  841. % so that they will be swapped properly when we switch language levels.
  842.  
  843. /.findencoding /.findencoding load def
  844. /findencoding /findencoding load def
  845. /.defineencoding /.defineencoding load def
  846.  
  847. (END ENCODING) VMDEBUG
  848.  
  849. % Define the Font category.
  850.  
  851. /.fontstatus {        % <fontname> .fontstatus <fontname> <found>
  852.   {        % Create a loop context just so we can exit it early.
  853.         % Check Fontmap.
  854.     Fontmap 1 index .knownget {
  855.       {
  856.     dup type /nametype eq {
  857.       .fontstatus { pop null exit } if
  858.     } {
  859.       dup type /stringtype eq {
  860.         findlibfile { closefile pop null exit } if pop
  861.       } {
  862.         % Procedure, assume success.
  863.         pop null exit
  864.       } ifelse
  865.     } ifelse
  866.       } forall dup null eq { pop true exit } if
  867.     } if
  868.         % Convert names to strings; give up on other types.
  869.     dup type /nametype eq { .namestring } if
  870.     dup type /stringtype ne { false exit } if
  871.         % Check the resource directory.
  872.     dup .fonttempstring /FontResourceDir getsystemparam .genericrfn
  873.     status {
  874.       pop pop pop pop true exit
  875.     } if
  876.         % Check for a file on the search path with the same name
  877.         % as the font.
  878.     findlibfile { closefile true exit } if
  879.         % Scan a FONTPATH directory and try again.
  880.     .scannextfontdir not { false exit } if
  881.   } loop
  882. } bind def
  883.  
  884. /Font mark
  885.  
  886. /InstanceType /dicttype
  887.  
  888. /DefineResource
  889.     { 2 copy //definefont exch pop
  890.       /Generic /Category findresource /DefineResource get exec
  891.     } bind
  892. /UndefineResource
  893.     { dup //undefinefont
  894.       /Generic /Category findresource /UndefineResource get exec
  895.     } bind
  896. /FindResource {
  897.     dup .getvminstance {
  898.       exch pop 0 get
  899.     } {
  900.       dup ResourceStatus {
  901.         pop 1 gt { .loadfontresource } { .GetInstance pop 0 get } ifelse
  902.       } {
  903.         .loadfontresource
  904.       } ifelse
  905.     } ifelse
  906. } bind
  907. /ResourceForAll {
  908.     { .scannextfontdir not { exit } if } loop
  909.     /Generic /Category findresource /ResourceForAll get exec
  910. } bind
  911. /.ResourceFileStatus {
  912.     .fontstatus { pop 2 -1 true } { pop false } ifelse
  913. } bind
  914.  
  915. /.loadfontresource {
  916.     dup vmstatus pop exch pop exch
  917.         % Hack: rebind .currentresourcefile so that all calls of
  918.         % definefont will know these are built-in fonts.
  919.     currentfile {pop //findfont exec} .execasresource  % (findfont is a procedure)
  920.     exch vmstatus pop exch pop exch sub
  921.         % stack: name font vmused
  922.         % findfont has the prerogative of not calling definefont
  923.         % in certain obscure cases of font substitution.
  924.     2 index .getvminstance {
  925.       dup 1 1 put
  926.       2 3 -1 roll put
  927.     } {
  928.       pop
  929.     } ifelse exch pop
  930. } bind
  931.  
  932. /.Instances FontDirectory length 2 mul dict
  933.  
  934. .definecategory            % Font
  935.  
  936. % Redefine font "operators".
  937. /.definefontmap
  938.  { /Font /Category findresource /.Instances get
  939.    dup 3 index known
  940.     { pop
  941.     }
  942.     { 2 index
  943.         % Make sure we create the array in global VM.
  944.       .currentglobal true .setglobal
  945.       [null 2 -1] exch .setglobal
  946.       .growput
  947.     }
  948.    ifelse
  949.    //.definefontmap exec
  950.  } bind def
  951.  
  952. % Make sure the old definitions are still in systemdict so that
  953. % they will get bound properly.
  954. systemdict begin
  955.   /.origdefinefont /definefont load def
  956.   /.origundefinefont /undefinefont load def
  957.   /.origfindfont /findfont load def
  958. end
  959. /definefont {
  960.   /Font defineresource
  961. } bind odef
  962. /undefinefont {
  963.   /Font undefineresource
  964. } bind odef
  965. % The Red Book requires that findfont be a procedure, not an operator,
  966. % but it still needs to restore the stacks reliably if it fails.
  967. /.findfontop {
  968.   /Font findresource
  969. } bind odef
  970. /findfont {
  971.   .findfontop
  972. } bind def    % Must be a procedure, not an operator
  973.  
  974. % Remove initialization utilities.
  975. currentdict /.definecategory .undef
  976. currentdict /.emptydict .undef
  977.  
  978. end                % level2dict
  979.  
  980. % Convert deferred resources after we finally switch to Level 2.
  981.  
  982. /.fixresources {
  983.     % Encoding resources
  984.   EncodingDirectory
  985.    { dup length 256 eq
  986.       { /Encoding defineresource pop }
  987.       { pop pop }
  988.      ifelse
  989.    } forall
  990.   /.findencoding { /Encoding findresource } bind def
  991.   /findencoding /.findencoding load def        % must be a procedure
  992.   /.defineencoding { /Encoding defineresource pop } bind def
  993.     % ColorRendering resources and ProcSet
  994.   systemdict /ColorRendering .knownget {
  995.     /ColorRendering exch /ProcSet defineresource pop
  996.     systemdict /ColorRendering undef
  997.     /DefaultColorRendering currentcolorrendering /ColorRendering defineresource pop
  998.   } if
  999.     % ColorSpace resources
  1000.   systemdict /CIEsRGB .knownget {
  1001.     /sRGB exch /ColorSpace defineresource pop
  1002.     systemdict /CIEsRGB undef
  1003.   } if
  1004.     % ColorSpaceFamily resources
  1005.   colorspacedict { pop dup /ColorSpaceFamily defineresource pop } forall
  1006.     % Filter resources
  1007.   filterdict { pop dup /Filter defineresource pop } forall
  1008.     % FontType and FMapType resources
  1009.   buildfontdict { pop dup /FontType defineresource pop } forall
  1010.   mark
  1011.     buildfontdict 0 known { 2 3 4 5 6 7 8 } if
  1012.     buildfontdict 9 known { 9 } if
  1013.   counttomark { dup /FMapType defineresource pop } repeat pop
  1014.     % FormType resources
  1015.   .formtypes { pop dup /FormType defineresource pop } forall
  1016.     % HalftoneType resources
  1017.   .halftonetypes { pop dup /HalftoneType defineresource pop } forall
  1018.     % ColorRenderingType resources
  1019.   .colorrenderingtypes {pop dup /ColorRenderingType defineresource pop} forall
  1020.     % ImageType resources
  1021.   .imagetypes { pop dup /ImageType defineresource pop } forall
  1022.     % PatternType resources
  1023.   .patterntypes { pop dup /PatternType defineresource pop } forall
  1024.     % Make the fixed resource categories immutable.
  1025.   /.shadingtypes where {
  1026.     pop .shadingtypes { pop dup /ShadingType defineresource pop } forall
  1027.   } if
  1028.   [ /ColorSpaceFamily /Emulator /Filter /IODevice /ColorRenderingType
  1029.     /FMapType /FontType /FormType /HalftoneType /ImageType /PatternType
  1030.     /.shadingtypes where { pop /ShadingType } if
  1031.   ] {
  1032.     /Category findresource
  1033.     dup /.Instances get readonly pop
  1034.     .LocalInstances readonly pop
  1035.     readonly pop
  1036.   } forall
  1037.     % clean up
  1038.   systemdict /.fixresources undef
  1039. } bind def
  1040.  
  1041. %% Replace 1 (gs_resmp.ps)
  1042. (gs_resmp.ps)  dup runlibfile VMDEBUG
  1043. %% Replace 1 (gs_resst.ps)
  1044. (gs_resst.ps)  dup runlibfile VMDEBUG
  1045.