![]() |
OpenType features in text and formulas | ![]() |
An OpenType font may carry several shapes of the same character and say, in its GSUB table, under which feature each of them is to be used: old style figures under onum, small capitals under smcp, dotless letters under dtls, script-size alternates under ssty. Its GPOS table holds the kerning between pairs of glyphs. This page describes how TeXmacs uses these tables: the features which a document asks for through the environment variable font-features, the three features which the mathematics uses by itself, and the pair kerning of text. How the features are chosen in the menus and in the font browser, from the point of view of a user, is explained in selecting fonts; how the tables are parsed, in the OpenType layout tables.
Every feature goes through one virtual method of font_rep:
If the font has a substitute for the character s under the four-letter feature, return in r the name of the alt-th one, counting from 0, as a glyph name <@XXXX>. The default returns false.
unicode_font_rep::get_feature_variant (
Plugins/Freetype/unicode_font.cpp
) implements it: it looks up the glyph id of s, asks the face for the map of the feature (tt_face_rep::gsub_feature, which parses it on first use and caches it per tag) and returns the requested alternate if there is one. A single substitution has one alternate, an alternate substitution several. The smart font forwards the call to the subfont which draws the character, so the substitute comes from the font which really draws it.
ot_font_features (name), exported to Scheme as font-available-features, returns the feature tags which a font file offers (tt_face_rep::gsub_tags), so that the menus can propose only what the font can do.
Most callers do not want one glyph but a whole font in which every glyph is replaced by its substitute. This is the decorator feature_font_rep of
Graphics/Fonts/feature_font.cpp
:
A font named base#featurealt which rewrites every string character by character, replacing each character by its substitute in base when there is one (feature_font_rep::rewrite, with a cache per character), and then delegates to base. Positions are mapped back from the rewritten string to the original one, so cursor positions, extents and the metric hooks (slopes, italic corrections, the cut-in kerns, the top accent attachment) refer to the characters of the document. magnify wraps the magnified base again, and the rubber font is the one of the base.
Because the substitution happens when the glyph is measured and drawn, the document is untouched: it still holds the characters which were typed, and copying, searching, spell checking and exporting see them.
The value of font-features (default empty,
Typeset/Env/env_default.cpp
) is a comma separated list of feature tags, each optionally followed by =n to select the n-th alternate. edit_env_rep::update_font (
Typeset/Env/env_semantics.cpp
) applies it with
Wrap fn in one feature_font per tag, from left to right, so that a later tag sees the glyphs which the earlier ones chose: onum,tnum takes old style figures and then their tabular form. Items whose tag is not four characters long are ignored, and a font which lacks a feature leaves its glyphs alone, so the variable is safe on a whole document.
The order of the decorators in update_font is: the font of the current size, then the ssty decorator of the mathematics (see below), then the features of font-features, then the effects of font-effects.
On the Scheme side,
progs/fonts/font-features.scm
holds the table of the features which the menus propose (font-feature-table: the figure styles, zero, the capital forms, hist, swsh, salt, calt and the stylistic sets ss01 to ss05), and the menus text-font-features-menu (Format\(\rightarrow\)Font features) and document-font-features-menu (Document\(\rightarrow\)Font\(\rightarrow\)Features), which list the proposed features of the font at the cursor (font-features-here, through font-logical-search) with a check mark for those in force. font-features-toggle sets the variable on the selection with make-with, font-features-toggle-global in the initial environment; turning on one figure style or spacing turns off its opposite (onum and lnum, tnum and pnum). The font browser (
progs/fonts/font-new-widgets.scm
) has a column and a tab of toggles, font-features-selector, which lists the features of the font the dialog has selected (selector-font-features-available, through the logical font of the sample text) and keeps the choice in the selector under the key :features.
Three features are applied by the typesetter itself, for fonts with a MATH table. They are what the specification of the table expects, and they are not under the control of font-features.
Many math fonts draw special shapes for the first and second script levels, with thicker strokes and wider spacing. In edit_env_rep::update_font, at index levels above 0, a font of math type MATH_TYPE_OPENTYPE (an untuned OpenType math font, not one of the hand-tuned TeX Gyre fonts) is wrapped in feature_font (fn, "ssty", min (index_level, 2) - 1): alternate 0 at the first script level, alternate 1 at the second and deeper ones. This happens in the same branch which takes the script sizes from scriptPercentScaleDown and scriptScriptPercentScaleDown, that is, only when the document keeps the default math-font-sizes.
An accent over \(i\) or \(j\) should sit on a dotless letter. concater_rep::typeset_wide (
Typeset/Concat/concat_math.cpp
) replaces an atomic body i or j of an accent above by its dtls substitute when the font has a table and offers one.
Over a base taller than FlattenedAccentBaseHeight, a narrow accent (one which the font does not stretch) is replaced by its flac substitute, in the OpenType branch of the accent construction of
Typeset/Boxes/Composite/math_boxes.cpp
.
The letters of a formula are another substitution, but not a GSUB one: for an untuned math font, the smart font takes them from the mathematical italic alphabet of the font (REWRITE_MATH_ITALIC, see mathematics from the MATH table).
The kerning of text is computed by the font metric of the face, tt_font_metric_rep::kerning (
Plugins/Freetype/tt_face.cpp
), which unicode_font_rep::get_xpositions and the drawing routines add between consecutive glyphs. FreeType only exposes the legacy kern table, which no OpenType math font and few recent text fonts ship: before this work, every such font was set without any kerning. kerning now first asks the face for the pair kerning of the GPOS feature kern (tt_face_rep::gpos_kern, parsed once by parse_gpos_kern), both explicit pairs and class matrices, and only falls back to FreeType when the font has no such data. Only the horizontal advance of the value records is used.
Kerning applies between the glyphs of one string drawn by one font, so it is not applied across a change of font, nor between a glyph and the substitute which a feature font put next to it in another font.
Graphics/Fonts/feature_font.cpp
feature_font and apply_features.
Plugins/Freetype/unicode_font.cpp
get_feature_variant and ot_font_features.
Plugins/Freetype/tt_face.cpp,
tt_tools.cpp
The cached feature maps and the kerning, and the GSUB and GPOS readers.
Typeset/Env/env_semantics.cpp
update_font: script sizes, ssty, font-features.
Typeset/Concat/concat_math.cpp,
Typeset/Boxes/Composite/math_boxes.cpp
dtls and flac.
progs/fonts/font-features.scm,
progs/fonts/font-new-widgets.scm
The menus and the font browser.
Only single (lookup type 1) and alternate (type 3) substitutions are read, also when wrapped in an extension lookup (type 7). Multiple substitutions, ligatures (liga, dlig, frac) and contextual lookups are ignored. A feature such as calt, which the menus propose, usually works through contextual lookups and then has no effect.
The feature is looked up in every script and language system of the table at once: the parser walks all feature records with the requested tag, and the first substitute found for a glyph wins. A font which substitutes differently by language gets an arbitrary one.
Of GPOS, only the horizontal advances of pair positioning are used. Explicit pairs take precedence over class matrices whatever the order of the subtables, and a later explicit pair overrides an earlier one, whereas the specification lets the first subtable which covers a pair decide. Mark attachment, cursive and contextual positioning are not used, which matters for fonts which place accents with mark/mkmk rather than with the top accent attachment of the MATH table.
When a font has GPOS kerning, the legacy kern table is no longer consulted at all, even for pairs the GPOS data do not mention.