% language=us runpath=texruns:manuals/luametatex

\environment luametatex-style

\startcomponent luametatex-fonts

\startchapter[reference=fonts,title={Fonts}]

\startsection[title={Introduction}]

The traditional \TEX\ ligature and kerning routines are build in but anything
more (like \OPENTYPE\ rendering) has to be implemented in \LUA. In \CONTEXT\ we
call the former base mode and the later node mode (we have some more modes). This
conforms to the \LUATEX\ philosophy. When you pass a font to the frontend only
the dimensions matter, as these are used in typesetting, and optionally ligatures
and kerns when you rely on the built|-|in font handler. For math some extra data
is needed, like information about extensibles and next in size glyphs. You can of
course put more information in your \LUA\ tables because when such a table is
passed to \TEX\ only that what is needed is filtered from it.

Because there is no built|-|in backend, virtual font information is not used. If
you want to be compatible you'd better make sure that your tables are okay, and
in that case you can best consult the \LUATEX\ manual. For instance, parameters
like \type {extend} are backend related and the standard \LUATEX\ backend sets
the standard here.

\stopsection

\startsection[title={Defining fonts}]

All \TEX\ fonts are represented to \LUA\ code as tables, and internally as
\CCODE\ structures. All keys in the table below are saved in the internal font
structure if they are present in the table passed to \type {font.define}. When
the callback is set, which is needed for \type {\font} to work, its function
gets the name and size passed, and it has to return a valid font identifier (a
positive number).

For the engine to work well, the following information has to be present at
the font level:

\starttabulate[|l|l|pl|]
\DB key                       \BC value type \BC description \NC \NR
\TB
\NC \type {name}              \NC string     \NC metric (file) name \NC \NR
\NC \type {original}          \NC string     \NC the name used in logging and feedback \NC \NR
\NC \type {designsize}        \NC number     \NC expected size (default: 655360 == 10pt) \NC \NR
\NC \type {size}              \NC number     \NC the required scaling (by default the same as designsize) \NC \NR
\HL
\NC \type {characters}        \NC table      \NC the defined glyphs of this font \NC \NR
\NC \type {fonts}             \NC table      \NC locally used fonts \NC \NR
\NC \type {parameters}        \NC hash       \NC default: 7 parameters, all zero \NC \NR
\HL
\NC \type {stretch}           \NC number     \NC the \quote {stretch} \NC \NR
\NC \type {shrink}            \NC number     \NC the \quote {shrink} \NC \NR
\NC \type {step}              \NC number     \NC the \quote {step} \NC \NR
\HL
\NC \type {textcontrol}       \NC bitset     \NC this controls various code paths in the text engine \NC \NR
\NC \type {hyphenchar}        \NC number     \NC default: \TEX's \prm {hyphenchar} \NC \NR
\HL
\NC \type {skewchar}          \NC number     \NC default: \TEX's \prm {skewchar} \NC \NR
\NC \type {nomath}            \NC boolean    \NC this key allows a minor speedup for text fonts; if it
                                                 is present and true, then \LUATEX\ will not check the
                                                 character entries for math|-|specific keys \NC \NR
\NC \type {oldmath}           \NC boolean    \NC this key flags a font as representing an old school \TEX\
                                                 math font and disables the \OPENTYPE\ code path \NC \NR
\NC \type {mathcontrol}       \NC bitset     \NC this controls various code paths in the math engine,
                                                 like enforcing the traditional code path \NC \NR
\NC \type {compactmath}       \NC boolean    \NC experimental: use the smaller chain to locate a character \NC \NR
\NC \type {textscale}         \NC number     \NC scale applied to math text \NC \NR
\NC \type {scriptscale}       \NC number     \NC scale applied to math script \NC \NR
\NC \type {scriptscriptscale} \NC number     \NC scale applied to math script script \NC \NR
\LL
\stoptabulate

The \type {parameters} is a hash with mixed key types. There are seven possible
string keys, as well as a number of integer indices (these start from 8 up). The
seven strings are actually used instead of the bottom seven indices, because that
gives a nicer user interface.

The names and their internal remapping are:

\starttabulate[|l|c|]
\DB name                  \BC remapping \NC \NR
\TB
\NC \type {slant}        \NC 1 \NC \NR
\NC \type {space}        \NC 2 \NC \NR
\NC \type {spacestretch} \NC 3 \NC \NR
\NC \type {spaceshrink}  \NC 4 \NC \NR
\NC \type {xheight}      \NC 5 \NC \NR
\NC \type {quad}         \NC 6 \NC \NR
\NC \type {extraspace}   \NC 7 \NC \NR
\LL
\stoptabulate

The \type {characters} table is a \LUA\ hash table where the keys are integers.
When a character in the input is turned into a glyph node, it gets a character
code that normally refers to an entry in that table. For proper paragraph
building and math rendering the following fields can be present in an entry in
the \type {characters} table. You can of course add all kind of extra fields. The
engine only uses those that it needs for typesetting a paragraph or formula. The
subtables that define ligatures and kerns are also hashes with integer keys, and
these indices should point to entries in the main characters table.

Providing ligatures and kerns this way permits \TEX\ to construct ligatures and
add inter|-|character kerning. However, normally you will use an \OPENTYPE\ font
in combination with \LUA\ code that does this. In \CONTEXT\ we have base mode
that uses the engine, and node mode that uses \LUA. A monospaced font normally
has no ligatures and kerns and is normally not processed at all.

We can group the parameters. All characters have the following base set. It must
be noted here that \OPENTYPE\ doesn't have a italic property and that the height
and depth are also not part of the design: one can choose to derive them from the
bounding box.

