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.
This commit is contained in:
dotCooCoo committed 2026-11-25 03:24:49 -08:00
1 parent 01c602dc40
commit 1872e52203
3 files changed
+41 -4

No files matched your search

+1
View File
@@ -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(),
})
}
+2
View File
@@ -210,6 +210,8 @@ pub struct FontFace {
pub(super) char_list: std::sync::OnceLock<Arc<[(char, u32)]>>,
/// [`Self::name_owner`]'s glyph names to characters, read once.
pub(super) named_chars: std::sync::OnceLock<std::collections::HashMap<String, char>>,
/// [`Self::glyph_name`]'s names by glyph id, read once.
pub(super) glyph_names: std::sync::OnceLock<Vec<Option<Box<str>>>>,
}
impl vectorcraft_doc::FileFontFace for FontFace {
+38 -4
View File
@@ -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<String> {
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() {