text/ttf.rs#
Source: lib/text/ttf.rs
ttf -- sfnt loader for runtime fonts: TrueType (glyf), OpenType (CFF and CFF2 outlines via
cff.rs), collections (TTC / OTC), colour tables (colr.rs) [src/text/ttf.s]
face_count(data) -> faces (1 for a plain sfnt, n for a TTC / OTC) / ERR_*
TtFont::open(data, face, ol, serial) -> the font context / ERR_*
glyph(cp), has(cp), adv(g), kern(g1, g2), kern_table(g1, g2), name(which)
bbox(g, ol), path(g, ol, path, gp) the outline through the outline buffer
sfnt_name(table, id) -> UTF-8 best record: Windows Unicode German (0x0407),
German variants, English (0x0409), other Windows languages, Unicode platform,
Mac Roman English
Errors: ERR_TRUNC (too short / truncated directory), ERR_HDR (not an sfnt, bad head /
maxp, a broken CFF / CFF2 table), ERR_UNSUPPORTED (no usable outlines), ERR_FAIL (face
index out of range), ERR_LIMIT (file >= 2 GB). A font with both: CFF2 before 'CFF ' (like
HarfBuzz and FreeType), 'CFF ' when the CFF2 table does not open.
Tables: head, hhea, hmtx, maxp, loca, glyf (simple + composite: offsets, scale, x/y scale,
2x2, point matching, SCALED_COMPONENT_OFFSET; nesting <= 16, <= 4096 components per
glyph), cmap (formats 0, 4, 6, 12, 13; (3,10) > (0,x) fmt 12 > (3,1) > (0,x) > (3,0)
symbol > (1,0) Mac Roman), OS/2, post, name, kern (version 0 and Apple version 1, format
0), GPOS pair adjustment (lookup type 2 formats 1 and 2, extension type 9; x advance of
the first glyph; 'kern' feature of the DFLT and latn default language systems). Every
offset is checked against its table and the file; malformed glyphs come out empty. Like
GDI / FreeType: glyf outlines are shifted by hmtx lsb - xMin; symbol fonts ((3,0) cmap)
also map U+0000..U+00FF and the Windows-1252 characters to U+F000 + code page byte; a CFF
FontMatrix that is not exactly 1/unitsPerEm scales the outline.
Variable fonts (var.rs): fvar / avar / gvar / HVAR / MVAR / GDEF's store are read at open
(var); the plain calls give the default instance, the *_var calls an instance at
normalized coordinates (registry.rs: VarFont):
prep_var(g, coords, serial, ol) the outline with gvar deltas (simple glyphs: points with
IUP; composites: the component offsets), shifted by its varied phantom point pp1; a
CFF2 font's charstrings with blend at coords (cff.rs)
adv_var(g, coords) hmtx + HVAR; without HVAR: hmtx + the phantom points'
deltas (adv_table: every glyph at once)
kern_var(g1, g2, coords) GPOS pair values + their VariationIndex deltas (GDEF)
metrics_var(coords, wght, ol) FONTMETRICS + header with MVAR deltas (hasc / hdsc /
hlgp on hhea and OS/2, hcla, hcld, xhgt, cpht, undo, unds, stro, strs)
A CFF2 font's advances: hmtx + HVAR (CFF2 has no phantom points); without fvar its outlines
are the default's.
ERR_FAIL: i32 = -1
ERR_TRUNC: i32 = -1
ERR_HDR: i32 = -2
ERR_NOMEM: i32 = -8
ERR_UNSUPPORTED: i32 = -9
ERR_IO: i32 = -13
ERR_LIMIT: i32 = -15
FILE_MAX: u64 = 0x7FFF_FFF0
NAME_CAP: usize = 256
// table indices (TT_*)
TT_HEAD: usize = 0
TT_HHEA: usize = 1
TT_HMTX: usize = 2
TT_MAXP: usize = 3
TT_LOCA: usize = 4
TT_GLYF: usize = 5
TT_CMAP: usize = 6
TT_OS2: usize = 7
TT_POST: usize = 8
TT_NAME: usize = 9
TT_KERN: usize = 10
TT_GPOS: usize = 11
TT_CFF: usize = 12
TT_COLR: usize = 13
TT_CPAL: usize = 14
TT_CBDT: usize = 15
TT_CBLC: usize = 16
TT_FVAR: usize = 17
TT_CFF2: usize = 18
TT_AVAR: usize = 20
TT_GVAR: usize = 21
TT_HVAR: usize = 22
TT_MVAR: usize = 23
TT_GDEF: usize = 24
TT_COUNT: usize = 25
TFK_TTF: u32 = 1
TFK_CFF: u32 = 2
TFF_LOCA_LONG: u32 = 2
TFF_SYMBOL: u32 = 4
TFF_COLR: u32 = 8
TFF_CBDT: u32 = 16
TFF_MACCMAP: u32 = 32
static MAC_ROMAN: [u16; 128]Mac OS Roman 0x80..0xFF -> Unicodestruct Tb<'a>(pub &'a [u8])bounds-checked big-endian reads of one table (t16 / t32 of the assembly: -1 when the read would cross the table end; offsets are u32 and wrap like the 32-bit registers)
impl Tb<'_>
fn len(&self) -> u32fn u8(&self, off: u32) -> i32fn u16(&self, off: u32) -> i32t16 -> u16 or -1fn u32(&self, off: u32) -> i64t32 -> u32 or -1fn face_count(d: &[u8]) -> i32font_face_count(data) -> faces / ERR_*fn sfnt_name(tb: Tb, id: u32, cap: usize) -> Vec<u8>sfnt_name(table, id) -> UTF-8 (empty = not found), at most cap - 1 bytesstruct TtFonta runtime font context (TrueType or CFF outlines)face_index: u32data: Vec<u8>hdr: Hdrfm: Metricsserial: u64-- outline buffer owner identitykind: u32flags: u32nglyphs: u32upem: u32tab: [(u32, u32); TT_COUNT]names: [Vec<u8>; 5]cpal: u32cpaln: u32
impl TtFont
fn open(data: Vec<u8>, face: u32, ol: &mut Outline, serial: u64) -> Result<Box<TtFont>, i32>ttf_open(data, face): the context, or ERR_*fn cff(&self) -> Option<&Cff>glyph for cp (0 = .notdef). Symbol fonts ((3,0) cmap) map like GDI: U+0000..U+00FF and the Windows-1252 characters to U+F000 + their code page byte [ttf_glyph] the CFF or CFF2 outlines of an OpenType font (None: TrueType outlines)fn glyph(&self, cp: u32) -> u32fn has(&self, cp: u32) -> boolfn adv(&self, g: u32) -> i32advance (hmtx; the last long metric repeats) [ttf_adv]fn kern_table(&self, g1: u32, g2: u32) -> i32sum of the 'kern' format 0 values [tt_kern_table]fn kern(&self, g1: u32, g2: u32) -> i32GPOS 'kern' when present, else 'kern' [ttf_kern]fn name(&self, which: u32) -> &[u8]font_name of this font ("" for an unknown id) [ttf_name]fn prep(&self, glyph: u32, ol: &mut Outline) -> boolthe outline of glyph into the buffer (owner set either way): true = not empty [ttf_prep]fn bbox(&self, g: u32, ol: &mut Outline) -> Option<[i32; 4]>control box (font units) and the prepared outline [ttf_box]fn path(&self, g: u32, ol: &mut Outline, path: &mut Path, gp: &Gp)append the outline to the path with the gp transform [ttf_path]