星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gs_resmp.ps 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. % Copyright (C) 2000 Artifex Software, Inc. 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 8954 2008-08-08 04:22:38Z ray $
  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. % Always place the enumerator in local VM,
  119. % because its elements may be in local VM.
  120. currentglobal 4 1 roll
  121. false setglobal
  122. currentdict % Category
  123. 6 dict begin % the temporary dictionary
  124. /Category exch def
  125. /InstancesStatus exch def
  126. /scr exch def
  127. /proc exch def
  128. /en_local_dict currentdict def
  129. //InstanceEnumeratorPattern //BindWithCurrentdict exec % Enumerator
  130. /status 0 def % variable for the current status to enumerate - do not bind with it !
  131. end
  132. exch setglobal
  133. } bind def
  134. /execstack_lookup % <object> execstack_lookup <object1>
  135. % <object> execstack_lookup null
  136. { % Checks whether execution stack contains a procedure starting with <object>,
  137. % and retrives the 2nd element of the procedure,
  138. % or null if the procedure was not found.
  139. %
  140. % Since 'execstack' actually renders subarrays of procedures,
  141. % the pattern for recognition must be like this :
  142. %
  143. % { <object> <object1>
  144. % CallSomething
  145. % } loop
  146. %
  147. % The solution with 'loop' depends on how GS implements cycles,
  148. % so it must not appear in documents, which are required to be interpreter independent.
  149. % Any other type of cycles are also acceptable.
  150. % If no repitition is really needed, just insert 'exit' into its body.
  151. % If <object> <object1> are not needed for the caller, insert "pop pop" after them.
  152. % If <object1> is really unuseful, the pattern may be simplified :
  153. %
  154. % { <object> pop
  155. % CallSomething
  156. % exit
  157. % } loop
  158. %
  159. % It will retrieve 'pop' or 'null'.
  160. %
  161. % Note that 2 topmost execstack elements are the execstack_lookup procedure and its caller.
  162. % We don't check them.
  163. currentglobal false setglobal % <object> bGlobal
  164. countexecstack array execstack % <object> bGlobal [execstack]
  165. dup null exch % <object> bGlobal [execstack] null [execstack]
  166. length 3 sub -1 0 { % <object> bGlobal [execstack] null i
  167. 2 index exch get % <object> bGlobal [execstack] null proc
  168. dup type dup /packedarraytype eq exch /arraytype eq or {
  169. dup rcheck {
  170. dup length 1 gt { % <object> bGlobal [execstack] null proc
  171. dup 0 get % <object> bGlobal [execstack] null proc elem0
  172. 5 index eq { % <object> bGlobal [execstack] null proc
  173. 1 get % <object> bGlobal [execstack] null object1
  174. exch pop exit % <object> bGlobal [execstack] object1
  175. } {
  176. pop
  177. } ifelse
  178. } {
  179. pop % <object> bGlobal [execstack] false
  180. } ifelse
  181. } {
  182. pop % <object> bGlobal [execstack] false
  183. } ifelse
  184. } {
  185. pop % <object> bGlobal [execstack] false
  186. } ifelse
  187. } for % <object> bGlobal [execstack] bResult
  188. exch pop exch setglobal exch pop % bResult
  189. } bind def
  190. currentpacking false setpacking
  191. /MethodsToRedefine 5 dict begin
  192. % Procedures in this dictionary really are patterns for new category methods.
  193. % The following names will be replaced with specific objects during BindWithCurrentdict :
  194. % .map - the map dictionary;
  195. % DefineResource, ResourceStatus, ResourceFileName, FindResource, ResourceForAll
  196. % - procedures from the original resource category.
  197. /FindResource % <Name> FindResource <dict>
  198. { RESMPDEBUG { (resmp FindResource beg ) print dup = } if
  199. dup ResourceStatus exec {
  200. pop 2 lt
  201. } {
  202. false
  203. } ifelse % bInVirtualMemory
  204. { FindResource exec
  205. } {
  206. dup dup .map exch .knownget { % /Name /Name <<record>>
  207. dup dup /RecordVirtualMethods get /IsActive get exec {
  208. 1 index .getvminstance { % /Name /Name <<record>> holder
  209. 1 get 1 eq
  210. } {
  211. true
  212. } ifelse % /Name /Name <<record>> bStatusIs1
  213. 4 1 roll % bStatusIs1 /Name /Name <<record>>
  214. dup /RecordVirtualMethods get /MakeInstance get exec
  215. % bStatusIs1 /Name /Name Instance size
  216. 5 1 roll % size bStatusIs1 /Name /Name Instance
  217. DefineResource exec % size bStatusIs1 /Name Instance
  218. % Make ResourceStatus to return correct values for this instance :
  219. % Hack: we replace status values in the instance holder :
  220. exch .getvminstance pop % size bStatusIs1 Instance holder
  221. dup 5 -1 roll 2 exch put % bStatusIs1 Instance holder
  222. 3 2 roll { % Instance holder
  223. 1 1 put % Instance
  224. } {
  225. pop
  226. } ifelse % Instance
  227. } { % /Name /Name <<record>>
  228. pop pop FindResource exec
  229. } ifelse
  230. } { % /Name /Name
  231. pop FindResource exec
  232. } ifelse
  233. } ifelse
  234. RESMPDEBUG { (resmp FindResource end) = } if
  235. } bind def
  236. /ResourceStatus % <Name> ResourceStatus <status> <size> true
  237. % <Name> ResourceStatus false
  238. { RESMPDEBUG { (resmp ResourceStatus beg ) print dup == } if
  239. dup ResourceStatus exec { % /Name status size
  240. 1 index 2 lt {
  241. % In VM - return with it.
  242. 3 2 roll pop true
  243. } {
  244. % Not in VM.
  245. exch pop exch % size /Name
  246. dup .map exch .knownget { % size /Name <<record>>
  247. dup dup /RecordVirtualMethods get /IsActive get exec {
  248. 3 2 roll pop % /Name <<record>>
  249. dup /RecordVirtualMethods get /GetSize get exec 2 exch true
  250. } { % size /Name <<record>>
  251. pop pop 2 exch true
  252. } ifelse
  253. } { % size /Name
  254. pop 2 exch true
  255. } ifelse
  256. } ifelse
  257. } { % /Name
  258. dup .map exch .knownget { % /Name <<record>>
  259. dup dup /RecordVirtualMethods get /IsActive get exec {
  260. dup /RecordVirtualMethods get /GetSize get exec 2 exch true
  261. } { % /Name <<record>>
  262. pop pop false
  263. } ifelse
  264. } { % /Name
  265. pop false
  266. } ifelse
  267. } ifelse
  268. RESMPDEBUG { (resmp ResourceStatus end) = } if
  269. } bind def
  270. /ResourceFileName % <Name> <scratch> ResourceFileName <string>
  271. { RESMPDEBUG { (resmp ResourceFileName beg ) print 1 index = } if
  272. exch % (scratch) /Name
  273. .map 1 index .knownget { % (scratch) /Name <<record>>
  274. RESMPDEBUG { (resmp ResourceFileName : have a map record.) = } if
  275. dup dup /RecordVirtualMethods get /IsActive get exec {
  276. RESMPDEBUG { (resmp ResourceFileName : record is active.) = } if
  277. dup /RecordVirtualMethods get /GetFilePath get exec % (string)
  278. RESMPDEBUG { (resmp ResourceFileName : retrieving ) print dup = } if
  279. } { % (scratch) /Name <<record>>
  280. RESMPDEBUG { (resmp ResourceFileName : record is NOT active.) = } if
  281. pop exch ResourceFileName exec
  282. RESMPDEBUG { (resmp ResourceFileName : retrieving ) print dup = } if
  283. } ifelse
  284. } {
  285. RESMPDEBUG { (resmp ResourceFileName : have NO map record.) = } if
  286. exch ResourceFileName exec
  287. RESMPDEBUG { (resmp ResourceFileName : retrieving ) print dup = } if
  288. } ifelse
  289. RESMPDEBUG { (resmp ResourceFileName end) = } if
  290. } bind def
  291. /ResourceForAll % <template> <proc> <scratch> ResourceForAll -
  292. { RESMPDEBUG { (resmp ResourceForAll beg ) print CategoryName =string cvs print ( ) print 2 index = } if
  293. % Create InstancesStatus dictionary :
  294. 20 dict % IS - Instances Status
  295. 4 1 roll % <<IS>> (templ) {proc} (sctarch)
  296. % Check if we are under another ResourceForAll :
  297. /.DisableResourceOrdering //execstack_lookup exec null eq 4 1 roll
  298. % <<IS>> bOrder (templ) {proc} (sctarch)
  299. % Put underlying resources to the InstancesStatus dictionary :
  300. currentdict % the category
  301. begin % ResourceForAll removes it locally.
  302. 2 index
  303. { cvn % <<IS>> bOrder (templ) {proc} (sctarch) /Name
  304. 4 index {
  305. dup ResourceStatus exec {pop 6 index 3 1 roll put} {pop} ifelse
  306. } {
  307. 5 index exch 2 put % Don't need the ordering, put '2' as a scratch.
  308. } ifelse
  309. }
  310. 2 index ResourceForAll exec % <<IS>> bOrder (templ) {proc} (sctarch)
  311. 4 3 roll pop % <<IS>> (templ) {proc} (sctarch)
  312. end
  313. % Put .map entries to the InstancesStatus dictionary :
  314. 4 -1 roll begin % (templ) {proc} (sctarch)
  315. .map { % (templ) {proc} (sctarch) /Name record
  316. dup dup /RecordVirtualMethods get /IsActive get exec {
  317. pop % (templ) {proc} (sctarch) /Name
  318. dup currentdict exch known {
  319. pop
  320. } {
  321. dup 2 index cvs % (templ) {proc} (sctarch) /Name (Name)
  322. 4 index .stringmatch { % (templ) {proc} (sctarch) /Name
  323. 2 def % It is not in VM.
  324. } {
  325. pop
  326. } ifelse
  327. } ifelse
  328. } { % (templ) {proc} (sctarch) /Name record
  329. pop pop
  330. } ifelse
  331. } forall % (templ) {proc} (sctarch)
  332. % prepare stacks for the enumeration :
  333. 3 2 roll pop % {proc} (sctarch)
  334. currentdict end % {proc} (scratch) <<IS>>
  335. % Make the enumerator and apply it :
  336. //MakeResourceEnumerator exec exec
  337. RESMPDEBUG { (resmp ResourceForAll end)= } if
  338. } bind def
  339. /GetCIDSystemInfoFromMap % <Name> GetCIDSystemInfoFromMap <Name>
  340. % <Name> GetCIDSystemInfoFromMap <dict>
  341. { RESMPDEBUG { (resmp GetCIDSystemInfoFromMap beg ) print dup = } if
  342. % This is a special function for communicating with GetCIDSystemInfo in gs_cidcm.ps .
  343. dup .map exch .knownget {
  344. RESMPDEBUG { (resmp GetCIDSystemInfoFromMap : have a map record.) = } if
  345. dup /RecordVirtualMethods get /GetCSI get exec
  346. dup null ne {
  347. RESMPDEBUG { (resmp GetCIDSystemInfoFromMap : retrieving a dict.) = } if
  348. exch
  349. } if
  350. pop
  351. } if
  352. RESMPDEBUG { (resmp GetCIDSystemInfoFromMap end) = } if
  353. } bind def
  354. currentdict end def
  355. setpacking
  356. /Redefine % <OptionsDict> Redefine -
  357. { % Before calling this proc, the OptionsDict must specify options for
  358. % the catregory to be redefined :
  359. % CategoryName - a name of category to redefine;
  360. % MapFileName - a string for the resource map file name;
  361. % VerifyMap - a procedure :
  362. % <raw_map> VerifyMap -
  363. % - checks the map for consistency
  364. % PreprocessRecord - a procedure :
  365. % <map> <Name> <raw_record> PreprocessRecord <map> <Name> <record> true
  366. % <map> <Name> <raw_record> PreprocessRecord <map> <Name> <raw_record> false
  367. % - converts a map record into a dictionary;
  368. % It must add RecordVirtualMethods dictionary to the record :
  369. % MakeInstance - a procedure :
  370. % <Name> <record> MakeInstance <Name> <Instance> <size>
  371. % - converts the record to resource instance;
  372. % GetFilePath - a procedure for ResourceFileName :
  373. % <scratch> <Name> <record> GetFilePath <filepath>
  374. % GetSize - a procedure for ResourceStatus :
  375. % <Name> <record> GetSize <size>
  376. % GetCSI - a procedure for obtaining CIDSystemInfo dictionary from the record :
  377. % <record> GetCSI <CSI>
  378. % <record> GetCSI null
  379. % IsActive - a procedure for skipping records depending on the current device :
  380. % <record> IsActive <bool>
  381. % Also it is allowed to contain additional entries for client's needs.
  382. % The OptionsDict is also used for storing some local variables.
  383. % If a category is being redefined several times with this function,
  384. % each redefinition must either use an unique map file,
  385. % or the map file should be scanned by the last redefinition
  386. % (and must be defined in the last one with /MapFileName).
  387. % This happens so because we must accumulate all variants of
  388. % methods before scanning the map. We would like to delay
  389. % the scanning until all redefinitions are done, but it requires
  390. % to implement a queue of "refinish" methods and execute it
  391. % at very end of the prelude.
  392. begin % OptionsDict
  393. CategoryName /Category findresource /OldCategory exch def
  394. OldCategory /.IsRedefinedWithMap known {
  395. % Already redefined with map - don't redefine, but enhance the map.
  396. OldCategory /NewCategory exch def
  397. } {
  398. % Redefine with a new category instance.
  399. OldCategory dup length dict
  400. dup /.PreprocessRecord 4 dict put
  401. copy /NewCategory exch def
  402. } ifelse
  403. % Provide the 'or' logic for PreprocessRecord,
  404. % to allow different record types to be mixed in a single map file.
  405. % We do this with building a dictionary of PreprocessRecord procedures,
  406. % which come from different calls to Redefine :
  407. NewCategory /.PreprocessRecord get dup length % <<pr>> l
  408. currentdict /PreprocessRecord get .growput
  409. currentdict /MapFileName known {
  410. MapFileName .libfile {
  411. 1 dict begin
  412. /; {} def
  413. mark exch cvx exec .dicttomark % <<map>>
  414. end
  415. dup VerifyMap % <<map>>
  416. } {
  417. QUIET not {
  418. currentdict /IsMapFileOptional .knownget not { false } if not {
  419. (Warning: the map file ) print dup =string cvs print ( was not found.) =
  420. } if
  421. } if
  422. pop 0 dict % <<map>>
  423. } ifelse
  424. } {
  425. currentdict /.map .knownget not {
  426. 0 dict % <<map>>
  427. } if
  428. } ifelse
  429. % Preprocess entries :
  430. dup NewCategory /.PreprocessRecord get % <<map>> <<map>> <<pr>>
  431. 3 1 roll { % <<pr>> <<map>> /Name raw_record
  432. false 3 1 roll % <<pr>> <<map>> false /Name raw_record
  433. 4 index { % <<pr>> <<map>> false /Name raw_record i {pr}
  434. exch pop % <<pr>> <<map>> false /Name raw_record {pr}
  435. exec { % <<pr>> <<map>> false /Name record
  436. 3 -1 roll pop true 3 1 roll % <<pr>> <<map>> true /Name record
  437. exit
  438. } if % <<pr>> <<map>> false /Name raw_record
  439. } forall
  440. 3 2 roll { % <<pr>> <<map>> /Name record
  441. 2 index 3 1 roll put % <<pr>> <<map>>
  442. } {
  443. exch % <<pr>> <<map>> raw_record /Name
  444. (Incorrect record ) print =string cvs print ( of the map file ) print MapFileName =string cvs print (.) =
  445. end % Pops OptionsDict from dstack.
  446. pop pop pop %
  447. /Redefine cvx /undefinedresource signalerror
  448. } ifelse
  449. } forall % <<pr>> <<map>>
  450. exch pop % <<map>>
  451. % Add the map :
  452. OldCategory /.IsRedefinedWithMap known { % <<map>>
  453. % Just add to the old map :
  454. OldCategory /.map get copy pop %
  455. } { % <<map>>
  456. % Store the map to both the category and OptionsDict :
  457. dup NewCategory exch /.map exch put
  458. /.map exch def %
  459. } ifelse
  460. OldCategory /.IsRedefinedWithMap known not {
  461. % Copy old methods to OptionsDict :
  462. [ /DefineResource /ResourceStatus /ResourceFileName
  463. /FindResource /ResourceForAll
  464. ] {
  465. dup OldCategory exch get def
  466. } forall
  467. % Build new methods :
  468. //MethodsToRedefine {
  469. //BindWithCurrentdict exec NewCategory 3 1 roll put
  470. } forall
  471. CategoryName /CIDFont ne {
  472. NewCategory /GetCIDSystemInfoFromMap undef
  473. % This is some ugly, sorry.
  474. } if
  475. % Redefine the category :
  476. NewCategory /.IsRedefinedWithMap true put
  477. CategoryName NewCategory /Category defineresource pop
  478. } if
  479. end % OptionsDict
  480. } bind executeonly def
  481. currentdict /PutPreprocessRecord .undef
  482. currentdict end
  483. /ProcSet defineresource pop
  484. setglobal .setlanguagelevel