From 311280dbc7a72d4dd40fbc5198d85d24452cf28b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 17:41:12 -0800 Subject: [PATCH] Fonts: the scan reads the fonts the registry lists when the font service is off With a complete platform lister the font catalog does not ask the font service and does not read the registry's font list, so a font listed only in the registry was noted as a change but never cataloged. The catalog's lister now adds the registry's full paths to the platform files it lists, as the catalog does itself when the font service is on. RegisteredFonts::changed reads the registry before it takes its lock, so a scan noting the registry's list does not wait for that read. The registry tests scan with a lister of their own that lists no platform files, so the process-wide lister another test sets does not change what they scan. The first one now checks that the registered font is cataloged. --- crates/text/src/fontdb/db.rs | 3 ++- crates/text/src/fontdb/dirs.rs | 28 +++++++++++++++++----------- crates/text/src/tests/sysfonts.rs | 8 ++++++-- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/crates/text/src/fontdb/db.rs b/crates/text/src/fontdb/db.rs index ea020c3e63..ff93eb7c52 100644 --- a/crates/text/src/fontdb/db.rs +++ b/crates/text/src/fontdb/db.rs @@ -50,9 +50,10 @@ impl FontDb { /// [`FontDb::for_system_scan_test`] as with a complete platform lister /// ([`super::set_platform_font_files_complete`]): the scan does not ask the platform's font service. + /// Its lister lists no platform files, so another test's process-wide lister does not change what it scans. #[cfg(test)] pub(crate) fn for_complete_lister_scan_test() -> Self { - Self::new(super::dirs::system_catalog_with(false)) + Self::new(super::dirs::system_catalog_with(false, Vec::new)) } fn new((catalog, registered): (Catalog, Option>)) -> Self { diff --git a/crates/text/src/fontdb/dirs.rs b/crates/text/src/fontdb/dirs.rs index 7bd1a0892e..dfde291368 100644 --- a/crates/text/src/fontdb/dirs.rs +++ b/crates/text/src/fontdb/dirs.rs @@ -93,20 +93,22 @@ pub(crate) fn uses_font_service() -> bool { /// ([`uses_font_service`]), the font store, VectorCraft's own folders ([`app_dirs`]) and the files /// [`set_platform_font_files`] lists, with the registry's fonts its own change check leaves out. pub(super) fn system_catalog() -> (Catalog, Option>) { - system_catalog_with(uses_font_service()) + system_catalog_with(uses_font_service(), platform_font_files) } -/// [`system_catalog`], asking the platform's font service when `service`. Without it the catalog -/// neither reads nor compares the fonts the Windows registry lists, so the database compares them +/// [`system_catalog`], asking the platform's font service when `service` and reading the files +/// `platform` lists. Without the service the catalog neither reads nor compares the fonts the +/// Windows registry lists, so its lister adds them to the scan and the database compares them /// ([`RegisteredFonts`]). -pub(super) fn system_catalog_with(service: bool) -> (Catalog, Option>) { +pub(super) fn system_catalog_with(service: bool, platform: fn() -> Vec) -> (Catalog, Option>) { let registered = (!service).then(|| Arc::new(RegisteredFonts::default())); let seen = registered.clone(); let lister = move || { + let mut files = platform(); if let Some(seen) = &seen { - seen.record(); + files.extend(seen.record()); } - platform_font_files() + files }; let catalog = Catalog::new(FontEnv::from_process()).with_font_service(service).with_lister(Arc::new(lister)); catalog.set_app_dirs(app_dirs()); @@ -122,15 +124,19 @@ pub(super) fn system_catalog_with(service: bool) -> (Catalog, Option>>); impl RegisteredFonts { - /// Note the registry's list for the scan that is starting. - fn record(&self) { + /// Note the registry's list for the scan that is starting, and return it for the scan to read. + fn record(&self) -> Vec { let now = registered_font_files(); - *self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(now); + *self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(now.clone()); + now } - /// Whether the registry's list changed since the last scan noted it: `false` before one. + /// Whether the registry's list changed since the last scan noted it: `false` before one. The + /// registry is read before the lock is taken, so a scan's [`RegisteredFonts::record`] does not + /// wait for the read. pub(super) fn changed(&self) -> bool { - self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner).as_ref().is_some_and(|seen| *seen != registered_font_files()) + let now = registered_font_files(); + self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner).as_ref().is_some_and(|seen| *seen != now) } } diff --git a/crates/text/src/tests/sysfonts.rs b/crates/text/src/tests/sysfonts.rs index 9786490607..420664f17a 100644 --- a/crates/text/src/tests/sysfonts.rs +++ b/crates/text/src/tests/sysfonts.rs @@ -787,7 +787,7 @@ fn fonts_the_platform_lists_outside_the_font_folders_are_cataloged() { /// With a complete platform lister the catalog does not compare the fonts the Windows registry /// lists, so the database does: a font registered from a folder no scan has read is a change, and -/// the scan that follows notes it. +/// the scan that follows catalogs it and notes it. #[test] fn a_font_registered_since_the_scan_is_a_change_with_a_complete_lister() { use crate::fontdb::REGISTERED_IN_TESTS; @@ -797,11 +797,15 @@ fn a_font_registered_since_the_scan_is_a_change_with_a_complete_lister() { db.load_system_fonts(); assert!(!db.installed_fonts_changed()); let registered = std::env::temp_dir().join(format!("vc-sysfonts-{}-registered", std::process::id())).join("Registered.ttf"); - REGISTERED_IN_TESTS.set(Some(vec![registered])); + std::fs::create_dir_all(registered.parent().unwrap()).unwrap(); + std::fs::write(®istered, renamed_to("SourceSans3-Regular.ttf", "Registry Sans")).unwrap(); + REGISTERED_IN_TESTS.set(Some(vec![registered.clone()])); assert!(db.installed_fonts_changed(), "a font registered since the scan"); db.load_system_fonts(); + assert_eq!(db.styles("Registry Sans"), ["Regular"], "the scan reads the font the registry lists"); assert!(!db.installed_fonts_changed(), "the scan noted the registered font"); REGISTERED_IN_TESTS.set(None); + let _ = std::fs::remove_dir_all(registered.parent().unwrap()); } /// Each look of the font watch (`FontDb::watch`) uses the database's check, so a font registered