home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / web2c / fontutil / vptovf.web (.txt) < prev   
Texinfo Document  |  1996-09-28  |  123KB  |  2,705 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 some for-loop indices for stricter Pascal (April 1990).
  4. % Version 1.2 fixed `nonexistent char 0' bug, and a bit more (September 1990).
  5. % Version 1.3 has more robust `out_scaled' (March 1991).
  6. % Here is TeX material that gets inserted after \input webmac
  7. \def\hang{\hangindent 3em\indent\ignorespaces}
  8. \font\ninerm=cmr9
  9. \let\mc=\ninerm % medium caps for names like SAIL
  10. \def\PASCAL{Pascal}
  11. \font\logo=logo10 % for the METAFONT logo
  12. \def\MF{{\logo METAFONT}}
  13. \def\(#1){} % this is used to make section names sort themselves better
  14. \def\9#1{} % this is used for sort keys in the index
  15. \def\title{VP\lowercase{to}VF}
  16. \def\contentspagenumber{201}
  17. \def\topofcontents{\null
  18.   \def\titlepage{F} % include headline on the contents page
  19.   \def\rheader{\mainfont\hfil \contentspagenumber}
  20.   \vfill
  21.   \centerline{\titlefont The {\ttitlefont VPtoVF} processor}
  22.   \vskip 15pt
  23.   \centerline{(Version 1.3, March 1991)}
  24.   \vfill}
  25. \def\botofcontents{\vfill
  26.   \centerline{\hsize 5in\baselineskip9pt
  27.     \vbox{\ninerm\noindent
  28.     The preparation of this program
  29.     was supported in part by the National Science
  30.     Foundation and by the System Development Foundation. `\TeX' is a
  31.     trademark of the American Mathematical Society.}}}
  32. \pageno=\contentspagenumber \advance\pageno by 1
  33. @* Introduction.
  34. The \.{VPtoVF} utility program converts virtual-property-list (``\.{VPL}'')
  35. files into an equivalent pair of files called a virtual font (``\.{VF}'') file
  36. and a \TeX\ font metric (``\.{TFM}'') file. It also makes a thorough check
  37. of the given \.{VPL} file, so that the \.{VF} file should be acceptable to
  38. device drivers and the \.{TFM} file should be acceptable to \TeX.
  39. \indent\.{VPtoVF} is an extended version of the program \.{PLtoTF}, which
  40. is part of the standard \TeX ware library. 
  41. The idea of a virtual font was inspired by the work of David R. Fuchs
  42. @^Fuchs, David Raymond@>
  43. who designed a similar set of conventions in 1984 while developing a
  44. device driver for ArborText, Inc. He wrote a somewhat similar program
  45. called \.{PLFONT}.
  46. The |banner| string defined here should be changed whenever \.{VPtoVF}
  47. gets modified.
  48. @d banner=='This is VPtoVF, Version 1.3' {printed when the program starts}
  49. @ This program is written entirely in standard \PASCAL, except that
  50. it has to do some slightly system-dependent character code conversion
  51. on input. Furthermore, lower case letters are used in error messages;
  52. they could be converted to upper case if necessary. The input is read
  53. from |vpl_file|, and the output is written on |vf_file| and |tfm_file|;
  54. error messages and
  55. other remarks are written on the |output| file, which the user may
  56. choose to assign to the terminal if the system permits it.
  57. @^system dependencies@>
  58. The term |print| is used instead of |write| when this program writes on
  59. the |output| file, so that all such output can be easily deflected.
  60. @d print(#)==write(#)
  61. @d print_ln(#)==write_ln(#)
  62. @p program VPtoVF(@!vpl_file,@!vf_file,@!tfm_file,@!output);
  63. const @<Constants in the outer block@>@/
  64. type @<Types in the outer block@>@/
  65. var @<Globals in the outer block@>@/
  66. procedure initialize; {this procedure gets things started properly}
  67.   var @<Local variables for initialization@>@/
  68.   begin print_ln(banner);@/
  69.   @<Set initial values@>@/
  70.   end;
  71. @ The following parameters can be changed at compile time to extend or
  72. reduce \.{VPtoVF}'s capacity.
  73. @<Constants...@>=
  74. @!buf_size=60; {length of lines displayed in error messages}
  75. @!max_header_bytes=100; {four times the maximum number of words allowed in
  76.   the \.{TFM} file header block, must be 1024 or less}
  77. @!vf_size=10000; {maximum length of |vf| data, in bytes}
  78. @!max_stack=100; {maximum depth of simulated \.{DVI} stack}
  79. @!max_param_words=30; {the maximum number of \.{fontdimen} parameters allowed}
  80. @!max_lig_steps=5000;
  81.   {maximum length of ligature program, must be at most $32767-257=32510$}
  82. @!max_kerns=500; {the maximum number of distinct kern values}
  83. @!hash_size=5003; {preferably a prime number, a bit larger than the number
  84.   of character pairs in lig/kern steps}
  85. @ Here are some macros for common programming idioms.
  86. @d incr(#) == #:=#+1 {increase a variable by unity}
  87. @d decr(#) == #:=#-1 {decrease a variable by unity}
  88. @d do_nothing == {empty statement}
  89. @* Property list description of font metric data.
  90. The idea behind \.{VPL} files is that precise details about fonts, i.e., the
  91. facts that are needed by typesetting routines like \TeX, sometimes have to
  92. be supplied by hand. The nested property-list format provides a reasonably
  93. convenient way to do this.
  94. A good deal of computation is necessary to parse and process a
  95. \.{VPL} file, so it would be inappropriate for \TeX\ itself to do this
  96. every time it loads a font. \TeX\ deals only with the compact descriptions
  97. of font metric data that appear in \.{TFM} files. Such data is so compact,
  98. however, it is almost impossible for anybody but a computer to read it.
  99. Device drivers also need a compact way to describe mappings from \TeX's idea
  100. of a font to the actual characters a device can produce. They can do this
  101. conveniently when given a packed sequence of bytes called a \.{VF} file.
  102. The purpose of \.{VPtoVF} is to convert from a human-oriented file of text
  103. to computer-oriented files of binary numbers. There's a companion program,
  104. \.{VFtoVP}, which goes the other way.
  105. @<Glob...@>=
  106. @!vpl_file:text;
  107. @ @<Set init...@>=
  108. reset(vpl_file);
  109. @ A \.{VPL} file is like a \.{PL} file with a few extra features, so we
  110. can begin to define it by reviewing the definition of \.{PL} files. The
  111. material in the next few sections is copied from the program \.{PLtoTF}.
  112. A \.{PL} file is a list of entries of the form
  113. $$\.{(PROPERTYNAME VALUE)}$$
  114. where the property name is one of a finite set of names understood by
  115. this program, and the value may itself in turn be a property list.
  116. The idea is best understood by looking at an example, so let's consider
  117. a fragment of the \.{PL} file for a hypothetical font.
  118. $$\vbox{\halign{\.{#}\hfil\cr
  119. (FAMILY NOVA)\cr
  120. (FACE F MIE)\cr
  121. (CODINGSCHEME ASCII)\cr
  122. (DESIGNSIZE D 10)\cr
  123. (DESIGNUNITS D 18)\cr
  124. (COMMENT A COMMENT IS IGNORED)\cr
  125. (COMMENT (EXCEPT THIS ONE ISN'T))\cr
  126. (COMMENT (ACTUALLY IT IS, EVEN THOUGH\cr
  127. \qquad\qquad IT SAYS IT ISN'T))\cr
  128. (FONTDIMEN\cr
  129. \qquad   (SLANT R -.25)\cr
  130. \qquad   (SPACE D 6)\cr
  131. \qquad   (SHRINK D 2)\cr
  132. \qquad   (STRETCH D 3)\cr
  133. \qquad   (XHEIGHT R 10.55)\cr
  134. \qquad   (QUAD D 18)\cr
  135. \qquad   )\cr
  136. (LIGTABLE\cr
  137. \qquad   (LABEL C f)\cr
  138. \qquad   (LIG C f O 200)\cr
  139. \qquad   (SKIP D 1)\cr
  140. \qquad   (LABEL O 200)\cr
  141. \qquad   (LIG C i O 201)\cr
  142. \qquad   (KRN O 51 R 1.5)\cr
  143. \qquad   (/LIG C ? C f)\cr
  144. \qquad   (STOP)\cr
  145. \qquad   )\cr
  146. (CHARACTER C f\cr
  147. \qquad   (CHARWD D 6)\cr
  148. \qquad   (CHARHT R 13.5)\cr
  149. \qquad   (CHARIC R 1.5)\cr
  150. \qquad   )\cr}}$$
  151. This example says that the font whose metric information is being described
  152. belongs to the hypothetical
  153. \.{NOVA} family; its face code is medium italic extended;
  154. and the characters appear in ASCII code positions. The design size is 10 points,
  155. and all other sizes in this \.{PL} file are given in units such that 18 units
  156. equals the design size. The font is slanted with a slope of $-.25$ (hence the
  157. letters actually slant backward---perhaps that is why the family name is
  158. \.{NOVA}). The normal space between words is 6 units (i.e., one third of
  159. the 18-unit design size), with glue that shrinks by 2 units or stretches by 3.
  160. The letters for which accents don't need to be raised or lowered are 10.55
  161. units high, and one em equals 18 units.
  162. The example ligature table is a bit trickier. It specifies that the
  163. letter \.f followed by another \.f is changed to code @'200, while
  164. code @'200 followed by \.i is changed to @'201; presumably codes @'200
  165. and @'201 represent the ligatures `ff' and `ffi'.  Moreover, in both cases
  166. \.f and @'200, if the following character is the code @'51 (which is a
  167. right parenthesis), an additional 1.5 units of space should be inserted
  168. before the @'51.  (The `\.{SKIP}~\.D~\.1' skips over one \.{LIG} or
  169. \.{KRN} command, which in this case is the second \.{LIG}; in this way
  170. two different ligature/kern programs can come together.)
  171. Finally, if either \.f or @'200 is followed by a question mark,
  172. the question mark is replaced by \.f and the ligature program is
  173. started over. (Thus, the character pair `\.{f?}' would actually become
  174. the ligature `ff', and `\.{ff?}' or `\.{f?f}' would become `fff'. To
  175. avoid this restart procedure, the \.{/LIG} command could be replaced
  176. by \.{/LIG>}; then `\.{f?} would become `f\kern0ptf' and `\.{f?f}'
  177. would become `f\kern0ptff'.)
  178. Character \.f itself is 6 units wide and 13.5 units tall, in this example.
  179. Its depth is zero (since \.{CHARDP} is not given), and its italic correction
  180. is 1.5 units.
  181. @ The example above illustrates most of the features found in \.{PL} files.
  182. Note that some property names, like \.{FAMILY} or \.{COMMENT}, take a
  183. string as their value; this string continues until the first unmatched
  184. right parenthesis. But most property names, like \.{DESIGNSIZE} and \.{SLANT}
  185. and \.{LABEL}, take a number as their value. This number can be expressed in
  186. a variety of ways, indicated by a prefixed code; \.D stands for decimal,
  187. \.H for hexadecimal, \.O for octal, \.R for real, \.C for character, and
  188. \.F for ``face.''  Other property names, like \.{LIG}, take two numbers as
  189. their value.  And still other names, like \.{FONTDIMEN} and \.{LIGTABLE} and
  190. \.{CHARACTER}, have more complicated values that involve property lists.
  191. A property name is supposed to be used only in an appropriate property
  192. list.  For example, \.{CHARWD} shouldn't occur on the outer level or
  193. within \.{FONTDIMEN}.
  194. The individual property-and-value pairs in a property list can appear in
  195. any order. For instance, `\.{SHRINK}' precedes `\.{STRETCH}' in the above
  196. example, although the \.{TFM} file always puts the stretch parameter first.
  197. One could even give the information about characters like `\.f' before
  198. specifying the number of units in the design size, or before specifying the
  199. ligature and kerning table. However, the \.{LIGTABLE} itself is an exception
  200. to this rule; the individual elements of the \.{LIGTABLE} property list
  201. can be reordered only to a certain extent without changing the meaning
  202. of that table.
  203. If property-and-value pairs are omitted, a default value is used. For example,
  204. we have already noted that the default for \.{CHARDP} is zero. The default
  205. for {\sl every\/} numeric value is, in fact, zero, unless otherwise stated
  206. below.
  207. If the same property name is used more than once, \.{VPtoVF} will not notice
  208. the discrepancy; it simply uses the final value given. Once again, however, the
  209. \.{LIGTABLE} is an exception to this rule; \.{VPtoVF} will complain if there
  210. is more than one label for some character. And of course many of the
  211. entries in the \.{LIGTABLE} property list have the same property name.
  212. @ A \.{VPL} file also includes information about how to create each character,
  213. by typesetting characters from other fonts and/or by drawing lines, etc.
  214. Such information is the value of the `\.{MAP}' property, which can be
  215. illustrated as follows:
  216. $$\vbox{\halign{\.{#}\hfil\cr
  217. (MAPFONT D 0 (FONTNAME Times-Roman))\cr
  218. (MAPFONT D 1 (FONTNAME Symbol))\cr
  219. (MAPFONT D 2 (FONTNAME cmr10)(FONTAT D 20))\cr
  220. (CHARACTER O 0 (MAP (SELECTFONT D 1)(SETCHAR C G)))\cr
  221. (CHARACTER O 76 (MAP (SETCHAR O 277)))\cr
  222. (CHARACTER D 197 (MAP\cr
  223. \qquad(PUSH)(SETCHAR C A)(POP)\cr
  224. \qquad(MOVEUP R 0.937)(MOVERIGHT R 1.5)(SETCHAR O 312)))\cr
  225. (CHARACTER O 200 (MAP (MOVEDOWN R 2.1)(SETRULE R 1 R 8)))\cr
  226. (CHARACTER O 201 (MAP\cr
  227. \qquad (SPECIAL ps: /SaveGray currentgray def .5 setgray)\cr
  228. \qquad (SELECTFONT D 2)(SETCHAR C A)\cr
  229. \qquad (SPECIAL ps: SaveGray setgray)))\cr
  230. (These specifications appear in addition to the conventional \.{PL}
  231. information. The \.{MAP} attribute can be mixed in with other attributes
  232. like \.{CHARWD} or it can be given separately.)
  233. In this example, the virtual font is composed of characters that can be
  234. fabricated from three actual fonts, `\.{Times-Roman}',
  235. `\.{Symbol}', and `\.{cmr10} \.{at} \.{20\\u}' (where \.{\\u}
  236. is the unit size in this \.{VPL} file). Character |@'0| is typeset as
  237. a `G' from the symbol font. Character |@'76| is typeset as character |@'277|
  238. from the ordinary Times font. (If no other font is selected, font
  239. number~0 is the default. If no \.{MAP} attribute is given, the default map
  240. is a character of the same number in the default font.)
  241. Character 197 (decimal) is more interesting: First an A is typeset (in the
  242. default font Times), and this is enclosed by \.{PUSH} and \.{POP} so that
  243. the original position is restored. Then the accent character |@'312| is
  244. typeset, after moving up .937 units and right 1.5 units.
  245. To typeset character |@'200| in this virtual font, we move down 2.1 units,
  246. then typeset a rule that is 1 unit high and 8 units wide.
  247. Finally, to typeset character |@'201|, we do something that requires a
  248. special ability to interpret PostScript commands; this example
  249. sets the PostScript ``color'' to 50\char`\%\ gray and typesets an `A'
  250. from \.{cmr10} in that color.
  251. In general, the \.{MAP} attribute of a virtual character can be any sequence
  252. of typesetting commands that might appear in a page of a \.{DVI} file.
  253. A single character might map into an entire page.
  254. @ But instead of relying on a hypothetical example, let's consider a complete
  255. grammar for \.{VPL} files, beginning with the (unchanged) grammatical rules
  256. for \.{PL} files. At the outer level, the following property names
  257. are valid in any \.{PL} file:
  258. \yskip\hang\.{CHECKSUM} (four-byte value). The value, which should be a
  259. nonnegative integer less than $2^{32}$, is used to identify a particular
  260. version of a font; it should match the check sum value stored with the font
  261. itself. An explicit check sum of zero is used to bypass
  262. check sum testing. If no checksum is specified in the \.{VPL} file,
  263. \.{VPtoVF} will compute the checksum that \MF\ would compute from the
  264. same data.
  265. \yskip\hang\.{DESIGNSIZE} (numeric value, default is 10). The value, which
  266. should be a real number in the range |1.0<=x<2048|, represents the default
  267. amount by which all quantities will be scaled if the font is not loaded
  268. with an `\.{at}' specification. For example, if one says
  269. `\.{\\font\\A=cmr10 at 15pt}' in \TeX\ language, the design size in the \.{TFM}
  270. file is ignored and effectively replaced by 15 points; but if one simply
  271. says `\.{\\font\\A=cmr10}' the stated design size is used. This quantity is
  272. always in units of printer's points.
  273. \yskip\hang\.{DESIGNUNITS} (numeric value, default is 1). The value
  274. should be a positive real number; it says how many units equals the design
  275. size (or the eventual `\.{at}' size, if the font is being scaled). For
  276. example, suppose you have a font that has been digitized with 600 pixels per
  277. em, and the design size is one em; then you could say `\.{(DESIGNUNITS R 600)}'
  278. if you wanted to give all of your measurements in units of pixels.
  279. \yskip\hang\.{CODINGSCHEME} (string value, default is `\.{UNSPECIFIED}').
  280. The string should not contain parentheses, and its length must be less than 40.
  281. It identifies the correspondence between the numeric codes and font characters.
  282. (\TeX\ ignores this information, but other software programs make use of it.)
  283. \yskip\hang\.{FAMILY} (string value, default is `\.{UNSPECIFIED}').
  284. The string should not contain parentheses, and its length must be less than 20.
  285. It identifies the name of the family to which this font belongs, e.g.,
  286. `\.{HELVETICA}'.  (\TeX\ ignores this information; but it is needed, for
  287. example, when converting \.{DVI} files to \.{PRESS} files for Xerox
  288. equipment.)
  289. \yskip\hang\.{FACE} (one-byte value). This number, which must lie between
  290. 0 and 255 inclusive, is a subsidiary ident\-ifi\-ca\-tion of the font within its
  291. family. For example, bold italic condensed fonts might have the same family name
  292. as light roman extended fonts, differing only in their face byte.  (\TeX\
  293. ignores this information; but it is needed, for example, when converting
  294. \.{DVI} files to \.{PRESS} files for Xerox equipment.)
  295. \yskip\hang\.{SEVENBITSAFEFLAG} (string value, default is `\.{FALSE}'). The
  296. value should start with either `\.T' (true) or `\.F' (false). If true, character
  297. codes less than 128 cannot lead to codes of 128 or more via ligatures or
  298. charlists or extensible characters. (\TeX82 ignores this flag, but older
  299. versions of \TeX\ would only accept \.{TFM} files that were seven-bit safe.)
  300. \.{VPtoVF} computes the correct value of this flag and gives an error message
  301. only if a claimed ``true'' value is incorrect.
  302. \yskip\hang\.{HEADER} (a one-byte value followed by a four-byte value).
  303. The one-byte value should be between 18 and a maximum limit that can be
  304. raised or lowered depending on the compile-time setting of |max_header_bytes|.
  305. The four-byte value goes into the header word whose index is the one-byte
  306. value; for example, to set |header[18]:=1|, one may write
  307. `\.{(HEADER D 18 O 1)}'. This notation is used for header information that
  308. is presently unnamed. (\TeX\ ignores it.)
  309. \yskip\hang\.{FONTDIMEN} (property list value). See below for the names
  310. allowed in this property list.
  311. \yskip\hang\.{LIGTABLE} (property list value). See below for the rules
  312. about this special kind of property list.
  313. \yskip\hang\.{BOUNDARYCHAR} (one-byte value). If this character appears in
  314. a \.{LIGTABLE} command, it matches ``end of word'' as well as itself.
  315. If no boundary character is given and no \.{LABEL} \.{BOUNDARYCHAR} occurs
  316. within \.{LIGTABLE}, word boundaries will not affect ligatures or kerning.
  317. \yskip\hang\.{CHARACTER}. The value is a one-byte integer followed by
  318. a property list. The integer represents the number of a character that is
  319. present in the font; the property list of a character is defined below.
  320. The default is an empty property list.
  321. @ Numeric property list values can be given in various forms identified by
  322. a prefixed letter.
  323. \yskip\hang\.C denotes an ASCII character, which should be a standard visible
  324. character that is not a parenthesis. The numeric value will therefore be
  325. between @'41 and @'176 but not @'50 or @'51.
  326. \yskip\hang\.D denotes an unsigned decimal integer, which must be
  327. less than $2^{32}$, i.e., at most `\.{D 4294967295}'.
  328. \yskip\hang\.F denotes a three-letter Xerox face code; the admissible codes
  329. are \.{MRR}, \.{MIR}, \.{BRR}, \.{BIR}, \.{LRR}, \.{LIR}, \.{MRC}, \.{MIC},
  330. \.{BRC}, \.{BIC}, \.{LRC}, \.{LIC}, \.{MRE}, \.{MIE}, \.{BRE}, \.{BIE},
  331. \.{LRE}, and \.{LIE}, denoting the integers 0 to 17, respectively.
  332. \yskip\hang\.O denotes an unsigned octal integer, which must be less than
  333. $2^{32}$, i.e., at most `\.{O 37777777777}'.
  334. \yskip\hang\.H denotes an unsigned hexadecimal integer, which must be less than
  335. $2^{32}$, i.e., at most `\.{H FFFFFFFF}'.
  336. \yskip\hang\.R denotes a real number in decimal notation, optionally preceded
  337. by a `\.+' or `\.-' sign, and optionally including a decimal point. The
  338. absolute value must be less than 2048.
  339. @ The property names allowed in a \.{FONTDIMEN} property list correspond to
  340. various \TeX\ parameters, each of which has a (real) numeric value. All
  341. of the parameters except \.{SLANT} are in design units. The admissible
  342. names are \.{SLANT}, \.{SPACE}, \.{STRETCH}, \.{SHRINK}, \.{XHEIGHT},
  343. \.{QUAD}, \.{EXTRASPACE}, \.{NUM1}, \.{NUM2}, \.{NUM3}, \.{DENOM1},
  344. \.{DENOM2}, \.{SUP1}, \.{SUP2}, \.{SUP3}, \.{SUB1}, \.{SUB2}, \.{SUPDROP},
  345. \.{SUBDROP}, \.{DELIM1}, \.{DELIM2}, and \.{AXISHEIGHT}, for parameters
  346. 1~to~22. The alternate names \.{DEFAULTRULETHICKNESS},
  347. \.{BIGOPSPACING1}, \.{BIGOPSPACING2}, \.{BIGOPSPACING3},
  348. \.{BIGOPSPACING4}, and \.{BIGOPSPACING5}, may also be used for parameters
  349. 8 to 13.
  350. The notation `\.{PARAMETER} $n$' provides another way to specify the
  351. $n$th parameter; for example, `\.{(PARAMETER} \.{D 1 R -.25)}' is another way
  352. to specify that the \.{SLANT} is $-0.25$. The value of $n$ must be positive
  353. and less than |max_param_words|.
  354. @ The elements of a \.{CHARACTER} property list can be of six different types.
  355. \yskip\hang\.{CHARWD} (real value) denotes the character's width in
  356. design units.
  357. \yskip\hang\.{CHARHT} (real value) denotes the character's height in
  358. design units.
  359. \yskip\hang\.{CHARDP} (real value) denotes the character's depth in
  360. design units.
  361. \yskip\hang\.{CHARIC} (real value) denotes the character's italic correction in
  362. design units.
  363. \yskip\hang\.{NEXTLARGER} (one-byte value), specifies the character that
  364. follows the present one in a ``charlist.'' The value must be the number of a
  365. character in the font, and there must be no infinite cycles of supposedly
  366. larger and larger characters.
  367. \yskip\hang\.{VARCHAR} (property list value), specifies an extensible character.
  368. This option and \.{NEXTLARGER} are mutually exclusive; i.e., they cannot
  369. both be used within the same \.{CHARACTER} list.
  370. \yskip\noindent
  371. The elements of a \.{VARCHAR} property list are either \.{TOP}, \.{MID},
  372. \.{BOT} or \.{REP}; the values are integers, which must be zero or the number
  373. of a character in the font. A zero value for \.{TOP}, \.{MID}, or \.{BOT} means
  374. that the corresponding piece of the extensible character is absent. A nonzero
  375. value, or a \.{REP} value of zero, denotes the character code used to make
  376. up the top, middle, bottom, or replicated piece of an extensible character.
  377. @ A \.{LIGTABLE} property list contains elements of four kinds, specifying a
  378. program in a simple command language that \TeX\ uses for ligatures and kerns.
  379. If several \.{LIGTABLE} lists appear, they are effectively concatenated into
  380. a single list.
  381. \yskip\hang\.{LABEL} (one-byte value) means that the program for the
  382. stated character value starts here. The integer must be the number of a
  383. character in the font; its \.{CHARACTER} property list must not have a
  384. \.{NEXTLARGER} or \.{VARCHAR} field. At least one \.{LIG} or \.{KRN} step
  385. must follow.
  386. \yskip\hang\.{LABEL} \.{BOUNDARYCHAR} means that the program for
  387. beginning-of-word ligatures starts here.
  388. \yskip\hang\.{LIG} (two one-byte values). The instruction `\.{(LIG} $c$ $r$\.)'
  389. means, ``If the next character is $c$, then insert character~$r$ and
  390. possibly delete the current character and/or~$c$;
  391. otherwise go on to the next instruction.''
  392. Characters $r$ and $c$ must be present in the font. \.{LIG} may be immediately
  393. preceded or followed by a slash, and then immediately followed by \.>
  394. characters not exceeding the number of slashes. Thus there are eight
  395. possible forms:
  396. $$\hbox to .8\hsize{\.{LIG}\hfil\.{/LIG}\hfil\.{/LIG>}\hfil
  397. \.{LIG/}\hfil\.{LIG/>}\hfil\.{/LIG/}\hfil\.{/LIG/>}\hfil\.{/LIG/>>}}$$
  398. The slashes specify retention of the left or right original character; the
  399. \.> signs specify passing over the result without further ligature processing.
  400. \yskip\hang\.{KRN} (a one-byte value and a real value). The instruction
  401. `\.{(KRN} $c$ $r$\.)' means, ``If the next character is $c$, then insert
  402. a blank space of width $r$ between the current character character and $c$;
  403. otherwise go on to the next intruction.'' The value of $r$, which is in
  404. units of the design size, is often negative. Character code $c$ must exist
  405. in the font.
  406. \yskip\hang\.{STOP} (no value). This instruction ends a ligature/kern program.
  407. It must follow either a \.{LIG} or \.{KRN} instruction, not a \.{LABEL}
  408. or \.{STOP} or \.{SKIP}.
  409. \yskip\hang\.{SKIP} (value in the range |0..127|). This instruction specifies
  410. continuation of a ligature/kern program after the specified number of \.{LIG}
  411. or \.{KRN} has been skipped over. The number of subsequent \.{LIG} and \.{KRN}
  412. instructions must therefore exceed this specified amount.
  413. @ In addition to all these possibilities, the property name \.{COMMENT} is
  414. allowed in any property list. Such comments are ignored.
  415. @ So that is what \.{PL} files hold. In a \.{VPL} file additional
  416. properties are recognized; two of these are valid on the outermost level:
  417. \yskip\hang\.{VTITLE} (string value, default is empty). The value will be
  418. reproduced at the beginning of the \.{VF} file (and printed on the terminal
  419. by \.{VFtoVP} when it examines that file).
  420. \yskip\hang\.{MAPFONT}. The value is a nonnegative integer followed by
  421. a property list. The integer represents an identifying number for fonts
  422. used in \.{MAP} attributes. The property list, which identifies the font and
  423. relative size, is defined below.
  424. \yskip\noindent
  425. And one additional ``virtual property'' is valid within a \.{CHARACTER}:
  426. \yskip\hang\.{MAP}. The value is a property list consisting of typesetting
  427. commands. Default is the single command \.{SETCHAR}~$c$, where $c$ is
  428. the current character number.
  429. @ The elements of a \.{MAPFONT} property list can be of the following types.
  430. \yskip\hang\.{FONTNAME} (string value, default is \.{NULL}).
  431. This is the font's identifying name.
  432. \yskip\hang\.{FONTAREA} (string value, default is empty). If the font appears
  433. in a nonstandard directory, according to local conventions, the directory
  434. name is given here. (This is system dependent, just as in \.{DVI} files.)
  435. \yskip\hang\.{FONTCHECKSUM} (four-byte value, default is zero). This value,
  436. which should be a nonnegative integer less than $2^{32}$, can be used to
  437. check that the font being referred to matches the intended font. If nonzero,
  438. it should equal the \.{CHECKSUM} parameter in that font.
  439. \yskip\hang\.{FONTAT} (numeric value, default is the \.{DESIGNUNITS} of the
  440. present virtual font). This value is relative to the design units of
  441. the present virtual font, hence it will be scaled when the virtual
  442. font is magnified or reduced.  It represents the value that will
  443. effectively replace the design size of the font being referred to,
  444. so that all characters will be scaled appropriately.
  445. \yskip\hang\.{FONTDSIZE} (numeric value, default is 10). This value is
  446. absolute, in units of printer's points. It should equal the \.{DESIGNSIZE}
  447. parameter in the font being referred to.
  448. \yskip\noindent
  449. If any of the
  450. string values contain parentheses, the parentheses must be balanced. Leading
  451. blanks are removed from the strings, but trailing blanks are not.
  452. @ Finally, the elements of a \.{MAP} property list are an ordered sequence
  453. of typesetting commands chosen from among the following:
  454. \yskip\hang\.{SELECTFONT} (four-byte integer value). The value must be the
  455. number of a previously defined \.{MAPFONT}. This font (or more precisely, the
  456. final font that is mapped to that code number, if two \.{MAPFONT} properties
  457. happen to specify the same code) will be used in subsequent \.{SETCHAR}
  458. instructions until overridden by another \.{SELECTFONT}. The first-specified
  459. \.{MAPFONT} is implicitly selected before the first \.{SELECTFONT} in every
  460. character's map.
  461. \yskip\hang\.{SETCHAR} (one-byte integer value). There must be a character of
  462. this number in the currently selected font. (\.{VPtoVF} doesn't check that
  463. the character is valid, but \.{VFtoVP} does.) That character is typeset at the
  464. current position, and the typesetter moves right by the \.{CHARWD} in
  465. that character's \.{TFM} file.
  466. \yskip\hang\.{SETRULE} (two real values). The first value specifies height,
  467. the second specifies width, in design units. If both height and width are
  468. positive, a rule is typeset at the current position. Then the typesetter
  469. moves right, by the specified width.
  470. \yskip\hang\.{MOVERIGHT}, \.{MOVELEFT}, \.{MOVEUP}, \.{MOVEDOWN} (real
  471. value). The typesetter moves its current position
  472. by the number of design units specified.
  473. \yskip\hang\.{PUSH} The current typesetter position is remembered, to
  474. be restored on a subsequent \.{POP}.
  475. \yskip\hang\.{POP} The current typesetter position is reset to where it
  476. was on the most recent unmatched \.{PUSH}. The \.{PUSH} and \.{POP}
  477. commands in any \.{MAP} must be properly nested like balanced parentheses.
  478. \yskip\hang\.{SPECIAL} (string value). The subsequent characters, starting
  479. with the first nonblank and ending just before the first `\.)' that has no
  480. matching `\.(', are interpreted according to local conventions with the
  481. same system-dependent meaning as a `special' (\\{xxx}) command
  482. in a \.{DVI} file.
  483. \yskip\hang\.{SPECIALHEX} (hexadecimal string value). The subsequent
  484. nonblank characters before the next `\.)' must consist entirely of
  485. hexadecimal digits, and they must contain an even number of such digits.
  486. Each pair of hex digits specifies a byte, and this string of bytes is
  487. treated just as the value of a \.{SPECIAL}. (This convention permits
  488. arbitrary byte strings to be represented in an ordinary text file.)
  489. @ Virtual font mapping is a recursive process, like macro expansion.
  490. Thus, a \.{MAPFONT} might
  491. specify another virtual font, whose characters are themselves mapped to
  492. other fonts. As an example of this possibility, consider the
  493. following curious file called \.{recurse.vpl}, which defines a
  494. virtual font that is self-contained and self-referential:
  495. $$\vbox{\halign{\.{#}\cr
  496. (VTITLE Example of recursion)\cr
  497. (MAPFONT D 0 (FONTNAME recurse)(FONTAT D 2))\cr
  498. (CHARACTER C A (CHARWD D 1)(CHARHT D 1)(MAP (SETRULE D 1 D 1)))\cr
  499. (CHARACTER C B (CHARWD D 2)(CHARHT D 2)(MAP (SETCHAR C A)))\cr
  500. (CHARACTER C C (CHARWD D 4)(CHARHT D 4)(MAP (SETCHAR C B)))\cr
  501. The design size is 10 points (the default), hence the character \.A
  502. in font \.{recurse} is a $10\times10$ point black square. Character \.B
  503. is typeset as character \.A in \.{recurse} {scaled} {2000}, hence it
  504. is a $20\times20$ point black square. And character \.C is typeset as
  505. character \.{B} in \.{recurse} {scaled} {2000}, hence its size is
  506. $40\times40$.
  507. Users are responsible for making sure that infinite recursion doesn't happen.
  508. @ So that is what \.{VPL} files hold. From these rules,
  509. you can guess (correctly) that \.{VPtoVF} operates in four main stages.
  510. First it assigns the default values to all properties; then it scans
  511. through the \.{VPL} file, changing property values as new ones are seen; then
  512. it checks the information and corrects any problems; and finally it outputs
  513. the \.{VF} and \.{TFM} files.
  514. @ The next question is, ``What are \.{VF} and
  515. \.{TFM} files?'' A complete answer to that question appears in the
  516. documentation of the companion programs, \.{VFtoVP} and
  517. \.{TFtoPL}, so the details will not
  518. be repeated here. Suffice it to say that a \.{VF} or
  519. \.{TFM} file stores all of the
  520. relevant font information in a sequence of 8-bit bytes. The number of
  521. bytes is always a multiple of 4, so we could regard the files
  522. as sequences of 32-bit words; but \TeX\ uses the byte interpretation,
  523. and so does \.{VPtoVF}. Note that the bytes are considered to be unsigned
  524. numbers.
  525. @<Glob...@>=
  526. @!vf_file:packed file of 0..255;
  527. @!tfm_file:packed file of 0..255;
  528. @ On some systems you may have to do something special to write a
  529. packed file of bytes. For example, the following code didn't work
  530. when it was first tried at Stanford, because packed files have to be
  531. opened with a special switch setting on the \PASCAL\ that was used.
  532. @^system dependencies@>
  533. @<Set init...@>=
  534. rewrite(vf_file); rewrite(tfm_file);
  535. @* Basic input routines.
  536. For the purposes of this program, a |byte| is an unsigned eight-bit quantity,
  537. and an |ASCII_code| is an integer between @'40 and @'177. Such ASCII codes
  538. correspond to one-character constants like \.{"A"} in \.{WEB} language.
  539. @<Types...@>=
  540. @!byte=0..255; {unsigned eight-bit quantity}
  541. @!ASCII_code=@'40..@'177; {standard ASCII code numbers}
  542. @ One of the things \.{VPtoVF} has to do is convert characters of strings
  543. to ASCII form, since that is the code used for the family name and the
  544. coding scheme in a \.{TFM} file. An array |xord| is used to do the
  545. conversion from |char|; the method below should work with little or no change
  546. on most \PASCAL\ systems.
  547. @^system dependencies@>
  548. @d first_ord=0 {ordinal number of the smallest element of |char|}
  549. @d last_ord=127 {ordinal number of the largest element of |char|}
  550. @<Global...@>=
  551. @!xord:array[char] of ASCII_code; {conversion table}
  552. @ @<Local variables for init...@>=
  553. @!k:integer; {all-purpose initialization index}
  554. @ Characters that should not appear in \.{VPL} files (except in comments)
  555. are mapped into @'177.
  556. @d invalid_code=@'177 {code deserving an error message}
  557. @<Set init...@>=
  558. for k:=first_ord to last_ord do xord[chr(k)]:=invalid_code;
  559. xord[' ']:=" "; xord['!']:="!"; xord['"']:=""""; xord['#']:="#";
  560. xord['$']:="$"; xord['%']:="%"; xord['&']:="&"; xord['''']:="'";
  561. xord['(']:="("; xord[')']:=")"; xord['*']:="*"; xord['+']:="+"; xord[',']:=",";
  562. xord['-']:="-"; xord['.']:="."; xord['/']:="/"; xord['0']:="0"; xord['1']:="1";
  563. xord['2']:="2"; xord['3']:="3"; xord['4']:="4"; xord['5']:="5"; xord['6']:="6";
  564. xord['7']:="7"; xord['8']:="8"; xord['9']:="9"; xord[':']:=":"; xord[';']:=";";
  565. xord['<']:="<"; xord['=']:="="; xord['>']:=">"; xord['?']:="?";
  566. xord['@@']:="@@"; xord['A']:="A"; xord['B']:="B"; xord['C']:="C";
  567. xord['D']:="D"; xord['E']:="E"; xord['F']:="F"; xord['G']:="G"; xord['H']:="H";
  568. xord['I']:="I"; xord['J']:="J"; xord['K']:="K"; xord['L']:="L"; xord['M']:="M";
  569. xord['N']:="N"; xord['O']:="O"; xord['P']:="P"; xord['Q']:="Q"; xord['R']:="R";
  570. xord['S']:="S"; xord['T']:="T"; xord['U']:="U"; xord['V']:="V"; xord['W']:="W";
  571. xord['X']:="X"; xord['Y']:="Y"; xord['Z']:="Z"; xord['[']:="["; xord['\']:="\";
  572. xord[']']:="]"; xord['^']:="^"; xord['_']:="_"; xord['`']:="`"; xord['a']:="a";
  573. xord['b']:="b"; xord['c']:="c"; xord['d']:="d"; xord['e']:="e"; xord['f']:="f";
  574. xord['g']:="g"; xord['h']:="h"; xord['i']:="i"; xord['j']:="j"; xord['k']:="k";
  575. xord['l']:="l"; xord['m']:="m"; xord['n']:="n"; xord['o']:="o"; xord['p']:="p";
  576. xord['q']:="q"; xord['r']:="r"; xord['s']:="s"; xord['t']:="t"; xord['u']:="u";
  577. xord['v']:="v"; xord['w']:="w"; xord['x']:="x"; xord['y']:="y"; xord['z']:="z";
  578. xord['{']:="{"; xord['|']:="|"; xord['}']:="}"; xord['~']:="~";
  579. @ In order to help catch errors of badly nested parentheses, \.{VPtoVF}
  580. assumes that the user will begin each line with a number of blank spaces equal
  581. to some constant times the number of open parentheses at the beginning of
  582. that line. However, the program doesn't know in advance what the constant
  583. is, nor does it want to print an error message on every line for a user
  584. who has followed no consistent pattern of indentation.
  585. Therefore the following strategy is adopted: If the user has been consistent
  586. with indentation for ten or more lines, an indentation error will be
  587. reported. The constant of indentation is reset on every line that should
  588. have nonzero indentation.
  589. @<Glob...@>=
  590. @!line:integer; {the number of the current line}
  591. @!good_indent:integer; {the number of lines since the last bad indentation}
  592. @!indent: integer; {the number of spaces per open parenthesis, zero if unknown}
  593. @!level: integer; {the current number of open parentheses}
  594. @ @<Set init...@>=
  595. line:=0; good_indent:=0; indent:=0; level:=0;
  596. @ The input need not really be broken into lines of any maximum length, and
  597. we could read it character by character without any buffering. But we shall
  598. place it into a small buffer so that offending lines can be displayed in error
  599. messages.
  600. @<Glob...@>=
  601. @!left_ln,@!right_ln:boolean; {are the left and right ends of the buffer
  602.   at end-of-line marks?}
  603. @!limit:0..buf_size; {position of the last character present in the buffer}
  604. @!loc:0..buf_size; {position of the last character read in the buffer}
  605. @!buffer:array[1..buf_size] of char;
  606. @!input_has_ended:boolean; {there is no more input to read}
  607. @ @<Set init...@>=
  608. limit:=0; loc:=0; left_ln:=true; right_ln:=true; input_has_ended:=false;
  609. @ Just before each  \.{CHARACTER} property list is evaluated, the character
  610. code is printed in octal notation. Up to eight such codes appear on a line;
  611. so we have a variable to keep track of how many are currently there.
  612. @<Glob...@>=
  613. @!chars_on_line:0..8; {the number of characters printed on the current line}
  614. @ @<Set init...@>=
  615. chars_on_line:=0;
  616. @ The following routine prints an error message and an indication of
  617. where the error was detected. The error message should not include any
  618. final punctuation, since this procedure supplies its own.
  619. @d err_print(#)==begin if chars_on_line>0 then print_ln(' ');
  620.   print(#); show_error_context;
  621.   end
  622. @p procedure show_error_context; {prints the current scanner location}
  623. var k:0..buf_size; {an index into |buffer|}
  624. begin print_ln(' (line ',line:1,').');
  625. if not left_ln then print('...');
  626. for k:=1 to loc do print(buffer[k]); {print the characters already scanned}
  627. print_ln(' ');
  628. if not left_ln then print('   ');
  629. for k:=1 to loc do print(' '); {space out the second line}
  630. for k:=loc+1 to limit do print(buffer[k]); {print the characters yet unseen}
  631. if right_ln then print_ln(' ')@+else print_ln('...');
  632. chars_on_line:=0;
  633. @ Here is a procedure that does the right thing when we are done
  634. reading the present contents of the buffer. It keeps |buffer[buf_size]|
  635. empty, in order to avoid range errors on certain \PASCAL\ compilers.
  636. An infinite sequence of right parentheses is placed at the end of the
  637. file, so that the program is sure to get out of whatever level of nesting
  638. it is in.
  639. On some systems it is desirable to modify this code so that tab marks
  640. in the buffer are replaced by blank spaces. (Simply setting
  641. |xord[chr(@'11)]:=" "| would not work; for example, two-line
  642. error messages would not come out properly aligned.)
  643. @^system dependencies@>
  644. @p procedure fill_buffer;
  645. begin left_ln:=right_ln; limit:=0; loc:=0;
  646. if left_ln then
  647.   begin if line>0 then read_ln(vpl_file);
  648.   incr(line);
  649.   end;
  650. if eof(vpl_file) then
  651.   begin limit:=1; buffer[1]:=')'; right_ln:=false; input_has_ended:=true;
  652.   end
  653. else  begin while (limit<buf_size-1)and(not eoln(vpl_file)) do
  654.     begin incr(limit); read(vpl_file,buffer[limit]);
  655.     end;
  656.   buffer[limit+1]:=' '; right_ln:=eoln(vpl_file);
  657.   if left_ln then @<Set |loc| to the number of leading blanks in
  658.     the buffer, and check the indentation@>;
  659.   end;
  660. @ The interesting part about |fill_buffer| is the part that learns what
  661. indentation conventions the user is following, if any.
  662. @d bad_indent(#)==begin if good_indent>=10 then err_print(#);
  663.   good_indent:=0; indent:=0;
  664.   end
  665. @<Set |loc|...@>=
  666. begin while (loc<limit)and(buffer[loc+1]=' ') do incr(loc);
  667. if loc<limit then
  668.   begin if level=0 then
  669.     if loc=0 then incr(good_indent)
  670.     else bad_indent('Warning: Indented line occurred at level zero')
  671. @.Warning: Indented line...@>
  672.   else if indent=0 then
  673.     if loc mod level=0 then
  674.       begin indent:=loc div level; good_indent:=1;
  675.       end
  676.     else good_indent:=0
  677.   else if indent*level=loc then incr(good_indent)
  678.   else bad_indent('Warning: Inconsistent indentation; ',
  679. @.Warning: Inconsistent indentation...@>
  680.     'you are at parenthesis level ',level:1);
  681.   end;
  682. @* Basic scanning routines.
  683. The global variable |cur_char| holds the ASCII code corresponding to the
  684. character most recently read from the input buffer, or to a character that
  685. has been substituted for the real one.
  686. @<Global...@>=
  687. @!cur_char:ASCII_code; {we have just read this}
  688. @ Here is a procedure that sets |cur_char| to an ASCII code for the
  689. next character of input, if that character is a letter or digit or slash
  690. or \.>. Otherwise
  691. it sets |cur_char:=" "|, and the input system will be poised to reread the
  692. character that was rejected, whether or not it was a space.
  693. Lower case letters are converted to upper case.
  694. @p procedure get_keyword_char;
  695. begin while (loc=limit)and(not right_ln) do fill_buffer;
  696. if loc=limit then cur_char:=" " {end-of-line counts as a delimiter}
  697. else  begin cur_char:=xord[buffer[loc+1]];
  698.   if cur_char>="a" then cur_char:=cur_char-@'40;
  699.   if ((cur_char>="0")and(cur_char<="9")) then incr(loc)
  700.   else if ((cur_char>="A")and(cur_char<="Z")) then incr(loc)
  701.   else if cur_char="/" then incr(loc)
  702.   else if cur_char=">" then incr(loc)
  703.   else cur_char:=" ";
  704.   end;
  705. @ The following procedure sets |cur_char| to the next character code,
  706. and converts lower case to upper case. If the character is a left or
  707. right parenthesis, it will not be ``digested''; the character will
  708. be read again and again, until the calling routine does something
  709. like `|incr(loc)|' to get past it. Such special treatment of parentheses
  710. insures that the structural information they contain won't be lost in
  711. the midst of other error recovery operations.
  712. @d backup==begin if (cur_char>")")or(cur_char<"(") then decr(loc);
  713.   end {undoes the effect of |get_next|}
  714. @p procedure get_next; {sets |cur_char| to next, balks at parentheses}
  715. begin while loc=limit do fill_buffer;
  716. incr(loc); cur_char:=xord[buffer[loc]];
  717. if cur_char>="a" then
  718.   if cur_char<="z" then cur_char:=cur_char-@'40 {uppercasify}
  719.   else  begin if cur_char=invalid_code then
  720.       begin err_print('Illegal character in the file');
  721. @.Illegal character...@>
  722.       cur_char:="?";
  723.       end;
  724.     end
  725. else if (cur_char<=")")and(cur_char>="(") then decr(loc);
  726. @ Here's a procedure that scans a hexadecimal digit or a right parenthesis.
  727. @p function get_hex:byte;
  728. var @!a:integer; {partial result}
  729. begin repeat get_next;
  730. until cur_char<>" ";
  731. a:=cur_char-")";
  732. if a>0 then
  733.   begin a:=cur_char-"0";
  734.   if cur_char>"9" then
  735.     if cur_char<"A" then a:=-1 else a:=cur_char-"A"+10;
  736.   end;
  737. if (a<0)or(a>15) then
  738.   begin err_print('Illegal hexadecimal digit'); get_hex:=0;
  739. @.Illegal hexadecimal digit@>
  740.   end
  741. else get_hex:=a;
  742. @ The next procedure is used to ignore the text of a comment, or to pass over
  743. erroneous material. As such, it has the privilege of passing parentheses.
  744. It stops after the first right parenthesis that drops the level below
  745. the level in force when the procedure was called.
  746. @p procedure skip_to_end_of_item;
  747. var l:integer; {initial value of |level|}
  748. begin l:=level;
  749. while level>=l do
  750.   begin while loc=limit do fill_buffer;
  751.   incr(loc);
  752.   if buffer[loc]=')' then decr(level)
  753.   else if buffer[loc]='(' then incr(level);
  754.   end;
  755. if input_has_ended then err_print('File ended unexpectedly: No closing ")"');
  756. @.File ended unexpectedly...@>
  757. cur_char:=" "; {now the right parenthesis has been read and digested}
  758. @ A similar procedure copies the bytes remaining in an item. The copied bytes
  759. go into an array |vf| that we'll declare later. Leading blanks are ignored.
  760. @d vf_store(#)==
  761.     begin vf[vf_ptr]:=#;
  762.     if vf_ptr=vf_size then err_print('I''m out of memory---increase my vfsize!')
  763. @.I'm out of memory...@>
  764.     else incr(vf_ptr);
  765.     end
  766. @p procedure copy_to_end_of_item;
  767. label 30;
  768. var l:integer; {initial value of |level|}
  769. @!nonblank_found:boolean; {have we seen a nonblank character yet?}
  770. begin l:=level; nonblank_found:=false;
  771. while true do
  772.   begin while loc=limit do fill_buffer;
  773.   if buffer[loc+1]=')' then
  774.     if level=l then goto 30@+else decr(level);
  775.   incr(loc);
  776.   if buffer[loc]='(' then incr(level);
  777.   if buffer[loc]<>' ' then nonblank_found:=true;
  778.   if nonblank_found then
  779.     if xord[buffer[loc]]=invalid_code then
  780.       begin err_print('Illegal character in the file');
  781. @.Illegal character...@>
  782.       vf_store("?");
  783.       end
  784.     else vf_store(xord[buffer[loc]]);
  785.   end;
  786. 30:end;
  787. @ Sometimes we merely want to skip past characters in the input until we
  788. reach a left or a right parenthesis. For example, we do this whenever we
  789. have finished scanning a property value and we hope that a right parenthesis
  790. is next (except for possible blank spaces).
  791. @d skip_to_paren==repeat get_next@;@+ until (cur_char="(")or(cur_char=")")
  792. @d skip_error(#)==begin err_print(#); skip_to_paren;
  793.   end {this gets to the right parenthesis if something goes wrong}
  794. @d flush_error(#)==begin err_print(#); skip_to_end_of_item;
  795.   end {this gets past the right parenthesis if something goes wrong}
  796. @ After a property value has been scanned, we want to move just past the
  797. right parenthesis that should come next in the input (except for possible
  798. blank spaces).
  799. @p procedure finish_the_property; {do this when the value has been scanned}
  800. begin while cur_char=" " do get_next;
  801. if cur_char<>")" then err_print('Junk after property value will be ignored');
  802. @.Junk after property value...@>
  803. skip_to_end_of_item;
  804. @* Scanning property names.
  805. We have to figure out the meaning of names that appear in the \.{VPL} file,
  806. by looking them up in a dictionary of known keywords. Keyword number $n$
  807. appears in locations |start[n]| through |start[n+1]-1| of an array called
  808. |dictionary|.
  809. @d max_name_index=100 {upper bound on the number of keywords}
  810. @d max_letters=666 {upper bound on the total length of all keywords}
  811. @<Global...@>=
  812. @!start:array[1..max_name_index] of 0..max_letters;
  813. @!dictionary:array[0..max_letters] of ASCII_code;
  814. @!start_ptr:0..max_name_index; {the first available place in |start|}
  815. @!dict_ptr:0..max_letters; {the first available place in |dictionary|}
  816. @ @<Set init...@>=
  817. start_ptr:=1; start[1]:=0; dict_ptr:=0;
  818. @ When we are looking for a name, we put it into the |cur_name| array.
  819. When we have found it, the corresponding |start| index will go into
  820. the global variable |name_ptr|.
  821. @d longest_name=20 {length of \.{DEFAULTRULETHICKNESS}}
  822. @<Glob...@>=
  823. @!cur_name:array[1..longest_name] of ASCII_code; {a name to look up}
  824. @!name_length:0..longest_name; {its length}
  825. @!name_ptr:0..max_name_index; {its ordinal number in the dictionary}
  826. @ A conventional hash table with linear probing (cf.\ Algorithm 6.4L
  827. in {\sl The Art of Computer Pro\-gram\-ming\/}) is used for the dictionary
  828. operations. If |nhash[h]=0|, the table position is empty, otherwise |nhash[h]|
  829. points into the |start| array.
  830. @d hash_prime=141 {size of the hash table}
  831. @<Glob...@>=
  832. @!nhash:array[0..hash_prime-1] of 0..max_name_index;
  833. @!cur_hash:0..hash_prime-1; {current position in the hash table}
  834. @ @<Local...@>=
  835. @!h:0..hash_prime-1; {runs through the hash table}
  836. @ @<Set init...@>=
  837. for h:=0 to hash_prime-1 do nhash[h]:=0;
  838. @ Since there is no chance of the hash table overflowing, the procedure
  839. is very simple. After |lookup| has done its work, |cur_hash| will point
  840. to the place where the given name was found, or where it should be inserted.
  841. @p procedure lookup; {finds |cur_name| in the dictionary}
  842. var k:0..longest_name; {index into |cur_name|}
  843. @!j:0..max_letters; {index into |dictionary|}
  844. @!not_found:boolean; {clumsy thing necessary to avoid |goto| statement}
  845. begin @<Compute the hash code, |cur_hash|, for |cur_name|@>;
  846. not_found:=true;
  847. while not_found do
  848.   begin if cur_hash=0 then cur_hash:=hash_prime-1@+else decr(cur_hash);
  849.   if nhash[cur_hash]=0 then not_found:=false
  850.   else  begin j:=start[nhash[cur_hash]];
  851.     if start[nhash[cur_hash]+1]=j+name_length then
  852.       begin not_found:=false;
  853.       for k:=1 to name_length do
  854.       if dictionary[j+k-1]<>cur_name[k] then not_found:=true;
  855.       end;
  856.     end;
  857.   end;
  858. name_ptr:=nhash[cur_hash];
  859. @ @<Compute the hash...@>=
  860. cur_hash:=cur_name[1];
  861. for k:=2 to name_length do
  862.   cur_hash:=(cur_hash+cur_hash+cur_name[k]) mod hash_prime
  863. @ The ``meaning'' of the keyword that begins at |start[k]| in the
  864. dictionary is kept in |equiv[k]|. The numeric |equiv| codes are given
  865. symbolic meanings by the following definitions.
  866. @d comment_code=0
  867. @d check_sum_code=1
  868. @d design_size_code=2
  869. @d design_units_code=3
  870. @d coding_scheme_code=4
  871. @d family_code=5
  872. @d face_code=6
  873. @d seven_bit_safe_flag_code=7
  874. @d header_code= 8
  875. @d font_dimen_code=9
  876. @d lig_table_code=10
  877. @d boundary_char_code=11
  878. @d virtual_title_code=12
  879. @d map_font_code=13
  880. @d character_code=14
  881. @d font_name_code=20
  882. @d font_area_code=21
  883. @d font_checksum_code=22
  884. @d font_at_code=23
  885. @d font_dsize_code=24
  886. @d parameter_code=30
  887. @d char_info_code=60
  888. @d width=1
  889. @d height=2
  890. @d depth=3
  891. @d italic=4
  892. @d char_wd_code=char_info_code+width
  893. @d char_ht_code=char_info_code+height
  894. @d char_dp_code=char_info_code+depth
  895. @d char_ic_code=char_info_code+italic
  896. @d next_larger_code=65
  897. @d map_code=66
  898. @d var_char_code=67
  899. @d select_font_code=80
  900. @d set_char_code=81
  901. @d set_rule_code=82
  902. @d move_right_code=83
  903. @d move_down_code=85
  904. @d push_code=87
  905. @d pop_code=88
  906. @d special_code=89
  907. @d special_hex_code=90
  908. @d label_code=100
  909. @d stop_code=101
  910. @d skip_code=102
  911. @d krn_code=103
  912. @d lig_code=104
  913. @<Glo...@>=
  914. @!equiv:array[0..max_name_index] of byte;
  915. @!cur_code:byte; {equivalent most recently found in |equiv|}
  916. @ We have to get the keywords into the hash table and into the dictionary in
  917. the first place (sigh). The procedure that does this has the desired
  918. |equiv| code as a parameter. In order to facilitate \.{WEB} macro writing
  919. for the initialization, the keyword being initialized is placed into the
  920. last positions of |cur_name|, instead of the first positions.
  921. @p procedure enter_name(v:byte); {|cur_name| goes into the dictionary}
  922. var k:0..longest_name;
  923. begin for k:=1 to name_length do
  924.   cur_name[k]:=cur_name[k+longest_name-name_length];
  925. {now the name has been shifted into the correct position}
  926. lookup; {this sets |cur_hash| to the proper insertion place}
  927. nhash[cur_hash]:=start_ptr; equiv[start_ptr]:=v;
  928. for k:=1 to name_length do
  929.   begin dictionary[dict_ptr]:=cur_name[k]; incr(dict_ptr);
  930.   end;
  931. incr(start_ptr); start[start_ptr]:=dict_ptr;
  932. @ Here are the macros to load a name of up to 20 letters into the
  933. dictionary. For example, the macro |load5| is used for five-letter keywords.
  934. @d tail(#)==enter_name(#)
  935. @d t20(#)==cur_name[20]:=#;tail
  936. @d t19(#)==cur_name[19]:=#;t20
  937. @d t18(#)==cur_name[18]:=#;t19
  938. @d t17(#)==cur_name[17]:=#;t18
  939. @d t16(#)==cur_name[16]:=#;t17
  940. @d t15(#)==cur_name[15]:=#;t16
  941. @d t14(#)==cur_name[14]:=#;t15
  942. @d t13(#)==cur_name[13]:=#;t14
  943. @d t12(#)==cur_name[12]:=#;t13
  944. @d t11(#)==cur_name[11]:=#;t12
  945. @d t10(#)==cur_name[10]:=#;t11
  946. @d t9(#)==cur_name[9]:=#;t10
  947. @d t8(#)==cur_name[8]:=#;t9
  948. @d t7(#)==cur_name[7]:=#;t8
  949. @d t6(#)==cur_name[6]:=#;t7
  950. @d t5(#)==cur_name[5]:=#;t6
  951. @d t4(#)==cur_name[4]:=#;t5
  952. @d t3(#)==cur_name[3]:=#;t4
  953. @d t2(#)==cur_name[2]:=#;t3
  954. @d t1(#)==cur_name[1]:=#;t2
  955. @d load3==name_length:=3;t18
  956. @d load4==name_length:=4;t17
  957. @d load5==name_length:=5;t16
  958. @d load6==name_length:=6;t15
  959. @d load7==name_length:=7;t14
  960. @d load8==name_length:=8;t13
  961. @d load9==name_length:=9;t12
  962. @d load10==name_length:=10;t11
  963. @d load11==name_length:=11;t10
  964. @d load12==name_length:=12;t9
  965. @d load13==name_length:=13;t8
  966. @d load14==name_length:=14;t7
  967. @d load15==name_length:=15;t6
  968. @d load16==name_length:=16;t5
  969. @d load17==name_length:=17;t4
  970. @d load18==name_length:=18;t3
  971. @d load19==name_length:=19;t2
  972. @d load20==name_length:=20;t1
  973. @ (Thank goodness for keyboard macros in the text editor used to create this
  974. \.{WEB} file.)
  975. @<Enter all the \.{PL} names and their equivalents,
  976.  except the parameter names@>=
  977. equiv[0]:=comment_code; {this is used after unknown keywords}
  978. load8("C")("H")("E")("C")("K")("S")("U")("M")(check_sum_code);@/
  979. load10("D")("E")("S")("I")("G")("N")("S")("I")("Z")("E")(design_size_code);@/
  980. load11("D")("E")("S")("I")("G")("N")
  981.   ("U")("N")("I")("T")("S")(design_units_code);@/
  982. load12("C")("O")("D")("I")("N")("G")
  983.   ("S")("C")("H")("E")("M")("E")(coding_scheme_code);@/
  984. load6("F")("A")("M")("I")("L")("Y")(family_code);@/
  985. load4("F")("A")("C")("E")(face_code);@/
  986. load16("S")("E")("V")("E")("N")("B")("I")("T")@/@t\hskip2em@>
  987.   ("S")("A")("F")("E")("F")("L")("A")("G")(seven_bit_safe_flag_code);@/
  988. load6("H")("E")("A")("D")("E")("R")(header_code);@/
  989. load9("F")("O")("N")("T")("D")("I")("M")("E")("N")(font_dimen_code);@/
  990. load8("L")("I")("G")("T")("A")("B")("L")("E")(lig_table_code);@/
  991. load12("B")("O")("U")("N")("D")("A")("R")("Y")("C")("H")("A")("R")
  992.   (boundary_char_code);@/
  993. load9("C")("H")("A")("R")("A")("C")("T")("E")("R")(character_code);@/
  994. load9("P")("A")("R")("A")("M")("E")("T")("E")("R")(parameter_code);@/
  995. load6("C")("H")("A")("R")("W")("D")(char_wd_code);@/
  996. load6("C")("H")("A")("R")("H")("T")(char_ht_code);@/
  997. load6("C")("H")("A")("R")("D")("P")(char_dp_code);@/
  998. load6("C")("H")("A")("R")("I")("C")(char_ic_code);@/
  999. load10("N")("E")("X")("T")("L")("A")("R")("G")("E")("R")(next_larger_code);@/
  1000. load7("V")("A")("R")("C")("H")("A")("R")(var_char_code);@/
  1001. load3("T")("O")("P")(var_char_code+1);@/
  1002. load3("M")("I")("D")(var_char_code+2);@/
  1003. load3("B")("O")("T")(var_char_code+3);@/
  1004. load3("R")("E")("P")(var_char_code+4);@/
  1005. load3("E")("X")("T")(var_char_code+4); {compatibility with older \.{PL} format}
  1006. load7("C")("O")("M")("M")("E")("N")("T")(comment_code);@/
  1007. load5("L")("A")("B")("E")("L")(label_code);@/
  1008. load4("S")("T")("O")("P")(stop_code);@/
  1009. load4("S")("K")("I")("P")(skip_code);@/
  1010. load3("K")("R")("N")(krn_code);@/
  1011. load3("L")("I")("G")(lig_code);@/
  1012. load4("/")("L")("I")("G")(lig_code+2);@/
  1013. load5("/")("L")("I")("G")(">")(lig_code+6);@/
  1014. load4("L")("I")("G")("/")(lig_code+1);@/
  1015. load5("L")("I")("G")("/")(">")(lig_code+5);@/
  1016. load5("/")("L")("I")("G")("/")(lig_code+3);@/
  1017. load6("/")("L")("I")("G")("/")(">")(lig_code+7);@/
  1018. load7("/")("L")("I")("G")("/")(">")(">")(lig_code+11);@/
  1019. @ \.{VPL} files may contain the following in addition to the \.{PL} names.
  1020. @<Enter all the \.{VPL} names@>=
  1021. load6("V")("T")("I")("T")("L")("E")(virtual_title_code);@/
  1022. load7("M")("A")("P")("F")("O")("N")("T")(map_font_code);@/
  1023. load3("M")("A")("P")(map_code);@/
  1024. load8("F")("O")("N")("T")("N")("A")("M")("E")(font_name_code);@/
  1025. load8("F")("O")("N")("T")("A")("R")("E")("A")(font_area_code);@/
  1026. load12("F")("O")("N")("T")
  1027.  ("C")("H")("E")("C")("K")("S")("U")("M")(font_checksum_code);@/
  1028. load6("F")("O")("N")("T")("A")("T")(font_at_code);@/
  1029. load9("F")("O")("N")("T")("D")("S")("I")("Z")("E")(font_dsize_code);@/
  1030. load10("S")("E")("L")("E")("C")("T")("F")("O")("N")("T")(select_font_code);@/
  1031. load7("S")("E")("T")("C")("H")("A")("R")(set_char_code);@/
  1032. load7("S")("E")("T")("R")("U")("L")("E")(set_rule_code);@/
  1033. load9("M")("O")("V")("E")("R")("I")("G")("H")("T")(move_right_code);@/
  1034. load8("M")("O")("V")("E")("L")("E")("F")("T")(move_right_code+1);@/
  1035. load8("M")("O")("V")("E")("D")("O")("W")("N")(move_down_code);@/
  1036. load6("M")("O")("V")("E")("U")("P")(move_down_code+1);@/
  1037. load4("P")("U")("S")("H")(push_code);@/
  1038. load3("P")("O")("P")(pop_code);@/
  1039. load7("S")("P")("E")("C")("I")("A")("L")(special_code);@/
  1040. load10("S")("P")("E")("C")("I")("A")("L")("H")("E")("X")(special_hex_code);@/
  1041. @ @<Enter the parameter names@>=
  1042. load5("S")("L")("A")("N")("T")(parameter_code+1);@/
  1043. load5("S")("P")("A")("C")("E")(parameter_code+2);@/
  1044. load7("S")("T")("R")("E")("T")("C")("H")(parameter_code+3);@/
  1045. load6("S")("H")("R")("I")("N")("K")(parameter_code+4);@/
  1046. load7("X")("H")("E")("I")("G")("H")("T")(parameter_code+5);@/
  1047. load4("Q")("U")("A")("D")(parameter_code+6);@/
  1048. load10("E")("X")("T")("R")("A")("S")("P")("A")("C")("E")(parameter_code+7);@/
  1049. load4("N")("U")("M")("1")(parameter_code+8);@/
  1050. load4("N")("U")("M")("2")(parameter_code+9);@/
  1051. load4("N")("U")("M")("3")(parameter_code+10);@/
  1052. load6("D")("E")("N")("O")("M")("1")(parameter_code+11);@/
  1053. load6("D")("E")("N")("O")("M")("2")(parameter_code+12);@/
  1054. load4("S")("U")("P")("1")(parameter_code+13);@/
  1055. load4("S")("U")("P")("2")(parameter_code+14);@/
  1056. load4("S")("U")("P")("3")(parameter_code+15);@/
  1057. load4("S")("U")("B")("1")(parameter_code+16);@/
  1058. load4("S")("U")("B")("2")(parameter_code+17);@/
  1059. load7("S")("U")("P")("D")("R")("O")("P")(parameter_code+18);@/
  1060. load7("S")("U")("B")("D")("R")("O")("P")(parameter_code+19);@/
  1061. load6("D")("E")("L")("I")("M")("1")(parameter_code+20);@/
  1062. load6("D")("E")("L")("I")("M")("2")(parameter_code+21);@/
  1063. load10("A")("X")("I")("S")("H")("E")("I")("G")("H")("T")(parameter_code+22);@/
  1064. load20("D")("E")("F")("A")("U")("L")("T")("R")("U")("L")("E")@/@t\hskip2em@>
  1065.   ("T")("H")("I")("C")("K")("N")("E")("S")("S")(parameter_code+8);@/
  1066. load13("B")("I")("G")("O")("P")
  1067.   ("S")("P")("A")("C")("I")("N")("G")("1")(parameter_code+9);@/
  1068. load13("B")("I")("G")("O")("P")
  1069.   ("S")("P")("A")("C")("I")("N")("G")("2")(parameter_code+10);@/
  1070. load13("B")("I")("G")("O")("P")
  1071.   ("S")("P")("A")("C")("I")("N")("G")("3")(parameter_code+11);@/
  1072. load13("B")("I")("G")("O")("P")
  1073.   ("S")("P")("A")("C")("I")("N")("G")("4")(parameter_code+12);@/
  1074. load13("B")("I")("G")("O")("P")
  1075.   ("S")("P")("A")("C")("I")("N")("G")("5")(parameter_code+13);@/
  1076. @ When a left parenthesis has been scanned, the following routine
  1077. is used to interpret the keyword that follows, and to store the
  1078. equivalent value in |cur_code|.
  1079. @p procedure get_name;
  1080. begin incr(loc); incr(level); {pass the left parenthesis}
  1081. cur_char:=" ";
  1082. while cur_char=" " do get_next;
  1083. if (cur_char>")")or(cur_char<"(") then decr(loc); {back up one character}
  1084. name_length:=0; get_keyword_char; {prepare to scan the name}
  1085. while cur_char<>" " do
  1086.   begin if name_length=longest_name then cur_name[1]:="X" {force error}
  1087.   else incr(name_length);
  1088.   cur_name[name_length]:=cur_char;
  1089.   get_keyword_char;
  1090.   end;
  1091. lookup;
  1092. if name_ptr=0 then err_print('Sorry, I don''t know that property name');
  1093. @.Sorry, I don't know...@>
  1094. cur_code:=equiv[name_ptr];
  1095. @* Scanning numeric data.
  1096. The next thing we need is a trio of subroutines to read the one-byte,
  1097. four-byte, and real numbers that may appear as property values.
  1098. These subroutines are careful to stick to numbers between $-2^{31}$
  1099. and $2^{31}-1$, inclusive, so that a computer with two's complement
  1100. 32-bit arithmetic will not be interrupted by overflow.
  1101. @ The first number scanner, which returns a one-byte value, surely has
  1102. no problems of arithmetic overflow.
  1103. @p function get_byte:byte; {scans a one-byte property value}
  1104. var acc:integer; {an accumulator}
  1105. @!t:ASCII_code; {the type of value to be scanned}
  1106. begin repeat get_next;
  1107. until cur_char<>" "; {skip the blanks before the type code}
  1108. t:=cur_char; acc:=0;
  1109. repeat get_next;
  1110. until cur_char<>" "; {skip the blanks after the type code}
  1111. if t="C" then @<Scan an ASCII character code@>
  1112. else if t="D" then @<Scan a small decimal number@>
  1113. else if t="O" then @<Scan a small octal number@>
  1114. else if t="H" then @<Scan a small hexadecimal number@>
  1115. else if t="F" then @<Scan a face code@>
  1116. else skip_error('You need "C" or "D" or "O" or "H" or "F" here');
  1117. @.You need "C" or "D" ...here@>
  1118. cur_char:=" "; get_byte:=acc;
  1119. @ The |get_next| routine converts lower case to upper case, but it leaves
  1120. the character in the buffer, so we can unconvert it.
  1121. @<Scan an ASCII...@>=
  1122. if (cur_char>=@'41)and(cur_char<=@'176)and
  1123.  ((cur_char<"(")or(cur_char>")")) then
  1124.   acc:=xord[buffer[loc]]
  1125. else skip_error('"C" value must be standard ASCII and not a paren')
  1126. @:C value}\.{"C" value must be...@>
  1127. @ @<Scan a small dec...@>=
  1128. begin while (cur_char>="0")and(cur_char<="9") do
  1129.   begin acc:=acc*10+cur_char-"0";
  1130.   if acc>255 then
  1131.     begin skip_error('This value shouldn''t exceed 255');
  1132. @.This value shouldn't...@>
  1133.     acc:=0; cur_char:=" ";
  1134.     end
  1135.   else get_next;
  1136.   end;
  1137. backup;
  1138. @ @<Scan a small oct...@>=
  1139. begin while (cur_char>="0")and(cur_char<="7") do
  1140.   begin acc:=acc*8+cur_char-"0";
  1141.   if acc>255 then
  1142.     begin skip_error('This value shouldn''t exceed ''377');
  1143. @.This value shouldn't...@>
  1144.     acc:=0; cur_char:=" ";
  1145.     end
  1146.   else get_next;
  1147.   end;
  1148. backup;
  1149. @ @<Scan a small hex...@>=
  1150. begin while ((cur_char>="0")and(cur_char<="9"))or
  1151.    ((cur_char>="A")and(cur_char<="F")) do
  1152.   begin if cur_char>="A" then cur_char:=cur_char+"0"+10-"A";
  1153.   acc:=acc*16+cur_char-"0";
  1154.   if acc>255 then
  1155.     begin skip_error('This value shouldn''t exceed "FF');
  1156. @.This value shouldn't...@>
  1157.     acc:=0; cur_char:=" ";
  1158.     end
  1159.   else get_next;
  1160.   end;
  1161. backup;
  1162. @ @<Scan a face...@>=
  1163. begin if cur_char="B" then acc:=2
  1164. else if cur_char="L" then acc:=4
  1165. else if cur_char<>"M" then acc:=18;
  1166. get_next;
  1167. if cur_char="I" then incr(acc)
  1168. else if cur_char<>"R" then acc:=18;
  1169. get_next;
  1170. if cur_char="C" then acc:=acc+6
  1171. else if cur_char="E" then acc:=acc+12
  1172. else if cur_char<>"R" then acc:=18;
  1173. if acc>=18 then
  1174.   begin skip_error('Illegal face code, I changed it to MRR');
  1175. @.Illegal face code...@>
  1176.   acc:=0;
  1177.   end;
  1178. @ The routine that scans a four-byte value puts its output into |cur_bytes|,
  1179. which is a record containing (yes, you guessed it) four bytes.
  1180. @<Types...@>=
  1181. @!four_bytes=record @!b0:byte;@+@!b1:byte;@+@!b2:byte;@+@!b3:byte;@+end;
  1182. @ @d c0==cur_bytes.b0
  1183. @d c1==cur_bytes.b1
  1184. @d c2==cur_bytes.b2
  1185. @d c3==cur_bytes.b3
  1186. @<Glob...@>=
  1187. @!cur_bytes:four_bytes; {a four-byte accumulator}
  1188. @!zero_bytes:four_bytes; {four bytes all zero}
  1189. @ @<Set init...@>=
  1190. zero_bytes.b0:=0; zero_bytes.b1:=0; zero_bytes.b2:=0; zero_bytes.b3:=0;
  1191. @ Since the |get_four_bytes| routine is used very infrequently, no attempt
  1192. has been made to make it fast; we only want it to work.
  1193. @p procedure get_four_bytes; {scans an unsigned constant and sets |four_bytes|}
  1194. var c:integer; {local two-byte accumulator}
  1195. @!r:integer; {radix}
  1196. begin repeat get_next;
  1197. until cur_char<>" "; {skip the blanks before the type code}
  1198. r:=0; cur_bytes:=zero_bytes; {start with the accumulator zero}
  1199. if cur_char="H" then r:=16
  1200. else if cur_char="O" then r:=8
  1201. else if cur_char="D" then r:=10
  1202. else skip_error('Decimal ("D"), octal ("O"), or hex ("H") value needed here');
  1203. @.Decimal ("D"), octal ("O"), or hex...@>
  1204. if r>0 then
  1205.   begin repeat get_next;
  1206.   until cur_char<>" "; {skip the blanks after the type code}
  1207.   while ((cur_char>="0")and(cur_char<="9"))or@|
  1208.       ((cur_char>="A")and(cur_char<="F")) do
  1209.     @<Multiply by |r|, add |cur_char-"0"|, and |get_next|@>;
  1210.   end;
  1211. @ @<Multiply by |r|...@>=
  1212. begin if cur_char>="A" then cur_char:=cur_char+"0"+10-"A";
  1213. if cur_char>="0"+r then skip_error('Illegal digit')
  1214. @.Illegal digit@>
  1215. else  begin c:=c3*r+cur_char-"0"; c3:=c mod 256;@/
  1216.   c:=c2*r+c div 256; c2:=c mod 256;@/
  1217.   c:=c1*r+c div 256; c1:=c mod 256;@/
  1218.   c:=c0*r+c div 256;
  1219.   if c<256 then c0:=c
  1220.   else begin cur_bytes:=zero_bytes;
  1221.     if r=8 then
  1222.       skip_error('Sorry, the maximum octal value is O 37777777777')
  1223. @.Sorry, the maximum...@>
  1224.     else if r=10 then
  1225.       skip_error('Sorry, the maximum decimal value is D 4294967295')
  1226.     else skip_error('Sorry, the maximum hex value is H FFFFFFFF');
  1227.     end;
  1228.   get_next;
  1229.   end;
  1230. @ The remaining scanning routine is the most interesting. It scans a real
  1231. constant and returns the nearest |fix_word| approximation to that constant.
  1232. A |fix_word| is a 32-bit integer that represents a real value that
  1233. has been multiplied by $2^{20}$. Since \.{VPtoVF} restricts the magnitude
  1234. of reals to 2048, the |fix_word| will have a magnitude less than $2^{31}$.
  1235. @d unity==@'4000000 {$2^{20}$, the |fix_word| 1.0}
  1236. @<Types...@>=
  1237. @!fix_word=integer; {a scaled real value with 20 bits of fraction}
  1238. @ When a real value is desired, we might as well treat `\.D' and `\.R'
  1239. formats as if they were identical.
  1240. @p function get_fix:fix_word; {scans a real property value}
  1241. var negative:boolean; {was there a minus sign?}
  1242. @!acc:integer; {an accumulator}
  1243. @!int_part:integer; {the integer part}
  1244. @!j:0..7; {the number of decimal places stored}
  1245. begin repeat get_next;
  1246. until cur_char<>" "; {skip the blanks before the type code}
  1247. negative:=false; acc:=0; {start with the accumulators zero}
  1248. if (cur_char<>"R")and(cur_char<>"D") then
  1249.   skip_error('An "R" or "D" value is needed here')
  1250. @.An "R" or "D" ... needed here@>
  1251. else  begin @<Scan the blanks and/or signs after the type code@>;
  1252.   while (cur_char>="0") and (cur_char<="9") do
  1253.     @<Multiply by 10, add |cur_char-"0"|, and |get_next|@>;
  1254.   int_part:=acc; acc:=0;
  1255.   if cur_char="." then @<Scan the fraction part and put it in |acc|@>;
  1256.   if (acc>=unity)and(int_part=2047) then
  1257.     skip_error('Real constants must be less than 2048')
  1258. @.Real constants must be...@>
  1259.   else acc:=int_part*unity+acc;
  1260.   end;
  1261. if negative then get_fix:=-acc@+else get_fix:=acc;
  1262. @ @<Scan the blanks...@>=
  1263. repeat get_next;
  1264. if cur_char="-" then
  1265.   begin cur_char:=" "; negative:=true;
  1266.   end
  1267. else if cur_char="+" then cur_char:=" ";
  1268. until cur_char<>" "
  1269. @ @<Multiply by 10...@>=
  1270. begin acc:=acc*10+cur_char-"0";
  1271. if acc>=2048 then
  1272.   begin skip_error('Real constants must be less than 2048');
  1273. @.Real constants must be...@>
  1274.   acc:=0; cur_char:=" ";
  1275.   end
  1276. else get_next;
  1277. @ To scan the fraction $.d_1d_2\ldots\,$, we keep track of up to seven
  1278. of the digits $d_j$. A correct result is obtained if we first compute
  1279. $f^\prime=\lfloor 2^{21}(d_1\ldots d_j)/10^j\rfloor$, after which
  1280. $f=\lfloor(f^\prime+1)/2\rfloor$. It is possible to have $f=1.0$.
  1281. @<Glob...@>=
  1282. @!fraction_digits:array[1..7] of integer; {$2^{21}$ times $d_j$}
  1283. @ @<Scan the frac...@>=
  1284. begin j:=0; get_next;
  1285. while (cur_char>="0")and(cur_char<="9") do
  1286.   begin if j<7 then
  1287.     begin incr(j); fraction_digits[j]:=@'10000000*(cur_char-"0");
  1288.     end;
  1289.   get_next;
  1290.   end;
  1291. acc:=0;
  1292. while j>0 do
  1293.   begin acc:=fraction_digits[j]+(acc div 10); decr(j);
  1294.   end;
  1295. acc:=(acc+10) div 20;
  1296. @* Storing the property values.
  1297. When property values have been found, they are squirreled away in a bunch
  1298. of arrays. The header information is unpacked into bytes in an array
  1299. called |header_bytes|. The ligature/kerning program is stored in an array
  1300. of type |four_bytes|.
  1301. Another |four_bytes| array holds the specifications of extensible characters.
  1302. The kerns and parameters are stored in separate arrays of |fix_word| values.
  1303. Virtual font data goes into an array |vf| of single-byte values.
  1304. We maintain information about at most 256 local fonts. (If this is inadequate,
  1305. several arrays need to be made longer and we need to output font definitions
  1306. that go beyond |fnt1| and |fnt_def1| in the \.{VF} file.)
  1307. Instead of storing the design size in the header array, we will keep it
  1308. in a |fix_word| variable until the last minute. The number of units in the
  1309. design size is also kept in a |fix_word|.
  1310. @<Glob...@>=
  1311. @!header_bytes:array[header_index] of byte; {the header block}
  1312. @!header_ptr:header_index; {the number of header bytes in use}
  1313. @!design_size:fix_word; {the design size}
  1314. @!design_units:fix_word; {reciprocal of the scaling factor}
  1315. @!frozen_du:boolean; {have we used |design_units| irrevocably?}
  1316. @!seven_bit_safe_flag:boolean; {does the file claim to be seven-bit-safe?}
  1317. @!lig_kern:array[0..max_lig_steps] of four_bytes; {the ligature program}
  1318. @!nl:0..32767; {the number of ligature/kern instructions so far}
  1319. @!min_nl:0..32767; {the final value of |nl| must be at least this}
  1320. @!kern:array[0..max_kerns] of fix_word; {the distinct kerning amounts}
  1321. @!nk:0..max_kerns; {the number of entries of |kern|}
  1322. @!exten:array[0..255] of four_bytes; {extensible character specs}
  1323. @!ne:0..256; {the number of extensible characters}
  1324. @!param:array[1..max_param_words] of fix_word; {\.{FONTDIMEN} parameters}
  1325. @!np:0..max_param_words; {the largest parameter set nonzero}
  1326. @!check_sum_specified:boolean; {did the user name the check sum?}
  1327. @!bchar:0..256; {the right boundary character, or 256 if unspecified}
  1328. @!vf:array[0..vf_size] of byte; {stored bytes for \.{VF} file}
  1329. @!vf_ptr:0..vf_size; {first unused location in |vf|}
  1330. @!vtitle_start:0..vf_size; {starting location of \.{VTITLE} string}
  1331. @!vtitle_length:byte; {length of \.{VTITLE} string}
  1332. @!packet_start:array[byte] of 0..vf_size;
  1333.   {beginning location of character packet}
  1334. @!packet_length:array[byte] of integer; {length of character packet}
  1335. @!font_ptr:0..256; {number of distinct local fonts seen}
  1336. @!cur_font:0..256; {number of the current local font}
  1337. @!fname_start:array[byte] of 0..vf_size; {beginning of local font name}
  1338. @!fname_length:array[byte] of byte; {length of local font name}
  1339. @!farea_start:array[byte] of 0..vf_size; {beginning of local font area}
  1340. @!farea_length:array[byte] of byte; {length of local font area}
  1341. @!font_checksum:array[byte] of four_bytes; {local font checksum}
  1342. @!font_number:array[0..256] of four_bytes; {local font id number}
  1343. @!font_at:array[byte] of fix_word; {local font ``at size''}
  1344. @!font_dsize:array[byte] of fix_word; {local font design size}
  1345. @ @<Types...@>=
  1346. @!header_index=0..max_header_bytes;
  1347. @!indx=0..@'77777;
  1348. @ @<Local...@>=
  1349. @!d:header_index; {an index into |header_bytes|}
  1350. @ We start by setting up the default values.
  1351. @d check_sum_loc=0
  1352. @d design_size_loc=4
  1353. @d coding_scheme_loc=8
  1354. @d family_loc=coding_scheme_loc+40
  1355. @d seven_flag_loc=family_loc+20
  1356. @d face_loc=seven_flag_loc+3
  1357. @<Set init...@>=
  1358. for d:=0 to 18*4-1 do header_bytes[d]:=0;
  1359. header_bytes[8]:=11; header_bytes[9]:="U";
  1360. header_bytes[10]:="N";
  1361. header_bytes[11]:="S";
  1362. header_bytes[12]:="P";
  1363. header_bytes[13]:="E";
  1364. header_bytes[14]:="C";
  1365. header_bytes[15]:="I";
  1366. header_bytes[16]:="F";
  1367. header_bytes[17]:="I";
  1368. header_bytes[18]:="E";
  1369. header_bytes[19]:="D";
  1370. @.UNSPECIFIED@>
  1371. for d:=family_loc to family_loc+11 do header_bytes[d]:=header_bytes[d-40];
  1372. design_size:=10*unity; design_units:=unity; frozen_du:=false;
  1373. seven_bit_safe_flag:=false;@/
  1374. header_ptr:=18*4; nl:=0; min_nl:=0; nk:=0; ne:=0; np:=0;@/
  1375. check_sum_specified:=false; bchar:=256;@/
  1376. vf_ptr:=0; vtitle_start:=0; vtitle_length:=0; font_ptr:=0;
  1377. for k:=0 to 255 do packet_start[k]:=vf_size;
  1378. for k:=0 to 127 do packet_length[k]:=1;
  1379. for k:=128 to 255 do packet_length[k]:=2;
  1380. @ Most of the dimensions, however, go into the |memory| array. There are
  1381. at most 257 widths, 257 heights, 257 depths, and 257 italic corrections,
  1382. since the value 0 is required but it need not be used. So |memory| has room
  1383. for 1028 entries, each of which is a |fix_word|. An auxiliary table called
  1384. |link| is used to link these words together in linear lists, so that
  1385. sorting and other operations can be done conveniently.
  1386. We also add four ``list head'' words to the |memory| and |link| arrays;
  1387. these are in locations |width| through |italic|, i.e., 1 through 4.
  1388. For example, |link[height]| points to the smallest element in
  1389. the sorted list of distinct heights that have appeared so far, and
  1390. |memory[height]| is the number of distinct heights.
  1391. @d mem_size=1028+4 {number of nonzero memory addresses}
  1392. @<Types...@>=
  1393. @!pointer=0..mem_size; {an index into memory}
  1394. @ The arrays |char_wd|, |char_ht|, |char_dp|, and |char_ic| contain
  1395. pointers to the |memory| array entries where the corresponding dimensions
  1396. appear. Two other arrays, |char_tag| and |char_remainder|, hold
  1397. the other information that \.{TFM} files pack into a |char_info_word|.
  1398. @d no_tag=0 {vanilla character}
  1399. @d lig_tag=1 {character has a ligature/kerning program}
  1400. @d list_tag=2 {character has a successor in a charlist}
  1401. @d ext_tag=3 {character is extensible}
  1402. @d bchar_label==char_remainder[256]
  1403.   {beginning of ligature program for left boundary}
  1404. @<Glob...@>=
  1405. @!memory:array[pointer] of fix_word; {character dimensions and kerns}
  1406. @!mem_ptr:pointer; {largest |memory| word in use}
  1407. @!link:array[pointer] of pointer; {to make lists of |memory| items}
  1408. @!char_wd:array[byte] of pointer; {pointers to the widths}
  1409. @!char_ht:array[byte] of pointer; {pointers to the heights}
  1410. @!char_dp:array[byte] of pointer; {pointers to the depths}
  1411. @!char_ic:array[byte] of pointer; {pointers to italic corrections}
  1412. @!char_tag:array[byte] of no_tag..ext_tag; {character tags}
  1413. @!char_remainder:array[0..256] of 0..65535; {pointers to ligature labels,
  1414.   next larger characters, or extensible characters}
  1415. @ @<Local...@>=
  1416. @!c:byte; {runs through all character codes}
  1417. @ @<Set init...@>=
  1418. bchar_label:=@'77777;
  1419. for c:=0 to 255 do
  1420.   begin char_wd[c]:=0; char_ht[c]:=0; char_dp[c]:=0; char_ic[c]:=0;@/
  1421.   char_tag[c]:=no_tag; char_remainder[c]:=0;
  1422.   end;
  1423. memory[0]:=@'17777777777; {an ``infinite'' element at the end of the lists}
  1424. memory[width]:=0; link[width]:=0; {width list is empty}
  1425. memory[height]:=0; link[height]:=0; {height list is empty}
  1426. memory[depth]:=0; link[depth]:=0; {depth list is empty}
  1427. memory[italic]:=0; link[italic]:=0; {italic list is empty}
  1428. mem_ptr:=italic;
  1429. @ As an example of these data structures, let us consider the simple
  1430. routine that inserts a potentially new element into one of the dimension
  1431. lists. The first parameter indicates the list head (i.e., |h=width| for
  1432. the width list, etc.); the second parameter is the value that is to be
  1433. inserted into the list if it is not already present.  The procedure
  1434. returns the value of the location where the dimension appears in |memory|.
  1435. The fact that |memory[0]| is larger than any legal dimension makes the
  1436. algorithm particularly short.
  1437. We do have to handle two somewhat subtle situations. A width of zero must be
  1438. put into the list, so that a zero-width character in the font will not appear
  1439. to be nonexistent (i.e., so that its |char_wd| index will not be zero), but
  1440. this does not need to be done for heights, depths, or italic corrections.
  1441. Furthermore, it is necessary to test for memory overflow even though we
  1442. have provided room for the maximum number of different dimensions in any
  1443. legal font, since the \.{VPL} file might foolishly give any number of
  1444. different sizes to the same character.
  1445. @p function sort_in(@!h:pointer;@!d:fix_word):pointer; {inserts into list}
  1446. var p:pointer; {the current node of interest}
  1447. begin if (d=0)and(h<>width) then sort_in:=0
  1448. else begin p:=h;
  1449.   while d>=memory[link[p]] do p:=link[p];
  1450.   if (d=memory[p])and(p<>h) then sort_in:=p
  1451.   else if mem_ptr=mem_size then
  1452.     begin err_print('Memory overflow: more than 1028 widths, etc');
  1453. @.Memory overflow...@>
  1454.     print_ln('Congratulations! It''s hard to make this error.');
  1455.     sort_in:=p;
  1456.     end
  1457.   else  begin incr(mem_ptr); memory[mem_ptr]:=d;
  1458.     link[mem_ptr]:=link[p]; link[p]:=mem_ptr; incr(memory[h]);
  1459.     sort_in:=mem_ptr;
  1460.     end;
  1461.   end;
  1462. @ When these lists of dimensions are eventually written to the \.{TFM}
  1463. file, we may have to do some rounding of values, because the \.{TFM} file
  1464. allows at most 256 widths, 16 heights, 16 depths, and 64 italic
  1465. corrections. The following procedure takes a given list head |h| and a
  1466. given dimension |d|, and returns the minimum $m$ such that the elements of
  1467. the list can be covered by $m$ intervals of width $d$.  It also sets
  1468. |next_d| to the smallest value $d^\prime>d$ such that the covering found
  1469. by this procedure would be different.  In particular, if $d=0$ it computes
  1470. the number of elements of the list, and sets |next_d| to the smallest
  1471. distance between two list elements. (The covering by intervals of width
  1472. |next_d| is not guaranteed to have fewer than $m$ elements, but in practice
  1473. this seems to happen most of the time.)
  1474. @<Glob...@>=
  1475. @!next_d:fix_word; {the next larger interval that is worth trying}
  1476. @ Once again we can make good use of the fact that |memory[0]| is ``infinite.''
  1477. @p function min_cover(@!h:pointer;@!d:fix_word):integer;
  1478. var p:pointer; {the current node of interest}
  1479. @!l:fix_word; {the least element covered by the current interval}
  1480. @!m:integer; {the current size of the cover being generated}
  1481. begin m:=0; p:=link[h]; next_d:=memory[0];
  1482. while p<>0 do
  1483.   begin incr(m); l:=memory[p];
  1484.   while memory[link[p]]<=l+d do p:=link[p];
  1485.   p:=link[p];
  1486.   if memory[p]-l<next_d then next_d:=memory[p]-l;
  1487.   end;
  1488. min_cover:=m;
  1489. @ The following procedure uses |min_cover| to determine the smallest $d$
  1490. such that a given list can be covered with at most a given number of
  1491. intervals.
  1492. @p function shorten(@!h:pointer;m:integer):fix_word; {finds best way to round}
  1493. var d:fix_word; {the current trial interval length}
  1494. @!k:integer; {the size of a minimum cover}
  1495. begin if memory[h]>m then
  1496.   begin excess:=memory[h]-m;
  1497.   k:=min_cover(h,0); d:=next_d; {now the answer is at least |d|}
  1498.   repeat d:=d+d; k:=min_cover(h,d);
  1499.   until k<=m; {first we ascend rapidly until finding the range}
  1500.   d:=d div 2; k:=min_cover(h,d); {now we run through the feasible steps}
  1501.   while k>m do
  1502.     begin d:=next_d; k:=min_cover(h,d);
  1503.     end;
  1504.   shorten:=d;
  1505.   end
  1506. else shorten:=0;
  1507. @ When we are nearly ready to output the \.{TFM} file, we will set
  1508. |index[p]:=k| if the dimension in |memory[p]| is being rounded to the
  1509. |k|th element of its list.
  1510. @<Glob...@>=
  1511. @!index:array[pointer] of byte;
  1512. @!excess:byte; {number of words to remove, if list is being shortened}
  1513. @ Here is the procedure that sets the |index| values. It also shortens
  1514. the list so that there is only one element per covering interval;
  1515. the remaining elements are the midpoints of their clusters.
  1516. @p procedure set_indices(@!h:pointer;@!d:fix_word); {reduces and indexes a list}
  1517. var p:pointer; {the current node of interest}
  1518. @!q:pointer; {trails one step behind |p|}
  1519. @!m:byte; {index number of nodes in the current interval}
  1520. @!l:fix_word; {least value in the current interval}
  1521. begin q:=h; p:=link[q]; m:=0;
  1522. while p<>0 do
  1523.   begin incr(m); l:=memory[p]; index[p]:=m;
  1524.   while memory[link[p]]<=l+d do
  1525.     begin p:=link[p]; index[p]:=m; decr(excess);
  1526.     if excess=0 then d:=0;
  1527.     end;
  1528.   link[q]:=p; memory[p]:=l+(memory[p]-l) div 2; q:=p; p:=link[p];
  1529.   end;
  1530. memory[h]:=m;
  1531. @* The input phase.
  1532. We're ready now to read and parse the \.{VPL} file, storing property
  1533. values as we go.
  1534. @<Glob...@>=
  1535. @!c:byte; {the current character or byte being processed}
  1536. @!x:fix_word; {current dimension of interest}
  1537. @!k:integer; {general-purpose index}
  1538. @ @<Read all the input@>=
  1539. cur_char:=" ";
  1540. repeat while cur_char=" " do get_next;
  1541. if cur_char="(" then @<Read a font property value@>
  1542. else if (cur_char=")")and not input_has_ended then
  1543.   begin err_print('Extra right parenthesis');
  1544.   incr(loc); cur_char:=" ";
  1545.   end
  1546. @.Extra right parenthesis@>
  1547. else if not input_has_ended then junk_error;
  1548. until input_has_ended
  1549. @ The |junk_error| routine just referred to is called when something
  1550. appears in the forbidden area between properties of a property list.
  1551. @p procedure junk_error; {gets past no man's land}
  1552. begin err_print('There''s junk here that is not in parentheses');
  1553. @.There's junk here...@>
  1554. skip_to_paren;
  1555. @ For each font property, we are supposed to read the data from the
  1556. left parenthesis that is the current value of |cur_char| to the right
  1557. parenthesis that matches it in the input. The main complication is
  1558. to recover with reasonable grace from various error conditions that might arise.
  1559. @<Read a font property value@>=
  1560. begin get_name;
  1561. if cur_code=comment_code then skip_to_end_of_item
  1562. else if cur_code>character_code then
  1563.   flush_error('This property name doesn''t belong on the outer level')
  1564. @.This property name doesn't belong...@>
  1565. else  begin @<Read the font property value specified by |cur_code|@>;
  1566.   finish_the_property;
  1567.   end;
  1568. @ @<Read the font property value spec...@>=
  1569. case cur_code of
  1570. check_sum_code: begin check_sum_specified:=true; read_four_bytes(check_sum_loc);
  1571.   end;
  1572. design_size_code: @<Read the design size@>;
  1573. design_units_code: @<Read the design units@>;
  1574. coding_scheme_code: read_BCPL(coding_scheme_loc,40);
  1575. family_code: read_BCPL(family_loc,20);
  1576. face_code:header_bytes[face_loc]:=get_byte;
  1577. seven_bit_safe_flag_code: @<Read the seven-bit-safe flag@>;
  1578. header_code: @<Read an indexed header word@>;
  1579. font_dimen_code: @<Read font parameter list@>;
  1580. lig_table_code: read_lig_kern;
  1581. boundary_char_code: bchar:=get_byte;
  1582. virtual_title_code: begin vtitle_start:=vf_ptr; copy_to_end_of_item;
  1583.   if vf_ptr>vtitle_start+255 then
  1584.     begin err_print('VTITLE clipped to 255 characters'); vtitle_length:=255;
  1585. @.VTITLE clipped...@>
  1586.     end
  1587.   else vtitle_length:=vf_ptr-vtitle_start;
  1588.   end;
  1589. map_font_code:@<Read a local font list@>;
  1590. character_code: read_char_info;
  1591. @ The |case| statement just given makes use of three subroutines that we
  1592. haven't defined yet. The first of these puts a 32-bit octal quantity
  1593. into four specified bytes of the header block.
  1594. @p procedure read_four_bytes(l:header_index);
  1595. begin get_four_bytes;
  1596. header_bytes[l]:=c0;
  1597. header_bytes[l+1]:=c1;
  1598. header_bytes[l+2]:=c2;
  1599. header_bytes[l+3]:=c3;
  1600. @ The second little procedure is used to scan a string and to store it in
  1601. the ``{\mc BCPL} format'' required by \.{TFM} files. The string is supposed
  1602. to contain at most |n| bytes, including the first byte (which holds the
  1603. length of the rest of the string).
  1604. @p procedure read_BCPL(l:header_index;n:byte);
  1605. var k:header_index;
  1606. begin k:=l;
  1607. while cur_char=" " do get_next;
  1608. while (cur_char<>"(")and(cur_char<>")") do
  1609.   begin if k<l+n then incr(k);
  1610.   if k<l+n then header_bytes[k]:=cur_char;
  1611.   get_next;
  1612.   end;
  1613. if k=l+n then
  1614.   begin err_print('String is too long; its first ',n-1:1,
  1615. @.String is too long...@>
  1616.     ' characters will be kept'); decr(k);
  1617.   end;
  1618. header_bytes[l]:=k-l;
  1619. while k<l+n-1 do {tidy up the remaining bytes by setting them to nulls}
  1620.   begin incr(k); header_bytes[k]:=0;
  1621.   end;
  1622. @ @<Read the design size@>=
  1623. begin next_d:=get_fix;
  1624. if next_d<unity then
  1625.   err_print('The design size must be at least 1')
  1626. @.The design size must...@>
  1627. else design_size:=next_d;
  1628. @ @<Read the design units@>=
  1629. begin next_d:=get_fix;
  1630. if next_d<=0 then
  1631.   err_print('The number of units per design size must be positive')
  1632. @.The number of units...@>
  1633. else if frozen_du then
  1634.   err_print('Sorry, it''s too late to change the design units')
  1635. @.Sorry, it's too late...@>
  1636. else design_units:=next_d;
  1637. @ @<Read the seven-bit-safe...@>=
  1638. begin while cur_char=" " do get_next;
  1639. if cur_char="T" then seven_bit_safe_flag:=true
  1640. else if cur_char="F" then seven_bit_safe_flag:=false
  1641. else err_print('The flag value should be "TRUE" or "FALSE"');
  1642. @.The flag value should be...@>
  1643. skip_to_paren;
  1644. @ @<Read an indexed header word@>=
  1645. begin c:=get_byte;
  1646. if c<18 then skip_error('HEADER indices should be 18 or more')
  1647. @.HEADER indices...@>
  1648. else if 4*c+4>max_header_bytes then
  1649.   skip_error('This HEADER index is too big for my present table size')
  1650. @.This HEADER index is too big...@>
  1651. else  begin while header_ptr<4*c+4 do
  1652.     begin header_bytes[header_ptr]:=0; incr(header_ptr);
  1653.     end;
  1654.   read_four_bytes(4*c);
  1655.   end;
  1656. @ The remaining kinds of font property values that need to be read are
  1657. those that involve property lists on higher levels. Each of these has a
  1658. loop similar to the one that was used at level zero. Then we put the
  1659. right parenthesis back so that `|finish_the_property|' will be happy;
  1660. there is probably a more elegant way to do this.
  1661. @d finish_inner_property_list==begin decr(loc); incr(level); cur_char:=")";
  1662.   end
  1663. @<Read font parameter list@>=
  1664. begin while level=1 do
  1665.   begin while cur_char=" " do get_next;
  1666.   if cur_char="(" then @<Read a parameter value@>
  1667.   else if cur_char=")" then skip_to_end_of_item
  1668.   else junk_error;
  1669.   end;
  1670. finish_inner_property_list;
  1671. @ @<Read a parameter value@>=
  1672. begin get_name;
  1673. if cur_code=comment_code then skip_to_end_of_item
  1674. else if (cur_code<parameter_code)or(cur_code>=char_wd_code) then
  1675.   flush_error('This property name doesn''t belong in a FONTDIMEN list')
  1676. @.This property name doesn't belong...@>
  1677. else  begin if cur_code=parameter_code then c:=get_byte
  1678.   else c:=cur_code-parameter_code;
  1679.   if c=0 then flush_error('PARAMETER index must not be zero')
  1680. @.PARAMETER index must not...@>
  1681.   else if c>max_param_words then
  1682.     flush_error('This PARAMETER index is too big for my present table size')
  1683. @.This PARAMETER index is too big...@>
  1684.   else  begin while np<c do
  1685.       begin incr(np); param[np]:=0;
  1686.       end;
  1687.     param[c]:=get_fix;
  1688.     finish_the_property;
  1689.     end;
  1690.   end;
  1691. @ @d numbers_differ==(font_number[cur_font].b3<>font_number[font_ptr].b3)or@|
  1692. (font_number[cur_font].b2<>font_number[font_ptr].b2)or@|
  1693. (font_number[cur_font].b1<>font_number[font_ptr].b1)or@|
  1694. (font_number[cur_font].b0<>font_number[font_ptr].b0)
  1695. @<Read a local font list@>=
  1696. begin get_four_bytes; font_number[font_ptr]:=cur_bytes; cur_font:=0;
  1697. while numbers_differ do incr(cur_font);
  1698. if cur_font=font_ptr then {it's a new font number}
  1699.   if font_ptr<256 then @<Initialize a new local font@>
  1700.   else err_print('I can handle only 256 different mapfonts');
  1701. @.I can handle only 256...@>
  1702. if cur_font=font_ptr then skip_to_end_of_item
  1703. else while level=1 do
  1704.   begin while cur_char=" " do get_next;
  1705.   if cur_char="(" then @<Read a local font property@>
  1706.   else if cur_char=")" then skip_to_end_of_item
  1707.   else junk_error;
  1708.   end;
  1709. finish_inner_property_list;
  1710. @ @<Initialize a new local font@>=
  1711. begin incr(font_ptr);
  1712. fname_start[cur_font]:=vf_size; fname_length[cur_font]:=4; {\.{NULL}}
  1713. farea_start[cur_font]:=vf_size; farea_length[cur_font]:=0;
  1714. font_checksum[cur_font]:=zero_bytes;
  1715. font_at[cur_font]:=@'4000000; {denotes design size of this virtual font}
  1716. font_dsize[cur_font]:=@'50000000; {the |fix_word| for 10}
  1717. @ @<Read a local font property@>=
  1718. begin get_name;
  1719. if cur_code=comment_code then skip_to_end_of_item
  1720. else if (cur_code<font_name_code)or(cur_code>font_dsize_code) then
  1721.   flush_error('This property name doesn''t belong in a MAPFONT list')
  1722. @.This property name doesn't belong...@>
  1723. else  begin case cur_code of
  1724.   font_name_code:@<Read a local font name@>;
  1725.   font_area_code:@<Read a local font area@>;
  1726.   font_checksum_code:begin get_four_bytes; font_checksum[cur_font]:=cur_bytes;
  1727.     end;
  1728.   font_at_code: begin frozen_du:=true;
  1729.     if design_units=unity then font_at[cur_font]:=get_fix
  1730.     else font_at[cur_font]:=round((get_fix/design_units)*1048576.0);
  1731.     end;
  1732.   font_dsize_code:font_dsize[cur_font]:=get_fix;
  1733.   end; {there are no other cases}
  1734.   finish_the_property;
  1735.   end;
  1736. @ @<Read a local font name@>=
  1737. begin fname_start[cur_font]:=vf_ptr; copy_to_end_of_item;
  1738. if vf_ptr>fname_start[cur_font]+255 then
  1739.   begin err_print('FONTNAME clipped to 255 characters');
  1740. @.FONTNAME clipped...@>
  1741.   fname_length[cur_font]:=255;
  1742.   end
  1743. else fname_length[cur_font]:=vf_ptr-fname_start[cur_font];
  1744. @ @<Read a local font area@>=
  1745. begin farea_start[cur_font]:=vf_ptr; copy_to_end_of_item;
  1746. if vf_ptr>farea_start[cur_font]+255 then
  1747.   begin err_print('FONTAREA clipped to 255 characters');
  1748. @.FONTAREA clipped...@>
  1749.   farea_length[cur_font]:=255;
  1750.   end
  1751. else farea_length[cur_font]:=vf_ptr-farea_start[cur_font];
  1752. @ @<Read ligature/kern list@>=
  1753. begin while level=1 do
  1754.   begin while cur_char=" " do get_next;
  1755.   if cur_char="(" then @<Read a ligature/kern command@>
  1756.   else if cur_char=")" then skip_to_end_of_item
  1757.   else junk_error;
  1758.   end;
  1759. finish_inner_property_list;
  1760. @ @<Read a ligature/kern command@>=
  1761. begin get_name;
  1762. if cur_code=comment_code then skip_to_end_of_item
  1763. else if cur_code<label_code then
  1764.   flush_error('This property name doesn''t belong in a LIGTABLE list')
  1765. @.This property name doesn't belong...@>
  1766. else  begin case cur_code of
  1767.   label_code:@<Read a label step@>;
  1768.   stop_code:@<Read a stop step@>;
  1769.   skip_code:@<Read a skip step@>;
  1770.   krn_code:@<Read a kerning step@>;
  1771.   lig_code,lig_code+1,lig_code+2,lig_code+3,lig_code+5,lig_code+6,lig_code+7,
  1772.     lig_code+11:@<Read a ligature step@>;
  1773.   end; {there are no other cases |>=label_code|}
  1774.   finish_the_property;
  1775.   end;
  1776. @ When a character is about to be tagged, we call the following
  1777. procedure so that an error message is given in case of multiple tags.
  1778. @p procedure check_tag(c:byte); {print error if |c| already tagged}
  1779. begin case char_tag[c] of
  1780. no_tag: do_nothing;
  1781. lig_tag: err_print('This character already appeared in a LIGTABLE LABEL');
  1782. @.This character already...@>
  1783. list_tag: err_print('This character already has a NEXTLARGER spec');
  1784. ext_tag: err_print('This character already has a VARCHAR spec');
  1785. @ @<Read a label step@>=
  1786. begin while cur_char=" " do get_next;
  1787. if cur_char="B" then
  1788.   begin bchar_label:=nl; skip_to_paren; {\.{LABEL BOUNDARYCHAR}}
  1789.   end
  1790. else begin backup; c:=get_byte;
  1791.   check_tag(c); char_tag[c]:=lig_tag; char_remainder[c]:=nl;
  1792.   end;
  1793. if min_nl<=nl then min_nl:=nl+1;
  1794. lk_step_ended:=false;
  1795. @ @d stop_flag=128 {value indicating `\.{STOP}' in a lig/kern program}
  1796. @d kern_flag=128 {op code for a kern step}
  1797. @<Globals...@>=
  1798. @!lk_step_ended:boolean;
  1799.   {was the last \.{LIGTABLE} property \.{LIG} or \.{KRN}?}
  1800. @!krn_ptr:0..max_kerns; {an index into |kern|}
  1801. @ @<Read a stop step@>=
  1802. if not lk_step_ended then
  1803.   err_print('STOP must follow LIG or KRN')
  1804. @.STOP must follow LIG or KRN@>
  1805. else begin lig_kern[nl-1].b0:=stop_flag; lk_step_ended:=false;
  1806.   end
  1807. @ @<Read a skip step@>=
  1808. if not lk_step_ended then
  1809.   err_print('SKIP must follow LIG or KRN')
  1810. @.SKIP must follow LIG or KRN@>
  1811. else begin c:=get_byte;
  1812.   if c>=128 then err_print('Maximum SKIP amount is 127')
  1813. @.Maximum SKIP amount...@>
  1814.   else if nl+c>=max_lig_steps then
  1815.     err_print('Sorry, LIGTABLE too long for me to handle')
  1816. @.Sorry, LIGTABLE too long...@>
  1817.   else begin lig_kern[nl-1].b0:=c;
  1818.     if min_nl<=nl+c then min_nl:=nl+c+1;
  1819.     end;
  1820.   lk_step_ended:=false;
  1821.   end
  1822. @ @<Read a ligature step@>=
  1823. begin lig_kern[nl].b0:=0;
  1824. lig_kern[nl].b2:=cur_code-lig_code;
  1825. lig_kern[nl].b1:=get_byte;
  1826. lig_kern[nl].b3:=get_byte;
  1827. if nl>=max_lig_steps-1 then
  1828.   err_print('Sorry, LIGTABLE too long for me to handle')
  1829. @.Sorry, LIGTABLE too long...@>
  1830. else incr(nl);
  1831. lk_step_ended:=true;
  1832. @ @<Read a kerning step@>=
  1833. begin lig_kern[nl].b0:=0; lig_kern[nl].b1:=get_byte;
  1834. kern[nk]:=get_fix; krn_ptr:=0;
  1835. while kern[krn_ptr]<>kern[nk] do incr(krn_ptr);
  1836. if krn_ptr=nk then
  1837.   begin if nk<max_kerns then incr(nk)
  1838.   else  begin err_print('Sorry, too many different kerns for me to handle');
  1839. @.Sorry, too many different kerns...@>
  1840.     decr(krn_ptr);
  1841.     end;
  1842.   end;
  1843. lig_kern[nl].b2:=kern_flag+(krn_ptr div 256);
  1844. lig_kern[nl].b3:=krn_ptr mod 256;
  1845. if nl>=max_lig_steps-1 then
  1846.   err_print('Sorry, LIGTABLE too long for me to handle')
  1847. @.Sorry, LIGTABLE too long...@>
  1848. else incr(nl);
  1849. lk_step_ended:=true;
  1850. @ Finally we come to the part of \.{VPtoVF}'s input mechanism
  1851. that is used most, the processing of individual character data.
  1852. @<Read character info list@>=
  1853. begin c:=get_byte; {read the character code that is being specified}
  1854. @<Print |c| in octal notation@>;
  1855. while level=1 do
  1856.   begin while cur_char=" " do get_next;
  1857.   if cur_char="(" then @<Read a character property@>
  1858.   else if cur_char=")" then skip_to_end_of_item
  1859.   else junk_error;
  1860.   end;
  1861. if char_wd[c]=0 then char_wd[c]:=sort_in(width,0); {legitimatize |c|}
  1862. finish_inner_property_list;
  1863. @ @<Read a character prop...@>=
  1864. begin get_name;
  1865. if cur_code=comment_code then skip_to_end_of_item
  1866. else if (cur_code<char_wd_code)or(cur_code>var_char_code) then
  1867.   flush_error('This property name doesn''t belong in a CHARACTER list')
  1868. @.This property name doesn't belong...@>
  1869. else  begin case cur_code of
  1870.   char_wd_code:char_wd[c]:=sort_in(width,get_fix);
  1871.   char_ht_code:char_ht[c]:=sort_in(height,get_fix);
  1872.   char_dp_code:char_dp[c]:=sort_in(depth,get_fix);
  1873.   char_ic_code:char_ic[c]:=sort_in(italic,get_fix);
  1874.   next_larger_code:begin check_tag(c); char_tag[c]:=list_tag;
  1875.     char_remainder[c]:=get_byte;
  1876.     end;
  1877.   map_code:read_packet(c);
  1878.   var_char_code:@<Read an extensible recipe for |c|@>;
  1879.   end;@/
  1880.   finish_the_property;
  1881.   end;
  1882. @ @<Read an extensible r...@>=
  1883. begin if ne=256 then
  1884.   err_print('At most 256 VARCHAR specs are allowed')
  1885. @.At most 256 VARCHAR specs...@>
  1886. else  begin check_tag(c); char_tag[c]:=ext_tag; char_remainder[c]:=ne;@/
  1887.   exten[ne]:=zero_bytes;
  1888.   while level=2 do
  1889.     begin while cur_char=" " do get_next;
  1890.     if cur_char="(" then @<Read an extensible piece@>
  1891.     else if cur_char=")" then skip_to_end_of_item
  1892.     else junk_error;
  1893.     end;
  1894.   incr(ne);
  1895.   finish_inner_property_list;
  1896.   end;
  1897. @ @<Read an extensible p...@>=
  1898. begin get_name;
  1899. if cur_code=comment_code then skip_to_end_of_item
  1900. else if (cur_code<var_char_code+1)or(cur_code>var_char_code+4) then
  1901.   flush_error('This property name doesn''t belong in a VARCHAR list')
  1902. @.This property name doesn't belong...@>
  1903. else  begin case cur_code-(var_char_code+1) of
  1904.   0:exten[ne].b0:=get_byte;
  1905.   1:exten[ne].b1:=get_byte;
  1906.   2:exten[ne].b2:=get_byte;
  1907.   3:exten[ne].b3:=get_byte;
  1908.   end;@/
  1909.   finish_the_property;
  1910.   end;
  1911. @* Assembling the mappings.
  1912. Each \.{MAP} property is a sequence of \.{DVI} instructions, for which
  1913. we need to know some of the opcodes.
  1914. @d set_char_0=0 {\.{DVI} command to typeset character 0 and move right}
  1915. @d set1=128 {typeset a character and move right}
  1916. @d set_rule=132 {typeset a rule and move right}
  1917. @d push=141 {save the current positions}
  1918. @d pop=142 {restore previous positions}
  1919. @d right1=143 {move right}
  1920. @d w0=147 {move right by |w|}
  1921. @d w1=148 {move right and set |w|}
  1922. @d x0=152 {move right by |x|}
  1923. @d x1=153 {move right and set |x|}
  1924. @d down1=157 {move down}
  1925. @d y0=161 {move down by |y|}
  1926. @d y1=162 {move down and set |y|}
  1927. @d z0=166 {move down by |z|}
  1928. @d z1=167 {move down and set |z|}
  1929. @d fnt_num_0=171 {set current font to 0}
  1930. @d fnt1=235 {set current font}
  1931. @d xxx1=239 {extension to \.{DVI} primitives}
  1932. @d xxx4=242 {potentially long extension to \.{DVI} primitives}
  1933. @d fnt_def1=243 {define the meaning of a font number}
  1934. @d pre=247 {preamble}
  1935. @d post=248 {postamble beginning}
  1936. @ We keep stacks of movement values, in order to optimize the \.{DVI} code
  1937. in simple cases.
  1938. @<Glob...@>=
  1939. @!hstack:array[0..max_stack] of 0..2; {number of known horizontal movements}
  1940. @!vstack:array[0..max_stack] of 0..2; {number of known vertical movements}
  1941. @!wstack,@!xstack,@!ystack,@!zstack:array[0..max_stack] of fix_word;
  1942. @!stack_ptr:0..max_stack;
  1943. @ The packet is built by straightforward assembly of \.{DVI} instructions.
  1944. @p @<Declare the |vf_fix| procedure@>@;@/
  1945. procedure read_packet(@!c:byte);
  1946. var @!cc:byte; {character being typeset}
  1947. @!x:fix_word; {movement}
  1948. @!h,@!v:0..2; {top of |hstack| and |vstack|}
  1949. @!special_start:0..vf_size; {location of |xxx1| command}
  1950. @!k:0..vf_size; {loop index}
  1951. begin packet_start[c]:=vf_ptr; stack_ptr:=0; h:=0; v:=0;
  1952. cur_font:=0;
  1953. while level=2 do
  1954.   begin while cur_char=" " do get_next;
  1955.   if cur_char="(" then @<Read and assemble a list of \.{DVI} commands@>
  1956.   else if cur_char=")" then skip_to_end_of_item
  1957.   else junk_error;
  1958.   end;
  1959. while stack_ptr>0 do
  1960.   begin err_print('Missing POP supplied');
  1961. @.Missing POP supplied@>
  1962.   vf_store(pop); decr(stack_ptr);
  1963.   end;
  1964. packet_length[c]:=vf_ptr-packet_start[c];
  1965. finish_inner_property_list;
  1966. @ @<Read and assemble a list of \.{DVI}...@>=
  1967. begin get_name;
  1968. if cur_code=comment_code then skip_to_end_of_item
  1969. else if (cur_code<select_font_code)or(cur_code>special_hex_code) then
  1970.   flush_error('This property name doesn''t belong in a MAP list')
  1971. @.This property name doesn't belong...@>
  1972. else  begin case cur_code of
  1973.   select_font_code:@<Assemble a font selection@>;
  1974.   set_char_code:@<Assemble a typesetting instruction@>;
  1975.   set_rule_code:@<Assemble a rulesetting instruction@>;
  1976.   move_right_code,move_right_code+1:@<Assemble a horizontal movement@>;
  1977.   move_down_code,move_down_code+1:@<Assemble a vertical movement@>;
  1978.   push_code:@<Assemble a stack push@>;
  1979.   pop_code:@<Assemble a stack pop@>;
  1980.   special_code,special_hex_code:@<Assemble a special command@>;
  1981.   end;@/
  1982.   finish_the_property;
  1983.   end;
  1984. @ @<Assemble a font selection@>=
  1985. begin get_four_bytes; font_number[font_ptr]:=cur_bytes;
  1986. cur_font:=0;
  1987. while numbers_differ do incr(cur_font);
  1988. if cur_font=font_ptr then err_print('Undefined MAPFONT cannot be selected')
  1989. @.Undefined MAPFONT...@>
  1990. else if cur_font<64 then vf_store(fnt_num_0+cur_font)
  1991. else begin vf_store(fnt1); vf_store(cur_font);
  1992.   end;
  1993. @ @<Assemble a typesetting instruction@>=
  1994. if cur_font=font_ptr then
  1995.  err_print('Character cannot be typeset in undefined font')
  1996. @.Character cannot be typeset...@>
  1997. else begin cc:=get_byte;
  1998.   if cc>=128 then vf_store(set1);
  1999.   vf_store(cc);
  2000.   end
  2001. @ Here's a procedure that converts a |fix_word| to a sequence of
  2002. \.{DVI} bytes.
  2003. @<Declare the |vf_fix|...@>=
  2004. procedure vf_fix(@!opcode:byte;@!x:fix_word);
  2005. var negative:boolean;
  2006. @!k:0..4; {number of bytes to typeset}
  2007. @!t:integer; {threshold}
  2008. begin frozen_du:=true;
  2009. if design_units<>unity then x:=round((x/design_units)*1048576.0);
  2010. if x>0 then negative:=false
  2011. else begin negative:=true; x:=-1-x;@+end;
  2012. if opcode=0 then
  2013.   begin k:=4; t:=@'100000000;@+end
  2014. else begin t:=127; k:=1;
  2015.   while x>t do
  2016.     begin t:=256*t+255; incr(k);
  2017.     end;
  2018.   vf_store(opcode+k-1); t:=t div 128 +1;
  2019.   end;
  2020. repeat if negative then
  2021.   begin vf_store(255-(x div t)); negative:=false;
  2022.   x:=(x div t)*t+t-1-x;
  2023.   end
  2024. else vf_store((x div t) mod 256);
  2025. decr(k); t:=t div 256;
  2026. until k=0;
  2027. @ @<Assemble a rulesetting instruction@>=
  2028. begin vf_store(set_rule); vf_fix(0,get_fix); vf_fix(0,get_fix);
  2029. @ @<Assemble a horizontal movement@>=
  2030. begin if cur_code=move_right_code then x:=get_fix@+else x:=-get_fix;
  2031. if h=0 then
  2032.   begin wstack[stack_ptr]:=x; h:=1; vf_fix(w1,x);@+end
  2033. else if x=wstack[stack_ptr] then vf_store(w0)
  2034. else if h=1 then
  2035.   begin xstack[stack_ptr]:=x; h:=2; vf_fix(x1,x);@+end
  2036. else if x=xstack[stack_ptr] then vf_store(x0)
  2037. else vf_fix(right1,x);
  2038. @ @<Assemble a vertical movement@>=
  2039. begin if cur_code=move_down_code then x:=get_fix@+else x:=-get_fix;
  2040. if v=0 then
  2041.   begin ystack[stack_ptr]:=x; v:=1; vf_fix(y1,x);@+end
  2042. else if x=ystack[stack_ptr] then vf_store(y0)
  2043. else if v=1 then
  2044.   begin zstack[stack_ptr]:=x; v:=2; vf_fix(z1,x);@+end
  2045. else if x=zstack[stack_ptr] then vf_store(z0)
  2046. else vf_fix(down1,x);
  2047. @ @<Assemble a stack push@>=
  2048. if stack_ptr=max_stack then {too pushy}
  2049.   err_print('Don''t push so much---stack is full!')
  2050. @.Don't push so much...@>
  2051. else begin vf_store(push); hstack[stack_ptr]:=h; vstack[stack_ptr]:=v;
  2052.   incr(stack_ptr); h:=0; v:=0;
  2053.   end
  2054. @ @<Assemble a stack pop@>=
  2055. if stack_ptr=0 then
  2056.   err_print('Empty stack cannot be popped')
  2057. @.Empty stack...@>
  2058. else begin vf_store(pop); decr(stack_ptr);
  2059.   h:=hstack[stack_ptr]; v:=vstack[stack_ptr];
  2060.   end
  2061. @ @<Assemble a special command@>=
  2062. begin vf_store(xxx1); vf_store(0); {dummy length}
  2063. special_start:=vf_ptr;
  2064. if cur_code=special_code then copy_to_end_of_item
  2065. else begin repeat x:=get_hex;
  2066.    if cur_char>")" then vf_store(x*16+get_hex);
  2067.   until cur_char<=")";
  2068.   end;
  2069. if vf_ptr-special_start>255 then @<Convert |xxx1| command to |xxx4|@>
  2070. else vf[special_start-1]:=vf_ptr-special_start;
  2071. @ @<Convert |xxx1|...@>=
  2072. if vf_ptr+3>vf_size then
  2073.   begin err_print('Special command being clipped---no room left!');
  2074. @.Special command being clipped...@>
  2075.   vf_ptr:=special_start+255; vf[special_start-1]:=255;
  2076.   end
  2077. else begin for k:=vf_ptr downto special_start do vf[k+3]:=vf[k];
  2078.   x:=vf_ptr-special_start; vf_ptr:=vf_ptr+3;
  2079.   vf[special_start-2]:=xxx4;
  2080.   vf[special_start-1]:=x div @'100000000;
  2081.   vf[special_start]:=(x div @'200000) mod 256;
  2082.   vf[special_start+1]:=(x div @'400) mod 256;
  2083.   vf[special_start+2]:=x mod 256;
  2084.   end
  2085. @ The input routine is now complete except for the following code,
  2086. which prints a progress report as the file is being read.
  2087. @p procedure print_octal(c:byte); {prints three octal digits}
  2088. begin print('''',(c div 64):1,((c div 8) mod 8):1,(c mod 8):1);
  2089. @ @<Print |c| in octal...@>=
  2090. begin if chars_on_line=8 then
  2091.   begin print_ln(' '); chars_on_line:=1;
  2092.   end
  2093. else  begin if chars_on_line>0 then print(' ');
  2094.   incr(chars_on_line);
  2095.   end;
  2096. print_octal(c); {progress report}
  2097. @* The checking and massaging phase.
  2098. Once the whole \.{VPL} file has been read in, we must check it for consistency
  2099. and correct any errors. This process consists mainly of running through
  2100. the characters that exist and seeing if they refer to characters that
  2101. don't exist. We also compute the true value of |seven_unsafe|; we make sure
  2102. that the charlists and ligature programs contain no loops; and we
  2103. shorten the lists of widths, heights, depths, and italic corrections,
  2104. if necessary, to keep from exceeding the required maximum sizes.
  2105. @<Glob...@>=
  2106. @!seven_unsafe:boolean; {do seven-bit characters generate eight-bit ones?}
  2107. @ @<Correct and check the information@>=
  2108. if nl>0 then @<Make sure the ligature/kerning program ends appropriately@>;
  2109. seven_unsafe:=false;
  2110. for c:=0 to 255 do if char_wd[c]<>0 then
  2111.     @<For all characters |g| generated by |c|,
  2112.     make sure that |char_wd[g]| is nonzero, and
  2113.     set |seven_unsafe| if |c<128<=g|@>;
  2114. if bchar_label<@'77777 then
  2115.   begin c:=256; @<Check ligature program of |c|@>;
  2116.   end;
  2117. if seven_bit_safe_flag and seven_unsafe then
  2118.   print_ln('The font is not really seven-bit-safe!');
  2119. @.The font is not...safe@>
  2120. @<Check for infinite ligature loops@>;
  2121. @<Doublecheck the lig/kern commands and the extensible recipes@>;
  2122. for c:=0 to 255 do
  2123.   @<Make sure that |c| is not the largest element of a charlist cycle@>;
  2124. @<Put the width, height, depth, and italic lists into final form@>
  2125. @ The checking that we need in several places is accomplished by three
  2126. macros that are only slightly tricky.
  2127. @d existence_tail(#)==begin char_wd[g]:=sort_in(width,0);
  2128.     print(#,' '); print_octal(c);
  2129.     print_ln(' had no CHARACTER spec.');
  2130.     end;
  2131.   end
  2132. @d check_existence_and_safety(#)==begin g:=#;
  2133.   if (g>=128)and(c<128) then seven_unsafe:=true;
  2134.   if char_wd[g]=0 then existence_tail
  2135. @d check_existence(#)==begin g:=#;
  2136.   if char_wd[g]=0 then existence_tail
  2137. @<For all characters |g| generated by |c|...@>=
  2138. case char_tag[c] of
  2139. no_tag: do_nothing;
  2140. lig_tag: @<Check ligature program of |c|@>;
  2141. list_tag: check_existence_and_safety(char_remainder[c])
  2142.   ('The character NEXTLARGER than');
  2143. @.The character NEXTLARGER...@>
  2144. ext_tag:@<Check the pieces of |exten[c]|@>;
  2145. @ @<Check the pieces...@>=
  2146. begin if exten[char_remainder[c]].b0>0 then
  2147.   check_existence_and_safety(exten[char_remainder[c]].b0)
  2148.     ('TOP piece of character');
  2149. @.TOP piece of character...@>
  2150. if exten[char_remainder[c]].b1>0 then
  2151.   check_existence_and_safety(exten[char_remainder[c]].b1)
  2152.     ('MID piece of character');
  2153. @.MID piece of character...@>
  2154. if exten[char_remainder[c]].b2>0 then
  2155.   check_existence_and_safety(exten[char_remainder[c]].b2)
  2156.     ('BOT piece of character');
  2157. @.BOT piece of character...@>
  2158. check_existence_and_safety(exten[char_remainder[c]].b3)
  2159.     ('REP piece of character');
  2160. @.REP piece of character...@>
  2161. @ @<Make sure that |c| is not the largest element of a charlist cycle@>=
  2162. if char_tag[c]=list_tag then
  2163.   begin g:=char_remainder[c];
  2164.   while (g<c)and(char_tag[g]=list_tag) do g:=char_remainder[g];
  2165.   if g=c then
  2166.     begin char_tag[c]:=no_tag;
  2167.     print('A cycle of NEXTLARGER characters has been broken at ');
  2168. @.A cycle of NEXTLARGER...@>
  2169.     print_octal(c); print_ln('.');
  2170.     end;
  2171.   end
  2172. @ @<Glob...@>=
  2173. @!delta:fix_word; {size of the intervals needed for rounding}
  2174. @ @d round_message(#)==if delta>0 then print_ln('I had to round some ',
  2175. @.I had to round...@>
  2176.   #,'s by ',(((delta+1) div 2)/@'4000000):1:7,' units.')
  2177. @<Put the width, height, depth, and italic lists into final form@>=
  2178. delta:=shorten(width,255); set_indices(width,delta); round_message('width');@/
  2179. delta:=shorten(height,15); set_indices(height,delta); round_message('height');@/
  2180. delta:=shorten(depth,15); set_indices(depth,delta); round_message('depth');@/
  2181. delta:=shorten(italic,63); set_indices(italic,delta);
  2182.   round_message('italic correction');
  2183. @ @d clear_lig_kern_entry== {make an unconditional \.{STOP}}
  2184.   lig_kern[nl].b0:=255; lig_kern[nl].b1:=0;
  2185.   lig_kern[nl].b2:=0; lig_kern[nl].b3:=0
  2186. @<Make sure the ligature/kerning program ends...@>=
  2187. begin if bchar_label<@'77777 then {make room for it}
  2188.   begin clear_lig_kern_entry; incr(nl);
  2189.   end; {|bchar_label| will be stored later}
  2190. while min_nl>nl do
  2191.   begin clear_lig_kern_entry; incr(nl);
  2192.   end;
  2193. if lig_kern[nl-1].b0=0 then lig_kern[nl-1].b0:=stop_flag;
  2194. @ It's not trivial to check for infinite loops generated by repeated
  2195. insertion of ligature characters. But fortunately there is a nice
  2196. algorithm for such testing, copied here from the program \.{TFtoPL}
  2197. where it is explained further.
  2198. @d simple=0 {$f(x,y)=z$}
  2199. @d left_z=1 {$f(x,y)=f(z,y)$}
  2200. @d right_z=2 {$f(x,y)=f(x,z)$}
  2201. @d both_z=3 {$f(x,y)=f(f(x,z),y)$}
  2202. @d pending=4 {$f(x,y)$ is being evaluated}
  2203. @ @<Glo...@>=
  2204. @!lig_ptr:0..max_lig_steps; {an index into |lig_kern|}
  2205. @!hash:array[0..hash_size] of 0..66048; {$256x+y+1$ for $x\le257$ and $y\le255$}
  2206. @!class:array[0..hash_size] of simple..pending;
  2207. @!lig_z:array[0..hash_size] of 0..257;
  2208. @!hash_ptr:0..hash_size; {the number of nonzero entries in |hash|}
  2209. @!hash_list:array[0..hash_size] of 0..hash_size; {list of those nonzero entries}
  2210. @!h,@!hh:0..hash_size; {indices into the hash table}
  2211. @!tt:indx; {temporary register}
  2212. @!x_lig_cycle,@!y_lig_cycle:0..256; {problematic ligature pair}
  2213. @ @<Set init...@>=
  2214. hash_ptr:=0; y_lig_cycle:=256;
  2215. for k:=0 to hash_size do hash[k]:=0;
  2216. @ @d lig_exam==lig_kern[lig_ptr].b1
  2217. @d lig_gen==lig_kern[lig_ptr].b3
  2218. @<Check lig...@>=
  2219. begin lig_ptr:=char_remainder[c];
  2220. repeat if hash_input(lig_ptr,c) then
  2221.   begin if lig_kern[lig_ptr].b2<kern_flag then
  2222.     begin if lig_exam<>bchar then
  2223.       check_existence(lig_exam)('LIG character examined by');
  2224. @.LIG character examined...@>
  2225.     check_existence(lig_gen)('LIG character generated by');
  2226. @.LIG character generated...@>
  2227.     if lig_gen>=128 then if(c<128)or(c=256) then
  2228.       if(lig_exam<128)or(lig_exam=bchar) then seven_unsafe:=true;
  2229.     end
  2230.   else if lig_exam<>bchar then
  2231.     check_existence(lig_exam)('KRN character examined by');
  2232. @.KRN character examined...@>
  2233.   end;
  2234. if lig_kern[lig_ptr].b0>=stop_flag then lig_ptr:=nl
  2235. else lig_ptr:=lig_ptr+1+lig_kern[lig_ptr].b0;
  2236. until lig_ptr>=nl;
  2237. @ The |hash_input| procedure is copied from \.{TFtoPL}, but it is made
  2238. into a boolean function that returns |false| if the ligature command
  2239. was masked by a previous one.
  2240. @p function hash_input(@!p,@!c:indx):boolean;
  2241.  {enter data for character |c| and command in location |p|, unless it isn't new}
  2242. label 30; {go here for a quick exit}
  2243. var @!cc:simple..both_z; {class of data being entered}
  2244. @!zz:0..255; {function value or ligature character being entered}
  2245. @!y:0..255; {the character after the cursor}
  2246. @!key:integer; {value to be stored in |hash|}
  2247. @!t:integer; {temporary register for swapping}
  2248. begin if hash_ptr=hash_size then
  2249.   begin hash_input:=false; goto 30;@+end;
  2250. @<Compute the command parameters |y|, |cc|, and |zz|@>;
  2251. key:=256*c+y+1; h:=(1009*key) mod hash_size;
  2252. while hash[h]>0 do
  2253.   begin if hash[h]<=key then
  2254.     begin if hash[h]=key then
  2255.       begin hash_input:=false; goto 30; {unused ligature command}
  2256.       end;
  2257.     t:=hash[h]; hash[h]:=key; key:=t; {do ordered-hash-table insertion}
  2258.     t:=class[h]; class[h]:=cc; cc:=t; {namely, do a swap}
  2259.     t:=lig_z[h]; lig_z[h]:=zz; zz:=t;
  2260.     end;
  2261.   if h>0 then decr(h)@+else h:=hash_size;
  2262.   end;
  2263. hash[h]:=key; class[h]:=cc; lig_z[h]:=zz;
  2264. incr(hash_ptr); hash_list[hash_ptr]:=h;
  2265. hash_input:=true;
  2266. 30:end;
  2267. @ @<Compute the command param...@>=
  2268. y:=lig_kern[p].b1; t:=lig_kern[p].b2; cc:=simple;
  2269. zz:=lig_kern[p].b3;
  2270. if t>=kern_flag then zz:=y
  2271. else begin case t of
  2272.   0,6:do_nothing; {\.{LIG},\.{/LIG>}}
  2273.   5,11:zz:=y; {\.{LIG/>}, \.{/LIG/>>}}
  2274.   1,7:cc:=left_z; {\.{LIG/}, \.{/LIG/>}}
  2275.   2:cc:=right_z; {\.{/LIG}}
  2276.   3:cc:=both_z; {\.{/LIG/}}
  2277.   end; {there are no other cases}
  2278.   end
  2279. @ (More good stuff from \.{TFtoPL}.)
  2280. @p function f(@!h,@!x,@!y:indx):indx; forward;@t\2@>
  2281.   {compute $f$ for arguments known to be in |hash[h]|}
  2282. function eval(@!x,@!y:indx):indx; {compute $f(x,y)$ with hashtable lookup}
  2283. var @!key:integer; {value sought in hash table}
  2284. begin key:=256*x+y+1; h:=(1009*key) mod hash_size;
  2285. while hash[h]>key do
  2286.   if h>0 then decr(h)@+else h:=hash_size;
  2287. if hash[h]<key then eval:=y {not in ordered hash table}
  2288. else eval:=f(h,x,y);
  2289. @ Pascal's beastly convention for |forward| declarations prevents us from
  2290. saying |function f(h,x,y:indx):indx| here.
  2291. @p function f;
  2292. begin case class[h] of
  2293. simple: do_nothing;
  2294. left_z: begin class[h]:=pending; lig_z[h]:=eval(lig_z[h],y); class[h]:=simple;
  2295.   end;
  2296. right_z: begin class[h]:=pending; lig_z[h]:=eval(x,lig_z[h]); class[h]:=simple;
  2297.   end;
  2298. both_z: begin class[h]:=pending; lig_z[h]:=eval(eval(x,lig_z[h]),y);
  2299.   class[h]:=simple;
  2300.   end;
  2301. pending: begin x_lig_cycle:=x; y_lig_cycle:=y; lig_z[h]:=257; class[h]:=simple;
  2302.   end; {the value 257 will break all cycles, since it's not in |hash|}
  2303. end; {there are no other cases}
  2304. f:=lig_z[h];
  2305. @ @<Check for infinite...@>=
  2306. if hash_ptr<hash_size then for hh:=1 to hash_ptr do
  2307.   begin tt:=hash_list[hh];
  2308.   if class[tt]>simple then {make sure $f$ is well defined}
  2309.   tt:=f(tt,(hash[tt]-1)div 256,(hash[tt]-1)mod 256);
  2310.   end;
  2311. if(hash_ptr=hash_size)or(y_lig_cycle<256) then
  2312.   begin if hash_ptr<hash_size then
  2313.     begin print('Infinite ligature loop starting with ');
  2314. @.Infinite ligature loop...@>
  2315.     if x_lig_cycle=256 then print('boundary')@+else print_octal(x_lig_cycle);
  2316.     print(' and '); print_octal(y_lig_cycle); print_ln('!');
  2317.     end
  2318.   else print_ln('Sorry, I haven''t room for so many ligature/kern pairs!');
  2319. @.Sorry, I haven't room...@>
  2320.   print_ln('All ligatures will be cleared.');
  2321.   for c:=0 to 255 do if char_tag[c]=lig_tag then
  2322.     begin char_tag[c]:=no_tag; char_remainder[c]:=0;
  2323.     end;
  2324.   nl:=0; bchar:=256; bchar_label:=@'77777;
  2325.   end
  2326. @ The lig/kern program may still contain references to nonexistent characters,
  2327. if parts of that program are never used. Similarly, there may be extensible
  2328. characters that are never used, because they were overridden by
  2329. \.{NEXTLARGER}, say. This would produce an invalid \.{TFM} file; so we
  2330. must fix such errors.
  2331. @d double_check_tail(#)==@t\1@>if char_wd[0]=0
  2332.       then char_wd[0]:=sort_in(width,0);
  2333.     print('Unused ',#,' refers to nonexistent character ');
  2334.     print_octal(c); print_ln('!');
  2335.     end;
  2336.   end
  2337. @d double_check_lig(#)==begin c:=lig_kern[lig_ptr].#;
  2338.   if char_wd[c]=0 then if c<>bchar then
  2339.     begin lig_kern[lig_ptr].#:=0; double_check_tail
  2340. @d double_check_ext(#)==begin c:=exten[g].#;
  2341.   if c>0 then if char_wd[c]=0 then
  2342.     begin exten[g].#:=0; double_check_tail
  2343. @d double_check_rep(#)==begin c:=exten[g].#;
  2344.   if char_wd[c]=0 then
  2345.     begin exten[g].#:=0; double_check_tail
  2346. @<Doublecheck...@>=
  2347. if nl>0 then for lig_ptr:=0 to nl-1 do
  2348.   if lig_kern[lig_ptr].b2<kern_flag then
  2349.     begin if lig_kern[lig_ptr].b0<255 then
  2350.       begin double_check_lig(b1)('LIG step'); double_check_lig(b3)('LIG step');
  2351.       end;
  2352.     end
  2353.   else double_check_lig(b1)('KRN step');
  2354. @.Unused LIG step...@>
  2355. @.Unused KRN step...@>
  2356. if ne>0 then for g:=0 to ne-1 do
  2357.   begin double_check_ext(b0)('VARCHAR TOP');
  2358.   double_check_ext(b1)('VARCHAR MID');
  2359.   double_check_ext(b2)('VARCHAR BOT');
  2360.   double_check_rep(b3)('VARCHAR REP');
  2361. @.Unused VARCHAR...@>
  2362.   end
  2363. @* The TFM output phase.
  2364. Now that we know how to get all of the font data correctly stored in
  2365. \.{VPtoVF}'s memory, it only remains to write the answers out.
  2366. First of all, it is convenient to have an abbreviation for output to the
  2367. \.{TFM} file:
  2368. @d out(#)==write(tfm_file,#)
  2369. @ The general plan for producing \.{TFM} files is long but simple:
  2370. @<Do the \.{TFM} output@>=
  2371. @<Compute the twelve subfile sizes@>;
  2372. @<Output the twelve subfile sizes@>;
  2373. @<Output the header block@>;
  2374. @<Output the character info@>;
  2375. @<Output the dimensions themselves@>;
  2376. @<Output the ligature/kern program@>;
  2377. @<Output the extensible character recipes@>;
  2378. @<Output the parameters@>
  2379. @ A \.{TFM} file begins with 12 numbers that tell how big its subfiles are.
  2380. We already know most of these numbers; for example, the number of distinct
  2381. widths is |memory[width]+1|, where the $+1$ accounts for the zero width that
  2382. is always supposed to be present. But we still should compute the beginning
  2383. and ending character codes (|bc| and |ec|), the number of header words (|lh|),
  2384. and the total number of words in the \.{TFM} file (|lf|).
  2385. @<Gl...@>=
  2386. @!bc:byte; {the smallest character code in the font}
  2387. @!ec:byte; {the largest character code in the font}
  2388. @!lh:byte; {the number of words in the header block}
  2389. @!lf:0..32767; {the number of words in the entire \.{TFM} file}
  2390. @!not_found:boolean; {has a font character been found?}
  2391. @!temp_width:fix_word; {width being used to compute a check sum}
  2392. @ It might turn out that no characters exist at all. But \.{VPtoVF} keeps
  2393. going and writes the \.{TFM} anyway. In this case |ec| will be~0 and |bc|
  2394. will be~1.
  2395. @<Compute the twelve...@>=
  2396. lh:=header_ptr div 4;@/
  2397. not_found:=true; bc:=0;
  2398. while not_found do
  2399.   if (char_wd[bc]>0)or(bc=255) then not_found:=false
  2400.   else incr(bc);
  2401. not_found:=true; ec:=255;
  2402. while not_found do
  2403.   if (char_wd[ec]>0)or(ec=0) then not_found:=false
  2404.   else decr(ec);
  2405. if bc>ec then bc:=1;
  2406. incr(memory[width]); incr(memory[height]); incr(memory[depth]);
  2407. incr(memory[italic]);@/
  2408. @<Compute the ligature/kern program offset@>;
  2409. lf:=6+lh+(ec-bc+1)+memory[width]+memory[height]+memory[depth]+
  2410. memory[italic]+nl+lk_offset+nk+ne+np;
  2411. @ @d out_size(#)==out((#) div 256); out((#) mod 256)
  2412. @<Output the twelve subfile sizes@>=
  2413. out_size(lf); out_size(lh); out_size(bc); out_size(ec);
  2414. out_size(memory[width]); out_size(memory[height]);
  2415. out_size(memory[depth]); out_size(memory[italic]);
  2416. out_size(nl+lk_offset); out_size(nk); out_size(ne); out_size(np);
  2417. @ The routines that follow need a few temporary variables of different types.
  2418. @<Gl...@>=
  2419. @!j:0..max_header_bytes; {index into |header_bytes|}
  2420. @!p:pointer; {index into |memory|}
  2421. @!q:width..italic; {runs through the list heads for dimensions}
  2422. @!par_ptr:0..max_param_words; {runs through the parameters}
  2423. @ The header block follows the subfile sizes. The necessary information all
  2424. appears in |header_bytes|, except that the design size and the seven-bit-safe
  2425. flag must still be set.
  2426. @<Output the header block@>=
  2427. if not check_sum_specified then @<Compute the check sum@>;
  2428. header_bytes[design_size_loc]:=design_size div @'100000000;
  2429.   {this works since |design_size>0|}
  2430. header_bytes[design_size_loc+1]:=(design_size div @'200000) mod 256;
  2431. header_bytes[design_size_loc+2]:=(design_size div 256) mod 256;
  2432. header_bytes[design_size_loc+3]:=design_size  mod 256;
  2433. if not seven_unsafe then header_bytes[seven_flag_loc]:=128;
  2434. for j:=0 to header_ptr-1 do out(header_bytes[j]);
  2435. @ @<Compute the check sum@>=
  2436. begin c0:=bc; c1:=ec; c2:=bc; c3:=ec;
  2437. for c:=bc to ec do if char_wd[c]>0 then
  2438.   begin temp_width:=memory[char_wd[c]];
  2439.   if design_units<>unity then
  2440.     temp_width:=round((temp_width/design_units)*1048576.0);
  2441.   temp_width:=temp_width + (c+4)*@'20000000; {this should be positive}
  2442.   c0:=(c0+c0+temp_width) mod 255;
  2443.   c1:=(c1+c1+temp_width) mod 253;
  2444.   c2:=(c2+c2+temp_width) mod 251;
  2445.   c3:=(c3+c3+temp_width) mod 247;
  2446.   end;
  2447. header_bytes[check_sum_loc]:=c0;
  2448. header_bytes[check_sum_loc+1]:=c1;
  2449. header_bytes[check_sum_loc+2]:=c2;
  2450. header_bytes[check_sum_loc+3]:=c3;
  2451. @ The next block contains packed |char_info|.
  2452. @<Output the character info@>=
  2453. index[0]:=0;
  2454. for c:=bc to ec do
  2455.   begin out(index[char_wd[c]]);
  2456.   out(index[char_ht[c]]*16+index[char_dp[c]]);
  2457.   out(index[char_ic[c]]*4+char_tag[c]);
  2458.   out(char_remainder[c]);
  2459.   end
  2460. @ When a scaled quantity is output, we may need to divide it by |design_units|.
  2461. The following subroutine takes care of this, using floating point arithmetic
  2462. only if |design_units<>1.0|.
  2463. @p procedure out_scaled(x:fix_word); {outputs a scaled |fix_word|}
  2464. var @!n:byte; {the first byte after the sign}
  2465. @!m:0..65535; {the two least significant bytes}
  2466. begin if abs(x/design_units)>=16.0 then
  2467.   begin print_ln('The relative dimension ',x/@'4000000:1:3,
  2468.     ' is too large.');
  2469. @.The relative dimension...@>
  2470.   print('  (Must be less than 16*designsize');
  2471.   if design_units<>unity then print(' =',design_units/@'200000:1:3,
  2472.       ' designunits');
  2473.   print_ln(')'); x:=0;
  2474.   end;
  2475. if design_units<>unity then x:=round((x/design_units)*1048576.0);
  2476. if x<0 then
  2477.   begin out(255); x:=x+@'100000000;
  2478.   if x<=0 then x:=1;
  2479.   end
  2480. else begin out(0);
  2481.   if x>=@'100000000 then x:=@'77777777;
  2482.   end;
  2483. n:=x div @'200000; m:=x mod @'200000;
  2484. out(n); out(m div 256); out(m mod 256);
  2485. @ We have output the packed indices for individual characters.
  2486. The scaled widths, heights, depths, and italic corrections are next.
  2487. @<Output the dimensions themselves@>=
  2488. for q:=width to italic do
  2489.   begin out(0); out(0); out(0); out(0); {output the zero word}
  2490.   p:=link[q]; {head of list}
  2491.   while p>0 do
  2492.     begin out_scaled(memory[p]);
  2493.     p:=link[p];
  2494.     end;
  2495.   end;
  2496. @ One embarrassing problem remains: The ligature/kern program might be very
  2497. long, but the starting addresses in |char_remainder| can be at most~255.
  2498. Therefore we need to output some indirect address information; we want to
  2499. compute |lk_offset| so that addition of |lk_offset| to all remainders makes
  2500. all but |lk_offset| distinct remainders less than~256.
  2501. For this we need a sorted table of all relevant remainders.
  2502. @<Glob...@>=
  2503. @!label_table:array[0..256] of record
  2504.   @!rr: -1..@'77777; {sorted label values}
  2505.   @!cc: byte; {associated characters}
  2506.   end;
  2507. @!label_ptr:0..256; {index of highest entry in |label_table|}
  2508. @!sort_ptr:0..256; {index into |label_table|}
  2509. @!lk_offset:0..256; {smallest offset value that might work}
  2510. @!t:0..@'77777; {label value that is being redirected}
  2511. @!extra_loc_needed:boolean; {do we need a special word for |bchar|?}
  2512. @ @<Compute the ligature/kern program offset@>=
  2513. @<Insert all labels into |label_table|@>;
  2514. if bchar<256 then
  2515.   begin extra_loc_needed:=true; lk_offset:=1;
  2516.   end
  2517. else begin extra_loc_needed:=false; lk_offset:=0;
  2518.   end;
  2519. @<Find the minimum |lk_offset| and adjust all remainders@>;
  2520. if bchar_label<@'77777 then
  2521.   begin lig_kern[nl-1].b2:=(bchar_label+lk_offset)div 256;
  2522.   lig_kern[nl-1].b3:=(bchar_label+lk_offset)mod 256;
  2523.   end
  2524. @ @<Insert all labels...@>=
  2525. label_ptr:=0; label_table[0].rr:=-1; {sentinel}
  2526. for c:=bc to ec do if char_tag[c]=lig_tag then
  2527.   begin sort_ptr:=label_ptr; {there's a hole at position |sort_ptr+1|}
  2528.   while label_table[sort_ptr].rr>char_remainder[c] do
  2529.     begin label_table[sort_ptr+1]:=label_table[sort_ptr];
  2530.     decr(sort_ptr); {move the hole}
  2531.     end;
  2532.   label_table[sort_ptr+1].cc:=c;
  2533.   label_table[sort_ptr+1].rr:=char_remainder[c];
  2534.   incr(label_ptr);
  2535.   end
  2536. @ @<Find the minimum |lk_offset| and adjust all remainders@>=
  2537. begin sort_ptr:=label_ptr; {the largest unallocated label}
  2538. if label_table[sort_ptr].rr+lk_offset > 255 then
  2539.   begin lk_offset:=0; extra_loc_needed:=false; {location 0 can do double duty}
  2540.   repeat char_remainder[label_table[sort_ptr].cc]:=lk_offset;
  2541.   while label_table[sort_ptr-1].rr=label_table[sort_ptr].rr do
  2542.     begin decr(sort_ptr); char_remainder[label_table[sort_ptr].cc]:=lk_offset;
  2543.     end;
  2544.   incr(lk_offset); decr(sort_ptr);
  2545.   until lk_offset+label_table[sort_ptr].rr<256;
  2546.     {N.B.: |lk_offset=256| satisfies this when |sort_ptr=0|}
  2547.   end;
  2548. if lk_offset>0 then while sort_ptr>0 do
  2549.   begin char_remainder[label_table[sort_ptr].cc]:=
  2550.     char_remainder[label_table[sort_ptr].cc]+lk_offset;
  2551.   decr(sort_ptr);
  2552.   end;
  2553. @ @<Output the ligature/kern program@>=
  2554. if extra_loc_needed then {|lk_offset=1|}
  2555.   begin out(255); out(bchar); out(0); out(0);
  2556.   end
  2557. else for sort_ptr:=1 to lk_offset do {output the redirection specs}
  2558.   begin t:=label_table[label_ptr].rr;
  2559.   if bchar<256 then
  2560.     begin out(255); out(bchar);
  2561.     end
  2562.   else begin out(254); out(0);
  2563.     end;
  2564.   out_size(t+lk_offset);
  2565.   repeat decr(label_ptr); until label_table[label_ptr].rr<t;
  2566.   end;
  2567. if nl>0 then for lig_ptr:=0 to nl-1 do
  2568.   begin out(lig_kern[lig_ptr].b0);
  2569.   out(lig_kern[lig_ptr].b1);
  2570.   out(lig_kern[lig_ptr].b2);
  2571.   out(lig_kern[lig_ptr].b3);
  2572.   end;
  2573. if nk>0 then for krn_ptr:=0 to nk-1 do out_scaled(kern[krn_ptr])
  2574. @ @<Output the extensible character recipes@>=
  2575. if ne>0 then for c:=0 to ne-1 do
  2576.   begin out(exten[c].b0);
  2577.   out(exten[c].b1);
  2578.   out(exten[c].b2);
  2579.   out(exten[c].b3);
  2580.   end;
  2581. @ For our grand finale, we wind everything up by outputting the parameters.
  2582. @<Output the parameters@>=
  2583. for par_ptr:=1 to np do
  2584.   begin if par_ptr=1 then
  2585.     @<Output the slant (|param[1]|) without scaling@>
  2586.   else out_scaled(param[par_ptr]);
  2587.   end
  2588. @ @<Output the slant...@>=
  2589. begin if param[1]<0 then
  2590.   begin param[1]:=param[1]+@'10000000000;
  2591.   out((param[1] div @'100000000)+256-64);
  2592.   end
  2593. else out(param[1] div @'100000000);
  2594. out((param[1] div @'200000) mod 256);
  2595. out((param[1] div 256) mod 256);
  2596. out(param[1] mod 256);
  2597. @* The VF output phase.
  2598. Output to |vf_file| is considerably simpler.
  2599. @d id_byte=202 {current version of \.{VF} format}
  2600. @d vout(#)==write(vf_file,#)
  2601. @<Glob...@>=
  2602. @!vcount:integer; {number of bytes written to |vf_file|}
  2603. @ We need a routine to output integers as four bytes. Negative values
  2604. will never be less than $-2^{24}$.
  2605. @p procedure vout_int(@!x:integer);
  2606. begin if x>=0 then vout(x div @'100000000)
  2607. else begin vout(255); x:=x+@'100000000;
  2608.   end;
  2609. vout((x div @'200000) mod 256);
  2610. vout((x div @'400) mod 256); vout(x mod 256);
  2611. @ @<Do the \.{VF} output@>=
  2612. vout(pre); vout(id_byte); vout(vtitle_length);
  2613. for k:=0 to vtitle_length-1 do vout(vf[vtitle_start+k]);
  2614. for k:=check_sum_loc to design_size_loc+3 do vout(header_bytes[k]);
  2615. vcount:=vtitle_length+11;
  2616. for cur_font:=0 to font_ptr-1 do @<Output a local font definition@>;
  2617. for c:=bc to ec do if char_wd[c]>0 then
  2618.   @<Output a packet for character |c|@>;
  2619. repeat vout(post); incr(vcount);
  2620. until vcount mod 4 = 0
  2621. @ @<Output a local font definition@>=
  2622. begin vout(fnt_def1); vout(cur_font);@/
  2623. vout(font_checksum[cur_font].b0);
  2624. vout(font_checksum[cur_font].b1);
  2625. vout(font_checksum[cur_font].b2);
  2626. vout(font_checksum[cur_font].b3);
  2627. vout_int(font_at[cur_font]);
  2628. vout_int(font_dsize[cur_font]);
  2629. vout(farea_length[cur_font]);
  2630. vout(fname_length[cur_font]);
  2631. for k:=0 to farea_length[cur_font]-1 do vout(vf[farea_start[cur_font]+k]);
  2632. if fname_start[cur_font]=vf_size then
  2633.   begin vout("N"); vout("U"); vout("L"); vout("L");
  2634.   end
  2635. else for k:=0 to fname_length[cur_font]-1 do vout(vf[fname_start[cur_font]+k]);
  2636. vcount:=vcount+12+farea_length[cur_font]+fname_length[cur_font];
  2637. @ @<Output a packet for character |c|@>=
  2638. begin x:=memory[char_wd[c]];
  2639. if design_units<>unity then x:=round((x/design_units)*1048576.0);
  2640. if (packet_length[c]>241)or(x<0)or(x>=@'100000000) then
  2641.   begin vout(242); vout_int(packet_length[c]); vout_int(c); vout_int(x);
  2642.   vcount:=vcount+13+packet_length[c];
  2643.   end
  2644. else begin vout(packet_length[c]); vout(c); vout(x div @'200000);
  2645.   vout((x div @'400) mod 256); vout(x mod 256);
  2646.   vcount:=vcount+5+packet_length[c];
  2647.   end;
  2648. if packet_start[c]=vf_size then
  2649.   begin if c>=128 then vout(set1);
  2650.   vout(c);
  2651.   end
  2652. else for k:=0 to packet_length[c]-1 do vout(vf[packet_start[c]+k]);
  2653. @* The main program.
  2654. The routines sketched out so far need to be packaged into separate procedures,
  2655. on some systems, since some \PASCAL\ compilers place a strict limit on the
  2656. size of a routine. The packaging is done here in an attempt to avoid some
  2657. system-dependent changes.
  2658. @p procedure param_enter;
  2659. begin @<Enter the parameter names@>;
  2660. procedure vpl_enter;
  2661. begin @<Enter all the \.{VPL} names@>;
  2662. procedure name_enter; {enter all names and their equivalents}
  2663. begin @<Enter all the \.{PL} names...@>;
  2664. vpl_enter; param_enter;
  2665. procedure read_lig_kern;
  2666. var @!krn_ptr:0..max_kerns; {an index into |kern|}
  2667. @!c:byte; {runs through all character codes}
  2668. begin @<Read ligature/kern list@>;
  2669. procedure read_char_info;
  2670. var @!c:byte; {the char}
  2671. begin @<Read character info list@>;
  2672. procedure read_input;
  2673. var @!c:byte; {header or parameter index}
  2674. begin @<Read all the input@>;
  2675. procedure corr_and_check;
  2676. var @!c:0..256; {runs through all character codes}
  2677. @!hh:0..hash_size; {an index into |hash_list|}
  2678. @!lig_ptr:0..max_lig_steps; {an index into |lig_kern|}
  2679. @!g:byte; {a character generated by the current character |c|}
  2680. begin @<Correct and check the information@>
  2681. procedure vf_output;
  2682. var @!c:byte; {runs through all character codes}
  2683. @!cur_font:0..256; {runs through all local fonts}
  2684. @!k:integer; {loop index}
  2685. begin @<Do the \.{VF} output@>;
  2686. @ Here is where \.{VPtoVF} begins and ends.
  2687. @p begin initialize;@/
  2688. name_enter;@/
  2689. read_input; print_ln('.');@/
  2690. corr_and_check;@/
  2691. @<Do the \.{TFM} output@>;
  2692. vf_output;
  2693. @* System-dependent changes.
  2694. This section should be replaced, if necessary, by changes to the program
  2695. that are necessary to make \.{VPtoVF} work at a particular installation.
  2696. It is usually best to design your change file so that all changes to
  2697. previous sections preserve the section numbering; then everybody's version
  2698. will be consistent with the printed program. More extensive changes,
  2699. which introduce new sections, can be inserted here; then only the index
  2700. itself will get a new section number.
  2701. @^system dependencies@>
  2702. @* Index.
  2703. Pointers to error messages appear here together with the section numbers
  2704. where each ident\-i\-fier is used.
  2705.