diff --git a/apps/vectorcraft/src/mac_fonts.rs b/apps/vectorcraft/src/mac_fonts.rs index 4e9f996d62..a98f76ea68 100644 --- a/apps/vectorcraft/src/mac_fonts.rs +++ b/apps/vectorcraft/src/mac_fonts.rs @@ -21,9 +21,10 @@ use objc2_core_text::CTFontManagerCopyAvailableFontURLs; /// The most fonts read from the list (a system has a few thousand faces). const MAX_FONTS: usize = 1 << 16; -/// Have the font scans read the font files CoreText lists. +/// Have the font scans read the font files CoreText lists, in place of asking the font service for +/// CoreText's fonts a second time. pub fn install() { - vectorcraft_text::set_platform_font_files(font_files); + vectorcraft_text::set_platform_font_files_complete(font_files); } /// The files of the fonts available to the app as CoreText lists them now (fonts registered since diff --git a/apps/vectorcraft/src/system_fonts.rs b/apps/vectorcraft/src/system_fonts.rs index fe9df48ca4..0edbdd41ab 100644 --- a/apps/vectorcraft/src/system_fonts.rs +++ b/apps/vectorcraft/src/system_fonts.rs @@ -40,9 +40,10 @@ const MAX_FACE_FILES: u32 = 8; /// The longest file path taken, in UTF-16 units (Windows' own limit). const MAX_PATH: u32 = 32_767; -/// Have the font scans read the font files DirectWrite and GDI list. +/// Have the font scans read the font files DirectWrite and GDI list, in place of asking the font +/// service for DirectWrite's collection a second time. pub fn install() { - vectorcraft_text::set_platform_font_files(font_files); + vectorcraft_text::set_platform_font_files_complete(font_files); } /// The local files of the fonts in DirectWrite's system font collection as it is now (fonts diff --git a/crates/text/src/fontdb.rs b/crates/text/src/fontdb.rs index 80d1230994..60be7f6251 100644 --- a/crates/text/src/fontdb.rs +++ b/crates/text/src/fontdb.rs @@ -46,12 +46,12 @@ use cache::GlyphCache; use cache::{CanonicalNames, GlyphColours, GlyphImages}; pub use composite::{CompositeCoverage, LentCompositeFonts}; use craft::font_catalog::Catalog; -#[cfg(test)] -pub(crate) use dirs::sfnt_of; pub use dirs::{ - PlatformFontFiles, app_font_dir, set_app_font_dir, set_platform_font_files, set_user_font_dirs, set_user_font_dirs_confined, system_font_dirs, - user_font_dirs, + PlatformFontFiles, app_font_dir, set_app_font_dir, set_platform_font_files, set_platform_font_files_complete, set_user_font_dirs, + set_user_font_dirs_confined, system_font_dirs, user_font_dirs, }; +#[cfg(test)] +pub(crate) use dirs::{sfnt_of, uses_font_service}; use face::Instances; pub(crate) use face::PathAlignHeights; pub use face::{FALLBACK_FAMILY, FontFace, IcfMargins, OutlineFormat}; diff --git a/crates/text/src/fontdb/dirs.rs b/crates/text/src/fontdb/dirs.rs index 51e16807a0..ed986dc8d4 100644 --- a/crates/text/src/fontdb/dirs.rs +++ b/crates/text/src/fontdb/dirs.rs @@ -47,6 +47,22 @@ pub fn set_platform_font_files(list: PlatformFontFiles) { let _ = list; } +/// [`set_platform_font_files`] for a lister that lists every font the platform's font service +/// lists. [`FontDb::global`]'s scan then reads the lister's files and does not also ask the font +/// service, which opens every installed face a second time. Only the first lister set counts. +pub fn set_platform_font_files_complete(list: PlatformFontFiles) { + #[cfg(not(target_arch = "wasm32"))] + if PLATFORM_FONT_FILES.set(list).is_ok() { + LISTER_IS_COMPLETE.store(true, std::sync::atomic::Ordering::Relaxed); + } + #[cfg(target_arch = "wasm32")] + let _ = list; +} + +/// Whether the lister was set with [`set_platform_font_files_complete`]. +#[cfg(not(target_arch = "wasm32"))] +static LISTER_IS_COMPLETE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + /// The most files [`platform_font_files`] takes from the lister. #[cfg(not(target_arch = "wasm32"))] const MAX_PLATFORM_FILES: usize = 1 << 16; @@ -64,10 +80,20 @@ fn platform_font_files() -> Vec { Vec::new() } -/// The catalog of [`FontDb::global`]: the platform's font folders and font service, the font -/// store, VectorCraft's own folders ([`app_dirs`]) and the files [`set_platform_font_files`] lists. +/// Whether [`system_catalog`] asks the platform's font service: not when the lister was set with +/// [`set_platform_font_files_complete`]. +pub(crate) fn uses_font_service() -> bool { + #[cfg(not(target_arch = "wasm32"))] + return !LISTER_IS_COMPLETE.load(std::sync::atomic::Ordering::Relaxed); + #[cfg(target_arch = "wasm32")] + true +} + +/// The catalog of [`FontDb::global`]: the platform's font folders, the platform's font service +/// ([`uses_font_service`]), the font store, VectorCraft's own folders ([`app_dirs`]) and the files +/// [`set_platform_font_files`] lists. pub(super) fn system_catalog() -> Catalog { - let catalog = Catalog::new(FontEnv::from_process()).with_lister(Arc::new(platform_font_files)); + let catalog = Catalog::new(FontEnv::from_process()).with_font_service(uses_font_service()).with_lister(Arc::new(platform_font_files)); catalog.set_app_dirs(app_dirs()); catalog } diff --git a/crates/text/src/lib.rs b/crates/text/src/lib.rs index af85cf99bd..73b3026361 100644 --- a/crates/text/src/lib.rs +++ b/crates/text/src/lib.rs @@ -49,8 +49,8 @@ pub use fontdb::{ CompositeCoverage, FALLBACK_FAMILY, FontClass, FontDb, FontFace, FontLikeness, FontMatch, FontTraits, GENERIC_FAMILIES, GlyphAlternate, GlyphCategory, GlyphCategoryKind, GlyphOrigin, IcfMargins, InstalledFace, JoiningPosition, LentCompositeFonts, MAX_FILE_FACES, OutlineFormat, PlatformFontFiles, RECENTLY_ADDED_WITHIN, RecentlyAdded, StylisticSet, Substitution, SubstitutionKind, UnloadedFile, WantedFont, WantedFonts, - app_font_dir, is_font_file, is_generic_family, is_suitcase, set_app_font_dir, set_platform_font_files, set_user_font_dirs, - set_user_font_dirs_confined, style_weight, system_font_dirs, user_font_dirs, + app_font_dir, is_font_file, is_generic_family, is_suitcase, set_app_font_dir, set_platform_font_files, set_platform_font_files_complete, + set_user_font_dirs, set_user_font_dirs_confined, style_weight, system_font_dirs, user_font_dirs, }; pub use hyphen::{hyphenation_exceptions, set_hyphenation_exceptions}; pub use layout::{envelope_ink, layout_inputs, register_envelope_ink}; diff --git a/crates/text/src/tests/sysfonts.rs b/crates/text/src/tests/sysfonts.rs index d82ca9ae38..0a8a8bc474 100644 --- a/crates/text/src/tests/sysfonts.rs +++ b/crates/text/src/tests/sysfonts.rs @@ -760,7 +760,9 @@ fn fonts_the_platform_lists_outside_the_font_folders_are_cataloged() { use std::sync::Mutex; static FILES: Mutex> = Mutex::new(Vec::new()); let list = |files: Vec| *FILES.lock().unwrap_or_else(|e| e.into_inner()) = files; - set_platform_font_files(|| FILES.lock().unwrap_or_else(|e| e.into_inner()).clone()); + assert!(crate::fontdb::uses_font_service(), "without a complete lister the scan asks the font service"); + set_platform_font_files_complete(|| FILES.lock().unwrap_or_else(|e| e.into_inner()).clone()); + assert!(!crate::fontdb::uses_font_service(), "with a complete lister the scan does not also ask the font service"); let file = service_font(); std::fs::create_dir_all(file.parent().unwrap()).unwrap(); let _ = std::fs::remove_file(&file);