gs_resmp.ps 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. % Copyright (C) 2000 artofcode LLC. All rights reserved.
  2. %
  3. % This software is provided AS-IS with no warranty, either express or
  4. % implied.
  5. %
  6. % This software is distributed under license and may not be copied,
  7. % modified or distributed except as expressly authorized under the terms
  8. % of the license contained in the file LICENSE in this distribution.
  9. %
  10. % For more information about licensing, please refer to
  11. % http://www.ghostscript.com/licensing/. For information on
  12. % commercial licensing, go to http://www.artifex.com/licensing/ or
  13. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  14. % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  15. % $Id: gs_resmp.ps,v 1.11 2004/10/25 15:11:37 igor Exp $
  16. % A procset to redefine a resource category with a resource map.
  17. % Public entries :
  18. % Redefine - a procedure for redefining a resource category with a map.
  19. % Methods for interpreting the resource map to be provided by client
  20. % in the argument dictionary.
  21. %
  22. % Note that the procedure Redefine is idempotential :
  23. % consequtive calls to it will not replace the category methods,
  24. % but will merge resource maps. If an interleaving redefinition
  25. % needs to cancel the idempotentity, it must remove the entry
  26. % /.IsRedefinedWithMap from the category dictionary.
  27. % MakeResourceEnumerator - this procedure is useful for
  28. % redefining any category. It provides a proper order of instances
  29. % and proper stacks during resourceforall.
  30. % BindWithCurrentdict - a procedure for generating temporary procedures
  31. % from templates, binding them with a local dictionary.
  32. % execstack_lookup - a procedure for communicating through the execution stack.
  33. % It allows for a callee to get an information from an indirect caller.
  34. % The procedures are designed for exeution witout putting
  35. % the procset instance onto the dictionary stack.
  36. languagelevel 2 .setlanguagelevel
  37. currentglobal true setglobal
  38. /MappedCategoryRedefiner 10 dict begin % The procset.
  39. currentpacking false setpacking
  40. /InstanceEnumeratorPattern % - InstanceEnumeratorPattern ...
  41. {
  42. % This is a pattern for enumeration procedure to be built dynamically,
  43. % applying BindWithCurrentdict with a temporary dictionary.
  44. % The following names will be replaced with specific objects
  45. % during BindWithCurrentdict :
  46. % en_local_dict - a dictionary for storing the local integer variable 'status'.
  47. % scr - the scratch string argument of resourceforall;
  48. % proc - the procedure argument of resourceforall;
  49. % InstancesStatus - a dictionary that maps resource instance names to their status value;
  50. % Category - the category to be enumerated.
  51. % When this procedure is called from ResourceForAll, the category is the current dictionary.
  52. % We remove it from the dictionary stack before performing the enumeration
  53. % to provide the <proc> to write to the underlying dictionary,
  54. % and put it back after the enumeration is completed.
  55. end
  56. {
  57. 0 1 2 {
  58. en_local_dict exch /status exch put
  59. InstancesStatus {
  60. en_local_dict /status get eq {
  61. scr cvs % ... (Font)
  62. proc exec %
  63. } {
  64. pop
  65. } ifelse % ...
  66. } forall
  67. } for % ...
  68. } stopped
  69. Category begin
  70. { stop } if
  71. } bind def
  72. % An auxiliary proc for BindWithCurrentdict :
  73. /.BindAux % <proc> BindAux <proc>
  74. { 0 exec
  75. } bind def
  76. setpacking
  77. /BindWithCurrentdict % <proc> BindWithCurrentdict <proc>
  78. {
  79. % Make a copy of the given procedure, binding in the values of all names
  80. % defined in currentdict.
  81. % Caution1 : this code cannot handle procedures that were already
  82. % bound recursively.
  83. % Caution2 : this code don't bind packedarrays. This was done
  84. % intentionally for a termination of the procedure tree.
  85. dup length array copy
  86. dup length 1 sub -1 0 {
  87. 2 copy get % {precopy} i {elem}
  88. dup dup type /arraytype eq exch xcheck and {
  89. % {precopy} i {elem}
  90. //.BindAux exec % {precopy} i {elem_copy}
  91. 2 index 3 1 roll put % {precopy}
  92. } {
  93. dup dup type /nametype eq exch xcheck and {
  94. % {precopy} i {elem}
  95. currentdict exch .knownget {
  96. 2 index 3 1 roll put % {precopy}
  97. } {
  98. pop
  99. } ifelse
  100. } {
  101. pop pop
  102. } ifelse
  103. } ifelse % {precopy}
  104. } for % {copy}
  105. cvx
  106. } bind def
  107. //.BindAux 0 //BindWithCurrentdict put % bind the recursive call in 'Bind'.
  108. /MakeResourceEnumerator % <proc> <scr> <InstancesStatus> MakeResourceEnumerator <Enumerator>
  109. {
  110. % Build the enumeration procedure :
  111. % Since the resourceforall procedure may leave values on the operand stack,
  112. % we cannot simply store the enumerator's local data on the stack.
  113. % We also cannot use a static dictionary to store local variables,
  114. % because of possible recursion in the resourceforall procedure.
  115. % To work around this, we create a copy of the enumeration procedure and
  116. % bind it dynamically with a temporary dictionary, which contains
  117. % local variables for the currently executing instance of resourceforall.
  118. currentdict % Category
  119. 6 dict begin % the temporary dictionary
  120. /Category exch def %
  121. /InstancesStatus exch def
  122. /scr exch def
  123. /proc exch def
  124. /en_local_dict currentdict def
  125. //InstanceEnumeratorPattern //BindWithCurrentdict exec % Enumerator
  126. /status 0 def % variable for the current status to enumerate - do not bind with it !
  127. end
  128. } bind def
  129. /execstack_lookup % <object> execstack_lookup <object1>
  130. % <object> execstack_lookup null
  131. { % Checks whether execution stack contains a procedure starting with <object>,
  132. % and retrives the 2nd element of the procedure,
  133. % or null if the procedure was not found.
  134. %
  135. % Since 'execstack' actually renders subarrays of procedures,
  136. % the pattern for recognition must be like this :
  137. %
  138. % { <object> <object1>
  139. % CallSomething
  140. % } loop
  141. %
  142. % The solution with 'loop' depends on how GS implements cycles,
  143. % so it must not appear in documents, which are required to be interpreter independent.
  144. % Any other type of cycles are also acceptable.
  145. % If no repitition is really needed, just insert 'exit' into its body.
  146. % If <object> <object1> are not needed for the caller, insert "pop pop" after them.
  147. % If <object1> is really unuseful, the pattern may be simplified :
  148. %
  149. % { <object> pop
  150. % CallSomething
  151. % exit
  152. % } loop
  153. %
  154. % It will retrieve 'pop' or 'null'.
  155. %
  156. % Note that 2 topmost execstack elements are the execstack_lookup procedure and its caller.
  157. % We don't check them.
  158. currentglobal false setglobal % <object> bGlobal
  159. countexecstack array execstack % <object> bGlobal [execstack]
  160. dup null exch % <object> bGlobal [execstack] null [execstack]
  161. length 3 sub -1 0 { % <object> bGlobal [execstack] null i
  162. 2 index exch get % <object> bGlobal [execstack] null proc
  163. dup type dup /packedarraytype eq exch /arraytype eq or {
  164. dup length 1 gt { % <object> bGlobal [execstack] null proc
  165. dup 0 get % <object> bGlobal [execstack] null proc elem0
  166. 5 index eq { % <object> bGlobal [execstack] null proc
  167. 1 get % <object> bGlobal [execstack] null object1
  168. exch pop exit % <object> bGlobal [execstack] object1
  169. } {
  170. pop
  171. } ifelse
  172. } {
  173. pop % <object> bGlobal [execstack] false
  174. } ifelse
  175. } {
  176. pop % <object> bGlobal [execstack] false
  177. } ifelse
  178. } for % <object> bGlobal [execstack] bResult
  179. exch pop exch setglobal exch pop % bResult
  180. } bind def
  181. currentpacking false setpacking
  182. /MethodsToRedefine 5 dict begin
  183. % Procedures in this dictionary really are patterns for new category methods.
  184. % The following names will be replaced with specific objects during BindWithCurrentdict :
  185. % .map - the map dictionary;
  186. % DefineResource, ResourceStatus, ResourceFileName, FindResource, ResourceForAll
  187. % - procedures from the original resource category.
  188. /FindResource % <Name> FindResource <dict>
  189. { RESMPDEBUG { (resmp FindResource beg ) print dup = } if
  190. dup ResourceStatus exec {
  191. pop 2 lt
  192. } {
  193. false
  194. } ifelse % bInVirtualMemory
  195. { FindResource exec
  196. } {
  197. dup dup .map exch .knownget { % /Name /Name <<record>>
  198. dup dup /RecordVirtualMethods get /IsActive get exec {
  199. 1 index .getvminstance { % /Name /Name <<record>> holder
  200. 1 get 1 eq
  201. } {
  202. true
  203. } ifelse % /Name /Name <<record>> bStatusIs1
  204. 4 1 roll % bStatusIs1 /Name /Name <<record>>
  205. dup /RecordVirtualMethods get /MakeInstance get exec
  206. % bStatusIs1 /Name /Name Instance size
  207. 5 1 roll % size bStatusIs1 /Name /Name Instance
  208. DefineResource exec % size bStatusIs1 /Name Instance
  209. % Make ResourceStatus to return correct values for this instance :
  210. % Hack: we replace status values in the instance holder :
  211. exch .getvminstance pop % size bStatusIs1 Instance holder
  212. dup 5 -1 roll 2 exch put % bStatusIs1 Instance holder
  213. 3 2 roll { % Instance holder
  214. 1 1 put % Instance
  215. } {
  216. pop
  217. } ifelse % Instance
  218. } { % /Name /Name <<record>>
  219. pop pop FindResource exec
  220. } ifelse
  221. } { % /Name /Name
  222. pop FindResource exec
  223. } ifelse
  224. } ifelse
  225. RESMPDEBUG { (resmp FindResource end) = } if
  226. } bind def
  227. /ResourceStatus % <Name> ResourceStatus <status> <size> true
  228. % <Name> ResourceStatus false
  229. { RESMPDEBUG { (resmp ResourceStatus beg ) print dup == } if
  230. dup ResourceStatus exec { % /Name status size
  231. 1 index 2 lt {
  232. % In VM - return with it.
  233. 3 2 roll pop true
  234. } {
  235. % Not in VM.
  236. exch pop exch % size /Name
  237. dup .map exch .knownget { % size /Name <<record>>
  238. dup dup /RecordVirtualMethods get /IsActive get exec {
  239. 3 2 roll pop % /Name <<record>>
  240. dup /RecordVirtualMethods get /GetSize get exec 2 exch true
  241. } { % size /Name <<record>>
  242. pop pop 2 exch true
  243. } ifelse
  244. } { % size /Name
  245. pop 2 exch true
  246. } ifelse
  247. } ifelse
  248. } { % /Name
  249. dup .map exch .knownget { % /Name <<record>>
  250. dup dup /RecordVirtualMethods get /IsActive get exec {
  251. dup /RecordVirtualMethods get /GetSize get exec 2 exch true
  252. } { % /Name <<record>>
  253. pop pop false
  254. } ifelse
  255. } { % /Name
  256. pop false
  257. } ifelse
  258. } ifelse
  259. RESMPDEBUG { (resmp ResourceStatus end) = } if
  260. } bind def
  261. /ResourceFileName % <Name> <scratch> ResourceFileName <string>
  262. { RESMPDEBUG { (resmp ResourceFileName beg ) print 1 index = } if
  263. exch % (scratch) /Name
  264. .map 1 index .knownget { % (scratch) /Name <<record>>
  265. RESMPDEBUG { (resmp ResourceFileName : have a map record.) = } if
  266. dup dup /RecordVirtualMethods get /IsActive get exec {
  267. RESMPDEBUG { (resmp ResourceFileName : record is active.) = } if
  268. dup /RecordVirtualMethods get /GetFilePath get exec % (string)
  269. RESMPDEBUG { (resmp ResourceFileName : retrieving ) print dup = } if
  270. } { % (scratch) /Name <<record>>
  271. RESMPDEBUG { (resmp ResourceFileName : record is NOT active.) = } if
  272. pop exch ResourceFileName exec
  273. RESMPDEBUG { (resmp ResourceFileName : retrieving ) print dup = } if
  274. } ifelse
  275. } {
  276. RESMPDEBUG { (resmp ResourceFileName : have NO map record.) = } if
  277. exch ResourceFileName exec
  278. RESMPDEBUG { (resmp ResourceFileName : retrieving ) print dup = } if
  279. } ifelse
  280. RESMPDEBUG { (resmp ResourceFileName end) = } if
  281. } bind def
  282. /ResourceForAll % <template> <proc> <scratch> ResourceForAll -
  283. { RESMPDEBUG { (resmp ResourceForAll beg ) print 2 index = } if
  284. % Create InstancesStatus dictionary :
  285. 20 dict % IS - Instances Status
  286. 4 1 roll % <<IS>> (templ) {proc} (sctarch)
  287. % <<IS>> bOrder (templ) {proc} (sctarch)
  288. % Check if we are under another ResourceForAll :
  289. /.DisableResourceOrdering //execstack_lookup exec null eq 4 1 roll
  290. % Put underlying resources to the InstancesStatus dictionary :
  291. currentdict % the category
  292. begin % ResourceForAll removes it locally.
  293. 2 index
  294. { cvn % <<IS>> bOrder (templ) {proc} (sctarch) /Name
  295. 4 index {
  296. dup ResourceStatus exec {pop 6 index 3 1 roll put} {pop} ifelse
  297. } {
  298. 5 index exch 2 put % Don't need the ordering, put '2' as a scratch.
  299. } ifelse
  300. }
  301. 2 index ResourceForAll exec % <<IS>> bOrder (templ) {proc} (sctarch)
  302. 4 3 roll pop % <<IS>> (templ) {proc} (sctarch)
  303. end
  304. % Put .map entries to the InstancesStatus dictionary :
  305. 4 -1 roll begin % (templ) {proc} (sctarch)
  306. .map { % (templ) {proc} (sctarch) /Name record
  307. dup dup /RecordVirtualMethods get /IsActive get exec {
  308. pop % (templ) {proc} (sctarch) /Name
  309. dup currentdict exch known {
  310. pop
  311. } {
  312. dup 2 index cvs % (templ) {proc} (sctarch) /Name (Name)
  313. 4 index .stringmatch { % (templ) {proc} (sctarch) /Name
  314. 2 def % It is not in VM.
  315. } {
  316. pop
  317. } ifelse
  318. } ifelse
  319. } { % (templ) {proc} (sctarch) /Name record
  320. pop pop
  321. } ifelse
  322. } forall % (templ) {proc} (sctarch)
  323. % prepare stacks for the enumeration :
  324. 3 2 roll pop % {proc} (sctarch)
  325. currentdict end % {proc} (scratch) <<IS>>
  326. % Make the enumerator and apply it :
  327. //MakeResourceEnumerator exec exec
  328. RESMPDEBUG { (resmp ResourceForAll end)= } if
  329. } bind def
  330. /GetCIDSystemInfoFromMap % <Name> GetCIDSystemInfoFromMap <Name>
  331. % <Name> GetCIDSystemInfoFromMap <dict>
  332. { RESMPDEBUG { (resmp GetCIDSystemInfoFromMap beg ) print dup = } if
  333. % This is a special function for communicating with GetCIDSystemInfo in gs_cidcm.ps .
  334. dup .map exch .knownget {
  335. RESMPDEBUG { (resmp GetCIDSystemInfoFromMap : have a map record.) = } if
  336. dup /RecordVirtualMethods get /GetCSI get exec
  337. dup null ne {
  338. RESMPDEBUG { (resmp GetCIDSystemInfoFromMap : retrieving a dict.) = } if
  339. exch
  340. } if
  341. pop
  342. } if
  343. RESMPDEBUG { (resmp GetCIDSystemInfoFromMap end) = } if
  344. } bind def
  345. currentdict end def
  346. setpacking
  347. /Redefine % <OptionsDict> Redefine -
  348. { % Before calling this proc, the OptionsDict must specify options for
  349. % the catregory to be redefined :
  350. % CategoryName - a name of category to redefine;
  351. % MapFileName - a string for the resource map file name;
  352. % VerifyMap - a procedure :
  353. % <raw_map> VerifyMap -
  354. % - checks the map for consistency
  355. % PreprocessRecord - a procedure :
  356. % <map> <Name> <raw_record> PreprocessRecord <map> <Name> <record> true
  357. % <map> <Name> <raw_record> PreprocessRecord <map> <Name> <raw_record> false
  358. % - converts a map record into a dictionary;
  359. % It must add RecordVirtualMethods dictionary to the record :
  360. % MakeInstance - a procedure :
  361. % <Name> <record> MakeInstance <Name> <Instance> <size>
  362. % - converts the record to resource instance;
  363. % GetFilePath - a procedure for ResourceFileName :
  364. % <scratch> <Name> <record> GetFilePath <filepath>
  365. % GetSize - a procedure for ResourceStatus :
  366. % <Name> <record> GetSize <size>
  367. % GetCSI - a procedure for obtaining CIDSystemInfo dictionary from the record :
  368. % <record> GetCSI <CSI>
  369. % <record> GetCSI null
  370. % IsActive - a procedure for skipping records depending on the current device :
  371. % <record> IsActive <bool>
  372. % Also it is allowed to contain additional entries for client's needs.
  373. % The OptionsDict is also used for storing some local variables.
  374. % If a category is being redefined several times with this function,
  375. % each redefinition must either use an unique map file,
  376. % or the map file should be scanned by the last redefinition
  377. % (and must be defined in the last one with /MapFileName).
  378. % This happens so because we must accumulate all variants of
  379. % methods before scanning the map. We would like to delay
  380. % the scanning until all redefinitions are done, but it requires
  381. % to implement a queue of "refinish" methods and execute it
  382. % at very end of the prelude.
  383. begin % OptionsDict
  384. CategoryName /Category findresource /OldCategory exch def
  385. OldCategory /.IsRedefinedWithMap known {
  386. % Already redefined with map - don't redefine, but enhance the map.
  387. OldCategory /NewCategory exch def
  388. } {
  389. % Redefine with a new category instance.
  390. OldCategory dup length dict
  391. dup /.PreprocessRecord 4 dict put
  392. copy /NewCategory exch def
  393. } ifelse
  394. % Provide the 'or' logic for PreprocessRecord,
  395. % to allow different record types to be mixed in a single map file.
  396. % We do this with building a dictionary of PreprocessRecord procedures,
  397. % which come from different calls to Redefine :
  398. NewCategory /.PreprocessRecord get dup length % <<pr>> l
  399. currentdict /PreprocessRecord get .growput
  400. currentdict /MapFileName known {
  401. MapFileName .libfile {
  402. 1 dict begin
  403. /; {} def
  404. mark exch cvx exec .dicttomark % <<map>>
  405. end
  406. dup VerifyMap % <<map>>
  407. } {
  408. QUIET not {
  409. currentdict /IsMapFileOptional .knownget not { false } if not {
  410. (Warning: the map file ) print dup =string cvs print ( was not found.) =
  411. } if
  412. } if
  413. pop 0 dict % <<map>>
  414. } ifelse
  415. } {
  416. currentdict /.map .knownget not {
  417. 0 dict % <<map>>
  418. } if
  419. } ifelse
  420. % Preprocess entries :
  421. dup NewCategory /.PreprocessRecord get % <<map>> <<map>> <<pr>>
  422. 3 1 roll { % <<pr>> <<map>> /Name raw_record
  423. false 3 1 roll % <<pr>> <<map>> false /Name raw_record
  424. 4 index { % <<pr>> <<map>> false /Name raw_record i {pr}
  425. exch pop % <<pr>> <<map>> false /Name raw_record {pr}
  426. exec { % <<pr>> <<map>> false /Name record
  427. 3 -1 roll pop true 3 1 roll % <<pr>> <<map>> true /Name record
  428. exit
  429. } if % <<pr>> <<map>> false /Name raw_record
  430. } forall
  431. 3 2 roll { % <<pr>> <<map>> /Name record
  432. 2 index 3 1 roll put % <<pr>> <<map>>
  433. } {
  434. exch % <<pr>> <<map>> raw_record /Name
  435. (Incorrect record ) print =string cvs print ( of the map file ) print MapFileName =string cvs print (.) =
  436. end % Pops OptionsDict from dstack.
  437. pop pop pop %
  438. /Redefine cvx /undefinedresource signalerror
  439. } ifelse
  440. } forall % <<pr>> <<map>>
  441. exch pop % <<map>>
  442. % Add the map :
  443. OldCategory /.IsRedefinedWithMap known { % <<map>>
  444. % Just add to the old map :
  445. OldCategory /.map get copy pop %
  446. } { % <<map>>
  447. % Store the map to both the category and OptionsDict :
  448. dup NewCategory exch /.map exch put
  449. /.map exch def %
  450. } ifelse
  451. OldCategory /.IsRedefinedWithMap known not {
  452. % Copy old methods to OptionsDict :
  453. [ /DefineResource /ResourceStatus /ResourceFileName
  454. /FindResource /ResourceForAll
  455. ] {
  456. dup OldCategory exch get def
  457. } forall
  458. % Build new methods :
  459. //MethodsToRedefine {
  460. //BindWithCurrentdict exec NewCategory 3 1 roll put
  461. } forall
  462. CategoryName /CIDFont ne {
  463. NewCategory /GetCIDSystemInfoFromMap undef
  464. % This is some ugly, sorry.
  465. } if
  466. % Redefine the category :
  467. NewCategory /.IsRedefinedWithMap true put
  468. CategoryName NewCategory /Category defineresource pop
  469. } if
  470. end % OptionsDict
  471. } bind executeonly def
  472. currentdict /PutPreprocessRecord .undef
  473. currentdict end
  474. /ProcSet defineresource pop
  475. setglobal .setlanguagelevel