Debian Codegraph @

Resource/Init/gs_ttf.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 % Support code for direct use of TrueType fonts.
17 % (Not needed for Type 42 fonts.)
18
19 % Note that if you want to use this file without including the ttfont.dev
20 % option when you built Ghostscript, you will need to load the following
21 % files before this one:
22 % lib/gs_mgl_e.ps
23 % lib/gs_mro_e.ps
24 % lib/gs_wan_e.ps
25
26 % Thanks to B. Jackowski and GUST (the Polish TeX Users' Group) for
27 % the glyf-splitting code.
28
29 % ---------------- Font loading machinery ---------------- %
30
31 % Augment the FONTPATH machinery so it recognizes TrueType fonts.
32
33 /.scanfontheaders where {
34 /.scanfontheaders [
35 /.scanfontheaders .systemvar aload pop (\000\001\000\000*) (true*) (wOFF*)
36 ] put
37 } if
38
39 % ---------------- Automatic Type 42 generation ---------------- %
40
41 % Load a TrueType font from a file as a Type 42 PostScript font.
42 % The thing that makes this really messy is the handling of encodings.
43 % There are 2 interacting tables that affect the encoding:
44 % 'cmap' provides multiple maps from character codes to glyph indices
45 % 'post' maps glyph indices to glyph names (if present)
46 % What we need to get out of this is:
47 % Encoding mapping character codes to glyph names
48 % (the composition of cmap and post)
49 % CharStrings mapping glyph names to glyph indices
50 % (the inverse of post)
51 % If the post table is missing, we have to take a guess based on the cmap
52 % table.
53
54 /.loadttfontdict 50 dict dup begin
55
56 /orgXUID AladdinEnterprisesXUID def
57 /maxstring 32764 def % half the maximum length of a PostScript string,
58 % must be a multiple of 4 (for hmtx / loca / vmtx)
59
60 /.invert_encoding % <array> invert_encoding <dict>
61 { dup 256 dict exch
62 0 exch 1 exch length 1 sub { % [] <> i
63 dup 3 index exch get % [] <> i v
64 dup /.notdef ne {
65 exch 2 index 2 index .knownget {
66 dup type /arraytype eq {
67 [ exch aload pop counttomark 2 add -1 roll ]
68 } {
69 exch 2 array astore
70 } ifelse
71 } if 2 index 3 1 roll put
72 } {
73 pop pop
74 } ifelse
75 } for
76 exch pop
77 } .forcebind def
78
79 % Make /MacRomanEncodingForTrueType including additional
80 % characters in Mac OS Roman encoding, which is missing
81 % in /MacRomanEncoding. See PDF spec 1.7, p. 431 or
82 % "Inside Macintosh: Text" 1-55, Figure 1-36.
83 % This is useful to exploit the glyphs via Apple Roman
84 % TrueType cmap subtable, but not appropriate for information
85 % interchange.
86
87 /MacRomanEncodingForTrueType
88 /MacRomanEncoding .findencoding
89 dup length array copy
90 <<
91 /notequal 173
92 /infinity 176
93 /lessequal 178
94 /greaterequal 179
95 /partialdiff 182
96 /summation 183
97 /product 184
98 /pi 185
99 /integral 186
100 /Omega 189
101 /radical 195
102 /approxequal 197
103 /Delta 198
104 /lozenge 215
105 /Euro 219
106 /apple 240
107 >> {
108 TTFDEBUG { (Extend MacRomanEncodingForTrueType for TrueType: ) =only } if
109
110 % check /.notdef to avoid overwriting
111 2 index 1 index get dup /.notdef eq {
112 TTFDEBUG { (insert /) =only 2 index =only ( @ ) =only 1 index //== exec } if
113 pop
114 exch 2 index 3 1 roll put
115 } {
116 TTFDEBUG { (cannot insert /) =only 2 index =only ( @ ) =only 1 index =only ( used for ) =only dup //== exec } if
117 pop
118 pop pop
119 } ifelse
120 } forall
121 aload pop
122 256 packedarray
123 5 1 index .registerencoding
124 .defineencoding
125
126 % Define the Macintosh standard mapping from characters to glyph indices.
127 /MacRomanEncoding dup .findencoding def
128 /MacGlyphEncoding dup .findencoding def
129 /MacRomanEncodingForTrueType dup .findencoding def
130
131 % Invert the MacRomanEncoding.
132 /.romanmacdict MacRomanEncodingForTrueType .invert_encoding def
133 /.latin1isodict ISOLatin1Encoding .invert_encoding def
134
135 % Define remapping for misnamed glyphs in TrueType 'post' tables.
136 % There are probably a lot more than this!
137 /postremap mark
138 /Eoverdot /Edotaccent
139 /eoverdot /edotaccent
140 .dicttomark readonly def
141
142 % Array used for fast pre-filling of cmap array
143 /.array1024z [ 1024 { 0 } repeat ] def
144
145 % ---- Utilities ---- %
146
147 % Define a serial number for creating unique XUIDs for TrueType fonts.
148 % We used to use the checkSumAdjustment value from the font, but this is
149 % not reliable, since some fonts don't set it correctly.
150 % Note that we must do this in a string to make it immune to save/restore.
151
152 % handle 64 bit integer objects
153 /xuidstring 16#ffffffff 0 gt
154 {
155 <8000000000000000>
156 }
157 {
158 <80000000>
159 } ifelse def
160 /curxuid { % - curxuid <int>
161 //.getCPSImode
162 //false //.setCPSImode
163 0 xuidstring { exch 8 bitshift exch add } forall
164 % for tbe benefit of pdfwrite/ps2write we want the resulting XUID contents
165 % to fit into a 32 bit value
166 16#ffffffff 0 gt
167 {
168 16#ffffffff idiv
169 } if
170 exch //.setCPSImode
171 } .forcebind def
172 /nextxuid { % - nextxuid -
173 3 -1 0 {
174 xuidstring 1 index 2 copy get dup 255 ne {
175 1 add put pop exit
176 } if pop 0 put pop
177 } for
178 } .internalbind def
179
180 % <string> <index> getu16 <integer>
181 /getu16 {
182 2 copy get 8 bitshift 3 1 roll 1 add get add
183 } .forcebind def
184
185 % <string> <index> gets16 <integer>
186 /gets16 {
187 getu16 16#8000 xor 16#8000 sub
188 } .forcebind def
189
190 % <string> <index> getu32 <integer>
191 /getu32 {
192 2 copy getu16 16 bitshift 3 1 roll 2 add getu16 add
193 } .forcebind def
194
195 % <string> <index> gets32 <integer>
196 /gets32 {
197 2 copy gets16 16 bitshift 3 1 roll 2 add getu16 add
198 } .bind def
199
200 % <string|array> <index> getu16 <integer>
201 /getu16a {
202 2 getinterval_from_stringarray 0 getu16
203 } .forcebind def
204
205 % <string|array> <index> gets16 <integer>
206 /gets16a {
207 2 getinterval_from_stringarray 0 gets16
208 } .forcebind def
209
210 % <string|array> <index> getu32a <integer>
211 /getu32a {
212 4 getinterval_from_stringarray 0 getu32
213 } .forcebind def
214
215 % <string|array> <index> gets32a <integer>
216 /gets32a {
217 4 getinterval_from_stringarray 0 gets32
218 } .bind def
219
220 16#ffffffff 0 gt { % 64-bit sign extension
221 { /gets32 /gets32a} {
222 mark 1 index load aload pop { 16#80000000 xor 16#80000000 sub } aload pop
223 //.packtomark exec cvx def
224 } .bind forall
225 } if
226
227 % <string> <index> <integer> putu16 -
228 /putu16 {
229 3 copy -8 bitshift put
230 exch 1 add exch 16#ff and put
231 } .forcebind def
232
233 % <string> <index> <integer> putu32 -
234 /putu32 {
235 3 copy -16 bitshift putu16
236 exch 2 add exch 16#ffff and putu16
237 } .forcebind def
238
239 % <nametable> <nameid> findname <string> true
240 % <nametable> <nameid> findname false
241 /findname {
242 TTFDEBUG { (findname: ) print dup =only } if
243 [ //false 4 2 roll
244 {
245 0 1 3 index 2 getu16 1 sub {
246 % Stack: false table id index
247 12 mul 6 add 2 index exch 12
248 % Check the table is actually long enough to contain the requested index
249 1 index add 2 index length gt
250 {
251 /pdfformaterror where
252 {
253 ( **** TrueType font has malformed name table.\n) exch /pdfformaterror get exec
254 PDFSTOPONERROR {
255 /.loadpdfttfont cvx /invalidfont signalerror
256 } if
257 } if
258 pop pop
259 exit
260 } if
261 12 getinterval
262 dup 6 getu16 2 index eq 1 index 8 getu16 0 ne and {
263 % We found the name we want.
264 exch pop
265 % Stack: false table record
266 dup 10 getu16 2 index 4 getu16 add
267 1 index 8 getu16 4 -1 roll 3 1 roll
268 3 copy add 1 index length
269 le {
270 pop
271 getinterval exch
272 % Stack: false string record
273 % Check for 8- vs. 16-bit characters.
274 is2byte { //true } { dup is2byte2 } ifelse { string2to1 } if //true //null 4 -1 roll exit
275 } {
276 pop pop pop pop
277 //false
278 exit
279 } ifelse
280 } if pop
281 } for
282 } stopped {
283 cleartomark //false
284 } {
285 pop pop
286 counttomark 1 add -1 roll pop
287 } ifelse
288 TTFDEBUG {
289 dup { ( = ) print 1 index //== exec } { ( not found) = } ifelse
290 } if
291 } .forcebind def
292
293 % <namerecord> is2byte <bool>
294 /is2byte {
295 dup 0 getu16
296 {
297 { pop //true } % Apple Unicode
298 { pop //false } % Macintosh Script manager
299 { 1 getu16 1 eq } % ISO
300 { 1 getu16 1 eq } % Microsoft
301 { pop //false } % default - *MUST* be last
302 }
303 % check for a valid Platform ID, if we don't get one, assume single byte encoding
304 dup length 1 sub
305 2 index lt {exch pop dup length 1 sub}{exch} ifelse
306 get exec
307 } .forcebind def
308
309 % <string> is2byte2 <bool>
310 /is2byte2 {
311 dup length
312 dup 2 mod 0 ne {
313 pop pop //false
314 } { % s l
315 //true exch % s b l
316 1 sub 0 exch 2 exch {
317 2 index exch get 0 ne {
318 pop //false exit
319 } if
320 } for
321 exch pop
322 } ifelse
323 } .forcebind def
324
325 % <string2> string2to1 <string>
326 /string2to1 {
327 dup length 2 idiv string dup
328 0 1 3 index length 1 sub {
329 3 index 1 index 2 mul 1 add get put dup
330 } for pop exch pop
331 } .forcebind def
332
333 % Each procedure in this dictionary is called as follows:
334 % <encodingtable> proc <glyph dictionary>
335 /cmapformats mark
336 0 { % Apple standard 1-to-1 mapping.
337 6 256 getinterval_from_stringarray
338 mark 0 3 -1 roll
339 { 1 index 1 add } forall pop
340 .dicttomark
341 } .forcebind
342 2 { % Apple 16bit CJK (ShiftJIS etc)
343
344 % /sHK_sz subHeaderKey_size % 1 * uint16
345 % /sH_sz subHeader_size % 4 * uint16
346 % /sH_len subHeader_length
347 % /cmapf2_tblen total table length
348 % /cmapf2_lang language code (not used)
349 % /sHKs subHeaderKeys
350
351 /sHK_sz 2 def
352 /sH_sz 8 def
353 dup 2 getu16a /cmapf2_tblen exch def
354
355 dup 4 getu16a /cmapf2_lang exch def
356
357 dup 6 256 sHK_sz mul getinterval_from_stringarray /sHKs exch def
358
359 0 % initialization value for /sH_len
360 0 1 255 {
361 sHKs exch
362 2 mul getu16a
363 1 index % get current max
364 1 index % get current subHeaderKey
365 lt {exch} if pop
366 } for
367 /sH_len exch def
368
369 dup 6 256 sHK_sz mul add
370 cmapf2_tblen 1 index sub getinterval_from_stringarray
371 /sH_gIA exch def
372
373 /cmapf2_glyph_array 0 dict def
374
375 /.cmapf2_putGID {
376 /cmapf2_ch cmapf2_ch_hi 8 bitshift cmapf2_ch_lo add def
377 firstCode cmapf2_ch_lo le
378 cmapf2_ch_lo firstCode entryCount add lt
379 and { % true: j is inside
380 sH_offset idRangeOffset add % offset to gI
381 cmapf2_ch_lo firstCode sub 2 mul % rel. pos. in range
382 add 6 add % offset in sH_gIA
383 sH_gIA exch getu16a
384 dup 0 gt { %
385 idDelta add
386 cmapf2_glyph_array exch cmapf2_ch exch put
387 } {
388 pop
389 % cmapf2_glyph_array cmapf2_ch 0 put
390 } ifelse
391 } { % false: j is outside
392 % cmapf2_glyph_array cmapf2_ch 0 put
393 } ifelse
394 } def
395
396 16#00 1 16#ff { % hi_byte scan
397 /cmapf2_ch_hi exch def
398 sHKs cmapf2_ch_hi sHK_sz mul getu16a
399 /sH_offset exch def
400 sH_gIA sH_offset sH_sz getinterval
401 dup 0 getu16a /firstCode exch def
402 dup 2 getu16a /entryCount exch def
403 dup 4 gets16a /idDelta exch def
404 dup 6 getu16a /idRangeOffset exch def
405 pop
406 sH_offset 0 eq {
407 /cmapf2_ch_lo cmapf2_ch_hi def
408 /cmapf2_ch_hi 0 def
409 .cmapf2_putGID
410 } {
411 16#00 1 16#ff { % lo_byte scan
412 /cmapf2_ch_lo exch def
413 .cmapf2_putGID
414 } for
415 } ifelse
416 } for
417 pop
418 cmapf2_glyph_array
419 } .forcebind
420 4 { % Microsoft/Adobe segmented mapping.
421 /etab exch def
422 /nseg2 etab 6 getu16a def
423 14 /endc etab 2 index nseg2 getinterval_from_stringarray def
424 % The Apple TrueType documentation omits the 2-byte
425 % 'reserved pad' that follows the endCount vector!
426 2 add
427 nseg2 add /startc etab 2 index nseg2 getinterval_from_stringarray def
428 nseg2 add /iddelta etab 2 index nseg2 getinterval_from_stringarray def
429 nseg2 add /idroff etab 2 index nseg2 getinterval_from_stringarray def
430 % The following hack allows us to properly handle
431 % idiosyncratic fonts that start at 0xf000:
432 pop
433 /firstcode startc 0 getu16a 16#ff00 and dup 16#f000 ne { pop 0 } if def
434 /putglyph {
435 glyphs code known
436 {
437 glyphs /.cmap_warning_issued known not {
438 (**** Warning: Invalid TTF cmap mapping (overlapping/repeated map)\n) print flush
439 glyphs /.cmap_warning_issued //true put
440 } if
441 pop
442 }
443 {glyphs code 3 -1 roll put}ifelse
444 /code code 1 add def
445 } bind def
446 /glyphs 0 dict def
447
448 % neg2 is number of segments x 2. A format 4 cmap, with usable
449 % mappings cannot (according to the spec) have only 1 segment,
450 % since format 4 requires a closing segment with start and end
451 % codes being 0xffff. Unfortunately, we have found badly subsetted
452 % TTF fonts which have one segment with valid mappings, and no
453 % closing segment.
454 % In such a case, force the loop to work as though as we have two
455 % segments - this works as we don't rely on the terminating segment
456 % but use the loop counter to spot completion - effrectively, we
457 % ignore the closing segment when it's present, anyway.
458 %
459 % This loop and/or putglyph do not account for out of spec fonts
460 % that have repeated or overlapping ranges in the segments. As we
461 % write the code/gid to a PS dictionary, later encounters with a
462 % given code will overwrite the earlier one(s). As this is outside
463 % of the spec, it's not clear how other consumers handle this
464 % condition, so we'll await a suitable example before making changes.
465 % Two choices are: drop later repeated/overlapping segments entirely,
466 % or only use codes from later, overlapping segments not already set
467 % by the earlier segment. (Inspired by Bug 700968).
468 % Revision: bug 703589 has an (invalid) cmap table with overlapping
469 % (actually repeating) ranges, and requires the *first* range definition
470 % to be used in order to render correctly. So /putglyph now refuses to
471 % overwrite existing keys in the "glyphs" dictionary
472 0 2 nseg2 dup 4 lt {pop 4}if 3 sub {
473 /i2 exch def
474 /scode startc i2 getu16a def
475 /ecode endc i2 getu16a def
476 % Bug 693538: see above
477 scode 0 eq ecode 0 eq and not {
478 scode ecode 1 add gt { /ecode scode 1 add def } if % Bug 691326. See above.
479 /code scode def
480 /delta iddelta i2 gets16a def
481 TTFDEBUG {
482 (scode=) print scode =only
483 ( ecode=) print ecode =only
484 ( delta=) print delta =only
485 ( droff=) print idroff i2 getu16 =
486 } if
487 idroff i2 getu16a dup 0 eq {
488 pop scode delta add 65535 and 1 ecode delta add 65535 and
489 { putglyph } for
490 } { % The +2 is for the 'reserved pad'.
491 /gloff exch 14 nseg2 3 mul add 2 add i2 add add def
492 0 1 ecode scode sub {
493 2 mul gloff add etab exch getu16a
494 dup 0 ne { delta add 65535 and } if putglyph
495 } for
496 } ifelse
497 } if
498 } for
499 % If we've encoutered an invalid table, remove the key before returning
500 glyphs /.cmap_warning_issued undef
501 glyphs /glyphs //null def % for GC
502 } .forcebind
503 6 { % Single interval lookup.
504 dup 6 getu16a /firstcode exch def
505 dup 8 getu16a /ng exch def
506 ng dict
507 dup
508 % Stack: tab dict dict
509 % Fill elements firstcode .. firstcode+nvalue-1 with glyph values
510 0 1 ng 1 sub {
511 dup firstcode add exch
512 2 mul 10 add 4 index exch getu16a 3 copy put pop pop
513 } for pop exch pop
514 } .forcebind
515 12 { % Microsoft/Adobe segmented mapping.
516 /etab exch def
517 /glyphs 0 dict def
518
519 0 1
520 etab 12 getu32a % nGroups
521 1 sub {
522 /ind exch def
523 /gid etab 12 ind mul 8 add 16 add getu32a def % startGlyphID
524 etab 12 ind mul 16 add getu32a % startCharCode
525 1
526 etab 12 ind mul 4 add 16 add getu32a % endCharCode
527 {
528 gid glyphs 3 1 roll put
529 /gid gid 1 add def
530 } for
531 } for
532 glyphs /glyphs //null def % for GC
533 } .forcebind
534 .dicttomark readonly def % cmapformats
535
536 % <cmaptab> cmapdict <glyph dictionary>
537 /cmapdict {
538 dup 0 getu16a cmapformats exch .knownget {
539 TTFDEBUG {
540 (cmap: format ) print 1 index 0 getu16a = flush
541 } if exec
542 } {
543 (Can't handle format ) print 0 getu16a = flush
544 mark 0 1 255 { dup } for .dicttomark
545 } ifelse
546 TTFDEBUG {
547 (cmap: length=) print dup length = dup ===
548 } if
549 } .forcebind def
550
551 /get_from_stringarray % <array|string> <offset> get_from_stringarray <int>
552 { 1 index type /stringtype eq {
553 get
554 } {
555 exch { % o ()
556 2 copy length ge {
557 length sub
558 } {
559 exch get exit
560 } ifelse
561 } forall
562 } ifelse
563 } .forcebind def
564
565 /getinterval_from_stringarray % <array|string> <offset> <length> getinterval_from_stringarray <string>
566 { % May allocate a string in VM.
567 2 index type /stringtype eq {
568 getinterval
569 } {
570 dup 65535 le {
571 string exch 0 % [] s o p
572 4 3 roll { % s o p Si
573 dup length % s o p Si lSi
574 dup 4 index lt {
575 3 index exch sub % s o p Si o'
576 exch pop 3 1 roll exch pop % s o' p
577 } { % s o p Si lSi
578 dup 3 1 roll % s o p lSi Si lSi
579 4 index sub % s o p lSi Si lSi-o
580 5 index length 4 index sub % s o p lSi Si lSi-o ls-p
581 2 copy gt { exch } if pop % s o p lSi Si minl
582 dup 3 1 roll % s o p lSi minl Si minl
583 5 index exch getinterval % s o p lSi minl from
584 5 index 4 index 3 index % s o p lSi minl from s p minl
585 getinterval % s o p lSi minl from to
586 copy pop % s o p lSi minl
587 exch pop add exch pop 0 exch % s 0 p'
588 dup 3 index length ge { exit } if
589 } ifelse
590 } forall
591 pop pop % s
592 } {
593 [ 4 1 roll
594 {
595 dup 0 eq {
596 pop pop pop exit
597 } if
598 3 copy
599 dup 65535 ge {
600 pop 65535
601 } if
602 getinterval_from_stringarray 4 1 roll
603 3 index length sub
604 exch
605 3 index length add
606 exch
607 } loop
608 ]
609 } ifelse
610 } ifelse
611 } .forcebind def
612
613 /string_array_size % <array|string> string_array_size <int>
614 { dup type /stringtype eq {
615 length
616 } {
617 0 exch { length add } forall
618 } ifelse
619 } .forcebind def
620
621 % Each procedure in this dictionary is called as follows:
622 % posttable <<proc>> glyphencoding
623 /postformats mark
624 16#00010000 { % 258 standard Macintosh glyphs.
625 pop MacGlyphEncoding
626 }
627 16#00020000 { % Detailed map, required by Microsoft fonts.
628 dup dup type /arraytype eq { 0 get } if length 36 lt {
629 TTFDEBUG { (post format 2.0 invalid.) = flush } if
630 pop [ ]
631 } {
632 /postglyphs exch def
633 /post_first postglyphs dup type /arraytype eq { 0 get } if def
634 post_first 32 getu16 /numglyphs exch def
635 % Build names array in the order they occur in the 'post' table
636 /postpos numglyphs 2 mul 34 add def
637 /total_length postglyphs //string_array_size exec def
638
639 % the number of names in a post table is not declared up front
640 % and there are fonts with more names than glyph indices <sigh>
641 % so we have to pre-process and find the highest index used.
642 % We start with a base value of numglyphs as it's most common,
643 % and we can safely have a array that's too large.
644 % Rather than parse out the indices twice, we store them in an
645 % array on the stack, which we then reuse, overwriting entries
646 % as we go later on.
647 numglyphs array numglyphs postglyphs
648 0 1 numglyphs 1 sub {
649 dup 2 mul 34 add 2 index exch 2 //getinterval_from_stringarray exec
650 dup 0 get 8 bitshift exch 1 get add
651 dup 5 index exch
652 4 -1 roll exch put
653 258 sub dup 3 index gt
654 % if we find a reference to an index higher than the value
655 % already on the stack, replace the value on the stack
656 { 3 -1 roll pop 2 1 roll} {pop} ifelse
657 } for
658 pop
659 1 add array 0 1 2 index length 1 sub {
660 postpos total_length ge {
661 % Fill the rest with .notdef
662 1 2 index length 1 sub { 1 index exch /.notdef put } for
663 exit
664 } if
665 % No name available, /postnames will be defined as an empty
666 % array and the glyph won't get a name attached.
667 postglyphs postpos //get_from_stringarray exec
668 postglyphs postpos 1 add 2 index
669 2 copy add total_length gt {
670 TTFDEBUG { (post table ends in the middle of the entry.) print flush } if
671 pop pop pop pop
672 % Fill the rest with .notdef
673 1 2 index length 1 sub { 1 index exch /.notdef put } for
674 exit
675 } if
676 //getinterval_from_stringarray exec cvn
677 exch postpos add 1 add /postpos exch def
678 2 index 3 1 roll
679 put
680 } for
681 /postnames exch def
682 % Some TrueType fonts converted by "Windows Type 1 Installer" have
683 % problematic post table including MacGlyphEncoding entries which
684 % should be omitted. Such extra entries in the beginning of glyphName
685 % array make /Encoding broken. If we find populary predefined
686 % ISOLatin1Encoding glyph name in the post table, empty encoding is
687 % returned. Some TrueType fonts for Microsoft Windows redefines
688 % MacGlyphEncoding glyph name out of predefined range). To permit
689 % such fonts, ISOLatin1Encoding is used to find broken post. Bug 689495.
690 /.broken_post //false def
691 tabdict /name .knownget {
692 (Windows Type 1 Installer V1.0) search {
693 pop pop pop
694 .latin1isodict postnames {
695 dup /.notdef ne {
696 2 copy known {
697 TTFDEBUG { (ignore post table that redefines ISOLatin1Encoding glyph name ) print dup //== exec flush } if
698 /.broken_post //true def
699 pop exit
700 } if
701 } if
702 pop
703 } forall
704 pop
705 } {
706 pop
707 } ifelse
708 } if
709 % Loop through the array of name indices we created above.
710 % This loop replaces name index in the array with the name
711 % it references.
712 0 1 2 index length 1 sub {
713 dup 2 index exch get dup 258 lt {
714 MacGlyphEncoding exch get
715 } {
716 258 sub postnames exch get
717 % At least some of Microsoft's TrueType fonts use incorrect
718 % (Adobe-incompatible) names for some glyphs.
719 % Correct for this here.
720 postremap 1 index .knownget { exch pop } if
721 } ifelse
722 2 index 3 1 roll
723 put
724 } for
725
726 .broken_post {
727 pop
728 [ postnames aload length 1 roll ]
729 } if
730 }
731 ifelse
732 } .forcebind
733 16#00030000 { % No map.
734 pop [ ]
735 } .forcebind
736 .dicttomark readonly def % postformats
737
738 /call.readtable
739 { .readtable
740 } .forcebind def
741 /call.readbigtable
742 { .readbigtable
743 } .forcebind def
744
745 % Each procedure in this dictionary is called as follows:
746 % <file> <length> -proc- <string|array_of_strings>
747 % Note that each table must have an even length, because of a strange
748 % Adobe requirement that each sfnts entry have even length.
749 /readtables mark
750 % Ordinary tables
751 (head) //call.readtable
752 (hhea) 1 index
753 (maxp) 1 index
754 (name) 1 index
755 (OS/2) 1 index
756 (post) //call.readbigtable
757 (vhea) //call.readtable
758 % Big tables
759 (cmap) //call.readbigtable
760 (GSUB) //call.readbigtable
761 (glyf) //call.readbigtable
762 (loca) 1 index
763 (hmtx) 1 index
764 (vmtx) 1 index
765 (cvt ) //call.readtable
766 (fpgm) 1 index
767 (prep) 1 index
768 .dicttomark
769 % Normally there would be a 'readonly' here, but the ttf2pf utility wants
770 % to include the 'kern' table as well, so we leave the readtables dictionary
771 % writable.
772 def % readtables
773
774 /readtables_stripped readtables dup length dict copy
775 dup (loca) { .skiptable } put
776 dup (glyf) { .skiptable } put
777 def
778
779 % Read a table as a single string.
780 % <file> <length> .skiptable <string>
781 /.skiptable {
782 pop pop ()
783 } .forcebind def
784
785 % Read a table as a single string.
786 % <file> <length> .readtable <string>
787 /.readtable {
788 dup dup 1 and add string
789 % Stack: f len str
790 dup 0 4 -1 roll getinterval
791 % Stack: f str str1
792 % Because of the absurd PostScript specification that gives an
793 % error for reading into an empty string, we have to check for
794 % this explicitly here.
795 3 -1 roll exch
796 dup () ne { readstring } if pop pop
797 } .forcebind def
798
799 % Read a big table (one that may exceed 64K).
800 % <file> <length> .readbigtable <string[s]>
801 /.readbigtable {
802 dup 65400 lt {
803 .readtable
804 } {
805 currentuserparams /VMReclaim get -2 vmreclaim
806 [ 4 2 roll {
807 % Stack: mark ... f left
808 dup maxstring le { exit } if
809 1 index maxstring string readstring pop 3 1 roll maxstring sub
810 } loop .readtable ]
811 exch vmreclaim
812 } ifelse
813 } .forcebind def
814
815 end readonly def % .loadttfontdict
816
817 % <tab> .printtab -
818 /.printtab {
819 dup 0 4 getinterval print ( ) print
820 dup 8 getu32 =only ( ) print
821 12 getu32 =
822 } .internalbind def
823
824 % <file> <bool> <SubfontID> .loadttfonttables -
825 % Pushes .loadttfontdict & scratch dict on d-stack.
826 % Defines f, offsets, tables, tabdict, tabs, tthdrlen.
827 % Skips loca and glyf if <bool> is true.
828 /.loadttfonttables {
829 //.loadttfontdict begin
830 40 dict begin
831 /SubfontID exch def
832 /load_stripped exch def
833 /f exch def
834 /offsets f 12 string readstring pop def
835 /tthdrlen offsets length def
836 load_stripped { readtables_stripped } { readtables } ifelse /readtables_ exch def
837 offsets 0 4 getinterval (ttcf) eq {
838 offsets 8 getu32 /num_fonts exch def
839 SubfontID num_fonts ge {
840 QUIET not { (True Type collection contains insufficient fonts.) = } if
841 /.loadttfonttables cvx /invalidfont signalerror
842 } if
843 f SubfontID 4 mul dup 0 ne {
844 () /SubFileDecode filter flushfile
845 } {
846 pop pop
847 } ifelse
848
849 f 4 string readstring pop
850 0 getu32 /ttc_offset exch def
851
852 f ttc_offset SubfontID 4 mul sub 16 sub
853 dup 0 ne {
854 () /SubFileDecode filter flushfile
855 } {
856 pop pop
857 } ifelse
858 /offsets f 12 string readstring pop def
859 } {
860 SubfontID 0 gt {
861 QUIET not { (SubfontID > 0 with a True Type file which is not a collection.) = } if
862 /.loadttfonttables cvx /invalidfont signalerror
863 } if
864 /ttc_offset 0 def
865 } ifelse
866 % Peek because table counter may be incorrect.
867 /tables f offsets 4 getu16 16 mul string .peekstring pop def
868 /tabdict tables length 16 idiv dict def
869 % tabs = tables we want to keep, sorted by file position.
870 /tabs [ 0 16 tables length 1 sub {
871 tables exch 16 getinterval
872 TTFDEBUG { dup //.printtab exec } if
873 dup 0 4 getinterval readtables_ 1 index known {
874 % put all 0 length tables at 0 to avoid overlap
875 1 index 12 getu32 0 eq { 1 index 8 0 putu32 } if
876 tabdict exch 2 index put
877 } {
878 pop pop
879 } ifelse
880 } for ] {
881 exch 8 getu32 exch 8 getu32 lt
882 } .sort def
883 % In certain malformed TrueType fonts, tables overlap.
884 % Truncate tables if necessary.
885 0 1 tabs length 2 sub {
886 dup tabs exch get exch 1 add tabs exch get
887 1 index 8 getu32 2 index 12 getu32 add
888 1 index 8 getu32 gt {
889 (**** Warning: ) print 1 index 0 4 getinterval print
890 ( overlaps ) print dup 0 4 getinterval print
891 (, truncating.) = flush
892 dup 8 getu32 2 index 8 getu32 sub
893 2 index 12 3 -1 roll putu32
894 } if pop pop
895 } for
896 } .forcebind def
897
898
899
900 % <file> <bool> <SubfontID> .loadwofftables -
901 % Pushes .loadttfontdict & scratch dict on d-stack.
902 % Defines f, offsets, tables, tabdict, tabs, tthdrlen.
903 % Skips loca and glyf if <bool> is true.
904 /.loadwofftables {
905 //.loadttfontdict begin
906 40 dict begin
907 /SubfontID exch def
908 /load_stripped exch def
909 /f exch def
910 % loading WOFFs "stripped" only works for .findfontvalue - we cannot
911 % (currently) use a stripped wOFF font
912 load_stripped { readtables_stripped } { readtables } ifelse /readtables_ exch def
913 /offsets 12 string def
914 /woffhdr f 44 string readstring pop def
915 /tthdrlen woffhdr length def
916 /ttc_offset 0 def
917 woffhdr 0 4 getinterval (wOFF) eq not
918 % allow loading wOFFOTTO fonts "stripped" for .findfontvalue
919 woffhdr 4 4 getinterval (OTTO) eq load_stripped not and or
920 {/.loadwofftables cvx /invalidfont signalerror}
921 {
922 offsets 0 woffhdr 4 4 getinterval putinterval
923 /tables f woffhdr 12 getu16 20 mul string .peekstring pop def
924 offsets 4 tables length 20 idiv putu16
925 /tabdict tables length 20 idiv dict def
926 /tabs [ 0 20 tables length 1 sub
927 {
928 tables exch 20 getinterval
929 TTFDEBUG { dup //.printtab exec } if
930 dup 0 4 getinterval readtables_ 1 index known
931 {
932 % put all 0 length tables at 0 to avoid overlap
933 1 index 12 getu32 0 eq { 1 index 8 0 putu32 } if
934 tabdict exch 2 index put
935 }
936 {pop pop}
937 ifelse
938 }for
939 ]
940 { exch 4 getu32 exch 4 getu32 lt } .sort
941 def
942 } ifelse
943 } .forcebind def
944
945 /.file_table_pos_names
946 mark
947 /glyf 0
948 /loca 0
949 .dicttomark readonly def
950
951 % - .readttdata -
952 % Read data. Updates offsets, tabs; stores data in tabdict.
953 /.readttdata {
954 /file_table_pos 10 dict def
955 /fpos tthdrlen ttc_offset add def
956 /sfpos offsets length tabs length 16 mul add def
957 offsets 4 tabs length putu16
958 0 1 tabs length 1 sub {
959 dup tabs exch get
960 dup 0 4 getinterval /tname exch def
961 dup length 20 eq
962 {
963 dup 4 getu32 /tpos exch def
964 dup 8 getu32 /tclen exch def
965 dup 12 getu32 /tlen exch def
966 }
967 {
968 dup 8 getu32 /tpos exch def
969 dup 12 getu32 /tlen exch def
970 /tclen tlen def
971 } ifelse
972 load_stripped //.file_table_pos_names tname known and {
973 pop
974 file_table_pos tname [tpos tlen tclen] put
975 tabdict tname () put
976 pop
977 } {
978 dup length 20 eq
979 {
980 0 16 getinterval dup 8 sfpos putu32
981 tabs 3 -2 roll put
982 }
983 {8 sfpos putu32 pop}
984 ifelse
985 % Skip data between the end of the previous table and
986 % the beginning of this one, if any.
987 tpos fpos gt {
988 load_stripped {
989 % 'setfileposition' is faster for skipping a big data.
990 f tpos setfileposition
991 } {
992 f tpos fpos sub () /SubFileDecode filter dup flushfile closefile
993 /fpos tpos def
994 } ifelse
995 } if
996 /ff
997 tlen tclen eq not
998 {f tclen () /SubFileDecode filter /FlateDecode filter}
999 {f tlen () /SubFileDecode filter}
1000 ifelse
1001 def
1002 ff tlen readtables_ tname get exec
1003 ff closefile
1004 tabdict tname 3 -1 roll put
1005 % Round up the table length to an even value.
1006 /sfpos sfpos tlen dup 1 and add add def
1007 } ifelse
1008 /fpos fpos tclen add def
1009 } for
1010 } .forcebind def
1011
1012 % Find the string in a list of strings that includes a given index.
1013 % <strings> <index> .findseg <string> <index'>
1014 /.findseg {
1015 exch {
1016 dup length 2 index gt { exch exit } if
1017 length sub
1018 } forall
1019 } .forcebind def
1020
1021 % - .makesfnts -
1022 % Defines checksum, getloca, head, locatable, numloca, post, sfnts, upem
1023 % Note that the 'loca' table may be out of order.
1024 /.makesfnts {
1025 //.readttdata exec
1026 /head tabdict /head get def
1027 /post tabdict /post .knownget {
1028 dup 0 get /post_first_part exch def
1029 } {
1030 //null
1031 } ifelse def
1032 load_stripped not {
1033 /locatable tabdict /loca get def
1034 /numloca
1035 locatable dup type /stringtype eq
1036 { length }
1037 { 0 exch { length add } forall }
1038 ifelse % no def yet
1039 locatable type /stringtype eq {
1040 /.indexloca {} def
1041 } {
1042 /.indexloca //.findseg def
1043 } ifelse
1044 head 50 getu16 0 ne {
1045 /getloca {
1046 2 bitshift locatable exch .indexloca getu32
1047 } def
1048 4 idiv 1 sub
1049 } {
1050 /getloca {
1051 dup add locatable exch .indexloca getu16 dup add
1052 } def
1053 2 idiv 1 sub
1054 } ifelse def % numloca
1055 } {
1056 % We did not load loca, take the number of glyphs from maxp.
1057 /numloca tabdict /maxp get 4 getu16 def
1058 } ifelse
1059 /sfnts [
1060 offsets tabs { concatstrings } forall
1061 tabs {
1062 0 4 getinterval tabdict exch get
1063 dup type /stringtype ne { aload pop } if
1064 } forall
1065 ] def
1066 } .forcebind odef
1067
1068 /first_post_string % - first_post_string <string>
1069 {
1070 post dup type /arraytype eq { 0 get } if
1071 } .internalbind def
1072
1073 % - .getpost -
1074 % Uses post, defines glyphencoding
1075 /.getpost {
1076 /glyphencoding post //null eq {
1077 TTFDEBUG { (post missing) = flush } if [ ]
1078 } {
1079 postformats first_post_string 0 getu32 .knownget {
1080 TTFDEBUG {
1081 (post: format ) print
1082 first_post_string
1083 dup 0 getu16 =only (,) print 2 getu16 = flush
1084 } if
1085 post exch exec
1086 } {
1087 TTFDEBUG { (post: unknown format ) print post 0 getu32 = flush } if [ ]
1088 } ifelse
1089 } ifelse
1090 TTFDEBUG { (post=) print dup //== exec } if
1091 def
1092 } .forcebind def
1093
1094 % - .ttkeys <key> <value> ...
1095 /.ttkeys {
1096 count /ttkeycount exch def
1097 /upem head 18 getu16 def
1098 /FontMatrix matrix
1099 /FontBBox [ 36 2 42 { head exch gets16 upem div } for ]
1100 nextxuid
1101 tabdict /name .knownget {
1102 % Find the names from the 'name' table.
1103 /names exch def
1104 /FontName names 6 findname not { names 4 findname not { curxuid 16#ffffffff and 16 32 string cvrs } if } if
1105 /fontname 1 index def
1106 /FontInfo mark
1107 names 0 findname { /Notice exch } if
1108 names 1 findname { /FamilyName exch } if
1109 names 4 findname { /FullName exch } if
1110 names 5 findname { /Version exch } if
1111 } {
1112 % No name table, fabricate a FontName.
1113 /FontName curxuid 16#ffffffff and 16 32 string cvrs
1114 /fontname 1 index def
1115 /FontInfo mark
1116 } ifelse
1117 % Stack: ... /FontInfo mark key1 value1 ...
1118 post //null ne {
1119 /ItalicAngle first_post_string 4 gets32 65536.0 div
1120 /isFixedPitch first_post_string 12 getu32 0 ne
1121 /UnderlinePosition first_post_string 8 gets16 upem div
1122 /UnderlineThickness first_post_string 10 gets16 upem div
1123 } if
1124 counttomark 0 ne { .dicttomark } { pop pop } ifelse
1125 /XUID [orgXUID 42 curxuid]
1126 TTFDEBUG {
1127 tabs { //.printtab exec } forall
1128 [ sfnts { length } forall ] //== exec
1129 count ttkeycount sub array astore dup { //== exec } forall aload pop
1130 } if
1131 /sfnts sfnts
1132 } .forcebind def
1133
1134 % ---------------- Standard TrueType font loading ---------------- %
1135
1136 % - .pickcmap_with_no_xlatmap -
1137 % Defines cmapsub, cmaptab
1138 /.pickcmap_with_no_xlatmap {
1139 tabdict /cmap get
1140 % The Apple cmap format is no help in determining the encoding.
1141 % Look for a Microsoft table. If we can't find one,
1142 % just use the first table, whatever it is.
1143 dup 4 8 getinterval_from_stringarray exch % () [] % the default
1144 0 1 2 index 2 getu16a 1 sub { % () [] i
1145 8 mul 4 add 1 index exch 8 getinterval_from_stringarray % () [] ()
1146 TTFDEBUG {
1147 (cmap: platform ) print dup 0 getu16 =only
1148 ( encoding ) print dup 2 getu16 = flush
1149 } if
1150 dup 0 getu16 3 eq { exch 3 -1 roll pop exit } if pop
1151 } for
1152 % Stack: subentry table
1153 /cmapsub 2 index def % () []
1154 exch 4 getu32 1 index string_array_size 1 index sub getinterval_from_stringarray
1155 /cmaptab exch def
1156 } .forcebind def
1157
1158 % - .pickcmap_with_xlatmap -
1159 % Defines cmapsub, cmaptab
1160 /.pickcmap_with_xlatmap {
1161 .xlatmap_dict /TrueType known not {
1162 (Emulating a CID font with a True Type file, ) print
1163 (the file gs/lib/xlatmap must contain /TrueType key.) =
1164 /.pickcmap_with_xlatmap cvx /configurationerror signalerror
1165 } if
1166 //false
1167 .xlatmap_dict /TrueType get
1168 dup length 2 sub 0 exch 2 exch { % bool [] i
1169 2 copy get % bool [] i ()
1170 (.) search { % bool [] i post match pre
1171 cvi exch pop exch cvi % bool [] i PlatID SpecID
1172 } {
1173 (gs/lib/xlatmap containg a record with an invalid (PlatformID.SpecificID)) =
1174 /.pickcmap_with_xlatmap cvx /configurationerror signalerror
1175 } ifelse
1176 TTFDEBUG {
1177 (Seeking a cmap for platform=) print 1 index =only ( encoding=) print dup =
1178 } if
1179 tabdict /cmap get % bool [] i PlatID SpecID (cmap)
1180 dup /cmaptab exch def % temporary
1181 0 1 2 index 2 getu16a 1 sub { % bool [] i PlatID SpecID (cmap) j
1182 8 mul 4 add 1 index exch 8
1183 getinterval_from_stringarray % bool [] i PlatID SpecID (cmap) (cmapsub)
1184 TTFDEBUG {
1185 (cmap: platform ) print dup 0 getu16 =only
1186 ( encoding ) print dup 2 getu16 = flush
1187 } if
1188 dup 0 getu16 4 index eq {
1189 dup 2 getu16 3 index eq { % bool [] i PlatID SpecID (cmap) (cmapsub)
1190 TTFDEBUG {
1191 (Choosen a cmap for platform=) print 3 index =only
1192 ( encoding=) print 2 index =
1193 } if
1194 /cmapsub 1 index def
1195 dup 4 getu32 % bool [] i PlatID SpecID (cmap) (cmapsub) p
1196 cmaptab 1 index getu16a % get cmap format
1197 8 lt { % length for traditional 16bit format 0, 2, 4, 6
1198 cmaptab 1 index 2 add getu16a
1199 } { % length for advanced 32bit format 8, 10, 12
1200 cmaptab 1 index 4 add getu32a
1201 } ifelse % bool [] i PlatID SpecID (cmap) (cmapsub) p l
1202 cmaptab 3 1 roll getinterval_from_stringarray
1203 /cmaptab exch def % bool [] i PlatID SpecID (cmap) (cmapsub)
1204 5 index 5 index 1 add get % bool [] i PlatID SpecID (cmap) (cmapsub) /Decoding
1205 /Decoding exch def % bool [] i PlatID SpecID (cmap) (cmapsub)
1206 7 -1 roll pop //true 7 1 roll % true [] i PlatID SpecID (cmap) (cmapsub)
1207 } if
1208 } if
1209 pop % true [] i PlatID SpecID (cmap)
1210 5 index { exit } if
1211 } for % bool [] i PlatID SpecID (cmap)
1212 pop pop pop pop % bool []
1213 1 index { exit } if
1214 } for % bool []
1215 pop % bool
1216 not {
1217 QUIET not { (True Type font doesn't contain a charset listed in gs/lib/xlatmap.) = } if
1218 /.pickcmap_with_xlatmap cvx /invalidfont signalerror
1219 } if %
1220 } .forcebind def
1221
1222 % - .pickcmap -
1223 % Defines cmapsub, cmaptab
1224 /.pickcmap {
1225 % Currently we use xlatmap only for emulation CIDFontType 2 with
1226 % a disk True Type font files, and use load_stripped
1227 % to check this regime. We would like to do so
1228 % while emulating a Type 42, but first the old code
1229 % about handling them to be changed
1230 % with adding a handling of a Decoding.
1231 % fixme : A correct way to fix this is to implenent
1232 % the Type 42 emulation with gs_fntem.ps .
1233 % Also note that PDF embedded fonts probably don't need a xlatmap -
1234 % see PDF spec, "Encodings for True Type fonts".
1235 load_stripped {
1236 //.pickcmap_with_xlatmap exec
1237 } {
1238 //.pickcmap_with_no_xlatmap exec
1239 } ifelse
1240 } .forcebind odef
1241 currentdict /.pickcmap_with_xlatmap .undef
1242 currentdict /.pickcmap_with_no_xlatmap .undef
1243
1244 % <glyph> .nname <_name>
1245 /.nname {
1246 =string cvs (_) exch concatstrings cvn
1247 } .internalbind def
1248
1249
1250 % - .charkeys /CharStrings <charstrings> /Encoding <encoding>
1251 % Resets glyphencoding
1252 /.charkeys {
1253 TTFDEBUG {
1254 (glyphencoding: length=) print glyphencoding dup length = === flush
1255 } if
1256 % Hack: if there is no usable post table but the cmap uses
1257 % the Microsoft Unicode encoding, use ISOLatin1Encoding.
1258 % if 'post' presents, .charkeys computes (with dropping minor details) :
1259 % CharStrings = glyphencoding^-1
1260 % Encoding = cmap*glyphencoding
1261 % because 'post' maps glyph indices to glyph names.
1262 % Otherwise .charkeys must compute (with dropping same details) :
1263 % CharStrings = glyphencoding^-1 * cmap
1264 % Encoding = glyphencoding
1265 % because glyphencoding is stubbed with an encoding,
1266 % which maps char codes to glyph names.
1267 glyphencoding length 0 eq {
1268 /have_post //false def
1269 cmapsub 0 4 getinterval <00030001> eq {
1270 TTFDEBUG { (No post but have cmap 3.1, so use ISOLatin1Encoding) = } if
1271 /glyphencoding ISOLatin1Encoding dup length array copy def
1272 } {
1273 TTFDEBUG { (No encoding info, use .GS_extended_SymbolEncoding) = } if
1274 /glyphencoding /.GS_extended_SymbolEncoding findencoding dup length array copy def
1275 } ifelse
1276 } {
1277 /have_post //true def
1278 } ifelse
1279 % If necessary, fabricate additional glyphencoding entries
1280 % to cover all of loca
1281 glyphencoding length numloca lt {
1282 /glyphencoding numloca array
1283 glyphencoding length dup 1 sub 0 1 3 2 roll {
1284 dup glyphencoding exch get
1285 3 index 3 1 roll put
1286 } for
1287 % /glyphencoding <newarray> <glyphencoding length>
1288 1 numloca 1 sub {
1289 1 index exch dup //.nname exec put
1290 } for
1291 def
1292 } if
1293
1294 /cmapa cmaptab cmapdict def
1295 % Some badly designed Chinese fonts have a post table
1296 % in which all glyphs other than 0 are named .null.
1297 % Use CharStrings to keep track of the reverse map from
1298 % names to glyphs, and don't let any name be used for
1299 % more than one glyph.
1300 /CharStrings glyphencoding dup length 1 add dict % +1 for .notdef
1301 0 1 3 index length 1 sub {
1302 % Stack: glyphencoding dict index
1303 dup 3 index 1 index get
1304 have_post not {
1305 exch
1306 cmapa exch .knownget not {
1307 0 % a stub for a while. Must skip the entry.
1308 } if
1309 exch
1310 } if
1311 3 index 1 index known
1312 { % The same name maps to more than one glyph. Change the name.
1313 % No special treatment for /.notdef glyph. Bug 689408.
1314 pop //.nname exec 3 index 2 index 2 index put
1315 2 index exch 3 -1 roll put
1316 } {
1317 3 index exch 3 -1 roll put % unique name
1318 pop
1319 } ifelse
1320 } for exch pop
1321 % If there is no .notdef entry, map it to glyph 0.
1322 dup /.notdef known not { dup /.notdef 0 put } if
1323
1324 TTFDEBUG {
1325 (CharStrings:)= dup { exch =string cvs print ( ) print //== exec } forall
1326 } if
1327
1328 % Provide all known aliases for each glyph
1329 cmapsub 0 4 getinterval <00030001> eq % is the cmap table a unicode one?
1330 cmapa //ReverseAdobeGlyphList //AdobeGlyphList
1331 { % CharStrings(dict) isunicode(boolean) cmap(dict) RAGL(dict) gname(name) codep(integer)
1332 exch
1333 dup 6 index exch .knownget
1334 { % CharStrings(dict) isunicode(boolean) cmap(dict) RAGL(dict) codep(integer) gname(name) gindex(integer)
1335 3 index 3 index .knownget
1336 { % CharStrings(dict) isunicode(boolean) cmap(dict) RAGL(dict) codep(integer) gname(name) gindex(integer) gnames(array)
1337 { % CharStrings(dict) isunicode(boolean) cmap(dict) RAGL(dict) codep(integer) gname(name) gindex(integer) gname(name)
1338 TTFDEBUG { (\n1 setting alias: ) print dup ==only
1339 ( to be the same as ) print 2 index //== exec } if
1340
1341 7 index 2 index 3 -1 roll exch put
1342 } forall
1343 pop pop pop
1344 }
1345 {
1346 pop pop
1347 } ifelse
1348 }
1349 { % CharStrings(dict) isunicode(boolean) cmap(dict) RAGL(dict) gname(name) codep(integer)
1350 exch
1351 % Only do this for Unicode cmap table
1352 4 index
1353 {
1354 dup 4 index exch .knownget
1355 { % CharStrings(dict) isunicode(boolean) cmap(dict) RAGL(dict) gname(name) codep(integer) gindex(integer)
1356 exch pop
1357 TTFDEBUG { (\n2 setting alias: ) print 1 index ==only
1358 ( to use glyph index: ) print dup //== exec } if
1359 5 index 3 1 roll put
1360 //false
1361 }
1362 {
1363 //true
1364 }ifelse
1365 }
1366 {
1367 //true
1368 } ifelse
1369
1370 { % CharStrings(dict) isunicode(boolean) cmap(dict) RAGL(dict) gname(name) codep(integer)
1371 16 4 string cvrs dup length neg 7 add (uni0000) dup
1372 4 -2 roll exch putinterval cvn 3 index 1 index .knownget
1373 { % CharStrings(dict) isunicode(boolean) cmap(dict) RAGL(dict) gname(name) codep(integer) gindex(integer)
1374 TTFDEBUG { (\3 nsetting alias: ) print 1 index ==only
1375 ( to be index: ) print dup //== exec } if
1376 exch pop 5 index 3 1 roll put
1377 }
1378 {
1379 pop pop
1380 } ifelse
1381 } if
1382 } ifelse
1383 } forall
1384 pop pop pop
1385
1386 readonly
1387
1388 /Encoding [
1389 have_post {
1390 0 1 255 {
1391 cmapa exch .knownget not { 0 } if
1392 glyphencoding dup length
1393 2 index le {
1394 pop pop /.notdef
1395 } {
1396 exch get
1397 } ifelse
1398 } for
1399 } {
1400 glyphencoding dup length 256 gt { 0 256 getinterval } if
1401 aload pop
1402 counttomark 256 exch sub { /.notdef } repeat
1403 } ifelse
1404 ]
1405 TTFDEBUG { (Encoding: ) print dup === flush } if
1406 } .forcebind odef
1407
1408 % ---------------- CIDFontType 2 font loading ---------------- %
1409
1410 % Fill a string with sequential CIDs starting from the initial value.
1411 % <string> <value> .fill_identity_cmap <string>
1412 /.fill_identity_cmap { % () v
1413 1 index length 2 sub % () v n-2
1414 0 2 3 2 roll { % () v 0 2 n-1
1415 3 copy exch % () v i () i v
1416 -8 bitshift % () v i () i v>>8
1417 put % () v i
1418 3 copy 1 add % () v i () v i+1
1419 exch 255 and % () v i () i+1 v&255
1420 put % () v i
1421 pop 1 add % () v+1
1422 } for
1423 pop
1424 } .forcebind def
1425
1426 % <CIDSystemInfo dict> <dict> .definettcidfont <font>
1427 /.definettcidfont {
1428 dup begin
1429 /CIDFontName fontname def
1430 /CIDFontType 2 def
1431 /CIDSystemInfo 4 -1 roll def
1432 /CharStrings mark /.notdef 0 .dicttomark def
1433 % The cmap isn't of any use even if it is present.
1434 % Just construct an identity CIDMap covering all the glyphs.
1435
1436 /CIDCount numloca % Wrong if a CIDFontType2 embedded into PDF with a non-Identity CIDToGIDMap.
1437 def % processCIDToGIDMap may replace.
1438 /CIDMap numloca maxstring le {
1439 % Use a single string.
1440 numloca 2 mul string 0 //.fill_identity_cmap exec
1441 } {
1442 % We must use 2 strings.
1443 maxstring 2 mul string 0 //.fill_identity_cmap exec
1444 numloca maxstring sub 2 mul string maxstring //.fill_identity_cmap exec
1445 2 array astore
1446 } ifelse
1447 def
1448
1449 /GDBytes 2 def
1450 end
1451
1452 end end dup /CIDFontName get exch /CIDFont defineresource
1453 } .forcebind def
1454
1455 % <CIDSystemInfo dict> <file> <Substite name> .loadttcidfont <cidtype2font>
1456 /.loadttcidfont {
1457 exch
1458 //false 0 .loadttfonttables
1459 .makesfnts
1460 % CIDFontType2 fonts don't have a cmap: they are indexed by CID.
1461 mark
1462 //.ttkeys exec
1463 .dicttomark dup % convert keys to dict and copy dict
1464 3 -1 roll % Bring PDF substitute name to top of stack
1465
1466 dup
1467 /fontname exch def
1468 /FontName exch put % replace any definition of /FontName
1469 //.definettcidfont exec
1470 } .forcebind odef
1471
1472 % <file> <SubfontID> .load_tt_font_stripped <font_data>
1473 % The font_data includes sfnts, NumGlyphs, TT_cmap, file_table_pos, Decoding.
1474 % CIDMap to be created later from TT_cmap.
1475 /.load_tt_font_stripped {
1476 //true exch .loadttfonttables
1477 .makesfnts
1478 .pickcmap
1479 mark
1480 //.ttkeys exec
1481 /NumGlyphs numloca
1482 /TT_cmap cmaptab cmapdict
1483 /file_table_pos file_table_pos
1484 /Decoding Decoding
1485 .dicttomark
1486 end end
1487 } .forcebind def
1488
1489 /.load_woff_for_cid {
1490 //false exch //.loadwofftables exec
1491 .makesfnts
1492 % we have to set load_stripped here so
1493 % .pickcmap will follow the right logic
1494 % for emulating a CIDFont
1495 /load_stripped //true def
1496 .pickcmap
1497 /load_stripped //false def
1498 mark
1499 //.ttkeys exec
1500 /NumGlyphs numloca
1501 /TT_cmap cmaptab cmapdict
1502 % /file_table_pos file_table_pos
1503 /Decoding Decoding
1504 .dicttomark
1505 end end
1506 } .forcebind def
1507
1508 % ---------------- PDF TrueType font loading ---------------- %
1509
1510 % Strictly speaking, this code should be loaded only if we have a PDF
1511 % interpreter, but it's so closely tied to the rest of the code in this
1512 % file that we always include it.
1513
1514 % <plat+enc> .findcmap <subtable> true
1515 % <plat+enc> .findcmap false
1516 /.findcmap {
1517 //false exch tabdict /cmap get
1518 % Some fonts have multiple cmaps with the same platform and
1519 % encoding. Use the first one we find.
1520 0 1 2 index 2 getu16a 1 sub {
1521 % Stack: false plat+enc cmap index
1522 8 mul 4 add 1 index exch 8 getinterval_from_stringarray
1523 dup 0 4 getinterval 3 index eq {
1524 4 getu32 1 index exch 1 index string_array_size 1 index sub getinterval_from_stringarray
1525 4 -1 roll not 4 2 roll exit
1526 } if pop
1527 } for
1528 % Stack: false plat+enc cmap || subtable true plat+enc cmap
1529 pop pop
1530 } .forcebind def
1531
1532 % Build .symbol_list for .pdfcharkeys .
1533 % It is a dictionary containing all SymbolEncoding glyph names
1534 % and random names for filling gaps in the character code range.
1535 /.symbol_list 256 dict def
1536 {
1537 =string 0 (x) 0 get put
1538 /SymbolEncoding .findencoding
1539 0 1 255 {
1540 dup 2 index exch get
1541 dup /.notdef eq {
1542 pop dup
1543 =string 1 3 getinterval cvs length 1 add
1544 =string exch 0 exch getinterval cvn
1545 } if
1546 exch //.symbol_list 3 1 roll put
1547 } for
1548 pop
1549 } .forcebind exec
1550
1551 % Create .GS_extended_SymbolEncoding as inverse of .symbol_list .
1552 {
1553 /.GS_extended_SymbolEncoding 256 array
1554 //.symbol_list {
1555 exch 2 index 3 1 roll put
1556 } forall
1557 .defineencoding
1558 } .forcebind exec
1559
1560 /.hexdigits (0123456789ABCDEF) def
1561
1562 /.is_hex_digit { % <int> .is_hex_digit <bool>
1563 dup 48 ge exch
1564 dup 57 le exch
1565 dup 65 ge exch
1566 70 le and
1567 3 1 roll
1568 and or
1569 } .forcebind def
1570
1571 /.popfex { pop //false exit } .internalbind def
1572 /.pop3ex { pop pop pop exit } .internalbind def
1573
1574 /.addglyph { % <dict> <name> <glyph#> .addglyph -
1575 1 index .namestring % d n g s
1576 dup length 7 eq {
1577 % Bug688467.ps doesn't work if the uniXXXX mapping is allowed with any cmap.
1578 % Allow it with cmap 3.1 only.
1579 currentdict /.allow_uniXXXX_glyph_names .knownget not { //false } if
1580 } { //false
1581 } ifelse % d n g s b
1582 {
1583 % An undocumented Adobe feature (not sure) :
1584 % if the name is uniXXXX, obtain Unicode code from it.
1585 % See bug 688946.
1586 TTFDEBUG { (Try uniXXXX:) print dup print } if
1587 { dup 0 get 117 ne //.popfex if % u
1588 dup 1 get 110 ne //.popfex if % n
1589 dup 2 get 105 ne //.popfex if % i
1590 dup 3 get //.is_hex_digit exec not //.popfex if
1591 dup 4 get //.is_hex_digit exec not //.popfex if
1592 dup 5 get //.is_hex_digit exec not //.popfex if
1593 dup 6 get //.is_hex_digit exec not //.popfex if
1594 dup 3 1 getinterval .hexdigits exch search pop length exch pop exch pop 12 bitshift exch
1595 dup 4 1 getinterval .hexdigits exch search pop length exch pop exch pop 8 bitshift exch
1596 dup 5 1 getinterval .hexdigits exch search pop length exch pop exch pop 4 bitshift exch
1597 dup 6 1 getinterval .hexdigits exch search pop length exch pop exch pop exch pop
1598 add add add
1599
1600 cmapencoding exch .knownget not { 0 } if
1601 dup 0 eq //.popfex if
1602
1603 TTFDEBUG { ( index=) print dup =string cvs print } if
1604 exch pop
1605 put //true exit
1606 } loop
1607 TTFDEBUG { ()= } if
1608 } {
1609 pop //false
1610 } ifelse
1611 not { % d n g
1612 {
1613 cmapencoding exch .knownget not { 0 } if
1614 dup 0 eq //.pop3ex if
1615 3 copy pop known //.pop3ex if
1616 put exit
1617 } loop
1618 } if
1619 } .forcebind def
1620
1621 % <chartoglyphmap> <subcmap> <AGL> .pdfmapchars /CharStrings <charstrings>
1622 /.pdfmapchars {
1623 exch cmapdict /cmapencoding exch def % c2g
1624 /CharStrings 0 dict % c2g /CS <<>>
1625 % Add glyphs of <AGL>*<subcmap> :
1626 3 2 roll { % /CS <<>> v
1627 dup type /arraytype eq { % /CS <<>> /name []
1628 { 3 copy //.addglyph exec
1629 pop
1630 } forall
1631 } {
1632 3 copy //.addglyph exec pop
1633 } ifelse
1634 pop
1635 } forall
1636 % Add glyphs of 'post' with lower priority :
1637 0 1 glyphencoding length 1 sub {
1638 dup glyphencoding exch get exch
1639 dup 0 ne {
1640 3 copy pop known not {
1641 3 copy put
1642 } if
1643 } if
1644 pop pop
1645 } for
1646
1647 % Finally, add glyphs from the prebuilt_encoding with lowest priority
1648 % this is a catch-all for poorly matched font/PDF combos
1649 3 2 roll { % /CS <<>> v
1650 dup type /arraytype eq { % /CS <<>> /name []
1651 { 3 copy //.addglyph exec
1652 pop
1653 } forall
1654 } {
1655 3 copy //.addglyph exec pop
1656 } ifelse
1657 pop
1658 } forall
1659
1660 dup /.notdef 0 put
1661 } .forcebind def
1662
1663 % <subtable> .pdfmapsymbolic /Encoding [] /CharStrings <<>>
1664 /.pdfmapsymbolic {
1665 //true /.render_notdef gput
1666 cmapdict
1667 dup length 1 add dict begin
1668 [ 64 { /.notdef /.notdef /.notdef /.notdef } repeat ]
1669 exch { % [] char glyph
1670 exch
1671 dup 255 gt {
1672 255 and
1673 2 index 1 index get /.notdef ne {
1674 pop pop
1675 //false
1676 } {
1677 //true
1678 } ifelse
1679 } {
1680 //true
1681 } ifelse {
1682 dup (01234567) cvs cvn % glyph char /char
1683 3 1 roll % /char glyph char
1684 3 index exch % /char glyph [] char
1685 3 index put % /char glyph
1686 def % -
1687 } if
1688 } forall
1689 /.notdef 0 def
1690 /Encoding exch
1691 /CharStrings currentdict end
1692 } .forcebind def
1693
1694 % If we're using a 1,0 cmap table, we need to store the table for the heuristic
1695 % to know whether to image or elide GID 0. See zfapi.c ps_get_glyphname_or_cid().
1696 % For other cmap types, we don't. Since this just pushes key/value pairs onto the
1697 % stack for a later dicttomark op, it doesn't matter that different branches can push
1698 % different numbers of objects.
1699 %
1700 % - .pdfcharkeys [/TT_cmap cmapdict] /CharStrings <charstrings> /Encoding <encoding>
1701 /.pdfcharkeys {
1702 % The following algorithms are per the PDF 1.7 Reference.
1703 TTFDEBUG { (.pdfcharkeys beg) = } if
1704
1705 % if the flags say symbolic, *and* the font contains a symbol or UCS-2
1706 % cmap table, ignore Encoding if there is one, treat as symbolic.
1707 % If the flags say symbolic, and the font doesn't have a symbol cmap
1708 % and we have an Encoding, treat it as non-symbolic
1709 % if we don't have an Encoding, try the MacRoman cmap table.
1710 % Finally, if none of that works, remove the Encoding, and treat it
1711 % as non-symbolic.
1712 is_symbolic {
1713 <00030000> //.findcmap exec
1714 { //true }
1715 {
1716 prebuilt_encoding //null eq {
1717 <00030001> //.findcmap exec
1718 {//true}
1719 {<00010000> //.findcmap exec}
1720 ifelse
1721 }
1722 {//false}
1723 ifelse
1724 } ifelse
1725
1726 {
1727 //.pdfmapsymbolic exec
1728 /prebuilt_encoding //null def
1729 //false
1730 } {
1731 /prebuilt_encoding //null def
1732 //true
1733 } ifelse
1734 } {
1735 //true
1736 }
1737 ifelse
1738 {
1739 <00030001> //.findcmap exec {
1740 prebuilt_encoding //null ne {
1741 TTFDEBUG { (Using cmap 3.1 with prebuilt_encoding for non-symbolic.) = } if
1742 //true /.render_notdef gput
1743 % The character code in /prebuilt_encoding[] is not guaranteed to be
1744 % compatible with the Microsoft UCS2 TrueType cmap subtable.
1745 % If the glyphname is known by AdobeGlyphList, the charcode for the glyph
1746 % is replaced by UCS2 charcode from AdobeGlyphList. Otherwise it is left
1747 % as in /prebuilt_encoding[]. /.pdfmapchars should dredge them.
1748
1749 /.allow_uniXXXX_glyph_names //true def
1750 AdobeGlyphList prebuilt_encoding .invert_encoding dup { % <<AGL>> <<pbe>> <glyphname> <pbecode>
1751 pop % <<AGL>> <<pbe>> <glyphname>
1752 TTFDEBUG { (check glypname /) print dup =only flush } if
1753 2 index 1 index .knownget { % <<AGL>> <<pbe>> <glyphname> <AGLcode>
1754 2 index 3 1 roll % <<AGL>> <<pbe>> <<pbe>> <glyphname> <AGLcode>
1755 TTFDEBUG { ( redefine codepoint by AdobeGlyphList, ) print dup //== exec flush } if
1756 put % <<AGL>> <<pbe>>
1757 } { % <<AGL>> <<pbe>> <glyphname>
1758 TTFDEBUG { ( unknown glyphname, leave as it is ) = flush } if
1759 pop % <<AGL>> <<pbe>>
1760 } ifelse
1761 } forall
1762 exch pop % <<pbe>>
1763 TTFDEBUG { dup (<<\n) print { exch ( ) print =only (\t) print //== exec } forall (>>\n) print flush } if
1764 exch AdobeGlyphList
1765 //.pdfmapchars exec
1766 /Encoding prebuilt_encoding
1767 } {
1768 % Likely this branch is now obsolete.
1769 TTFDEBUG { (Using cmap 3.1 for non-symbolic.) = } if
1770 0 dict exch
1771 AdobeGlyphList //.pdfmapchars exec
1772 /Encoding /WinAnsiEncoding .findencoding
1773 % WinAnsiEncoding is just a stub here.
1774 % It will be replaced with one from font resource,
1775 % because PDF spec requires it.
1776 } ifelse
1777 } {
1778 <00010000> //.findcmap exec {
1779 /TT_cmap 1 index cmapdict 3 -1 roll
1780 TTFDEBUG { (Using cmap 1.0 for non-symbolic.) = } if
1781 prebuilt_encoding //null ne {
1782 prebuilt_encoding .invert_encoding exch .romanmacdict //.pdfmapchars exec
1783 prebuilt_encoding
1784 } {
1785 0 dict exch .romanmacdict //.pdfmapchars exec
1786 /MacRomanEncodingForTrueType .findencoding
1787 } ifelse
1788 /Encoding exch
1789 } {
1790 % Apply the default algorithm for using the 'post'.
1791 .charkeys
1792 % But use PDF encoding when available.
1793 prebuilt_encoding //null ne {
1794 pop prebuilt_encoding
1795 } if
1796 } ifelse
1797 } ifelse
1798 } if
1799 TTFDEBUG { (.pdfcharkeys end) = } if
1800 } .forcebind def
1801
1802 % <file> <is_symbolic> <Encoding|null> <FontName> .loadpdfttfont <type42font>
1803 /.loadpdfttfont {
1804 TTFDEBUG { (.loadpdfttfont FontName=) print dup == } if
1805 /font_name gput
1806 TTFDEBUG { (.loadpdfttfont Encoding=) print dup //== exec } if
1807 /prebuilt_encoding gput % for .pdfcharkeys
1808 /is_symbolic gput
1809 % init key for whether to display the TTF notdef
1810 //false /.render_notdef gput
1811 //false 0 .loadttfonttables
1812 .makesfnts
1813 tabdict /cmap known not {
1814 ( **** Warning: Embedded TT font lacks required cmap table. Using identity cmap.\n)
1815 pdfformaterror
1816 % Unknown (i.e. not 0 ) language ID brings in post table.
1817 % Fields: cmap_version, number_of_subtables,
1818 % plarform_id, encoding_id, offset,
1819 % format, subtable_length, language,
1820 % 256 bytes of identity mapping.
1821 tabdict /cmap
1822 < 0000 0001
1823 0001 D00D 0000000c
1824 0000 0106 0000
1825 00 01 02 03 04 05 06 07 08 09 0a 0d 0c 0d 0e 0f
1826 10 11 12 13 14 15 16 17 18 19 1a 1d 1c 1d 1e 1f
1827 20 21 22 23 24 25 26 27 28 29 2a 2d 2c 2d 2e 2f
1828 30 31 32 33 34 35 36 37 38 39 3a 3d 3c 3d 3e 3f
1829 40 41 42 43 44 45 46 47 48 49 4a 4d 4c 4d 4e 4f
1830 50 51 52 53 54 55 56 57 58 59 5a 5d 5c 5d 5e 5f
1831 60 61 62 63 64 65 66 67 68 69 6a 6d 6c 6d 6e 6f
1832 70 71 72 73 74 75 76 77 78 79 7a 7d 7c 7d 7e 7f
1833 80 81 82 83 84 85 86 87 88 89 8a 8d 8c 8d 8e 8f
1834 90 91 92 93 94 95 96 97 98 99 9a 9d 9c 9d 9e 9f
1835 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ad ac ad ae af
1836 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bd bc bd be bf
1837 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cd cc cd ce cf
1838 d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da dd dc dd de df
1839 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea ed ec ed ee ef
1840 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fd fc fd fe ff
1841 > readonly put
1842 } if
1843 .getpost
1844 .pickcmap
1845 mark
1846 //.pdfcharkeys exec
1847 //.ttkeys exec
1848 /FontType 42
1849 /PaintType 0
1850 /.render_notdef .render_notdef
1851 TTFDEBUG {
1852 (numloca=) print numloca =
1853 } if
1854 .dicttomark
1855 dup /FontName font_name put % replace any definition of /FontName
1856 end end
1857 .completefont
1858 } .forcebind odef
1859
1860 %--------- Define main type42font loading routines, after their dependencies ------------
1861 % <file> .loadttfont <type42font>
1862 /.loadttfont {
1863 TTFDEBUG { (.loadttfont) = } if
1864 //false
1865 % if this came from Fontmap, there may be a dictionary on the dict stack
1866 % with a SubfontID in it. Validate it *is* a Fontmap record by also
1867 % checking for a Path key
1868 /SubfontID where
1869 {
1870 dup /Path known
1871 {/SubfontID get}
1872 {pop 0} ifelse
1873 }
1874 {0} ifelse
1875 .loadttfonttables
1876 .makesfnts
1877 .getpost
1878 .pickcmap
1879 mark
1880 % if we're loading to substitute for TTF in a PDF,
1881 % load the font the "PDF way"
1882 /prebuilt_encoding where
1883 {pop //.pdfcharkeys exec}
1884 {.charkeys} ifelse
1885 //.ttkeys exec
1886 /FontType 42
1887 /PaintType 0
1888 TTFDEBUG {
1889 (numloca=) print numloca =
1890 } if
1891 .dicttomark
1892 end end dup /FontName get exch definefont
1893 } .forcebind def
1894
1895 % <file> .loadwofffont <type42font>
1896 /.loadwofffont
1897 {
1898 TTFDEBUG { (.loadwofffont) = } if
1899 //false 0 //.loadwofftables exec
1900 .makesfnts
1901 .getpost
1902 .pickcmap
1903 mark
1904 .charkeys
1905 //.ttkeys exec
1906 /FontType 42
1907 /PaintType 0
1908 TTFDEBUG {
1909 (numloca=) print numloca =
1910 } if
1911 .dicttomark
1912 end end dup /FontName get exch
1913 definefont
1914 } .forcebind def
1915
1916 % ----- Utilities for loading possible TrueType font ------
1917 % <file> .findttfontname <fname> true
1918 % <file> .findttfontname false
1919 % Closes the file in either case.
1920 /.findttfontname {
1921 //true 0 .loadttfonttables
1922 tabdict /name .knownget {
1923 dup 8 getu32 f exch setfileposition
1924 12 getu32
1925 dup 65535 gt { pop 65535 } if % protect against extremely large name
1926 string f exch readstring pop
1927 dup
1928 6 findname not {
1929 4 findname % Try FullName
1930 } {
1931 exch pop //true
1932 }
1933 ifelse
1934 } {
1935 //false
1936 } ifelse
1937 f closefile end end
1938 } .forcebind def
1939
1940 /.findwoffname {
1941 //true 0 //.loadwofftables exec
1942 tabdict /name .knownget {
1943 dup /clen exch 8 getu32 def
1944 dup /olen exch 12 getu32 def
1945 4 getu32 f exch setfileposition
1946 olen dup 65535 gt { pop 65535 } if % protect against extremely large name
1947 /fs
1948 clen olen ne
1949 {f clen () /SubFileDecode filter /FlateDecode filter}
1950 {f clen () /SubFileDecode filter}
1951 ifelse def
1952 string fs exch readstring pop
1953 fs closefile
1954 dup
1955 6 findname not {
1956 4 findname % Try FullName
1957 } {
1958 exch pop //true
1959 }
1960 ifelse
1961 } {
1962 //false
1963 } ifelse
1964 f closefile end end
1965 } .forcebind def
1966
1967 /tt_tag_dict << <00010000> 0 (true) 0 (typ1) 0 (ttcf) 0 >> readonly def
1968 /ttf_otf_tag_dict << <00010000> 0 (true) 0 (typ1) 0 (ttcf) 0 (OTTO) 0>> readonly def
1969 % The wOFF tag is immediately followed by the sfnt "flavour" so for simplicity
1970 % combine them
1971 /woff_tag_dict <<
1972 (wOFF) 0
1973 <774F464600010000> 0 % (wOFF)<00010000> .concatstrings
1974 (wOFFtrue) 0
1975 (wOFFtyp1) 0
1976 >> readonly def
1977
1978 % <file> .is_ttf_or_otf <bool>
1979 /.is_ttf_or_otf {
1980 dup 0 setfileposition (1234) .peekstring { //ttf_otf_tag_dict exch known } { //false } ifelse
1981 } .forcebind def
1982
1983 /.is_woff {
1984 dup 0 setfileposition (1234) .peekstring { //woff_tag_dict exch known } { //false } ifelse
1985 } .forcebind def
1986
1987 % <file> <key> .findfontvalue <value> true
1988 % <file> <key> .findfontvalue false
1989 % Closes the file in either case.
1990 /.findnonttfontvalue /.findfontvalue load def
1991 /.findfontvalue {
1992 1 index //.is_woff exec {
1993 dup /FontType eq {
1994 pop closefile 42 //true
1995 } {
1996 dup /FontName eq { pop //.findwoffname exec} { pop closefile //false } ifelse
1997 } ifelse
1998 }
1999 {
2000 1 index //.is_ttf_or_otf exec {
2001 % If this is a font at all, it's a TrueType font.
2002 dup /FontType eq {
2003 pop closefile 42 //true
2004 } {
2005 dup /FontName eq { pop //.findttfontname exec} { pop closefile //false } ifelse
2006 } ifelse
2007 } {
2008 % Not a TrueType font.
2009 .findnonttfontvalue
2010 } ifelse
2011 } ifelse
2012 } .forcebind def
2013
2014 % Load a font file that might be a TrueType font.
2015 % <file> .loadfontfile -
2016 /.loadnonttfontfile /.loadfontfile load def
2017 /.loadfontfile {
2018 dup (12345678) .peekstring { //woff_tag_dict exch known } { //false } ifelse
2019 {
2020 //.loadwofffont exec pop
2021 }
2022 {
2023 dup (1234) .peekstring { //tt_tag_dict exch known } { //false } ifelse {
2024 % If this is a font at all, it's a TrueType font.
2025 //.loadttfont exec pop
2026 } {
2027 % Not a TrueType font.
2028 .loadnonttfontfile
2029 } ifelse
2030 } ifelse
2031 } .forcebind def
2032
2033 % Undef the local utility funcs
2034 [
2035 /.findttfontname
2036 /.findwoffname
2037 /ttf_otf_tag_dict
2038 /tt_tag_dict
2039 /woff_tag_dict
2040 /.is_ttf_or_otf
2041 /.is_woff
2042 ] currentdict .undefinternalnames
2043 % ----- END .loadfontfile that supports possible TrueType font ------
2044
2045 % Undef these, not needed outside this file
2046 [
2047 /.addglyph
2048 /.definettcidfont
2049 /.fill_identity_cmap
2050 /.findseg
2051 /.loadttfontdict
2052 /.is_hex_digit
2053 /.pop3ex
2054 /.popfex
2055 /.ttkeys
2056 /.printtab
2057 /.readttdata
2058 /.nname
2059 /.loadttfont
2060 /.loadwofffont
2061 /.loadwofftables
2062 /.findcmap
2063 /.pdfmapsymbolic
2064 /.pdfcharkeys
2065 /.pdfmapchars
2066 ] systemdict .undefinternalnames