Debian Codegraph @

Resource/Init/gs_cmap.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 % ProcSet for implementing CMap resources.
17 % When this is run, systemdict is still writable.
18
19 % NOTE: Rearranged fonts are not implemented yet.
20
21 [
22 /CMERGE_DEBUG
23 /USE_CIDCHAR_AS_RANGE
24 ] {dup where {pop pop} { currentdict exch //false def pop } ifelse} forall
25
26 % ---------------- Public operators ---------------- %
27
28 /.rewriteTempMapsNotDef {
29 %
30 % Before building .CodeMapData from .TempMaps,
31 % we need to replace dst type codes in the notdef map with the value 3,
32 % which corresponds to CODE_VALUE_NOTDEF, see gxfcmap.h .
33 %
34 CMAPDEBUG { (rewriting TempMapsNotDef\n) print flush } if
35 .TempMaps 2 get
36 dup length 0 gt {
37 0 get
38 CMAPDEBUG { (...original...\n) print flush } if
39 1 5 2 index length 1 sub {
40 { 1 index exch get 2 3 put } stopped
41 { CMAPDEBUG { (cannot rewrite\n) print flush } if }
42 { CMAPDEBUG { (rewrite\n) print flush } if } ifelse
43 } for
44 } if
45 pop
46 CMAPDEBUG { (...FINISHED...\n) print } if
47 } .internalbind def
48
49 /.composefontdict { % <name> <cmap|cmapname> <fonts> composefont <name> <font_dict>
50 10 dict begin
51 /CMap 2 index dup type /dicttype ne { /CMap findresource } if def
52 /FDepVector 1 index cvlit def % temporarily
53 /Encoding [ 0 1 FDepVector length 1 sub { } for ] def
54 /FontInfo 1 dict def % for .processToUnicode in pdf_font.ps .
55 /FDepVector [ 0 1 FDepVector length 1 sub {
56 % Stack: name cmap[name] fonts /FDepVector [ fonts... i
57 FDepVector 1 index get
58 dup type /dicttype ne {
59 dup /CIDFont resourcestatus {
60 pop pop /CIDFont
61 } {
62 /Font
63 } ifelse findresource
64 } if
65 exch CMap /FontMatrices get dup length 2 index gt {
66 exch get dup //null eq { pop } { makefont } ifelse
67 } {
68 pop pop
69 } ifelse
70 } for ] readonly def
71 /FMapType 9 def
72 /FontMatrix matrix def
73 /FontName 3 index def
74 CMap /WMode .knownget { /WMode exch def } if
75 /FontType 0 def
76 pop pop currentdict end
77 } .internalbind odef
78
79 % composefont doesn't appear in CMap files -- it's documented in
80 % the "PostScript Language Reference Manual Supplement".
81 /composefont { % <name> <cmap|cmapname> <fonts> composefont <font>
82 .composefontdict /Font defineresource
83 } .internalbind def
84
85 % ---------------- CMap operators ---------------- %
86
87 40 dict begin
88
89 % Our internal .CodeMapData structure closely mirrors the structures
90 % defined in gxfcmap.h (q.v.). () indicate a string, [] indicate an array,
91 % ? indicates a Boolean, # indicates an integer, {} for grouping.
92 % [[[(first) (last) ...]+] % code space ranges
93 % [[(prefix) (key_size,?is_range,value_type,value_size) (keys...)
94 % {(values...) | [value ...]} #font_index ]+] % code mappings
95 % ...]
96 % <<same>> % notdef mappings
97 % ]
98 % FontMatrices is the array of matrices defined by begin/endusematrix.
99 % All of the arrays and strings are read-only after they have been built.
100 %
101 % Note that the code in zfcmap.c that constructs the C structures from
102 % the PostScript structures has intimate knowledge of the above format.
103
104 % ****** NOTE: The code currently only handles "well-behaved" CMaps:
105 % - CID values only (no bfchars), 16-bit
106 % - Entries (both code space and map) must be sorted
107 % - Only the last byte must vary in each map range, except for
108 % the identity mapping
109
110 % ------ Font-level operators ------ %
111
112 /begincmap { % - begincmap -
113 /.CodeMapData [[[]] [[]] [[]]] def
114 /FontMatrices [] def
115 /.FontIndex 0 def
116 /.TempMaps [20 dict 50 dict 50 dict] def
117 /CodeMap //null def % for .buildcmap
118 } .internalbind def
119
120 /endcmap { % - endcmap -
121 //.rewriteTempMapsNotDef exec
122
123 CMAPDEBUG {
124 2 (*** undefined charmap ***)
125 1 (*** defined charmap ***)
126 0 (*** code space ranges ***)
127 3 { =
128 .TempMaps exch get
129 0 1 2 index length 1 sub {
130 dup //== exec (\t) print
131 1 index exch get //== exec
132 } for
133 pop
134 } repeat
135 } if
136
137 /.CodeMapData dup load [ exch
138 .TempMaps aload pop begin begin begin
139 {
140 [ exch aload pop
141 0 1 currentdict length 1 sub {
142 currentdict exch get
143 } for
144 ]
145 end
146 } forall
147 ] .endmap def
148
149 CMAPDEBUG {
150 (*** Content of .CodeMapData ***) =
151 0 .CodeMapData { exch dup //== exec 1 add exch (\t) print //== exec } forall
152 pop
153 } if
154
155 currentdict /.TempMaps undef
156 /FontMatrices FontMatrices .endmap def
157 } .internalbind def
158
159 /.endmap { % <map> .endmap <map>
160 dup type /arraytype eq {
161 % This might be a shared read-only array inherited via usecmap.
162 % Don't try to update its elements if this is the case.
163 dup wcheck {
164 0 1 2 index length 1 sub {
165 2 copy 2 copy get .endmap put pop
166 } for readonly
167 } if
168 } {
169 dup type /stringtype eq { readonly } if
170 } ifelse
171 } .internalbind def
172
173 /.appendmap { % -mark- <elt> ... <array#> .appendmap -
174 .TempMaps exch get counttomark 1 add 1 roll
175 ] 1 index length exch put
176 } .internalbind def
177
178 /begincodespacerange { % <count> begincodespacerange -
179 pop mark
180 } .internalbind def
181
182 /endcodespacerange { % <code_lo> <code_hi> ... endcodespacerange -
183 0 .appendmap
184 } .internalbind def
185
186 /usecmap { % <CMap_name> usecmap -
187 /CMap findresource dup
188 % Copy the top level of .CodeMapData
189 /.CodeMapData exch /.CodeMapData get copyarray def
190 /FontMatrices exch /FontMatrices get copyarray def
191 } .internalbind def
192
193 /usefont { % <fontID> usefont -
194 /.FontIndex exch def
195 } .internalbind def
196
197 /beginusematrix { % <fontID> beginusematrix -
198 FontMatrices wcheck not FontMatrices length 2 index le or {
199 FontMatrices length 1 index 1 add .max array
200 dup 0 FontMatrices putinterval
201 /FontMatrices exch def
202 } if
203 } .internalbind def
204
205 /endusematrix { % <matrix> endusematrix -
206 FontMatrices 3 1 roll put
207 } .internalbind def
208
209 % ------ Rearranged font operators ------ %
210
211 /beginrearrangedfont { % <font_name> <font*> beginrearrangedfont -
212 10 dict begin
213 /.FontNames exch def
214 /.FontName exch def
215 begincmap
216 } .internalbind def
217 /endrearrangedfont { % - endrearrangedfont -
218 (REARRANGED FONTS NOT IMPLEMENTED YET.) = flush
219 .FontName .FontNames 0 get findfont end definefont pop
220 } .internalbind def
221
222 % ------ Character name/code selector operators ------ %
223
224 /beginbfchar { % <count> beginbfchar -
225 pop mark
226 } .internalbind def
227 /endbfchar { % [ <code> <to_code|charname> ... endbfchar
228 ] [ //true % [<da><ta>] [ true
229 3 -1 roll % [ true [<da><ta>]
230 {
231 exch {
232 //false % [ <da> false
233 } { % [ <da> <ta>
234 .addbfchar % [ prefix params key value font_index
235 //true
236 } ifelse
237 } forall
238 pop
239 1 .appendmap
240 } .internalbind def
241
242 /beginbfrange { % <count> beginbfrange -
243 pop mark
244 } .internalbind def
245 /endbfrange { % <code_lo> <code_hi> <to_code|(charname*)> ...
246 % endbfrange -
247 ] [ 0 % [<da><ta><set>] [ 0
248 3 -1 roll % [ 0 [<da><ta><set>]
249 {
250 exch % [ <da> 0
251 { 1 2
252 {
253 dup type dup /arraytype eq exch /packedarraytype eq or {
254 % Array value, split up.
255 exch pop {
256 % Stack: code to_code|charname
257 1 index exch .addbfchar
258 % Increment the code. As noted above, we require
259 % that only the last byte vary, but we still must
260 % mask it after incrementing, in case the last
261 % value was 0xff.
262 % Stack: code prefix params key value fontindex
263 6 -1 roll dup length string copy
264 dup dup length 1 sub 2 copy get 1 add 255 and put
265 } forall pop
266 } {
267 % Single value, handle directly.
268 .addbfrange
269 } ifelse
270 0
271 }
272 } exch get exec
273 } forall
274 pop
275 1 .appendmap
276 } .internalbind def
277
278 /.addbfchar { % <code> <to_code|charname> .addbfchar
279 % <prefix> <params> <key> <value> <font_index>
280 1 index exch .addbfrange
281 } .internalbind def
282 /.addbfrange { % <code_lo> <code_hi> <to_code|charname>
283 % .addbfrange <<same as .addbfchar>>
284 4 string dup 3
285 3 index type /nametype eq {
286 2 index 2 1 put % dst = CODE_VALUE_GLYPH, see gxfcmap.h .
287 4 -1 roll 1 array astore 4 1 roll 4
288 } {
289 2 index 2 2 put % dst = CODE_VALUE_CHARS, see gxfcmap.h .
290 3 index length
291 } ifelse put
292 % Stack: code_lo code_hi value params
293 3 index 3 index eq {
294 % Single value.
295 3 -1 roll pop exch () exch
296 } {
297 % Range.
298 dup 0 4 index length put
299 dup 1 1 put
300 4 2 roll
301 1 index dup length 1 sub 0 exch getinterval 5 1 roll % prefix
302 % Stack: prefix value params code_lo code_hi
303 concatstrings
304 3 -1 roll
305 } ifelse
306 .FontIndex
307 } .internalbind def
308
309 % ------ CID selector operators ------ %
310
311 /begincidchar { % <count> begincidchar -
312 pop mark
313 } .internalbind def
314 /endcidchar { % <code> <cid> ... endcidchar -
315 1 .endmapchars
316 } .internalbind def
317
318 /begincidrange { % <count> begincidrange -
319 pop mark
320 } .internalbind def
321 /endcidrange { % <code_lo> <code_hi> <cid_base> ... endcidrange -
322 1 .endmapranges
323 } .internalbind def
324
325 /.endmapchars { % -mark- <code> <cid> ... <map#> .endmapchars -
326 counttomark 1 add 1 roll
327 counttomark 2 idiv {
328 counttomark -2 roll % process in correct order
329 exch % <cid> <code>
330 % Construct prefix, params, key, value, font_index
331 dup length 1 eq { % 1-byte
332 <00 00 00 02> () % <prefix> <param> <null_key>
333 } { % N-byte
334 dup 0 1 getinterval exch % make 1-byte prefix
335 4 string dup 0
336 USE_CIDCHAR_AS_RANGE {
337 <00 01 00 02> % skelton for param
338 } {
339 <00 00 00 02> % skelton for param
340 } ifelse
341 putinterval
342 exch % <prefix> <param> <code>
343 dup length % <prefix> <param> <code> N
344 1 sub % <prefix> <param> <code> N-1
345 dup % <prefix> <param> <code> N-1 N-1
346 3 index % <prefix> <param> <code> N-1 N-1 <param>
347 exch % <prefix> <param> <code> N-1 <param> N-1
348 0 exch % <prefix> <param> <code> N-1 <param> 0 N-1
349 put % <prefix> <param'> <code> N-1
350 1 exch % <prefix> <param'> <code> 1 N-1
351 getinterval % <prefix> <param'> <key>
352
353 USE_CIDCHAR_AS_RANGE {
354 dup length 2 mul string % <key> <dkey>
355 dup % <key> <dkey> <dkey>
356 2 index % <key> <dkey> <dkey> <key>
357 0 exch putinterval % <key> <dkey'>
358 dup % <key> <dkey'> <dkey'>
359 3 -1 roll % <dkey'> <dkey'> <key>
360 dup length % <dkey'> <dkey'> <key> N-1
361 exch putinterval % <dkey''>
362 } if
363
364 } ifelse
365
366 4 -1 roll % <prefix> <param'> <key> <cid>
367 .endmapvalue % <prefix> <param'> <key> <hex_cid> <font_idx>
368 % prefix params keys value fontindex
369 counttomark 9 gt { % 2 (or more) ranges (1 range = 5 item)
370 4 index 10 index eq % compare prefix
371 4 index 10 index eq and % compare params
372 1 index 7 index eq and % compare fontindex
373 {
374 CMAPDEBUG { (merge! char\n) print } if
375 pop 4 2 roll pop pop
376 % prefix params keys value fontindex keys2 value2
377 5 -1 roll 3 -1 roll concatstrings
378 % prefix params value fontindex value2 keys'
379 4 -1 roll 3 -1 roll concatstrings
380 % prefix params fontindex keys' values'
381 3 -1 roll
382 } if
383 } if % end of 2 (or more) ranges
384 CMERGE_DEBUG {
385 ( prefix:) print 4 index =only
386 ( param:) print 3 index =only
387 ( key:) print 2 index =only
388 ( hex_cid:) print 1 index =only
389 ( font_idx:) print 0 index //== exec flush
390 } if
391 } repeat
392 counttomark 2 add -1 roll .appendmap
393 } .internalbind def
394
395 /.endmapranges { % -mark- <code_lo> <code_hi> <cid_base> ... <map#>
396 % .endmapranges -
397 counttomark 1 add 1 roll
398 counttomark 3 idiv {
399 counttomark -3 roll % process in correct order
400 % Construct prefix, params, key_lo, key_hi, value, font_index
401 3 1 roll % <cid_base> <code_lo> <code_hi>
402 % prefix key
403 % 1-byte code: () .
404 % 1-byte range: () .
405 % N-byte code: . (*)
406 % N-byte range: (*) (*)
407 dup 2 index eq { % <code_lo> == <code_hi>
408 % 0: prefix_len for 1-byte code
409 % 1: prefix_len for N-byte code
410 dup length 1 eq { 0 } { 1 } ifelse
411 } { % <code_lo> != <code_hi>
412 % calculate prefix_len for *-byte range
413 dup length 1 sub % <cid_base> <code_lo> <code_hi> <code_len-1>
414 0 % initial value for N
415 { % <cid_base> <code_lo> <code_hi> (code_len-1) N
416 dup 2 index ge { exit } if % if (N >= len - 1) exit
417 3 index 1 index get % N-th byte of code_lo
418 3 index 2 index get % N-th byte of code_hi
419 eq { 1 add } { exit } ifelse
420 } loop
421 exch pop % discard <code_len-1>
422 } ifelse
423 % cid_base code_lo code_hi prefix_len
424
425 % Althogh Adobe CPSI with native CID/CMap support accept
426 % multi-dimensional range specification in notdef & cidrange
427 % (and CID is calculated as relative position in multi-dimensional
428 % range), but older CPSI & ATM cannot handle it.
429 %
430 % GS accepts such specification, but it's recommended to keep
431 % from using this feature for notdef & cidrange.
432 % Following is a disabler of this feature.
433 % -------------------------------------------------------------
434 % counttomark 1 add index % get map#
435 % 0 ne { % if not codespacerange
436 % 1 index length % get code length
437 % 1 index % get prefix length
438 % sub % calculate key length
439 % 1 gt { % if (key_len > 1),
440 % (.endmapranges error) = flush
441 % (multi-dimensional range specification is used out of codespacerange)
442 % = flush
443 % (/) =only
444 % CMapName CMapName length string cvs =only
445 % (: <) =only
446 % 2 index (%stdout) (w) file exch writehexstring
447 % (> <) =only
448 % 1 index (%stdout) (w) file exch writehexstring
449 % (>\n) =only flush
450 % quit
451 % } if
452 % } if
453 % -------------------------------------------------------------
454
455 1 index exch 0 exch getinterval
456 % cid_base code_lo code_hi prefix
457 dup length 3 index length exch sub
458 % cid_base code_lo code_hi prefix range_len
459 dup 255 gt {
460 (too long coderange specification for current GS\n) print
461 /.endmapranges cvx /rangecheck signalerror
462 } if
463 <00 01 00 02> 4 string copy % create initialized param
464 dup 0 4 -1 roll put % put range_len into param
465
466 % get key_hi
467 3 -1 roll dup length 3 index length dup 3 1 roll sub getinterval
468
469 % get key_lo
470 4 -1 roll dup length 4 index length dup 3 1 roll sub getinterval
471
472 % make "keys" (concatenated key_lo + key_hi)
473 exch concatstrings
474
475 %
476 4 -1 roll
477 .endmapvalue
478
479 % See if we can merge with the previous value.
480 % The prefix, params, and font index must match.
481 % prefix params keys value fontindex
482 counttomark 9 gt { % 2 (or more) ranges (1 range = 5 item)
483 4 index 10 index eq % compare prefix
484 4 index 10 index eq and % compare params
485 1 index 7 index eq and % compare fontindex
486 {
487 CMAPDEBUG { (merge!\n) print } if
488 pop 4 2 roll pop pop
489 % prefix params keys value fontindex keys2 value2
490 5 -1 roll 3 -1 roll concatstrings
491 % prefix params value fontindex value2 keys'
492 4 -1 roll 3 -1 roll concatstrings
493 % prefix params fontindex keys' values'
494 3 -1 roll
495 } if
496 } if % end of 2 (or more) ranges
497 } repeat
498 counttomark 2 add -1 roll .appendmap
499 } .internalbind def
500
501 /.endmapvalue { % <cid> .endmapvalue (hi,lo) .FontIndex
502 2 string dup 0 3 index -8 bitshift put % value
503 dup 1 4 -1 roll 255 and put
504 .FontIndex % font_index
505 } .internalbind def
506
507 % ------ notdef operators ------ %
508
509 /beginnotdefchar { % <count> beginnotdefchar -
510 pop mark
511 } .internalbind def
512 /endnotdefchar { % <code> <cid> ... endnotdefchar -
513 2 .endmapchars
514 } .internalbind def
515
516 /beginnotdefrange { % <count> beginnotdefrange -
517 pop mark
518 } .internalbind def
519 /endnotdefrange { % <code_lo> <code_hi> <cid> ... endnotdefrange -
520 2 .endmapranges
521 } .internalbind def
522
523 % ---------------- Resource category definition ---------------- %
524
525 currentdict end
526
527 languagelevel exch 2 .setlanguagelevel
528
529 /CMap /Generic /Category findresource dup length dict .copydict
530 dup /InstanceType /dicttype put
531 dup /DefineResource {
532 % The AdobePS5 Windows driver emits code that attempts to
533 % create CMaps without the required CMapName entry.
534 % Work around this here.
535 dup /CMapName known not {
536 dup wcheck not {
537 .currentglobal exch dup wcheck .setglobal
538 dup length dict .copydict exch .setglobal
539 } if
540 dup gcheck 2 index gcheck not and {
541 exch .currentglobal exch //true .setglobal
542 dup length string copy exch .setglobal exch
543 } if dup /CMapName 3 index put
544 } if
545 % Adobe PS CET 23-25 and others define an almost empty CMap. Tolerate this.
546 % With the above, we can actually tolerate: /name << >> defineresource
547 dup /CIDSystemInfo known not {
548 dup wcheck not {
549 .currentglobal exch dup wcheck .setglobal
550 dup length dict .copydict exch .setglobal
551 } if
552 dup /CIDSystemInfo [ //null ] put
553 } if
554 dup /CodeMap .knownget { //null eq { .buildcmap } if } if
555 /Generic /Category findresource /DefineResource get exec
556 } .internalbind put
557 /Category defineresource pop
558 % We might have loaded CID font support already.
559 /CIDInit /ProcSet 2 copy { findresource } //.internalstopped exec
560 % An interior `stopped' might have reset VM allocation to local.
561 //true .setglobal
562 { pop pop 3 -1 roll }
563 { dup length 4 index length add dict .copydict 4 -1 roll exch .copydict }
564 ifelse exch defineresource pop
565
566 currentdict /.rewriteTempMapsNotDef .undef
567
568 .setlanguagelevel