\starttabulate[|l|l|pl|]
\DB key                     \BC type    \BC description \NC\NR
\TB
\NC \type {width}           \NC number  \NC width in sp (default 0) \NC\NR
\NC \type {height}          \NC number  \NC height in sp (default 0) \NC\NR
\NC \type {depth}           \NC number  \NC depth in sp (default 0) \NC\NR
\NC \type {italic}          \NC number  \NC italic correction in sp (default 0) \NC\NR
\stoptabulate

Then there are three parameters that are more optional and relate to advanced
optical paragraph optimization:

\starttabulate[|l|l|pl|]
\DB key                     \BC type    \BC description \NC\NR
\TB
\NC \type {leftprotruding}  \NC number  \NC left protruding factor (\prm {lpcode}) \NC\NR
\NC \type {rightprotruding} \NC number  \NC right protruding factor (\prm {rpcode}) \NC\NR
\NC \type {expansion}       \NC number  \NC expansion factor (\prm {efcode}) \NC\NR
\stoptabulate

From \TEX\ we inherit the following tables. Ligatures are only used in so call
base mode, when the engine does the font magic. Kerns are used in text and
optionally in math: More details follow below.

\starttabulate[|l|l|pl|]
\DB key                     \BC type    \BC description \NC\NR
\TB
\NC \type {ligatures}       \NC table   \NC ligaturing information \NC\NR
\NC \type {kerns}           \NC table   \NC kerning information \NC\NR
\stoptabulate

The next two fields control the engine and are a variant on \TEX's \TFM\ tag
property. In a future we might provide a bit more (local) control although
currently we see no need. Originally the tag and next field were combined into a
packed integer but in current \LUAMETATEX\ we have a 32 bit tag and the next
field moved to the math blob as it only is used as variant selector.

\starttabulate[|l|l|pl|]
\DB key                     \BC type    \BC description \NC\NR
\TB
\NC \type {tag}             \NC number  \NC a bitset, currently not really exposed \NC\NR
%NC \type {reserved}        \NC number  \NC note for myself \NC \NR
\stoptabulate

In a math font characters have many more fields.

\starttabulate[|l|l|pl|]
\DB key                      \BC type    \BC description \NC\NR
\TB
\NC \type {smaller}          \NC number  \NC the next smaller math size character \NC\NR
\NC \type {mirror}           \NC number  \NC a right to left alternative \NC\NR
\NC \type {flataccent}       \NC number  \NC an accent alternative with less height (\OPENTYPE) \NC\NR
\NC \type {next}             \NC number  \NC \quote {next larger} character index \NC\NR
\HL
\NC \type {topleft}          \NC number  \NC alternative script kern \NC\NR
\NC \type {topright}         \NC number  \NC alternative script kern \NC\NR
\NC \type {bottomleft}       \NC number  \NC alternative script kern \NC\NR
\NC \type {bottomright}      \NC number  \NC alternative script kern \NC\NR
\HL
\NC \type {topmargin}        \NC number  \NC alternative accent calculation margin \NC\NR
\NC \type {bottomargin}      \NC number  \NC alternative accent calculation margin \NC\NR
\NC \type {leftmargin}       \NC number  \NC alternative accent calculation margin \NC\NR
\NC \type {rightmargin}      \NC number  \NC alternative accent calculation margin \NC\NR
\HL
\NC \type {topovershoot}     \NC number  \NC accent width tolerance \NC\NR
\NC \type {bottomovershoot}  \NC number  \NC accent width tolerance \NC\NR
\HL
\NC \type {topanchor}        \NC number  \NC horizontal top accent alignment position \NC\NR
\NC \type {bottomanchor}     \NC number  \NC horizontal bottom accent alignment position \NC\NR
\HL
\NC \type {innerlocation}    \NC string  \NC \type {left} or \type {right}  \NC\NR
\NC \type {innerxoffset}     \NC number  \NC radical degree horizontal position \NC\NR
\NC \type {inneryoffset}     \NC number  \NC radical degree vertical position \NC\NR
\HL
\NC \type {parts}            \NC table   \NC constituent parts of an extensible \NC \NR
\NC \type {partsitalic}      \NC number  \NC the italic correction applied with the extensible \NC \NR
\NC \type {partsorientation} \NC number  \NC \type {horizontal} or \type {vertical} \NC\NR
\HL
\NC \type {mathkerns}        \NC table   \NC math cut-in specifications \NC\NR
\HL
\NC \type {extensible}       \NC table   \NC stretch a fixed width accent to fit\NC\NR
\LL
\stoptabulate

Now some more details follow. For example, here is the character \quote {f}
(decimal 102) in the font \type {cmr10 at 10pt}. The numbers that represent
dimensions are in scaled points. Of course you will use Latin Modern \OPENTYPE\
instead but the principles are the same:

\starttyping
[102] = {
    ["width"]  = 200250,
    ["height"] = 455111,
    ["depth"]  = 0,
    ["italic"] = 50973,
    ["kerns"]  = {
        [63] = 50973,
        [93] = 50973,
        [39] = 50973,
        [33] = 50973,
        [41] = 50973
    },
    ["ligatures"] = {
        [102] = { ["char"] = 11, ["type"] = 0 },
        [108] = { ["char"] = 13, ["type"] = 0 },
        [105] = { ["char"] = 12, ["type"] = 0 }
    }
}
\stoptyping

Two very special string indexes can be used also: \type {leftboundary} is a
virtual character whose ligatures and kerns are used to handle word boundary
processing. \type {rightboundary} is similar but not actually used for anything
(yet).

The values of \type {leftprotrusion} and \type {rightprotrusion} are used only
when \prm {protrudechars} is non-zero. Whether or not \type {expansion} is used
depends on the font's global expansion settings, as well as on the value of \prm
{adjustspacing}.

