Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub fn commands() -> Vec<C> {
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), "##,
Expand Down Expand Up @@ -335,7 +335,7 @@ pub fn commands() -> Vec<C> {
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), "##,
Expand Down
22 changes: 19 additions & 3 deletions crates/engine/src/cmd/layout/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] = &[
Expand Down Expand Up @@ -1668,7 +1684,7 @@ mod tests {
let err = s.run("layout.addElement", &json!({"kind": "northArrow", "bold": true})).unwrap_err();
assert_eq!(
err,
"layout.addElement: a north arrow takes no \"bold\" (it reads layout, kind, frame, x, y, w, h, background, style, fill, outline, font, fallback)"
"layout.addElement: a north arrow takes no \"bold\" (it reads layout, kind, frame, x, y, w, h, background, style, fill, outline, font, fallback, halo, haloColor, haloWidth)"
);

let err = s.run("layout.addElement", &json!({"kind": "rectangle", "angle": 30})).unwrap_err();
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/src/cmd/layout/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions crates/engine/src/cmd/layout/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ fn changed_indicators(kind: &ElementKind, v: &Value) -> Result<Vec<ExtentIndicat
/// or when `p` has none. A bad value is an error, leaving nothing changed.
pub(in crate::cmd::layout) fn restyle(kind: &ElementKind, p: &Value) -> Result<Option<ElementKind>, 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")? {
Expand All @@ -49,13 +49,15 @@ pub(in crate::cmd::layout) fn restyle(kind: &ElementKind, p: &Value) -> Result<O
None => *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,
fill: color(p, "fill", *fill)?,
outline: color(p, "outline", *outline)?,
font,
font_name,
halo,
}))
}
ElementKind::ScaleBar {
Expand Down Expand Up @@ -434,7 +436,7 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> 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.
Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
63 changes: 59 additions & 4 deletions crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading