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
/>.
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$
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|
,
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.
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.
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
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|
169 \hang third byte
: |@
!italic_index|
(6 bits
) times
4, plus |@
!tag|
171 \hang fourth byte
: |@
!remainder|
(8 bits
)\par
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
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
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
257 In an \.
{OFM
} file
, eight bytes are used
, two bytes for each field.
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
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
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
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
); \
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; }
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?
*/
375 char
*fname
= luatex_find_file
(nom
, find_font_file_callback
);
378 callback_id
= callback_defined
(read_font_file_callback
);
379 if
(callback_id
> 0) {
381 run_callback
(callback_id
, "S->bSd", fname
, &opened, tfm_buf,
383 if
(res
&& opened && (*tfm_siz > 0)) {
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
);
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
]; \
440 #define store_char_info
(z
) \
441 { if
(font_level
!=-1) { \
442 fget
; read_sixteen_unsigned
(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
); \
453 a
=tfm_buffer
[++tfm_byte
]; \
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
]; \
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
; \
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
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
;
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) {
517 while
(z
>= 0x800000) {
525 d
= (eight_bits
) (sq
% 256);
527 c
= (eight_bits
) (sq
% 256);
529 b
= (eight_bits
) (sq
% 256);
531 a
= (eight_bits
) (sq
% 256);
533 sq
= (sq
+ 1073741824) + 1073741824; /* braces for optimizing compiler
*/
534 d
= (eight_bits
) (sq
% 256);
536 c
= (eight_bits
) (sq
% 256);
538 b
= (eight_bits
) (sq
% 256);
540 a
= (eight_bits
) ((sq
+ 128) % 256);
543 normal_error
("vf", "vf scaling");
544 sw
= (((((d
* z
) >> 8) + (c
* z
)) >> 8) + (b
* z
)) / beta
;
550 normal_error
("vf", "vf scaling");
551 return sw
; /* not reached
, just to make the compiler happy
*/
555 #define check_existence
(z
) \
556 { check_byte_range
(z
); \
557 if
(!char_exists
(f
,z
)) tfm_abort
; \
560 typedef struct tfmcharacterinfo
{
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
;
578 int a
, b
, c
=0, d
=0; /* byte variables
*/
580 int font_level
, header_length
;
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|
*/
590 scaled z
; /* the design size or the ``at'' size
*/
592 char beta
; /* :1.
.16 */
593 int
*xligs
, *xkerns
; /* aux. for ligkern processing
*/
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
*/
618 memset
(&ci, 0, sizeof(tfmcharacterinfo));
620 if
(open_tfm_file
(cnom
, &tfm_buffer, &tfm_size) != 1)
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
*/
634 read_sixteen
(first_two
);
635 if
(first_two
!= 0) {
644 if
((bc
> ec
+ 1) ||
(ec
> 255))
646 if
(bc
> 255) { /* |bc
=256| and |ec
=255|
*/
672 read_sixteen
(font_level
);
679 if
((bc
> ec
+ 1) ||
(ec
> 65535))
681 if
(bc
> 65535) { /* |bc
=65536| and |ec
=65535|
*/
693 read_thirtytwo
(font_dir
); /* junk
*/
697 ncw
= 2 * (ec
- bc
+ 1);
700 (header_length
+ lh
+ ncw
+ nw
+ nh
+ nd
+ ni
+ nlw
+ nk
+ neew
+ np
))
702 if
((nw
== 0) ||
(nh
== 0) ||
(nd
== 0) ||
(ni
== 0))
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
)
712 /* Use size fields to allocate font information
*/
714 set_font_natural_dir
(f
, font_dir
);
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.
*/
733 store_four_bytes
(tmp
);
734 font_checksum
(f
) = (unsigned
) tmp
;
736 read_sixteen
(z
); /* this rejects a negative design size
*/
740 z
= (z
* 16) + (fbyte
>> 4);
748 lh--
; /* ignore the rest of the header
*/
751 /* read the arrays before the character info
*/
753 set_font_dsize
(f
, z
);
755 z
= (s
>= 0 ? s
: xn_over_d
(z
, -s
, 1000));
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$
*/
768 while
(z
>= 040000000) {
770 alpha
= alpha
+ alpha
;
772 beta
= (char
) (256 / alpha
);
774 /* beta cannot be zero
*/
776 normal_error
("vf", "vf reading");
779 /* Read box dimensions
*/
781 for
(k
= 0; k
< nw
; k
++) {
785 if
(widths
[0] != 0) /* \\
{width
}[0] must be zero
*/
787 for
(k
= 0; k
< nh
; k
++) {
792 tfm_abort
; /* \\
{height
}[0] must be zero
*/
793 for
(k
= 0; k
< nd
; k
++) {
798 tfm_abort
; /* \\
{depth
}[0] must be zero
*/
799 for
(k
= 0; k
< ni
; k
++) {
804 tfm_abort
; /* \\
{italic
}[0] must be zero
*/
807 /* Read ligature
/kern program
*/
809 bch_label
= nl
; /* infinity
*/
812 for
(k
= 0; k
< nl
; k
++) {
813 read_four_quarters
(qw
);
816 if
(256 * c
+ d
>= nl
)
818 if
(a
== 255 && k == 0)
822 if
(b
!=bchar
) check_existence
(b
);
826 check_existence
(d
); /* check ligature
*/
828 } else if
(256 * (c
- 128) + d
>= nk
) {
829 tfm_abort
; /* check kern
*/
831 if
((a
< 128) && (k - 0 + a + 1 >= nl))
836 bch_label
= 256 * c
+ d
;
839 /* the actual kerns
*/
840 for
(k
= 0; k
< nk
; k
++) {
845 /* Read extensible character recipes
*/
846 for
(k
= 0; k
< ne
; k
++) {
847 read_four_quarters
(qw
);
851 /* Read font parameters
*/
854 set_font_params
(f
, np
);
856 for
(k
= 1; k
<= np
; k
++) {
857 if
(k
== 1) { /* the |slant| parameter is a pure number
*/
863 sw
= sw
* 256 + fbyte
;
865 sw
= sw
* 256 + fbyte
;
867 sw
= (sw
* 16) + (fbyte
>> 4);
868 set_font_param
(f
, k
, sw
);
870 store_scaled
(font_param
(f
, k
));
874 tfm_byte
= saved_tfm_byte
;
876 /* fix up the left boundary character
*/
879 if
(bch_label
!= nl
) {
882 if
(skip_byte
(k
) > stop_flag
)
883 k
= lig_kern_restart
(k
);
886 if
(skip_byte
(k
) <= stop_flag
) {
887 if
(op_byte
(k
) >= kern_flag
) { /* kern
*/
893 if
(skip_byte
(k
) == 0) {
896 if
(skip_byte
(k
) >= stop_flag
)
898 k
+= skip_byte
(k
) + 1;
902 if
(fkerns
> 0 || fligs
> 0) {
904 cligs
= xcalloc
((unsigned
) (fligs
+ 1), sizeof
(liginfo
));
906 ckerns
= xcalloc
((unsigned
) (fkerns
+ 1), sizeof
(kerninfo
));
911 if
(skip_byte
(k
) > stop_flag
)
912 k
= lig_kern_restart
(k
);
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) +
922 set_ligature_item
(cligs
[fligs
], (char
) (op_byte
(k
) * 2 + 1),
923 next_char
(k
), rem_byte
(k
));
927 if
(skip_byte
(k
) == 0) {
930 if
(skip_byte
(k
) >= stop_flag
)
932 k
+= skip_byte
(k
) + 1;
935 if
(fkerns
> 0 || fligs
> 0) {
936 co
= get_charinfo
(f
, left_boundarychar
);
938 set_kern_item
(ckerns
[fkerns
], end_kern
, 0);
940 set_charinfo_kerns
(co
, ckerns
);
943 set_ligature_item
(cligs
[fligs
], 0, end_ligature
, 0);
945 set_charinfo_ligatures
(co
, cligs
);
947 set_charinfo_remainder
(co
, 0);
951 /* Read character data
*/
952 for
(k
= bc
; k
<= ec
; k
++) {
954 if
(ci._width_index
== 0)
956 if
(ci._width_index
>= nw || ci._height_index
>= nh ||
957 ci._depth_index
>= nd || ci._italic_index
>= ni
)
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.
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
*/
982 tfm_abort
; /* yes
, there's a cycle
*/
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);
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
*/
1017 if
(skip_byte
(k
) <= stop_flag
) {
1018 if
(op_byte
(k
) >= kern_flag
) { /* kern
*/
1020 if
(next_char
(k
) == bchar
)
1024 if
(next_char
(k
) == bchar
)
1028 if
(skip_byte
(k
) == 0) {
1031 if
(skip_byte
(k
) >= stop_flag
)
1033 k
+= skip_byte
(k
) + 1;
1042 for
(i
= bc
; i
<= ec
; i
++) {
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
*/
1051 cligs
= xcalloc
((unsigned
) (xligs
[i
] + 1), sizeof
(liginfo
));
1053 ckerns
= xcalloc
((unsigned
) (xkerns
[i
] + 1), sizeof
(kerninfo
));
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) +
1063 set_kern_item
(ckerns
[fkerns
], next_char
(k
),
1064 kerns
[256 * (op_byte
(k
) - 128) +
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
));
1074 set_ligature_item
(cligs
[fligs
],
1075 (char
) (op_byte
(k
) * 2 + 1),
1076 next_char
(k
), rem_byte
(k
));
1080 if
(skip_byte
(k
) == 0) {
1083 if
(skip_byte
(k
) >= stop_flag
)
1085 k
+= skip_byte
(k
) + 1;
1088 if
(fkerns
> 0 || fligs
> 0) {
1089 co
= get_charinfo
(f
, i
);
1091 set_kern_item
(ckerns
[fkerns
], end_kern
, 0);
1093 set_charinfo_kerns
(co
, ckerns
);
1096 set_ligature_item
(cligs
[fligs
], 0, end_ligature
, 0);
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
1113 if
(bchar
!= 65536) {
1114 co
= copy_charinfo
(char_info
(f
, bchar
));
1115 set_right_boundary
(f
, co
);