1% tfmofm.w
2%
3% Copyright 2006-2012 Taco Hoekwater <taco@@luatex.org>
4%
5% This file is part of LuaTeX.
6%
7% LuaTeX is free software; you can redistribute it and/or modify it under
8% the terms of the GNU General Public License as published by the Free
9% Software Foundation; either version 2 of the License, or (at your
10% option) any later version.
11%
12% LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14% FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15% License for more details.
16%
17% You should have received a copy of the GNU General Public License along
18% with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
19
20@ @c
21
22
23#include "ptexlib.h"
24
25@ Here are some macros that help process ligatures and kerns
26@c
27#define lig_kern_start(f,c)   char_remainder(f,c)
28#define stop_flag 128           /* value indicating `\.{STOP}' in a lig/kern program */
29#define kern_flag 128           /* op code for a kern step */
30
31#define skip_byte(z)         lig_kerns[z].b0
32#define next_char(z)         lig_kerns[z].b1
33#define op_byte(z)           lig_kerns[z].b2
34#define rem_byte(z)          lig_kerns[z].b3
35#define lig_kern_restart(c)  (256*op_byte(c)+rem_byte(c))
36
37
38@
39The information in a \.{TFM} file appears in a sequence of 8-bit bytes.
40Since the number of bytes is always a multiple of 4, we could
41also regard the file as a sequence of 32-bit words, but \TeX\ uses the
42byte interpretation. The format of \.{TFM} files was designed by
43Lyle Ramshaw in 1980. The intent is to convey a lot of different kinds
44@^Ramshaw, Lyle Harold@>
45of information in a compact but useful form.
46
47$\Omega$ is capable of reading not only \.{TFM} files, but also
48\.{OFM} files, which can describe fonts with up to 65536 characters
49and with huge lig/kern tables.  These fonts will often be virtual
50fonts built up from real fonts with 256 characters, but $\Omega$
51is not aware of this.
52
53The documentation below describes \.{TFM} files, with slight additions
54to show where \.{OFM} files differ.
55
56@
57The first 24 bytes (6 words) of a \.{TFM} file contain twelve 16-bit
58integers that give the lengths of the various subsequent portions
59of the file. These twelve integers are, in order:
60$$\vbox{\halign{\hfil#&$\null=\null$#\hfil\cr
61|lf|&length of the entire file, in words;\cr
62|lh|&length of the header data, in words;\cr
63|bc|&smallest character code in the font;\cr
64|ec|&largest character code in the font;\cr
65|nw|&number of words in the width table;\cr
66|nh|&number of words in the height table;\cr
67|nd|&number of words in the depth table;\cr
68|ni|&number of words in the italic correction table;\cr
69|nl|&number of words in the lig/kern table;\cr
70|nk|&number of words in the kern table;\cr
71|ne|&number of words in the extensible character table;\cr
72|np|&number of font parameter words.\cr}}$$
73They are all nonnegative and less than $2^{15}$. We must have |bc-1<=ec<=255|,
74and
75$$\hbox{|lf=6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np|.}$$
76Note that a \.{TFM} font may contain as many as 256 characters
77(if |bc=0| and |ec=255|), and as few as 0 characters (if |bc=ec+1|).
78
79Incidentally, when two or more 8-bit bytes are combined to form an integer of
8016 or more bits, the most significant bytes appear first in the file.
81This is called BigEndian order.
82@!@^BigEndian order@>
83
84The first 52 bytes (13 words) of an \.{OFM} file contains thirteen
8532-bit integers that give the lengths of the various subsequent
86portions of the file.  The first word is 0 (future versions of
87\.{OFM} files could have different values;  what is important is that
88the first two bytes be 0 to differentiate \.{TFM} and \.{OFM} files).
89The next twelve integers are as above, all nonegative and less
90than~$2^{31}$.  We must have |bc-1<=ec<=65535|, and
91$$\hbox{|lf=13+lh+2*(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np|.}$$
92Note that an \.{OFM} font may contain as many as 65536 characters
93(if |bc=0| and |ec=65535|), and as few as 0 characters (if |bc=ec+1|).
94
95The rest of the \.{TFM} file may be regarded as a sequence of ten data
96arrays having the informal specification
97$$\def\arr$[#1]#2${\&{array} $[#1]$ \&{of} #2}
98\def\doarr\PB#1{\arr#1}
99\vbox{\halign{\hfil\\{#}&$\,:\,$\doarr#\hfil\cr
100header&|[0..lh-1]stuff|\cr
101char\_info&|[bc..ec]char_info_word|\cr
102width&|[0..nw-1]fix_word|\cr
103height&|[0..nh-1]fix_word|\cr
104depth&|[0..nd-1]fix_word|\cr
105italic&|[0..ni-1]fix_word|\cr
106lig\_kern&|[0..nl-1]lig_kern_command|\cr
107kern&|[0..nk-1]fix_word|\cr
108exten&|[0..ne-1]extensible_recipe|\cr
109param&\omit |[1..np]fix_word|\cr}}$$
110The most important data type used here is a |@!fix_word|, which is
111a 32-bit representation of a binary fraction. A |fix_word| is a signed
112quantity, with the two's complement of the entire word used to represent
113negation. Of the 32 bits in a |fix_word|, exactly 12 are to the left of the
114binary point; thus, the largest |fix_word| value is $2048-2^{-20}$, and
115the smallest is $-2048$. We will see below, however, that all but two of
116the |fix_word| values must lie between $-16$ and $+16$.
117
118The first data array is a block of header information, which contains
119general facts about the font. The header must contain at least two words,
120|header[0]| and |header[1]|, whose meaning is explained below.
121Additional header information of use to other software routines might
122also be included, but \TeX82 does not need to know about such details.
123For example, 16 more words of header information are in use at the Xerox
124Palo Alto Research Center; the first ten specify the character coding
125scheme used (e.g., `\.{XEROX text}' or `\.{TeX math symbols}'), the next five
126give the font identifier (e.g., `\.{HELVETICA}' or `\.{CMSY}'), and the
127last gives the ``face byte.'' The program that converts \.{DVI} files
128to Xerox printing format gets this information by looking at the \.{TFM}
129file, which it needs to read anyway because of other information that
130is not explicitly repeated in \.{DVI}~format.
131
132\yskip\hang|header[0]| is a 32-bit check sum that \TeX\ will copy into
133the \.{DVI} output file. Later on when the \.{DVI} file is printed,
134possibly on another computer, the actual font that gets used is supposed
135to have a check sum that agrees with the one in the \.{TFM} file used by
136\TeX. In this way, users will be warned about potential incompatibilities.
137(However, if the check sum is zero in either the font file or the \.{TFM}
138file, no check is made.)  The actual relation between this check sum and
139the rest of the \.{TFM} file is not important; the check sum is simply an
140identification number with the property that incompatible fonts almost
141always have distinct check sums.
142@^check sum@>
143
144\yskip\hang|header[1]| is a |fix_word| containing the design size of
145the font, in units of \TeX\ points. This number must be at least 1.0; it is
146fairly arbitrary, but usually the design size is 10.0 for a ``10 point''
147font, i.e., a font that was designed to look best at a 10-point size,
148whatever that really means. When a \TeX\ user asks for a font
149`\.{at} $\delta$ \.{pt}', the effect is to override the design size
150and replace it by $\delta$, and to multiply the $x$ and~$y$ coordinates
151of the points in the font image by a factor of $\delta$ divided by the
152design size.  {\sl All other dimensions in the\/ \.{TFM} file are
153|fix_word|\kern-1pt\ numbers in design-size units}, with the exception of
154|param[1]| (which denotes the slant ratio). Thus, for example, the value
155of |param[6]|, which defines the \.{em} unit, is often the |fix_word| value
156$2^{20}=1.0$, since many fonts have a design size equal to one em.
157The other dimensions must be less than 16 design-size units in absolute
158value; thus, |header[1]| and |param[1]| are the only |fix_word|
159entries in the whole \.{TFM} file whose first byte might be something
160besides 0 or 255.
161
162Next comes the |char_info| array, which contains one |@!char_info_word|
163per character. Each word in this part of a \.{TFM} file contains six fields
164packed into four bytes as follows.
165
166\yskip\hang first byte: |@!width_index| (8 bits)\par
167\hang second byte: |@!height_index| (4 bits) times 16, plus |@!depth_index|
168  (4~bits)\par
169\hang third byte: |@!italic_index| (6 bits) times 4, plus |@!tag|
170  (2~bits)\par
171\hang fourth byte: |@!remainder| (8 bits)\par
172\yskip\noindent
173The actual width of a character is \\{width}|[width_index]|, in design-size
174units; this is a device for compressing information, since many characters
175have the same width. Since it is quite common for many characters
176to have the same height, depth, or italic correction, the \.{TFM} format
177imposes a limit of 16 different heights, 16 different depths, and
17864 different italic corrections.
179
180For \.{OFM} files, two words (eight bytes) are used.
181The arrangement is as follows.
182
183\yskip\hang first and second bytes: |@!width_index| (16 bits)\par
184\hang third byte: |@!height_index| (8 bits)\par
185\hang fourth byte: |@!depth_index| (8~bits)\par
186\hang fifth and sixth bytes:
187|@!italic_index| (14 bits) times 4, plus |@!tag| (2~bits)\par
188\hang seventh and eighth bytes: |@!remainder| (16 bits)\par
189\yskip\noindent
190Therefore the \.{OFM} format imposes a limit of 256 different heights,
191256 different depths, and 16384 different italic corrections.
192
193@!@^italic correction@>
194The italic correction of a character has two different uses.
195(a)~In ordinary text, the italic correction is added to the width only if
196the \TeX\ user specifies `\.{\\/}' after the character.
197(b)~In math formulas, the italic correction is always added to the width,
198except with respect to the positioning of subscripts.
199
200Incidentally, the relation $\\{width}[0]=\\{height}[0]=\\{depth}[0]=
201\\{italic}[0]=0$ should always hold, so that an index of zero implies a
202value of zero.  The |width_index| should never be zero unless the
203character does not exist in the font, since a character is valid if and
204only if it lies between |bc| and |ec| and has a nonzero |width_index|.
205
206
207@ \TeX\ checks the information of a \.{TFM} file for validity as the
208file is being read in, so that no further checks will be needed when
209typesetting is going on. The somewhat tedious subroutine that does this
210is called |read_font_info|. It has four parameters: the user font
211identifier~|u|, the file name and area strings |nom| and |aire|, and the
212``at'' size~|s|. If |s|~is negative, it's the negative of a scale factor
213to be applied to the design size; |s=-1000| is the normal case.
214Otherwise |s| will be substituted for the design size; in this
215case, |s| must be positive and less than $2048\rm\,pt$
216(i.e., it must be less than $2^{27}$ when considered as an integer).
217
218The subroutine opens and closes a global file variable called |tfm_file|.
219It returns the value of the internal font number that was just loaded.
220If an error is detected, an error message is issued and no font
221information is stored; |null_font| is returned in this case.
222
223@
224The |tag| field in a |char_info_word| has four values that explain how to
225interpret the |remainder| field.
226
227\yskip\hang|tag=0| (|no_tag|) means that |remainder| is unused.\par
228\hang|tag=1| (|lig_tag|) means that this character has a ligature/kerning
229program starting at position |remainder| in the |lig_kern| array.\par
230\hang|tag=2| (|list_tag|) means that this character is part of a chain of
231characters of ascending sizes, and not the largest in the chain.  The
232|remainder| field gives the character code of the next larger character.\par
233\hang|tag=3| (|ext_tag|) means that this character code represents an
234extensible character, i.e., a character that is built up of smaller pieces
235so that it can be made arbitrarily large. The pieces are specified in
236|@!exten[remainder]|.\par
237\yskip\noindent
238Characters with |tag=2| and |tag=3| are treated as characters with |tag=0|
239unless they are used in special circumstances in math formulas. For example,
240the \.{\\sum} operation looks for a |list_tag|, and the \.{\\left}
241operation looks for both |list_tag| and |ext_tag|.
242
243
244@ The |lig_kern| array contains instructions in a simple programming language
245that explains what to do for special letter pairs. Each word in this array,
246in a \.{TFM} file, is a |@!lig_kern_command| of four bytes.
247
248\yskip\hang first byte: |skip_byte|, indicates that this is the final program
249  step if the byte is 128 or more, otherwise the next step is obtained by
250  skipping this number of intervening steps.\par
251\hang second byte: |next_char|, ``if |next_char| follows the current character,
252  then perform the operation and stop, otherwise continue.''\par
253\hang third byte: |op_byte|, indicates a ligature step if less than~128,
254  a kern step otherwise.\par
255\hang fourth byte: |remainder|.\par
256\yskip\noindent
257In an \.{OFM} file, eight bytes are used, two bytes for each field.
258
259In a kern step, an
260additional space equal to |kern[256*(op_byte-128)+remainder]| is inserted
261between the current character and |next_char|. This amount is
262often negative, so that the characters are brought closer together
263by kerning; but it might be positive.
264
265There are eight kinds of ligature steps, having |op_byte| codes $4a+2b+c$ where
266$0\le a\le b+c$ and $0\le b,c\le1$. The character whose code is
267|remainder| is inserted between the current character and |next_char|;
268then the current character is deleted if $b=0$, and |next_char| is
269deleted if $c=0$; then we pass over $a$~characters to reach the next
270current character (which may have a ligature/kerning program of its own).
271
272If the very first instruction of the |lig_kern| array has |skip_byte=255|,
273the |next_char| byte is the so-called right boundary character of this font;
274the value of |next_char| need not lie between |bc| and~|ec|.
275If the very last instruction of the |lig_kern| array has |skip_byte=255|,
276there is a special ligature/kerning program for a left boundary character,
277beginning at location |256*op_byte+remainder|.
278The interpretation is that \TeX\ puts implicit boundary characters
279before and after each consecutive string of characters from the same font.
280These implicit characters do not appear in the output, but they can affect
281ligatures and kerning.
282
283If the very first instruction of a character's |lig_kern| program has
284|skip_byte>128|, the program actually begins in location
285|256*op_byte+remainder|. This feature allows access to large |lig_kern|
286arrays, because the first instruction must otherwise
287appear in a location |<=255| in a \.{TFM} file, |<=65535| in an \.{OFM} file.
288
289Any instruction with |skip_byte>128| in the |lig_kern| array must satisfy
290the condition
291$$\hbox{|256*op_byte+remainder<nl|.}$$
292If such an instruction is encountered during
293normal program execution, it denotes an unconditional halt; no ligature
294or kerning command is performed.
295
296
297@ Extensible characters are specified by an |@!extensible_recipe|, which
298consists of four bytes in a \.{TFM} file,
299called |@!top|, |@!mid|, |@!bot|, and |@!rep| (in this order).
300In an \.{OFM} file, each field takes two bytes, for eight in total.
301These bytes are the character codes of individual pieces used to
302build up a large symbol.  If |top|, |mid|, or |bot| are zero, they are not
303present in the built-up result. For example, an extensible vertical line is
304like an extensible bracket, except that the top and bottom pieces are missing.
305
306Let $T$, $M$, $B$, and $R$ denote the respective pieces, or an empty box
307if the piece isn't present. Then the extensible characters have the form
308$TR^kMR^kB$ from top to bottom, for some |k>=0|, unless $M$ is absent;
309in the latter case we can have $TR^kB$ for both even and odd values of~|k|.
310The width of the extensible character is the width of $R$; and the
311height-plus-depth is the sum of the individual height-plus-depths of the
312components used, since the pieces are butted together in a vertical list.
313
314
315@
316The final portion of a \.{TFM} file is the |param| array, which is another
317sequence of |fix_word| values.
318
319\yskip\hang|param[1]=slant| is the amount of italic slant, which is used
320to help position accents. For example, |slant=.25| means that when you go
321up one unit, you also go .25 units to the right. The |slant| is a pure
322number; it's the only |fix_word| other than the design size itself that is
323not scaled by the design size.
324
325\hang|param[2]=space| is the normal spacing between words in text.
326Note that character |" "| in the font need not have anything to do with
327blank spaces.
328
329\hang|param[3]=space_stretch| is the amount of glue stretching between words.
330
331\hang|param[4]=space_shrink| is the amount of glue shrinking between words.
332
333\hang|param[5]=x_height| is the size of one ex in the font; it is also
334the height of letters for which accents don't have to be raised or lowered.
335
336\hang|param[6]=quad| is the size of one em in the font.
337
338\hang|param[7]=extra_space| is the amount added to |param[2]| at the
339ends of sentences.
340
341\yskip\noindent
342If fewer than seven parameters are present, \TeX\ sets the missing parameters
343to zero. Fonts used for math symbols are required to have
344additional parameter information, which is explained later.
345
346
347@
348 There are programs called \.{TFtoPL} and \.{PLtoTF} that convert
349 between the \.{TFM} format and a symbolic property-list format
350 that can be easily edited. These programs contain extensive
351 diagnostic information, so \TeX\ does not have to bother giving
352 precise details about why it rejects a particular \.{TFM} file.
353
354@c
355#define tfm_abort { font_tables[f]->_font_name = NULL;      \
356                    font_tables[f]->_font_area = NULL;      \
357                    xfree(tfm_buffer); xfree(kerns);      \
358        xfree(widths);  xfree(heights);  xfree(depths);     \
359        xfree(italics);  xfree(extens);  xfree(lig_kerns);  \
360        xfree(xligs);  xfree(xkerns);           \
361      return 0; }
362
363#define tfm_success { xfree(tfm_buffer); xfree(kerns);       \
364                xfree(widths);  xfree(heights);  xfree(depths);    \
365          xfree(italics);  xfree(extens);  xfree(lig_kerns); \
366          xfree(xligs);  xfree(xkerns); return 1; }
367
368@ @c
369static int open_tfm_file(const char *nom, unsigned char **tfm_buf, int *tfm_siz)
370{
371    boolean res;                /* was the callback successful? */
372    boolean opened;             /* was |tfm_file| successfully opened? */
373    int callback_id;
374    FILE *tfm_file;
375    char *fname = luatex_find_file(nom, find_font_file_callback);
376    if (!fname)
377        return -1;
378    callback_id = callback_defined(read_font_file_callback);
379    if (callback_id > 0) {
380        res =
381            run_callback(callback_id, "S->bSd", fname, &opened, tfm_buf,
382                         tfm_siz);
383        if (res && opened && (*tfm_siz > 0)) {
384            return 1;
385        }
386        if (!opened)
387            return -1;
388    } else {
389        if (luatex_open_input
390            (&(tfm_file), fname, kpse_ofm_format, FOPEN_RBIN_MODE, true)) {
391            res = read_tfm_file(tfm_file, tfm_buf, tfm_siz);
392            close_file(tfm_file);
393            if (res) {
394                return 1;
395            }
396        } else {
397            return -1;
398        }
399    }
400    return 0;
401}
402
403
404@
405  Note: A malformed \.{TFM} file might be shorter than it claims to be;
406  thus |eof(tfm_file)| might be true when |read_font_info| refers to
407  |tfm_file^| or when it says |get(tfm_file)|. If such circumstances
408  cause system error messages, you will have to defeat them somehow,
409  for example by defining |fget| to be `\ignorespaces|begin get(tfm_file);|
410  |if eof(tfm_file) then abort; end|\unskip'.
411  @^system dependencies@>
412@c
413
414#define fget  tfm_byte++
415#define fbyte tfm_buffer[tfm_byte]
416
417#define read_sixteen(a)                                                 \
418  { a=tfm_buffer[tfm_byte++];                                           \
419    if (a>127) { tfm_abort; }                                               \
420    a=(a*256)+tfm_buffer[tfm_byte]; }
421
422#define read_sixteen_unsigned(a)                                        \
423  { a=tfm_buffer[tfm_byte++];                                           \
424    a=(a*256)+tfm_buffer[tfm_byte]; }
425
426#define read_thirtytwo(a)                                               \
427  { a=tfm_buffer[++tfm_byte];                                           \
428    if (a>127) { tfm_abort; }                                               \
429    a=(a*256)+tfm_buffer[++tfm_byte];                                   \
430    a=(a*256)+tfm_buffer[++tfm_byte];                                   \
431    a=(a*256)+tfm_buffer[++tfm_byte]; }
432
433#define store_four_bytes(z)                                             \
434  { a=tfm_buffer[++tfm_byte];           \
435    a=(a*256)+tfm_buffer[++tfm_byte];         \
436    a=(a*256)+tfm_buffer[++tfm_byte];         \
437    a=(a*256)+tfm_buffer[++tfm_byte];         \
438    z = a; }
439
440#define store_char_info(z)                                              \
441  { if (font_level!=-1) {                                               \
442      fget; read_sixteen_unsigned(a);         \
443      ci._width_index=a;            \
444      fget; read_sixteen_unsigned(b);         \
445      ci._height_index=b>>8;            \
446      ci._depth_index=b%256;            \
447      fget; read_sixteen_unsigned(c);         \
448      ci._italic_index=c>>8;            \
449      ci._tag=(unsigned char)(c%4);	      \
450      fget; read_sixteen_unsigned(d);         \
451      ci._remainder=d;              \
452    } else {                                                            \
453      a=tfm_buffer[++tfm_byte];           \
454      ci._width_index=a;            \
455      b=tfm_buffer[++tfm_byte];           \
456      ci._height_index=b>>4;            \
457      ci._depth_index=b%16;           \
458      c=tfm_buffer[++tfm_byte];           \
459      ci._italic_index=c>>2;            \
460      ci._tag=(unsigned char)(c%4);	  \
461      d=tfm_buffer[++tfm_byte];           \
462      ci._remainder=d;              \
463    } }
464
465#define read_four_quarters(q)           \
466  { if (font_level!=-1) {                                        \
467      fget; read_sixteen_unsigned(a); q.b0=(quarterword)a;	 \
468      fget; read_sixteen_unsigned(b); q.b1=(quarterword)b;       \
469      fget; read_sixteen_unsigned(c); q.b2=(quarterword)c;       \
470      fget; read_sixteen_unsigned(d); q.b3=(quarterword)d;       \
471      } else {							 \
472      a=tfm_buffer[++tfm_byte]; q.b0=(quarterword)a;         \
473      b=tfm_buffer[++tfm_byte]; q.b1=(quarterword)b;         \
474      c=tfm_buffer[++tfm_byte]; q.b2=(quarterword)c;         \
475      d=tfm_buffer[++tfm_byte]; q.b3=(quarterword)d;	     \
476    } }
477
478#define check_byte_range(z)  { if ((z<bc)||(z>ec)) tfm_abort ; }
479
480
481@ A |fix_word| whose four bytes are $(a,b,c,d)$ from left to right represents
482   the number
483   $$x=\left\{\vcenter{\halign{$#$,\hfil\qquad&if $#$\hfil\cr
484   b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=0;\cr
485   -16+b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=255.\cr}}\right.$$
486   (No other choices of |a| are allowed, since the magnitude of a number in
487   design-size units must be less than 16.)  We want to multiply this
488   quantity by the integer~|z|, which is known to be less than $2^{27}$.
489   If $|z|<2^{23}$, the individual multiplications $b\cdot z$,
490   $c\cdot z$, $d\cdot z$ cannot overflow; otherwise we will divide |z| by 2,
491   4, 8, or 16, to obtain a multiplier less than $2^{23}$, and we can
492   compensate for this later. If |z| has thereby been replaced by
493   $|z|^\prime=|z|/2^e$, let $\beta=2^{4-e}$; we shall compute
494   $$\lfloor(b+c\cdot2^{-8}+d\cdot2^{-16})\,z^\prime/\beta\rfloor$$
495   if $a=0$, or the same quantity minus $\alpha=2^{4+e}z^\prime$ if $a=255$.
496   This calculation must be done exactly, in order to guarantee portability
497   of \TeX\ between computers.
498
499@c
500#define store_scaled(zz)                                                   \
501  { fget; a=fbyte; fget; b=fbyte;                                          \
502    fget; c=fbyte; fget; d=fbyte;                                          \
503    sw=(((((d*z)>>8)+(c*z))>>8)+(b*z)) / beta;                             \
504    if (a==0) { zz=sw; } else if (a==255) { zz=sw-alpha; } else tfm_abort; \
505  }
506
507scaled store_scaled_f(scaled sq, scaled z_in)
508{
509    eight_bits a, b, c, d;
510    scaled sw;
511    static int alpha, beta;     /* beta:1..16 */
512    static scaled z, z_prev = 0;
513    /* Replace |z| by $|z|^\prime$ and compute $\alpha,\beta$ */
514    if (z_in != z_prev || z_prev == 0) {
515        z = z_prev = z_in;
516        alpha = 16;
517        while (z >= 0x800000) {
518            z /= 2;
519            alpha += alpha;
520        }
521        beta = 256 / alpha;
522        alpha *= z;
523    };
524    if (sq >= 0) {
525        d = (eight_bits) (sq % 256);
526        sq = sq / 256;
527        c = (eight_bits) (sq % 256);
528        sq = sq / 256;
529        b = (eight_bits) (sq % 256);
530        sq = sq / 256;
531        a = (eight_bits) (sq % 256);
532    } else {
533        sq = (sq + 1073741824) + 1073741824;    /* braces for optimizing compiler */
534        d = (eight_bits) (sq % 256);
535        sq = sq / 256;
536        c = (eight_bits) (sq % 256);
537        sq = sq / 256;
538        b = (eight_bits) (sq % 256);
539        sq = sq / 256;
540        a = (eight_bits) ((sq + 128) % 256);
541    }
542    sw = (((((d * z) >> 8) + (c * z)) >> 8) + (b * z)) / beta;
543    if (a == 0)
544        return sw;
545    else if (a == 255)
546        return (sw - alpha);
547    else
548        pdf_error("vf", "vf scaling");
549    return sw;                  /* not reached, just to make the compiler happy */
550}
551
552@ @c
553#define  check_existence(z)                                             \
554  { check_byte_range(z);                                                \
555    if (!char_exists(f,z)) tfm_abort;         \
556  }
557
558typedef struct tfmcharacterinfo {
559    int _kern_index;
560    int _lig_index;
561    int _width_index;
562    int _height_index;
563    int _depth_index;
564    int _italic_index;
565    int _remainder;
566    unsigned char _tag;
567} tfmcharacterinfo;
568
569@ @c
570int read_tfm_info(internal_font_number f, const char *cnom, scaled s)
571{
572    int k;                      /* index into |font_info| */
573    halfword lf, lh, bc, ec, nw, nh, nd, ni, nl, nk, ne, np, slh;       /* sizes of subfiles */
574    scaled *widths, *heights, *depths, *italics, *kerns;
575    halfword font_dir;
576    int a, b, c=0, d=0;             /* byte variables */
577    int i;                      /* counter */
578    int font_level, header_length;
579    int ncw, nlw, neew;
580    tfmcharacterinfo ci;
581    charinfo *co;
582    four_quarters qw;
583    four_quarters *lig_kerns, *extens;
584    scaled sw;                  /* accumulators */
585    int bch_label;              /* left boundary start location, or infinity */
586    int bchar;                  /* |:0..too_big_char;| *//* right boundary character, or |too_big_char| */
587    int first_two;
588    scaled z;                   /* the design size or the ``at'' size */
589    int alpha;
590    char beta;                  /* :1..16 */
591    int *xligs, *xkerns;        /* aux. for ligkern processing */
592    liginfo *cligs;
593    kerninfo *ckerns;
594    int fligs, fkerns;
595    char *tmpnam;
596    int tfm_byte = 0;           /* index into |tfm_buffer| */
597    int saved_tfm_byte = 0;     /* saved index into |tfm_buffer| */
598    unsigned char *tfm_buffer = NULL;   /* byte buffer for tfm files */
599    int tfm_size = 0;           /* total size of the tfm file */
600    int tmp;
601
602    widths = NULL;
603    heights = NULL;
604    depths = NULL;
605    italics = NULL;
606    kerns = NULL;
607    lig_kerns = NULL;
608    extens = NULL;
609    xkerns = NULL;
610    ckerns = NULL;
611    xligs = NULL;
612    cligs = NULL;
613
614    font_dir = 0;
615
616    memset(&ci, 0, sizeof(tfmcharacterinfo));
617
618    if (open_tfm_file(cnom, &tfm_buffer, &tfm_size) != 1)
619        tfm_abort;
620
621    /* cnom can be an absolute filename, xbasename() fixes that. */
622
623    tmpnam = strdup(xbasename(cnom));
624    if (strcmp(tmpnam + strlen(tmpnam) - 4, ".tfm") == 0 || strcmp(tmpnam + strlen(tmpnam) - 4, ".ofm") == 0) {
625        *(tmpnam + strlen(tmpnam) - 4) = 0;
626    }
627    set_font_name(f, tmpnam);
628    set_font_area(f, NULL);
629
630    /* Read the {\.{TFM}} size fields */
631    ncw = 0;
632    read_sixteen(first_two);
633    if (first_two != 0) {
634        font_level = -1;
635        lf = first_two;
636        fget;
637        read_sixteen(lh);
638        fget;
639        read_sixteen(bc);
640        fget;
641        read_sixteen(ec);
642        if ((bc > ec + 1) || (ec > 255))
643            tfm_abort;
644        if (bc > 255) {         /* |bc=256| and |ec=255| */
645            bc = 1;
646            ec = 0;
647        };
648        fget;
649        read_sixteen(nw);
650        fget;
651        read_sixteen(nh);
652        fget;
653        read_sixteen(nd);
654        fget;
655        read_sixteen(ni);
656        fget;
657        read_sixteen(nl);
658        fget;
659        read_sixteen(nk);
660        fget;
661        read_sixteen(ne);
662        fget;
663        read_sixteen(np);
664        header_length = 6;
665        ncw = (ec - bc + 1);
666        nlw = nl;
667        neew = ne;
668    } else {
669        fget;
670        read_sixteen(font_level);
671        if (font_level != 0)
672            tfm_abort;
673        read_thirtytwo(lf);
674        read_thirtytwo(lh);
675        read_thirtytwo(bc);
676        read_thirtytwo(ec);
677        if ((bc > ec + 1) || (ec > 65535))
678            tfm_abort;
679        if (bc > 65535) {       /* |bc=65536| and |ec=65535| */
680            bc = 1;
681            ec = 0;
682        };
683        read_thirtytwo(nw);
684        read_thirtytwo(nh);
685        read_thirtytwo(nd);
686        read_thirtytwo(ni);
687        read_thirtytwo(nl);
688        read_thirtytwo(nk);
689        read_thirtytwo(ne);
690        read_thirtytwo(np);
691        read_thirtytwo(font_dir);       /* junk */
692        nlw = 2 * nl;
693        neew = 2 * ne;
694        header_length = 14;
695        ncw = 2 * (ec - bc + 1);
696    };
697    if (lf !=
698        (header_length + lh + ncw + nw + nh + nd + ni + nlw + nk + neew + np))
699        tfm_abort;
700    if ((nw == 0) || (nh == 0) || (nd == 0) || (ni == 0))
701        tfm_abort;
702
703    /*
704       We check to see that the \.{TFM} file doesn't end prematurely; but
705       no error message is given for files having more than |lf| words.
706     */
707    if (lf * 4 > tfm_size)
708        tfm_abort;
709
710    /* Use size fields to allocate font information */
711
712    set_font_natural_dir(f, font_dir);
713    set_font_bc(f, bc);
714    set_font_ec(f, ec);
715
716    /* read the arrays first */
717    widths = xmalloc((unsigned) ((unsigned) nw * sizeof(scaled)));
718    heights = xmalloc((unsigned) ((unsigned) nh * sizeof(scaled)));
719    depths = xmalloc((unsigned) ((unsigned) nd * sizeof(scaled)));
720    italics = xmalloc((unsigned) ((unsigned) ni * sizeof(scaled)));
721    extens = xmalloc((unsigned) ((unsigned) ne * sizeof(four_quarters)));
722    lig_kerns = xmalloc((unsigned) ((unsigned) nl * sizeof(four_quarters)));
723    kerns = xmalloc((unsigned) ((unsigned) nk * sizeof(scaled)));
724
725    /* Read the {\.{TFM}} header */
726
727    /* Only the first two words of the header are needed by \TeX82. */
728    slh = lh;
729    if (lh < 2)
730        tfm_abort;
731    store_four_bytes(tmp);
732    font_checksum(f) = (unsigned) tmp;
733    fget;
734    read_sixteen(z);            /* this rejects a negative design size */
735    fget;
736    z = z * 256 + fbyte;
737    fget;
738    z = (z * 16) + (fbyte >> 4);
739    if (z < unity)
740        tfm_abort;
741    while (lh > 2) {
742        fget;
743        fget;
744        fget;
745        fget;
746        lh--;                   /* ignore the rest of the header */
747    };
748
749    /* read the arrays before the character info */
750
751    set_font_dsize(f, z);
752    if (s != -1000) {
753        z = (s >= 0 ? s : xn_over_d(z, -s, 1000));
754    }
755    set_font_size(f, z);
756
757    if (np > 7)
758        set_font_params(f, np);
759
760    saved_tfm_byte = tfm_byte;
761    tfm_byte = (header_length + slh + ncw) * 4 - 1;
762
763    /* Replace |z| by $|z|^\prime$ and compute $\alpha,\beta$ */
764
765    alpha = 16;
766    while (z >= 040000000) {
767        z = z >> 1;
768        alpha = alpha + alpha;
769    };
770    beta = (char) (256 / alpha);
771    alpha = alpha * z;
772
773    /* Read box dimensions */
774
775    for (k = 0; k < nw; k++) {
776        store_scaled(sw);
777        widths[k] = sw;
778    }
779    if (widths[0] != 0)         /* \\{width}[0] must be zero */
780        tfm_abort;
781    for (k = 0; k < nh; k++) {
782        store_scaled(sw);
783        heights[k] = sw;
784    }
785    if (heights[0] != 0)
786        tfm_abort;              /* \\{height}[0] must be zero */
787    for (k = 0; k < nd; k++) {
788        store_scaled(sw);
789        depths[k] = sw;
790    }
791    if (depths[0] != 0)
792        tfm_abort;              /* \\{depth}[0] must be zero */
793    for (k = 0; k < ni; k++) {
794        store_scaled(sw);
795        italics[k] = sw;
796    }
797    if (italics[0] != 0)
798        tfm_abort;              /* \\{italic}[0] must be zero */
799
800
801    /* Read ligature/kern program */
802
803    bch_label = nl;             /* infinity */
804    bchar = 65536;
805    if (nl > 0) {
806        for (k = 0; k < nl; k++) {
807            read_four_quarters(qw);
808            lig_kerns[k] = qw;
809            if (a > 128) {
810                if (256 * c + d >= nl)
811                    tfm_abort;
812                if (a == 255 && k == 0)
813                    bchar = b;
814            } else {
815#if 0
816                if (b!=bchar) check_existence(b);
817#endif
818                if (c < 128) {
819#if 0
820                    check_existence(d); /* check ligature */
821#endif
822                } else if (256 * (c - 128) + d >= nk) {
823                    tfm_abort;  /* check kern */
824                }
825                if ((a < 128) && (k - 0 + a + 1 >= nl))
826                    tfm_abort;
827            };
828        };
829        if (a == 255)
830            bch_label = 256 * c + d;
831    };
832
833    /* the actual kerns */
834    for (k = 0; k < nk; k++) {
835        store_scaled(sw);
836        kerns[k] = sw;
837    }
838
839    /* Read extensible character recipes */
840    for (k = 0; k < ne; k++) {
841        read_four_quarters(qw);
842        extens[k] = qw;
843    }
844
845    /* Read font parameters */
846
847    if (np > 7) {
848        set_font_params(f, np);
849    }
850    for (k = 1; k <= np; k++) {
851        if (k == 1) {           /* the |slant| parameter is a pure number */
852            fget;
853            sw = fbyte;
854            if (sw > 127)
855                sw = sw - 256;
856            fget;
857            sw = sw * 256 + fbyte;
858            fget;
859            sw = sw * 256 + fbyte;
860            fget;
861            sw = (sw * 16) + (fbyte >> 4);
862            set_font_param(f, k, sw);
863        } else {
864            store_scaled(font_param(f, k));
865        }
866    }
867
868    tfm_byte = saved_tfm_byte;
869
870    /* fix up the left boundary character */
871    fligs = 0;
872    fkerns = 0;
873    if (bch_label != nl) {
874        k = bch_label;
875#if 0
876           if (skip_byte(k) > stop_flag)
877           k = lig_kern_restart(k);
878#endif
879        while (1) {
880            if (skip_byte(k) <= stop_flag) {
881                if (op_byte(k) >= kern_flag) {  /* kern */
882                    fkerns++;
883                } else {        /* lig */
884                    fligs++;
885                }
886            }
887            if (skip_byte(k) == 0) {
888                k++;
889            } else {
890                if (skip_byte(k) >= stop_flag)
891                    break;
892                k += skip_byte(k) + 1;
893            }
894        }
895    }
896    if (fkerns > 0 || fligs > 0) {
897        if (fligs > 0)
898            cligs = xcalloc((unsigned) (fligs + 1), sizeof(liginfo));
899        if (fkerns > 0)
900            ckerns = xcalloc((unsigned) (fkerns + 1), sizeof(kerninfo));
901        fligs = 0;
902        fkerns = 0;
903        k = bch_label;
904#if 0
905        if (skip_byte(k) > stop_flag)
906           k = lig_kern_restart(k);
907#endif
908        while (1) {
909            if (skip_byte(k) <= stop_flag) {
910                if (op_byte(k) >= kern_flag) {  /* kern */
911                    set_kern_item(ckerns[fkerns], next_char(k),
912                                  kerns[256 * (op_byte(k) - 128) +
913                                        rem_byte(k)]);
914                    fkerns++;
915                } else {        /* lig */
916                    set_ligature_item(cligs[fligs], (char) (op_byte(k) * 2 + 1),
917                                      next_char(k), rem_byte(k));
918                    fligs++;
919                }
920            }
921            if (skip_byte(k) == 0) {
922                k++;
923            } else {
924                if (skip_byte(k) >= stop_flag)
925                    break;
926                k += skip_byte(k) + 1;
927            }
928        }
929        if (fkerns > 0 || fligs > 0) {
930            co = get_charinfo(f, left_boundarychar);
931            if (fkerns > 0) {
932                set_kern_item(ckerns[fkerns], end_kern, 0);
933                fkerns++;
934                set_charinfo_kerns(co, ckerns);
935            }
936            if (fligs > 0) {
937                set_ligature_item(cligs[fligs], 0, end_ligature, 0);
938                fligs++;
939                set_charinfo_ligatures(co, cligs);
940            }
941            set_charinfo_remainder(co, 0);
942        }
943    }
944
945    /* Read character data */
946    for (k = bc; k <= ec; k++) {
947        store_char_info(k);
948        if (ci._width_index == 0)
949            continue;
950        if (ci._width_index >= nw || ci._height_index >= nh ||
951            ci._depth_index >= nd || ci._italic_index >= ni)
952            tfm_abort;
953        d = ci._remainder;
954        switch (ci._tag) {
955        case lig_tag:
956            if (d >= nl)
957                tfm_abort;
958            break;
959        case ext_tag:
960            if (d >= ne)
961                tfm_abort;
962            break;
963        case list_tag:
964            /* We want to make sure that there is no cycle of characters linked together
965               by |list_tag| entries, since such a cycle would get \TeX\ into an endless
966               loop. If such a cycle exists, the routine here detects it when processing
967               the largest character code in the cycle.
968             */
969            check_byte_range(d);
970            while (d < k) {     /* |current_character == k| */
971                if (char_tag(f, d) != list_tag)
972                    goto NOT_FOUND;     /* not a cycle */
973                d = char_remainder(f, d);       /* next character on the list */
974            };
975            if (d == k)
976                tfm_abort;      /* yes, there's a cycle */
977          NOT_FOUND:
978            break;
979        }
980        /* put it in the actual font */
981        co = get_charinfo(f, k);
982        set_charinfo_index(co, k);
983        set_charinfo_tag(co, ci._tag);
984        if (ci._tag == ext_tag) {
985            set_charinfo_extensible(co, extens[ci._remainder].b0,       /* top */
986                                    extens[ci._remainder].b2,   /* bot */
987                                    extens[ci._remainder].b1,   /* mid */
988                                    extens[ci._remainder].b3);  /* rep */
989            set_charinfo_remainder(co, 0);
990        } else {
991            set_charinfo_remainder(co, ci._remainder);
992        }
993        set_charinfo_width(co, widths[ci._width_index]);
994        set_charinfo_height(co, heights[ci._height_index]);
995        set_charinfo_depth(co, depths[ci._depth_index]);
996        set_charinfo_italic(co, italics[ci._italic_index]);
997    };
998
999    /* first pass: count ligs and kerns */
1000
1001    xligs = xcalloc((unsigned) (ec + 1), sizeof(int));
1002    xkerns = xcalloc((unsigned) (ec + 1), sizeof(int));
1003
1004    for (i = bc; i <= ec; i++) {
1005        if (char_tag(f, i) == lig_tag) {
1006            k = lig_kern_start(f, i);
1007            if (skip_byte(k) > stop_flag)
1008                k = lig_kern_restart(k);
1009            /* now k is the start index */
1010            while (1) {
1011                if (skip_byte(k) <= stop_flag) {
1012                    if (op_byte(k) >= kern_flag) {      /* kern */
1013                        xkerns[i]++;
1014                        if (next_char(k) == bchar)
1015                            xkerns[i]++;
1016                    } else {    /* lig */
1017                        xligs[i]++;
1018                        if (next_char(k) == bchar)
1019                            xligs[i]++;
1020                    }
1021                }
1022                if (skip_byte(k) == 0) {
1023                    k++;
1024                } else {
1025                    if (skip_byte(k) >= stop_flag)
1026                        break;
1027                    k += skip_byte(k) + 1;
1028                }
1029            }
1030        }
1031    }
1032
1033    cligs = NULL;
1034    ckerns = NULL;
1035
1036    for (i = bc; i <= ec; i++) {
1037        fligs = 0;
1038        fkerns = 0;
1039        if (char_tag(f, i) == lig_tag) {
1040            k = lig_kern_start(f, i);
1041            if (skip_byte(k) > stop_flag)
1042                k = lig_kern_restart(k);
1043            /* now k is the start index */
1044            if (xligs[i] > 0)
1045                cligs = xcalloc((unsigned) (xligs[i] + 1), sizeof(liginfo));
1046            if (xkerns[i] > 0)
1047                ckerns = xcalloc((unsigned) (xkerns[i] + 1), sizeof(kerninfo));
1048            while (1) {
1049                if (skip_byte(k) <= stop_flag) {
1050                    if (op_byte(k) >= kern_flag) {      /* kern */
1051                        if (next_char(k) == bchar) {
1052                            set_kern_item(ckerns[fkerns], right_boundarychar,
1053                                          kerns[256 * (op_byte(k) - 128) +
1054                                                rem_byte(k)]);
1055                            fkerns++;
1056                        }
1057                        set_kern_item(ckerns[fkerns], next_char(k),
1058                                      kerns[256 * (op_byte(k) - 128) +
1059                                            rem_byte(k)]);
1060                        fkerns++;
1061                    } else {    /* lig */
1062                        if (next_char(k) == bchar) {
1063                            set_ligature_item(cligs[fligs],
1064                                              (char) (op_byte(k) * 2 + 1),
1065                                              right_boundarychar, rem_byte(k));
1066                            fligs++;
1067                        }
1068                        set_ligature_item(cligs[fligs],
1069                                          (char) (op_byte(k) * 2 + 1),
1070                                          next_char(k), rem_byte(k));
1071                        fligs++;
1072                    }
1073                }
1074                if (skip_byte(k) == 0) {
1075                    k++;
1076                } else {
1077                    if (skip_byte(k) >= stop_flag)
1078                        break;
1079                    k += skip_byte(k) + 1;
1080                }
1081            }
1082            if (fkerns > 0 || fligs > 0) {
1083                co = get_charinfo(f, i);
1084                if (fkerns > 0) {
1085                    set_kern_item(ckerns[fkerns], end_kern, 0);
1086                    fkerns++;
1087                    set_charinfo_kerns(co, ckerns);
1088                }
1089                if (fligs > 0) {
1090                    set_ligature_item(cligs[fligs], 0, end_ligature, 0);
1091                    fligs++;
1092                    set_charinfo_ligatures(co, cligs);
1093                }
1094                set_charinfo_remainder(co, 0);
1095            }
1096        }
1097    }
1098
1099
1100    /* Make final adjustments and |goto done| */
1101
1102    /* Now to wrap it up, we have checked all the necessary things about the \.{TFM}
1103       file, and all we need to do is put the finishing touches on the data for
1104       the new font.
1105     */
1106
1107    if (bchar != 65536) {
1108        co = copy_charinfo(char_info(f, bchar));
1109        set_right_boundary(f, co);
1110    }
1111
1112    tfm_success;
1113}
1114