diff --git a/crates/engine-core/src/cmd/type_extra_cmds.rs b/crates/engine-core/src/cmd/type_extra_cmds.rs index c3ce98effe..b62936078a 100644 --- a/crates/engine-core/src/cmd/type_extra_cmds.rs +++ b/crates/engine-core/src/cmd/type_extra_cmds.rs @@ -925,8 +925,14 @@ fn replace_fonts(s: &mut Session, map: &serde_json::Map, all: boo if missing.is_empty() { return Ok(json!({"missing": [], "replaced": 0})); } + // A map that names none of the missing families replaces nothing, and that adds no History state (#18324). + if !missing.iter().any(|f| pick(f).is_some()) { + return Ok(json!({"missing": missing, "replaced": 0})); + } + // The state is named after what ran: the fonts of a map, or all of them (#18324). + let name = if all { "Replace All Missing Fonts" } else { "Replace Fonts" }; let mut n = 0; - s.edit("Replace Missing Fonts", |doc, _| { + s.edit(name, |doc, _| { let (dpi, format) = (doc.resolution_dpi, doc.pixel_format()); for id in &ids { if let Some(Layer { content: LayerContent::Text(t), .. }) = doc.layer_mut(*id) { @@ -1141,3 +1147,7 @@ mod tests; #[cfg(test)] #[path = "type_anti_alias_history_tests.rs"] mod anti_alias_history_tests; + +#[cfg(test)] +#[path = "type_missing_fonts_history_tests.rs"] +mod missing_fonts_history_tests; diff --git a/crates/engine-core/src/cmd/type_missing_fonts_history_tests.rs b/crates/engine-core/src/cmd/type_missing_fonts_history_tests.rs new file mode 100644 index 0000000000..9212a2d7eb --- /dev/null +++ b/crates/engine-core/src/cmd/type_missing_fonts_history_tests.rs @@ -0,0 +1,65 @@ +//! #18324: replacing missing fonts one by one adds a History state named "Replace Fonts", and replacing all of them +//! adds one named "Replace All Missing Fonts", as the reference application names them, instead of "Replace Missing +//! Fonts" for both. A run that replaces nothing adds no state. + +use serde_json::{Value, json}; + +use crate::Session; + +const MISSING: &str = "No Such Font Family"; + +/// A document with one type layer set in `font`. +fn session(font: &str) -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 300, "height": 120})).unwrap(); + s.execute("type.create", json!({"x": 20, "y": 70, "text": "Ab", "size": 36})).unwrap(); + s.execute("type.setStyle", json!({"font": font})).unwrap(); + s +} + +/// How many History states there are, and the name of the newest. +fn history(s: &Session) -> (usize, Option) { + let history = &s.active().unwrap().history; + (history.past_len(), history.undo_label().map(String::from)) +} + +/// Runs `cmd` on a document whose type layer uses a missing font, and returns the states it added with the name of +/// the newest and the command's `replaced` count. +fn run(cmd: &str, params: Value) -> (usize, Option, Value) { + let mut s = session(MISSING); + let states = history(&s).0; + let replaced = s.execute(cmd, params).unwrap()["replaced"].clone(); + let (now, name) = history(&s); + (now - states, name, replaced) +} + +#[test] +fn replacing_a_font_from_the_map_adds_one_state_named_replace_fonts() { + let (added, name, replaced) = run("type.resolveMissingFonts", json!({"map": {MISSING: "JetBrains Mono"}})); + assert_eq!((added, name.as_deref(), replaced), (1, Some("Replace Fonts"), json!(1))); +} + +#[test] +fn replacing_all_adds_one_state_named_replace_all_missing_fonts() { + let (added, name, replaced) = run("type.replaceAllMissingFonts", json!({})); + assert_eq!((added, name.as_deref(), replaced), (1, Some("Replace All Missing Fonts"), json!(1))); +} + +#[test] +fn a_run_that_replaces_nothing_adds_no_state() { + // No font is missing. + for (cmd, params) in [("type.replaceAllMissingFonts", json!({})), ("type.resolveMissingFonts", json!({"map": {MISSING: "JetBrains Mono"}}))] { + let mut s = session("JetBrains Mono"); + let before = history(&s); + assert_eq!(s.execute(cmd, params).unwrap()["replaced"], 0, "{cmd}"); + assert_eq!(history(&s), before, "{cmd}"); + } + // A font is missing, but the map does not replace it. + for map in [json!({}), json!({"Some Other Family": "JetBrains Mono"})] { + let mut s = session(MISSING); + let before = history(&s); + let r = s.execute("type.resolveMissingFonts", json!({"map": map})).unwrap(); + assert_eq!((&r["missing"], &r["replaced"]), (&json!([MISSING]), &json!(0)), "{map}"); + assert_eq!(history(&s), before, "{map}"); + } +}