From 4e10c914afca3094d9359ab86a166792e7c0e71d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 15:04:42 -0500 Subject: [PATCH 1/6] fix(layout): draw a map frame at its scale past the map view's coarsest zoom (#3362) --- .../engine/src/cmd/layout/publication/mod.rs | 85 +++----------- crates/engine/src/tests.rs | 105 ++++-------------- crates/engine/src/tests/layout_frame_scale.rs | 83 ++++++++++++++ crates/map/src/layout/frame.rs | 8 ++ crates/map/src/layout/tests.rs | 22 ++++ crates/render/src/layout/tests.rs | 4 +- crates/ui-egui/src/layoutview/interact.rs | 11 +- crates/ui-egui/src/layoutview/tests.rs | 30 +++++ 8 files changed, 195 insertions(+), 153 deletions(-) create mode 100644 crates/engine/src/tests/layout_frame_scale.rs diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index 06aa62922a..cf4c4c4ed1 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -1071,13 +1071,6 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu let (inner_w, inner_h) = inner_frame((fw, fh), (room_side, room_end)); let (center, scale) = fit_frame(s.map(), &extent, rectangle.as_ref(), whole_globe, (inner_w, inner_h)) .ok_or("the extent can't be drawn on the map's projection")?; - if whole_globe && center != s.map().center_lonlat() { - warnings.push(format!( - "the globe is centred at {:.1}° rather than the view's {:.1}° of latitude: nearer the pole, the frame can't draw it small enough to fit", - center.y, - s.map().center_lonlat().y - )); - } // A name given past the limit is refused, as layout.new refuses it, not cut (#2639). let given = crate::cmd::within("name", "a layout's name", ps(p, "name").unwrap_or("Publication").trim(), crate::cmd::MAX_NAME)?; let (name, name_cut) = unique_name(s, "New Publication Layout", "the name given", &given); @@ -1169,19 +1162,8 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu m.graticule = src.graticule; m.body = src.body.clone(); m.set_projection(rustgis_map::Projection::Globe); - // Sized on the equator and centred as near the area as the camera draws a globe that - // small, as the whole globe's frame is (#2337): at the area's own latitude near a pole - // the camera stops at its coarsest resolution, and the disc came out larger than its - // circle, clipped. Further from the pole, the area is still on the disc and outlined. let globe = globe_scale(&m, Coord::new(center.x, 0.0), side * 0.92); - let (at, whole) = globe_locator_center(&m, center, globe, side); - if at != center { - warnings.push(format!( - "the globe locator is centred at {:.1}° rather than the map frame's {:.1}° of latitude: nearer the pole, it can't draw the globe small enough to fit", - at.y, center.y - )); - } - if !whole { + if !globe_fits(&m, center, globe, side) { warnings.push("the globe locator is too small to show the whole globe: its circle clips it".into()); } // Each polygon simplified to a tenth of a millimetre on the disc (its scale in metres), @@ -1274,13 +1256,13 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu } None => s.project.maps.push(m), } - Some((name, globe, FrameShape::Circle, at)) + Some((name, globe, FrameShape::Circle, center)) } }; // The inset first: it outlines the area, and the globe after it outlines the inset. let inset_locator = locators .contains(&Locator::Inset) - .then(|| (s.map().name.clone(), round_scale(scale * fw / side * 4.0), FrameShape::Rectangle, center)); + .then(|| (s.map().name.clone(), inset_scale(s.map(), center, scale, fw / side), FrameShape::Rectangle, center)); let locator_maps: Vec<_> = [(Locator::Inset, inset_locator), (Locator::Globe, globe_locator)] .into_iter() .filter_map(|(kind, frame)| frame.map(|frame| (kind, frame))) @@ -2281,11 +2263,10 @@ fn fit_frame( } // The globe seen whole fills the frame, centred where the view looks (#2337): fitting the // whole Earth's flat footprint instead drew it a fraction of the frame, around 0°, 0°. - // Its size at a scale is the same at any latitude, so it is worked out on the equator, - // where the probe stays within the camera's resolutions. + // Its size at a scale is the same at any latitude, so it is worked out on the equator. let view = map.center_lonlat(); let scale = round_scale(globe_scale(map, Coord::new(view.x, 0.0), w.min(h) * WHOLE_GLOBE_FILL)); - Some((drawable_globe_center(map, view, scale), scale)) + Some((view, scale)) } /// Reads `map`'s streamed layers as a frame `size` (mm) at 1:`scale` around `center` draws them, @@ -2416,53 +2397,21 @@ pub(in crate::cmd::layout) fn shows_whole_globe(map: &rustgis_map::Map, p: &Valu /// less than all of it, for the atmosphere's glow at its rim. pub(in crate::cmd::layout) const WHOLE_GLOBE_FILL: f64 = 0.94; -/// Where a frame of `map` at 1:`scale` centres the globe seen whole: at `view`, or as near it on -/// its meridian as the frame's camera can draw the globe that small. Towards a pole the camera's -/// resolution for a scale grows as one over the cosine of the latitude, and past its coarsest -/// ([`rustgis_map::camera::MAX_RES`]) the globe would be drawn larger than the frame (#2337). -pub(in crate::cmd::layout) fn drawable_globe_center(map: &Map, view: Coord, scale: f64) -> Coord { - let fits = - |lat: f64| rustgis_map::layout::frame_camera(map, Coord::new(view.x, lat), scale, 0.0, 0.0).res < rustgis_map::camera::MAX_RES; - if fits(view.y) { - return view; - } - // The latitude in between that the camera still draws at the scale, found by halving. - let (mut fit, mut over) = (0.0f64, view.y.abs()); - for _ in 0..40 { - let mid = (fit + over) / 2.0; - if fits(mid.copysign(view.y)) { fit = mid } else { over = mid } - } - Coord::new(view.x, fit.copysign(view.y)) +/// The scale of an inset locator `times` narrower than a frame at 1:`scale` around `center`: +/// four times the frame's width, but no farther out than the map view zooms, past which a flat +/// world is a small picture in an empty box (#3362); never narrower than the frame. +fn inset_scale(map: &Map, center: Coord, scale: f64, times: f64) -> f64 { + let wide = round_scale(scale * times * 4.0); + let res = rustgis_map::layout::frame_camera(map, center, wide, 0.0, 0.0).res; + wide.min(wide * rustgis_map::camera::MAX_RES / res).max(scale) } -/// Centres the globe locator as close to the area as its circle can hold the rendered disc. -/// If even the equator does not fit, or the area would be behind the rim, keep the locator -/// centred on the area and let its circle clip the globe (#2394). -pub(crate) fn globe_locator_center(map: &Map, area: Coord, scale: f64, side_mm: f64) -> (Coord, bool) { +/// Whether the globe a frame of `map` at 1:`scale` draws, centred on `center`, fits whole in a +/// circle `side_mm` across. +fn globe_fits(map: &Map, center: Coord, scale: f64, side_mm: f64) -> bool { let side = side_mm * rustgis_map::layout::PT_PER_MM; - let globe = |at| { - let camera = rustgis_map::layout::frame_camera(map, at, scale, 0.0, 0.0); - rustgis_map::globe::Globe::new(&camera, side, side) - }; - let fits = |at| 2.0 * globe(at).disc_radius() <= side; - if fits(area) { - return (area, true); - } - let equator = Coord::new(area.x, 0.0); - if !fits(equator) { - return (area, false); - } - // At a fixed scale the disc only grows towards the pole, even after resolution is clamped. - let (mut inside, mut outside) = (equator, area); - for _ in 0..40 { - let mid = Coord::new(area.x, (inside.y + outside.y) / 2.0); - if fits(mid) { - inside = mid; - } else { - outside = mid; - } - } - if globe(inside).forward(area).is_some() { (inside, true) } else { (area, false) } + let camera = rustgis_map::layout::frame_camera(map, center, scale, 0.0, 0.0); + 2.0 * rustgis_map::globe::Globe::new(&camera, side, side).disc_radius() <= side } /// Parses the publication's locator choice, a name or a list of "inset" and "globe", with its diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 0383d19d11..e3858eae0d 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -172,6 +172,9 @@ mod layout_publication_locators; #[path = "tests/layout_publication_crs.rs"] mod layout_publication_crs; +#[path = "tests/layout_frame_scale.rs"] +mod layout_frame_scale; + #[path = "tests/sde_connections.rs"] mod sde_connections; @@ -15640,8 +15643,8 @@ fn a_publication_of_the_whole_globe_fills_its_frame() { assert!((nx - cx).hypot(ny - cy) >= across / 2.0, "{profile}: the legend's corner at {nx:.0}, {ny:.0} is over the globe"); } } - // Looking at the Arctic, where the frame's camera can't draw the globe that small at the - // view's latitude: centred as near it as fits, said, the globe still within the frame. + // Looking at the Arctic, the globe is centred where the view looks there too (#3362), still + // within the frame. s.run("map.goToXY", &json!({"lon": 20.0, "lat": 84.0})).unwrap(); for profile in ["figure-1col", "a4-portrait"] { let r = s.run("layout.publication", &json!({"profile": profile})).unwrap(); @@ -15651,9 +15654,8 @@ fn a_publication_of_the_whole_globe_fills_its_frame() { let cam = frame_camera(s.map(), *center, *scale, 0.0, 0.0); let across = 2.0 * rustgis_map::globe::Globe::new(&cam, frame.w * PT_PER_MM, frame.h * PT_PER_MM).disc_radius() / PT_PER_MM; assert!(across < frame.w.min(frame.h), "{profile}: a disc {across:.0} mm across in {:.0} × {:.0} mm", frame.w, frame.h); - assert!((center.x - 20.0).abs() < 1e-6 && center.y > 0.0 && center.y <= 84.0, "{profile}: {center:?}"); - let said = r["warnings"].to_string(); - assert_eq!(said.contains("rather than the view's 84.0°"), center.y < 84.0 - 1e-6, "{profile}: {said}"); + assert!((center.x - 20.0).abs() < 1e-6 && (center.y - 84.0).abs() < 1e-6, "{profile}: {center:?}"); + assert!(!r["warnings"].to_string().contains("rather than the view's"), "{profile}: {}", r["warnings"]); } // A locator asked for still comes, and "none" asked for is no news; an extent given keeps // the scale bar and the locator. @@ -15669,69 +15671,9 @@ fn a_publication_of_the_whole_globe_fills_its_frame() { assert!(!warnings.contains("the map shows the whole globe"), "{warnings}"); } -/// A globe locator moved off a polar area keeps the area on the disc (#2348): at the sizes where -/// the nearest centre the camera draws would leave it behind the rim (a locator just large enough -/// for the globe on the equator), it keeps the area in the middle, clipped, instead. -#[test] -fn a_globe_locator_never_hides_its_area_behind_the_rim() { - use rustgis_geom::Coord; - use rustgis_map::layout::{PT_PER_MM, frame_camera}; - let mut m = rustgis_map::Map::new("Locator"); - m.set_projection(rustgis_map::Projection::Globe); - let (area, side) = (Coord::new(20.0, 89.0), 40.0); - let (mut moved, mut hidden) = (0, 0); - // Steps of 0.2 %: the sizes that would hide it span about 1 % of the scale. - for i in 0..4000 { - let scale = 1e8 * 1.002f64.powi(i); - let (at, whole) = crate::cmd::layout::globe_locator_center(&m, area, scale, side); - let equator = frame_camera(&m, Coord::new(area.x, 0.0), scale, 0.0, 0.0).res < rustgis_map::camera::MAX_RES; - if whole { - let g = rustgis_map::globe::Globe::new(&frame_camera(&m, at, scale, 0.0, 0.0), side * PT_PER_MM, side * PT_PER_MM); - assert!(g.forward(area).is_some(), "1:{scale:.0}: the area behind the rim from {at:?}"); - moved += usize::from(at != area); - } else { - assert_eq!(at, area, "1:{scale:.0}: a clipped locator keeps the area in the middle"); - hidden += usize::from(equator); - } - } - assert!(moved > 0 && hidden > 0, "{moved} moved, {hidden} kept on the area though the equator fits"); -} - -/// A globe locator a little over the smallest globe the camera draws holds it whole at that limit, -/// on the equator, and says nothing is clipped; one under it is clipped (#2348). -#[test] -fn a_globe_locator_at_the_camera_limit_is_whole_when_its_disc_fits() { - use rustgis_geom::Coord; - use rustgis_map::layout::{PT_PER_MM, frame_camera}; - let mut m = rustgis_map::Map::new("Locator"); - m.set_projection(rustgis_map::Projection::Globe); - // A scale past the camera's coarsest everywhere: the smallest globe it draws, on the equator. - let (area, scale) = (Coord::new(20.0, 30.0), 1e11); - let equator = Coord::new(area.x, 0.0); - let smallest = |side: f64| { - let cam = frame_camera(&m, equator, scale, 0.0, 0.0); - assert!(cam.res >= rustgis_map::camera::MAX_RES); - 2.0 * rustgis_map::globe::Globe::new(&cam, side * PT_PER_MM, side * PT_PER_MM).disc_radius() / PT_PER_MM - }; - let over = smallest(19.0) + 0.5; - let (at, whole) = crate::cmd::layout::globe_locator_center(&m, area, scale, over); - assert!(whole && at.x == area.x && (0.0..area.y).contains(&at.y) && at.y > 0.0, "a {over:.1} mm locator at {at:?}"); - let globe_at = |center| { - let cam = frame_camera(&m, center, scale, 0.0, 0.0); - let side = over * PT_PER_MM; - rustgis_map::globe::Globe::new(&cam, side, side) - }; - let globe = globe_at(at); - assert!(2.0 * globe.disc_radius() <= over * PT_PER_MM && globe.forward(area).is_some()); - assert!(2.0 * globe_at(Coord::new(at.x, at.y + 0.01)).disc_radius() > over * PT_PER_MM); - let under = smallest(19.0) - 0.5; - assert_eq!(crate::cmd::layout::globe_locator_center(&m, area, scale, under), (area, false), "a {under:.1} mm locator"); -} - -/// A publication's globe locator near a pole shows the globe whole in its circle (#2337): sized on -/// the equator and centred as near the area as the camera draws it, not at the area's latitude, -/// where the camera stops at its coarsest resolution and the disc came out larger than the circle. -/// On a flat map with an Arctic extent, and on the globe seen whole with a locator asked for. +/// A publication's globe locator near a pole shows the globe whole in its circle (#2337), centred +/// on the area there as anywhere (#3362). On a flat map with an Arctic extent, and on the globe +/// seen whole with a locator asked for. #[test] fn a_globe_locator_near_a_pole_fits_its_circle() { use rustgis_map::layout::{ElementKind, PT_PER_MM, frame_camera}; @@ -15757,25 +15699,22 @@ fn a_globe_locator_near_a_pole_fits_its_circle() { }; const MOVED: &str = "the globe locator is centred at"; const CLIPPED: &str = "the globe locator is too small"; - let r = s.run("layout.publication", &json!({"profile": "a4-portrait", "extent": [10.0, 76.0, 30.0, 81.0]})).unwrap(); - fits(&s, "Svalbard on a flat map"); - assert!(said(&r, MOVED) && !said(&r, CLIPPED), "Svalbard: {}", r["warnings"]); - // At the camera's coarsest resolution the globe still fits this 18 mm circle, - // and the polar extent is visible from the closest fitting centre (#2394). - let r = - s.run("layout.publication", &json!({"profile": "figure-1col", "extent": [10.0, 83.0, 30.0, 84.0], "locator": "globe"})).unwrap(); - fits(&s, "an 18 mm polar locator"); - assert!(said(&r, MOVED) && !said(&r, CLIPPED), "a figure's locator: {}", r["warnings"]); + for (case, p) in [ + ("Svalbard on a flat map", json!({"profile": "a4-portrait", "extent": [10.0, 76.0, 30.0, 81.0]})), + ("an 18 mm polar locator", json!({"profile": "figure-1col", "extent": [10.0, 83.0, 30.0, 84.0], "locator": "globe"})), + ("north-east Brazil", json!({"profile": "a4-portrait", "extent": [-46.8, -11.6, -38.5, -2.2]})), + ] { + let r = s.run("layout.publication", &p).unwrap(); + fits(&s, case); + assert!(!said(&r, MOVED) && !said(&r, CLIPPED), "{case}: {}", r["warnings"]); + centred_with_the_map(&s, case); + } s.run("map.projection", &json!({"type": "globe"})).unwrap(); s.run("map.goToXY", &json!({"lon": 20.0, "lat": 84.0})).unwrap(); let r = s.run("layout.publication", &json!({"profile": "a4-portrait", "locator": "globe"})).unwrap(); fits(&s, "the whole globe at 84° N"); - assert!(said(&r, MOVED) && !said(&r, CLIPPED), "the whole globe: {}", r["warnings"]); - // Away from the poles the locator stays centred on the area, as before, and says nothing. - let r = s.run("layout.publication", &json!({"profile": "a4-portrait", "extent": [-46.8, -11.6, -38.5, -2.2]})).unwrap(); - fits(&s, "north-east Brazil"); - assert!(!said(&r, MOVED) && !said(&r, CLIPPED), "north-east Brazil: {}", r["warnings"]); - centred_with_the_map(&s, "north-east Brazil"); + assert!(!said(&r, MOVED) && !said(&r, CLIPPED), "the whole globe: {}", r["warnings"]); + centred_with_the_map(&s, "the whole globe at 84° N"); } /// Arrangements of a publication layout's surrounds (#1379): by name, corner by corner, the diff --git a/crates/engine/src/tests/layout_frame_scale.rs b/crates/engine/src/tests/layout_frame_scale.rs new file mode 100644 index 0000000000..82f2acad12 --- /dev/null +++ b/crates/engine/src/tests/layout_frame_scale.rs @@ -0,0 +1,83 @@ +//! A map frame draws at the scale it has, past the map view's coarsest zoom (#3362). + +use super::*; +use rustgis_geom::Coord; +use rustgis_map::camera::MAX_RES; +use rustgis_map::layout::{ElementKind, Layout, PAPER_M_PER_PT, PT_PER_MM, frame_camera}; + +/// The frame called `name` on the last layout: its camera, its size in points and its scale. +fn frame(s: &Session, name: &str) -> (rustgis_map::Camera, (f64, f64), f64) { + let l: &Layout = s.project.layouts.last().unwrap(); + let e = l.elements.iter().find(|e| e.name == name).unwrap_or_else(|| panic!("no {name}")); + let ElementKind::MapFrame { map, center, scale, .. } = &e.kind else { panic!("{name} isn't a map frame") }; + let m = s.project.maps.iter().find(|m| &m.name == map).unwrap(); + (frame_camera(m, *center, *scale, 0.0, 0.0), (e.w * PT_PER_MM, e.h * PT_PER_MM), *scale) +} + +/// The x range of the map the frame called `name` shows, in map units. +fn shown_x(s: &Session, name: &str) -> (f64, f64) { + let (cam, (w, h), _) = frame(s, name); + (cam.to_map(0.0, h / 2.0, w, h).x, cam.to_map(w, h / 2.0, w, h).x) +} + +/// A world map in a blank layout's 40 mm frame, zoomed to all its layers. +fn small_world_frame() -> Session { + let mut s = world(); + s.run("layout.new", &json!({"name": "Small", "template": "blank"})).unwrap(); + s.run("layout.addFrame", &json!({"x": 20, "y": 20, "w": 40, "h": 40})).unwrap(); + s.run("layout.frameZoom", &json!({"to": "full"})).unwrap(); + s +} + +#[test] +fn a_small_frame_zoomed_to_full_extent_holds_it() { + let s = small_world_frame(); + let (cam, _, _) = frame(&s, "Map Frame"); + assert!(cam.res > MAX_RES, "this frame needs a coarser scale than the view's limit: {}", cam.res); + let e = s.map().shown_extent(); + let flat = s.map().flat(); + let (west, east) = + (flat.forward_checked(Coord::new(e.min_x, 0.0)).unwrap().x, flat.forward_checked(Coord::new(e.max_x, 0.0)).unwrap().x); + let (left, right) = shown_x(&s, "Map Frame"); + assert!(left <= west && right >= east, "{left} to {right} doesn't hold {west} to {east}"); +} + +#[test] +fn a_small_frames_scale_text_reads_its_scale() { + let s = small_world_frame(); + let (cam, _, scale) = frame(&s, "Map Frame"); + let drawn = s.map().ground_res_at(&cam) / PAPER_M_PER_PT; + assert!((drawn / scale - 1.0).abs() < 1e-9, "drawn at 1:{drawn:.0}, set to 1:{scale:.0}"); +} + +#[test] +fn a_publication_holds_a_rectangle_wider_than_the_views_limit() { + let mut s = world(); + let p = json!({"profile": "figure-1col", "grid": "none", "crs": "EPSG:3857", "extent": [-30_000_000, -1000, 30_000_000, 1000]}); + s.run("layout.publication", &p).unwrap(); + let (left, right) = shown_x(&s, "Map Frame"); + assert!(left <= -30_000_000.0 && right >= 30_000_000.0, "{left} to {right}"); +} + +/// The inset shows four times the frame's width, but no farther out than the map view goes: past +/// that the world is a small picture in an empty box. It never shows less than the frame. +#[test] +fn an_inset_locator_stops_at_the_views_coarsest_zoom() { + let mut s = world(); + for (case, extent, stops) in [ + ("a continent", [-150.0, -20.0, -40.0, 60.0], true), + ("a country", [-10.0, 40.0, 0.0, 45.0], false), + ("more than the world", [-180.0, -85.0, 180.0, 85.0], false), + ] { + s.run("layout.publication", &json!({"profile": "a4-portrait", "extent": extent, "locator": "inset"})).unwrap(); + let (inset, _, inset_scale) = frame(&s, "Locator"); + let (_, _, main_scale) = frame(&s, "Map Frame"); + assert!(inset_scale >= main_scale, "{case}: the inset at 1:{inset_scale}, the frame at 1:{main_scale}"); + if stops { + assert!((inset.res / MAX_RES - 1.0).abs() < 1e-6, "{case}: {}", inset.res); + } else if case == "a country" { + let times = (shown_x(&s, "Locator").1 - shown_x(&s, "Locator").0) / (shown_x(&s, "Map Frame").1 - shown_x(&s, "Map Frame").0); + assert!((3.8..4.2).contains(×), "{case}: the inset shows {times:.2} times the frame"); + } + } +} diff --git a/crates/map/src/layout/frame.rs b/crates/map/src/layout/frame.rs index ba2023a1b0..eb1c3b3789 100644 --- a/crates/map/src/layout/frame.rs +++ b/crates/map/src/layout/frame.rs @@ -99,7 +99,15 @@ pub fn frame_camera(map: &Map, center: Coord, scale: f64, bearing: f64, pitch: f } let per = map.ground_per_unit(cam.center).max(1e-9); cam.res = scale * PAPER_M_PER_PT / per; + let res = cam.res; cam.clamp(); + // A frame draws at the scale it has: [`crate::camera::MAX_RES`] is how far the map view zooms + // out, and a small frame of a wide area needs coarser (#3362). Cutting the frame's scale to it + // showed less than the frame was fitted to; limiting the fit instead would keep such a frame + // from ever holding the area. + if res.is_finite() { + cam.res = res.max(crate::camera::MIN_RES); + } cam } diff --git a/crates/map/src/layout/tests.rs b/crates/map/src/layout/tests.rs index b927e570c3..2ecc9dadd4 100644 --- a/crates/map/src/layout/tests.rs +++ b/crates/map/src/layout/tests.rs @@ -1130,6 +1130,28 @@ fn frame_camera_keeps_paper_scale() { assert_eq!(fit_scale(&map, &find, 100.0, 100.0).map(|(_, s)| s), Some(MIN_FRAME_SCALE)); } +/// A frame's camera keeps its scale past the map view's coarsest zoom (#3362), up to the coarsest +/// a frame takes; the view itself stops there. +#[test] +fn frame_camera_keeps_a_scale_past_the_views_coarsest_zoom() { + use crate::camera::MAX_RES; + let map = Map::new("Map"); + for (lat, scale) in [(0.0, 1e9), (0.0, MAX_FRAME_SCALE), (60.0, 3.5e8)] { + let cam = frame_camera(&map, Coord::new(0.0, lat), scale, 0.0, 0.0); + let ground = map.ground_res_at(&cam) / PAPER_M_PER_PT; + assert!(cam.res > MAX_RES, "{lat}° at 1:{scale}: {}", cam.res); + assert!((ground / scale - 1.0).abs() < 1e-9, "{lat}° at 1:{scale}: drawn at 1:{ground}"); + } + for bad in [f64::NAN, f64::INFINITY] { + let res = frame_camera(&map, Coord::new(0.0, 0.0), bad, 0.0, 0.0).res; + assert!(res.is_finite() && res <= MAX_RES, "{bad}: {res}"); + } + let mut view = map.camera; + view.res = 1e9; + view.clamp(); + assert_eq!(view.res, MAX_RES); +} + #[test] fn automatic_scale_bars_count_in_what_the_plan_shows() { assert_eq!(ScaleUnits::Auto.resolve(5_000.0), (1000.0, "km")); diff --git a/crates/render/src/layout/tests.rs b/crates/render/src/layout/tests.rs index 14402cc912..3a1a01f430 100644 --- a/crates/render/src/layout/tests.rs +++ b/crates/render/src/layout/tests.rs @@ -4690,7 +4690,9 @@ fn a_frame_partly_off_the_globe_is_outlined_without_a_chord() { }; let source_scale = (1..400).map(|i| f64::from(i) * 1e6).find(|&s| disc(s) < 0.5 * sw - 3.0).unwrap(); assert!(disc(source_scale) > 0.5 * sh + 2.0, "a disc {:.1} mm across", 2.0 * disc(source_scale)); - let (at, scale) = (center, 2.0 * source_scale); + // About 1:604,000,000, what this locator drew when a frame stopped at the view's coarsest zoom + // (#3362): the rim gap below is measured in pixels of this disc. + let (at, scale) = (center, 1.9 * source_scale); let mut l = Layout::new("Layout", 100.0, 100.0); let source = l.add("Main", (55.0, 70.0, sw, sh), frame(center, source_scale, None)); l.add("Locator", (5.0, 5.0, 50.0, 50.0), frame(at, scale, Some(source))); diff --git a/crates/ui-egui/src/layoutview/interact.rs b/crates/ui-egui/src/layoutview/interact.rs index c3add9210b..908815efb6 100644 --- a/crates/ui-egui/src/layoutview/interact.rs +++ b/crates/ui-egui/src/layoutview/interact.rs @@ -362,8 +362,17 @@ pub(super) fn zoom_frame(app: &mut RustGisApp, layout: &Layout, li: usize, id: u // A turned or tilted frame zooms as the map view does, about the anchor. if cam.bearing != 0.0 || cam.pitch != 0.0 { let (w, h) = (e.w * PT_PER_MM, e.h * PT_PER_MM); + // Not [`Camera::zoom_at`]: it stops at the map view's zoom limits, a frame at its own + // (#3362). + let (sx, sy) = ((ax - e.x) * PT_PER_MM, (ay - e.y) * PT_PER_MM); + // Over the sky of a tilted frame, about the centre. + let (sx, sy) = if cam.try_to_map(sx, sy, w, h).is_some() { (sx, sy) } else { (w / 2.0, h / 2.0) }; + let anchor = cam.to_map(sx, sy, w, h); let mut c = cam; - c.zoom_at(*scale / new_scale, (ax - e.x) * PT_PER_MM, (ay - e.y) * PT_PER_MM, w, h); + c.res *= new_scale / *scale; + let after = c.to_map(sx, sy, w, h); + c.center.x += anchor.x - after.x; + c.center.y += anchor.y - after.y; if let Some(ll) = m.to_lonlat(c.center) { let _ = app.run("layout.update", json!({"layout": li, "element": id, "scale": new_scale, "center": [ll.x, ll.y]})); } diff --git a/crates/ui-egui/src/layoutview/tests.rs b/crates/ui-egui/src/layoutview/tests.rs index d5566fabfe..afae219bc7 100644 --- a/crates/ui-egui/src/layoutview/tests.rs +++ b/crates/ui-egui/src/layoutview/tests.rs @@ -910,6 +910,36 @@ fn zooming_a_frame_keeps_the_map_under_the_pointer() { assert_eq!(app.session.undo_depth(), undo + 1, "one undo step"); } +/// A turned frame coarser than the map view's limit zooms about the pointer too (#3362). +#[test] +fn zooming_a_turned_world_frame_keeps_the_map_under_the_pointer() { + use rustgis_engine::map::layout::{ElementKind, PT_PER_MM, frame_camera}; + let mut app = RustGisApp::with_sample(); + app.run("layout.new", json!({"name": "Zoom"})).unwrap(); + let li = app.session.active_layout.unwrap(); + let id = app.session.project.layouts[li].first_frame().unwrap(); + app.run("layout.update", json!({"layout": li, "element": id, "scale": 1.2e9, "bearing": 180, "center": [0.0, 0.0]})).unwrap(); + let layout = app.session.project.layouts[li].clone(); + let e = layout.element(id).unwrap().clone(); + // Beside the centre: half a turn keeps it on the equator, where the resolution for a scale + // stays the same. + let anchor = (e.x + e.w * 0.55, e.y + e.h / 2.0); + // The map point under the anchor, and map units per point. + let map_at = |app: &RustGisApp| { + let e = app.session.project.layouts[li].element(id).unwrap().clone(); + let ElementKind::MapFrame { map, center, scale, bearing, .. } = &e.kind else { panic!("not a map frame") }; + let m = app.session.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map)).unwrap(); + let cam = frame_camera(m, *center, *scale, *bearing, 0.0); + let (w, h) = (e.w * PT_PER_MM, e.h * PT_PER_MM); + (cam.to_map((anchor.0 - e.x) * PT_PER_MM, (anchor.1 - e.y) * PT_PER_MM, w, h), cam.res) + }; + let before = map_at(&app); + super::zoom_frame(&mut app, &layout, li, id, 0.8, anchor); + let after = map_at(&app); + assert!((after.1 / before.1 - 0.8).abs() < 0.02, "the resolution follows: {before:?} → {after:?}"); + assert!((after.0.x - before.0.x).hypot(after.0.y - before.0.y) < after.1, "the anchor moved: {before:?} → {after:?}"); +} + /// A wheel zoom still waiting is applied, not lost, when another frame is zoomed; one waiting /// for a frame of another layout is dropped, never applied to the open layout. #[test] From 1519c739b8eec875c644c60fd3aae288ab3f3ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 18:08:46 -0500 Subject: [PATCH 2/6] fix(layout): keep a zoomed turned frame's centre on the map (#3362) --- crates/ui-egui/src/layoutview/interact.rs | 2 ++ crates/ui-egui/src/layoutview/tests.rs | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/crates/ui-egui/src/layoutview/interact.rs b/crates/ui-egui/src/layoutview/interact.rs index 908815efb6..b8af6da66b 100644 --- a/crates/ui-egui/src/layoutview/interact.rs +++ b/crates/ui-egui/src/layoutview/interact.rs @@ -373,6 +373,8 @@ pub(super) fn zoom_frame(app: &mut RustGisApp, layout: &Layout, li: usize, id: u let after = c.to_map(sx, sy, w, h); c.center.x += anchor.x - after.x; c.center.y += anchor.y - after.y; + // The centre within the map's bounds, as there; only the centre is read from here. + c.clamp(); if let Some(ll) = m.to_lonlat(c.center) { let _ = app.run("layout.update", json!({"layout": li, "element": id, "scale": new_scale, "center": [ll.x, ll.y]})); } diff --git a/crates/ui-egui/src/layoutview/tests.rs b/crates/ui-egui/src/layoutview/tests.rs index afae219bc7..4d4b2149d7 100644 --- a/crates/ui-egui/src/layoutview/tests.rs +++ b/crates/ui-egui/src/layoutview/tests.rs @@ -940,6 +940,26 @@ fn zooming_a_turned_world_frame_keeps_the_map_under_the_pointer() { assert!((after.0.x - before.0.x).hypot(after.0.y - before.0.y) < after.1, "the anchor moved: {before:?} → {after:?}"); } +/// Zoomed about a far corner of a frame many worlds wide, the centre stops at the map's edge, as +/// the map view's does, rather than at a meridian turns away (#3362). +#[test] +fn zooming_a_very_coarse_turned_frame_keeps_its_centre_on_the_map() { + use rustgis_engine::map::layout::ElementKind; + let mut app = RustGisApp::with_sample(); + app.run("layout.new", json!({"name": "Zoom"})).unwrap(); + let li = app.session.active_layout.unwrap(); + let id = app.session.project.layouts[li].first_frame().unwrap(); + app.run("layout.update", json!({"layout": li, "element": id, "scale": 1e10, "bearing": 30, "center": [0.0, 0.0]})).unwrap(); + let layout = app.session.project.layouts[li].clone(); + let e = layout.element(id).unwrap().clone(); + super::zoom_frame(&mut app, &layout, li, id, 0.5, (e.x + e.w, e.y + e.h)); + let Some(ElementKind::MapFrame { center, scale, .. }) = app.session.project.layouts[li].element(id).map(|e| e.kind.clone()) else { + panic!("not a map frame") + }; + assert_eq!(scale, 5e9); + assert!(center.x.abs() <= 270.001 && center.y.abs() <= 85.06, "{center:?}"); +} + /// A wheel zoom still waiting is applied, not lost, when another frame is zoomed; one waiting /// for a frame of another layout is dropped, never applied to the open layout. #[test] From 96468acc4d1d76c36ca32f20c28475bd93780ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 18:27:50 -0500 Subject: [PATCH 3/6] refactor(layout): drop the clipped globe locator notice no publication can reach (#3362) --- crates/engine/src/cmd/layout/publication/mod.rs | 11 ----------- crates/engine/src/tests.rs | 12 +++--------- crates/ui-egui/src/layoutview/interact.rs | 3 ++- 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index cf4c4c4ed1..20c91c9c86 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -1163,9 +1163,6 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu m.body = src.body.clone(); m.set_projection(rustgis_map::Projection::Globe); let globe = globe_scale(&m, Coord::new(center.x, 0.0), side * 0.92); - if !globe_fits(&m, center, globe, side) { - warnings.push("the globe locator is too small to show the whole globe: its circle clips it".into()); - } // Each polygon simplified to a tenth of a millimetre on the disc (its scale in metres), // so the project keeps a sketch of the layers for it, not a second full copy of them. let tolerance_m = globe * 1e-4; @@ -2406,14 +2403,6 @@ fn inset_scale(map: &Map, center: Coord, scale: f64, times: f64) -> f64 { wide.min(wide * rustgis_map::camera::MAX_RES / res).max(scale) } -/// Whether the globe a frame of `map` at 1:`scale` draws, centred on `center`, fits whole in a -/// circle `side_mm` across. -fn globe_fits(map: &Map, center: Coord, scale: f64, side_mm: f64) -> bool { - let side = side_mm * rustgis_map::layout::PT_PER_MM; - let camera = rustgis_map::layout::frame_camera(map, center, scale, 0.0, 0.0); - 2.0 * rustgis_map::globe::Globe::new(&camera, side, side).disc_radius() <= side -} - /// Parses the publication's locator choice, a name or a list of "inset" and "globe", with its /// default: a globe on a sheet, none on a figure or where the map is itself the globe seen whole (it /// would only show it again, smaller). A list is a set: whatever its order, the inset comes first. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index e3858eae0d..4661ae8a6b 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -15647,7 +15647,7 @@ fn a_publication_of_the_whole_globe_fills_its_frame() { // within the frame. s.run("map.goToXY", &json!({"lon": 20.0, "lat": 84.0})).unwrap(); for profile in ["figure-1col", "a4-portrait"] { - let r = s.run("layout.publication", &json!({"profile": profile})).unwrap(); + s.run("layout.publication", &json!({"profile": profile})).unwrap(); let l = s.project.layouts.last().unwrap(); let frame = l.elements.iter().find(|e| e.name == "Map Frame").unwrap(); let ElementKind::MapFrame { center, scale, .. } = &frame.kind else { panic!() }; @@ -15655,7 +15655,6 @@ fn a_publication_of_the_whole_globe_fills_its_frame() { let across = 2.0 * rustgis_map::globe::Globe::new(&cam, frame.w * PT_PER_MM, frame.h * PT_PER_MM).disc_radius() / PT_PER_MM; assert!(across < frame.w.min(frame.h), "{profile}: a disc {across:.0} mm across in {:.0} × {:.0} mm", frame.w, frame.h); assert!((center.x - 20.0).abs() < 1e-6 && (center.y - 84.0).abs() < 1e-6, "{profile}: {center:?}"); - assert!(!r["warnings"].to_string().contains("rather than the view's"), "{profile}: {}", r["warnings"]); } // A locator asked for still comes, and "none" asked for is no news; an extent given keeps // the scale bar and the locator. @@ -15687,7 +15686,6 @@ fn a_globe_locator_near_a_pole_fits_its_circle() { let across = 2.0 * rustgis_map::globe::Globe::new(&cam, locator.w * PT_PER_MM, locator.h * PT_PER_MM).disc_radius() / PT_PER_MM; assert!(across <= locator.w.min(locator.h), "{case}: a disc {across:.0} mm across in a {:.0} mm circle", locator.w); }; - let said = |r: &Value, start: &str| r["warnings"].as_array().unwrap().iter().filter_map(Value::as_str).any(|w| w.starts_with(start)); // The locator centred where the map frame is. let centred_with_the_map = |s: &Session, case: &str| { let l = s.project.layouts.last().unwrap(); @@ -15697,23 +15695,19 @@ fn a_globe_locator_near_a_pole_fits_its_circle() { }; assert_eq!(centre("Locator"), centre("Map Frame"), "{case}"); }; - const MOVED: &str = "the globe locator is centred at"; - const CLIPPED: &str = "the globe locator is too small"; for (case, p) in [ ("Svalbard on a flat map", json!({"profile": "a4-portrait", "extent": [10.0, 76.0, 30.0, 81.0]})), ("an 18 mm polar locator", json!({"profile": "figure-1col", "extent": [10.0, 83.0, 30.0, 84.0], "locator": "globe"})), ("north-east Brazil", json!({"profile": "a4-portrait", "extent": [-46.8, -11.6, -38.5, -2.2]})), ] { - let r = s.run("layout.publication", &p).unwrap(); + s.run("layout.publication", &p).unwrap(); fits(&s, case); - assert!(!said(&r, MOVED) && !said(&r, CLIPPED), "{case}: {}", r["warnings"]); centred_with_the_map(&s, case); } s.run("map.projection", &json!({"type": "globe"})).unwrap(); s.run("map.goToXY", &json!({"lon": 20.0, "lat": 84.0})).unwrap(); - let r = s.run("layout.publication", &json!({"profile": "a4-portrait", "locator": "globe"})).unwrap(); + s.run("layout.publication", &json!({"profile": "a4-portrait", "locator": "globe"})).unwrap(); fits(&s, "the whole globe at 84° N"); - assert!(!said(&r, MOVED) && !said(&r, CLIPPED), "the whole globe: {}", r["warnings"]); centred_with_the_map(&s, "the whole globe at 84° N"); } diff --git a/crates/ui-egui/src/layoutview/interact.rs b/crates/ui-egui/src/layoutview/interact.rs index b8af6da66b..124c158cfc 100644 --- a/crates/ui-egui/src/layoutview/interact.rs +++ b/crates/ui-egui/src/layoutview/interact.rs @@ -373,7 +373,8 @@ pub(super) fn zoom_frame(app: &mut RustGisApp, layout: &Layout, li: usize, id: u let after = c.to_map(sx, sy, w, h); c.center.x += anchor.x - after.x; c.center.y += anchor.y - after.y; - // The centre within the map's bounds, as there; only the centre is read from here. + // Keeps the centre within the map's bounds, as `zoom_at` does. It cuts the resolution too, + // which doesn't matter: only the centre is read below. c.clamp(); if let Some(ll) = m.to_lonlat(c.center) { let _ = app.run("layout.update", json!({"layout": li, "element": id, "scale": new_scale, "center": [ll.x, ll.y]})); From 30ce84222449a2061ad14903969df270ee22d8f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 18:45:42 -0500 Subject: [PATCH 4/6] fix(layout): keep all of a frame inside its inset locator (#3362) --- .../engine/src/cmd/layout/publication/mod.rs | 16 ++++--- crates/engine/src/tests/layout_frame_scale.rs | 43 +++++++++++++------ 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index 20c91c9c86..5af980bb67 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -1259,7 +1259,7 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu // The inset first: it outlines the area, and the globe after it outlines the inset. let inset_locator = locators .contains(&Locator::Inset) - .then(|| (s.map().name.clone(), inset_scale(s.map(), center, scale, fw / side), FrameShape::Rectangle, center)); + .then(|| (s.map().name.clone(), inset_scale(s.map(), center, scale, (fw, fh), side), FrameShape::Rectangle, center)); let locator_maps: Vec<_> = [(Locator::Inset, inset_locator), (Locator::Globe, globe_locator)] .into_iter() .filter_map(|(kind, frame)| frame.map(|frame| (kind, frame))) @@ -2394,13 +2394,15 @@ pub(in crate::cmd::layout) fn shows_whole_globe(map: &rustgis_map::Map, p: &Valu /// less than all of it, for the atmosphere's glow at its rim. pub(in crate::cmd::layout) const WHOLE_GLOBE_FILL: f64 = 0.94; -/// The scale of an inset locator `times` narrower than a frame at 1:`scale` around `center`: -/// four times the frame's width, but no farther out than the map view zooms, past which a flat -/// world is a small picture in an empty box (#3362); never narrower than the frame. -fn inset_scale(map: &Map, center: Coord, scale: f64, times: f64) -> f64 { - let wide = round_scale(scale * times * 4.0); +/// The scale of a square inset locator `side` mm across, of a frame `(w, h)` mm at 1:`scale` +/// around `center`: four times the frame's width, but no farther out than the map view zooms, past +/// which a flat world is a small picture in an empty box (#3362); always far enough out to hold +/// all the frame shows. +fn inset_scale(map: &Map, center: Coord, scale: f64, (w, h): (f64, f64), side: f64) -> f64 { + let wide = round_scale(scale * w / side * 4.0); let res = rustgis_map::layout::frame_camera(map, center, wide, 0.0, 0.0).res; - wide.min(wide * rustgis_map::camera::MAX_RES / res).max(scale) + let holds = scale * w.max(h) / side; + wide.min(wide * rustgis_map::camera::MAX_RES / res).max(holds).min(rustgis_map::layout::MAX_FRAME_SCALE) } /// Parses the publication's locator choice, a name or a list of "inset" and "globe", with its diff --git a/crates/engine/src/tests/layout_frame_scale.rs b/crates/engine/src/tests/layout_frame_scale.rs index 82f2acad12..5cf4fc583e 100644 --- a/crates/engine/src/tests/layout_frame_scale.rs +++ b/crates/engine/src/tests/layout_frame_scale.rs @@ -59,24 +59,43 @@ fn a_publication_holds_a_rectangle_wider_than_the_views_limit() { assert!(left <= -30_000_000.0 && right >= 30_000_000.0, "{left} to {right}"); } +/// The ground the frame called `name` shows, in map units: west, east, south, north. +fn shown(s: &Session, name: &str) -> [f64; 4] { + let (cam, (w, h), _) = frame(s, name); + let (a, b) = (cam.to_map(0.0, 0.0, w, h), cam.to_map(w, h, w, h)); + [a.x, b.x, b.y, a.y] +} + /// The inset shows four times the frame's width, but no farther out than the map view goes: past -/// that the world is a small picture in an empty box. It never shows less than the frame. +/// that the world is a small picture in an empty box. It always holds all the frame shows. #[test] -fn an_inset_locator_stops_at_the_views_coarsest_zoom() { +fn an_inset_locator_stops_at_the_views_coarsest_zoom_and_holds_the_frame() { let mut s = world(); - for (case, extent, stops) in [ - ("a continent", [-150.0, -20.0, -40.0, 60.0], true), - ("a country", [-10.0, 40.0, 0.0, 45.0], false), - ("more than the world", [-180.0, -85.0, 180.0, 85.0], false), + let wide = json!({"profile": "figure-1col", "crs": "EPSG:3857", "extent": [-35_000_000, -1000, 35_000_000, 1000]}); + for (case, mut p, stops) in [ + ("a continent", json!({"profile": "a4-portrait", "extent": [-150.0, -20.0, -40.0, 60.0]}), true), + ("a country", json!({"profile": "a4-portrait", "extent": [-10.0, 40.0, 0.0, 45.0]}), false), + ("most of the world", json!({"profile": "a4-portrait", "extent": [-180.0, -85.0, 180.0, 85.0]}), false), + ("a frame past the limit itself", wide, false), ] { - s.run("layout.publication", &json!({"profile": "a4-portrait", "extent": extent, "locator": "inset"})).unwrap(); - let (inset, _, inset_scale) = frame(&s, "Locator"); - let (_, _, main_scale) = frame(&s, "Map Frame"); - assert!(inset_scale >= main_scale, "{case}: the inset at 1:{inset_scale}, the frame at 1:{main_scale}"); + p.as_object_mut().unwrap().insert("locator".into(), json!("inset")); + if case == "a frame past the limit itself" { + s.run("layout.publication", &p).unwrap(); + assert!(frame(&s, "Map Frame").0.res > MAX_RES, "{case}: {}", frame(&s, "Map Frame").0.res); + } + s.run("layout.publication", &p).unwrap(); + let ([w, e, so, n], [iw, ie, iso, inorth]) = (shown(&s, "Map Frame"), shown(&s, "Locator")); + let slack = (e - w) * 1e-9; + assert!( + iw <= w + slack && ie >= e - slack && iso <= so + slack && inorth >= n - slack, + "{case}: the inset shows {iw} to {ie}, {iso} to {inorth} of the frame's {w} to {e}, {so} to {n}" + ); if stops { + let (inset, _, _) = frame(&s, "Locator"); assert!((inset.res / MAX_RES - 1.0).abs() < 1e-6, "{case}: {}", inset.res); - } else if case == "a country" { - let times = (shown_x(&s, "Locator").1 - shown_x(&s, "Locator").0) / (shown_x(&s, "Map Frame").1 - shown_x(&s, "Map Frame").0); + } + if case == "a country" { + let times = (ie - iw) / (e - w); assert!((3.8..4.2).contains(×), "{case}: the inset shows {times:.2} times the frame"); } } From c4089866cf510ba0486824574755bdeb26a460db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 19:01:36 -0500 Subject: [PATCH 5/6] fix(layout): place the inset locator by the map frame as placed (#3362) --- .../engine/src/cmd/layout/publication/mod.rs | 15 ++++++--- crates/engine/src/tests/layout_frame_scale.rs | 31 +++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index 5af980bb67..2f8130c5e5 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -1257,9 +1257,8 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu } }; // The inset first: it outlines the area, and the globe after it outlines the inset. - let inset_locator = locators - .contains(&Locator::Inset) - .then(|| (s.map().name.clone(), inset_scale(s.map(), center, scale, (fw, fh), side), FrameShape::Rectangle, center)); + // The inset's centre and scale here are the frame's: its own are worked out as it is added. + let inset_locator = locators.contains(&Locator::Inset).then(|| (s.map().name.clone(), scale, FrameShape::Rectangle, center)); let locator_maps: Vec<_> = [(Locator::Inset, inset_locator), (Locator::Globe, globe_locator)] .into_iter() .filter_map(|(kind, frame)| frame.map(|frame| (kind, frame))) @@ -1942,13 +1941,19 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu Locator::Inset => "Locator Inset", Locator::Globe => "Locator Globe", }; + // The inset follows the map frame as placed: the legend's room may have moved or + // zoomed it out (#3362). + let (locator_center, locator_scale) = match kind { + Locator::Inset => (center, inset_scale(s.map(), center, scale, (fw, fh), side)), + Locator::Globe => (*locator_center, *locator_scale), + }; target = l.add( name, (r.0 + slot as f64 * (side + locator_gap), r.1, side, r.3), ElementKind::MapFrame { map: map.clone(), - center: *locator_center, - scale: *locator_scale, + center: locator_center, + scale: locator_scale, shape: shape.clone(), grid: None, sections: Vec::new(), diff --git a/crates/engine/src/tests/layout_frame_scale.rs b/crates/engine/src/tests/layout_frame_scale.rs index 5cf4fc583e..254aa16d0f 100644 --- a/crates/engine/src/tests/layout_frame_scale.rs +++ b/crates/engine/src/tests/layout_frame_scale.rs @@ -100,3 +100,34 @@ fn an_inset_locator_stops_at_the_views_coarsest_zoom_and_holds_the_frame() { } } } + +/// Where the legend's room moves or zooms out the map frame, the inset follows it: centred on it +/// and holding all it shows. +#[test] +fn an_inset_locator_follows_a_frame_moved_for_the_legend() { + let dir = std::env::temp_dir().join(format!("rustgis-inset-moved-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("a_layer_name.geojson"); + let data = json!({"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {}, "geometry": + {"type": "Polygon", "coordinates": [[[0.0, 0.0], [15.0, 0.0], [15.0, 10.0], [0.0, 10.0], [0.0, 0.0]]]}}]}); + std::fs::write(&path, data.to_string()).unwrap(); + // A legend long enough to move the map aside, then one that has it drawn at a smaller scale. + for (layers, extent, said) in [(30, [0.0, 0.0, 15.0, 10.0], "the map is moved"), (12, [5.0, 4.0, 8.7, 6.0], "the map is drawn at 1:")] { + let mut s = Session::new(Project::default()); + for _ in 0..layers { + s.run("layer.addData", &json!({"path": path.display().to_string()})).unwrap(); + } + let p = json!({"profile": "a4-landscape", "extent": extent, "author": "", "locator": "inset"}); + let r = s.run("layout.publication", &p).unwrap(); + assert!(r["warnings"].to_string().contains(said), "{said}: {}", r["warnings"]); + let l = s.project.layouts.last().unwrap(); + let centre = |name: &str| match l.elements.iter().find(|e| e.name == name).map(|e| &e.kind) { + Some(ElementKind::MapFrame { center, .. }) => *center, + _ => panic!("{said}: no {name}"), + }; + assert_eq!(centre("Locator"), centre("Map Frame"), "{said}"); + let ([w, e, so, n], [iw, ie, iso, inorth]) = (shown(&s, "Map Frame"), shown(&s, "Locator")); + assert!(iw <= w && ie >= e && iso <= so && inorth >= n, "{said}"); + } + let _ = std::fs::remove_dir_all(&dir); +} From 22191dcde98f30a3ac405083e87d85e6bc4f041a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 19:09:39 -0500 Subject: [PATCH 6/6] docs(layout): say when an inset locator can't hold its frame (#3362) --- crates/engine/src/cmd/layout/publication/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index 2f8130c5e5..14e03045f8 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -2401,8 +2401,9 @@ pub(in crate::cmd::layout) const WHOLE_GLOBE_FILL: f64 = 0.94; /// The scale of a square inset locator `side` mm across, of a frame `(w, h)` mm at 1:`scale` /// around `center`: four times the frame's width, but no farther out than the map view zooms, past -/// which a flat world is a small picture in an empty box (#3362); always far enough out to hold -/// all the frame shows. +/// which a flat world is a small picture in an empty box (#3362); far enough out to hold all the +/// frame shows, up to [`rustgis_map::layout::MAX_FRAME_SCALE`], which only a locator a long note +/// has squeezed under about a centimetre reaches (the publication says the column has little room). fn inset_scale(map: &Map, center: Coord, scale: f64, (w, h): (f64, f64), side: f64) -> f64 { let wide = round_scale(scale * w / side * 4.0); let res = rustgis_map::layout::frame_camera(map, center, wide, 0.0, 0.0).res;