home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gs333ini / gs3.33 / gs_res.ps < prev    next >
Text File  |  1995-12-09  |  14KB  |  526 lines

  1. %    Copyright (C) 1994 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % Initialization file for Level 2 resource machinery.
  16. % When this is run, systemdict is still writable,
  17. % but everything defined here goes into level2dict.
  18.  
  19. level2dict begin
  20.  
  21. (BEGIN RESOURCES) VMDEBUG
  22.  
  23. % We keep track of (global) instances with another entry in the resource
  24. % dictionary, an Instances dictionary.  For categories with implicit
  25. % instances, the values in Instances are the same as the keys;
  26. % for other categories, the values are [instance status size].
  27.  
  28. % Note that the dictionary that defines a resource category is stored
  29. % in global memory.  The PostScript manual says that each category must
  30. % manage global and local instances separately.  However, objects in
  31. % global memory other than systemdict can't reference objects in local memory.
  32. % This means that the resource category dictionary, which would otherwise be
  33. % the obvious place to keep track of the instances, can't be used to keep
  34. % track of local instances.  Instead, we define a dictionary in local VM
  35. % called localinstancedict, in which the key is the category name and
  36. % the value is the analogue of Instances for local instances.
  37.  
  38. % We don't currently implement automatic resource unloading.
  39. % When we do, it should be hooked to the garbage collector.
  40.  
  41. currentglobal false setglobal systemdict begin
  42.   /localinstancedict 5 dict def
  43. end true setglobal
  44. /.emptydict 0 dict readonly def
  45. setglobal
  46.  
  47. % Resource category dictionaries have the following keys (those marked with
  48. % * are optional):
  49. %    Standard, defined in the Red Book:
  50. %        Category (name)
  51. %        *InstanceType (name)
  52. %        DefineResource
  53. %        UndefineResource
  54. %        FindResource
  55. %        ResourceStatus
  56. %        ResourceForAll
  57. %        *ResourceFileName
  58. %    Additional, specific to our implementation:
  59. %        Instances (dictionary)
  60. %        .LocalInstances
  61. %            - .LocalInstances <dict>
  62. %        .GetInstance
  63. %            <key> .GetInstance <instance> -true-
  64. %            <key> .GetInstance -false-
  65. %        .CheckResource
  66. %            <value> .CheckResource <ok>
  67. %        .DoLoadResource
  68. %            <key> .DoLoadResource - (may give an error)
  69. %        .LoadResource
  70. %            <key> .LoadResource - (may give an error)
  71. %        .ResourceFile
  72. %            <key> .ResourceFile <file> -true-
  73. %            <key> .ResourceFile <key> -false-
  74. % All the above procedures expect that the top dictionary on the d-stack
  75. % is the resource dictionary.
  76.  
  77. % Define enough of the Category category so we can define other categories.
  78. % The dictionary we're about to create will become the Category
  79. % category definition dictionary.
  80.  
  81. 12 dict begin
  82.  
  83.         % Standard entries
  84.  
  85. /Category /Category def
  86. /InstanceType /dicttype def
  87.  
  88. /DefineResource
  89.     { dup .CheckResource
  90.        { dup /Category 3 index cvlit .growput readonly
  91.          dup [ exch 0 -1 ] exch
  92.          Instances 4 2 roll put
  93.        }
  94.        { /typecheck signalerror
  95.        }
  96.       ifelse
  97.     } bind def
  98. /FindResource        % (redefined below)
  99.     { Instances exch get 0 get
  100.     } bind def
  101.  
  102.         % Additional entries
  103.  
  104. /Instances 25 dict def
  105. Instances /Category [currentdict 0 -1] put
  106.  
  107. /.LocalInstances 0 dict def
  108. /.GetInstance
  109.     { Instances exch .knownget
  110.     } bind def
  111. /.CheckResource
  112.     { dup gcheck currentglobal and
  113.        { /DefineResource /FindResource /ResourceForAll /ResourceStatus
  114.          /UndefineResource }
  115.        { 2 index exch known and }
  116.       forall exch pop
  117.     } bind def
  118.  
  119. Instances end begin    % for the base case of findresource
  120.  
  121. (END CATEGORY) VMDEBUG
  122.  
  123. % Define the resource operators.  I don't see how we can possibly restore
  124. % the stacks after an error, since the procedure may have popped and
  125. % pushed elements arbitrarily....
  126.  
  127. mark
  128. /defineresource
  129.     { /Category findresource dup begin
  130.       /InstanceType known
  131.        { dup type InstanceType ne
  132.           { dup type /packedarraytype eq InstanceType /arraytype eq and
  133.         not { /typecheck signalerror } if
  134.           } if
  135.        } if
  136.       /DefineResource load stopped end { stop } if
  137.     }
  138. /findresource
  139.     { dup /Category eq
  140.        { pop //Category 0 get } { /Category findresource } ifelse
  141.       begin
  142.       /FindResource load stopped end { stop } if
  143.     }
  144. /resourceforall
  145.     { /Category findresource begin
  146.       /ResourceForAll load stopped end { stop } if
  147.     }
  148. /resourcestatus
  149.     { /Category findresource begin
  150.       /ResourceStatus load stopped end { stop } if
  151.     }
  152. /undefineresource
  153.     { /Category findresource begin
  154.       /UndefineResource load stopped end { stop } if
  155.     }
  156. end        % Instances of Category
  157. counttomark 2 idiv { bind odef } repeat pop
  158.  
  159. % Define the Generic category.
  160.  
  161. /Generic mark
  162.  
  163.         % Standard entries
  164.  
  165. % We're still running in Level 1 mode, so dictionaries won't expand.
  166. % Leave room for the /Category entry.
  167. /Category null
  168.  
  169. /DefineResource
  170.     { dup .CheckResource
  171.        { dup [ exch 0 -1 ] exch
  172.          currentglobal
  173.           { false setglobal 2 index UndefineResource    % remove local def if any
  174.         true setglobal
  175.         Instances dup //.emptydict eq
  176.          { pop 3 dict /Instances 1 index def
  177.          }
  178.         if
  179.           }
  180.           { .LocalInstances dup //.emptydict eq
  181.              { pop 3 dict localinstancedict Category 2 index put
  182.          }
  183.         if
  184.           }
  185.          ifelse
  186.          4 2 roll .growput
  187.        }
  188.        { /typecheck signalerror
  189.        }
  190.       ifelse
  191.     } bind
  192. /UndefineResource
  193.     {  { dup 2 index .knownget
  194.           { dup 1 get 1 ge
  195.          { dup 0 null put 1 2 put pop pop }
  196.          { pop exch undef }
  197.         ifelse
  198.           }
  199.           { pop pop
  200.           }
  201.          ifelse
  202.        }
  203.       currentglobal
  204.        { 2 copy Instances exch exec
  205.        }
  206.       if .LocalInstances exch exec
  207.     } bind
  208. /FindResource
  209.     { dup ResourceStatus
  210.        { pop 1 gt            % not in VM
  211.           { .DoLoadResource
  212.           }
  213.          if
  214.          .GetInstance pop        % can't fail
  215.          0 get
  216.        }
  217.        { /undefinedresource signalerror
  218.        }
  219.       ifelse
  220.     } bind
  221. /ResourceStatus
  222.     { dup .GetInstance
  223.        { exch pop dup 1 get exch 2 get true }
  224.        { .ResourceFile
  225.           { closefile 2 -1 true }
  226.           { pop false }
  227.          ifelse
  228.        }
  229.       ifelse
  230.     } bind
  231. /ResourceForAll
  232.     { % **************** Doesn't present instance groups in
  233.       % **************** the correct order yet.
  234.       % We construct a new procedure so we don't have to use
  235.       % static resources to hold the iteration state.
  236.       3 packedarray        % template, proc, scratch
  237.       { exch pop    % stack contains: key, {template, proc, scratch}
  238.         2 copy 0 get .stringmatch
  239.          { 1 index type dup /stringtype eq exch /nametype eq or
  240.         { 2 copy 2 get cvs
  241.           exch 1 get 3 -1 roll pop
  242.         }
  243.         { 1 get
  244.         }
  245.            ifelse exec
  246.          }
  247.          { pop pop
  248.          }
  249.         ifelse
  250.       } /exec cvx 3 packedarray cvx
  251.       % We must pop the resource dictionary off the dict stack
  252.       % when doing the actual iteration, and restore it afterwards.
  253.       currentglobal .LocalInstances length 0 eq or not
  254.        {        % We must do local instances, and do them first.
  255.          /forall cvx 1 index currentdict 3 packedarray cvx
  256.          .LocalInstances 3 1 roll end exec begin
  257.        }
  258.       if
  259.       Instances exch
  260.       /forall cvx currentdict 2 packedarray cvx
  261.       end exec begin
  262.     } bind
  263.  
  264.         % Additional entries
  265.  
  266. % Unfortunately, we can't create the real Instances dictionary now,
  267. % because if someone copies the Generic category (which pp. 95-96 of the
  268. % 2nd Edition Red Book says is legitimate), they'll wind up sharing
  269. % the Instances.  Instead, we have to create Instances on demand,
  270. % just like the entry in localinstancedict.
  271. % We also have to prevent anyone from creating instances of Generic itself.
  272. /Instances //.emptydict
  273.  
  274. /.LocalInstances
  275.     { localinstancedict Category .knownget not { //.emptydict } if
  276.     } bind
  277. /.GetInstance
  278.     { currentglobal
  279.        { Instances exch .knownget }
  280.        { .LocalInstances 1 index .knownget
  281.           { exch pop true }
  282.           { Instances exch .knownget }
  283.          ifelse
  284.        }
  285.       ifelse
  286.     } bind
  287. /.CheckResource
  288.     { pop true
  289.     } bind
  290. /.DoLoadResource
  291.     { dup vmstatus pop exch pop exch
  292.       .LoadResource
  293.       vmstatus pop exch pop exch sub
  294.       1 index .GetInstance not
  295.        { pop /undefinedresource signalerror }    % didn't load
  296.       if
  297.       dup 1 1 put
  298.       2 3 -1 roll put
  299.     } bind
  300. /.LoadResource
  301.     { dup .ResourceFile
  302.        { exch pop currentglobal
  303.           { run }
  304.           { true setglobal { run } stopped false setglobal { stop } if }
  305.          ifelse
  306.        }
  307.        { /undefinedresource signalerror
  308.        }
  309.      ifelse
  310.     } bind
  311. /.ResourceFile
  312.     { currentdict /ResourceFileName known
  313.        { mark 1 index 100 string { ResourceFileName }
  314.          stopped
  315.           { cleartomark false }
  316.           { exch pop findlibfile
  317.          { exch pop exch pop true }
  318.          { false }
  319.         ifelse
  320.           }
  321.          ifelse
  322.        }
  323.        { false }
  324.       ifelse
  325.     } bind
  326.  
  327. .dicttomark
  328. /Category defineresource pop
  329.  
  330. % Fill in the rest of the Category category.
  331. /Category /Category findresource dup
  332. /Generic /Category findresource begin
  333.  { /FindResource /ResourceForAll /ResourceStatus /UndefineResource /.ResourceFile }
  334.  { dup load put dup } forall
  335. pop readonly pop end
  336.  
  337. (END GENERIC) VMDEBUG
  338.  
  339. % Define the fixed categories.
  340.  
  341. mark
  342.     % Things other than types
  343.  /ColorSpaceFamily
  344.    mark colorspacedict { pop } forall .packtomark
  345.  /Emulator
  346.    mark EMULATORS { cvn } forall .packtomark
  347.  /Filter
  348.    mark filterdict { pop } forall .packtomark
  349.  /IODevice
  350.     % Loop until the .getiodevice gets a rangecheck.
  351.    errordict /rangecheck 2 copy get
  352.    errordict /rangecheck { pop stop } put    % pop the command
  353.    mark 0 { {dup .getiodevice exch 1 add} loop} stopped pop pop pop .packtomark
  354.    4 1 roll put
  355.    .clearerror
  356.     % Types
  357.  /setcolorrendering where
  358.   { pop /ColorRenderingType
  359.      {1}
  360.   } if
  361.  /.buildfont0 where
  362.   { pop /FMapType
  363.      {2 3 4 5 6 7 8}
  364.   } if
  365.  /FontType
  366.    [/.buildfont0 where {pop 0} if
  367.     /.buildfont1 where {pop 1} if
  368.     3]
  369.  /FormType
  370.    {1}
  371.  /HalftoneType
  372.    {1 2 3 4 5}
  373.  /ImageType
  374.    {1}
  375.  /PatternType
  376.    {1}            % should check for Pattern color space
  377. counttomark 2 idiv
  378.  { mark
  379.  
  380.         % Standard entries
  381.  
  382.    /DefineResource
  383.     { /invalidaccess signalerror } bind
  384.    /UndefineResource
  385.     { /invalidaccess signalerror } bind
  386.    /FindResource
  387.     { Instances exch get } bind
  388.    /ResourceStatus
  389.     { Instances exch known { 0 0 true } { false } ifelse } bind
  390.    /ResourceForAll
  391.     /Generic /Category findresource /ResourceForAll get
  392.  
  393.         % Additional entries
  394.  
  395.    counttomark 2 add -1 roll
  396.    dup length dict dup begin exch { dup def } forall end readonly
  397.    /Instances exch
  398.    /.LocalInstances    % used by ResourceForAll
  399.     0 dict def
  400.  
  401.    .dicttomark /Category defineresource pop
  402.  } repeat pop
  403.  
  404. (END FIXED) VMDEBUG
  405.  
  406. % Define the other built-in categories.
  407.  
  408. /.definecategory    % <name> -mark- <key1> ... <valuen> .definecategory -
  409.  { counttomark 2 idiv 2 add        % Instances, Category
  410.    /Generic /Category findresource dup maxlength 3 -1 roll add dict copy begin
  411.    counttomark 2 idiv { def } repeat pop    % pop the mark
  412.    currentdict end /Category defineresource pop
  413.  } bind def
  414.  
  415. /ColorRendering mark /InstanceType /dicttype .definecategory
  416. /ColorSpace mark /InstanceType /arraytype .definecategory
  417. /Form mark /InstanceType /dicttype .definecategory
  418. /Halftone mark /InstanceType /dicttype .definecategory
  419. /Pattern mark /InstanceType /dicttype .definecategory
  420. /ProcSet mark /InstanceType /dicttype .definecategory
  421.  
  422. (END MISC) VMDEBUG
  423.  
  424. % Define the Encoding category.
  425.  
  426. /Encoding mark
  427.  
  428. /InstanceType /arraytype
  429.  
  430. % Handle lazily loaded encodings that aren't loaded yet.
  431.  
  432. /Instances mark
  433.   .encodingdict
  434.    { length 256 eq { pop } { [null 2 -1] } ifelse
  435.    } forall
  436. .dicttomark
  437.  
  438. /.ResourceFileDict mark
  439.   .encodingdict
  440.    { dup length 256 eq { pop pop } { 0 get } ifelse
  441.    } forall
  442. .dicttomark
  443.  
  444. /ResourceFileName
  445.  { exch dup .ResourceFileDict exch .knownget
  446.     { exch pop exch copy }
  447.     { exch pop /undefinedresource signalerror }
  448.    ifelse
  449.  } bind
  450.  
  451. .definecategory            % Encoding
  452.  
  453. /.findencoding { /Encoding findresource } bind def
  454. /findencoding /.findencoding load odef
  455. /.defineencoding
  456.  { 2 copy /Encoding defineresource pop
  457.    //.encodingdict 3 1 roll put
  458.  } bind def
  459.  
  460. (END ENCODING) VMDEBUG
  461.  
  462. % Define the Font category.
  463.  
  464. /Font mark
  465.  
  466. /InstanceType /dicttype
  467.  
  468. /DefineResource
  469.     { 2 copy //definefont exch pop
  470.       /Generic /Category findresource /DefineResource get exec
  471.     } bind
  472. /UndefineResource
  473.     { dup //undefinefont
  474.       /Generic /Category findresource /UndefineResource get exec
  475.     } bind
  476. /FindResource
  477.     { dup ResourceStatus
  478.        { pop 1 gt { .DoLoadResource } if }
  479.        { .DoLoadResource }
  480.       ifelse
  481.       .GetInstance pop 0 get
  482.     } bind
  483.  
  484. /.LoadResource
  485.     { //findfont exec pop
  486.     } bind
  487.  
  488. /Instances FontDirectory length 2 mul dict
  489.  
  490. .definecategory            % Font
  491.  
  492. % Make entries for fonts already loaded.
  493. /Font /Category findresource begin
  494. FontDirectory
  495.  { dup .gcheck { Instances } { .LocalInstances } ifelse
  496.    3 1 roll [exch 0 -1] .growput
  497.  }
  498. forall end
  499.  
  500. % Redefine font "operators".
  501. /.definefontmap
  502.  { /Font /Category findresource /Instances get
  503.    dup 3 index known
  504.     { pop }
  505.     { 2 index [null 2 -1] .growput }
  506.    ifelse
  507.    //.definefontmap exec
  508.  } bind def
  509. Fontmap { .definefontmap } forall
  510.  
  511. /definefont
  512.     { /Font defineresource } bind odef
  513. /undefinefont
  514.     { /Font undefineresource } bind odef
  515. /findfont
  516.     { /Font findresource } bind def    % Must be a procedure, not an operator
  517.  
  518. % Remove initialization utilities.
  519. currentdict /.definecategory undef
  520. currentdict /.emptydict undef
  521.  
  522. end                % level2dict
  523.