The values of \type {topanchor}, \type {bottomanchor} and \type {mathkern} are
used only for math accent and superscript placement, see \at {page} [math] in
this manual for details. The italic corrections are a story in themselves and
discussed in detail in other manuals. The additional parameters that deal with
kerns, margins, overshoots, inner anchoring, etc. are engine specific and not
part of \OPENTYPE. More information can be found in the \CONTEXT\ distribution;
they relate the upgraded math engine project by Mikael and Hans.

A math character can have a \type {next} field that points to a next larger
shape. However, the presence of \type {extensible} will overrule \type {next}, if
that is also present. The \type {extensible} field in turn can be overruled by
\type {parts}, the \OPENTYPE\ version. The \type {extensible} table is very
simple:

\starttabulate[|l|l|p|]
\DB key        \BC type   \BC description                \NC\NR
\TB
\NC \type{top} \NC number \NC top character index        \NC\NR
\NC \type{mid} \NC number \NC middle character index     \NC\NR
\NC \type{bot} \NC number \NC bottom character index     \NC\NR
\NC \type{rep} \NC number \NC repeatable character index \NC\NR
\LL
\stoptabulate

The \type {parts} entru is an arrays of components. Each of those components is
itself a hash of up to five keys:

\starttabulate[|l|l|p|]
\DB key             \BC type   \BC explanation \NC \NR
\TB
\NC \type{glyph}    \NC number \NC The character index. Note that this is an encoding number, not a name. \NC \NR
\NC \type{extender} \NC number \NC One (1) if this part is repeatable, zero (0) otherwise. \NC \NR
\NC \type{start}    \NC number \NC The maximum overlap at the starting side (in scaled points). \NC \NR
\NC \type{end}      \NC number \NC The maximum overlap at the ending side (in scaled points). \NC \NR
\NC \type{advance}  \NC number \NC The total advance width of this item. It can be zero or missing,
                                   then the natural size of the glyph for character \type {component}
                                   is used. \NC \NR
\LL
\stoptabulate

The traditional (text and math) \type {kerns} table is a hash indexed by
character index (and \quote {character index} is defined as either a
non|-|negative integer or the string value \type {rightboundary}), with the
values of the kerning to be applied, in scaled points.

The traditional (text) \type {ligatures} table is a hash indexed by character
index (and \quote {character index} is defined as either a non|-|negative integer
or the string value \type {rightboundary}), with the values being yet another
small hash, with two fields:

\starttabulate[|l|l|p|]
\DB key         \BC type   \BC description \NC \NR
\TB
\NC \type{type} \NC number \NC the type of this ligature command, default 0 \NC \NR
\NC \type{char} \NC number \NC the character index of the resultant ligature \NC \NR
\LL
\stoptabulate

The \type {char} field in a ligature is required. The \type {type} field inside a
ligature is the numerical or string value of one of the eight possible ligature
types supported by \TEX. When \TEX\ inserts a new ligature, it puts the new glyph
in the middle of the left and right glyphs. The original left and right glyphs
can optionally be retained, and when at least one of them is kept, it is also
possible to move the new \quote {insertion point} forward one or two places. The
glyph that ends up to the right of the insertion point will become the next
\quote {left}.

\starttabulate[|l|c|l|l|]
\DB textual (Knuth)       \BC number \BC string        \BC result      \NC\NR
\TB
\NC \type{l + r =: n}     \NC 0      \NC \type{=:}     \NC \type{|n}   \NC\NR
\NC \type{l + r =:| n}    \NC 1      \NC \type{=:|}    \NC \type{|nr}  \NC\NR
\NC \type{l + r |=: n}    \NC 2      \NC \type{|=:}    \NC \type{|ln}  \NC\NR
\NC \type{l + r |=:| n}   \NC 3      \NC \type{|=:|}   \NC \type{|lnr} \NC\NR
\NC \type{l + r  =:|> n}  \NC 5      \NC \type{=:|>}   \NC \type{n|r}  \NC\NR
\NC \type{l + r |=:> n}   \NC 6      \NC \type{|=:>}   \NC \type{l|n}  \NC\NR
\NC \type{l + r |=:|> n}  \NC 7      \NC \type{|=:|>}  \NC \type{l|nr} \NC\NR
\NC \type{l + r |=:|>> n} \NC 11     \NC \type{|=:|>>} \NC \type{ln|r} \NC\NR
\LL
\stoptabulate

The default value is~0, and can be left out. That signifies a \quote {normal}
ligature where the ligature replaces both original glyphs. In this table the~\type {|}
indicates the final insertion point.

% The \type {mathcontrol} bitset is mostly there for experimental purposes. Because
% there is inconsistency in the \OPENTYPE\ math fonts with respect to for instance
% glyph dimensions, it is possible to force the traditional code path. We just mention
% the possible flags:
%
% \startluacode
%     context.starttabulate { "|||" }
%     context.DB() context("value") context.BC() context("effect") context.NC() context.NR()
%     context.TB()
%     for k, v in table.sortedhash(tex.getmathcontrolvalues()) do
%         context.NC() context("0x%04X",k) context.NC() context(v) context.NC() context.NR()
%     end
%     context.LL()
%     context.stoptabulate()
% \stopluacode

Compact math is an experimental feature. The smaller field in a character
definition of a text character can point to a script character that itself can
point to a scriptscript one. When set the \type {textscale}, \type {scriptscale}
and \type {scriptscriptscale} is applied to those.

Bidirectional math is also experimental and driven by (in \CONTEXT\ speak) tweaks
which means that it has to be set up explicitly as it uses a combination of
fonts. In \CONTEXT\ is also uses specific features of the font subsystems that
hook into the backend where we have a more advanced virtual font subsystem than
in \LUATEX. Because this is macro package dependent it will not be discussed
here.

