From 560190a2a970ec5a5de5e7f5938f541e0d520266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Wed, 2 Dec 2026 23:04:29 -0500 Subject: [PATCH 1/2] feat(layout): a halo behind a north arrow's N (#3645) --- crates/engine/src/cmd/layout.rs | 4 +- crates/engine/src/cmd/layout/elements.rs | 20 +- crates/engine/src/cmd/layout/text.rs | 4 +- crates/engine/src/cmd/layout/update.rs | 8 +- crates/engine/src/project.rs | 1 + crates/engine/src/project_capabilities.rs | 63 ++++- crates/engine/src/tests.rs | 4 + crates/engine/src/tests/layout_north_halo.rs | 221 ++++++++++++++++++ crates/engine/src/tests/layout_scale_halo.rs | 4 +- crates/map/src/layout/elements.rs | 18 +- crates/render/src/layout.rs | 4 +- crates/render/src/layout/north.rs | 17 +- crates/render/src/layout/tests.rs | 54 +++++ crates/ui-egui/src/layoutview/format/north.rs | 3 +- crates/ui-egui/src/layoutview/tests.rs | 37 +++ docs/maintaining.md | 5 + docs/user-guide/layouts.md | 7 +- schema/fixtures/layout_north_arrow_halo.rgis | 1 + schema/project.schema.json | 2 + 19 files changed, 450 insertions(+), 27 deletions(-) create mode 100644 crates/engine/src/tests/layout_north_halo.rs create mode 100644 schema/fixtures/layout_north_arrow_halo.rgis diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 9e4b6afa35..5040598b3e 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -212,7 +212,7 @@ pub fn commands() -> Vec { r##""letterSpacing"?: points, -5 to 50 (text, dynamicText), "##, r##""rotation"?: degrees clockwise about the box's centre, 90 reading top to bottom and -90 bottom to top, kept within -180 to 180 (text, dynamicText; refused for other kinds), "##, r##""wrap"?: bool (text, dynamicText: broken into lines at spaces to fit the box's width when drawn, after its dynamic text is filled in, a word wider than the box broken across lines; default false, lines breaking only at the text's own line breaks; refused for other kinds), "##, - r##""halo"?: bool (scaleBar: a halo behind its numbers and unit, kept as text in PDF and SVG; default false), "haloColor"?: "#RRGGBB"|"#RRGGBBAA"|""|null (scaleBar; ""/null: white or dark, whichever stands out from its "color"), "haloWidth"?: points 0-10|null (scaleBar: how far it reaches beyond the letters; default and null 1; held to its range; refused for other kinds), "##, + r##""halo"?: bool (scaleBar: a halo behind its numbers and unit; northArrow: behind its N; kept as text in PDF and SVG; default false), "haloColor"?: "#RRGGBB"|"#RRGGBBAA"|""|null (scaleBar, northArrow; ""/null: white or dark, whichever stands out from a bar's "color" or an arrow's "outline"), "haloWidth"?: points 0-10|null (scaleBar, northArrow: how far it reaches beyond the letters; default and null 1; held to its range; refused for other kinds), "##, r##""valign"?: "top"|"middle"|"bottom" (text, dynamicText: where its block of lines sits down its box; default top; a block taller than its box is drawn from the top, its last lines left out, which a warning says; refused for other kinds), "##, r##""lineSpacing"?: 0.5 to 3 (text, dynamicText: the distance between its lines, a multiple of the usual; default 1; refused outside that range, and for other kinds), "##, r##""case"?: "none"|"upper"|"lower" (text, dynamicText: drawn in upper or lower case once its dynamic text is filled in, its text kept as typed; default none; refused for other kinds), "##, @@ -335,7 +335,7 @@ pub fn commands() -> Vec { r##""letterSpacing"?, "##, r##""rotation"?: degrees clockwise (text, as in layout.addElement; refused for other elements), "##, r##""wrap"?: bool (text, as in layout.addElement; refused for other elements), "##, - r##""halo"?: bool, "haloColor"?, "haloWidth"? (scaleBar, as in layout.addElement; turned off, its colour and width are kept; refused for other elements), "##, + r##""halo"?: bool, "haloColor"?, "haloWidth"? (scaleBar, northArrow, as in layout.addElement; turned off, its colour and width are kept; refused for other elements), "##, r##""valign"?, "##, r##""lineSpacing"?, "##, r##""case"? (text, as in layout.addElement; null leaves each as it is; refused for other elements), "##, diff --git a/crates/engine/src/cmd/layout/elements.rs b/crates/engine/src/cmd/layout/elements.rs index 72d635119c..8476ab3c94 100644 --- a/crates/engine/src/cmd/layout/elements.rs +++ b/crates/engine/src/cmd/layout/elements.rs @@ -1152,8 +1152,24 @@ pub fn add_element_reads_frame(kind: &str) -> bool { } /// Keys read by `layout.addElement` for a north arrow (#3074). -const ADD_NORTH_ARROW_KEYS: &[&str] = - &["layout", "kind", "frame", "x", "y", "w", "h", "background", "style", "fill", "outline", "font", "fallback"]; +const ADD_NORTH_ARROW_KEYS: &[&str] = &[ + "layout", + "kind", + "frame", + "x", + "y", + "w", + "h", + "background", + "style", + "fill", + "outline", + "font", + "fallback", + "halo", + "haloColor", + "haloWidth", +]; /// Keys read by `layout.addElement` for a scale bar (#3074). const ADD_SCALE_BAR_KEYS: &[&str] = &[ diff --git a/crates/engine/src/cmd/layout/text.rs b/crates/engine/src/cmd/layout/text.rs index b84e736160..4f8fc37cc4 100644 --- a/crates/engine/src/cmd/layout/text.rs +++ b/crates/engine/src/cmd/layout/text.rs @@ -349,11 +349,11 @@ pub(crate) fn text_block_settings(p: &Value, current: (TextVAlign, f64, TextCase Ok((valign, spacing, case)) } -/// The keys a scale bar's halo is given by (#3633). +/// The keys a scale bar's (#3633) or north arrow's (#3645) halo is given by. pub(in crate::cmd::layout) const HALO_KEYS: [&str; 3] = ["halo", "haloColor", "haloWidth"]; /// What halo keys given for an element that takes none are told. -pub(in crate::cmd::layout) const ONLY_HALO: &str = "only scale bars take a halo"; +pub(in crate::cmd::layout) const ONLY_HALO: &str = "only scale bars and north arrows take a halo"; /// An element's halo from [`HALO_KEYS`] in `p` over `current`, its colour and width read as /// `labels.style` reads a label's: ""/null the automatic colour, a width held to its range and diff --git a/crates/engine/src/cmd/layout/update.rs b/crates/engine/src/cmd/layout/update.rs index dfeca224c5..f317fc2721 100644 --- a/crates/engine/src/cmd/layout/update.rs +++ b/crates/engine/src/cmd/layout/update.rs @@ -39,8 +39,8 @@ fn changed_indicators(kind: &ElementKind, v: &Value) -> Result Result, String> { match kind { - ElementKind::NorthArrow { frame, style, fill, outline, font, font_name } => { - if ["style", "fill", "outline", "font", "fallback"].iter().all(|k| p.get(k).is_none()) { + ElementKind::NorthArrow { frame, style, fill, outline, font, font_name, halo } => { + if ["style", "fill", "outline", "font", "fallback"].iter().chain(HALO_KEYS.iter()).all(|k| p.get(k).is_none()) { return Ok(None); } let style = match given_name(p, "style", "classic, compass, triangle or star")? { @@ -49,6 +49,7 @@ pub(in crate::cmd::layout) fn restyle(kind: &ElementKind, p: &Value) -> Result *style, }; let (font, font_name) = text_font(p, (*font, font_name.clone()))?; + let halo = text_halo(p, *halo)?; Ok(Some(ElementKind::NorthArrow { frame: *frame, style, @@ -56,6 +57,7 @@ pub(in crate::cmd::layout) fn restyle(kind: &ElementKind, p: &Value) -> Result CmdResult { _ if gives_text_block(p) => return Err(ONLY_TEXT_BLOCK.into()), _ => None, }; - if !bar && HALO_KEYS.iter().any(|k| gives(p, k)) { + if !bar && !matches!(kind_now, Some(ElementKind::NorthArrow { .. })) && HALO_KEYS.iter().any(|k| gives(p, k)) { return Err(ONLY_HALO.into()); } // "unitLabel": null too, since it means something on a scale bar. diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 47bf4fee7c..1fc7ac8a57 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -117,6 +117,7 @@ pub const CAPABILITIES: &[&str] = &[ "layout-north-arrow-font", "layout-scale-bar-font", "layout-scale-bar-halo", + "layout-north-arrow-halo", "field-generation", "field-package", FIELD_CONFLICTS, diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index ce0c344f21..cba30bbb35 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -419,6 +419,7 @@ const COMMAND_CAPABILITIES: &[(&str, CommandCheck)] = &[ ("layout-north-arrow-font", command_uses_north_arrow_font), ("layout-scale-bar-font", command_uses_scale_bar_font), ("layout-scale-bar-halo", command_uses_scale_bar_halo), + ("layout-north-arrow-halo", command_uses_north_arrow_halo), ("blank-paper", command_uses_blank_paper), ("layout-frame-border", command_uses_frame_border), ("layout-section", command_uses_section), @@ -1625,16 +1626,34 @@ fn scale_bars_with_halo(e: &rustgis_map::layout::Element) -> bool { /// [`command_uses_north_arrow_font`] reads a north arrow's font. fn command_uses_scale_bar_halo(id: &str, params: &Value) -> bool { match id { - "layout.addElement" => { - params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("scaleBar")) - && params.get("halo").and_then(Value::as_bool) == Some(true) - } + "layout.addElement" => adds_with_halo(params, "scaleBar"), "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"halo"], scale_bars_with_halo), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_scale_bar_halo)), _ => false, } } +fn north_arrows_with_halo(e: &rustgis_map::layout::Element) -> bool { + e.kind.north_arrow_uses_halo() +} + +/// A layout command that turns a north arrow's halo on (#3645), read as +/// [`command_uses_north_arrow_font`] reads its font. +fn command_uses_north_arrow_halo(id: &str, params: &Value) -> bool { + match id { + "layout.addElement" => adds_with_halo(params, "northArrow"), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"halo"], north_arrows_with_halo), + "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_north_arrow_halo)), + _ => false, + } +} + +/// Whether a `layout.addElement` of a `kind` element turns its halo on. +fn adds_with_halo(params: &Value, kind: &str) -> bool { + params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case(kind)) + && params.get("halo").and_then(Value::as_bool) == Some(true) +} + /// Whether a `layout.addElement` of a `kind` element gives it a family other than the sans. fn adds_with_font(params: &Value, kind: &str) -> bool { params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case(kind)) @@ -2266,6 +2285,7 @@ fn layout_capabilities(layouts: &[rustgis_map::layout::Layout]) -> Vec<&'static (elements().any(north_arrows_with_font), "layout-north-arrow-font"), (elements().any(scale_bars_with_font), "layout-scale-bar-font"), (elements().any(scale_bars_with_halo), "layout-scale-bar-halo"), + (elements().any(north_arrows_with_halo), "layout-north-arrow-halo"), (elements().any(|e| e.kind.has_custom_border()), "layout-frame-border"), (elements().any(|e| e.kind.has_sections()), "layout-section"), (elements().any(|e| e.kind.is_profile()), "layout-profile"), @@ -2714,6 +2734,9 @@ mod tests { "layout-scale-bar-halo" if wanted => { s.run("layout.addElement", &json!({"kind": "scaleBar", "halo": true})).unwrap(); } + "layout-north-arrow-halo" if wanted => { + s.run("layout.addElement", &json!({"kind": "northArrow", "halo": true})).unwrap(); + } "label-obstacles" if wanted => { let points = rustgis_data::Dataset::new("pts", rustgis_data::GeometryType::Point, Vec::new()); let id = s.map_mut().add_dataset(points); @@ -2736,6 +2759,7 @@ mod tests { "layout-north-arrow-font", "layout-scale-bar-font", "layout-scale-bar-halo", + "layout-north-arrow-halo", ]; let mut listeners = Vec::new(); for capability in checks { @@ -3197,6 +3221,7 @@ mod tests { (&["layout-north-arrow-font"], "layout.addElement", json!({"kind": "northArrow", "font": "serif"})), (&["layout-scale-bar-font"], "layout.addElement", json!({"kind": "scaleBar", "font": "serif"})), (&["layout-scale-bar-halo"], "layout.addElement", json!({"kind": "scaleBar", "halo": true})), + (&["layout-north-arrow-halo"], "layout.addElement", json!({"kind": "northArrow", "halo": true})), ]; let mut covered = std::collections::BTreeSet::new(); for (capability, id, params) in samples { @@ -3502,6 +3527,7 @@ mod tests { "layout-north-arrow-font", "layout-scale-bar-font", "layout-scale-bar-halo", + "layout-north-arrow-halo", "layout-frame-border", "layout-text-size", "layout-scale-bar-details", @@ -4800,6 +4826,35 @@ mod tests { } } + /// A north arrow's halo (#3645) requires `layout-north-arrow-halo` only while it is on. + #[test] + fn north_arrow_halo_capability_follows_saved_layouts() { + use rustgis_map::layout::ElementKind; + let fixture = Project::from_json(include_str!("../../../schema/fixtures/layout_north_arrow_halo.rgis")).unwrap(); + let saved = serde_json::to_value(&fixture).unwrap(); + assert_eq!(saved["requires"], json!(["layout-north-arrow-halo"])); + let mut off = fixture; + for e in &mut off.layouts[0].elements { + if let ElementKind::NorthArrow { halo, .. } = &mut e.kind { + halo.on = false; + } + } + assert!(serde_json::to_value(&off).unwrap().get("requires").is_none()); + let mut headerless = saved; + headerless.as_object_mut().unwrap().remove("requires"); + for (id, params, required) in [ + ("project.openText", json!({"text": headerless.to_string()}), true), + ("project.openText", json!({"text": serde_json::to_value(&off).unwrap().to_string()}), false), + ("layout.import", json!({"text": headerless["layouts"][0].to_string()}), true), + ] { + let mut document = serde_json::to_value(&off).unwrap(); + document["tasks"] = json!([{"name": "Task", "steps": [{"title": "Arrow", "command": id, "params": params}]}]); + let kept = serde_json::to_value(Project::from_json(&document.to_string()).unwrap()).unwrap(); + let has = kept["requires"].as_array().is_some_and(|r| r.contains(&json!("layout-north-arrow-halo"))); + assert_eq!(has, required, "{id} {params}"); + } + } + /// A scale bar's font (#3468) requires `layout-scale-bar-font`, in a saved layout and in /// a saved command. #[test] diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index e65d4f6735..13c6e80dd3 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -244,6 +244,10 @@ mod layout_scale_font; #[path = "tests/layout_scale_halo.rs"] mod layout_scale_halo; +/// A north arrow's halo (#3645). +#[path = "tests/layout_north_halo.rs"] +mod layout_north_halo; + /// A mosaic publication sheet (#3513). #[path = "tests/layout_mosaic.rs"] mod layout_mosaic; diff --git a/crates/engine/src/tests/layout_north_halo.rs b/crates/engine/src/tests/layout_north_halo.rs new file mode 100644 index 0000000000..1a2e7f840a --- /dev/null +++ b/crates/engine/src/tests/layout_north_halo.rs @@ -0,0 +1,221 @@ +//! A halo behind a layout north arrow's `N` (#3645). + +use super::*; + +/// A map layout with a north arrow of its own; the arrow's element id. +fn arrowed(params: Value) -> (Session, u64) { + let mut s = world(); + s.run("layout.new", &json!({"template": "map"})).unwrap(); + let mut p = params; + p["kind"] = json!("northArrow"); + let id = s.run("layout.addElement", &p).unwrap()["element"].as_u64().unwrap(); + (s, id) +} + +/// The stored north arrow `id`'s halo, as the project saves it. +fn halo(s: &Session, id: u64) -> Value { + let e = s.project.layouts[0].elements.iter().find(|e| u64::from(e.id) == id).unwrap(); + serde_json::to_value(&e.kind).unwrap()["NorthArrow"]["halo"].clone() +} + +fn colour(r: u8, g: u8, b: u8) -> Value { + serde_json::to_value(rustgis_map::Rgba::rgb(r, g, b)).unwrap() +} + +fn update(s: &mut Session, id: u64, mut params: Value) -> Result { + params["element"] = json!(id); + s.run("layout.update", ¶ms) +} + +/// Whether the project, as saved, requires the north arrow's halo capability. +fn requires_halo(s: &Session) -> bool { + let saved = serde_json::to_value(&s.project).unwrap(); + saved["requires"].as_array().is_some_and(|r| r.contains(&json!("layout-north-arrow-halo"))) +} + +/// Runs `command` and expects it refused naming `said`, with nothing changed and no undo step. +fn refused(s: &mut Session, command: &str, params: Value, said: &str) { + let before = serde_json::to_value(&s.project).unwrap(); + let undo = s.undo_label(); + let err = s.run(command, ¶ms).expect_err(¶ms.to_string()); + assert!(err.contains(said), "{params}: {err}"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before, "{params}"); + assert_eq!(s.undo_label(), undo, "{params}"); +} + +fn svg(s: &mut Session) -> String { + rustgis_render::svg::layout_svg(&s.project.layouts[0], &mut s.project.maps, 72.0, &Default::default()).unwrap().0 +} + +#[test] +fn a_north_arrow_without_a_halo_saves_none() { + for params in [json!({}), json!({"halo": false}), json!({"haloColor": null}), json!({"haloColor": ""}), json!({"haloWidth": null})] { + let (s, id) = arrowed(params.clone()); + assert_eq!(halo(&s, id), Value::Null, "{params}"); + assert!(!requires_halo(&s), "{params}"); + } +} + +#[test] +fn a_halo_turned_on_is_automatic_and_a_point_wide() { + let (mut s, id) = arrowed(json!({})); + let plain = svg(&mut s); + update(&mut s, id, json!({"halo": true})).unwrap(); + assert_eq!(halo(&s, id), json!({"on": true, "width": 1.0})); + assert!(requires_halo(&s)); + assert_ne!(svg(&mut s), plain, "the halo is drawn"); + update(&mut s, id, json!({"haloColor": "#ff00ff"})).unwrap(); + let drawn = svg(&mut s); + // (The graticule's labels have white halos of their own.) + let halos = |svg: &str| { + let copy = r#"