diff --git a/crates/assistant/skills/print-layout/SKILL.md b/crates/assistant/skills/print-layout/SKILL.md index 7bc304379b..278196ec6c 100644 --- a/crates/assistant/skills/print-layout/SKILL.md +++ b/crates/assistant/skills/print-layout/SKILL.md @@ -8,7 +8,7 @@ description: Make a printable page layout — map frame, title, legend, north ar 1. Get the map right first: symbology, labels and extent. A layout's map frame shows the map's current view. 2. For a finished sheet or a figure for a paper in one step, use `layout.publication` with a `"profile"` (a page size alone, `"a4"`, `"letter"`, `"legal"`, `"tabloid"`, `"a5"` to `"a0"`, for a sheet turned to the area's shape, or with its orientation, `"a4-portrait"`, `"letter-landscape"`, … to choose; `"figure-1col"`, `"thesis-full"`, … for a figure), the `"extent"` - to fit, and the texts: `"title"`, `"subtitle"`, `"sources"`, `"note"`, a `"locator"` (`"globe"`: the visible polygon layers held in the project, not streamed ones, over the basemap; `"inset"`; `["inset", "globe"]`, both, the globe outlining the inset; or `"none"`; when omitted, `"globe"` on a sheet and `"none"` on a figure), and `"decimal": ","` for a map in a language with a decimal comma. A sheet's `"style"` arranges it: `"classic"` (the default), `"banner"`, `"panel"`, `"editorial"`, `"modular"`, `"poster"` (for a poster-like sheet) or `"edge"` (the map from side to side of the page; it also takes `"paper"`, the page colour as `"#RRGGBB"`, `"kicker"`, a short line over the title, and `"figures"`, up to three `{"value", "label"}` under the map, whose texts may hold layer statistics such as `{count:Layer}`), with an `"accent"` colour as `"#RRGGBB"`. Its + to fit, and the texts: `"title"`, `"subtitle"`, `"sources"`, `"note"`, a `"locator"` (`"globe"`: the visible polygon layers held in the project, not streamed ones, over the basemap; `"inset"`; `["inset", "globe"]`, both, the globe outlining the inset; or `"none"`; when omitted, `"globe"` on a sheet and `"none"` on a figure), and `"decimal": ","` for a map in a language with a decimal comma. A sheet's `"style"` arranges it: `"classic"` (the default), `"banner"`, `"panel"`, `"editorial"`, `"modular"`, `"poster"` (for a poster-like sheet) or `"edge"` (the map from side to side of the page; it also takes `"paper"`, the page colour as `"#RRGGBB"`, `"kicker"`, a short line over the title, and `"figures"`, up to three `{"value", "label"}` under the map, whose texts may hold layer statistics such as `{count:Layer}`; `"panel"` takes `"figures"` too, stacked in its panel), with an `"accent"` colour as `"#RRGGBB"`. Its surrounds (locator, north arrow, legend, scale bar) start from an `"arrangement"` (`"classic"`, `"mirrored"`, `"legendRight"`, `"legendTop"`, or a corner for each; `"best"`, the default, tries the user's favourite, set with `layout.arrangement`, and the others), and one over the data moves to the freest place in the frame, the largest first (the legend, as a rule); read its `"arrangement"`, `"moved"` and `"warnings"`. It places the frame at a round scale, the graticule, legend (a long one in up to three columns: its own choice, though a legend takes up to six), scale bar and north arrow, and wraps the footer. `"grid"`, `"scaleBar"` and `"northArrow"` take `"none"` to leave that part out, or an object of its options (the grid's `"step"` or `"type"`, the bar's `"units"` or `"style"`, the arrow's `"style"`, `"fill"`, `"outline"` and `"show"`). Its result lists every element's id and name (`"elements"`), so skip to step 5 to adjust them. Otherwise, diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index cbcb4abcc0..0d9dd0f628 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -91,7 +91,7 @@ pub fn commands() -> Vec { r#""style"?: "classic" (the default: the title over the map, the footer under it) | "banner" (the title and subtitle in a band of the accent colour across the top) | "panel" (the map at the left, a panel at the right with the title, locator, legend, sources and reference) | "editorial" (the title large in serif, a column at the right with the note, the locator and the legend) | "modular" (cards on a light page: the map, the locator and the legend each in one, the sources and reference in tinted ones) | "poster" (the page in the accent colour, the title in light text, the map in a light card, the footer in darker blocks) | "edge" (the map from one side of the page to the other with no border, a kicker and the title over it, the footer with no boxes under a thin rule) (a sheet's arrangement; figures leave it to the caption), "#, r##""paper"?: "#RRGGBB" (an edge sheet's page colour, as layout.page's "color"; white by default; on a dark one its texts are light), "##, r#""kicker"?: text (an edge sheet's short line in spaced capitals over the title, in the accent colour), "#, - r#""figures"?: up to three {"value": text, "label"?: text} (an edge sheet's headline figures, in columns under the map; a value may hold layer statistics such as {count:Layer}), "#, + r#""figures"?: up to three {"value": text, "label"?: text} (a sheet's headline figures: an edge sheet's in columns under the map, a panel sheet's stacked in its panel; a value may hold layer statistics such as {count:Layer}), "#, r#""accent"?: a colour as #RRGGBB (the style's colour: its band, panel tint, headings and rules, a poster's page), "#, r#""arrangement"?: "best" (the default: the favourite, each arrangement by name and each saved with layout.arrangement laid out, the one moving the least area off its corners kept, then the one leaving the least over the data, the favourite on a tie) | "favourite" (layout.arrangement's) | an arrangement by name (built in or saved) or corner by corner, as layout.arrangement takes (the corners the locator, north arrow, legend and scale bar are at home in; one covering what the map draws there still moves), "#, r#""coverage"?: "*.png" (writes the coverage grid the placement searched, as it settled on it, three pixels a cell: what the map draws dark, the free cells white, both tinted blue in the strips the graticule's labels keep, the surrounds placed over the map outlined in red; the answer gives its path)} → a new layout of the active map at a round scale, its texts wrapped to their boxes; {projection}, {datum} and {grid} in the texts are filled from the map"#, diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index c03d6e0e41..93c4efd885 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -146,11 +146,15 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu if accent.a() != 255 { return Err("accent is an opaque colour as #RRGGBB".into()); } - // Only an edge sheet draws these: given to another, they are refused rather than left out. + // Only some sheets draw these: given to another, they are refused rather than left out. let edge = sheet && style == Style::Edge; - if let Some(k) = ["paper", "kicker", "figures"].into_iter().find(|k| !edge && p.get(*k).is_some_and(|v| !v.is_null())) { + let given = |k: &str| p.get(k).is_some_and(|v| !v.is_null()); + if let Some(k) = ["paper", "kicker"].into_iter().find(|k| !edge && given(k)) { return Err(format!("{k} is for a sheet with \"style\": \"edge\"")); } + if !(edge || sheet && style == Style::Panel) && given("figures") { + return Err("figures is for a sheet with \"style\": \"edge\" or \"panel\"".into()); + } // White is the page's own colour, kept as none, as `layout.page` keeps it. let page_colour = crate::cmd::opaque_color(p, "paper")?.flatten().filter(|c| *c != Rgba::rgb(0xFF, 0xFF, 0xFF)); let kicker = one("kicker")?; @@ -310,6 +314,9 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu // A later pass failing where an earlier one held (its shorter page leaving the texts too // little room) settles on the last that held: only the first pass's failure is the request's. let (mut held, mut settled) = (0.0f64, false); + // The least a panel with headline figures is cut to: a shorter one fails the next pass, and the + // sheet would be left uncut. + let mut panel_needs = 0.0f64; let warned = warnings.len(); let (w, h, (fx, fy, fw, fh), column) = loop { around.clear(); @@ -708,6 +715,29 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu )); y += 4.0 * k; } + // The headline figures, one under another, each value over its label. + for (i, (value, label)) in headlines.iter().enumerate() { + let value = wrap_as(value, tw, title_pt, true); + let vh = value.lines.len() as f64 * pitch(title_pt); + let name = format!("Figure {} Value", i + 1); + around.push((name, (ix, y, tw, vh), pub_text(value, title_pt, ink, true, TextAlign::Left))); + y += vh; + let label = label.as_deref().map(|t| wrap(t, tw, body_pt)).unwrap_or_default(); + if !label.lines.is_empty() { + let lh = label.lines.len() as f64 * pitch(body_pt); + let name = format!("Figure {} Label", i + 1); + around.push((name, (ix, y, tw, lh), pub_text(label, body_pt, soft, false, TextAlign::Left))); + y += lh; + } + y += if i + 1 < headlines.len() { 2.0 * k } else { 3.0 * k }; + } + if !under && !headlines.is_empty() { + // The height down to here, the 30·k the check below asks for the locator and + // the legend with 1·k to spare (a pass may land 0.5 mm short), and what the + // sources and reference take from the map once under it. + let moved = foot - footer(&mut Vec::new(), &plain, true, true); + panel_needs = y - top + ip + 31.0 * k + moved; + } // The sources and the reference, stacked up from the panel's foot. let mut yb = panel_bottom - ip; for (name, head, lines) in sections(tw, head_pt, body_pt).into_iter().rev().filter(|_| !under) { @@ -728,10 +758,10 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu yb -= 3.0 * k; } if yb - y < 30.0 * k { - return Err( - "the panel's texts leave no room for the locator and the legend: shorten the title, the sources or the reference" - .into(), - ); + let figures = if headlines.is_empty() { "" } else { " the figures," }; + return Err(format!( + "the panel's texts leave no room for the locator and the legend: shorten the title,{figures} the sources or the reference" + )); } (page_w, page_h, (x0, top, mw, room(top, panel_bottom)?), Some(Column { x: ix, y, w: tw, h: yb - y })) } @@ -870,7 +900,7 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu target = (sheet && matches!(style, Style::Panel | Style::Editorial)) .then(|| aspect(s, &extent)) .flatten() - .map(|a| (((lw - 2.0 * room_side).max(1.0) * a + 2.0 * room_end) * 1.15).max(40.0 * k)) + .map(|a| (((lw - 2.0 * room_side).max(1.0) * a + 2.0 * room_end) * 1.15).max(40.0 * k).max(panel_needs)) // Only a frame clearly too tall is cut. .filter(|n| *n < 0.97 * lh); } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index c53fa3acae..52299843d6 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -126,6 +126,9 @@ mod layout_removed_map; #[path = "tests/layout_edge.rs"] mod layout_edge; +#[path = "tests/layout_panel_figures.rs"] +mod layout_panel_figures; + #[path = "tests/layout_chart_notices.rs"] mod layout_chart_notices; diff --git a/crates/engine/src/tests/layout_edge.rs b/crates/engine/src/tests/layout_edge.rs index 4cadb55675..f6ff7f3b14 100644 --- a/crates/engine/src/tests/layout_edge.rs +++ b/crates/engine/src/tests/layout_edge.rs @@ -191,7 +191,9 @@ fn paper_and_kicker_are_refused_with_another_style_and_on_a_figure() { let mut s = world(); let figures = json!([{"value": "12 %"}]); for (key, value) in [("paper", json!("#0B1B2B")), ("kicker", json!("Day 22")), ("figures", figures), ("figures", json!([]))] { - for style in ["classic", "banner", "panel", "editorial", "modular", "poster"] { + // A panel sheet takes the figures too (#3263). + let panel = (key != "figures").then_some("panel"); + for style in ["classic", "banner", "editorial", "modular", "poster"].into_iter().chain(panel) { let e = s.run("layout.publication", &json!({"extent": PIAUI, "style": style, key: value})).unwrap_err(); assert!(e.contains(key) && e.contains("\"edge\""), "{style}: {e}"); } diff --git a/crates/engine/src/tests/layout_panel_figures.rs b/crates/engine/src/tests/layout_panel_figures.rs new file mode 100644 index 0000000000..e395d2de13 --- /dev/null +++ b/crates/engine/src/tests/layout_panel_figures.rs @@ -0,0 +1,149 @@ +//! Headline figures on `layout.publication`'s panel style (#3263): stacked in the side panel under +//! the title and subtitle, over the locator and the legend. + +use super::*; +use rustgis_map::layout::{Element, ElementKind, Layout}; + +const PIAUI: [f64; 4] = [-46.8, -11.6, -38.5, -2.2]; + +/// A panel sheet of Piauí with a title, a subtitle and sources, `extra`'s parameters over these. +fn panel(s: &mut Session, extra: Value) -> Result { + let mut p = json!({"profile": "a4-portrait", "style": "panel", "extent": PIAUI, "title": "PIAUÍ", + "subtitle": "Vegetation trend, 2001–2025", "sources": ["Boundaries: IBGE 2022"]}); + for (k, v) in extra.as_object().unwrap() { + p[k] = v.clone(); + } + s.run("layout.publication", &p) +} + +fn last(s: &Session) -> &Layout { + s.project.layouts.last().unwrap() +} + +fn find<'a>(l: &'a Layout, name: &str) -> Option<&'a Element> { + l.elements.iter().find(|e| e.name == name) +} + +fn named<'a>(l: &'a Layout, name: &str) -> &'a Element { + find(l, name).unwrap_or_else(|| panic!("no {name}")) +} + +fn boxes(l: &Layout) -> Vec<(String, f64, f64, f64, f64)> { + l.elements.iter().map(|e| (e.name.clone(), e.x, e.y, e.w, e.h)).collect() +} + +fn three_figures() -> Value { + json!([{"value": "{count:Countries}", "label": "countries"}, {"value": "12 %", "label": "of the area"}, + {"value": "2001–2025"}]) +} + +/// The figures are inside the panel, one under another, and the locator and the legend under the +/// last of them. +fn assert_stacked(l: &Layout, n: usize, what: &str) { + let panel = named(l, "Panel Box"); + let mut bottom = find(l, "Panel Rule").map_or(panel.y, |r| r.y); + for i in 1..=n { + let value = named(l, &format!("Figure {i} Value")); + for e in [Some(value), find(l, &format!("Figure {i} Label"))].into_iter().flatten() { + assert!(e.x >= panel.x - 1e-6 && e.x + e.w <= panel.x + panel.w + 1e-6, "{what}: {} leaves the panel", e.name); + assert!(e.y >= bottom - 1e-6, "{what}: {} overlaps what is over it", e.name); + bottom = e.y + e.h; + } + } + assert!(bottom <= panel.y + panel.h + 1e-6, "{what}: the figures pass the panel's foot"); + assert!(find(l, &format!("Figure {} Value", n + 1)).is_none(), "{what}"); + let (mut locators, mut legends) = (0, 0); + for e in &l.elements { + let (locator, legend) = (e.name.starts_with("Locator"), e.name.starts_with("Legend")); + if locator || legend { + assert!(e.y >= bottom - 1e-6, "{what}: {} over the figures", e.name); + } + (locators, legends) = (locators + usize::from(locator), legends + usize::from(legend)); + } + assert!(locators > 0 && legends > 0, "{what}: {locators} locators, {legends} legends"); +} + +#[test] +fn a_panel_stacks_its_headline_figures_over_the_locator_and_the_legend() { + let mut s = world(); + for n in 1..=3 { + let all = three_figures(); + panel(&mut s, json!({"figures": all.as_array().unwrap()[..n]})).unwrap(); + let l = last(&s); + assert_stacked(l, n, &format!("{n} figures")); + let ElementKind::Text { text, bold, size, wrap, .. } = &named(l, "Figure 1 Value").kind else { panic!() }; + assert_eq!((text.as_str(), *bold, *size, *wrap), ("{count:Countries}", true, 18.0, true)); + } + assert!(find(last(&s), "Figure 3 Label").is_none()); +} + +#[test] +fn a_panel_with_no_title_puts_the_first_figure_at_its_top() { + let mut s = world(); + panel(&mut s, json!({"title": null, "subtitle": null, "figures": [{"value": "12 %"}]})).unwrap(); + let l = last(&s); + assert!(find(l, "Panel Rule").is_none()); + assert!((named(l, "Figure 1 Value").y - named(l, "Panel Box").y - 3.0).abs() < 1e-6); + assert_stacked(l, 1, "no title"); +} + +#[test] +fn a_panels_figures_fit_every_page_size() { + for profile in crate::cmd::layout::PUBLICATION_PROFILES.iter().filter(|p| p.sheet) { + let mut s = world(); + panel(&mut s, json!({"profile": profile.id, "figures": three_figures()})).unwrap_or_else(|e| panic!("{}: {e}", profile.id)); + assert_stacked(last(&s), 3, profile.id); + } +} + +#[test] +fn a_panel_cut_to_a_low_area_moves_its_figures_with_it() { + // Areas a bare panel is cut to, which the figures' room must not leave uncut. + for south in [-20.0, -24.0, -28.0] { + let mut s = world(); + panel(&mut s, json!({"extent": [-60.0, south, -30.0, -2.0], "figures": three_figures()})).unwrap(); + let l = last(&s); + // A4's margin and padding put an uncut panel's top at 15 mm. + assert!(named(l, "Panel Box").y > 16.0, "{south}: {}", named(l, "Panel Box").y); + assert_stacked(l, 3, &format!("cut to {south}")); + } +} + +#[test] +fn a_panel_with_no_figures_is_laid_out_as_before() { + let mut s = world(); + panel(&mut s, json!({})).unwrap(); + let bare = boxes(last(&s)); + for figures in [json!(null), json!([])] { + panel(&mut s, json!({"figures": figures})).unwrap(); + assert_eq!(boxes(last(&s)), bare, "{figures}"); + } +} + +#[test] +fn a_panels_texts_that_leave_no_room_name_the_figures() { + let long = "a figure's value that takes many lines of its column ".repeat(30); + let mut s = world(); + let e = panel(&mut s, json!({"figures": [{"value": long}]})).unwrap_err(); + assert!(e.contains("no room") && e.contains("the figures"), "{e}"); + assert!(s.project.layouts.is_empty()); +} + +#[test] +fn a_panels_figures_are_checked_as_an_edge_sheets() { + let mut s = world(); + let one = json!({"value": "1"}); + for bad in [json!([one, one, one, one]), json!([{"value": ""}]), json!("12 %")] { + let e = panel(&mut s, json!({"figures": bad})).unwrap_err(); + assert!(e.contains("figures"), "{bad}: {e}"); + } + assert!(s.project.layouts.is_empty()); +} + +#[test] +fn a_panel_with_figures_is_one_undo_step() { + let mut s = world(); + panel(&mut s, json!({"figures": three_figures()})).unwrap(); + s.run("edit.undo", &Value::Null).unwrap(); + assert!(s.project.layouts.is_empty()); +} diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 2bebf00ae5..42c404cc2e 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -240,8 +240,10 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu one the texts are light), `"kicker"` (a short line in spaced capitals over the title) and `"figures"` (up to three `{"value", "label"}`, the label optional, in columns of one width under the map, the value large and bold; a value or label may hold layer statistics such as - `{count:Cities}`); other styles and figures refuse the three. Its graticule labels sit at the - page's side edges, so a printer with no bleed may clip them. `"accent"` (`#RRGGBB`) colours the + `{count:Cities}`). A panel sheet takes `"figures"` too, stacked in its panel under the title + and subtitle, over the locator and the legend; it refuses `"paper"` and `"kicker"`, and the + other styles and figures refuse all three. An edge sheet's graticule labels sit at the page's side + edges, so a printer with no bleed may clip them. `"accent"` (`#RRGGBB`) colours the band, the panel's tint, the headings and rules, a poster's page and an edge sheet's kicker (a deep brown, green or blue by default, a brick red on an edge sheet). A figure leaves the style to its caption. In the menu, each sheet opens a list of the seven