home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / tex / lindner / texware.zoo / source / tftopl / tftopl.lzh / TFTOPL.CWB < prev    next >
Encoding:
Text File  |  1990-05-21  |  63.8 KB  |  1,736 lines

  1. % This program by D. E. Knuth is not copyrighted and can be used freely.
  2. % Version 0 was implemented in January 1982.
  3. % In February 1982 a new restriction on ligature steps was added.
  4. % In June 1982 the routines were divided into smaller pieces for IBM people,
  5. % and the result was designated "Version 1" in September 1982.
  6. % Slight changes were made in October, 1982, for version 0.6 of TeX.
  7. % Version 2 (July 1983) was released with TeX version 0.999.
  8. % Version 2.1 (September 1983) changed TEXINFO to FONTDIMEN.
  9. % Version 2.2 (February 1984) simplified decimal fraction output.
  10. % Version 2.3 (May 1984) fixed a bug when lh=17.
  11. % Version 2.4 (July 1984) fixed a bug involving unused ligature code.
  12. % Version 2.5 (September 1985) updated the standard codingscheme names.
  13. % Version 3 (October 1989) introduced new ligature capabilities.
  14. % Version 3.1 (November 1989) renamed z[] to lig_z[] for better portability.
  15.  
  16. % Here is TeX material that gets inserted after \input webmac
  17. \def\hang{\hangindent 3em\indent\ignorespaces}
  18. \font\ninerm=cmr9
  19. \let\mc=\ninerm % medium caps for names like SAIL
  20. \def\PASCAL{Pascal}
  21.  
  22. \def\(#1){} % this is used to make section names sort themselves better
  23. \def\9#1{} % this is used for sort keys in the index
  24.  
  25. \def\title{TF\lowercase{to}PL}
  26. \def\contentspagenumber{201}
  27. \def\topofcontents{\null
  28.   \def\titlepage{F} % include headline on the contents page
  29.   \def\rheader{\mainfont\hfil \contentspagenumber}
  30.   \vfill
  31.   \centerline{\titlefont The {\ttitlefont TFtoPL} processor}
  32.   \vskip 15pt
  33.   \centerline{(Version 3.1, November 1989)}
  34.   \vfill}
  35. \def\botofcontents{\vfill
  36.   \centerline{\hsize 5in\baselineskip9pt
  37.     \vbox{\ninerm\noindent
  38.     The preparation of this report
  39.     was supported in part by the National Science
  40.     Foundation under grants IST-8201926 and MCS-8300984,
  41.     and by the System Development Foundation. `\TeX' is a
  42.     trademark of the American Mathematical Society.}}}
  43. \pageno=\contentspagenumber \advance\pageno by 1
  44.  
  45. @* Introduction.
  46. The \.{TFtoPL} utility program converts \TeX\ font metric (``\.{TFM}'')
  47. files into equivalent property-list (``\.{PL}'') files. It also
  48. makes a thorough check of the given \.{TFM} file, using essentially the
  49. same algorithm as \TeX. Thus if \TeX\ complains that a \.{TFM}
  50. file is ``bad,'' this program will pinpoint the source or sources of
  51. badness. A \.{PL} file output by this program can be edited with
  52. a normal text editor, and the result can be converted back to \.{TFM}
  53. format using the companion program \.{PLtoTF}.
  54.  
  55. The first \.{TFtoPL} program was designed by Leo Guibas in the summer of
  56. 1978. Contributions by Frank Liang, Doug Wyatt, and Lyle Ramshaw
  57. also had a significant effect on the evolution of the present code.
  58.  
  59. Extensions for an enhanced ligature mechanism were added by the author in 1989.
  60.  
  61. The |banner| string defined here should be changed whenever \.{TFtoPL}
  62. gets modified.
  63.  
  64. @d @!banner "This is TFtoPL, Version 3.1" /* printed when the program starts */
  65. @d @!local_banner "Local Version"
  66.  
  67. @ This program is written entirely in standard \PASCAL, except that
  68. it occasionally has lower case letters in strings that are output.
  69. Such letters can be converted to upper case if necessary. The input is read
  70. from |tfm_file|, and the output is written on |pl_file|; error messages and
  71. other remarks are written on the |output| file, which the user may
  72. choose to assign to the terminal if the system permits it.
  73. @^system dependencies@>
  74.  
  75. The term |print| is used instead of |write| when this program writes on
  76. the |output| file, so that all such output can be easily deflected.
  77. @d NAME_LENGTH 120
  78. @d odd(a) ((a)&0x1)
  79. @c
  80. #include <stdio.h>
  81. #include <stdlib.h>
  82. #include <string.h>
  83. #include "portab.h"
  84. @#
  85. FILE * @!output = stdout;
  86. char tfm_name[NAME_LENGTH], pl_name[NAME_LENGTH], output_name[NAME_LENGTH];
  87. @#
  88. @<Types in the outer block@>@/
  89. @<Globals in the outer block@>@/
  90. void initialize(void) /* this procedure gets things started properly */
  91. { if (output_name[0] != '\0') {
  92.      output = fopen(output_name, "w");
  93.      if (output == NULL) {
  94.         fprintf(stderr, "I can't open log file \"%s\"\n", output_name);
  95.         fprintf(stderr, "The output will appear on the terminal\n");
  96.         output = stdout;
  97.         }
  98.      }
  99.   fprintf(output, "%s\n", banner);@/
  100.   fprintf(output, "%s\n", local_banner);@/
  101.   @<Set initial values@>@/
  102.   }
  103.  
  104. @ If the program has to stop prematurely, it goes to the
  105. `|final_end|'.
  106.  
  107. @ The following parameters can be changed at compile time to extend or
  108. reduce \.{TFtoPL}'s capacity.
  109. @d @!tfm_size 30000 /* maximum length of |tfm| data, in bytes */
  110. @d @!lig_size 5000 /* maximum length of |lig_kern| program, in words */
  111. @d @!hash_size 5003 /* preferably a prime number, a bit larger than the number
  112.   of character pairs in lig/kern steps */
  113.  
  114. @
  115. @<Types...@>=
  116. typedef UWORD @!tfm_size_type;
  117. typedef UWORD @!lig_size_type;
  118. typedef UWORD @!hash_size_type;
  119.  
  120. @ Here are some macros for common programming idioms.
  121.  
  122. @d incr(a)   (a)++ /* increase a variable by unity */
  123. @d decr(a)   (a)-- /* decrease a variable by unity */
  124. @d do_nothing   /* empty statement */
  125.  
  126. @* Font metric data.
  127. The idea behind \.{TFM} files is that typesetting routines like \TeX\
  128. need a compact way to store the relevant information about several
  129. dozen fonts, and computer centers need a compact way to store the
  130. relevant information about several hundred fonts. \.{TFM} files are
  131. compact, and most of the information they contain is highly relevant,
  132. so they provide a solution to the problem.
  133.  
  134. The information in a \.{TFM} file appears in a sequence of 8-bit bytes.
  135. Since the number of bytes is always a multiple of 4, we could
  136. also regard the file as a sequence of 32-bit words; but \TeX\ uses the
  137. byte interpretation, and so does \.{TFtoPL}. Note that the bytes
  138. are considered to be unsigned numbers.
  139.  
  140. @<Glob...@>=
  141. FILE * @!tfm_file;
  142.  
  143. @ On some systems you may have to do something special to read a
  144. packed file of bytes. For example, the following code didn't work
  145. when it was first tried at Stanford, because packed files have to be
  146. opened with a special switch setting on the \PASCAL\ that was used.
  147. @^system dependencies@>
  148.  
  149. @<Set init...@>=
  150. tfm_file = fopen(tfm_name, "rb");
  151. if (tfm_file == NULL) {
  152.    fprintf(stderr, "I can't open TFM file \"%s\"\n", tfm_name);
  153.    exit(1);
  154.    }
  155.  
  156. @ The first 24 bytes (6 words) of a \.{TFM} file contain twelve 16-bit
  157. integers that give the lengths of the various subsequent portions
  158. of the file. These twelve integers are, in order:
  159. $$\vbox{\halign{\hfil#&$\null=\null$#\hfil\cr
  160. |@!lf|&length of the entire file, in words;\cr
  161. |@!lh|&length of the header data, in words;\cr
  162. |@!bc|&smallest character code in the font;\cr
  163. |@!ec|&largest character code in the font;\cr
  164. |@!nw|&number of words in the width table;\cr
  165. |@!nh|&number of words in the height table;\cr
  166. |@!nd|&number of words in the depth table;\cr
  167. |@!ni|&number of words in the italic correction table;\cr
  168. |@!nl|&number of words in the ligfdivkern table;\cr
  169. |@!nk|&number of words in the kern table;\cr
  170. |@!ne|&number of words in the extensible character table;\cr
  171. |@!np|&number of font parameter words.\cr}}$$
  172. They are all nonnegative and less than $2^{15}$. We must have |bc-1<=ec<=255|,
  173. |ne<=256|, and
  174. $$\hbox{|lf=6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np|.}$$
  175. Note that a font may contain as many as 256 characters (if |bc=0| and |ec=255|),
  176. and as few as 0 characters (if |bc=ec+1|).
  177.  
  178. Incidentally, when two or more 8-bit bytes are combined to form an integer of
  179. 16 or more bits, the most significant bytes appear first in the file.
  180. This is called BigEndian order.
  181.  
  182. @<Glob...@>=
  183. UWORD @!lf,@!lh,@!bc,@!ec,@!nw,@!nh,@!nd,@!ni,@!nl,@!nk,@!ne,@!np;
  184.   /* subfile sizes */
  185.  
  186. @ The rest of the \.{TFM} file may be regarded as a sequence of ten data
  187. arrays having the informal specification
  188. $$\def\arr$[#1]#2${\&{array} $[#1]$ \&{of} #2}
  189. \vbox{\halign{\hfil\\{#}&$\,:\,$\arr#\hfil\cr
  190. header&|[0..lh-1]stuff|\cr
  191. char\_info&|[bc..ec]char_info_word|\cr
  192. width&|[0..nw-1]fix_word|\cr
  193. height&|[0..nh-1]fix_word|\cr
  194. depth&|[0..nd-1]fix_word|\cr
  195. italic&|[0..ni-1]fix_word|\cr
  196. lig\_kern&|[0..nl-1]lig_kern_command|\cr
  197. kern&|[0..nk-1]fix_word|\cr
  198. exten&|[0..ne-1]extensible_recipe|\cr
  199. param&|[1..np]fix_word|\cr}}$$
  200. The most important data type used here is a |@!fix_word|, which is
  201. a 32-bit representation of a binary fraction. A |fix_word| is a signed
  202. quantity, with the two's complement of the entire word used to represent
  203. negation. Of the 32 bits in a |fix_word|, exactly 12 are to the left of the
  204. binary point; thus, the largest |fix_word| value is $2048-2^{-20}$, and
  205. the smallest is $-2048$. We will see below, however, that all but one of
  206. the |fix_word| values will lie between $-16$ and $+16$.
  207.  
  208. @ The first data array is a block of header information, which contains
  209. general facts about the font. The header must contain at least two words,
  210. and for \.{TFM} files to be used with Xerox printing software it must
  211. contain at least 18 words, allocated as described below. When different
  212. kinds of devices need to be interfaced, it may be necessary to add further
  213. words to the header block.
  214.  
  215. \yskip\hang|header[0]| is a 32-bit check sum that \TeX\ will copy into the
  216. \.{DVI} output file whenever it uses the font.  Later on when the \.{DVI}
  217. file is printed, possibly on another computer, the actual font that gets
  218. used is supposed to have a check sum that agrees with the one in the
  219. \.{TFM} file used by \TeX. In this way, users will be warned about
  220. potential incompatibilities. (However, if the check sum is zero in either
  221. the font file or the \.{TFM} file, no check is made.)  The actual relation
  222. between this check sum and the rest of the \.{TFM} file is not important;
  223. the check sum is simply an identification number with the property that
  224. incompatible fonts almost always have distinct check sums.
  225. @^check sum@>
  226.  
  227. \yskip\hang|header[1]| is a |fix_word| containing the design size of the
  228. font, in units of \TeX\ points (7227 \TeX\ points = 254 cm).  This number
  229. must be at least 1.0; it is fairly arbitrary, but usually the design size
  230. is 10.0 for a ``10 point'' font, i.e., a font that was designed to look
  231. best at a 10-point size, whatever that really means. When a \TeX\ user
  232. asks for a font `\.{at} $\delta$ \.{pt}', the effect is to override the
  233. design size and replace it by $\delta$, and to multiply the $x$ and~$y$
  234. coordinates of the points in the font image by a factor of $\delta$
  235. divided by the design size.  {\sl All other dimensions in the\fdiv\ \.{TFM}
  236. file are |fix_word|\kern-1pt\ numbers in design-size units.} Thus, for example,
  237. the value of |param[6]|, one \.{em} or \.{\\quad}, is often the |fix_word|
  238. value $2^{20}=1.0$, since many fonts have a design size equal to one em.
  239. The other dimensions must be less than 16 design-size units in absolute
  240. value; thus, |header[1]| and |param[1]| are the only |fix_word| entries in
  241. the whole \.{TFM} file whose first byte might be something besides 0 or
  242. 255.  @^design size@>
  243.  
  244. \yskip\hang|header[2..11]|, if present, contains 40 bytes that identify
  245. the character coding scheme. The first byte, which must be between 0 and
  246. 39, is the number of subsequent ASCII bytes actually relevant in this
  247. string, which is intended to specify what character-code-to-symbol
  248. convention is present in the font.  Examples are \.{ASCII} for standard
  249. ASCII, \.{TeX text} for fonts like \.{cmr10} and \.{cmti9}, \.{TeX math
  250. extension} for \.{cmex10}, \.{XEROX text} for Xerox fonts, \.{GRAPHIC} for
  251. special-purpose non-alphabetic fonts, \.{UNSPECIFIED} for the default case
  252. when there is no information.  Parentheses should not appear in this name.
  253. (Such a string is said to be in {\mc BCPL} format.)
  254. @^coding scheme@>
  255.  
  256. \yskip\hang|header[12..16]|, if present, contains 20 bytes that name the
  257. font family (e.g., \.{CMR} or \.{HELVETICA}), in {\mc BCPL} format.
  258. This field is also known as the ``font identifier.''
  259. @^family name@>
  260. @^font identifier@>
  261.  
  262. \yskip\hang|header[17]|, if present, contains a first byte called the
  263. |seven_bit_safe_flag|, then two bytes that are ignored, and a fourth byte
  264. called the |face|. If the value of the fourth byte is less than 18, it has
  265. the following interpretation as a ``weight, slope, and expansion'':  Add 0
  266. or 2 or 4 (for medium or bold or light) to 0 or 1 (for roman or italic) to
  267. 0 or 6 or 12 (for regular or condensed or extended).  For example, 13 is
  268. 0+1+12, so it represents medium italic extended.  A three-letter code
  269. (e.g., \.{MIE}) can be used for such |face| data.
  270.  
  271. \yskip\hang|header[18..@twhatever@>]| might also be present; the individual
  272. words are simply called |header[18]|, |header[19]|, etc., at the moment.
  273.  
  274. @ Next comes the |char_info| array, which contains one |char_info_word|
  275. per character. Each |char_info_word| contains six fields packed into
  276. four bytes as follows.
  277.  
  278. \yskip\hang first byte: |width_index| (8 bits)\par
  279. \hang second byte: |height_index| (4 bits) times 16, plus |depth_index|
  280.   (4~bits)\par
  281. \hang third byte: |italic_index| (6 bits) times 4, plus |tag|
  282.   (2~bits)\par
  283. \hang fourth byte: |remainder| (8 bits)\par
  284. \yskip\noindent
  285. The actual width of a character is |width[width_index]|, in design-size
  286. units; this is a device for compressing information, since many characters
  287. have the same width. Since it is quite common for many characters
  288. to have the same height, depth, or italic correction, the \.{TFM} format
  289. imposes a limit of 16 different heights, 16 different depths, and
  290. 64 different italic corrections.
  291.  
  292. Incidentally, the relation |width[0]=height[0]=depth[0]=italic[0]=0|
  293. should always hold, so that an index of zero implies a value of zero.
  294. The |width_index| should never be zero unless the character does
  295. not exist in the font, since a character is valid if and only if it lies
  296. between |bc| and |ec| and has a nonzero |width_index|.
  297.  
  298. @ The |tag| field in a |char_info_word| has four values that explain how to
  299. interpret the |remainder| field.
  300.  
  301. \yskip\hang|tag=0| (|no_tag|) means that |remainder| is unused.\par
  302. \hang|tag=1| (|lig_tag|) means that this character has a ligaturefdivkerning
  303. program starting at |lig_kern[remainder]|.\par
  304. \hang|tag=2| (|list_tag|) means that this character is part of a chain of
  305. characters of ascending sizes, and not the largest in the chain.  The
  306. |remainder| field gives the character code of the next larger character.\par
  307. \hang|tag=3| (|ext_tag|) means that this character code represents an
  308. extensible character, i.e., a character that is built up of smaller pieces
  309. so that it can be made arbitrarily large. The pieces are specified in
  310. |exten[remainder]|.\par
  311.  
  312. @d no_tag 0 /* vanilla character */
  313. @d lig_tag 1 /* character has a ligature/kerning program */
  314. @d list_tag 2 /* character has a successor in a charlist */
  315. @d ext_tag 3 /* character is extensible */
  316.  
  317. @ The |lig_kern| array contains instructions in a simple programming language
  318. that explains what to do for special letter pairs. Each word is a
  319. |lig_kern_command| of four bytes.
  320.  
  321. \yskip\hang first byte: |skip_byte|, indicates that this is the final program
  322.   step if the byte is 128 or more, otherwise the next step is obtained by
  323.   skipping this number of intervening steps.\par
  324. \hang second byte: |next_char|, ``if |next_char| follows the current character,
  325.   then perform the operation and stop, otherwise continue.''\par
  326. \hang third byte: |op_byte|, indicates a ligature step if less than~128,
  327.   a kern step otherwise.\par
  328. \hang fourth byte: |remainder|.\par
  329. \yskip\noindent
  330. In a kern step, an
  331. additional space equal to |kern[256*(op_byte-128)+remainder]| is inserted
  332. between the current character and |next_char|. This amount is
  333. often negative, so that the characters are brought closer together
  334. by kerning; but it might be positive.
  335.  
  336. There are eight kinds of ligature steps, having |op_byte| codes $4a+2b+c$ where
  337. $0\le a\le b+c$ and $0\le b,c\le1$. The character whose code is
  338. |remainder| is inserted between the current character and |next_char|;
  339. then the current character is deleted if $b=0$, and |next_char| is
  340. deleted if $c=0$; then we pass over $a$~characters to reach the next
  341. current character (which may have a ligaturefdivkerning program of its own).
  342.  
  343. Notice that if $a=0$ and $b=1$, the current character is unchanged; if
  344. $a=b$ and $c=1$, the current character is changed but the next character is
  345. unchanged. \.{TFtoPL} will check to see that infinite loops are avoided.
  346.  
  347. If the very first instruction of the |lig_kern| array has |skip_byte=255|,
  348. the |next_char| byte is the so-called right boundary character of this font;
  349. the value of |next_char| need not lie between |bc| and~|ec|.
  350. If the very last instruction of the |lig_kern| array has |skip_byte=255|,
  351. there is a special ligaturefdivkerning program for a left boundary character,
  352. beginning at location |256*op_byte+remainder|.
  353. The interpretation is that \TeX\ puts implicit boundary characters
  354. before and after each consecutive string of characters from the same font.
  355. These implicit characters do not appear in the output, but they can affect
  356. ligatures and kerning.
  357.  
  358. If the very first instruction of a character's |lig_kern| program has
  359. |skip_byte>128|, the program actually begins in location
  360. |256*op_byte+remainder|. This feature allows access to large |lig_kern|
  361. arrays, because the first instruction must otherwise
  362. appear in a location |<=255|.
  363.  
  364. Any instruction with |skip_byte>128| in the |lig_kern| array must have
  365. |256*op_byte+remainder<nl|. If such an instruction is encountered during
  366. normal program execution, it denotes an unconditional halt; no ligature
  367. command is performed.
  368.  
  369. @d stop_flag 128 /* value indicating `\.{STOP}' in a lig/kern program */
  370. @d kern_flag 128 /* op code for a kern step */
  371.  
  372. @ Extensible characters are specified by an |extensible_recipe|,
  373. which consists of four bytes called |top|, |mid|,
  374. |bot|, and |rep| (in this order). These bytes are the character codes
  375. of individual pieces used to build up a large symbol.
  376. If |top|, |mid|, or |bot| are zero,
  377. they are not present in the built-up result. For example, an extensible
  378. vertical line is like an extensible bracket, except that the top and
  379. bottom pieces are missing.
  380.  
  381.  
  382. @ The final portion of a \.{TFM} file is the |param| array, which is another
  383. sequence of |fix_word| values.
  384.  
  385. \yskip\hang|param[1]=@!slant| is the amount of italic slant, which is used
  386. to help position accents. For example, |slant=.25| means that when you go
  387. up one unit, you also go .25 units to the right. The |slant| is a pure
  388. number; it's the only |fix_word| other than the design size itself that is
  389. not scaled by the design size.
  390.  
  391. \hang|param[2]=space| is the normal spacing between words in text.
  392. Note that character |" "| in the font need not have anything to do with
  393. blank spaces.
  394.  
  395. \hang|param[3]=space_stretch| is the amount of glue stretching between words.
  396.  
  397. \hang|param[4]=space_shrink| is the amount of glue shrinking between words.
  398.  
  399. \hang|param[5]=x_height| is the height of letters for which accents don't
  400. have to be raised or lowered.
  401.  
  402. \hang|param[6]=quad| is the size of one em in the font.
  403.  
  404. \hang|param[7]=extra_space| is the amount added to |param[2]| at the
  405. ends of sentences.
  406.  
  407. When the character coding scheme is \.{TeX math symbols}, the font is
  408. supposed to have 15 additional parameters called |num1|, |num2|, |num3|,
  409. |denom1|, |denom2|, |sup1|, |sup2|, |sup3|, |sub1|, |sub2|, |supdrop|,
  410. |subdrop|, |delim1|, |delim2|, and |axis_height|, respectively. When the
  411. character coding scheme is \.{TeX math extension}, the font is supposed to
  412. have six additional parameters called |default_rule_thickness| and
  413. |big_op_spacing1| through |big_op_spacing5|.
  414.  
  415. @ So that is what \.{TFM} files hold. The next question is, ``What about
  416. \.{PL} files?'' A complete answer to that question appears in the
  417. documentation of the companion program, \.{PLtoTF}, so it will not
  418. be repeated here. Suffice it to say that a \.{PL} file is an ordinary
  419. \PASCAL\ text file, and that the output of \.{TFtoPL} uses only a
  420. subset of the possible constructions that might appear in a \.{PL} file.
  421. Furthermore, hardly anybody really wants to look at the formal
  422. definition of \.{PL} format, because it is almost self-explanatory when
  423. you see an example or two.
  424.  
  425. @<Glob...@>=
  426. FILE * @!pl_file;
  427.  
  428. @ @<Set init...@>=
  429. pl_file = fopen(pl_name, "w");
  430. if (pl_file == NULL) {
  431.    fprintf(stderr, "I can't open the PL file \"%s\"\n", pl_name);
  432.    exit(1);
  433.    }
  434.  
  435. @* Unpacked representation.
  436. The first thing \.{TFtoPL} does is read the entire |tfm_file| into an array of
  437. bytes, |tfm[0..(4*lf-1)]|.
  438.  
  439. @<Types...@>=
  440. typedef UBYTE @!byte; /* unsigned eight-bit quantity */
  441. typedef tfm_size_type @!index; /* address of a byte in |tfm| */
  442.  
  443. @d tfm(a) internal_tfm[a+1000]
  444.  
  445. @ @<Glob...@>=
  446. byte @!internal_tfm[tfm_size+1002]; /* the input data all goes here */
  447.  /* the negative addresses avoid range checks for invalid characters */
  448.  
  449. @ The input may, of course, be all screwed up and not a \.{TFM} file
  450. at all. So we begin cautiously.
  451.  
  452. @d pabort(a) { fprintf(output, "%s\n", a);
  453.   fprintf(output, "%s\n", "Sorry, but I can't go on; are you sure this is a TFM?");
  454.   goto final_end;
  455.   }
  456. @d pabortp(a, b) { fprintf(output, "%s\n", a, b);
  457.   fprintf(output, "%s\n", "Sorry, but I can't go on; are you sure this is a TFM?");
  458.   goto final_end;
  459.   }
  460. @d pabortpp(a, b, c) { fprintf(output, "%s\n", a, b, c);
  461.   fprintf(output, "%s\n", "Sorry, but I can't go on; are you sure this is a TFM?");
  462.   goto final_end;
  463.   }
  464.  
  465. @<Read the whole input file@>=
  466. fread(&tfm(0), sizeof(tfm(0)), 1, tfm_file);
  467. if (tfm(0) > 127) pabort("The first byte of the input file exceeds 127!");
  468. @.The first byte...@>
  469. if (feof(tfm_file)) pabort("The input file is only one byte long!");
  470. @.The input...one byte long@>
  471. fread(&tfm(1), sizeof(tfm(1)), 1, tfm_file); lf = tfm(0)*0400+tfm(1);
  472. if (lf == 0)
  473.   pabort("The file claims to have length zero, but that's impossible!");
  474. @.The file claims...@>
  475. if (4*lf-1 > tfm_size) pabort("The file is bigger than I can handle!");
  476. @.The file is bigger...@>
  477. for (tfm_ptr=2; tfm_ptr<4*lf; tfm_ptr++)
  478.   { if (feof(tfm_file))
  479.     pabort("The file has fewer bytes than it claims!");
  480. @.The file has fewer bytes...@>
  481.   fread(&tfm(tfm_ptr), sizeof(tfm(tfm_ptr)), 1, tfm_file);
  482.   }
  483. getc(tfm_file);
  484. if (!feof(tfm_file))
  485.   { fprintf(output, "%s\n", "There's some extra junk at the end of the TFM file,");
  486. @.There's some extra junk...@>
  487.   fprintf(output, "%s\n", "but I'll proceed as if it weren't there.");
  488.   }
  489.  
  490. @ After the file has been read successfully, we look at the subfile sizes
  491. to see if they check out.
  492.  
  493. @d eval_two_bytes(a) { if (tfm(tfm_ptr) > 127)
  494.     pabort("One of the subfile sizes is negative!");
  495. @.One of the subfile sizes...@>
  496.   a = tfm(tfm_ptr)*0400+tfm(tfm_ptr+1);
  497.   tfm_ptr += 2;
  498.   }
  499.  
  500. @<Set subfile sizes |lh|, |bc|, \dots, |np|@>=
  501. { tfm_ptr = 2;@/
  502. eval_two_bytes(lh);
  503. eval_two_bytes(bc);
  504. eval_two_bytes(ec);
  505. eval_two_bytes(nw);
  506. eval_two_bytes(nh);
  507. eval_two_bytes(nd);
  508. eval_two_bytes(ni);
  509. eval_two_bytes(nl);
  510. eval_two_bytes(nk);
  511. eval_two_bytes(ne);
  512. eval_two_bytes(np);
  513. if (lh < 2) pabortp("The header length is only %ld!", (long)lh);
  514. @.The header length...@>
  515. if (nl > 4*lig_size)
  516.   pabort("The lig/kern program is longer than I can handle!");
  517. @.The lig/kern program...@>
  518. if ((bc > ec+1) || (ec > 255)) pabortpp("The character code range %ld..%ld is illegal!", (long)bc, (long)ec);
  519. @.The character code range...@>
  520. if ((nw == 0) || (nh == 0) || (nd == 0) || (ni == 0))
  521.   pabort("Incomplete subfiles for character dimensions!");
  522. @.Incomplete subfiles...@>
  523. if (ne > 256) pabortp("There are %ld extensible recipes!", (long)ne);
  524. @.There are ... recipes@>
  525. if (lf != 6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np)
  526.   pabort("Subfile sizes don't add up to the stated total!");
  527. @.Subfile sizes don't add up...@>
  528. }
  529.  
  530. @ Once the input data successfully passes these basic checks,
  531. \.{TFtoPL} believes that it is a \.{TFM} file, and the conversion
  532. to \.{PL} format will take place. Access to the various subfiles
  533. is facilitated by computing the following base addresses. For example,
  534. the |char_info| for character |c| will start in location
  535. |4*(char_base+c)| of the |tfm| array.
  536.  
  537. @<Globals...@>=
  538. LONG @!char_base,@!width_base,@!height_base,@!depth_base,@!italic_base,
  539. @!lig_kern_base,@!kern_base,@!exten_base,@!param_base;
  540.   /* base addresses for the subfiles */
  541.  
  542. @ @<Compute the base addresses@>=
  543. { char_base = 6+lh-bc;
  544. width_base = char_base+ec+1;
  545. height_base = width_base+nw;
  546. depth_base = height_base+nh;
  547. italic_base = depth_base+nd;
  548. lig_kern_base = italic_base+ni;
  549. kern_base = lig_kern_base+nl;
  550. exten_base = kern_base+nk;
  551. param_base = exten_base+ne-1;
  552. }
  553.  
  554. @ Of course we want to define macros that suppress the detail of how the
  555. font information is actually encoded. Each word will be referred to by
  556. the |tfm| index of its first byte. For example, if |c| is a character
  557. code between |bc| and |ec|, then |tfm[char_info(c)]| will be the
  558. first byte of its |char_info|, i.e., the |width_index|; furthermore
  559. |width(c)| will point to the |fix_word| for |c|'s width.
  560.  
  561. @d check_sum 24
  562. @d design_size check_sum+4
  563. @d scheme design_size+4
  564. @d family scheme+40
  565. @d random_word family+20
  566. @d char_info(a) 4*(char_base+a)
  567. @d width_index(a) tfm(char_info(a))
  568. @d nonexistent(a) ((a < bc) || (a > ec) || (width_index(a) == 0))
  569. @d height_index(a) (tfm(char_info(a)+1) / 16)
  570. @d depth_index(a) (tfm(char_info(a)+1) % 16)
  571. @d italic_index(a) (tfm(char_info(a)+2) / 4)
  572. @d tag(a) (tfm(char_info(a)+2) % 4)
  573. @d reset_tag(a) tfm(char_info(a)+2) = 4*italic_index(a)+no_tag
  574. @d remainder(a) tfm(char_info(a)+3)
  575. @d width(a) 4*(width_base+width_index(a))
  576. @d height(a) 4*(height_base+height_index(a))
  577. @d depth(a) 4*(depth_base+depth_index(a))
  578. @d italic(a) 4*(italic_base+italic_index(a))
  579. @d exten(a) 4*(exten_base+remainder(a))
  580. @d lig_step(a) 4*(lig_kern_base+(a))
  581. @d kern(a) 4*(kern_base+a) /* here \#\ is an index, not a character */
  582. @d param(a) 4*(param_base+a) /* likewise */
  583.  
  584. @ One of the things we would like to do is take cognizance of fonts whose
  585. character coding scheme is \.{TeX math symbols} or \.{TeX math extension};
  586. we will set the |font_type| variable to one of the three choices
  587. |vanilla|, |mathsy|, or |mathex|.
  588.  
  589. @d vanilla 0 /* not a special scheme */
  590. @d mathsy 1 /* \.{TeX math symbols} scheme */
  591. @d mathex 2 /* \.{TeX math extension} scheme */
  592.  
  593. @<Glob...@>=
  594. UBYTE @!font_type; /* is this font special? */
  595.  
  596. @* Basic output subroutines.
  597. Let us now define some procedures that will reduce the rest of \.{TFtoPL}'s
  598. work to a triviality.
  599.  
  600. First of all, it is convenient to have an abbreviation for output to the
  601. \.{PL} file:
  602.  
  603. @d out(a) fprintf(pl_file, a)
  604. @d outc(a) putc(a, pl_file)
  605. @d outl(a) fprintf(pl_file, "%ld", (long)(a))
  606.  
  607. @ In order to stick to standard \PASCAL, we use three strings called
  608. |ASCII_04|, |ASCII_10|, and |ASCII_14|, in terms of which we can do the
  609. appropriate conversion of ASCII codes. Three other little strings are
  610. used to produce |face| codes like \.{MIE}.
  611.  
  612. @<Glob...@>=
  613. char @!ASCII_04[33],
  614.      @!ASCII_10[33],
  615.      @!ASCII_14[33];
  616.   /* strings for output in the user's external character set */
  617. char @!MBL_string[4],
  618.      @!RI_string[4],
  619.      @!RCE_string[4];
  620.   /* handy string constants for |face| codes */
  621.  
  622. @ @<Set init...@>=
  623. strcpy(ASCII_04+1, " !\"#$%&'()*+,-./0123456789:;<=>?");@/
  624. strcpy(ASCII_10+1, "â•ħABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_");@/
  625. strcpy(ASCII_14+1, "`abcdefghijklmnopqrstuvwxyz{|}~ ");@/
  626. strcpy(MBL_string+1, "MBL"); strcpy(RI_string+1, "RI ");
  627. strcpy(RCE_string+1, "RCE");
  628.  
  629. @ The array |dig| will hold a sequence of digits to be output.
  630.  
  631. @<Glob...@>=
  632. UBYTE @!dig[12];
  633.  
  634. @ Here, in fact, are two procedures that output |dig[j-1]|$\,\ldots\,$|dig[0]|,
  635. given $j>0$.
  636.  
  637. @c void out_digs(LONG j) /* outputs |j| digits */
  638. { do { decr(j); outc('0'+dig[j]);
  639.      }
  640.   while(j != 0);
  641. }
  642. @#
  643. void print_digs(LONG j) /* prints |j| digits */
  644. { do { decr(j); putc('0'+dig[j], output);
  645.      }
  646.   while(j != 0);
  647. }
  648.  
  649. @ The |print_octal| procedure indicates how |print_digs| can be used.
  650. Since this procedure is used only to print character codes, it always
  651. produces three digits.
  652.  
  653. @c void print_octal(byte c) /* prints octal value of |c| */
  654. {
  655. UBYTE j; /* index into |dig| */
  656. @#
  657. putc('\'', output); /* an apostrophe indicates the octal notation */
  658. for (j=0; j<=2; j++)
  659.   { dig[j] = c % 8; c = c / 8;
  660.   }
  661. print_digs(3);
  662. }
  663.  
  664. @ A \.{PL} file has nested parentheses, and we want to format the output
  665. so that its structure is clear. The |level| variable keeps track of the
  666. depth of nesting.
  667.  
  668. @<Glob...@>=
  669. UWORD @!level;
  670.  
  671. @ @<Set init...@>=
  672. level = 0;
  673.  
  674. @ Three simple procedures suffice to produce the desired structure in the
  675. output.
  676.  
  677. @c void out_ln(void) /* finishes one line, indents the next */
  678. {
  679. UBYTE l;
  680. @#
  681. putc('\n', pl_file);
  682. for (l=1; l<=level; l++)  out("   ");
  683. }
  684. @#
  685. void left(void) /* outputs a left parenthesis */
  686. { incr(level); outc('(');
  687. }
  688. @#
  689. void right(void) /* outputs a right parenthesis and finishes a line */
  690. { decr(level); outc(')'); out_ln();
  691. }
  692.  
  693. @ The value associated with a property can be output in a variety of
  694. ways. For example, we might want to output a {\mc BCPL} string that
  695. begins in |tfm[k]|:
  696.  
  697. @c void out_BCPL(index @!k) /* outputs a string, preceded by a blank space */
  698. {
  699. UBYTE l; /* the number of bytes remaining */
  700. @#
  701. outc(' '); l = tfm(k);
  702. while (l > 0) {
  703.   incr(k); decr(l);
  704.   switch (tfm(k)/040) {
  705.     case 1: outc(ASCII_04[1+(tfm(k)%040)]); break;
  706.     case 2: outc(ASCII_10[1+(tfm(k)%040)]); break;
  707.     case 3: outc(ASCII_14[1+(tfm(k)%040)]); break;
  708.     }
  709.   }
  710. }
  711.  
  712. @ The property value might also be a sequence of |l| bytes, beginning
  713. in |tfm[k]|, that we would like to output in octal notation.
  714. The following procedure assumes that |l<=4|, but larger values of |l|
  715. could be handled easily by enlarging the |dig| array and increasing
  716. the upper bounds on |b| and |j|.
  717.  
  718. @c void out_octal(index @!k, index @!l) /* outputs |l| bytes in octal */
  719. {
  720. UWORD a; /* accumulator for bits not yet output */
  721. UBYTE @!b; /* the number of significant bits in |a| */
  722. UBYTE @!j; /* the number of digits of output */
  723. @#
  724. out(" O "); /* specify octal format */
  725. a = 0; b = 0; j = 0;
  726. while( l > 0 ) @<Reduce \(1)|l| by one, preserving the invariants@>;
  727. while( (a > 0) || (j == 0) )
  728.   { dig[j] = a % 8; a = a / 8; incr(j);
  729.   }
  730. out_digs(j);
  731. }
  732.  
  733. @ @<Reduce \(1)|l|...@>=
  734. { decr(l);
  735. if (tfm(k+l) != 0)
  736.   { while( b > 2 )
  737.     { dig[j] = a % 8; a = a / 8; b = b-3; incr(j);
  738.     }
  739.   switch (b) {
  740.     case 0: a = tfm(k+l); break;
  741.     case 1: a = a+2*tfm(k+l); break;
  742.     case 2: a = a+4*tfm(k+l); break;
  743.     }
  744.   }
  745. b = b+8;
  746. }
  747.  
  748. @ The property value may be a character, which is output in octal
  749. unless it is a letter or a digit. This procedure is the only place
  750. where a lowercase letter will be output to the \.{PL} file.
  751. @^system dependencies@>
  752.  
  753. @c void out_char(byte @!c) /* outputs a character */
  754. { if (font_type > vanilla)
  755.   { tfm(0) = c; out_octal(0, 1);
  756.   }
  757. else if ((c >= '0') && (c <= '9')) {
  758.   out(" C "); outc(c); }
  759. else if ((c >= 'A') && (c <= 'Z')) {
  760.   out(" C "); outc(ASCII_10[c-'A'+2]); }
  761. else if ((c >= 'a') && (c <= 'z')) {
  762.   out(" C "); outc(ASCII_14[c-'a'+2]); }
  763. else  { tfm(0) = c; out_octal(0, 1);
  764.   }
  765. }
  766.  
  767. @ The property value might be a ``face'' byte, which is output in the
  768. curious code mentioned earlier, provided that it is less than 18.
  769.  
  770. @c void out_face(index @!k) /* outputs a |face| */
  771. {
  772. UBYTE s; /* the slope */
  773. UBYTE @!b; /* the weight and expansion */
  774. @#
  775. if (tfm(k) >= 18) out_octal(k,1);
  776. else  { out(" F ");  /* specify face-code format */
  777.   s = tfm(k) % 2; b = tfm(k) / 2;
  778.   outc(MBL_string[1+(b % 3)]);
  779.   outc(RI_string[1+s]);
  780.   outc(RCE_string[1+(b / 3)]);
  781.   }
  782. }
  783.  
  784. @ And finally, the value might be a |fix_word|, which is output in
  785. decimal notation with just enough decimal places for \.{PLtoTF}
  786. to recover every bit of the given |fix_word|.
  787.  
  788. All of the numbers involved in the intermediate calculations of
  789. this procedure will be nonnegative and less than $10\cdot2^{24}$.
  790.  
  791. @c void out_fix(index @!k) /* outputs a |fix_word| */
  792. {
  793. UWORD a; /* accumulator for the integer part */
  794. LONG @!f; /* accumulator for the fraction part */
  795. UBYTE @!j; /* index into |dig| */
  796. LONG @!delta; /* amount if allowable inaccuracy */
  797. @#
  798. out(" R "); /* specify real format */
  799. a = (tfm(k)*16)+(tfm(k+1) / 16);
  800. f = ((long)(tfm(k+1) % 16)*0400+tfm(k+2))*0400+tfm(k+3);
  801. if (a > 03777) @<Reduce \(2)negative to positive@>;
  802. @<Output the integer part, |a|, in decimal notation@>;
  803. @<Output the fraction part, $|f|/2^{20}$, in decimal notation@>;
  804. }
  805.  
  806. @ The following code outputs at least one digit even if |a=0|.
  807.  
  808. @<Output the integer...@>=
  809. { j = 0;
  810. do { dig[j] = a % 10; a = a / 10; incr(j);
  811.    }
  812. while(a != 0);
  813. out_digs(j);
  814. }
  815.  
  816. @ And the following code outputs at least one digit to the right
  817. of the decimal point.
  818.  
  819. @<Output the fraction...@>=
  820. { outc('.'); f = 10*f+5; delta = 10;
  821. do { if (delta > 04000000L) f = f+02000000L-(delta / 2);
  822.   outl(f / 04000000L); f = 10*(f % 04000000L); delta = delta*10;
  823.   }
  824. while(f > delta);
  825. }
  826.  
  827. @ @<Reduce \(2)negative to positive@>=
  828. { outc('-'); a = 010000-a;
  829. if (f > 0)
  830.   { f = 04000000L-f; decr(a);
  831.   }
  832. }
  833.  
  834. @* Doing it.
  835. \TeX\ checks the information of a \.{TFM} file for validity as the
  836. file is being read in, so that no further checks will be needed when
  837. typesetting is going on. And when it finds something wrong, it justs
  838. calls the file ``bad,'' without identifying the nature of the problem,
  839. since \.{TFM} files are supposed to be good almost all of the time.
  840.  
  841. Of course, a bad file shows up every now and again, and that's where
  842. \.{TFtoPL} comes in. This program wants to catch at least as many errors as
  843. \TeX\ does, and to give informative error messages besides.
  844. All of the errors are corrected, so that the \.{PL} output will
  845. be correct (unless, of course, the \.{TFM} file was so loused up
  846. that no attempt is being made to fathom it).
  847.  
  848. @ Just before each character is processed, its code is printed in octal
  849. notation. Up to eight such codes appear on a line; so we have a variable
  850. to keep track of how many are currently there. We also keep track of
  851. whether or not any errors have had to be corrected.
  852.  
  853. @<Glob...@>=
  854. UBYTE @!chars_on_line; /* the number of characters printed on the current line */
  855. boolean @!perfect; /* was the file free of errors? */
  856.  
  857. @ @<Set init...@>=
  858. chars_on_line = 0;@/
  859. perfect = true; /* innocent until proved guilty */
  860.  
  861. @ Error messages are given with the help of the |bad| and |range_error|
  862. and |bad_char| macros:
  863.  
  864. @d bad(a) { perfect = false; if (chars_on_line > 0) fprintf(output, " \n");
  865.   chars_on_line = 0; fprintf(output, "Bad TFM file: %s", a);
  866.   }
  867. @d badp(a, b) { perfect = false; if (chars_on_line > 0) fprintf(output, " \n");
  868.   chars_on_line = 0; fprintf(output, "Bad TFM file: "); fprintf(output, a, b);
  869.   }
  870. @d badpp(a, b, c) { perfect = false; if (chars_on_line > 0) fprintf(output, " \n");
  871.   chars_on_line = 0; fprintf(output, "Bad TFM file: "); fprintf(output, a, b, c);
  872.   }
  873. @.Bad TFM file@>
  874. @d range_error(a) { perfect = false;
  875.   fprintf(output, " \n%s index for character ", a);
  876.   print_octal(c); fprintf(output, "%s\n", " is too large;");
  877.   fprintf(output, "%s\n", "so I reset it to zero.");
  878.   }
  879. @d bad_char(a, b) { perfect = false; if (chars_on_line > 0) fprintf(output, "%s\n", " ");
  880.   chars_on_line = 0; fprintf(output, "Bad TFM file: %ld nonexistent character ", (long)(a));
  881.   print_octal(b); fprintf(output, ".\n");
  882.   }
  883. @d correct_bad_char(a, b)  { perfect = false;
  884.   if (chars_on_line > 0) fprintf(output, "%s\n", " ");
  885.   chars_on_line = 0; fprintf(output, "Bad TFM file: %ld nonexistent character ", (long)a);
  886.   print_octal(tfm(b)); fprintf(output, ".\n"); tfm(b) = bc;
  887.   }
  888.  
  889. @<Glob...@>=
  890. UWORD @!i; /* an index to words of a subfile */
  891. UWORD @!c; /* a random character */
  892. UBYTE @!d; /* byte number in a word */
  893. index @!k; /* a random index */
  894. UWORD @!r; /* a random two-byte value */
  895. UBYTE @!count; /* for when we need to enumerate a small set */
  896.  
  897. @ There are a lot of simple things to do, and they have to be done one
  898. at a time, so we might as well get down to business.  The first things
  899. that \.{TFtoPL} will put into the \.{PL} file appear in the header part.
  900.  
  901. @<Do the header@>=
  902. { font_type = vanilla;
  903. if (lh >= 12)
  904.   { @<Set the true |font_type|@>;
  905.   if (lh >= 17)
  906.     { @<Output the family name@>;
  907.     if (lh >= 18) @<Output the rest of the header@>;
  908.     }
  909.   @<Output the character coding scheme@>;
  910.   }
  911. @<Output the design size@>;
  912. @<Output the check sum@>;
  913. @<Output the |seven_bit_safe_flag|@>;
  914. }
  915.  
  916. @ @<Output the check sum@>=
  917. left(); out("CHECKSUM"); out_octal(check_sum,4);
  918. right()
  919.  
  920. @ Incorrect design sizes are changed to 10 points.
  921.  
  922. @d bad_design(a) { bad("Design size "a"!");
  923. @.Design size wrong@>
  924.   fprintf(output, "%s\n", "I've set it to 10 points.");
  925.   out(" D 10");
  926.   }
  927.  
  928. @ @<Output the design size@>=
  929. left(); out("DESIGNSIZE");
  930. if (tfm(design_size) > 127) bad_design("negative")
  931. else if ((tfm(design_size) == 0) && (tfm(design_size+1) < 16))
  932.   bad_design("too small")
  933. else out_fix(design_size);
  934. right();
  935. out("(COMMENT DESIGNSIZE IS IN POINTS)"); out_ln();
  936. out("(COMMENT OTHER SIZES ARE MULTIPLES OF DESIGNSIZE)"); out_ln()
  937. @.DESIGNSIZE IS IN POINTS@>
  938.  
  939. @ Since we have to check two different {\mc BCPL} strings for validity,
  940. we might as well write a subroutine to make the check.
  941.  
  942. @c void check_BCPL(index @!k, index @!l) /* checks a string of length |<l| */
  943. {
  944. index j; /* runs through the string */
  945. byte @!c; /* character being checked */
  946. @#
  947. if (tfm(k) >= l)
  948.   { bad("String is too long; I've shortened it drastically.");
  949. @.String is too long...@>
  950.   tfm(k) = 1;
  951.   }
  952. for (j=k+1; j<=k+tfm(k); j++)
  953.   { c = tfm(j);
  954.   if ((c == '(') || (c == ')'))
  955.     { bad("Parenthesis in string has been changed to slash.");
  956. @.Parenthesis...changed to slash@>
  957.     tfm(j) = '/';
  958.     }
  959.   else if ((c < ' ') || (c > '~'))
  960.     { bad("Nonstandard ASCII code has been blotted out.");
  961. @.Nonstandard ASCII code...@>
  962.     tfm(j) = '?';
  963.     }
  964.   else if ((c >= 'a') && (c <= 'z')) tfm(j) = c+'A'-'a'; /* upper-casify letters */
  965.   }
  966. }
  967.  
  968. @ The |font_type| starts out |vanilla|; possibly we need to reset it.
  969.  
  970. @<Set the true |font_type|@>=
  971. { check_BCPL(scheme,40);
  972. if ((tfm(scheme) >= 11) && @|(tfm(scheme+1) == 'T') && @|
  973. (tfm(scheme+2) == 'E') && @|(tfm(scheme+3) == 'X') && @|
  974. (tfm(scheme+4) == ' ') && @|(tfm(scheme+5) == 'M') && @|
  975. (tfm(scheme+6) == 'A') && @|(tfm(scheme+7) == 'T') && @|
  976. (tfm(scheme+8) == 'H') && @|(tfm(scheme+9) == ' '))
  977.   { if ((tfm(scheme+10) == 'S') && (tfm(scheme+11) == 'Y')) font_type = mathsy;
  978.   else if ((tfm(scheme+10) == 'E') && (tfm(scheme+11) == 'X')) font_type = mathex;
  979.   }
  980. }
  981.  
  982. @ @<Output the character coding scheme@>=
  983. left(); out("CODINGSCHEME");
  984. out_BCPL(scheme);
  985. right()
  986.  
  987. @ @<Output the family name@>=
  988. left(); out("FAMILY");
  989. check_BCPL(family,20);
  990. out_BCPL(family);
  991. right()
  992.  
  993. @ @<Output the rest of the header@>=
  994. { left(); out("FACE"); out_face(random_word+3); right();
  995. for (i=18; i<lh; i++)
  996.   { left(); out("HEADER D "); outl(i);
  997.   out_octal(check_sum+4*i,@,4); right();
  998.   }
  999. }
  1000.  
  1001. @ This program does not check to see if the |seven_bit_safe_flag| has the
  1002. correct setting, i.e., if it really reflects the seven-bit-safety of
  1003. the \.{TFM} file; the stated value is merely put into the \.{PL} file.
  1004. The \.{PLtoTF} program will store a correct value and give a warning
  1005. message if a file falsely claims to be safe.
  1006.  
  1007. @<Output the |seven_bit_safe_flag|@>=
  1008. if ((lh > 17) && (tfm(random_word) > 127))
  1009.   { left(); out("SEVENBITSAFEFLAG TRUE"); right();
  1010.   }
  1011.  
  1012. @ The next thing to take care of is the list of parameters.
  1013.  
  1014. @<Do the parameters@>=
  1015. if (np > 0)
  1016.   { left(); out("FONTDIMEN"); out_ln();
  1017.   for (i=1; i<=np; i++)  @<Check and output the $i$th parameter@>;
  1018.   right();
  1019.   }
  1020. @<Check to see if |np| is complete for this font type@>;
  1021.  
  1022. @ @<Check to see if |np|...@>=
  1023. if ((font_type == mathsy) && (np != 22))
  1024.   fprintf(output, "Unusual number of fontdimen parameters for a math symbols font (%ld not 22).", (long)np);
  1025. @.Unusual number of fontdimen...@>
  1026. else if ((font_type == mathex) && (np != 13))
  1027.   fprintf(output, "Unusual number of fontdimen parameters for an extension font (%ld not 13).", (long)np);
  1028.  
  1029. @ All |fix_word| values except the design size and the first parameter
  1030. will be checked to make sure that they are less than 16.0 in magnitude,
  1031. using the |check_fix| macro:
  1032.  
  1033. @d check_fix(a, b) if ((tfm(a) > 0) && (tfm(a) < 255))
  1034.   { tfm(a) = 0; tfm((a)+1) = 0; tfm((a)+2) = 0; tfm((a)+3) = 0;
  1035.   badpp("%s %ld is too big;", b ,(long)i);
  1036.   fprintf(output, "%s\n", "I have set it to zero.");
  1037.   }
  1038.  
  1039. @<Check and output the $i$th parameter@>=
  1040. { left();
  1041. if (i == 1) out("SLANT"); /* this parameter is not checked */
  1042. else  { check_fix(param(i), "Parameter");@/
  1043. @.Parameter n is too big@>
  1044.   @<Output the name of parameter $i$@>;
  1045.   }
  1046. out_fix((index)(param(i))); right();
  1047. }
  1048.  
  1049. @ @<Output the name...@>=
  1050. if (i <= 7) switch (i) {
  1051.   case 2: out("SPACE"); break;
  1052.   case 3: out("STRETCH"); break;
  1053.   case 4: out("SHRINK"); break;
  1054.   case 5: out("XHEIGHT"); break;
  1055.   case 6: out("QUAD"); break;
  1056.   case 7: out("EXTRASPACE");  break;
  1057.   }
  1058. else if ((i <= 22) && (font_type == mathsy)) switch (i) {
  1059.   case 8: out("NUM1"); break;
  1060.   case 9: out("NUM2"); break;
  1061.   case 10: out("NUM3"); break;
  1062.   case 11: out("DENOM1"); break;
  1063.   case 12: out("DENOM2"); break;
  1064.   case 13: out("SUP1"); break;
  1065.   case 14: out("SUP2"); break;
  1066.   case 15: out("SUP3"); break;
  1067.   case 16: out("SUB1"); break;
  1068.   case 17: out("SUB2"); break;
  1069.   case 18: out("SUPDROP"); break;
  1070.   case 19: out("SUBDROP"); break;
  1071.   case 20: out("DELIM1"); break;
  1072.   case 21: out("DELIM2"); break;
  1073.   case 22: out("AXISHEIGHT"); break;
  1074.   }
  1075. else if ((i <= 13) && (font_type == mathex))
  1076.   if (i == 8) out("DEFAULTRULETHICKNESS");
  1077.   else { out("BIGOPSPACING"); outl(i-8);}
  1078. else {out("PARAMETER D "); outl(i);}
  1079.  
  1080. @ We need to check the range of all the remaining |fix_word| values,
  1081. and to make sure that |width[0]=0|, etc.
  1082.  
  1083. @d nonzero_fix(a) (tfm(a) > 0) || (tfm(a+1) > 0) || (tfm(a+2) > 0) || (tfm(a+3) > 0)
  1084.  
  1085. @<Check the |fix_word| entries@>=
  1086. if (nonzero_fix(4*width_base)) bad("width[0] should be zero.");
  1087. @.should be zero@>
  1088. if (nonzero_fix(4*height_base)) bad("height[0] should be zero.");
  1089. if (nonzero_fix(4*depth_base)) bad("depth[0] should be zero.");
  1090. if (nonzero_fix(4*italic_base)) bad("italic[0] should be zero.");
  1091. for (i=0; i<nw; i++)  check_fix(4*(width_base+i), "Width");
  1092. @.Width n is too big@>
  1093. for (i=0; i<nh; i++) check_fix(4*(height_base+i), "Height");
  1094. @.Height n is too big@>
  1095. for (i=0; i<nd; i++) check_fix(4*(depth_base+i), "Depth");
  1096. @.Depth n is too big@>
  1097. for (i=0; i<ni; i++) check_fix(4*(italic_base+i), "Italic correction");
  1098. @.Italic correction n is too big@>
  1099. if (nk > 0) for (i=0; i<nk; i++) check_fix(kern(i), "Kern");
  1100. @.Kern n is too big@>
  1101.  
  1102. @ The ligaturefdivkerning program comes next. Before we can put it out in
  1103. \.{PL} format, we need to make a table of ``labels'' that will be inserted
  1104. into the program. For each character |c| whose |tag| is |lig_tag| and
  1105. whose starting address is |r|, we will store the pair |(c,r)| in the
  1106. |label_table| array. If there's a boundary-char program starting at~|r|,
  1107. we also store the pair |(256,r)|.
  1108. This array is sorted by its second components, using the
  1109. simple method of straight insertion.
  1110.  
  1111. @<Glob...@>=
  1112. struct {
  1113.    UWORD @!cc;
  1114.    lig_size_type @!rr;
  1115.    } @!label_table[259];
  1116. UWORD @!label_ptr; /* the largest entry in |label_table| */
  1117. UWORD @!sort_ptr; /* index into |label_table| */
  1118. UWORD @!boundary_char; /* boundary character, or 256 if none */
  1119. UWORD @!bchar_label; /* beginning of boundary character program */
  1120.  
  1121. @ @<Set init...@>=
  1122. boundary_char = 256; bchar_label = (UWORD)(077777L);@/
  1123. label_ptr = 0; label_table[0].rr = 0; /* a sentinel appears at the bottom */
  1124.  
  1125. @ We'll also identify and remove inaccessible program steps, using the
  1126. |activity| array.
  1127.  
  1128. @d unreachable 0 /* a program step not known to be reachable */
  1129. @d pass_through 1 /* a program step passed through on initialization */
  1130. @d accessible 2 /* a program step that can be relevant */
  1131.  
  1132. @<Glob...@>=
  1133. UBYTE @!activity[lig_size+1];
  1134. lig_size_type @!ai, @!acti; /* indices into |activity| */
  1135.  
  1136. @ @<Do the ligatures and kerns@>=
  1137. if (nl > 0)
  1138.   { for (ai=0; ai<nl; ai++) activity[ai] = unreachable;
  1139.   @<Check for a boundary char@>;
  1140.   }
  1141. @<Build the label table@>;
  1142. if (nl > 0)
  1143.   { left(); out("LIGTABLE"); out_ln();@/
  1144.   @<Compute the |activity| array@>;
  1145.   @<Output and correct the ligature/kern program@>;
  1146.   right();
  1147.   @<Check for ligature cycles@>;
  1148.   }
  1149.  
  1150. @ We build the label table even when |nl=0|, because this catches errors
  1151. that would not otherwise be detected.
  1152.  
  1153. @<Build...@>=
  1154. for (c=bc; c<=ec; c++) if (tag(c) == lig_tag)
  1155.   { r = remainder(c);
  1156.   if (r < nl)
  1157.     { if (tfm(lig_step(r)) > stop_flag)
  1158.       { r = 256*tfm(lig_step(r)+2)+tfm(lig_step(r)+3);
  1159.       if (r < nl) if (activity[remainder(c)] == unreachable)
  1160.         activity[remainder(c)] = pass_through;
  1161.       }
  1162.     }
  1163.   if (r >= nl)
  1164.     { perfect = false; fprintf(output, "%s\n", " ");
  1165.     fprintf(output, "Ligature/kern starting index for character "); print_octal(c);
  1166.     fprintf(output, "%s\n", " is too large;"); fprintf(output, "%s\n", "so I removed it."); reset_tag(c);
  1167. @.Ligature/kern starting index...@>
  1168.     }
  1169.   else @<Insert |(c,r)| into |label_table|@>;
  1170.   }
  1171. label_table[label_ptr+1].rr = lig_size; /* put ``infinite'' sentinel at the end */
  1172.  
  1173. @ @<Insert |(c,r)|...@>=
  1174. { sort_ptr = label_ptr; /* there's a hole at position |sort_ptr+1| */
  1175. while( label_table[sort_ptr].rr > r )
  1176.   { label_table[sort_ptr+1] = label_table[sort_ptr];
  1177.   decr(sort_ptr); /* move the hole */
  1178.   }
  1179. label_table[sort_ptr+1].cc = c;
  1180. label_table[sort_ptr+1].rr = r; /* fill the hole */
  1181. incr(label_ptr); activity[r] = accessible;
  1182. }
  1183.  
  1184. @ @<Check for a bound...@>=
  1185. if (tfm(lig_step(0)) == 255)
  1186.   { left(); out("BOUNDARYCHAR");
  1187.   boundary_char = tfm(lig_step(0)+1); out_char(boundary_char); right();
  1188.   activity[0] = pass_through;
  1189.   }
  1190. if (tfm(lig_step(nl-1)) == 255)
  1191.   { r = 256*tfm(lig_step(nl-1)+2)+tfm(lig_step(nl-1)+3);
  1192.   if (r >= nl)
  1193.     { perfect = false; fprintf(output, "%s\n", " ");
  1194.     fprintf(output, "Ligature/kern starting index for boundarychar is too large;");
  1195.     fprintf(output, "%s\n", "so I removed it.");
  1196. @.Ligature/kern starting index...@>
  1197.     }
  1198.   else { label_ptr = 1; label_table[1].cc = 256; label_table[1].rr = r;
  1199.     bchar_label = r; activity[r] = accessible;
  1200.     }
  1201.   activity[nl-1] = pass_through;
  1202.   }
  1203.  
  1204. @ @<Compute the |activity| array@>=
  1205. for (ai=0; ai<nl; ai++) if (activity[ai] == accessible)
  1206.   { r = tfm(lig_step(ai));
  1207.   if (r < stop_flag)
  1208.     { r = r+ai+1;
  1209.     if (r >= nl)
  1210.       { badp("Ligature/kern step %ld skips too far;", (long)ai);
  1211. @.Lig...skips too far@>
  1212.       fprintf(output, "%s\n", "I made it stop."); tfm(lig_step(ai)) = stop_flag;
  1213.       }
  1214.     else activity[r] = accessible;
  1215.     }
  1216.   }
  1217.  
  1218. @ We ignore |pass_through| items, which don't need to be mentioned in
  1219. the \.{PL} file.
  1220.  
  1221. @<Output and correct the ligature...@>=
  1222. sort_ptr = 1; /* point to the next label that will be needed */
  1223. for (acti=0; acti<nl; acti++) if (activity[acti] != pass_through)
  1224.   { i = acti; @<Take care of commenting out unreachable steps@>;
  1225.   @<Output any labels for step $i$@>;
  1226.   @<Output step $i$ of the ligature/kern program@>;
  1227.   }
  1228. if (level == 2) right() /* the final step was unreachable */
  1229.  
  1230. @ @<Output any labels...@>=
  1231. while( i == label_table[sort_ptr].rr )
  1232.   { left(); out("LABEL");
  1233.   if (label_table[sort_ptr].cc == 256) out(" BOUNDARYCHAR");
  1234.   else out_char(label_table[sort_ptr].cc);
  1235.   right(); incr(sort_ptr);
  1236.   }
  1237.  
  1238. @ @<Take care of commenting out...@>=
  1239. if (activity[i] == unreachable)
  1240.   { if (level == 1)
  1241.     { left(); out("COMMENT THIS PART OF THE PROGRAM IS NEVER USED!"); out_ln();
  1242.     }
  1243.   }
  1244. else if (level == 2) right()
  1245.  
  1246. @ @<Output step $i$...@>=
  1247. { k = (index)(lig_step(i));
  1248. if (tfm(k) > stop_flag)
  1249.   { if (256*tfm(k+2)+tfm(k+3) >= nl)
  1250.     bad("Ligature unconditional stop command address is too big.");
  1251. @.Ligature unconditional stop...@>
  1252.   }
  1253. else if (tfm(k+2) >= kern_flag) @<Output a kern step@>@;
  1254. else @<Output a ligature step@>;
  1255. if (tfm(k) > 0)
  1256.   if (level == 1) @<Output either \.{SKIP} or \.{STOP}@>;
  1257. }
  1258.  
  1259. @ The \.{SKIP} command is a bit tricky, because we will be omitting all
  1260. inaccessible commands.
  1261.  
  1262. @<Output either...@>=
  1263. { if (tfm(k) >= stop_flag) out("(STOP)");
  1264. else { count = 0;
  1265.   for (ai=i+1; ai<=i+tfm(k); ai++) if (activity[ai] == accessible) incr(count);
  1266.   out("(SKIP D "); outl(count); outc(')'); /* possibly $count=0$, so who cares */
  1267.   }
  1268. out_ln();
  1269. }
  1270.  
  1271. @ @<Output a kern step@>=
  1272. { if (nonexistent(tfm(k+1))) if (tfm(k+1) != boundary_char)
  1273.   correct_bad_char("Kern step for", k+1);
  1274. @.Kern step for nonexistent...@>
  1275. left(); out("KRN"); out_char(tfm(k+1));
  1276. r = 256*(tfm(k+2)-kern_flag)+tfm(k+3);
  1277. if (r >= nk)
  1278.   { bad("Kern index too large.");
  1279. @.Kern index too large@>
  1280.   out(" R 0.0");
  1281.   }
  1282. else out_fix((index)(kern(r)));
  1283. right();
  1284. }
  1285.  
  1286. @ @<Output a ligature step@>=
  1287. { if (nonexistent(tfm(k+1))) if (tfm(k+1) != boundary_char)
  1288.   correct_bad_char("Ligature step for", k+1);
  1289. @.Ligature step for nonexistent...@>
  1290. if (nonexistent(tfm(k+3)))
  1291.   correct_bad_char("Ligature step produces the", k+3);
  1292. @.Ligature step produces...@>
  1293. left(); r = tfm(k+2);
  1294. if ((r == 4) || ((r > 7) && (r != 11)))
  1295.   { fprintf(output, "%s\n", "Ligature step with nonstandard code changed to LIG");
  1296.   r = 0; tfm(k+2) = 0;
  1297.   }
  1298. if (r%4 > 1) outc('/');
  1299. out("LIG");
  1300. if (odd(r)) outc('/');
  1301. while( r > 3 )
  1302.   { outc('>'); r = r-4;
  1303.   }
  1304. out_char(tfm(k+1)); out_char(tfm(k+3)); right();
  1305. }
  1306.  
  1307. @ The last thing on \.{TFtoPL}'s agenda is to go through the
  1308. list of |char_info| and spew out the information about each individual
  1309. character.
  1310.  
  1311. @<Do the characters@>=
  1312. sort_ptr = 0; /* this will suppress `\.{STOP}' lines in ligature comments */
  1313. for (c=bc; c<=ec; c++) if (width_index(c) > 0)
  1314.   { if (chars_on_line == 8)
  1315.     { fprintf(output, "%s\n", " "); chars_on_line = 1;
  1316.     }
  1317.   else  { if (chars_on_line > 0) putc(' ', output);
  1318.     incr(chars_on_line);
  1319.     }
  1320.   print_octal(c); /* progress report */
  1321.   left(); out("CHARACTER"); out_char(c); out_ln();
  1322.   @<Output the character's width@>;
  1323.   if (height_index(c) > 0) @<Output the character's height@>;
  1324.   if (depth_index(c) > 0) @<Output the character's depth@>;
  1325.   if (italic_index(c) > 0) @<Output the italic correction@>;
  1326.   switch (tag(c)) {
  1327.     case no_tag: do_nothing; break;
  1328.     case lig_tag: @<Output the applicable part of the ligature/kern
  1329.       program as a comment@>; break;
  1330.     case list_tag: @<Output the character link unless there is a problem@>; break;
  1331.     case ext_tag: @<Output an extensible character recipe@>; break;
  1332.     }/* there are no other cases */
  1333.   right();
  1334.   }
  1335.  
  1336. @ @<Output the character's width@>=
  1337. { left(); out("CHARWD");
  1338. if (width_index(c) >= nw) range_error("Width")@;
  1339. else out_fix((index)(width(c)));
  1340. right();
  1341. }
  1342.  
  1343. @ @<Output the character's height@>=
  1344. if (height_index(c) >= nh) range_error("Height")@;
  1345. @.Height index for char...@>
  1346. else  { left(); out("CHARHT"); out_fix((index)(height(c))); right();
  1347.   }
  1348.  
  1349. @ @<Output the character's depth@>=
  1350. if (depth_index(c) >= nd) range_error("Depth")@;
  1351. @.Depth index for char@>
  1352. else  { left(); out("CHARDP"); out_fix((index)(depth(c))); right();
  1353.   }
  1354.  
  1355. @ @<Output the italic correction@>=
  1356. if (italic_index(c) >= ni) range_error("Italic correction")@;
  1357. @.Italic correction index for char...@>
  1358. else  { left(); out("CHARIC"); out_fix((index)(italic(c))); right();
  1359.   }
  1360.  
  1361. @ @<Output the applicable part of the ligature...@>=
  1362. { left(); out("COMMENT"); out_ln();@/
  1363. i = remainder(c);
  1364. r = (UWORD)(lig_step(i));
  1365. if (tfm(r) > stop_flag) i = 256*tfm(r+2)+tfm(r+3);
  1366. do { @<Output step...@>;
  1367. if (tfm(k) >= stop_flag) i = nl;
  1368. else i = i+1+tfm(k);
  1369. } while(i < nl);
  1370. right();
  1371. }
  1372.  
  1373. @ We want to make sure that there is no cycle of characters linked together
  1374. by |list_tag| entries, since such a cycle would get \TeX\ into an endless
  1375. loop. If such a cycle exists, the routine here detects it when processing
  1376. the largest character code in the cycle.
  1377.  
  1378. @<Output the character link unless there is a problem@>=
  1379. { r = remainder(c);
  1380. if (nonexistent(r))
  1381.   { bad_char("Character list link to", r); reset_tag(c);
  1382. @.Character list link...@>
  1383.   }
  1384. else  { while ((r < c) && (tag(r) == list_tag)) r = remainder(r);
  1385.   if (r == c)
  1386.     { bad("Cycle in a character list!");
  1387. @.Cycle in a character list@>
  1388.     fprintf(output, "Character "); print_octal(c);
  1389.     fprintf(output, "%s\n", " now ends the list.");
  1390.     reset_tag(c);
  1391.     }
  1392.   else  { left(); out("NEXTLARGER"); out_char(remainder(c));
  1393.     right();
  1394.     }
  1395.   }
  1396. }
  1397.  
  1398. @ @<Output an extensible character recipe@>=
  1399. if (remainder(c) >= ne)
  1400.   { range_error("Extensible"); reset_tag(c);
  1401. @.Extensible index for char@>
  1402.   }
  1403. else  { left(); out("VARCHAR"); out_ln();
  1404.   @<Output the extensible pieces that exist@>;
  1405.   right();
  1406.   }
  1407.  
  1408. @ @<Output the extensible pieces that...@>=
  1409. for (k=0; k<=3; k++) if ((k == 3) || (tfm(exten(c)+k) > 0))
  1410.   { left();
  1411.   switch (k) {
  1412.     case 0: out("TOP"); break;
  1413.     case 1: out("MID"); break;
  1414.     case 2: out("BOT"); break;
  1415.     case 3: out("REP"); break;
  1416.     }
  1417.   if (nonexistent(tfm(exten(c)+k))) out_char(c);
  1418.   else out_char(tfm(exten(c)+k));
  1419.   right();
  1420.   }
  1421.  
  1422. @ Some of the extensible recipes may not actually be used, but \TeX\ will
  1423. complain about them anyway if they refer to nonexistent characters.
  1424. Therefore \.{TFtoPL} must check them too.
  1425.  
  1426. @<Check the extensible recipes@>=
  1427. if (ne > 0) for (c=0; c<ne; c++) for (d=0; d<=3; d++)
  1428.   { k = (index)(4*(exten_base+c)+d);
  1429.   if ((tfm(k) > 0) || (d == 3))
  1430.     { if (nonexistent(tfm(k)))
  1431.       { bad_char("Extensible recipe involves the", tfm(k));
  1432. @.Extensible recipe involves...@>
  1433.       if (d < 3) tfm(k) = 0;
  1434.       }
  1435.     }
  1436.   }
  1437.  
  1438. @* Checking for ligature loops.
  1439. We have programmed almost everything but the most interesting calculation of
  1440. all, which has been saved for last as a special treat. \TeX's extended ligature
  1441. mechanism allows unwary users to specify sequences of ligature replacements
  1442. that never terminate. For example, the pair of commands
  1443. $$\.{(fdivLIG $x$ $y$) (fdivLIG $y$ $x$)}$$
  1444. alternately replaces character $x$ by character $y$ and vice versa. A similar
  1445. loop occurs if \.{(LIGfdiv $z$ $y$)} occurs in the program for $x$ and
  1446.  \.{(LIGfdiv $z$ $x$)} occurs in the program for $y$.
  1447.  
  1448. More complicated loops are also possible. For example, suppose the ligature
  1449. programs for $x$ and $y$ are
  1450. $$\vcenter{\halign{#\hfil\cr
  1451. \.{(LABEL $x$)(fdivLIGfdiv $z$ $w$)(fdivLIGfdiv> $w$ $y$)} \dots,\cr
  1452. \.{(LABEL $y$)(LIG $w$ $x$)} \dots;\cr}}$$
  1453. then the adjacent characters $xz$ change to $xwz$, $xywz$, $xxz$, $xxwz$,
  1454. \dots, ad infinitum.
  1455.  
  1456. @ To detect such loops, \.{TFtoPL} attempts to evaluate the function
  1457. $f(x,y)$ for all character pairs $x$ and~$y$, where $f$ is defined as
  1458. follows: If the current character is $x$ and the next character is
  1459. $y$, we say the ``cursor'' is between $x$ and $y$; when the cursor
  1460. first moves past $y$, the character immediately to its left is
  1461. $f(x,y)$. This function is defined if and only if no infinite loop is
  1462. generated when the cursor is between $x$ and~$y$.
  1463.  
  1464. The function $f(x,y)$ can be defined recursively. It turns out that all pairs
  1465. $(x,y)$ belong to one of five classes. The simplest class has $f(x,y)=y$; this
  1466. happens if there's no ligature between $x$ and $y$, or in the cases
  1467. \.{LIGfdiv>} and \.{fdivLIGfdiv>>}. Another simple class arises when there's a
  1468. \.{LIG} or \.{fdivLIG>} between $x$ and~$y$, generating the character~$z$;
  1469. then $f(x,y)=z$. Otherwise we always have $f(x,y)$ equal to
  1470. either $f(x,z)$ or $f(z,y)$ or $f(f(x,z),y)$, where $z$ is the inserted
  1471. ligature character.
  1472.  
  1473. The first two of these classes can be merged; we can also consider
  1474. $(x,y)$ to belong to the simple class when $f(x,y)$ has been evaluated.
  1475. For technical reasons we allow $x$ to be 256 (for the boundary character
  1476. at the left) or 257 (in cases when an error has been detected).
  1477.  
  1478. For each pair $(x,y)$ having a ligature program step, we store
  1479. $(x,y)$ in a hash table from which the values $z$ and $class$ can be read.
  1480.  
  1481. @d simple 0 /* $f(x,y)=z$ */
  1482. @d left_z 1 /* $f(x,y)=f(z,y)$ */
  1483. @d right_z 2 /* $f(x,y)=f(x,z)$ */
  1484. @d both_z 3 /* $f(x,y)=f(f(x,z),y)$ */
  1485. @d pending 4 /* $f(x,y)$ is being evaluated */
  1486.  
  1487. @<Glob...@>=
  1488. LONG @!hash[hash_size+1]; /* $256x+y+1$ for $x\le257$ and $y\le255$ */
  1489. UBYTE @!class[hash_size+1];
  1490. UWORD @!lig_z[hash_size+1];
  1491. hash_size_type @!hash_ptr; /* the number of nonzero entries in |hash| */
  1492. hash_size_type @!hash_list[hash_size+1]; /* list of those nonzero entries */
  1493. hash_size_type @!h,@!hh; /* indices into the hash table */
  1494. UWORD @!x_lig_cycle,@!y_lig_cycle; /* problematic ligature pair */
  1495.  
  1496. @ @<Check for ligature cycles@>=
  1497. hash_ptr = 0; y_lig_cycle = 256;
  1498. for (hh=0; hh<=hash_size; hh++) hash[hh] = 0; /* clear the hash table */
  1499. for (c=bc; c<=ec; c++) if (tag(c) == lig_tag)
  1500.   { i = remainder(c);
  1501.   if (tfm(lig_step(i)) > stop_flag)
  1502.     i = 256*tfm(lig_step(i)+2)+tfm(lig_step(i)+3);
  1503.   @<Enter data for character $c$ starting at location |i| in the hash table@>;
  1504.   }
  1505. if (bchar_label < nl)
  1506.   { c = 256; i = bchar_label;
  1507.   @<Enter data for character $c$ starting at location |i| in the hash table@>;
  1508.   }
  1509. if (hash_ptr == hash_size)
  1510.   { fprintf(output, "%s\n", "Sorry, I haven't room for so many ligature/kern pairs!");
  1511. @.Sorry, I haven't room...@>
  1512.   goto final_end;
  1513.   }
  1514. for (hh=1; hh<=hash_ptr; hh++)
  1515.   { r = hash_list[hh];
  1516.   if (class[r] > simple) /* make sure $f$ is defined */
  1517.      r = f(r, (index)((hash[r]-1)/256), (index)((hash[r]-1)%256));
  1518.   }
  1519. if (y_lig_cycle < 256)
  1520.   {  fprintf(output, "Infinite ligature loop starting with ");
  1521. @.Infinite ligature loop...@>
  1522.   if (x_lig_cycle == 256) fprintf(output, "boundary");@+else print_octal(x_lig_cycle);
  1523.   fprintf(output, " and "); print_octal(y_lig_cycle); fprintf(output, "%s\n", "!");
  1524.   out("(INFINITE LIGATURE LOOP MUST BE BROKEN!)"); goto final_end;
  1525.   }
  1526.  
  1527. @ @<Enter data for character $c$...@>=
  1528. do { hash_input(); k = tfm(lig_step(i));
  1529.   if (k >= stop_flag) i = nl;
  1530.   else i = i+1+k;
  1531.   }
  1532. while(i < nl);
  1533.  
  1534. @ We use an ``ordered hash table'' with linear probing, because such a table
  1535. is efficient when the lookup of a random key tends to be unsuccessful.
  1536.  
  1537. @c void hash_input(void) /* enter data for character |c| and command |i| */
  1538. {
  1539. UBYTE @!cc; /* class of data being entered */
  1540. UBYTE @!zz; /* function value or ligature character being entered */
  1541. UBYTE @!y; /* the character after the cursor */
  1542. LONG @!key; /* value to be stored in |hash| */
  1543. LONG @!t; /* temporary register for swapping */
  1544. @#
  1545. if (hash_ptr == hash_size) return;
  1546. @<Compute the command parameters |y|, |cc|, and |zz|@>;
  1547. key = 256*c+y+1; h = (index)((1009*key) % hash_size);
  1548. while( hash[h] > 0 )
  1549.   { if (hash[h] <= key)
  1550.     { if (hash[h] == key) return; /* unused ligature command */
  1551.     t = hash[h]; hash[h] = key; key = t; /* do ordered-hash-table insertion */
  1552.     t = class[h]; class[h] = cc; cc = t; /* namely, do a swap */
  1553.     t = lig_z[h]; lig_z[h] = zz; zz = t;
  1554.     }
  1555.   if (h > 0) decr(h);@+else h = hash_size;
  1556.   }
  1557. hash[h] = key; class[h] = cc; lig_z[h] = zz; incr(hash_ptr); hash_list[hash_ptr] = h;
  1558. }
  1559.  
  1560. @ We must store kern commands as well as ligature commands, because the former
  1561. might make the latter inapplicable.
  1562.  
  1563. @<Compute the command param...@>=
  1564. k = (index)(lig_step(i)); y = tfm(k+1); t = tfm(k+2); cc = simple; zz = tfm(k+3);
  1565. if (t >= kern_flag) zz = y;
  1566. else { switch (t) {
  1567.     case 0:
  1568.     case 6: do_nothing; break; /* \.{LIG},\.{/LIG>} */
  1569.     case 5:
  1570.     case 11: zz = y; break; /* \.{LIG/>}, \.{/LIG/>>} */
  1571.     case 1:
  1572.     case 7: cc = left_z; break; /* \.{LIG/}, \.{/LIG/>} */
  1573.     case 2: cc = right_z; break; /* \.{/LIG} */
  1574.     case 3: cc = both_z; break; /* \.{/LIG/} */
  1575.     }/* there are no other cases */
  1576.   }
  1577.  
  1578. @ Evaluation of $f(x,y)$ is handled by two mutually recursive procedures.
  1579. Kind of a neat algorithm, generalizing a depth-first search.
  1580.  
  1581. @c index f(index @!h, index @!x, index @!y);@t\2@>
  1582.   /* compute $f$ for arguments known to be in |hash[h]| */
  1583. index eval(index @!x, index @!y) /* compute $f(x,y)$ with hashtable lookup */
  1584. {
  1585. LONG @!key; /* value sought in hash table */
  1586. @#
  1587. key = 256*x+y+1; h = (index)((1009*key) % hash_size);
  1588. while( hash[h] > key )
  1589.   if (h > 0) decr(h);@+else h = hash_size;
  1590. if (hash[h] < key) return(y); /* not in ordered hash table */
  1591. else return(f(h, x, y));
  1592. }
  1593.  
  1594. @ Pascal's beastly convention for |forward| declarations prevents us from
  1595. saying |function f(h,x,y:index):index| here.
  1596.  
  1597. @c index f(index @!h, index @!x, index @!y)
  1598. {
  1599. switch (class[h]) {
  1600.   case simple: do_nothing; break;
  1601.   case left_z: class[h] = pending; lig_z[h] = eval(lig_z[h],y); class[h] = simple;
  1602.     break;
  1603.   case right_z: class[h] = pending; lig_z[h] = eval(x,lig_z[h]); class[h] = simple;
  1604.     break;
  1605.   case both_z: class[h] = pending; lig_z[h] = eval(eval(x,lig_z[h]),y);
  1606.     class[h] = simple; break;
  1607.   case pending: x_lig_cycle = x; y_lig_cycle = y; lig_z[h] = 257; class[h] = simple;
  1608.     break;/* the value 257 will break all cycles, since it's not in |hash| */
  1609.   }/* there are no other cases */
  1610. return(lig_z[h]);
  1611. }
  1612.  
  1613. @* The main program.
  1614. The routines sketched out so far need to be packaged into separate procedures,
  1615. on some systems, since some \PASCAL\ compilers place a strict limit on the
  1616. size of a routine. The packaging is done here in an attempt to avoid some
  1617. system-dependent changes.
  1618.  
  1619. First comes the |organize| procedure, which reads the input data and
  1620. gets ready for subsequent events. If something goes wrong, the routine
  1621. returns |false|.
  1622.  
  1623. @c boolean organize(void)
  1624. {
  1625. index tfm_ptr; /* an index into |tfm| */
  1626. @#
  1627. @<Read the whole input file@>;@/
  1628. @<Set subfile sizes |lh|, |bc|, \dots, |np|@>;@/
  1629. @<Compute the base addresses@>;@/
  1630. return(true);
  1631. final_end: return(false);
  1632. }
  1633.  
  1634. @ Next we do the simple things.
  1635.  
  1636. @c void do_simple_things(void)
  1637. {
  1638. UWORD i; /* an index to words of a subfile */
  1639. @#
  1640. @<Do the header@>;@/
  1641. @<Do the parameters@>;@/
  1642. @<Check the |fix_word| entries@>@/
  1643. }
  1644.  
  1645. @ And then there's a routine for individual characters.
  1646.  
  1647. @c void do_characters(void)
  1648. {
  1649. byte @!c; /* character being done */
  1650. index @!k; /* a random index */
  1651. lig_size_type @!ai; /* index into |activity| */
  1652. @#
  1653. @<Do the characters@>;@/
  1654. }
  1655.  
  1656. @ Here is where \.{TFtoPL} begins and ends.
  1657. @c
  1658. @<The function |append_extension|@>@/
  1659. int main(int argc, char ** argv)
  1660. {
  1661. @<Scan the file names@>@/
  1662. initialize();@/
  1663. if (!organize()) return(1);
  1664. do_simple_things();@/
  1665. @<Do the ligatures and kerns@>;
  1666. @<Check the extensible recipes@>;
  1667. do_characters(); fprintf(output, "%s\n", ".");@/
  1668. if (level != 0) fprintf(output, "%s\n", "This program isn't working!");
  1669. @.This program isn't working@>
  1670. if (!perfect)
  1671.   out("(COMMENT THE TFM FILE WAS BAD, SO THE DATA HAS BEEN CHANGED!)");
  1672. @.THE TFM FILE WAS BAD...@>
  1673. return(0);
  1674. final_end:return(1);
  1675. }
  1676.  
  1677. @* System-dependent changes.
  1678. This section should be replaced, if necessary, by changes to the program
  1679. that are necessary to make \.{TFtoPL} work at a particular installation.
  1680. It is usually best to design your change file so that all changes to
  1681. previous sections preserve the section numbering; then everybody's version
  1682. will be consistent with the printed program. More extensive changes,
  1683. which introduce new sections, can be inserted here; then only the index
  1684. itself will get a new section number.
  1685. @^system dependencies@>
  1686.  
  1687. @<The function |append_extension|@>=
  1688. void append_extension(char * name, char * extension)
  1689. {
  1690. char * p;
  1691.  
  1692.      p = name + strlen(name)-1;
  1693.      while (p > name) {
  1694.         if ((*p == '.') || (*p == '\\') || (*p == ':'))
  1695.            break;
  1696.         p--;
  1697.         }
  1698.      if ((p == name) || (*p != '.')) {
  1699.         strcat(name, ".");
  1700.         strcat(name, extension);
  1701.         }
  1702.      else {
  1703.         p++;
  1704.         if (strcmp(p, extension) != 0)
  1705.            strcpy(p, extension);
  1706.         }
  1707. }
  1708.  
  1709. @<Scan the file names@>=
  1710. argv++;
  1711. argc--;
  1712. if (argc == 0) {
  1713.    fprintf(stderr, "! No TFM file speciefied\n");
  1714.    exit(1);
  1715.    }
  1716. strcpy(tfm_name, *argv);
  1717. append_extension(tfm_name, "tfm");
  1718. argv++;
  1719. argc--;
  1720. @#
  1721. if (argc != 0) {
  1722.    strcpy(pl_name, *argv);
  1723.    argv++;
  1724.    argc--;
  1725.    }
  1726. else
  1727.    strcpy(pl_name, tfm_name);
  1728. append_extension(pl_name, "pl");
  1729.  
  1730. @* Index.
  1731. Pointers to error messages appear here together with the section numbers
  1732. where each ident\-i\-fier is used.
  1733.