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
4 changes: 2 additions & 2 deletions crates/engine/command-surface.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ layer.transparency layer? value
layer.visibility layer? visible?
layer.zoomTo layer?
layout.activate name layout
layout.addElement layout? kind frame? of? text? size? color? bold? italic? align? letterSpacing? rotation? wrap? font? fallback? title? allClasses? hideOther? showNoData? exclude? columns? textSize? orientation? ticks? tickSide? reverse? fill? width? radius? dash? dir? head? angle? length? image? path? layer? field? label? group? statistic? order? bars? other? stack? values? decimals? barColor? classes? labelAlign? mirror? max? hideLabels? rows? step? type? crs? short? mgrs? labels? style? tickLength? format? hemisphere? edges? labelOrientation? labelSize? off? outline? units? divisions? x? y? w? h? background?
layout.addElement layout? kind frame? of? text? size? color? bold? italic? align? letterSpacing? rotation? wrap? font? fallback? title? allClasses? hideOther? showNoData? exclude? columns? textSize? orientation? ticks? tickSide? reverse? fill? width? radius? dash? dir? head? angle? length? image? path? layer? field? label? group? statistic? order? bars? other? stack? values? decimals? barColor? classes? labelAlign? mirror? max? hideLabels? barOutline? rows? step? type? crs? short? mgrs? labels? style? tickLength? format? hemisphere? edges? labelOrientation? labelSize? off? outline? units? divisions? x? y? w? h? background?
layout.addFrame layout? shape? points? map? x? y? w? h?
layout.arrange layout? element to
layout.arrangement favourite? save? arrangement? remove?
Expand All @@ -303,7 +303,7 @@ layout.save layout? path
layout.series layout? layer nameField? frame? margin? off?
layout.seriesPage layout? page
layout.seriesRefresh layout?
layout.update layout? element x? y? w? h? name? visible? locked? text? size? color? bold? italic? align? letterSpacing? rotation? wrap? font? fallback? title? allClasses? hideOther? showNoData? exclude? labels? columns? textSize? orientation? ticks? tickSide? reverse? style? fill? outline? radius? width? dash? dir? head? angle? length? units? divisions? map? scale? center? bearing? pitch? grid? extentIndicator? border? layer? field? label? group? statistic? order? bars? other? stack? values? decimals? barColor? classes? labelAlign? mirror? max? hideLabels? rows? background?
layout.update layout? element x? y? w? h? name? visible? locked? text? size? color? bold? italic? align? letterSpacing? rotation? wrap? font? fallback? title? allClasses? hideOther? showNoData? exclude? labels? columns? textSize? orientation? ticks? tickSide? reverse? style? fill? outline? radius? width? dash? dir? head? angle? length? units? divisions? map? scale? center? bearing? pitch? grid? extentIndicator? border? layer? field? label? group? statistic? order? bars? other? stack? values? decimals? barColor? classes? labelAlign? mirror? max? barOutline? hideLabels? rows? background?
locator.find locator? text limit? zoom?
map.activate name
map.addTiles url kind? name? bounds? attribution? minZoom? maxZoom? tileSize? singleImage? tms? subdomains? layers? styleLayers? opacity? blend? overlay? top? replace? id? view? start? end? unit? interval? dates? legend? connection?
Expand Down
67 changes: 51 additions & 16 deletions crates/engine/src/cmd/layout.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/engine/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub const CAPABILITIES: &[&str] = &[
"layout-chart-classes",
"layout-chart-stack",
"layout-chart-facing",
"layout-chart-outline",
"field-generation",
"field-package",
FIELD_CONFLICTS,
Expand Down
85 changes: 85 additions & 0 deletions crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ const COMMAND_CAPABILITIES: &[(&str, CommandCheck)] = &[
("layout-chart-classes", command_uses_chart_classes),
("layout-chart-stack", command_uses_chart_stack),
("layout-chart-facing", command_uses_chart_facing),
("layout-chart-outline", command_uses_chart_outline),
("blank-paper", command_uses_blank_paper),
("layout-frame-border", command_uses_frame_border),
("layout-text-size", command_uses_text_size),
Expand Down Expand Up @@ -437,6 +438,32 @@ fn command_uses_chart_facing(id: &str, params: &Value) -> bool {
}
}

fn charts_outlined(e: &rustgis_map::layout::Element) -> bool {
e.kind.chart_uses_outline()
}

/// A layout command that draws a line round a chart's bars (#2487 part 4b): an object without a
/// width (the outline's, or the 0.5 points it is first given) or with one above 0. Null, or a
/// width of 0, draws none, so it needs nothing.
fn command_uses_chart_outline(id: &str, params: &Value) -> bool {
let outlined = || {
// The width read as the command reads it, a number or a number in text.
params.get("barOutline").filter(|o| o.is_object()).is_some_and(|o| match o.get("width") {
None | Some(Value::Null) => true,
Some(_) => crate::cmd::f(o, "width").is_some_and(|w| w > 0.0),
})
};
match id {
"layout.addElement" => {
params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && outlined()
}
"layout.update" => outlined(),
"layout.import" => imported_layout_uses(params, &["\"bar_outline\""], charts_outlined),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_outline)),
_ => false,
}
}

