From ca93d960ec4405c65f80ca09ae530dc0b3ef170d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 22 Nov 2026 08:28:24 -0500 Subject: [PATCH 1/5] A locked scale bar keeps its size; moved bars, frame centres and maps re-measure (#2698) A scale bar's textSize given as null changes it, as a legend's does, so a locked bar refuses it. A scale bar moved with x or y is told whether its labels still fit. A publication's scale box is measured again when its frame's centre or map changes, each basis on its own map: the text under the bar fills {center} and {map} from them. A grid's label that reaches further in from its edge than the frame is across is left out, as a label without room is, rather than cut by the frame. --- crates/engine/src/cmd/layout.rs | 33 ++++++++++++++++++++++---------- crates/engine/src/tests.rs | 27 ++++++++++++++++++++++++++ crates/map/src/layout.rs | 4 ++-- crates/render/src/layout.rs | 34 +++++++++++++++++++++++++++------ 4 files changed, 80 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 3fede227fe..857fed9619 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -5521,7 +5521,10 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { let refit = basis.filter(|basis| { // A text size given back (`null`) changes the bar's labels too. let restyled = id == basis.bar_id && (given(&["units", "divisions"]) || p.get("textSize").is_some()); - (id == basis.frame && given(&["scale", "w"])) || (id == basis.text_id && given(&["text", "size", "bold"])) || restyled + // Its frame's centre and map fill the text's tokens and set the bar's ground too. + (id == basis.frame && given(&["scale", "w", "center", "map"])) + || (id == basis.text_id && given(&["text", "size", "bold"])) + || restyled }); // A map frame drawn at another view: its scale bars' lengths and labels change (#2581). let frame_view = given(&["scale", "center", "bearing", "pitch", "map"]) @@ -5531,8 +5534,14 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { } else if let Some(basis) = &refit { refit_scale_box(s, li, basis); } - // After the scale box is refitted: a text is measured in the box it is saved with. - if shown || TEXT_FIT_KEYS.iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) || BAR_FIT_KEYS.iter().any(|k| p.get(*k).is_some()) { + // After the scale box is refitted: a text is measured in the box it is saved with, and a scale + // bar moved, whose labels can then run off the page, too. + let bar = s.project.layouts.get(li).and_then(|l| l.element(id)).is_some_and(|e| matches!(e.kind, ElementKind::ScaleBar { .. })); + if shown + || TEXT_FIT_KEYS.iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) + || BAR_FIT_KEYS.iter().any(|k| p.get(*k).is_some()) + || (bar && given(&["x", "y"])) + { say_text_fit(s, li, id); } say_held(s, "Element Properties", held); @@ -5573,11 +5582,12 @@ fn scale_text_mm(text: &str, text_pt: f64, bold: bool) -> f64 { /// What a publication places over its map, which a scale bar box measured again must stay off. const PLACED_OVER_MAP: [&str; 3] = ["Legend Box", "Locator", "North Arrow"]; -/// What a publication's scale bar box is measured from: its frame (id, centre, scale, width), its -/// bar (id, units, divisions) and the text under the bar (id, text, size, boldness). +/// What a publication's scale bar box is measured from: its frame (id, map, centre, scale, width), +/// its bar (id, units, divisions, text size) and the text under the bar (id, text, size, boldness). #[derive(Clone)] struct BoxBasis { frame: u32, + map: String, center: Coord, scale: f64, frame_w: f64, @@ -5629,9 +5639,10 @@ fn scale_box_basis(s: &Session, li: usize) -> Option { return None; }; let fr = l.element(*frame)?; - let ElementKind::MapFrame { center, scale, .. } = &fr.kind else { return None }; + let ElementKind::MapFrame { map, center, scale, .. } = &fr.kind else { return None }; Some(BoxBasis { frame: *frame, + map: map.clone(), center: *center, scale: *scale, frame_w: fr.w, @@ -5663,7 +5674,8 @@ fn frame_view_changed(s: &mut Session, li: usize, frame: u32, basis: Option<&Box } } -/// A publication's scale bar box measured again after its frame's scale or width changed (#1379): +/// A publication's scale bar box measured again after its frame's scale, width, centre or map +/// changed (#1379, #2581), each measured on its own map and centre: /// the "Scale Box", the "Scale Bar" of `frame` and the "Scale" under it, as a publication lays them /// out (one place, one width, none locked), take the width that bar and the text under it (its /// tokens filled as the page draws them, measured as drawn) now need, kept on the side of the @@ -5680,8 +5692,9 @@ fn refit_scale_box(s: &mut Session, li: usize, before: &BoxBasis) { let Some(now) = scale_box_basis(s, li).filter(|b| b.frame == frame) else { return }; let Some(l) = s.project.layouts.get(li) else { return }; let Some(fr) = l.element(frame) else { return }; - let ElementKind::MapFrame { map, .. } = &fr.kind else { return }; - let Some(m) = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map)) else { return }; + // Each basis measured on its own map: a frame given another map fills the text from it. + let map_of = |b: &BoxBasis| s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(&b.map)); + let (Some(m), Some(was)) = (map_of(&now), map_of(before)) else { return }; let named = |name: &str| l.elements.iter().find(|e| e.name == name); let (Some(boxed), Some(bar), Some(text)) = (named("Scale Box"), named("Scale Bar"), named("Scale")) else { return }; let (ElementKind::ScaleBar { frame: of, .. }, ElementKind::Text { text: written, letter_spacing, font, font_name, .. }) = @@ -5708,7 +5721,7 @@ fn refit_scale_box(s: &mut Session, li: usize, before: &BoxBasis) { return; } // The width the box had from its basis before the change: another one was set by hand. - if (boxed.w - basis_width(l, m, before)).abs() > 0.01 { + if (boxed.w - basis_width(l, was, before)).abs() > 0.01 { return; } let w = basis_width(l, m, &now); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 4716190df8..469f4fb539 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -12062,6 +12062,25 @@ fn publication_layouts_fit_their_pages() { assert!(widths.iter().any(|w| (w - first).abs() > 0.5), "the box changed: {first} mm, then {widths:?}"); // Its bar's labels at 40 points need a wider box for the last one, and 8 gives the width back. assert!(widths[2] > widths[1] + 0.5 && (widths[3] - widths[1]).abs() < 1e-9, "{widths:?}"); + // A text naming the frame's centre and map follows them: a new centre or map, each measured + // from its own, leaves the box as wide as it is automatically then. + let scale_text = id_in(&s, "Scale"); + s.run("map.new", &json!({"name": "The second map, longer"})).unwrap(); + let mut followed = Vec::new(); + for (text, change) in [ + ("{scale} {center}", json!({"element": frame, "center": [-120.5, -45.25]})), + ("{scale} {map}", json!({"element": frame, "map": "The second map, longer"})), + ] { + s.run("layout.update", &json!({"layout": li, "element": scale_text, "text": text})).unwrap(); + let mut change = change; + change["layout"] = json!(li); + let before = width(&s); + s.run("layout.update", &change).unwrap(); + let auto = crate::cmd::layout::scale_box_auto(&s, li).unwrap(); + assert!((width(&s) - auto).abs() < 1e-9, "{change}: {} mm, {auto} mm automatic", width(&s)); + followed.push(width(&s) - before); + } + assert!(followed.iter().all(|d| d.abs() > 0.1), "each changed the box: {followed:?}"); // A long title wraps within the frame, and pushes the map down; the result names every // element for layout.update. let r = s.run("layout.publication", &json!({"extent": piaui, "title": long})).unwrap(); @@ -14236,6 +14255,14 @@ fn scale_bars_and_grids_take_a_label_size() { s.run(change.0, &change.1).unwrap(); assert!(said(&s, mark).contains("its box is too small"), "{change:?}: {}", said(&s, mark)); } + // Moved off the page's edge, its labels are told too; a locked bar takes no size, null too. + let mark = s.notice_mark(); + s.run("layout.update", &json!({"element": bar, "x": 250})).unwrap(); + assert!(said(&s, mark).contains("runs off the page"), "{}", said(&s, mark)); + s.run("layout.update", &json!({"element": bar, "x": 10, "locked": true})).unwrap(); + assert!(s.run("layout.update", &json!({"element": bar, "textSize": null})).is_err()); + assert_eq!(bar_size(&s, bar), 40.0, "the locked bar keeps its size"); + s.run("layout.update", &json!({"element": bar, "locked": false})).unwrap(); let mark = s.notice_mark(); s.run("layout.update", &json!({"element": bar, "textSize": null})).unwrap(); assert_eq!(said(&s, mark), "", "the default fits"); diff --git a/crates/map/src/layout.rs b/crates/map/src/layout.rs index 7c7f1ba3d3..74c144eff8 100644 --- a/crates/map/src/layout.rs +++ b/crates/map/src/layout.rs @@ -1486,11 +1486,11 @@ impl ElementKind { /// Whether `key` given as null changes an element of this kind, giving it back its default: /// an ink's `color`, a chart's `title` (the title it is given) and `barColor`, and a legend's - /// `textSize` (#2581). + /// or scale bar's `textSize` (#2581). pub fn null_resets(&self, key: &str) -> bool { (self.takes_ink() && key == "color") || (matches!(self, ElementKind::Chart { .. }) && matches!(key, "title" | "barColor")) - || (matches!(self, ElementKind::Legend { .. }) && key == "textSize") + || (matches!(self, ElementKind::Legend { .. } | ElementKind::ScaleBar { .. }) && key == "textSize") } /// Its ink, when it [takes one](ElementKind::takes_ink) and has one; `None` draws in [`INK`]. diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 42f5938551..1aa1a88c4b 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -1905,8 +1905,17 @@ fn draw_grid(pm: &mut Canvas, map: &Map, view: &View, g: &Grid, comma: bool) -> Edge::Left | Edge::Right if !vertical => label_px, _ => text::measure(s, label_px / 1.33, false), }; + // How far a label reaches in from its edge: past the inset, its height, or a side's flat + // label its width. + let across = |edge: Edge, s: &str| { + inset + + match edge { + Edge::Left | Edge::Right if !vertical => text::measure(s, label_px / 1.33, false), + _ => label_px, + } + }; let mut drawn = Vec::new(); - for (edge, at, s) in kept_labels(labels, (pw, ph), along, label_px, unit) { + for (edge, at, s) in kept_labels(labels, (pw, ph), (along, across), label_px, unit) { let tw = text::measure(&s, label_px / 1.33, false); // Its centre, and half its size across and down (its halo and descenders included). let (flat, upright) = ((tw / 2.0, label_px * 0.6), (label_px * 0.6, tw / 2.0)); @@ -2005,8 +2014,9 @@ fn mgrs_squares( /// The graticule labels drawn, of those at each edge crossing (`labels`: edge, place along it, /// text, a meridian's or not) on a frame `size` pixels, `label_h` tall: away from the corners (all -/// of a label, `along` its edge, fits, and none at a side's ends by a top or bottom label), and -/// none over another. Meridians are labelled along the top and +/// of a label, `along` its edge, fits, and none at a side's ends by a top or bottom label), within +/// the frame across (how far it reaches in from its edge, `across`, at most the frame's other +/// side: a label larger than a short frame isn't cut by it), and none over another. Meridians are labelled along the top and /// bottom and parallels along the sides first; a line that leaves the frame through the other /// edges (a slanting meridian on a projected map) is labelled there only well clear of the labels /// already set on that edge. Their room from the corners and between them is in `unit`s, a point @@ -2016,7 +2026,7 @@ fn mgrs_squares( fn kept_labels( mut labels: Vec<(Edge, f32, String, bool)>, (pw, ph): (f32, f32), - along: impl Fn(Edge, &str) -> f32, + (along, across): (impl Fn(Edge, &str) -> f32, impl Fn(Edge, &str) -> f32), label_h: f32, unit: f32, ) -> Vec<(Edge, f32, String)> { @@ -2027,7 +2037,10 @@ fn kept_labels( for (edge, at, s, lon) in labels { let length = along(edge, &s); let side = matches!(edge, Edge::Left | Edge::Right); - let limit = if side { ph } else { pw }; + let (limit, room) = if side { (ph, pw) } else { (pw, ph) }; + if across(edge, &s) > room { + continue; + } // A side's labels keep two label heights from the corners, below the top's labels and // above the bottom's, so a corner holds no two. let margin = (length / 2.0 + 3.0 * unit).max(12.0 * unit).max(if side { 2.0 * label_h } else { 0.0 }); @@ -8648,8 +8661,17 @@ Elaboração: RustGIS", label(Edge::Top, 4.0, "44°W", true), label(Edge::Right, 15.0, "2°S", false), ]; - let kept: Vec = kept_labels(labels, (600.0, 500.0), along, 10.0, 1.0).into_iter().map(|(.., s)| s).collect(); + let kept: Vec = + kept_labels(labels.clone(), (600.0, 500.0), (along, |_: Edge, _: &str| 20.0), 10.0, 1.0).into_iter().map(|(.., s)| s).collect(); assert_eq!(kept, ["6°S", "42°W", "34°W"]); + // Labels reaching further in than a short frame is across are left out, not cut by it: + // on a frame 30 pixels tall, the top's; the sides', 600 wide, keep theirs. + let kept: Vec = kept_labels(labels, (600.0, 30.0), (along, |_: Edge, _: &str| 40.0), 10.0, 1.0) + .into_iter() + .filter(|(edge, ..)| *edge == Edge::Top) + .map(|(.., s)| s) + .collect(); + assert!(kept.is_empty(), "{kept:?}"); } /// A layout with a decimal comma swaps the marks of the numbers it prints, `{scale}` included, From a23553f5f3ef80785cade54d8332a862cdf293ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 22 Nov 2026 08:57:56 -0500 Subject: [PATCH 2/5] Grid labels too large across a narrow frame are left out on every side; review nits (#2698) A test now covers a side's labels on a frame too narrow for them. The kept_labels doc is rewrapped, bar_inset's doc says BAR_INSET is in pixels, and say_text_fit names its notices without shadowing. --- crates/engine/src/cmd/layout.rs | 6 +++--- crates/render/src/layout.rs | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 857fed9619..6bac1c7c9d 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -3550,13 +3550,13 @@ fn say_text_fit(s: &mut Session, li: usize, id: u32) { }; let Some(loss) = text_fit_loss(s, li, id) else { return }; // A scale bar's labels (#2581): its box too small for them, or one off the page. - let said = if bar { + let notices = if bar { rustgis_render::layout::scale_bar_fit_notices(&[loss]) } else { rustgis_render::layout::lines_left_out_notice(&loss).into_iter().collect() }; - for said in said { - s.warn(said); + for notice in notices { + s.warn(notice); } } diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 1aa1a88c4b..47abf0fb19 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -2015,12 +2015,12 @@ fn mgrs_squares( /// The graticule labels drawn, of those at each edge crossing (`labels`: edge, place along it, /// text, a meridian's or not) on a frame `size` pixels, `label_h` tall: away from the corners (all /// of a label, `along` its edge, fits, and none at a side's ends by a top or bottom label), within -/// the frame across (how far it reaches in from its edge, `across`, at most the frame's other -/// side: a label larger than a short frame isn't cut by it), and none over another. Meridians are labelled along the top and -/// bottom and parallels along the sides first; a line that leaves the frame through the other -/// edges (a slanting meridian on a projected map) is labelled there only well clear of the labels -/// already set on that edge. Their room from the corners and between them is in `unit`s, a point -/// at the labels' default size in pixels, so it follows their size. +/// the frame across (how far it reaches in from its edge, `across`, at most the frame's other side: +/// a label larger than a short frame isn't cut by it), and none over another. Meridians are +/// labelled along the top and bottom and parallels along the sides first; a line that leaves the +/// frame through the other edges (a slanting meridian on a projected map) is labelled there only +/// well clear of the labels already set on that edge. Their room from the corners and between them +/// is in `unit`s, a point at the labels' default size in pixels, so it follows their size. /// A label left out for want of room is thinning, as on any map, not a loss to report: the grid /// still shows its lines, and the user guide says labels go where there is room. fn kept_labels( @@ -2254,7 +2254,8 @@ pub fn scale_bar_width_pt(ground_per_pt: f64, width_pt: f64, bar: (ScaleUnits, u /// How far a scale bar starts in from its box's left side, in pixels, for its first label `first` /// at `k` times the default size (#2581): [`BAR_INSET`] in proportion, or more, so that label /// reaches no further past the side than it does at the default size, as the bar moves down to -/// keep its labels from reaching higher. At the default size, [`BAR_INSET`]. +/// keep its labels from reaching higher. At the default size, [`BAR_INSET`]: pixels, as the +/// drawing has always placed the bar, not following `ppp`, which only the labels' widths do. fn bar_inset(first: &str, k: f32, ppp: f32) -> f32 { let half = |k: f32| text::measure(first, SCALE_BAR_TEXT_PT as f32 * k * ppp, false) / 2.0; (BAR_INSET * k).max(half(k) - (half(1.0) - BAR_INSET).max(0.0)) @@ -8672,6 +8673,11 @@ Elaboração: RustGIS", .map(|(.., s)| s) .collect(); assert!(kept.is_empty(), "{kept:?}"); + // And on a frame 15 pixels wide, the sides' labels, whose flat labels reach their width in. + let across = |edge: Edge, s: &str| if matches!(edge, Edge::Left | Edge::Right) { s.len() as f32 * 5.0 + 6.0 } else { 6.0 }; + let side = vec![label(Edge::Right, 200.0, "6°S", false), label(Edge::Left, 250.0, "8°S", false)]; + assert_eq!(kept_labels(side.clone(), (600.0, 500.0), (along, across), 10.0, 1.0).len(), 2, "a wide frame keeps them"); + assert!(kept_labels(side, (15.0, 500.0), (along, across), 10.0, 1.0).is_empty(), "a narrow one leaves them out"); } /// A layout with a decimal comma swaps the marks of the numbers it prints, `{scale}` included, From d654d6a2554e3a39fccf846d17459d9512b905aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 22 Nov 2026 09:22:00 -0500 Subject: [PATCH 3/5] A scale bar's end label over its first makes its box too small (#2698) Above the default size, an end label running into the first label, both always drawn, now counts as a box too small for its labels and is said, as a label past the box's side is. TextLoss::below_box says what it means for a scale bar, and the labels' middle over the bar is one named constant. --- crates/render/src/layout.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 47abf0fb19..41ed6f6466 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -272,7 +272,9 @@ pub struct TextLoss { /// the line past the 200 drawn ([`wrapped`]). pub at_least: bool, /// Some of them are left out below its box, among the first 200: a taller box or a smaller - /// text draws them. + /// text draws them. For a scale bar, its box is too small for its labels and bar + /// ([`BarLayout::cramped`]): its bar drawn below it, or its end label past its side or over + /// its first label. pub below_box: bool, /// Some of them come past the 200 lines a text draws, whatever its box: only splitting the /// text draws them. @@ -2204,6 +2206,8 @@ const BAR_INSET: f32 = 2.0; /// How far a scale bar label's digits rise above the middle of their line, in label sizes: what /// the room over the bar holds. const BAR_LABEL_RISE: f32 = 0.5; +/// How far the middle of a scale bar's labels' line sits over the bar's top, in label sizes. +const BAR_LABEL_MID: f32 = 0.7; /// What a scale bar counts: its whole length in its unit, that unit in metres and its abbreviation. #[derive(Clone, Copy, Debug, PartialEq)] @@ -2274,7 +2278,8 @@ struct BarLayout { label_y: f32, labels: Vec<(f32, String)>, /// Its box is too small for its labels and bar: too short, the bar drawn below it, or too - /// narrow, an end label reaching past its side further than the default size's does. + /// narrow, an end label reaching past its side further than the default size's does or, above + /// the default size, over its first label. cramped: bool, } @@ -2296,13 +2301,13 @@ fn bar_layout(r: Rect, ground_per_pt: f64, bar: (ScaleUnits, usize, f64), ppp: f // short for both has the bar below it ([`BarLayout::cramped`]). At the default size the bar is // where it always was. let px = SCALE_BAR_TEXT_PT as f32 * k * 1.33 * ppp as f32; - let rise = |px: f32| px * (0.7 + BAR_LABEL_RISE); + let rise = |px: f32| px * (BAR_LABEL_MID + BAR_LABEL_RISE); let at = r.y() + r.height() * 0.62 - bar_h / 2.0; let top = r.y().min(at - rise(SCALE_BAR_TEXT_PT as f32 * 1.33 * ppp as f32)); let first = bar_label(total, divisions, 0, abbr, comma); let (x0, y0) = (r.x() + bar_inset(&first, k, ppp as f32), at.max(top + rise(px))); let seg = len / divisions as f32; - let label_y = y0 - px * 0.7; + let label_y = y0 - px * BAR_LABEL_MID; let gap = 3.0 * k * ppp as f32; let all: Vec<(f32, String)> = (0..=divisions).map(|i| (x0 + seg * i as f32, bar_label(total, divisions, i, abbr, comma))).collect(); let half = |s: &str| text::measure(s, px / 1.33, false) / 2.0; @@ -2324,7 +2329,14 @@ fn bar_layout(r: Rect, ground_per_pt: f64, bar: (ScaleUnits, usize, f64), ppp: f let half8 = |s: &str| text::measure(s, SCALE_BAR_TEXT_PT as f32 * ppp as f32, false) / 2.0; let x8 = r.x() + bar_inset(&first, 1.0, ppp as f32); let past_side = labels.last().is_some_and(|(x, s)| x + half(s) > r.right().max(x8 + (x - x0) + half8(s)) + 0.01); - let cramped = y0 + bar_h > r.bottom() + 0.01 || past_side; + // Its end label is drawn as its first is, whatever the room: above the default size, running + // into the first, the box is too narrow for them too. + let clash = k > 1.0 + && match (labels.first(), labels.last()) { + (Some((fx, fs)), Some((ex, es))) if labels.len() > 1 => ex - half(es) < fx + half(fs), + _ => false, + }; + let cramped = y0 + bar_h > r.bottom() + 0.01 || past_side || clash; Some(BarLayout { x0, y0, len, bar_h, px, label_y, labels, cramped }) } @@ -8509,6 +8521,20 @@ Elaboração: RustGIS", |pt: f64| bar_layout(Rect::from_xywh(0.0, 0.0, 90.0, 400.0).unwrap(), 100.0, (ScaleUnits::Auto, 4, pt), 1.0, false).unwrap(); let (n8, n40) = (narrow(8.0), narrow(40.0)); assert!(!n8.cramped && n40.cramped && n40.y0 + n40.bar_h < 400.0 && n40.len == n8.len, "{} {}", n40.y0, n40.len); + // Its end label, always drawn as its first is, running into the first above the default + // size reaches past the box's side too: the box is too small, and said. + for pt in [9.0, 12.0, 20.0, 40.0, 100.0] { + for w in (20..400).step_by(7) { + let Some(b) = bar_layout(Rect::from_xywh(0.0, 0.0, w as f32, 400.0).unwrap(), 100.0, (ScaleUnits::Auto, 4, pt), 1.0, false) + else { + continue; + }; + let half = |s: &str| text::measure(s, b.px / 1.33, false) / 2.0; + let ((fx, fs), (ex, es)) = (&b.labels[0], &b.labels[b.labels.len() - 1]); + let clash = b.labels.len() > 1 && ex - half(es) < fx + half(fs); + assert!(!clash || b.cramped, "{pt} pt in {w} px: its end label over its first, not said"); + } + } // A publication's short box (17 pixels): the default reaches over it, as it always did, // and a larger size reaches no higher. let (short8, short12) = (in_box(17.0, 8.0), in_box(17.0, 12.0)); From 3687374975c3f88f398a483c84b8db3b166f0bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 22 Nov 2026 10:01:42 -0500 Subject: [PATCH 4/5] Scale bar cut-off advice follows the count; an imported layout is read once (#2698) The export's advice for scale bars alone says to move them inside the page or make their boxes larger, in the singular or the plural. A layout.import's layout is read once for all the keys of a capability rather than once per key. kept_labels says a label too large across is left out at any size, and the inset test states the default's reach. --- crates/engine/src/project_capabilities.rs | 39 +++++++++++------------ crates/render/src/layout.rs | 25 +++++++++------ 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index dc208cc0cb..7e07cfa675 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -213,9 +213,7 @@ fn command_uses_text_size(id: &str, params: &Value) -> bool { "layout.update" => { params.get("textSize").is_some_and(|v| !v.is_null()) || params.get("grid").is_some_and(|g| sized(g, "labelSize", GRID_LABEL_PT)) } - "layout.import" => { - ["\"text_size\"", "\"label_size\""].iter().any(|key| imported_layout_uses(params, key, |e| e.kind.has_custom_text_size())) - } + "layout.import" => imported_layout_uses(params, &["\"text_size\"", "\"label_size\""], |e| e.kind.has_custom_text_size()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_text_size)), _ => false, } @@ -227,7 +225,7 @@ fn command_uses_text_size(id: &str, params: &Value) -> bool { fn command_uses_frame_border(id: &str, params: &Value) -> bool { match id { "layout.update" => params.get("border").is_some_and(|b| !b.is_null()), - "layout.import" => imported_layout_uses(params, "\"border_", |e| e.kind.has_custom_border()), + "layout.import" => imported_layout_uses(params, &["\"border_"], |e| e.kind.has_custom_border()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_frame_border)), _ => false, } @@ -252,7 +250,7 @@ fn command_uses_blank_paper(id: &str, params: &Value) -> bool { fn command_uses_element_background(id: &str, params: &Value) -> bool { match id { "layout.addElement" | "layout.update" => params.get("background").is_some_and(|b| !b.is_null()), - "layout.import" => imported_layout_uses(params, "\"background\"", |e| e.background.is_some()), + "layout.import" => imported_layout_uses(params, &["\"background\""], |e| e.background.is_some()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_background)), _ => false, } @@ -263,7 +261,7 @@ fn command_uses_element_background(id: &str, params: &Value) -> bool { /// and `layout.update`, so such a command fails there instead of losing the ink. fn command_uses_element_ink(id: &str, params: &Value) -> bool { match id { - "layout.import" => imported_layout_uses(params, "\"color\"", |e| e.kind.ink().is_some()), + "layout.import" => imported_layout_uses(params, &["\"color\""], |e| e.kind.ink().is_some()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_ink)), _ => false, } @@ -280,7 +278,7 @@ fn wraps(e: &rustgis_map::layout::Element) -> bool { fn command_uses_element_wrap(id: &str, params: &Value) -> bool { match id { "layout.addElement" | "layout.update" => params.get("wrap").and_then(crate::cmd::bool_value) == Some(true), - "layout.import" => imported_layout_uses(params, "\"wrap\"", wraps), + "layout.import" => imported_layout_uses(params, &["\"wrap\""], wraps), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_wrap)), _ => false, } @@ -310,9 +308,7 @@ fn command_uses_chart_data(id: &str, params: &Value) -> bool { || params.get("bars").and_then(Value::as_u64).is_some_and(|n| n != CHART_BARS as u64) || params.get("other").and_then(crate::cmd::bool_value) == Some(true) } - "layout.import" => ["\"group\"", "\"statistic\"", "\"order\"", "\"bars\"", "\"other\""] - .iter() - .any(|key| imported_layout_uses(params, key, charts_data)), + "layout.import" => imported_layout_uses(params, &["\"group\"", "\"statistic\"", "\"order\"", "\"bars\"", "\"other\""], charts_data), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_data)), _ => false, } @@ -343,9 +339,11 @@ fn command_uses_chart_look(id: &str, params: &Value) -> bool { && (only_chart() || text("orientation").as_deref() == Some("vertical") || params.get("title").is_some_and(|t| !t.is_null())) } "layout.update" => only_chart(), - "layout.import" => ["\"orientation\"", "\"values\"", "\"decimals\"", "\"title\"", "\"bar_color\"", "\"label_align\""] - .iter() - .any(|key| imported_layout_uses(params, key, charts_look)), + "layout.import" => imported_layout_uses( + params, + &["\"orientation\"", "\"values\"", "\"decimals\"", "\"title\"", "\"bar_color\"", "\"label_align\""], + charts_look, + ), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_look)), _ => false, } @@ -366,7 +364,7 @@ fn command_uses_chart_classes(id: &str, params: &Value) -> bool { params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && classes() } "layout.update" => classes(), - "layout.import" => imported_layout_uses(params, "\"classes\"", charts_classes), + "layout.import" => imported_layout_uses(params, &["\"classes\""], charts_classes), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_classes)), _ => false, } @@ -386,7 +384,7 @@ fn command_uses_chart_stack(id: &str, params: &Value) -> bool { params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && stack() } "layout.update" => stack(), - "layout.import" => imported_layout_uses(params, "\"stack\"", charts_stack), + "layout.import" => imported_layout_uses(params, &["\"stack\""], charts_stack), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_stack)), _ => false, } @@ -394,14 +392,15 @@ fn command_uses_chart_stack(id: &str, params: &Value) -> bool { /// Whether a `layout.import` carries its layout as `"text"` with an element `uses`: an older /// reader reads it without the keys it doesn't know. One read from a `"path"` is a layout file, -/// which carries no format header yet (#2277). Every save asks, so a text without the feature's -/// `key` (a saved layout writes it only when set) isn't read through, unless it has a backslash: -/// JSON may spell the key with escapes (`"\u0063olor"`). -fn imported_layout_uses(params: &Value, key: &str, uses: fn(&rustgis_map::layout::Element) -> bool) -> bool { +/// which carries no format header yet (#2277). Every save asks, so a text with none of the +/// feature's `keys` (a saved layout writes them only when set) isn't read through, unless it has a +/// backslash: JSON may spell a key with escapes (`"\u0063olor"`). It is read once, whatever its +/// keys. +fn imported_layout_uses(params: &Value, keys: &[&str], uses: fn(&rustgis_map::layout::Element) -> bool) -> bool { params .get("text") .and_then(Value::as_str) - .filter(|t| t.contains(key) || t.contains('\\')) + .filter(|t| keys.iter().any(|key| t.contains(key)) || t.contains('\\')) .and_then(|t| serde_json::from_str::(t).ok()) .is_some_and(|l| l.elements.iter().any(uses)) } diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 41ed6f6466..2a39a07121 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -456,8 +456,10 @@ pub fn clipped_text_notice(layout: &Layout, clipped: &[(u32, String)]) -> Option .collect::>(); // Scale bars alone: their labels are no text to break (#2581). let bars = clipped.iter().all(|(id, _)| matches!(layout.element(*id).map(|e| &e.kind), Some(ElementKind::ScaleBar { .. }))); - let advice = if bars { - "make the scale bar larger or move it inside the page, or reduce its text size" + let advice = if bars && clipped.len() == 1 { + "move it inside the page or make its box larger, or reduce its text size" + } else if bars { + "move them inside the page or make their boxes larger, or reduce their text size" } else if wraps.contains(&Some(false)) { "resize or move the text, reduce its size, add line breaks, or turn on Wrap to the box" } else if wraps.iter().all(|w| *w == Some(true)) { @@ -2018,11 +2020,12 @@ fn mgrs_squares( /// text, a meridian's or not) on a frame `size` pixels, `label_h` tall: away from the corners (all /// of a label, `along` its edge, fits, and none at a side's ends by a top or bottom label), within /// the frame across (how far it reaches in from its edge, `across`, at most the frame's other side: -/// a label larger than a short frame isn't cut by it), and none over another. Meridians are -/// labelled along the top and bottom and parallels along the sides first; a line that leaves the -/// frame through the other edges (a slanting meridian on a projected map) is labelled there only -/// well clear of the labels already set on that edge. Their room from the corners and between them -/// is in `unit`s, a point at the labels' default size in pixels, so it follows their size. +/// one reaching further in is left out, at any size, rather than cut by the frame), and none over +/// another. Meridians are labelled along the top and bottom and parallels along the sides first; a +/// line that leaves the frame through the other edges (a slanting meridian on a projected map) is +/// labelled there only well clear of the labels already set on that edge. Their room from the +/// corners and between them is in `unit`s, a point at the labels' default size in pixels, so it +/// follows their size. /// A label left out for want of room is thinning, as on any map, not a loss to report: the grid /// still shows its lines, and the user guide says labels go where there is room. fn kept_labels( @@ -8489,9 +8492,11 @@ Elaboração: RustGIS", let (small, big) = (at(SCALE_BAR_TEXT_PT), at(16.0)); assert_eq!((small.len, small.bar_h, small.y0), (big.len, big.bar_h, big.y0), "the bar keeps its size"); assert!((big.px - 2.0 * small.px).abs() < 1e-3, "{} against {}", big.px, small.px); - // Its inset follows, and more: its first label reaches no further left than at 8 points. + // Its inset follows, and more: its first label reaches no further left than at 8 points, + // where its "0" pokes a little past the box's side. let reach = |b: &BarLayout| b.x0 - text::measure(&b.labels[0].1, b.px / 1.33, false) / 2.0; - assert!(big.x0 >= 2.0 * BAR_INSET - 1e-3 && (reach(&big) - reach(&small).min(0.0)).abs() < 1e-3, "{} {}", big.x0, reach(&big)); + assert!(reach(&small) < 0.0, "{}", reach(&small)); + assert!(big.x0 >= 2.0 * BAR_INSET - 1e-3 && (reach(&big) - reach(&small)).abs() < 1e-3, "{} {}", big.x0, reach(&big)); let over = |b: &BarLayout| b.y0 - b.label_y; assert!((over(&big) - 2.0 * over(&small)).abs() < 1e-3, "its room over the bar follows"); // The default draws the bar as before: an 8-point label 1.33 pixels a point, 2 pixels in. @@ -8564,7 +8569,7 @@ Elaboração: RustGIS", } assert_eq!(clipped_text(&l, &maps, 96.0, &info), vec![(bar, "Scale Bar".into())]); let note = clipped_text_notice(&l, &clipped_text(&l, &maps, 96.0, &info)).unwrap(); - assert!(note.contains("make the scale bar larger"), "{note}"); + assert!(note.ends_with("move it inside the page or make its box larger, or reduce its text size"), "{note}"); let loss = text_loss(&l, &maps, 96.0, &info, bar).unwrap(); assert!(loss.below_box && scale_bar_fit_notices(std::slice::from_ref(&loss))[0].contains("its box is too small"), "{loss:?}"); // A tall box holds them at 20 points; off the page's right edge, its last label is cut. From 7fb3556d8bc2ff3e2d8e597005f66fbad5b46729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 22 Nov 2026 10:51:09 -0500 Subject: [PATCH 5/5] Layouts guide: the scale bar and grid follow-ups (#2698) Grid labels reaching further in than the frame is across are left out; a scale bar's move and an end label run into the first are said; a locked scale bar refuses a new text size, null included. --- docs/user-guide/layouts.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 2a2137ea06..adbc2684ca 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -227,7 +227,8 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu the left and right; the bottom and left to start with), the side labels horizontal or running along the edge, and their **Label size** (`"labelSize"`, 4 to 100 points, 8 by default; their room from the frame's edge and a measured grid's MGRS square names follow in proportion, - labels that would then touch are left out, and `null` gives the default back). From a command: + labels that would then touch, or that reach further in than the frame is across, are left out, + and `null` gives the default back). From a command: `layout.addElement={"kind":"grid","step":0.5,"style":"ticks","format":"dms","edges":["top","bottom","left","right"],"labelOrientation":"vertical"}` (given again, it changes only the options it names), or `layout.update` with `"grid": {…}` (`false` removes the grid). On a map on the globe the lines are drawn on the @@ -278,10 +279,11 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu length and height and moves down, and right, in its box to make room for larger labels, which reach no further past its top and left than the default size's, and `null` gives the default back) (`"style":"line","units":"miles","divisions":2,"color":"#1F4E79","textSize":12`). A box too - small for its labels and bar (too short, which draws the bar below it, or too narrow for its last - label), or a label that runs off the page, is said when its size, text size, units or divisions - change or its frame is drawn at another scale, and an export reports it as cut off, as it does a - text. A legend's + small for its labels and bar (too short, which draws the bar below it, too narrow for its last + label, or so narrow that a larger last label runs into the first), or a label that runs off the + page, is said when its place, size, text size, units or divisions change or its frame is drawn + at another scale, and an export reports it as cut off, as it does a text. A locked scale bar + refuses a new text size, `null` included. A legend's parts: - **What a legend lists**: what its frame draws: only layers with a feature at the frame's scale (through their definition query, display filters and scale range) and, for Unique