Debian Codegraph @

Resource/Init/gs_cidcm.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 % Extending Font resource category with CIDFont-CMap fonts.
17
18 languagelevel 2 .setlanguagelevel currentglobal //true setglobal
19
20 % In the comments below, 'CSI' is an abbreviation/acronym for CIDSystemInfo.
21 % We pre-scan resource files to retrieve the CSI from them.
22 % First we define a hidden procset .prs_dict containing
23 % necessary variables and procedures.
24 % Then we redefine the old /Font category using this procset.
25
26 % We maintain internal caches for the CSI values retrieved from
27 % resource files. This supposes that document doesn't uninstall
28 % resource files. To disable caching, set enable_cache to false.
29
30 % We assume that names starting with '.prs' do not appear in resource files.
31 % If this causes any problem, this prefix should be systematically changed
32 % in this file. ('prs' is an abbreviation for 'prescan'.)
33
34 25 dict begin
35
36 % Define local variables :
37
38 /.prs_dict currentdict def % self-reference (constant)
39 /.prs_empty 0 dict readonly def
40 /path_buffer 8192 string def
41 /name_buffer 1024 string def
42 /minus (-) 0 get def % character code constant for '-'
43 /period (.) 0 get def % character code constant for '.'
44 /CMap 10 dict def % CSI cache for CMaps
45 /CIDFont 10 dict def % CSI cache for CIDFonts
46 /enable_cache //true def % set false to disable cache
47
48 % The folloving variables are just placeholders for ones to be set
49 % dynamically :
50 /.prsFile 0 def % file to prescan
51 /.prsResult 0 def % result of prescan
52 /.prsDictCount 0 def % save the dictionary stack depth
53
54 % Define a dummy CIDInit procset to use while pre-scanning :
55
56 /DummyCIDInit 15 dict
57 begin
58
59 /begincmap {} def
60 /usecmap {pop} .internalbind def
61
62 {stop} .internalbind
63 [ /begincodespacerange /endcodespacerange /beginnotdefchar /endnotdefchar
64 /beginnotdefrange /endnotdefrange /begincidchar /endcidchar /begincidrange
65 /endcidrange /endcmap /usefont /StartData
66 ] {
67 1 index def
68 } .internalbind forall
69 pop
70
71 currentdict end def
72
73 % Define a local 'findresource' for pre-scanning :
74 % (it returns the dummy CIDInit instead of the regular CIDInit ProcSet)
75
76 /findresource { % <InstName> <CatName> findresource <inst>
77 2 copy /ProcSet eq exch % /InstName /CatName bool /InstName
78 /CIDInit eq and {
79 pop pop //DummyCIDInit
80 } {
81 //findresource exec
82 } ifelse
83 } .internalbind def
84
85 % Define procedures for pre-scanning :
86
87 /StopIfCSIDefined { % - StopIfCSIDefined -
88
89 % Check if the dictionary stack contains a dictionary containing /CIDSystemInfo.
90 % The search is limited to the top .prsDictCount dictionaries in the stack.
91 % If so, retrieve the CSI, and execute stop to terminate the pre-scanning of the file.
92 % Otherwise, do nothing, so the pre-scanning continues.
93
94 countdictstack //.prs_dict /.prsDictCount get sub dup {
95 currentdict /CIDSystemInfo .knownget {
96 //.prs_dict exch /.prsResult exch put
97 stop
98 } if
99 currentdict exch end
100 } repeat {
101 begin
102 } repeat
103 } .internalbind def
104
105 /PrescanFile { % - PrescanFile -
106 { //.prs_dict /.prsFile get token {
107 dup type % token type
108 dup /nametype eq exch /operatortype eq or {
109 dup xcheck {
110 exec
111 //StopIfCSIDefined exec
112 } if
113 } if
114 } {
115 stop
116 } ifelse
117 } loop
118 } .internalbind odef
119
120 /GetCIDSystemInfoFromFile { % <file> GetCIDSystemInfoFromFile <CSI>
121
122 % This procedure reads resource files with 'token',
123 % executing the tokens untill /CIDSystemInfo appears to be defined.
124 % Normally the resource file creates a new dictionary on
125 % dictionary stack and defines /CIDSystemInfo in it.
126 %
127 % Returns an empty dictionary if no CIDSystemInfo is found.
128
129 RESMPDEBUG { (cidcm GetCIDSystemInfoFromFile beg) = } if
130 //.prs_dict begin
131 /.prsFile exch def
132 /.prsResult //.prs_empty def
133 /.prsDictCount countdictstack def
134 RESMPDEBUG { (cidcm GetCIDSystemInfoFromFile will PrescanFile.) = } if
135 { //PrescanFile } stopped pop
136 //.prs_dict /.prsResult get
137 end
138 RESMPDEBUG { (cidcm GetCIDSystemInfoFromFile end) = } if
139 } .internalbind def
140
141 /GetCIDSystemInfo { % <InstName> <CatName> GetCIDSystemInfo <CSI>
142
143 % Retrieve CSI, using caches.
144
145 RESMPDEBUG { (cidcm GetCIDSystemInfo beg) = } if
146 /Category findresource begin % /InstName
147 dup ResourceStatus
148 {
149 pop 2 lt {
150 FindResource /CIDSystemInfo .knownget not {
151 //.prs_empty
152 } if % CSI
153 } { % /InstName
154 currentdict /GetCIDSystemInfoFromMap .knownget {
155 exec
156 } if
157 dup type /nametype eq
158 {
159 RESMPDEBUG { (cidcm GetCIDSystemInfo got a name.) = } if
160 //.prs_dict Category get % /InstName CSIs
161 dup 2 index known
162 //enable_cache and {
163 RESMPDEBUG { (cidcm GetCIDSystemInfo from cache.) = } if
164 exch get % CSI
165 } {
166 RESMPDEBUG { (cidcm GetCIDSystemInfo from file.) = } if
167 exch % CSIs /InstName
168 dup //path_buffer ResourceFileName % CSIs /InstName (path)
169 RESMPDEBUG { (cidcm GetCIDSystemInfo from file ) print dup = } if
170 currentglobal exch //true setglobal % CSIs /InstName g (path)
171 mark exch % CSIs /InstName g [ (path)
172 { (r) file } stopped {
173 cleartomark //.prs_empty
174 } {
175 exch 1 index % CSIs /InstName g file [ file
176 //GetCIDSystemInfoFromFile stopped {
177 cleartomark closefile //.prs_empty
178 } {
179 exch pop exch closefile
180 } ifelse
181 } ifelse % CSIs /InstName g CSI
182 exch setglobal % CSIs /InstName CSI
183 dup 4 1 roll % CSI CSIs /InstName CSI
184 put % CSI
185 RESMPDEBUG {
186 (cidcm GetCIDSystemInfo got from file : <<) print
187 dup { exch //=string cvs print ( ) print
188 //=string cvs print ( ) print
189 } forall
190 (>>) =
191 } if
192 } ifelse
193 } if
194 } ifelse
195 } {
196 pop //.prs_empty
197 } ifelse
198 end
199 RESMPDEBUG { (cidcm GetCIDSystemInfo end) = } if
200 } .internalbind def
201
202 /IsCompatibleCSI { % <CSI-M> <CSI-F> IsCompatibleCSI <bool>
203
204 % The CSI in a CIDFont may be an array, a dict, or null.
205 % If it is an array, it must be of 1 element, which is a dict.
206 % In this case the dict is used for testing the compatibility.
207 % Two dicts are compatible iff they contain same /Ordering and /Registry.
208 % Identity CMap is compatible with any CIDFont.
209
210 exch % CSI-F CSI-M
211 { dup type /arraytype eq {
212 dup length 1 ne {
213 pop pop //false exit
214 } if
215 0 get
216 } if % CSI-F CSI-M
217 dup type /dicttype ne {
218 pop pop //false exit
219 } if % CSI-F <<CSI-M>>
220 exch % <<CSI-M>> CSI-F
221 dup type /dicttype ne {
222 pop pop //false exit
223 } if % <<CSI-M>> <<CSI-F>>
224 dup /Ordering .knownget {
225 /Identity eq {
226 pop pop //true exit
227 } if
228 } if
229 //true % <<CSI-M>> <<CSI-F>> bEQ
230 [/Registry /Ordering] {
231 2 index 1 index .knownget not {
232 1234567
233 } if % <<CSI-M>> <<CSI-F>> bEQ /key vF
234 exch % <<CSI-M>> <<CSI-F>> bEQ vF /key
235 4 index exch .knownget not {
236 7654321
237 } if % <<CSI-M>> <<CSI-F>> bEQ vF vM
238 eq and % <<CSI-M>> <<CSI-F>> bEQ
239 } forall
240 exch pop exch pop % bEQ
241 exit
242 } loop
243 } .internalbind def
244
245 /IsWellComposed { % <CIDFontName> <CMapName> IsWellComposed <bool>
246
247 % Check if the given CIDFont and CMap have compatible CSIs.
248 exch % /CMapName /CIDFontName
249 /CIDFont //GetCIDSystemInfo exec % /CMapName CSI-F
250 dup type /dicttype eq {
251 dup length 0 ne {
252 exch % CSI-F /CMapName
253 /CMap //GetCIDSystemInfo exec % CSI-F CSI-M
254 //IsCompatibleCSI exec % bool
255 } {
256 pop pop //false
257 } ifelse
258 } {
259 pop pop //false
260 } ifelse
261 } .internalbind def
262
263 /IsComposedFont { % <FontName> IsComposedFont <CIDFontName> <CMapName> true
264 % <FontName> IsComposedFont false
265
266 % Check if the given font name may be decomposed into CIDFont.CMap, CIDFont-CMap
267 % or into CIDFont--CMap, such that CIDFont and CMap have compatible CSIs.
268 % FontName
269 dup type /stringtype ne {
270 //name_buffer cvs
271 } if % (FontName)
272 { dup length 2 sub -1 1 {
273 % (FontName) i
274 2 copy get dup //minus eq exch //period eq or {
275 2 copy 2 copy % (FontName) i (FontName) i (FontName) i
276 2 copy get //minus eq {
277 2 copy 1 sub get //minus eq {
278 1 sub
279 } if
280 } if % (FontName) i (FontName) i (FontName) i0
281 0 exch getinterval cvn % (FontName) i (FontName) i /CIDFontName
282 3 1 roll % (FontName) i /CIDFontName (FontName) i
283 1 add dup % (FontName) i /CIDFontName (FontName) i1 i1
284 5 index length % (FontName) i /CIDFontName (FontName) i1 i1 l
285 exch sub getinterval cvn % (FontName) i /CIDFontName /CMapName
286 2 copy //IsWellComposed exec { % (FontName) i /CIDFontName /CMapName
287 4 2 roll pop pop % /CIDFontName /CMapName
288 stop
289 } if
290 pop pop pop
291 } {
292 pop
293 } ifelse % (FontName)
294 } for
295 pop
296 } stopped
297 } .internalbind def
298
299 /ComposeName { % <CIDFont> <CMap> <scr> ComposeName <CIDFont-CMap>
300 dup dup 5 2 roll % (scr) (scr) /CIDFont /CMap (scr)
301 3 2 roll exch cvs length dup % (scr) (scr) /CMap l0 l0
302 4 -1 roll exch //minus put % (scr) /CMap l0
303 1 add dup % (scr) /CMap l1 l1
304 3 index dup length % (scr) /CMap l1 l1 (scr) L
305 2 index sub % (scr) /CMap l1 l1 (scr) LT
306 3 2 roll % (scr) /CMap l1 (scr) LT l1
307 exch getinterval % (scr) /CMap l1 (scrT)
308 3 2 roll exch cvs length % (scr) l1 l2
309 add 0 exch getinterval % (CIDFont-CMap)
310 } .internalbind def
311
312 % Redefine the /Font category with CIDFont-CMap construction :
313
314 % The following code supposes that the following names are not
315 % defined in the old /Font category dictionary :
316 % /IsComposedFont, /IsWellComposed .
317
318 /Font /Category findresource dup length dict copy begin
319
320 /FindResource { % <InstName> FindResource <inst>
321 dup //ResourceStatus exec {
322 pop pop //FindResource exec
323 } {
324 dup //IsComposedFont exec { % /FontName /CIDFontName /CMapName
325 exch [ exch ] composefont % inst
326 } {
327 //FindResource exec
328 } ifelse
329 } ifelse
330 } .internalbind def
331
332 /ResourceStatus { % <InstName> ResourceStatus <nStatus> <nSize> true
333 % <InstName> ResourceStatus false
334 dup //ResourceStatus exec {
335 3 2 roll pop //true % nStatus nSize true
336 } {
337 //IsComposedFont exec { % /CIDFontName /CMapName
338 /CMap resourcestatus { % /CIDFontName nStatusM nSizeM
339 exch pop exch % nSizeM /CIDFontName
340 /CIDFont resourcestatus { % nSizeM nStatusF nSizeF
341 exch pop % nSizeF nSizeM
342 dup 0 ge {
343 exch dup 0 ge {
344 add
345 } {
346 exch pop
347 } ifelse
348 } {
349 pop
350 } ifelse % nSize
351 2 exch //true % nStatus nSize true
352 } {
353 pop pop pop //false % work around buggy resource file
354 } ifelse
355 } {
356 pop pop pop //false % work around buggy resource file
357 } ifelse
358 } {
359 //false
360 } ifelse
361 } ifelse
362 } .internalbind def
363
364 /ResourceForAll { % <template> <proc> <scratch> ResourceForAll -
365
366 % We suppose that the resourceforall procedure does not
367 % define or install new fonts, CMaps, and/or CIDFonts.
368
369 % First we create 3 temporary dictionaries to store temporary data
370 % about fonts, CMaps and CIDFonts.
371 % These dictionaries must be created dynamically, to allow for a possible
372 % recursive call to resourceforall from the resourceforall procedure.
373 currentglobal //false setglobal
374 20 dict 20 dict 20 dict % (templ) proc (scr) g <<CIDFont>> <<CMap>> <<Fonts>>
375
376 % Store resource identifiers into local dictionaries
377 % A resource instance can have a key that is not a name or a string. In this
378 % case, resourceforall passes the key directly to proc instead of copying it
379 % into the scratch string. This case can arise only for a resource instance
380 % defined in virtual memory by a previous defineresource
381
382 % Discard non-string keys of CIDFont and CMap because <CIDFontName>-<CMapName>
383 % is only defined for names.
384
385 { /.DisableResourceOrdering pop % gs_resmp accesses this through execstack - don't remove !
386
387 6 index [ 2 index {exch //null put} aload pop ] cvx bind 6 index //ResourceForAll exec
388
389 (*) [ 3 index {exch dup type /stringtype eq { cvn dup put } { pop pop } ifelse } aload pop
390 ] cvx bind 6 index /CMap resourceforall
391
392 (*) [ 4 index {exch dup type /stringtype eq { cvn dup put } { pop pop } ifelse } aload pop
393 ] cvx bind 6 index /CIDFont resourceforall
394
395 exit
396 } loop % This loop is a pattern for execstack_lookup - don't remove !
397 4 -1 roll setglobal % (templ) proc (scr) <<CIDFont>> <<CMap>> <<Fonts>>
398
399 %% Make the list of fonts in the form (/Name status) :
400
401 % (templ) proc (scr) <<CIDFont>> <<CMap>> <<Fonts>>
402 dup {
403 pop dup
404 //ResourceStatus exec {
405 pop 2 index % (templ) proc (scr) <<CIDFont>> <<CMap>> <<Fonts>> /Name nStatus <<Font>>
406 3 1 roll put % (templ) proc (scr) <<CIDFont>> <<CMap>> <<Fonts>>
407 } {
408 pop
409 } ifelse
410 } forall % (templ) proc (scr) <<CIDFont>> <<CMap>> <<Fonts>>
411
412 %% Add CIDFont-CMap to it (filtering duplicates) :
413
414 3 2 roll {
415 3 index {
416 3 1 roll % (templ) proc (scr) <<CMap>> <<Font>> /CIDFont /CMap /CIDFont /CMap
417 6 index //ComposeName exec % (templ) proc (scr) <<CMap>> <<Font>> /CIDFont /CMap (Font)
418 dup 8 index .stringmatch {
419 cvn % (templ) proc (scr) <<CMap>> <<Font>> /CIDFont /CMap /Font
420 dup 4 index exch known {
421 pop pop
422 } {
423 2 index % (templ) proc (scr) <<CMap>> <<Font>> /CIDFont /CMap /Font /CIDFont
424 4 2 roll % (templ) proc (scr) <<CMap>> <<Font>> /Font /CIDFont /CIDFont /CMap
425 //IsWellComposed exec {
426 exch 2 index exch 2 put % (templ) proc (scr) <<CMap>> <<Font>> /CIDFont
427 } {
428 exch pop
429 } ifelse
430 } ifelse
431 } {
432 pop pop
433 } ifelse
434 dup % (templ) proc (scr) <<CMap>> <<Font>> /CIDFont /CIDFont
435 } forall
436 pop pop % (templ) proc (scr) <<CMap>> <<Font>>
437 } forall % (templ) proc (scr) <<CMap>> <<Font>>
438 exch pop % (templ) proc (scr) <<Font>>
439 4 3 roll pop % proc (scr) <<Font>>
440
441 % Make the enumerator and apply it :
442 /MappedCategoryRedefiner /ProcSet findresource /MakeResourceEnumerator get exec exec
443 } .internalbind def
444
445 currentdict end /Font exch /Category defineresource pop
446 end
447 setglobal .setlanguagelevel