/// Whether a `layout.import` carries its layout as `"text"` with an element `uses`: an older
/// reader reads it without the keys it doesn't know. One read from a `"path"` is a layout file,
/// whose own header requires what it uses (#2277). Every save asks, so a text with none of the
Expand Down Expand Up @@ -743,6 +770,7 @@ fn layout_capabilities(layouts: &[rustgis_map::layout::Layout]) -> Vec<&'static
(elements().any(charts_classes), "layout-chart-classes"),
(elements().any(charts_stack), "layout-chart-stack"),
(elements().any(charts_facing), "layout-chart-facing"),
(elements().any(charts_outlined), "layout-chart-outline"),
(elements().any(|e| e.kind.has_custom_border()), "layout-frame-border"),
(elements().any(|e| e.kind.has_custom_text_size()), "layout-text-size"),
]
Expand Down Expand Up @@ -1486,6 +1514,63 @@ mod tests {
}
}

/// A line round a chart's bars (#2487 part 4b) requires `layout-chart-outline`, in a saved
/// layout and in the commands that give it one; none, null or a width of 0 need nothing.
#[test]
fn chart_outline_capability_follows_saved_layouts_and_commands() {
use rustgis_map::layout::ElementKind;
let fixture = Project::from_json(include_str!("../../../schema/fixtures/layout_chart_outline.rgis")).unwrap();
let saved = serde_json::to_value(&fixture).unwrap();
assert_eq!(saved["requires"], json!(["layout-chart-outline"]));
let mut clean = fixture;
for e in &mut clean.layouts[0].elements {
if let ElementKind::Chart { bar_outline, .. } = &mut e.kind {
*bar_outline = None;
}
}
let base = serde_json::to_value(&clean).unwrap();
assert!(base.get("requires").is_none(), "a chart without an outline needs nothing");
assert!(!base["layouts"].to_string().contains("\"bar_outline\""), "nor writes it");
// One of no width, or below 0 (a hand-edited file), is let go on read: it needs nothing
// and isn't written back.
for width in [0.0, -1.0] {
let mut zero = base.clone();
zero["layouts"][0]["elements"][1]["kind"]["Chart"]["bar_outline"] = json!({"color": [0, 0, 0, 255], "width": width});
let saved_zero = serde_json::to_value(Project::from_json(&zero.to_string()).unwrap()).unwrap();
assert!(saved_zero.get("requires").is_none(), "{width}");
assert!(!saved_zero["layouts"].to_string().contains("\"bar_outline\""), "{width}");
}
let mut document = base.clone();
document["tasks"] = json!([{"name": "Task", "steps": [{"title": "Chart", "command": "layout.import", "params": {"text": saved["layouts"][0].to_string()}}]}]);
let imported = serde_json::to_value(Project::from_json(&document.to_string()).unwrap()).unwrap();
assert_eq!(imported["requires"], json!(["layout-chart-outline"]));
for (id, params, required) in [
("layout.addElement", json!({"kind": "chart", "barOutline": {}}), true),
("layout.addElement", json!({"kind": "chart", "barOutline": {"color": "#000000", "width": 1}}), true),
("layout.addElement", json!({"kind": "chart", "barOutline": {"width": 0}}), false),
("layout.addElement", json!({"kind": "chart", "barOutline": null}), false),
("layout.addElement", json!({"kind": "legend", "barOutline": {}}), false),
("layout.update", json!({"element": 2, "barOutline": {"color": "#C2185B"}}), true),
("layout.update", json!({"element": 2, "barOutline": {"width": 0}}), false),
("layout.update", json!({"element": 2, "barOutline": {"width": "2"}}), true),
("layout.update", json!({"element": 2, "barOutline": {"width": "0"}}), false),
("layout.update", json!({"element": 2, "barOutline": null}), false),
("layout.import", json!({"text": base["layouts"][0].to_string()}), false),
] {
let script = format!("{id}= {params}");
for value in [
json!([{"name": "Task", "steps": [{"title": "Chart", "command": id, "params": params}]}]),
json!([{"name": "Script", "steps": [{"title": "Chart", "command": "script.run", "params": {"script": script}}]}]),
] {
let mut document = base.clone();
document["tasks"] = value;
let saved = serde_json::to_value(Project::from_json(&document.to_string()).unwrap()).unwrap();
let expected = if required { json!(["layout-chart-outline"]) } else { Value::Null };
assert_eq!(saved.get("requires").cloned().unwrap_or(Value::Null), expected, "{id} {params}");
}
}
}

