JFLib

text/fontdb.rs#

Source: lib/text/fontdb.rs

fontdb -- system font database: enumerate the installed fonts, group them into families,
match styles, cache the result                                      [src/text/fontdb.s]

  scan(flags) -> faces found      walk the font folders (recursively, depth <= 8) and read
       only the table directory, name, OS/2 and head of every .ttf / .otf / .ttc / .otc
       (faces with glyf, CFF, CFF2 or CBDT outlines; every face of a collection);
       files already in a loaded cache with the same size are not opened again (flags
       FDB_RESCAN: ignore the cache).  Folders: jfgui_FONTDIRS (';' separated, on Linux
       also ':'), else Windows %WINDIR%/Fonts and %LOCALAPPDATA%/Microsoft/Windows/Fonts;
       Linux /usr/share/fonts, /usr/local/share/fonts, $XDG_DATA_HOME/fonts (else
       ~/.local/share/fonts), ~/.fonts; macOS and iOS /System/Library/Fonts, /Library/Fonts,
       ~/Library/Fonts (directories are visited once: symlink loops)
  scan_dirs(dirs, flags) -> faces found   the same in these folders only (tests, apps with
       their own font folders)
  resolve(conf, family, lang, weight, italic) -> (face, FS_SYN_*, family)   the first installed
       of the FontConf's candidates (generic families, substitutes, the language's choices;
       fontconf.rs), else sans-serif's; load_resolved(text, conf, ...) -> (font id, family)
       (a variable face: its instance for weight / italic, load_var)
  clear()                         forget the scan result (the cache stays)
  family_count(), family_name(i), family_faces(i) -> &[FdFace], family_range(i)   families
       sorted case-insensitively
  face_count(), face(i)           every face, family order
  find(family, weight, italic) -> (face index, FS_SYN_* flags to add)   family = legacy
       family name (FN_FAMILY, like GDI), else a typographic family; closest weight,
       italic before upright; synthetic bold / italic asked when the family lacks them
  load(text, family, weight, italic) -> font id / ERR_*   find + load_file (+ style; a
       variable face: load_var); each (face, style) is loaded once until the next scan
  variations(face) -> Option<&FdVar>   a variable face's axes and named instances (fvar,
       names from its name table; read from the file on first use, kept until the next scan)
  var_values(face, weight, italic, size_pt, axes) -> (values, FS_SYN_* still needed)   the
       axis values of a choice: the axes named, else weight -> wght, italic -> ital = 1 or
       slnt = its most slanted value, the size in points -> opsz (clamped to each axis);
       synthetic bold / italic only where no axis gives them
  load_var(text, face, syn, weight, italic, size_pt, axes) -> font id / ERR_*   load_face +
       Text::vary at var_values (a face that is not variable: load_face)
  save_cache(path) / load_cache(path) -> 0 / -1

