From 1872e522033b3b12e308adf22b52ca7059a2be6d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 03:24:49 -0800 Subject: [PATCH] Glyphs: a font's glyph names are read once, so the panel stops scanning the post table for every cell FontFace::glyph_name looked each name up in the font. In a version 2.0 post table the names are one run of strings, and read-fonts walks that run from its start for every lookup. The Glyphs panel builds the tooltip of every visible cell in every frame, and the tooltip of a glyph that no character maps carries its name, so each frame scanned the table once per such cell. glyph_name now reads every name of the face in one pass the first time it is asked, through skrifa's name iterator, and keeps them on the face. A new test checks the kept names against each glyph's own lookup for every bundled face. --- crates/text/src/fontdb/build.rs | 1 + crates/text/src/fontdb/face.rs | 2 ++ crates/text/src/fontdb/gsub.rs | 42 +++++++++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/crates/text/src/fontdb/build.rs b/crates/text/src/fontdb/build.rs index 38f5aece65..b3f6fbbff8 100644 --- a/crates/text/src/fontdb/build.rs +++ b/crates/text/src/fontdb/build.rs @@ -83,6 +83,7 @@ pub(super) fn make_face(bytes: FontBytes, index: u32, spec: FaceStyle, path: Opt unencoded: std::sync::OnceLock::new(), char_list: std::sync::OnceLock::new(), named_chars: std::sync::OnceLock::new(), + glyph_names: std::sync::OnceLock::new(), }) } diff --git a/crates/text/src/fontdb/face.rs b/crates/text/src/fontdb/face.rs index c418d40308..85633460dc 100644 --- a/crates/text/src/fontdb/face.rs +++ b/crates/text/src/fontdb/face.rs @@ -210,6 +210,8 @@ pub struct FontFace { pub(super) char_list: std::sync::OnceLock>, /// [`Self::name_owner`]'s glyph names to characters, read once. pub(super) named_chars: std::sync::OnceLock>, + /// [`Self::glyph_name`]'s names by glyph id, read once. + pub(super) glyph_names: std::sync::OnceLock>>>, } impl vectorcraft_doc::FileFontFace for FontFace { diff --git a/crates/text/src/fontdb/gsub.rs b/crates/text/src/fontdb/gsub.rs index 7529a82929..2fef163351 100644 --- a/crates/text/src/fontdb/gsub.rs +++ b/crates/text/src/fontdb/gsub.rs @@ -589,11 +589,23 @@ impl FontFace { } /// The name of glyph `gid` from the font's `post` or `CFF` table: `None` for a glyph past the - /// face's glyphs, or when the font has no names (the ids are then `gid12`). + /// face's glyphs, or when the font has no names (the ids are then `gid12`). The names are read + /// once for the face, in one pass over the table. pub fn glyph_name(&self, gid: u32) -> Option { - let font = self.skrifa()?; - let name = font.glyph_names().get(skrifa::GlyphId::new(gid))?; - (!name.is_synthesized()).then(|| name.as_str().to_string()) + let names = self.glyph_names.get_or_init(|| { + let Some(font) = self.skrifa() else { return Vec::new() }; + let names = font.glyph_names(); + let mut out = vec![None; names.num_glyphs() as usize]; + for (id, name) in names.iter() { + if let Some(slot) = out.get_mut(id.to_u32() as usize) + && !name.is_synthesized() + { + *slot = Some(Box::from(name.as_str())); + } + } + out + }); + names.get(usize::try_from(gid).ok()?)?.as_deref().map(str::to_string) } /// The kinds of glyph the face's `GSUB` table gives ([`GlyphCategoryKind`]), in the order of @@ -651,6 +663,28 @@ mod tests { assert!(face.glyph_categories().is_empty()); } + /// The names read in one pass are the names the font gives each glyph id, for every bundled + /// face, and a glyph past the face's glyphs has none. + #[test] + fn glyph_names_read_once_match_each_glyphs_own_name() { + let db = super::super::FontDb::with_font_dirs(vec![]); + let mut checked = 0; + for family in db.families() { + for style in db.styles(&family) { + let Some(face) = db.face(&family, &style) else { continue }; + let Some(font) = face.skrifa() else { continue }; + let names = font.glyph_names(); + for gid in 0..face.glyph_count() { + let own = names.get(skrifa::GlyphId::new(gid)).filter(|n| !n.is_synthesized()).map(|n| n.as_str().to_string()); + assert_eq!(face.glyph_name(gid), own, "{family} {style}: glyph {gid}"); + checked += 1; + } + assert_eq!(face.glyph_name(face.glyph_count()), None, "{family} {style}"); + } + } + assert!(checked > 0, "the bundled faces have glyphs"); + } + #[test] fn kinds_round_trip_through_their_ids() { for kind in GlyphCategoryKind::all() {