Repository navigation
fix(layout): keep a scale bar's labels evenly spaced when they don't all fit #3499
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
Merged
jgregoriorincon
merged 3 commits into
opengeos:main
from
jgregoriorincon:fix/3490-scale-bar-even-labels
Dec 1, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
3f223e4
fix(layout): keep a scale bar's labels evenly spaced when they don't …
jgregoriorincon b27daca
fix(layout): say how a crowded scale bar's labels are thinned
jgregoriorincon c66c95a
test(layout): check a scale bar thins no more than it must
jgregoriorincon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| //! Which labels a scale bar keeps when they don't all fit (#3490). | ||
|
|
||
| use super::*; | ||
|
|
||
| /// The labels' spans of a bar whose divisions are 10 apart, each label `widths[j]` wide and | ||
| /// centred on its division. | ||
| fn spans(widths: &[f32]) -> Vec<(f32, f32)> { | ||
| widths.iter().enumerate().map(|(j, w)| (10.0 * j as f32 - w / 2.0, 10.0 * j as f32 + w / 2.0)).collect() | ||
| } | ||
|
|
||
| /// What [`scale_bar_kept`] keeps of `n` labels all `w` wide, with `before` divisions before zero. | ||
| fn even(n: usize, w: f32, before: usize) -> Vec<usize> { | ||
| scale_bar_kept(&spans(&vec![w; n]), before, 1.0) | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_scale_bar_keeps_every_label_that_fits() { | ||
| assert_eq!(even(5, 8.0, 0), [0, 1, 2, 3, 4]); | ||
| assert_eq!(even(2, 8.0, 0), [0, 1]); | ||
| } | ||
|
|
||
| /// The case of the issue: the third label clears its neighbours but the fourth would touch the | ||
| /// last, so every second one is labelled rather than all but the fourth. | ||
| #[test] | ||
| fn a_scale_bar_with_one_label_too_close_labels_every_second_division() { | ||
| assert_eq!(scale_bar_kept(&spans(&[2.0, 7.0, 7.0, 7.0, 16.0]), 0, 1.0), [0, 2, 4]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_scale_bar_with_no_room_for_a_step_widens_it() { | ||
| assert_eq!(even(5, 25.0, 0), [0, 4], "every second one still touches"); | ||
| assert_eq!(even(11, 15.0, 0), [0, 2, 4, 6, 8, 10]); | ||
| assert_eq!(even(11, 35.0, 0), [0, 5, 10]); | ||
| assert_eq!(even(8, 15.0, 0), [0, 7], "seven divisions have no even step between"); | ||
| assert_eq!(even(7, 25.0, 0), [0, 3, 6]); | ||
| } | ||
|
|
||
| /// The ends are drawn even where they touch each other. Above the default text size that makes the | ||
| /// box too narrow ([`BarLayout::cramped`]); at the default size the bar isn't flagged for it. | ||
| #[test] | ||
| fn a_scale_bar_always_keeps_its_ends() { | ||
| assert_eq!(even(5, 90.0, 0), [0, 4]); | ||
| assert_eq!(even(2, 90.0, 0), [0, 1]); | ||
| } | ||
|
|
||
| /// With divisions before zero the step counts from zero, and the first label is kept. | ||
| #[test] | ||
| fn a_scale_bars_step_counts_from_zero() { | ||
| // -2 -1 0 1 2 3 4 | ||
| assert_eq!(even(7, 15.0, 2), [0, 2, 4, 6]); | ||
| // -1 touches 0, which every step keeps: only the ends are left. | ||
| assert_eq!(scale_bar_kept(&spans(&[14.0, 14.0, 4.0, 4.0, 4.0, 4.0]), 1, 1.0), [0, 5]); | ||
| } | ||
|
|
||
| /// Over widths, text sizes, divisions and faces, the labels a bar places, read back from where | ||
| /// they stand along it, are its ends and, between them, every `step`th division from zero: none | ||
| /// touching but ends alone, and no finer step clear. | ||
| #[test] | ||
| fn a_scale_bar_draws_evenly_spaced_labels() { | ||
| let mut faces = vec![text::Face::default()]; | ||
| if crate::fonts::serif_available() { | ||
| let mut serif = ElementKind::scale_bar(0); | ||
| if let ElementKind::ScaleBar { font, .. } = &mut serif { | ||
| *font = rustgis_map::layout::FontFamily::Serif; | ||
| } | ||
| faces.push(BarOptions::of(&serif).unwrap().face); | ||
| } | ||
| // The bars that kept every label, some between their ends, and their ends alone. | ||
| let mut seen = [0usize; 3]; | ||
| for face in faces { | ||
| for (divisions, before) in [(4, 0), (6, 0), (7, 0), (10, 0), (4, 2), (6, 1)] { | ||
| for text_size in [8.0, 16.0, 40.0] { | ||
| for w in (60..500).step_by(11) { | ||
| let bar = BarOptions { divisions, divisions_before_zero: before, text_size, face, ..BarOptions::default() }; | ||
| let r = Rect::from_xywh(0.0, 0.0, w as f32, 400.0).unwrap(); | ||
| let Some(b) = bar_layout(r, 100.0, &bar, 1.0, false) else { continue }; | ||
| let seg = b.len / b.divs as f32; | ||
| let at: Vec<usize> = b.labels.iter().map(|(x, _)| ((x - b.x0) / seg).round() as usize).collect(); | ||
| let after = b.divs - b.before; | ||
| let step = |s: usize| (0..=b.divs).filter(move |&j| j == 0 || j == b.divs || j.abs_diff(b.before) % s == 0); | ||
| // Whether the labels of divisions `set` clear each other, each measured where it stands. | ||
| let clear = |set: &[usize]| { | ||
| let span = |j: usize| { | ||
| let each = b.total / b.divs as f64; | ||
| let mut s = bar_number(each * (j as f64 - b.before as f64), each, None, false); | ||
| if j == b.divs { | ||
| let abbr = bar_plan(100.0, f64::from(w), ScaleUnits::Auto).expect("a bar").abbr; | ||
| s = format!("{s} {abbr}"); | ||
| } | ||
| let (x, half) = (b.x0 + seg * j as f32, text::measure_in(&s, b.px / 1.33, face) / 2.0); | ||
| (x - half, x + half) | ||
| }; | ||
| let gap = 3.0 * (b.px / (SCALE_BAR_TEXT_PT as f32 * 1.33)); | ||
| set.windows(2).all(|p| span(p[0]).1 + gap <= span(p[1]).0) | ||
| }; | ||
|
jgregoriorincon marked this conversation as resolved.
Resolved
|
||
| let finer: Vec<Vec<usize>> = | ||
| (1..=after).filter(|s| after % s == 0).map(|s| step(s).collect()).take_while(|set| *set != at).collect(); | ||
| let case = format!("{divisions}+{before} at {text_size} pt in {w} px: {at:?}"); | ||
| let stepped = (1..=after).filter(|s| after % s == 0).any(|s| step(s).eq(at.iter().copied())); | ||
| assert!(stepped || at == [0, b.divs], "{case}"); | ||
| assert!(at == [0, b.divs] || clear(&at), "touching: {case}"); | ||
| assert!(finer.iter().all(|set| !clear(set)), "a finer step was clear: {case}"); | ||
| let ends_alone = usize::from(at.len() <= 2); | ||
| let thinned = usize::from(at.len() <= b.divs); | ||
| seen[thinned + ends_alone] += 1; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| assert!(seen.iter().all(|&n| n > 20), "every outcome is swept: {seen:?}"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.