host: self-hosted Google Fonts (gfonts) and the font cache (fonts)
gfonts (pure): CSS2 API URLs, the fonts.googleapis.com/fonts.gstatic.com allowlist, CSS parsing and face selection, woff2 checks, the sha256 manifest cache format and the page script, whose origin guard and runtime global now come from the SiteSpec. fonts (net feature): the download client (allowlist re-checked per request, no redirects, size caps), the family/preview caches and install/remove/faces/preview for the app's shell-only commands. Tests and CSS fixtures moved with them.
This commit is contained in:
parent
683c931ae8
commit
71fc0a653b
6 changed files with 1176 additions and 0 deletions
308
crates/waybetter-host/src/fonts.rs
Normal file
308
crates/waybetter-host/src/fonts.rs
Normal file
|
|
@ -0,0 +1,308 @@
|
||||||
|
//! 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<PathBuf> = OnceLock::new();
|
||||||
|
|
||||||
|
/// Sets the font cache folder: `<app data>/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<Option<HashMap<String, serde_json::Value>>> = Mutex::new(None);
|
||||||
|
/// Text-subset preview cache, keyed by `"<family>|<weights>|<text>"`, capped at 400 entries.
|
||||||
|
static PREVIEWS: Mutex<Option<HashMap<String, Vec<serde_json::Value>>>> = 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<serde_json::Value> {
|
||||||
|
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<String> {
|
||||||
|
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<reqwest::Client> = 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<Vec<u8>, 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<Vec<u8>, 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<Face>, per_file_max: usize, family_max: Option<usize>) -> Result<Vec<(Face, Vec<u8>)>, 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<gfonts::Manifest> 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<Vec<InstalledFont>, 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<InstalledFont, String> {
|
||||||
|
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<serde_json::Value, String> {
|
||||||
|
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<Vec<serde_json::Value>, 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<Vec<serde_json::Value>, 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
750
crates/waybetter-host/src/gfonts.rs
Normal file
750
crates/waybetter-host/src/gfonts.rs
Normal file
|
|
@ -0,0 +1,750 @@
|
||||||
|
//! Google Fonts, self-hosted: a site's CSP usually blocks remote `@font-face` fetches, so the app
|
||||||
|
//! downloads woff2 files natively (`fonts.googleapis.com`/`fonts.gstatic.com` only) and hands the
|
||||||
|
//! raw bytes to site pages as `FontFace(ArrayBuffer)`.
|
||||||
|
//!
|
||||||
|
//! Pure module (no network, no Tauri): CSS parsing, URL/byte validation, the on-disk cache format
|
||||||
|
//! and the page script. [`crate::fonts`] (`net` feature) downloads and caches with it.
|
||||||
|
|
||||||
|
use crate::site::SiteSpec;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
pub const CSS_ORIGIN: &str = "https://fonts.googleapis.com";
|
||||||
|
pub const FILE_HOST: &str = "fonts.gstatic.com";
|
||||||
|
pub const MAX_CSS_BYTES: usize = 256 * 1024;
|
||||||
|
pub const MAX_FILE_BYTES: usize = 1_500_000;
|
||||||
|
pub const MAX_FAMILY_BYTES: usize = 4_000_000;
|
||||||
|
pub const MAX_CACHE_BYTES: usize = 80_000_000;
|
||||||
|
pub const MAX_FAMILIES: usize = 64;
|
||||||
|
pub const MAX_PREVIEW_TEXT: usize = 120;
|
||||||
|
pub const MAX_PREVIEW_BYTES: usize = 300_000;
|
||||||
|
pub const KEEP_SUBSETS: [&str; 2] = ["latin", "latin-ext"];
|
||||||
|
pub const FONTS_DIR: &str = "fonts";
|
||||||
|
pub const MANIFEST: &str = "manifest.json";
|
||||||
|
/// Google only serves woff2 to modern desktop browsers; a stale/unknown UA gets ttf/eot instead.
|
||||||
|
pub const USER_AGENT: &str =
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36";
|
||||||
|
|
||||||
|
/// Google family names: letters, digits and single interior spaces (`"Noto Sans JP"`, `"Lato"`).
|
||||||
|
pub fn valid_family(name: &str) -> bool {
|
||||||
|
if name.is_empty() || name.len() > 48 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let bytes = name.as_bytes();
|
||||||
|
if bytes[0] == b' ' || bytes[bytes.len() - 1] == b' ' {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if name.contains(" ") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
bytes.iter().all(|b| b.is_ascii_alphanumeric() || *b == b' ')
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase", tag = "kind")]
|
||||||
|
pub enum Weights {
|
||||||
|
Range { min: u16, max: u16 },
|
||||||
|
List { values: Vec<u16> },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Weights {
|
||||||
|
pub fn validate(&self) -> Result<(), String> {
|
||||||
|
match self {
|
||||||
|
Weights::Range { min, max } => {
|
||||||
|
if !(100..=1000).contains(min) || !(100..=1000).contains(max) || min > max {
|
||||||
|
return Err("VALIDATION: invalid weight range".into());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Weights::List { values } => {
|
||||||
|
if values.is_empty() || values.len() > 9 {
|
||||||
|
return Err("VALIDATION: 1-9 weights".into());
|
||||||
|
}
|
||||||
|
if !values.iter().all(|w| (100..=1000).contains(w)) {
|
||||||
|
return Err("VALIDATION: invalid weight".into());
|
||||||
|
}
|
||||||
|
let mut sorted = values.clone();
|
||||||
|
sorted.sort_unstable();
|
||||||
|
sorted.dedup();
|
||||||
|
if sorted.len() != values.len() {
|
||||||
|
return Err("VALIDATION: duplicate weight".into());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The css2 `wght@...` axis query, e.g. `"wght@100..900"` or `"wght@400;700"`.
|
||||||
|
pub fn query(&self) -> String {
|
||||||
|
match self {
|
||||||
|
Weights::Range { min, max } => format!("wght@{min}..{max}"),
|
||||||
|
Weights::List { values } => {
|
||||||
|
let mut sorted = values.clone();
|
||||||
|
sorted.sort_unstable();
|
||||||
|
let parts: Vec<String> = sorted.iter().map(|w| w.to_string()).collect();
|
||||||
|
format!("wght@{}", parts.join(";"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Builds `https://fonts.googleapis.com/css2?family=<family>:<weights>&display=swap[&text=...]`.
|
||||||
|
pub fn css_url(family: &str, weights: &Weights, text: Option<&str>) -> Result<Url, String> {
|
||||||
|
if !valid_family(family) {
|
||||||
|
return Err("VALIDATION: invalid family name".into());
|
||||||
|
}
|
||||||
|
weights.validate()?;
|
||||||
|
if let Some(text) = text {
|
||||||
|
let chars = text.chars().count();
|
||||||
|
if chars == 0 || chars > MAX_PREVIEW_TEXT {
|
||||||
|
return Err("VALIDATION: preview text length".into());
|
||||||
|
}
|
||||||
|
if text.chars().any(|c| c.is_control()) {
|
||||||
|
return Err("VALIDATION: preview text contains control characters".into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut url = Url::parse(&format!("{CSS_ORIGIN}/css2")).map_err(|_| "VALIDATION: unreachable")?;
|
||||||
|
{
|
||||||
|
let mut pairs = url.query_pairs_mut();
|
||||||
|
pairs.append_pair("family", &format!("{family}:{}", weights.query()));
|
||||||
|
pairs.append_pair("display", "swap");
|
||||||
|
if let Some(text) = text {
|
||||||
|
pairs.append_pair("text", text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `https://fonts.googleapis.com/css2?...` only, no userinfo, no explicit port.
|
||||||
|
pub fn allowed_css_url(u: &Url) -> bool {
|
||||||
|
u.scheme() == "https"
|
||||||
|
&& u.host_str() == Some("fonts.googleapis.com")
|
||||||
|
&& u.path() == "/css2"
|
||||||
|
&& u.username().is_empty()
|
||||||
|
&& u.password().is_none()
|
||||||
|
&& u.port().is_none()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `https://fonts.gstatic.com/s/...` (static) or `/l/font...` (`&text=` subset preview) only.
|
||||||
|
pub fn allowed_file_url(u: &Url) -> bool {
|
||||||
|
u.scheme() == "https"
|
||||||
|
&& u.host_str() == Some(FILE_HOST)
|
||||||
|
&& u.username().is_empty()
|
||||||
|
&& u.password().is_none()
|
||||||
|
&& u.port().is_none()
|
||||||
|
&& (u.path().starts_with("/s/") || u.path().starts_with("/l/font"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub struct Face {
|
||||||
|
pub subset: Option<String>,
|
||||||
|
pub style: String,
|
||||||
|
pub weight: (u16, u16),
|
||||||
|
pub url: String,
|
||||||
|
pub unicode_range: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn slice_between<'a>(text: &'a str, start: &str, terminators: &[char]) -> Option<&'a str> {
|
||||||
|
let idx = text.find(start)? + start.len();
|
||||||
|
let rest = &text[idx..];
|
||||||
|
let end = rest.find(|c| terminators.contains(&c)).unwrap_or(rest.len());
|
||||||
|
Some(rest[..end].trim())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parses `font-weight: 400;` or `font-weight: 100 900;` into `(min, max)`.
|
||||||
|
fn parse_weight(value: &str) -> Option<(u16, u16)> {
|
||||||
|
let mut parts = value.split_whitespace();
|
||||||
|
let a: u16 = parts.next()?.parse().ok()?;
|
||||||
|
let b: u16 = match parts.next() {
|
||||||
|
Some(s) => s.parse().ok()?,
|
||||||
|
None => a,
|
||||||
|
};
|
||||||
|
if parts.next().is_some() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
if !(1..=1000).contains(&a) || !(1..=1000).contains(&b) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some((a.min(b), a.max(b)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extracts the first `url(...) format('woff2')` from a `src:` declaration value.
|
||||||
|
fn woff2_url(src: &str) -> Option<String> {
|
||||||
|
let mut rest = src;
|
||||||
|
loop {
|
||||||
|
let url_idx = rest.find("url(")?;
|
||||||
|
let after_url = &rest[url_idx + 4..];
|
||||||
|
let close = after_url.find(')')?;
|
||||||
|
let raw = after_url[..close].trim();
|
||||||
|
let raw = raw.trim_matches(|c| c == '\'' || c == '"');
|
||||||
|
let after_close = &after_url[close + 1..];
|
||||||
|
let format_ok = after_close
|
||||||
|
.find("format(")
|
||||||
|
.map(|fi| {
|
||||||
|
let fafter = &after_close[fi + 7..];
|
||||||
|
let fclose = fafter.find(')').unwrap_or(0);
|
||||||
|
let fmt = fafter[..fclose].trim().trim_matches(|c| c == '\'' || c == '"');
|
||||||
|
fmt.eq_ignore_ascii_case("woff2")
|
||||||
|
})
|
||||||
|
.unwrap_or(false);
|
||||||
|
// Stop scanning this declaration once we pass the next comma boundary (a src: list entry).
|
||||||
|
let boundary = after_close.find(',').unwrap_or(after_close.len());
|
||||||
|
if format_ok {
|
||||||
|
return Some(raw.to_string());
|
||||||
|
}
|
||||||
|
rest = &after_close[boundary..];
|
||||||
|
if !rest.contains("url(") {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Hand-written `@font-face` block parser (no regex crate): tracks the `/* subset */` comment
|
||||||
|
/// that precedes each block, and pulls `font-style`, `font-weight`, `src` (woff2 only) and
|
||||||
|
/// `unicode-range` out of it.
|
||||||
|
pub fn parse_css(css: &str) -> Result<Vec<Face>, String> {
|
||||||
|
let trimmed = css.trim_start();
|
||||||
|
if trimmed.starts_with('<') {
|
||||||
|
return Err("FONTS: unknown family".into());
|
||||||
|
}
|
||||||
|
let mut faces = Vec::new();
|
||||||
|
let mut pos = 0usize;
|
||||||
|
let mut pending_subset: Option<String> = None;
|
||||||
|
while let Some(rel) = {
|
||||||
|
let comment_rel = css[pos..].find("/*");
|
||||||
|
let face_rel = css[pos..].find("@font-face");
|
||||||
|
match (comment_rel, face_rel) {
|
||||||
|
(Some(c), Some(f)) => Some(c.min(f)),
|
||||||
|
(Some(c), None) => Some(c),
|
||||||
|
(None, Some(f)) => Some(f),
|
||||||
|
(None, None) => None,
|
||||||
|
}
|
||||||
|
} {
|
||||||
|
let abs = pos + rel;
|
||||||
|
if css[abs..].starts_with("/*") {
|
||||||
|
let after = abs + 2;
|
||||||
|
let Some(end_rel) = css[after..].find("*/") else { break };
|
||||||
|
let comment = css[after..after + end_rel].trim();
|
||||||
|
if !comment.is_empty() && comment.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-') {
|
||||||
|
pending_subset = Some(comment.to_string());
|
||||||
|
}
|
||||||
|
pos = after + end_rel + 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// "@font-face"
|
||||||
|
let Some(brace_rel) = css[abs..].find('{') else { break };
|
||||||
|
let brace_abs = abs + brace_rel;
|
||||||
|
let Some(close_rel) = css[brace_abs..].find('}') else { break };
|
||||||
|
let block = &css[brace_abs + 1..brace_abs + close_rel];
|
||||||
|
pos = brace_abs + close_rel + 1;
|
||||||
|
|
||||||
|
let style = slice_between(block, "font-style:", &[';']).unwrap_or("normal").to_string();
|
||||||
|
let weight_raw = slice_between(block, "font-weight:", &[';']);
|
||||||
|
let src_raw = slice_between(block, "src:", &[';']);
|
||||||
|
let unicode_range = slice_between(block, "unicode-range:", &[';']).map(|s| s.to_string());
|
||||||
|
|
||||||
|
let (Some(weight_raw), Some(src_raw)) = (weight_raw, src_raw) else {
|
||||||
|
pending_subset = None;
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let Some(weight) = parse_weight(weight_raw) else {
|
||||||
|
pending_subset = None;
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let Some(url_str) = woff2_url(src_raw) else {
|
||||||
|
pending_subset = None;
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let Ok(parsed_url) = Url::parse(&url_str) else {
|
||||||
|
pending_subset = None;
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
if !allowed_file_url(&parsed_url) {
|
||||||
|
pending_subset = None;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
faces.push(Face { subset: pending_subset.take(), style, weight, url: url_str, unicode_range });
|
||||||
|
}
|
||||||
|
if faces.is_empty() {
|
||||||
|
return Err("FONTS: no usable font faces".into());
|
||||||
|
}
|
||||||
|
Ok(faces)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Keeps normal-style faces; if any carry a subset label, keeps only `latin`/`latin-ext`
|
||||||
|
/// (unlabeled faces, e.g. a `&text=` preview, are all kept). Capped at 24 faces.
|
||||||
|
pub fn select_faces(faces: Vec<Face>) -> Vec<Face> {
|
||||||
|
let normal: Vec<Face> = faces.into_iter().filter(|f| f.style == "normal").collect();
|
||||||
|
let has_labels = normal.iter().any(|f| f.subset.is_some());
|
||||||
|
let mut kept: Vec<Face> = if has_labels {
|
||||||
|
normal.into_iter().filter(|f| f.subset.as_deref().is_some_and(|s| KEEP_SUBSETS.contains(&s))).collect()
|
||||||
|
} else {
|
||||||
|
normal
|
||||||
|
};
|
||||||
|
kept.truncate(24);
|
||||||
|
kept
|
||||||
|
}
|
||||||
|
|
||||||
|
/// WOFF2 header: signature(4) flavor(4) length(4) numTables(2) reserved(2) totalSfntSize(4)
|
||||||
|
/// totalCompressedSize(4) majorVersion(2) minorVersion(2) metaOffset(4) metaLength(4)
|
||||||
|
/// metaOrigLength(4) privOffset(4) privLength(4) = 48 bytes.
|
||||||
|
pub fn is_woff2(bytes: &[u8]) -> bool {
|
||||||
|
if bytes.len() < 48 || &bytes[0..4] != b"wOF2" {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let length = u32::from_be_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]) as usize;
|
||||||
|
length == bytes.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn slug(family: &str) -> String {
|
||||||
|
family.to_lowercase().replace(' ', "-")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||||
|
pub struct CachedFace {
|
||||||
|
/// `"0.woff2"`, `"1.woff2"`, ...
|
||||||
|
pub file: String,
|
||||||
|
pub weight: [u16; 2],
|
||||||
|
pub unicode_range: Option<String>,
|
||||||
|
pub subset: Option<String>,
|
||||||
|
pub bytes: usize,
|
||||||
|
/// Lowercase hex.
|
||||||
|
pub sha256: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||||
|
pub struct Manifest {
|
||||||
|
pub family: String,
|
||||||
|
pub weights: Weights,
|
||||||
|
pub faces: Vec<CachedFace>,
|
||||||
|
pub bytes: usize,
|
||||||
|
/// RFC3339.
|
||||||
|
pub installed_at: String,
|
||||||
|
pub source: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn valid_face_file(file: &str) -> Option<usize> {
|
||||||
|
let n = file.strip_suffix(".woff2")?;
|
||||||
|
if n.is_empty() || !n.bytes().all(|b| b.is_ascii_digit()) || (n.len() > 1 && n.starts_with('0')) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let n: usize = n.parse().ok()?;
|
||||||
|
if n >= 24 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Manifest {
|
||||||
|
pub fn validate(&self) -> Result<(), String> {
|
||||||
|
if !valid_family(&self.family) {
|
||||||
|
return Err("VALIDATION: invalid family name".into());
|
||||||
|
}
|
||||||
|
self.weights.validate()?;
|
||||||
|
if self.faces.is_empty() || self.faces.len() > 24 {
|
||||||
|
return Err("VALIDATION: invalid face count".into());
|
||||||
|
}
|
||||||
|
let mut total = 0usize;
|
||||||
|
for face in &self.faces {
|
||||||
|
if valid_face_file(&face.file).is_none() {
|
||||||
|
return Err("VALIDATION: invalid cached face file name".into());
|
||||||
|
}
|
||||||
|
if face.sha256.len() != 64 || !face.sha256.bytes().all(|b| b.is_ascii_hexdigit()) {
|
||||||
|
return Err("VALIDATION: invalid sha256".into());
|
||||||
|
}
|
||||||
|
if face.weight[0] > face.weight[1] || face.weight[0] < 1 || face.weight[1] > 1000 {
|
||||||
|
return Err("VALIDATION: invalid cached face weight".into());
|
||||||
|
}
|
||||||
|
total += face.bytes;
|
||||||
|
}
|
||||||
|
if total != self.bytes || total > MAX_FAMILY_BYTES {
|
||||||
|
return Err("VALIDATION: family size mismatch".into());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sha256_hex(bytes: &[u8]) -> String {
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.update(bytes);
|
||||||
|
hasher.finalize().iter().map(|b| format!("{b:02x}")).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads and verifies every cached face file under `dir` against `manifest`; returns their bytes
|
||||||
|
/// in manifest order. Any size/hash/magic mismatch is treated as a damaged cache, not a partial one.
|
||||||
|
pub fn verify_cached(dir: &Path, manifest: &Manifest) -> Result<Vec<Vec<u8>>, String> {
|
||||||
|
manifest.validate()?;
|
||||||
|
let mut out = Vec::with_capacity(manifest.faces.len());
|
||||||
|
for face in &manifest.faces {
|
||||||
|
let bytes = std::fs::read(dir.join(&face.file)).map_err(|_| "FONTS: cache damaged")?;
|
||||||
|
if bytes.len() != face.bytes || !is_woff2(&bytes) || sha256_hex(&bytes) != face.sha256 {
|
||||||
|
return Err("FONTS: cache damaged".into());
|
||||||
|
}
|
||||||
|
out.push(bytes);
|
||||||
|
}
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `fonts_root/<slug(family)>`, or `None` when `family` is not a valid Google Fonts name.
|
||||||
|
pub fn family_dir(fonts_root: &Path, family: &str) -> Option<PathBuf> {
|
||||||
|
if !valid_family(family) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(fonts_root.join(slug(family)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_manifest(fonts_root: &Path, family: &str) -> Option<Manifest> {
|
||||||
|
let dir = family_dir(fonts_root, family)?;
|
||||||
|
let text = std::fs::read_to_string(dir.join(MANIFEST)).ok()?;
|
||||||
|
let manifest: Manifest = serde_json::from_str(&text).ok()?;
|
||||||
|
manifest.validate().ok()?;
|
||||||
|
if manifest.family != family {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(manifest)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_installed(fonts_root: &Path) -> Vec<Manifest> {
|
||||||
|
let Ok(entries) = std::fs::read_dir(fonts_root) else { return Vec::new() };
|
||||||
|
let mut out = Vec::new();
|
||||||
|
for entry in entries.flatten() {
|
||||||
|
if out.len() >= MAX_FAMILIES {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let Ok(file_type) = entry.file_type() else { continue };
|
||||||
|
if !file_type.is_dir() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let text = std::fs::read_to_string(entry.path().join(MANIFEST)).ok();
|
||||||
|
let Some(text) = text else { continue };
|
||||||
|
let Ok(manifest) = serde_json::from_str::<Manifest>(&text) else { continue };
|
||||||
|
if manifest.validate().is_err() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
out.push(manifest);
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cache_bytes(fonts_root: &Path) -> usize {
|
||||||
|
list_installed(fonts_root).iter().map(|m| m.bytes).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validates every buffer, writes them into a fresh temp sibling directory, then swaps it in for
|
||||||
|
/// the family directory (best-effort atomic: remove old, rename temp into place).
|
||||||
|
pub fn write_family(
|
||||||
|
fonts_root: &Path,
|
||||||
|
family: &str,
|
||||||
|
weights: &Weights,
|
||||||
|
faces: &[(Face, Vec<u8>)],
|
||||||
|
installed_at: String,
|
||||||
|
) -> Result<Manifest, String> {
|
||||||
|
if !valid_family(family) {
|
||||||
|
return Err("VALIDATION: invalid family name".into());
|
||||||
|
}
|
||||||
|
weights.validate()?;
|
||||||
|
if faces.is_empty() || faces.len() > 24 {
|
||||||
|
return Err("VALIDATION: invalid face count".into());
|
||||||
|
}
|
||||||
|
let mut total = 0usize;
|
||||||
|
for (_, bytes) in faces {
|
||||||
|
if bytes.len() > MAX_FILE_BYTES || !is_woff2(bytes) {
|
||||||
|
return Err("FONTS: invalid font file".into());
|
||||||
|
}
|
||||||
|
total += bytes.len();
|
||||||
|
}
|
||||||
|
if total > MAX_FAMILY_BYTES {
|
||||||
|
return Err("FONTS: family too large".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let final_dir = family_dir(fonts_root, family).ok_or("VALIDATION: invalid family name")?;
|
||||||
|
let unique = std::time::SystemTime::now()
|
||||||
|
.duration_since(std::time::UNIX_EPOCH)
|
||||||
|
.map(|d| d.as_nanos())
|
||||||
|
.unwrap_or(0);
|
||||||
|
let tmp_dir = fonts_root.join(format!(".tmp-{}-{}-{unique}", slug(family), std::process::id()));
|
||||||
|
std::fs::create_dir_all(&tmp_dir).map_err(|error| format!("STORAGE: {error}"))?;
|
||||||
|
|
||||||
|
let mut cached_faces = Vec::with_capacity(faces.len());
|
||||||
|
for (index, (face, bytes)) in faces.iter().enumerate() {
|
||||||
|
let file = format!("{index}.woff2");
|
||||||
|
std::fs::write(tmp_dir.join(&file), bytes).map_err(|error| format!("STORAGE: {error}"))?;
|
||||||
|
cached_faces.push(CachedFace {
|
||||||
|
file,
|
||||||
|
weight: [face.weight.0, face.weight.1],
|
||||||
|
unicode_range: face.unicode_range.clone(),
|
||||||
|
subset: face.subset.clone(),
|
||||||
|
bytes: bytes.len(),
|
||||||
|
sha256: sha256_hex(bytes),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let manifest = Manifest {
|
||||||
|
family: family.to_string(),
|
||||||
|
weights: weights.clone(),
|
||||||
|
faces: cached_faces,
|
||||||
|
bytes: total,
|
||||||
|
installed_at,
|
||||||
|
source: "google".into(),
|
||||||
|
};
|
||||||
|
manifest.validate()?;
|
||||||
|
let json = serde_json::to_string_pretty(&manifest).map_err(|_| "STORAGE: serialization failed")?;
|
||||||
|
std::fs::write(tmp_dir.join(MANIFEST), json).map_err(|error| format!("STORAGE: {error}"))?;
|
||||||
|
|
||||||
|
if final_dir.exists() {
|
||||||
|
std::fs::remove_dir_all(&final_dir).map_err(|error| format!("STORAGE: {error}"))?;
|
||||||
|
}
|
||||||
|
std::fs::rename(&tmp_dir, &final_dir).map_err(|error| format!("STORAGE: {error}"))?;
|
||||||
|
Ok(manifest)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_family(fonts_root: &Path, family: &str) -> Result<(), String> {
|
||||||
|
let dir = family_dir(fonts_root, family).ok_or("VALIDATION: invalid family name")?;
|
||||||
|
if dir.exists() {
|
||||||
|
std::fs::remove_dir_all(&dir).map_err(|error| format!("STORAGE: {error}"))?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `{ "family": ..., "faces": [ { "weight": [min,max], "unicodeRange": ..., "data": "<base64>" } ] }`
|
||||||
|
pub fn faces_json(manifest: &Manifest, bytes: &[Vec<u8>]) -> serde_json::Value {
|
||||||
|
use base64::Engine;
|
||||||
|
let faces: Vec<serde_json::Value> = manifest
|
||||||
|
.faces
|
||||||
|
.iter()
|
||||||
|
.zip(bytes)
|
||||||
|
.map(|(face, data)| {
|
||||||
|
serde_json::json!({
|
||||||
|
"weight": face.weight,
|
||||||
|
"unicodeRange": face.unicode_range,
|
||||||
|
"data": base64::engine::general_purpose::STANDARD.encode(data),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
serde_json::json!({ "family": manifest.family, "faces": faces })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The script evaluated in every site tab so the page runtime (`window.<runtime_global>.fonts`) can
|
||||||
|
/// hand cached font bytes to `document.fonts` without the site's CSP ever seeing a remote font URL.
|
||||||
|
/// It does nothing outside the top frame on the site's primary origin.
|
||||||
|
pub fn page_script(site: &SiteSpec, fonts: &[serde_json::Value]) -> Option<String> {
|
||||||
|
if fonts.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let json = serde_json::to_string(fonts).ok()?;
|
||||||
|
let json = json.replace("</", "<\\/").replace('\u{2028}', "\\u2028").replace('\u{2029}', "\\u2029");
|
||||||
|
let (origin, global) = (site.primary_origin(), site.runtime_global);
|
||||||
|
Some(format!(
|
||||||
|
"(()=>{{if(window.top!==window||location.origin!=='{origin}')return;const f={json};const BT=window.{global};if(BT&&BT.fonts)BT.fonts.provide(f);else(window.__wbFonts=window.__wbFonts||[]).push(...f);}})();"
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::site::testing::SITE;
|
||||||
|
|
||||||
|
const INTER_CSS: &str = include_str!("../tests/fixtures/gfonts/inter.css");
|
||||||
|
const LATO_CSS: &str = include_str!("../tests/fixtures/gfonts/lato.css");
|
||||||
|
const LATO_TEXT_CSS: &str = include_str!("../tests/fixtures/gfonts/lato-text.css");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parses_inter_variable_font() {
|
||||||
|
let faces = parse_css(INTER_CSS).unwrap();
|
||||||
|
assert_eq!(faces.len(), 7);
|
||||||
|
let subsets: Vec<&str> = faces.iter().map(|f| f.subset.as_deref().unwrap()).collect();
|
||||||
|
assert_eq!(subsets, ["cyrillic-ext", "cyrillic", "greek-ext", "greek", "vietnamese", "latin-ext", "latin"]);
|
||||||
|
assert!(faces.iter().all(|f| f.weight == (100, 900)));
|
||||||
|
assert!(faces.iter().all(|f| f.style == "normal"));
|
||||||
|
|
||||||
|
let selected = select_faces(faces);
|
||||||
|
assert_eq!(selected.len(), 2);
|
||||||
|
let mut subsets: Vec<&str> = selected.iter().map(|f| f.subset.as_deref().unwrap()).collect();
|
||||||
|
subsets.sort_unstable();
|
||||||
|
assert_eq!(subsets, ["latin", "latin-ext"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parses_lato_static_weights() {
|
||||||
|
let faces = parse_css(LATO_CSS).unwrap();
|
||||||
|
assert_eq!(faces.len(), 4);
|
||||||
|
assert!(faces.iter().all(|f| f.weight == (400, 400) || f.weight == (700, 700)));
|
||||||
|
let selected = select_faces(faces);
|
||||||
|
assert_eq!(selected.len(), 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parses_lato_text_preview_without_subset_labels() {
|
||||||
|
let faces = parse_css(LATO_TEXT_CSS).unwrap();
|
||||||
|
assert_eq!(faces.len(), 2);
|
||||||
|
assert!(faces.iter().all(|f| f.subset.is_none()));
|
||||||
|
assert!(faces.iter().all(|f| f.url.contains("/l/font")));
|
||||||
|
let selected = select_faces(faces);
|
||||||
|
assert_eq!(selected.len(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn html_response_is_unknown_family() {
|
||||||
|
let err = parse_css("<!DOCTYPE html><html></html>").unwrap_err();
|
||||||
|
assert_eq!(err, "FONTS: unknown family");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty_css_has_no_usable_faces() {
|
||||||
|
let err = parse_css("/* nothing here */").unwrap_err();
|
||||||
|
assert_eq!(err, "FONTS: no usable font faces");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn css_url_allowlist_rejects_bad_hosts_and_paths() {
|
||||||
|
assert!(allowed_css_url(&Url::parse("https://fonts.googleapis.com/css2?family=Lato").unwrap()));
|
||||||
|
assert!(!allowed_css_url(&Url::parse("http://fonts.googleapis.com/css2?family=Lato").unwrap()));
|
||||||
|
assert!(!allowed_css_url(&Url::parse("https://fonts.googleapis.com/css3?family=Lato").unwrap()));
|
||||||
|
assert!(!allowed_css_url(&Url::parse("https://evil.test/fonts.googleapis.com/css2").unwrap()));
|
||||||
|
assert!(!allowed_css_url(&Url::parse("https://user@fonts.googleapis.com/css2").unwrap()));
|
||||||
|
assert!(!allowed_css_url(&Url::parse("https://fonts.googleapis.com:8443/css2").unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn file_url_allowlist_rejects_lookalike_hosts() {
|
||||||
|
assert!(allowed_file_url(&Url::parse("https://fonts.gstatic.com/s/lato/v25/x.woff2").unwrap()));
|
||||||
|
assert!(allowed_file_url(&Url::parse("https://fonts.gstatic.com/l/font?kit=abc").unwrap()));
|
||||||
|
assert!(!allowed_file_url(&Url::parse("https://fonts.gstatic.com.evil.test/s/lato/v25/x.woff2").unwrap()));
|
||||||
|
assert!(!allowed_file_url(&Url::parse("https://evil.test/fonts.gstatic.com/s/lato/v25/x.woff2").unwrap()));
|
||||||
|
assert!(!allowed_file_url(&Url::parse("http://fonts.gstatic.com/s/lato/v25/x.woff2").unwrap()));
|
||||||
|
assert!(!allowed_file_url(&Url::parse("https://fonts.gstatic.com/other/x.woff2").unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn css_url_encoding_and_validation() {
|
||||||
|
let url = css_url("Noto Sans JP", &Weights::List { values: vec![700, 400] }, None).unwrap();
|
||||||
|
assert!(url.as_str().starts_with("https://fonts.googleapis.com/css2?"));
|
||||||
|
assert!(url.as_str().contains("family=Noto+Sans+JP%3Awght%40400%3B700") || url.query().unwrap().contains("family=Noto+Sans+JP:wght@400;700"));
|
||||||
|
assert!(url.query().unwrap().contains("display=swap"));
|
||||||
|
|
||||||
|
let with_text = css_url("Lato", &Weights::Range { min: 400, max: 700 }, Some("Hi")).unwrap();
|
||||||
|
assert!(with_text.query().unwrap().contains("text=Hi"));
|
||||||
|
|
||||||
|
assert!(css_url("../x", &Weights::Range { min: 400, max: 400 }, None).is_err());
|
||||||
|
assert!(css_url("a\u{0}", &Weights::Range { min: 400, max: 400 }, None).is_err());
|
||||||
|
assert!(css_url("", &Weights::Range { min: 400, max: 400 }, None).is_err());
|
||||||
|
assert!(css_url(&"a".repeat(49), &Weights::Range { min: 400, max: 400 }, None).is_err());
|
||||||
|
assert!(css_url("Lato", &Weights::Range { min: 50, max: 400 }, None).is_err());
|
||||||
|
assert!(css_url("Lato", &Weights::Range { min: 400, max: 400 }, Some(&"a".repeat(121))).is_err());
|
||||||
|
assert!(css_url("Lato", &Weights::Range { min: 400, max: 400 }, Some("")).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn is_woff2_checks_magic_and_length_field() {
|
||||||
|
let mut good = vec![0u8; 48];
|
||||||
|
good[0..4].copy_from_slice(b"wOF2");
|
||||||
|
good[8..12].copy_from_slice(&48u32.to_be_bytes());
|
||||||
|
assert!(is_woff2(&good));
|
||||||
|
|
||||||
|
let mut bad_magic = good.clone();
|
||||||
|
bad_magic[0..4].copy_from_slice(b"XXXX");
|
||||||
|
assert!(!is_woff2(&bad_magic));
|
||||||
|
|
||||||
|
let mut bad_length = good.clone();
|
||||||
|
bad_length[8..12].copy_from_slice(&999u32.to_be_bytes());
|
||||||
|
assert!(!is_woff2(&bad_length));
|
||||||
|
|
||||||
|
assert!(!is_woff2(&[0u8; 10]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn write_family_read_manifest_verify_round_trip() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let mut face_bytes = vec![0u8; 100];
|
||||||
|
face_bytes[0..4].copy_from_slice(b"wOF2");
|
||||||
|
face_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: Some("U+00-FF".into()) };
|
||||||
|
let weights = Weights::List { values: vec![400] };
|
||||||
|
let manifest = write_family(dir.path(), "Lato", &weights, &[(face, face_bytes.clone())], "2026-01-01T00:00:00Z".into()).unwrap();
|
||||||
|
assert_eq!(manifest.bytes, 100);
|
||||||
|
|
||||||
|
let read = read_manifest(dir.path(), "Lato").unwrap();
|
||||||
|
assert_eq!(read, manifest);
|
||||||
|
|
||||||
|
let family_dir_path = family_dir(dir.path(), "Lato").unwrap();
|
||||||
|
let verified = verify_cached(&family_dir_path, &read).unwrap();
|
||||||
|
assert_eq!(verified, vec![face_bytes]);
|
||||||
|
|
||||||
|
assert_eq!(cache_bytes(dir.path()), 100);
|
||||||
|
assert_eq!(list_installed(dir.path()).len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tampered_cache_file_fails_verification() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let mut face_bytes = vec![0u8; 60];
|
||||||
|
face_bytes[0..4].copy_from_slice(b"wOF2");
|
||||||
|
face_bytes[8..12].copy_from_slice(&60u32.to_be_bytes());
|
||||||
|
let face = Face { subset: None, style: "normal".into(), weight: (400, 400), url: "https://fonts.gstatic.com/s/x.woff2".into(), unicode_range: None };
|
||||||
|
let weights = Weights::List { values: vec![400] };
|
||||||
|
let manifest = write_family(dir.path(), "Lato", &weights, &[(face, face_bytes)], "2026-01-01T00:00:00Z".into()).unwrap();
|
||||||
|
|
||||||
|
let family_dir_path = family_dir(dir.path(), "Lato").unwrap();
|
||||||
|
std::fs::write(family_dir_path.join(&manifest.faces[0].file), vec![1u8; 60]).unwrap();
|
||||||
|
let err = verify_cached(&family_dir_path, &manifest).unwrap_err();
|
||||||
|
assert_eq!(err, "FONTS: cache damaged");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oversized_buffer_is_rejected() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let mut face_bytes = vec![0u8; 48];
|
||||||
|
face_bytes[0..4].copy_from_slice(b"wOF2");
|
||||||
|
face_bytes[8..12].copy_from_slice(&(MAX_FILE_BYTES as u32 + 1).to_be_bytes());
|
||||||
|
face_bytes.resize(MAX_FILE_BYTES + 1, 0);
|
||||||
|
let face = Face { subset: None, style: "normal".into(), weight: (400, 400), url: "https://fonts.gstatic.com/s/x.woff2".into(), unicode_range: None };
|
||||||
|
let weights = Weights::List { values: vec![400] };
|
||||||
|
let err = write_family(dir.path(), "Lato", &weights, &[(face, face_bytes)], "2026-01-01T00:00:00Z".into()).unwrap_err();
|
||||||
|
assert_eq!(err, "FONTS: invalid font file");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn manifest_with_path_traversal_file_name_is_rejected() {
|
||||||
|
let manifest = Manifest {
|
||||||
|
family: "Lato".into(),
|
||||||
|
weights: Weights::List { values: vec![400] },
|
||||||
|
faces: vec![CachedFace { file: "../evil.woff2".into(), weight: [400, 400], unicode_range: None, subset: None, bytes: 48, sha256: "0".repeat(64) }],
|
||||||
|
bytes: 48,
|
||||||
|
installed_at: "2026-01-01T00:00:00Z".into(),
|
||||||
|
source: "google".into(),
|
||||||
|
};
|
||||||
|
assert!(manifest.validate().is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn page_script_escapes_closing_script_tags() {
|
||||||
|
let fonts = vec![serde_json::json!({ "family": "</script><script>alert(1)</script>", "faces": [] })];
|
||||||
|
let script = page_script(&SITE, &fonts).unwrap();
|
||||||
|
assert!(script.starts_with("(()=>{if(window.top!==window||location.origin!=='https://x.com')return;"));
|
||||||
|
assert!(script.contains("const BT=window.BetterTwttr;"));
|
||||||
|
assert!(!script.contains("</script>"));
|
||||||
|
assert!(script.contains("<\\/script>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn page_script_is_none_for_empty_fonts() {
|
||||||
|
assert!(page_script(&SITE, &[]).is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn valid_family_rejects_bad_names() {
|
||||||
|
assert!(valid_family("Lato"));
|
||||||
|
assert!(valid_family("Noto Sans JP"));
|
||||||
|
assert!(!valid_family(""));
|
||||||
|
assert!(!valid_family(" Lato"));
|
||||||
|
assert!(!valid_family("Lato "));
|
||||||
|
assert!(!valid_family("Lato Bold"));
|
||||||
|
assert!(!valid_family("../x"));
|
||||||
|
assert!(!valid_family(&"a".repeat(49)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -22,6 +22,8 @@
|
||||||
//! - [`devmode`]: developer mode's debounced watcher of plugins, custom CSS and a theme file.
|
//! - [`devmode`]: developer mode's debounced watcher of plugins, custom CSS and a theme file.
|
||||||
//! - [`alerts`]: local keyword alerts (keywords, snippets, per-item/per-keyword rate limits).
|
//! - [`alerts`]: local keyword alerts (keywords, snippets, per-item/per-keyword rate limits).
|
||||||
//! - [`media`]: gallery folders (path containment) and the opt-in clipboard history (private copies skipped).
|
//! - [`media`]: gallery folders (path containment) and the opt-in clipboard history (private copies skipped).
|
||||||
|
//! - [`gfonts`]: self-hosted Google Fonts: CSS parsing, URL/woff2 checks, the cache format, the page script;
|
||||||
|
//! [`fonts`] (`net` feature) downloads and caches them.
|
||||||
//! - [`store`]: installing themes and plugins from `waybetter://install` links, dropped files and the
|
//! - [`store`]: installing themes and plugins from `waybetter://install` links, dropped files and the
|
||||||
//! signed multi-app wayBetter Store (allow-list, caps, catalog signatures, staging, install records);
|
//! signed multi-app wayBetter Store (allow-list, caps, catalog signatures, staging, install records);
|
||||||
//! downloads with the `net` feature.
|
//! downloads with the `net` feature.
|
||||||
|
|
@ -31,6 +33,9 @@
|
||||||
pub mod alerts;
|
pub mod alerts;
|
||||||
pub mod backup;
|
pub mod backup;
|
||||||
pub mod devmode;
|
pub mod devmode;
|
||||||
|
#[cfg(feature = "net")]
|
||||||
|
pub mod fonts;
|
||||||
|
pub mod gfonts;
|
||||||
pub mod hotfix;
|
pub mod hotfix;
|
||||||
pub mod i18n;
|
pub mod i18n;
|
||||||
pub mod layout;
|
pub mod layout;
|
||||||
|
|
|
||||||
63
crates/waybetter-host/tests/fixtures/gfonts/inter.css
vendored
Normal file
63
crates/waybetter-host/tests/fixtures/gfonts/inter.css
vendored
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
/* cyrillic-ext */
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Inter';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 100 900;
|
||||||
|
font-display: swap;
|
||||||
|
src: url(https://fonts.gstatic.com/s/inter/v20/UcC73FwrK3iLTeHuS_nVMrMxCp50SjIa2JL7SUc.woff2) format('woff2');
|
||||||
|
unicode-range: U+0460-052F, U+1C80-1C8A, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
|
||||||
|
}
|
||||||
|
/* cyrillic */
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Inter';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 100 900;
|
||||||
|
font-display: swap;
|
||||||
|
src: url(https://fonts.gstatic.com/s/inter/v20/UcC73FwrK3iLTeHuS_nVMrMxCp50SjIa0ZL7SUc.woff2) format('woff2');
|
||||||
|
unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
|
||||||
|
}
|
||||||
|
/* greek-ext */
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Inter';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 100 900;
|
||||||
|
font-display: swap;
|
||||||
|
src: url(https://fonts.gstatic.com/s/inter/v20/UcC73FwrK3iLTeHuS_nVMrMxCp50SjIa2ZL7SUc.woff2) format('woff2');
|
||||||
|
unicode-range: U+1F00-1FFF;
|
||||||
|
}
|
||||||
|
/* greek */
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Inter';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 100 900;
|
||||||
|
font-display: swap;
|
||||||
|
src: url(https://fonts.gstatic.com/s/inter/v20/UcC73FwrK3iLTeHuS_nVMrMxCp50SjIa1pL7SUc.woff2) format('woff2');
|
||||||
|
unicode-range: U+0370-0377, U+037A-037F, U+0384-038A, U+038C, U+038E-03A1, U+03A3-03FF;
|
||||||
|
}
|
||||||
|
/* vietnamese */
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Inter';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 100 900;
|
||||||
|
font-display: swap;
|
||||||
|
src: url(https://fonts.gstatic.com/s/inter/v20/UcC73FwrK3iLTeHuS_nVMrMxCp50SjIa2pL7SUc.woff2) format('woff2');
|
||||||
|
unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+0300-0301, U+0303-0304, U+0308-0309, U+0323, U+0329, U+1EA0-1EF9, U+20AB;
|
||||||
|
}
|
||||||
|
/* latin-ext */
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Inter';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 100 900;
|
||||||
|
font-display: swap;
|
||||||
|
src: url(https://fonts.gstatic.com/s/inter/v20/UcC73FwrK3iLTeHuS_nVMrMxCp50SjIa25L7SUc.woff2) format('woff2');
|
||||||
|
unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
|
||||||
|
}
|
||||||
|
/* latin */
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Inter';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 100 900;
|
||||||
|
font-display: swap;
|
||||||
|
src: url(https://fonts.gstatic.com/s/inter/v20/UcC73FwrK3iLTeHuS_nVMrMxCp50SjIa1ZL7.woff2) format('woff2');
|
||||||
|
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
|
||||||
|
}
|
||||||
14
crates/waybetter-host/tests/fixtures/gfonts/lato-text.css
vendored
Normal file
14
crates/waybetter-host/tests/fixtures/gfonts/lato-text.css
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Lato';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 400;
|
||||||
|
src: url(https://fonts.gstatic.com/l/font?kit=S6uyw4BMUTPHvxwiUS_jT0aNgbBanoA&skey=2d58b92a99e1c086&v=v25) format('woff2');
|
||||||
|
unicode-range: U+20, U+4c, U+53, U+61, U+65, U+6c-6d, U+6f-70, U+74;
|
||||||
|
}
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Lato';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 700;
|
||||||
|
src: url(https://fonts.gstatic.com/l/font?kit=S6u9w4BMUTPHh6UVewqdFgekvrxQgpk3pro&skey=3480a19627739c0d&v=v25) format('woff2');
|
||||||
|
unicode-range: U+20, U+4c, U+53, U+61, U+65, U+6c-6d, U+6f-70, U+74;
|
||||||
|
}
|
||||||
36
crates/waybetter-host/tests/fixtures/gfonts/lato.css
vendored
Normal file
36
crates/waybetter-host/tests/fixtures/gfonts/lato.css
vendored
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
/* latin-ext */
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Lato';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 400;
|
||||||
|
font-display: swap;
|
||||||
|
src: url(https://fonts.gstatic.com/s/lato/v25/S6uyw4BMUTPHjxAwXjeu.woff2) format('woff2');
|
||||||
|
unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
|
||||||
|
}
|
||||||
|
/* latin */
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Lato';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 400;
|
||||||
|
font-display: swap;
|
||||||
|
src: url(https://fonts.gstatic.com/s/lato/v25/S6uyw4BMUTPHjx4wXg.woff2) format('woff2');
|
||||||
|
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
|
||||||
|
}
|
||||||
|
/* latin-ext */
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Lato';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 700;
|
||||||
|
font-display: swap;
|
||||||
|
src: url(https://fonts.gstatic.com/s/lato/v25/S6u9w4BMUTPHh6UVSwaPGR_p.woff2) format('woff2');
|
||||||
|
unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
|
||||||
|
}
|
||||||
|
/* latin */
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Lato';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 700;
|
||||||
|
font-display: swap;
|
||||||
|
src: url(https://fonts.gstatic.com/s/lato/v25/S6u9w4BMUTPHh6UVSwiPGQ.woff2) format('woff2');
|
||||||
|
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue