Merge #18419 into the merge queue batch

Type: name the missing-font History states "Replace Fonts" and "Replace All Missing Fonts" (#18324)
This commit is contained in:
David Mann committed 2026-12-01 01:20:45 +01:00
commit a13b937741
2 files changed
+76 -1

No files matched your search

+11 -1
View File
@@ -925,8 +925,14 @@ fn replace_fonts(s: &mut Session, map: &serde_json::Map<String, Value>, 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;
@@ -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<String>) {
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<String>, 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}");
}
}