gs_fonts.ps 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. % Copyright (C) 1990, 2000 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This file is part of AFPL Ghostscript.
  4. %
  5. % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  6. % distributor accepts any responsibility for the consequences of using it, or
  7. % for whether it serves any particular purpose or works at all, unless he or
  8. % she says so in writing. Refer to the Aladdin Free Public License (the
  9. % "License") for full details.
  10. %
  11. % Every copy of AFPL Ghostscript must include a copy of the License, normally
  12. % in a plain ASCII text file named PUBLIC. The License grants you the right
  13. % to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14. % conditions described in the License. Among other things, the License
  15. % requires that the copyright notice and this notice be preserved on all
  16. % copies.
  17. % $Id: gs_fonts.ps,v 1.13.2.1 2002/01/25 06:33:09 rayjj Exp $
  18. % Font initialization and management code.
  19. % Define the default font.
  20. /defaultfontname /Courier def
  21. % Define the name of the font map file.
  22. /defaultfontmap (Fontmap) def
  23. % ------ End of editable parameters ------ %
  24. % Define the UniqueIDs and organization XUID assigned to Aladdin.
  25. % UniqueIDs 5,066,501 - 5,066,580 are assigned as follows:
  26. % 01 and 02 for shareware Cyrillic
  27. % 33 through 67 for Type 1 versions of the Hershey fonts
  28. % UniqueIDs 5,115,501 - 5,115,600 are currently unassigned.
  29. /AladdinEnterprisesXUID 107 def
  30. % If SUBSTFONT is defined, make it the default font.
  31. /SUBSTFONT where { pop /defaultfontname /SUBSTFONT load def } if
  32. % Define a reliable way of accessing FontDirectory in systemdict.
  33. /.FontDirectory
  34. { /FontDirectory .systemvar
  35. } .bind odef
  36. % If DISKFONTS is true, we load individual CharStrings as they are needed.
  37. % (This is intended primarily for machines with very small memories.)
  38. % In this case, we define another dictionary, parallel to FontDirectory,
  39. % that retains an open file for every font loaded.
  40. /FontFileDirectory 10 dict def
  41. % Define a temporary string for local use, since using =string
  42. % interferes with some PostScript programs.
  43. /.fonttempstring 128 string def
  44. % Split up a search path into individual directories or files.
  45. /.pathlist % <path> .pathlist <dir1|file1> ...
  46. { { dup length 0 eq { pop exit } if
  47. .filenamelistseparator search not { exit } if
  48. exch pop exch
  49. }
  50. loop
  51. } bind def
  52. % Load a font name -> font file name map.
  53. userdict /Fontmap .FontDirectory maxlength dict put
  54. /.loadFontmap { % <file> .loadFontmap -
  55. % We would like to simply execute .definefontmap as we read,
  56. % but we have to maintain backward compatibility with an older
  57. % specification that makes later entries override earlier
  58. % ones within the same file.
  59. 50 dict exch .readFontmap
  60. { .definefontmap } forall
  61. } bind def
  62. /.readFontmap { % <dict> <file> .readFontmap <dict>
  63. { dup token not { closefile exit } if
  64. % stack: dict file fontname
  65. % This is a hack to get around the absurd habit of MS-DOS editors
  66. % of adding an EOF character at the end of the file.
  67. dup (\032) eq { pop closefile exit } if
  68. 1 index token not
  69. { (Fontmap entry for ) print dup =only
  70. ( has no associated file or alias name! Giving up.) = flush
  71. {.readFontmap} 0 get 1 .quit
  72. } if
  73. dup type dup /stringtype eq exch /nametype eq or not
  74. { (Fontmap entry for ) print 1 index =only
  75. ( has an invalid file or alias name! Giving up.) = flush
  76. {.readFontmap} 0 get 1 .quit
  77. } if
  78. % stack: dict file fontname filename|aliasname
  79. 1 index type /stringtype eq
  80. 1 index type /nametype eq and 1 index xcheck and
  81. 1 index /run eq 2 index /.runlibfile eq or and {
  82. % This is an inclusion entry.
  83. pop findlibfile { exch pop } { file } ifelse
  84. 2 index exch .readFontmap pop
  85. } {
  86. % This is a real entry.
  87. % Read and pop tokens until a semicolon.
  88. { 2 index token not
  89. { (Fontmap entry for ) print 1 index =only
  90. ( ends prematurely! Giving up.) = flush
  91. {.loadFontmap} 0 get 1 .quit
  92. } if
  93. dup /; eq { pop 3 index 3 1 roll .growput exit } if
  94. pop
  95. } loop
  96. } ifelse
  97. } loop
  98. } bind def
  99. % Add an entry in Fontmap. We redefine this if the Level 2
  100. % resource machinery is loaded.
  101. /.definefontmap % <fontname> <file|alias> .definefontmap -
  102. { % Since Fontmap is global, make sure the values are storable.
  103. .currentglobal 3 1 roll true .setglobal
  104. dup type /stringtype eq
  105. { dup .gcheck not { dup length string copy } if
  106. }
  107. if
  108. Fontmap 3 -1 roll 2 copy .knownget
  109. { % Add an element to the end of the existing value,
  110. % unless it's the same as the current last element.
  111. mark exch aload pop counttomark 4 add -1 roll
  112. 2 copy eq { cleartomark pop pop } { ] readonly .growput } ifelse
  113. }
  114. { % Make a new entry.
  115. mark 4 -1 roll ] readonly .growput
  116. }
  117. ifelse .setglobal
  118. } bind def
  119. % Parse a font file just enough to find the FontName or FontType.
  120. /.findfontvalue { % <file> <key> .findfontvalue <value> true
  121. % <file> <key> .findfontvalue false
  122. % Closes the file in either case.
  123. exch dup read not { -1 } if
  124. 2 copy unread 16#80 eq {
  125. dup (xxxxxx) readstring pop pop % skip .PFB header
  126. } if
  127. { % Stack: key file
  128. % Protect ourselves against syntax errors here.
  129. dup { token } stopped { pop false exit } if
  130. not { false exit } if % end of file
  131. dup /eexec eq { pop false exit } if % reached eexec section
  132. dup /Subrs eq { pop false exit } if % Subrs without eexec
  133. dup /CharStrings eq { pop false exit } if % CharStrings without eexec
  134. dup 3 index eq
  135. { xcheck not { dup token exit } if } % found key
  136. { pop }
  137. ifelse
  138. } loop
  139. % Stack: key file value true (or)
  140. % Stack: key file false
  141. dup { 4 } { 3 } ifelse -2 roll closefile pop
  142. } bind def
  143. /.findfontname
  144. { /FontName .findfontvalue
  145. } bind def
  146. % If there is no FONTPATH, try to get one from the environment.
  147. NOFONTPATH { /FONTPATH () def } if
  148. /FONTPATH where
  149. { pop }
  150. { /FONTPATH (GS_FONTPATH) getenv not { () } if def }
  151. ifelse
  152. FONTPATH length 0 eq { (%END FONTPATH) .skipeof } if
  153. /FONTPATH [ FONTPATH .pathlist ] def
  154. % Scan directories looking for plausible fonts. "Plausible" means that
  155. % the file begins with %!PS-AdobeFont or %!FontType1, or with \200\001
  156. % followed by four arbitrary bytes and then either of these strings.
  157. % To speed up the search, we skip any file whose name appears in
  158. % the Fontmap (with any extension and upper/lower case variation) already,
  159. % and any file whose extension definitely indicates it is not a font.
  160. %
  161. % NOTE: The current implementation of this procedure is somewhat Unix/DOS-
  162. % specific. It assumes that '/' and '\' are directory separators, and that
  163. % the part of a file name following the last '.' is the extension.
  164. %
  165. /.lowerstring % <string> .lowerstring <lowerstring>
  166. { 0 1 2 index length 1 sub
  167. { 2 copy get dup 65 ge exch 90 le and
  168. { 2 copy 2 copy get 32 add put }
  169. if pop
  170. }
  171. for
  172. } bind def
  173. /.splitfilename { % <dir.../base.extn> .basename <base> <extn>
  174. % Make the file name read-only to detect aliasing bugs.
  175. % We really don't like doing this, but we've had one
  176. % such bug already.
  177. readonly {
  178. (/) search { true } { (\\) search } ifelse { pop pop } { exit } ifelse
  179. } loop
  180. dup { (.) search { pop pop } { exit } ifelse } loop
  181. 2 copy eq {
  182. pop ()
  183. } {
  184. exch dup length 2 index length 1 add sub 0 exch getinterval exch
  185. } ifelse
  186. } bind def
  187. /.scanfontdict 1 dict def % establish a binding
  188. /.scanfontbegin
  189. { % Construct the table of all file names already in Fontmap.
  190. currentglobal true setglobal
  191. .scanfontdict dup maxlength Fontmap length 2 add .max .setmaxlength
  192. Fontmap
  193. { exch pop
  194. { dup type /stringtype eq
  195. { .splitfilename pop .fonttempstring copy .lowerstring cvn
  196. .scanfontdict exch true put
  197. }
  198. { pop
  199. }
  200. ifelse
  201. }
  202. forall
  203. }
  204. forall
  205. setglobal
  206. } bind def
  207. /.scanfontskip mark
  208. % Strings are converted to names anyway, so....
  209. /afm true
  210. /bat true
  211. /c true
  212. /cmd true
  213. /com true
  214. /dir true
  215. /dll true
  216. /doc true
  217. /drv true
  218. /exe true
  219. /fon true
  220. /fot true
  221. /h true
  222. /o true
  223. /obj true
  224. /pfm true
  225. /pss true % Adobe Multiple Master font instances
  226. /txt true
  227. .dicttomark def
  228. /.scan1fontstring 128 string def
  229. % %%BeginFont: is not per Adobe documentation, but a few fonts have it.
  230. /.scanfontheaders [(%!PS-Adobe*) (%!FontType*) (%%BeginFont:*)] def
  231. 0 .scanfontheaders { length max } forall 6 add % extra for PFB header
  232. /.scan1fontfirst exch string def
  233. /.scanfontdir % <dirname> .scanfontdir -
  234. { currentglobal exch true setglobal
  235. QUIET not { (Scanning ) print dup print ( for fonts...) print flush } if
  236. (*) 1 index .filenamedirseparator
  237. dup (\\) eq { pop (\\\\) } if % double \ for pattern match
  238. exch concatstrings concatstrings
  239. 0 0 0 4 -1 roll % found scanned files
  240. { % stack: <fontcount> <scancount> <filecount> <filename>
  241. exch 1 add exch % increment filecount
  242. dup .splitfilename .fonttempstring copy .lowerstring
  243. % stack: <fontcount> <scancount> <filecount+1> <filename>
  244. % <BASE> <ext>
  245. .scanfontskip exch known exch .scanfontdict exch known or
  246. { pop
  247. % stack: <fontcount> <scancount> <filecount+1>
  248. }
  249. { 3 -1 roll 1 add 3 1 roll
  250. % stack: <fontcount> <scancount+1> <filecount+1> <filename>
  251. dup (r) { file } .internalstopped
  252. { pop pop null ()
  253. % stack: <fontcount> <scancount+1> <filecount+1> <filename>
  254. % null ()
  255. }
  256. {
  257. % On some platforms, the file operator will open directories,
  258. % but an error will occur if we try to read from one.
  259. % Handle this possibility here.
  260. dup .scan1fontfirst { readstring } .internalstopped
  261. { pop pop () }
  262. { pop }
  263. ifelse
  264. % stack: <fontcount> <scancount+1> <filecount+1>
  265. % <filename> <file> <header>
  266. }
  267. ifelse
  268. % Check for PFB file header.
  269. dup (\200\001????*) .stringmatch
  270. { dup length 6 sub 6 exch getinterval }
  271. if
  272. % Check for font file headers.
  273. false .scanfontheaders
  274. { 2 index exch .stringmatch or
  275. }
  276. forall exch pop
  277. { % stack: <fontcount> <scancount+1> <filecount+1> <filename>
  278. % <file>
  279. dup 0 setfileposition .findfontname
  280. { dup Fontmap exch known
  281. { pop pop
  282. }
  283. { exch copystring exch
  284. DEBUG { ( ) print dup =only flush } if
  285. 1 index .definefontmap
  286. .splitfilename pop true .scanfontdict 3 1 roll .growput
  287. % Increment fontcount.
  288. 3 -1 roll 1 add 3 1 roll
  289. }
  290. ifelse
  291. }
  292. { pop
  293. }
  294. ifelse
  295. }
  296. % .findfontname will have done a closefile in the above case.
  297. { dup null eq { pop } { closefile } ifelse pop
  298. }
  299. ifelse
  300. }
  301. ifelse
  302. }
  303. .scan1fontstring filenameforall
  304. QUIET
  305. { pop pop pop }
  306. { ( ) print =only ( files, ) print =only ( scanned, ) print
  307. =only ( new fonts.) = flush
  308. }
  309. ifelse
  310. setglobal
  311. } bind def
  312. %END FONTPATH
  313. % Create the dictionary that registers the .buildfont procedure (called by
  314. % definefont) for each FontType.
  315. /buildfontdict 20 dict def
  316. % Register Type 3 fonts, which are always supported, for definefont.
  317. buildfontdict 3 /.buildfont3 cvx put
  318. % Register Type 0 fonts if they are supported. Strictly speaking,
  319. % we should do this in its own file (gs_type0.ps), but since this is
  320. % the only thing that would be in that file, it's simpler to put it here.
  321. /.buildfont0 where { pop buildfontdict 0 /.buildfont0 cvx put } if
  322. % Define definefont. This is a procedure built on a set of operators
  323. % that do all the error checking and key insertion.
  324. /.growfontdict
  325. { % Grow the font dictionary, if necessary, to ensure room for an
  326. % added entry, making sure there is at least one slot left for FID.
  327. dup maxlength 1 index length sub 2 lt
  328. { dup dup wcheck
  329. { .growdict }
  330. { .growdictlength dict .copydict }
  331. ifelse
  332. }
  333. { dup wcheck not { dup maxlength dict .copydict } if
  334. }
  335. ifelse
  336. } bind def
  337. /.completefont {
  338. { % Check for disabled platform fonts.
  339. NOPLATFONTS
  340. { % Make sure we leave room for FID.
  341. .growfontdict dup /ExactSize 0 put
  342. }
  343. { % Hack: if the Encoding looks like it might be the
  344. % Symbol or Dingbats encoding, load those now (for the
  345. % benefit of platform font matching) just in case
  346. % the font didn't actually reference them.
  347. % Note that some types of font don't have an Encoding.
  348. dup /Encoding .knownget {
  349. dup length 65 ge {
  350. 64 get
  351. dup /congruent eq { SymbolEncoding pop } if
  352. /a9 eq { DingbatsEncoding pop } if
  353. } {
  354. pop
  355. } ifelse
  356. } if
  357. }
  358. ifelse
  359. true exch
  360. % If this is a CIDFont, CIDFontType takes precedence
  361. % over FontType.
  362. dup /CIDFontType known {
  363. /.buildcidfont where {
  364. pop exch not exch % true => false
  365. } if
  366. } if
  367. exch {
  368. dup /FontType get //buildfontdict exch get exec
  369. } {
  370. .buildcidfont
  371. } ifelse
  372. DISKFONTS
  373. { FontFileDirectory 2 index known
  374. { dup /FontFile FontFileDirectory 4 index get .growput
  375. }
  376. if
  377. }
  378. if
  379. readonly % stack: name fontdict
  380. } stopped { /invalidfont signalerror } if
  381. } bind odef
  382. /definefont
  383. { .completefont
  384. % If the current allocation mode is global, also enter
  385. % the font in LocalFontDirectory.
  386. .currentglobal
  387. { //systemdict /LocalFontDirectory .knownget
  388. { 2 index 2 index .growput }
  389. if
  390. }
  391. if
  392. dup .FontDirectory 4 -2 roll .growput
  393. % If the font originated as a resource, register it.
  394. currentfile .currentresourcefile eq { dup .registerfont } if
  395. } odef
  396. % Define a procedure for defining aliased fonts.
  397. % We use this only for explicitly aliased fonts, not substituted fonts:
  398. % we think this matches the observed behavior of Adobe interpreters.
  399. /.aliasfont % <name> <font> .aliasfont <newFont>
  400. { .currentglobal 3 1 roll dup .gcheck .setglobal
  401. dup length 2 add dict
  402. dup 3 -1 roll { 1 index /FID eq { pop pop } { put dup } ifelse } forall
  403. % Stack: global fontname newfont newfont.
  404. % We might be defining a global font whose FontName
  405. % is a local string. This is weird, but legal,
  406. % and doesn't cause problems anywhere else:
  407. % to avoid any possible problems in this case, do a cvn.
  408. % We might also be defining (as an alias) a global font
  409. % whose FontName is a local non-string, if someone passed a
  410. % garbage value to findfont. In this case, just don't
  411. % call definefont at all.
  412. 2 index dup type /stringtype eq exch .gcheck or 1 index .gcheck not or
  413. { /FontName 3 index dup type /stringtype eq { cvn } if put
  414. % Don't bind in definefont, since Level 2 redefines it.
  415. /definefont .systemvar exec
  416. }
  417. { .completefont pop exch pop
  418. }
  419. ifelse exch .setglobal
  420. } odef % so findfont will bind it
  421. % Define .loadfontfile for loading a font. If we recognize Type 1 and/or
  422. % TrueType fonts, gs_type1.ps and/or gs_ttf.ps will redefine this.
  423. /.loadfontfile {
  424. % According to Ed Taft, Adobe interpreters push userdict
  425. % before loading a font, and pop it afterwards.
  426. userdict begin
  427. cvx exec
  428. end
  429. } bind def
  430. /.loadfont
  431. { % Some buggy fonts leave extra junk on the stack,
  432. % so we have to make a closure that records the stack depth
  433. % in a fail-safe way.
  434. {{.loadfontfile} .execasresource} count 1 sub 2 .execn
  435. count exch sub { pop } repeat
  436. } bind def
  437. % Find an alternate font to substitute for an unknown one.
  438. % We go to some trouble to parse the font name and extract
  439. % properties from it. Later entries take priority over earlier.
  440. /.substitutefaces [
  441. % Guess at suitable substitutions for random unknown fonts.
  442. [(Book) /NewCenturySchlbk 0]
  443. [(Grot) /Helvetica 0]
  444. [(Roman) /Times 0]
  445. [(Chancery) /ZapfChancery-MediumItalic 0]
  446. % If the family name appears in the font name,
  447. % use a font from that family.
  448. [(Arial) /Helvetica 0]
  449. [(Avant) /AvantGarde 0]
  450. [(Bookman) /Bookman 0]
  451. [(Century) /NewCenturySchlbk 0]
  452. [(Cour) /Courier 0]
  453. [(Frut) /Helvetica 0]
  454. [(Garamond) /Palatino 0]
  455. [(Geneva) /Helvetica 0]
  456. [(Helv) /Helvetica 0]
  457. [(NewYork) /Bookman 0]
  458. [(Pala) /Palatino 0]
  459. [(Schlbk) /NewCenturySchlbk 0]
  460. [(Swiss) /Helvetica 0]
  461. [(Symbol) /Symbol 0]
  462. [(Times) /Times 0]
  463. % Substitute for Adobe Multiple Master fonts.
  464. [(Minion) /Times 0]
  465. [(Myriad) /Helvetica 0]
  466. % If the font wants to be monospace, use Courier.
  467. [(Monospace) /Courier 0]
  468. [(Typewriter) /Courier 0]
  469. % Define substitutes for the other Adobe PostScript 3 fonts.
  470. % For some of them, the substitution is pretty bad!
  471. [(Albertus) /Palatino 0]
  472. [(AntiqueOlive) /Helvetica 0]
  473. [(Bodoni) /NewCenturySchlbk 0]
  474. [(Chicago) /Helvetica 2]
  475. [(Clarendon) /Bookman 0]
  476. [(Cooper) /NewCenturySchlbk 0]
  477. [(Copperplate) /AvantGarde 0] % inappropriate, small-cap font
  478. [(Coronet) /ZapfChancery-MediumItalic 0]
  479. [(Eurostile) /Helvetica 0]
  480. [(Geneva) /Courier 2] % should be fixed-pitch sans demi
  481. [(GillSans) /Helvetica 2]
  482. [(GillSans-Light) /Helvetica 0]
  483. [(Goudy) /Palatino 0]
  484. [(Hoefler) /NewCenturySchlbk 0]
  485. [(Joanna) /Times 0]
  486. [(LetterGothic) /Courier 0] % should be fixed-pitch sans
  487. [(LubalinGraph-Book) /Bookman 2]
  488. [(LubalinGraph-Demi) /Bookman 0]
  489. [(Marigold) /ZapfChancery-MediumItalic 0]
  490. [(MonaLisa-Recut) /Palatino 0] % inappropriate
  491. [(Monaco) /Courier 2] % should be fixed-pitch sans demi
  492. [(Optima) /Helvetica 0]
  493. [(Oxford) /ZapfChancery-MediumItalic 0]
  494. [(Tekton) /Helvetica 0]
  495. [(Univers) /Helvetica 0]
  496. ] readonly def
  497. /.substituteproperties [
  498. [(It) 9] [(Oblique) 1]
  499. [(Black) 2] [(Bd) 2] [(Bold) 2] [(bold) 2] [(Demi) 2] [(Heavy) 2] [(Sb) 2]
  500. [(Cn) 4] [(Cond) 4] [(Narrow) 4] [(Pkg) 4] [(Compr) 4]
  501. [(Serif) 8] [(Sans) -8]
  502. ] readonly def
  503. /.fontnameproperties { % <int> <string|name> .fontnameproperties
  504. % <int'>
  505. .fontnamestring
  506. .substituteproperties {
  507. 2 copy 0 get search {
  508. pop pop pop dup length 1 sub 1 exch getinterval 3 -1 roll exch {
  509. dup 0 ge { or } { neg not and } ifelse
  510. } forall exch
  511. } {
  512. pop pop
  513. } ifelse
  514. } forall pop
  515. } bind def
  516. /.substitutefamilies mark
  517. /AvantGarde
  518. {/AvantGarde-Book /AvantGarde-BookOblique
  519. /AvantGarde-Demi /AvantGarde-DemiOblique}
  520. /Bookman
  521. {/Bookman-Demi /Bookman-DemiItalic /Bookman-Light /Bookman-LightItalic}
  522. /Courier
  523. {/Courier /Courier-Oblique /Courier-Bold /Courier-BoldOblique}
  524. /Helvetica
  525. {/Helvetica /Helvetica-Oblique /Helvetica-Bold /Helvetica-BoldOblique
  526. /Helvetica-Narrow /Helvetica-Narrow-Oblique
  527. /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique}
  528. /NewCenturySchlbk
  529. {/NewCenturySchlbk-Roman /NewCenturySchlbk-Italic
  530. /NewCenturySchlbk-Bold /NewCenturySchlbk-BoldItalic}
  531. /Palatino
  532. {/Palatino-Roman /Palatino-Italic /Palatino-Bold /Palatino-BoldItalic}
  533. /Symbol
  534. {/Symbol /Symbol /Symbol /Symbol}
  535. /Times
  536. {/Times-Roman /Times-Italic /Times-Bold /Times-BoldItalic}
  537. /ZapfChancery-MediumItalic
  538. {/ZapfChancery-MediumItalic}
  539. .dicttomark readonly def
  540. /.nametostring { % <name> .nametostring <string>
  541. % <other> .nametostring <other>
  542. dup type /nametype eq { .namestring } if
  543. } bind def
  544. /.fontnamestring { % <fontname> .fontnamestring <string|name>
  545. dup type dup /nametype eq {
  546. pop .namestring
  547. } {
  548. /stringtype ne { pop () } if
  549. } ifelse
  550. } bind def
  551. /.substitutefontname { % <fontname> <properties> .substitutefontname
  552. % <altname|null>
  553. % Look for properties and/or a face name in the font name.
  554. % If we find any, use Times (serif) or Helvetica (sans) as the
  555. % base font; otherwise, use the default font.
  556. % Note that the "substituted" font name may be the same as
  557. % the requested one; the caller must check this.
  558. exch .fontnamestring {
  559. defaultfontname /Helvetica-Oblique /Helvetica-Bold /Helvetica-BoldOblique
  560. /Helvetica-Narrow /Helvetica-Narrow-Oblique
  561. /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique
  562. /Times-Roman /Times-Italic /Times-Bold /Times-BoldItalic
  563. /Helvetica-Narrow /Helvetica-Narrow-Oblique
  564. /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique
  565. } 3 1 roll
  566. % Stack: facelist properties fontname
  567. % Look for a face name.
  568. .substitutefaces {
  569. 2 copy 0 get search {
  570. pop pop pop
  571. % Stack: facelist properties fontname [(pattern) family properties]
  572. dup 2 get 4 -1 roll or 3 1 roll
  573. 1 get .substitutefamilies exch get
  574. 4 -1 roll pop 3 1 roll
  575. } {
  576. pop pop
  577. } ifelse
  578. } forall pop
  579. 1 index length mod get exec
  580. } bind def
  581. /.substitutefont { % <fontname> .substitutefont <altname>
  582. dup 0 exch .fontnameproperties .substitutefontname
  583. % Only accept fonts known in the Fontmap.
  584. Fontmap 1 index known not { pop defaultfontname } if
  585. } bind def
  586. % If requested, make (and recognize) fake entries in FontDirectory for fonts
  587. % present in Fontmap but not actually loaded. Thanks to Ray Johnston for
  588. % the idea behind this code.
  589. FAKEFONTS not { (%END FAKEFONTS) .skipeof } if
  590. % We use the presence or absence of the FontMatrix key to indicate whether
  591. % a font is real or fake. We must pop the arguments at the very end,
  592. % so that stack protection will be effective.
  593. /definefont { % <name> <font> definefont <font>
  594. dup /FontMatrix known {
  595. //definefont
  596. } {
  597. 2 copy /FontName get findfont //definefont exch pop exch pop
  598. } ifelse
  599. } bind odef
  600. /scalefont { % <font> <scale> scalefont <font>
  601. 1 index /FontMatrix known {
  602. //scalefont
  603. } {
  604. 1 index /FontName get findfont 1 index //scalefont
  605. exch pop exch pop
  606. } ifelse
  607. } bind odef
  608. /makefont { % <font> <matrix> makefont <font>
  609. 1 index /FontMatrix known {
  610. //makefont
  611. } {
  612. 1 index /FontName get findfont 1 index //makefont
  613. exch pop exch pop
  614. } ifelse
  615. } bind odef
  616. /setfont { % <font> setfont -
  617. dup /FontMatrix known {
  618. //setfont
  619. } {
  620. dup /FontName get findfont //setfont pop
  621. } ifelse
  622. } bind odef
  623. %END FAKEFONTS
  624. % Define findfont so it tries to load a font if it's not found.
  625. % The Red Book requires that findfont be a procedure, not an operator,
  626. % but it still needs to restore the stacks reliably if it fails,
  627. % so we do all the work in an operator.
  628. /.findfont {
  629. mark 1 index
  630. //systemdict begin .dofindfont
  631. % Define any needed aliases.
  632. counttomark 1 sub { .aliasfont } repeat end
  633. exch pop exch pop
  634. } odef
  635. /findfont {
  636. .findfont
  637. } bind def
  638. % Check whether the font name we are about to look for is already on the list
  639. % of aliases we're accumulating; if so, cause an error.
  640. /.checkalias % -mark- <alias1> ... <name> .checkalias <<same>>
  641. { counttomark 1 sub -1 1
  642. { index 1 index eq
  643. { pop QUIET not
  644. { (Unable to substitute for font.) = flush
  645. } if
  646. /findfont cvx /invalidfont signalerror
  647. }
  648. if
  649. }
  650. for
  651. } bind def
  652. % Get a (non-fake) font if present in a FontDirectory.
  653. /.fontknownget % <fontdir> <fontname> .fontknownget <font> true
  654. % <fontdir> <fontname> .fontknownget false
  655. { .knownget
  656. { FAKEFONTS
  657. { dup /FontMatrix known { true } { pop false } ifelse }
  658. { true }
  659. ifelse
  660. }
  661. { false
  662. }
  663. ifelse
  664. } bind def
  665. % This is the standard procedure for handling font substitution.
  666. % Its location is per an Adobe newsgroup posting.
  667. % It is called with the font name on the stack, standing in for findfont.
  668. /.stdsubstfont { % mark <alias1> ... <fontname> .stdsubstfont <font>
  669. /SUBSTFONT where {
  670. pop QUIET not {
  671. (Substituting for font ) print dup =only
  672. (.) = flush
  673. } if
  674. % No aliasing.
  675. cleartomark mark defaultfontname
  676. } {
  677. dup .substitutefont
  678. 2 copy eq { pop defaultfontname } if
  679. .checkalias
  680. QUIET not {
  681. SHORTERRORS {
  682. (%%[) print 1 index =only
  683. ( not found, substituting ) print dup =only (]%%)
  684. } {
  685. (Substituting font ) print dup =only
  686. ( for ) print 1 index =only (.)
  687. } ifelse = flush
  688. } if
  689. % Remove all the accumulated aliases.
  690. counttomark 1 add 1 roll cleartomark mark exch
  691. } ifelse
  692. .dofindfont
  693. } bind def
  694. $error /SubstituteFont { .stdsubstfont } put
  695. % Scan the next directory on FONTPATH.
  696. /.scannextfontdir { % - .scannextfontdir <bool>
  697. % If we haven't scanned all the directories in
  698. % FONTPATH, scan the next one.
  699. null 0 1 FONTPATH length 1 sub {
  700. FONTPATH 1 index get null ne { exch pop exit } if pop
  701. } for dup null ne {
  702. dup 0 eq { .scanfontbegin } if
  703. FONTPATH 1 index get .scanfontdir
  704. FONTPATH exch null put true
  705. } {
  706. pop false
  707. } ifelse
  708. } bind def
  709. % Do the work of findfont, including substitution, defaulting, and
  710. % scanning of FONTPATH.
  711. /.dofindfont { % <fontname> .dofindfont <font>
  712. .tryfindfont not {
  713. % We didn't find the font. If we haven't scanned
  714. % all the directories in FONTPATH, scan the next one
  715. % now and look for the font again.
  716. .scannextfontdir {
  717. % Start over with an empty alias list.
  718. counttomark 1 sub { pop } repeat
  719. .dofindfont
  720. } {
  721. % No luck. Make sure we're not already
  722. % looking for the default font.
  723. dup defaultfontname eq {
  724. QUIET not {
  725. (Unable to load default font ) print
  726. dup =only (! Giving up.) = flush
  727. } if
  728. /findfont cvx /invalidfont signalerror
  729. } if
  730. % Substitute for the font. Don't alias.
  731. $error /SubstituteFont get exec
  732. } ifelse
  733. } if
  734. } bind def
  735. % Try to find a font using only the present contents of Fontmap.
  736. /.tryfindfont { % <fontname> .tryfindfont <font> true
  737. % <fontname> .tryfindfont false
  738. .FontDirectory 1 index .fontknownget
  739. { % Already loaded
  740. exch pop true
  741. }
  742. { dup Fontmap exch .knownget not
  743. { % Unknown font name. Look for a file with the
  744. % same name as the requested font.
  745. .tryloadfont
  746. }
  747. { % Try each element of the Fontmap in turn.
  748. false exch % (in case we exhaust the list)
  749. % Stack: fontname false fontmaplist
  750. { exch pop
  751. dup type /nametype eq
  752. { % Font alias
  753. .checkalias .tryfindfont exit
  754. }
  755. { dup dup type dup /arraytype eq exch /packedarraytype eq or exch xcheck and
  756. { % Font with a procedural definition
  757. exec % The procedure will load the font.
  758. % Check to make sure this really happened.
  759. .FontDirectory 1 index .knownget
  760. { exch pop true exit }
  761. if
  762. }
  763. { % Font file name
  764. .loadfontloop { true exit } if
  765. }
  766. ifelse
  767. }
  768. ifelse false
  769. }
  770. forall
  771. % Stack: font true -or- fontname false
  772. { true
  773. }
  774. { % None of the Fontmap entries worked.
  775. % Try loading a file with the same name
  776. % as the requested font.
  777. .tryloadfont
  778. }
  779. ifelse
  780. }
  781. ifelse
  782. }
  783. ifelse
  784. } bind def
  785. % Attempt to load a font from a file.
  786. /.tryloadfont { % <fontname> .tryloadfont <font> true
  787. % <fontname> .tryloadfont false
  788. dup .nametostring
  789. % Hack: check for the presence of the resource machinery.
  790. /.genericrfn where {
  791. pop
  792. 2 copy .fonttempstring /FontResourceDir getsystemparam .genericrfn
  793. .loadfontloop {
  794. exch pop exch pop true
  795. } {
  796. dup .nametostring .loadfontloop
  797. } ifelse
  798. } {
  799. .loadfontloop
  800. } ifelse
  801. } bind def
  802. /.loadfontloop { % <fontname> <filename> .loadfontloop
  803. % <font> true
  804. % -or-
  805. % <fontname> false
  806. % See above regarding the use of 'loop'.
  807. { % Is the font name a string?
  808. dup type /stringtype ne
  809. { QUIET not
  810. { (Can't find font with non-string name: ) print dup =only (.) = flush
  811. }
  812. if pop false exit
  813. }
  814. if
  815. % Can we open the file?
  816. findlibfile not
  817. { QUIET not
  818. { (Can't find \(or can't open\) font file ) print dup print
  819. (.) = flush
  820. }
  821. if pop false exit
  822. }
  823. if
  824. % Stack: fontname fontfilename fontfile
  825. DISKFONTS
  826. { .currentglobal true .setglobal
  827. 2 index (r) file
  828. FontFileDirectory exch 5 index exch .growput
  829. .setglobal
  830. }
  831. if
  832. QUIET not
  833. { (Loading ) print 2 index =only
  834. ( font from ) print 1 index print (... ) print flush
  835. }
  836. if
  837. % If LOCALFONTS isn't set, load the font into local or global
  838. % VM according to FontType; if LOCALFONTS is set, load the font
  839. % into the current VM, which is what Adobe printers (but not
  840. % DPS or CPSI) do.
  841. LOCALFONTS { false } { /setglobal where } ifelse
  842. { pop /FontType .findfontvalue { 1 eq } { false } ifelse
  843. % .setglobal, like setglobal, aliases FontDirectory to
  844. % GlobalFontDirectory if appropriate. However, we mustn't
  845. % allow the current version of .setglobal to be bound in,
  846. % because it's different depending on language level.
  847. .currentglobal exch /.setglobal .systemvar exec
  848. % Remove the fake definition, if any.
  849. .FontDirectory 3 index .undef
  850. 1 index (r) file .loadfont .FontDirectory exch
  851. /.setglobal .systemvar exec
  852. }
  853. { .loadfont .FontDirectory
  854. }
  855. ifelse
  856. % Stack: fontname fontfilename fontdirectory
  857. QUIET not
  858. { //systemdict /level2dict known
  859. { .currentglobal false .setglobal vmstatus
  860. true .setglobal vmstatus 3 -1 roll pop
  861. 6 -1 roll .setglobal 5
  862. }
  863. { vmstatus 3
  864. }
  865. ifelse { =only ( ) print } repeat
  866. (done.) = flush
  867. } if
  868. % Check to make sure the font was actually loaded.
  869. dup 3 index .fontknownget
  870. { 4 1 roll pop pop pop true exit } if
  871. % Maybe the file had a different FontName.
  872. % See if we can get a FontName from the file, and if so,
  873. % whether a font by that name exists now.
  874. exch (r) file .findfontname
  875. { 2 copy .fontknownget
  876. { % Yes. Stack: origfontname fontdirectory filefontname fontdict
  877. 3 -1 roll pop exch
  878. QUIET
  879. { pop
  880. }
  881. { (Using ) print =only
  882. ( font for ) print 1 index =only
  883. (.) = flush
  884. }
  885. ifelse true exit
  886. }
  887. if pop
  888. }
  889. if pop
  890. % The font definitely did not load correctly.
  891. QUIET not
  892. { (Loading ) print dup =only
  893. ( font failed.) = flush
  894. } if
  895. false exit
  896. } loop % end of loop
  897. } bind def
  898. % Define a procedure to load all known fonts.
  899. % This isn't likely to be very useful.
  900. /loadallfonts
  901. { Fontmap { pop findfont pop } forall
  902. } bind def
  903. % If requested, load all the fonts defined in the Fontmap into FontDirectory
  904. % as "fake" fonts i.e., font dicts with only FontName and FontType defined.
  905. % (We define FontType only for the sake of some questionable code in the
  906. % Apple Printer Utility 2.0 font inquiry code.)
  907. %
  908. % Note that this procedure only creates fake fonts in the FontDirectory
  909. % associated with the current VM. This is because in multi-context systems,
  910. % creating the fake fonts in local VM leads to undesirable complications.
  911. /.definefakefonts
  912. {
  913. }
  914. {
  915. (gs_fonts FAKEFONTS) VMDEBUG
  916. Fontmap {
  917. pop dup type /stringtype eq { cvn } if
  918. .FontDirectory 1 index known not {
  919. 2 dict dup /FontName 3 index put
  920. dup /FontType 1 put
  921. .FontDirectory 3 1 roll put
  922. } {
  923. pop
  924. } ifelse
  925. } forall
  926. }
  927. FAKEFONTS { exch } if pop def % don't bind, .current/setglobal get redefined
  928. % Install initial fonts from Fontmap.
  929. /.loadinitialfonts
  930. { NOFONTMAP not
  931. { /FONTMAP where
  932. { pop [ FONTMAP .pathlist ]
  933. { dup VMDEBUG findlibfile
  934. { exch pop .loadFontmap }
  935. { /undefinedfilename signalerror }
  936. ifelse
  937. }
  938. }
  939. { LIBPATH
  940. { defaultfontmap 1 index .filenamedirseparator
  941. exch concatstrings concatstrings dup VMDEBUG
  942. (r) { file } .internalstopped
  943. { pop pop } { .loadFontmap } ifelse
  944. }
  945. }
  946. ifelse forall
  947. }
  948. if
  949. .definefakefonts % current VM is global
  950. } def % don't bind, .current/setglobal get redefined
  951. % ---------------- Synthetic font support ---------------- %
  952. % Create a new font by modifying an existing one. paramdict contains
  953. % entries with the same keys as the ones found in a Type 1 font;
  954. % it should also contain enough empty entries to allow adding the
  955. % corresponding non-overridden entries from the original font dictionary,
  956. % including FID. If paramdict includes a FontInfo entry, this will
  957. % also override the original font's FontInfo, entry by entry;
  958. % again, it must contain enough empty entries.
  959. % Note that this procedure does not perform a definefont.
  960. /.makemodifiedfont % <fontdict> <paramdict> .makemodifiedfont <fontdict'>
  961. { exch
  962. { % Stack: destdict key value
  963. 1 index /FID ne
  964. { 2 index 2 index known
  965. { % Skip fontdict entry supplied in paramdict, but
  966. % handle FontInfo specially.
  967. 1 index /FontInfo eq
  968. { 2 index 2 index get % new FontInfo
  969. 1 index % old FontInfo
  970. { % Stack: destdict key value destinfo key value
  971. 2 index 2 index known
  972. { pop pop }
  973. { 2 index 3 1 roll put }
  974. ifelse
  975. }
  976. forall pop
  977. }
  978. if
  979. }
  980. { % No override, copy the fontdict entry.
  981. 2 index 3 1 roll put
  982. dup dup % to match pop pop below
  983. }
  984. ifelse
  985. }
  986. if
  987. pop pop
  988. } forall
  989. } bind def
  990. % Make a modified font and define it. Note that unlike definefont,
  991. % this does not leave the font on the operand stack.
  992. /.definemodifiedfont % <fontdict> <paramdict> .definemodifiedfont -
  993. { .makemodifiedfont
  994. dup /FontName get exch definefont pop
  995. } bind def