DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge PR #935
Brandon Thomas35ec71f04fa6
diff --cc crates/fonts/src/fontdb.rsindex 500192e2e,e56a6a36e..b9d0dc969--- a/crates/fonts/src/fontdb.rs+++ b/crates/fonts/src/fontdb.rs@@@ -360,9 -354,9 +364,10 @@@ struct CatalogEntrystruct ScannedFace {family: String,style: String,+ wws: Option<(String, String)>,group: FontGroup,native: Option<String>,+ postscript: Option<String>,}/// Process-wide font database: the fonts every document shares (bundled, installed, added), and@@@ -639,8 -658,8 +669,9 @@@ fn file_face_names(path: &std::path::Pacrate::group::classify(&skrifa::FontRef::new(&font).ok()?, &family)}};+ let postscript = name(&f, &[StringId::POSTSCRIPT_NAME]);- Some(ScannedFace { family, style, group, native, postscript })+ let wws = wws_names(&f, &family, &style);- Some(ScannedFace { family, style, wws, group, native })++ Some(ScannedFace { family, style, wws, group, native, postscript })}).collect()}@@@ -1082,14 -1087,7 +1115,15 @@@ impl FontDbcontinue;}for f in file_face_names(&p) {- found.push(CatalogEntry { family: f.family, style: f.style, wws: f.wws, path: p.clone(), group: f.group, native: f.native });+ found.push(CatalogEntry {+ family: f.family,+ style: f.style,++ wws: f.wws,+ path: p.clone(),+ group: f.group,+ native: f.native,+ postscript: f.postscript,+ });}}}@@@ -1549,23 -1540,8 +1596,23 @@@ impl ScopedFonts<'_>self.has_exact_family(family) || without_format_suffix(family).is_some_and(|base| self.has_exact_family(base))}+ /// The family and style of the face whose PostScript name is `name`, ignoring case (the+ /// document's, loaded, or installed on the system, not loading it). SVGs exported from+ /// Illustrator name fonts this way (`font-family="'MyriadPro-Regular'"`).+ pub fn postscript_face(&self, name: &str) -> Option<(String, String)> {+ let named = |ps: Option<&str>| ps.is_some_and(|ps| ps.eq_ignore_ascii_case(name));+ if let Some(f) = self.own().iter().chain(self.db.read_faces().iter()).find(|f| named(f.postscript_name())) {+ return Some((f.family.clone(), f.style.clone()));+ }+ #[cfg(not(target_arch = "wasm32"))]+ if let Some(c) = self.db.read_catalog().iter().find(|c| named(c.postscript.as_deref())) {+ return Some((c.family.clone(), c.style.clone()));+ }+ None+ }+fn has_exact_family(&self, family: &str) -> bool {- if self.own().iter().any(|f| f.family.eq_ignore_ascii_case(family)) || self.db.is_loaded(family) {+ if self.own().iter().any(|f| f.family.eq_ignore_ascii_case(family) || has_wws_family(f, family)) || self.db.is_loaded(family) {return true;}#[cfg(not(target_arch = "wasm32"))]diff --cc crates/fonts/src/tests_docfonts.rsindex f2f73bca7,f5d84b8a7..f7bad7947--- a/crates/fonts/src/tests_docfonts.rs+++ b/crates/fonts/src/tests_docfonts.rs@@@ -167,26 -167,22 +167,46 @@@ fn restricted_licence_reads_the_embeddiassert_eq!(db.face(FALLBACK_FAMILY, "Regular").source, FontSource::Bundled);}+#[test]+fn the_access_check_vets_the_folder_and_every_file_before_it_is_opened() {+ let folder = temp_dir("access");+ write_font(&folder, "kept.ttf", "DocFont Kept", &['k']);+ write_font(&folder, "refused.ttf", "DocFont Refused", &['r']);+ let db = FontDb::with_font_dirs(vec![]);+ let asked = std::sync::Mutex::new(Vec::new());+ let allow = |p: &Path| {+ asked.lock().unwrap().push(p.to_path_buf());+ if p.ends_with("refused.ttf") { Err("refused.ttf: outside the read root".to_string()) } else { Ok(()) }+ };+ let r = db.load_document_fonts(&folder, &allow);+ assert_eq!(r.faces, 1, "{:?}", r.skipped);+ assert_eq!(r.skipped, vec!["refused.ttf: outside the read root".to_string()]);+ let asked = asked.into_inner().unwrap();+ assert_eq!(asked.first().map(PathBuf::as_path), Some(folder.as_path()), "the folder is asked about first");+ assert!(asked.iter().any(|p| p.ends_with("kept.ttf")) && asked.iter().any(|p| p.ends_with("refused.ttf")));++ // A refused folder is not even listed.+ let r = db.load_document_fonts(&folder, &|_| Err("no".to_string()));+ assert_eq!((r.faces, r.scope, r.skipped), (0, 0, vec!["no".to_string()]));+ let _ = std::fs::remove_dir_all(&folder);+}+++ #[test]+ fn optical_size_face_is_found_by_its_wws_names() {+ let folder = temp_dir("wws");+ let bytes = font_with_wws("DocFont Kepler", "Light Display", "DocFont Kepler Display", "Light", &['k']).unwrap();+ std::fs::write(folder.join("light-display.otf"), bytes).unwrap();+ let db = FontDb::with_font_dirs(vec![]);- let r = db.load_document_fonts(&folder);++ let r = db.load_document_fonts(&folder, &|_| Ok(()));+ assert_eq!((r.faces, r.skipped.len()), (1, 0), "{:?}", r.skipped);+ let fonts = db.scoped(r.scope);+ assert!(fonts.has_family("DocFont Kepler Display"), "the WWS family counts as available");+ assert_eq!(fonts.face("DocFont Kepler Display", "Light").style, "Light Display");+ // `font.list` checks the style against these: "Light" isn't a missing style.+ assert_eq!(fonts.styles("DocFont Kepler Display"), ["Light"]);+ // The typographic names still find it.+ assert!(fonts.has_family("DocFont Kepler"));+ assert_eq!(fonts.face("DocFont Kepler", "Light Display").style, "Light Display");+ assert_eq!(fonts.styles("DocFont Kepler"), ["Light Display"]);+ let _ = std::fs::remove_dir_all(&folder);+ }