JFLib

text/registry.rs#

Source: lib/text/registry.rs

registry -- the font registry: embedded SGF1 fonts, runtime fonts (ttf.rs) and the derived
fonts of font_style (synthetic bold / italic / sharp)       [src/text/font.s, outline.s]

  register(data) -> id / -1            add an SGF1 font
  load_mem(data, face) / load_file(path, face) -> id / ERR_*   runtime fonts (ttf.rs)
  style(font, flags) -> id / ERR_*     derived font with FS_SYN_BOLD (outline dilated by
       upem/30, advance + upem/30), FS_SYN_ITALIC (x += y * tan 12 deg) and/or FS_SHARP
       (vertical grid fitting); flags 0 = font itself; the same (font, flags) pair always
       gives the same id
  unload(id) -> 0 / -1                 a runtime font and every font derived from it; the
       glyph cache is flushed; the id may be reused later
  count(), valid(id), hdr(font) (font_data), glyph(font, cp), has, adv, kern, kern_table,
  upem, metrics, metric, is_color, name, layers, glyph_box, glyph_path
Variable fonts (var.rs):
  var_axes(font) / var_instances(font)   fvar's axes and named instances (empty: not variable)
  var_values(font) -> user values per axis (an instance's, else the defaults)
  vary(font, &[(tag, value)]) -> id / ERR_*   an instance (VarFont: the base font at
       normalized coordinates; own id, serial and metrics, so glyph cache entries, outline
       buffer and shaping engines never mix instances); axes not named keep font's values;
       all-default coordinates give the variable font itself; the same coordinates the same
       id; synthetic styles of font carry over; unload(base) unloads its instances
  vary_named(font, i) -> id / ERR_*    named instance i

Font ids 0..3 are the embedded fonts (never reused).  An unknown or unloaded id behaves
like font 0.
FONT_EMBEDDED: u32 = 4  // ids below this are the embedded fonts (never unloaded)
FONT_MAX_IDS: u32 = 4096  // registry capacity
  • struct SynFont a font derived by font_style (TFK_SYN context)
    • hdr: Hdr
    • fm: Metrics
    • base: u32
    • syn: u32
    • strength: f32 -- embolden strength in font units
    • serial: u64

impl Text

  • fn register(&mut self, data: &'static [u8]) -> i32 font_register(data) -> id / -1
  • fn set_embedded(&mut self, fonts: [&'static [u8]; 4], names: [[&'static str; 5]; 4]) replace the embedded fonts 0..3 (SGF1 or TrueType / OpenType; a style switched at run time): the ids keep their meaning, fonts derived from them (style) take the new metrics, cached glyphs are dropped; each face starts with the legacy raster policy
  • fn face_count(data: &[u8]) -> i32 font_face_count(data) -> faces (1 for a plain sfnt, n for a TTC / OTC) / ERR_*
  • fn load_mem(&mut self, data: &[u8], face: u32) -> i32 font_load_mem(data, face) -> id / ERR_* (the data is copied)
  • fn load_file(&mut self, path: &str, face: u32) -> i32 font_load_file(path, face) -> id / ERR_* (ERR_IO when it cannot be read)
  • fn unload(&mut self, id: u32) -> i32 font_unload(id) -> 0 / -1
  • fn count(&self) -> u32 font_count() -> ids in use are below this value
  • fn valid(&self, id: u32) -> bool font_valid(id): the id names a registered font
  • fn hdr(&self, font: u32) -> Hdr font_data: the SGF1-compatible header (an unknown id gives font 0)
  • fn upem(&self, font: u32) -> i32
  • fn glyph(&self, font: u32, cp: u32) -> u32 font_glyph(font, cp) -> glyph (SGF1: fallback glyph, runtime: 0 = .notdef)
  • fn has(&self, font: u32, cp: u32) -> bool font_has(font, cp)
  • fn adv(&self, font: u32, g: u32) -> i32 font_adv(font, glyph): advance in font units (synthetic bold included)
  • fn kern(&self, font: u32, g1: u32, g2: u32) -> i32 font_kern(font, g1, g2): kerning in font units (signed)
  • fn kern_table(&self, font: u32, g1: u32, g2: u32) -> i32 font_kern_table: the 'kern' table only (runtime fonts; others: font_kern)
  • fn is_color(&self, font: u32) -> bool font_is_color: COLR layers or CBDT bitmaps
  • fn layers(&self, font: u32, g: u32, cap: usize, out: &mut Vec<(u32, u32)>) font_layers(font, glyph, cap) into out: {glyph | LAYER_FG, argb}; empty = draw it normally
  • fn name(&self, font: u32, which: u32) -> &[u8] font_name(font, FN_*) -> UTF-8 ("" when unknown)
  • fn metrics(&self, font: u32) -> Option<Metrics> font_metrics(font) -> FONTMETRICS (None when no font exists)
  • fn metric(&self, font: u32, field: u32) -> i32 font_metric(font, FM_*) -> one FONTMETRICS field
  • fn glyph_box(&mut self, font: u32, g: u32) -> Option<[i32; 4]> glyph_box(font, glyph) -> {xmin, ymin, xmax, ymax} (font units, control box); None = empty. Runtime fonts leave the glyph's outline in the outline buffer.
  • fn glyph_path(&mut self, font: u32, g: u32) glyph_path(font, glyph): append the outline to the path, transformed by gp (origin x / y in float pixels, sx / sy float pixels per unit)
  • fn var_axes(&self, font: u32) -> Vec<VarAxis> the variation axes of a font (fvar; empty: not a variable font); instances and the fonts derived from them report their variable font's
  • fn var_instances(&self, font: u32) -> Vec<VarInstance> the named instances of a variable font (fvar; empty: none or not variable)
  • fn var_values(&self, font: u32) -> Vec<f32> the axis values of a font in user units, one per axis of var_axes (an instance's own, else the defaults)
  • fn var_coords(&self, font: u32) -> Vec<i16> the normalized coordinates of an instance: F2Dot14 bits per axis with avar applied (what HarfBuzz's hb_font_get_var_coords_normalized reports); empty for a variable font itself (its default instance) and for fonts that are not variable
  • fn vary(&mut self, font: u32, values: &[([u8; 4], f32)]) -> i32 an instance of a variable font with axis values in user units (tag, value): axes not named keep font's values (an instance's, else the defaults); values are clamped to their axis, unknown tags ignored -> font id / ERR_*. All-default coordinates give the variable font itself, the same coordinates the same id; synthetic styles of font (style) carry over; a font that is not variable gives itself. Each instance has its own glyph cache entries and metrics (MVAR); unloading the variable font unloads its instances.
  • fn vary_named(&mut self, font: u32, i: usize) -> i32 named instance i of a variable font (var_instances) -> id / ERR_* (ERR_FAIL: no such instance)
  • fn style(&mut self, font: u32, flags: u32) -> i32 font_style(font, FS_*) -> id / ERR_*

Source: docs/API.md in the JFLib repository.