diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index a29711ee9..fc7b50013 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -8,7 +8,9 @@ author, source and license. Library code and original assets are under the repos |---|---|---|---| | `crates/cms/profiles/photocraft-coated-cmyk.icc` | PhotoCraft contributors; generated by `craft_cms::synth::coated_cmyk` | [PhotoCraft `crates/cms`](https://github.com/storytold/photocraft/tree/b3222ee8e7e6bcb4799c37f904c595f20f9baf5e/crates/cms) | CC0-1.0 | | `crates/cms/profiles/craft-coated-fogra39.icc` | craft-libs contributors; generated by `crates/cms/tests/fogra39.rs` from the FOGRA39 data below | this repository | CC0-1.0 | -| `crates/font-store/index/google-fonts-index.json.gz` | google-fonts-index contributors; generated from the METADATA.pb files of [google/fonts](https://github.com/google/fonts) at `45e3d5269e4a17d823f6d84470bc084dc5ae46f6` | [aiongg/google-fonts-index](https://github.com/aiongg/google-fonts-index/tree/8d52c53eaa40f17e7441f8234389cdbed196eb7b) | Apache-2.0 | +| `crates/font-store/index/sources.json` (unmodified) | craft-font-index contributors | [aiongg/craft-font-index@5a9a7f8](https://github.com/aiongg/craft-font-index/tree/5a9a7f8038c8f3e2e1583a79c92a816e8e71b34f) | Apache-2.0 | +| `crates/font-store/index/indexes/google-fonts.json.gz` (unmodified) | craft-font-index contributors; generated from the METADATA.pb files of [google/fonts](https://github.com/google/fonts) at `45e3d5269e4a17d823f6d84470bc084dc5ae46f6` | [aiongg/craft-font-index@5a9a7f8](https://github.com/aiongg/craft-font-index/tree/5a9a7f8038c8f3e2e1583a79c92a816e8e71b34f) | Apache-2.0 | +| `crates/font-store/index/indexes/noto.json.gz` (unmodified) | craft-font-index contributors; generated from [notofonts/notofonts.github.io](https://github.com/notofonts/notofonts.github.io) at `93fac8abebe6c7fed0c5b7d2456d8fa484fa146c` and [noto-cjk](https://github.com/notofonts/noto-cjk) releases | [aiongg/craft-font-index@5a9a7f8](https://github.com/aiongg/craft-font-index/tree/5a9a7f8038c8f3e2e1583a79c92a816e8e71b34f) | Apache-2.0 | | `crates/cms/tests/data/FOGRA39.txt` (unmodified) | Fogra | [Fogra characterisation data](https://fogra.org/en/downloads/work-tools/characterisation-data), `FOGRA39_to_FOGRA60_v2.zip` | Fogra terms: free use and redistribution, unmodified, with Fogra identified as the source; "FOGRA39" identifies the reference data only and implies no certification, approval or endorsement by Fogra (full text in [NOTICE](NOTICE)) | | `crates/i18n/data/collation.postcard` | Unicode, Inc. (ICU 78.1 collation data, CLDR 48.2.1); generated by `crates/i18n/data/generate-collation.sh` | [ICU](https://github.com/unicode-org/icu/releases/tag/release-78.1rc) | Unicode-3.0 | | `crates/text/assets/hyphen/en-us.dic` | Grady Ward (Moby Hyphenator II), converted by DesignCraft | [Project Gutenberg eBook #3204](https://www.gutenberg.org/ebooks/3204) | Public domain | diff --git a/crates/font-manager/src/panel/app.rs b/crates/font-manager/src/panel/app.rs index ccdca997e..66fd9b5ad 100644 --- a/crates/font-manager/src/panel/app.rs +++ b/crates/font-manager/src/panel/app.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use crate::craft_font_catalog::{FontEnv, Limits, Os}; use crate::filter::{Query, Search}; -use crate::index::{Category, Family, Index, categories}; +use crate::index::{Category, Family, Index, categories, source_label}; use crate::installed::{InstalledFamily, group}; use crate::store::{Installer, RemoveRequest, StoreError}; use crate::{Catalog, FaceInfo, Source}; @@ -209,6 +209,11 @@ pub(crate) fn family_key(source: &str, family: &str) -> String { format!("{source}\n{}", family.to_lowercase()) } +/// The host of an https URL. +fn url_host(url: &str) -> Option<&str> { + url.strip_prefix("https://")?.split(['/', '?', '#']).next().filter(|h| !h.is_empty()) +} + pub(crate) fn category_name(c: Category) -> &'static str { match c { Category::SansSerif => tl!("Sans serif"), @@ -671,14 +676,27 @@ impl FontsPanel { let Some(pending) = self.asking.clone() else { return }; let Ok(c) = &self.catalogue else { return }; let Some(family) = c.index.families().get(pending.family()) else { return }; + let file = match &pending { + Pending::InstallFile(_, path) => family.files.iter().find(|f| &f.path == path), + _ => family.files.first(), + }; let size = match &pending { - Pending::InstallFile(_, path) => family.files.iter().find(|f| &f.path == path).map_or(0, |f| f.size), + Pending::InstallFile(..) => file.map_or(0, |f| f.size), _ => family.size(), }; - let text = fmt( - "Craft Font Manager downloads {family} ({size}) from Google Fonts' GitHub repository through jsDelivr, and checks every file before using it.", - &[("family", &family.family), ("size", &i18n::size(size))], - ); + let (source, curated) = source_label(&c.index, family); + // A curated source's files come through its repository's CDN; a source the user added + // can point anywhere, so the dialog names the server. + let host = file.filter(|_| !curated).and_then(|f| c.index.file_urls(family, f).first().and_then(|u| url_host(u)).map(str::to_owned)); + let args = [("family", family.family.as_str()), ("size", &i18n::size(size)), ("source", source), ("host", host.as_deref().unwrap_or(""))]; + let text = match &host { + None => fmt("Craft Font Manager downloads {family} ({size}) from {source}, and checks every file before using it.", &args), + Some(_) => fmt( + "Craft Font Manager downloads {family} ({size}) from {host}, the server of {source}, a source you added, and checks every file before using it.", + &args, + ), + }; + let title = fmt("Download from {source}?", &[("source", source)]); let s = self.skin.clone(); let mut allow = false; let mut cancel = false; @@ -687,7 +705,7 @@ impl FontsPanel { let response = egui::Modal::new(egui::Id::unique("craft-font-manager.consent")).frame(frame).show(ctx, |ui| { ui.set_max_width(440.0); ui.spacing_mut().item_spacing.y = 10.0; - let title = egui::RichText::new(tl!("Download from Google Fonts?")).size(22.0).family(s.look.display.clone()).color(s.p.ink_strong); + let title = egui::RichText::new(&title).size(22.0).family(s.look.display.clone()).color(s.p.ink_strong); ui.add(egui::Label::new(title).wrap()); ui.add(egui::Label::new(egui::RichText::new(text).color(s.p.ink)).wrap()); ui.add(egui::Label::new(egui::RichText::new(tl!("Downloads stay allowed until you turn them off.")).color(s.p.muted)).wrap()); diff --git a/crates/font-manager/src/panel/browse.rs b/crates/font-manager/src/panel/browse.rs index e7d471cbb..9973ae3fb 100644 --- a/crates/font-manager/src/panel/browse.rs +++ b/crates/font-manager/src/panel/browse.rs @@ -86,7 +86,7 @@ impl FontsPanel { if ui.checkbox(&mut allowed, tl!("Allow downloads")).changed() { let _ = self.set_downloads_allowed(allowed); } - let note = tl!("From Google Fonts' GitHub repository, only when you install or preview a font."); + let note = tl!("From each font's source, only when you install or preview a font."); ui.add(egui::Label::new(egui::RichText::new(note).size(12.0).color(s.p.muted)).wrap()); } }); diff --git a/crates/font-manager/src/panel/faces.rs b/crates/font-manager/src/panel/faces.rs index c912d3e8c..54f0f54c6 100644 --- a/crates/font-manager/src/panel/faces.rs +++ b/crates/font-manager/src/panel/faces.rs @@ -1,8 +1,10 @@ //! Drawing text in a font face itself: each face shown becomes an egui font family of its own. //! //! Adding a font makes egui rebuild its fonts, so faces are added a few per frame, and once more -//! than [`MAX_REGISTERED`] are loaded they are all taken out again, which keeps memory to the faces -//! on screen. Only the panel's own faces are touched: the host's fonts stay as they are. +//! than [`MAX_REGISTERED`] are loaded the ones not drawn in the last frame are taken out again, +//! which keeps memory to the faces on screen. Faces still on screen stay, however many there are, +//! so a full screen doesn't load its faces again every frame. Only the panel's own faces are +//! touched: the host's fonts stay as they are. use std::collections::{HashMap, HashSet}; use std::sync::Arc; @@ -33,9 +35,11 @@ pub enum Shown { #[derive(Default)] pub struct FaceFonts { - registered: HashMap, + /// Each face's family and the frame it was last drawn in. + registered: HashMap, failed: HashSet, budget: usize, + frame: u64, } fn key(owner: Owner, face: &FaceInfo) -> String { @@ -50,22 +54,35 @@ fn key(owner: Owner, face: &FaceInfo) -> String { impl FaceFonts { pub fn begin_frame(&mut self, ctx: &Context) { self.budget = PER_FRAME; - if self.registered.len() > MAX_REGISTERED { + let stale = self.next_frame(); + if !stale.is_empty() { let mut definitions = ctx.fonts(|f| f.definitions().clone()); - definitions.font_data.retain(|name, _| !self.registered.contains_key(name)); - definitions.families.retain(|family, _| !self.registered.values().any(|f| f == family)); + definitions.font_data.retain(|name, _| !stale.contains_key(name)); + definitions.families.retain(|family, _| !stale.values().any(|f| f == family)); ctx.set_fonts(definitions); - self.registered.clear(); } } + /// Starts a frame, and once more than [`MAX_REGISTERED`] faces are registered takes out the + /// ones the last frame didn't draw, returning them. + fn next_frame(&mut self) -> HashMap { + self.frame += 1; + if self.registered.len() <= MAX_REGISTERED { + return HashMap::new(); + } + let last = self.frame - 1; + let stale: Vec = self.registered.iter().filter(|(_, (_, used))| *used < last).map(|(k, _)| k.clone()).collect(); + stale.into_iter().filter_map(|k| self.registered.remove(&k).map(|(family, _)| (k, family))).collect() + } + /// The family that draws `face`, once egui has loaded it. pub fn family(&mut self, ctx: &Context, catalog: &Catalog, owner: Owner, face: &FaceInfo) -> Shown { let key = key(owner, face); if self.failed.contains(&key) { return Shown::Unavailable; } - if let Some(family) = self.registered.get(&key) { + if let Some((family, used)) = self.registered.get_mut(&key) { + *used = self.frame; let loaded = ctx.fonts(|f| f.definitions().families.contains_key(family)); if loaded { return Shown::Ready(family.clone()); @@ -89,8 +106,37 @@ impl FaceFonts { let family = FontFamily::Name(key.as_str().into()); let insert = InsertFontFamily { family: family.clone(), priority: FontPriority::Highest }; ctx.add_font(FontInsert::new(&key, FontData::from_blob(blob, data.index).tweak(tweak), vec![insert])); - self.registered.insert(key, family); + self.registered.insert(key, (family, self.frame)); ctx.request_repaint(); Shown::Loading } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_faces_off_screen_are_taken_out() { + let mut faces = FaceFonts::default(); + let register = |faces: &mut FaceFonts, range: std::ops::Range| { + for n in range { + faces.registered.insert(format!("face:{n}"), (FontFamily::Name(format!("face:{n}").into()), faces.frame)); + } + }; + faces.next_frame(); + register(&mut faces, 0..MAX_REGISTERED); + assert!(faces.next_frame().is_empty()); + // A screen of more faces than the limit keeps them all, frame after frame. + register(&mut faces, 0..MAX_REGISTERED + 10); + assert!(faces.next_frame().is_empty()); + register(&mut faces, 0..MAX_REGISTERED + 10); + assert!(faces.next_frame().is_empty()); + // Scrolled on: the faces the last frame didn't draw go, and only those. + register(&mut faces, 20..MAX_REGISTERED + 20); + let stale = faces.next_frame(); + assert_eq!(stale.len(), 20); + assert!(stale.keys().all(|k| k[5..].parse::().unwrap() < 20)); + assert_eq!(faces.registered.len(), MAX_REGISTERED); + } +} diff --git a/crates/font-manager/src/panel/i18n.rs b/crates/font-manager/src/panel/i18n.rs index 4c1f4b8c9..7e57a8185 100644 --- a/crates/font-manager/src/panel/i18n.rs +++ b/crates/font-manager/src/panel/i18n.rs @@ -65,9 +65,10 @@ pub fn number(n: u64) -> String { mod tests { use super::*; - /// Every row reads, and every string the panel shows has a row in each catalog. + /// Every row reads. The strings the panel shows without a row are listed (run with + /// `--nocapture`); they show in English, so a missing row never fails. #[test] - fn catalogs_cover_the_interface() { + fn catalogs_load_and_missing_rows_are_listed() { let (_, problems) = I18n::new(CATALOGS, LANGUAGES); assert!(problems.is_empty(), "{problems:#?}"); // English-only by design: style and weight names, licence and product names. @@ -92,10 +93,10 @@ mod tests { let sources = [include_str!("app.rs"), include_str!("browse.rs"), include_str!("details.rs"), include_str!("sources.rs")]; let mut keys = Vec::new(); for source in sources { - for marker in ["tl!(\"", "fmt(\"", "fmt(\n \""] { + for marker in ["tl!(", "fmt("] { for part in source.split(marker).skip(1) { - if let Some(end) = part.find('"') { - keys.push(part[..end].to_owned()); + if let Some(key) = part.trim_start().strip_prefix('"').and_then(|p| p.find('"').map(|end| &p[..end])) { + keys.push(key.to_owned()); } } } @@ -105,7 +106,9 @@ mod tests { for lang in ["ja", "ar"] { let lang = I18N.match_tag(lang).unwrap_or(Lang::EN); let missing: Vec<_> = keys.iter().filter(|k| I18N.t(lang, k) == k.as_str()).collect(); - assert!(missing.is_empty(), "{}: {missing:#?}", I18N.code(lang)); + if !missing.is_empty() { + println!("{} is missing {} strings: {missing:#?}", I18N.code(lang), missing.len()); + } } } } diff --git a/crates/font-store/README.md b/crates/font-store/README.md index 40ba41824..122163020 100644 --- a/crates/font-store/README.md +++ b/crates/font-store/README.md @@ -98,7 +98,7 @@ indexes/ the last indexes fetched: sources.json, .json.gz, u .lock held while the store changes ``` -A Google Fonts family's folder is google/fonts' own folder name (`lato/`); another source's is `/` (`notofonts/noto-sans-cjk-jp/`, `user-fonts-example-org-myfonts/some-family/`). +A Google Fonts family's folder is google/fonts' own folder name (`lato/`); another source's is `/` (`notofonts/noto-sans-cjk-jp/`, `user-fonts-example-org-myfonts-bfeb06e2/some-family/`). `manifest.json` lists each installed family: its name, source, folder, licence and licence file, and per file its name, path in its repository, size, sha256, the index commit it came from, and its PostScript names. An entry is one family of one source, so the same name from two sources is two entries. A manifest that can't be read is moved aside to `manifest.json.bad` and counts as empty. diff --git a/crates/font-store/src/source.rs b/crates/font-store/src/source.rs index 2eb4f79fb..977e33cce 100644 --- a/crates/font-store/src/source.rs +++ b/crates/font-store/src/source.rs @@ -152,6 +152,15 @@ pub(crate) fn slug(s: &str) -> String { if out.is_empty() { crate::verify::sha256_hex(s.as_bytes()).chars().take(16).collect() } else { out } } +/// A user source's folder name: its ID's slug and a hash of the whole ID, since IDs that differ +/// only in punctuation, case or past the slug's length share a slug. +fn user_slug(id: &str) -> String { + let hash: String = crate::verify::sha256_hex(id.as_bytes()).chars().take(8).collect(); + let mut s = slug(id); + s.truncate(MAX_SEGMENT - hash.len() - 1); + format!("{}-{hash}", s.trim_end_matches('-')) +} + /// A relative path in a repository: `/`-separated, no empty, `.` or `..` segment, no `\`, and a /// file name the store can write. pub(crate) fn check_path(path: &str) -> Result<&str, IndexError> { @@ -317,7 +326,7 @@ fn check(raw: Raw, origin: Origin<'_>) -> Result { for s in &raw.scripts { check_str("script", &s.id)?; } - let slug = slug(&id); + let slug = if host.is_some() { user_slug(&id) } else { slug(&id) }; let ctx = Ctx { declared: &declared, id: &id, diff --git a/crates/font-store/src/store.rs b/crates/font-store/src/store.rs index ff9a59374..db1bd6f0f 100644 --- a/crates/font-store/src/store.rs +++ b/crates/font-store/src/store.rs @@ -623,21 +623,27 @@ impl FontStore { } /// Removes some files of an installed family (one style, say), by their names in its folder. - /// Taking out the last one removes the family. + /// Taking out the last one removes the family. Names the family's manifest entry doesn't list + /// are left alone. pub fn remove_files(&self, source: &str, name: &str, files: &[String]) -> Result<(), Error> { let result = { let _lock = self.lock()?; let mut manifest = self.read_manifest(); let pos = manifest.families.iter().position(|f| f.is(source, name)).ok_or_else(|| Error::NotInstalled(name.to_string()))?; let Some(entry) = manifest.families.get_mut(pos) else { return Err(Error::NotInstalled(name.to_string())) }; - entry.files.retain(|f| !files.contains(&f.name)); + // Only listed, safe names become paths, so a request can't reach outside the folder. + let removing: Vec = entry.files.iter().map(|f| f.name.clone()).filter(|n| safe_file_name(n) && files.contains(n)).collect(); + if removing.is_empty() { + return Ok(()); + } + entry.files.retain(|f| !removing.contains(&f.name)); let folder = self.dir.join(&entry.dir); let stage = Stage::new(&self.dir)?; let moving: Vec<(PathBuf, PathBuf)> = if entry.files.is_empty() { manifest.families.remove(pos); vec![(folder, stage.dir.join("removed"))] } else { - files.iter().map(|f| (folder.join(f), stage.dir.join(f))).collect() + removing.iter().map(|f| (folder.join(f), stage.dir.join(f))).collect() }; let mut moved = Vec::new(); for (from, to) in &moving { diff --git a/crates/font-store/src/tests.rs b/crates/font-store/src/tests.rs index 62b407bc7..cb0420e81 100644 --- a/crates/font-store/src/tests.rs +++ b/crates/font-store/src/tests.rs @@ -396,7 +396,7 @@ fn a_user_index_is_never_curated_and_cant_shadow_a_built_in_source() { assert_eq!(source.warnings.len(), 1); let cjk = index.family("Noto Sans CJK Test").unwrap(); assert!(!cjk.is_curated()); - assert_eq!(cjk.dir(), "user-fonts-example-org-notofonts/noto-sans-cjk-test"); + assert_eq!(cjk.dir(), "user-fonts-example-org-notofonts-6b687c53/noto-sans-cjk-test"); assert_eq!(index.file_urls(cjk, &cjk.files[0]), [format!("https://fonts.example.org/files/{CJK_PATH}")]); // Merged after a built-in source of the ID it claims, it is a separate source. let merged = Index::merge([index_of(¬o_fixture().index).unwrap(), index]); @@ -405,6 +405,18 @@ fn a_user_index_is_never_curated_and_cant_shadow_a_built_in_source() { assert!(Index::from_user_bytes(v.to_string().as_bytes(), "http://fonts.example.org/index.json").is_err()); } +#[test] +fn user_sources_whose_ids_slug_alike_get_their_own_folders() { + let mut v = noto_fixture().index; + v["source_info"]["file_urls"] = json!(["https://fonts.example.org/files/{path}"]); + let dir = |url: &str| Index::from_user_bytes(v.to_string().as_bytes(), url).unwrap().family("Noto Sans CJK Test").unwrap().dir().to_string(); + let (a, b) = (dir("https://fonts.example.org/index.json"), dir("https://fonts-example.org/index.json")); + assert_ne!(a, b); + assert!(a.split('/').all(|s| s.len() <= 64) && b.split('/').all(|s| s.len() <= 64)); + let long = format!("https://{}.example.org/index.json", "a".repeat(60)); + assert!(dir(&long).split('/').all(|s| s.len() <= 64)); +} + #[cfg(not(target_arch = "wasm32"))] mod store { use std::collections::HashMap; @@ -661,6 +673,30 @@ mod store { let _ = std::fs::remove_dir_all(root); } + #[test] + fn removing_files_touches_only_listed_names() { + let server = Server::start(); + let f = fixture(); + server.serve_fixture(&f); + let index = index_of(&f.index).unwrap(); + let dir = temp("remove-listed"); + let store = user_store(&server, &dir); + store.install_family(&index, index.family("Test Sans").unwrap(), &Cancel::new(), &mut none).unwrap(); + std::fs::write(dir.join("outside.ttf"), b"x").unwrap(); + std::fs::write(dir.join("testsans/Unlisted.ttf"), b"x").unwrap(); + + store.remove_files(GOOGLE_FONTS, "Test Sans", &["../outside.ttf".into(), "Unlisted.ttf".into()]).unwrap(); + assert!(dir.join("outside.ttf").exists()); + assert!(dir.join("testsans/Unlisted.ttf").exists()); + assert_eq!(store.installed_family(GOOGLE_FONTS, "Test Sans").unwrap().files.len(), 3); + + store.remove_files(GOOGLE_FONTS, "Test Sans", &["../outside.ttf".into(), "TestSans-Bold.ttf".into()]).unwrap(); + assert!(dir.join("outside.ttf").exists()); + assert!(!dir.join("testsans/TestSans-Bold.ttf").exists()); + assert_eq!(store.installed_family(GOOGLE_FONTS, "Test Sans").unwrap().files.len(), 2); + let _ = std::fs::remove_dir_all(dir); + } + #[test] fn jsdelivr_failures_fall_back_to_raw_github() { let server = Server::start(); @@ -950,7 +986,7 @@ mod store { let family = theirs[1]; let installed = store.install_family(&index, family, &Cancel::new(), &mut none).unwrap(); assert_eq!(installed.source, "user:fonts.example.org/notofonts"); - assert!(dir.join("user-fonts-example-org-notofonts/noto-sans-cjk-test/LICENSE").is_file()); + assert!(dir.join("user-fonts-example-org-notofonts-6b687c53/noto-sans-cjk-test/LICENSE").is_file()); // A changed file shows as changed, from the network and from the cache. v["families"][1]["designer"] = json!("Someone else");