FontCache
Mojo struct 🡭
FontCache
@memory_only
struct FontCacheConstruct one, then pass it by cache= into draw_text/measure_text/measure_text_block for every call reusing the same fonts. No cleanup, and no setup: the one font scan happens on the first lookup, and every later lookup skips it.
Implemented traits
AnyType, Deinitable, Movable
Methods
__init__
fn def __init__(out self)Scans nothing. The installed fonts are read on the first lookup that misses the path dictionaries – see this module’s docstring for why once, and why then.
Args:
- self (
Self)
Returns:
Self
has_scanned
fn def has_scanned(self) -> BoolWhether the installed fonts have been scanned yet: False after construction, True from the first lookup that needed them.
Args:
- self (
Self)
Returns:
Bool: True once the scan has happened.
glyph_mask_count
fn def glyph_mask_count(self) -> IntHow many rasterized glyph masks the cache holds.
Args:
- self (
Self)
Returns:
Int: The number of distinct (face, size, codepoint, sub-pixel
offset) masks cached so far, across both generations.
glyph_mask_bytes
fn def glyph_mask_bytes(self) -> IntHow much the rasterized-glyph store is holding.
Counts each entry’s mask, its key, and a fixed charge for the
dictionary slot around them, across both generations. Stays
under _GLYPH_MASK_BUDGET except while a single mask larger
than a generation’s share is live, which is possible only for
very large text and lasts one turnover.
Args:
- self (
Self)
Returns:
Int: Bytes currently held.
glyph_mask_turnovers
fn def glyph_mask_turnovers(self) -> IntHow many times the store has released a generation.
Zero until a run of distinct glyphs reaches one of the two bounds. Useful for confirming that a workload actually exercises eviction rather than merely fitting.
Args:
- self (
Self)
Returns:
Int: The number of turnovers so far.
clear_glyph_masks
fn def clear_glyph_masks(mut self)Release every rasterized glyph mask, keeping the resolved font paths and faces. Later draws rasterize their masks again.
Args:
- self (
Self)
resolve
fn def resolve(mut self, family: String, slant: FontSlant, weight: FontWeight) -> StringCached resolve_font_file: the scanned font database is matched once per distinct (family, slant, weight), and every later call for that combination reads the path from the Dict.
Args:
- self (
Self) - family (
String): Font family name or generic alias. - slant (
FontSlant): Requested upright/italic/oblique style. - weight (
FontWeight): Requested normal/bold weight.
Returns:
String: The matched font’s absolute file path.
Raises:
Error: no fonts are installed on this machine.
resolve_for_char
fn def resolve_for_char(mut self, family: String, slant: FontSlant, weight: FontWeight, codepoint: Int) -> StringCached resolve_font_file_for_char, keyed additionally on codepoint, since a charset-constrained match can return a different font than the unconstrained one.
Args:
- self (
Self) - family (
String): Font family name or generic alias. - slant (
FontSlant): Requested upright/italic/oblique style. - weight (
FontWeight): Requested normal/bold weight. - codepoint (
Int): Unicode codepoint the matched font should contain.
Returns:
String: The matched font’s absolute file path.
Raises:
Error: no fonts are installed on this machine.
resolve_face
fn def resolve_face(mut self, family: String, slant: FontSlant, weight: FontWeight, size: Float64) -> ArcPointer[TTFFace]resolve plus _face_for_path in one call. draw_text calls it twice per invocation, so with a shared cache the second is a hit.
Args:
- self (
Self) - family (
String): Font family name or generic alias. - slant (
FontSlant): Requested upright/italic/oblique style. - weight (
FontWeight): Requested normal/bold weight. - size (
Float64): Pixel size to rasterize glyphs at.
Returns:
ArcPointer[TTFFace]: The resolved, sized font face, shared across every caller
requesting the same (path, size).
Raises:
Error: no fonts are installed on this machine, or the resolved file can’t be parsed.
resolve_face_for_char
fn def resolve_face_for_char(mut self, family: String, slant: FontSlant, weight: FontWeight, codepoint: Int, size: Float64) -> ArcPointer[TTFFace]resolve_face’s fallback-glyph counterpart. resolve_for_char deduplicates the path lookup; this also stops each fallback glyph from re-parsing the file.
Args:
- self (
Self) - family (
String): Font family name or generic alias. - slant (
FontSlant): Requested upright/italic/oblique style. - weight (
FontWeight): Requested normal/bold weight. - codepoint (
Int): Unicode codepoint the matched font should contain. - size (
Float64): Pixel size to rasterize glyphs at.
Returns:
ArcPointer[TTFFace]: The resolved, sized font face, shared across every caller
requesting the same (path, size).
Raises:
Error: no fonts are installed on this machine, or the resolved file can’t be parsed.