Debian Codegraph @

Resource/Init/gs_res.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 % Initialization file for Level 2 resource machinery.
17 % When this is run, systemdict is still writable,
18 % but (almost) everything defined here goes into level2dict.
19
20 level2dict begin
21
22 (BEGIN RESOURCES) VMDEBUG
23
24 % We keep track of (global) instances with another entry in the resource
25 % dictionary, an .Instances dictionary. For categories with implicit
26 % instances, the values in .Instances are the same as the keys;
27 % for other categories, the values are [instance status size].
28
29 % Note that the dictionary that defines a resource category is stored
30 % in global VM. The PostScript manual says that each category must
31 % manage global and local instances separately. However, objects in
32 % global VM other than systemdict can't reference objects in local VM.
33 % This means that the resource category dictionary, which would otherwise be
34 % the obvious place to keep track of the instances, can't be used to keep
35 % track of local instances. Instead, we define a dictionary in local VM
36 % called localinstancedict, in which the key is the category name and
37 % the value is the analogue of .Instances for local instances.
38
39 % We don't currently implement automatic resource unloading.
40 % When and if we do, it should be hooked to the garbage collector.
41 % However, Ed Taft of Adobe says their interpreters don't implement this
42 % either, so we aren't going to worry about it for a while.
43
44 currentglobal //false setglobal
45 systemdict /localinstancedict 5 dict
46 .forceput % localinstancedict is local, systemdict is global
47 //true setglobal
48 /.emptydict 0 dict readonly def
49 setglobal
50
51 % Resource category dictionaries have the following keys (those marked with
52 % * are optional):
53 % Standard, defined in the Red Book:
54 % Category (name)
55 % *InstanceType (name)
56 % DefineResource
57 % <key> <instance> DefineResource <instance>
58 % UndefineResource
59 % <key> UndefineResource -
60 % FindResource
61 % <key> FindResource <instance>
62 % ResourceStatus
63 % <key> ResourceStatus <status> <size> true
64 % <key> ResourceStatus false
65 % ResourceForAll
66 % <template> <proc> <scratch> ResourceForAll -
67 % *ResourceFileName
68 % <key> <scratch> ResourceFileName <filename>
69 % Additional, specific to our implementation:
70 % .Instances (dictionary)
71 % .LocalInstances
72 % - .LocalInstances <dict>
73 % .GetInstance
74 % <key> .GetInstance <instance> -true-
75 % <key> .GetInstance -false-
76 % .CheckResource
77 % <key> <value> .CheckResource <key> <value> <ok>
78 % (or may give an error if not OK)
79 % .DoLoadResource
80 % <key> .DoLoadResource <key> (may give an error)
81 % .LoadResource
82 % <key> .LoadResource - (may give an error)
83 % .ResourceFile
84 % <key> .ResourceFile <file> -true-
85 % <key> .ResourceFile <key> -false-
86 % .ResourceFileStatus
87 % <key> .ResourceFileStatus 2 <vmusage> -true-
88 % <key> .ResourceFileStatus -false-
89 % All the above procedures expect that the top dictionary on the d-stack
90 % is the resource dictionary.
91
92 % Define enough of the Category category so we can define other categories.
93 % The dictionary we're about to create will become the Category
94 % category definition dictionary.
95
96 % .findcategory and .resourceexec are only called from within the
97 % implementation of the resource 'operators', so they don't have to worry
98 % about cleaning up the stack if they fail (the interpreter's stack
99 % protection machinery for pseudo-operators takes care of this).
100 % Note that all places that look up categories must use .findcategory
101 % so that the command in case of error will be correct rather than an
102 % internal invocation of findresource.
103 /.findcategory { % <name> .findcategory -
104 % (pushes the category on the dstack)
105 /Category .findresource begin % note: *not* findresource
106 } .internalbind def
107
108 % If an error occurs within the logic of a resource operator (after operand
109 % acquisition and checking), the Adobe interpreters report the operator name,
110 % not the operator object, as the command in $error. For this reason, and
111 % this reason only, all resource operators must wrap their logic code in
112 % /<opername> cvx { ...logic... } .errorexec
113
114 % The Category resource signals /undefined rather than /undefinedresource,
115 % both when referenced implicitly (to look up the category for a general
116 % resource operation) and when it is accessed directly (/Category /xxx
117 % findresource). Because of this, all resource operators must use
118 % .undefinedresource rather than signalling undefinedresource directly.
119 /.undefinedresource { % <command> .undefinedresource -
120 /Category dup load eq { /undefined } { /undefinedresource } ifelse
121 signaloperror
122 } .internalbind def
123
124 /.resourceexec { % <key> /xxxResource .resourceexec -
125 % (also pops the category from the dstack)
126 load exec end
127 } .internalbind def
128
129 % .getvminstance treats instances on disk as undefined.
130 /.getvminstance { % <key> .getvminstance <instance> -true-
131 % <key> .getvminstance -false-
132 .GetInstance {
133 dup 1 get 2 ne { //true } { pop //false } ifelse
134 } {
135 //false
136 } ifelse
137 } .internalbind def
138
139 20 dict begin
140
141 % Standard entries
142
143 /Category /Category def
144 /InstanceType /dicttype def
145
146 /DefineResource {
147 .CheckResource {
148 dup /Category 3 index cvlit .growput
149 dup [ exch 0 -1 ] exch
150 .Instances 4 2 roll put
151 % Make the Category dictionary read-only. We will have to
152 % use .forceput / .forceput later to replace the dummy,
153 % empty .Instances dictionary with the real one later.
154 readonly
155 }{
156 /defineresource cvx /typecheck signaloperror
157 } ifelse
158 } .internalbind odef
159 /FindResource % (redefined below)
160 { .Instances exch get 0 get
161 } .internalbind def
162
163 % Additional entries
164
165 /.Instances 30 dict def
166 .Instances /Category [currentdict 0 -1] put
167
168 /.LocalInstances 0 dict def
169 /.GetInstance
170 { .Instances exch .knownget
171 } .internalbind def
172 /.CheckResource
173 { dup gcheck currentglobal and
174 { /DefineResource /FindResource /ResourceForAll /ResourceStatus
175 /UndefineResource }
176 { 2 index exch known and }
177 forall
178 not { /defineresource cvx /invalidaccess signaloperror } if
179 //true
180 } .internalbind def
181
182 .Instances end begin % for the base case of findresource
183
184 (END CATEGORY) VMDEBUG
185
186 % Define the resource operators. We use the "stack protection" feature of
187 % odef to make sure the stacks are restored properly on an error.
188 % This requires that the operators not pop anything from the stack until
189 % they have executed their logic successfully. We can't make this
190 % work for resourceforall, because the procedure it executes mustn't see
191 % the operands of resourceforall on the stack, but we can make it work for
192 % the others.
193
194 % findresource is the only operator that needs to bind //Category.
195 % We define its contents as a separate procedure so that .findcategory
196 % can use it without entering another level of pseudo-operator.
197 /.findresource { % <key> <category> findresource <instance>
198 2 copy dup /Category eq
199 { pop //Category 0 get begin } { //.findcategory exec } ifelse
200 /FindResource //.resourceexec exec exch pop exch pop
201 } .internalbind
202 end % .Instances of Category
203 def
204 /findresource {
205 % See above re .errorexec.
206 1 .argindex % also catch stackunderflow
207 dup type /stringtype eq { cvn } if % for CET 23-13-04
208 3 1 roll exch pop
209 dup type /nametype ne {
210 /findresource .systemvar /typecheck signalerror
211 } if
212 /findresource cvx //.findresource .errorexec
213 } .internalbind odef
214
215 /defineresource { % <key> <instance> <category> defineresource <instance>
216 2 .argindex 2 index 2 index % catch stackunderflow
217 % See above re .errorexec.
218 /defineresource cvx {
219 //.findcategory exec
220 currentdict /InstanceType known {
221 dup type InstanceType ne {
222 dup type /packedarraytype eq InstanceType /arraytype eq and
223 not { /defineresource cvx /typecheck signaloperror } if
224 } if
225 } if
226 /DefineResource //.resourceexec exec
227 4 1 roll pop pop pop
228 } .errorexec
229 } .internalbind odef
230 % We must prevent resourceforall from automatically restoring the stacks,
231 % because we don't want the stacks restored if proc causes an error or
232 % executes a 'stop'. On the other hand, resourceforall is defined in the
233 % PLRM as an operator, so it must have type /operatortype. We hack this
234 % by taking advantage of the fact that the interpreter optimizes tail
235 % calls, so stack protection doesn't apply to the very last token of an
236 % operator procedure.
237 /resourceforall1 { % <template> <proc> <scratch> <category> resourceforall1 -
238 dup //.findcategory exec
239 /ResourceForAll load
240 % Stack: <template> <proc> <scratch> <category> proc
241 exch pop % pop the category
242 exec end
243 } .forcebind def
244 /resourceforall { % <template> <proc> <scratch> <category> resourceforall1 -
245 //resourceforall1 exec % see above
246 } .forcebind odef
247 /resourcestatus { % <key> <category> resourcestatus <status> <size> true
248 % <key> <category> resourcestatus false
249 {
250 0 .argindex type /nametype ne {
251 % CET 23-26 wants typecheck here, not undefineresource that happens
252 % without the check.
253 /resourcestatus cvx /typecheck signalerror
254 } if
255 2 copy //.findcategory exec /ResourceStatus //.resourceexec exec
256 { 4 2 roll pop pop //true } { pop pop //false } ifelse
257 } stopped {
258 % Although resourcestatus is an operator, Adobe uses executable name
259 % for error reporting. CET 23-26
260 /resourcestatus cvx $error /errorname get signalerror
261 } if
262 } .forcebind odef
263 /undefineresource { % <key> <category> undefineresource -
264 0 .argindex type /nametype ne {
265 /undefinedresource cvx /typecheck signaloperror
266 } if
267 1 .argindex 1 index % catch stackunderflow
268
269 { //.findcategory exec /UndefineResource //.resourceexec exec pop pop
270 } stopped {
271 % Although undefineresource is an operator, Adobe uses executable name
272 % here but uses operator for the errors above. CET 23-33
273 /undefineresource cvx $error /errorname get signalerror
274 } if
275 } .forcebind odef
276
277 % Define the system parameters used for the Generic implementation of
278 % ResourceFileName.
279 systemdict begin
280
281 % - .default_resource_dir <string>
282 /.default_resource_dir {
283 /LIBPATH .systemvar {
284 dup .file_name_current eq {
285 pop
286 } {
287 (Resource) rsearch {
288 exch concatstrings
289 exch pop
290 .file_name_separator concatstrings exit
291 } {
292 pop
293 } ifelse
294 } ifelse
295 } forall
296 } .internalbind def
297
298 % <path> <name> <string> .resource_dir_name <path> <name> <string>
299 /.resource_dir_name
300 { systemdict 2 index .knownget {
301 exch pop
302 systemdict 1 index undef
303 } {
304 dup () ne {
305 .file_name_directory_separator concatstrings
306 } if
307 2 index exch //false
308 .file_name_combine not {
309 (Error: .default_resource_dir returned ) print exch print ( that can't combine with ) print =
310 /.default_resource_dir cvx /configurationerror signalerror
311 } if
312 } ifelse
313 } .internalbind def
314
315 currentdict /pssystemparams known not {
316 /pssystemparams 10 dict readonly def
317 } if
318 pssystemparams begin
319 //.default_resource_dir exec
320 /FontResourceDir (Font) //.resource_dir_name exec
321 readonly currentdict 3 1 roll .forceput % pssys'params is r-o
322 /GenericResourceDir () //.resource_dir_name exec
323 readonly currentdict 3 1 roll .forceput % pssys'params is r-o
324 pop % .default_resource_dir
325 /GenericResourcePathSep
326 .file_name_separator readonly currentdict 3 1 roll .forceput % pssys'params is r-o
327 currentdict (%diskFontResourceDir) cvn (/Resource/Font/) readonly .forceput % pssys'params is r-o
328 currentdict (%diskGenericResourceDir) cvn (/Resource/) readonly .forceput % pssys'params is r-o
329 end
330 end
331
332 % Check if GenericResourceDir presents in LIBPATH.
333
334 % The value of GenericResourceDir must end with directory separator.
335 % We use .file_name_combine to check it.
336 % Comments use OpenVMS syntax, because it is the most complicated case.
337 (x) pssystemparams /GenericResourcePathSep get
338 (y) concatstrings concatstrings dup length % (x]y) l1
339 pssystemparams /GenericResourceDir get dup length exch % (x]y) l1 l2 (dir)
340 3 index //true .file_name_combine not {
341 exch
342 (File name ) print print ( cant combine with ) print =
343 /GenericResourceDir cvx /configurationerror signaloperror
344 } if
345 dup length % (x]y) l1 l2 (dir.x]y) l
346 4 2 roll add % (x]y) (dir.x]y) l ll
347 ne {
348 (GenericResourceDir value does not end with directory separator.\n) =
349 /GenericResourceDir cvx /configurationerror signaloperror
350 } if
351 pop pop
352
353 pssystemparams dup /GenericResourceDir get exch /GenericResourcePathSep get
354 (Init) exch (gs_init.ps) concatstrings concatstrings concatstrings
355 status {
356 pop pop pop pop
357 } {
358 (\n*** Warning: GenericResourceDir doesn't point to a valid resource directory.) =
359 ( the -sGenericResourceDir=... option can be used to set this.\n) =
360 flush
361 } ifelse
362
363 % Define the generic algorithm for computing resource file names.
364 /.rfnstring 8192 string def
365 /.genericrfn % <key> <scratch> <prefix> .genericrfn <filename>
366 { 3 -1 roll //.rfnstring cvs concatstrings exch copy
367 } .internalbind def
368
369 % Define the Generic category.
370
371 /Generic mark
372
373 % Standard entries
374
375 % We're still running in Level 1 mode, so dictionaries won't expand.
376 % Leave room for the /Category entry.
377 /Category //null
378
379 % Implement the body of Generic resourceforall for local, global, and
380 % external cases. 'args' is [template proc scratch resdict].
381 /.enumerateresource { % <key> [- <proc> <scratch>] .enumerateresource -
382 1 index type dup /stringtype eq exch /nametype eq or {
383 exch 1 index 2 get cvs exch
384 } if
385 % Use .setstackprotect to prevent the stacks from being restored if
386 % an error occurs during execution of proc.
387 1 get //false .setstackprotect exec //true .setstackprotect
388 } .internalbind def
389 /.localresourceforall { % <key> <value> <args> .localr'forall -
390 exch pop
391 2 copy 0 get .stringmatch { //.enumerateresource exec } { pop pop } ifelse
392 } .internalbind def
393 /.globalresourceforall { % <key> <value> <args> .globalr'forall -
394 exch pop
395 2 copy 0 get .stringmatch {
396 dup 3 get begin .LocalInstances end 2 index known not {
397 //.enumerateresource exec
398 } {
399 pop pop
400 } ifelse
401 } {
402 pop pop
403 } ifelse
404 } .internalbind def
405 /.externalresourceforall { % <filename> <len> <args> .externalr'forall -
406 3 1 roll 1 index length 1 index sub getinterval exch
407 dup 3 get begin .Instances .LocalInstances end
408 % Stack: key args insts localinsts
409 3 index known {
410 pop pop pop
411 } {
412 2 index known { pop pop } { //.enumerateresource exec } ifelse
413 } ifelse
414 } .internalbind def
415
416 /DefineResource dup {
417 .CheckResource
418 { dup [ exch 0 -1 ]
419 % Stack: key value instance
420 currentglobal
421 { //false setglobal 2 index UndefineResource % remove local def if any
422 //true setglobal
423 .Instances dup //.emptydict eq {
424 pop 3 dict
425 % As noted above, Category dictionaries are read-only,
426 % so we have to use .forceput here.
427 currentdict /.Instances 2 index .forceput % Category dict is read-only
428 } executeonly if
429 } executeonly
430 { .LocalInstances dup //.emptydict eq
431 { pop 3 dict localinstancedict Category 2 index put
432 }
433 if
434 }
435 ifelse
436 % Stack: key value instance instancedict
437 3 index 2 index .growput
438 % Now make the resource value read-only.
439 0 2 copy get { readonly } //.internalstopped exec pop
440 dup 4 1 roll put exch pop exch pop
441 } executeonly
442 { /defineresource cvx /typecheck signaloperror
443 }
444 ifelse
445 } .forcebind .makeoperator % executeonly to prevent access to .forceput
446 /UndefineResource
447 { { dup 2 index .knownget
448 { dup 1 get 1 ge
449 { dup 0 //null put 1 2 put pop pop }
450 { pop exch .undef }
451 ifelse
452 }
453 { pop pop
454 }
455 ifelse
456 }
457 currentglobal
458 { 2 copy .Instances exch exec
459 }
460 if .LocalInstances exch exec
461 } .forcebind
462 % Because of some badly designed code in Adobe's CID font downloader that
463 % makes findresource and resourcestatus deliberately inconsistent with each
464 % other, the default FindResource must not call ResourceStatus if there is
465 % an instance of the desired name already defined in VM.
466 /FindResource {
467 dup //null eq {
468 % CET 13-06 wants /typecheck for "null findencoding" but
469 % .knownget doesn't fail on null
470 /findresource cvx /typecheck signaloperror
471 } if
472 dup //.getvminstance exec {
473 exch pop 0 get
474 } {
475 dup ResourceStatus {
476 pop 1 gt {
477 .DoLoadResource //.getvminstance exec not {
478 /findresource cvx //.undefinedresource exec
479 } if 0 get
480 } {
481 .GetInstance pop 0 get
482 } ifelse
483 } {
484 /findresource cvx //.undefinedresource exec
485 } ifelse
486 } ifelse
487 } .forcebind
488 % Because of some badly designed code in Adobe's CID font downloader, the
489 % definition of ResourceStatus for Generic and Font must be the same (!).
490 % We patch around this by using an intermediate .ResourceFileStatus procedure.
491 /ResourceStatus {
492 dup .GetInstance {
493 exch pop dup 1 get exch 2 get //true
494 } {
495 .ResourceFileStatus
496 } ifelse
497 } .forcebind
498 /.ResourceFileStatus {
499 .ResourceFile { closefile 2 -1 //true } { pop //false } ifelse
500 } .internalbind
501 /ResourceForAll {
502 % Construct a new procedure to hold the arguments.
503 % All objects constructed here must be in local VM to avoid
504 % a possible invalidaccess.
505 currentdict 4 .localvmpackedarray % [template proc scratch resdict]
506 % We must pop the resource dictionary off the dict stack
507 % when doing the actual iteration, and restore it afterwards.
508 .currentglobal not {
509 .LocalInstances length 0 ne {
510 % We must do local instances, and do them first.
511 //.localresourceforall {exec} 0 get 3 .localvmpackedarray cvx
512 .LocalInstances exch {forall} 0 get 1 index 0 get
513 currentdict end 3 .execn begin
514 } if
515 } if
516 % Do global instances next.
517 //.globalresourceforall {exec} 0 get 3 .localvmpackedarray cvx
518 .Instances exch cvx {forall} 0 get 1 index 0 get
519 currentdict end 3 .execn begin
520 mark % args [
521 Category .namestring .file_name_separator concatstrings
522 2 index 0 get % args [ (c/) (t)
523 1 index length 3 1 roll % args [ l (c/) (t)
524 concatstrings % args [ l (c/t)
525 [
526 //true /LIBPATH .systemvar 3 index
527 .generate_dir_list_templates_with_length % args (t) [ l [(pt) Lp ...]
528 % also add on the Resources as specified by the GenericResourceDir
529 //true [ currentsystemparams /GenericResourceDir get]
530 counttomark 1 add index .generate_dir_list_templates_with_length
531 ] exch pop
532 dup length 1 sub 0 exch 2 exch { % args [ l [] i
533 2 copy get % args [ l [] i (pt)
534 exch 2 index exch 1 add get % args [ l [] (pt) Lp
535 3 index add
536 exch % args [ l [] Lp (pt)
537
538 { % args [ l [] Lp (pf)
539 dup length % args [ l [] Lp (pf) Lpf
540 2 index sub % args [ l [] Lp (pf) Lf
541 2 index exch % args [ l [] Lp (pf) Lp Lf
542 getinterval cvn dup % args [ l [] Lp /n /n
543 5 2 roll % args [ /n /n l [] Lp
544 } //.rfnstring filenameforall
545 pop % args [ /n1 /n1 ... /nN /nN l []
546 } for % args [ /n1 /n1 ... /nN /nN l []
547 pop pop
548 .dicttomark % An easy way to exclude duplicates. % args <</n/n>>
549 % {
550 { pop } 0 get
551 2 index 2 get { cvs 0 } aload pop 5 index
552 //.externalresourceforall {exec} 0 get
553 % }
554 7 .localvmpackedarray cvx
555 3 2 roll pop % args
556 { forall } 0 get
557 currentdict end 2 .execn begin
558 } .forcebind
559
560 /ResourceFileName { % /in (scr) --> (p/c/n)
561 exch //.rfnstring cvs % (scr) (n)
562 /GenericResourcePathSep getsystemparam exch % (scr) (/) (n)
563 Category .namestring % (scr) (/) (n) (c)
564 3 1 roll % (scr) (c) (/) (n)
565 concatstrings concatstrings % (scr) (c/n)
566 /GenericResourceDir getsystemparam 1 index % (scr) (c/n) (p/) (c/n)
567 concatstrings % (scr) (c/n) (p/c/n)
568 dup status {
569 pop pop pop pop exch pop % (scr) (p/c/n)
570 } {
571 exch
572 .libfile
573 {//true}
574 {
575 pop dup .libfile
576 {//true}
577 {//false}
578 ifelse
579 } ifelse
580
581 {
582 dup .filename pop
583 exch closefile
584 exch pop
585 }
586 {pop}
587 ifelse
588 } ifelse
589 exch copy % (p/c/n)
590 } .forcebind
591
592 % Additional entries
593
594 % Unfortunately, we can't create the real .Instances dictionary now,
595 % because if someone copies the Generic category (which pp. 95-96 of the
596 % 2nd Edition Red Book says is legitimate), they'll wind up sharing
597 % the .Instances. Instead, we have to create .Instances on demand,
598 % just like the entry in localinstancedict.
599 % We also have to prevent anyone from creating instances of Generic itself.
600 /.Instances //.emptydict
601
602 /.LocalInstances
603 { localinstancedict Category .knownget not { //.emptydict } if
604 } .internalbind
605 /.GetInstance
606 { currentglobal
607 { .Instances exch .knownget }
608 { .LocalInstances 1 index .knownget
609 { exch pop //true }
610 { .Instances exch .knownget }
611 ifelse
612 }
613 ifelse
614 } .internalbind
615 /.CheckResource
616 { //true
617 } .internalbind
618 /.vmused {
619 % - .vmused <usedvalue>
620 % usedvalue = vmstatus in global + vmstatus in local.
621 0 2 {
622 .currentglobal not .setglobal
623 vmstatus pop exch pop add
624 } repeat
625 } .internalbind odef
626 /.DoLoadResource {
627 % .LoadResource may push entries on the operand stack.
628 % It is an undocumented feature of Adobe implementations,
629 % which we must match for the sake of some badly written
630 % font downloading code, that such entries are popped
631 % automatically.
632 count 1 index cvlit //.vmused
633 % Stack: key count litkey memused
634 {.LoadResource} 4 1 roll 4 .execn
635 % Stack: ... count key memused
636 //.vmused exch sub
637 1 index //.getvminstance exec not {
638 pop dup //.undefinedresource exec % didn't load
639 } if
640 dup 1 1 put
641 2 3 -1 roll put
642 % Stack: ... count key
643 exch count 1 sub exch sub {exch pop} repeat
644 } .internalbind
645 /.LoadResource
646 { dup .ResourceFile
647 { exch pop currentglobal
648 { //.runresource exec }
649 { //true setglobal { //.runresource exec } stopped //false setglobal { stop } if }
650 ifelse
651 }
652 { dup //.undefinedresource exec
653 }
654 ifelse
655 } .internalbind
656 /.ResourceFile
657 {
658 Category //.rfnstring cvs length % key l
659 dup //.rfnstring dup length 2 index sub % key l l (buf) L-l
660 3 2 roll exch getinterval % key l ()
661 .file_name_directory_separator exch copy length add % key l1
662 dup //.rfnstring dup length 2 index sub % key l1 l1 (buf) L-l
663 3 2 roll exch getinterval % key l1 ()
664 2 index exch cvs length add % key l2
665 //.rfnstring exch 0 exch getinterval % key (relative_path)
666 .libfile {
667 exch pop //true
668 } {
669 pop
670 currentdict /ResourceFileName known {
671 mark 1 index //.rfnstring { ResourceFileName } //.internalstopped exec {
672 cleartomark //false
673 } {
674 (r) { file } //.internalstopped exec {
675 cleartomark //false
676 } {
677 exch pop exch pop //true
678 } ifelse
679 } ifelse
680 } {
681 pop //false
682 } ifelse
683 } ifelse
684 } .internalbind
685
686 .dicttomark
687 /Category defineresource pop
688
689 % Fill in the rest of the Category category.
690 /Category /Category findresource dup
691 /Generic /Category findresource begin {
692 /FindResource /ResourceForAll /ResourceStatus /.ResourceFileStatus
693 /UndefineResource /ResourceFileName
694 /.ResourceFile /.LoadResource /.DoLoadResource
695 } { dup load put dup } forall
696 pop readonly pop end
697
698 (END GENERIC) VMDEBUG
699
700 % Define the fixed categories.
701
702 mark
703 % Non-Type categories with existing entries.
704 /ColorSpaceFamily
705 { } % These must be deferred, because optional features may add some.
706 /Emulator
707 mark EMULATORS { <00> search { exch pop cvn exch }{ cvn exit } ifelse } .forcebind loop //.packtomark exec
708 /Filter
709 { } % These must be deferred, because optional features may add some.
710 /IODevice
711 % Loop until the .getiodevice gets a rangecheck.
712 errordict /rangecheck 2 copy get
713 errordict /rangecheck { pop stop } put % pop the command
714 mark 0 { {
715 dup .getiodevice dup //null eq { pop } { exch } ifelse 1 add
716 } loop} //.internalstopped exec
717 pop pop pop //.packtomark exec
718 4 1 roll put
719 //.clearerror exec
720 % Type categories listed in the Red Book.
721 /ColorRenderingType
722 { } % These must be deferred, because optional features may add some.
723 /FMapType
724 { } % These must be deferred, because optional features may add some.
725 /FontType
726 { } % These must be deferred, because optional features may add some.
727 /FormType
728 { } % These must be deferred, because optional features may add some.
729 /HalftoneType
730 { } % These must be deferred, because optional features may add some.
731 /ImageType
732 { } % Deferred, optional features may add some.
733 /PatternType
734 { } % Deferred, optional features may add some.
735 % Type categories added since the Red Book.
736 /setsmoothness where {
737 pop /ShadingType { } % Deferred, optional features may add some.
738 } if
739 counttomark 2 idiv
740 { mark
741
742 % Standard entries
743
744 % We'd like to prohibit defineresource,
745 % but because optional features may add entries, we can't.
746 % We can at least require that the key and value match.
747 /DefineResource
748 { currentglobal not
749 { /defineresource cvx /invalidaccess signaloperror }
750 { 2 copy ne
751 { /defineresource cvx /rangecheck signaloperror }
752 { dup .Instances 4 -2 roll .growput }
753 ifelse
754 }
755 ifelse
756 } .internalbind
757 /UndefineResource
758 { /undefineresource cvx /invalidaccess signaloperror } .internalbind
759 /FindResource
760 { .Instances 1 index .knownget
761 { exch pop }
762 { /findresource cvx //.undefinedresource exec }
763 ifelse
764 } .internalbind
765 /ResourceStatus
766 { .Instances exch known { 0 0 //true } { //false } ifelse } .internalbind
767 /ResourceForAll
768 /Generic //.findcategory exec /ResourceForAll load end
769
770 % Additional entries
771
772 counttomark 2 add -1 roll
773 dup length dict dup begin exch { dup def } forall end
774 % We'd like to make the .Instances readonly here,
775 % but because optional features may add entries, we can't.
776 /.Instances exch
777 /.LocalInstances % used by ResourceForAll
778 0 dict def
779
780 .dicttomark /Category defineresource pop
781 } repeat pop
782
783 (END FIXED) VMDEBUG
784
785 % Define the other built-in categories.
786
787 /.definecategory % <name> -mark- <key1> ... <valuen> .definecategory -
788 { counttomark 2 idiv 2 add % .Instances, Category
789 /Generic /Category findresource dup maxlength 3 -1 roll add
790 dict .copydict begin
791 counttomark 2 idiv { def } repeat pop % pop the mark
792 currentdict end /Category defineresource pop
793 } .internalbind def
794
795 /ColorRendering mark /InstanceType /dicttype .definecategory
796 % ColorSpace is defined below
797 % Encoding is defined below
798 % Font is defined below
799 /Form mark /InstanceType /dicttype .definecategory
800 /Halftone mark /InstanceType /dicttype .definecategory
801 /Pattern mark /InstanceType /dicttype .definecategory
802 /ProcSet mark /InstanceType /dicttype .definecategory
803 % Added since the Red Book:
804 /ControlLanguage mark /InstanceType /dicttype .definecategory
805 /HWOptions mark /InstanceType /dicttype .definecategory
806 /Localization mark /InstanceType /dicttype .definecategory
807 /PDL mark /InstanceType /dicttype .definecategory
808 % CIDFont, CIDMap, and CMap are defined in gs_cidfn.ps
809 % FontSet is defined in gs_cff.ps
810 % IdiomSet is defined in gs_ll3.ps
811 % InkParams and TrapParams are defined in gs_trap.ps
812
813 (END MISC) VMDEBUG
814
815 % Define the OutputDevice category.
816 /OutputDevice mark
817 /InstanceType /dicttype
818 /.Instances mark
819 %% devicedict is not created yet so here we employ a technique similar to
820 %% that used to create it, in order to get the device names. We run a loop
821 %% executing .getdevice with incremental numbers until we get an error.
822 %% The devicedict creation only stops on a rangecheck, we stop on any error.
823 %% We need to use .internalstopped, not stopped or we get an invalidacces
824 %% later in this file. Instances of /OutputDevice are dictionaries, and the
825 %% only required key is a /PageSize. The array of 4 numbers are minimum to
826 %% maximum and are matches for the Adobe Acrobat Distiller values.
827 0
828 {
829 {dup .getdevice .devicename cvn 1 dict dup /PageSize [1 1 14400 14400] put [exch readonly 0 -1] 3 -1 roll 1 add} loop
830 } //.internalstopped exec pop
831 %% Remove the count, and the duplicate, from the stack
832 pop pop
833 .dicttomark
834 .definecategory
835
836 % Define the ColorSpace category.
837
838 /.defaultcsnames mark
839 /DefaultGray 0
840 /DefaultRGB 1
841 /DefaultCMYK 2
842 .dicttomark readonly def
843
844 % The "hooks" are no-ops here, redefined in LL3.
845 /.definedefaultcs { % <index> <value> .definedefaultcs -
846 pop pop
847 } .internalbind def
848 /.undefinedefaultcs { % <index> .undefinedefaultcs -
849 pop
850 } .internalbind def
851
852 /ColorSpace mark
853
854 /InstanceType /arraytype
855
856 % We keep track of whether there are any local definitions for any of
857 % the Default keys. This information must get saved and restored in
858 % parallel with the local instance dictionary, so it must be stored in
859 % local VM.
860 userdict /.localcsdefaults //false put
861
862 /DefineResource {
863 2 copy /Generic /Category findresource /DefineResource get exec
864 exch pop
865 exch //.defaultcsnames exch .knownget {
866 1 index //.definedefaultcs exec
867 currentglobal not { .userdict /.localcsdefaults //true put } if
868 } if
869 } .internalbind
870
871 /UndefineResource {
872 dup /Generic /Category findresource /UndefineResource get exec
873 //.defaultcsnames 1 index .knownget {
874 % Stack: resname index
875 currentglobal {
876 //.undefinedefaultcs exec pop
877 } {
878 % We removed the local definition, but there might be a global one.
879 exch .GetInstance {
880 0 get //.definedefaultcs exec
881 } {
882 //.undefinedefaultcs exec
883 } ifelse
884 % Recompute .localcsdefaults by scanning. This is rarely needed.
885 .userdict /.localcsdefaults //false //.defaultcsnames {
886 pop .LocalInstances exch known { pop //true exit } if
887 } forall put
888 } ifelse
889 } {
890 pop
891 } ifelse
892 } .internalbind
893
894 .definecategory % ColorSpace
895
896 % Define the Encoding category.
897
898 /Encoding mark
899
900 /InstanceType /arraytype
901
902 % Handle already-registered encodings, including lazily loaded encodings
903 % that aren't loaded yet.
904
905 /.Instances mark
906 EncodingDirectory
907 {dup length 256 eq { [ exch readonly 0 -1 ] } { pop [//null 2 -1] } ifelse
908 } forall
909 .dicttomark
910
911 /.ResourceFileDict mark
912 EncodingDirectory
913 { dup length 256 eq { pop pop } { 0 get } ifelse
914 } forall
915 .dicttomark
916
917 /ResourceFileName
918 { .ResourceFileDict 2 index .knownget
919 { exch copy exch pop }
920 { /Generic /Category findresource /ResourceFileName get exec }
921 ifelse
922 } .internalbind
923
924 .definecategory % Encoding
925
926 % Make placeholders in level2dict for the redefined Encoding operators,
927 % so that they will be swapped properly when we switch language levels.
928
929 /.findencoding /.findencoding load def
930 /findencoding /findencoding load def
931 /.defineencoding /.defineencoding load def
932
933 (END ENCODING) VMDEBUG
934
935 % Define the Font category.
936
937 /.fontstatusaux { % <fontname> .fontstatusaux <fontname> <found>
938 { % Create a loop context just so we can exit it early.
939 % Check Fontmap.
940 Fontmap 1 index .knownget
941 { //true }
942 { .nativeFontmap 1 index .knownget } ifelse
943
944 {
945 {
946 dup type /nametype eq {
947 .fontstatus { pop //null exit } if
948 } {
949 dup type /dicttype eq {/Path .knownget pop} if
950 dup type /stringtype eq {
951 findlibfile { closefile pop //null exit } if pop
952 } {
953 % Procedure, assume success.
954 pop //null exit
955 } ifelse
956 } ifelse
957 } forall dup //null eq { pop //true exit } if
958 } if
959
960
961 dup / eq { //false exit } if % / throws an error from findlibfile
962 % Convert names to strings; give up on other types.
963 dup type /nametype eq { .namestring } if
964 dup type /stringtype ne { //false exit } if
965 % Check the resource directory.
966 dup //.fonttempstring /FontResourceDir getsystemparam .genericrfn
967 status {
968 pop pop pop pop //true exit
969 } if
970 % Check for a file on the search path with the same name
971 % as the font.
972 findlibfile { closefile //true exit } if
973 % Scan a FONTPATH directory and try again.
974 //.scannextfontdir exec not { //false exit } if
975 } loop
976 } .internalbind def
977
978 /.fontstatus { % <fontname> .fontstatus <fontname> <found>
979 //.fontstatusaux exec
980 { //true }
981 {
982 .buildnativefontmap
983 { //.fontstatusaux exec }
984 { //false } ifelse
985 } ifelse
986 } .internalbind def
987 currentdict /.fontstatusaux .undef
988
989 /Font mark
990
991 /InstanceType /dicttype
992
993 /DefineResource
994 { 2 copy //definefont exch pop
995 /Generic /Category findresource /DefineResource get exec
996 } .internalbind
997 /UndefineResource
998 { dup //undefinefont
999 /Generic /Category findresource /UndefineResource get exec
1000 } .internalbind
1001 /FindResource {
1002 dup //.getvminstance exec {
1003 exch pop 0 get
1004 } {
1005 dup ResourceStatus {
1006 pop 1 gt { .loadfontresource } { .GetInstance pop 0 get } ifelse
1007 } {
1008 .loadfontresource
1009 } ifelse
1010 } ifelse
1011 } .internalbind
1012 /ResourceForAll {
1013 { //.scannextfontdir exec not { exit } if } loop
1014 /Generic /Category findresource /ResourceForAll get exec
1015 } .forcebind
1016 /.ResourceFileStatus {
1017 .fontstatus { pop 2 -1 //true } { pop //false } ifelse
1018 } .internalbind
1019
1020 /.loadfontresource {
1021 dup //.vmused exch
1022 % Hack: rebind .currentresourcefile so that all calls of
1023 % definefont will know these are built-in fonts.
1024 currentfile {pop //findfont exec} .execasresource % (findfont is a procedure)
1025 exch //.vmused exch sub
1026 % stack: name font vmused
1027 % findfont has the prerogative of not calling definefont
1028 % in certain obscure cases of font substitution.
1029 2 index //.getvminstance exec {
1030 dup 1 1 put
1031 2 3 -1 roll put
1032 } {
1033 pop
1034 } ifelse exch pop
1035 } systemdict /CETMODE .knownget not {//false} if {.bind}{.internalbind}ifelse
1036
1037 /.Instances FontDirectory length 2 mul dict
1038
1039 .definecategory % Font
1040
1041 % Redefine font "operators".
1042 /.definefontmap
1043 { /Font /Category findresource /.Instances get
1044 dup 3 index known
1045 { pop
1046 }
1047 { 2 index
1048 % Make sure we create the array in global VM.
1049 .currentglobal //true .setglobal
1050 [//null 2 -1] exch .setglobal
1051 .growput
1052 }
1053 ifelse
1054 //.definefontmap exec
1055 } .internalbind def
1056
1057 /definefont {
1058 { /Font defineresource } stopped {
1059 /definefont cvx $error /errorname get signalerror
1060 } if
1061 } .internalbind odef
1062 /undefinefont {
1063 /Font undefineresource
1064 } .internalbind odef
1065 % The Red Book requires that findfont be a procedure, not an operator,
1066 % but it still needs to restore the stacks reliably if it fails.
1067 /.findfontop {
1068 { /Font findresource } stopped {
1069 pop /findfont cvx $error /errorname get signalerror
1070 } if
1071 } .internalbind odef
1072 /findfont {
1073 .findfontop
1074 } .internalbind def % Must be a procedure, not an operator
1075
1076 % Remove initialization utilities.
1077 currentdict /.definecategory .undef
1078 currentdict /.emptydict .undef
1079
1080 end % level2dict
1081
1082 % Convert deferred resources after we finally switch to Level 2.
1083
1084 /.fixresources {
1085 % Encoding resources
1086 EncodingDirectory
1087 { dup length 256 eq
1088 { /Encoding defineresource pop }
1089 { pop pop }
1090 ifelse
1091 } forall
1092 /.findencoding {
1093 { /Encoding findresource } stopped {
1094 pop /findencoding cvx $error /errorname get signalerror
1095 } if
1096 } .internalbind def
1097 /findencoding /.findencoding load def % must be a procedure
1098 /.defineencoding { /Encoding defineresource pop } .internalbind def
1099 % ColorRendering resources and ProcSet
1100 systemdict /ColorRendering .knownget {
1101 /ColorRendering exch /ProcSet defineresource pop
1102 systemdict /ColorRendering undef
1103 /DefaultColorRendering currentcolorrendering /ColorRendering defineresource pop
1104 } if
1105 % ColorSpace resources
1106 systemdict /CIEsRGB .knownget {
1107 /sRGB exch /ColorSpace defineresource pop
1108 systemdict /CIEsRGB undef
1109 } if
1110 systemdict /CIEsRGBICC .knownget {
1111 /sRGBICC exch /ColorSpace defineresource pop
1112 systemdict /CIEsRGBICC undef
1113 } if
1114 systemdict /CIEsGRAYICC .knownget {
1115 /sGrayICC exch /ColorSpace defineresource pop
1116 systemdict /CIEsGRAYICC undef
1117 } if
1118 systemdict /CIEesRGBICC .knownget {
1119 /esRGBICC exch /ColorSpace defineresource pop
1120 systemdict /CIEesRGBICC undef
1121 } if
1122 systemdict /CIErommRGBICC .knownget {
1123 /rommRGBICC exch /ColorSpace defineresource pop
1124 systemdict /CIErommRGBICC undef
1125 } if
1126 % ColorSpaceFamily resources
1127 colorspacedict { pop dup /ColorSpaceFamily defineresource pop } forall
1128 % Filter resources
1129 filterdict { pop dup /Filter defineresource pop } forall
1130 % FontType and FMapType resources
1131 buildfontdict { pop dup /FontType defineresource pop } forall
1132 mark
1133 buildfontdict 0 known { 2 3 4 5 6 7 8 } if
1134 buildfontdict 9 known { 9 } if
1135 counttomark { dup /FMapType defineresource pop } repeat pop
1136 % FormType resources
1137 .formtypes { pop dup /FormType defineresource pop } forall
1138 % HalftoneType resources
1139 .halftonetypes { pop dup /HalftoneType defineresource pop } forall
1140 % ColorRenderingType resources
1141 .colorrenderingtypes {pop dup /ColorRenderingType defineresource pop} forall
1142 % ImageType resources
1143 .imagetypes { pop dup /ImageType defineresource pop } forall
1144 % PatternType resources
1145 .patterntypes { pop dup /PatternType defineresource pop } forall
1146 % Make the fixed resource categories immutable.
1147 /.shadingtypes where {
1148 pop .shadingtypes { pop dup /ShadingType defineresource pop } forall
1149 } if
1150 [ /ColorSpaceFamily /Emulator /Filter /IODevice /ColorRenderingType
1151 /FMapType /FontType /FormType /HalftoneType /ImageType /PatternType
1152 /.shadingtypes where { pop /ShadingType } if
1153 ] {
1154 /Category findresource
1155 dup /.Instances get readonly pop
1156 .LocalInstances readonly pop
1157 readonly pop
1158 } forall
1159 % clean up
1160 systemdict /.fixresources undef
1161 } .internalbind def
1162
1163 %% Replace 1 (gs_resmp.ps)
1164 (gs_resmp.ps) dup runlibfile VMDEBUG
1165
1166 [
1167 /.default_resource_dir
1168 /.resource_dir_name
1169 /.fonttempstring /.scannextfontdir % from gs_fonts.ps
1170 ] systemdict .undefinternalnames
1171
1172 [
1173 /.definedefaultcs
1174 /.undefinedefaultcs
1175 /.defaultcsnames
1176 /.enumerateresource
1177 /.externalresourceforall
1178 /.getvminstance
1179 /.globalresourceforall
1180 /.localresourceforall
1181 /resourceforall1
1182 /.resourceexec
1183 /.undefinedresource
1184 /.vmused
1185 ] dup level2dict .undefinternalnames
1186 systemdict .undefinternalnames