//! Google Fonts downloads and the font cache (`net` feature): fetches woff2 files from //! `fonts.googleapis.com`/`fonts.gstatic.com` only (allowlist re-checked before every request, no //! redirects or cookies, size caps, woff2 magic), caches them under the app-data `fonts/` folder //! with a sha256 manifest ([`crate::gfonts`]), and builds the page script from cached bytes, never //! a remote font URL. The app wraps these in its own shell-only commands and, after an install or //! removal, re-sends fonts to its site tabs. use crate::gfonts::{self, Face, Weights}; use crate::site::SiteSpec; use base64::Engine; use serde::Serialize; use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Mutex, OnceLock}; use std::time::Duration; use url::Url; static ROOT: OnceLock = OnceLock::new(); /// Sets the font cache folder: `/fonts` (created). Call once at startup. pub fn init(app_data: &Path) { let root = app_data.join(gfonts::FONTS_DIR); let _ = std::fs::create_dir_all(&root); let _ = ROOT.set(root); } pub fn root() -> Option<&'static Path> { ROOT.get().map(PathBuf::as_path) } fn root_or_err() -> Result<&'static Path, String> { root().ok_or_else(|| "STORAGE: no application data directory".into()) } /// Installed-family faces (`faces_json`), keyed by family; lazily filled, invalidated on install/remove. static FACES: Mutex>> = Mutex::new(None); /// Text-subset preview cache, keyed by `"||"`, capped at 400 entries. static PREVIEWS: Mutex>>> = Mutex::new(None); /// At most 6 preview fetches in flight (no `tokio::sync::Semaphore`: only the `time` feature is enabled). static PREVIEW_INFLIGHT: AtomicUsize = AtomicUsize::new(0); const MAX_PREVIEW_INFLIGHT: usize = 6; const MAX_PREVIEW_CACHE: usize = 400; fn invalidate_caches() { *FACES.lock().unwrap_or_else(|e| e.into_inner()) = None; *PREVIEWS.lock().unwrap_or_else(|e| e.into_inner()) = None; } fn faces_for(family: &str) -> Option { let mut guard = FACES.lock().unwrap_or_else(|e| e.into_inner()); let cache = guard.get_or_insert_with(HashMap::new); if let Some(value) = cache.get(family) { return Some(value.clone()); } drop(guard); let root = root()?; let manifest = gfonts::read_manifest(root, family)?; let dir = gfonts::family_dir(root, family)?; let bytes = gfonts::verify_cached(&dir, &manifest).ok()?; let value = gfonts::faces_json(&manifest, &bytes); let mut guard = FACES.lock().unwrap_or_else(|e| e.into_inner()); guard.get_or_insert_with(HashMap::new).insert(family.to_string(), value.clone()); Some(value) } /// Builds the page script for up to 8 distinct, installed, valid families. Missing families are /// skipped silently (e.g. removed since the page last asked). pub fn page_script_for(site: &SiteSpec, families: &[String]) -> Option { let mut seen = std::collections::HashSet::new(); let mut fonts = Vec::new(); for family in families { if fonts.len() >= 8 || !seen.insert(family.clone()) { continue; } if !gfonts::valid_family(family) { continue; } if let Some(value) = faces_for(family) { fonts.push(value); } } gfonts::page_script(site, &fonts) } fn client() -> Result<&'static reqwest::Client, String> { static CLIENT: OnceLock = OnceLock::new(); if let Some(client) = CLIENT.get() { return Ok(client); } if rustls::crypto::CryptoProvider::get_default().is_none() { let _ = rustls::crypto::ring::default_provider().install_default(); } let client = reqwest::Client::builder() .user_agent(gfonts::USER_AGENT) .redirect(reqwest::redirect::Policy::none()) .timeout(Duration::from_secs(30)) .build() .map_err(|e| format!("NETWORK: {e}"))?; Ok(CLIENT.get_or_init(|| client)) } async fn read_limited(mut response: reqwest::Response, max: usize) -> Result, String> { if response.content_length().is_some_and(|len| len as usize > max) { return Err("NETWORK: response too large".into()); } let mut bytes = Vec::new(); while let Some(chunk) = response.chunk().await.map_err(|e| format!("NETWORK: {e}"))? { if bytes.len() + chunk.len() > max { return Err("NETWORK: response too large".into()); } bytes.extend_from_slice(&chunk); } Ok(bytes) } /// Fetches `url`, re-checking the css/file allowlist right before the request. A 400 means Google /// didn't recognize the family/kit; any other non-200 is a plain network error. async fn fetch(url: &Url, max: usize) -> Result, String> { if !gfonts::allowed_css_url(url) && !gfonts::allowed_file_url(url) { return Err("VALIDATION: URL not allowed".into()); } let response = client()?.get(url.clone()).send().await.map_err(|e| format!("NETWORK: {e}"))?; let status = response.status(); if status.as_u16() == 400 { return Err("FONTS: unknown family".into()); } if status.as_u16() != 200 { return Err(format!("NETWORK: status {}", status.as_u16())); } read_limited(response, max).await } async fn download_faces(faces: Vec, per_file_max: usize, family_max: Option) -> Result)>, String> { let mut out = Vec::with_capacity(faces.len()); let mut total = 0usize; for face in faces { let url = Url::parse(&face.url).map_err(|_| "VALIDATION: invalid face URL")?; let bytes = fetch(&url, per_file_max).await?; if !gfonts::is_woff2(&bytes) { return Err("FONTS: invalid font file".into()); } total += bytes.len(); if let Some(max) = family_max { if total > max { return Err("FONTS: family too large".into()); } } out.push((face, bytes)); } Ok(out) } #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct InstalledFont { family: String, weights: Weights, bytes: usize, faces: usize, installed_at: String, } impl From for InstalledFont { fn from(m: gfonts::Manifest) -> Self { InstalledFont { family: m.family, weights: m.weights, bytes: m.bytes, faces: m.faces.len(), installed_at: m.installed_at } } } /// The installed families. pub fn installed() -> Result, String> { let root = root_or_err()?; Ok(gfonts::list_installed(root).into_iter().map(InstalledFont::from).collect()) } /// Downloads and caches `family` in `weights` (replacing an installed copy). The caller re-sends /// fonts to its tabs afterwards. pub async fn install(family: String, weights: Weights) -> Result { if !gfonts::valid_family(&family) { return Err("VALIDATION: invalid family name".into()); } weights.validate()?; let root = root_or_err()?; let already_installed = gfonts::read_manifest(root, &family).is_some(); let installed = gfonts::list_installed(root); if !already_installed && installed.len() >= gfonts::MAX_FAMILIES { return Err("FONTS: too many installed families".into()); } let current_bytes: usize = installed.iter().filter(|m| m.family != family).map(|m| m.bytes).sum(); let css_url = gfonts::css_url(&family, &weights, None)?; let css_bytes = fetch(&css_url, gfonts::MAX_CSS_BYTES).await?; let css = String::from_utf8(css_bytes).map_err(|_| "FONTS: invalid CSS response")?; let faces = gfonts::select_faces(gfonts::parse_css(&css)?); if faces.is_empty() { return Err("FONTS: no usable font faces".into()); } let remaining = gfonts::MAX_CACHE_BYTES.saturating_sub(current_bytes); let downloaded = download_faces(faces, gfonts::MAX_FILE_BYTES, Some(remaining.min(gfonts::MAX_FAMILY_BYTES))).await?; let installed_at = chrono::Utc::now().to_rfc3339(); let manifest = gfonts::write_family(root, &family, &weights, &downloaded, installed_at)?; invalidate_caches(); Ok(InstalledFont::from(manifest)) } /// Removes an installed family. The caller re-sends fonts to its tabs afterwards. pub fn remove(family: &str) -> Result<(), String> { let root = root_or_err()?; gfonts::remove_family(root, family)?; invalidate_caches(); Ok(()) } /// An installed family's faces with their bytes (verified against the manifest). pub fn faces(family: &str) -> Result { if !gfonts::valid_family(family) { return Err("VALIDATION: invalid family name".into()); } faces_for(family).ok_or_else(|| "FONTS: not installed".to_string()) } fn preview_key(family: &str, weights: &Weights, text: &str) -> String { format!("{family}|{}|{text}", weights.query()) } /// A text-subset preview of a family that is not installed (cached, at most 6 fetches at once). pub async fn preview(family: String, weights: Weights, text: String) -> Result, String> { let key = preview_key(&family, &weights, &text); { let mut guard = PREVIEWS.lock().unwrap_or_else(|e| e.into_inner()); if let Some(cached) = guard.get_or_insert_with(HashMap::new).get(&key) { return Ok(cached.clone()); } } if PREVIEW_INFLIGHT.fetch_add(1, Ordering::SeqCst) >= MAX_PREVIEW_INFLIGHT { PREVIEW_INFLIGHT.fetch_sub(1, Ordering::SeqCst); return Err("FONTS: busy".into()); } let result = preview_inner(&family, &weights, &text).await; PREVIEW_INFLIGHT.fetch_sub(1, Ordering::SeqCst); let faces = result?; let mut guard = PREVIEWS.lock().unwrap_or_else(|e| e.into_inner()); let cache = guard.get_or_insert_with(HashMap::new); if cache.len() >= MAX_PREVIEW_CACHE { cache.clear(); } cache.insert(key, faces.clone()); Ok(faces) } async fn preview_inner(family: &str, weights: &Weights, text: &str) -> Result, String> { let css_url = gfonts::css_url(family, weights, Some(text))?; let css_bytes = fetch(&css_url, gfonts::MAX_CSS_BYTES).await?; let css = String::from_utf8(css_bytes).map_err(|_| "FONTS: invalid CSS response")?; let faces = gfonts::select_faces(gfonts::parse_css(&css)?); if faces.is_empty() { return Err("FONTS: no usable font faces".into()); } let downloaded = download_faces(faces, gfonts::MAX_PREVIEW_BYTES, None).await?; let encoder = base64::engine::general_purpose::STANDARD; Ok(downloaded .into_iter() .map(|(face, bytes)| { serde_json::json!({ "weight": [face.weight.0, face.weight.1], "unicodeRange": face.unicode_range, "data": encoder.encode(&bytes), }) }) .collect()) } #[cfg(test)] mod tests { use super::*; use crate::site::testing::SITE; #[test] fn cache_serves_installed_families_to_the_page_and_forgets_removed_ones() { let data = tempfile::tempdir().unwrap(); init(data.path()); let root = root().unwrap(); assert!(installed().unwrap().is_empty()); let mut bytes = vec![0u8; 100]; bytes[0..4].copy_from_slice(b"wOF2"); bytes[8..12].copy_from_slice(&100u32.to_be_bytes()); let face = Face { subset: Some("latin".into()), style: "normal".into(), weight: (400, 400), url: "https://fonts.gstatic.com/s/x.woff2".into(), unicode_range: None }; gfonts::write_family(root, "Lato", &Weights::List { values: vec![400] }, &[(face, bytes)], "2026-01-01T00:00:00Z".into()).unwrap(); assert_eq!(installed().unwrap().len(), 1); assert_eq!(faces("Lato").unwrap()["family"], "Lato"); assert!(faces("../Lato").is_err()); let families = ["Lato".to_owned(), "Lato".to_owned(), "Missing".to_owned(), "bad/name".to_owned()]; let script = page_script_for(&SITE, &families).unwrap(); assert_eq!(script.matches("\"family\":\"Lato\"").count(), 1, "deduplicated, missing and invalid skipped"); remove("Lato").unwrap(); assert!(faces("Lato").is_err(), "the face cache is invalidated"); assert!(page_script_for(&SITE, &families).is_none()); } }