Families group faces by the legacy family name (name ID 1); names prefer German, then
English.  Cache file (little endian): "FDB1", u32 version 4, u32 files, u32 faces, u32
pool bytes, u64 FNV-1a hash of everything after the 28-byte header; files {u32 path, u32
size, u32 first face, u32 faces}; faces {u32 file, u32 face index, u16 weight, u16 flags,
u32 family, u32 style, u32 full, u32 typo family, u32 0}; pool of NUL-terminated UTF-8
strings (offsets above point into it).  Entries are validated by path and file size.
Version 3: CFF2 faces count (a version 2 cache lists CFF2-only files without faces, so it
is not read).  Version 4: FDFF_NOLATIN.
FDB_RESCAN: u32 = 1
FDFF_ITALIC: u16 = 1
FDFF_BOLD: u16 = 2
FDFF_OBLIQUE: u16 = 4
FDFF_CFF: u16 = 8
FDFF_COLOR: u16 = 16
FDFF_VAR: u16 = 32
FDFF_SYMBOL: u16 = 64
/// CFF2 outlines (FDFF_CFF is set as well)
FDFF_CFF2: u16 = 128
/// the cmap lacks A or a: a font for another script (see FontDb::resolve)
FDFF_NOLATIN: u16 = 256
  • struct FdFace a face of the database (FDFACE)

    • path: String
    • style: String -- FN_STYLE
    • full: String -- FN_FULL
    • family: String -- FN_FAMILY, the grouping key
    • tfamily: String -- FN_TYPO_FAMILY
    • face: u32 -- face index inside the file (TTC / OTC)
    • weight: u16 -- usWeightClass
    • flags: u16 -- FDFF_*
  • struct FdVar the variations of an installed variable face (FontDb::variations)

    • axes: Vec<VarAxis> -- fvar's axes (names from the face's name table)
    • instances: Vec<VarInstance> -- its named instances
  • struct FontDb

  • fn file_family(path: &str, face: u32) -> Option<String> the family name (name ID 1, as a scan groups the face) of face face of a font file, read from its name table only -> None for a file that is no font or has no such face (fontconf.rs: the families of Android's fonts.xml, which names files)

  • fn fnv64(d: &[u8]) -> u64 FNV-1a 64 (hash_fnv64, seed 0 = the offset basis)

impl FontDb

  • fn new() -> FontDb
  • fn clear(&mut self) fontdb_clear(): forget the scan result (the cache stays)
  • fn family_count(&self) -> usize
  • fn family_name(&self, i: usize) -> &str the family's name ("" when i is out of range)
  • fn family_range(&self, i: usize) -> core::ops::Range<usize> the indices (face(i), variations(i)) of family i's faces (empty when out of range)
  • fn family_faces(&self, i: usize) -> &[FdFace] the faces of family i (empty when out of range)
  • fn face_count(&self) -> usize
  • fn face(&self, i: usize) -> Option<&FdFace>
  • fn face_by_file(&self, file: &str, index: u32) -> Option<usize> the face index of the font file named file (its name in a folder of the scan, as Android's fonts.xml names it; ASCII case-insensitive) -> face index
  • fn scan(&mut self, flags: u32) -> usize fontdb_scan(flags) -> number of faces
  • fn scan_dirs(&mut self, dirs: &[&str], flags: u32) -> usize scan these folders instead of the system's -> number of faces
  • fn find(&self, family: &str, weight: u32, italic: bool) -> Option<(usize, u32)> fontdb_find(family, weight, italic) -> (face index, FS_SYN_* flags to add)
  • fn resolve(&self, conf: &FontConf, family: &str, lang: &str, weight: u32, italic: bool) -> Option<(usize, u32, String)> the first installed candidate of conf for family in lang (generic families, substitutes, the language's choices), else sans-serif's -> (face index, FS_SYN_* flags, the family name found). A substitute without Latin letters (FDFF_NOLATIN) only counts when nothing else fits, except for emoji and math: fontconfig ranks the language above such a rule's family, and Fedora's rules put Noto Sans Arabic first in sans-serif and monospace. Another script's characters come from fallback.rs
  • fn load_resolved(&mut self, text: &mut Text, conf: &FontConf, family: &str, lang: &str, weight: u32, italic: bool) -> (i32, String) resolve and load -> (font id / ERR_*, the family found)
  • fn load(&mut self, text: &mut Text, family: &str, weight: u32, italic: bool) -> i32 fontdb_load(text, family, weight, italic) -> font id / ERR_*
  • fn load_face(&mut self, text: &mut Text, fi: usize, syn: u32) -> i32 load face fi with the synthetic styles syn (FS_SYN_*) -> font id / ERR_*; each (face, style) once until the next scan
  • fn variations(&mut self, i: usize) -> Option<&FdVar> the axes and named instances of face i (None: not a variable face, or its file cannot be read); read from the file on first use
  • fn var_values(&mut self, i: usize, weight: u32, italic: bool, size_pt: Option<f32>, axes: &[([u8; 4], f32)]) -> Option<(Vec<f32>, u32)> the axis values of a choice on variable face i (see the module header) -> (one value per axis, the FS_SYN_* flags still needed); None: not a variable face
  • fn load_var(&mut self, text: &mut Text, fi: usize, syn: u32, weight: u32, italic: bool, size_pt: Option<f32>, axes: &[([u8; 4], f32)]) -> i32 load variable face fi at the axis values of a choice (var_values) with the synthetic styles syn asks for that no axis gives -> font id / ERR_*; a face that is not variable: load_face(fi, syn)
  • fn save_cache(&self, path: &str) -> i32 fontdb_save_cache(path) -> 0 / -1
  • fn load_cache(&mut self, path: &str) -> i32 fontdb_load_cache(path) -> 0 / -1 (a malformed file is rejected as a whole)

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