1 % Copyright (C) 1995, 2000 Aladdin Enterprises. All rights reserved.
3 % This software is provided AS-IS with no warranty, either express or
6 % This software is distributed under license and may not be copied,
7 % modified or distributed except as expressly authorized under the terms
8 % of the license contained in the file LICENSE in this distribution.
10 % For more information about licensing, please refer to
11 % http://www.ghostscript.com/licensing/. For information on
12 % commercial licensing, go to http://www.artifex.com/licensing/ or
13 % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14 % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
16 % $Id: gs_cmap.ps,v 1.19 2005/08/17 21:54:55 igor Exp $
17 % ProcSet for implementing CMap resources.
18 % When this is run, systemdict is still writable.
20 % NOTE: Rearranged fonts are not implemented yet.
25 ] {dup where {pop pop} { currentdict exch //false def pop } ifelse} forall
27 % ---------------- Public operators ---------------- %
29 /.rewriteTempMapsNotDef {
31 % Before building .CodeMapData from .TempMaps,
32 % we need to replace dst type codes in the notdef map with the value 3,
33 % which corresponds to CODE_VALUE_NOTDEF, see gxfcmap.h .
35 CMAPDEBUG { (rewriting TempMapsNotDef\n) print flush } if
39 CMAPDEBUG { (...original...\n) print flush } if
40 1 5 2 index length 1 sub {
41 { 1 index exch get 2 3 put } stopped
42 { CMAPDEBUG { (cannot rewrite\n) print flush } if }
43 { CMAPDEBUG { (rewrite\n) print flush } if } ifelse
47 CMAPDEBUG { (...FINISHED...\n) print } if
50 % composefont doesn't appear in CMap files -- it's documented in
51 % the "PostScript Language Reference Manual Supplement".
52 /composefont { % <name> <cmap|cmapname> <fonts> composefont <font>
54 /CMap 2 index dup type /dicttype ne { /CMap findresource } if def
55 /FDepVector 1 index cvlit def % temporarily
56 /Encoding [ 0 1 FDepVector length 1 sub { } for ] def
57 /FontInfo 1 dict def % for .processToUnicode in pdf_font.ps .
58 /FDepVector [ 0 1 FDepVector length 1 sub {
59 % Stack: name cmap[name] fonts /FDepVector [ fonts... i
60 FDepVector 1 index get
61 dup type /dicttype ne {
62 dup /CIDFont resourcestatus {
68 exch CMap /FontMatrices get dup length 2 index gt {
69 exch get dup //null eq { pop } { makefont } ifelse
75 /FontMatrix matrix def
77 CMap /WMode .knownget { /WMode exch def } if
79 pop pop currentdict end /Font defineresource
82 % ---------------- CMap operators ---------------- %
86 % Our internal .CodeMapData structure closely mirrors the structures
87 % defined in gxfcmap.h (q.v.). () indicate a string, [] indicate an array,
88 % ? indicates a Boolean, # indicates an integer, {} for grouping.
89 % [[[(first) (last) ...]+] % code space ranges
90 % [[(prefix) (key_size,?is_range,value_type,value_size) (keys...)
91 % {(values...) | [value ...]} #font_index ]+] % code mappings
93 % <<same>> % notdef mappings
95 % FontMatrices is the array of matrices defined by begin/endusematrix.
96 % All of the arrays and strings are read-only after they have been built.
98 % Note that the code in zfcmap.c that constructs the C structures from
99 % the PostScript structures has intimate knowledge of the above format.
101 % ****** NOTE: The code currently only handles "well-behaved" CMaps:
102 % - CID values only (no bfchars), 16-bit
103 % - Entries (both code space and map) must be sorted
104 % - Only the last byte must vary in each map range, except for
105 % the identity mapping
107 % ------ Font-level operators ------ %
109 /begincmap { % - begincmap -
110 /.CodeMapData [[[]] [[]] [[]]] def
113 /.TempMaps [20 dict 50 dict 50 dict] def
114 /CodeMap //null def % for .buildcmap
117 /endcmap { % - endcmap -
118 .rewriteTempMapsNotDef
121 2 (*** undefined charmap ***)
122 1 (*** defined charmap ***)
123 0 (*** code space ranges ***)
126 0 1 2 index length 1 sub {
134 /.CodeMapData dup load [ exch
135 .TempMaps aload pop begin begin begin
138 0 1 currentdict length 1 sub {
147 (*** Content of .CodeMapData ***) =
148 0 .CodeMapData { exch dup == 1 add exch (\t) print == } forall
152 currentdict /.TempMaps undef
153 /FontMatrices FontMatrices .endmap def
156 /.endmap { % <map> .endmap <map>
157 dup type /arraytype eq {
158 % This might be a shared read-only array inherited via usecmap.
159 % Don't try to update its elements if this is the case.
161 0 1 2 index length 1 sub {
162 2 copy 2 copy get .endmap put pop
166 dup type /stringtype eq { readonly } if
170 /.appendmap { % -mark- <elt> ... <array#> .appendmap -
171 .TempMaps exch get counttomark 1 add 1 roll
172 ] 1 index length exch put
175 /begincodespacerange { % <count> begincodespacerange -
179 /endcodespacerange { % <code_lo> <code_hi> ... endcodespacerange -
183 /usecmap { % <CMap_name> usecmap -
184 /CMap findresource dup
185 % Copy the top level of .CodeMapData
186 /.CodeMapData exch /.CodeMapData get copyarray def
187 /FontMatrices exch /FontMatrices get copyarray def
190 /usefont { % <fontID> usefont -
194 /beginusematrix { % <fontID> beginusematrix -
195 FontMatrices wcheck not FontMatrices length 2 index le or {
196 FontMatrices length 1 index 1 add .max array
197 dup 0 FontMatrices putinterval
198 /FontMatrices exch def
202 /endusematrix { % <matrix> endusematrix -
203 FontMatrices 3 1 roll put
206 % ------ Rearranged font operators ------ %
208 /beginrearrangedfont { % <font_name> <font*> beginrearrangedfont -
214 /endrearrangedfont { % - endrearrangedfont -
215 (REARRANGED FONTS NOT IMPLEMENTED YET.) = flush
216 FontName .FontNames 0 get findfont end definefont pop
219 % ------ Character name/code selector operators ------ %
221 /beginbfchar { % <count> beginbfchar -
224 /endbfchar { % <code> <to_code|charname> ... endbfchar
226 counttomark -2 roll % process in correct order
228 } repeat 1 .appendmap
231 /beginbfrange { % <count> beginbfrange -
234 /endbfrange { % <code_lo> <code_hi> <to_code|(charname*)> ...
237 counttomark -3 roll % process in correct order
238 dup type dup /arraytype eq exch /packedarraytype eq or {
239 % Array value, split up.
241 % Stack: code to_code|charname
242 1 index exch .addbfchar
243 % Increment the code. As noted above, we require
244 % that only the last byte vary, but we still must
245 % mask it after incrementing, in case the last
247 % Stack: code prefix params key value fontindex
248 6 -1 roll dup length string copy
249 dup dup length 1 sub 2 copy get 1 add 255 and put
252 % Single value, handle directly.
255 } repeat 1 .appendmap
258 /.addbfchar { % <code> <to_code|charname> .addbfchar
259 % <prefix> <params> <key> <value> <font_index>
260 1 index exch .addbfrange
262 /.addbfrange { % <code_lo> <code_hi> <to_code|charname>
263 % .addbfrange <<same as .addbfchar>>
265 3 index type /nametype eq {
266 2 index 2 1 put % dst = CODE_VALUE_GLYPH, see gxfcmap.h .
267 4 -1 roll 1 array astore 4 1 roll 4
269 2 index 2 2 put % dst = CODE_VALUE_CHARS, see gxfcmap.h .
272 % Stack: code_lo code_hi value params
275 3 -1 roll pop exch () exch
278 dup 0 1 put dup 1 1 put
280 dup dup length 1 sub 0 exch getinterval 5 1 roll % prefix
281 % Stack: prefix value params code_lo code_hi
282 2 { exch dup length 1 sub 1 getinterval } repeat concatstrings
288 % ------ CID selector operators ------ %
290 /begincidchar { % <count> begincidchar -
293 /endcidchar { % <code> <cid> ... endcidchar -
297 /begincidrange { % <count> begincidrange -
300 /endcidrange { % <code_lo> <code_hi> <cid_base> ... endcidrange -
304 /.endmapchars { % -mark- <code> <cid> ... <map#> .endmapchars -
305 counttomark 1 add 1 roll
307 counttomark -2 roll % process in correct order
309 % Construct prefix, params, key, value, font_index
310 dup length 1 eq { % 1-byte
311 <00 00 00 02> () % <prefix> <param> <null_key>
313 dup 0 1 getinterval exch % make 1-byte prefix
315 USE_CIDCHAR_AS_RANGE {
316 <00 01 00 02> % skelton for param
318 <00 00 00 02> % skelton for param
321 exch % <prefix> <param> <code>
322 dup length % <prefix> <param> <code> N
323 1 sub % <prefix> <param> <code> N-1
324 dup % <prefix> <param> <code> N-1 N-1
325 3 index % <prefix> <param> <code> N-1 N-1 <param>
326 exch % <prefix> <param> <code> N-1 <param> N-1
327 0 exch % <prefix> <param> <code> N-1 <param> 0 N-1
328 put % <prefix> <param'> <code> N-1
329 1 exch % <prefix> <param'> <code> 1 N-1
330 getinterval % <prefix> <param'> <key>
332 USE_CIDCHAR_AS_RANGE {
333 dup length 2 mul string % <key> <dkey>
334 dup % <key> <dkey> <dkey>
335 2 index % <key> <dkey> <dkey> <key>
336 0 exch putinterval % <key> <dkey'>
337 dup % <key> <dkey'> <dkey'>
338 3 -1 roll % <dkey'> <dkey'> <key>
339 dup length % <dkey'> <dkey'> <key> N-1
340 exch putinterval % <dkey''>
345 4 -1 roll % <prefix> <param'> <key> <cid>
346 .endmapvalue % <prefix> <param'> <key> <hex_cid> <font_idx>
347 % prefix params keys value fontindex
348 counttomark 5 gt { % 2 (or more) ranges (1 range = 5 item)
349 4 index 10 index eq % compare prefix
350 4 index 10 index eq and % compare params
351 1 index 7 index eq and % compare fontindex
353 CMAPDEBUG { (merge! char\n) print } if
355 % prefix params keys value fontindex keys2 value2
356 5 -1 roll 3 -1 roll concatstrings
357 % prefix params value fontindex value2 keys'
358 4 -1 roll 3 -1 roll concatstrings
359 % prefix params fontindex keys' values'
362 } if % end of 2 (or more) ranges
364 ( prefix:) print 4 index =only
365 ( param:) print 3 index =only
366 ( key:) print 2 index =only
367 ( hex_cid:) print 1 index =only
368 ( font_idx:) print 0 index == flush
371 counttomark 2 add -1 roll .appendmap
374 /.endmapranges { % -mark- <code_lo> <code_hi> <cid_base> ... <map#>
376 counttomark 1 add 1 roll
378 counttomark -3 roll % process in correct order
379 % Construct prefix, params, key_lo, key_hi, value, font_index
380 3 1 roll % <cid_base> <code_lo> <code_hi>
385 % N-byte range: (*) (*)
386 dup 2 index eq { % <code_lo> == <code_hi>
387 % 0: prefix_len for 1-byte code
388 % 1: prefix_len for N-byte code
389 dup length 1 eq { 0 } { 1 } ifelse
390 } { % <code_lo> != <code_hi>
391 % calculate prefix_len for *-byte range
392 dup length 1 sub % <cid_base> <code_lo> <code_hi> <code_len-1>
393 0 % initial value for N
394 { % <cid_base> <code_lo> <code_hi> (code_len-1) N
395 dup 2 index ge { exit } if % if (N >= len - 1) exit
396 3 index 1 index get % N-th byte of code_lo
397 3 index 2 index get % N-th byte of code_hi
398 eq { 1 add } { exit } ifelse
400 exch pop % discard <code_len-1>
402 % cid_base code_lo code_hi prefix_len
404 % Althogh Adobe CPSI with native CID/CMap support accept
405 % multi-dimensional range specification in notdef & cidrange
406 % (and CID is calculated as relative position in multi-dimensional
407 % range), but older CPSI & ATM cannot handle it.
409 % GS accepts such specification, but it's recommended to keep
410 % from using this feature for notdef & cidrange.
411 % Following is a disabler of this feature.
412 % -------------------------------------------------------------
413 % counttomark 1 add index % get map#
414 % 0 ne { % if not codespacerange
415 % 1 index length % get code length
416 % 1 index % get prefix length
417 % sub % calculate key length
418 % 1 gt { % if (key_len > 1),
419 % (.endmapranges error) = flush
420 % (multi-dimensional range specification is used out of codespacerange)
423 % CMapName CMapName length string cvs =only
425 % 2 index (%stdout) (w) file exch writehexstring
427 % 1 index (%stdout) (w) file exch writehexstring
432 % -------------------------------------------------------------
434 1 index exch 0 exch getinterval
435 % cid_base code_lo code_hi prefix
436 dup length 3 index length exch sub
437 % cid_base code_lo code_hi prefix range_len
439 (too long coderange specification for current GS\n) print
442 <00 01 00 02> 4 string copy % create initialized param
443 dup 0 4 -1 roll put % put range_len into param
446 3 -1 roll dup length 3 index length dup 3 1 roll sub getinterval
449 4 -1 roll dup length 4 index length dup 3 1 roll sub getinterval
451 % make "keys" (concatenated key_lo + key_hi)
458 % See if we can merge with the previous value.
459 % The prefix, params, and font index must match.
460 % prefix params keys value fontindex
461 counttomark 5 gt { % 2 (or more) ranges (1 range = 5 item)
462 4 index 10 index eq % compare prefix
463 4 index 10 index eq and % compare params
464 1 index 7 index eq and % compare fontindex
466 CMAPDEBUG { (merge!\n) print } if
468 % prefix params keys value fontindex keys2 value2
469 5 -1 roll 3 -1 roll concatstrings
470 % prefix params value fontindex value2 keys'
471 4 -1 roll 3 -1 roll concatstrings
472 % prefix params fontindex keys' values'
475 } if % end of 2 (or more) ranges
477 counttomark 2 add -1 roll .appendmap
480 /.endmapvalue { % <cid> .endmapvalue (hi,lo) .FontIndex
481 2 string dup 0 3 index -8 bitshift put % value
482 dup 1 4 -1 roll 255 and put
483 .FontIndex % font_index
486 % ------ notdef operators ------ %
488 /beginnotdefchar { % <count> beginnotdefchar -
491 /endnotdefchar { % <code> <cid> ... endnotdefchar -
495 /beginnotdefrange { % <count> beginnotdefrange -
498 /endnotdefrange { % <code_lo> <code_hi> <cid> ... endnotdefrange -
502 % ---------------- Resource category definition ---------------- %
506 languagelevel exch 2 .setlanguagelevel
508 /CMap /Generic /Category findresource dup length dict .copydict
509 dup /InstanceType /dicttype put
510 dup /DefineResource {
511 % The AdobePS5 Windows driver emits code that attempts to
512 % create CMaps without the required CMapName entry.
513 % Work around this here.
514 dup /CMapName known not {
516 .currentglobal exch dup wcheck .setglobal
517 dup length dict .copydict exch .setglobal
519 dup gcheck 2 index gcheck not and {
520 exch .currentglobal exch //true .setglobal
521 dup length string copy exch .setglobal exch
522 } if dup /CMapName 3 index put
524 dup /CodeMap get //null eq { .buildcmap } if
525 /Generic /Category findresource /DefineResource get exec
527 /Category defineresource pop
528 % We might have loaded CID font support already.
529 /CIDInit /ProcSet 2 copy { findresource } .internalstopped
530 % An interior `stopped' might have reset VM allocation to local.
532 { pop pop 3 -1 roll }
533 { dup length 4 index length add dict .copydict 4 -1 roll exch .copydict }
534 ifelse exch defineresource pop