Repository navigation
fix(layout): refuse a colour the element kind doesn't have (#1455) #1459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -88,10 +88,10 @@ pub fn commands() -> Vec<C> { | |||||||||
| r#"{"layout"?, "shape"?: "rectangle"|"circle"|"ellipse"|"polygon"|"lasso", "points"?: [[u, v], …] (polygon/lasso, 0..1 across the frame), "map"?: name, "x"?, "y"?, "w"?, "h"?: mm}"#, | ||||||||||
| ), | ||||||||||
| C::new("layout.addElement", "Add Element", "Publish › Map Surrounds", add_element).params( | ||||||||||
| r##"{"layout"?, "kind": "northArrow"|"scaleBar"|"legend"|"colorbar"|"text"|"dynamicText"|"picture"|"table"|"chart"|"rectangle" (a neatline framing the page unless any of x, y, w, h is given)|"ellipse"|"circle"|"line"|"arrow"|"extentIndicator"|"grid", "frame"?: element id (default: the first map frame), "of"?: frame id (extentIndicator, which also takes "color" and "width" in points, default red and 2), "text"?, "size"?, "color"?: "#RRGGBB"|"#RRGGBBAA", "bold"?: bool (text), "italic"?: bool, "align"?: "left"|"center"|"right", "letterSpacing"?: points, -5 to 50 (text, dynamicText), "font"?: "sans"|"serif"|an installed font family's name, as labels.fonts lists them (text, dynamicText; a family not installed here is drawn in its fallback, and a serif where none is installed in the sans, with a warning; refused for other kinds), "fallback"?: "sans"|"serif" (a named font's, where it isn't installed; default its own class, else sans), "title"?: legend title (default "Legend"), "allClasses"?: bool (legend: every class, not only those the frame draws), "hideOther"?: bool (legend: leave out <all other values>), "exclude"?: [layer names] (legend: layers it leaves out though the map draws them), "orientation"?: "horizontal"|"vertical" (colorbar), "ticks"?: 2..21 (colorbar: labelled values, ends included), "tickSide"?: "below"|"above"|"right"|"left" (colorbar), "reverse"?: bool (colorbar: high values first), "fill"?: "#RRGGBBAA" (rectangle, ellipse, circle, northArrow; scaleBar: the alternating bar's light divisions, default white), "width"?: outline width in points (rectangle, ellipse, circle; 0 for none) or line width in points (line, grid), "radius"?: mm, 0 to 50 (rectangle: rounded corners), "dir"?: "horizontal"|"vertical"|"falling"|"rising" (line), "head"?: "none"|"end"|"start"|"both" (line; default "end" for an arrow), "angle"?: degrees anticlockwise from pointing right, centred on the page unless x or y is given (line, arrow; it sets the box and the direction, so w, h and dir are not used with it), "length"?: mm (line, arrow), "image"?: base64, "path"?: picture file, "layer"?: (table, chart; colorbar: a raster layer, default the top visible one), "field"?, "label"?, "rows"?, "step"?: degrees, or a measured grid's units (metres) (0 picks one), "type"?: "graticule" (latitudes and longitudes, default) | "measured" (eastings and northings in a projected system, as ArcGIS Pro's measured grid), "crs"?: a measured grid's coordinate system: EPSG:25832, WKT or a PROJ string ("" or none: the map's when projected, else the UTM zone at the frame's centre), "short"?: bool (a measured grid's labels as their kilometres' last two digits, 43 for 543000), "labels"?: bool, "style"?: "lines"|"ticks"|"linesAndTicks" (grid) or "classic"|"compass"|"triangle"|"star" (northArrow) or "alternating"|"stepped"|"line" (scaleBar), "tickLength"?: mm, "format"?: "decimal"|"dms", "hemisphere"?: bool (N/S/E/W rather than a sign), "edges"?: ["top","bottom","left","right"] (labelled edges), "labelOrientation"?: "horizontal"|"vertical", "off"?: bool (grid; options not given keep the grid's), "outline"?: "#RRGGBB" (northArrow), "units"?: "auto"|"kilometers"|"meters"|"miles"|"feet", "divisions"?: 1..10 (scaleBar, whose "color" is its lines, dark divisions and labels, default #222222), "x"?, "y"?, "w"?, "h"?: mm}"##, | ||||||||||
| r##"{"layout"?, "kind": "northArrow"|"scaleBar"|"legend"|"colorbar"|"text"|"dynamicText"|"picture"|"table"|"chart"|"rectangle" (a neatline framing the page unless any of x, y, w, h is given)|"ellipse"|"circle"|"line"|"arrow"|"extentIndicator"|"grid", "frame"?: element id (default: the first map frame), "of"?: frame id (extentIndicator, which also takes "color" and "width" in points, default red and 2), "text"?, "size"?, "color"?: "#RRGGBB"|"#RRGGBBAA" (text, dynamicText, line, arrow, rectangle, ellipse, circle: its outline; extentIndicator; grid; scaleBar; refused for other kinds), "bold"?: bool (text), "italic"?: bool, "align"?: "left"|"center"|"right", "letterSpacing"?: points, -5 to 50 (text, dynamicText), "font"?: "sans"|"serif"|an installed font family's name, as labels.fonts lists them (text, dynamicText; a family not installed here is drawn in its fallback, and a serif where none is installed in the sans, with a warning; refused for other kinds), "fallback"?: "sans"|"serif" (a named font's, where it isn't installed; default its own class, else sans), "title"?: legend title (default "Legend"), "allClasses"?: bool (legend: every class, not only those the frame draws), "hideOther"?: bool (legend: leave out <all other values>), "exclude"?: [layer names] (legend: layers it leaves out though the map draws them), "orientation"?: "horizontal"|"vertical" (colorbar), "ticks"?: 2..21 (colorbar: labelled values, ends included), "tickSide"?: "below"|"above"|"right"|"left" (colorbar), "reverse"?: bool (colorbar: high values first), "fill"?: "#RRGGBBAA" (rectangle, ellipse, circle, northArrow; scaleBar: the alternating bar's light divisions, default white; refused for other kinds), "width"?: outline width in points (rectangle, ellipse, circle; 0 for none) or line width in points (line, grid), "radius"?: mm, 0 to 50 (rectangle: rounded corners), "dir"?: "horizontal"|"vertical"|"falling"|"rising" (line), "head"?: "none"|"end"|"start"|"both" (line; default "end" for an arrow), "angle"?: degrees anticlockwise from pointing right, centred on the page unless x or y is given (line, arrow; it sets the box and the direction, so w, h and dir are not used with it), "length"?: mm (line, arrow), "image"?: base64, "path"?: picture file, "layer"?: (table, chart; colorbar: a raster layer, default the top visible one), "field"?, "label"?, "rows"?, "step"?: degrees, or a measured grid's units (metres) (0 picks one), "type"?: "graticule" (latitudes and longitudes, default) | "measured" (eastings and northings in a projected system, as ArcGIS Pro's measured grid), "crs"?: a measured grid's coordinate system: EPSG:25832, WKT or a PROJ string ("" or none: the map's when projected, else the UTM zone at the frame's centre), "short"?: bool (a measured grid's labels as their kilometres' last two digits, 43 for 543000), "labels"?: bool, "style"?: "lines"|"ticks"|"linesAndTicks" (grid) or "classic"|"compass"|"triangle"|"star" (northArrow) or "alternating"|"stepped"|"line" (scaleBar), "tickLength"?: mm, "format"?: "decimal"|"dms", "hemisphere"?: bool (N/S/E/W rather than a sign), "edges"?: ["top","bottom","left","right"] (labelled edges), "labelOrientation"?: "horizontal"|"vertical", "off"?: bool (grid; options not given keep the grid's), "outline"?: "#RRGGBB" (northArrow, or a rectangle's, ellipse's or circle's outline instead of color; refused for other kinds), "units"?: "auto"|"kilometers"|"meters"|"miles"|"feet", "divisions"?: 1..10 (scaleBar, whose "color" is its lines, dark divisions and labels, default #222222), "x"?, "y"?, "w"?, "h"?: mm}"##, | ||||||||||
| ), | ||||||||||
| C::new("layout.update", "Element Properties", "Publish › Element", update).params( | ||||||||||
| r##"{"layout"?, "element": id, "x"?, "y"?, "w"?, "h"?: mm, "name"?, "visible"?, "locked"?: bool (a locked element takes no other change until unlocked; "locked": false unlocks it and may come with changes), "text"?, "size"?, "color"? (text, line; scaleBar: its lines, dark divisions and labels), "bold"?, "italic"?, "align"?, "letterSpacing"?, "font"?: "sans"|"serif"|an installed font family's name, "fallback"?: "sans"|"serif" (text, as in layout.addElement; refused for other elements), "title"?, "allClasses"?: bool (legend), "hideOther"?: bool (legend), "exclude"?: [layer names] (legend; [] lists every layer again), "orientation"?, "ticks"?, "tickSide"?, "reverse"?: (colorbar), "style"?: (northArrow, scaleBar), "fill"?, "outline"? (northArrow, rectangle, ellipse; for a rectangle or ellipse, color sets it when outline is not given; a scaleBar's fill is the alternating bar's light divisions), "radius"?: mm (rectangle: rounded corners), "width"?: points (rectangle, ellipse, line), "dir"?, "head"?, "angle"?, "length"? (line: turned or sized about its centre, then moved by any x or y; dir does not come with angle or length), "units"?, "divisions"? (scaleBar), "map"?, "scale"?, "center"?: [lon, lat], "bearing"?: degrees clockwise from north, "pitch"?: degrees of tilt (0 to 85), "grid"?: {grid options as in layout.addElement} | false (map frame), "extentIndicator"?: {"color"?, "width"?: points} | false (map frame: its extent indicator's style, or removes it), "layer"?: (colorbar: "" for the top visible raster), "field"?, "rows"?}"##, | ||||||||||
| r##"{"layout"?, "element": id, "x"?, "y"?, "w"?, "h"?: mm, "name"?, "visible"?, "locked"?: bool (a locked element takes no other change until unlocked; "locked": false unlocks it and may come with changes), "text"?, "size"?, "color"? (text, line, rectangle, ellipse: its outline; scaleBar: its lines, dark divisions and labels; refused for other elements), "bold"?, "italic"?, "align"?, "letterSpacing"?, "font"?: "sans"|"serif"|an installed font family's name, "fallback"?: "sans"|"serif" (text, as in layout.addElement; refused for other elements), "title"?, "allClasses"?: bool (legend), "hideOther"?: bool (legend), "exclude"?: [layer names] (legend; [] lists every layer again), "orientation"?, "ticks"?, "tickSide"?, "reverse"?: (colorbar), "style"?: (northArrow, scaleBar), "fill"?, "outline"? (northArrow, rectangle, ellipse, and a scaleBar's fill; refused for other elements; for a rectangle or ellipse, color sets it when outline is not given; a scaleBar's fill is the alternating bar's light divisions), "radius"?: mm (rectangle: rounded corners), "width"?: points (rectangle, ellipse, line), "dir"?, "head"?, "angle"?, "length"? (line: turned or sized about its centre, then moved by any x or y; dir does not come with angle or length), "units"?, "divisions"? (scaleBar), "map"?, "scale"?, "center"?: [lon, lat], "bearing"?: degrees clockwise from north, "pitch"?: degrees of tilt (0 to 85), "grid"?: {grid options as in layout.addElement} | false (map frame), "extentIndicator"?: {"color"?, "width"?: points} | false (map frame: its extent indicator's style, or removes it), "layer"?: (colorbar: "" for the top visible raster), "field"?, "rows"?}"##, | ||||||||||
| ), | ||||||||||
| C::new("layout.guides", "Guides", "Publish › Page Setup", guides).params( | ||||||||||
| r#"{"layout"?, "x"?: [mm] (vertical guides, from the page's left edge), "y"?: [mm] (horizontal guides, from its top edge), "clear"?: bool (remove every guide first)}"#, | ||||||||||
|
|
@@ -2230,6 +2230,20 @@ fn add_element(s: &mut Session, p: &Value) -> CmdResult { | |||||||||
| if kind != "rectangle" && p.get("radius").is_some_and(|v| !v.is_null()) { | ||||||||||
| return Err(ONLY_RECTANGLE_RADIUS.into()); | ||||||||||
| } | ||||||||||
| if !matches!( | ||||||||||
| kind, | ||||||||||
| "text" | "dynamicText" | "rectangle" | "ellipse" | "circle" | "line" | "arrow" | "extentIndicator" | "grid" | "scaleBar" | ||||||||||
| ) && gives(p, "color") | ||||||||||
| { | ||||||||||
| return Err(ONLY_COLOR_ADDED.into()); | ||||||||||
| } | ||||||||||
| let shape = matches!(kind, "northArrow" | "rectangle" | "ellipse" | "circle"); | ||||||||||
| if gives(p, "fill") && !shape && kind != "scaleBar" { | ||||||||||
| return Err(ONLY_FILL.into()); | ||||||||||
| } | ||||||||||
| if gives(p, "outline") && !shape { | ||||||||||
| return Err(ONLY_OUTLINE.into()); | ||||||||||
| } | ||||||||||
| let i = index(s, p)?; | ||||||||||
| let image = if kind == "picture" { Some(super::graphics::picture(p)?) } else { None }; | ||||||||||
| let layer_name = |s: &Session, l: &Layout, frame: u32| -> Result<String, String> { | ||||||||||
|
|
@@ -2336,7 +2350,7 @@ fn add_element(s: &mut Session, p: &Value) -> CmdResult { | |||||||||
| "rectangle" => { | ||||||||||
| let k = ElementKind::Rectangle { | ||||||||||
| fill: color(p, "fill", Rgba::new(0, 0, 0, 0))?, | ||||||||||
| outline: color(p, "color", Rgba::rgb(0x22, 0x22, 0x22))?, | ||||||||||
| outline: color(p, "outline", color(p, "color", Rgba::rgb(0x22, 0x22, 0x22))?)?, | ||||||||||
| width: shape_width(p, 1.0)?.clamp(0.0, 50.0), | ||||||||||
| radius: corner_radius(p, 0.0)?, | ||||||||||
| }; | ||||||||||
|
|
@@ -2350,7 +2364,7 @@ fn add_element(s: &mut Session, p: &Value) -> CmdResult { | |||||||||
| "ellipse" | "circle" => { | ||||||||||
| let k = ElementKind::Ellipse { | ||||||||||
| fill: color(p, "fill", Rgba::new(0, 0, 0, 0))?, | ||||||||||
| outline: color(p, "color", Rgba::rgb(0x22, 0x22, 0x22))?, | ||||||||||
| outline: color(p, "outline", color(p, "color", Rgba::rgb(0x22, 0x22, 0x22))?)?, | ||||||||||
| width: shape_width(p, 1.0)?.clamp(0.0, 50.0), | ||||||||||
| }; | ||||||||||
| // A circle is an ellipse in a square box. | ||||||||||
|
|
@@ -2631,6 +2645,20 @@ const ONLY_TEXT_FONT: &str = "only text elements take a font or a fallback"; | |||||||||
| /// them as applied. | ||||||||||
| const ONLY_RECTANGLE_RADIUS: &str = "only a rectangle rounds its corners"; | ||||||||||
|
|
||||||||||
| /// Colours are refused for the elements that don't draw them, as a family is, so a script doesn't | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quality (nit, for the release quality pass): this doc comment is hard to follow. "added also an extent indicator's or a grid's" reads as a dangling clause. Something like:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reworded in 6ae84d9, along the lines you suggested, with the scale bar added (see below). |
||||||||||
| /// take them as applied: "color" is a text's, a line's, an arrow's, a scale bar's or a shape's | ||||||||||
| /// outline (a rectangle's or an ellipse's), and, when adding, also an extent indicator's or a grid's. | ||||||||||
| const ONLY_COLOR_ADDED: &str = | ||||||||||
| "only text, lines, arrows, rectangles, ellipses, circles, extent indicators, grids and scale bars take a color"; | ||||||||||
| const ONLY_COLOR_UPDATED: &str = "only text, lines, arrows, rectangles, ellipses, circles and scale bars take a color"; | ||||||||||
| const ONLY_FILL: &str = "only north arrows, rectangles, ellipses, circles and scale bars take a fill"; | ||||||||||
| const ONLY_OUTLINE: &str = "only north arrows, rectangles, ellipses and circles take an outline"; | ||||||||||
|
|
||||||||||
| /// Whether `p` gives `k`: `null` is not given. | ||||||||||
| fn gives(p: &Value, k: &str) -> bool { | ||||||||||
| p.get(k).is_some_and(|v| !v.is_null()) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Whether `p` gives a `font` or a `fallback`, which only text elements take. | ||||||||||
| fn gives_font(p: &Value) -> bool { | ||||||||||
| ["font", "fallback"].iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) | ||||||||||
|
|
@@ -2985,6 +3013,28 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { | |||||||||
| ), | ||||||||||
| None => None, | ||||||||||
| }; | ||||||||||
| let kind_now = s.project.layouts.get(li).and_then(|l| l.element(id)).map(|e| &e.kind); | ||||||||||
| if gives(p, "color") | ||||||||||
| && !matches!( | ||||||||||
| kind_now, | ||||||||||
| Some( | ||||||||||
| ElementKind::Text { .. } | ||||||||||
| | ElementKind::Line { .. } | ||||||||||
| | ElementKind::ScaleBar { .. } | ||||||||||
| | ElementKind::Rectangle { .. } | ||||||||||
| | ElementKind::Ellipse { .. } | ||||||||||
| ) | ||||||||||
| ) | ||||||||||
| { | ||||||||||
| return Err(ONLY_COLOR_UPDATED.into()); | ||||||||||
| } | ||||||||||
| let shape = matches!(kind_now, Some(ElementKind::NorthArrow { .. } | ElementKind::Rectangle { .. } | ElementKind::Ellipse { .. })); | ||||||||||
| if gives(p, "fill") && !shape && !matches!(kind_now, Some(ElementKind::ScaleBar { .. })) { | ||||||||||
| return Err(ONLY_FILL.into()); | ||||||||||
| } | ||||||||||
| if gives(p, "outline") && !shape { | ||||||||||
| return Err(ONLY_OUTLINE.into()); | ||||||||||
| } | ||||||||||
| // A colour that isn't text is refused, as `color` does when adding (the default is unused). | ||||||||||
| let col = match p.get("color").filter(|v| !v.is_null()) { | ||||||||||
| Some(_) => Some(color(p, "color", Rgba::rgb(0, 0, 0))?), | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quality (doc accuracy, nit): in the
layout.updateparams string, the"outline"?entry reads(northArrow, rectangle, ellipse, and a scaleBar's fill; ...), which says an outline applies to a scale bar's fill. The"fill"?entry has no description at all. Suggested wording, to be made in the string itself:"fill"? (northArrow, rectangle, ellipse; a scaleBar's light divisions; refused for other elements)and"outline"? (northArrow, rectangle, ellipse; refused for other elements; for a rectangle or ellipse, color sets it when outline is not given). The string is too long to quote as a suggestion block, so this is prose only. Per AGENTS.md this is a quality point and can wait for the release quality pass.