Skip to content
Merged
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
181 changes: 124 additions & 57 deletions crates/engine/src/cmd/layout.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/engine/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub const CAPABILITIES: &[&str] = &[
"blank-paper",
"layout-frame-border",
NATIVE_COORDINATES,
"layout-legend-text",
"layout-text-size",
"label-guide",
PROJECT_CRS,
];
Expand Down
130 changes: 90 additions & 40 deletions crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ impl Project {
{
required.push("layout-frame-border");
}
if !required.contains(&"layout-legend-text")
&& self.layouts.iter().any(|l| l.elements.iter().any(|e| e.kind.has_custom_legend_text()))
if !required.contains(&"layout-text-size") && self.layouts.iter().any(|l| l.elements.iter().any(|e| e.kind.has_custom_text_size()))
{
required.push("layout-legend-text");
required.push("layout-text-size");
}
// A coordinate system whose PROJ definition, which an older reader reads, doesn't place
// points as its WKT does (angles in grads, a datum transformation the definition can't
Expand Down Expand Up @@ -149,7 +148,7 @@ const COMMAND_CAPABILITIES: &[(&str, CommandCheck)] = &[
("layout-chart-stack", command_uses_chart_stack),
("blank-paper", command_uses_blank_paper),
("layout-frame-border", command_uses_frame_border),
("layout-legend-text", command_uses_legend_text),
("layout-text-size", command_uses_text_size),
];

/// A saved `map.graticule` that switches to the MGRS grid, its type read as the command reads it:
Expand Down Expand Up @@ -188,20 +187,29 @@ fn command_colors_graticule(id: &str, params: &Value) -> bool {
}
}

/// A layout command that gives a legend a text size other than the default (#2581): an older
/// reader warns that it ignores `textSize` and draws the legend at 9 points. `null` or 9 only give
/// the default, which an older reader's legends always have. A saved `layout.update` isn't told
/// which element it changes, so one this release refuses (on another kind) requires it too, as for
/// the other layout keys.
fn command_uses_legend_text(id: &str, params: &Value) -> bool {
let sized = || params.get("textSize").is_some_and(|v| !v.is_null() && v.as_f64() != Some(rustgis_map::layout::LEGEND_TEXT_PT));
/// A layout command that sizes a legend's text, a scale bar's labels or a grid's labels other
/// than their default (#2581): an older reader warns that it ignores `textSize` or `labelSize`
/// and draws them at the default. `null` or the element's default (a legend's 9 points, a scale
/// bar's or a grid's 8) need nothing. A saved `layout.update` isn't told which element it
/// changes, so any `textSize` but `null` requires it there, as for the other layout keys; a
/// grid's size is one of its `"grid"` options there.
fn command_uses_text_size(id: &str, params: &Value) -> bool {
use rustgis_map::layout::{GRID_LABEL_PT, LEGEND_TEXT_PT, SCALE_BAR_TEXT_PT};
let sized = |p: &Value, key: &str, default: f64| p.get(key).is_some_and(|v| !v.is_null() && v.as_f64() != Some(default));
let kind = |k: &str| params.get("kind").and_then(Value::as_str).is_some_and(|v| v.trim().eq_ignore_ascii_case(k));
match id {
"layout.addElement" => {
params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("legend")) && sized()
(kind("legend") && sized(params, "textSize", LEGEND_TEXT_PT))
|| (kind("scaleBar") && sized(params, "textSize", SCALE_BAR_TEXT_PT))
|| (kind("grid") && sized(params, "labelSize", GRID_LABEL_PT))
}
"layout.update" => sized(),
"layout.import" => imported_layout_uses(params, "\"text_size\"", |e| e.kind.has_custom_legend_text()),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_text)),
"layout.update" => {
params.get("textSize").is_some_and(|v| !v.is_null()) || params.get("grid").is_some_and(|g| sized(g, "labelSize", GRID_LABEL_PT))
}
"layout.import" => {
["\"text_size\"", "\"label_size\""].iter().any(|key| imported_layout_uses(params, key, |e| e.kind.has_custom_text_size()))
}
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_text_size)),
_ => false,
}
}
Expand Down Expand Up @@ -1529,35 +1537,70 @@ mod tests {
assert!(Project::from_json(&unknown.to_string()).unwrap_err().contains("future-border"));
}

