home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / unixtex-6.1b-src.lha / unixtex-6.1b / web2c / fontutil / vftovp.web (.txt) < prev    next >
Encoding:
Texinfo Document  |  1990-09-23  |  86.8 KB  |  2,026 lines

  1. % This program by D. E. Knuth is not copyrighted and can be used freely.
  2. % Version 1 was implemented in December 1989.
  3. % Version 1.1 fixed problems of strict Pascal (April 1990).
  4. % Version 1.2 fixed various bugs found by Peter Breitenlohner (September 1990).
  5. % Here is TeX material that gets inserted after \input webmac
  6. \def\hang{\hangindent 3em\indent\ignorespaces}
  7. \font\ninerm=cmr9
  8. \let\mc=\ninerm % medium caps for names like SAIL
  9. \def\PASCAL{Pascal}
  10. \def\(#1){} % this is used to make section names sort themselves better
  11. \def\9#1{} % this is used for sort keys in the index
  12. \def\title{VF\lowercase{to}VP}
  13. \def\contentspagenumber{101}
  14. \def\topofcontents{\null
  15.   \def\titlepage{F} % include headline on the contents page
  16.   \def\rheader{\mainfont\hfil \contentspagenumber}
  17.   \vfill
  18.   \centerline{\titlefont The {\ttitlefont VFtoVP} processor}
  19.   \vskip 15pt
  20.   \centerline{(Version 1.2, September 1990)}
  21.   \vfill}
  22. \def\botofcontents{\vfill
  23.   \centerline{\hsize 5in\baselineskip9pt
  24.     \vbox{\ninerm\noindent
  25.     The preparation of this program
  26.     was supported in part by the National Science
  27.     Foundation and by the System Development Foundation. `\TeX' is a
  28.     trademark of the American Mathematical Society.}}}
  29. \pageno=\contentspagenumber \advance\pageno by 1
  30. @* Introduction.
  31. The \.{VFtoVP} utility program converts a virtual font (``\.{VF}'') file
  32. and its associated \TeX\ font metric (``\.{TFM}'')
  33. file into an equivalent virtual-property-list (``\.{VPL}'') file. It also
  34. makes a thorough check of the given files, using algorithms that are
  35. essentially the same as those used by
  36. \.{DVI} device drivers and by \TeX. Thus if \TeX\ or a \.{DVI} driver
  37. complains that a \.{TFM} or \.{VF}
  38. file is ``bad,'' this program will pinpoint the source or sources of
  39. badness. A \.{VPL} file output by this program can be edited with
  40. a normal text editor, and the result can be converted back to \.{VF} and \.{TFM}
  41. format using the companion program \.{VPtoVF}.
  42. \indent\.{VFtoVP} is an extended version of the program \.{TFtoPL}, which
  43. is part of the standard \TeX ware library.
  44. The idea of a virtual font was inspired by the work of David R. Fuchs
  45. @^Fuchs, David Raymond@>
  46. who designed a similar set of conventions in 1984 while developing a
  47. device driver for ArborText, Inc. He wrote a somewhat similar program
  48. called \.{AMFtoXPL}.
  49. The |banner| string defined here should be changed whenever \.{VFtoVP}
  50. gets modified.
  51. @d banner=='This is VFtoVP, Version 1.2' {printed when the program starts}
  52. @ This program is written entirely in standard \PASCAL, except that
  53. it occasionally has lower case letters in strings that are output.
  54. Such letters can be converted to upper case if necessary. The input is read
  55. from |vf_file| and |tfm_file|; the output is written on |vpl_file|.
  56. Error messages and
  57. other remarks are written on the |output| file, which the user may
  58. choose to assign to the terminal if the system permits it.
  59. @^system dependencies@>
  60. The term |print| is used instead of |write| when this program writes on
  61. the |output| file, so that all such output can be easily deflected.
  62. @d print(#)==write(#)
  63. @d print_ln(#)==write_ln(#)
  64. @p program VFtoVP(@!vf_file,@!tfm_file,@!vpl_file,@!output);
  65. label @<Labels in the outer block@>@/
  66. const @<Constants in the outer block@>@/
  67. type @<Types in the outer block@>@/
  68. var @<Globals in the outer block@>@/
  69. procedure initialize; {this procedure gets things started properly}
  70.   var @!k:integer; {all-purpose index for initialization}
  71.   begin print_ln(banner);@/
  72.   @<Set initial values@>@/
  73.   end;
  74. @ If the program has to stop prematurely, it goes to the
  75. `|final_end|'.
  76. @d final_end=9999 {label for the end of it all}
  77. @<Labels...@>=final_end;
  78. @ The following parameters can be changed at compile time to extend or
  79. reduce \.{VFtoVP}'s capacity.
  80. @<Constants...@>=
  81. @!tfm_size=30000; {maximum length of |tfm| data, in bytes}
  82. @!vf_size=10000; {maximum length of |vf| data, in bytes}
  83. @!max_fonts=300; {maximum number of local fonts in the |vf| file}
  84. @!lig_size=5000; {maximum length of |lig_kern| program, in words}
  85. @!hash_size=5003; {preferably a prime number, a bit larger than the number
  86.   of character pairs in lig/kern steps}
  87. @!name_length=50; {a file name shouldn't be longer than this}
  88. @!max_stack=50; {maximum depth of \.{DVI} stack in character packets}
  89. @ Here are some macros for common programming idioms.
  90. @d incr(#) == #:=#+1 {increase a variable by unity}
  91. @d decr(#) == #:=#-1 {decrease a variable by unity}
  92. @d do_nothing == {empty statement}
  93. @d exit=10 {go here to leave a procedure}
  94. @d not_found=45 {go here when you've found nothing}
  95. @d return==goto exit {terminate a procedure call}
  96. @f return==nil
  97. @<Types...@>=
  98. @!byte=0..255; {unsigned eight-bit quantity}
  99. @* Virtual fonts.  The idea behind \.{VF} files is that a general
  100. interface mechanism is needed to switch between the myriad font
  101. layouts provided by different suppliers of typesetting equipment.
  102. Without such a mechanism, people must go to great lengths writing
  103. inscrutable macros whenever they want to use typesetting conventions
  104. based on one font layout in connection with actual fonts that have
  105. another layout. This puts an extra burden on the typesetting system,
  106. interfering with the other things it needs to do (like kerning,
  107. hyphenation, and ligature formation).
  108. These difficulties go away when we have a ``virtual font,''
  109. i.e., a font that exists in a logical sense but not a physical sense.
  110. A typesetting system like \TeX\ can do its job without knowing where the
  111. actual characters come from; a device driver can then do its job by
  112. letting a \.{VF} file tell what actual characters correspond to the
  113. characters \TeX\ imagined were present. The actual characters
  114. can be shifted and/or magnified and/or combined with other characters
  115. from many different fonts. A virtual font can even make use of characters
  116. from virtual fonts, including itself.
  117. Virtual fonts also allow convenient character substitutions for proofreading
  118. purposes, when fonts designed for one output device are unavailable on another.
  119. @ A \.{VF} file is organized as a stream of 8-bit bytes, using conventions
  120. borrowed from \.{DVI} and \.{PK} files. Thus, a device driver that knows
  121. about \.{DVI} and \.{PK} format will already
  122. contain most of the mechanisms necessary to process \.{VF} files. 
  123. We shall assume that \.{DVI} format is understood; the conventions in the
  124. \.{DVI} documentation (see, for example, {\sl \TeX: The Program}, part 31)
  125. are adopted here to define \.{VF} format.
  126. A preamble
  127. appears at the beginning, followed by a sequence of character definitions,
  128. followed by a postamble. More precisely, the first byte of every \.{VF} file
  129. must be the first byte of the following ``preamble command'':
  130. \yskip\hang|pre| 247 |i[1]| |k[1]| |x[k]| |cs[4]| |ds[4]|.
  131. Here |i| is the identification byte of \.{VF}, currently 202. The string
  132. |x| is merely a comment, usually indicating the source of the \.{VF} file.
  133. Parameters |cs| and |ds| are respectively the check sum and the design size
  134. of the virtual font; they should match the first two words in the header of
  135. the \.{TFM} file, as described below.
  136. \yskip
  137. After the |pre| command, the preamble continues with font definitions;
  138. every font needed to specify ``actual'' characters in later
  139. \\{set\_char} commands is defined here. The font definitions are
  140. exactly the same in \.{VF} files as they are in \.{DVI} files, except
  141. that the scaled size |s| is relative and the design size |d| is absolute:
  142. \yskip\hang|fnt_def1| 243 |k[1]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  143. Define font |k|, where |0<=k<256|.
  144. \yskip\hang|@!fnt_def2| 244 |k[2]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  145. Define font |k|, where |0<=k<65536|.
  146. \yskip\hang|@!fnt_def3| 245 |k[3]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  147. Define font |k|, where |0<=k<@t$2^{24}$@>|.
  148. \yskip\hang|@!fnt_def4| 246 |k[4]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  149. Define font |k|, where |@t$-2^{31}$@><=k<@t$2^{31}$@>|.
  150. \yskip\noindent
  151. These font numbers |k| are ``local''; they have no relation to font numbers
  152. defined in the \.{DVI} file that uses this virtual font. The dimension~|s|,
  153. which represents the scaled size of the local font being defined,
  154. is a |fix_word| relative to the design size of the virtual font.
  155. Thus if the local font is to be used at the same size
  156. as the design size of the virtual font itself, |s| will be the
  157. integer value $2^{20}$. The value of |s| must be positive and less than
  158. $2^{24}$ (thus less than 16 when considered as a |fix_word|). 
  159. The dimension~|d| is a |fix_word| in units of printer's points; hence it
  160. is identical to the design size found in the corresponding \.{TFM} file.
  161. @d id_byte=202
  162. @<Glob...@>=
  163. @!vf_file:packed file of byte;
  164. @ The preamble is followed by zero or more character packets, where each
  165. character packet begins with a byte that is $<243$. Character packets have
  166. two formats, one long and one short:
  167. \yskip\hang|long_char| 242 |pl[4]| |cc[4]| |tfm[4]| |dvi[pl]|. This long form
  168. specifies a virtual character in the general case.
  169. \yskip\hang|short_char0..short_char241|
  170. |pl[1]| |cc[1]| |tfm[3]| |dvi[pl]|. This short form specifies a
  171. virtual character in the common case
  172. when |0<=pl<242| and |0<=cc<256| and $0\le|tfm|<2^{24}$.
  173. \yskip\noindent
  174. Here |pl| denotes the packet length following the |tfm| value; |cc| is
  175. the character code; and |tfm| is the character width copied from the
  176. \.{TFM} file for this virtual font. There should be at most one character
  177. packet having any given |cc| code.
  178. The |dvi| bytes are a sequence of complete \.{DVI} commands, properly
  179. nested with respect to |push| and |pop|. All \.{DVI} operations are
  180. permitted except |bop|, |eop|, and commands with opcodes |>=243|.
  181. Font selection commands (|fnt_num0| through |fnt4|) must refer to fonts
  182. defined in the preamble.
  183. Dimensions that appear in the \.{DVI} instructions are analogous to
  184. |fix_word| quantities; i.e., they are integer multiples of $2^{-20}$ times
  185. the design size of the virtual font. For example, if the virtual font
  186. has design size $10\,$pt, the \.{DVI} command to move down $5\,$pt
  187. would be a \\{down} instruction with parameter $2^{19}$. The virtual font
  188. itself might be used at a different size, say $12\,$pt; then that
  189. \\{down} instruction would move down $6\,$pt instead. Each dimension
  190. must be less than $2^{24}$ in absolute value.
  191. Device drivers processing \.{VF} files treat the sequences of |dvi| bytes
  192. as subroutines or macros, implicitly enclosing them with |push| and |pop|.
  193. Each subroutine begins with |w=x=y=z=0|, and with current font~|f| the
  194. number of the first-defined in the preamble (undefined if there's no
  195. such font). After the |dvi| commands have been
  196. performed, the |h| and~|v| position registers of \.{DVI} format and the
  197. current font~|f| are restored to their former values;
  198. then, if the subroutine has been invoked by a \\{set\_char} or \\{set}
  199. command, |h|~is increased by the \.{TFM} width
  200. (properly scaled)---just as if a simple character had been typeset.
  201. @d long_char=242 {\.{VF} command for general character packet}
  202. @d set_char_0=0 {\.{DVI} command to typeset character 0 and move right}
  203. @d set1=128 {typeset a character and move right}
  204. @d set_rule=132 {typeset a rule and move right}
  205. @d put1=133 {typeset a character}
  206. @d put_rule=137 {typeset a rule}
  207. @d nop=138 {no operation}
  208. @d push=141 {save the current positions}
  209. @d pop=142 {restore previous positions}
  210. @d right1=143 {move right}
  211. @d w0=147 {move right by |w|}
  212. @d w1=148 {move right and set |w|}
  213. @d x0=152 {move right by |x|}
  214. @d x1=153 {move right and set |x|}
  215. @d down1=157 {move down}
  216. @d y0=161 {move down by |y|}
  217. @d y1=162 {move down and set |y|}
  218. @d z0=166 {move down by |z|}
  219. @d z1=167 {move down and set |z|}
  220. @d fnt_num_0=171 {set current font to 0}
  221. @d fnt1=235 {set current font}
  222. @d xxx1=239 {extension to \.{DVI} primitives}
  223. @d xxx4=242 {potentially long extension to \.{DVI} primitives}
  224. @d fnt_def1=243 {define the meaning of a font number}
  225. @d pre=247 {preamble}
  226. @d post=248 {postamble beginning}
  227. @d improper_DVI_for_VF==139,140,243,244,245,246,247,248,249,250,251,252,
  228.     253,254,255
  229. @ The character packets are followed by a trivial postamble, consisting of
  230. one or more bytes all equal to |post| (248). The total number of bytes
  231. in the file should be a multiple of~4.
  232. @* Font metric data.
  233. The idea behind \.{TFM} files is that typesetting routines like \TeX\
  234. need a compact way to store the relevant information about several
  235. dozen fonts, and computer centers need a compact way to store the
  236. relevant information about several hundred fonts. \.{TFM} files are
  237. compact, and most of the information they contain is highly relevant,
  238. so they provide a solution to the problem.
  239. The information in a \.{TFM} file appears in a sequence of 8-bit bytes.
  240. Since the number of bytes is always a multiple of 4, we could
  241. also regard the file as a sequence of 32-bit words; but \TeX\ uses the
  242. byte interpretation, and so does \.{VFtoVP}. Note that the bytes
  243. are considered to be unsigned numbers.
  244. @<Glob...@>=
  245. @!tfm_file:packed file of byte;
  246. @ On some systems you may have to do something special to read a
  247. packed file of bytes. For example, the following code didn't work
  248. when it was first tried at Stanford, because packed files have to be
  249. opened with a special switch setting on the \PASCAL\ that was used.
  250. @^system dependencies@>
  251. @<Set init...@>=
  252. reset(tfm_file); reset(vf_file);
  253. @ The first 24 bytes (6 words) of a \.{TFM} file contain twelve 16-bit
  254. integers that give the lengths of the various subsequent portions
  255. of the file. These twelve integers are, in order:
  256. $$\vbox{\halign{\hfil#&$\null=\null$#\hfil\cr
  257. |@!lf|&length of the entire file, in words;\cr
  258. |@!lh|&length of the header data, in words;\cr
  259. |@!bc|&smallest character code in the font;\cr
  260. |@!ec|&largest character code in the font;\cr
  261. |@!nw|&number of words in the width table;\cr
  262. |@!nh|&number of words in the height table;\cr
  263. |@!nd|&number of words in the depth table;\cr
  264. |@!ni|&number of words in the italic correction table;\cr
  265. |@!nl|&number of words in the lig/kern table;\cr
  266. |@!nk|&number of words in the kern table;\cr
  267. |@!ne|&number of words in the extensible character table;\cr
  268. |@!np|&number of font parameter words.\cr}}$$
  269. They are all nonnegative and less than $2^{15}$. We must have |bc-1<=ec<=255|,
  270. |ne<=256|, and
  271. $$\hbox{|lf=6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np|.}$$
  272. Note that a font may contain as many as 256 characters (if |bc=0| and |ec=255|),
  273. and as few as 0 characters (if |bc=ec+1|).
  274. Incidentally, when two or more 8-bit bytes are combined to form an integer of
  275. 16 or more bits, the most significant bytes appear first in the file.
  276. This is called BigEndian order.
  277. @<Glob...@>=
  278. @!lf,@!lh,@!bc,@!ec,@!nw,@!nh,@!nd,@!ni,@!nl,@!nk,@!ne,@!np:0..@'77777;
  279.   {subfile sizes}
  280. @ The rest of the \.{TFM} file may be regarded as a sequence of ten data
  281. arrays having the informal specification
  282. $$\def\arr$[#1]#2${\&{array} $[#1]$ \&{of} #2}
  283. \vbox{\halign{\hfil\\{#}&$\,:\,$\arr#\hfil\cr
  284. header&|[0..lh-1]stuff|\cr
  285. char\_info&|[bc..ec]char_info_word|\cr
  286. width&|[0..nw-1]fix_word|\cr
  287. height&|[0..nh-1]fix_word|\cr
  288. depth&|[0..nd-1]fix_word|\cr
  289. italic&|[0..ni-1]fix_word|\cr
  290. lig\_kern&|[0..nl-1]lig_kern_command|\cr
  291. kern&|[0..nk-1]fix_word|\cr
  292. exten&|[0..ne-1]extensible_recipe|\cr
  293. param&|[1..np]fix_word|\cr}}$$
  294. The most important data type used here is a |@!fix_word|, which is
  295. a 32-bit representation of a binary fraction. A |fix_word| is a signed
  296. quantity, with the two's complement of the entire word used to represent
  297. negation. Of the 32 bits in a |fix_word|, exactly 12 are to the left of the
  298. binary point; thus, the largest |fix_word| value is $2048-2^{-20}$, and
  299. the smallest is $-2048$. We will see below, however, that all but one of
  300. the |fix_word| values will lie between $-16$ and $+16$.
  301. @ The first data array is a block of header information, which contains
  302. general facts about the font. The header must contain at least two words,
  303. and for \.{TFM} files to be used with Xerox printing software it must
  304. contain at least 18 words, allocated as described below. When different
  305. kinds of devices need to be interfaced, it may be necessary to add further
  306. words to the header block.
  307. \yskip\hang|header[0]| is a 32-bit check sum that \TeX\ will copy into the
  308. \.{DVI} output file whenever it uses the font.  Later on when the \.{DVI}
  309. file is printed, possibly on another computer, the actual font that gets
  310. used is supposed to have a check sum that agrees with the one in the
  311. \.{TFM} file used by \TeX. In this way, users will be warned about
  312. potential incompatibilities. (However, if the check sum is zero in either
  313. the font file or the \.{TFM} file, no check is made.)  The actual relation
  314. between this check sum and the rest of the \.{TFM} file is not important;
  315. the check sum is simply an identification number with the property that
  316. incompatible fonts almost always have distinct check sums.
  317. @^check sum@>
  318. \yskip\hang|header[1]| is a |fix_word| containing the design size of the
  319. font, in units of \TeX\ points (7227 \TeX\ points = 254 cm).  This number
  320. must be at least 1.0; it is fairly arbitrary, but usually the design size
  321. is 10.0 for a ``10 point'' font, i.e., a font that was designed to look
  322. best at a 10-point size, whatever that really means. When a \TeX\ user
  323. asks for a font `\.{at} $\delta$ \.{pt}', the effect is to override the
  324. design size and replace it by $\delta$, and to multiply the $x$ and~$y$
  325. coordinates of the points in the font image by a factor of $\delta$
  326. divided by the design size.  {\sl All other dimensions in the\/\ \.{TFM}
  327. file are |fix_word|\kern-1pt\ numbers in design-size units.} Thus, for example,
  328. the value of |param[6]|, one \.{em} or \.{\\quad}, is often the |fix_word|
  329. value $2^{20}=1.0$, since many fonts have a design size equal to one em.
  330. The other dimensions must be less than 16 design-size units in absolute
  331. value; thus, |header[1]| and |param[1]| are the only |fix_word| entries in
  332. the whole \.{TFM} file whose first byte might be something besides 0 or
  333. 255.  @^design size@>
  334. \yskip\hang|header[2..11]|, if present, contains 40 bytes that identify
  335. the character coding scheme. The first byte, which must be between 0 and
  336. 39, is the number of subsequent ASCII bytes actually relevant in this
  337. string, which is intended to specify what character-code-to-symbol
  338. convention is present in the font.  Examples are \.{ASCII} for standard
  339. ASCII, \.{TeX text} for fonts like \.{cmr10} and \.{cmti9}, \.{TeX math
  340. extension} for \.{cmex10}, \.{XEROX text} for Xerox fonts, \.{GRAPHIC} for
  341. special-purpose non-alphabetic fonts, \.{UNSPECIFIED} for the default case
  342. when there is no information.  Parentheses should not appear in this name.
  343. (Such a string is said to be in {\mc BCPL} format.)
  344. @^coding scheme@>
  345. \yskip\hang|header[12..16]|, if present, contains 20 bytes that name the
  346. font family (e.g., \.{CMR} or \.{HELVETICA}), in {\mc BCPL} format.
  347. This field is also known as the ``font identifier.''
  348. @^family name@>
  349. @^font identifier@>
  350. \yskip\hang|header[17]|, if present, contains a first byte called the
  351. |seven_bit_safe_flag|, then two bytes that are ignored, and a fourth byte
  352. called the |face|. If the value of the fourth byte is less than 18, it has
  353. the following interpretation as a ``weight, slope, and expansion'':  Add 0
  354. or 2 or 4 (for medium or bold or light) to 0 or 1 (for roman or italic) to
  355. 0 or 6 or 12 (for regular or condensed or extended).  For example, 13 is
  356. 0+1+12, so it represents medium italic extended.  A three-letter code
  357. (e.g., \.{MIE}) can be used for such |face| data.
  358. \yskip\hang|header[18..@twhatever@>]| might also be present; the individual
  359. words are simply called |header[18]|, |header[19]|, etc., at the moment.
  360. @ Next comes the |char_info| array, which contains one |char_info_word|
  361. per character. Each |char_info_word| contains six fields packed into
  362. four bytes as follows.
  363. \yskip\hang first byte: |width_index| (8 bits)\par
  364. \hang second byte: |height_index| (4 bits) times 16, plus |depth_index|
  365.   (4~bits)\par
  366. \hang third byte: |italic_index| (6 bits) times 4, plus |tag|
  367.   (2~bits)\par
  368. \hang fourth byte: |remainder| (8 bits)\par
  369. \yskip\noindent
  370. The actual width of a character is |width[width_index]|, in design-size
  371. units; this is a device for compressing information, since many characters
  372. have the same width. Since it is quite common for many characters
  373. to have the same height, depth, or italic correction, the \.{TFM} format
  374. imposes a limit of 16 different heights, 16 different depths, and
  375. 64 different italic corrections.
  376. Incidentally, the relation |width[0]=height[0]=depth[0]=italic[0]=0|
  377. should always hold, so that an index of zero implies a value of zero.
  378. The |width_index| should never be zero unless the character does
  379. not exist in the font, since a character is valid if and only if it lies
  380. between |bc| and |ec| and has a nonzero |width_index|.
  381. @ The |tag| field in a |char_info_word| has four values that explain how to
  382. interpret the |remainder| field.
  383. \yskip\hang|tag=0| (|no_tag|) means that |remainder| is unused.\par
  384. \hang|tag=1| (|lig_tag|) means that this character has a ligature/kerning
  385. program starting at |lig_kern[remainder]|.\par
  386. \hang|tag=2| (|list_tag|) means that this character is part of a chain of
  387. characters of ascending sizes, and not the largest in the chain.  The
  388. |remainder| field gives the character code of the next larger character.\par
  389. \hang|tag=3| (|ext_tag|) means that this character code represents an
  390. extensible character, i.e., a character that is built up of smaller pieces
  391. so that it can be made arbitrarily large. The pieces are specified in
  392. |exten[remainder]|.\par
  393. @d no_tag=0 {vanilla character}
  394. @d lig_tag=1 {character has a ligature/kerning program}
  395. @d list_tag=2 {character has a successor in a charlist}
  396. @d ext_tag=3 {character is extensible}
  397. @ The |lig_kern| array contains instructions in a simple programming language
  398. that explains what to do for special letter pairs. Each word is a
  399. |lig_kern_command| of four bytes.
  400. \yskip\hang first byte: |skip_byte|, indicates that this is the final program
  401.   step if the byte is 128 or more, otherwise the next step is obtained by
  402.   skipping this number of intervening steps.\par
  403. \hang second byte: |next_char|, ``if |next_char| follows the current character,
  404.   then perform the operation and stop, otherwise continue.''\par
  405. \hang third byte: |op_byte|, indicates a ligature step if less than~128,
  406.   a kern step otherwise.\par
  407. \hang fourth byte: |remainder|.\par
  408. \yskip\noindent
  409. In a kern step, an
  410. additional space equal to |kern[256*(op_byte-128)+remainder]| is inserted
  411. between the current character and |next_char|. This amount is
  412. often negative, so that the characters are brought closer together
  413. by kerning; but it might be positive.
  414. There are eight kinds of ligature steps, having |op_byte| codes $4a+2b+c$ where
  415. $0\le a\le b+c$ and $0\le b,c\le1$. The character whose code is
  416. |remainder| is inserted between the current character and |next_char|;
  417. then the current character is deleted if $b=0$, and |next_char| is
  418. deleted if $c=0$; then we pass over $a$~characters to reach the next
  419. current character (which may have a ligature/kerning program of its own).
  420. Notice that if $a=0$ and $b=1$, the current character is unchanged; if
  421. $a=b$ and $c=1$, the current character is changed but the next character is
  422. unchanged. \.{VFtoVP} will check to see that infinite loops are avoided.
  423. If the very first instruction of the |lig_kern| array has |skip_byte=255|,
  424. the |next_char| byte is the so-called right boundary character of this font;
  425. the value of |next_char| need not lie between |bc| and~|ec|.
  426. If the very last instruction of the |lig_kern| array has |skip_byte=255|,
  427. there is a special ligature/kerning program for a left boundary character,
  428. beginning at location |256*op_byte+remainder|.
  429. The interpretation is that \TeX\ puts implicit boundary characters
  430. before and after each consecutive string of characters from the same font.
  431. These implicit characters do not appear in the output, but they can affect
  432. ligatures and kerning.
  433. If the very first instruction of a character's |lig_kern| program has
  434. |skip_byte>128|, the program actually begins in location
  435. |256*op_byte+remainder|. This feature allows access to large |lig_kern|
  436. arrays, because the first instruction must otherwise
  437. appear in a location |<=255|.
  438. Any instruction with |skip_byte>128| in the |lig_kern| array must have
  439. |256*op_byte+remainder<nl|. If such an instruction is encountered during
  440. normal program execution, it denotes an unconditional halt; no ligature
  441. command is performed.
  442. @d stop_flag=128 {value indicating `\.{STOP}' in a lig/kern program}
  443. @d kern_flag=128 {op code for a kern step}
  444. @ Extensible characters are specified by an |extensible_recipe|,
  445. which consists of four bytes called |top|, |mid|,
  446. |bot|, and |rep| (in this order). These bytes are the character codes
  447. of individual pieces used to build up a large symbol.
  448. If |top|, |mid|, or |bot| are zero,
  449. they are not present in the built-up result. For example, an extensible
  450. vertical line is like an extensible bracket, except that the top and
  451. bottom pieces are missing.
  452. @ The final portion of a \.{TFM} file is the |param| array, which is another
  453. sequence of |fix_word| values.
  454. \yskip\hang|param[1]=@!slant| is the amount of italic slant, which is used
  455. to help position accents. For example, |slant=.25| means that when you go
  456. up one unit, you also go .25 units to the right. The |slant| is a pure
  457. number; it's the only |fix_word| other than the design size itself that is
  458. not scaled by the design size.
  459. \hang|param[2]=space| is the normal spacing between words in text.
  460. Note that character |" "| in the font need not have anything to do with
  461. blank spaces.
  462. \hang|param[3]=space_stretch| is the amount of glue stretching between words.
  463. \hang|param[4]=space_shrink| is the amount of glue shrinking between words.
  464. \hang|param[5]=x_height| is the height of letters for which accents don't
  465. have to be raised or lowered.
  466. \hang|param[6]=quad| is the size of one em in the font.
  467. \hang|param[7]=extra_space| is the amount added to |param[2]| at the
  468. ends of sentences.
  469. When the character coding scheme is \.{TeX math symbols}, the font is
  470. supposed to have 15 additional parameters called |num1|, |num2|, |num3|,
  471. |denom1|, |denom2|, |sup1|, |sup2|, |sup3|, |sub1|, |sub2|, |supdrop|,
  472. |subdrop|, |delim1|, |delim2|, and |axis_height|, respectively. When the
  473. character coding scheme is \.{TeX math extension}, the font is supposed to
  474. have six additional parameters called |default_rule_thickness| and
  475. |big_op_spacing1| through |big_op_spacing5|.
  476. @ So that is what \.{TFM} files hold. The next question is, ``What about
  477. \.{VPL} files?'' A complete answer to that question appears in the
  478. documentation of the companion program, \.{VPtoVF}, so it will not
  479. be repeated here. Suffice it to say that a \.{VPL} file is an ordinary
  480. \PASCAL\ text file, and that the output of \.{VFtoVP} uses only a
  481. subset of the possible constructions that might appear in a \.{VPL} file.
  482. Furthermore, hardly anybody really wants to look at the formal
  483. definition of \.{VPL} format, because it is almost self-explanatory when
  484. you see an example or two.
  485. @<Glob...@>=
  486. @!vpl_file:text;
  487. @ @<Set init...@>=
  488. rewrite(vpl_file);
  489. @* Unpacking the TFM file.
  490. The first thing \.{VFtoVP} does is read the entire |tfm_file| into an array of
  491. bytes, |tfm[0..(4*lf-1)]|.
  492. @<Types...@>=
  493. @!index=0..tfm_size; {address of a byte in |tfm|}
  494. @ @<Glob...@>=
  495. @!tfm:array [-1000..tfm_size] of byte; {the \.{TFM} input data all goes here}
  496.  {the negative addresses avoid range checks for invalid characters}
  497. @ The input may, of course, be all screwed up and not a \.{TFM} file
  498. at all. So we begin cautiously.
  499. @d abort(#)==begin print_ln(#);
  500.   print_ln('Sorry, but I can''t go on; are you sure this is a TFM?');
  501.   goto final_end;
  502.   end
  503. @<Read the whole \.{TFM} file@>=
  504. read(tfm_file,tfm[0]);
  505. if tfm[0]>127 then abort('The first byte of the input file exceeds 127!');
  506. @.The first byte...@>
  507. if eof(tfm_file) then abort('The input file is only one byte long!');
  508. @.The input...one byte long@>
  509. read(tfm_file,tfm[1]); lf:=tfm[0]*@'400+tfm[1];
  510. if lf=0 then
  511.   abort('The file claims to have length zero, but that''s impossible!');
  512. @.The file claims...@>
  513. if 4*lf-1>tfm_size then abort('The file is bigger than I can handle!');
  514. @.The file is bigger...@>
  515. for tfm_ptr:=2 to 4*lf-1 do
  516.   begin if eof(tfm_file) then
  517.     abort('The file has fewer bytes than it claims!');
  518. @.The file has fewer bytes...@>
  519.   read(tfm_file,tfm[tfm_ptr]);
  520.   end;
  521. if not eof(tfm_file) then
  522.   begin print_ln('There''s some extra junk at the end of the TFM file,');
  523. @.There's some extra junk...@>
  524.   print_ln('but I''ll proceed as if it weren''t there.');
  525.   end
  526. @ After the file has been read successfully, we look at the subfile sizes
  527. to see if they check out.
  528. @d eval_two_bytes(#)==begin if tfm[tfm_ptr]>127 then
  529.     abort('One of the subfile sizes is negative!');
  530. @.One of the subfile sizes...@>
  531.   #:=tfm[tfm_ptr]*@'400+tfm[tfm_ptr+1];
  532.   tfm_ptr:=tfm_ptr+2;
  533.   end
  534. @<Set subfile sizes |lh|, |bc|, \dots, |np|@>=
  535. begin tfm_ptr:=2;@/
  536. eval_two_bytes(lh);
  537. eval_two_bytes(bc);
  538. eval_two_bytes(ec);
  539. eval_two_bytes(nw);
  540. eval_two_bytes(nh);
  541. eval_two_bytes(nd);
  542. eval_two_bytes(ni);
  543. eval_two_bytes(nl);
  544. eval_two_bytes(nk);
  545. eval_two_bytes(ne);
  546. eval_two_bytes(np);
  547. if lh<2 then abort('The header length is only ',lh:1,'!');
  548. @.The header length...@>
  549. if nl>4*lig_size then
  550.   abort('The lig/kern program is longer than I can handle!');
  551. @.The lig/kern program...@>
  552. if (bc>ec+1)or(ec>255) then abort('The character code range ',
  553. @.The character code range...@>
  554.   bc:1,'..',ec:1,'is illegal!');
  555. if (nw=0)or(nh=0)or(nd=0)or(ni=0) then
  556.   abort('Incomplete subfiles for character dimensions!');
  557. @.Incomplete subfiles...@>
  558. if ne>256 then abort('There are ',ne:1,' extensible recipes!');
  559. @.There are ... recipes@>
  560. if lf<>6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np then
  561.   abort('Subfile sizes don''t add up to the stated total!');
  562. @.Subfile sizes don't add up...@>
  563. @ Once the input data successfully passes these basic checks,
  564. \.{VFtoVP} believes that it is a \.{TFM} file, and the conversion
  565. to \.{VPL} format will take place. Access to the various subfiles
  566. is facilitated by computing the following base addresses. For example,
  567. the |char_info| for character |c| will start in location
  568. |4*(char_base+c)| of the |tfm| array.
  569. @<Globals...@>=
  570. @!char_base,@!width_base,@!height_base,@!depth_base,@!italic_base,
  571. @!lig_kern_base,@!kern_base,@!exten_base,@!param_base:integer;
  572.   {base addresses for the subfiles}
  573. @ @<Compute the base addresses@>=
  574. begin char_base:=6+lh-bc;
  575. width_base:=char_base+ec+1;
  576. height_base:=width_base+nw;
  577. depth_base:=height_base+nh;
  578. italic_base:=depth_base+nd;
  579. lig_kern_base:=italic_base+ni;
  580. kern_base:=lig_kern_base+nl;
  581. exten_base:=kern_base+nk;
  582. param_base:=exten_base+ne-1;
  583. @ Of course we want to define macros that suppress the detail of how the
  584. font information is actually encoded. Each word will be referred to by
  585. the |tfm| index of its first byte. For example, if |c| is a character
  586. code between |bc| and |ec|, then |tfm[char_info(c)]| will be the
  587. first byte of its |char_info|, i.e., the |width_index|; furthermore
  588. |width(c)| will point to the |fix_word| for |c|'s width.
  589. @d check_sum=24
  590. @d design_size=check_sum+4
  591. @d scheme=design_size+4
  592. @d family=scheme+40
  593. @d random_word=family+20
  594. @d char_info(#)==4*(char_base+#)
  595. @d width_index(#)==tfm[char_info(#)]
  596. @d nonexistent(#)==((#<bc)or(#>ec)or(width_index(#)=0))
  597. @d height_index(#)==(tfm[char_info(#)+1] div 16)
  598. @d depth_index(#)==(tfm[char_info(#)+1] mod 16)
  599. @d italic_index(#)==(tfm[char_info(#)+2] div 4)
  600. @d tag(#)==(tfm[char_info(#)+2] mod 4)
  601. @d reset_tag(#)==tfm[char_info(#)+2]:=4*italic_index(#)+no_tag
  602. @d remainder(#)==tfm[char_info(#)+3]
  603. @d width(#)==4*(width_base+width_index(#))
  604. @d height(#)==4*(height_base+height_index(#))
  605. @d depth(#)==4*(depth_base+depth_index(#))
  606. @d italic(#)==4*(italic_base+italic_index(#))
  607. @d exten(#)==4*(exten_base+remainder(#))
  608. @d lig_step(#)==4*(lig_kern_base+(#))
  609. @d kern(#)==4*(kern_base+#) {here \#\ is an index, not a character}
  610. @d param(#)==4*(param_base+#) {likewise}
  611. @ One of the things we would like to do is take cognizance of fonts whose
  612. character coding scheme is \.{TeX math symbols} or \.{TeX math extension};
  613. we will set the |font_type| variable to one of the three choices
  614. |vanilla|, |mathsy|, or |mathex|.
  615. @d vanilla=0 {not a special scheme}
  616. @d mathsy=1 {\.{TeX math symbols} scheme}
  617. @d mathex=2 {\.{TeX math extension} scheme}
  618. @<Glob...@>=
  619. @!font_type:vanilla..mathex; {is this font special?}
  620. @* Unpacking the VF file.
  621. Once the \.{TFM} file has been brought into memory, \.{VFtoVP} completes
  622. the input phase by reading the \.{VF} information into another array of bytes.
  623. In this case we don't store all the data; we check the redundant bytes
  624. for consistency with their \.{TFM} counterparts, and we partially decode
  625. the packets.
  626. @<Glob...@>=
  627. @!vf:array[0..vf_size] of byte; {the \.{VF} input data goes here}
  628. @!font_number:array[0..max_fonts] of integer; {local font numbers}
  629. @!font_start,@!font_chars:array[0..max_fonts] of 0..vf_size; {font info}
  630. @!font_ptr:0..max_fonts; {number of local fonts}
  631. @!packet_start,@!packet_end:array[byte] of 0..vf_size;
  632.   {character packet boundaries}
  633. @!packet_found:boolean; {at least one packet has appeared}
  634. @!temp_byte:byte;@+@!count:integer; {registers for simple calculations}
  635. @!real_dsize:real; {the design size, converted to floating point}
  636. @!pl:integer; {packet length}
  637. @!vf_ptr:0..vf_size; {first unused location in |vf|}
  638. @!vf_count:integer; {number of bytes read from |vf_file|}
  639. @ Again we cautiously verify that we've been given decent data.
  640. @d read_vf(#)==read(vf_file,#)
  641. @d vf_abort(#)==
  642.   begin print_ln(#);
  643.   print_ln('Sorry, but I can''t go on; are you sure this is a VF?');
  644.   goto final_end;
  645.   end
  646. @<Read the whole \.{VF} file@>=
  647. read_vf(temp_byte);
  648. if temp_byte<>pre then vf_abort('The first byte isn''t `pre''!');
  649. @.The first byte...@>
  650. @<Read the preamble command@>;
  651. @<Read and store the font definitions and character packets@>;
  652. @<Read and verify the postamble@>
  653. @ @d vf_store(#)==@t@>@;@/
  654.   if vf_ptr+#>=vf_size then vf_abort('The file is bigger than I can handle!');
  655. @.The file is bigger...@>
  656.   for k:=vf_ptr to vf_ptr+#-1 do
  657.     begin if eof(vf_file) then vf_abort('The file ended prematurely!');
  658. @.The file ended prematurely@>
  659.     read_vf(vf[k]);
  660.     end;
  661.   vf_count:=vf_count+#; vf_ptr:=vf_ptr+#
  662. @<Read the preamble command@>=
  663. if eof(vf_file) then vf_abort('The input file is only one byte long!');
  664. @.The input...one byte long@>
  665. read_vf(temp_byte);
  666. if temp_byte<>id_byte then vf_abort('Wrong VF version number in second byte!');
  667. @.Wrong VF version...@>
  668. if eof(vf_file) then vf_abort('The input file is only two bytes long!');
  669. read_vf(temp_byte); {read the length of introductory comment}
  670. vf_count:=11; vf_ptr:=0; vf_store(temp_byte);
  671. for k:=0 to vf_ptr-1 do print(xchr[vf[k]]);
  672. print_ln(' '); count:=0;
  673. for k:=0 to 7 do
  674.   begin if eof(vf_file) then vf_abort('The file ended prematurely!');
  675. @.The file ended prematurely@>
  676.   read_vf(temp_byte);
  677.   if temp_byte=tfm[check_sum+k] then incr(count);
  678.   end;
  679. real_dsize:=(((tfm[design_size]*256+tfm[design_size+1])*256+tfm[design_size+2])
  680.  *256+tfm[design_size+3])/@'4000000;
  681. if count<>8 then
  682.   begin print_ln('Check sum and/or design size mismatch.');
  683. @.Check sum...mismatch@>
  684.   print_ln('Data from TFM file will be assumed correct.');
  685.   end
  686. @ @<Read and store the font definitions and character packets@>=
  687. for k:=0 to 255 do packet_start[k]:=vf_size;
  688. font_ptr:=0; packet_found:=false; font_start[0]:=vf_ptr;
  689. repeat if eof(vf_file) then
  690.   begin print_ln('File ended without a postamble!'); temp_byte:=post;
  691. @.File ended without a postamble@>
  692.   end
  693. else begin read_vf(temp_byte); incr(vf_count);
  694.   if temp_byte<>post then
  695.     if temp_byte>long_char then @<Read and store a font definition@>
  696.     else @<Read and store a character packet@>;
  697.   end;
  698. until temp_byte=post
  699. @ @<Read and verify the postamble@>=
  700. while (temp_byte=post)and not eof(vf_file) do
  701.   begin read_vf(temp_byte); incr(vf_count);
  702.   end;
  703. if not eof(vf_file) then
  704.   begin print_ln('There''s some extra junk at the end of the VF file.');
  705. @.There's some extra junk...@>
  706.   print_ln('I''ll proceed as if it weren''t there.');
  707.   end;
  708. if vf_count mod 4 <> 0 then
  709.   print_ln('VF data not a multiple of 4 bytes')
  710. @.VF data not a multiple of 4 bytes@>
  711. @ @<Read and store a font definition@>=
  712. begin if packet_found or(temp_byte>=pre) then
  713.   vf_abort('Illegal byte ',temp_byte:1,' at beginning of character packet!');
  714. @.Illegal byte...@>
  715. font_number[font_ptr]:=vf_read(temp_byte-fnt_def1+1);
  716. if font_ptr=max_fonts then vf_abort('I can''t handle that many fonts!');
  717. @.I can't handle that many fonts@>
  718. vf_store(14); {|c[4]| |s[4]| |d[4]| |a[1]| |l[1]|}
  719. if vf[vf_ptr-10]>0 then {|s| is negative or exceeds $2^{24}-1$}
  720.   vf_abort('Mapped font size is too big!');
  721. @.Mapped font size...big@>
  722. a:=vf[vf_ptr-2]; l:=vf[vf_ptr-1]; vf_store(a+l); {|n[a+l]|}
  723. @<Print the name of the local font@>;
  724. @<Read the local font's \.{TFM} file and record the characters it contains@>;
  725. incr(font_ptr); font_start[font_ptr]:=vf_ptr; 
  726. @ The font area may need to be separated from the font name on some systems.
  727. Here we simply reproduce the font area and font name (with no space
  728. or punctuation between them).
  729. @^system dependencies@>
  730. @<Print the name...@>=
  731. print('MAPFONT ',font_ptr:1,': ');
  732. for k:=font_start[font_ptr]+14 to vf_ptr-1 do print(xchr[vf[k]]);
  733. k:=font_start[font_ptr]+5;
  734. print_ln(' at ',(((vf[k]*256+vf[k+1])*256+vf[k+2])/@'4000000)*real_dsize:2:2,
  735.   'pt')
  736. @ Now we must read in another \.{TFM} file. But this time we needn't be so
  737. careful, because we merely want to discover which characters are present.
  738. The next few sections of the program are copied pretty much verbatim from
  739. \.{DVItype}, so that system-dependent modifications can be copied from existing
  740. software.
  741. It turns out to be convenient to read four bytes at a time, when we are
  742. inputting from the local \.{TFM} files. The input goes into global variables
  743. |b0|, |b1|, |b2|, and |b3|, with |b0| getting the first byte and |b3|
  744. the fourth.
  745. @<Glob...@>=
  746. @!a:integer; {length of the area/directory spec}
  747. @!l:integer; {length of the font name proper}
  748. @!cur_name:packed array[1..name_length] of char; {external name,
  749.   with no lower case letters}
  750. @!b0,@!b1,@!b2,@!b3: byte; {four bytes input at once}
  751. @!font_lh:0..@'77777; {header length of current local font}
  752. @!font_bc,@!font_ec:0..@'77777; {character range of current local font}
  753. @ The |read_tfm_word| procedure sets |b0| through |b3| to the next
  754. four bytes in the current \.{TFM} file.
  755. @^system dependencies@>
  756. @d read_tfm(#)==if eof(tfm_file) then #:=0@+else read(tfm_file,#)
  757. @p procedure read_tfm_word;
  758. begin read_tfm(b0); read_tfm(b1);
  759. read_tfm(b2); read_tfm(b3);
  760. @ We use the |vf| array to store a list of all valid characters in the
  761. local font, beginning at location |font_chars[f]|.
  762. @<Read the local font's \.{TFM} file...@>=
  763. font_chars[font_ptr]:=vf_ptr;
  764. @<Move font name into the |cur_name| string@>;
  765. reset(tfm_file,cur_name);
  766. @^system dependencies@>
  767. if eof(tfm_file) then
  768.   print_ln('---not loaded, TFM file can''t be opened!')
  769. @.TFM file can\'t be opened@>
  770. else  begin font_bc:=0; font_ec:=256; {will cause error if not modified soon}
  771.   read_tfm_word;
  772.   if b2<128 then
  773.     begin font_lh:=b2*256+b3; read_tfm_word;
  774.     if (b0<128) and (b2<128) then
  775.       begin font_bc:=b0*256+b1; font_ec:=b2*256+b3;
  776.       end;
  777.     end;
  778.   if font_bc<=font_ec then
  779.     if font_ec>255 then print_ln('---not loaded, bad TFM file!')
  780. @.bad TFM file@>
  781.     else begin for k:=0 to 3+font_lh do
  782.         begin read_tfm_word;
  783.         if k=4 then @<Check the check sum@>;
  784.         if k=5 then @<Check the design size@>;
  785.         end;
  786.       for k:=font_bc to font_ec do
  787.         begin read_tfm_word;
  788.         if b0>0 then {character |k| exists in the font}
  789.           begin vf[vf_ptr]:=k; incr(vf_ptr);
  790.           if vf_ptr=vf_size then vf_abort('I''m out of VF memory!');
  791. @.I'm out of VF memory@>
  792.           end;
  793.         end;
  794.       end;
  795.   if eof(tfm_file) then
  796.     print_ln('---trouble is brewing, TFM file ended too soon!');
  797. @.trouble is brewing...@>
  798.   end;
  799. incr(vf_ptr) {leave space for character search later}
  800. @ @<Check the check sum@>=
  801. if b0+b1+b2+b3>0 then
  802.   if(b0<>vf[font_start[font_ptr]])or@|
  803.    (b1<>vf[font_start[font_ptr]+1])or@|
  804.    (b2<>vf[font_start[font_ptr]+2])or@|
  805.    (b3<>vf[font_start[font_ptr]+3]) then
  806.     begin print_ln('Check sum in VF file being replaced by TFM check sum');
  807. @.Check sum...replaced...@>
  808.     vf[font_start[font_ptr]]:=b0;
  809.     vf[font_start[font_ptr]+1]:=b1;
  810.     vf[font_start[font_ptr]+2]:=b2;
  811.     vf[font_start[font_ptr]+3]:=b3;
  812.     end
  813. @ @<Check the design size@>=
  814. if(b0<>vf[font_start[font_ptr]+8])or@|
  815.  (b1<>vf[font_start[font_ptr]+9])or@|
  816.  (b2<>vf[font_start[font_ptr]+10])or@|
  817.  (b3<>vf[font_start[font_ptr]+11]) then
  818.   begin print_ln('Design size in VF file being replaced by TFM design size');
  819. @.Design size...replaced...@>
  820.   vf[font_start[font_ptr]+8]:=b0;
  821.   vf[font_start[font_ptr]+9]:=b1;
  822.   vf[font_start[font_ptr]+10]:=b2;
  823.   vf[font_start[font_ptr]+11]:=b3;
  824.   end
  825. @ If no font directory has been specified, \.{DVI}-reading software
  826. is supposed to use the default font directory, which is a
  827. system-dependent place where the standard fonts are kept.
  828. The string variable |default_directory| contains the name of this area.
  829. @^system dependencies@>
  830. @d default_directory_name=='TeXfonts:' {change this to the correct name}
  831. @d default_directory_name_length=9 {change this to the correct length}
  832. @<Glob...@>=
  833. @!default_directory:packed array[1..default_directory_name_length] of char;
  834. @ @<Set init...@>=
  835. default_directory:=default_directory_name;
  836. @ The string |cur_name| is supposed to be set to the external name of the
  837. \.{TFM} file for the current font. This usually means that we need to
  838. prepend the name of the default directory, and
  839. to append the suffix `\.{.TFM}'. Furthermore, we change lower case letters
  840. to upper case, since |cur_name| is a \PASCAL\ string.
  841. @^system dependencies@>
  842. @<Move font name into the |cur_name| string@>=
  843. for k:=1 to name_length do cur_name[k]:=' ';
  844. if a=0 then
  845.   begin for k:=1 to default_directory_name_length do
  846.     cur_name[k]:=default_directory[k];
  847.   r:=default_directory_name_length;
  848.   end
  849. else r:=0;
  850. for k:=font_start[font_ptr]+14 to vf_ptr-1 do
  851.   begin incr(r);
  852.   if r+4>name_length then vf_abort('Font name too long for me!');
  853. @.Font name too long for me@>
  854.   if (vf[k]>="a")and(vf[k]<="z") then
  855.       cur_name[r]:=xchr[vf[k]-@'40]
  856.   else cur_name[r]:=xchr[vf[k]];
  857.   end;
  858. cur_name[r+1]:='.'; cur_name[r+2]:='T'; cur_name[r+3]:='F'; cur_name[r+4]:='M'
  859. @ It's convenient to have a subroutine
  860. that reads a |k|-byte number from |vf_file|.
  861. @d get_vf(#)==if eof(vf_file) then #:=0 @+else read_vf(#)
  862. @p function vf_read(@!k:integer):integer; {actually |1<=k<=4|}
  863. var @!b:byte; {input byte}
  864. @!a:integer; {accumulator}
  865. begin vf_count:=vf_count+k; get_vf(b); a:=b;
  866. if k=4 then if b>=128 then a:=a-256; {4-byte numbers are signed}
  867. while k>1 do
  868.   begin get_vf(b);
  869.   a:=256*a+b; decr(k);
  870.   end;
  871. vf_read:=a;
  872. @ The \.{VF} format supports arbitrary 4-byte character codes,
  873. but \.{VPL} format presently does not.
  874. Therefore we give up if the character code is
  875. not between 0 and~255.
  876. After more experience is gained with present-day \.{VPL} files, the
  877. best way to extend them to arbitrary character codes will become clear;
  878. the extensions to \.{VFtoVP} and \.{VPtoVF} should not be difficult.
  879. @<Read and store a character packet@>=
  880. begin if temp_byte=long_char then
  881.   begin pl:=vf_read(4); c:=vf_read(4); count:=vf_read(4);
  882.     {|pl[4]| |cc[4]| |tfm[4]|}
  883.   end
  884. else begin pl:=temp_byte; c:=vf_read(1); count:=vf_read(3);
  885.     {|pl[1]| |cc[1]| |tfm[3]|}
  886.   end;
  887. if nonexistent(c) then vf_abort('Character ',c:1,' does not exist!');
  888. @.Character c does not exist@>
  889. if packet_start[c]<vf_size then
  890.   print_ln('Discarding earlier packet for character ',c:1);
  891. @.Discarding earlier packet...@>
  892. if count<>tfm_width(c) then
  893.   print_ln('Incorrect TFM width for character ',c:1,' in VF file');
  894. @.Incorrect TFM width...@>
  895. if pl<0 then vf_abort('Negative packet length!');
  896. @.Negative packet length@>
  897. packet_start[c]:=vf_ptr; vf_store(pl); packet_end[c]:=vf_ptr-1;
  898. packet_found:=true;
  899. @ The preceding code requires a simple subroutine that evaluates \.{TFM} data.
  900. @p function tfm_width(@!c:byte):integer;
  901. var @!a:integer; {accumulator}
  902. @!k:index; {index into |tfm|}
  903. begin k:=width(c); {we assume that character |c| exists}
  904. a:=tfm[k];
  905. if a>=128 then a:=a-256;
  906. tfm_width:=((256*a+tfm[k+1])*256+tfm[k+2])*256+tfm[k+3];
  907. @* Basic output subroutines.
  908. Let us now define some procedures that will reduce the rest of \.{VFtoVP}'s
  909. work to a triviality.
  910. First of all, it is convenient to have an abbreviation for output to the
  911. \.{VPL} file:
  912. @d out(#)==write(vpl_file,#)
  913. @ In order to stick to standard \PASCAL, we use an |xchr| array to do
  914. appropriate conversion of ASCII codes. Three other little strings are
  915. used to produce |face| codes like \.{MIE}.
  916. @<Glob...@>=
  917. @!ASCII_04,@!ASCII_10,@!ASCII_14: packed array [1..32] of char;
  918.   {strings for output in the user's external character set}
  919. @!xchr:packed array [0..255] of char;
  920. @!MBL_string,@!RI_string,@!RCE_string:packed array [1..3] of char;
  921.   {handy string constants for |face| codes}
  922. @ @<Set init...@>=
  923. ASCII_04:=' !"#$%&''()*+,-./0123456789:;<=>?';@/
  924. ASCII_10:='@@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_';@/
  925. ASCII_14:='`abcdefghijklmnopqrstuvwxyz{|}~?';@/
  926. for k:=0 to 255 do xchr[k]:='?';
  927. for k:=0 to @'37 do
  928.   begin xchr[k+@'40]:=ASCII_04[k+1];
  929.   xchr[k+@'100]:=ASCII_10[k+1];
  930.   xchr[k+@'140]:=ASCII_14[k+1];
  931.   end;
  932. MBL_string:='MBL'; RI_string:='RI '; RCE_string:='RCE';
  933. @ The array |dig| will hold a sequence of digits to be output.
  934. @<Glob...@>=
  935. @!dig:array[0..11] of 0..9;
  936. @ Here, in fact, are two procedures that output |dig[j-1]|$\,\ldots\,$|dig[0]|,
  937. given $j>0$.
  938. @p procedure out_digs(j:integer); {outputs |j| digits}
  939. begin repeat decr(j); out(dig[j]:1);
  940. until j=0;
  941. procedure print_digs(j:integer); {prints |j| digits}
  942. begin repeat decr(j); print(dig[j]:1);
  943. until j=0;
  944. @ The |print_octal| procedure indicates how |print_digs| can be used.
  945. Since this procedure is used only to print character codes, it always
  946. produces three digits.
  947. @p procedure print_octal(c:byte); {prints octal value of |c|}
  948. var j:0..2; {index into |dig|}
  949. begin print(''''); {an apostrophe indicates the octal notation}
  950. for j:=0 to 2 do
  951.   begin dig[j]:=c mod 8; c:=c div 8;
  952.   end;
  953. print_digs(3);
  954. @ A \.{VPL} file has nested parentheses, and we want to format the output
  955. so that its structure is clear. The |level| variable keeps track of the
  956. depth of nesting.
  957. @<Glob...@>=
  958. @!level:0..5;
  959. @ @<Set init...@>=
  960. level:=0;
  961. @ Three simple procedures suffice to produce the desired structure in the
  962. output.
  963. @p procedure out_ln; {finishes one line, indents the next}
  964. var l:0..5;
  965. begin write_ln(vpl_file);
  966. for l:=1 to level do out('   ');
  967. procedure left; {outputs a left parenthesis}
  968. begin incr(level); out('(');
  969. procedure right; {outputs a right parenthesis and finishes a line}
  970. begin decr(level); out(')'); out_ln;
  971. @ The value associated with a property can be output in a variety of
  972. ways. For example, we might want to output a {\mc BCPL} string that
  973. begins in |tfm[k]|:
  974. @p procedure out_BCPL(@!k:index); {outputs a string, preceded by a blank space}
  975. var l:0..39; {the number of bytes remaining}
  976. begin out(' '); l:=tfm[k];
  977. while l>0 do
  978.   begin incr(k); decr(l); out(xchr[tfm[k]]);
  979.   end;
  980. @ The property value might also be a sequence of |l| bytes, beginning
  981. in |tfm[k]|, that we would like to output in octal notation.
  982. The following procedure assumes that |l<=4|, but larger values of |l|
  983. could be handled easily by enlarging the |dig| array and increasing
  984. the upper bounds on |b| and |j|.
  985. @p procedure out_octal(@!k,@!l:index); {outputs |l| bytes in octal}
  986. var a:0..@'1777; {accumulator for bits not yet output}
  987. @!b:0..32; {the number of significant bits in |a|}
  988. @!j:0..11; {the number of digits of output}
  989. begin out(' O '); {specify octal format}
  990. a:=0; b:=0; j:=0;
  991. while l>0 do @<Reduce \(1)|l| by one, preserving the invariants@>;
  992. while (a>0)or(j=0) do
  993.   begin dig[j]:=a mod 8; a:=a div 8; incr(j);
  994.   end;
  995. out_digs(j);
  996. @ @<Reduce \(1)|l|...@>=
  997. begin decr(l);
  998. if tfm[k+l]<>0 then
  999.   begin while b>2 do
  1000.     begin dig[j]:=a mod 8; a:=a div 8; b:=b-3; incr(j);
  1001.     end;
  1002.   case b of
  1003.   0: a:=tfm[k+l];
  1004.   1:a:=a+2*tfm[k+l];
  1005.   2:a:=a+4*tfm[k+l];
  1006.   end;
  1007.   end;
  1008. b:=b+8;
  1009. @ The property value may be a character, which is output in octal
  1010. unless it is a letter or a digit.
  1011. @^system dependencies@>
  1012. @p procedure out_char(@!c:byte); {outputs a character}
  1013. begin if font_type>vanilla then
  1014.   begin tfm[0]:=c; out_octal(0,1)
  1015.   end
  1016. else if ((c>="0")and(c<="9"))or@|
  1017.    ((c>="A")and(c<="Z"))or@|
  1018.    ((c>="a")and(c<="z")) then out(' C ',xchr[c])
  1019. else begin tfm[0]:=c; out_octal(0,1);
  1020.   end;
  1021. @ The property value might be a ``face'' byte, which is output in the
  1022. curious code mentioned earlier, provided that it is less than 18.
  1023. @p procedure out_face(@!k:index); {outputs a |face|}
  1024. var s:0..1; {the slope}
  1025. @!b:0..8; {the weight and expansion}
  1026. begin if tfm[k]>=18 then out_octal(k,1)
  1027. else  begin out(' F ');  {specify face-code format}
  1028.   s:=tfm[k] mod 2; b:=tfm[k] div 2;
  1029.   out(MBL_string[1+(b mod 3)]);
  1030.   out(RI_string[1+s]);
  1031.   out(RCE_string[1+(b div 3)]);
  1032.   end;
  1033. @ And finally, the value might be a |fix_word|, which is output in
  1034. decimal notation with just enough decimal places for \.{VPtoVF}
  1035. to recover every bit of the given |fix_word|.
  1036. All of the numbers involved in the intermediate calculations of
  1037. this procedure will be nonnegative and less than $10\cdot2^{24}$.
  1038. @p procedure out_fix(@!k:index); {outputs a |fix_word|}
  1039. var a:0..@'7777; {accumulator for the integer part}
  1040. @!f:integer; {accumulator for the fraction part}
  1041. @!j:0..12; {index into |dig|}
  1042. @!delta:integer; {amount if allowable inaccuracy}
  1043. begin out(' R '); {specify real format}
  1044. a:=(tfm[k]*16)+(tfm[k+1] div 16);
  1045. f:=((tfm[k+1] mod 16)*@'400+tfm[k+2])*@'400+tfm[k+3];
  1046. if a>@'3777 then @<Reduce \(2)negative to positive@>;
  1047. @<Output the integer part, |a|, in decimal notation@>;
  1048. @<Output the fraction part, $|f|/2^{20}$, in decimal notation@>;
  1049. @ The following code outputs at least one digit even if |a=0|.
  1050. @<Output the integer...@>=
  1051. begin j:=0;
  1052. repeat dig[j]:=a mod 10; a:=a div 10; incr(j);
  1053. until a=0;
  1054. out_digs(j);
  1055. @ And the following code outputs at least one digit to the right
  1056. of the decimal point.
  1057. @<Output the fraction...@>=
  1058. begin out('.'); f:=10*f+5; delta:=10;
  1059. repeat if delta>@'4000000 then f:=f+@'2000000-(delta div 2);
  1060. out(f div @'4000000:1); f:=10*(f mod @'4000000); delta:=delta*10;
  1061. until f<=delta;
  1062. @ @<Reduce \(2)negative to positive@>=
  1063. begin out('-'); a:=@'10000-a;
  1064. if f>0 then
  1065.   begin f:=@'4000000-f; decr(a);
  1066.   end;
  1067. @* Outputting the TFM info.
  1068. \TeX\ checks the information of a \.{TFM} file for validity as the
  1069. file is being read in, so that no further checks will be needed when
  1070. typesetting is going on. And when it finds something wrong, it justs
  1071. calls the file ``bad,'' without identifying the nature of the problem,
  1072. since \.{TFM} files are supposed to be good almost all of the time.
  1073. Of course, a bad file shows up every now and again, and that's where
  1074. \.{VFtoVP} comes in. This program wants to catch at least as many errors as
  1075. \TeX\ does, and to give informative error messages besides.
  1076. All of the errors are corrected, so that the \.{VPL} output will
  1077. be correct (unless, of course, the \.{TFM} file was so loused up
  1078. that no attempt is being made to fathom it).
  1079. @ Just before each character is processed, its code is printed in octal
  1080. notation. Up to eight such codes appear on a line; so we have a variable
  1081. to keep track of how many are currently there. We also keep track of
  1082. whether or not any errors have had to be corrected.
  1083. @<Glob...@>=
  1084. @!chars_on_line:0..8; {the number of characters printed on the current line}
  1085. @!perfect:boolean; {was the file free of errors?}
  1086. @ @<Set init...@>=
  1087. chars_on_line:=0;@/
  1088. perfect:=true; {innocent until proved guilty}
  1089. @ Error messages are given with the help of the |bad| and |range_error|
  1090. and |bad_char| macros:
  1091. @d bad(#)==begin perfect:=false; if chars_on_line>0 then print_ln(' ');
  1092.   chars_on_line:=0; print_ln('Bad TFM file: ',#);
  1093.   end
  1094. @.Bad TFM file@>
  1095. @d range_error(#)==begin perfect:=false; print_ln(' ');
  1096.   print(#,' index for character ');
  1097.   print_octal(c); print_ln(' is too large;');
  1098.   print_ln('so I reset it to zero.');
  1099.   end
  1100. @d bad_char_tail(#)==print_octal(#); print_ln('.');
  1101.   end
  1102. @d bad_char(#)==begin perfect:=false; if chars_on_line>0 then print_ln(' ');
  1103.   chars_on_line:=0; print('Bad TFM file: ',#,' nonexistent character ');
  1104.   bad_char_tail
  1105. @d correct_bad_char_tail(#)==print_octal(tfm[#]); print_ln('.'); tfm[#]:=bc;
  1106.   end
  1107. @d correct_bad_char(#)== begin perfect:=false;
  1108.   if chars_on_line>0 then print_ln(' ');
  1109.   chars_on_line:=0; print('Bad TFM file: ',#,' nonexistent character ');
  1110.   correct_bad_char_tail
  1111. @<Glob...@>=
  1112. @!i:0..@'77777; {an index to words of a subfile}
  1113. @!c:0..256; {a random character}
  1114. @!d:0..3; {byte number in a word}
  1115. @!k:index; {a random index}
  1116. @!r:0..65535; {a random two-byte value}
  1117. @ There are a lot of simple things to do, and they have to be done one
  1118. at a time, so we might as well get down to business.  The first things
  1119. that \.{VFtoVP} will put into the \.{VPL} file appear in the header part.
  1120. @<Do the header@>=
  1121. begin font_type:=vanilla;
  1122. if lh>=12 then
  1123.   begin @<Set the true |font_type|@>;
  1124.   if lh>=17 then
  1125.     begin @<Output the family name@>;
  1126.     if lh>=18 then @<Output the rest of the header@>;
  1127.     end;
  1128.   @<Output the character coding scheme@>;
  1129.   end;
  1130. @<Output the design size@>;
  1131. @<Output the check sum@>;
  1132. @<Output the |seven_bit_safe_flag|@>;
  1133. @ @<Output the check sum@>=
  1134. left; out('CHECKSUM'); out_octal(check_sum,4);
  1135. right
  1136. @ Incorrect design sizes are changed to 10 points.
  1137. @d bad_design(#)==begin bad('Design size ',#,'!');
  1138. @.Design size wrong@>
  1139.   print_ln('I''ve set it to 10 points.');
  1140.   out(' D 10');
  1141.   end
  1142. @ @<Output the design size@>=
  1143. left; out('DESIGNSIZE');
  1144. if tfm[design_size]>127 then bad_design('negative')
  1145. else if (tfm[design_size]=0)and(tfm[design_size+1]<16) then
  1146.   bad_design('too small')
  1147. else out_fix(design_size);
  1148. right;
  1149. out('(COMMENT DESIGNSIZE IS IN POINTS)'); out_ln;
  1150. out('(COMMENT OTHER SIZES ARE MULTIPLES OF DESIGNSIZE)'); out_ln
  1151. @.DESIGNSIZE IS IN POINTS@>
  1152. @ Since we have to check two different {\mc BCPL} strings for validity,
  1153. we might as well write a subroutine to make the check.
  1154. @p procedure check_BCPL(@!k,@!l:index); {checks a string of length |<l|}
  1155. var j:index; {runs through the string}
  1156. @!c:byte; {character being checked}
  1157. begin if tfm[k]>=l then
  1158.   begin bad('String is too long; I''ve shortened it drastically.');
  1159. @.String is too long...@>
  1160.   tfm[k]:=1;
  1161.   end;
  1162. for j:=k+1 to k+tfm[k] do
  1163.   begin c:=tfm[j];
  1164.   if (c="(")or(c=")") then
  1165.     begin bad('Parenthesis in string has been changed to slash.');
  1166. @.Parenthesis...changed to slash@>
  1167.     tfm[j]:="/";
  1168.     end
  1169.   else if (c<" ")or(c>"~") then
  1170.     begin bad('Nonstandard ASCII code has been blotted out.');
  1171. @.Nonstandard ASCII code...@>
  1172.     tfm[j]:="?";
  1173.     end
  1174.   else if (c>="a")and(c<="z") then tfm[j]:=c+"A"-"a"; {upper-casify letters}
  1175.   end;
  1176. @ The |font_type| starts out |vanilla|; possibly we need to reset it.
  1177. @<Set the true |font_type|@>=
  1178. begin check_BCPL(scheme,40);
  1179. if (tfm[scheme]>=11)and@|(tfm[scheme+1]="T")and@|
  1180.   (tfm[scheme+2]="E")and@|(tfm[scheme+3]="X")and@|
  1181.   (tfm[scheme+4]=" ")and@|(tfm[scheme+5]="M")and@|
  1182.   (tfm[scheme+6]="A")and@|(tfm[scheme+7]="T")and@|
  1183.   (tfm[scheme+8]="H")and@|(tfm[scheme+9]=" ") then
  1184.   begin if (tfm[scheme+10]="S")and(tfm[scheme+11]="Y") then font_type:=mathsy
  1185.   else if (tfm[scheme+10]="E")and(tfm[scheme+11]="X") then font_type:=mathex;
  1186.   end;
  1187. @ @<Output the character coding scheme@>=
  1188. left; out('CODINGSCHEME');
  1189. out_BCPL(scheme);
  1190. right
  1191. @ @<Output the family name@>=
  1192. left; out('FAMILY');
  1193. check_BCPL(family,20);
  1194. out_BCPL(family);
  1195. right
  1196. @ @<Output the rest of the header@>=
  1197. begin left; out('FACE'); out_face(random_word+3); right;
  1198. for i:=18 to lh-1 do
  1199.   begin left; out('HEADER D ',i:1);
  1200.   out_octal(check_sum+4*i,@,4); right;
  1201.   end;
  1202. @ This program does not check to see if the |seven_bit_safe_flag| has the
  1203. correct setting, i.e., if it really reflects the seven-bit-safety of
  1204. the \.{TFM} file; the stated value is merely put into the \.{VPL} file.
  1205. The \.{VPtoVF} program will store a correct value and give a warning
  1206. message if a file falsely claims to be safe.
  1207. @<Output the |seven_bit_safe_flag|@>=
  1208. if (lh>17) and (tfm[random_word]>127) then
  1209.   begin left; out('SEVENBITSAFEFLAG TRUE'); right;
  1210.   end
  1211. @ The next thing to take care of is the list of parameters.
  1212. @<Do the parameters@>=
  1213. if np>0 then
  1214.   begin left; out('FONTDIMEN'); out_ln;
  1215.   for i:=1 to np do @<Check and output the $i$th parameter@>;
  1216.   right;
  1217.   end;
  1218. @<Check to see if |np| is complete for this font type@>;
  1219. @ @<Check to see if |np|...@>=
  1220. if (font_type=mathsy)and(np<>22) then
  1221.   print_ln('Unusual number of fontdimen parameters for a math symbols font (',
  1222. @.Unusual number of fontdimen...@>
  1223.     np:1,' not 22).')
  1224. else if (font_type=mathex)and(np<>13) then
  1225.   print_ln('Unusual number of fontdimen parameters for an extension font (',
  1226.     np:1,' not 13).')
  1227. @ All |fix_word| values except the design size and the first parameter
  1228. will be checked to make sure that they are less than 16.0 in magnitude,
  1229. using the |check_fix| macro:
  1230. @d check_fix_tail(#)==bad(#,' ',i:1,' is too big;');
  1231.   print_ln('I have set it to zero.');
  1232.   end
  1233. @d check_fix(#)==if (tfm[#]>0)and(tfm[#]<255) then
  1234.   begin tfm[#]:=0; tfm[(#)+1]:=0; tfm[(#)+2]:=0; tfm[(#)+3]:=0;
  1235.   check_fix_tail
  1236. @<Check and output the $i$th parameter@>=
  1237. begin left;
  1238. if i=1 then out('SLANT') {this parameter is not checked}
  1239. else  begin check_fix(param(i))('Parameter');@/
  1240. @.Parameter n is too big@>
  1241.   @<Output the name of parameter $i$@>;
  1242.   end;
  1243. out_fix(param(i)); right;
  1244. @ @<Output the name...@>=
  1245. if i<=7 then case i of
  1246.   2:out('SPACE');@+3:out('STRETCH');@+4:out('SHRINK');
  1247.   5:out('XHEIGHT');@+6:out('QUAD');@+7:out('EXTRASPACE')@+end
  1248. else if (i<=22)and(font_type=mathsy) then case i of
  1249.   8:out('NUM1');@+9:out('NUM2');@+10:out('NUM3');
  1250.   11:out('DENOM1');@+12:out('DENOM2');
  1251.   13:out('SUP1');@+14:out('SUP2');@+15:out('SUP3');
  1252.   16:out('SUB1');@+17:out('SUB2');
  1253.   18:out('SUPDROP');@+19:out('SUBDROP');
  1254.   20:out('DELIM1');@+21:out('DELIM2');
  1255.   22:out('AXISHEIGHT')@+end
  1256. else if (i<=13)and(font_type=mathex) then
  1257.   if i=8 then out('DEFAULTRULETHICKNESS')
  1258.   else out('BIGOPSPACING',i-8:1)
  1259. else out('PARAMETER D ',i:1)
  1260. @ We need to check the range of all the remaining |fix_word| values,
  1261. and to make sure that |width[0]=0|, etc.
  1262. @d nonzero_fix(#)==(tfm[#]>0)or(tfm[#+1]>0)or(tfm[#+2]>0)or(tfm[#+3]>0)
  1263. @<Check the |fix_word| entries@>=
  1264. if nonzero_fix(4*width_base) then bad('width[0] should be zero.');
  1265. @.should be zero@>
  1266. if nonzero_fix(4*height_base) then bad('height[0] should be zero.');
  1267. if nonzero_fix(4*depth_base) then bad('depth[0] should be zero.');
  1268. if nonzero_fix(4*italic_base) then bad('italic[0] should be zero.');
  1269. for i:=0 to nw-1 do check_fix(4*(width_base+i))('Width');
  1270. @.Width n is too big@>
  1271. for i:=0 to nh-1 do check_fix(4*(height_base+i))('Height');
  1272. @.Height n is too big@>
  1273. for i:=0 to nd-1 do check_fix(4*(depth_base+i))('Depth');
  1274. @.Depth n is too big@>
  1275. for i:=0 to ni-1 do check_fix(4*(italic_base+i))('Italic correction');
  1276. @.Italic correction n is too big@>
  1277. if nk>0 then for i:=0 to nk-1 do check_fix(kern(i))('Kern');
  1278. @.Kern n is too big@>
  1279. @ The ligature/kerning program comes next. Before we can put it out in
  1280. \.{VPL} format, we need to make a table of ``labels'' that will be inserted
  1281. into the program. For each character |c| whose |tag| is |lig_tag| and
  1282. whose starting address is |r|, we will store the pair |(c,r)| in the
  1283. |label_table| array. If there's a boundary-char program starting at~|r|,
  1284. we also store the pair |(256,r)|.
  1285. This array is sorted by its second components, using the
  1286. simple method of straight insertion.
  1287. @<Glob...@>=
  1288. @!label_table:array[0..258] of record@t@>@/@!cc:0..256;@!rr:0..lig_size;end;
  1289. @!label_ptr: 0..257; {the largest entry in |label_table|}
  1290. @!sort_ptr:0..257; {index into |label_table|}
  1291. @!boundary_char:0..256; {boundary character, or 256 if none}
  1292. @!bchar_label:0..@'77777; {beginning of boundary character program}
  1293. @ @<Set init...@>=
  1294. boundary_char:=256; bchar_label:=@'77777;@/
  1295. label_ptr:=0; label_table[0].rr:=0; {a sentinel appears at the bottom}
  1296. @ We'll also identify and remove inaccessible program steps, using the
  1297. |activity| array.
  1298. @d unreachable=0 {a program step not known to be reachable}
  1299. @d pass_through=1 {a program step passed through on initialization}
  1300. @d accessible=2 {a program step that can be relevant}
  1301. @<Glob...@>=
  1302. @!activity:array[0..lig_size] of unreachable..accessible;
  1303. @!ai,@!acti:0..lig_size; {indices into |activity|}
  1304. @ @<Do the ligatures and kerns@>=
  1305. if nl>0 then
  1306.   begin for ai:=0 to nl-1 do activity[ai]:=unreachable;
  1307.   @<Check for a boundary char@>;
  1308.   end;
  1309. @<Build the label table@>;
  1310. if nl>0 then
  1311.   begin left; out('LIGTABLE'); out_ln;@/
  1312.   @<Compute the |activity| array@>;
  1313.   @<Output and correct the ligature/kern program@>;
  1314.   right;
  1315.   @<Check for ligature cycles@>;
  1316.   end
  1317. @ We build the label table even when |nl=0|, because this catches errors
  1318. that would not otherwise be detected.
  1319. @<Build...@>=
  1320. for c:=bc to ec do if tag(c)=lig_tag then
  1321.   begin r:=remainder(c);
  1322.   if r<nl then
  1323.     begin if tfm[lig_step(r)]>stop_flag then
  1324.       begin r:=256*tfm[lig_step(r)+2]+tfm[lig_step(r)+3];
  1325.       if r<nl then if activity[remainder(c)]=unreachable then
  1326.         activity[remainder(c)]:=pass_through;
  1327.       end;
  1328.     end;
  1329.   if r>=nl then
  1330.     begin perfect:=false; print_ln(' ');
  1331.     print('Ligature/kern starting index for character '); print_octal(c);
  1332.     print_ln(' is too large;'); print_ln('so I removed it.'); reset_tag(c);
  1333. @.Ligature/kern starting index...@>
  1334.     end
  1335.   else @<Insert |(c,r)| into |label_table|@>;
  1336.   end;
  1337. label_table[label_ptr+1].rr:=lig_size; {put ``infinite'' sentinel at the end}
  1338. @ @<Insert |(c,r)|...@>=
  1339. begin sort_ptr:=label_ptr; {there's a hole at position |sort_ptr+1|}
  1340. while label_table[sort_ptr].rr>r do
  1341.   begin label_table[sort_ptr+1]:=label_table[sort_ptr];
  1342.   decr(sort_ptr); {move the hole}
  1343.   end;
  1344. label_table[sort_ptr+1].cc:=c;
  1345. label_table[sort_ptr+1].rr:=r; {fill the hole}
  1346. incr(label_ptr); activity[r]:=accessible;
  1347. @ @<Check for a bound...@>=
  1348. if tfm[lig_step(0)]=255 then
  1349.   begin left; out('BOUNDARYCHAR');
  1350.   boundary_char:=tfm[lig_step(0)+1]; out_char(boundary_char); right;
  1351.   activity[0]:=pass_through;
  1352.   end;
  1353. if tfm[lig_step(nl-1)]=255 then
  1354.   begin r:=256*tfm[lig_step(nl-1)+2]+tfm[lig_step(nl-1)+3];
  1355.   if r>=nl then
  1356.     begin perfect:=false; print_ln(' ');
  1357.     print('Ligature/kern starting index for boundarychar is too large;');
  1358.     print_ln('so I removed it.');
  1359. @.Ligature/kern starting index...@>
  1360.     end
  1361.   else begin label_ptr:=1; label_table[1].cc:=256; label_table[1].rr:=r;
  1362.     bchar_label:=r; activity[r]:=accessible;
  1363.     end;
  1364.   activity[nl-1]:=pass_through;
  1365.   end
  1366. @ @<Compute the |activity| array@>=
  1367. for ai:=0 to nl-1 do if activity[ai]=accessible then
  1368.   begin r:=tfm[lig_step(ai)];
  1369.   if r<stop_flag then
  1370.     begin r:=r+ai+1;
  1371.     if r>=nl then
  1372.       begin bad('Ligature/kern step ',ai:1,' skips too far;');
  1373. @.Lig...skips too far@>
  1374.       print_ln('I made it stop.'); tfm[lig_step(ai)]:=stop_flag;
  1375.       end
  1376.     else activity[r]:=accessible;
  1377.     end;
  1378.   end
  1379. @ We ignore |pass_through| items, which don't need to be mentioned in
  1380. the \.{VPL} file.
  1381. @<Output and correct the ligature...@>=
  1382. sort_ptr:=1; {point to the next label that will be needed}
  1383. for acti:=0 to nl-1 do if activity[acti]<>pass_through then
  1384.   begin i:=acti; @<Take care of commenting out unreachable steps@>;
  1385.   @<Output any labels for step $i$@>;
  1386.   @<Output step $i$ of the ligature/kern program@>;
  1387.   end;
  1388. if level=2 then right {the final step was unreachable}
  1389. @ @<Output any labels...@>=
  1390. while i=label_table[sort_ptr].rr do
  1391.   begin left; out('LABEL');
  1392.   if label_table[sort_ptr].cc=256 then out(' BOUNDARYCHAR')
  1393.   else out_char(label_table[sort_ptr].cc);
  1394.   right; incr(sort_ptr);
  1395.   end
  1396. @ @<Take care of commenting out...@>=
  1397. if activity[i]=unreachable then
  1398.   begin if level=1 then
  1399.     begin left; out('COMMENT THIS PART OF THE PROGRAM IS NEVER USED!'); out_ln;
  1400.     end
  1401.   end
  1402. else if level=2 then right
  1403. @ @<Output step $i$...@>=
  1404. begin k:=lig_step(i);
  1405. if tfm[k]>stop_flag then
  1406.   begin if 256*tfm[k+2]+tfm[k+3]>=nl then
  1407.     bad('Ligature unconditional stop command address is too big.');
  1408. @.Ligature unconditional stop...@>
  1409.   end
  1410. else if tfm[k+2]>=kern_flag then @<Output a kern step@>
  1411. else @<Output a ligature step@>;
  1412. if tfm[k]>0 then
  1413.   if level=1 then @<Output either \.{SKIP} or \.{STOP}@>;
  1414. @ The \.{SKIP} command is a bit tricky, because we will be omitting all
  1415. inaccessible commands.
  1416. @<Output either...@>=
  1417. begin if tfm[k]>=stop_flag then out('(STOP)')
  1418. else begin count:=0;
  1419.   for ai:=i+1 to i+tfm[k] do if activity[ai]=accessible then incr(count);
  1420.   out('(SKIP D ',count:1,')'); {possibly $count=0$, so who cares}
  1421.   end;
  1422. out_ln;
  1423. @ @<Output a kern step@>=
  1424. begin if nonexistent(tfm[k+1]) then if tfm[k+1]<>boundary_char then
  1425.   correct_bad_char('Kern step for')(k+1);
  1426. @.Kern step for nonexistent...@>
  1427. left; out('KRN'); out_char(tfm[k+1]);
  1428. r:=256*(tfm[k+2]-kern_flag)+tfm[k+3];
  1429. if r>=nk then
  1430.   begin bad('Kern index too large.');
  1431. @.Kern index too large@>
  1432.   out(' R 0.0');
  1433.   end
  1434. else out_fix(kern(r));
  1435. right;
  1436. @ @<Output a ligature step@>=
  1437. begin if nonexistent(tfm[k+1]) then if tfm[k+1]<>boundary_char then
  1438.   correct_bad_char('Ligature step for')(k+1);
  1439. @.Ligature step for nonexistent...@>
  1440. if nonexistent(tfm[k+3]) then
  1441.   correct_bad_char('Ligature step produces the')(k+3);
  1442. @.Ligature step produces...@>
  1443. left; r:=tfm[k+2];
  1444. if (r=4)or((r>7)and(r<>11)) then
  1445.   begin print_ln('Ligature step with nonstandard code changed to LIG');
  1446.   r:=0; tfm[k+2]:=0;
  1447.   end;
  1448. if r mod 4>1 then out('/');
  1449. out('LIG');
  1450. if odd(r) then out('/');
  1451. while r>3 do
  1452.   begin out('>'); r:=r-4;
  1453.   end;
  1454. out_char(tfm[k+1]); out_char(tfm[k+3]); right;
  1455. @ The last thing on \.{VFtoVP}'s agenda is to go through the
  1456. list of |char_info| and spew out the information about each individual
  1457. character.
  1458. @<Do the characters@>=
  1459. sort_ptr:=0; {this will suppress `\.{STOP}' lines in ligature comments}
  1460. for c:=bc to ec do if width_index(c)>0 then
  1461.   begin if chars_on_line=8 then
  1462.     begin print_ln(' '); chars_on_line:=1;
  1463.     end
  1464.   else  begin if chars_on_line>0 then print(' ');
  1465.     incr(chars_on_line);
  1466.     end;
  1467.   print_octal(c); {progress report}
  1468.   left; out('CHARACTER'); out_char(c); out_ln;
  1469.   @<Output the character's width@>;
  1470.   if height_index(c)>0 then @<Output the character's height@>;
  1471.   if depth_index(c)>0 then @<Output the character's depth@>;
  1472.   if italic_index(c)>0 then @<Output the italic correction@>;
  1473.   case tag(c) of
  1474.   no_tag: do_nothing;
  1475.   lig_tag: @<Output the applicable part of the ligature/kern
  1476.     program as a comment@>;
  1477.   list_tag: @<Output the character link unless there is a problem@>;
  1478.   ext_tag: @<Output an extensible character recipe@>;
  1479.   end;@/
  1480.   if not do_map(c) then goto final_end;
  1481.   right;
  1482.   end
  1483. @ @<Output the character's width@>=
  1484. begin left; out('CHARWD');
  1485. if width_index(c)>=nw then range_error('Width')
  1486. else out_fix(width(c));
  1487. right;
  1488. @ @<Output the character's height@>=
  1489. if height_index(c)>=nh then range_error('Height')
  1490. @.Height index for char...@>
  1491. else  begin left; out('CHARHT'); out_fix(height(c)); right;
  1492.   end
  1493. @ @<Output the character's depth@>=
  1494. if depth_index(c)>=nd then range_error('Depth')
  1495. @.Depth index for char@>
  1496. else  begin left; out('CHARDP'); out_fix(depth(c)); right;
  1497.   end
  1498. @ @<Output the italic correction@>=
  1499. if italic_index(c)>=ni then range_error('Italic correction')
  1500. @.Italic correction index for char...@>
  1501. else  begin left; out('CHARIC'); out_fix(italic(c)); right;
  1502.   end
  1503. @ @<Output the applicable part of the ligature...@>=
  1504. begin left; out('COMMENT'); out_ln;@/
  1505. i:=remainder(c); r:=lig_step(i);
  1506. if tfm[r]>stop_flag then i:=256*tfm[r+2]+tfm[r+3];
  1507. repeat @<Output step...@>;
  1508. if tfm[k]>=stop_flag then i:=nl
  1509. else i:=i+1+tfm[k];
  1510. until i>=nl;
  1511. right;
  1512. @ We want to make sure that there is no cycle of characters linked together
  1513. by |list_tag| entries, since such a cycle would get \TeX\ into an endless
  1514. loop. If such a cycle exists, the routine here detects it when processing
  1515. the largest character code in the cycle.
  1516. @<Output the character link unless there is a problem@>=
  1517. begin r:=remainder(c);
  1518. if nonexistent(r) then
  1519.   begin bad_char('Character list link to')(r); reset_tag(c);
  1520. @.Character list link...@>
  1521.   end
  1522. else  begin while (r<c)and(tag(r)=list_tag) do r:=remainder(r);
  1523.   if r=c then
  1524.     begin bad('Cycle in a character list!');
  1525. @.Cycle in a character list@>
  1526.     print('Character '); print_octal(c);
  1527.     print_ln(' now ends the list.');
  1528.     reset_tag(c);
  1529.     end
  1530.   else  begin left; out('NEXTLARGER'); out_char(remainder(c));
  1531.     right;
  1532.     end;
  1533.   end;
  1534. @ @<Output an extensible character recipe@>=
  1535. if remainder(c)>=ne then
  1536.   begin range_error('Extensible'); reset_tag(c);
  1537. @.Extensible index for char@>
  1538.   end
  1539. else  begin left; out('VARCHAR'); out_ln;
  1540.   @<Output the extensible pieces that exist@>;
  1541.   right;
  1542.   end
  1543. @ @<Output the extensible pieces that...@>=
  1544. for k:=0 to 3 do if (k=3)or(tfm[exten(c)+k]>0) then
  1545.   begin left;
  1546.   case k of
  1547.   0:out('TOP');@+1:out('MID');@+2:out('BOT');@+3:out('REP')@+end;
  1548.   if nonexistent(tfm[exten(c)+k]) then out_char(c)
  1549.   else out_char(tfm[exten(c)+k]);
  1550.   right;
  1551.   end
  1552. @ Some of the extensible recipes may not actually be used, but \TeX\ will
  1553. complain about them anyway if they refer to nonexistent characters.
  1554. Therefore \.{VFtoVP} must check them too.
  1555. @<Check the extensible recipes@>=
  1556. if ne>0 then for c:=0 to ne-1 do for d:=0 to 3 do
  1557.   begin k:=4*(exten_base+c)+d;
  1558.   if (tfm[k]>0)or(d=3) then
  1559.     begin if nonexistent(tfm[k]) then
  1560.       begin bad_char('Extensible recipe involves the')(tfm[k]);
  1561. @.Extensible recipe involves...@>
  1562.       if d<3 then tfm[k]:=0;
  1563.       end;
  1564.     end;
  1565.   end
  1566. @* Checking for ligature loops.
  1567. We have programmed almost everything but the most interesting calculation of
  1568. all, which has been saved for last as a special treat. \TeX's extended ligature
  1569. mechanism allows unwary users to specify sequences of ligature replacements
  1570. that never terminate. For example, the pair of commands
  1571. $$\.{(/LIG $x$ $y$) (/LIG $y$ $x$)}$$
  1572. alternately replaces character $x$ by character $y$ and vice versa. A similar
  1573. loop occurs if \.{(LIG/ $z$ $y$)} occurs in the program for $x$ and
  1574.  \.{(LIG/ $z$ $x$)} occurs in the program for $y$.
  1575. More complicated loops are also possible. For example, suppose the ligature
  1576. programs for $x$ and $y$ are
  1577. $$\vcenter{\halign{#\hfil\cr
  1578. \.{(LABEL $x$)(/LIG/ $z$ $w$)(/LIG/> $w$ $y$)} \dots,\cr
  1579. \.{(LABEL $y$)(LIG $w$ $x$)} \dots;\cr}}$$
  1580. then the adjacent characters $xz$ change to $xwz$, $xywz$, $xxz$, $xxwz$,
  1581. \dots, ad infinitum.
  1582. @ To detect such loops, \.{VFtoVP} attempts to evaluate the function
  1583. $f(x,y)$ for all character pairs $x$ and~$y$, where $f$ is defined as
  1584. follows: If the current character is $x$ and the next character is
  1585. $y$, we say the ``cursor'' is between $x$ and $y$; when the cursor
  1586. first moves past $y$, the character immediately to its left is
  1587. $f(x,y)$. This function is defined if and only if no infinite loop is
  1588. generated when the cursor is between $x$ and~$y$.
  1589. The function $f(x,y)$ can be defined recursively. It turns out that all pairs
  1590. $(x,y)$ belong to one of five classes. The simplest class has $f(x,y)=y$; this
  1591. happens if there's no ligature between $x$ and $y$, or in the cases
  1592. \.{LIG/>} and \.{/LIG/>>}. Another simple class arises when there's a
  1593. \.{LIG} or \.{/LIG>} between $x$ and~$y$, generating the character~$z$;
  1594. then $f(x,y)=z$. Otherwise we always have $f(x,y)$ equal to
  1595. either $f(x,z)$ or $f(z,y)$ or $f(f(x,z),y)$, where $z$ is the inserted
  1596. ligature character.
  1597. The first two of these classes can be merged; we can also consider
  1598. $(x,y)$ to belong to the simple class when $f(x,y)$ has been evaluated.
  1599. For technical reasons we allow $x$ to be 256 (for the boundary character
  1600. at the left) or 257 (in cases when an error has been detected).
  1601. For each pair $(x,y)$ having a ligature program step, we store
  1602. $(x,y)$ in a hash table from which the values $z$ and $class$ can be read.
  1603. @d simple=0 {$f(x,y)=z$}
  1604. @d left_z=1 {$f(x,y)=f(z,y)$}
  1605. @d right_z=2 {$f(x,y)=f(x,z)$}
  1606. @d both_z=3 {$f(x,y)=f(f(x,z),y)$}
  1607. @d pending=4 {$f(x,y)$ is being evaluated}
  1608. @<Glob...@>=
  1609. @!hash:array[0..hash_size] of 0..66048; {$256x+y+1$ for $x\le257$ and $y\le255$}
  1610. @!class:array[0..hash_size] of simple..pending;
  1611. @!lig_z:array[0..hash_size] of 0..257;
  1612. @!hash_ptr:0..hash_size; {the number of nonzero entries in |hash|}
  1613. @!hash_list:array[0..hash_size] of 0..hash_size; {list of those nonzero entries}
  1614. @!h,@!hh:0..hash_size; {indices into the hash table}
  1615. @!x_lig_cycle,@!y_lig_cycle:0..256; {problematic ligature pair}
  1616. @ @<Check for ligature cycles@>=
  1617. hash_ptr:=0; y_lig_cycle:=256;
  1618. for hh:=0 to hash_size do hash[hh]:=0; {clear the hash table}
  1619. for c:=bc to ec do if tag(c)=lig_tag then
  1620.   begin i:=remainder(c);
  1621.   if tfm[lig_step(i)]>stop_flag then
  1622.     i:=256*tfm[lig_step(i)+2]+tfm[lig_step(i)+3];
  1623.   @<Enter data for character $c$ starting at location |i| in the hash table@>;
  1624.   end;
  1625. if bchar_label<nl then
  1626.   begin c:=256; i:=bchar_label;
  1627.   @<Enter data for character $c$ starting at location |i| in the hash table@>;
  1628.   end;
  1629. if hash_ptr=hash_size then
  1630.   begin print_ln('Sorry, I haven''t room for so many ligature/kern pairs!');
  1631. @.Sorry, I haven't room...@>
  1632.   goto final_end;
  1633.   end;
  1634. for hh:=1 to hash_ptr do
  1635.   begin r:=hash_list[hh];
  1636.   if class[r]>simple then {make sure $f$ is defined}
  1637.      r:=f(r,(hash[r]-1)div 256,(hash[r]-1)mod 256);
  1638.   end;
  1639. if y_lig_cycle<256 then
  1640.   begin  print('Infinite ligature loop starting with ');
  1641. @.Infinite ligature loop...@>
  1642.   if x_lig_cycle=256 then print('boundary')@+else print_octal(x_lig_cycle);
  1643.   print(' and '); print_octal(y_lig_cycle); print_ln('!');
  1644.   out('(INFINITE LIGATURE LOOP MUST BE BROKEN!)'); goto final_end;
  1645.   end
  1646. @ @<Enter data for character $c$...@>=
  1647. repeat hash_input; k:=tfm[lig_step(i)];
  1648. if k>=stop_flag then i:=nl
  1649. else i:=i+1+k;
  1650. until i>=nl
  1651. @ We use an ``ordered hash table'' with linear probing, because such a table
  1652. is efficient when the lookup of a random key tends to be unsuccessful.
  1653. @p procedure hash_input; {enter data for character |c| and command |i|}
  1654. label exit;
  1655. var @!cc:simple..both_z; {class of data being entered}
  1656. @!zz:0..255; {function value or ligature character being entered}
  1657. @!y:0..255; {the character after the cursor}
  1658. @!key:integer; {value to be stored in |hash|}
  1659. @!t:integer; {temporary register for swapping}
  1660. begin if hash_ptr=hash_size then return;
  1661. @<Compute the command parameters |y|, |cc|, and |zz|@>;
  1662. key:=256*c+y+1; h:=(1009*key) mod hash_size;
  1663. while hash[h]>0 do
  1664.   begin if hash[h]<=key then
  1665.     begin if hash[h]=key then return; {unused ligature command}
  1666.     t:=hash[h]; hash[h]:=key; key:=t; {do ordered-hash-table insertion}
  1667.     t:=class[h]; class[h]:=cc; cc:=t; {namely, do a swap}
  1668.     t:=lig_z[h]; lig_z[h]:=zz; zz:=t;
  1669.     end;
  1670.   if h>0 then decr(h)@+else h:=hash_size;
  1671.   end;
  1672. hash[h]:=key; class[h]:=cc; lig_z[h]:=zz;
  1673. incr(hash_ptr); hash_list[hash_ptr]:=h;
  1674. exit:end;
  1675. @ We must store kern commands as well as ligature commands, because the former
  1676. might make the latter inapplicable.
  1677. @<Compute the command param...@>=
  1678. k:=lig_step(i); y:=tfm[k+1]; t:=tfm[k+2]; cc:=simple; zz:=tfm[k+3];
  1679. if t>=kern_flag then zz:=y
  1680. else begin case t of
  1681.   0,6:do_nothing; {\.{LIG},\.{/LIG>}}
  1682.   5,11:zz:=y; {\.{LIG/>}, \.{/LIG/>>}}
  1683.   1,7:cc:=left_z; {\.{LIG/}, \.{/LIG/>}}
  1684.   2:cc:=right_z; {\.{/LIG}}
  1685.   3:cc:=both_z; {\.{/LIG/}}
  1686.   end; {there are no other cases}
  1687.   end
  1688. @ Evaluation of $f(x,y)$ is handled by two mutually recursive procedures.
  1689. Kind of a neat algorithm, generalizing a depth-first search.
  1690. @p function f(@!h,@!x,@!y:index):index; forward;@t\2@>
  1691.   {compute $f$ for arguments known to be in |hash[h]|}
  1692. function eval(@!x,@!y:index):index; {compute $f(x,y)$ with hashtable lookup}
  1693. var @!key:integer; {value sought in hash table}
  1694. begin key:=256*x+y+1; h:=(1009*key) mod hash_size;
  1695. while hash[h]>key do
  1696.   if h>0 then decr(h)@+else h:=hash_size;
  1697. if hash[h]<key then eval:=y {not in ordered hash table}
  1698. else eval:=f(h,x,y);
  1699. @ Pascal's beastly convention for |forward| declarations prevents us from
  1700. saying |function f(h,x,y:index):index| here.
  1701. @p function f;
  1702. begin case class[h] of
  1703. simple: do_nothing;
  1704. left_z: begin class[h]:=pending; lig_z[h]:=eval(lig_z[h],y); class[h]:=simple;
  1705.   end;
  1706. right_z: begin class[h]:=pending; lig_z[h]:=eval(x,lig_z[h]); class[h]:=simple;
  1707.   end;
  1708. both_z: begin class[h]:=pending; lig_z[h]:=eval(eval(x,lig_z[h]),y);
  1709.   class[h]:=simple;
  1710.   end;
  1711. pending: begin x_lig_cycle:=x; y_lig_cycle:=y; lig_z[h]:=257; class[h]:=simple;
  1712.   end; {the value 257 will break all cycles, since it's not in |hash|}
  1713. end; {there are no other cases}
  1714. f:=lig_z[h];
  1715. @* Outputting the VF info.
  1716. The routines we've used for output from the |tfm| array have counterparts
  1717. for output from |vf|. One difference is that the string outputs from |vf|
  1718. need to be checked for balanced parentheses. The |string_balance| routine
  1719. tests the string of length~|l| that starts at location~|k|.
  1720. @p function string_balance(@!k,@!l:integer):boolean;
  1721. label not_found,exit;
  1722. var @!j,@!bal:integer;
  1723. begin if l>0 then if vf[k]=" " then goto not_found;
  1724.   {a leading blank is considered unbalanced}
  1725. bal:=0;
  1726. for j:=k to k+l-1 do
  1727.   begin if (vf[j]<" ")or(vf[j]>=127) then goto not_found;
  1728.   if vf[j]="(" then incr(bal)
  1729.   else if vf[j]=")" then
  1730.     if bal=0 then goto not_found else decr(bal);
  1731.   end;
  1732. if bal>0 then goto not_found;
  1733. string_balance:=true; return;
  1734. not_found:string_balance:=false;
  1735. exit:end;
  1736. @ @d bad_vf(#)==begin perfect:=false; if chars_on_line>0 then print_ln(' ');
  1737.   chars_on_line:=0; print_ln('Bad VF file: ',#);
  1738.   end
  1739. @.Bad VF file@>
  1740. @<Do the virtual font title@>=
  1741. if string_balance(0,font_start[0]) then
  1742.   begin left; out('VTITLE ');
  1743.   for k:=0 to font_start[0]-1 do out(xchr[vf[k]]);
  1744.   right;
  1745.   end
  1746. else bad_vf('Title is not a balanced ASCII string')
  1747. @.Title is not balanced@>
  1748. @ We can re-use some code by moving |fix_word| data to |tfm|, using the
  1749. fact that the design size has already been output.
  1750. @p procedure out_as_fix(@!x:integer);
  1751. var @!k:1..3;
  1752. begin if abs(x)>=@'100000000 then
  1753.   bad_vf('Oversize dimension has been reset to zero.');
  1754. @.Oversize dimension...@>
  1755. if x>=0 then tfm[design_size]:=0
  1756. else begin tfm[design_size]:=255; x:=x+@'100000000;
  1757.   end;
  1758. for k:=3 downto 1 do
  1759.   begin tfm[design_size+k]:=x mod 256; x:=x div 256;
  1760.   end;
  1761. out_fix(design_size);
  1762. @ @<Do the local fonts@>=
  1763. for f:=0 to font_ptr-1 do
  1764.   begin left; out('MAPFONT D ',f:1); out_ln;
  1765.   @<Output the font area and name@>;
  1766.   for k:=0 to 11 do tfm[k]:=vf[font_start[f]+k];
  1767.   if tfm[0]+tfm[1]+tfm[2]+tfm[3]>0 then
  1768.     begin left; out('FONTCHECKSUM'); out_octal(0,4); right;
  1769.     end;
  1770.   left; out('FONTAT'); out_fix(4); right;
  1771.   left; out('FONTDSIZE'); out_fix(8); right; right;
  1772.   end
  1773. @ @<Output the font area and name@>=
  1774. a:=vf[font_start[f]+12]; l:=vf[font_start[f]+13];
  1775. if a>0 then
  1776.   if not string_balance(font_start[f]+14,a) then
  1777.     bad_vf('Improper font area will be ignored')
  1778. @.Improper font area@>
  1779.   else begin left; out('FONTAREA ');
  1780.     for k:=font_start[f]+14 to font_start[f]+a+13 do out(xchr[vf[k]]);
  1781.     right;
  1782.     end;
  1783. if (l=0)or not string_balance(font_start[f]+14+a,l) then
  1784.   bad_vf('Improper font name will be ignored')
  1785. @.Improper font name@>
  1786. else begin left; out('FONTNAME ');
  1787.   for k:=font_start[f]+14+a to font_start[f]+a+l+13 do out(xchr[vf[k]]);
  1788.   right;
  1789.   end
  1790. @ Now we get to the interesting part of \.{VF} output, where \.{DVI}
  1791. commands are translated into symbolic form. The \.{VPL} language is a subset
  1792. of \.{DVI}, so we sometimes need to output semantic equivalents of
  1793. the commands instead of producing a literal translation. This causes a
  1794. small but tolerable loss of efficiency. We need to simulate the stack
  1795. used by \.{DVI}-reading software.
  1796. @<Glob...@>=
  1797. @!top:0..max_stack; {\.{DVI} stack pointer}
  1798. @!wstack,@!xstack,@!ystack,@!zstack:array[0..max_stack] of integer;
  1799.  {stacked values of \.{DVI} registers |w|, |x|, |y|, |z|}
  1800. @!vf_limit:0..vf_size; {the current packet ends here}
  1801. @!o:byte; {the current opcode}
  1802. @ @<Do the packet for character |c|@>=
  1803. if packet_start[c]=vf_size then
  1804.   bad_vf('Missing packet for character ',c:1)
  1805. @.Missing packet@>
  1806. else begin left; out('MAP'); out_ln;
  1807.   top:=0; wstack[0]:=0; xstack[0]:=0; ystack[0]:=0; zstack[0]:=0;
  1808.   vf_ptr:=packet_start[c]; vf_limit:=packet_end[c]+1; f:=0;
  1809.   while vf_ptr<vf_limit do
  1810.     begin o:=vf[vf_ptr]; incr(vf_ptr);
  1811.     case o of
  1812.     @<Cases of \.{DVI} instructions that can appear in character packets@>@;
  1813.     improper_DVI_for_VF: bad_vf('Illegal DVI code ',o:1,' will be ignored');
  1814.     end; {there are no other cases}
  1815.     end;
  1816.   if top>0 then
  1817.     begin bad_vf('More pushes than pops!');
  1818. @.More pushes than pops@>
  1819.     repeat out('(POP)'); decr(top);@+until top=0;
  1820.     end;
  1821.   right;
  1822.   end
  1823. @ A procedure called |get_bytes| helps fetch the parameters of \.{DVI} commands.
  1824. @p function get_bytes(@!k:integer;@!signed:boolean):integer;
  1825. var @!a:integer; {accumulator}
  1826. begin if vf_ptr+k>vf_limit then
  1827.   begin bad_vf('Packet ended prematurely'); k:=vf_limit-vf_ptr;
  1828.   end;
  1829. a:=vf[vf_ptr];
  1830. if (k=4) or signed then
  1831.   if a>=128 then a:=a-256;
  1832. incr(vf_ptr);
  1833. while k>1 do
  1834.   begin a:=a*256+vf[vf_ptr]; incr(vf_ptr); decr(k);
  1835.   end;
  1836. get_bytes:=a;
  1837. @ Let's look at the simplest cases first, in order to get some experience.
  1838. @d four_cases(#)==#,#+1,#+2,#+3
  1839. @d eight_cases(#)==four_cases(#),four_cases(#+4)
  1840. @d sixteen_cases(#)==eight_cases(#),eight_cases(#+8)
  1841. @d thirty_two_cases(#)==sixteen_cases(#),sixteen_cases(#+16)
  1842. @d sixty_four_cases(#)==thirty_two_cases(#),thirty_two_cases(#+32)
  1843. @<Cases...@>=
  1844. nop:do_nothing;
  1845. push:begin if top=max_stack then
  1846.     begin print_ln('Stack overflow!'); goto final_end;
  1847. @.Stack overflow@>
  1848.     end;
  1849.   incr(top); wstack[top]:=wstack[top-1]; xstack[top]:=xstack[top-1];
  1850.   ystack[top]:=ystack[top-1]; zstack[top]:=zstack[top-1]; out('(PUSH)');
  1851.   out_ln;
  1852.   end;                            
  1853. pop:if top=0 then bad_vf('More pops than pushes!')
  1854. @.More pops than pushes@>
  1855.   else begin decr(top); out('(POP)'); out_ln;
  1856.     end;
  1857. set_rule,put_rule:begin if o=put_rule then out('(PUSH)');
  1858.   left; out('SETRULE'); out_as_fix(get_bytes(4,true));
  1859.   out_as_fix(get_bytes(4,true));
  1860.   if o=put_rule then out(')(POP');
  1861.   right;
  1862.   end;
  1863. @ Horizontal and vertical motions become \.{RIGHT} and \.{DOWN} in \.{VPL}
  1864. lingo.
  1865. @<Cases...@>=
  1866. four_cases(right1):begin out('(MOVERIGHT');
  1867.   out_as_fix(get_bytes(o-right1+1,true));
  1868.   out(')'); out_ln;@+end;
  1869. w0,four_cases(w1):begin if o<>w0 then wstack[top]:=get_bytes(o-w1+1,true);
  1870.   out('(MOVERIGHT'); out_as_fix(wstack[top]); out(')'); out_ln;@+end;
  1871. x0,four_cases(x1):begin if o<>x0 then xstack[top]:=get_bytes(o-x1+1,true);
  1872.   out('(MOVERIGHT'); out_as_fix(xstack[top]); out(')'); out_ln;@+end;
  1873. four_cases(down1):begin out('(MOVEDOWN'); out_as_fix(get_bytes(o-down1+1,true));
  1874.   out(')'); out_ln;@+end;
  1875. y0,four_cases(y1):begin if o<>y0 then ystack[top]:=get_bytes(o-y1+1,true);
  1876.   out('(MOVEDOWN'); out_as_fix(ystack[top]); out(')'); out_ln;@+end;
  1877. z0,four_cases(z1):begin if o<>z0 then zstack[top]:=get_bytes(o-z1+1,true);
  1878.   out('(MOVEDOWN'); out_as_fix(zstack[top]); out(')'); out_ln;@+end;
  1879. @ Variable |f| always refers to the current font. If |f=font_ptr|, it's
  1880. a font that hasn't been defined (so its characters will be ignored).
  1881. @<Cases...@>=
  1882. sixty_four_cases(fnt_num_0),four_cases(fnt1):begin f:=0;
  1883.   if o>=fnt1 then font_number[font_ptr]:=get_bytes(o-fnt1+1,false)
  1884.   else font_number[font_ptr]:=o-fnt_num_0;
  1885.   while font_number[f]<>font_number[font_ptr] do incr(f);
  1886.   if f=font_ptr then bad_vf('Undeclared font selected')
  1887. @.Undeclared font selected@>
  1888.   else begin out('(SELECTFONT D ',f:1,')'); out_ln;
  1889.     end;
  1890.   end;
  1891. @ Before we typeset a character we make sure that it exists.
  1892. @<Cases...@>=
  1893. sixty_four_cases(set_char_0),sixty_four_cases(set_char_0+64),
  1894.  four_cases(set1),four_cases(put1):begin if o>=set1 then
  1895.     if o>=put1 then c:=get_bytes(o-put1+1,false)
  1896.     else c:=get_bytes(o-set1+1,false)
  1897.   else c:=o;
  1898.   if f=font_ptr then
  1899.     bad_vf('Character ',c:1,' in undeclared font will be ignored')
  1900. @.Character...will be ignored@>
  1901.   else begin vf[font_start[f+1]-1]:=c; {store |c| in the ``hole'' we left}
  1902.     k:=font_chars[f];@+while vf[k]<>c do incr(k);
  1903.     if k=font_start[f+1]-1 then
  1904.       bad_vf('Character ',c:1,' in font ',f:1,' will be ignored')
  1905.     else begin if o>=put1 then out('(PUSH)');
  1906.       left; out('SETCHAR'); out_char(c);
  1907.       if o>=put1 then out(')(POP');
  1908.       right;
  1909.       end;
  1910.     end;
  1911.   end;
  1912. @ The ``special'' commands are the only ones remaining to be dealt with.
  1913. We use a hexadecimal
  1914. output in the general case, if a simple string would be inadequate.
  1915. @d out_hex(#)==begin a:=#;
  1916.     if a<10 then out(a:1)
  1917.     else out(xchr[a-10+"A"]);
  1918.     end
  1919. @<Cases...@>=
  1920. four_cases(xxx1):begin k:=get_bytes(o-xxx1+1,false);
  1921.   if k<0 then bad_vf('String of negative length!')
  1922.   else begin left;
  1923.     if k+vf_ptr>vf_limit then
  1924.       begin bad_vf('Special command truncated to packet length');
  1925.       k:=vf_limit-vf_ptr;
  1926.       end;
  1927.     if (k>64)or not string_balance(vf_ptr,k) then
  1928.       begin out('SPECIALHEX ');
  1929.       while k>0 do
  1930.         begin if k mod 32=0 then out_ln
  1931.         else if k mod 4=0 then out(' ');
  1932.         out_hex(vf[vf_ptr] div 16); out_hex(vf[vf_ptr] mod 16);
  1933.         incr(vf_ptr); decr(k);
  1934.         end;
  1935.       end
  1936.     else begin out('SPECIAL ');
  1937.       while k>0 do
  1938.         begin out(xchr[vf[vf_ptr]]); incr(vf_ptr); decr(k);
  1939.         end;
  1940.       end;
  1941.     right;
  1942.     end;
  1943.   end;
  1944. @* The main program.
  1945. The routines sketched out so far need to be packaged into separate procedures,
  1946. on some systems, since some \PASCAL\ compilers place a strict limit on the
  1947. size of a routine. The packaging is done here in an attempt to avoid some
  1948. system-dependent changes.
  1949. First come the |vf_input| and |organize| procedures, which read the input data
  1950. and get ready for subsequent events. If something goes wrong, the routines
  1951. return |false|.
  1952. @p function vf_input:boolean;
  1953. label final_end, exit;
  1954. var vf_ptr:0..vf_size; {an index into |vf|}
  1955. @!k:integer; {all-purpose index}
  1956. @!c:integer; {character code}
  1957. begin @<Read the whole \.{VF} file@>;
  1958. vf_input:=true; return;
  1959. final_end: vf_input:=false;
  1960. exit: end;
  1961. function organize:boolean;
  1962. label final_end, exit;
  1963. var tfm_ptr:index; {an index into |tfm|}
  1964. begin @<Read the whole \.{TFM} file@>;
  1965. @<Set subfile sizes |lh|, |bc|, \dots, |np|@>;
  1966. @<Compute the base addresses@>;
  1967. organize:=vf_input; return;
  1968. final_end: organize:=false;
  1969. exit: end;
  1970. @ Next we do the simple things.
  1971. @p procedure do_simple_things;
  1972. var i:0..@'77777; {an index to words of a subfile}
  1973. @!f:0..vf_size; {local font number}
  1974. @!k:integer; {all-purpose index}
  1975. begin @<Do the virtual font title@>;
  1976. @<Do the header@>;
  1977. @<Do the parameters@>;
  1978. @<Do the local fonts@>;
  1979. @<Check the |fix_word| entries@>;
  1980. @ And then there's a routine for individual characters.
  1981. @p function do_map(@!c:byte):boolean;
  1982. label final_end,exit;
  1983. var @!k:integer;
  1984. @!f:0..vf_size; {current font number}
  1985. begin @<Do the packet for character |c|@>;
  1986. do_map:=true; return;
  1987. final_end: do_map:=false;
  1988. exit:end;
  1989. function do_characters:boolean;
  1990. label final_end, exit;
  1991. var @!c:byte; {character being done}
  1992. @!k:index; {a random index}
  1993. @!ai:0..lig_size; {index into |activity|}
  1994. begin @<Do the characters@>;@/
  1995. do_characters:=true; return;
  1996. final_end: do_characters:=false;
  1997. exit:end;
  1998. @ Here is where \.{VFtoVP} begins and ends.
  1999. @p begin initialize;@/
  2000. if not organize then goto final_end;
  2001. do_simple_things;@/
  2002. @<Do the ligatures and kerns@>;
  2003. @<Check the extensible recipes@>;
  2004. if not do_characters then goto final_end;
  2005. print_ln('.');@/
  2006. if level<>0 then print_ln('This program isn''t working!');
  2007. @.This program isn't working@>
  2008. if not perfect then
  2009.   begin out('(COMMENT THE TFM AND/OR VF FILE WAS BAD, ');
  2010.   out('SO THE DATA HAS BEEN CHANGED!)');
  2011.   end;
  2012. @.THE TFM AND/OR VF FILE WAS BAD...@>
  2013. final_end:end.
  2014. @* System-dependent changes.
  2015. This section should be replaced, if necessary, by changes to the program
  2016. that are necessary to make \.{VFtoVP} work at a particular installation.
  2017. It is usually best to design your change file so that all changes to
  2018. previous sections preserve the section numbering; then everybody's version
  2019. will be consistent with the printed program. More extensive changes,
  2020. which introduce new sections, can be inserted here; then only the index
  2021. itself will get a new section number.
  2022. @^system dependencies@>
  2023. @* Index.
  2024. Pointers to error messages appear here together with the section numbers
  2025. where each ident\-i\-fier is used.
  2026.