Repository navigation
feat(layout): a publication's scale bar box the size of its bar (#1379) #1826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3727316
e900ca3
c11b877
dec485f
6fc049d
cceaa64
76405b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1825,7 +1825,24 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { | |
| sizes = None; | ||
| warnings.push("a WMS or WMTS layer's legend picture is still loading: the legend has a fixed size to fit by hand".into()); | ||
| } | ||
| let (sw, sh, nh) = ((fw * 0.3).max(25.0), if sheet { 8.0 * k } else { 6.0 }, pitch(small_pt) + 0.5); | ||
| let (sh, nh) = (if sheet { 8.0 * k } else { 6.0 }, pitch(small_pt) + 0.5); | ||
| // The scale bar's box at a scale: the size of its bar and labels and of the scale under them, | ||
| // with the padding (#1379). The bar is the one a box 30 % of the frame's width (25 mm at least) | ||
| // would draw, which sets its length and divisions. The box is the narrowest that draws that same | ||
| // bar: the renderer gives a bar at most 85 % of its box, so the box is at least the bar's length | ||
| // over 0.85. | ||
| let bar_kind = match ElementKind::scale_bar(frame) { | ||
| ElementKind::ScaleBar { units, divisions, .. } => Some((units, divisions)), | ||
| _ => None, | ||
| }; | ||
| let scale_box = |ground_per_pt: f64, scale: f64| { | ||
| let text = ("{scale}", small_pt, false); | ||
| let w = bar_kind.map_or((fw * 0.3).max(25.0), |bar| scale_box_width(ground_per_pt, scale, fw, bar, comma, text)); | ||
| (w, sh + nh) | ||
| }; | ||
| let ground_per_pt = |s: &Session, center: Coord, scale: f64| { | ||
| s.map().ground_res_at(&rustgis_map::layout::frame_camera(s.map(), center, scale, 0.0, 0.0)) | ||
| }; | ||
| let pad = 2.0; | ||
| // The surrounds to place: what each is and its size (the arrangement gives its corner). | ||
| let mut surrounds: Vec<(Surround, f64, f64)> = Vec::new(); | ||
|
|
@@ -1846,7 +1863,8 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { | |
| if content_h > 0.0 && !legend_in_column { | ||
| surrounds.push((Surround::Legend, (content_w + 2.0 * pad).min(fw * 0.45), (content_h + 2.0 * pad).min(fh * 0.6))); | ||
| } | ||
| surrounds.push((Surround::Scale, sw, sh + nh)); | ||
| // Its width is set once the frame's centre and scale are settled, below; nothing reads it before. | ||
| surrounds.push((Surround::Scale, 0.0, sh + nh)); | ||
| // A corner's rectangle for a surround `w` × `h`. | ||
| let at = |corner: usize, w: f64, h: f64| { | ||
| let x = if corner.is_multiple_of(2) { fx + left } else { fx + fw - right - w }; | ||
|
|
@@ -1899,12 +1917,11 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { | |
| bands | ||
| }; | ||
| let size_of = |kind: Surround| surrounds.iter().find(|x| x.0 == kind).map(|x| (x.1, x.2)); | ||
| let (locator_side, arrow_size, scale_size) = | ||
| (size_of(Surround::Locator).map(|x| x.0), size_of(Surround::Arrow), size_of(Surround::Scale)); | ||
| let (locator_side, arrow_size) = (size_of(Surround::Locator).map(|x| x.0), size_of(Surround::Arrow)); | ||
| // The fewest columns from `from` up in which the surrounds group in a band around `clip`, with | ||
| // their places. | ||
| let grouped = |clip: Rect, from: usize, known: &[(f64, f64); MAX_LEGEND_COLUMNS]| { | ||
| let (arrow, scale_bar) = (arrow_size?, scale_size?); | ||
| // their places; the scale bar's box `scale_bar` is the one at the scale tried. | ||
| let grouped = |clip: Rect, from: usize, known: &[(f64, f64); MAX_LEGEND_COLUMNS], scale_bar: (f64, f64)| { | ||
| let arrow = arrow_size?; | ||
| (from..known.len()).find_map(|i| { | ||
| bands_around(clip) | ||
| .iter() | ||
|
|
@@ -2061,8 +2078,10 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { | |
| } | ||
| let room = free_room(&moved); | ||
| let Some(n) = need(room) else { continue }; | ||
| // Under a veil the whole group has to fit, not the legend alone. | ||
| let Some(n) = (if veil { grouped(clip, n, here).map(|(i, _)| i) } else { Some(n) }) else { continue }; | ||
| // Under a veil the whole group has to fit, not the legend alone, the scale bar's box | ||
| // as drawn there. | ||
| let in_group = |s: &Session| grouped(clip, n, here, scale_box(ground_per_pt(s, there, tried), tried)); | ||
| let Some(n) = (if veil { in_group(s).map(|(i, _)| i) } else { Some(n) }) else { continue }; | ||
| if found.is_none_or(|(fewest, most)| n < fewest || (n == fewest && room.0 * room.1 > most)) { | ||
| found = Some((n, room.0 * room.1)); | ||
| (center, scale, coverage, map_shift) = (there, tried, Some(moved), (dx, dy)); | ||
|
|
@@ -2101,6 +2120,11 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { | |
| } | ||
| } | ||
| } | ||
| // The scale bar's box as the frame is drawn, moved or at another scale or not. | ||
| let bar_box = scale_box(ground_per_pt(s, center, scale), scale); | ||
| if let Some(entry) = surrounds.iter_mut().find(|x| x.0 == Surround::Scale) { | ||
| (entry.1, entry.2) = bar_box; | ||
| } | ||
| // Its cells summed from the top left (a summed-area table), so a rectangle's count is four | ||
| // lookups, however large: the search off the corners tries thousands. | ||
| let sums: Option<(usize, usize, Vec<u32>)> = coverage.as_ref().map(|(gw, gh, cells)| { | ||
|
|
@@ -2247,7 +2271,8 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { | |
| // it no room. | ||
| let mut arrangement_used = arrangement_name(homes); | ||
| if let (Some(clip), Some(known)) = (veiled, sizes) | ||
| && let Some((i, (legend_place, locator_place, arrow_place, scale_place))) = grouped(clip, 0, &known) | ||
| && let Some((i, (legend_place, locator_place, arrow_place, scale_place))) = | ||
| grouped(clip, 0, &known, scale_box(ground_per_pt(s, center, scale), scale)) | ||
| { | ||
| legend_cols = i + 1; | ||
| (cw, ch) = known[i]; | ||
|
|
@@ -2415,9 +2440,9 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { | |
| } | ||
| if let Some((sx, sy, w, h)) = spot_of(Surround::Scale) { | ||
| placed.push(("scale bar", (sx, sy, w, h))); | ||
| l.add("Scale Box", (sx, sy, sw, sh + nh), surround_box(paper)); | ||
| l.add("Scale Bar", (sx, sy, sw, sh), ElementKind::scale_bar(frame)); | ||
| l.add("Scale", (sx, sy + sh, sw, nh), pub_text("{scale}".into(), small_pt, ink, false, TextAlign::Center)); | ||
| l.add("Scale Box", (sx, sy, w, sh + nh), surround_box(paper)); | ||
| l.add("Scale Bar", (sx, sy, w, sh), ElementKind::scale_bar(frame)); | ||
| l.add("Scale", (sx, sy + sh, w, nh), pub_text("{scale}".into(), small_pt, ink, false, TextAlign::Center)); | ||
| } | ||
| // What placing couldn't avoid: a surround over the map's drawing while a clearly freer corner | ||
| // went to another (more surrounds than free corners), or two overlapping (a frame too small | ||
|
|
@@ -3863,12 +3888,115 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { | |
| if let Some(text) = p.get("grid").and_then(|grid| grid.get("crs")).and_then(Value::as_str) { | ||
| super::say_crs_note(s, text); | ||
| } | ||
| // A frame drawn at another scale or width: a publication's scale bar box measured again. | ||
| let frame = s.project.layouts.get(li).and_then(|l| l.element(id)).map(|e| &e.kind); | ||
| if matches!(frame, Some(ElementKind::MapFrame { .. })) && ["scale", "w"].iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gap (~50%): the box isn't measured again when its text or bar is restyled. The refit runs only for a map frame's
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aligned in 76405b7, logic unchanged: |
||
| refit_scale_box(s, li, id); | ||
| } | ||
| if let Some(w) = font_told { | ||
| return Ok(json!({"warning": w})); | ||
| } | ||
| Ok(Value::Null) | ||
| } | ||
|
|
||
| /// The width (mm) of a publication's scale bar box (#1379), for a frame `frame_w` mm wide at | ||
| /// `scale` and `ground_per_pt` ground metres a point: the bar a box 30 % of the frame's width (25 | ||
| /// mm at least) would draw sets its length and divisions, and the box is the narrowest that draws | ||
| /// that same bar (at least its length over the 85 % of a box the renderer gives a bar), its last | ||
| /// label included, and holds the text written under it: `text` (its `{scale}` filled), in | ||
| /// `text_pt` points, bold or not. | ||
| fn scale_box_width(ground_per_pt: f64, scale: f64, frame_w_mm: f64, bar: (ScaleUnits, usize), comma: bool, text: (&str, f64, bool)) -> f64 { | ||
| let (text, text_pt, bold) = text; | ||
| let (widest, pt_per_mm) = ((frame_w_mm * 0.3).max(25.0), rustgis_map::layout::PT_PER_MM); | ||
| let bar_w = crate::render::layout::scale_bar_width_pt(ground_per_pt, widest * pt_per_mm, bar, comma, 1.5 * pt_per_mm) | ||
| .map_or(widest, |w| w / pt_per_mm); | ||
| let written = text.replace("{scale}", &crate::render::layout::scale_ratio_text(scale, comma)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug (medium confidence, ~60%): the text is measured with only
Failure: a Scale text of This is new: the earlier thread asked for expanded content to be measured, and the reply covers only
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 6fc049d: the refit fills the text's tokens with the function the page draws with (
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| bar_w.max(scale_text_mm(&written, text_pt, bold)).min(widest) | ||
| } | ||
|
|
||
| /// The width (mm) a scale bar box gives the text under its bar, its tokens filled: its longest | ||
| /// line in `text_pt` points, bold or not, and a millimetre each side. | ||
| fn scale_text_mm(text: &str, text_pt: f64, bold: bool) -> f64 { | ||
| text.lines().map(|line| crate::render::report::width_mm(line, text_pt as f32, bold)).fold(0.0, f64::max) + 2.0 | ||
| } | ||
|
|
||
| /// 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"]; | ||
|
|
||
| /// A publication's scale bar box measured again after its frame's scale or width changed (#1379): | ||
| /// 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 | ||
| /// frame they are nearer. A layout without them, with them moved apart, with that text spaced out | ||
| /// or in another font, naming a map series page (which differs from page to page), or of a frame | ||
| /// other than the first (whose map the page fills the text from) is left as it is; so is a text | ||
| /// longer than the widest box a publication gives (a box widened by hand for it keeps its width). | ||
| /// A change to the text or the bar alone doesn't measure the box again; the next change of the | ||
| /// frame's scale or width does. A box that would then cover the legend, locator or north arrow, | ||
| /// which it didn't, stays as it was, which is said. | ||
| fn refit_scale_box(s: &mut Session, li: usize, frame: u32) { | ||
| let Some(l) = s.project.layouts.get(li) else { return }; | ||
| let Some(fr) = l.element(frame) else { return }; | ||
| let ElementKind::MapFrame { map, center, scale, .. } = &fr.kind else { return }; | ||
| let Some(m) = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map)) else { return }; | ||
| let ground_per_pt = m.ground_res_at(&rustgis_map::layout::frame_camera(m, *center, *scale, 0.0, 0.0)); | ||
| 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, units, divisions, .. }, | ||
| ElementKind::Text { text: written, size, bold, letter_spacing, font, font_name, .. }, | ||
| ) = (&bar.kind, &text.kind) | ||
| else { | ||
| return; | ||
| }; | ||
| if *letter_spacing != 0.0 | ||
| || *font != FontFamily::Sans | ||
| || font_name.is_some() | ||
| || (l.series.is_some() && written.contains("{page")) | ||
| || l.first_frame() != Some(frame) | ||
| { | ||
| return; | ||
| } | ||
| // Its tokens filled as the page draws them, `{scale}` from the frame. | ||
| let info = crate::render::layout::PageInfo { page: 1, pages: 1, page_name: String::new() }; | ||
| let cam = rustgis_map::layout::frame_camera(m, *center, *scale, 0.0, 0.0); | ||
| let filled = crate::render::layout::expand(written, l, &info, Some((m, cam))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possible mismatch with the drawn text in a layout with more than one map frame (confidence ~35%). This measures Suggested fix: skip the refit when the layout has more than one map frame, or measure with the same first-frame choice the renderer makes.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in cceaa64 with the one-line route: the refit runs only when the scale bar's frame is the layout's first (
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| let together = [bar, text].iter().all(|e| (e.x - boxed.x).abs() < 1e-6 && (e.w - boxed.w).abs() < 1e-6 && !e.locked); | ||
| if *of != frame || !together || boxed.locked { | ||
| return; | ||
| } | ||
| // A text longer than the widest box a publication gives: a width set by hand for it is kept. | ||
| if scale_text_mm(&filled, *size, *bold) > (fr.w * 0.3).max(25.0) { | ||
| return; | ||
| } | ||
| let w = scale_box_width(ground_per_pt, *scale, fr.w, (*units, *divisions), l.decimal_comma, (&filled, *size, *bold)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug (about 50% confidence): a width set by hand is still overwritten when the text fits. The new guard at line 3969 keeps a hand-set width only when the text is wider than the widest box. When the text fits, this line computes the automatic width and the loop below writes it to all three elements. So a box the user widened to 40 mm, for text that fits in 25 mm, is reset to the automatic width by the next This is the point of the still-open thread on the earlier One possible fix: refit only when the current width is the automatic one for the previous scale.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #1826 was merged before this was pushed, so it's in #1867: the box is measured again only while its width is still the automatic one from what it was measured from before the change (frame and text); a width set by hand, below the widest too, is kept. A change to the text under the bar refits by the same rule. Tested. |
||
| let x = if boxed.x + boxed.w / 2.0 > fr.x + fr.w / 2.0 { boxed.x + boxed.w - w } else { boxed.x }; | ||
| let covers = |(x, w): (f64, f64), e: &rustgis_map::layout::Element| { | ||
| x < e.x + e.w && e.x < x + w && boxed.y < e.y + e.h && e.y < boxed.y + boxed.h | ||
| }; | ||
| let newly = l | ||
| .elements | ||
| .iter() | ||
| .filter(|e| PLACED_OVER_MAP.contains(&e.name.as_str()) && e.visible) | ||
| .find(|e| covers((x, w), e) && !covers((boxed.x, boxed.w), e)) | ||
| .map(|e| e.name.to_lowercase().replace(" box", "")); | ||
| if let Some(what) = newly { | ||
| s.notify( | ||
| format!("the scale bar's box would now cover the {what}, so it is left as it was: move or widen it by hand, or make the layout again with layout.publication"), | ||
| false, | ||
| ); | ||
| return; | ||
| } | ||
| let ids = [boxed.id, bar.id, text.id]; | ||
| if let Some(l) = s.project.layouts.get_mut(li) { | ||
| for id in ids { | ||
| if let Some(e) = l.element_mut(id) { | ||
| (e.x, e.w) = (x, w); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug (~65%): a refit overwrites a width the user set, and can clip text. This is the point CodeRabbit raised on the earlier thread (its line has moved, the problem is still there), and no reply covers it.
Possible fix: keep the current width when the text needs more than
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 76405b7: when the text under the bar, its tokens filled, needs more than the widest box a publication gives (30 % of the frame, 25 mm at least), the refit leaves the box alone, so a box widened by hand for it keeps its width. The test widens the three elements to 120 mm for a long text, changes the frame's scale, and checks the width stays. |
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Points a frame at an extent. | ||
| fn set_frame(s: &mut Session, li: usize, frame: u32, e: &Extent) -> Result<(), String> { | ||
| let l = s.project.layouts.get(li).ok_or("no layout")?; | ||
|
|
@@ -3924,6 +4052,7 @@ fn frame_zoom(s: &mut Session, p: &Value) -> CmdResult { | |
| } | ||
| o => return Err(format!("unknown zoom '{o}'")), | ||
| } | ||
| refit_scale_box(s, li, frame); | ||
| Ok(json!({"element": frame})) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug (medium confidence, ~55%): the Scale Box is sized once at publication and not re-measured when the frame's scale changes later.
The box width now depends on the scale (bar length, its last label, and the scale text
1:…). But the frame's scale can change after the layout is built, and nothing re-sizes the Scale surround then:layout.frameZoom(frame_zoom, toview/full/selection/layer) setscenterandscaledirectly (set_frame, ~line 3909 and 3933).layout.updateaccepts"scale"for a map frame (~line 3853).The renderer then draws the bar with
bar_planat the new ground resolution inside the old width. The last label's half-width can overflow the 1.5 mm pad the box was sized with. The scale text can also overflow the box, becausetext_wonly added 2 mm for the old ratio text (e.g.1:2.000.000→1:10.000.000). Nothing reports this.Possible fixes: re-run
scale_boxinframe_zoom/updateafter a scale change, or document it in the Limits next to the restyle case. The PR's "Limits" section mentions onlylayout.updaterestyling, not frame scale changes.I did not render this case, so the confidence is moderate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in c11b877:
layout.updatewith a frame's"scale"or"w", andlayout.frameZoom, measure a publication's scale bar box again with the same functionlayout.publicationuses (scale_box_width), from the bar's own units and divisions, so a restyled bar is measured as it is. The box keeps the side of the frame it is nearer. Only the Scale Box, Scale Bar and Scale a publication lays out together are touched: one place, one width, none locked.publication_layouts_fit_their_pageschanges the frame to 1:40,000,000 and 1:1,500,000 and checks that the written scale fits in the box and that the three share one width.