JFLib

text/fontconf.rs#

Source: lib/text/fontconf.rs

fontconf -- generic families, substitutes and choices per language: a light fontconfig

  FontConf::new()                  no rules
  FontConf::system()               the rules of this system: on Linux the <alias> and
                                   language rules of fontconfig's files (/etc/fonts/fonts.conf
                                   and what it includes), on Android those of
                                   /system/etc/fonts.xml (familyset) -- or instead the file of
                                   jfgui_FONTCONF (either format, every OS; tests) --, then
                                   the built-in rules of this OS
  conf.parse(text, dir) / conf.load(path) -> bool   add the rules of a fontconfig file (or
                                   of Android's fonts.xml: a <familyset>, see familyset)
  conf.sys_families() -> &[SysFamily]   (fonts.xml) the default family and the fallback
                                   families in the file's order, their fonts as (file, face)
  conf.sys_family(db, file, face) -> Option<String>   the family name of one of those fonts
  conf.add_alias(family, prefer, accept, default)    add an alias
  conf.add_lang(lang, family, mode, families)        add a language rule (LR_*)
  conf.candidates(family, lang) -> Vec<String>       the families to try, best first
  conf.lang_families(lang) -> Vec<String>            what the language puts into
                                   sans-serif beyond what every language gets (fallback.rs
                                   tries them first: Han characters in Japanese forms for ja)
  conf.files() -> usize            fontconfig files read
  is_generic(name)                 GENERICS: sans-serif, serif, monospace, cursive, fantasy,
                                   system-ui, emoji, math (also fontconfig's sans and mono)

FontDb::resolve(&conf, family, lang, weight, italic) (fontdb.rs) finds the first installed
candidate with Latin letters (without any, the first installed one); for a name nothing
installed matches, the candidates of sans-serif.

Rules, fontconfig's semantics in short: an alias for F puts its <prefer> families right
before F (so a later alias's prefer list follows an earlier one), its <accept> families
right after F (a later list before an earlier one), its <default> families at the end.  A
<match target="pattern"> applies when its tests are only lang (eq / contains) and family
(eq) and they hold; its family edits prepend (before F), prepend_first, append (after F),
append_last or assign (in place of F).  Other matches (charset, spacing, prgname, target
font: hinting) are skipped.  The families in the list that have rules of their own are
expanded in place (4 levels), only their <default> and append_last families go to the end
of the whole list.  Layers: the files' rules first, the built-in ones after them;
names compare ASCII-case-insensitively, each once.  Languages: "ja" matches ja and ja-JP;
zh-Hans / zh-CN / zh-SG / plain zh also match zh-cn, zh-Hant / zh-TW zh-tw, zh-HK / zh-MO
zh-hk and zh-tw.  <include> follows files and folders (*.conf in name order) relative to
the including file's folder, prefix="xdg" ($XDG_CONFIG_HOME, else ~/.config) and "~/"; at
most FILES_MAX files of at most 1 MiB.
/// add_lang modes (fontconfig's edit modes)
LR_PREPEND: u8 = 0
LR_PREPEND_FIRST: u8 = 1
LR_APPEND: u8 = 2
LR_APPEND_LAST: u8 = 3
LR_ASSIGN: u8 = 4
/// the generic families
GENERICS: [&str; 10] = ["sans-serif", "serif", "monospace", "cursive", "fantasy" ...
  • fn is_generic(name: &str) -> bool a generic family name (GENERICS)

  • struct SysFamily one family of Android's fonts.xml in its order (sys_families): its languages (the lang attribute's tags as written, "und-Arab", "ja", "zh-Hant"; none for the default family) and its faces as (file name, face index), for sans-serif text and for serif text (fallbackFor="serif"), each once

    • langs: Vec<String>
    • faces: Vec<(String, u32)>
    • serif: Vec<(String, u32)>
  • struct FontConf generic families, substitutes and language choices (see the module header)

impl FontConf

  • fn new() -> FontConf
  • fn system() -> FontConf the rules of this system (see the module header)
  • fn files(&self) -> usize fontconfig files read (Android's fonts.xml counts as one)
  • fn sys_families(&self) -> &[SysFamily] (Android) the families of fonts.xml in its order: the default family (its first named one, sans-serif), then every fallback family (without a name; not those with ignore="true") -- fallback.rs tries them in this order. Empty without such a file
  • fn sys_family(&mut self, db: &crate::fontdb::FontDb, file: &str, index: u32) -> Option<String> (fonts.xml) the family name of face index of a font file it names: the scan's face of that file (db), else the name read from the file itself -- the scan keeps one of several files with the same names (Android links DroidSans.ttf to Roboto-Regular.ttf), so the file may stand there under another name. None: no such font in the font folders
  • fn load(&mut self, path: &str) -> bool add the rules of a fontconfig file (and of what it includes) -> it could be read
  • fn parse(&mut self, text: &str, dir: &str) -> usize add the rules of fontconfig text (includes relative to dir) -> rules found
  • fn add_alias(&mut self, family: &str, prefer: &[&str], accept: &[&str], default: &[&str]) add an alias: prefer before the family, accept after it, default at the end
  • fn add_lang(&mut self, lang: Option<&str>, family: Option<&str>, mode: u8, families: &[&str]) add a language rule (lang None: every language; family None: every family)
  • fn candidates(&self, family: &str, lang: &str) -> Vec<String> the families to try for a name in a language, best first (each once; generic names stay in the list where they stand)
  • fn lang_families(&self, lang: &str) -> Vec<String> the families a language adds to sans-serif (fallback.rs tries them first)

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