#[test]
fn field_identity_is_preserved_and_required_in_a_plain_saved_project() {
let project = Project::from_json(include_str!("../../../schema/fixtures/field_origin.rgis")).unwrap();
Expand Down
95 changes: 95 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43918,6 +43918,101 @@ fn layout_charts_face_each_other() {
}
}

/// A line round a chart's bars (#2487 part 4b): set when the chart is added or updated, each key
/// not given keeping the outline's (or the one it is first given), a width past 5 points held to
/// it and said, kept when its layer changes, undone in one step, taken away by null or a width of
/// 0 (none written then), refused when bad (changing nothing) and for other kinds, and a null
/// refused on a locked chart like any change.
#[test]
fn layout_charts_draw_a_line_round_their_bars() {
use rustgis_data::{Dataset, Field, FieldType, GeometryType, Value};
use rustgis_map::Rgba;
use rustgis_map::layout::{ChartOutline, ElementKind};
let mut s = Session::new(sample::world_project());
for name in ["Places", "Towns"] {
let mut ds =
Dataset::new(name, GeometryType::Point, vec![Field::new("name", FieldType::Text), Field::new("people", FieldType::Double)]);
ds.push(None, vec![Value::Text("P".into()), Value::Float(10.0)]);
s.map_mut().add_dataset(ds);
}
s.run("layout.new", &json!({"name": "Charts"})).unwrap();
let r = s.run("layout.addElement", &json!({"kind": "chart", "layer": "Places", "field": "people", "barOutline": {}})).unwrap();
let chart = r["element"].as_u64().unwrap() as u32;
let outline = |s: &Session| match s.project.layouts[0].element(chart).map(|e| &e.kind) {
Some(ElementKind::Chart { bar_outline, .. }) => *bar_outline,
_ => panic!("a chart"),
};
let dark = Rgba::rgb(0x22, 0x22, 0x22);
assert_eq!(outline(&s), Some(ChartOutline { color: dark, width: 0.5 }), "first given in dark grey, half a point");
// The keys not given keep the outline's.
s.run("layout.update", &json!({"element": chart, "barOutline": {"color": "#C2185B"}})).unwrap();
let pink = Rgba::rgb(0xC2, 0x18, 0x5B);
assert_eq!(outline(&s), Some(ChartOutline { color: pink, width: 0.5 }));
let mark = s.notice_mark();
s.run("layout.update", &json!({"element": chart, "barOutline": {"width": 2}})).unwrap();
assert_eq!(outline(&s), Some(ChartOutline { color: pink, width: 2.0 }));
let said = s.notices_since(mark).iter().map(|n| n.text.clone()).collect::<Vec<_>>();
assert!(!said.iter().any(|t| t.contains("held to")), "a width within the limits is not held: {said:?}");
// Past 5 points it is held to them, and said.
let mark = s.notice_mark();
s.run("layout.update", &json!({"element": chart, "barOutline": {"width": 9}})).unwrap();
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
assert_eq!(outline(&s).map(|o| o.width), Some(5.0));
let said = s.notices_since(mark).iter().map(|n| n.text.clone()).collect::<Vec<_>>();
assert!(said.iter().any(|t| t.contains("barOutline width 9 held to 5")), "{said:?}");
// Given as text, as forms send numbers, the same.
let mark = s.notice_mark();
s.run("layout.update", &json!({"element": chart, "barOutline": {"width": "8"}})).unwrap();
assert_eq!(outline(&s).map(|o| o.width), Some(5.0));
let said = s.notices_since(mark).iter().map(|n| n.text.clone()).collect::<Vec<_>>();
assert!(said.iter().any(|t| t.contains("barOutline width 8 held to 5")), "{said:?}");
// On a new layer the chart keeps it.
s.run("layout.update", &json!({"element": chart, "layer": "Towns"})).unwrap();
assert_eq!(outline(&s), Some(ChartOutline { color: pink, width: 5.0 }));
// Null takes it away, in one step undone, and a width of 0 too; none written then.
s.run("layout.update", &json!({"element": chart, "barOutline": null})).unwrap();
assert_eq!(outline(&s), None);
let saved = serde_json::to_string(&s.project.layouts[0].element(chart).unwrap().kind).unwrap();
assert!(!saved.contains("\"bar_outline\""), "{saved}");
s.run("edit.undo", &serde_json::Value::Null).unwrap();
assert_eq!(outline(&s), Some(ChartOutline { color: pink, width: 5.0 }));
s.run("layout.update", &json!({"element": chart, "barOutline": {"width": 0}})).unwrap();
assert_eq!(outline(&s), None);
s.run("layout.update", &json!({"element": chart, "barOutline": {"color": "#000000", "width": 1}})).unwrap();
// A locked chart takes no null either: it is a change.
s.run("layout.update", &json!({"element": chart, "locked": true})).unwrap();
assert!(s.run("layout.update", &json!({"element": chart, "barOutline": null})).is_err());
assert!(outline(&s).is_some());
s.run("layout.update", &json!({"element": chart, "locked": false})).unwrap();
// A bad one is refused, naming it, and nothing changes.
let before = outline(&s);
for (bad, said) in [
(json!(true), "barOutline is an object"),
(json!("#000000"), "barOutline is an object"),
(json!({"color": "dark"}), "color must be #RRGGBB"),
(json!({"width": "thick"}), "width must be a number"),
] {
let p = json!({"element": chart, "x": 99, "barOutline": bad});
let err = s.run("layout.update", &p).unwrap_err();
assert!(err.contains(said), "{bad}: {err}");
let add = json!({"kind": "chart", "layer": "Places", "barOutline": bad});
assert!(s.run("layout.addElement", &add).is_err(), "{bad}");
}
assert_eq!(outline(&s), before);
assert!(s.project.layouts[0].element(chart).is_some_and(|e| e.x != 99.0));
// Only a chart takes it.
let rect =
s.run("layout.addElement", &json!({"kind": "rectangle", "x": 1, "y": 1, "w": 5, "h": 5})).unwrap()["element"].as_u64().unwrap();
assert!(s.run("layout.update", &json!({"element": rect, "barOutline": {}})).unwrap_err().contains("only charts"));
let p = json!({"kind": "table", "layer": "Places", "barOutline": {}});
assert!(s.run("layout.addElement", &p).unwrap_err().contains("only charts"));
// A null asks for none, which another kind has: taken as not given, as every chart key's
// null is and as the help says, and nothing changes.
let before = s.project.layouts[0].element(rect as u32).cloned();
s.run("layout.update", &json!({"element": rect, "barOutline": null})).unwrap();
assert_eq!(s.project.layouts[0].element(rect as u32).cloned(), before);
s.run("layout.addElement", &json!({"kind": "rectangle", "barOutline": null})).unwrap();
}

