diff --git a/crates/text/src/fallback.rs b/crates/text/src/fallback.rs index d9fc2283..eb84167c 100644 --- a/crates/text/src/fallback.rs +++ b/crates/text/src/fallback.rs @@ -9,6 +9,7 @@ use crate::Fonts; use skrifa::MetadataProvider as _; use std::collections::HashMap; +use std::sync::Arc; /// Families tried, in order, for characters a run's own face doesn't have: faces with wide /// coverage that come with Windows, macOS and common Linux desktops, then the system faces for @@ -100,13 +101,19 @@ impl<'f> Coverage<'f> { pub(crate) fn has(&mut self, id: fontdb::ID, c: char) -> bool { let fonts = self.fonts; - *self.memo.entry((id, c)).or_insert_with(|| fonts.db.with_face_data(id, |data, index| crate::face_has(data, index, c)).unwrap_or(false)) + *self.memo.entry((id, c)).or_insert_with(|| fonts.covers(id, c)) } } impl Fonts { - /// The faces of [`FALLBACK_FAMILIES`] on this computer in a weight and slant, in order. - pub(crate) fn fallback_list(&self, bold: bool, italic: bool) -> Vec { + /// The faces of [`FALLBACK_FAMILIES`] on this computer in a weight and slant, in order + /// (found once for each). + pub(crate) fn fallback_list(&self, bold: bool, italic: bool) -> Arc<[fontdb::ID]> { + if let Ok(known) = self.lists.lock() + && let Some(list) = known.get(&(bold, italic)) + { + return list.clone(); + } let mut out: Vec = Vec::new(); for name in FALLBACK_FAMILIES { let q = fontdb::Query { @@ -121,6 +128,10 @@ impl Fonts { out.push(id); } } + let out: Arc<[fontdb::ID]> = out.into(); + if let Ok(mut known) = self.lists.lock() { + known.insert((bold, italic), out.clone()); + } out } @@ -144,6 +155,24 @@ impl Fonts { found } + /// Whether face `id` has a glyph for `c`: read from the font once and remembered, so text + /// that keeps needing a fallback (every keystroke lays the block out again) doesn't open + /// the fallback faces each time. + pub(crate) fn covers(&self, id: fontdb::ID, c: char) -> bool { + if let Ok(known) = self.covered.lock() + && let Some(has) = known.get(&(id, c)) + { + return *has; + } + let has = self.db.with_face_data(id, |data, index| crate::face_has(data, index, c)).unwrap_or(false); + if let Ok(mut known) = self.covered.lock() + && known.len() < MAX_REMEMBERED * 16 + { + known.insert((id, c), has); + } + has + } + /// A face's family name. pub(crate) fn family_of(&self, id: fontdb::ID) -> String { self.db.face(id).and_then(|f| f.families.first().map(|(n, _)| n.clone())).unwrap_or_default() diff --git a/crates/text/src/lib.rs b/crates/text/src/lib.rs index 4b768066..529dddbb 100644 --- a/crates/text/src/lib.rs +++ b/crates/text/src/lib.rs @@ -467,9 +467,16 @@ pub struct Fonts { db: fontdb::Database, families: Vec, /// Which face, if any, has each character the fallback list didn't (`Fonts::any_face_with`). - wide: std::sync::Mutex>>, + wide: Remembered>, + /// The fallback families' faces by weight and slant (`Fonts::fallback_list`), found once. + lists: Remembered<(bool, bool), Arc<[fontdb::ID]>>, + /// Whether a face has a character, for the letters fallback asked about (`Fonts::covers`). + covered: Remembered<(fontdb::ID, char), bool>, } +/// Answers about the fonts that are worked out once and kept (ADR 0130). +type Remembered = std::sync::Mutex>; + static FONTS: OnceLock = OnceLock::new(); static EXTRA: OnceLock>> = OnceLock::new(); @@ -504,7 +511,7 @@ impl Fonts { let mut families: Vec = db.faces().filter_map(|f| f.families.first().map(|(n, _)| n.clone())).filter(|n| !n.starts_with('.')).collect(); families.sort_by_key(|f| f.to_lowercase()); families.dedup(); - Fonts { db, families, wide: std::sync::Mutex::default() } + Fonts { db, families, wide: Remembered::default(), lists: Remembered::default(), covered: Remembered::default() } } /// A library of just these faces (tests, and documents carrying their own fonts). diff --git a/crates/text/src/rich.rs b/crates/text/src/rich.rs index a01b3868..f6d3adf3 100644 --- a/crates/text/src/rich.rs +++ b/crates/text/src/rich.rs @@ -480,7 +480,7 @@ impl Fonts { let had = drawn.iter().filter(|c| cover.has(own, **c)).count(); let mut chosen: Option = None; if had < drawn.len() { - let candidates = lists.entry((st.bold, st.italic)).or_insert_with(|| self.fallback_list(st.bold, st.italic).into_iter().filter(|id| *id != own).collect()); + let candidates = lists.entry((st.bold, st.italic)).or_insert_with(|| self.fallback_list(st.bold, st.italic).iter().copied().filter(|id| *id != own).collect()); let mut pick = pick_face(&drawn, had, candidates.len(), &mut |f, c| candidates.get(f).is_some_and(|id| cover.has(*id, c))); let lacks = |id: Option, cover: &mut Coverage| drawn.iter().copied().find(|c| !cover.has(id.unwrap_or(own), *c)); // No listed face has all of it: any installed face with the letter.