![]() |
Virtual fonts and the vfn language | ![]() |
A virtual font provides glyphs which are computed from the glyphs of a base font. Each virtual glyph is defined by a small expression, for instance
(longrightarrow (glue minus rightarrow))
(nleadsto (join (0 0 leadsto) (0 0 Arrownot)))
(emu-backslash (hor-flip /)) |
which says that a long arrow is a minus sign glued to an arrow, that a negated <leadsto> is the superposition of <leadsto> and of a negation slash, and that a backslash can be obtained by mirroring a slash. The base font may itself be a smart font, so the components can come from anywhere.
A virtual font must be able to do two things with such an expression:
Compile it into a bitmap glyph and a metric (logical and ink bounding boxes). This is virtual_font_rep::compile; it is used for the metrics in all cases and for drawing on the screen. Glyph operations are those of
Graphics/Bitmap_fonts/bitmap_font.hpp
(join, move, hor_flip, stretched, clip, …).
Draw it directly on a renderer, using the vector drawing of the components and the transformations of the renderer (virtual_font_rep::draw_tree). This is used for printing and PDF export, so that virtual symbols remain vector graphics. Operations which can only be done on bitmaps (pixel intersection, flood filling, …) force the bitmap path.
Virtual font definitions are stored in $TEXMACS_PATH/fonts/virtual (and may be overridden in $TEXMACS_HOME_PATH/fonts/virtual). A file name.vfn contains one Scheme expression
(virtual-font
(name-1 definition-1)
(name-2 definition-2)
…) |
The standard files are:
Traditional constructions: long arrows, negated relations, dots, flipped letters, … Used by the old TeX based math fonts and as the last fallback of smart fonts.
Basic building blocks for the emulation of symbols in arbitrary text fonts: a good minus sign, slashes of various sizes for negations, arrow heads and arrow bars, sim, approx, equiv, centered dots, … All other emu-* fonts are stacked on top of it.
Emulation of Greek capitals, binary operators, relations, orderings, set relations and arrows from the glyphs of a text font.
Brackets: angle brackets built from slashes, floors and ceilings from square brackets, double and triple brackets and bars (names emu-…).
Big operators (big-iint-1, big-oint-2, …) and parameterized extensible delimiters (rubber-lparenthesis-#, …) for poor_rubber_font and rubber_assemble_font.
Files are loaded by load_virtual (
Graphics/Fonts/translator.cpp
), usually through load_translator, which first looks for an encoding file name.enc in
fonts/enc
and otherwise loads the virtual font. Both produce a translator:
struct translator_rep: rep<translator> {
int cur_c;
hashmap<string,int> dict; // symbol -> code
array<tree> virt_def; // code -> definition (virtual fonts)
}; |
For a virtual font, the code of an entry is its position in the file (starting at 1). Names of more than one character are stored in the dictionary with angle brackets, so the entry longrightarrow defines the symbol <longrightarrow>. Translators are resources: each file is parsed only once per session.
Builds the virtual font name on top of base. Its resource name is the base name followed by #virtual-namesize@hdpi (or #enhance-… in extend mode, with xvdpi if the resolutions differ). The extend flag selects between two modes:
Plain mode (extend == false): the font only provides the symbols of the .vfn file. Atomic names inside the definitions always refer to the base font. This is how smart fonts use tradi-* and emu-bracket, with the smart font itself as base (("virtual" name) subfonts).
Extend mode (extend == true): the font provides all symbols of the base font, plus the virtual symbols which the base font does not have. Atomic names inside definitions refer to the base font if it supports them, and otherwise to the other definitions of the same file, recursively. This is how the emu-* fonts are used: virtual_font (virtual_font (main, "emu-fundamental", …, true), "emu-arrows", …, true).
The class keeps the base font, the translator virt, the resolution, a font_metric and font_glyphs table indexed by code (filled lazily), and caches trm (metrics of compiled subexpressions) and sup_bit, sup_svg (results of the support tests).
Numbers in definitions are expressed in units of the font size: the horizontal unit hunit= ((size*hdpi)/72)*PIXEL and the vertical unit vunit (same with vdpi) correspond to one em of the base font. Thus (0.3 0 .) is a period moved 0.3 em to the right. Some operators instead use fractions of the bounding box of a glyph; this is indicated below.
Inside a definition, an atom denotes a glyph:
An atom of one character denotes that character: /, =, [, ., +.
A longer atom foo denotes the universal symbol <foo>: minus, rightarrow, less (for <), #2039 (for the Unicode character <#2039>).
The parentheses, which cannot be written as atoms, are #28 and #29.
An entry whose name ends with -# defines a family of symbols. A request for <rubber-lparenthesis-7> is decoded by decode_sharp into the entry <rubber-lparenthesis-#> and the parameter 7, and every atom # in the definition is replaced by the parameter (subst_sharp; atoms like #239C which start with a hexadecimal digit are left alone). For example, from emu-large.vfn and emu-alt-large.vfn:
(rubber-lparenthesis-# (ver-extend #28 0.5 # 0.1))
(rubber-lparenthesis-#
(glue-above #239D
(glue-above (ver-take #239C 0.5 # 0.25)
#239B))) |
The first one stretches an ordinary parenthesis at half its height by \(0.1 \times n\) em; the second one assembles the Unicode parenthesis pieces U+239B–U+239D with a variable number of copies of the extension piece.
virtually_defined (c, name) tells whether a symbol is defined in a virtual font file, taking parameterized symbols into account.
virtual_font_rep::get_char (s, fnm, fng) returns the code of s together with the metric and glyph tables to be used, compiling the definition on first use:
For <name>, the code is virt->dict[s], after decoding of a numeric parameter if needed. The glyph is stored in the global tables of the font.
A string of one byte is interpreted as a code, i.e. as the position of the entry in the file. This is the convention of the old math_font, whose translators map symbols to codes.
A string consisting of a code byte followed by a parameter (another legacy convention, used by poor_rubber_font and math_font) creates a separate one-glyph table for this parameter value.
The following reference lists all constructs recognized by virtual_font_rep::compile_bis (bitmaps and metrics), virtual_font_rep::draw_tree (vector drawing) and virtual_font_rep::supported. In the descriptions, \(g\), \(g_1\), \(g_2\) denote glyph expressions, \(r\) a reference glyph expression, and numeric arguments may be written * to mean “default” or “do not change”. Operators marked bitmap only are not drawn as vector graphics: a glyph which uses them is always rendered as a bitmap.
The glyph \(g\) moved by \((\operatorname{dx}, \operatorname{dy})\) em.
Evaluate the numeric expression \(\operatorname{expr}\) (see below) and substitute the result for every atom \(\operatorname{var}\) in \(g\).
The first of \(g_1, \ldots, g_{n - 1}\) which is supported (all its components exist), and \(g_n\) otherwise.
The glyph \(g\), but only supported if the base font is a Unicode font of one of the given families (its name contains unicode:fam). Used inside or to apply font specific constructions.
The glyph \(g\), with the given slope (returned by get_left_slope and get_right_slope) and right italic correction in em.
Superposition with a common origin; the bounding boxes are merged.
Superposition, with \(g_2\) horizontally centered on \(g_1\).
Horizontal concatenation with a small overlap of \(1.75\) points (in units of wpt), suitable for joining strokes. (glue* g1 g2 [d]) concatenates without overlap; the optional \(d\) shifts \(g_2\) further by \(d\) times the size unit of the font (negative for an overlap).
Concatenation of the ink of \(g_1\) and \(g_2\) with a slight overlap, centered in the logical width of the glued glyphs.
\(g_2\) on top of (resp. below) \(g_1\), with an optional extra separation in em.
\(g_2\) below \(g_1\), the whole being raised by half the height of \(g_2\).
A stack with the separation between the bars of =, vertically centered on = resp. <.
\(g_2\) moved as close as possible to the right (left) of \(g_1\) without collision (collision_offset); the logical width is extended by \(f\) times the width of \(g_2\).
Pixelwise intersection and difference. Bitmap only.
Attach the bar \(g_2\) to the side of \(g_1\) (the starred forms bar-right*, bar-bottom* do not align first). Bitmap only.
The negation of a relation: \(\operatorname{bar}\) is centered on \(g\), vertically magnified if its height does not fit, and the logical box of \(g\) is kept.
The slash \(g\), transformed to fit the box of \(r\).
Scale by the given factors.
Vertical (horizontal) stretching which preserves strokes of width \(\operatorname{pen}\) em.
Mirror images.
Rotation by \(\pm 90\) degrees. (rotate g angle [xf yf]) rotates by an arbitrary angle (in radians) around a point given as fractions of the logical box.
Scale \(g\) so that its logical (for scale*: ink) width and height become those of \(r\); the exponents \(\operatorname{sx}\), \(\operatorname{sy}\) interpolate between no scaling (0 or *) and full scaling (1). The variants fscale and fscale* (bitmap only) take a fifth argument, a pen width, and use widen and deepen instead of a plain stretching.
Scale the horizontal ink of \(g\) to the one of \(r\) and center it on \(r\).
Make \(g\) wider by \(n\) em (or \(n \times f\) em), by repeating the column at the fraction \(\operatorname{pos}\) of its width. ver-extend does the same vertically, and (ver-take g pos n [f]) only keeps the repeated part. (hor-take g pos n [f]) is the horizontal counterpart of ver-take: a piece of width \(n\) (or \(n \times f\)) times the logical width of \(g\), made of the column at the fraction \(\operatorname{pos}\) of its width; on printers it is drawn as vectors, by repeating narrow clipped copies of the glyph.
Special bitmap manipulations: making a stroke curly, removing serifs (of the character \(c\)), keeping the bottom edge, filling a closed contour from a point given as fractions of the ink box, junctions, and a circle of radius \(r\) and pen width \(w\) em. Bitmap only.
The glyph \(g\), but always rendered as a bitmap.
A copy of the glyph.
Move \(g\) so that the point at the fractions \((\operatorname{xa}, \operatorname{ya})\) of its logical box coincides with the point \((\operatorname{xa}_2, \operatorname{ya}_2)\) (default: the same fractions) of the logical box of \(r\). A * means “no move in this direction”. align* does the same using the ink boxes.
Replace (parts of) the logical box by the ink box.
Enlarge the logical box by the given amounts in em (trailing arguments may be omitted).
Move horizontally so that the left (right) side of the logical box is at zero.
Clip to the given coordinates in em.
Clip to a part of the logical box given by fractions, and optionally move the result by fractions of the box.
Draw \(g\) with (parts of) the logical box of \(r\).
Choose the narrower, wider, lower or higher glyph.
The second argument of with is evaluated by virtual_font_rep::exec, which returns numbers as strings (or "error"):
Arithmetic (- and / are binary).
Coordinates and dimensions of the logical box, in em.
Coordinate of a point given by fractions of the box; with four arguments, the point is moved in the direction + or - until the ink boundary (probe).
Measured stroke width (between the horizontal fractions \(x 1\) and \(x 2\), at height \(y\)) or stroke height (at \(x\), between \(y 1\) and \(y 2\)) of the glyph.
The distance between the bars of =, and the thickness of a minus sign.
For instance, the approx symbol of emu-fundamental.vfn superposes two copies of the emulated sim, shifted by the difference between the heights of minus and =:
(sim (align ~ = 0 0.5))
(approx (with y (- (height minus) (height =))
(align (join sim (0 y sim)) = * 0.5))) |
compile (t, ex) first consults the caches: the metric of each compiled subexpression is stored in trm, but the glyph itself is only cached in trg for the expensive curly construction. The glyphs of whole symbols are stored in the font_glyphs table of the font by get_char. For an atom, the glyph comes from base_fn->get_glyph and the metric from base_fn->get_extents, unless (in extend mode) the atom is a virtual symbol of the same file. Consequently the base font must implement get_glyph; Unicode fonts rasterize their outlines with FreeType.
void
virtual_font_rep::draw_fixed (renderer ren, string s, SI x, SI y) {
if (extend && base_fn->supports (s))
base_fn->draw_fixed (ren, s, x, y);
else if (ren->is_screen || !supported (s, true)) {
font_metric cfnm;
font_glyphs cfng;
int c= get_char (s, cfnm, cfng);
if (c != -1) ren->draw (c, cfng, x, y);
}
else {
tree t= get_tree (s);
if (t != "") draw_tree (ren, t, x, y);
}
} |
On the screen virtual glyphs are drawn as bitmaps through renderer_rep::draw (int, font_glyphs, SI, SI); since the font is magnified to the zoom factor before drawing, the bitmaps are computed at the screen resolution. On printers, supported (s, true) checks that the definition only uses vector-capable operators, and that a component taken from a base font which is itself a virtual font is drawn as vectors there too; if so, draw_tree draws the components with the base font, using ren->set_transformation for magnify, scale, flips and rotations (through draw_transformed) and ren->clip for clip, part and the extensions (through draw_clipped, which slightly enlarges the clipping box vertically). Otherwise the glyph is drawn as a bitmap, which the PDF renderer embeds as a Type 3 font.
supports (s) is true if (in extend mode) the base font supports s or if the definition of s exists and all its components are supported. In the latter case, or succeeds as soon as one alternative is supported, and font checks the name of the base font. Two routines serve the font inspector: virtual_font_constructs (fn, s) tells whether s is drawn by a construction rather than taken from the font which the virtual font extends, and virtual_font_draws_vectors (fn, s) whether a PDF export draws it as vectors; the second also decides when a smart font prefers STIX Two Math to an emulation (see the resolution algorithm). get_xpositions treats each virtual symbol as one block (inner positions are put in the middle), the slope and right correction come from italic annotations, and magnify rebuilds the virtual font on the magnified base font.
virtual_enhance_font (font base, string virt) (
Graphics/Fonts/virtual_enhance.cpp
) wraps base and an extend mode virtual font virt built on it. All symbols supported by the base font are rendered by the base font; universal symbols <…> which it lacks are taken from the virtual font. poor_rubber_font uses it to add the emu-bracket symbols to a Unicode font before building extensible delimiters from them.