GNU TeXmacsUser manualReferenceAboutEnglish
TFM metrics, PK glyphs and Type 1 substitution

This page describes how a TeX font of a given family, size and resolution is loaded (load_tex in

Plugins/Metafont/load_tex.cpp

), how its metrics and glyphs are decoded, and how the glyphs are represented and prepared for display.

1.Loading a font

void load_tex (string family, int size, int dpi, int dsize, tex_font_metric& tfm, font_glyphs& pk)
(metrics and glyphs of a TeX font)

Loads the metric (load_tex_tfm) and then the glyphs (load_tex_pk) of familysize at dpi dots per inch. dsize is the design size to fall back on (10 by default; 0 means “the design size stored in the metric”). If the font cannot be found, ecrm at the same size is tried instead; if this fails too, the program stops with FAILED ("Tex seems not to be installed properly").

After loading, rubber_fix marks the pieces of all extensible characters (top, middle, bottom and repeated parts) with a status (1 for a bottom piece, 2 for a top piece, 3 for middle and repeated pieces) and resets their vertical offset; the status is used when the glyphs are shrunk for display, see below.

Sizes.
For some LaTeX fonts, non-integer sizes are encoded by multiplying the size by 100: larm1050 is larm at 10.5pt, and larm1000 is the same as larm10. This is why find_font multiplies the size by 100 for the la and gr font trees, and why the helper mag (dpi, size, dsize), which computes the magnified resolution \(\operatorname{dpi} \cdot \operatorname{size}/\operatorname{dsize}\), first brings both sizes to the same convention when one of them is at least 316 and the other one is below 100.
Finding a metric.
load_tex_tfm (family, size, dsize, tfm) first consults the tfm:familysize entry of font_cache.scm, which remembers the size that was used last time. Otherwise it searches without generating anything (if generation is enabled at all), and only then searches again with generation allowed. The search itself (try_tfm) tries, in this order,
  1. the requested size;

  2. for sizes above 333 (the \(\times 100\) convention), the rounded size divided by 100;

  3. the neighbouring sizes \(\operatorname{size} \pm 1\) and \(\operatorname{size} \pm 2\) (the nearer one in the direction of 10 first), and the same sizes in the \(\times 100\) convention;

  4. the fall-back design size dsize, and finally size 10.

When a metric of another size is used, its design size field (header[1]) is scaled with mag, so that the font is magnified to the requested size. A block of code which preferred the traditional TeX design sizes (5 to 12, 17) is still present but disabled (if (false)), since the shipped fonts are Type 1 fonts which can be scaled to any size.

Finding glyphs.
load_tex_pk calls try_pk for the requested size, then for the design size and for size 10 (with the resolution magnified accordingly), and finally, for sizes in the \(\times 100\) convention, for the size divided by 100. try_pk first tries a Type 1 substitute, and only if there is none opens or generates a PK file named familysize.dpipk.

2.The class tex_font_metric_rep

struct tex_font_metric_rep
(a decoded .tfm file)

Declared in

Plugins/Metafont/load_tfm.hpp

; a resource (RESOURCE(tex_font_metric)), so that each file is decoded only once per session and shared by all fonts which use it. Its fields are those of the .tfm format: the lengths lf, lh, bc, ec (first and last character code), nw, nh, nd, ni, nl, nk, ne and np, and the arrays header, char_info, width, height, depth, italic, lig_kern, kern, exten and param. In addition, left, right, left_prog and right_prog record the boundary character information, and size is the design size in points.

load_tfm (file, family, size) reads the file with load_string, decodes the twelve 16-bit lengths, checks that they add up to the file length lf (and fails with FAILED ("invalid tfm file") otherwise), and then reads the arrays of 32-bit words. It also overrides the slant parameter of the italic and slanted variants of the old Adobe font clones by Dobkin (times-ti, palatino-sl, …), whose metrics are wrong.

The accessors decode the packed character information: w (c), h (c), d (c) and i (c) give the width, height, depth and italic correction of a character (0 outside \([\operatorname{bc}, \operatorname{ec}]\)); tag (c) and rem (c) give the tag (0: none, 1: ligature/kerning program, 2: next larger character, 3: extensible recipe) and the remainder; top, mid, bot and rep give the pieces of an extensible character; list_len and nth_in_list walk the chain of successively larger characters. The font parameters are available as slope (clamped to \(\pm 0.25\) if absurd), spc, spc_stretch, spc_shrink, x_height, spc_quad and spc_extra. All values are in the fixed point units of the .tfm format (\(2^{20}\) per design size); the fonts convert them with a factor unit.

