First commit. Version 0.1.0
This commit is contained in:
133
src/system_fonts.rs
Normal file
133
src/system_fonts.rs
Normal file
@@ -0,0 +1,133 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-only
|
||||
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>
|
||||
|
||||
//! Lazy per-glyph fallback font resolution.
|
||||
//!
|
||||
//! The default font ([`crate::theme::fallback::FALLBACK_FONT`], Sora)
|
||||
//! covers Latin and a portion of extended Latin; everything outside
|
||||
//! that band — Cyrillic, Devanagari, Arabic, Hebrew, Thai, the CJK
|
||||
//! ideographic block — relies on a chain of system Noto fonts loaded
|
||||
//! on demand: a fallback file is read off disk the first time some
|
||||
//! glyph asks for it and the resulting `Arc<Font>` is cached
|
||||
//! process-wide.
|
||||
//!
|
||||
//! Each `(path, face)` entry in [`FALLBACK_FONT_CANDIDATES`] gets its
|
||||
//! own `OnceLock<Option<Arc<Font>>>` slot. `Some(font)` means the
|
||||
//! file was read and parsed successfully; `None` means it's missing
|
||||
//! or unparseable, locked in for the rest of the process so we don't
|
||||
//! re-`stat` the same path on every glyph miss. The `OnceLock`
|
||||
//! handles concurrent first-loads correctly: at most one thread runs
|
||||
//! the closure for a given slot, the rest wait for the result.
|
||||
//!
|
||||
//! Renderers (`SoftwareCanvas` and `GlesCanvas`) consult this module
|
||||
//! through [`lookup`] in their `font_for_char` paths.
|
||||
|
||||
use std::sync::{ Arc, OnceLock };
|
||||
|
||||
use fontdue::{ Font, FontSettings };
|
||||
|
||||
/// Per-script fallback font path with the TrueType-collection face
|
||||
/// index fontdue should load (most files are single-face → 0; CJK
|
||||
/// `.ttc` archives carry many faces, see the SC face on the canonical
|
||||
/// Adobe-built `NotoSansCJK-Regular.ttc`).
|
||||
struct FallbackFontSpec
|
||||
{
|
||||
path: &'static str,
|
||||
face: u32,
|
||||
}
|
||||
|
||||
/// Ordered fallback chain consulted on a glyph miss. Order matters:
|
||||
/// the first slot whose font owns a non-zero glyph index for the
|
||||
/// codepoint wins, so the broadest families come first (Noto Sans
|
||||
/// covers Cyrillic, Greek, extended Latin) and the script-specific
|
||||
/// + CJK packs trail. DejaVu is the last resort — most distros carry
|
||||
/// it under one path or another.
|
||||
const FALLBACK_FONT_CANDIDATES: &[ FallbackFontSpec ] =
|
||||
&[
|
||||
// Noto Sans — Cyrillic, Greek, extended Latin.
|
||||
FallbackFontSpec { path: "/usr/share/fonts/truetype/noto/NotoSans-Regular.ttf", face: 0 },
|
||||
FallbackFontSpec { path: "/usr/share/fonts/noto/NotoSans-Regular.ttf", face: 0 },
|
||||
|
||||
// Devanagari (Hindi).
|
||||
FallbackFontSpec { path: "/usr/share/fonts/truetype/noto/NotoSansDevanagari-Regular.ttf", face: 0 },
|
||||
FallbackFontSpec { path: "/usr/share/fonts/noto/NotoSansDevanagari-Regular.ttf", face: 0 },
|
||||
|
||||
// Arabic, Hebrew, Thai — common scripts cheap to keep.
|
||||
FallbackFontSpec { path: "/usr/share/fonts/truetype/noto/NotoSansArabic-Regular.ttf", face: 0 },
|
||||
FallbackFontSpec { path: "/usr/share/fonts/truetype/noto/NotoSansHebrew-Regular.ttf", face: 0 },
|
||||
FallbackFontSpec { path: "/usr/share/fonts/truetype/noto/NotoSansThai-Regular.ttf", face: 0 },
|
||||
|
||||
// CJK packs ship as `.ttc`. The collection layout on the canonical
|
||||
// Adobe-built `NotoSansCJK-Regular.ttc` is 0=JP, 1=KR, 2=SC, 3=TC,
|
||||
// 4=HK; we want CJK shared ideographs available in any locale, so
|
||||
// any of the faces is fine — pick SC (face 2) as the broadest
|
||||
// baseline. ~30 MB on disk; under the lazy loader this only fires
|
||||
// when a user-visible string actually contains a CJK codepoint.
|
||||
FallbackFontSpec { path: "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc", face: 2 },
|
||||
FallbackFontSpec { path: "/usr/share/fonts/opentype/noto-cjk/NotoSansCJK-Regular.ttc", face: 2 },
|
||||
FallbackFontSpec { path: "/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc", face: 2 },
|
||||
|
||||
// Last resort — DejaVu has broad-but-shallow coverage of most
|
||||
// scripts and ships almost everywhere.
|
||||
FallbackFontSpec { path: "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", face: 0 },
|
||||
];
|
||||
|
||||
/// Per-slot lazy state. `Vec` length matches
|
||||
/// [`FALLBACK_FONT_CANDIDATES`]; each `OnceLock` resolves
|
||||
/// independently so the act of looking up a Devanagari codepoint
|
||||
/// doesn't drag the CJK pack into memory. `None` inside a resolved
|
||||
/// slot means the file was missing or fontdue rejected it — a sticky
|
||||
/// negative result so subsequent misses skip the slot in O(1).
|
||||
fn slots() -> &'static [ OnceLock<Option<Arc<Font>>> ]
|
||||
{
|
||||
static SLOTS: OnceLock<Vec<OnceLock<Option<Arc<Font>>>>> = OnceLock::new();
|
||||
SLOTS.get_or_init( ||
|
||||
{
|
||||
( 0..FALLBACK_FONT_CANDIDATES.len() )
|
||||
.map( |_| OnceLock::new() )
|
||||
.collect()
|
||||
} )
|
||||
}
|
||||
|
||||
/// Try to load and parse the fallback font at slot `idx`. Each
|
||||
/// `OnceLock` wraps `Option<Arc<Font>>` so a missing or malformed
|
||||
/// file is recorded as `None` and never re-attempted.
|
||||
fn slot_font( idx: usize ) -> Option<Arc<Font>>
|
||||
{
|
||||
let slot = &slots()[ idx ];
|
||||
slot.get_or_init( ||
|
||||
{
|
||||
let spec = &FALLBACK_FONT_CANDIDATES[ idx ];
|
||||
let bytes = std::fs::read( spec.path ).ok()?;
|
||||
let opts = FontSettings { collection_index: spec.face, ..FontSettings::default() };
|
||||
Font::from_bytes( bytes.as_slice(), opts ).ok().map( Arc::new )
|
||||
} )
|
||||
.as_ref()
|
||||
.map( Arc::clone )
|
||||
}
|
||||
|
||||
/// Find the first fallback font that has a non-zero glyph index for
|
||||
/// `ch`, loading it from disk on the first hit and caching the
|
||||
/// `Arc<Font>` for the rest of the process. Returns `None` if no
|
||||
/// installed fallback covers the codepoint — the caller then paints
|
||||
/// the primary font's `.notdef` rather than dropping the glyph.
|
||||
///
|
||||
/// Side effect: walking the chain may load and cache a slot even if
|
||||
/// it doesn't end up covering `ch` (`lookup_glyph_index` reads the
|
||||
/// `cmap` table, which requires the font to be parsed). That's
|
||||
/// acceptable — the slot is cached on the first encounter regardless,
|
||||
/// and most coverage gaps in early slots are the small Noto Sans
|
||||
/// scripts (Devanagari, Arabic, …) whose total weight is a fraction
|
||||
/// of the CJK pack everyone was paying for unconditionally.
|
||||
pub fn lookup( ch: char ) -> Option<Arc<Font>>
|
||||
{
|
||||
for idx in 0..FALLBACK_FONT_CANDIDATES.len()
|
||||
{
|
||||
let Some( font ) = slot_font( idx ) else { continue };
|
||||
if font.lookup_glyph_index( ch ) != 0
|
||||
{
|
||||
return Some( font );
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
Reference in New Issue
Block a user