diff --git a/crates/engine/src/cmd/format/font.rs b/crates/engine/src/cmd/format/font.rs index db7e4ec6e..b8bc622f8 100644 --- a/crates/engine/src/cmd/format/font.rs +++ b/crates/engine/src/cmd/format/font.rs @@ -46,17 +46,22 @@ pub(super) fn subscript(s: &mut Session, p: &Value) -> Result { } /// A typeface name from `key`: trimmed, and the length `format.fontDialog` allows. +/// +/// Absent, as `opt_usize_param` and `id_param` read an absent key, so a call that leaves a script +/// out keeps that typeface. Present but not a string is refused: `str_param` answers `None` to +/// both, so `{"family": "Lato", "eastAsia": 42}` used to set the Latin typeface, leave the East +/// Asian one alone and report success -- the one thing the caller asked for silently not done +/// (noted in review). fn typeface_param(p: &Value, key: &str) -> Result> { - match str_param(p, key) { - None => Ok(None), - Some(v) => { - let t = v.trim(); - if t.is_empty() || t.chars().count() > 200 { - return Err(bad("format.font", format!("`{key}` must be 1-200 characters"))); - } - Ok(Some(t.to_string())) - } + let v = match p.get(key) { + None | Some(Value::Null) => return Ok(None), + Some(v) => v.as_str().ok_or_else(|| bad("format.font", format!("`{key}` is not a typeface name: {v}")))?, + }; + let t = v.trim(); + if t.is_empty() || t.chars().count() > 200 { + return Err(bad("format.font", format!("`{key}` must be 1-200 characters"))); } + Ok(Some(t.to_string())) } pub(super) fn font(s: &mut Session, p: &Value) -> Result { diff --git a/crates/engine/src/cmd/text.rs b/crates/engine/src/cmd/text.rs index d441e6421..b922bf8d9 100644 --- a/crates/engine/src/cmd/text.rs +++ b/crates/engine/src/cmd/text.rs @@ -254,6 +254,30 @@ mod font_script_tests { for p in [json!({"family": " "}), json!({"family": "Lato", "eastAsia": ""}), json!({"family": "Lato", "complexScript": "x".repeat(201)})] { assert!(s.execute("format.font", &p).is_err(), "{p} should be rejected"); } + + // A script key that is present but not a string is refused, not read as absent. It used + // to set `family` and report success with the script typeface untouched, so the one thing + // the call asked for was the one thing that did not happen (noted in review). + for p in [ + json!({"family": "Lato", "eastAsia": 42}), + json!({"family": "Lato", "complexScript": true}), + json!({"family": "Lato", "eastAsia": ["Noto Sans CJK SC"]}), + json!({"family": "Lato", "complexScript": {"name": "Noto Sans Arabic"}}), + json!({"family": 42}), + ] { + let e = s.execute("format.font", &p).expect_err(&format!("{p} should be rejected")); + assert!(format!("{e}").contains("is not a typeface name"), "{p}: {e}"); + } + // Nothing above changed a run: the first call in this test left `family` unset, and a + // refused call must not have set it on the way to the error. + assert_eq!(typefaces(&mut s), vec![(None, None, None)]); + + // An explicit `null` reads as absent, as it does for an index or a shape id, so the + // script keeps whatever typeface it had. + s.execute("format.font", &json!({"family": "Lato", "eastAsia": "Noto Sans CJK SC"})).unwrap(); + s.execute("format.font", &json!({"family": "Lato", "eastAsia": null})).unwrap(); + assert_eq!(typefaces(&mut s), vec![(Some("Lato".into()), Some("Noto Sans CJK SC".into()), None)]); + // A name with spaces around it is kept, trimmed. s.execute("format.font", &json!({"family": " Lato ", "eastAsia": " Noto Sans CJK SC "})).unwrap(); assert_eq!(typefaces(&mut s), vec![(Some("Lato".into()), Some("Noto Sans CJK SC".into()), None)]);