Ligatures and kerning.
execute (s, n, buf, ker, m) runs the ligature and kerning program of the font on the character codes s[0..n-1]. It produces the resulting characters in buf, with the kerning to be added after each character in ker, and returns their number in m. It handles the kerning instructions, the ligature instructions with their operation byte \(4 a + 2 b + c\) (which says whether the left and right characters are kept and how many characters to pass over), the stop condition (skip byte of 128 or more) and the indirect start of a program (first skip byte above 128); see the pitfalls for a doubt about the operations which keep exactly one of the two characters. The input is processed on a stack, so that new ligatures can be formed with the result of previous ones; strings for which the buffers would overflow make the program fail (FAILED ("string too complex for ligature kerning")). get_xpositions computes the positions of the characters of a string in the same way, optionally without ligatures.

3.The PK loader

struct pk_loader
(decoding of .pk files)

Declared in

Plugins/Metafont/load_pk.hpp

. It is created with the file, the metric and the resolution, and reads the whole file into memory. load_pk () checks the preamble (command 247, format 89), then scans all character packets. For each character of the metric's range with a non-empty bitmap, it creates an empty glyph of the right size and offsets, and records the position and flag byte of the packed bitmap, without decoding it; specials (commands 240 to 244) and no-ops are skipped.

Finally, the logical width lwidth of every glyph is computed in pixels from the .tfm width.

The bitmaps are decoded lazily: the glyphs of a PK font are a pk_font_glyphs_rep (in

load_tex.cpp

), whose get (c) unpacks the bitmap of character c the first time it is requested (pk_loader::unpack, which implements the run-length and dyn_f nybble packing of the format, and the raw bitmap case). Characters outside \([\operatorname{bc}, \operatorname{ec}]\) return an empty glyph.

4.Type 1 substitution

If TeXmacs is built with FreeType (USE_FREETYPE), try_pk first asks tt_find_name (family, size) (

Plugins/Freetype/tt_file.cpp

) for an outline font with the same name. tt_find_name_sub tries familysize, the size divided by 100 for the \(\times 100\) convention, and then a list of usual design sizes (17 for sizes of 15 and more, 12 above 12, 5 to 9 for small sizes, then 10, 700, 1700, 1000 and the bare family name). An outline font exists if tt_font_find finds a file name.pfb (looked up with resolve_tex, hence in the Type 1 path) or, failing that, a .ttf, .ttc, .otf or .dfont file. The result is cached as tt:familysize.

If a substitute is found, the glyphs of the font are the tt_font_glyphs of that file at the requested size and resolution: FreeType rasterizes each glyph into a glyph on request. The face is opened with the Adobe custom character map (ft_select_charmap (face, ft_encoding_adobe_custom) in

tt_face.cpp

), so that character codes are those of the .tfm file. Only the glyphs come from the outline font: the metrics, the ligatures and the kerning still come from the .tfm file.

5.Glyphs

struct glyph_rep
(a bitmap character)

Declared in

Graphics/Bitmap_fonts/bitmap_font.hpp

; the handle glyph is reference counted and may be nil. The fields are the pixel width and height, the offsets xoff and yoff of the reference point (from the left edge and from the top row), the logical width lwidth, the depth (1 for a bitmap, more for grey levels), the extensible piece status, the index of the glyph in its physical font, an artistic flag set by effects, and the raster. One-bit rasters are packed eight pixels per byte, least significant bit first; deeper ones use one byte per pixel. get_x (i, j) and set_x address the raster with the origin at the top left, get (i, j) and set with the origin at the reference point and \(j\) pointing upwards.

font_metric_rep and font_glyphs_rep are the abstract tables of metrics and glyphs indexed by character code, which renderers and the index_glyph mechanism of fonts use (see TeXmacs fonts). For TeX fonts, the metric table is a tfm_font_metric_rep (

tex_font.cpp

), which combines the logical box of the .tfm file with the ink box of the glyph.

Shrinking for display.
Glyphs are rasterized at the resolution of the font (600 dpi at the default zoom), and the screen renderers reduce them by the shrinking factor (see the renderer interface) with shrink (gl, xf, yf, xo, yo) (
Graphics/Bitmap_fonts/glyph_shrink.cpp

). Every black input pixel is first thickened into a small block, whose size grows with the shrinking factor and with the pixel_ratio of high resolution screens (no thickening for glyphs produced by artistic effects); each output pixel then counts the black pixels of the block of input pixels it covers, which gives a grey level used as an alpha value (scaled to at most 64 levels). For ordinary glyphs, the horizontal phase is first adjusted (get_hor_shift) so that columns containing long vertical runs, that is the stems, fall on whole output pixels. For the pieces of extensible characters marked by rubber_fix, the joining row is made as opaque as its neighbour after shrinking (adjust_top, adjust_bot) and the vertical offset is reset, so that the pieces of a large delimiter join without visible seams; note that this adjustment is compiled out in Qt and Qtwk builds (#ifndef QTTEXMACS), and kept with the other ports. The renderers cache the shrunk glyph as an image per character, font, shrinking factor and color (for instance the character_image table of the Qt renderer).

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