1 % Copyright (C) 1990-2003 artofcode LLC. 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_fonts.ps,v 1.48 2004/11/24 20:09:01 ghostgum Exp $
17 % Font initialization and management code.
19 % Define the default font.
20 /defaultfontname /Courier def
22 % Define the name of the font map file.
23 % Note that the "%%Replace " comment below provides the font map file name
24 % for compiling initialization files into executable. Most likely it should be
25 % consistent with the one specified here.
26 /defaultfontmap (Fontmap) def
28 /defaultfontmap_content 50 dict
30 /; { 2 index 3 1 roll .growput } bind def
31 %% Replace 0 (Fontmap)
34 % ------ End of editable parameters ------ %
36 % Define the UniqueIDs and organization XUID assigned to Aladdin.
37 % UniqueIDs 5,066,501 - 5,066,580 are assigned as follows:
38 % 01 and 02 for shareware Cyrillic
39 % 33 through 67 for Type 1 versions of the Hershey fonts
40 % UniqueIDs 5,115,501 - 5,115,600 are currently unassigned.
41 /AladdinEnterprisesXUID 107 def
43 % If SUBSTFONT is defined, make it the default font.
44 /SUBSTFONT where { pop /defaultfontname /SUBSTFONT load def } if
46 % Define a reliable way of accessing FontDirectory in systemdict.
48 { /FontDirectory .systemvar
51 % If DISKFONTS is true, we load individual CharStrings as they are needed.
52 % (This is intended primarily for machines with very small memories.)
53 % In this case, we define another dictionary, parallel to FontDirectory,
54 % that retains an open file for every font loaded.
55 /FontFileDirectory 10 dict def
57 % Define a temporary string for local use, since using =string
58 % interferes with some PostScript programs.
59 /.fonttempstring 8192 string def
61 % Split up a search path into individual directories or files.
62 /.pathlist % <path> .pathlist <dir1|file1> ...
63 { { dup length 0 eq { pop exit } if
64 .filenamelistseparator search not { exit } if
70 % Load a font name -> font file name map.
71 userdict /Fontmap .FontDirectory maxlength dict put
72 /.loadFontmap { % <file> .loadFontmap -
73 % We would like to simply execute .definefontmap as we read,
74 % but we have to maintain backward compatibility with an older
75 % specification that makes later entries override earlier
76 % ones within the same file.
77 50 dict exch .readFontmap
78 { .definefontmap } forall
80 /.readFontmap { % <dict> <file> .readFontmap <dict>
81 { dup token not { closefile exit } if
82 % stack: dict file fontname
83 % This is a hack to get around the absurd habit of MS-DOS editors
84 % of adding an EOF character at the end of the file.
85 dup (\032) eq { pop closefile exit } if
87 { (Fontmap entry for ) print dup =only
88 ( has no associated file or alias name! Giving up.) = flush
89 {.readFontmap} 0 get 1 .quit
91 dup type dup /stringtype eq exch /nametype eq or not
92 { (Fontmap entry for ) print 1 index =only
93 ( has an invalid file or alias name! Giving up.) = flush
94 {.readFontmap} 0 get 1 .quit
96 % stack: dict file fontname filename|aliasname
97 1 index type /stringtype eq
98 1 index type /nametype eq and 1 index xcheck and
99 1 index /run eq 2 index /.runlibfile eq or and {
100 % This is an inclusion entry.
101 pop findlibfile { exch pop } { file } ifelse
102 2 index exch .readFontmap pop
104 % This is a real entry.
105 % Read and pop tokens until a semicolon.
107 { (Fontmap entry for ) print 1 index =only
108 ( ends prematurely! Giving up.) = flush
109 {.loadFontmap} 0 get 1 .quit
111 dup /; eq { pop 3 index 3 1 roll .growput exit } if
117 % Add an entry in Fontmap. We redefine this if the Level 2
118 % resource machinery is loaded.
119 /.definefontmap % <fontname> <file|alias> .definefontmap -
120 { % Since Fontmap is global, make sure the values are storable.
121 % If the fontname contains Unicode (first byte == \000) and
122 % this is not an alias definition, define an alias using ASCII
123 % (stripping out the high \000 bytes). Observed with some TT fonts.
124 1 index 100 string cvs 0 get 0 eq 1 index type /nametype ne and {
125 1 index 100 string cvs dup length 2 div cvi string true exch
126 0 1 2 index length 1 sub {
127 % stack: fontname filename fontnamestring addflag newstring index
128 dup 4 index exch 2 mul get 0 ne {
129 % High byte of pair is not \000
133 dup 4 index exch 2 mul 1 add get 2 index 3 1 roll put
136 DEBUG { (\nAdding alias for: ) print 1 index ==only ( as: ) print dup == flush } if
137 cvn exch cvn .definefontmap % recurse with an alias
139 pop pop % discard the name
142 .currentglobal 3 1 roll true .setglobal
143 dup type /stringtype eq
144 { dup .gcheck not { dup length string copy } if
147 Fontmap 3 -1 roll 2 copy .knownget
148 { % Add an element to the end of the existing value,
149 % unless it's the same as the current last element.
150 mark exch aload pop counttomark 4 add -1 roll
151 2 copy eq { cleartomark pop pop } { ] readonly .growput } ifelse
153 { % Make a new entry.
154 mark 4 -1 roll ] readonly .growput
159 % Parse a font file just enough to find the FontName or FontType.
160 /.findfontvalue { % <file> <key> .findfontvalue <value> true
161 % <file> <key> .findfontvalue false
162 % Closes the file in either case.
164 2 copy unread 16#80 eq {
165 dup (xxxxxx) readstring pop pop % skip .PFB header
168 % Protect ourselves against syntax errors here.
169 dup { token } stopped { pop false exit } if
170 not { false exit } if % end of file
171 dup /eexec eq { pop false exit } if % reached eexec section
172 dup /Subrs eq { pop false exit } if % Subrs without eexec
173 dup /CharStrings eq { pop false exit } if % CharStrings without eexec
175 { xcheck not { dup token exit } if } % found key
179 % Stack: key file value true (or)
180 % Stack: key file false
181 dup { 4 } { 3 } ifelse -2 roll closefile pop
182 } { closefile pop false } ifelse
185 { /FontName .findfontvalue
188 % If there is no FONTPATH, try to get one from the environment.
189 NOFONTPATH { /FONTPATH () def } if
192 { /FONTPATH (GS_FONTPATH) getenv not { () } if def }
194 FONTPATH length 0 eq { (%END FONTPATH) .skipeof } if
195 /FONTPATH [ FONTPATH .pathlist ] def
197 % Scan directories looking for plausible fonts. "Plausible" means that
198 % the file begins with %!PS-AdobeFont or %!FontType1, or with \200\001
199 % followed by four arbitrary bytes and then either of these strings.
200 % To speed up the search, we skip any file whose name appears in
201 % the Fontmap (with any extension and upper/lower case variation) already,
202 % and any file whose extension definitely indicates it is not a font.
204 % NOTE: The current implementation of this procedure is somewhat Unix/DOS-
205 % specific. It assumes that '/' and '\' are directory separators, and that
206 % the part of a file name following the last '.' is the extension.
208 /.lowerstring % <string> .lowerstring <lowerstring>
209 { 0 1 2 index length 1 sub
210 { 2 copy get dup 65 ge exch 90 le and
211 { 2 copy 2 copy get 32 add put }
216 /.splitfilename { % <dir.../base.extn> .basename <base> <extn>
217 % Make the file name read-only to detect aliasing bugs.
218 % We really don't like doing this, but we've had one
221 (/) search { true } { (\\) search } ifelse { pop pop } { exit } ifelse
223 dup { (.) search { pop pop } { exit } ifelse } loop
227 exch dup length 2 index length 1 add sub 0 exch getinterval exch
230 /.scanfontdict 1 dict def % establish a binding
232 { % Construct the table of all file names already in Fontmap.
233 currentglobal true setglobal
234 .scanfontdict dup maxlength Fontmap length 2 add .max .setmaxlength
237 { dup type /stringtype eq
238 { .splitfilename pop .fonttempstring copy .lowerstring cvn
239 .scanfontdict exch true put
251 % Strings are converted to names anyway, so....
268 /pss true % Adobe Multiple Master font instances
271 /.scan1fontstring 8192 string def
272 % %%BeginFont: is not per Adobe documentation, but a few fonts have it.
273 /.scanfontheaders [(%!PS-Adobe*) (%!FontType*) (%%BeginFont:*)] def
274 0 .scanfontheaders { length .max } forall 6 add % extra for PFB header
275 /.scan1fontfirst exch string def
276 /.scanfontdir % <dirname> .scanfontdir -
277 { currentglobal exch true setglobal
278 QUIET not { (Scanning ) print dup print ( for fonts...) print flush } if
279 [ 1 index ] (*) .generate_dir_list_templates
280 0 0 0 4 -1 roll % found scanned files
281 { % stack: <fontcount> <scancount> <filecount> <filename>
282 exch 1 add exch % increment filecount
283 dup .splitfilename .fonttempstring copy .lowerstring
284 % stack: <fontcount> <scancount> <filecount+1> <filename>
286 .scanfontskip exch known exch .scanfontdict exch known or
288 % stack: <fontcount> <scancount> <filecount+1>
290 { 3 -1 roll 1 add 3 1 roll
291 % stack: <fontcount> <scancount+1> <filecount+1> <filename>
292 dup (r) { file } .internalstopped
294 % stack: <fontcount> <scancount+1> <filecount+1> <filename>
298 % On some platforms, the file operator will open directories,
299 % but an error will occur if we try to read from one.
300 % Handle this possibility here.
301 dup .scan1fontfirst { readstring } .internalstopped
305 % stack: <fontcount> <scancount+1> <filecount+1>
306 % <filename> <file> <header>
309 % Check for PFB file header.
310 dup (\200\001????*) .stringmatch
311 { dup length 6 sub 6 exch getinterval }
313 % Check for font file headers.
314 false .scanfontheaders
315 { 2 index exch .stringmatch or
318 { % stack: <fontcount> <scancount+1> <filecount+1> <filename>
320 dup 0 setfileposition .findfontname
321 { dup Fontmap exch known
324 { exch copystring exch
325 DEBUG { ( ) print dup =only flush } if
326 1 index .definefontmap
327 .splitfilename pop true .scanfontdict 3 1 roll .growput
328 % Increment fontcount.
329 3 -1 roll 1 add 3 1 roll
337 % .findfontname will have done a closefile in the above case.
338 { dup null eq { pop } { closefile } ifelse pop
344 .scan1fontstring filenameforall
347 { ( ) print =only ( files, ) print =only ( scanned, ) print
348 =only ( new fonts.) = flush
357 % Try to enumerate native fonts registered with the os
358 % and add them to the fontmap. This relies on a custom
359 % operator which calls platform-specific C code. It
360 % returns an array of arrays, each containing a pair
361 % of strings: what the system thinks is the ps name,
362 % and the access path.
363 /.setnativefontmapbuilt { % set whether we've been run
364 systemdict exch /.nativefontmapbuilt exch .forceput
365 } .bind executeonly def
366 false .setnativefontmapbuilt
367 /.buildnativefontmap { % - .buildnativefontmap <bool>
369 (Querying operating system for font files...\n)
376 % stack: [ (name) (path) ]
377 % verify the font name ourselves
378 dup 1 get (r) { file } stopped
380 % skip the entry if we can't open the returned path
383 % we could open the font file
385 not { dup 0 get } if % stack: (newname) [ (name) (path) ]
386 % DEBUG { ( found ) print dup print (\n) print flush } if
387 % add entry to the fontmap
388 1 index exch 0 exch dup type /nametype ne {cvn} if put
389 aload pop .definefontmap
393 % record that we've been run
394 true .setnativefontmapbuilt
397 % Create the dictionary that registers the .buildfont procedure
398 % (called by definefont) for each FontType.
399 /buildfontdict 20 dict def
401 % Register Type 3 fonts, which are always supported, for definefont.
402 buildfontdict 3 /.buildfont3 cvx put
404 % Register Type 0 fonts if they are supported. Strictly speaking,
405 % we should do this in its own file (gs_type0.ps), but since this is
406 % the only thing that would be in that file, it's simpler to put it here.
407 /.buildfont0 where { pop buildfontdict 0 /.buildfont0 cvx put } if
409 % Define definefont. This is a procedure built on a set of operators
410 % that do all the error checking and key insertion.
412 { % Grow the font dictionary, if necessary, to ensure room for an
413 % added entry, making sure there is at least one slot left for FID.
414 dup maxlength 1 index length sub 2 lt
417 { .growdictlength dict .copydict }
420 { dup wcheck not { dup maxlength dict .copydict } if
425 { % Check for disabled platform fonts.
427 { % Make sure we leave room for FID.
428 .growfontdict dup /ExactSize 0 put
430 { % Hack: if the Encoding looks like it might be the
431 % Symbol or Dingbats encoding, load those now (for the
432 % benefit of platform font matching) just in case
433 % the font didn't actually reference them.
434 % Note that some types of font don't have an Encoding.
435 dup /Encoding .knownget {
438 dup /congruent eq { SymbolEncoding pop } if
439 /a9 eq { DingbatsEncoding pop } if
446 dup /.OrigFont known not {
447 dup dup /.OrigFont exch .growput
450 % If this is a CIDFont, CIDFontType takes precedence
452 dup /CIDFontType known {
453 /.buildcidfont where {
454 pop exch not exch % true => false
458 dup /FontType get //buildfontdict exch get exec
464 FontFileDirectory 2 index known {
465 dup /FontFile FontFileDirectory 4 index get .growput
468 systemdict /ProvideUnicode .knownget not { false } if {
469 /FontEmulationProcs /ProcSet findresource
470 /ProvideUnicodeDecoding get exec
472 readonly % stack: name fontdict
473 } stopped { /invalidfont signalerror } if
477 % If the current allocation mode is global, also enter
478 % the font in LocalFontDirectory.
480 { //systemdict /LocalFontDirectory .knownget
481 { 2 index 2 index .growput }
485 dup .FontDirectory 4 -2 roll .growput
486 % If the font originated as a resource, register it.
487 currentfile .currentresourcefile eq { dup .registerfont } if
490 % Define a procedure for defining aliased fonts.
491 % We use this only for explicitly aliased fonts, not substituted fonts:
492 % we think this matches the observed behavior of Adobe interpreters.
493 /.aliasfont % <name> <font> .aliasfont <newFont>
494 { .currentglobal 3 1 roll dup .gcheck .setglobal
495 % <bool> <name> <font>
496 dup length 2 add dict % <bool> <name> <font> <dict>
497 dup 3 -1 roll % <bool> <name> <dict> <dict> <font>
498 { 1 index /FID eq { pop pop } { put dup } ifelse } forall
499 % <bool> <name> <dict> <dict>
500 % Stack: global fontname newfont newfont.
501 % We might be defining a global font whose FontName
502 % is a local string. This is weird, but legal,
503 % and doesn't cause problems anywhere else:
504 % to avoid any possible problems in this case, do a cvn.
505 % We might also be defining (as an alias) a global font
506 % whose FontName is a local non-string, if someone passed a
507 % garbage value to findfont. In this case, just don't
508 % call definefont at all.
509 2 index dup type /stringtype eq exch .gcheck or 1 index .gcheck not or
510 { pop % <bool> <name> <dict>
511 1 index dup type /stringtype eq { cvn } if
512 % <bool> <name> <dict> <name1>
514 % We want to know whether we alias a font,
515 % because in this case its FontName to be replaced with the alias.
516 % There is no simple way to know that at this point.
517 % But if the original font has defaultfontname,
518 % we probably substitute it rather than alias.
519 % Using such condition as an approximation to the strong condition.
521 % Note it works wrongly if Fontmap maps something to defaultfontname like this :
522 % /Courier /NimbusMonL-Regu ;
523 % /Something /Courier ;
524 % The FontName of Something will not be /Something. It will be /Courier .
526 1 index /FontName get defaultfontname ne {
527 2 copy /FontName exch put
529 1 index exch /.Alias exch put % <bool> <name> <dict>
530 dup dup /.OrigFont exch .growput
531 % Don't bind in definefont, since Level 2 redefines it.
532 /definefont .systemvar exec
534 { .completefont pop exch pop
538 } odef % so findfont will bind it
540 % Define .loadfontfile for loading a font. If we recognize Type 1 and/or
541 % TrueType fonts, gs_type1.ps and/or gs_ttf.ps will redefine this.
543 % According to Ed Taft, Adobe interpreters push userdict
544 % before loading a font, and pop it afterwards.
550 //systemdict /.loadingfont 3 -1 roll .forceput
551 } .bind odef % .forceput must be bound and hidden
553 { % Some buggy fonts leave extra junk on the stack,
554 % so we have to make a closure that records the stack depth
555 % in a fail-safe way.
557 { /FAPI_hook_disable pop % gs_fapi accesses this with execstack_lookup - don't remove !
558 {{.loadfontfile} .execasresource} count 1 sub 2 .execn
559 count exch sub { pop } repeat
561 } loop % this loop is a pattern for execstack_lookup, don't remove !
562 false .setloadingfont
565 % Find an alternate font to substitute for an unknown one.
566 % We go to some trouble to parse the font name and extract
567 % properties from it. Later entries take priority over earlier.
569 % Guess at suitable substitutions for random unknown fonts.
570 [(Book) /NewCenturySchlbk 0]
571 [(Grot) /Helvetica 0]
573 [(Chancery) /ZapfChancery-MediumItalic 0]
574 % If the family name appears in the font name,
575 % use a font from that family.
576 [(Arial) /Helvetica 0]
577 [(Avant) /AvantGarde 0]
578 [(Bookman) /Bookman 0]
579 [(Century) /NewCenturySchlbk 0]
581 [(Frut) /Helvetica 0]
582 [(Garamond) /Palatino 0]
583 [(Geneva) /Helvetica 0]
584 [(Helv) /Helvetica 0]
585 [(NewYork) /Bookman 0]
587 [(Schlbk) /NewCenturySchlbk 0]
588 [(Swiss) /Helvetica 0]
591 % Substitute for Adobe Multiple Master fonts.
593 [(Myriad) /Helvetica 0]
594 % If the font wants to be monospace, use Courier.
595 [(Monospace) /Courier 0]
596 [(Typewriter) /Courier 0]
597 % Define substitutes for the other Adobe PostScript 3 fonts.
598 % For some of them, the substitution is pretty bad!
599 [(Albertus) /Palatino 0]
600 [(AntiqueOlive) /Helvetica 0]
601 [(Bodoni) /NewCenturySchlbk 0]
602 [(Chicago) /Helvetica 2]
603 [(Clarendon) /Bookman 0]
604 [(Cooper) /NewCenturySchlbk 0]
605 [(Copperplate) /AvantGarde 0] % inappropriate, small-cap font
606 [(Coronet) /ZapfChancery-MediumItalic 0]
607 [(Eurostile) /Helvetica 0]
608 [(Geneva) /Courier 2] % should be fixed-pitch sans demi
609 [(GillSans) /Helvetica 2]
610 [(GillSans-Light) /Helvetica 0]
611 [(Goudy) /Palatino 0]
612 [(Hoefler) /NewCenturySchlbk 0]
614 [(LetterGothic) /Courier 0] % should be fixed-pitch sans
615 [(LubalinGraph-Book) /Bookman 2]
616 [(LubalinGraph-Demi) /Bookman 0]
617 [(Marigold) /ZapfChancery-MediumItalic 0]
618 [(MonaLisa-Recut) /Palatino 0] % inappropriate
619 [(Monaco) /Courier 2] % should be fixed-pitch sans demi
620 [(Optima) /Helvetica 0]
621 [(Oxford) /ZapfChancery-MediumItalic 0]
622 [(Tekton) /Helvetica 0]
623 [(Univers) /Helvetica 0]
625 /.substituteproperties [
626 [(It) 9] [(Oblique) 1]
627 [(Black) 2] [(Bd) 2] [(Bold) 2] [(bold) 2] [(Demi) 2] [(Heavy) 2] [(Sb) 2]
628 [(Cn) 4] [(Cond) 4] [(Narrow) 4] [(Pkg) 4] [(Compr) 4]
629 [(Serif) 8] [(Sans) -8]
631 /.fontnameproperties { % <int> <string|name> .fontnameproperties
634 .substituteproperties {
635 2 copy 0 get search {
636 pop pop pop dup length 1 sub 1 exch getinterval 3 -1 roll exch {
637 dup 0 ge { or } { neg not and } ifelse
644 /.substitutefamilies mark
646 {/AvantGarde-Book /AvantGarde-BookOblique
647 /AvantGarde-Demi /AvantGarde-DemiOblique}
649 {/Bookman-Demi /Bookman-DemiItalic /Bookman-Light /Bookman-LightItalic}
651 {/Courier /Courier-Oblique /Courier-Bold /Courier-BoldOblique}
653 {/Helvetica /Helvetica-Oblique /Helvetica-Bold /Helvetica-BoldOblique
654 /Helvetica-Narrow /Helvetica-Narrow-Oblique
655 /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique}
657 {/NewCenturySchlbk-Roman /NewCenturySchlbk-Italic
658 /NewCenturySchlbk-Bold /NewCenturySchlbk-BoldItalic}
660 {/Palatino-Roman /Palatino-Italic /Palatino-Bold /Palatino-BoldItalic}
662 {/Symbol /Symbol /Symbol /Symbol}
664 {/Times-Roman /Times-Italic /Times-Bold /Times-BoldItalic}
665 /ZapfChancery-MediumItalic
666 {/ZapfChancery-MediumItalic}
667 .dicttomark readonly def
668 /.nametostring { % <name> .nametostring <string>
669 % <other> .nametostring <other>
670 dup type /nametype eq { .namestring } if
672 /.fontnamestring { % <fontname> .fontnamestring <string|name>
673 dup type dup /nametype eq {
676 /stringtype ne { pop () } if
679 /.substitutefontname { % <fontname> <properties> .substitutefontname
681 % Look for properties and/or a face name in the font name.
682 % If we find any, use Times (serif) or Helvetica (sans) as the
683 % base font; otherwise, use the default font.
684 % Note that the "substituted" font name may be the same as
685 % the requested one; the caller must check this.
686 exch .fontnamestring {
687 defaultfontname /Helvetica-Oblique /Helvetica-Bold /Helvetica-BoldOblique
688 /Helvetica-Narrow /Helvetica-Narrow-Oblique
689 /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique
690 /Times-Roman /Times-Italic /Times-Bold /Times-BoldItalic
691 /Helvetica-Narrow /Helvetica-Narrow-Oblique
692 /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique
694 % Stack: facelist properties fontname
695 % Look for a face name.
697 2 copy 0 get search {
699 % Stack: facelist properties fontname [(pattern) family properties]
700 dup 2 get 4 -1 roll or 3 1 roll
701 1 get .substitutefamilies exch get
702 4 -1 roll pop 3 1 roll
707 1 index length mod get exec
709 /.substitutefont { % <fontname> .substitutefont <altname>
710 dup 0 exch .fontnameproperties .substitutefontname
711 % Only accept fonts known in the Fontmap.
712 Fontmap 1 index known not { pop defaultfontname } if
715 % If requested, make (and recognize) fake entries in FontDirectory for fonts
716 % present in Fontmap but not actually loaded. Thanks to Ray Johnston for
717 % the idea behind this code.
718 FAKEFONTS not { (%END FAKEFONTS) .skipeof } if
720 % We use the presence or absence of the FontMatrix key to indicate whether
721 % a font is real or fake. We must pop the arguments at the very end,
722 % so that stack protection will be effective.
724 /definefont { % <name> <font> definefont <font>
725 dup /FontMatrix known {
728 2 copy /FontName get findfont //definefont exch pop exch pop
732 /scalefont { % <font> <scale> scalefont <font>
733 1 index /FontMatrix known {
736 1 index /FontName get findfont 1 index //scalefont
741 /makefont { % <font> <matrix> makefont <font>
742 1 index /FontMatrix known {
745 1 index /FontName get findfont 1 index //makefont
750 /setfont { % <font> setfont -
751 dup /FontMatrix known {
754 dup /FontName get findfont //setfont pop
760 % Define findfont so it tries to load a font if it's not found.
761 % The Red Book requires that findfont be a procedure, not an operator,
762 % but it still needs to restore the stacks reliably if it fails,
763 % so we do all the work in an operator.
764 /.findfont { % <fontname> .findfont <font>
765 mark 1 index % <fontname> mark <fontname>
766 //systemdict begin .dofindfont
767 % <fontname> mark <alias> ... <font>
768 % Define any needed aliases.
769 counttomark 1 sub { .aliasfont } repeat end
770 % <fontname> mark <font>
776 % Check whether the font name we are about to look for is already on the list
777 % of aliases we're accumulating; if so, cause an error.
778 /.checkalias % -mark- <alias1> ... <name> .checkalias <<same>>
779 { counttomark 1 sub -1 1
782 { (Unable to substitute for font.) = flush
784 /findfont cvx /invalidfont signalerror
790 % Get a (non-fake) font if present in a FontDirectory.
791 /.fontknownget % <fontdir> <fontname> .fontknownget <font> true
792 % <fontdir> <fontname> .fontknownget false
795 { dup /FontMatrix known { true } { pop false } ifelse }
803 % This is the standard procedure for handling font substitution.
804 % Its location is per an Adobe newsgroup posting.
805 % It is called with the font name on the stack, standing in for findfont.
806 /.stdsubstfont { % mark <alias1> ... <fontname> .stdsubstfont mark <alias1> ... <aliasN> <font>
809 (Substituting for font ) print dup =only
813 % This mode is incompatible with high level devices.
814 cleartomark mark defaultfontname
817 2 copy eq { pop defaultfontname } if
821 (%%[) print 1 index =only
822 ( not found, substituting ) print dup =only (]%%)
824 (Substituting font ) print dup =only
825 ( for ) print 1 index =only (.)
832 % Default font substitution does {pop /Courier} om many implementations.
833 % GS post-process font substitution in .stdsubstfont and uses {} for
834 % backward compatibility
835 $error /SubstituteFont { } put
837 % Scan the next directory on FONTPATH.
838 /.scannextfontdir { % - .scannextfontdir <bool>
839 % If we haven't scanned all the directories in
840 % FONTPATH, scan the next one.
841 null 0 1 FONTPATH length 1 sub {
842 FONTPATH 1 index get null ne { exch pop exit } if pop
844 dup 0 eq { .scanfontbegin } if
845 FONTPATH 1 index get .scanfontdir
846 FONTPATH exch null put true
851 % Do the work of findfont, including substitution, defaulting, and
852 % scanning of FONTPATH.
853 /.dofindfont { % mark <fontname> .dofindfont % mark <alias> ... <font>
855 % We didn't find the font. If we haven't scanned
856 % all the directories in FONTPATH, scan the next one
857 % now and look for the font again.
859 % Start over with an empty alias list.
860 counttomark 1 sub { pop } repeat % mark <fontname>
863 % No more directories to scan. Try building the native
864 % font map entries if we haven't already done so.
865 systemdict /.nativefontmapbuilt get not { .buildnativefontmap } { false } ifelse {
866 % Same stack as at the beginning of .dofindfont.
867 .dofindfont % start over
869 % No luck. Make sure we're not already
870 % looking for the default font.
872 (Didn't find this font on the system!\n)
875 dup defaultfontname eq {
877 (Unable to load default font ) print
878 dup =only (! Giving up.) = flush
880 /findfont cvx /invalidfont signalerror
882 % Substitute for the font. Don't alias.
883 % Same stack as at the beginning of .dofindfont.
884 $error /SubstituteFont get exec
886 % igorm: I guess the surrounding code assumes that .stdsubstfont
887 % must ADD an alias to allow .checkalias and .findfont to work properly.
888 % Also I guess that a trailing recursion is
889 % used in .dofindfont and through .stdsubstfont
890 % just to represent a simple iteration,
891 % which accumulates the aliases after the mark.
897 % Try to find a font using only the present contents of Fontmap.
898 /.tryfindfont { % <fontname> .tryfindfont <font> true
899 % <fontname> .tryfindfont false
900 .FontDirectory 1 index .fontknownget
904 { dup Fontmap exch .knownget not
905 { % Unknown font name. Look for a file with the
906 % same name as the requested font.
909 { % Try each element of the Fontmap in turn.
910 false exch % (in case we exhaust the list)
911 % Stack: fontname false fontmaplist
913 dup type /nametype eq
915 .checkalias .tryfindfont exit
917 { dup dup type dup /arraytype eq exch /packedarraytype eq or exch xcheck and
918 { % Font with a procedural definition
919 exec % The procedure will load the font.
920 % Check to make sure this really happened.
921 .FontDirectory 1 index .knownget
922 { exch pop true exit }
926 .loadfontloop { true exit } if
933 % Stack: font true -or- fontname false
936 { % None of the Fontmap entries worked.
937 % Try loading a file with the same name
938 % as the requested font.
948 % any user of .putgstringcopy must use bind and executeonly
949 /.putgstringcopy % <dict> <name> <string> .putgstringcopy -
950 { 2 index gcheck currentglobal
955 dup length string copy
958 } .bind odef % must be bound and hidden for .forceput
961 % Attempt to load a font from a file.
962 /.tryloadfont { % <fontname> .tryloadfont <font> true
963 % <fontname> .tryloadfont false
965 % Hack: check for the presence of the resource machinery.
968 pop dup .fonttempstring /FontResourceDir getsystemparam .genericrfn
972 dup .nametostring .loadfontloop
978 /.loadfontloop { % <fontname> <filename> .loadfontloop
982 % See above regarding the use of 'loop'.
983 { % Is the font name a string?
984 dup type /stringtype ne
986 { (Can't find font with non-string name: ) print dup =only (.) = flush
991 % Can we open the file?
994 { (Can't find \(or can't open\) font file ) print dup print
1001 % Stack: fontname fontfilename fontfile
1003 { .currentglobal true .setglobal
1005 FontFileDirectory exch 5 index exch .growput
1010 { (Loading ) print 2 index =only
1011 ( font from ) print 1 index print (... ) print flush
1014 % If LOCALFONTS isn't set, load the font into local or global
1015 % VM according to FontType; if LOCALFONTS is set, load the font
1016 % into the current VM, which is what Adobe printers (but not
1018 LOCALFONTS { false } { /setglobal where } ifelse
1019 { pop /FontType .findfontvalue { 1 eq } { false } ifelse
1020 % .setglobal, like setglobal, aliases FontDirectory to
1021 % GlobalFontDirectory if appropriate. However, we mustn't
1022 % allow the current version of .setglobal to be bound in,
1023 % because it's different depending on language level.
1024 .currentglobal exch /.setglobal .systemvar exec
1025 % Remove the fake definition, if any.
1026 .FontDirectory 3 index .undef
1027 1 index (r) file .loadfont .FontDirectory exch
1028 /.setglobal .systemvar exec
1030 { .loadfont .FontDirectory
1033 % Stack: fontname fontfilename fontdirectory
1035 { //systemdict /level2dict known
1036 { .currentglobal false .setglobal vmstatus
1037 true .setglobal vmstatus 3 -1 roll pop
1038 6 -1 roll .setglobal 5
1042 ifelse { =only ( ) print } repeat
1046 % Check to make sure the font was actually loaded.
1047 dup 3 index .fontknownget
1048 { dup /PathLoad 4 index //.putgstringcopy exec
1049 4 1 roll pop pop pop true exit
1052 % Maybe the file had a different FontName.
1053 % See if we can get a FontName from the file, and if so,
1054 % whether a font by that name exists now.
1055 exch dup % Stack: origfontname fontdirectory path path
1056 (r) file .findfontname
1057 { % Stack: origfontname fontdirectory path filefontname
1058 2 index 1 index .fontknownget
1059 { % Yes. Stack: origfontname fontdirectory path filefontname fontdict
1060 dup 4 -1 roll /PathLoad exch //.putgstringcopy exec
1061 % Stack: origfontname fontdirectory filefontname fontdict
1063 % Stack: origfontname fontdict filefontname
1067 { (Using ) print =only
1068 ( font for ) print 1 index =only
1071 ifelse % Stack: origfontname fontdict
1075 if pop % Stack: origfontname fontdirectory path
1077 if pop pop % Stack: origfontname
1079 % The font definitely did not load correctly.
1081 { (Loading ) print dup =only
1082 ( font failed.) = flush
1086 } loop % end of loop
1088 } bind executeonly def % must be bound and hidden for .putgstringcopy
1090 currentdict /.putgstringcopy .undef
1092 % Define a procedure to load all known fonts.
1093 % This isn't likely to be very useful.
1095 { Fontmap { pop findfont pop } forall
1098 % If requested, load all the fonts defined in the Fontmap into FontDirectory
1099 % as "fake" fonts i.e., font dicts with only FontName and FontType defined.
1100 % (We define FontType only for the sake of some questionable code in the
1101 % Apple Printer Utility 2.0 font inquiry code.)
1103 % Note that this procedure only creates fake fonts in the FontDirectory
1104 % associated with the current VM. This is because in multi-context systems,
1105 % creating the fake fonts in local VM leads to undesirable complications.
1110 (gs_fonts FAKEFONTS) VMDEBUG
1112 pop dup type /stringtype eq { cvn } if
1113 .FontDirectory 1 index known not {
1114 2 dict dup /FontName 3 index put
1116 .FontDirectory 3 1 roll put
1122 FAKEFONTS { exch } if pop def % don't bind, .current/setglobal get redefined
1124 % Install initial fonts from Fontmap.
1128 { pop [ FONTMAP .pathlist ]
1129 { dup VMDEBUG findlibfile
1130 { exch pop .loadFontmap }
1131 { /undefinedfilename signalerror }
1137 false .file_name_combine
1140 (r) { file } .internalstopped {
1142 defaultfontmap_content { .definefontmap } forall
1154 userdict /defaultfontmap_content .undef
1155 .definefakefonts % current VM is global
1156 } def % don't bind, .current/setglobal get redefined
1158 % ---------------- Synthetic font support ---------------- %
1160 % Create a new font by modifying an existing one. paramdict contains
1161 % entries with the same keys as the ones found in a Type 1 font;
1162 % it should also contain enough empty entries to allow adding the
1163 % corresponding non-overridden entries from the original font dictionary,
1164 % including FID. If paramdict includes a FontInfo entry, this will
1165 % also override the original font's FontInfo, entry by entry;
1166 % again, it must contain enough empty entries.
1168 % Note that this procedure does not perform a definefont.
1170 /.makemodifiedfont % <fontdict> <paramdict> .makemodifiedfont <fontdict'>
1172 { % Stack: destdict key value
1174 { 2 index 2 index known
1175 { % Skip fontdict entry supplied in paramdict, but
1176 % handle FontInfo specially.
1177 1 index /FontInfo eq
1178 { 2 index 2 index get % new FontInfo
1179 1 index % old FontInfo
1180 { % Stack: destdict key value destinfo key value
1181 2 index 2 index known
1183 { 2 index 3 1 roll put }
1190 { % No override, copy the fontdict entry.
1191 2 index 3 1 roll put
1192 dup dup % to match pop pop below
1201 % Make a modified font and define it. Note that unlike definefont,
1202 % this does not leave the font on the operand stack.
1204 /.definemodifiedfont % <fontdict> <paramdict> .definemodifiedfont -
1206 dup /FontName get exch definefont pop