mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 19:51:26 -05:00
Fonts: the startup scan lists the installed fonts once
Since the move to the craft font catalog, the global catalog asked the platform's font service (DirectWrite on Windows, CoreText on macOS) and also ran the app's platform lister, which lists the same fonts again. On Windows the font service opens and maps every installed face, so the background font scan did that work twice at every start. set_platform_font_files_complete sets a lister that lists every font the font service lists. The catalog then reads the lister's files and does not ask the font service. set_platform_font_files keeps adding to the font service as before. The desktop app and vectorcraft-cli install their Windows and macOS listers with the new call; both listers already read DirectWrite's system collection (plus GDI's fonts) and CoreText's available fonts.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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<PathBuf> {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -760,7 +760,9 @@ fn fonts_the_platform_lists_outside_the_font_folders_are_cataloged() {
|
||||
use std::sync::Mutex;
|
||||
static FILES: Mutex<Vec<String>> = Mutex::new(Vec::new());
|
||||
let list = |files: Vec<String>| *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);
|
||||
|
||||
Reference in New Issue
Block a user