fix getsup (HH)
[luatex.git] / source / texk / web2c / luatexdir / font / tfmofm.w
blob6eeccdda214d9aa728c4b8cbbf3035d64169fe88
1 % tfmofm.w
3 % Copyright 2006-2012 Taco Hoekwater <taco@@luatex.org>
5 % This file is part of LuaTeX.
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.
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.
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/>.
20 @ @c
23 #include "ptexlib.h"
25 @ Here are some macros that help process ligatures and kerns
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 */
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))
39 The information in a \.{TFM} file appears in a sequence of 8-bit bytes.
40 Since the number of bytes is always a multiple of 4, we could
41 also regard the file as a sequence of 32-bit words, but \TeX\ uses the
42 byte interpretation. The format of \.{TFM} files was designed by
43 Lyle Ramshaw in 1980. The intent is to convey a lot of different kinds
44 @^Ramshaw, Lyle Harold@>
45 of information in a compact but useful form.
47 $\Omega$ is capable of reading not only \.{TFM} files, but also
48 \.{OFM} files, which can describe fonts with up to 65536 characters
49 and with huge lig/kern tables. These fonts will often be virtual
50 fonts built up from real fonts with 256 characters, but $\Omega$
51 is not aware of this.
53 The documentation below describes \.{TFM} files, with slight additions
54 to show where \.{OFM} files differ.
57 The first 24 bytes (6 words) of a \.{TFM} file contain twelve 16-bit
58 integers that give the lengths of the various subsequent portions
59 of 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}}$$
73 They are all nonnegative and less than $2^{15}$. We must have |bc-1<=ec<=255|,
74 and
75 $$\hbox{|lf=6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np|.}$$
76 Note 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|).
79 Incidentally, when two or more 8-bit bytes are combined to form an integer of
80 16 or more bits, the most significant bytes appear first in the file.
81 This is called BigEndian order.
82 @!@^BigEndian order@>
84 The first 52 bytes (13 words) of an \.{OFM} file contains thirteen
85 32-bit integers that give the lengths of the various subsequent
86 portions of the file. The first word is 0 (future versions of
87 \.{OFM} files could have different values; what is important is that
88 the first two bytes be 0 to differentiate \.{TFM} and \.{OFM} files).
89 The next twelve integers are as above, all nonegative and less
90 than~$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|.}$$
92 Note 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|).
95 The rest of the \.{TFM} file may be regarded as a sequence of ten data
96 arrays 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
100 header&|[0..lh-1]stuff|\cr
101 char\_info&|[bc..ec]char_info_word|\cr
102 width&|[0..nw-1]fix_word|\cr
103 height&|[0..nh-1]fix_word|\cr
104 depth&|[0..nd-1]fix_word|\cr
105 italic&|[0..ni-1]fix_word|\cr
106 lig\_kern&|[0..nl-1]lig_kern_command|\cr
107 kern&|[0..nk-1]fix_word|\cr
108 exten&|[0..ne-1]extensible_recipe|\cr
109 param&\omit |[1..np]fix_word|\cr}}$$
110 The most important data type used here is a |@!fix_word|, which is
111 a 32-bit representation of a binary fraction. A |fix_word| is a signed
112 quantity, with the two's complement of the entire word used to represent
113 negation. Of the 32 bits in a |fix_word|, exactly 12 are to the left of the
114 binary point; thus, the largest |fix_word| value is $2048-2^{-20}$, and
115 the smallest is $-2048$. We will see below, however, that all but two of
116 the |fix_word| values must lie between $-16$ and $+16$.
118 The first data array is a block of header information, which contains
119 general facts about the font. The header must contain at least two words,
120 |header[0]| and |header[1]|, whose meaning is explained below.
121 Additional header information of use to other software routines might
122 also be included, but \TeX82 does not need to know about such details.
123 For example, 16 more words of header information are in use at the Xerox
124 Palo Alto Research Center; the first ten specify the character coding
125 scheme used (e.g., `\.{XEROX text}' or `\.{TeX math symbols}'), the next five
126 give the font identifier (e.g., `\.{HELVETICA}' or `\.{CMSY}'), and the
127 last gives the ``face byte.'' The program that converts \.{DVI} files
128 to Xerox printing format gets this information by looking at the \.{TFM}
129 file, which it needs to read anyway because of other information that
130 is not explicitly repeated in \.{DVI}~format.
132 \yskip\hang|header[0]| is a 32-bit check sum that \TeX\ will copy into
133 the \.{DVI} output file. Later on when the \.{DVI} file is printed,
134 possibly on another computer, the actual font that gets used is supposed
135 to 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}
138 file, no check is made.) The actual relation between this check sum and
139 the rest of the \.{TFM} file is not important; the check sum is simply an
140 identification number with the property that incompatible fonts almost
141 always have distinct check sums.
142 @^check sum@>
144 \yskip\hang|header[1]| is a |fix_word| containing the design size of
145 the font, in units of \TeX\ points. This number must be at least 1.0; it is
146 fairly arbitrary, but usually the design size is 10.0 for a ``10 point''
147 font, i.e., a font that was designed to look best at a 10-point size,
148 whatever that really means. When a \TeX\ user asks for a font
149 `\.{at} $\delta$ \.{pt}', the effect is to override the design size
150 and replace it by $\delta$, and to multiply the $x$ and~$y$ coordinates
151 of the points in the font image by a factor of $\delta$ divided by the
152 design 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
155 of |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.
157 The other dimensions must be less than 16 design-size units in absolute
158 value; thus, |header[1]| and |param[1]| are the only |fix_word|
159 entries in the whole \.{TFM} file whose first byte might be something
160 besides 0 or 255.
162 Next comes the |char_info| array, which contains one |@!char_info_word|
163 per character. Each word in this part of a \.{TFM} file contains six fields
164 packed into four bytes as follows.
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
173 The actual width of a character is \\{width}|[width_index]|, in design-size
174 units; this is a device for compressing information, since many characters
175 have the same width. Since it is quite common for many characters
176 to have the same height, depth, or italic correction, the \.{TFM} format
177 imposes a limit of 16 different heights, 16 different depths, and
178 64 different italic corrections.
180 For \.{OFM} files, two words (eight bytes) are used.
181 The arrangement is as follows.
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
190 Therefore the \.{OFM} format imposes a limit of 256 different heights,
191 256 different depths, and 16384 different italic corrections.
193 @!@^italic correction@>
194 The italic correction of a character has two different uses.
195 (a)~In ordinary text, the italic correction is added to the width only if
196 the \TeX\ user specifies `\.{\\/}' after the character.
197 (b)~In math formulas, the italic correction is always added to the width,
198 except with respect to the positioning of subscripts.
200 Incidentally, the relation $\\{width}[0]=\\{height}[0]=\\{depth}[0]=
201 \\{italic}[0]=0$ should always hold, so that an index of zero implies a
202 value of zero. The |width_index| should never be zero unless the
203 character does not exist in the font, since a character is valid if and
204 only if it lies between |bc| and |ec| and has a nonzero |width_index|.
207 @ \TeX\ checks the information of a \.{TFM} file for validity as the
208 file is being read in, so that no further checks will be needed when
209 typesetting is going on. The somewhat tedious subroutine that does this
210 is called |read_font_info|. It has four parameters: the user font
211 identifier~|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
213 to be applied to the design size; |s=-1000| is the normal case.
214 Otherwise |s| will be substituted for the design size; in this
215 case, |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).
218 The subroutine opens and closes a global file variable called |tfm_file|.
219 It returns the value of the internal font number that was just loaded.
220 If an error is detected, an error message is issued and no font
221 information is stored; |null_font| is returned in this case.
224 The |tag| field in a |char_info_word| has four values that explain how to
225 interpret the |remainder| field.
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
229 program 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
231 characters 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
234 extensible character, i.e., a character that is built up of smaller pieces
235 so that it can be made arbitrarily large. The pieces are specified in
236 |@!exten[remainder]|.\par
237 \yskip\noindent
238 Characters with |tag=2| and |tag=3| are treated as characters with |tag=0|
239 unless they are used in special circumstances in math formulas. For example,
240 the \.{\\sum} operation looks for a |list_tag|, and the \.{\\left}
241 operation looks for both |list_tag| and |ext_tag|.
244 @ The |lig_kern| array contains instructions in a simple programming language
245 that explains what to do for special letter pairs. Each word in this array,
246 in a \.{TFM} file, is a |@!lig_kern_command| of four bytes.
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
257 In an \.{OFM} file, eight bytes are used, two bytes for each field.
259 In a kern step, an
260 additional space equal to |kern[256*(op_byte-128)+remainder]| is inserted
261 between the current character and |next_char|. This amount is
262 often negative, so that the characters are brought closer together
263 by kerning; but it might be positive.
265 There 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|;
268 then the current character is deleted if $b=0$, and |next_char| is
269 deleted if $c=0$; then we pass over $a$~characters to reach the next
270 current character (which may have a ligature/kerning program of its own).
272 If the very first instruction of the |lig_kern| array has |skip_byte=255|,
273 the |next_char| byte is the so-called right boundary character of this font;
274 the value of |next_char| need not lie between |bc| and~|ec|.
275 If the very last instruction of the |lig_kern| array has |skip_byte=255|,
276 there is a special ligature/kerning program for a left boundary character,
277 beginning at location |256*op_byte+remainder|.
278 The interpretation is that \TeX\ puts implicit boundary characters
279 before and after each consecutive string of characters from the same font.
280 These implicit characters do not appear in the output, but they can affect
281 ligatures and kerning.
283 If 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|
286 arrays, because the first instruction must otherwise
287 appear in a location |<=255| in a \.{TFM} file, |<=65535| in an \.{OFM} file.
289 Any instruction with |skip_byte>128| in the |lig_kern| array must satisfy
290 the condition
291 $$\hbox{|256*op_byte+remainder<nl|.}$$
292 If such an instruction is encountered during
293 normal program execution, it denotes an unconditional halt; no ligature
294 or kerning command is performed.
297 @ Extensible characters are specified by an |@!extensible_recipe|, which
298 consists of four bytes in a \.{TFM} file,
299 called |@!top|, |@!mid|, |@!bot|, and |@!rep| (in this order).
300 In an \.{OFM} file, each field takes two bytes, for eight in total.
301 These bytes are the character codes of individual pieces used to
302 build up a large symbol. If |top|, |mid|, or |bot| are zero, they are not
303 present in the built-up result. For example, an extensible vertical line is
304 like an extensible bracket, except that the top and bottom pieces are missing.
306 Let $T$, $M$, $B$, and $R$ denote the respective pieces, or an empty box
307 if 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;
309 in the latter case we can have $TR^kB$ for both even and odd values of~|k|.
310 The width of the extensible character is the width of $R$; and the
311 height-plus-depth is the sum of the individual height-plus-depths of the
312 components used, since the pieces are butted together in a vertical list.
316 The final portion of a \.{TFM} file is the |param| array, which is another
317 sequence of |fix_word| values.
319 \yskip\hang|param[1]=slant| is the amount of italic slant, which is used
320 to help position accents. For example, |slant=.25| means that when you go
321 up one unit, you also go .25 units to the right. The |slant| is a pure
322 number; it's the only |fix_word| other than the design size itself that is
323 not scaled by the design size.
325 \hang|param[2]=space| is the normal spacing between words in text.
326 Note that character |" "| in the font need not have anything to do with
327 blank spaces.
329 \hang|param[3]=space_stretch| is the amount of glue stretching between words.
331 \hang|param[4]=space_shrink| is the amount of glue shrinking between words.
333 \hang|param[5]=x_height| is the size of one ex in the font; it is also
334 the height of letters for which accents don't have to be raised or lowered.
336 \hang|param[6]=quad| is the size of one em in the font.
338 \hang|param[7]=extra_space| is the amount added to |param[2]| at the
339 ends of sentences.
341 \yskip\noindent
342 If fewer than seven parameters are present, \TeX\ sets the missing parameters
343 to zero. Fonts used for math symbols are required to have
344 additional parameter information, which is explained later.
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.
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; }
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; }
368 @ @c
369 static int open_tfm_file(const char *nom, unsigned char **tfm_buf, int *tfm_siz)
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;
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;
396 } else {
397 return -1;
400 return 0;
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@>
414 #define fget tfm_byte++
415 #define fbyte tfm_buffer[tfm_byte]
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]; }
422 #define read_sixteen_unsigned(a) \
423 { a=tfm_buffer[tfm_byte++]; \
424 a=(a*256)+tfm_buffer[tfm_byte]; }
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]; }
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; }
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; \
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; \
478 #define check_byte_range(z) { if ((z<bc)||(z>ec)) tfm_abort ; }
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.
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; \
507 scaled store_scaled_f(scaled sq, scaled z_in)
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;
521 beta = 256 / alpha;
522 alpha *= z;
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);
542 if (beta==0)
543 normal_error("vf", "vf scaling");
544 sw = (((((d * z) >> 8) + (c * z)) >> 8) + (b * z)) / beta;
545 if (a == 0)
546 return sw;
547 else if (a == 255)
548 return (sw - alpha);
549 else
550 normal_error("vf", "vf scaling");
551 return sw; /* not reached, just to make the compiler happy */
554 @ @c
555 #define check_existence(z) \
556 { check_byte_range(z); \
557 if (!char_exists(f,z)) tfm_abort; \
560 typedef struct tfmcharacterinfo {
561 int _kern_index;
562 int _lig_index;
563 int _width_index;
564 int _height_index;
565 int _depth_index;
566 int _italic_index;
567 int _remainder;
568 unsigned char _tag;
569 } tfmcharacterinfo;
571 @ @c
572 int read_tfm_info(internal_font_number f, const char *cnom, scaled s)
574 int k; /* index into |font_info| */
575 halfword lf, lh, bc, ec, nw, nh, nd, ni, nl, nk, ne, np, slh; /* sizes of subfiles */
576 scaled *widths, *heights, *depths, *italics, *kerns;
577 halfword font_dir;
578 int a, b, c=0, d=0; /* byte variables */
579 int i; /* counter */
580 int font_level, header_length;
581 int ncw, nlw, neew;
582 tfmcharacterinfo ci;
583 charinfo *co;
584 four_quarters qw;
585 four_quarters *lig_kerns, *extens;
586 scaled sw; /* accumulators */
587 int bch_label; /* left boundary start location, or infinity */
588 int bchar; /* |:0..too_big_char;| *//* right boundary character, or |too_big_char| */
589 int first_two;
590 scaled z; /* the design size or the ``at'' size */
591 int alpha;
592 char beta; /* :1..16 */
593 int *xligs, *xkerns; /* aux. for ligkern processing */
594 liginfo *cligs;
595 kerninfo *ckerns;
596 int fligs, fkerns;
597 char *tmpnam;
598 int tfm_byte = 0; /* index into |tfm_buffer| */
599 int saved_tfm_byte = 0; /* saved index into |tfm_buffer| */
600 unsigned char *tfm_buffer = NULL; /* byte buffer for tfm files */
601 int tfm_size = 0; /* total size of the tfm file */
602 int tmp;
604 widths = NULL;
605 heights = NULL;
606 depths = NULL;
607 italics = NULL;
608 kerns = NULL;
609 lig_kerns = NULL;
610 extens = NULL;
611 xkerns = NULL;
612 ckerns = NULL;
613 xligs = NULL;
614 cligs = NULL;
616 font_dir = 0;
618 memset(&ci, 0, sizeof(tfmcharacterinfo));
620 if (open_tfm_file(cnom, &tfm_buffer, &tfm_size) != 1)
621 tfm_abort;
623 /* cnom can be an absolute filename, xbasename() fixes that. */
625 tmpnam = strdup(xbasename(cnom));
626 if (strcmp(tmpnam + strlen(tmpnam) - 4, ".tfm") == 0 || strcmp(tmpnam + strlen(tmpnam) - 4, ".ofm") == 0) {
627 *(tmpnam + strlen(tmpnam) - 4) = 0;
629 set_font_name(f, tmpnam);
630 set_font_area(f, NULL);
632 /* Read the {\.{TFM}} size fields */
633 ncw = 0;
634 read_sixteen(first_two);
635 if (first_two != 0) {
636 font_level = -1;
637 lf = first_two;
638 fget;
639 read_sixteen(lh);
640 fget;
641 read_sixteen(bc);
642 fget;
643 read_sixteen(ec);
644 if ((bc > ec + 1) || (ec > 255))
645 tfm_abort;
646 if (bc > 255) { /* |bc=256| and |ec=255| */
647 bc = 1;
648 ec = 0;
650 fget;
651 read_sixteen(nw);
652 fget;
653 read_sixteen(nh);
654 fget;
655 read_sixteen(nd);
656 fget;
657 read_sixteen(ni);
658 fget;
659 read_sixteen(nl);
660 fget;
661 read_sixteen(nk);
662 fget;
663 read_sixteen(ne);
664 fget;
665 read_sixteen(np);
666 header_length = 6;
667 ncw = (ec - bc + 1);
668 nlw = nl;
669 neew = ne;
670 } else {
671 fget;
672 read_sixteen(font_level);
673 if (font_level != 0)
674 tfm_abort;
675 read_thirtytwo(lf);
676 read_thirtytwo(lh);
677 read_thirtytwo(bc);
678 read_thirtytwo(ec);
679 if ((bc > ec + 1) || (ec > 65535))
680 tfm_abort;
681 if (bc > 65535) { /* |bc=65536| and |ec=65535| */
682 bc = 1;
683 ec = 0;
685 read_thirtytwo(nw);
686 read_thirtytwo(nh);
687 read_thirtytwo(nd);
688 read_thirtytwo(ni);
689 read_thirtytwo(nl);
690 read_thirtytwo(nk);
691 read_thirtytwo(ne);
692 read_thirtytwo(np);
693 read_thirtytwo(font_dir); /* junk */
694 nlw = 2 * nl;
695 neew = 2 * ne;
696 header_length = 14;
697 ncw = 2 * (ec - bc + 1);
699 if (lf !=
700 (header_length + lh + ncw + nw + nh + nd + ni + nlw + nk + neew + np))
701 tfm_abort;
702 if ((nw == 0) || (nh == 0) || (nd == 0) || (ni == 0))
703 tfm_abort;
706 We check to see that the \.{TFM} file doesn't end prematurely; but
707 no error message is given for files having more than |lf| words.
709 if (lf * 4 > tfm_size)
710 tfm_abort;
712 /* Use size fields to allocate font information */
714 set_font_natural_dir(f, font_dir);
715 set_font_bc(f, bc);
716 set_font_ec(f, ec);
718 /* read the arrays first */
719 widths = xmalloc((unsigned) ((unsigned) nw * sizeof(scaled)));
720 heights = xmalloc((unsigned) ((unsigned) nh * sizeof(scaled)));
721 depths = xmalloc((unsigned) ((unsigned) nd * sizeof(scaled)));
722 italics = xmalloc((unsigned) ((unsigned) ni * sizeof(scaled)));
723 extens = xmalloc((unsigned) ((unsigned) ne * sizeof(four_quarters)));
724 lig_kerns = xmalloc((unsigned) ((unsigned) nl * sizeof(four_quarters)));
725 kerns = xmalloc((unsigned) ((unsigned) nk * sizeof(scaled)));
727 /* Read the {\.{TFM}} header */
729 /* Only the first two words of the header are needed by \TeX82. */
730 slh = lh;
731 if (lh < 2)
732 tfm_abort;
733 store_four_bytes(tmp);
734 font_checksum(f) = (unsigned) tmp;
735 fget;
736 read_sixteen(z); /* this rejects a negative design size */
737 fget;
738 z = z * 256 + fbyte;
739 fget;
740 z = (z * 16) + (fbyte >> 4);
741 if (z < unity)
742 tfm_abort;
743 while (lh > 2) {
744 fget;
745 fget;
746 fget;
747 fget;
748 lh--; /* ignore the rest of the header */
751 /* read the arrays before the character info */
753 set_font_dsize(f, z);
754 if (s != -1000) {
755 z = (s >= 0 ? s : xn_over_d(z, -s, 1000));
757 set_font_size(f, z);
759 if (np > 7)
760 set_font_params(f, np);
762 saved_tfm_byte = tfm_byte;
763 tfm_byte = (header_length + slh + ncw) * 4 - 1;
765 /* Replace |z| by $|z|^\prime$ and compute $\alpha,\beta$ */
767 alpha = 16;
768 while (z >= 040000000) {
769 z = z >> 1;
770 alpha = alpha + alpha;
772 beta = (char) (256 / alpha);
774 /* beta cannot be zero */
775 if (beta==0)
776 normal_error("vf", "vf reading");
777 alpha = alpha * z;
779 /* Read box dimensions */
781 for (k = 0; k < nw; k++) {
782 store_scaled(sw);
783 widths[k] = sw;
785 if (widths[0] != 0) /* \\{width}[0] must be zero */
786 tfm_abort;
787 for (k = 0; k < nh; k++) {
788 store_scaled(sw);
789 heights[k] = sw;
791 if (heights[0] != 0)
792 tfm_abort; /* \\{height}[0] must be zero */
793 for (k = 0; k < nd; k++) {
794 store_scaled(sw);
795 depths[k] = sw;
797 if (depths[0] != 0)
798 tfm_abort; /* \\{depth}[0] must be zero */
799 for (k = 0; k < ni; k++) {
800 store_scaled(sw);
801 italics[k] = sw;
803 if (italics[0] != 0)
804 tfm_abort; /* \\{italic}[0] must be zero */
807 /* Read ligature/kern program */
809 bch_label = nl; /* infinity */
810 bchar = 65536;
811 if (nl > 0) {
812 for (k = 0; k < nl; k++) {
813 read_four_quarters(qw);
814 lig_kerns[k] = qw;
815 if (a > 128) {
816 if (256 * c + d >= nl)
817 tfm_abort;
818 if (a == 255 && k == 0)
819 bchar = b;
820 } else {
821 #if 0
822 if (b!=bchar) check_existence(b);
823 #endif
824 if (c < 128) {
825 #if 0
826 check_existence(d); /* check ligature */
827 #endif
828 } else if (256 * (c - 128) + d >= nk) {
829 tfm_abort; /* check kern */
831 if ((a < 128) && (k - 0 + a + 1 >= nl))
832 tfm_abort;
835 if (a == 255)
836 bch_label = 256 * c + d;
839 /* the actual kerns */
840 for (k = 0; k < nk; k++) {
841 store_scaled(sw);
842 kerns[k] = sw;
845 /* Read extensible character recipes */
846 for (k = 0; k < ne; k++) {
847 read_four_quarters(qw);
848 extens[k] = qw;
851 /* Read font parameters */
853 if (np > 7) {
854 set_font_params(f, np);
856 for (k = 1; k <= np; k++) {
857 if (k == 1) { /* the |slant| parameter is a pure number */
858 fget;
859 sw = fbyte;
860 if (sw > 127)
861 sw = sw - 256;
862 fget;
863 sw = sw * 256 + fbyte;
864 fget;
865 sw = sw * 256 + fbyte;
866 fget;
867 sw = (sw * 16) + (fbyte >> 4);
868 set_font_param(f, k, sw);
869 } else {
870 store_scaled(font_param(f, k));
874 tfm_byte = saved_tfm_byte;
876 /* fix up the left boundary character */
877 fligs = 0;
878 fkerns = 0;
879 if (bch_label != nl) {
880 k = bch_label;
881 #if 0
882 if (skip_byte(k) > stop_flag)
883 k = lig_kern_restart(k);
884 #endif
885 while (1) {
886 if (skip_byte(k) <= stop_flag) {
887 if (op_byte(k) >= kern_flag) { /* kern */
888 fkerns++;
889 } else { /* lig */
890 fligs++;
893 if (skip_byte(k) == 0) {
894 k++;
895 } else {
896 if (skip_byte(k) >= stop_flag)
897 break;
898 k += skip_byte(k) + 1;
902 if (fkerns > 0 || fligs > 0) {
903 if (fligs > 0)
904 cligs = xcalloc((unsigned) (fligs + 1), sizeof(liginfo));
905 if (fkerns > 0)
906 ckerns = xcalloc((unsigned) (fkerns + 1), sizeof(kerninfo));
907 fligs = 0;
908 fkerns = 0;
909 k = bch_label;
910 #if 0
911 if (skip_byte(k) > stop_flag)
912 k = lig_kern_restart(k);
913 #endif
914 while (1) {
915 if (skip_byte(k) <= stop_flag) {
916 if (op_byte(k) >= kern_flag) { /* kern */
917 set_kern_item(ckerns[fkerns], next_char(k),
918 kerns[256 * (op_byte(k) - 128) +
919 rem_byte(k)]);
920 fkerns++;
921 } else { /* lig */
922 set_ligature_item(cligs[fligs], (char) (op_byte(k) * 2 + 1),
923 next_char(k), rem_byte(k));
924 fligs++;
927 if (skip_byte(k) == 0) {
928 k++;
929 } else {
930 if (skip_byte(k) >= stop_flag)
931 break;
932 k += skip_byte(k) + 1;
935 if (fkerns > 0 || fligs > 0) {
936 co = get_charinfo(f, left_boundarychar);
937 if (fkerns > 0) {
938 set_kern_item(ckerns[fkerns], end_kern, 0);
939 fkerns++;
940 set_charinfo_kerns(co, ckerns);
942 if (fligs > 0) {
943 set_ligature_item(cligs[fligs], 0, end_ligature, 0);
944 fligs++;
945 set_charinfo_ligatures(co, cligs);
947 set_charinfo_remainder(co, 0);
951 /* Read character data */
952 for (k = bc; k <= ec; k++) {
953 store_char_info(k);
954 if (ci._width_index == 0)
955 continue;
956 if (ci._width_index >= nw || ci._height_index >= nh ||
957 ci._depth_index >= nd || ci._italic_index >= ni)
958 tfm_abort;
959 d = ci._remainder;
960 switch (ci._tag) {
961 case lig_tag:
962 if (d >= nl)
963 tfm_abort;
964 break;
965 case ext_tag:
966 if (d >= ne)
967 tfm_abort;
968 break;
969 case list_tag:
970 /* We want to make sure that there is no cycle of characters linked together
971 by |list_tag| entries, since such a cycle would get \TeX\ into an endless
972 loop. If such a cycle exists, the routine here detects it when processing
973 the largest character code in the cycle.
975 check_byte_range(d);
976 while (d < k) { /* |current_character == k| */
977 if (char_tag(f, d) != list_tag)
978 goto NOT_FOUND; /* not a cycle */
979 d = char_remainder(f, d); /* next character on the list */
981 if (d == k)
982 tfm_abort; /* yes, there's a cycle */
983 NOT_FOUND:
984 break;
986 /* put it in the actual font */
987 co = get_charinfo(f, k);
988 set_charinfo_index(co, k);
989 set_charinfo_tag(co, ci._tag);
990 if (ci._tag == ext_tag) {
991 set_charinfo_extensible(co, extens[ci._remainder].b0, /* top */
992 extens[ci._remainder].b2, /* bot */
993 extens[ci._remainder].b1, /* mid */
994 extens[ci._remainder].b3); /* rep */
995 set_charinfo_remainder(co, 0);
996 } else {
997 set_charinfo_remainder(co, ci._remainder);
999 set_charinfo_width(co, widths[ci._width_index]);
1000 set_charinfo_height(co, heights[ci._height_index]);
1001 set_charinfo_depth(co, depths[ci._depth_index]);
1002 set_charinfo_italic(co, italics[ci._italic_index]);
1005 /* first pass: count ligs and kerns */
1007 xligs = xcalloc((unsigned) (ec + 1), sizeof(int));
1008 xkerns = xcalloc((unsigned) (ec + 1), sizeof(int));
1010 for (i = bc; i <= ec; i++) {
1011 if (char_tag(f, i) == lig_tag) {
1012 k = lig_kern_start(f, i);
1013 if (skip_byte(k) > stop_flag)
1014 k = lig_kern_restart(k);
1015 /* now k is the start index */
1016 while (1) {
1017 if (skip_byte(k) <= stop_flag) {
1018 if (op_byte(k) >= kern_flag) { /* kern */
1019 xkerns[i]++;
1020 if (next_char(k) == bchar)
1021 xkerns[i]++;
1022 } else { /* lig */
1023 xligs[i]++;
1024 if (next_char(k) == bchar)
1025 xligs[i]++;
1028 if (skip_byte(k) == 0) {
1029 k++;
1030 } else {
1031 if (skip_byte(k) >= stop_flag)
1032 break;
1033 k += skip_byte(k) + 1;
1039 cligs = NULL;
1040 ckerns = NULL;
1042 for (i = bc; i <= ec; i++) {
1043 fligs = 0;
1044 fkerns = 0;
1045 if (char_tag(f, i) == lig_tag) {
1046 k = lig_kern_start(f, i);
1047 if (skip_byte(k) > stop_flag)
1048 k = lig_kern_restart(k);
1049 /* now k is the start index */
1050 if (xligs[i] > 0)
1051 cligs = xcalloc((unsigned) (xligs[i] + 1), sizeof(liginfo));
1052 if (xkerns[i] > 0)
1053 ckerns = xcalloc((unsigned) (xkerns[i] + 1), sizeof(kerninfo));
1054 while (1) {
1055 if (skip_byte(k) <= stop_flag) {
1056 if (op_byte(k) >= kern_flag) { /* kern */
1057 if (next_char(k) == bchar) {
1058 set_kern_item(ckerns[fkerns], right_boundarychar,
1059 kerns[256 * (op_byte(k) - 128) +
1060 rem_byte(k)]);
1061 fkerns++;
1063 set_kern_item(ckerns[fkerns], next_char(k),
1064 kerns[256 * (op_byte(k) - 128) +
1065 rem_byte(k)]);
1066 fkerns++;
1067 } else { /* lig */
1068 if (next_char(k) == bchar) {
1069 set_ligature_item(cligs[fligs],
1070 (char) (op_byte(k) * 2 + 1),
1071 right_boundarychar, rem_byte(k));
1072 fligs++;
1074 set_ligature_item(cligs[fligs],
1075 (char) (op_byte(k) * 2 + 1),
1076 next_char(k), rem_byte(k));
1077 fligs++;
1080 if (skip_byte(k) == 0) {
1081 k++;
1082 } else {
1083 if (skip_byte(k) >= stop_flag)
1084 break;
1085 k += skip_byte(k) + 1;
1088 if (fkerns > 0 || fligs > 0) {
1089 co = get_charinfo(f, i);
1090 if (fkerns > 0) {
1091 set_kern_item(ckerns[fkerns], end_kern, 0);
1092 fkerns++;
1093 set_charinfo_kerns(co, ckerns);
1095 if (fligs > 0) {
1096 set_ligature_item(cligs[fligs], 0, end_ligature, 0);
1097 fligs++;
1098 set_charinfo_ligatures(co, cligs);
1100 set_charinfo_remainder(co, 0);
1106 /* Make final adjustments and |goto done| */
1108 /* Now to wrap it up, we have checked all the necessary things about the \.{TFM}
1109 file, and all we need to do is put the finishing touches on the data for
1110 the new font.
1113 if (bchar != 65536) {
1114 co = copy_charinfo(char_info(f, bchar));
1115 set_right_boundary(f, co);
1118 tfm_success;