/// A stack field with more values than the key has colours gathers the rest in its Other, and
/// the chart says how many (#2487 part 3b); one that fits says nothing.
#[test]
Expand Down
3 changes: 2 additions & 1 deletion crates/engine/src/tests/layout_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn saved_file(s: &mut Session, layout: usize, tag: &str) -> (String, Value) {
}

/// A saved layout file requires what its elements use, as the fixture that holds only that layout
/// requires: the ten capabilities, two of them at once, and none. Imported into a project without
/// requires: the eleven capabilities, two of them at once, and none. Imported into a project without
/// the layout, it is the same layout, and saved again it is the same file.
#[test]
fn a_saved_layout_file_requires_what_its_elements_use_and_reads_back_the_same() {
Expand All @@ -100,6 +100,7 @@ fn a_saved_layout_file_requires_what_its_elements_use_and_reads_back_the_same()
("classes", include_str!("../../../../schema/fixtures/layout_chart_classes.rgis")),
("stack", include_str!("../../../../schema/fixtures/layout_chart_stack.rgis")),
("facing", include_str!("../../../../schema/fixtures/layout_chart_facing.rgis")),
("outline", include_str!("../../../../schema/fixtures/layout_chart_outline.rgis")),
("border", include_str!("../../../../schema/fixtures/layout_frame_border.rgis")),
("legend-text", include_str!("../../../../schema/fixtures/layout_legend_text.rgis")),
("scale-grid-text", include_str!("../../../../schema/fixtures/layout_scale_grid_text.rgis")),
Expand Down
Loading
Loading