summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlgs/Resource/Init/gs_res.ps
blob: 0f8cb8f6cd1125f407fe65579bc535c6adaff076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
% Copyright (C) 2001-2019 Artifex Software, Inc.
% All Rights Reserved.
%
% This software is provided AS-IS with no warranty, either express or
% implied.
%
% This software is distributed under license and may not be copied,
% modified or distributed except as expressly authorized under the terms
% of the license contained in the file LICENSE in this distribution.
%
% Refer to licensing information at http://www.artifex.com or contact
% Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
% CA 94945, U.S.A., +1(415)492-9861, for further information.
%

% Initialization file for Level 2 resource machinery.
% When this is run, systemdict is still writable,
% but (almost) everything defined here goes into level2dict.

level2dict begin

(BEGIN RESOURCES) VMDEBUG

% We keep track of (global) instances with another entry in the resource
% dictionary, an .Instances dictionary.  For categories with implicit
% instances, the values in .Instances are the same as the keys;
% for other categories, the values are [instance status size].

% Note that the dictionary that defines a resource category is stored
% in global VM.  The PostScript manual says that each category must
% manage global and local instances separately.  However, objects in
% global VM other than systemdict can't reference objects in local VM.
% This means that the resource category dictionary, which would otherwise be
% the obvious place to keep track of the instances, can't be used to keep
% track of local instances.  Instead, we define a dictionary in local VM
% called localinstancedict, in which the key is the category name and
% the value is the analogue of .Instances for local instances.

% We don't currently implement automatic resource unloading.
% When and if we do, it should be hooked to the garbage collector.
% However, Ed Taft of Adobe says their interpreters don't implement this
% either, so we aren't going to worry about it for a while.

currentglobal //false setglobal
  systemdict /localinstancedict 5 dict
  .forceput	% localinstancedict is local, systemdict is global
//true setglobal
/.emptydict 0 dict readonly def
setglobal

% Resource category dictionaries have the following keys (those marked with
% * are optional):
%	Standard, defined in the Red Book:
%		Category (name)
%		*InstanceType (name)
%		DefineResource
%			<key> <instance> DefineResource <instance>
%		UndefineResource
%			<key> UndefineResource -
%		FindResource
%			<key> FindResource <instance>
%		ResourceStatus
%			<key> ResourceStatus <status> <size> true
%			<key> ResourceStatus false
%		ResourceForAll
%			<template> <proc> <scratch> ResourceForAll -
%		*ResourceFileName
%			<key> <scratch> ResourceFileName <filename>
%	Additional, specific to our implementation:
%		.Instances (dictionary)
%		.LocalInstances
%			- .LocalInstances <dict>
%		.GetInstance
%			<key> .GetInstance <instance> -true-
%			<key> .GetInstance -false-
%		.CheckResource
%			<key> <value> .CheckResource <key> <value> <ok>
%			  (or may give an error if not OK)
%		.DoLoadResource
%			<key> .DoLoadResource <key> (may give an error)
%		.LoadResource
%			<key> .LoadResource - (may give an error)
%		.ResourceFile
%			<key> .ResourceFile <file> -true-
%			<key> .ResourceFile <key> -false-
%		.ResourceFileStatus
%			<key> .ResourceFileStatus 2 <vmusage> -true-
%			<key> .ResourceFileStatus -false-
% All the above procedures expect that the top dictionary on the d-stack
% is the resource dictionary.

% Define enough of the Category category so we can define other categories.
% The dictionary we're about to create will become the Category
% category definition dictionary.

% .findcategory and .resourceexec are only called from within the
% implementation of the resource 'operators', so they don't have to worry
% about cleaning up the stack if they fail (the interpreter's stack
% protection machinery for pseudo-operators takes care of this).
% Note that all places that look up categories must use .findcategory
% so that the command in case of error will be correct rather than an
% internal invocation of findresource.
/.findcategory {	% <name> .findcategory -
                        %   (pushes the category on the dstack)
    /Category .findresource begin	% note: *not* findresource
} bind def

% If an error occurs within the logic of a resource operator (after operand
% acquisition and checking), the Adobe interpreters report the operator name,
% not the operator object, as the command in $error.  For this reason, and
% this reason only, all resource operators must wrap their logic code in
%	/<opername> cvx { ...logic... } .errorexec

% The Category resource signals /undefined rather than /undefinedresource,
% both when referenced implicitly (to look up the category for a general
% resource operation) and when it is accessed directly (/Category /xxx
% findresource).  Because of this, all resource operators must use
% .undefinedresource rather than signalling undefinedresource directly.
/.undefinedresource {	% <command> .undefinedresource -
    /Category dup load eq { /undefined } { /undefinedresource } ifelse
    signaloperror
} bind def

/.resourceexec {	% <key> /xxxResource .resourceexec -
                        %   (also pops the category from the dstack)
  load exec end
} bind def