% The \type {textcontrol} field is used to control some aspects of text processing.
% More options might be added in the future.

% \startluacode
%     context.starttabulate { "|||" }
%     context.DB() context("value") context.BC() context("effect") context.NC() context.NR()
%     context.TB()
%     for k, v in table.sortedhash(tex.gettextcontrolvalues()) do
%         context.NC() context("0x%04X",k) context.NC() context(v) context.NC() context.NR()
%     end
%     context.LL()
%     context.stoptabulate()
% \stopluacode
%
% In \CONTEXT\ these are interfaced via pseudo features. The math control flags of
% a font can be overloaded by \prm {mathcontrolmode} on the spot and the set
% controls of a font can be queried by \prm {fontmathcontrol}. The text control
% flags in a font always win over the ones set by other parameters, like \prm
% {hyphenationmode}. They can be queried with \prm {fonttextcontrol}.

\stopsection

\startsection[reference=virtualfonts,title={Virtual fonts}]

% \topicindex {fonts+virtual}

Virtual fonts have been introduced to overcome limitations of good old \TEX. They
were mostly used for providing a direct mapping from for instance accented
characters onto a glyph. The backend was responsible for turning a reference to a
character slot into a real glyph, possibly constructed from other glyphs. In our
case there is no backend so there is also no need to pass this information
through \TEX. But it can of course be part of the font information and because it is
a kind of standard, we describe it here.

A character is virtual when it has a \type {commands} array as part of the data.
A virtual character can itself point to virtual characters but be careful with
nesting as you can create loops and overflow the stack (which often indicates an
error anyway).

At the font level there can be a an (indexed) \type {fonts} table. The values are
one- or two|-|key hashes themselves, each entry indicating one of the base fonts
in a virtual font. In case your font is referring to itself in for instance a
virtual font, you can use the \type {slot} command with a zero font reference,
which indicates that the font itself is used. So, a table looks like this:

\starttyping
fonts = {
  { name = "ptmr8a", size = 655360 },
  { name = "psyr", size = 600000 },
  { id = 38 }
}
\stoptyping

The first referenced font (at index~1) in this virtual font is \type {ptrmr8a}
loaded at 10pt, and the second is \type {psyr} loaded at a little over 9pt. The
third one is a previously defined font that is known to \LUATEX\ as font id~38.
The array index numbers are used by the character command definitions that are
part of each character.

The \type {commands} array is a hash where each item is another small array, with
the first entry representing a command and the extra items being the parameters
to that command. The frontend is only interested in the dimensions, ligatures and
kerns of a font, which is the reason why the \TEX\ engine didn't have to be
extended when virtual fonts showed up: dealing with it is up to the driver that
comes after the backend. In \PDFTEX\ and \LUATEX\ that driver is integrated so
there the backend also deals with virtual fonts. The first block in the next
table is what the standard mentions. The \type {special} command is indeed
special because it is an extension container. The mentioned engines only support
pseudo standards where the content starts with \type {pdf:}. The last block is
\LUATEX\ specific and will not be found in native fonts. These entries can be
used in virtual fonts that are constructed in \LUA.

But \unknown\ in \LUAMETATEX\ there is no backend built in but we might assume
that the one provided deals with these entries. However, a provided backend can
provide more and that is indeed what happens in \CONTEXT. There, because we no
longer have compacting (of passed tables) and unpacking (when embedding) of these
tables going on we stay in the \LUA\ domain. None of the virtual specification is
ever seen in the engine.

\starttabulate[|l|l|l|p|]
\DB command        \BC arguments \BC type      \BC description \NC \NR
\TB
\NC \type{font}    \NC 1         \NC number    \NC select a new font from the local \type {fonts} table \NC \NR
\NC \type{char}    \NC 1         \NC number    \NC typeset this character number from the current font,
                                                   and move right by the character's width \NC \NR
\NC \type{push}    \NC 0         \NC           \NC save current position\NC \NR
\NC \type{pop}     \NC 0         \NC           \NC pop position \NC \NR
\NC \type{rule}    \NC 2         \NC 2 numbers \NC output a rule $ht*wd$, and move right. \NC \NR
\NC \type{down}    \NC 1         \NC number    \NC move down on the page \NC \NR
\NC \type{right}   \NC 1         \NC number    \NC move right on the page \NC \NR
\HL
\NC \type{special} \NC 1         \NC string    \NC output a driver directive \NC \NR
\HL
\NC \type{nop}     \NC 0         \NC           \NC do nothing \NC \NR
\NC \type{slot}    \NC 2         \NC 2 numbers \NC a shortcut for the combination of a font and char command\NC \NR
\NC \type{node}    \NC 1         \NC node      \NC output this node (list), and move right
                                                   by the width of this list\NC \NR
\NC \type{pdf}     \NC 2         \NC 2 strings \NC output a \PDF\ literal, the first string is one of \type {origin},
                                                   \type {page}, \type {text}, \type {font}, \type {direct} or \type {raw}; if you
                                                   have one string only \type {origin} is assumed \NC \NR
\NC \type{lua}     \NC 1         \NC string,
                                     function  \NC execute a \LUA\ script when the glyph is embedded; in case of a
                                                   function it gets the font id and character code passed \NC \NR
\NC \type{image}   \NC 1         \NC image     \NC depends on the backend \NC \NR
\NC \type{comment} \NC any       \NC any       \NC the arguments of this command are ignored \NC \NR
\LL
\stoptabulate

When a font id is set to~0 then it will be replaced by the currently assigned
font id. This prevents the need for hackery with future id's.

