mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 05:53:08 -05:00
Fonts: on Windows a font registered outside the font folders shows without a refresh
Since the startup scan lists the installed fonts once (#10978), the Windows app gives the font catalog a complete lister and turns its font service off. The catalog then stopped comparing the fonts the registry lists, so a font registered from a folder no scan had read stayed out of the font menus until Refresh Font List or a restart. The font database now notes the registry's list at each scan of such a catalog and reports a change when it differs, so the next check rescans. This stays until the craft-libs pin has Catalog::with_registry (storytold/craft-libs#5676), which moves the comparison back into the catalog.
This commit is contained in:
1 parent
52c071ac3a
commit
ae569930dc
6 files changed
+124
-16
No files matched your search
Generated
+1
@@ -9219,6 +9219,7 @@ dependencies = [
|
||||
"vectorcraft-doc",
|
||||
"vectorcraft-geom 0.25.1",
|
||||
"vello_cpu",
|
||||
"windows-registry",
|
||||
"write-fonts",
|
||||
]
|
||||
|
||||
|
||||
@@ -48,6 +48,12 @@ write-fonts = "0.48"
|
||||
# The craft-fonts face types of the tables `build.rs` writes (`CRAFT_FONTS`, and `SERVED_FONTS` on the web).
|
||||
craft-fonts-build = { workspace = true }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
# The fonts registered in the registry, compared by the font database's change check while the
|
||||
# font catalog's own check leaves them out (src/fontdb/dirs.rs); MIT OR Apache-2.0, already in the
|
||||
# build through the font catalog.
|
||||
windows-registry = "0.6"
|
||||
|
||||
[build-dependencies]
|
||||
# Embeds the craft-fonts faces (`Embed::write`) and, for the web, writes the served ones (`Embed::serve`).
|
||||
craft-fonts-build = { workspace = true }
|
||||
|
||||
@@ -51,7 +51,7 @@ pub use dirs::{
|
||||
set_user_font_dirs_confined, system_font_dirs, user_font_dirs,
|
||||
};
|
||||
#[cfg(test)]
|
||||
pub(crate) use dirs::{sfnt_of, uses_font_service};
|
||||
pub(crate) use dirs::{REGISTERED_IN_TESTS, sfnt_of, uses_font_service};
|
||||
use face::Instances;
|
||||
pub(crate) use face::PathAlignHeights;
|
||||
pub use face::{FALLBACK_FAMILY, FontFace, IcfMargins, OutlineFormat};
|
||||
@@ -103,6 +103,9 @@ pub struct FontDb {
|
||||
/// 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.
|
||||
catalog: Arc<Catalog>,
|
||||
/// The registry's fonts as the catalog's last scan saw them, when the catalog does not compare
|
||||
/// them itself ([`FontDb::installed_fonts_changed`]).
|
||||
registered: Option<Arc<dirs::RegisteredFonts>>,
|
||||
/// 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,
|
||||
|
||||
@@ -4,7 +4,7 @@ use super::build::make_face;
|
||||
use super::cache::{CanonicalNames, GlyphCache};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use super::dirs::confined_font_dirs;
|
||||
use super::dirs::system_catalog;
|
||||
use super::dirs::{RegisteredFonts, system_catalog};
|
||||
use super::face::{BUNDLED, FontBytes, FontFace, Instances};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use super::names::SysFallback;
|
||||
@@ -32,14 +32,14 @@ impl FontDb {
|
||||
/// A database holding the bundled fonts, whose system font scan reads `font_dirs` (folders,
|
||||
/// read with their subfolders, and font files) and nothing else.
|
||||
pub fn with_font_dirs(font_dirs: Vec<PathBuf>) -> Self {
|
||||
Self::new(font_dirs_catalog(font_dirs))
|
||||
Self::new((font_dirs_catalog(font_dirs), None))
|
||||
}
|
||||
|
||||
/// [`FontDb::with_font_dirs`] whose every scan also calls `lister` before it reads a folder,
|
||||
/// for tests that hold a scan until they let it go on.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn with_font_dirs_and_lister(font_dirs: Vec<PathBuf>, lister: craft::font_catalog::Lister) -> Self {
|
||||
Self::new(font_dirs_catalog(font_dirs).with_lister(lister))
|
||||
Self::new((font_dirs_catalog(font_dirs).with_lister(lister), None))
|
||||
}
|
||||
|
||||
/// Same scan paths as [`FontDb::global`] (platform lister and font folders), for tests.
|
||||
@@ -48,7 +48,14 @@ impl FontDb {
|
||||
Self::new(system_catalog())
|
||||
}
|
||||
|
||||
fn new(catalog: Catalog) -> Self {
|
||||
/// [`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.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn for_complete_lister_scan_test() -> Self {
|
||||
Self::new(super::dirs::system_catalog_with(false))
|
||||
}
|
||||
|
||||
fn new((catalog, registered): (Catalog, Option<Arc<RegisteredFonts>>)) -> Self {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
catalog.set_confined_dirs(confined_font_dirs());
|
||||
let mut faces = Vec::new();
|
||||
@@ -65,6 +72,7 @@ impl FontDb {
|
||||
images: Mutex::new(GlyphCache::default()),
|
||||
colours: Mutex::new(GlyphCache::default()),
|
||||
catalog: Arc::new(catalog),
|
||||
registered,
|
||||
bundled_cataloged: std::sync::Once::new(),
|
||||
file_faces: RwLock::new(super::FileFaces::default()),
|
||||
canonical_cache: Mutex::new(CanonicalNames::new()),
|
||||
@@ -420,16 +428,16 @@ impl FontDb {
|
||||
}
|
||||
|
||||
/// Whether fonts were installed or removed since the last scan (a font folder it read
|
||||
/// changed, or a new one is to be read), for [`FontDb::load_system_fonts`] to catalog them.
|
||||
/// Costs a look at each folder the scan read, not a scan. `false` before the first scan and on
|
||||
/// wasm.
|
||||
/// changed, a new one is to be read, or on Windows the registry's font list changed), for
|
||||
/// [`FontDb::load_system_fonts`] to catalog them. Costs a look at each folder the scan read,
|
||||
/// not a scan. `false` before the first scan and on wasm.
|
||||
pub fn installed_fonts_changed(&self) -> bool {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
self.catalog.installed_fonts_changed()
|
||||
}
|
||||
let folders = self.catalog.installed_fonts_changed();
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
false
|
||||
let folders = false;
|
||||
// No catalog leaves the registry to the database on wasm, where the font service is on.
|
||||
folders || (self.catalog.is_ready() && self.registered.as_ref().is_some_and(|r| r.changed()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -91,11 +91,82 @@ pub(crate) fn uses_font_service() -> bool {
|
||||
|
||||
/// 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_font_service(uses_font_service()).with_lister(Arc::new(platform_font_files));
|
||||
/// [`set_platform_font_files`] lists, with the registry's fonts its own change check leaves out.
|
||||
pub(super) fn system_catalog() -> (Catalog, Option<Arc<RegisteredFonts>>) {
|
||||
system_catalog_with(uses_font_service())
|
||||
}
|
||||
|
||||
/// [`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
|
||||
/// ([`RegisteredFonts`]).
|
||||
pub(super) fn system_catalog_with(service: bool) -> (Catalog, Option<Arc<RegisteredFonts>>) {
|
||||
let registered = (!service).then(|| Arc::new(RegisteredFonts::default()));
|
||||
let seen = registered.clone();
|
||||
let lister = move || {
|
||||
if let Some(seen) = &seen {
|
||||
seen.record();
|
||||
}
|
||||
platform_font_files()
|
||||
};
|
||||
let catalog = Catalog::new(FontEnv::from_process()).with_font_service(service).with_lister(Arc::new(lister));
|
||||
catalog.set_app_dirs(app_dirs());
|
||||
catalog
|
||||
(catalog, registered)
|
||||
}
|
||||
|
||||
/// The font files the Windows registry lists by full path, as the last scan of a catalog whose
|
||||
/// font service is off read them. That catalog does not compare them, so without this a font
|
||||
/// registered from a folder no scan has read waits for a manual rescan. Remove it once the pinned
|
||||
/// font catalog has `Catalog::with_registry` (storytold/craft-libs#5676) and [`system_catalog_with`]
|
||||
/// turns that on.
|
||||
#[derive(Default)]
|
||||
pub(super) struct RegisteredFonts(super::Mutex<Option<Vec<PathBuf>>>);
|
||||
|
||||
impl RegisteredFonts {
|
||||
/// Note the registry's list for the scan that is starting.
|
||||
fn record(&self) {
|
||||
let now = registered_font_files();
|
||||
*self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(now);
|
||||
}
|
||||
|
||||
/// Whether the registry's list changed since the last scan noted it: `false` before one.
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// What [`registered_font_files`] gives on this thread in tests, in place of the registry.
|
||||
pub(crate) static REGISTERED_IN_TESTS: std::cell::RefCell<Option<Vec<PathBuf>>> = const { std::cell::RefCell::new(None) };
|
||||
}
|
||||
|
||||
/// The full paths the registry lists for the machine's and the current user's fonts, sorted and
|
||||
/// each once, as the font catalog reads them. Bare file names (files in the Windows font folder)
|
||||
/// are left out: the scan reads that folder. Empty elsewhere than on Windows.
|
||||
fn registered_font_files() -> Vec<PathBuf> {
|
||||
#[cfg(test)]
|
||||
if let Some(files) = REGISTERED_IN_TESTS.with_borrow(Clone::clone) {
|
||||
return files;
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
const MAX_REGISTERED: usize = 1 << 16;
|
||||
const KEY: &str = r"Software\Microsoft\Windows NT\CurrentVersion\Fonts";
|
||||
use windows_registry::{CURRENT_USER, LOCAL_MACHINE};
|
||||
let mut files: Vec<PathBuf> = [LOCAL_MACHINE, CURRENT_USER]
|
||||
.into_iter()
|
||||
.filter_map(|root| root.open(KEY).ok())
|
||||
.flat_map(|key| key.values().map(|vs| vs.filter_map(|(_, v)| String::try_from(v).ok()).collect::<Vec<_>>()).unwrap_or_default())
|
||||
.take(MAX_REGISTERED)
|
||||
.map(PathBuf::from)
|
||||
.filter(|p| p.is_absolute())
|
||||
.collect();
|
||||
files.sort();
|
||||
files.dedup();
|
||||
files
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// The folders VectorCraft adds to the platform's: its own Fonts folder ([`set_app_font_dir`]),
|
||||
|
||||
@@ -785,6 +785,25 @@ fn fonts_the_platform_lists_outside_the_font_folders_are_cataloged() {
|
||||
assert_eq!(dirs.iter().filter(|d| **d == file).count(), 1, "{dirs:?}");
|
||||
}
|
||||
|
||||
/// 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.
|
||||
#[test]
|
||||
fn a_font_registered_since_the_scan_is_a_change_with_a_complete_lister() {
|
||||
use crate::fontdb::REGISTERED_IN_TESTS;
|
||||
REGISTERED_IN_TESTS.set(Some(Vec::new()));
|
||||
let db = FontDb::for_complete_lister_scan_test();
|
||||
assert!(!db.installed_fonts_changed(), "nothing to compare with before the first scan");
|
||||
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]));
|
||||
assert!(db.installed_fonts_changed(), "a font registered since the scan");
|
||||
db.load_system_fonts();
|
||||
assert!(!db.installed_fonts_changed(), "the scan noted the registered font");
|
||||
REGISTERED_IN_TESTS.set(None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_resource_fork_gives_up_its_font_files() {
|
||||
let (regular, bold) = (renamed("SourceSans3-Regular.ttf"), renamed("SourceSans3-Bold.ttf"));
|
||||
|
||||
Reference in new issue
Block a user