Debian Codegraph @

texk/web2c/luatexdir/font/tfmofm.c

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