% .getvminstance treats instances on disk as undefined.
/.getvminstance {	% <key> .getvminstance <instance> -true-
                        % <key> .getvminstance -false-
  .GetInstance {
    dup 1 get 2 ne { //true } { pop //false } ifelse
  } {
    //false
  } ifelse
} bind def

20 dict begin

                % Standard entries

/Category /Category def
/InstanceType /dicttype def

/DefineResource {
        .CheckResource {
          dup /Category 3 index cvlit .growput
          dup [ exch 0 -1 ] exch
          .Instances 4 2 roll put
                % Make the Category dictionary read-only.  We will have to
                % use .forceput / .forceput later to replace the dummy,
                % empty .Instances dictionary with the real one later.
          readonly
        }{
          /defineresource cvx /typecheck signaloperror
        } ifelse
} bind executeonly odef
/FindResource		% (redefined below)
        { .Instances exch get 0 get
        } bind executeonly def

                % Additional entries

/.Instances 30 dict def
.Instances /Category [currentdict 0 -1] put

/.LocalInstances 0 dict def
/.GetInstance
        { .Instances exch .knownget
        } bind def
/.CheckResource
        { dup gcheck currentglobal and
           { /DefineResource /FindResource /ResourceForAll /ResourceStatus
             /UndefineResource }
           { 2 index exch known and }
          forall
          not { /defineresource cvx /invalidaccess signaloperror } if
          //true
        } bind def

.Instances end begin	% for the base case of findresource

(END CATEGORY) VMDEBUG

% Define the resource operators.  We use the "stack protection" feature of
% odef to make sure the stacks are restored properly on an error.
% This requires that the operators not pop anything from the stack until
% they have executed their logic successfully.  We can't make this
% work for resourceforall, because the procedure it executes mustn't see
% the operands of resourceforall on the stack, but we can make it work for
% the others.

% findresource is the only operator that needs to bind //Category.
% We define its contents as a separate procedure so that .findcategory
% can use it without entering another level of pseudo-operator.
/.findresource {		% <key> <category> findresource <instance>
        2 copy dup /Category eq
          { pop //Category 0 get begin } { //.findcategory exec } ifelse
        /FindResource //.resourceexec exec exch pop exch pop
} bind
end		% .Instances of Category
def
/findresource {
    % See above re .errorexec.
    1 .argindex 	% also catch stackunderflow
    dup type /stringtype eq { cvn } if  % for CET 23-13-04
    3 1 roll exch pop
    dup type /nametype ne {
      /findresource .systemvar /typecheck signalerror
    } if
    /findresource cvx //.findresource .errorexec
} bind executeonly odef

/defineresource {	% <key> <instance> <category> defineresource <instance>
    2 .argindex 2 index 2 index	% catch stackunderflow
    % See above re .errorexec.
    /defineresource cvx {
        //.findcategory exec
        currentdict /InstanceType known {
          dup type InstanceType ne {
            dup type /packedarraytype eq InstanceType /arraytype eq and
            not { /defineresource cvx /typecheck signaloperror } if
          } if
        } if
        /DefineResource //.resourceexec exec
        4 1 roll pop pop pop
    } .errorexec
} bind executeonly odef
% We must prevent resourceforall from automatically restoring the stacks,
% because we don't want the stacks restored if proc causes an error or
% executes a 'stop'. On the other hand, resourceforall is defined in the
% PLRM as an operator, so it must have type /operatortype.  We hack this
% by taking advantage of the fact that the interpreter optimizes tail
% calls, so stack protection doesn't apply to the very last token of an
% operator procedure.
/resourceforall1 {	% <template> <proc> <scratch> <category> resourceforall1 -
        dup //.findcategory exec
        /ResourceForAll load
        % Stack: <template> <proc> <scratch> <category> proc
        exch pop		% pop the category
        exec end
} bind executeonly def
/resourceforall {	% <template> <proc> <scratch> <category> resourceforall1 -
        //resourceforall1 exec		% see above
} bind executeonly odef
/resourcestatus {	% <key> <category> resourcestatus <status> <size> true
                        % <key> <category> resourcestatus false
  {
    0 .argindex type /nametype ne {
      % CET 23-26 wants typecheck here, not undefineresource that happens
      % without the check.
      /resourcestatus cvx /typecheck signalerror
    } if
    2 copy //.findcategory exec /ResourceStatus //.resourceexec exec
    { 4 2 roll pop pop //true } { pop pop //false } ifelse
  } stopped {
    % Although resourcestatus is an operator, Adobe uses executable name
    % for error reporting. CET 23-26
    /resourcestatus cvx $error /errorname get signalerror
  } if
} bind executeonly odef
/undefineresource {	% <key> <category> undefineresource -
  0 .argindex type /nametype ne {
    /undefinedresource cvx /typecheck signaloperror
  } if
  1 .argindex 1 index		% catch stackunderflow

  { //.findcategory exec /UndefineResource //.resourceexec exec pop pop
  } stopped {
    % Although undefineresource is an operator, Adobe uses executable name
    % here but uses operator for the errors above. CET 23-33
    /undefineresource cvx $error /errorname get signalerror
  } if
} bind executeonly odef

% Define the system parameters used for the Generic implementation of
% ResourceFileName.
systemdict begin

%     - .default_resource_dir <string>
/.default_resource_dir {
  /LIBPATH .systemvar {
    dup .file_name_current eq {
      pop
    } {
      (Resource) search {
        exch concatstrings
        exch pop
        .file_name_separator concatstrings exit
      } {
        pop
      } ifelse
    } ifelse
  } forall
} bind def

%  <path> <name> <string> .resource_dir_name <path> <name> <string>
/.resource_dir_name
{  systemdict 2 index .knownget {
     exch pop
     systemdict 1 index undef
   } {
     dup () ne {
     .file_name_directory_separator concatstrings
    } if
    2 index exch //false
    .file_name_combine not {
      (Error: .default_resource_dir returned ) print exch print ( that can't combine with ) print =
      /.default_resource_dir cvx /configurationerror signalerror
    } if
  } ifelse
} bind def

currentdict /pssystemparams known not {
  /pssystemparams 10 dict readonly def
} if
pssystemparams begin
  //.default_resource_dir exec
  /FontResourceDir (Font) //.resource_dir_name exec
     readonly currentdict 3 1 roll .forceput	% pssys'params is r-o
  /GenericResourceDir () //.resource_dir_name exec
     readonly currentdict 3 1 roll .forceput	% pssys'params is r-o
  pop % .default_resource_dir
  /GenericResourcePathSep
        .file_name_separator readonly currentdict 3 1 roll .forceput		% pssys'params is r-o
  currentdict (%diskFontResourceDir) cvn (/Resource/Font/) readonly .forceput	% pssys'params is r-o
  currentdict (%diskGenericResourceDir) cvn (/Resource/) readonly .forceput	% pssys'params is r-o
end
end

% Check if GenericResourceDir presents in LIBPATH.

% The value of GenericResourceDir must end with directory separator.
% We use .file_name_combine to check it.
% Comments use OpenVMS syntax, because it is the most complicated case.
(x) pssystemparams /GenericResourcePathSep get
(y) concatstrings concatstrings dup length              % (x]y) l1
pssystemparams /GenericResourceDir get dup length exch  % (x]y) l1 l2 (dir)
3 index //true .file_name_combine not {
  exch
  (File name ) print print ( cant combine with ) print =
  /GenericResourceDir cvx /configurationerror signaloperror
} if
dup length                                              % (x]y) l1 l2 (dir.x]y) l
4 2 roll add                                            % (x]y) (dir.x]y) l ll
ne {
  (GenericResourceDir value does not end with directory separator.\n) =
  /GenericResourceDir cvx /configurationerror signaloperror
} if
pop pop

