PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Open: warn about type in fonts that aren't installed (#4559) (#4576)
Dan Hiorns39f3660def96
diff --git a/crates/ui-egui/src/file_open.rs b/crates/ui-egui/src/file_open.rsindex 785d234d0..ac1ed2ba7 100644--- a/crates/ui-egui/src/file_open.rs+++ b/crates/ui-egui/src/file_open.rs@@ -33,6 +33,20 @@ pub enum DropTarget {Elsewhere,}+/// `warnings` from an import, plus one for each font family the active document's type uses that+/// isn't installed (#4559): Photoshop warns about missing fonts as a file opens, and the CLI and+/// automation already report them; the type would otherwise silently render in a fallback face.+pub(crate) fn with_missing_fonts(app: &PhotocraftApp, mut warnings: Vec<String>) -> Vec<String> {+ if let Some(st) = app.session.active() {+ for w in photocraft_engine::type_extra_cmds::missing_font_warnings(&st.doc) {+ if !warnings.contains(&w) {+ warnings.push(w);+ }+ }+ }+ warnings+}+/// A dropped file's name ("dropped" when it has none).fn dropped_name(f: &egui::DroppedFileHandle) -> String {f.path().file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "dropped".into())diff --git a/crates/ui-egui/src/file_open/tests.rs b/crates/ui-egui/src/file_open/tests.rsindex 67887877e..0177fc838 100644--- a/crates/ui-egui/src/file_open/tests.rs+++ b/crates/ui-egui/src/file_open/tests.rs@@ -817,3 +817,46 @@ fn saving_a_new_document_lists_it_in_recent_files() {answer(&mut app);assert_eq!(app.ui.recent_files, ["/pics/new.psd"], "a copy isn't the document's file");}++/// #4559: a file whose type uses a font that isn't installed opens with a notice naming it, as+/// Photoshop warns on open (the CLI and automation already did); the file still opens. Type in+/// an installed face adds nothing, and a font the importer already warned about isn't repeated.+#[test]+fn opening_type_in_a_missing_font_warns() {+ fn doc_with_type(font: Option<&str>) -> Document {+ let mut s = photocraft_engine::Session::new();+ s.execute("file.new", json!({"width": 40, "height": 20})).unwrap();+ let mut p = json!({"text": "Hi", "x": 2, "y": 12, "size": 10});+ if let Some(f) = font {+ p["font"] = json!(f);+ }+ s.execute("type.create", p).unwrap();+ (*s.active().unwrap().doc).clone()+ }+ const MISSING: &str = "NoSuchFamily4559";+ let warning = format!("font '{MISSING}' is not installed; text may render using a fallback face");+ let (with_missing, installed) = (doc_with_type(Some(MISSING)), doc_with_type(None));+ let already = warning.clone();+ let services = Services {+ import: Some(Box::new(move |name: &str, _bytes: &[u8], _depth: usize| {+ Ok(match name {+ "installed.psd" => (installed.clone(), Vec::new()),+ "warned.psd" => (with_missing.clone(), vec![already.clone()]),+ _ => (with_missing.clone(), Vec::new()),+ })+ })),+ ..Default::default()+ };+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), services);+ let warnings = app.open_bytes("poster.psd", b"x").unwrap();+ assert_eq!(warnings, vec![warning.clone()]);+ assert!(app.ui.status.contains(MISSING), "{}", app.ui.status);+ assert_eq!(app.ui.notices.len(), 1);+ assert_eq!(app.ui.notices[0].lines, vec![warning.clone()]);+ assert_eq!(app.session.documents().len(), 1, "the file still opens");+ // Type in an installed face: no notice.+ assert!(app.open_bytes("installed.psd", b"x").unwrap().is_empty());+ assert_eq!(app.ui.notices.len(), 1);+ // The importer's own warning for the same font isn't doubled.+ assert_eq!(app.open_bytes("warned.psd", b"x").unwrap(), vec![warning]);+}diff --git a/crates/ui-egui/src/jobs_ui.rs b/crates/ui-egui/src/jobs_ui.rsindex 83218402d..99479f4e3 100644--- a/crates/ui-egui/src/jobs_ui.rs+++ b/crates/ui-egui/src/jobs_ui.rs@@ -540,6 +540,7 @@ fn finish_open(app: &mut PhotocraftApp, name: &str, path: Option<&str>, v: &Valuapp.opened_from(p);}app.sync_views();+ let warnings = crate::file_open::with_missing_fonts(app, warnings);app.ui.status = crate::i18n::fmt(tl!("Opened {name}"), &[("name", name)]);app.ui.status_error = false;// A raw opens in Camera Raw first; the dialog says how it was developed.diff --git a/crates/ui-egui/src/jobs_ui_tests.rs b/crates/ui-egui/src/jobs_ui_tests.rsindex 3e2750f48..f2b7d0fbc 100644--- a/crates/ui-egui/src/jobs_ui_tests.rs+++ b/crates/ui-egui/src/jobs_ui_tests.rs@@ -766,3 +766,19 @@ fn esc_cancels_a_background_free_transform_commit() {assert!(!st.history.can_redo());assert_eq!(st.doc.layers.len(), before.layers.len());}++/// #4559: a background open (the desktop app's) also warns, when its document arrives, about+/// type in a font that isn't installed.+#[test]+fn background_open_warns_about_missing_fonts() {+ let mut s = photocraft_engine::Session::new();+ s.execute("file.new", json!({"width": 40, "height": 20})).unwrap();+ s.execute("type.create", json!({"text": "Hi", "x": 2, "y": 12, "size": 10, "font": "NoSuchFamily4559"})).unwrap();+ let doc = (*s.active().unwrap().doc).clone();+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ // What the open job leaves behind: the decoded document in the session, then its result.+ let (index, _) = app.session.open_document(doc, None);+ super::finish_open(&mut app, "poster.psd", None, &json!({"document": index, "warnings": []}), None).unwrap();+ let want = vec!["font 'NoSuchFamily4559' is not installed; text may render using a fallback face".to_string()];+ assert!(app.ui.notices.last().is_some_and(|n| n.title.contains("poster.psd") && n.lines == want), "{:?}", app.ui.notices);+}diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex f87a7ec1c..b600437c1 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1190,6 +1190,7 @@ impl PhotocraftApp {st.source_read_only = photocraft_io::affinity::is_affinity(bytes);}self.sync_views();+ let warnings = file_open::with_missing_fonts(self, warnings);self.ui.status = crate::i18n::fmt(tl!("Opened {name}"), &[("name", name)]);self.ui.status_error = false;if camera_raw_ui::wants_open_dialog(self, &warnings) {