/// A legend's text size other than the default (#2581) is required where it is saved, so an
/// older reader refuses the project instead of drawing the legend at 9 points and saving it so.
/// The default size is not written and needs nothing.
/// A legend's, scale bar's or grid's text size other than its default (#2581) is required
/// where it is saved, so an older reader refuses the project instead of drawing it at the
/// default and saving it so. Default sizes are not written and need nothing.
#[test]
fn legend_text_capability_follows_saved_layouts_and_commands() {
let fixture = Project::from_json(include_str!("../../../schema/fixtures/layout_legend_text.rgis")).unwrap();
let saved = serde_json::to_value(&fixture).unwrap();
assert_eq!(saved["requires"], json!(["layout-legend-text"]));
let elements = &saved["layouts"][0]["elements"];
fn text_size_capability_follows_saved_layouts_and_commands() {
use rustgis_map::layout::{ElementKind, GRID_LABEL_PT, LEGEND_TEXT_PT, SCALE_BAR_TEXT_PT};
let legends = Project::from_json(include_str!("../../../schema/fixtures/layout_legend_text.rgis")).unwrap();
let bars = Project::from_json(include_str!("../../../schema/fixtures/layout_scale_grid_text.rgis")).unwrap();
for fixture in [&legends, &bars] {
let saved = serde_json::to_value(fixture).unwrap();
assert_eq!(saved["requires"], json!(["layout-text-size"]));
assert_eq!(Project::from_json(&saved.to_string()).unwrap().layouts, fixture.layouts);
}
let elements = &serde_json::to_value(&legends).unwrap()["layouts"][0]["elements"];
assert!(elements[1]["kind"]["Legend"].get("text_size").is_none());
assert_eq!(elements[2]["kind"]["Legend"]["text_size"], json!(18.0));
let reopened = Project::from_json(&saved.to_string()).unwrap();
assert_eq!(reopened.layouts, fixture.layouts);
let mut clean = fixture;
let elements = &serde_json::to_value(&bars).unwrap()["layouts"][0]["elements"];
assert_eq!(elements[0]["kind"]["MapFrame"]["grid"]["label_size"], json!(14.0));
assert!(elements[1]["kind"]["ScaleBar"].get("text_size").is_none());
assert_eq!(elements[2]["kind"]["ScaleBar"]["text_size"], json!(16.0));
// Each size alone needs it; all at their defaults, nothing.
let sized = |grid: bool, bar: bool| {
let mut p = bars.clone();
for e in &mut p.layouts[0].elements {
match &mut e.kind {
ElementKind::MapFrame { grid: Some(g), .. } if !grid => g.label_size = GRID_LABEL_PT,
ElementKind::ScaleBar { text_size, .. } if !bar => *text_size = SCALE_BAR_TEXT_PT,
_ => {}
}
}
serde_json::to_value(&p).unwrap()
};
assert_eq!(sized(true, false)["requires"], json!(["layout-text-size"]), "a grid's size alone");
assert_eq!(sized(false, true)["requires"], json!(["layout-text-size"]), "a scale bar's size alone");
let mut clean = legends;
for e in &mut clean.layouts[0].elements {
if let rustgis_map::layout::ElementKind::Legend { text_size, .. } = &mut e.kind {
*text_size = rustgis_map::layout::LEGEND_TEXT_PT;
if let ElementKind::Legend { text_size, .. } = &mut e.kind {
*text_size = LEGEND_TEXT_PT;
}
}
let base = serde_json::to_value(&clean).unwrap();
assert!(serde_json::to_value(&clean).unwrap().get("requires").is_none(), "a legend at its default");
let base = sized(false, false);
assert!(base.get("requires").is_none(), "default sizes need nothing");
// A saved command that sizes a legend's text needs it; null or 9 only give the default.
// A saved command that sizes one needs it; null or the element's default only give the
// default. An update isn't told which element it sizes: any textSize but null needs it.
for (id, params, required) in [
("layout.update", json!({"element": 2, "textSize": 18}), true),
("layout.update", json!({"element": 2, "textSize": 9}), true),
("layout.update", json!({"element": 3, "textSize": 8}), true),
("layout.update", json!({"element": 2, "textSize": null}), false),
("layout.update", json!({"element": 2, "textSize": 9}), false),
("layout.update", json!({"element": 2, "title": "Key"}), false),
("layout.update", json!({"element": 1, "grid": {"labelSize": 12}}), true),
("layout.update", json!({"element": 1, "grid": {"labelSize": 8}}), false),
("layout.update", json!({"element": 1, "grid": {"labelSize": null}}), false),
("layout.update", json!({"element": 1, "grid": false}), false),
("layout.addElement", json!({"kind": "legend", "textSize": 6}), true),
("layout.addElement", json!({"kind": "legend", "textSize": 9}), false),
("layout.addElement", json!({"kind": "legend", "textSize": 8}), true),
("layout.addElement", json!({"kind": "legend"}), false),
("layout.addElement", json!({"kind": "scaleBar", "textSize": 12}), true),
("layout.addElement", json!({"kind": "scaleBar", "textSize": 8}), false),
("layout.addElement", json!({"kind": "scaleBar"}), false),
("layout.addElement", json!({"kind": "grid", "labelSize": 12}), true),
("layout.addElement", json!({"kind": "grid", "labelSize": null}), false),
("layout.addElement", json!({"kind": "text", "text": "textSize", "size": 18}), false),
] {
let script = format!("{id} {params}");
Expand All @@ -1568,23 +1611,30 @@ mod tests {
let mut document = base.clone();
document["tasks"] = value;
let saved = serde_json::to_value(Project::from_json(&document.to_string()).unwrap()).unwrap();
if required {
assert_eq!(saved["requires"], json!(["layout-legend-text"]), "{id} {params}");
} else {
assert!(saved.get("requires").is_none(), "{id} {params}");
}
let expected = if required { json!(["layout-text-size"]) } else { Value::Null };
assert_eq!(saved["requires"], expected, "{id} {params}");
}
}
// An imported layout's text with a sized legend needs it; with the default, none.
for (layout, required) in [(&saved["layouts"][0], true), (&base["layouts"][0], false)] {
// An imported layout's text with a sized element needs it; with the defaults, none.
let legend_layout = serde_json::to_value(
Project::from_json(include_str!("../../../schema/fixtures/layout_legend_text.rgis")).unwrap(),
)
.unwrap()["layouts"][0]
.clone();
for (layout, required) in [
(legend_layout, true),
(sized(true, false)["layouts"][0].clone(), true),
(sized(false, true)["layouts"][0].clone(), true),
(base["layouts"][0].clone(), false),
] {
let params = json!({"text": layout.to_string()});
let mut document = base.clone();
document["tasks"] = json!([{"name": "Task", "steps": [{"title": "Import", "command": "layout.import", "params": params}]}]);
let saved = serde_json::to_value(Project::from_json(&document.to_string()).unwrap()).unwrap();
assert_eq!(saved.get("requires").is_some(), required, "{layout}");
}
let mut unknown = saved;
unknown["requires"] = json!(["layout-legend-text", "future-legend"]);
let mut unknown = serde_json::to_value(&bars).unwrap();
unknown["requires"] = json!(["layout-text-size", "future-legend"]);
assert!(Project::from_json(&unknown.to_string()).unwrap_err().contains("future-legend"));
}

Expand Down
Loading
Loading