]> git.lizzy.rs Git - plan9front.git/blob - sys/lib/ghostscript/gs_fonts.ps
Import sources from 2011-03-30 iso image - sys/lib
[plan9front.git] / sys / lib / ghostscript / gs_fonts.ps
1 %    Copyright (C) 1990-2003 artofcode LLC.  All rights reserved.
2
3 % This software is provided AS-IS with no warranty, either express or
4 % implied.
5
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.
9
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.
15
16 % $Id: gs_fonts.ps,v 1.48 2004/11/24 20:09:01 ghostgum Exp $
17 % Font initialization and management code.
18
19 % Define the default font.
20 /defaultfontname /Courier def
21
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
27
28 /defaultfontmap_content 50 dict
29 1 dict begin
30 /; { 2 index 3 1 roll .growput } bind def
31 %% Replace 0 (Fontmap)
32 end def
33
34 % ------ End of editable parameters ------ %
35
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
42
43 % If SUBSTFONT is defined, make it the default font.
44 /SUBSTFONT where { pop /defaultfontname /SUBSTFONT load def } if
45
46 % Define a reliable way of accessing FontDirectory in systemdict.
47 /.FontDirectory
48 { /FontDirectory .systemvar
49 } .bind odef
50
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
56
57 % Define a temporary string for local use, since using =string
58 % interferes with some PostScript programs.
59 /.fonttempstring 8192 string def
60
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
65       exch pop exch
66     }
67    loop
68  } bind def
69
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
79 } bind def
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
86       1 index token not
87        { (Fontmap entry for ) print dup =only
88          ( has no associated file or alias name!  Giving up.) = flush
89          {.readFontmap} 0 get 1 .quit
90        } if
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
95        } if
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
103       } {
104                 % This is a real entry.
105                 % Read and pop tokens until a semicolon.
106        { 2 index token not
107           { (Fontmap entry for ) print 1 index =only
108             ( ends prematurely!  Giving up.) = flush
109             {.loadFontmap} 0 get 1 .quit
110           } if
111          dup /; eq { pop 3 index 3 1 roll .growput exit } if
112          pop
113        } loop
114       } ifelse
115     } loop
116 } bind def
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
130          pop pop false exch
131          exit
132        } if
133        dup 4 index exch 2 mul 1 add get 2 index 3 1 roll put
134      } for
135      exch {
136        DEBUG { (\nAdding alias for: ) print 1 index ==only ( as: ) print dup == flush } if
137          cvn exch cvn .definefontmap    % recurse with an alias
138      } {
139        pop pop  % discard the name
140      } ifelse
141    } if
142    .currentglobal 3 1 roll true .setglobal
143    dup type /stringtype eq
144     { dup .gcheck not { dup length string copy } if
145     }
146    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
152     }
153     {           % Make a new entry.
154       mark 4 -1 roll ] readonly .growput
155     }
156    ifelse .setglobal
157  } bind def
158
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.
163   exch dup read {
164     2 copy unread 16#80 eq {
165       dup (xxxxxx) readstring pop pop             % skip .PFB header
166     } if
167     {           % Stack: key file
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
174       dup 3 index eq
175        { xcheck not { dup token exit } if }       % found key
176        { pop }
177       ifelse
178     } loop
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
183 } bind def
184 /.findfontname
185  { /FontName .findfontvalue
186  } bind def
187
188 % If there is no FONTPATH, try to get one from the environment.
189 NOFONTPATH { /FONTPATH () def } if
190 /FONTPATH where
191  { pop }
192  { /FONTPATH (GS_FONTPATH) getenv not { () } if def }
193 ifelse
194 FONTPATH length 0 eq { (%END FONTPATH) .skipeof } if
195 /FONTPATH [ FONTPATH .pathlist ] def
196
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.
203 %
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.
207 %
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 }
212      if pop
213     }
214    for
215  } bind def
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
219                 % such bug already.
220   readonly {
221     (/) search { true } { (\\) search } ifelse { pop pop } { exit } ifelse
222   } loop
223   dup { (.) search { pop pop } { exit } ifelse } loop
224   2 copy eq {
225     pop ()
226   } {
227     exch dup length 2 index length 1 add sub 0 exch getinterval exch
228   } ifelse
229 } bind def
230 /.scanfontdict 1 dict def               % establish a binding
231 /.scanfontbegin
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
235    Fontmap
236     { exch pop
237        { dup type /stringtype eq
238           { .splitfilename pop .fonttempstring copy .lowerstring cvn
239             .scanfontdict exch true put
240           }
241           { pop
242           }
243          ifelse
244        }
245       forall
246     }
247    forall
248    setglobal
249  } bind def
250 /.scanfontskip mark
251                 % Strings are converted to names anyway, so....
252   /afm true
253   /bat true
254   /c true
255   /cmd true
256   /com true
257   /dir true
258   /dll true
259   /doc true
260   /drv true
261   /exe true
262   /fon true
263   /fot true
264   /h true
265   /o true
266   /obj true
267   /pfm true
268   /pss true             % Adobe Multiple Master font instances
269   /txt true
270 .dicttomark def
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>
285                 %       <BASE> <ext>
286       .scanfontskip exch known exch .scanfontdict exch known or
287        { pop
288                 % stack: <fontcount> <scancount> <filecount+1>
289        }
290        { 3 -1 roll 1 add 3 1 roll
291                 % stack: <fontcount> <scancount+1> <filecount+1> <filename>
292          dup (r) { file } .internalstopped
293           { pop pop null ()
294                 % stack: <fontcount> <scancount+1> <filecount+1> <filename>
295                 %       null ()
296           }
297           {
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
302              { pop pop () }
303              { pop }
304             ifelse
305                 % stack: <fontcount> <scancount+1> <filecount+1>
306                 %       <filename> <file> <header>
307           }
308          ifelse
309                 % Check for PFB file header.
310          dup (\200\001????*) .stringmatch
311           { dup length 6 sub 6 exch getinterval }
312          if
313                 % Check for font file headers.
314          false .scanfontheaders
315           { 2 index exch .stringmatch or
316           }
317          forall exch pop
318           {     % stack: <fontcount> <scancount+1> <filecount+1> <filename>
319                 %       <file>
320             dup 0 setfileposition .findfontname
321              { dup Fontmap exch known
322                 { pop pop
323                 }
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
330                 }
331                ifelse
332              }
333              { pop
334              }
335             ifelse
336           }
337                 % .findfontname will have done a closefile in the above case.
338           { dup null eq { pop } { closefile } ifelse pop
339           }
340          ifelse
341        }
342       ifelse
343     }
344    .scan1fontstring filenameforall
345    QUIET
346     { pop pop pop }
347     { ( ) print =only ( files, ) print =only ( scanned, ) print
348       =only ( new fonts.) = flush
349     }
350    ifelse
351    pop
352    setglobal
353  } bind def
354
355 %END FONTPATH
356
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>
368   QUIET not {
369     (Querying operating system for font files...\n)
370     print flush
371   } if
372   .getnativefonts dup
373   {
374     exch
375     {
376       % stack: [ (name) (path) ]
377       % verify the font name ourselves
378       dup 1 get (r) { file } stopped
379       {
380         % skip the entry if we can't open the returned path 
381         pop pop pop
382       }{
383         % we could open the font file
384         .findfontname
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
390       } ifelse
391     } forall
392   } if
393   % record that we've been run
394   true .setnativefontmapbuilt
395 } bind def
396
397 % Create the dictionary that registers the .buildfont procedure
398 % (called by definefont) for each FontType.
399 /buildfontdict 20 dict def
400
401 % Register Type 3 fonts, which are always supported, for definefont.
402 buildfontdict 3 /.buildfont3 cvx put
403
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
408
409 % Define definefont. This is a procedure built on a set of operators
410 % that do all the error checking and key insertion.
411 /.growfontdict
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
415     { dup dup wcheck
416        { .growdict }
417        { .growdictlength dict .copydict }
418       ifelse
419     }
420     { dup wcheck not { dup maxlength dict .copydict } if
421     }
422    ifelse
423  } bind def 
424 /.completefont {
425   {             % Check for disabled platform fonts.
426       NOPLATFONTS
427        {        % Make sure we leave room for FID.
428          .growfontdict dup /ExactSize 0 put
429        }
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 {
436            dup length 65 ge {
437              64 get
438              dup /congruent eq { SymbolEncoding pop } if
439              /a9 eq { DingbatsEncoding pop } if
440            } {
441              pop
442            } ifelse
443          } if
444        }
445       ifelse
446       dup /.OrigFont known not {
447         dup dup /.OrigFont exch .growput
448       } if
449       true exch
450                 % If this is a CIDFont, CIDFontType takes precedence
451                 % over FontType.
452       dup /CIDFontType known {
453         /.buildcidfont where {
454           pop exch not exch     % true => false
455         } if
456       } if
457       exch {
458         dup /FontType get //buildfontdict exch get exec
459       } {
460         .buildcidfont
461       } ifelse
462
463       DISKFONTS {
464         FontFileDirectory 2 index known {
465           dup /FontFile FontFileDirectory 4 index get .growput
466           } if
467       } if
468       systemdict /ProvideUnicode .knownget not { false } if {
469         /FontEmulationProcs /ProcSet findresource
470         /ProvideUnicodeDecoding get exec
471       } if
472       readonly          % stack: name fontdict
473   } stopped { /invalidfont signalerror } if
474 } bind odef
475 /definefont
476  { .completefont
477                 % If the current allocation mode is global, also enter
478                 % the font in LocalFontDirectory.
479    .currentglobal
480     { //systemdict /LocalFontDirectory .knownget
481        { 2 index 2 index .growput }
482       if
483     }
484    if
485    dup .FontDirectory 4 -2 roll .growput
486                 % If the font originated as a resource, register it.
487    currentfile .currentresourcefile eq { dup .registerfont } if
488  } odef
489
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>
513            % HACK:
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.
520            %
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 .
525            %
526       1 index /FontName get defaultfontname ne {
527         2 copy /FontName exch put
528       } if
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
533     }
534     { .completefont pop exch pop
535     }
536    ifelse
537    exch .setglobal
538  } odef         % so findfont will bind it
539
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.
542 /.loadfontfile {
543                 % According to Ed Taft, Adobe interpreters push userdict
544                 % before loading a font, and pop it afterwards.
545   userdict begin
546     cvx exec
547   end
548 } bind def
549 /.setloadingfont {
550    //systemdict /.loadingfont 3 -1 roll .forceput
551 } .bind odef % .forceput must be bound and hidden
552 /.loadfont
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.
556    true .setloadingfont
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
560      exit
561    } loop % this loop is a pattern for execstack_lookup, don't remove !
562    false .setloadingfont
563  } bind def
564
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.
568 /.substitutefaces [
569         % Guess at suitable substitutions for random unknown fonts.
570   [(Book) /NewCenturySchlbk 0]
571   [(Grot) /Helvetica 0]
572   [(Roman) /Times 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]
580   [(Cour) /Courier 0]
581   [(Frut) /Helvetica 0]
582   [(Garamond) /Palatino 0]
583   [(Geneva) /Helvetica 0]
584   [(Helv) /Helvetica 0]
585   [(NewYork) /Bookman 0]
586   [(Pala) /Palatino 0]
587   [(Schlbk) /NewCenturySchlbk 0]
588   [(Swiss) /Helvetica 0]
589   [(Symbol) /Symbol 0]
590   [(Times) /Times 0]
591         % Substitute for Adobe Multiple Master fonts.
592   [(Minion) /Times 0]
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]
613   [(Joanna) /Times 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]
624 ] readonly def
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]
630 ] readonly def
631 /.fontnameproperties {          % <int> <string|name> .fontnameproperties
632                                 %   <int'>
633   .fontnamestring
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
638       } forall exch
639     } {
640       pop pop
641     } ifelse
642   } forall pop
643 } bind def
644 /.substitutefamilies mark
645   /AvantGarde
646     {/AvantGarde-Book /AvantGarde-BookOblique
647      /AvantGarde-Demi /AvantGarde-DemiOblique}
648   /Bookman
649     {/Bookman-Demi /Bookman-DemiItalic /Bookman-Light /Bookman-LightItalic}
650   /Courier
651     {/Courier /Courier-Oblique /Courier-Bold /Courier-BoldOblique}
652   /Helvetica
653     {/Helvetica /Helvetica-Oblique /Helvetica-Bold /Helvetica-BoldOblique
654      /Helvetica-Narrow /Helvetica-Narrow-Oblique
655      /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique}
656   /NewCenturySchlbk
657     {/NewCenturySchlbk-Roman /NewCenturySchlbk-Italic
658      /NewCenturySchlbk-Bold /NewCenturySchlbk-BoldItalic}
659   /Palatino
660     {/Palatino-Roman /Palatino-Italic /Palatino-Bold /Palatino-BoldItalic}
661   /Symbol
662     {/Symbol /Symbol /Symbol /Symbol}
663   /Times
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
671 } bind def
672 /.fontnamestring {              % <fontname> .fontnamestring <string|name>
673   dup type dup /nametype eq {
674     pop .namestring
675   } {
676     /stringtype ne { pop () } if
677   } ifelse
678 } bind def
679 /.substitutefontname {          % <fontname> <properties> .substitutefontname
680                                 %   <altname|null>
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
693   } 3 1 roll
694         % Stack: facelist properties fontname
695         % Look for a face name.
696   .substitutefaces {
697     2 copy 0 get search {
698       pop pop pop
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
703     } {
704       pop pop
705     } ifelse
706   } forall pop
707   1 index length mod get exec
708 } bind def
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
713 } bind def
714
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
719
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.
723
724 /definefont {           % <name> <font> definefont <font>
725   dup /FontMatrix known {
726     //definefont
727   } {
728     2 copy /FontName get findfont //definefont exch pop exch pop
729   } ifelse
730 } bind odef
731
732 /scalefont {            % <font> <scale> scalefont <font>
733   1 index /FontMatrix known {
734     //scalefont
735   } {
736     1 index /FontName get findfont 1 index //scalefont
737     exch pop exch pop
738   } ifelse
739 } bind odef
740
741 /makefont {             % <font> <matrix> makefont <font>
742   1 index /FontMatrix known {
743     //makefont
744   } {
745     1 index /FontName get findfont 1 index //makefont
746     exch pop exch pop
747   } ifelse
748 } bind odef
749
750 /setfont {              % <font> setfont -
751   dup /FontMatrix known {
752     //setfont
753   } {
754     dup /FontName get findfont //setfont pop
755   } ifelse
756 } bind odef
757
758 %END FAKEFONTS
759
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>
771   exch pop exch pop
772 } odef
773 /findfont {
774   .findfont
775 } bind def
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
780     { index 1 index eq
781        { pop QUIET not
782           { (Unable to substitute for font.) = flush
783           } if
784          /findfont cvx /invalidfont signalerror
785        }
786       if
787     }
788    for
789  } bind def
790 % Get a (non-fake) font if present in a FontDirectory.
791 /.fontknownget          % <fontdir> <fontname> .fontknownget <font> true
792                         % <fontdir> <fontname> .fontknownget false
793  { .knownget
794     { FAKEFONTS
795        { dup /FontMatrix known { true } { pop false } ifelse }
796        { true }
797       ifelse
798     }
799     { false
800     }
801    ifelse
802  } bind def
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>
807       /SUBSTFONT where {
808         pop QUIET not {
809           (Substituting for font ) print dup =only
810           (.) = flush
811         } if
812                         % No aliasing.
813                         % This mode is incompatible with high level devices.
814         cleartomark mark defaultfontname
815       } {
816         dup .substitutefont
817         2 copy eq { pop defaultfontname } if
818         .checkalias
819         QUIET not {
820           SHORTERRORS {
821             (%%[) print 1 index =only
822             ( not found, substituting ) print dup =only (]%%)
823           } {
824             (Substituting font ) print dup =only
825             ( for ) print 1 index =only (.)
826           } ifelse = flush
827         } if
828       } ifelse
829   .dofindfont
830 } bind def
831
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
836
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
843   } for dup null ne {
844     dup 0 eq { .scanfontbegin } if
845     FONTPATH 1 index get .scanfontdir
846     FONTPATH exch null put true
847   } {
848     pop false
849   } ifelse
850 } bind def
851 % Do the work of findfont, including substitution, defaulting, and
852 % scanning of FONTPATH.
853 /.dofindfont {   %  mark <fontname> .dofindfont % mark <alias> ... <font>
854   .tryfindfont not {
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.
858     .scannextfontdir {
859                         % Start over with an empty alias list.
860       counttomark 1 sub { pop } repeat    % mark <fontname> 
861       .dofindfont
862     } {
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
868       } {
869                         % No luck.  Make sure we're not already
870                         % looking for the default font.
871         QUIET not {
872           (Didn't find this font on the system!\n)
873           print
874         } if
875         dup defaultfontname eq {
876           QUIET not {
877             (Unable to load default font ) print
878             dup =only (!  Giving up.) = flush
879           } if
880           /findfont cvx /invalidfont signalerror
881         } if
882                         % Substitute for the font.  Don't alias.
883                         % Same stack as at the beginning of .dofindfont.
884         $error /SubstituteFont get exec
885                          %
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.
892         .stdsubstfont
893       } ifelse
894     } ifelse
895   } if
896 } bind def
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
901     {                   % Already loaded
902       exch pop true
903     }
904     { dup Fontmap exch .knownget not
905        {                % Unknown font name.  Look for a file with the
906                         % same name as the requested font.
907          .tryloadfont
908        }
909        {                % Try each element of the Fontmap in turn.
910          false exch     % (in case we exhaust the list)
911                         % Stack: fontname false fontmaplist
912          { exch pop
913            dup type /nametype eq
914             {                   % Font alias
915               .checkalias .tryfindfont exit
916             }
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 }
923                  if
924                }
925                {                % Font file name
926                  .loadfontloop { true exit } if
927                }
928               ifelse
929             }
930            ifelse false
931          }
932          forall
933                         % Stack: font true -or- fontname false
934          { true
935          }
936          {                      % None of the Fontmap entries worked.
937                                 % Try loading a file with the same name
938                                 % as the requested font.
939            .tryloadfont
940          }
941         ifelse
942        }
943       ifelse
944     }
945    ifelse
946  } bind def
947
948 % any user of .putgstringcopy must use bind and executeonly
949 /.putgstringcopy  %   <dict> <name> <string> .putgstringcopy -
950 { 2 index gcheck currentglobal
951   2 copy eq {
952     pop pop .forceput
953   } {
954     5 1 roll setglobal
955     dup length string copy
956     .forceput setglobal
957   } ifelse
958 } .bind odef % must be bound and hidden for .forceput
959
960
961 % Attempt to load a font from a file.
962 /.tryloadfont {         % <fontname> .tryloadfont <font> true
963                         % <fontname> .tryloadfont false
964   dup .nametostring
965                 % Hack: check for the presence of the resource machinery.
966   /.genericrfn where {
967     pop
968     pop dup .fonttempstring /FontResourceDir getsystemparam .genericrfn
969     .loadfontloop {
970       //true
971     } {
972       dup .nametostring .loadfontloop
973     } ifelse
974   } {
975     .loadfontloop
976   } ifelse
977 } bind def
978 /.loadfontloop {        % <fontname> <filename> .loadfontloop
979                         %   <font> true
980                         % -or-
981                         %   <fontname> false
982                         % See above regarding the use of 'loop'.
983     {                   % Is the font name a string?
984         dup type /stringtype ne
985          { QUIET not
986             { (Can't find font with non-string name: ) print dup =only (.) = flush
987             }
988            if pop false exit
989          }
990         if
991                         % Can we open the file?
992         findlibfile not
993          { QUIET not
994             { (Can't find \(or can't open\) font file ) print dup print
995               (.) = flush
996             }
997            if pop false exit
998          }
999         if
1000
1001                         % Stack: fontname fontfilename fontfile
1002         DISKFONTS
1003          { .currentglobal true .setglobal
1004            2 index (r) file
1005            FontFileDirectory exch 5 index exch .growput
1006            .setglobal
1007          }
1008         if
1009         QUIET not
1010          { (Loading ) print 2 index =only
1011            ( font from ) print 1 index print (... ) print flush
1012          }
1013         if
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
1017         % DPS or CPSI) do.
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
1029          }
1030          { .loadfont .FontDirectory
1031          }
1032         ifelse
1033                 % Stack: fontname fontfilename fontdirectory
1034         QUIET not
1035          { //systemdict /level2dict known
1036             { .currentglobal false .setglobal vmstatus
1037               true .setglobal vmstatus 3 -1 roll pop
1038               6 -1 roll .setglobal 5
1039             }
1040             { vmstatus 3
1041             }
1042            ifelse { =only ( ) print } repeat
1043            (done.) = flush
1044          } if
1045
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 
1050          } if
1051
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
1062               3 -1 roll pop exch
1063                       % Stack: origfontname fontdict filefontname
1064               QUIET
1065                { pop
1066                }
1067                { (Using ) print =only
1068                  ( font for ) print 1 index =only
1069                  (.) = flush
1070                }
1071               ifelse  % Stack: origfontname fontdict
1072               exch pop true exit
1073                       % Stack: fontdict
1074             }
1075            if pop % Stack: origfontname fontdirectory path
1076          }
1077         if pop pop  % Stack: origfontname
1078
1079                 % The font definitely did not load correctly.
1080         QUIET not
1081          { (Loading ) print dup =only
1082            ( font failed.) = flush
1083          } if
1084         false exit
1085
1086     } loop              % end of loop
1087
1088  } bind executeonly def % must be bound and hidden for .putgstringcopy
1089
1090 currentdict /.putgstringcopy .undef
1091
1092 % Define a procedure to load all known fonts.
1093 % This isn't likely to be very useful.
1094 /loadallfonts
1095  { Fontmap { pop findfont pop } forall
1096  } bind def
1097
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.)
1102 %
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.
1106 /.definefakefonts
1107     {
1108     }
1109     {
1110       (gs_fonts FAKEFONTS) VMDEBUG
1111       Fontmap {
1112         pop dup type /stringtype eq { cvn } if
1113         .FontDirectory 1 index known not {
1114           2 dict dup /FontName 3 index put
1115           dup /FontType 1 put
1116           .FontDirectory 3 1 roll put
1117         } {
1118           pop
1119         } ifelse
1120       } forall
1121     }
1122 FAKEFONTS { exch } if pop def   % don't bind, .current/setglobal get redefined
1123
1124 % Install initial fonts from Fontmap.
1125 /.loadinitialfonts
1126  { NOFONTMAP not
1127     { /FONTMAP where
1128           { pop [ FONTMAP .pathlist ]
1129              { dup VMDEBUG findlibfile
1130                 { exch pop .loadFontmap }
1131                 { /undefinedfilename signalerror }
1132                ifelse
1133              }
1134           }
1135           { LIBPATH
1136              { defaultfontmap
1137                false .file_name_combine
1138                {
1139                  dup VMDEBUG
1140                  (r) { file } .internalstopped {
1141                    pop pop
1142                    defaultfontmap_content { .definefontmap } forall
1143                  } {
1144                    .loadFontmap
1145                  } ifelse
1146                } {
1147                  pop pop
1148                } ifelse
1149              }
1150           }
1151          ifelse forall
1152     }
1153    if
1154    userdict /defaultfontmap_content .undef
1155    .definefakefonts     % current VM is global
1156  } def                  % don't bind, .current/setglobal get redefined
1157
1158 % ---------------- Synthetic font support ---------------- %
1159
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.
1167
1168 % Note that this procedure does not perform a definefont.
1169
1170 /.makemodifiedfont      % <fontdict> <paramdict> .makemodifiedfont <fontdict'>
1171  { exch
1172     {                   % Stack: destdict key value
1173       1 index /FID ne
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
1182                    { pop pop }
1183                    { 2 index 3 1 roll put }
1184                   ifelse
1185                 }
1186                forall pop
1187              }
1188             if
1189           }
1190           {             % No override, copy the fontdict entry.
1191             2 index 3 1 roll put
1192             dup dup     % to match pop pop below
1193           }
1194          ifelse
1195        }
1196       if
1197       pop pop
1198     } forall
1199  } bind def
1200
1201 % Make a modified font and define it.  Note that unlike definefont,
1202 % this does not leave the font on the operand stack.
1203
1204 /.definemodifiedfont    % <fontdict> <paramdict> .definemodifiedfont -
1205  { .makemodifiedfont
1206    dup /FontName get exch definefont pop
1207  } bind def