another update to the manual (+ some cleanup in libs) (HH)
[luatex.git] / manual / luatex-fonts.tex
blob9a1a7030e5c3edcb493119c883c02d0c1330893a
1 % language=uk
3 \environment luatex-style
4 \environment luatex-logos
6 \startcomponent luatex-fonts
8 \startchapter[reference=fonts,title={Font structure}]
10 All \TEX\ fonts are represented to \LUA\ code as tables, and internally as
11 \CCODE~structures. All keys in the table below are saved in the internal font
12 structure if they are present in the table returned by the \type {define_font}
13 callback, or if they result from the normal \TFM|/|\VF\ reading routines if there
14 is no \type {define_font} callback defined.
16 The column \quote {\VF} means that this key will be created by the \type
17 {font.read_vf()} routine, \quote {\TFM} means that the key will be created by the
18 \type {font.read_tfm()} routine, and \quote{used} means whether or not the
19 \LUATEX\ engine itself will do something with the key.
21 The top|-|level keys in the table are as follows:
23 \starttabulate[|Tl|c|c|c|l|p|]
24 \NC \rmbf key \NC \bf vf \NC \bf tfm \NC \bf used \NC \bf value type \NC \bf description \NC \NR
25 \NC name \NC yes \NC yes \NC yes \NC string \NC metric (file) name \NC \NR
26 \NC area \NC no \NC yes \NC yes \NC string \NC (directory) location, typically empty \NC \NR
27 \NC used \NC no \NC yes \NC yes \NC boolean\NC indicates usage (initial: false) \NC \NR
28 \NC characters \NC yes \NC yes \NC yes \NC table \NC the defined glyphs of this font \NC \NR
29 \NC checksum \NC yes \NC yes \NC no \NC number \NC default: 0 \NC \NR
30 \NC designsize \NC no \NC yes \NC yes \NC number \NC expected size (default: 655360 == 10pt) \NC \NR
31 \NC direction \NC no \NC yes \NC yes \NC number \NC default: 0 \NC \NR
32 \NC encodingbytes \NC no \NC no \NC yes \NC number \NC default: depends on \type {format} \NC \NR
33 \NC encodingname \NC no \NC no \NC yes \NC string \NC encoding name \NC \NR
34 \NC fonts \NC yes \NC no \NC yes \NC table \NC locally used fonts \NC \NR
35 \NC psname \NC no \NC no \NC yes \NC string \NC This is the \POSTSCRIPT\ fontname in the incoming font
36 source, and it's used as fontname identifier in the \PDF\
37 output. \NC \NR
38 \NC fullname \NC no \NC no \NC yes \NC string \NC output font name, used as a fallback in the \PDF\ output
39 if the \type {psname} is not set \NC \NR
40 \NC header \NC yes \NC no \NC no \NC string \NC header comments, if any \NC \NR
41 \NC hyphenchar \NC no \NC no \NC yes \NC number \NC default: \TEX's \type {\hyphenchar} \NC \NR
42 \NC parameters \NC no \NC yes \NC yes \NC hash \NC default: 7 parameters, all zero \NC \NR
43 \NC size \NC no \NC yes \NC yes \NC number \NC loaded (at) size. (default: same as designsize) \NC \NR
44 \NC skewchar \NC no \NC no \NC yes \NC number \NC default: \TEX's \type {\skewchar} \NC \NR
45 \NC type \NC yes \NC no \NC yes \NC string \NC basic type of this font \NC \NR
46 \NC format \NC no \NC no \NC yes \NC string \NC disk format type \NC \NR
47 \NC embedding \NC no \NC no \NC yes \NC string \NC \PDF\ inclusion \NC \NR
48 \NC filename \NC no \NC no \NC yes \NC string \NC the name of the font on disk \NC \NR
49 \NC tounicode \NC no \NC yes \NC yes \NC number \NC When this is set to~1 \LUATEX\ assumes per|-|glyph
50 tounicode entries are present in the font. \NC \NR
51 \NC stretch \NC no \NC no \NC yes \NC number \NC the \quote {stretch} value from \type
52 {\expandglyphsinfont} \NC \NR
53 \NC shrink \NC no \NC no \NC yes \NC number \NC the \quote {shrink} value from \type
54 {\expandglyphsinfont} \NC \NR
55 \NC step \NC no \NC no \NC yes \NC number \NC the \quote {step} value from \type
56 {\expandglyphsinfont} \NC \NR
57 \NC auto_expand \NC no \NC no \NC yes \NC boolean\NC the \quote {autoexpand} keyword from \crlf
58 \type {\expandglyphsinfont} \NC \NR
59 \NC expansion_factor \NC no \NC no \NC no \NC number \NC the actual expansion factor of an expanded font \NC \NR
60 \NC attributes \NC no \NC no \NC yes \NC string \NC the \type {\pdffontattr} \NC \NR
61 \NC cache \NC no \NC no \NC yes \NC string \NC This key controls caching of the \LUA\ table on the
62 \TEX\ end where \type {yes} means: use a reference to
63 the table that is passed to \LUATEX\ (this is the
64 default), and no \type {no} means: don't store the
65 table reference, don't cache any \LUA\ data for this
66 font while \type {renew} means: don't store the table
67 reference, but save a reference to the table that is
68 created at the first access to one of its fields in font.
69 Note: the saved reference is thread|-|local, so be
70 careful when you are using coroutines: an error will be
71 thrown if the table has been cached in one thread, but
72 you reference it from another thread. \NC \NR
73 \NC nomath \NC no \NC no \NC yes \NC boolean\NC This key allows a minor speedup for text fonts. If it
74 is present and true, then \LUATEX\ will not check the
75 character entries for math|-|specific keys. \NC \NR
76 \NC slant \NC no \NC no \NC yes \NC number \NC This has the same semantics as the \type {SlantFont}
77 operator in font map files. \NC \NR
78 \NC extent \NC no \NC no \NC yes \NC number \NC This has the same semantics as the \type {ExtendFont}
79 operator in font map files. \NC \NR
80 \stoptabulate
82 The key \type {name} is always required. The keys \type {stretch}, \type
83 {shrink}, \type {step} and optionally \type {auto_expand} only have meaning when
84 used together: they can be used to replace a post|-|loading \type
85 {\expandglyphsinfont} command. The \type {expansion_factor} is value that can be
86 present inside a font in \type {font.fonts}. It is the actual expansion factor (a
87 value between \type {-shrink} and \type {stretch}, with step \type {step}) of a
88 font that was automatically generated by the font expansion algorithm. The key
89 \type {attributes} can be used to set font attributes in the \PDF\ file. The key
90 \type {used} is set by the engine when a font is actively in use, this makes sure
91 that the font's definition is written to the output file (\DVI\ or \PDF). The
92 \TFM\ reader sets it to false. The \type {direction} is a number signalling the
93 \quote {normal} direction for this font. There are sixteen possibilities:
95 \starttabulate[|Tc|Tc|Tc|Tc|]
96 \NC \rmbf number \NC \rmbf meaning \NC \rmbf number \NC \rmbf meaning \NC\NR
97 \NC 0 \NC LT \NC 8 \NC TT \NC\NR
98 \NC 1 \NC LL \NC 9 \NC TL \NC\NR
99 \NC 2 \NC LB \NC 10 \NC TB \NC\NR
100 \NC 3 \NC LR \NC 11 \NC TR \NC\NR
101 \NC 4 \NC RT \NC 12 \NC BT \NC\NR
102 \NC 5 \NC RL \NC 13 \NC BL \NC\NR
103 \NC 6 \NC RB \NC 14 \NC BB \NC\NR
104 \NC 7 \NC RR \NC 15 \NC BR \NC\NR
105 \stoptabulate
107 These are \OMEGA|-|style direction abbreviations: the first character indicates
108 the \quote {first} edge of the character glyphs (the edge that is seen first in
109 the writing direction), the second the \quote {top} side. Keep in mind that
110 \LUATEX\ has a bit different directional model so these values are not used for
111 anything.
113 The \type {parameters} is a hash with mixed key types. There are seven possible
114 string keys, as well as a number of integer indices (these start from 8 up). The
115 seven strings are actually used instead of the bottom seven indices, because that
116 gives a nicer user interface.
118 The names and their internal remapping are:
120 \starttabulate[|lT|c|]
121 \NC \rmbf name \NC \rmbf remapping \NC\NR
122 \NC slant \NC 1 \NC\NR
123 \NC space \NC 2 \NC\NR
124 \NC space_stretch \NC 3 \NC\NR
125 \NC space_shrink \NC 4 \NC\NR
126 \NC x_height \NC 5 \NC\NR
127 \NC quad \NC 6 \NC\NR
128 \NC extra_space \NC 7 \NC\LR
129 \stoptabulate
131 The keys \type {type}, \type {format}, \type {embedding}, \type {fullname} and
132 \type {filename} are used to embed \OPENTYPE\ fonts in the result \PDF.
134 The \type {characters} table is a list of character hashes indexed by an integer
135 number. The number is the \quote {internal code} \TEX\ knows this character by.
137 Two very special string indexes can be used also: \type {left_boundary} is a
138 virtual character whose ligatures and kerns are used to handle word boundary
139 processing. \type {right_boundary} is similar but not actually used for anything
140 (yet).
142 Other index keys are ignored.
144 Each character hash itself is a hash. For example, here is the character \quote
145 {f} (decimal 102) in the font \type {cmr10 at 10pt}:
147 \starttyping
148 [102] = {
149 ['width'] = 200250,
150 ['height'] = 455111,
151 ['depth'] = 0,
152 ['italic'] = 50973,
153 ['kerns'] = {
154 [63] = 50973,
155 [93] = 50973,
156 [39] = 50973,
157 [33] = 50973,
158 [41] = 50973
160 ['ligatures'] = {
161 [102] = {
162 ['char'] = 11,
163 ['type'] = 0
165 [108] = {
166 ['char'] = 13,
167 ['type'] = 0
169 [105] = {
170 ['char'] = 12,
171 ['type'] = 0
175 \stoptyping
177 The following top|-|level keys can be present inside a character hash:
179 \starttabulate[|lT|c|c|c|l|p|]
180 \NC \rmbf key \NC \bf vf \NC \bf tfm \NC \bf used \NC \bf type \NC \bf description \NC\NR
181 \NC width \NC yes \NC yes \NC yes \NC number \NC character's width, in sp (default 0) \NC\NR
182 \NC height \NC no \NC yes \NC yes \NC number \NC character's height, in sp (default 0) \NC\NR
183 \NC depth \NC no \NC yes \NC yes \NC number \NC character's depth, in sp (default 0) \NC\NR
184 \NC italic \NC no \NC yes \NC yes \NC number \NC character's italic correction, in sp (default zero) \NC\NR
185 \NC top_accent \NC no \NC no \NC maybe \NC number \NC character's top accent alignment place, in sp (default zero) \NC\NR
186 \NC bot_accent \NC no \NC no \NC maybe \NC number \NC character's bottom accent alignment place, in sp (default zero) \NC\NR
187 \NC left_protruding \NC no \NC no \NC maybe \NC number \NC character's \type {\lpcode} \NC\NR
188 \NC right_protruding \NC no \NC no \NC maybe \NC number \NC character's \type {\rpcode} \NC\NR
189 \NC expansion_factor \NC no \NC no \NC maybe \NC number \NC character's \type {\efcode} \NC\NR
190 \NC tounicode \NC no \NC no \NC maybe \NC string \NC character's \UNICODE\ equivalent(s), in \UTF|-|16BE hexadecimal format \NC\NR
191 \NC next \NC no \NC yes \NC yes \NC number \NC the \quote {next larger} character index \NC\NR
192 \NC extensible \NC no \NC yes \NC yes \NC table \NC the constituent parts of an extensible recipe \NC\NR
193 \NC vert_variants \NC no \NC no \NC yes \NC table \NC constituent parts of a vertical variant set \NC \NR
194 \NC horiz_variants \NC no \NC no \NC yes \NC table \NC constituent parts of a horizontal variant set \NC \NR
195 \NC kerns \NC no \NC yes \NC yes \NC table \NC kerning information \NC\NR
196 \NC ligatures \NC no \NC yes \NC yes \NC table \NC ligaturing information \NC\NR
197 \NC commands \NC yes \NC no \NC yes \NC array \NC virtual font commands \NC\NR
198 \NC name \NC no \NC no \NC no \NC string \NC the character (\POSTSCRIPT) name \NC\NR
199 \NC index \NC no \NC no \NC yes \NC number \NC the (\OPENTYPE\ or \TRUETYPE) font glyph index \NC\NR
200 \NC used \NC no \NC yes \NC yes \NC boolean \NC typeset already (default: false)? \NC\NR
201 \NC mathkern \NC no \NC no \NC yes \NC table \NC math cut-in specifications \NC\NR
202 \stoptabulate
204 The values of \type {top_accent}, \type {bot_accent} and \type {mathkern} are
205 used only for math accent and superscript placement, see the \at {math chapter}
206 [math] in this manual for details.
208 The values of \type {left_protruding} and \type {right_protruding} are used only
209 when \type {\protrudechars} is non-zero.
211 Whether or not \type {expansion_factor} is used depends on the font's global
212 expansion settings, as well as on the value of \type {\adjustspacing}.
214 The usage of \type {tounicode} is this: if this font specifies a \type
215 {tounicode=1} at the top level, then \LUATEX\ will construct a \type {/ToUnicode}
216 entry for the \PDF\ font (or font subset) based on the character|-|level \type
217 {tounicode} strings, where they are available. If a character does not have a
218 sensible \UNICODE\ equivalent, do not provide a string either (no empty strings).
220 If the font level \type {tounicode} is not set, then \LUATEX\ will build up \type
221 {/ToUnicode} based on the \TEX\ code points you used, and any character-level
222 \type {tounicodes} will be ignored. The string format is exactly the format that
223 is expected by Adobe \CMAP\ files (\UTF-16BE in hexadecimal encoding), minus the
224 enclosing angle brackets. For instance the \type {tounicode} for a \type {fi}
225 ligature would be \type {00660069}. When you pass a number the conversion will be
226 done for you.
228 The presence of \type {extensible} will overrule \type {next}, if that is also
229 present. It in in turn can be overruled by \type {vert_variants}.
231 The \type {extensible} table is very simple:
233 \starttabulate[|lT|l|p|]
234 \NC \rmbf key \NC \bf type \NC \bf description \NC\NR
235 \NC top \NC number \NC top character index \NC\NR
236 \NC mid \NC number \NC middle character index \NC\NR
237 \NC bot \NC number \NC bottom character index \NC\NR
238 \NC rep \NC number \NC repeatable character index \NC\NR
239 \stoptabulate
241 The \type {horiz_variants} and \type {vert_variants} are arrays of components.
242 Each of those components is itself a hash of up to five keys:
244 \starttabulate[|lT|l|p|]
245 \NC \rmbf key \NC \bf type \NC \bf explanation \NC\NR
246 \NC glyph \NC number \NC The character index. Note that this is an encoding number, not a name. \NC \NR
247 \NC extender \NC number \NC One (1) if this part is repeatable, zero (0) otherwise. \NC \NR
248 \NC start \NC number \NC The maximum overlap at the starting side (in scaled points). \NC \NR
249 \NC end \NC number \NC The maximum overlap at the ending side (in scaled points). \NC \NR
250 \NC advance \NC number \NC The total advance width of this item. It can be zero or missing,
251 then the natural size of the glyph for character \type {component}
252 is used. \NC \NR
253 \stoptabulate
255 The \type {kerns} table is a hash indexed by character index (and \quote
256 {character index} is defined as either a non|-|negative integer or the string
257 value \type {right_boundary}), with the values the kerning to be applied, in
258 scaled points.
260 The \type {ligatures} table is a hash indexed by character index (and \quote
261 {character index} is defined as either a non|-|negative integer or the string
262 value \type {right_boundary}), with the values being yet another small hash, with
263 two fields:
265 \starttabulate[|lT|l|p|]
266 \NC \rmbf key \NC \bf type \NC \bf description \NC \NR
267 \NC type \NC number \NC the type of this ligature command, default 0 \NC \NR
268 \NC char \NC number \NC the character index of the resultant ligature \NC \NR
269 \stoptabulate
271 The \type {char} field in a ligature is required.
273 The \type {type} field inside a ligature is the numerical or string value of one
274 of the eight possible ligature types supported by \TEX. When \TEX\ inserts a new
275 ligature, it puts the new glyph in the middle of the left and right glyphs. The
276 original left and right glyphs can optionally be retained, and when at least one
277 of them is kept, it is also possible to move the new \quote {insertion point}
278 forward one or two places. The glyph that ends up to the right of the insertion
279 point will become the next \quote {left}.
281 \starttabulate[|l|c|l|l|]
282 \NC \bf textual (Knuth) \NC \bf number \NC \bf string \NC result \NC\NR
283 \NC \type{l + r =: n} \NC 0 \NC \type{=:} \NC \type{|n} \NC\NR
284 \NC \type{l + r =:| n} \NC 1 \NC \type{=:|} \NC \type{|nr} \NC\NR
285 \NC \type{l + r |=: n} \NC 2 \NC \type{|=:} \NC \type{|ln} \NC\NR
286 \NC \type{l + r |=:| n} \NC 3 \NC \type{|=:|} \NC \type{|lnr} \NC\NR
287 \NC \type{l + r =:|> n} \NC 5 \NC \type{=:|>} \NC \type{n|r} \NC\NR
288 \NC \type{l + r |=:> n} \NC 6 \NC \type{|=:>} \NC \type{l|n} \NC\NR
289 \NC \type{l + r |=:|> n} \NC 7 \NC \type{|=:|>} \NC \type{l|nr} \NC\NR
290 \NC \type{l + r |=:|>> n} \NC 11 \NC \type{|=:|>>} \NC \type{ln|r} \NC\NR
291 \stoptabulate
293 The default value is~0, and can be left out. That signifies a \quote {normal}
294 ligature where the ligature replaces both original glyphs. In this table the~\type {|}
295 indicates the final insertion point.
297 The \type {commands} array is explained below.
299 \section {Real fonts}
301 Whether or not a \TEX\ font is a \quote {real} font that should be written to the
302 \PDF\ document is decided by the \type {type} value in the top|-|level font
303 structure. If the value is \type {real}, then this is a proper font, and the
304 inclusion mechanism will attempt to add the needed font object definitions to the
305 \PDF. Values for \type {type} are:
307 \starttabulate[|Tl|p|]
308 \NC \rmbf value \NC \rmbf description \NC\NR
309 \NC real \NC this is a base font \NC\NR
310 \NC virtual \NC this is a virtual font \NC\NR
311 \stoptabulate
313 The actions to be taken depend on a number of different variables:
315 \startitemize[packed]
316 \startitem
317 Whether the used font fits in an 8-bit encoding scheme or not.
318 \stopitem
319 \startitem
320 The type of the disk font file.
321 \stopitem
322 \startitem
323 The level of embedding requested.
324 \stopitem
325 \stopitemize
327 A font that uses anything other than an 8-bit encoding vector has to be written
328 to the \PDF\ in a different way.
330 The rule is: if the font table has \type {encodingbytes} set to~2, then this is a
331 wide font, in all other cases it isn't. The value~2 is the default for \OPENTYPE\
332 and \TRUETYPE\ fonts loaded via \LUA. For \TYPEONE\ fonts, you have to set \type
333 {encodingbytes} to~2 explicitly. For \PK\ bitmap fonts, wide font encoding is not
334 supported at all.
336 If no special care is needed, \LUATEX\ currently falls back to the
337 mapfile|-|based solution used by \PDFTEX\ and \DVIPS. This behaviour might
338 silently be removed in the future, in which case the related primitives and \LUA\
339 functions will become no|-|ops.
341 If a \quote {wide} font is used, the new subsystem kicks in, and some
342 extra fields have to be present in the font structure. In this case, \LUATEX\
343 does not use a map file at all.
345 The extra fields are: \type {format}, \type {embedding}, \type {fullname}, \type
346 {cidinfo} (as explained above), \type {filename}, and the \type {index} key in
347 the separate characters.
349 Values for \type {format} are:
351 \starttabulate[|Tl|p|]
352 \NC \rmbf value \NC \rmbf description \NC \NR
353 \NC type1 \NC this is a \POSTSCRIPT\ \TYPEONE\ font \NC \NR
354 \NC type3 \NC this is a bitmapped (\PK) font \NC \NR
355 \NC truetype \NC this is a \TRUETYPE\ or \TRUETYPE|-|based \OPENTYPE\ font \NC \NR
356 \NC opentype \NC this is a \POSTSCRIPT|-|based \OPENTYPE\ font \NC \NR
357 \stoptabulate
359 \type {type3} fonts are provided for backward compatibility only, and do not
360 support the new wide encoding options.
362 Values for \type {embedding} are:
364 \starttabulate[|Tl|p|]
365 \NC \rmbf value \NC \rmbf description \NC \NR
366 \NC no \NC don't embed the font at all \NC \NR
367 \NC subset \NC include and atttempt to subset the font \NC \NR
368 \NC full \NC include this font in its entirety \NC \NR
369 \stoptabulate
371 The other fields are used as follows: The \type {fullname} will be the
372 \POSTSCRIPT|/|\PDF\ font name. The \type {cidinfo} will be used as the character
373 set (the CID \type {/Ordering} and \type {/Registry} keys). The \type {filename}
374 points to the actual font file. If you include the full path in the \type
375 {filename} or if the file is in the local directory, \LUATEX\ will run a little
376 bit more efficient because it will not have to re|-|run the \type {find_xxx_file}
377 callback in that case.
379 Be careful: when mixing old and new fonts in one document, it is possible to
380 create \POSTSCRIPT\ name clashes that can result in printing errors. When this
381 happens, you have to change the \type {fullname} of the font.
383 Typeset strings are written out in a wide format using 2~bytes per glyph, using
384 the \type {index} key in the character information as value. The overall effect
385 is like having an encoding based on numbers instead of traditional (\POSTSCRIPT)
386 name|-|based reencoding. The way to get the correct \type {index} numbers for
387 \TYPEONE\ fonts is by loading the font via \type {fontloader.open} and use the table
388 indices as \type {index} fields.
390 In order to make sure that cut and paste of the final document works okay you can
391 best make sure that there is a \type {tounicode} vector enforced.
393 \section[virtualfonts]{Virtual fonts}
395 You have to take the following steps if you want \LUATEX\ to treat the returned
396 table from \type {define_font} as a virtual font:
398 \startitemize[packed]
399 \startitem
400 Set the top|-|level key \type {type} to \type {virtual}.
401 \stopitem
402 \startitem
403 Make sure there is at least one valid entry in \type {fonts} (see below).
404 \stopitem
405 \startitem
406 Give a \type {commands} array to every character (see below).
407 \stopitem
408 \stopitemize
410 The presence of the toplevel \type {type} key with the specific value \type
411 {virtual} will trigger handling of the rest of the special virtual font fields in
412 the table, but the mere existence of 'type' is enough to prevent \LUATEX\ from
413 looking for a virtual font on its own.
415 Therefore, this also works \quote {in reverse}: if you are absolutely certain
416 that a font is not a virtual font, assigning the value \type {base} or \type
417 {real} to \type {type} will inhibit \LUATEX\ from looking for a virtual font
418 file, thereby saving you a disk search.
420 The \type {fonts} is another \LUA\ array. The values are one- or two|-|key
421 hashes themselves, each entry indicating one of the base fonts in a virtual font.
422 In case your font is referring to itself, you can use the \type {font.nextid()}
423 function which returns the index of the next to be defined font which is probably
424 the currently defined one.
426 An example makes this easy to understand
428 \starttyping
429 fonts = {
430 { name = 'ptmr8a', size = 655360 },
431 { name = 'psyr', size = 600000 },
432 { id = 38 }
434 \stoptyping
436 says that the first referenced font (index 1) in this virtual font is \type
437 {ptrmr8a} loaded at 10pt, and the second is \type {psyr} loaded at a little over
438 9pt. The third one is previously defined font that is known to \LUATEX\ as font id
439 \quote {38}.
441 The array index numbers are used by the character command definitions that are
442 part of each character.
444 The \type {commands} array is a hash where each item is another small array,
445 with the first entry representing a command and the extra items being the
446 parameters to that command. The allowed commands and their arguments are:
448 \starttabulate[|Tl|l|l|p|]
449 \NC \rmbf command name \NC \bf arguments \NC \bf type \NC \bf description \NC\NR
450 \NC font \NC 1 \NC number \NC select a new font from the local \type {fonts} table\NC\NR
451 \NC char \NC 1 \NC number \NC typeset this character number from the current font,
452 and move right by the character's width\NC\NR
453 \NC node \NC 1 \NC node \NC output this node (list), and move right
454 by the width of this list\NC\NR
455 \NC slot \NC 2 \NC number \NC a shortcut for the combination of a font and char command\NC\NR
456 \NC push \NC 0 \NC \NC save current position\NC\NR
457 \NC nop \NC 0 \NC \NC do nothing \NC\NR
458 \NC pop \NC 0 \NC \NC pop position \NC\NR
459 \NC rule \NC 2 \NC 2 numbers \NC output a rule $ht*wd$, and move right.\NC\NR
460 \NC down \NC 1 \NC number \NC move down on the page\NC\NR
461 \NC right \NC 1 \NC number \NC move right on the page\NC\NR
462 \NC special \NC 1 \NC string \NC output a \type {\special} command\NC\NR
463 \NC lua \NC 1 \NC string \NC execute a \LUA\ script (at \type {\latelua} time)\NC\NR
464 \NC image \NC 1 \NC image \NC output an image (the argument can be either an \type
465 {<image>} variable or an \type {image_spec} table)\NC\NR
466 \NC comment \NC any \NC any \NC the arguments of this command are ignored\NC\NR
467 \stoptabulate
469 When a font id is set to~0 then it will be replaced by the currently assigned
470 font id. This prevents the need for hackery with future id's (normally one could
471 use \type {font.nextid} but when more complex fonts are built in the meantime
472 other instances could have been loaded.
474 Here is a rather elaborate glyph commands example:
476 \starttyping
478 commands = {
479 { 'push' }, -- remember where we are
480 { 'right', 5000 }, -- move right about 0.08pt
481 { 'font', 3 }, -- select the fonts[3] entry
482 { 'char', 97 }, -- place character 97 (ASCII 'a')
483 { 'pop' }, -- go all the way back
484 { 'down', -200000 }, -- move upwards by about 3pt
485 { 'special', 'pdf: 1 0 0 rg' } -- switch to red color
486 { 'rule', 500000, 20000 } -- draw a bar
487 { 'special','pdf: 0 g' } -- back to black
490 \stoptyping
492 The default value for \type {font} is always~1 at the start of the
493 \type {commands} array. Therefore, if the virtual font is essentially only a
494 re|-|encoding, then you do usually not have create an explicit \quote {font}
495 command in the array.
497 Rules inside of \type {commands} arrays are built up using only two dimensions:
498 they do not have depth. For correct vertical placement, an extra \type {down}
499 command may be needed.
501 Regardless of the amount of movement you create within the \type {commands}, the
502 output pointer will always move by exactly the width that was given in the \type
503 {width} key of the character hash. Any movements that take place inside the \type
504 {commands} array are ignored on the upper level.
506 \subsection{Artificial fonts}
508 Even in a \quote {real} font, there can be virtual characters. When \LUATEX\
509 encounters a \type {commands} field inside a character when it becomes time to
510 typeset the character, it will interpret the commands, just like for a true
511 virtual character. In this case, if you have created no \quote {fonts} array,
512 then the default (and only) \quote {base} font is taken to be the current font
513 itself. In practice, this means that you can create virtual duplicates of
514 existing characters which is useful if you want to create composite characters.
516 Note: this feature does {\it not\/} work the other way around. There can not be
517 \quote {real} characters in a virtual font! You cannot use this technique for
518 font re-encoding either; you need a truly virtual font for that (because
519 characters that are already present cannot be altered).
521 \subsection{Example virtual font}
523 Finally, here is a plain \TEX\ input file with a virtual font demonstration:
525 \startbuffer
526 \directlua {
527 callback.register('define_font',
528 function (name,size)
529 if name == 'cmr10-red' then
530 f = font.read_tfm('cmr10',size)
531 f.name = 'cmr10-red'
532 f.type = 'virtual'
533 f.fonts = {{ name = 'cmr10', size = size }}
534 for i,v in pairs(f.characters) do
535 if (string.char(i)):find('[tacohanshartmut]') then
536 v.commands = {
537 {'special','pdf: 1 0 0 rg'},
538 {'char',i},
539 {'special','pdf: 0 g'},
541 else
542 v.commands = {{'char',i}}
545 else
546 f = font.read_tfm(name,size)
548 return f
553 \font\myfont = cmr10-red at 10pt \myfont This is a line of text \par
554 \font\myfontx= cmr10 at 10pt \myfontx Here is another line of text \par
555 \stopbuffer
557 \typebuffer
559 \section{The \type {font} library}
561 The font library provides the interface into the internals of the font system,
562 and also it contains helper functions to load traditional \TEX\ font metrics
563 formats. Other font loading functionality is provided by the \type {fontloader}
564 library that will be discussed in the next section.
566 \subsection{Loading a \TFM\ file}
568 The behavior documented in this subsection is considered stable in the sense that
569 there will not be backward-incompatible changes any more.
571 \startfunctioncall
572 <table> fnt =
573 font.read_tfm(<string> name, <number> s)
574 \stopfunctioncall
576 The number is a bit special:
578 \startitemize
579 \startitem
580 If it is positive, it specifies an \quote {at size} in scaled points.
581 \stopitem
582 \startitem
583 If it is negative, its absolute value represents a \quote {scaled}
584 setting relative to the designsize of the font.
585 \stopitem
586 \stopitemize
588 The internal structure of the metrics font table that is returned is explained in
589 \in {chapter} [fonts].
591 \subsection{Loading a \VF\ file}
593 The behavior documented in this subsection is considered stable in the sense that
594 there will not be backward-incompatible changes any more.
596 \startfunctioncall
597 <table> vf_fnt =
598 font.read_vf(<string> name, <number> s)
599 \stopfunctioncall
601 The meaning of the number \type {s} and the format of the returned table are
602 similar to the ones in the \type {read_tfm()} function.
604 \subsection{The fonts array}
606 The whole table of \TEX\ fonts is accessible from \LUA\ using a virtual array.
608 \starttyping
609 font.fonts[n] = { ... }
610 <table> f = font.fonts[n]
611 \stoptyping
613 See \in {chapter} [fonts] for the structure of the tables. Because this is a
614 virtual array, you cannot call \type {pairs} on it, but see below for the \type
615 {font.each} iterator.
617 The two metatable functions implementing the virtual array are:
619 \startfunctioncall
620 <table> f = font.getfont(<number> n)
621 font.setfont(<number> n, <table> f)
622 \stopfunctioncall
624 Note that at the moment, each access to the \type {font.fonts} or call to \type
625 {font.getfont} creates a \LUA\ table for the whole font. This process can be quite
626 slow. In a later version of \LUATEX, this interface will change (it will start
627 using userdata objects instead of actual tables).
629 Also note the following: assignments can only be made to fonts that have already
630 been defined in \TEX, but have not been accessed {\it at all\/} since that
631 definition. This limits the usability of the write access to \type {font.fonts}
632 quite a lot, a less stringent ruleset will likely be implemented later.
634 \subsection{Checking a font's status}
636 You can test for the status of a font by calling this function:
638 \startfunctioncall
639 <boolean> f =
640 font.frozen(<number> n)
641 \stopfunctioncall
643 The return value is one of \type {true} (unassignable), \type {false} (can be
644 changed) or \type {nil} (not a valid font at all).
646 \subsection{Defining a font directly}
648 You can define your own font into \type {font.fonts} by calling this function:
650 \startfunctioncall
651 <number> i =
652 font.define(<table> f)
653 \stopfunctioncall
655 The return value is the internal id number of the defined font (the index into
656 \type {font.fonts}). If the font creation fails, an error is raised. The table
657 is a font structure, as explained in \in {chapter} [fonts].
659 \subsection{Projected next font id}
661 \startfunctioncall
662 <number> i =
663 font.nextid()
664 \stopfunctioncall
666 This returns the font id number that would be returned by a \type {font.define}
667 call if it was executed at this spot in the code flow. This is useful for virtual
668 fonts that need to reference themselves.
670 \subsection{Font id}
672 \startfunctioncall
673 <number> i =
674 font.id(<string> csname)
675 \stopfunctioncall
677 This returns the font id associated with \type {csname} string, or $-1$ if \type
678 {csname} is not defined.
680 \subsection{Currently active font}
682 \startfunctioncall
683 <number> i = font.current()
684 font.current(<number> i)
685 \stopfunctioncall
687 This gets or sets the currently used font number.
689 \subsection{Maximum font id}
691 \startfunctioncall
692 <number> i =
693 font.max()
694 \stopfunctioncall
696 This is the largest used index in \type {font.fonts}.
698 \subsection{Iterating over all fonts}
700 \startfunctioncall
701 for i,v in font.each() do
704 \stopfunctioncall
706 This is an iterator over each of the defined \TEX\ fonts. The first returned
707 value is the index in \type {font.fonts}, the second the font itself, as a \LUA\
708 table. The indices are listed incrementally, but they do not always form an array
709 of consecutive numbers: in some cases there can be holes in the sequence.
711 \stopchapter
713 \stopcomponent