Debian Codegraph @

Resource/Init/gs_resmp.ps

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