![]() |
Emulated (poor man's) fonts | ![]() |
An emulated font is a wrapper around a base font which modifies the appearance of its glyphs: it makes them bolder, slanted, wider, smaller, hollow, irregular or blurred. The implementations are the files Graphics/Fonts/poor_*.cpp; the constructors are declared in
Graphics/Fonts/font.hpp
:
font poor_rubber_font (font base);
font poor_smallcaps_font (font base);
font poor_italic_font (font base, double slant);
font poor_stretched_font (font base, double zoomx, double zoomy);
font poor_extended_font (font base, double factor, double lw);
font poor_extended_font (font base, double factor);
font poor_mono_font (font base, double lw, double phw);
font poor_bold_font (font base, double lofat, double upfat);
font poor_bold_font (font base);
font poor_bbb_font (font base, double penw, double penh, double fatw);
font poor_bbb_font (font base);
font poor_distorted_font (font base, tree kind);
font poor_effected_font (font base, tree kind); |
Emulated fonts are used in three situations:
Automatically, when the font database has no font for the requested series, shape or variant (missing bold, italic, small capitals or blackboard bold), see below.
By smart fonts, for particular characters: bold wide accents, double struck letters, extensible delimiters.
Explicitly, through the font-effects environment variable (see font effects).
All emulated fonts follow the same pattern. Let us take poor_italic_font_rep as an example.
The class has a field base and the parameters of the emulation. The constructor calls copy_math_pars (base) and adjusts the global parameters which change (for italics: the slope; for bold: the interword space, wquad and wline; for monospaced fonts: the spaces).
supports is delegated to the base font.
get_extents and get_xpositions call the base font and transform the result (for italics, the ink box is enlarged according to the slant; for extended fonts, all horizontal coordinates are multiplied by the factor; for bold fonts, each glyph gets wider and the positions are recomputed glyph by glyph using advance_glyph).
index_glyph obtains the glyph and metric tables of the base font and returns transformed tables, using the operations of
Graphics/Bitmap_fonts/bitmap_font.hpp
on whole tables: slanted, stretched, extended, bolden, make_bbb, mono, distorted, effected. get_glyph does the same for a single glyph.
draw_fixed cuts the string into glyphs with base->advance_glyph and draws each of them with ren->draw (c, fng, x, y) using the transformed tables.
magnify builds the same emulation on the magnified base font.
The constructor function builds a resource name such as pooritalic[base,slant] or poorbold[base,lofat,upfat] and uses make (font, name, …), so that identical emulations are shared.
Drawing through transformed bitmap tables works on any renderer, but on printers it would produce bitmap (Type 3) fonts. Several emulations therefore have a separate printer path which keeps the output vectorial:
On printers (!ren->is_screen), install a transformation (slanting resp. scaling) with ren->set_transformation, draw the base font, and reset the transformation. poor_stretched_font_rep also falls back on this method on the screen when the base font has no glyph tables.
Always draws the base font, glyph by glyph, centered in a cell of width wquad; glyphs which are too wide are horizontally compressed with a scaling transformation.
On printers, draws the base glyph nine times with small horizontal offsets (and, for characters which need extra width, from a horizontally stretched base font with a slight vertical slope), which simulates a thicker pen.
The other emulations (poor_bbb_font_rep, poor_extended_font_rep, poor_distorted_font_rep, poor_effected_font_rep) always draw bitmaps.
Emboldening by bolden. lofat and upfat are the extra pen widths for lower and upper case letters, as fractions of the design size wfn; the version without parameters uses wline/wfn. A small table (get_bold_multiplier) makes wide letters such as M and W gain more width than the others.
Slanting by slanted. The automatic emulation uses the slant 0.25001 on a font compressed horizontally by \(5 / 6\); this precise value is recognized by concat_math (
Typeset/Concat/concat_math.cpp
) when positioning scripts.
Not a bitmap transformation but a small dispatcher, similar to a smart font: runs of lower case characters (uni_upcase_char differs) are upcased and drawn with a magnified version of the base font, whose horizontal and vertical factors are derived from the ratio of the heights of x and X.
Blackboard bold (“double struck”) letters built by make_bbb, which doubles the strokes; the pen dimensions and the extra width are fractions of wfn.
Anisotropic magnification. Since fonts can be magnified isotropically, the constructor reduces to base->magnify (zoomx) followed by a vertical stretching by zoomy/zoomx, and returns the base font if no stretching is needed. This is also the default font_rep::poor_magnify.
Horizontal extension or condensation which, unlike a plain stretching, preserves the width of vertical strokes (extended, widen). The two argument version measures the stroke width lw from the glyphs o and O.
A monospaced version of a proportional font: every character occupies lw times the quad width of the base font, and characters wider than phw quads are compressed.
Random distortions of the glyphs (distorted in
Graphics/Bitmap_fonts/glyph_distorted.cpp
); kind is a tuple (degraded threshold frequency), (distorted strength frequency) or (gnawed strength frequency).
Graphical effects applied to the glyphs (
glyph_effected.cpp
). Currently only (blurred radius [dx dy]) is recognized: it is translated into a Gaussian blur effect (EFF_BLUR, EFF_GAUSSIAN), optionally moved (EFF_MOVE).
Extensible delimiters and big operators for Unicode fonts without such glyphs; see below.
Two related wrappers are not emulations of a variant but are used by the font effects: recolored_font (base, kind) draws the base font with the color kind (it changes the pencil of the renderer and restores it afterwards), and superposed_font (fns, ref) draws all fonts of fns on top of each other, taking metrics and glyphs from fns[ref] (the ink boxes are merged).
The automatic emulation is decided during the font selection, in find_closest (
Graphics/Fonts/font_translate.cpp
, see also the chapter on font selection). After finding the closest physical font, it compares the requested logical features with the features of the font which was found (both those declared in the database and those guessed from its name). If a feature was requested but is not present, a suffix is appended to the name which is returned:
The variant gets the suffix -poorbbb.
The series gets the suffix -poorbf.
The shape gets the suffix -poorsc.
The shape gets the suffix -poorit.
closest_font then calls find_font (family, variant, series, shape, sz, dpi) (
Graphics/Fonts/find_font.cpp
), which recognizes these suffixes, recursively finds the font without the suffix, and wraps it:
if (ends (shape, "-poorit")) {
string shape2= shape (0, N(shape) - 7);
font fn= find_font (family, variant, series, shape2, sz, dpi);
if (!is_nil (fn)) {
font nafn= fn->magnify (5.0/6.0, 1.0);
font itfn= poor_italic_font (nafn, 0.25001);
// NOTE: precise value 0.25001 also used in 'concat_math'
font::instances (s)= (pointer) itfn.rep;
return itfn;
}
}
else if (ends (shape, "-poorsc")) … // poor_smallcaps_font (fn)
else if (ends (series, "-poorbf")) … // poor_bold_font (fn)
else if (ends (variant, "-poorbbb")) … // poor_bbb_font (fn) |
Since only one suffix is handled per call and the recursive call handles the next one, combinations such as a bold italic emulation of a regular font work naturally. Because smart fonts obtain all their subfonts through closest_font, the emulation also applies to the mathematical alphabets: if a family has no outline variant, the bbb subfont of the smart font is an emulated blackboard bold version of the family.
Besides the automatic emulation, smart fonts create emulated subfonts directly in smart_font_rep::initialize_font:
poor-bold: poor_bold_font of the medium version of the smart font with fatness \((5 / 3 - 1) \cdot \operatorname{wline}/\operatorname{wfn}\), used for wide accents in bold text.
poor-bbb: poor_bbb_font of the upright smart font, with pen dimensions derived from the font database characteristics, used for the symbols <bbb-X>.
rubber: rubber_font of another subfont, which is usually a poor_rubber_font, but a rubber_unicode_font when the subfont has an OpenType MATH table (see stretchable glyphs).
Mathematical typesetting needs delimiters of arbitrary size (<left-(-n>, <mid-|-n>, …) and big operators (<big-sum-1>, <big-sum-2>). Ordinary Unicode text fonts provide none of them. poor_rubber_font (base) builds them from the ordinary glyphs. A font with a MATH table does not go through this emulation: its make_rubber_font returns a rubber_unicode_font which uses the size variants and the assemblies of the table, so that the poor rubber font now only serves the fonts which have none.
The base font is first enhanced with the bracket emulations: virtual_enhance_font (base, "emu-bracket"), so that angle brackets, floors, ceilings, double brackets and double bars exist.
The font keeps an array larger of fonts, created on demand by get_font (nr). For \(\operatorname{nr} \leqslant 9\) (2*MAGNIFIED_NUMBER+1) these are poor_stretched_fonts of the base font, magnified vertically by \(2^{\lfloor \operatorname{nr}/ 2 \rfloor / 4}\) and horizontally by the square root (or, for odd \(\operatorname{nr}\), the fourth root) of this factor; odd numbers are used for thin delimiters (is_thin). Numbers 10 and 11 are stretched versions of the virtual font emu-large, number 12 is rubber_unicode_font (base), and number 13 is the virtual font emu-large built on the poor rubber font itself. The numbers from 14 (SLASH_BASE) to 29 are further stretched versions of the base font for the slashes: a slash cannot be extended by repeating a straight middle part, so beyond the magnified sizes it keeps being stretched, by \(2^{k / 4}\) vertically and the fourth root of this factor horizontally.
search_font (s, r) maps a requested symbol to a font number and a symbol in that font. Small delimiters (size up to MAGNIFIED_NUMBER, as well as angle brackets of any size) are magnified versions of the base glyph, possibly replaced by an emu-… construction if the base font lacks the glyph. Larger slashes and backslashes go to the fonts from SLASH_BASE on; a backslash whose height differs by more than 5% from the one of the slash is replaced by <emu-backslash>, made from the slash, at the largest magnified size. Larger delimiters use the parameterized virtual glyphs rubber-lparenthesis-#, rubber-lbracket-#, … of emu-large.vfn, with the parameter \(\max (n - 5, 0) + 1\) (the constant HUGE_ADJUST is 1); they are requested with the legacy encoding “code byte followed by the parameter”. Big operators come from the base font if it has them (big_flag, for the TeX Gyre fonts and for any font with a MATH table, ot_math) or from emu-large.
The global has_poor_rubber (in
Graphics/Fonts/font.cpp
, true by default) enables this mechanism; use_poor_rubber (fn) is consulted by the typesetter (
Typeset/Concat/concat_post.cpp
), which always creates a delimiter box for such fonts, even for small delimiters.
error_font (fn) (
Graphics/Fonts/font.cpp
) is the last resort of every smart font. It claims to support every string, measures it with fn and draws it in red with fn. The error font of a smart font is based on the sans serif variant of the roman family. Note that error_font_rep::draw_fixed sets the pencil to red without restoring it.