Merge pull request #8120 from dotCooCoo/perf/glyphs-panel-cmap-once

Glyphs: read a face's character list once instead of on every frame
This commit is contained in:
BFlat
2026-11-16 23:53:24 +07:00
committed by GitHub
6 changed files with 282 additions and 24 deletions
+1
View File
@@ -81,6 +81,7 @@ pub(super) fn make_face(bytes: FontBytes, index: u32, spec: FaceStyle, path: Opt
svg_glyphs,
gsub: std::sync::OnceLock::new(),
unencoded: std::sync::OnceLock::new(),
char_list: std::sync::OnceLock::new(),
named_chars: std::sync::OnceLock::new(),
})
}
+16 -5
View File
@@ -206,6 +206,8 @@ pub struct FontFace {
pub(super) gsub: std::sync::OnceLock<super::gsub::GsubIndex>,
/// [`Self::unencoded_glyphs`], read once.
pub(super) unencoded: std::sync::OnceLock<std::sync::Arc<[u32]>>,
/// [`Self::mapped_chars`], read once.
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>>,
}
@@ -368,11 +370,20 @@ impl FontFace {
}
/// Every mapped character and its glyph id, sorted by code point (the Glyphs panel).
pub fn chars(&self) -> Vec<(char, u32)> {
let Some(f) = self.skrifa() else { return vec![] };
let mut v: Vec<(char, u32)> = f.charmap().mappings().filter_map(|(cp, g)| char::from_u32(cp).map(|c| (c, g.to_u32()))).collect();
v.sort_unstable_by_key(|x| x.0);
v.dedup_by_key(|x| x.0);
v
self.mapped_chars().to_vec()
}
/// [`Self::chars`], read from the font once for the face and shared by every caller. Empty when
/// the font cannot be read or maps no character.
pub fn mapped_chars(&self) -> Arc<[(char, u32)]> {
self.char_list
.get_or_init(|| {
let Some(f) = self.skrifa() else { return Arc::from([]) };
let mut v: Vec<(char, u32)> = f.charmap().mappings().filter_map(|(cp, g)| char::from_u32(cp).map(|c| (c, g.to_u32()))).collect();
v.sort_unstable_by_key(|x| x.0);
v.dedup_by_key(|x| x.0);
v.into()
})
.clone()
}
/// Advance width of glyph `gid` in font units.
pub fn advance(&self, gid: u32) -> f64 {
+46
View File
@@ -1505,6 +1505,52 @@ fn remapped_source_sans(family: &str, postscript: &str, mapping: &[(char, char)]
Some(builder.build())
}
/// The family of [`large_cmap_font`].
pub const LARGE_CMAP_FAMILY: &str = "Largecmap Sans";
/// The family of [`cmapless_font`].
pub const CMAPLESS_FAMILY: &str = "Cmapless Sans";
/// The bundled Source Sans 3 Regular with a `cmap` of `count` characters from U+20000 up, about as
/// many as a large CJK font maps when `count` is 50,000. Each character maps to the font's next
/// glyph in turn after `.notdef`, so most glyphs have several characters.
pub fn large_cmap_font(count: u32) -> Option<Vec<u8>> {
use write_fonts::tables::cmap::Cmap;
const BASE: &[u8] = include_bytes!("../../../assets/fonts/SourceSans3-Regular.ttf");
let font = skrifa::FontRef::new(BASE).ok()?;
let glyphs = u32::from(font.maxp().ok()?.num_glyphs()).checked_sub(1).filter(|n| *n > 0)?;
let mappings = (0..count).map(|i| Some((char::from_u32(0x2_0000_u32.checked_add(i)?)?, write_fonts::types::GlyphId::new(1 + i % glyphs))));
let cmap = Cmap::from_mappings(mappings.collect::<Option<Vec<_>>>()?).ok()?;
source_sans_with_cmap(LARGE_CMAP_FAMILY, "LargecmapSans-Regular", Some(&cmap))
}
/// The bundled Source Sans 3 Regular without a `cmap` table: it maps no character.
pub fn cmapless_font() -> Option<Vec<u8>> {
source_sans_with_cmap(CMAPLESS_FAMILY, "CmaplessSans-Regular", None)
}
/// The bundled Source Sans 3 Regular named `family`, with `cmap` in place of its own (none when
/// `cmap` is `None`), and its other tables kept.
fn source_sans_with_cmap(family: &str, postscript: &str, cmap: Option<&write_fonts::tables::cmap::Cmap>) -> Option<Vec<u8>> {
const BASE: &[u8] = include_bytes!("../../../assets/fonts/SourceSans3-Regular.ttf");
let font = skrifa::FontRef::new(BASE).ok()?;
let full = format!("{family} Regular");
let name = name_table(&[(1, family), (2, "Regular"), (4, full.as_str()), (6, postscript)])?;
let mut builder = FontBuilder::new();
if let Some(cmap) = cmap {
builder.add_table(cmap).ok()?;
}
builder.add_raw(Tag::new(b"name"), name);
for r in font.table_directory.table_records() {
let tag = Tag::new(&r.tag().to_be_bytes());
if tag != Tag::new(b"cmap") && !builder.contains(tag) {
builder.add_raw(tag, font.table_data(r.tag())?.as_bytes());
}
}
Some(builder.build())
}
/// The family of [`tall_ideograph_font`].
pub const TALL_IDEOGRAPH_FAMILY: &str = "Tall Ideograph Sans";
+1
View File
@@ -8,6 +8,7 @@ mod alternates;
mod axes;
mod bidi;
mod bitmaps;
mod char_list;
mod colr;
mod combos;
mod composers;
+115
View File
@@ -0,0 +1,115 @@
//! A face's list of mapped characters (the Glyphs panel's grid): read from the font once for the
//! face, and the same list, in the same order, as a fresh read of the font's `cmap`.
use std::collections::BTreeMap;
use std::sync::Arc;
use skrifa::MetadataProvider;
use crate::test_fonts::{CMAPLESS_FAMILY, LARGE_CMAP_FAMILY, VARIABLE_FAMILY, cmapless_font, large_cmap_font, variable_font};
use crate::*;
/// The characters `face` maps, read from its `cmap` each time: every mapping with a valid code point,
/// sorted by code point, one per character.
fn read_chars(face: &FontFace) -> Vec<(char, u32)> {
let Some(f) = face.skrifa() else { return vec![] };
let mut v: Vec<(char, u32)> = f.charmap().mappings().filter_map(|(cp, g)| char::from_u32(cp).map(|c| (c, g.to_u32()))).collect();
v.sort_unstable_by_key(|x| x.0);
v.dedup_by_key(|x| x.0);
v
}
/// A database of the bundled fonts, the test variable font, a font with a large `cmap` and one with
/// no `cmap`.
fn db() -> FontDb {
let db = FontDb::with_font_dirs(vec![]);
assert!(db.add_font(variable_font().expect("the variable test font builds")) > 1, "its named instances are faces");
assert_eq!(db.add_font(large_cmap_font(50_000).expect("the large test font builds")), 1);
assert_eq!(db.add_font(cmapless_font().expect("the cmapless test font builds")), 1);
db
}
/// Faces of every kind the panel browses: static faces, a variable font's named instance and an
/// instance at other axis settings, a large `cmap`, and no `cmap`.
fn faces(db: &FontDb) -> Vec<Arc<FontFace>> {
let mut faces: Vec<Arc<FontFace>> = [
(FALLBACK_FAMILY, "Regular"),
(FALLBACK_FAMILY, "Bold"),
("Inter", "Regular"),
("JetBrains Mono", "Regular"),
(VARIABLE_FAMILY, "Regular"),
(VARIABLE_FAMILY, "Bold"),
(LARGE_CMAP_FAMILY, "Regular"),
(CMAPLESS_FAMILY, "Regular"),
]
.into_iter()
.map(|(family, style)| db.face(family, style).unwrap_or_else(|| panic!("{family} {style} is loaded")))
.collect();
let regular = db.face(VARIABLE_FAMILY, "Regular").expect("the variable face");
let axes: BTreeMap<String, f64> = [("wght".to_string(), 620.0)].into_iter().collect();
let instance = db.instance(&regular, &axes);
assert_ne!(instance.id(), regular.id(), "a new instance");
faces.push(instance);
faces
}
#[test]
fn the_character_list_is_the_fonts_cmap_read_once() {
let db = db();
let faces = faces(&db);
for face in &faces {
let list = face.mapped_chars();
assert_eq!(&list[..], read_chars(face).as_slice(), "{face:?}");
assert_eq!(face.chars(), read_chars(face), "{face:?}");
assert!(Arc::ptr_eq(&list, &face.mapped_chars()), "{face:?}: read once");
}
let large = db.face(LARGE_CMAP_FAMILY, "Regular").expect("the large face");
assert_eq!(large.mapped_chars().len(), 50_000);
let none = db.face(CMAPLESS_FAMILY, "Regular").expect("the cmapless face");
assert!(none.mapped_chars().is_empty(), "a font with no cmap maps no character");
// Each face keeps its own list: an instance of a variable font shares its characters, not the
// list read for another face.
for (i, a) in faces.iter().enumerate() {
for b in &faces[i + 1..] {
assert!(a.mapped_chars().is_empty() || !Arc::ptr_eq(&a.mapped_chars(), &b.mapped_chars()), "{a:?} and {b:?}");
}
}
}
/// Times one face's character list read from the font each time against the list read once, for the
/// bundled Source Sans 3 Regular and a font mapping 50,000 characters. Run with
/// `cargo test --release -p vectorcraft-text --lib character_list_timing -- --ignored --nocapture`.
#[test]
#[ignore = "manual timing probe; wall-clock performance depends on machine load"]
fn character_list_timing() {
const CALLS: usize = 100;
let db = db();
for (family, style) in [(FALLBACK_FAMILY, "Regular"), (LARGE_CMAP_FAMILY, "Regular")] {
let face = db.face(family, style).expect("the face");
let mut read = vec![];
let mut kept = vec![];
for _ in 0..7 {
let start = std::time::Instant::now();
let mut n = 0;
for _ in 0..CALLS {
n += std::hint::black_box(read_chars(&face)).len();
}
read.push(start.elapsed().as_secs_f64() * 1000.0 / CALLS as f64);
let start = std::time::Instant::now();
let mut m = 0;
for _ in 0..CALLS {
m += std::hint::black_box(face.mapped_chars()).len();
}
kept.push(start.elapsed().as_secs_f64() * 1000.0 / CALLS as f64);
assert_eq!(n, m);
}
read.sort_by(f64::total_cmp);
kept.sort_by(f64::total_cmp);
println!(
"{family} {style}, {} characters: read each time median {:.4} ms, read once median {:.6} ms per call; samples {read:.4?} and {kept:.6?}",
face.mapped_chars().len(),
read[3],
kept[3]
);
}
}
+103 -19
View File
@@ -299,6 +299,40 @@ fn insert(app: &mut VectorcraftApp, ctx: &egui::Context, ch: char) -> bool {
}
}
/// The grid's cells for `face`: the characters listed, with their glyphs, then the glyphs listed by
/// glyph id. `category` gives the glyphs of one of the face's categories; without it, the cells are
/// the characters of `lo..=hi`, and in Entire Font (`subset` 0) also the glyphs no character maps.
fn grid_glyphs(
face: &vectorcraft_text::FontFace,
category: Option<&vectorcraft_text::GlyphCategory>,
subset: usize,
(lo, hi): (u32, u32),
) -> (Vec<(char, u32)>, std::sync::Arc<[u32]>) {
let all = face.mapped_chars();
match category {
// A category lists its glyphs that a character maps by that character, and the others by
// glyph id after them.
Some(c) => {
let wanted: HashSet<u32> = c.glyphs.iter().copied().collect();
// A glyph several characters map is listed once, by the first of them; a glyph only a
// space or control character maps is listed by glyph id, as no character it can insert.
let mut mapped: HashSet<u32> = HashSet::new();
let chars: Vec<(char, u32)> =
all.iter().copied().filter(|(c, g)| !c.is_control() && !c.is_whitespace() && wanted.contains(g) && mapped.insert(*g)).collect();
(chars, c.glyphs.iter().copied().filter(|g| !mapped.contains(g)).collect())
}
None => {
// The list is sorted by code point, so the range's characters lie between two bounds.
let start = all.partition_point(|(c, _)| (*c as u32) < lo);
let end = all.partition_point(|(c, _)| (*c as u32) <= hi);
let chars = all.get(start..end).unwrap_or_default().iter().copied().filter(|(c, _)| !c.is_control() && !c.is_whitespace()).collect();
// Entire Font also lists the glyphs no character maps (positional forms, alternates,
// ligatures) by glyph id, after the characters.
(chars, if subset == 0 { face.unencoded_glyphs() } else { std::sync::Arc::from([]) })
}
}
}
/// Browse and insert glyphs from the chosen font, with font controls below the glyph grid.
pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
let t = Tokens::get(ui.ctx());
@@ -335,25 +369,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
ui.label(egui::RichText::new(tl!("No fonts are available.")).color(t.text_dim));
return;
};
let (chars, unencoded): (Vec<(char, u32)>, Vec<u32>) = match category.and_then(|i| face.glyph_categories().get(i)) {
// A category lists its glyphs that a character maps by that character, and the others by
// glyph id after them.
Some(c) => {
let wanted: HashSet<u32> = c.glyphs.iter().copied().collect();
// A glyph several characters map is listed once, by the first of them; a glyph only a
// space or control character maps is listed by glyph id, as no character it can insert.
let mut mapped: HashSet<u32> = HashSet::new();
let chars: Vec<(char, u32)> =
face.chars().into_iter().filter(|(c, g)| !c.is_control() && !c.is_whitespace() && wanted.contains(g) && mapped.insert(*g)).collect();
(chars, c.glyphs.iter().copied().filter(|g| !mapped.contains(g)).collect())
}
None => {
let chars = face.chars().into_iter().filter(|(c, _)| (lo..=hi).contains(&(*c as u32)) && !c.is_control() && !c.is_whitespace()).collect();
// Entire Font also lists the glyphs no character maps (positional forms, alternates,
// ligatures) by glyph id, after the characters.
(chars, if subset == 0 { face.unencoded_glyphs().to_vec() } else { vec![] })
}
};
let (chars, unencoded) = grid_glyphs(&face, category.and_then(|i| face.glyph_categories().get(i)), subset, (lo, hi));
let total = chars.len() + unencoded.len();
let cell = CELL_STEPS[cell_index(app)];
let cols = ((w / cell).floor() as usize).max(1);
@@ -2922,4 +2938,72 @@ mod tests {
state.doc.walk(|n| texts += usize::from(matches!(n.kind, NodeKind::Text(_))));
assert_eq!(texts, 0, "neither glyph created or changed text");
}
/// The grid's cells built from the face's character list read once are the cells built from a
/// fresh read of the font on every frame, for every Show choice, every category, a change of
/// face back and forth, and a font with no `cmap`.
#[test]
fn the_grid_lists_the_same_cells_from_the_character_list_read_once() {
use vectorcraft_text::test_fonts::{CATEGORIES_FAMILY, CMAPLESS_FAMILY, categories_font, cmapless_font};
/// The cells as the grid listed them on each frame from its own copy of the face's characters
/// (`vectorcraft-text`'s tests check that list against a fresh read of the font).
fn read_cells(
face: &vectorcraft_text::FontFace,
category: Option<usize>,
subset: usize,
(lo, hi): (u32, u32),
) -> (Vec<(char, u32)>, Vec<u32>) {
match category.and_then(|i| face.glyph_categories().get(i)) {
Some(c) => {
let wanted: HashSet<u32> = c.glyphs.iter().copied().collect();
let mut mapped: HashSet<u32> = HashSet::new();
let chars: Vec<(char, u32)> = face
.chars()
.into_iter()
.filter(|(c, g)| !c.is_control() && !c.is_whitespace() && wanted.contains(g) && mapped.insert(*g))
.collect();
(chars, c.glyphs.iter().copied().filter(|g| !mapped.contains(g)).collect())
}
None => {
let chars =
face.chars().into_iter().filter(|(c, _)| (lo..=hi).contains(&(*c as u32)) && !c.is_control() && !c.is_whitespace()).collect();
(chars, if subset == 0 { face.unencoded_glyphs().to_vec() } else { vec![] })
}
}
}
let db = FontDb::with_font_dirs(vec![]);
assert_eq!(db.add_font(categories_font().expect("the categories test font builds")), 1);
assert_eq!(db.add_font(cmapless_font().expect("the cmapless test font builds")), 1);
let faces: Vec<_> = [
(vectorcraft_text::FALLBACK_FAMILY, "Regular"),
("Inter", "Regular"),
(CATEGORIES_FAMILY, "Regular"),
(vectorcraft_text::FALLBACK_FAMILY, "Regular"),
(CMAPLESS_FAMILY, "Regular"),
("JetBrains Mono", "Regular"),
]
.into_iter()
.map(|(family, style)| db.face(family, style).unwrap_or_else(|| panic!("{family} {style} is loaded")))
.collect();
let categories = db.face(CATEGORIES_FAMILY, "Regular").expect("the categories face");
assert!(!categories.glyph_categories().is_empty(), "the test font has categories");
let none = db.face(CMAPLESS_FAMILY, "Regular").expect("the cmapless face");
assert!(none.mapped_chars().is_empty(), "no character is mapped");
for face in &faces {
for (subset, &(name, lo, hi)) in SUBSETS.iter().enumerate() {
let (chars, unencoded) = grid_glyphs(face, None, subset, (lo, hi));
assert_eq!((chars, unencoded.to_vec()), read_cells(face, None, subset, (lo, hi)), "{face:?} {name}");
}
for (i, c) in face.glyph_categories().iter().enumerate() {
let (chars, unencoded) = grid_glyphs(face, Some(c), CATEGORY, (0, 0x10FFFF));
assert_eq!((chars, unencoded.to_vec()), read_cells(face, Some(i), CATEGORY, (0, 0x10FFFF)), "{face:?} {:?}", c.kind);
}
}
// Entire Font lists every glyph of a font with no `cmap` by glyph id.
let (chars, unencoded) = grid_glyphs(&none, None, 0, (0, 0x10FFFF));
assert!(chars.is_empty());
assert!(!unencoded.is_empty(), "the glyphs are listed by glyph id");
}
}