The \type {pdf} option also accepts a \type {mode} keyword in which case the
third argument sets the mode. That option will change the mode in an efficient
way (passing an empty string would result in an extra empty lines in the \PDF\
file. This option only makes sense for virtual fonts. The \type {font} mode only
makes sense in virtual fonts. Modes are somewhat fuzzy and partially inherited
from \PDFTEX.

\starttabulate[|l|p|]
\DB mode           \BC description \NC \NR
\TB
\NC \type {origin} \NC enter page mode and set the position \NC \NR
\NC \type {page}   \NC enter page mode \NC \NR
\NC \type {text}   \NC enter text mode \NC \NR
\NC \type {font}   \NC enter font mode (kind of text mode, only in virtual fonts) \NC \NR
\NC \type {always} \NC finish the current string and force a transform if needed \NC \NR
\NC \type {raw}    \NC finish the current string \NC \NR
\LL
\stoptabulate

You always need to check what \PDF\ code is generated because there can be all
kind of interferences with optimization in the backend and fonts are complicated
anyway. Here is a rather elaborate glyph commands example using such keys:

\starttyping
...
commands = {
    { "push" },                     -- remember where we are
    { "right", 5000 },              -- move right about 0.08pt
    { "font", 3 },                  -- select the fonts[3] entry
    { "char", 97 },                 -- place character 97 (ASCII 'a')
 -- { "slot", 2, 97 },              -- an alternative for the previous two
    { "pop" },                      -- go all the way back
    { "down", -200000 },            -- move upwards by about 3pt
    { "special", "pdf: 1 0 0 rg" }  -- switch to red color
 -- { "pdf", "origin", "1 0 0 rg" } -- switch to red color (alternative)
    { "rule", 500000, 20000 }       -- draw a bar
    { "special", "pdf: 0 g" }       -- back to black
 -- { "pdf", "origin", "0 g" }      -- back to black (alternative)
}
...
\stoptyping

The default value for \type {font} is always~1 at the start of the
\type {commands} array. Therefore, if the virtual font is essentially only a
re|-|encoding, then you do usually not have created an explicit \quote {font}
command in the array.

Rules inside of \type {commands} arrays are built up using only two dimensions:
they do not have depth. For correct vertical placement, an extra \type {down}
command may be needed.

Regardless of the amount of movement you create within the \type {commands}, the
output pointer will always move by exactly the width that was given in the \type
{width} key of the character hash. Any movements that take place inside the \type
{commands} array are ignored on the upper level.

The special can have a \type {pdf:}, \type {pdf:origin:},  \type {pdf:page:},
\type {pdf:direct:} or  \type {pdf:raw:} prefix. When you have to concatenate
strings using the \type {pdf} command might be more efficient.

{\em For the record: in \CONTEXT\ \LMTX\ we no longer support the \type {pdf}, \type
{image} and \type {special} keywords.}

\stopsection

\startsection[title={Additional \TEX\ commands}]

\startsubsection[title={Font syntax}]

\topicindex {fonts}

\LUATEX\ will accept a braced argument as a font name:

\starttyping
\font\myfont = {cmr10}
\stoptyping

This allows for embedded spaces, without the need for double quotes. Macro
expansion takes place inside the argument.

\stopsubsection

\startsubsection[title={\prm {fontid}, \prm {setfontid} and \prm {mathstylefontid}}]

\startsyntax
\fontid\font
\stopsyntax

This primitive expands into a number. The currently used font id is
\number\fontid\font. Here are some more: \footnote {Contrary to \LUATEX\ this is
now a number so you need to use \prm {number} or \prm {the}. The same is true
for some other numbers and dimensions that for some reason ended up in the
serializer that produced a sequence of tokens.}

\starttabulate[|l|c|c|]
\DB style \BC command \BC font id \NC \NR
\TB
\NC normal      \NC \type {\tf} \NC \tf \number\fontid\font \NC \NR
\NC bold        \NC \type {\bf} \NC \bf \number\fontid\font \NC \NR
\NC italic      \NC \type {\it} \NC \it \number\fontid\font \NC \NR
\NC bold italic \NC \type {\bi} \NC \bi \number\fontid\font \NC \NR
\LL
\stoptabulate

These numbers depend on the macro package used because each one has its own way
of dealing with fonts. They can also differ per run, as they can depend on the
order of loading fonts. For instance, when in \CONTEXT\ virtual math \UNICODE\
fonts are used, we can easily get over a hundred ids in use. Not all ids have to
be bound to a real font, after all it's just a number.

The primitive \prm {setfontid} can be used to enable a font with the given id,
which of course needs to be a valid one.

In math mode the font id depends on the style because there we have a family of
three related fonts. In this document we get the following identifiers:

\starttabulate[|l|c|]
\NC \type {$ \the \mathstylefontid \scriptscriptstyle \fam$} \NC $\the\mathstylefontid\scriptscriptstyle \fam$ \NC \NR
\NC \type {$ \the \mathstylefontid \scriptstyle       \fam$} \NC $\the\mathstylefontid\scriptstyle       \fam$ \NC \NR
\NC \type {$ \the \mathstylefontid \textstyle         \fam$} \NC $\the\mathstylefontid\textstyle         \fam$ \NC \NR
\stoptabulate

\stopsubsection

\startsubsection[title={\prm {fontspecifiedname} and \prm {fontspecifiedsize}}]

These two primitives provide some details about the given font:

\startbuffer
{\tf [\fontspecifiedname\font] [\the\fontspecifiedsize\font]}
{\bf [\fontspecifiedname\font] [\the\fontspecifiedsize\font]}
{\it [\fontspecifiedname\font] [\the\fontspecifiedsize\font]}
\stopbuffer

\typebuffer

So for this document we get:

\startlines
\getbuffer
\stoplines

Of course this also depends on the macro package because that is responsible for
implementing font support and because all that is driven by callbacks the
reported name doesn't even have to resemble a font.

\stopsubsection

\startsubsection[title={\prm {glyphoptions}}]

\topicindex {ligatures+suppress}
\topicindex {kerns+suppress}
\topicindex {expansion+suppress}
\topicindex {protrusion+suppress}

In \LUATEX\ the \type {\noligs} and \type {\nokerns} primitives suppress these
features but in \LUAMETATEX\ these primitives are gone. They are replace by a more
generic control primitive \prm {glyphoptions}. This numerical parameter is a
bitset with the following fields:

% todo: use values

\starttabulate[|l|pl|]
\DB value       \BC effect \NC\NR
\TB
\NC \type{0x01} \NC prevent left ligature             \NC \NR
\NC \type{0x02} \NC prevent right ligature            \NC \NR
\NC \type{0x04} \NC block left kern                   \NC \NR
\NC \type{0x08} \NC block right kern                  \NC \NR
\NC \type{0x10} \NC don't apply  expansion            \NC \NR
\NC \type{0x20} \NC don't apply protrusion            \NC \NR
\NC \type{0x40} \NC apply xoffset to width            \NC \NR
\NC \type{0x80} \NC apply yoffset to height and depth \NC \NR
\LL
\stoptabulate

The effects speak for themselves. They provide detailed control over individual
glyph, this because the current value of this option is stored with glyphs.

\stopsubsection

\startsubsection[title={\prm {glyphscale}, \prm {glyphxscale}, \prm {glyphyscale} and \prm {scaledfontdimen}}]

The three scale parameters control the current scaling. They are traditional
\TEX\ integer parameters that operate independent of each other. The scaling is
reflected in the dimensions of glyphs as well as in the related font dimensions,
which means that units like \type {ex} and \type {em} work as expected. If you
query a font dimensions with \prm {fontdimen} you get the raw value but with \prm
{scaledfontdimen} you get the useable value.

\stopsubsection

\startsubsection[title={\prm {glyphxscaled}, \prm {glyphyscaled}}]

These two relate to the previous one:

\startbuffer
{\glyphxscale 1500 \the\glyphxscaled 100pt} and
{\glyphyscale  750 \the\glyphyscaled 100pt}
\stopbuffer

\typebuffer

We get: \inlinebuffer.

\stopsubsection

\startsubsection[title={Scaled fontdimensions}]

When you use \prm {glyphscale},  \prm {glyphxscale} and|/|or  \prm {glyphyscale} the font
dimensions

\starttabulate[|l|c|c|c|]
\DB dimension                     \BC scale \NC xscale \NC yscale \NC\NR
\TB
\NC \prm {scaledemwidth}          \NC \star \NC \star  \NC        \NC\NR
\NC \prm {scaledexheight}         \NC \star \NC        \NC \star  \NC\NR
\NC \prm {scaledextraspace}       \NC \star \NC \star  \NC        \NC\NR
\NC \prm {scaledinterwordshrink}  \NC \star \NC \star  \NC        \NC\NR
\NC \prm {scaledinterwordspace}   \NC \star \NC \star  \NC        \NC\NR
\NC \prm {scaledinterwordstretch} \NC \star \NC \star  \NC        \NC\NR
\NC \prm {scaledslantperpoint}    \NC \star \NC \star  \NC        \NC\NR
\LL
\stoptabulate

The next table shows the effective sized when we scale by 2000. The last two
columns scale twice: the shared scale and the x or y scale.

\def\ShowThem#1%
  {\normalexpanded{
   \BC \small \prm {\csstring\parametermark1}
   \NC {\localcontrolled{\glyphscale2000 \glyphxscale1000 \glyphyscale 1000} \withoutpt\parametermark1}
   \NC {\localcontrolled{\glyphscale1000 \glyphxscale2000 \glyphyscale 1000} \withoutpt\parametermark1}
   \NC {\localcontrolled{\glyphscale1000 \glyphxscale1000 \glyphyscale 2000} \withoutpt\parametermark1}
   \NC {\localcontrolled{\glyphscale2000 \glyphxscale2000 \glyphyscale 1000} \withoutpt\parametermark1}
   \NC {\localcontrolled{\glyphscale2000 \glyphxscale1000 \glyphyscale 2000} \withoutpt\parametermark1}
   \NC \NR}}

\starttabulate[|l|c{.}|c{.}|c{.}|c{.}|c{.}|]
    \ShowThem\scaledemwidth
    \ShowThem\scaledexheight
    \ShowThem\scaledextraspace
    \ShowThem\scaledinterwordshrink
    \ShowThem\scaledinterwordspace
    \ShowThem\scaledinterwordstretch
    \ShowThem\scaledslantperpoint
\stoptabulate

\startsubsection[title={\prm {fontspecdef}, \prm {fontspecid}, \prm {fontspecscale}, , \prm {fontspecxscale}, \prm {fontspecyscale}}]

Because we have three scale related primitives \prm {glyphscale}, \prm
{glyphxscale} and \prm {glyphyscale}, we also have a way to quickly set them all.

\starttyping
\fontspecdef \MyFontA 2 all 1000
\fontspecdef \MyFontB \MyFontA xscale 1200
\stoptyping

The defined control sequence will set the font id (which is 2 in the case of
\type {\MyFontA}) as well as the scale(s). Possible keywords are \type {scale},
\type {xscale}, \type {yscale} and \type {all}. By default the values are 1000.
Instead of an id an already defined specification can be given in which case we
start from a copy. This mechanism is still somewhat experimental and might
evolve. The main reason for introducing it is that it gives less tracing.

Say that we have:

\startbuffer
\fontspecdef\MyFoo\font xscale 1200 \relax
\stopbuffer

\typebuffer \getbuffer

The four properties of such a specification can then be queried as follows:

\startbuffer
[\the\fontspecid    \MyFoo]
[\the\fontspecscale \MyFoo]
[\the\fontspecxscale\MyFoo]
[\the\fontspecyscale\MyFoo]
\stopbuffer

\typebuffer \getbuffer

A font specification obeys grouping but is not a register. Like \prm {integerdef}
and \prm {dimendef} it is just a control sequence with a special meaning.

% \the\fontspecifiedsize\font
%     \fontspecifiedname\font

\stopsubsection

\startsubsection[title={\prm {glyphxoffset}, \prm {glyphyoffset}}]

These two parameters control the horizontal and vertical shift of glyphs with,
when applied to a stretch of them, the horizontal offset probably being the least
useful.

\stopsubsection

\startsubsection[title={\prm {glyph}}]

This command is a variation in \prm {char} that takes keywords:

\starttabulate[|l|p|]
\DB keyword \BC effect \NC type \NC \NR
\TB
\NC \type {xoffset} \NC (virtual) horizontal shift \NC dimension  \NC \NR
\NC \type {yoffset} \NC (virtual) vertical shift   \NC dimension  \NC \NR
\NC \type {xscale}  \NC horizontal scaling         \NC integer    \NC \NR
\NC \type {yscale}  \NC vertical scaling           \NC integer    \NC \NR
\NC \type {options} \NC glyph options              \NC bitset     \NC \NR
\NC \type {font}    \NC font                       \NC identifier \NC \NR
\NC \type {id}      \NC font                       \NC integer    \NC \NR
\LL
\stoptabulate

The values default to the currently set values. Here is a \CONTEXT\ example:

\startbuffer
\ruledhbox{
    \ruledhbox{\glyph yoffset 1ex options 0 123}
    \ruledhbox{\glyph xoffset .5em yoffset 1ex options "C0 125}
    \ruledhbox{baseline\glyphyoffset 1ex \glyphxscale 800 \glyphyscale\glyphxscale raised}
}
\stopbuffer

\typebuffer

Visualized:

\getbuffer

\stopsubsection

\startsubsection[title={\prm{nospaces}}]

\topicindex {spaces+suppress}

This new primitive can be used to overrule the usual \prm {spaceskip} related
heuristics when a space character is seen in a text flow. The value~\type{1}
triggers no injection while \type{2} results in injection of a zero skip. In \in
{figure} [fig:nospaces] we see the results for four characters separated by a
space.

\startplacefigure[reference=fig:nospaces,title={The \prm {nospaces} options.}]
\startcombination[3*2]
    {\ruledhbox to 5cm{\vtop{\hsize 10mm\nospaces=0\relax x x x x \par}\hss}} {\type {0 / hsize 10mm}}
    {\ruledhbox to 5cm{\vtop{\hsize 10mm\nospaces=1\relax x x x x \par}\hss}} {\type {1 / hsize 10mm}}
    {\ruledhbox to 5cm{\vtop{\hsize 10mm\nospaces=2\relax x x x x \par}\hss}} {\type {2 / hsize 10mm}}
    {\ruledhbox to 5cm{\vtop{\hsize  1mm\nospaces=0\relax x x x x \par}\hss}} {\type {0 / hsize 1mm}}
    {\ruledhbox to 5cm{\vtop{\hsize  1mm\nospaces=1\relax x x x x \par}\hss}} {\type {1 / hsize 1mm}}
    {\ruledhbox to 5cm{\vtop{\hsize  1mm\nospaces=2\relax x x x x \par}\hss}} {\type {2 / hsize 1mm}}
\stopcombination
\stopplacefigure

\stopsubsection

\startsubsection[title={\prm{protrusionboundary}}]

\topicindex {protrusion}
\topicindex {boundaries}

The protrusion detection mechanism is enhanced a bit to enable a bit more complex
situations. When protrusion characters are identified some nodes are skipped:

\startitemize[packed,columns,two]
\startitem zero glue \stopitem
\startitem penalties \stopitem
\startitem empty discretionaries \stopitem
\startitem normal zero kerns \stopitem
\startitem rules with zero dimensions \stopitem
\startitem math nodes with a surround of zero \stopitem
\startitem dir nodes \stopitem
\startitem empty horizontal lists \stopitem
\startitem local par nodes \stopitem
\startitem inserts, marks and adjusts \stopitem
\startitem boundaries \stopitem
\startitem whatsits \stopitem
\stopitemize

Because this can not be enough, you can also use a protrusion boundary node to
make the next node being ignored. When the value is~1 or~3, the next node will be
ignored in the test when locating a left boundary condition. When the value is~2
or~3, the previous node will be ignored when locating a right boundary condition
(the search goes from right to left). This permits protrusion combined with for
instance content moved into the margin:

\starttyping
\protrusionboundary1\llap{!\quad}«Who needs protrusion?»
\stoptyping

\stopsubsection

\startsubsection[title={\prm{fontcharta}}]

\topicindex {glyphs + properties}

The \prm {fontcharwd}, \prm {fontcharht}, \prm {fontchardp} and \prm {fontcharic}
give access to character properties. To this repertoire \LUAMETATEX\ adds the top
accent accessor \prm {fontcharta} which came in handy for tracing. You pass a
font reference and character code. Normally only \OPENTYPE\ math fonts have this
property.

\stopsubsection

\stopsection

\startsection[title={The \LUA\ font library}][library=font]

\startsubsection[title={Introduction}]

The \LUA\ font library is reduced to a few commands. Contrary to \LUATEX\ there
is no loading of \TFM\ or \VF\ files. The explanation of the following commands
is in the \LUATEX\ manual.

\starttabulate[|l|pl|]
\DB function              \BC description \NC\NR
\TB
\NC \type {current}       \NC returns the id of the currently active font \NC \NR
\NC \type {max}           \NC returns the last assigned font identifier \NC \NR
\NC \type {setfont}       \NC enables a font setfont (sets the current font id) \NC \NR
\NC \type {addcharacters} \NC adds characters to a font \NC \NR
\NC \type {define}        \NC defined a font \NC \NR
\NC \type {id}            \NC returns the id that relates to a command name \NC \NR
\LL
\stoptabulate

For practical reasons the management of font identifiers is still done by \TEX\
but it can become an experiment to delegate that to \LUA\ as well.

\stopsubsection

\startsubsection[title={Defining a font with \type {define}, \type {addcharacters} and \type
{setfont}}]

\topicindex {fonts+define}
\topicindex {fonts+extend}

Normally you will use a callback to define a font but there's also a \LUA\
function that does the job.

\startfunctioncall
id = font.define(<table> f)
\stopfunctioncall

Within reasonable bounds you can extend a font after it has been defined. Because
some properties are best left unchanged this is limited to adding characters.

\startfunctioncall
font.addcharacters(<number n>, <table> f)
\stopfunctioncall

The table passed can have the fields \type {characters} which is a (sub)table
like the one used in define, and for virtual fonts a \type {fonts} table can be
added. The characters defined in the \type {characters} table are added (when not
yet present) or replace an existing entry. Keep in mind that replacing can have
side effects because a character already can have been used. Instead of posing
restrictions we expect the user to be careful. The \type {setfont} helper is
a more drastic replacer and only works when a font has not been used yet.

\stopsubsection

\startsubsection[title={Font ids: \type {id}, \type {max} and \type {current}}]

\topicindex {fonts+id}
\topicindex {fonts+current}

\startfunctioncall
<number> i = font.id(<string> csname)
\stopfunctioncall

This returns the font id associated with \type {csname}, or $-1$ if \type
{csname} is not defined.

\startfunctioncall
<number> i = font.max()
\stopfunctioncall

This is the largest used index so far. The currently active font id can be
queried or set with:

\startfunctioncall
<number> i = font.current()
font.current(<number> i)
\stopfunctioncall

\stopsubsection

\startsubsection[title={Glyph data: \prm {glyphdatafield}, \prm {glyphscriptfield}, \prm {glyphstatefield}}]

These primitives can be used to set an additional glyph properties. Of course
it's very macro package dependant what is done with that. It started with just
the first one as experiment, simply because we had some room left in the glyph
data structure. It's basically an single attribute. Then, when we got rid of the
ligature pointer we could either drop it or use that extra field for some more,
and because \CONTEXT\ already used the data field, that is what happened. The
script and state fields are shorts, that is, they run from zero to \type {0xFFFF}
where we assume that zero means \quote {unset}. Although they can be used for
whatever purpose their use in \CONTEXT\ is fixed.

\stopsubsection

\startsubsection[title={Scaling math fonts with \prm {glyphtextscale} etc}]

More details about fonts in math mode can be found in the chapter about math so
here we just mention a few primitives. The internal \prm {glyphtextscale}, \prm
{glyphscriptscale} and \prm {glyphscriptscriptscale} registers can be set to
enforce additional scaling of math, like this:

\startbuffer
$                            a = b^2 = c^{d^2}$
$\glyphtextscale         800 a = b^2 = c^{d^2}$
$\glyphscriptscale       800 a = b^2 = c^{d^2}$
$\glyphscriptscriptscale 800 a = b^2 = c^{d^2}$
\stopbuffer

\typebuffer

You can of course set them all in any mix as long as the value is larger than
zero and doesn't exceed 1000. In \CONTEXT\ we use this for special purposes so
don't mess with it there. as there can be side unexpected (but otherwise valid)
side effects.

\startlines
\getbuffer
\stoplines

The next few reported values depend on the font setup. A math font can be loaded
at a certain scale and further scaled on the fly. An open type math font comes with
recommended script and scriptscript scales and gets passed to the engine scaled. The
values reported by \prm {mathscale} are {\em additional} scales.

\startbuffer
$\the\mathscale\textfont        \zerocount$
$\the\mathscale\scriptfont      \zerocount$
$\the\mathscale\scriptscriptfont\zerocount$
\stopbuffer

\typebuffer

In \CONTEXT\ we use this for some experiments (of which some made it into
features) but discussing this fall behind this manual. You cannot set these
values because the engine has to work with consistent settings and messing around
with fonts during a run only works well if the backend also cooperates. Also the
values only makes sense in the perspective of the used macro package.

\stopsubsection

\startsubsection[title={Tracing}]

The \prm {tracingfonts} primitive that has been inherited from \PDFTEX\ has
been adapted to support variants in reporting the font. The reason for this
extension is that a csname not always makes sense. The zero case is the default.

\starttabulate[|l|l|]
\DB value \BC reported \NC \NR
\TB
\NC \type{0} \NC \type{\foo xyz} \NC \NR
\NC \type{1} \NC \type{\foo (bar)} \NC \NR
\NC \type{2} \NC \type{<bar> xyz} \NC \NR
\NC \type{3} \NC \type{<bar @ ..pt> xyz} \NC \NR
\NC \type{4} \NC \type{<id>} \NC \NR
\NC \type{5} \NC \type{<id: bar>} \NC \NR
\NC \type{6} \NC \type{<id: bar @ ..pt> xyz} \NC \NR
\LL
\stoptabulate

\stopsubsection

\stopsection

\stopchapter

\stopcomponent

