From 10fbd62491c71187562633b0e21fea4fb5b7dfd2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 16:32:02 -0800 Subject: [PATCH] Fonts: the catalog reads the bundled fonts in place instead of a copy FontDb added the bundled fonts to the font catalog with add_bytes on the first call of FontDb::catalog, which copied every bundled font file into the heap (3,381,864 bytes for the nine fonts in assets/fonts, more with craft-fonts embedded). The app reads the catalog at startup, so every session held those copies. FontDb::new now registers them with Catalog::add_static (craft-libs #4888, in the pinned rev), which reads the faces from the binary's own bytes, so the deferral through a Once is gone. The test checks that the bundled faces are in the catalog as soon as the database is made, and that Catalog::face_data returns the embedded bytes themselves (same pointer), so a copy fails it. --- crates/text/src/fontdb.rs | 25 +++++++++++++++++-------- crates/text/src/fontdb/db.rs | 22 ++++++++-------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/crates/text/src/fontdb.rs b/crates/text/src/fontdb.rs index 60be7f6251..2170725c62 100644 --- a/crates/text/src/fontdb.rs +++ b/crates/text/src/fontdb.rs @@ -100,11 +100,9 @@ pub struct FontDb { colours: Mutex, /// The installed fonts: the system font scan's faces, found by any of their names /// ([`FontDb::catalog`]). Lookups by family name wait for its first scan, so what they find - /// never depends on what ran before them. The bundled faces join it on the first call of - /// [`FontDb::catalog`], so its faces are read through that call. + /// never depends on what ran before them. It also holds the bundled faces, added when the + /// database is made and read from the binary in place. catalog: Arc, - /// The bundled faces were added to [`Self::catalog`]. - bundled_cataloged: std::sync::Once, /// Fonts files carried for their type ([`vectorcraft_doc::FileFonts`]): apart from the faces, /// so no family name, font menu, fallback or PostScript name lookup finds them. pub(super) file_faces: RwLock, @@ -399,14 +397,25 @@ mod bundled_catalog_tests { } #[test] - fn the_bundled_fonts_join_the_catalog_when_it_is_first_read() { + fn the_bundled_fonts_are_in_the_catalog_from_the_start() { let db = FontDb::with_font_dirs(Vec::new()); assert!(!db.read_faces().is_empty(), "the database draws with its bundled faces from the start"); - assert_eq!(bundled_in_catalog(&db.catalog), 0, "making the database copies no bundled font into the catalog"); + let added = bundled_in_catalog(&db.catalog); + assert!(added > 0, "making the database adds the bundled fonts to the catalog"); + // The catalog reads each bundled face from the bytes in the binary, not from a copy. + let embedded: Vec<*const u8> = super::face::BUNDLED.iter().map(|b| b.as_ptr()).collect(); + for face in db.catalog.faces().iter().filter(|f| f.source == craft::font_catalog::Source::Bundled) { + let data = db.catalog.face_data(face).expect("a bundled face's bytes"); + let ptr = data.bytes().as_ptr(); + assert!( + embedded.contains(&ptr) || crate::craft_fonts::document_fonts().iter().any(|f| f.bytes.as_ptr() == ptr), + "{} {} is read from a copy of its font file", + face.family, + face.style + ); + } // A lookup that only the catalog answers finds a bundled face. assert_eq!(db.by_postscript_name("SourceSans3-Bold"), Some(("Source Sans 3".to_string(), "Bold".to_string()))); - let added = bundled_in_catalog(&db.catalog); - assert!(added > 0, "the first read of the catalog adds the bundled fonts"); assert!(db.catalog().families().iter().any(|f| f == "Inter"), "font pickers list a bundled family"); assert_eq!(bundled_in_catalog(db.catalog()), added, "later reads add them no second time"); } diff --git a/crates/text/src/fontdb/db.rs b/crates/text/src/fontdb/db.rs index b65dbab4f2..32895f707d 100644 --- a/crates/text/src/fontdb/db.rs +++ b/crates/text/src/fontdb/db.rs @@ -1,4 +1,4 @@ -use craft::font_catalog::{Catalog, FontEnv, Limits, Os, Source}; +use craft::font_catalog::{Catalog, FontEnv, Os, Source}; use super::build::make_face; use super::cache::{CanonicalNames, GlyphCache}; @@ -58,6 +58,11 @@ impl FontDb { faces.push(Arc::new(f)); } } + // The catalog reads the bytes in place, so font pickers list the bundled faces without a + // second copy of them in memory. + if let Err(e) = catalog.add_static(data, Source::Bundled) { + log::warn!("a bundled font is not in the font catalog: {e}"); + } } Self { faces: RwLock::new(faces), @@ -65,7 +70,6 @@ impl FontDb { images: Mutex::new(GlyphCache::default()), colours: Mutex::new(GlyphCache::default()), catalog: Arc::new(catalog), - bundled_cataloged: std::sync::Once::new(), file_faces: RwLock::new(super::FileFaces::default()), canonical_cache: Mutex::new(CanonicalNames::new()), #[cfg(not(target_arch = "wasm32"))] @@ -121,19 +125,9 @@ impl FontDb { /// The installed fonts this database finds by name, shared with the framework's font menus and /// pickers. Its lookups wait for its first scan ([`FontDb::scan_in_background`]). /// - /// The first call adds the bundled fonts to it as [`Source::Bundled`] faces, so font pickers - /// list them. A run that never looks a font up in the catalog, such as exporting a file without - /// type, does not hold the catalog's copies of their bytes. - // The catalog keeps its own copy of the bytes it is given. Adding the bundled fonts on first use - // can go once the catalog takes static bytes without copying them (craft-libs#4888). + /// It holds the bundled fonts as [`Source::Bundled`] faces, read from the binary in place, so + /// font pickers list them. pub fn catalog(&self) -> &Arc { - self.bundled_cataloged.call_once(|| { - for data in bundled_fonts() { - if let Err(e) = self.catalog.add_bytes(data.to_vec(), Source::Bundled, &Limits::default()) { - log::warn!("a bundled font is not in the font catalog: {e}"); - } - } - }); &self.catalog }