From 2b6a53aa47545bdae03e2b1371a18b2e33e45a31 Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 24 Nov 2026 21:37:52 +0100 Subject: [PATCH] font-store: the preview service's plain types and PreviewStore build on wasm, so font-manager does again The 24 Nov main sync (#3300) brought the font manager's preview-service code (#2782), which names LegalText, PreviewServiceSource, PREVIEW_SERVICE_ENV and PreviewStore on every target, while font-store only had them in its native-only store and preview modules. craft-font-manager, and with it the craft crate, no longer built for wasm32 (photocraft's web build). They move to an ungated service module; PreviewStore::http_client stays native only, and preview keeps its PreviewStore re-export. The fake store checks a preview-service address only where the native check exists. Co-Authored-By: Claude Opus 5.5 --- crates/font-manager/src/store.rs | 2 +- crates/font-manager/src/store/fake.rs | 4 ++ crates/font-store/src/lib.rs | 7 ++-- crates/font-store/src/preview.rs | 26 +----------- crates/font-store/src/service.rs | 57 +++++++++++++++++++++++++++ crates/font-store/src/store.rs | 26 +----------- 6 files changed, 67 insertions(+), 55 deletions(-) create mode 100644 crates/font-store/src/service.rs diff --git a/crates/font-manager/src/store.rs b/crates/font-manager/src/store.rs index 4e8bc303db..8f12de134c 100644 --- a/crates/font-manager/src/store.rs +++ b/crates/font-manager/src/store.rs @@ -13,7 +13,7 @@ use std::sync::{Condvar, Mutex}; #[cfg(not(target_arch = "wasm32"))] use craft_font_catalog::Catalog; -pub use craft_font_store::preview::PreviewStore; +pub use craft_font_store::PreviewStore; #[cfg(not(target_arch = "wasm32"))] use craft_font_store::{Cancel, Client, Face, FontStore, Network, PINNED_INDEX, verify_font}; pub use craft_font_store::{LegalText, PREVIEW_SERVICE_ENV, PreviewServiceSource, UserSource}; diff --git a/crates/font-manager/src/store/fake.rs b/crates/font-manager/src/store/fake.rs index e91e8c3562..869333641a 100644 --- a/crates/font-manager/src/store/fake.rs +++ b/crates/font-manager/src/store/fake.rs @@ -281,7 +281,11 @@ impl Installer for FakeStore { } fn set_preview_service(&self, url: Option<&str>) -> Result<(), StoreError> { + // The address check is the native store's; the browser has no preview service to reach. + #[cfg(not(target_arch = "wasm32"))] let url = url.map(craft_font_store::check_preview_service).transpose().map_err(err)?; + #[cfg(target_arch = "wasm32")] + let url = url.map(str::to_owned); *self.preview_service.lock().map_err(err)? = url; Ok(()) } diff --git a/crates/font-store/src/lib.rs b/crates/font-store/src/lib.rs index 7623ae8758..d55fc84fe8 100644 --- a/crates/font-store/src/lib.rs +++ b/crates/font-store/src/lib.rs @@ -24,6 +24,7 @@ mod index; mod pin; +mod service; mod source; mod sources; mod verify; @@ -42,6 +43,7 @@ pub use index::{ MAX_FILE_BYTES, MAX_INDEX_BYTES, MAX_LICENSE_BYTES, Sample, Script, SourceProblem, Unavailable, parse_style, }; pub use pin::{IndexPin, PINNED_INDEX}; +pub use service::{LegalText, PREVIEW_SERVICE_ENV, PreviewServiceSource, PreviewStore}; pub use sources::{SourceEntry, SourcesFile}; pub use verify::{VerifyError, is_font, sha256_hex, verify_font}; @@ -67,10 +69,7 @@ pub use language_packs::{ MAX_PACK_INDEX_BYTES, MAX_PACK_UNPACKED_BYTES, PackEntry, PackIndex, PackIndexPin, language_pack_index_overridden, }; #[cfg(not(target_arch = "wasm32"))] -pub use store::{ - Caller, Error, FontStore, InstalledFamily, InstalledFile, LegalText, Network, PREVIEW_SERVICE_ENV, PreviewServiceSource, Problem, Progress, - check_preview_service, -}; +pub use store::{Caller, Error, FontStore, InstalledFamily, InstalledFile, Network, Problem, Progress, check_preview_service}; #[cfg(test)] mod tests; diff --git a/crates/font-store/src/preview.rs b/crates/font-store/src/preview.rs index f7b9b7b3c9..3e9b6d0740 100644 --- a/crates/font-store/src/preview.rs +++ b/crates/font-store/src/preview.rs @@ -246,31 +246,7 @@ pub struct Preview { pub font: Result, PreviewError>, } -/// What [`Previews`] reads from the font store: the service and the user's setting on each request, the client and -/// the cache folder when the pool is made. The font manager's `Installer` builds on it. -pub trait PreviewStore: Send + Sync { - /// The preview service's base URL, if one is set: [`PREVIEW_SERVICE_ENV`](crate::PREVIEW_SERVICE_ENV), else the - /// font store's `preview_service` setting, else the host's default. Installs try it as a mirror first. - fn preview_service(&self) -> Option { - None - } - - /// The store's HTTP client, if it has one. [`Previews`] fetches with it, so a preview service that installs - /// found unreachable is skipped by previews too, and the reverse. - fn http_client(&self) -> Option { - None - } - - /// Whether previews may be fetched from the preview service: the user's setting, on by default. - fn previews_enabled(&self) -> bool { - true - } - - /// The folder [`Previews`] caches preview subsets in; none keeps them only for as long as the caller holds them. - fn preview_cache(&self) -> Option { - None - } -} +pub use crate::service::PreviewStore; /// How the pool downloads: the store's [`Client`], or a stand-in in tests. pub trait Fetch: Send + Sync { diff --git a/crates/font-store/src/service.rs b/crates/font-store/src/service.rs new file mode 100644 index 0000000000..996c26c57a --- /dev/null +++ b/crates/font-store/src/service.rs @@ -0,0 +1,57 @@ +//! What the font store and the preview service's client share with the font manager on every target: which legal +//! text to read, where the preview service comes from, and what the preview pool reads from a store. The store and +//! the client themselves are native only. + +use std::path::PathBuf; + +/// Which of a family's legal texts [`FontStore::legal_text`](crate::FontStore::legal_text) reads. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum LegalText { + /// The licence file (`OFL.txt`, `LICENSE.txt`). + License, + /// An Apache family's `NOTICE` file. + Notice, +} + +/// The environment variable that sets the preview service's base URL; it beats the saved setting. +pub const PREVIEW_SERVICE_ENV: &str = "CRAFT_FONT_PREVIEW_URL"; + +/// Where the preview service in effect comes from. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PreviewServiceSource { + /// [`PREVIEW_SERVICE_ENV`]; the setting can't change it. + Env, + /// The `preview_service` setting in `settings.json`. + Saved, + /// The host's default, [`FontStore::with_default_preview_service`](crate::FontStore::with_default_preview_service). + Default, + /// There is no service. + None, +} + +/// What [`Previews`](crate::preview::Previews) reads from the font store: the service and the user's setting on each request, the client and +/// the cache folder when the pool is made. The font manager's `Installer` builds on it. +pub trait PreviewStore: Send + Sync { + /// The preview service's base URL, if one is set: [`PREVIEW_SERVICE_ENV`](crate::PREVIEW_SERVICE_ENV), else the + /// font store's `preview_service` setting, else the host's default. Installs try it as a mirror first. + fn preview_service(&self) -> Option { + None + } + + /// The store's HTTP client, if it has one. [`Previews`](crate::preview::Previews) fetches with it, so a preview service that installs + /// found unreachable is skipped by previews too, and the reverse. + #[cfg(not(target_arch = "wasm32"))] + fn http_client(&self) -> Option { + None + } + + /// Whether previews may be fetched from the preview service: the user's setting, on by default. + fn previews_enabled(&self) -> bool { + true + } + + /// The folder [`Previews`](crate::preview::Previews) caches preview subsets in; none keeps them only for as long as the caller holds them. + fn preview_cache(&self) -> Option { + None + } +} diff --git a/crates/font-store/src/store.rs b/crates/font-store/src/store.rs index c90c0af457..b5015c481d 100644 --- a/crates/font-store/src/store.rs +++ b/crates/font-store/src/store.rs @@ -30,6 +30,7 @@ use crate::index::{ Face, Family, FontFile, GOOGLE_FONTS, Index, IndexError, LicenseFile, MAX_INDEX_BYTES, MAX_LICENSE_BYTES, SourceProblem, Unavailable, }; use crate::pin::IndexPin; +use crate::service::{LegalText, PREVIEW_SERVICE_ENV, PreviewServiceSource}; use crate::source::{is_source_url, safe_dir, safe_file_name, slug}; use crate::sources::{MAX_SOURCES_BYTES, SourceEntry, SourcesFile}; use crate::verify::{VerifyError, is_font, sha256_hex, verify_font}; @@ -237,15 +238,6 @@ pub struct Problem { pub error: Option, } -/// Which of a family's legal texts [`FontStore::legal_text`] reads. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum LegalText { - /// The licence file (`OFL.txt`, `LICENSE.txt`). - License, - /// An Apache family's `NOTICE` file. - Notice, -} - /// A licence or NOTICE file's size and sha256, when the index has them. A format 1.0 index has /// neither for the licence: the size cap is then the only check. fn check_license(file: Option<&LicenseFile>, bytes: &[u8]) -> Result<(), VerifyError> { @@ -690,22 +682,6 @@ pub(crate) fn read_limited(path: &Path, max: u64) -> Option> { std::fs::read(path).ok() } -/// The environment variable that sets the preview service's base URL; it beats the saved setting. -pub const PREVIEW_SERVICE_ENV: &str = "CRAFT_FONT_PREVIEW_URL"; - -/// Where the preview service in effect comes from. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum PreviewServiceSource { - /// [`PREVIEW_SERVICE_ENV`]; the setting can't change it. - Env, - /// The `preview_service` setting in `settings.json`. - Saved, - /// The host's default, [`FontStore::with_default_preview_service`]. - Default, - /// There is no service. - None, -} - /// The environment value when set and not empty, else the setting; an unusable value counts as /// unset. #[cfg(test)]