font2pcl.ps 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. % Copyright (C) 1993, 1994, 1995, 1997 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: font2pcl.ps,v 1.2 2000/09/19 18:29:11 lpd Exp $
  18. % font2pcl.ps
  19. % Write out a font as a PCL bitmap font.
  20. /pcldict 60 dict def
  21. % Write out the current font as a PCL bitmap font.
  22. % The current transformation matrix defines the font size and orientation.
  23. /WriteResolution? false def % true=use "resolution bound font" format,
  24. % false=use older format
  25. /LJ4 false def % true=use LJ4 Typeface code
  26. % false=use LJIIP/IID/IIIx Typeface code
  27. pcldict begin % internal procedures
  28. /findstring % <string> <substring> findstring <bool>
  29. { search { pop pop pop true } { pop false } ifelse
  30. } def
  31. % Determine which set of keywords is present in a string.
  32. % The last keyword set must be empty.
  33. /keysearch % <string> <array of arrays of keywords> keysearch <index>
  34. { 0 1 2 index length 1 sub
  35. { 2 copy get true exch
  36. { % Stack: <string> <a.a.k.> <index> <bool> <keyword>
  37. 4 index exch findstring and
  38. }
  39. forall
  40. { 0 exch getinterval exit
  41. }
  42. if pop
  43. }
  44. for
  45. exch pop length % invalid index if missing
  46. } def
  47. % Determine the device height of a string in quarter-dots.
  48. /charheight % <string> charheight <int>
  49. { gsave newpath 0 0 moveto false charpath
  50. pathbbox exch pop exch sub exch pop 0 exch grestore
  51. dtransform add abs 4 mul cvi
  52. } def
  53. % Compute an integer version of the transformed FontBBox.
  54. /inflate % <num> inflate <num>
  55. { dup 0 gt { ceiling } { floor } ifelse
  56. } def
  57. /ixbbox % - ixbbox <llx> <lly> <urx> <ury>
  58. { /FontBBox load aload pop % might be executable or literal
  59. 4 2 roll transform exch truncate cvi exch truncate cvi
  60. 4 2 roll transform exch inflate cvi exch inflate cvi
  61. } def
  62. % Determine the original font of a possibly transformed font.
  63. % Since some badly behaved PostScript files construct transformed
  64. % fonts "by hand", we can't just rely on the OrigFont pointers.
  65. % Instead, if a font with the given name exists, and if its
  66. % entries for FontType and UniqueID match those of the font we
  67. % obtain by following the OrigFont chain, we use that font.
  68. /origfont
  69. { { dup /OrigFont known not { exit } if /OrigFont get } loop
  70. FontDirectory 1 index /FontName get .knownget
  71. { % Stack: origfont namedfont
  72. 1 index /FontType get 1 index /FontType get eq
  73. { 1 index /UniqueID .knownget
  74. { 1 index /UniqueID .knownget
  75. { eq { exch } if }
  76. { pop }
  77. ifelse
  78. }
  79. if
  80. }
  81. if pop
  82. }
  83. if
  84. } def
  85. % Determine the bounding box of the current device's image.
  86. % Free variables: row, zerow.
  87. /devbbox % <rw> <rh> devbbox <ymin> <ymax1> <xmin> <xmax1>
  88. { % Find top and bottom whitespace.
  89. dup
  90. { dup 0 eq { exit } if 1 sub
  91. dup currentdevice exch row copyscanlines
  92. zerow ne { 1 add exit } if
  93. }
  94. loop % ymax1
  95. 0
  96. { 2 copy eq { exit } if
  97. dup currentdevice exch row copyscanlines
  98. zerow ne { exit } if
  99. 1 add
  100. }
  101. loop % ymin
  102. exch
  103. % Find left and right whitespace.
  104. 3 index 0
  105. % Stack: rw rh ymin ymax1 xmin xmax1
  106. 3 index 1 4 index 1 sub
  107. { currentdevice exch row copyscanlines .findzeros
  108. exch 4 1 roll max 3 1 roll min exch
  109. }
  110. for % xmin xmax1
  111. % Special check: xmin > xmax1 if height = 0
  112. 2 copy gt { exch pop dup } if
  113. 6 -2 roll pop pop
  114. } def
  115. % Write values on outfile.
  116. /w1 { 255 and outfile exch write } def
  117. /w2 { dup -8 bitshift w1 w1 } def
  118. /wbyte % <byte> <label> wbyte
  119. { VDEBUG { print ( =byte= ) print dup == flush } { pop } ifelse w1
  120. } def
  121. /wword % <word16> <label> wword
  122. { VDEBUG { print ( =word= ) print dup == flush } { pop } ifelse w2
  123. } def
  124. /wdword % <word32> <label> wdword
  125. { VDEBUG { print ( =dword= ) print dup == flush } { pop } ifelse
  126. dup -16 bitshift w2 w2
  127. } def
  128. /style.posture.keys
  129. [ { (Italic) } { (Oblique) }
  130. { }
  131. ] def
  132. /style.posture.values <010100> def
  133. /style.appearance.width.keys
  134. [ { (Ultra) (Compressed) }
  135. { (Extra) (Compressed) }
  136. { (Extra) (Condensed) }
  137. { (Extra) (Extended) }
  138. { (Extra) (Expanded) }
  139. { (Compressed) }
  140. { (Condensed) }
  141. { (Extended) }
  142. { (Expanded) }
  143. { }
  144. ] def
  145. /style.appearance.width.values <04030207070201060600> def
  146. /width.type.keys
  147. [ { (Ultra) (Compressed) }
  148. { (Extra) (Compressed) }
  149. { (Extra) (Condensed) }
  150. { (Extra) (Expanded) }
  151. { (Compressed) }
  152. { (Condensed) }
  153. { (Expanded) }
  154. { }
  155. ] def
  156. /width.type.values <fbfcfd03fdfe0200> def
  157. /stroke.weight.keys
  158. [ { (Ultra) (Thin) }
  159. { (Ultra) (Black) }
  160. { (Extra) (Thin) }
  161. { (Extra) (Light) }
  162. { (Extra) (Bold) }
  163. { (Extra) (Black) }
  164. { (Demi) (Light) }
  165. { (Demi) (Bold) }
  166. { (Semi) (Light) }
  167. { (Semi) (Bold) }
  168. { (Thin) }
  169. { (Light) }
  170. { (Bold) }
  171. { (Black) }
  172. { }
  173. ] def
  174. /stroke.weight.values <f907fafc0406fe02ff01fbfd030500> def
  175. /vendor.keys
  176. [ { (Agfa) }
  177. { (Bitstream) }
  178. { (Linotype) }
  179. { (Monotype) }
  180. { (Adobe) }
  181. { }
  182. ] def
  183. /vendor.default.index 4 def % might as well be Adobe
  184. /old.vendor.values <020406080a00> def
  185. /new.vendor.values <010203040500> def
  186. /vendor.initials (CBLMA\000) def
  187. currentdict readonly end pop % pcldict
  188. % Convert and write a PCL font for the current font and transformation.
  189. % Write the font header. We split this off only to avoid overflowing
  190. % the limit on the maximum size of a procedure.
  191. % Free variables: outfile uury u0y rw rh orientation uh ully
  192. /writefontheader
  193. { outfile (\033\)s) writestring
  194. outfile 64 WriteResolution? { 4 add } if
  195. Copyright length add write==only
  196. outfile (W) writestring
  197. WriteResolution? { 20 68 } { 0 64 } ifelse
  198. (Font Descriptor Size) wword
  199. (Header Format) wbyte
  200. 1 (Font Type) wbyte
  201. FullName style.posture.keys keysearch style.posture.values exch get
  202. FullName style.appearance.width.keys keysearch
  203. style.appearance.width.values exch get 4 mul add
  204. PaintType 2 eq { 32 add } if
  205. /style exch def
  206. style -8 bitshift (Style MSB) wbyte
  207. 0 (Reserved) wbyte
  208. /baseline uury 1 sub u0y sub def
  209. baseline (Baseline Position) wword
  210. rw (Cell Width) wword
  211. rh (Cell Height) wword
  212. orientation (Orientation) wbyte
  213. FontInfo /isFixedPitch .knownget not { false } if
  214. { 0 } { 1 } ifelse (Spacing) wbyte
  215. % Use loop/exit to fake a multiple-exit block.
  216. { Encoding StandardEncoding eq { 10 (J) exit } if
  217. Encoding ISOLatin1Encoding eq { 11 (J) exit } if
  218. Encoding SymbolEncoding eq { 19 (M) exit } if
  219. Encoding DingbatsEncoding eq { 10 (L) exit } if
  220. % (Warning: unknown Encoding, using ISOLatin1.\n) print flush
  221. 11 (J) exit
  222. }
  223. loop
  224. 0 get 64 sub exch 32 mul add (Symbol Set) wword
  225. ( ) stringwidth pop 0 dtransform add abs 4 mul
  226. /pitch exch def
  227. pitch cvi (Pitch) wword
  228. uh 4 mul (Height) wword % Height
  229. (x) charheight (x-Height) wword
  230. FullName width.type.keys keysearch
  231. width.type.values exch get (Width Type) wbyte
  232. style 255 and (Style LSB) wbyte
  233. FullName stroke.weight.keys keysearch
  234. stroke.weight.values exch get (Stroke Weight) wbyte
  235. FullName vendor.keys keysearch
  236. dup vendor.initials exch get 0 eq
  237. { % No vendor in FullName, try Notice
  238. pop Copyright vendor.keys keysearch
  239. dup vendor.initials exch get 0 eq { pop vendor.default.index } if
  240. }
  241. if
  242. /vendor.index exch def
  243. 0 (Typeface LSB) wbyte % punt
  244. 0 (Typeface MSB) wbyte % punt
  245. 0 (Serif Style) wbyte % punt
  246. 2 (Quality) wbyte
  247. 0 (Placement) wbyte
  248. gsave FontMatrix concat rot neg rotate
  249. /ulwidth
  250. FontInfo /UnderlineThickness .knownget
  251. { 0 exch dtransform exch pop abs }
  252. { resolution 100 div }
  253. ifelse def
  254. FontInfo /UnderlinePosition .knownget
  255. { 0 exch transform exch pop negY ulwidth 2 div add }
  256. { ully ulwidth add }
  257. ifelse u0y sub
  258. round cvi 1 max 255 min (Underline Position) wbyte
  259. ulwidth round cvi 1 max 255 min (Underline Thickness) wbyte
  260. grestore
  261. uh 1.2 mul 4 mul cvi (Text Height) wword
  262. (average lowercase character) dup stringwidth
  263. pop 0 dtransform add abs
  264. exch length div 4 mul cvi (Text Width) wword
  265. 0
  266. { dup Encoding exch get /.notdef ne { exit } if
  267. 1 add
  268. }
  269. loop (First Code) wword
  270. 255
  271. { dup Encoding exch get /.notdef ne { exit } if
  272. 1 sub
  273. }
  274. loop (Last Code) wword
  275. pitch dup cvi sub 256 mul cvi (Pitch Extended) wbyte
  276. 0 (Height Extended) wbyte
  277. 0 (Cap Height) wword % (default)
  278. currentfont /UniqueID known { UniqueID } { 0 } ifelse
  279. 16#c1000000 add (Font Number (Adobe UniqueID)) wdword
  280. FontName length 16 max string
  281. dup FontName exch cvs pop
  282. outfile exch 0 16 getinterval writestring % Font Name
  283. WriteResolution?
  284. { resolution dup (X Resolution) wword (Y Resolution) wword
  285. }
  286. if
  287. outfile Copyright writestring % Copyright
  288. } def
  289. /writePCL % <fontfile> <resolution> writePCL -
  290. {
  291. save
  292. currentfont begin
  293. pcldict begin
  294. 80 dict begin % allow for recursion
  295. /saved exch def
  296. /resolution exch def
  297. /outfile exch def
  298. matrix currentmatrix dup 4 0 put dup 5 0 put setmatrix
  299. % Supply some default values so we don't have to check later.
  300. currentfont /FontInfo known not { /FontInfo 1 dict def } if
  301. currentfont /FontName known not { /FontName () def } if
  302. /Copyright FontInfo /Notice .knownget not { () } if def
  303. /FullName
  304. FontInfo /FullName .knownget not
  305. { FontName dup length string cvs }
  306. if def
  307. % Determine the original font, and its relationship to this one.
  308. /OrigFont currentfont origfont def
  309. /OrigMatrix OrigFont /FontMatrix get def
  310. /OrigMatrixInverse OrigMatrix matrix invertmatrix def
  311. /ScaleMatrix matrix currentfont OrigFont ne
  312. { FontMatrix exch OrigMatrixInverse exch concatmatrix
  313. } if
  314. def
  315. /CurrentScaleMatrix
  316. matrix currentmatrix
  317. matrix defaultmatrix
  318. dup 0 get 1 index 3 get mul 0 lt
  319. 1 index dup 1 get exch 2 get mul 0 gt or
  320. /flipY exch def
  321. dup invertmatrix
  322. dup concatmatrix
  323. def
  324. /negY flipY { {neg} } { {} } ifelse def
  325. % Print debugging information.
  326. /CDEBUG where { pop } { /CDEBUG false def } ifelse
  327. /VDEBUG where { pop } { /VDEBUG false def } ifelse
  328. CDEBUG { /VDEBUG true def } if
  329. DEBUG
  330. { (currentmatrix: ) print matrix currentmatrix ==
  331. (defaultmatrix: ) print matrix defaultmatrix ==
  332. (flipY: ) print flipY ==
  333. (scaling matrix: ) print CurrentScaleMatrix ==
  334. (FontMatrix: ) print FontMatrix ==
  335. (FontBBox: ) print /FontBBox load ==
  336. currentfont OrigFont ne
  337. { OrigFont /FontName .knownget { (orig FontName: ) print == } if
  338. (orig FontMatrix: ) print OrigMatrix ==
  339. } if
  340. currentfont /ScaleMatrix .knownget { (ScaleMatrix: ) print == } if
  341. gsave
  342. FontMatrix concat
  343. (combined matrix: ) print matrix currentmatrix ==
  344. grestore
  345. flush
  346. } if
  347. % Determine the orientation.
  348. ScaleMatrix matrix currentmatrix dup concatmatrix
  349. 0 1 3
  350. { 1 index 1 get 0 eq 2 index 2 get 0 eq and 2 index 0 get 0 gt and
  351. { exit } if
  352. pop -90 matrix rotate exch dup concatmatrix
  353. }
  354. for
  355. dup type /integertype ne
  356. { (Only rotations by multiples of 90 degrees are supported:\n) print
  357. == flush
  358. saved end end end restore stop
  359. }
  360. if
  361. /orientation exch def
  362. /rot orientation 90 mul def
  363. DEBUG { (orientation: ) print orientation == flush } if
  364. dup dup 0 get exch 3 get negY sub abs 0.5 ge
  365. { (Only identical scaling in X and Y is supported:\n) print
  366. exch flipY 3 array astore ==
  367. currentdevice .devicename ==
  368. matrix defaultmatrix == flush
  369. saved end end end restore stop
  370. }
  371. if pop
  372. % Determine the font metrics, in the PCL character coordinate system,
  373. % which has +Y going towards the top of the page.
  374. gsave
  375. FontMatrix concat
  376. 0 0 transform
  377. negY round cvi /r0y exch def
  378. round cvi /r0x exch def
  379. ixbbox
  380. negY /rury exch def /rurx exch def
  381. negY /rlly exch def /rllx exch def
  382. /rminx rllx rurx min def
  383. /rminy rlly negY rury negY min def
  384. /rw rurx rllx sub abs def
  385. /rh rury rlly sub abs def
  386. gsave rot neg rotate
  387. 0 0 transform
  388. negY round cvi /u0y exch def
  389. round cvi /u0x exch def
  390. ixbbox
  391. negY /uury exch def /uurx exch def
  392. negY /ully exch def /ullx exch def
  393. /uw uurx ullx sub def
  394. /uh uury ully sub def
  395. grestore
  396. DEBUG
  397. { (rmatrix: ) print matrix currentmatrix ==
  398. (rFontBBox: ) print [rllx rlly rurx rury] ==
  399. (uFontBBox: ) print [ullx ully uurx uury] ==
  400. flush
  401. } if
  402. grestore
  403. % Disable the character cache, to avoid excessive allocation
  404. % and memory sandbars.
  405. mark cachestatus /upper exch def
  406. cleartomark 0 setcachelimit
  407. % Write the font header.
  408. writefontheader
  409. % Establish an image device for rasterizing characters.
  410. matrix currentmatrix
  411. dup 4 rminx neg put
  412. dup 5 rminy neg put
  413. % Round the width up to a multiple of 8
  414. % so we don't get garbage bits in the last byte of each row.
  415. rw 7 add -8 and rh <ff 00> makeimagedevice
  416. /cdevice exch def
  417. nulldevice % prevent page device switching
  418. cdevice setdevice
  419. % Rasterize each character in turn.
  420. /raster rw 7 add 8 idiv def
  421. /row raster string def
  422. /zerow row length string def
  423. 0 1 Encoding length 1 sub
  424. { /cindex exch def
  425. Encoding cindex get /.notdef ne
  426. { VDEBUG { Encoding cindex get == flush } if
  427. erasepage initgraphics
  428. 0 0 moveto currentpoint transform add
  429. ( ) dup 0 cindex put show
  430. currentpoint transform add exch sub round cvi
  431. /cwidth exch abs def
  432. rw rh devbbox
  433. VDEBUG
  434. { (image bbox: ) print 4 copy 4 2 roll 4 array astore == flush
  435. } if
  436. % Save the device bounding box.
  437. % Note that this is in current device coordinates,
  438. % not PCL (right-handed) coordinates.
  439. /bqx exch def /bpx exch def /bqy exch def /bpy exch def
  440. % Re-render with the character justified to (0,0).
  441. % This may be either the lower left or the upper left corner.
  442. bpx neg bpy neg idtransform moveto
  443. erasepage
  444. VDEBUG { (show point: ) print [ currentpoint transform ] == flush } if
  445. ( ) dup 0 cindex put show
  446. % Find the bounding box. Note that xmin and ymin are now 0,
  447. % xmax1 = xw, and ymax1 = yh.
  448. rw rh devbbox
  449. /xw exch def
  450. % xmin or ymin can be non-zero only if the character is blank.
  451. xw 0 eq
  452. { pop }
  453. { dup 0 ne { (Non-zero xmin! ) print = } { pop } ifelse }
  454. ifelse
  455. /yh exch def
  456. yh 0 eq
  457. { pop }
  458. { dup 0 ne { (Non-zero ymin! ) print = } { pop } ifelse }
  459. ifelse
  460. /xbw xw 7 add 8 idiv def
  461. /xright raster 8 mul xw sub def
  462. % Write the Character Code command.
  463. outfile (\033*c) writestring
  464. outfile cindex write==only
  465. outfile (E) writestring
  466. % Write the Character Definition command.
  467. outfile (\033\(s) writestring
  468. yh xbw mul 16 add
  469. outfile exch write=only
  470. % Record the character position for the .PCM file.
  471. /cfpos outfile fileposition 1 add def
  472. outfile (W\004\000\016\001) writestring
  473. orientation (Orientation) wbyte 0 (Reserved) wbyte
  474. rminx bpx add r0x sub (Left Offset) wword
  475. flipY { rminy bpy add neg } { rminy bqy add } ifelse r0y sub
  476. (Top Offset) wword
  477. xw (Character Width) wword
  478. yh (Character Height) wword
  479. cwidth orientation 2 ge { neg } if 4 mul (Delta X) wword
  480. % Write the character data.
  481. flipY { 0 1 yh 1 sub } { yh 1 sub -1 0 } ifelse
  482. { cdevice exch row copyscanlines
  483. 0 xbw getinterval
  484. CDEBUG
  485. { dup
  486. { 8
  487. { dup 128 ge { (+) } { (.) } ifelse print
  488. 127 and 1 bitshift
  489. }
  490. repeat pop
  491. }
  492. forall (\n) print
  493. }
  494. if
  495. outfile exch writestring
  496. }
  497. for
  498. }
  499. { /bpx 0 def /bpy 0 def /bqx 0 def /bqy 0 def
  500. /cwidth 0 def
  501. /cfpos 0 def
  502. }
  503. ifelse
  504. }
  505. for
  506. % Wrap up.
  507. upper setcachelimit
  508. outfile closefile
  509. nulldevice % prevent page device switching
  510. saved end end end restore
  511. } def
  512. % Provide definitions for testing with older or non-custom interpreters.
  513. /.findzeros where { pop (%END) .skipeof } if
  514. /.findzeros
  515. { userdict begin /zs exch def /zl zs length def
  516. 0 { dup zl ge { exit } if dup zs exch get 0 ne { exit } if 1 add } loop
  517. zl { dup 0 eq { exit } if dup 1 sub zs exch get 0 ne { exit } if 1 sub } loop
  518. exch 3 bitshift exch 3 bitshift
  519. 2 copy lt
  520. { exch zs 1 index -3 bitshift get
  521. { dup 16#80 and 0 ne { exit } if exch 1 add exch 1 bitshift } loop pop
  522. exch zs 1 index -3 bitshift 1 sub get
  523. { dup 1 and 0 ne { exit } if exch 1 sub exch -1 bitshift } loop pop
  524. }
  525. if end
  526. } bind def
  527. %END
  528. /write=only where { pop (%END) .skipeof } if
  529. /w=s 128 string def
  530. /write=only
  531. { w=s cvs writestring
  532. } bind def
  533. %END
  534. %**************** Test
  535. /PCLTEST where {
  536. pop
  537. /DEBUG true def
  538. /CDEBUG true def
  539. /VDEBUG true def
  540. /Times-Roman findfont 10 scalefont setfont
  541. (t.pcf) (w) file
  542. 300 72 div dup scale
  543. 300 writePCL
  544. flush quit
  545. } if