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