mirror of
https://github.com/storytold/AniCraft.git
synced 2026-11-30 01:54:14 +01:00
perf(text): remember the fallback faces and which letters they have (M5)
A block that keeps needing a fallback face is laid out again on every edit; asking each listed face for each letter again opened about thirty font files a time. Measured on a three-character block with two characters no face has: 1.0 ms a layout before, 0.09 ms after.
This commit is contained in:
1 parent
912d524c6e
commit
19efbd7e54
3 files changed
+42
-6
No files matched your search
@@ -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<fontdb::ID> {
|
||||
/// 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<fontdb::ID> = 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()
|
||||
|
||||
@@ -467,9 +467,16 @@ pub struct Fonts {
|
||||
db: fontdb::Database,
|
||||
families: Vec<String>,
|
||||
/// Which face, if any, has each character the fallback list didn't (`Fonts::any_face_with`).
|
||||
wide: std::sync::Mutex<std::collections::HashMap<char, Option<fontdb::ID>>>,
|
||||
wide: Remembered<char, Option<fontdb::ID>>,
|
||||
/// 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<K, V> = std::sync::Mutex<std::collections::HashMap<K, V>>;
|
||||
|
||||
static FONTS: OnceLock<Fonts> = OnceLock::new();
|
||||
static EXTRA: OnceLock<Vec<Arc<[u8]>>> = OnceLock::new();
|
||||
|
||||
@@ -504,7 +511,7 @@ impl Fonts {
|
||||
let mut families: Vec<String> = 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).
|
||||
|
||||
@@ -480,7 +480,7 @@ impl Fonts {
|
||||
let had = drawn.iter().filter(|c| cover.has(own, **c)).count();
|
||||
let mut chosen: Option<usize> = 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<fontdb::ID>, 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.
|
||||
|
||||
Reference in new issue
Block a user