pssystemparams dup /GenericResourceDir get exch /GenericResourcePathSep get
(Init) exch (gs_init.ps) concatstrings concatstrings concatstrings
status {
  pop pop pop pop
} {
  (\n*** Warning: GenericResourceDir doesn't point to a valid resource directory.) =
  (               the -sGenericResourceDir=... option can be used to set this.\n) =
  flush
} ifelse

% Define the generic algorithm for computing resource file names.
/.rfnstring 8192 string def
/.genericrfn		% <key> <scratch> <prefix> .genericrfn <filename>
 { 3 -1 roll //.rfnstring cvs concatstrings exch copy
 } bind def

% Define the Generic category.

/Generic mark

                % Standard entries

% We're still running in Level 1 mode, so dictionaries won't expand.
% Leave room for the /Category entry.
/Category //null

% Implement the body of Generic resourceforall for local, global, and
% external cases.  'args' is [template proc scratch resdict].
/.enumerateresource {	% <key> [- <proc> <scratch>] .enumerateresource -
  1 index type dup /stringtype eq exch /nametype eq or {
    exch 1 index 2 get cvs exch
  } if
        % Use .setstackprotect to prevent the stacks from being restored if
        % an error occurs during execution of proc.
  1 get //false .setstackprotect exec //true .setstackprotect
} bind def
/.localresourceforall {		% <key> <value> <args> .localr'forall -
  exch pop
  2 copy 0 get .stringmatch { //.enumerateresource exec } { pop pop } ifelse
} bind def
/.globalresourceforall {	% <key> <value> <args> .globalr'forall -
  exch pop
  2 copy 0 get .stringmatch {
    dup 3 get begin .LocalInstances end 2 index known not {
      //.enumerateresource exec
    } {
      pop pop
    } ifelse
  } {
    pop pop
  } ifelse
} bind def
/.externalresourceforall {	% <filename> <len> <args> .externalr'forall -
  3 1 roll 1 index length 1 index sub getinterval exch
  dup 3 get begin .Instances .LocalInstances end
                % Stack: key args insts localinsts
  3 index known {
    pop pop pop
  } {
    2 index known { pop pop } { //.enumerateresource exec } ifelse
  } ifelse
} bind def

/DefineResource dup {
        .CheckResource
           { dup [ exch 0 -1 ]
                        % Stack: key value instance
             currentglobal
              { //false setglobal 2 index UndefineResource	% remove local def if any
                //true setglobal
                .Instances dup //.emptydict eq {
                  pop 3 dict
                        % As noted above, Category dictionaries are read-only,
                        % so we have to use .forceput here.
                  currentdict /.Instances 2 index .forceput	% Category dict is read-only
                } executeonly if
              } executeonly
              { .LocalInstances dup //.emptydict eq
                 { pop 3 dict localinstancedict Category 2 index put
                 }
                if
              }
             ifelse
                        % Stack: key value instance instancedict
             3 index 2 index .growput
                        % Now make the resource value read-only.
             0 2 copy get { readonly } //.internalstopped exec pop
             dup 4 1 roll put exch pop exch pop
           } executeonly
           { /defineresource cvx /typecheck signaloperror
           }
        ifelse
} .bind executeonly .makeoperator		% executeonly to prevent access to .forceput
/UndefineResource
        {  { dup 2 index .knownget
              { dup 1 get 1 ge
                 { dup 0 //null put 1 2 put pop pop }
                 { pop exch .undef }
                ifelse
              }
              { pop pop
              }
             ifelse
           }
          currentglobal
           { 2 copy .Instances exch exec
           }
          if .LocalInstances exch exec
        } bind executeonly
% Because of some badly designed code in Adobe's CID font downloader that
% makes findresource and resourcestatus deliberately inconsistent with each
% other, the default FindResource must not call ResourceStatus if there is
% an instance of the desired name already defined in VM.
/FindResource {
        dup //null eq {
          % CET 13-06 wants /typecheck for "null findencoding" but
          % .knownget doesn't fail on null
          /findresource cvx /typecheck signaloperror
        } if
        dup //.getvminstance exec {
          exch pop 0 get
        } {
          dup ResourceStatus {
            pop 1 gt {
              .DoLoadResource //.getvminstance exec not {
                /findresource cvx //.undefinedresource exec
              } if 0 get
            } {
              .GetInstance pop 0 get
            } ifelse
          } {
           /findresource cvx //.undefinedresource exec
          } ifelse
        } ifelse
} bind executeonly
% Because of some badly designed code in Adobe's CID font downloader, the
% definition of ResourceStatus for Generic and Font must be the same (!).
% We patch around this by using an intermediate .ResourceFileStatus procedure.
/ResourceStatus {
        dup .GetInstance {
          exch pop dup 1 get exch 2 get //true
        } {
          .ResourceFileStatus
        } ifelse
} bind executeonly
/.ResourceFileStatus {
        .ResourceFile { closefile 2 -1 //true } { pop //false } ifelse
} bind executeonly
/ResourceForAll {
                % Construct a new procedure to hold the arguments.
                % All objects constructed here must be in local VM to avoid
                % a possible invalidaccess.
        currentdict 4 .localvmpackedarray	% [template proc scratch resdict]
                % We must pop the resource dictionary off the dict stack
                % when doing the actual iteration, and restore it afterwards.
        .currentglobal not {
          .LocalInstances length 0 ne {
                % We must do local instances, and do them first.
            //.localresourceforall {exec} 0 get 3 .localvmpackedarray cvx
            .LocalInstances exch {forall} 0 get 1 index 0 get
            currentdict end 3 .execn begin
          } if
        } if
                % Do global instances next.
        //.globalresourceforall {exec} 0 get 3 .localvmpackedarray cvx
        .Instances exch cvx {forall} 0 get 1 index 0 get
        currentdict end 3 .execn begin
        mark                                             % args [
        Category .namestring .file_name_separator concatstrings
        2 index 0 get                                    % args [ (c/) (t)
        1 index length 3 1 roll                          % args [ l (c/) (t)
        concatstrings                                    % args [ l (c/t)
        [
          //true /LIBPATH .systemvar 3 index
          .generate_dir_list_templates_with_length       % args (t) [ l [(pt) Lp ...]
            % also add on the Resources as specified by the GenericResourceDir
          //true [ currentsystemparams /GenericResourceDir get]
          counttomark 1 add index .generate_dir_list_templates_with_length
          ] exch pop
        dup length 1 sub 0 exch 2 exch {                 % args [ l [] i
          2 copy get                                     % args [ l [] i (pt)
          exch 2 index exch 1 add get                    % args [ l [] (pt) Lp
          3 index add
          exch                                           % args [ l [] Lp (pt)

          {                                              % args [ l [] Lp (pf)
            dup length                                   % args [ l [] Lp (pf) Lpf
            2 index sub                                  % args [ l [] Lp (pf) Lf
            2 index exch                                 % args [ l [] Lp (pf) Lp Lf
            getinterval cvn dup                          % args [ l [] Lp /n /n
            5 2 roll                                     % args [ /n /n l [] Lp
          } //.rfnstring filenameforall
          pop                                            % args [ /n1 /n1 ... /nN /nN l []
        } for                                            % args [ /n1 /n1 ... /nN /nN l []
        pop pop
        .dicttomark % An easy way to exclude duplicates. % args <</n/n>>
          % {
          { pop } 0 get
          2 index 2 get { cvs 0 } aload pop 5 index
          //.externalresourceforall {exec} 0 get
          % }
        7 .localvmpackedarray cvx
        3 2 roll pop % args
        { forall } 0 get
        currentdict end 2 .execn begin
} bind executeonly

/ResourceFileName  {                          % /in (scr) --> (p/c/n)
  exch //.rfnstring cvs                       % (scr) (n)
  /GenericResourcePathSep getsystemparam exch % (scr) (/) (n)
  Category .namestring                        % (scr) (/) (n) (c)
  3 1 roll                                    % (scr) (c) (/) (n)
  concatstrings concatstrings                 % (scr) (c/n)
  /GenericResourceDir getsystemparam 1 index  % (scr) (c/n) (p/) (c/n)
  concatstrings                               % (scr) (c/n) (p/c/n)
  dup status {
    pop pop pop pop exch pop                  % (scr) (p/c/n)
  } {
    exch
    .libfile
    {//true}
    {
      pop dup .libfile
      {//true}
      {//false}
      ifelse
    } ifelse

    {
      dup .filename pop
      exch closefile
      exch pop
    }
    {pop}
    ifelse
  } ifelse
  exch copy                                   % (p/c/n)
} bind executeonly

                % Additional entries

% Unfortunately, we can't create the real .Instances dictionary now,
% because if someone copies the Generic category (which pp. 95-96 of the
% 2nd Edition Red Book says is legitimate), they'll wind up sharing
% the .Instances.  Instead, we have to create .Instances on demand,
% just like the entry in localinstancedict.
% We also have to prevent anyone from creating instances of Generic itself.
/.Instances //.emptydict

/.LocalInstances
        { localinstancedict Category .knownget not { //.emptydict } if
        } bind
/.GetInstance
        { currentglobal
           { .Instances exch .knownget }
           { .LocalInstances 1 index .knownget
              { exch pop //true }
              { .Instances exch .knownget }
             ifelse
           }
          ifelse
        } bind
/.CheckResource
        { //true
        } bind
/.vmused {
                % - .vmused <usedvalue>
                % usedvalue = vmstatus in global + vmstatus in local.
  0 2 {
    .currentglobal not .setglobal
    vmstatus pop exch pop add
  } repeat
} bind executeonly odef
/.DoLoadResource {
                % .LoadResource may push entries on the operand stack.
                % It is an undocumented feature of Adobe implementations,
                % which we must match for the sake of some badly written
                % font downloading code, that such entries are popped
                % automatically.
        count 1 index cvlit //.vmused
                % Stack: key count litkey memused
        {.LoadResource} 4 1 roll 4 .execn
                % Stack: ... count key memused
        //.vmused exch sub
        1 index //.getvminstance exec not {
          pop dup //.undefinedresource exec	% didn't load
        } if
        dup 1 1 put
        2 3 -1 roll put
                % Stack: ... count key
        exch count 1 sub exch sub {exch pop} repeat
} bind
/.LoadResource
        { dup .ResourceFile
           { exch pop currentglobal
              { //.runresource exec }
              { //true setglobal { //.runresource exec } stopped //false setglobal { stop } if }
             ifelse
           }
           { dup //.undefinedresource exec
           }
         ifelse
        } bind
/.ResourceFile
        {
          Category //.rfnstring cvs length                      % key l
          dup //.rfnstring dup length 2 index sub               % key l l (buf) L-l
          3 2 roll exch getinterval                             % key l ()
          .file_name_directory_separator exch copy length add   % key l1
          dup //.rfnstring dup length 2 index sub               % key l1 l1 (buf) L-l
          3 2 roll exch getinterval                             % key l1 ()
          2 index exch cvs length add                           % key l2
          //.rfnstring exch 0 exch getinterval                  % key (relative_path)
          .libfile {
            exch pop //true
          } {
            pop
            currentdict /ResourceFileName known {
              mark 1 index //.rfnstring { ResourceFileName } //.internalstopped exec {
                cleartomark //false
              } {
                (r) { file } //.internalstopped exec {
                  cleartomark //false
                } {
                  exch pop exch pop //true
                } ifelse
              } ifelse
             } {
               pop //false
             } ifelse
          } ifelse
        } bind

.dicttomark
/Category defineresource pop

% Fill in the rest of the Category category.
/Category /Category findresource dup
/Generic /Category findresource begin {
  /FindResource /ResourceForAll /ResourceStatus /.ResourceFileStatus
  /UndefineResource /ResourceFileName
  /.ResourceFile /.LoadResource /.DoLoadResource
} { dup load put dup } forall
pop readonly pop end

(END GENERIC) VMDEBUG

% Define the fixed categories.

mark
        % Non-Type categories with existing entries.
 /ColorSpaceFamily
   { }	% These must be deferred, because optional features may add some.
 /Emulator
   mark EMULATORS { <00> search { exch pop cvn exch }{ cvn exit } ifelse } .bind loop //.packtomark exec
 /Filter
   { }	% These must be deferred, because optional features may add some.
 /IODevice
        % Loop until the .getiodevice gets a rangecheck.
   errordict /rangecheck 2 copy get
   errordict /rangecheck { pop stop } put	% pop the command
   mark 0 { {
    dup .getiodevice dup //null eq { pop } { exch } ifelse 1 add
   } loop} //.internalstopped exec
   pop pop pop //.packtomark exec
   4 1 roll put
   //.clearerror exec
        % Type categories listed in the Red Book.
 /ColorRenderingType
   { }	% These must be deferred, because optional features may add some.
 /FMapType
   { }	% These must be deferred, because optional features may add some.
 /FontType
   { }	% These must be deferred, because optional features may add some.
 /FormType
   { }	% These must be deferred, because optional features may add some.
 /HalftoneType
   { }	% These must be deferred, because optional features may add some.
 /ImageType
   { }	% Deferred, optional features may add some.
 /PatternType
   { }  % Deferred, optional features may add some.
        % Type categories added since the Red Book.
 /setsmoothness where {
   pop /ShadingType { }	% Deferred, optional features may add some.
 } if
counttomark 2 idiv
 { mark

                % Standard entries

                % We'd like to prohibit defineresource,
                % but because optional features may add entries, we can't.
                % We can at least require that the key and value match.
   /DefineResource
        { currentglobal not
           { /defineresource cvx /invalidaccess signaloperror }
           { 2 copy ne
              { /defineresource cvx /rangecheck signaloperror }
              { dup .Instances 4 -2 roll .growput }
             ifelse
           }
          ifelse
        } bind executeonly
   /UndefineResource
        { /undefineresource cvx /invalidaccess signaloperror } bind executeonly
   /FindResource
        { .Instances 1 index .knownget
           { exch pop }
           { /findresource cvx //.undefinedresource exec }
          ifelse
        } bind executeonly
   /ResourceStatus
        { .Instances exch known { 0 0 //true } { //false } ifelse } bind executeonly
   /ResourceForAll
        /Generic //.findcategory exec /ResourceForAll load end

                % Additional entries

   counttomark 2 add -1 roll
   dup length dict dup begin exch { dup def } forall end
                % We'd like to make the .Instances readonly here,
                % but because optional features may add entries, we can't.
   /.Instances exch
   /.LocalInstances	% used by ResourceForAll
        0 dict def

   .dicttomark /Category defineresource pop
 } repeat pop

(END FIXED) VMDEBUG

% Define the other built-in categories.

/.definecategory	% <name> -mark- <key1> ... <valuen> .definecategory -
 { counttomark 2 idiv 2 add		% .Instances, Category
   /Generic /Category findresource dup maxlength 3 -1 roll add
   dict .copydict begin
   counttomark 2 idiv { def } repeat pop	% pop the mark
   currentdict end /Category defineresource pop
 } bind def

/ColorRendering mark /InstanceType /dicttype .definecategory
% ColorSpace is defined below
% Encoding is defined below
% Font is defined below
/Form mark /InstanceType /dicttype .definecategory
/Halftone mark /InstanceType /dicttype .definecategory
/Pattern mark /InstanceType /dicttype .definecategory
/ProcSet mark /InstanceType /dicttype .definecategory
% Added since the Red Book:
/ControlLanguage mark /InstanceType /dicttype .definecategory
/HWOptions mark /InstanceType /dicttype .definecategory
/Localization mark /InstanceType /dicttype .definecategory
/PDL mark /InstanceType /dicttype .definecategory
% CIDFont, CIDMap, and CMap are defined in gs_cidfn.ps
% FontSet is defined in gs_cff.ps
% IdiomSet is defined in gs_ll3.ps
% InkParams and TrapParams are defined in gs_trap.ps

(END MISC) VMDEBUG

% Define the OutputDevice category.
/OutputDevice mark
/InstanceType /dicttype
/.Instances mark
%% devicedict is not created yet so here we employ a technique similar to
%% that used to create it, in order to get the device names. We run a loop
%% executing .getdevice with incremental numbers until we get an error.
%% The devicedict creation only stops on a rangecheck, we stop on any error.
%% We need to use .internalstopped, not stopped or we get an invalidacces
%% later in this file. Instances of /OutputDevice are dictionaries, and the
%% only required key is a /PageSize. The array of 4 numbers are minimum to
%% maximum and are matches for the Adobe Acrobat Distiller values.
0
{
  {dup .getdevice .devicename cvn 1 dict dup /PageSize [1 1 14400 14400] put [exch readonly 0 -1] 3 -1 roll 1 add} loop
} //.internalstopped exec pop
%% Remove the count, and the duplicate, from the stack
pop pop
.dicttomark
.definecategory

% Define the ColorSpace category.

/.defaultcsnames mark
  /DefaultGray 0
  /DefaultRGB 1
  /DefaultCMYK 2
.dicttomark readonly def

% The "hooks" are no-ops here, redefined in LL3.
/.definedefaultcs {	% <index> <value> .definedefaultcs -
  pop pop
} bind def
/.undefinedefaultcs {	% <index> .undefinedefaultcs -
  pop
} bind def

/ColorSpace mark

/InstanceType /arraytype

% We keep track of whether there are any local definitions for any of
% the Default keys.  This information must get saved and restored in
% parallel with the local instance dictionary, so it must be stored in
% local VM.
userdict /.localcsdefaults //false put

/DefineResource {
  2 copy /Generic /Category findresource /DefineResource get exec
  exch pop
  exch //.defaultcsnames exch .knownget {
    1 index //.definedefaultcs exec
    currentglobal not { .userdict /.localcsdefaults //true put } if
  } if
} bind executeonly

/UndefineResource {
  dup /Generic /Category findresource /UndefineResource get exec
  //.defaultcsnames 1 index .knownget {
        % Stack: resname index
    currentglobal {
      //.undefinedefaultcs exec pop
    } {
        % We removed the local definition, but there might be a global one.
      exch .GetInstance {
        0 get //.definedefaultcs exec
      } {
        //.undefinedefaultcs exec
      } ifelse
        % Recompute .localcsdefaults by scanning.  This is rarely needed.
      .userdict /.localcsdefaults //false //.defaultcsnames {
        pop .LocalInstances exch known { pop //true exit } if
      } forall put
    } ifelse
  } {
    pop
  } ifelse
} bind executeonly

.definecategory			% ColorSpace

% Define the Encoding category.

/Encoding mark

/InstanceType /arraytype

% Handle already-registered encodings, including lazily loaded encodings
% that aren't loaded yet.

/.Instances mark
  EncodingDirectory
   { dup length 256 eq { [ exch readonly 0 -1 ] } { pop [//null 2 -1] } ifelse
   } forall
.dicttomark

/.ResourceFileDict mark
  EncodingDirectory
   { dup length 256 eq { pop pop } { 0 get } ifelse
   } forall
.dicttomark

/ResourceFileName
 { .ResourceFileDict 2 index .knownget
    { exch copy exch pop }
    { /Generic /Category findresource /ResourceFileName get exec }
   ifelse
 } bind executeonly

.definecategory			% Encoding

% Make placeholders in level2dict for the redefined Encoding operators,
% so that they will be swapped properly when we switch language levels.

/.findencoding /.findencoding load def
/findencoding /findencoding load def
/.defineencoding /.defineencoding load def

(END ENCODING) VMDEBUG

% Define the Font category.

/.fontstatusaux {		% <fontname> .fontstatusaux <fontname> <found>
  {		% Create a loop context just so we can exit it early.
                % Check Fontmap.
    Fontmap 1 index .knownget
    { //true }
    { .nativeFontmap 1 index .knownget } ifelse

    {
      {
        dup type /nametype eq {
          .fontstatus { pop //null exit } if
        } {
          dup type /dicttype eq {/Path .knownget pop} if
          dup type /stringtype eq {
            findlibfile { closefile pop //null exit } if pop
          } {
                % Procedure, assume success.
            pop //null exit
          } ifelse
        } ifelse
      } forall dup //null eq { pop //true exit } if
    } if


    dup / eq { //false exit } if  % / throws an error from findlibfile
                % Convert names to strings; give up on other types.
    dup type /nametype eq { .namestring } if
    dup type /stringtype ne { //false exit } if
                % Check the resource directory.
    dup //.fonttempstring /FontResourceDir getsystemparam .genericrfn
    status {
      pop pop pop pop //true exit
    } if
                % Check for a file on the search path with the same name
                % as the font.
    findlibfile { closefile //true exit } if
                % Scan a FONTPATH directory and try again.
    //.scannextfontdir exec not { //false exit } if
  } loop
} bind def

/.fontstatus {		% <fontname> .fontstatus <fontname> <found>
  //.fontstatusaux exec
  { //true }
  {
    .buildnativefontmap
    { //.fontstatusaux exec }
    { //false } ifelse
  } ifelse
} bind executeonly def
currentdict /.fontstatusaux .undef

/Font mark

/InstanceType /dicttype

/DefineResource
        { 2 copy //definefont exch pop
          /Generic /Category findresource /DefineResource get exec
        } bind executeonly
/UndefineResource
        { dup //undefinefont
          /Generic /Category findresource /UndefineResource get exec
        } bind executeonly
/FindResource {
        dup //.getvminstance exec {
          exch pop 0 get
        } {
          dup ResourceStatus {
            pop 1 gt { .loadfontresource } { .GetInstance pop 0 get } ifelse
          } {
            .loadfontresource
          } ifelse
        } ifelse
} bind executeonly
/ResourceForAll {
        { //.scannextfontdir exec not { exit } if } loop
        /Generic /Category findresource /ResourceForAll get exec
} bind executeonly
/.ResourceFileStatus {
        .fontstatus { pop 2 -1 //true } { pop //false } ifelse
} bind executeonly

/.loadfontresource {
        dup //.vmused exch
                % Hack: rebind .currentresourcefile so that all calls of
                % definefont will know these are built-in fonts.
        currentfile {pop //findfont exec} .execasresource  % (findfont is a procedure)
        exch //.vmused exch sub
                % stack: name font vmused
                % findfont has the prerogative of not calling definefont
                % in certain obscure cases of font substitution.
        2 index //.getvminstance exec {
          dup 1 1 put
          2 3 -1 roll put
        } {
          pop
        } ifelse exch pop
} bind

/.Instances FontDirectory length 2 mul dict

.definecategory			% Font

% Redefine font "operators".
/.definefontmap
 { /Font /Category findresource /.Instances get
   dup 3 index known
    { pop
    }
    { 2 index
                % Make sure we create the array in global VM.
      .currentglobal //true .setglobal
      [//null 2 -1] exch .setglobal
      .growput
    }
   ifelse
   //.definefontmap exec
 } bind def

% Make sure the old definitions are still in systemdict so that
% they will get bound properly.
% NOTE: Mystery code...  I can't just delete this, but don't understand why.
% Instead we will undef these three operators in gs_init.ps after all the initialization is done.
 systemdict begin
  /.origdefinefont /definefont load def
  /.origundefinefont /undefinefont load def
  /.origfindfont /findfont load def
end
/definefont {
  { /Font defineresource } stopped {
      /definefont cvx $error /errorname get signalerror
  } if
} bind executeonly odef
/undefinefont {
  /Font undefineresource
} bind executeonly odef
% The Red Book requires that findfont be a procedure, not an operator,
% but it still needs to restore the stacks reliably if it fails.
/.findfontop {
  { /Font findresource } stopped {
    pop /findfont $error /errorname get signalerror
  } if
} bind executeonly odef
/findfont {
  .findfontop
} bind executeonly def	% Must be a procedure, not an operator

% Remove initialization utilities.
currentdict /.definecategory .undef
currentdict /.emptydict .undef

end				% level2dict

% Convert deferred resources after we finally switch to Level 2.

/.fixresources {
        % Encoding resources
  EncodingDirectory
   { dup length 256 eq
      { /Encoding defineresource pop }
      { pop pop }
     ifelse
   } forall
  /.findencoding {
    { /Encoding findresource } stopped {
      pop /findencoding $error /errorname get signalerror
    } if
  } bind def
  /findencoding /.findencoding load def		% must be a procedure
  /.defineencoding { /Encoding defineresource pop } bind def
        % ColorRendering resources and ProcSet
  systemdict /ColorRendering .knownget {
    /ColorRendering exch /ProcSet defineresource pop
    systemdict /ColorRendering undef
    /DefaultColorRendering currentcolorrendering /ColorRendering defineresource pop
  } if
        % ColorSpace resources
  systemdict /CIEsRGB .knownget {
    /sRGB exch /ColorSpace defineresource pop
    systemdict /CIEsRGB undef
  } if
  systemdict /CIEsRGBICC .knownget {
    /sRGBICC exch /ColorSpace defineresource pop
    systemdict /CIEsRGBICC undef
  } if
  systemdict /CIEsGRAYICC .knownget {
    /sGrayICC exch /ColorSpace defineresource pop
    systemdict /CIEsGRAYICC undef
  } if
  systemdict /CIEesRGBICC .knownget {
    /esRGBICC exch /ColorSpace defineresource pop
    systemdict /CIEesRGBICC undef
  } if
  systemdict /CIErommRGBICC .knownget {
    /rommRGBICC exch /ColorSpace defineresource pop
    systemdict /CIErommRGBICC undef
  } if
  % ColorSpaceFamily resources
  colorspacedict { pop dup /ColorSpaceFamily defineresource pop } forall
        % Filter resources
  filterdict { pop dup /Filter defineresource pop } forall
        % FontType and FMapType resources
  buildfontdict { pop dup /FontType defineresource pop } forall
  mark
    buildfontdict 0 known { 2 3 4 5 6 7 8 } if
    buildfontdict 9 known { 9 } if
  counttomark { dup /FMapType defineresource pop } repeat pop
        % FormType resources
  .formtypes { pop dup /FormType defineresource pop } forall
        % HalftoneType resources
  .halftonetypes { pop dup /HalftoneType defineresource pop } forall
        % ColorRenderingType resources
  .colorrenderingtypes {pop dup /ColorRenderingType defineresource pop} forall
        % ImageType resources
  .imagetypes { pop dup /ImageType defineresource pop } forall
        % PatternType resources
  .patterntypes { pop dup /PatternType defineresource pop } forall
        % Make the fixed resource categories immutable.
  /.shadingtypes where {
    pop .shadingtypes { pop dup /ShadingType defineresource pop } forall
  } if
  [ /ColorSpaceFamily /Emulator /Filter /IODevice /ColorRenderingType
    /FMapType /FontType /FormType /HalftoneType /ImageType /PatternType
    /.shadingtypes where { pop /ShadingType } if
  ] {
    /Category findresource
    dup /.Instances get readonly pop
    .LocalInstances readonly pop
    readonly pop
  } forall
        % clean up
  systemdict /.fixresources undef
} bind def

%% Replace 1 (gs_resmp.ps)
(gs_resmp.ps)  dup runlibfile VMDEBUG

[
    /.default_resource_dir
    /.resource_dir_name
    /.fonttempstring /.scannextfontdir % from gs_fonts.ps
]
{systemdict exch .forceundef} forall

[
    /.definedefaultcs
    /.undefinedefaultcs
    /.defaultcsnames
    /.enumerateresource
    /.externalresourceforall
    /.getvminstance
    /.globalresourceforall
    /.localresourceforall
    /resourceforall1
    /.resourceexec
    /.undefinedresource
    /.vmused
]
{level2dict exch .forceundef} forall