diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index 661d7e4a9f..6449fea89b 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -313,8 +313,8 @@ 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); - // A panel's height down to its locator, and what the sources and reference take from the map - // once under it. + // A panel's or an editorial column's height from the frame's top down to its locator, and what + // a panel's sources and reference take from the map once under it. let mut panel_head = None; // Context around the data (neighbouring borders, the states around in one colour), which the // placement already sets aside, is left out of the legend too, unless "contextInLegend" (#1379). @@ -337,7 +337,7 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu let legend_pad = 2.0; // What the legend of a frame `fw` × `fh` mm shows and the room it asks for, in `column` or over // the map, `map` read for that frame (`reading`: a read still running). The one measure of it: - // the placement's, and the check of a panel's cut. + // the placement's, and the check of a sheet's cut. let legend_content = |map: &mut rustgis_map::Map, center: Coord, scale: f64, (fw, fh): (f64, f64), column: Option, reading: bool| { let mut said = Vec::new(); @@ -912,6 +912,9 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu .push("the column's note leaves the locator and the legend little room under it: shorten the note".into()); } let ft = y + 4.0 * k; + // The column over the locator, from the frame's top: none of it moves + // under a cut frame. + panel_head = Some((cy - ft, 0.0)); (page_w, page_h, (x0, ft, mw, room(ft, fb)?), Some(Column { x: cx, y: cy, w: cw, h: (fb - cy).max(0.0) })) } } @@ -983,9 +986,10 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu .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)); - // The least a panel is cut to, with 1·k to spare (a pass may land 0.5 mm short): its - // texts down to the locator and the 30·k the panel's check asks for, or the 40·k the - // map's frame needs. A shorter one fails the next pass and leaves the sheet uncut. + // The least a sheet is cut to, with 1·k to spare (a pass may land 0.5 mm short): its + // column's texts down to the locator and 30·k for it and the legend (which the panel's + // check asks for), or the 40·k the map's frame needs. A shorter frame fails the next + // pass and leaves the sheet uncut. let least = panel_head.map_or(0.0, |(head, _)| (head + 31.0 * k).max(41.0 * k)); // Only a frame clearly too tall is cut, and one the page can give up with what moves // under the map. @@ -1000,9 +1004,9 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu let moving = if freed > 0.0 { 0.0 } else { moved_under(panel_head) }; (held, freed) = (freed, (freed + lh - n - moving).max(0.0)); } - // A panel's cut is taken only with its legend whole in the column the frame leaves: else - // it is cut less, or, with no pass or no cut left for that, not at all. - _ if freed > 0.0 && style == Style::Panel => { + // A cut is taken only with the legend whole in the column the frame leaves: else the + // sheet is cut less, or, with no pass or no cut left for that, not at all. + _ if freed > 0.0 => { // The height the frame must grow to for its legend: none when the column holds it. let short = laid.3.filter(|_| legend.is_some() || colorbar).and_then(|c| { let inner = inner_frame((lw, lh), (room_side, room_end)); diff --git a/crates/engine/src/tests/layout_panel_cut.rs b/crates/engine/src/tests/layout_panel_cut.rs index d53c0b0230..7be413fbbf 100644 --- a/crates/engine/src/tests/layout_panel_cut.rs +++ b/crates/engine/src/tests/layout_panel_cut.rs @@ -1,5 +1,6 @@ -//! The cut of `layout.publication`'s panel sheet for an area much wider than tall (#3266): cut as -//! far as the panel allows, rather than not at all. +//! The cut of `layout.publication`'s panel sheet, and its editorial page, for an area much wider +//! than tall (#3266, #3331): cut as far as the column allows, with the legend whole, rather than +//! not at all or with rows lost. use super::*; use rustgis_map::layout::{Element, ElementKind, Layout}; @@ -47,16 +48,26 @@ fn frame(l: &Layout) -> f64 { named(l, "Map Frame").h } -/// The height left to the locator and the legend, from the first of them to the panel's foot. +/// The same sheet in the editorial style. +fn editorial(extra: Value) -> Value { + let mut p = json!({"style": "editorial"}); + for (k, v) in extra.as_object().unwrap() { + p[k] = v.clone(); + } + p +} + +/// The height left to the locator and the legend, from the first of them to their column's foot, +/// which is the map frame's. fn column(l: &Layout) -> f64 { - let panel = named(l, "Panel Box"); + let map = named(l, "Map Frame"); let top = l .elements .iter() .filter(|e| e.name.starts_with("Locator") || e.name.starts_with("Legend")) .map(|e| e.y) .fold(f64::INFINITY, f64::min); - panel.y + panel.h - top + map.y + map.h - top } /// Cut well short of the uncut frame, the locator and the legend still with their 30 mm (on A4). @@ -161,6 +172,16 @@ fn a_legend_no_cut_sheet_could_hold_leaves_the_sheet_whole() { assert!(warnings.contains(LEGEND_CUT), "{warnings}"); } +#[test] +fn an_editorial_legend_no_cut_page_could_hold_leaves_the_page_whole() { + let mut s = world(); + s.run("layer.symbology", &json!({"layer": "Countries", "type": "unique", "field": "name"})).unwrap(); + let uncut = frame(&panel(editorial(json!({"extent": TALL}))).unwrap()); + let (l, warnings) = panel_of(&mut s, editorial(json!({}))).unwrap(); + assert!(frame(&l) >= 0.97 * uncut, "{} from {uncut}", frame(&l)); + assert!(warnings.contains(LEGEND_CUT), "{warnings}"); +} + #[test] fn a_tall_area_stays_whole() { let tall = panel(json!({"extent": TALL})).unwrap(); @@ -285,34 +306,36 @@ fn no_cut_sheet_clips_its_legend() { ("no key", plain, json!({"legend": "none", "colorbar": "none"})), ("context listed", plain, json!({"contextInLegend": true})), ]; - let mut cut = 0; + let mut cut = [0; 2]; for profile in crate::cmd::layout::PUBLICATION_PROFILES.iter().filter(|p| p.sheet) { for (name, setup, extra) in &cases { - for south in [-20.0, -3.0] { - let mut p = json!({"profile": profile.id, "extent": area(south)}); - for (k, v) in extra.as_object().unwrap() { - p[k] = v.clone(); - } - let mut s = world(); - setup(&mut s); - let what = format!("{} {name} {south}", profile.id); - let (l, warnings) = panel_of(&mut s, p).unwrap_or_else(|e| panic!("{what}: {e}")); - // An uncut panel starts at the page's margin and padding: 15 mm on A4. - let whole = named(&l, "Panel Box").y <= 15.0 * l.width.min(l.height) / 210.0 + 0.5; - if !whole { - cut += 1; - if *name == "raster colorbar" { - assert!(l.elements.iter().any(|e| e.kind.code() == "colorbar"), "{what}: no colorbar"); + for (i, (style, top)) in [("panel", "Panel Box"), ("editorial", "Column Rule")].into_iter().enumerate() { + for south in [-20.0, -3.0] { + let mut p = json!({"profile": profile.id, "style": style, "extent": area(south)}); + for (k, v) in extra.as_object().unwrap() { + p[k] = v.clone(); } - for lost in [LEGEND_CUT, "leaves the legend no room", "colorbar of"] { - assert!(!warnings.contains(lost), "{what}: {warnings}"); + let mut s = world(); + setup(&mut s); + let what = format!("{} {style} {name} {south}", profile.id); + let (l, warnings) = panel_of(&mut s, p).unwrap_or_else(|e| panic!("{what}: {e}")); + // An uncut sheet's column starts at the page's margin and padding: 15 mm on A4. + let whole = named(&l, top).y <= 15.0 * l.width.min(l.height) / 210.0 + 0.5; + if !whole { + cut[i] += 1; + if *name == "raster colorbar" { + assert!(l.elements.iter().any(|e| e.kind.code() == "colorbar"), "{what}: no colorbar"); + } + for lost in [LEGEND_CUT, "leaves the legend no room", "colorbar of"] { + assert!(!warnings.contains(lost), "{what}: {warnings}"); + } } } } } } let _ = std::fs::remove_file(matrix_raster()); - assert!(cut > 100, "{cut} cut sheets"); + assert!(cut.iter().all(|n| *n > 100), "{cut:?} cut sheets"); } #[test] @@ -326,3 +349,47 @@ fn a_very_wide_area_is_cut_down_to_what_the_panel_needs() { assert!((33.0..=35.0).contains(&room), "{sources} sources: {room} mm under the locator's top"); } } + +#[test] +fn an_editorial_page_of_a_very_wide_area_is_cut_with_its_legend_whole() { + let uncut = frame(&panel(editorial(json!({"extent": TALL}))).unwrap()); + // -16 and -3 are the issue's, without a subtitle as reported and with one. + for south in [-20.0, -16.0, -3.0] { + for subtitle in [json!(null), json!("Vegetation trend, 2001–2025")] { + let extra = editorial(json!({"extent": area(south), "subtitle": subtitle})); + assert_cut_whole(extra, uncut, &format!("south {south}, subtitle {subtitle}")); + } + } +} + +#[test] +fn a_tall_area_keeps_an_editorial_page_whole() { + let tall = panel(editorial(json!({"extent": TALL}))).unwrap(); + assert!((named(&tall, "Column Rule").y - 15.0).abs() < 1e-6); +} + +/// What a column too short under its note is warned with. +const LITTLE_ROOM: &str = "leaves the locator and the legend little room"; + +#[test] +fn an_editorial_page_is_not_cut_below_what_its_column_needs() { + let uncut = frame(&panel(editorial(json!({"extent": TALL}))).unwrap()); + let (l, warnings) = panel_of(&mut world(), editorial(json!({"legend": "none"}))).unwrap(); + assert_cut(&l, uncut, "no legend"); + assert!(!warnings.contains(LITTLE_ROOM), "{warnings}"); + let mut cut = 0; + for n in 1..=12 { + let note = "The states of the northeast, and the trend of their vegetation. ".repeat(n); + // A note an uncut page has no room for is the request's own failure. + let tall = panel_of(&mut world(), editorial(json!({"extent": TALL, "note": note, "legend": "none"}))); + let Ok((tall, said)) = tall else { continue }; + let (l, warnings) = + panel_of(&mut world(), editorial(json!({"note": note, "legend": "none"}))).unwrap_or_else(|e| panic!("{n} notes: {e}")); + if frame(&l) < 0.97 * frame(&tall) { + cut += 1; + assert!(column(&l) >= 30.0, "{n} notes: {}", column(&l)); + } + assert!(!warnings.contains(LITTLE_ROOM) || said.contains(LITTLE_ROOM), "{n} notes: {warnings}"); + } + assert!(cut > 0); +}