From cd303599f3ac649d01a24c1699326759c47802d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Tue, 1 Dec 2026 10:54:10 -0500 Subject: [PATCH] fix(layout): a mosaic's veil group skips bands that hide a panel number (#3538) --- .../engine/src/cmd/layout/publication/mod.rs | 36 ++++---- .../src/cmd/layout/publication/mosaic.rs | 2 +- crates/engine/src/tests/layout_mosaic.rs | 82 ++++++++++++++----- 3 files changed, 79 insertions(+), 41 deletions(-) diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index b1f5ec0e73..2d0a661334 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -1469,13 +1469,20 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu }; let size_of = |kind: Surround| surrounds.iter().find(|x| x.0 == kind).map(|x| (x.1, x.2)); let (locator_size, arrow_size) = (size_of(Surround::Locator), size_of(Surround::Arrow)); - // The fewest columns from `from` up in which the surrounds group in a band around `clip`, with - // their places; the scale bar's box `scale_bar` is the one at the scale tried. - let grouped = |clip: Rect, from: usize, known: &[(f64, f64); PUBLICATION_LEGEND_COLUMNS], scale_bar: (f64, f64)| { + // The fewest columns from `from` up in which the surrounds group in a band around `clip` that + // hides none of the key map's `numbers`, with their places; the scale bar's box `scale_bar` is + // the one at the scale tried. + type Numbers = [(usize, Rect)]; + let grouped = |clip: Rect, from: usize, known: &[(f64, f64); PUBLICATION_LEGEND_COLUMNS], scale_bar: (f64, f64), numbers: &Numbers| { + let hides = |place: Rect| numbers.iter().any(|n| overlap_area(&[n.1, place]) > 0.0); (from..known.len()).find_map(|i| { bands_around(clip) .iter() - .find_map(|band| veil_panel(*band, whole(known[i]), locator_size, arrow_size, scale_bar, GROUP_GAP)) + .find_map(|band| { + veil_panel(*band, whole(known[i]), locator_size, arrow_size, scale_bar, GROUP_GAP).filter( + |&(legend, locator, arrow, bar)| ![Some(legend), locator, arrow, Some(bar)].into_iter().flatten().any(hides), + ) + }) .map(|places| (i, places)) }) }; @@ -1673,7 +1680,10 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu let Some(n) = need(&legend_sizes, room) else { continue }; // Under a veil the whole group has to fit, not the legend alone, the scale bar's box // as drawn there. - let in_group = |s: &Session| grouped(clip, n, &legend_sizes, scale_box(ground_per_pt(s, there, tried), tried)); + let in_group = |s: &Session| { + let numbers = number_zones(s.map(), there, tried, (fx, fy, fw, fh), &cells, &panel_views); + grouped(clip, n, &legend_sizes, scale_box(ground_per_pt(s, there, tried), tried), &numbers) + }; let Some(n) = (if veil { in_group(s).map(|(i, _)| i) } else { Some(n) }) else { continue }; let off = visual_centre_offset((clip.0 + clip.2 / 2.0, clip.1 + clip.3 / 2.0), (w, h)); fits.push((n, off, room.0 * room.1, there, moved, (dx, dy), veil.then_some(clip), legend_sizes, legend_context)); @@ -1890,19 +1900,9 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu // it no room. let mut arrangement_used = tries.iter().find(|(_, h)| *h == homes).map_or_else(|| arrangement_name(homes).to_string(), |(n, _)| n.clone()); - let mut group = veiled - .zip(sizes) - .and_then(|(clip, known)| grouped(clip, 0, &known, scale_box(ground_per_pt(s, center, scale), scale)).map(|g| (g, known))); - // A group that would hide a panel's number isn't taken, nor the map veiled for it: the corners, - // which keep off the numbers, stay. - if let Some(((_, (legend_place, locator_place, arrow_place, scale_place)), _)) = &group - && [Some(*legend_place), *locator_place, *arrow_place, Some(*scale_place)] - .iter() - .flatten() - .any(|place| numbers.iter().any(|n| overlap_area(&[n.1, *place]) > 0.0)) - { - (group, veiled) = (None, None); - } + let group = veiled.zip(sizes).and_then(|(clip, known)| { + grouped(clip, 0, &known, scale_box(ground_per_pt(s, center, scale), scale), &numbers).map(|g| (g, known)) + }); if let Some(((i, (legend_place, locator_place, arrow_place, scale_place)), known)) = group { legend_cols = i + 1; (cw, ch) = known[i]; diff --git a/crates/engine/src/cmd/layout/publication/mosaic.rs b/crates/engine/src/cmd/layout/publication/mosaic.rs index 2c4c94ce7f..7f3b3a52ee 100644 --- a/crates/engine/src/cmd/layout/publication/mosaic.rs +++ b/crates/engine/src/cmd/layout/publication/mosaic.rs @@ -137,7 +137,7 @@ pub(super) fn add_panels(l: &mut Layout, map: &str, key: u32, cells: &[Cell], vi } /// The side (mm) of the square a number on the key map takes: a mosaic's one digit, drawn bold at -/// about 13 points, with its halo. +/// 10 points, with its halo. const NUMBER_ZONE_MM: f64 = 6.0; /// The square around each panel's number on the key map, in page millimetres, with the panel's diff --git a/crates/engine/src/tests/layout_mosaic.rs b/crates/engine/src/tests/layout_mosaic.rs index 1bb5dd7659..dce69d7ff1 100644 --- a/crates/engine/src/tests/layout_mosaic.rs +++ b/crates/engine/src/tests/layout_mosaic.rs @@ -1,6 +1,7 @@ //! `layout.publication`'s mosaic style (#3513): a key map and detail frames numbered on it. use super::*; +use rustgis_geom::Coord; use rustgis_map::layout::{Element, ElementKind}; const PANELS: [[f64; 4]; 3] = [[-46.0, -11.0, -43.0, -8.0], [-42.0, -8.0, -39.0, -5.0], [-45.0, -5.0, -42.0, -2.0]]; @@ -363,36 +364,53 @@ fn a_panel_the_key_map_cant_number_is_warned_about_on_any_view() { } } -#[test] -fn a_veiled_key_maps_group_covers_no_panels_number() { - let path = std::env::temp_dir().join(format!("rustgis-mosaic-veil-{}.geojson", std::process::id())); +/// Where on the map of `s` the middle of its legend sits, as a panel's `[w, s, e, n]` around it. +fn panel_under_the_legend(s: &Session) -> Value { + let (guide, legend) = (find(s, "Map Frame"), find(s, "Legend Box")); + let ElementKind::MapFrame { center, scale, .. } = &guide.kind else { panic!("no frame") }; + let cam = rustgis_map::layout::frame_camera(s.map(), *center, *scale, 0.0, 0.0); + let pt = rustgis_map::layout::PT_PER_MM; + let (x, y) = (legend.x + legend.w / 2.0 - guide.x, legend.y + legend.h / 2.0 - guide.y); + let at = s.map().to_lonlat(cam.to_map(x * pt, y * pt, guide.w * pt, guide.h * pt)).unwrap(); + json!([at.x - 0.05, at.y - 0.05, at.x + 0.05, at.y + 0.05]) +} + +/// A map running on past a narrow extent, with a long legend: veiled, the surrounds in a band. +fn narrow_extent_sheet(name: &str, panels: Value, veil: bool) -> (Session, Value) { + let path = std::env::temp_dir().join(format!("rustgis-mosaic-{name}-{}.geojson", std::process::id())); let ring = json!([[[0.0, 0.0], [15.0, 0.0], [15.0, 10.0], [0.0, 10.0], [0.0, 0.0]]]); let feature = json!({"type": "Feature", "properties": {}, "geometry": {"type": "Polygon", "coordinates": ring}}); std::fs::write(&path, json!({"type": "FeatureCollection", "features": [feature]}).to_string()).unwrap(); - // A map running on past a narrow extent, with a long legend: veiled, the surrounds in a band. - let veiled = |panels: Value| { - let mut s = Session::new(Project::default()); - for _ in 0..12 { - s.run("layer.addData", &json!({"path": path.display().to_string()})).unwrap(); - } - let parts = json!({"profile": "a4-landscape", "extent": [7, 4, 9, 6], "author": "", "locator": "none", "panels": panels}); - let r = publish(&mut s, parts).unwrap(); - (s, r) - }; - let (plain, r) = veiled(json!([[7.9, 4.9, 8.1, 5.1]])); + let mut s = Session::new(Project::default()); + for _ in 0..12 { + s.run("layer.addData", &json!({"path": path.display().to_string()})).unwrap(); + } + std::fs::remove_file(&path).unwrap(); + let parts = json!({"profile": "a4-landscape", "extent": [7, 4, 9, 6], "author": "", "locator": "none", "panels": panels, "veil": veil}); + let r = publish(&mut s, parts).unwrap(); + (s, r) +} + +#[test] +fn a_veiled_key_maps_group_covers_no_panels_number() { + let center = json!([7.9, 4.9, 8.1, 5.1]); + let (plain, r) = narrow_extent_sheet("veil", json!([center]), true); assert_eq!(r["veiled"], json!(true), "{r}"); + let plain_scale = r["scale"].clone(); // A second panel where the group's legend went, outside the extent. - let (guide, legend) = (find(&plain, "Map Frame"), find(&plain, "Legend Box")); - let ElementKind::MapFrame { center, scale, .. } = &guide.kind else { panic!("no frame") }; - let cam = rustgis_map::layout::frame_camera(plain.map(), *center, *scale, 0.0, 0.0); - let pt = rustgis_map::layout::PT_PER_MM; - let (x, y) = (legend.x + legend.w / 2.0 - guide.x, legend.y + legend.h / 2.0 - guide.y); - let at = plain.map().to_lonlat(cam.to_map(x * pt, y * pt, guide.w * pt, guide.h * pt)).unwrap(); - let (s, r) = veiled(json!([[7.9, 4.9, 8.1, 5.1], [at.x - 0.05, at.y - 0.05, at.x + 0.05, at.y + 0.05]])); - std::fs::remove_file(&path).unwrap(); + let under = panel_under_the_legend(&plain); + let (s, r) = narrow_extent_sheet("veil", json!([center, under]), true); + let at = Coord::new( + (under[0].as_f64().unwrap() + under[2].as_f64().unwrap()) / 2.0, + (under[1].as_f64().unwrap() + under[3].as_f64().unwrap()) / 2.0, + ); + // Another band takes the group, so the veil stays, at the same scale (#3538). + assert_eq!(r["veiled"], json!(true), "{r}"); + assert_eq!(r["scale"], plain_scale, "{r}"); let guide = find(&s, "Map Frame"); let ElementKind::MapFrame { center, scale, .. } = &guide.kind else { panic!("no frame") }; let cam = rustgis_map::layout::frame_camera(s.map(), *center, *scale, 0.0, 0.0); + let pt = rustgis_map::layout::PT_PER_MM; let (nx, ny) = cam.to_screen(s.map().to_map(at).unwrap(), guide.w * pt, guide.h * pt); let (px, py) = (guide.x + nx / pt, guide.y + ny / pt); for name in ["Legend Box", "Scale Box", "North Arrow"] { @@ -402,3 +420,23 @@ fn a_veiled_key_maps_group_covers_no_panels_number() { } assert!(!warnings(&r).iter().any(|w| w.contains("is covered by")), "{r}"); } + +#[test] +fn a_mosaic_with_no_band_clear_of_its_numbers_is_as_an_unveiled_one() { + // A panel under each place the veil's group takes, until no band at any scale is clear. + let mut panels = vec![json!([7.9, 4.9, 8.1, 5.1])]; + let (s, r) = loop { + let (s, r) = narrow_extent_sheet("band", json!(panels), true); + if r["veiled"] != json!(true) || panels.len() == 6 { + break (s, r); + } + panels.push(panel_under_the_legend(&s)); + }; + assert_eq!(r["veiled"], json!(false), "{r}"); + // Nothing the veil's search tried is left behind: its scale, its move, or what it counted as + // free (#3538). + let (off, answer) = narrow_extent_sheet("band", json!(panels), false); + assert_eq!(r, answer); + let placed = |s: &Session| elements(s).iter().map(|e| (e.name.clone(), e.x, e.y, e.w, e.h)).collect::>(); + assert_eq!(placed(&s), placed(&off)); +}