From 5a974bbfdf554fc4a80250f63f5d52b662ee1622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Tue, 1 Dec 2026 14:56:36 -0500 Subject: [PATCH] fix(layout): a publication's map isn't moved toward a pole and drawn smaller for the legend's room (#3514) --- .../engine/src/cmd/layout/publication/mod.rs | 23 ++++++++-- crates/engine/src/tests.rs | 3 ++ .../src/tests/layout_publication_moves.rs | 44 +++++++++++++++++++ docs/user-guide/layouts.md | 3 +- 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 crates/engine/src/tests/layout_publication_moves.rs diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index 2d0a661334..9c8695705a 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -1492,6 +1492,7 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu let at = flat.forward_checked(center).filter(|_| !plane.is_empty()); if let Some(at) = at.filter(|_| coverage.as_ref().is_some_and(|c| need(free_room(c)).is_none())) { let (centred, asked_scale) = (center, scale); + let ground_at = flat.ground_per_unit(at); let continuous = coverage.as_ref().is_some_and(|(gw, gh, cells)| { let mm = 1000.0 * flat.ground_per_unit(plane.center()) * s.map().body().ratio() / asked_scale; let (half_w, half_h) = (plane.width() * mm / 2.0, plane.height() * mm / 2.0); @@ -1629,8 +1630,14 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu // by `dx` and south by `dy` is the wide one's part that far west and north of // its middle while that scale moves a corner of it one cell of the coverage grid // at most, which the cut is exact to anyway; past that, it is drawn on its own - // (what is context then judged on itself, for the legend too), once. - let here_mm = mm * flat.ground_per_unit(Coord::new(cx, cy)) / flat.ground_per_unit(plane.center()); + // (what is context then judged on itself, for the legend too), once. A move that + // would draw the extent more than `MOVE_SIZE_CHANGE` larger or smaller isn't made + // either: a smaller extent fits any margins, so the margins check can't refuse it. + // A sheet then goes on to the scales tried after this one (zooming out, said); a + // figure keeps its scale, so with no move left the legend is laid out where the + // map is, as at any place no move makes room (#3514). + let ground_here = flat.ground_per_unit(Coord::new(cx, cy)); + let here_mm = mm * ground_here / flat.ground_per_unit(plane.center()); let clip = ( fx + fw / 2.0 + (plane.min_x - cx) * here_mm, fy + fh / 2.0 - (plane.max_y - cy) * here_mm, @@ -1641,10 +1648,11 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu && clip.1 >= fy + top - 0.5 && clip.0 + clip.2 <= fx + fw - right + 0.5 && clip.1 + clip.3 <= fy + fh - bottom + 0.5; - if !still && !kept_whole { + let size_change = (1.0 - ground_here / ground_at).abs(); + if !still && (!kept_whole || size_change > MOVE_SIZE_CHANGE) { continue; } - let drift = (1.0 - flat.ground_per_unit(Coord::new(cx, cy)) / flat.ground_per_unit(at)).abs() * fw.hypot(fh) / 2.0; + let drift = size_change * fw.hypot(fh) / 2.0; let (mut moved, legend_sizes, legend_context) = if drift <= fw.max(fh) / 384.0 { (crop_cells(whole_frame, wide, (slack_x - dx, slack_y - dy), (fw, fh)), *here, here_context.clone()) } else if let Some((_, own, own_context, own_sizes)) = own_drawn.iter().find(|o| o.0 == (tried, dx, dy)) { @@ -2398,6 +2406,13 @@ pub(in crate::cmd::layout) fn publication_flag(p: &Value, key: &str) -> Result f64 { let mid = ((e.min_y + e.max_y) / 2.0).to_radians(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 2b65676ea9..25eea854d2 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -190,6 +190,9 @@ mod layout_publication_crs; #[path = "tests/layout_frame_scale.rs"] mod layout_frame_scale; +#[path = "tests/layout_publication_moves.rs"] +mod layout_publication_moves; + #[path = "tests/layer_ids.rs"] mod layer_ids; diff --git a/crates/engine/src/tests/layout_publication_moves.rs b/crates/engine/src/tests/layout_publication_moves.rs new file mode 100644 index 0000000000..5e808345eb --- /dev/null +++ b/crates/engine/src/tests/layout_publication_moves.rs @@ -0,0 +1,44 @@ +//! A move of the publication's map for the legend's room keeps the extent's drawn size (#3514). + +use super::*; +use rustgis_geom::Coord; +use rustgis_map::layout::ElementKind; + +/// The centre of the layout's last map frame. +fn frame_centre(s: &Session) -> Coord { + let l = s.project.layouts.last().unwrap(); + let ElementKind::MapFrame { center, .. } = &l.elements.iter().find(|e| e.name == "Map Frame").unwrap().kind else { + panic!("no Map Frame") + }; + *center +} + +/// A world extent with a one-column legend asks for the room the map leaves free; on Web Mercator +/// a move toward a pole there drew the extent several times smaller, and said nothing (#3514). +#[test] +fn a_move_for_the_legends_room_does_not_shrink_the_extent_toward_a_pole() { + for profile in ["a4-portrait", "a5-portrait", "thesis-full"] { + // Where the extent sits centred (no legend, nothing to move aside), and with the legend. + let extent = json!([-111.5, -16.6, 146.5, 62.6]); + let mut centred = world(); + let centred_answer = centred.run("layout.publication", &json!({"profile": profile, "extent": extent, "legend": "none"})).unwrap(); + let mut s = world(); + let answer = s.run("layout.publication", &json!({"profile": profile, "extent": extent, "legend": {"columns": 1}})).unwrap(); + let flat = s.map().flat(); + let at = |lonlat: Coord| flat.ground_per_unit(flat.forward_checked(lonlat).unwrap()); + let stretch = at(frame_centre(&s)) / at(frame_centre(¢red)); + // The legend asked for in one column is laid out in one column, with no move or veil. + assert_eq!( + (answer["legendColumns"].as_u64(), answer["veiled"].as_bool(), answer["mapShift"][0].as_f64()), + (Some(1), Some(false), Some(0.0)), + "{profile}: {answer}" + ); + // Not drawn at a smaller scale instead: the same scale as centred. + assert_eq!(answer["scale"], centred_answer["scale"], "{profile}: {answer}"); + // 0.20 is `MOVE_SIZE_CHANGE` in `cmd/layout/publication/mod.rs`; keep them equal. + assert!( + (stretch - 1.0).abs() <= 0.20, + "{profile}: the frame's centre draws the extent {stretch} times its size, more than 20 % (the search's own zoom-out step, 1.25, as a fraction)" + ); + } +} diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 02318123cc..9eddc4c32b 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -171,7 +171,8 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu nearest, the one with the most room is kept. Where the projection stretches, as Web Mercator does away from the equator, the moved frame is drawn at its new centre's ground scale, and the extent has to stay whole at that - scale. Only when no move makes room for it, a continuous map with a long legend is veiled: at + scale and keep its size within 20%: a move that would draw it 20% larger or smaller isn't made. + Only when no move makes room for it, a continuous map with a long legend is veiled: at the asked scale, at least 80% of the extent's part of the frame and 30% of the rest are drawn (not counting the layers just named), and the legend's box in one column, its colorbar included, is taller than a quarter of the frame; a sparser map stays unveiled. The veil covers what is