GNU TeXmacsUser manualReferenceAboutEnglish
Virtual fonts and the vfn language

1.Principle

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:

2.Loading virtual fonts

2.1.The .vfn files

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:

tradi-long.vfn, tradi-negate.vfn, tradi-misc.vfn

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.

emu-fundamental.vfn

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.

emu-greek.vfn, emu-operators.vfn, emu-relations.vfn, emu-orderings.vfn, emu-setrels.vfn, emu-arrows.vfn

Emulation of Greek capitals, binary operators, relations, orderings, set relations and arrows from the glyphs of a text font.

emu-bracket.vfn

Brackets: angle brackets built from slashes, floors and ceilings from square brackets, double and triple brackets and bars (names emu-…).

emu-large.vfn, emu-alt-large.vfn

Big operators (big-iint-1, big-oint-2, …) and parameterized extensible delimiters (rubber-lparenthesis-#, …) for poor_rubber_font and rubber_assemble_font.

2.2.Translators

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.

2.3.The virtual_font_rep class

font virtual_font (font base, string name, int size, int hdpi, int vdpi, bool extend)
(construct a virtual font)

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:

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).

2.4.Units

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.

3.Symbols and lookup

3.1.Naming components

Inside a definition, an atom denotes a glyph:

3.2.Parameterized symbols

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.

3.3.Getting a glyph

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:

4.The vfn language

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.

4.1.Selection and variables

(dx dy g)

The glyph \(g\) moved by \((\operatorname{dx}, \operatorname{dy})\) em.

(with var expr g)

Evaluate the numeric expression \(\operatorname{expr}\) (see below) and substitute the result for every atom \(\operatorname{var}\) in \(g\).

(or g1 … gn)

The first of \(g_1, \ldots, g_{n - 1}\) which is supported (all its components exist), and \(g_n\) otherwise.

(font g fam1 …)

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.

(italic g slope corr)

The glyph \(g\), with the given slope (returned by get_left_slope and get_right_slope) and right italic correction in em.

4.2.Combination

(join g1 … gn)

Superposition with a common origin; the bounding boxes are merged.

(add g1 g2)

Superposition, with \(g_2\) horizontally centered on \(g_1\).

(glue g1 g2)

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).

(row g1 g2)

Concatenation of the ink of \(g_1\) and \(g_2\) with a slight overlap, centered in the logical width of the glued glyphs.

(glue-above g1 g2 [sep]), (glue-below …)

\(g_2\) on top of (resp. below) \(g_1\), with an optional extra separation in em.

(stack g1 g2 [sep])

\(g_2\) below \(g_1\), the whole being raised by half the height of \(g_2\).

(stack-equal g1 g2), (stack-less …)

A stack with the separation between the bars of =, vertically centered on = resp. <.

(right-fit g1 g2 f), (left-fit …)

\(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\).

(intersect g1 g2), (exclude g1 g2)

Pixelwise intersection and difference. Bitmap only.

(bar-right g1 g2), bar-left, bar-top, bar-bottom

Attach the bar \(g_2\) to the side of \(g_1\) (the starred forms bar-right*, bar-bottom* do not align first). Bitmap only.

(negate g bar)

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.

(reslash g r)

The slash \(g\), transformed to fit the box of \(r\).

4.3.Geometric transformations

(magnify g mx my)

Scale by the given factors.

(deepen g my pen), (widen g mx pen)

Vertical (horizontal) stretching which preserves strokes of width \(\operatorname{pen}\) em.

(hor-flip g), (ver-flip g)

Mirror images.

(rot-left g), (rot-right g)

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 r sx sy), scale*

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.

(hor-scale g r)

Scale the horizontal ink of \(g\) to the one of \(r\) and center it on \(r\).

(hor-extend g pos n [f])

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.

(curly g), (unserif g [c]), (bottom-edge g [penh keepy]), (flood-fill g xf yf), (junc-left g w), (junc-right …), (circle r w)

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.

(bitmap g)

The glyph \(g\), but always rendered as a bitmap.

(copy g)

A copy of the glyph.

4.4.Positioning and bounding boxes

(align g r xa ya [xa2 ya2])

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.

(crop g), hor-crop, ver-crop, left-crop, right-crop, top-crop, bottom-crop

Replace (parts of) the logical box by the ink box.

(enlarge g l r b t)

Enlarge the logical box by the given amounts in em (trailing arguments may be omitted).

(unindent g), (unindent* g)

Move horizontally so that the left (right) side of the logical box is at zero.

(clip g x1 x2 y1 y2)

Clip to the given coordinates in em.

(part g x1 x2 y1 y2 [dx dy])

Clip to a part of the logical box given by fractions, and optionally move the result by fractions of the box.

(pretend g r), hor-pretend, ver-pretend, left-pretend, right-pretend

Draw \(g\) with (parts of) the logical box of \(r\).

(min-width g1 g2), max-width, min-height, max-height

Choose the narrower, wider, lower or higher glyph.

4.5.Numeric expressions

The second argument of with is evaluated by virtual_font_rep::exec, which returns numbers as strings (or "error"):

+, -, *, /, min, max

Arithmetic (- and / are binary).

(left g), right, bottom, top, width, height

Coordinates and dimensions of the logical box, in em.

(xpos g xf), (ypos g yf), (xpos g xf yf dir)

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).

(penw g x1 x2 y), (penh g x y1 y2)

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.

(sep-equal), (frac-width)

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)))

5.Rendering virtual glyphs

5.1.Metrics and glyphs

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.

5.2.Drawing

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.

5.3.Other font routines

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.

6.Enhancing a 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.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".