Skip to content

fix(layout): keep a scale bar's labels evenly spaced when they don't all fit - #3499

Merged
jgregoriorincon merged 3 commits into
opengeos:mainfrom
jgregoriorincon:fix/3490-scale-bar-even-labels
Dec 1, 2026
Merged

jgregoriorincon merged 3 commits into
opengeos:mainfrom
jgregoriorincon:fix/3490-scale-bar-even-labels

Conversation

@jgregoriorincon

@jgregoriorincon jgregoriorincon commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When a layout scale bar's labels don't all fit, they were skipped from left to right: each was kept if it cleared the one before it and the last one. A bar of four divisions could read 0 2500 5000 ........ 10000 km, with 7500 left out although an even 0 5000 10000 km fits.

Now the bar labels its ends and every stepth division from zero, where step is the smallest even share of the divisions after zero at which no two labels touch (0 · 5000 · 10000 km; six divisions may read every second or every third). When no step fits, only the ends are labelled, which are always drawn, as before. With divisions before zero the step counts from zero and the first label is always drawn, so the stretch at the left end can be shorter than the step (-1 0 2 4); if that first label would touch zero, only the ends are left.

The rule is one pure function, scale_bar_kept in crates/render/src/layout/scale_bar.rs; the drawing, the cut-off check and the box measure all read the labels from there.

What changes: a bar that already shows all its labels draws as before. A crowded one can now show fewer labels than before, evenly spaced. The pinned hash of scale_bars_without_details_draw_as_before changes for this: of its 648 drawings, 186 differ, all of them ones where the old loop had already left labels out. With a prime number of divisions (3, 5, 7) there is no even step in between, so a crowded bar labels only its ends, where the old loop could keep a few unevenly spaced labels.

Reach: the change is in the page renderer, so it reaches the layout view, PNG, PDF and SVG exports, desktop, browser, command line and Python alike. No command, parameter, pane or saved field changes, so ribbon, search, Assistant, MCP and plugins have nothing new to show; undo is unaffected.

Testing

  • New tests in crates/render/src/layout/tests/scale_bar_labels.rs: the rule on its own spans (no fonts involved) and a sweep over widths, text sizes, divisions and faces. Four failed with the old loop.
  • Full local run: fmt, clippy, doctests, layers and schema pass; nextest 11,689 passed and 8 failed (7 in rustgis-geolibre, 1 symbology smoke test), in code this change doesn't touch. After the last wording commit: fmt, clippy and the 1,159 rustgis-render tests pass.
  • ui_shot before and after on the sample project (120 mm bar, Kilometers, 4 divisions, 12 to 18 pt, sans and serif): 12 and 14 pt export the same bytes, 16 and 18 pt differ.

UI evidence

A four-division bar, sans at 16 pt (top) and serif at 18 pt (bottom).

Before and after: a crowded scale bar

Related issue

Closes #3490

Summary by CodeRabbit

  • Improvements
    • Scale-bar labels are thinned at even intervals when space is limited. Labels at both ends remain visible, even when they touch.
  • Documentation
    • Updated the layout guide to explain how scale-bar labels are thinned.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The scale-bar layout now keeps both end labels and selects intermediate labels at evenly spaced intervals when labels do not fit. Tests cover label selection and rendering. The user guide describes the updated behavior.

Changes: Scale-bar label selection

Layer / File(s) Summary
Select and verify evenly spaced labels
crates/render/src/layout/scale_bar.rs, crates/render/src/layout/tests.rs, crates/render/src/layout/tests/scale_bar_labels.rs, docs/user-guide/layouts.md
bar_layout uses scale_bar_kept to retain both ends and choose intermediate labels at evenly spaced divisions. Tests cover fit, spacing, endpoints, and divisions before zero. The pinned-rendering hash and user guide reflect the updated behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

  • opengeos/RustGIS#1826: Its scale-bar box sizing uses the label set affected by this change.
  • opengeos/RustGIS#3073: It introduced the layout module structure and test path used by this change.
  • opengeos/RustGIS#2666: It introduced scale-bar details, including divisions before zero, which the label selection tests cover.

Suggested reviewers: giswqs

Merge Risk: Minimal · up to c66c9

Crowded scale-bar labels are now thinned at even intervals, keeping both ends. Only drawings where labels were already being omitted should change. No actionable merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: scale-bar labels are thinned to an even spacing when they do not all fit. It also has the conventional fix(layout) prefix and matches the summary, the objectives, and …
Linked Issues check Passed [#3490] The issue requires that, when scale-bar labels do not all fit, the kept labels are evenly spaced with both ends included (for example every second or fourth division). At head, `scale_bar_kept…
Out of Scope Changes check Passed The changed files serve the issue. scale_bar.rs holds the new label-selection function and its use in layout. tests.rs registers the new test module and updates the pinned hash of the scale-bar dr…
Description check Passed The description covers the change in detail. It explains the new rule: label the ends and every step-th division from zero, where the step is the smallest even share at which no two labels touch. It a…
Full details: Title check

Explanation

The title names the main change: scale-bar labels are thinned to an even spacing when they do not all fit. It also has the conventional fix(layout) prefix and matches the summary, the objectives, and issue #3490. The title is specific and is not vague.

Full details: Linked Issues check

Explanation

[#3490] The issue requires that, when scale-bar labels do not all fit, the kept labels are evenly spaced with both ends included (for example every second or fourth division). At head, scale_bar_kept in crates/render/src/layout/scale_bar.rs replaces the left-to-right greedy filter. The BarLayout.labels doc comment states the rule: ends always drawn, intermediate labels thinned evenly. The new tests in crates/render/src/layout/tests/scale_bar_labels.rs check the rule directly. For 4 divisions, even(5, 25.0, 0) gives [0, 4] and scale_bar_kept(&spans(&[2.0, 7.0, 7.0, 7.0, 16.0]), 0, 1.0) gives [0, 2, 4], so the issue's uneven pattern (0 1250 5000, label at division 1 and division 4 with 2500 omitted) cannot occur. The sweep test checks that each placed label set is the ends plus a valid step from zero, or the ends only, and that no finer step would clear. Drawing, cut-off and box measurement read labels from the same function (scale_bar_box_width_pt and bar_layout). Limit: the body of scale_bar_kept was truncated in my file read, so the verdict rests on the doc comments, the unit tests and the sweep test rather than a direct read of the function body. The PR description's reported test run is not independently re-run.

Full details: Out of Scope Changes check

Explanation

The changed files serve the issue. scale_bar.rs holds the new label-selection function and its use in layout. tests.rs registers the new test module and updates the pinned hash of the scale-bar drawing test, which changes by design because crowded bars now thin evenly. scale_bar_labels.rs holds tests for the new rule. docs/user-guide/layouts.md documents the thinning behaviour that this change adds. The PR description states that no command, parameter, pane or saved field changes. The 8 failing nextest cases are reported in rustgis-geolibre and one symbology smoke test. They lie outside this change, and I did not find a link between them and the changed files. Minor note: the raw summary says the pinned test is renamed to scale_bars_without_details_draw_as_pinned, while the PR description names scale_bars_without_details_draw_as_before. This is a documentation mismatch, not out-of-scope code. Limit: per-hunk diffs were not read in full, so this verdict relies on the file list, the summary and the head file reads.

Full details: Description check

Explanation

The description covers the change in detail. It explains the new rule: label the ends and every step-th division from zero, where the step is the smallest even share at which no two labels touch. It also explains the fallback to ends-only and the behavior when divisions precede zero. The Testing section lists new tests, the full local run results, and the ui_shot comparisons. The UI evidence section includes before and after screenshots. The Related issue section links Closes #3490. The template's required sections are all present and filled in, so the check passes.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit counts the marks in line,
And keeps the ends at either sign.
When crowded labels need more space,
Even steps set their spacing pace.
The guide now tells the same design.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 3a0bfc49-3dba-42a8-8261-2c84c6c0d82c
Commits

Reviewing files that changed from the base of the PR and between b27daca and c66c95a.

Files selected for processing: 3

  • crates/render/src/layout/tests.rs
  • crates/render/src/layout/tests/scale_bar_labels.rs
  • docs/user-guide/layouts.md

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

Comment thread crates/render/src/layout/tests.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

I found no bugs, security problems or AGENTS.md breaks. The new scale_bar_kept logic is sound. I traced its test expectations by hand, and they match the code. I could not run the test suite here (no shell), so the claims about test results are the PR author's, not mine.

Bugs: none found.

Security: none found.

Performance: none. The thinning search is a few divisors over at most about ten divisions per draw, and the function uses get rather than indexing.

Quality (four inline comments, all category quality; confidence high for each):

  • The sweep test a_scale_bar_draws_evenly_spaced_labels also passes when a bar falls back to its ends, so it cannot catch a bar that thins too far. It also skips bars silently with else { continue }.
  • even(7 + 1, …) is eight labels. The call should say even(8, …).
  • The doc on the a_scale_bar_always_keeps_its_ends test says BarLayout::cramped reports touching ends. It only does so above the default text size.
  • The PINNED hash now holds the thinned output, but the test name scale_bars_without_details_draw_as_before and its doc still describe the old baseline.
  • Smaller points I did not post, for the release quality pass: the user-guide paragraph in docs/user-guide/layouts.md has an awkward line break before "Fill" and an unclear "seven then keep only the ends".

AGENTS.md: none. Checked against the rules that apply here:

  • No unwrap, expect or unsafe in the non-test code.
  • Thinning is documented as a rendering choice, not data loss.
  • The change is in shared bar_layout, so the layout view, PNG, PDF, SVG and the text-cut check all read the same labels.
  • No command, parameter or saved field changes, so command-surface.txt and the schema stay as they are.
  • is_multiple_of is within rust-version 1.92, and the new lines are under the 140-column limit in rustfmt.toml.

Twins checked: the colorbar's greedy tick thinning (colorbar.rs:168) uses the same skip rule, but its ticks sit at data values, not on a regular grid, so even thinning does not apply. No other twin needs this change.

Prior concerns: none, since the review threads list is empty.

Change size

Compared with main (merge base 1fc44f9 to PR head b27daca), the whole PR.

Files changed Added Modified Removed Renamed Size before Size after Total change
4 1 3 0 0 625.4 KB 630.4 KB +5.0 KB
Path Size before Size after Size change
crates/render/src/layout/tests/scale_bar_labels.rs 0 B 3.7 KB +3.7 KB
crates/render/src/layout/scale_bar.rs 34.7 KB 35.7 KB +1.0 KB
docs/user-guide/layouts.md 102.5 KB 102.7 KB +200 B
crates/render/src/layout/tests.rs 488.3 KB 488.3 KB +76 B

Comment thread crates/render/src/layout/scale_bar.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

I read the diff, the base AGENTS.md, bar_layout, scale_bar_box_width_pt, losses, the test file and the colorbar code that has the same pattern. I could not compile or run the tests here (no shell), so everything below comes from reading the code. The pinned hash and the sweep's pass/fail are unverified.

Bugs: none found (confidence: high). I checked:

  • spans and kept index safely (get, contains, saturating_sub). A degenerate spans yields [0, divs], which is harmless.
  • right_label and the cramped checks still see the same ends as before, because the end label is always kept.
  • The drawing, the text-cut check (losses) and the box measure all read BarLayout::labels, so they agree.
  • The hand-worked cases in scale_bar_labels.rs ([0,2,4], [0,4], [0,2,4,6], [0,5], [0,3,6]) match the rule.

Security: none found. The change adds no input parsing or I/O.

Performance: none found. The new function is O(divisors × divisions) on at most 11 labels. Text measurement happens once per label, as the old loop did.

Quality (2 inline, plus notes):

  • scale_bar.rs:538: only divisors of the count are tried as steps. A prime count (3, 5, 7) can drop straight to the ends even when a non-even labelling would fit. Example: the old loop kept 0, 2, 5 and this keeps 0, 5. This is documented, so it may be intended, but the PR should say so. Confidence: medium.
  • scale_bar_labels.rs:82: the sweep re-derives label text, measure and gap instead of reading what bar_layout draws. Confidence: high that it is a maintainability risk; no defect today.
  • Twin not covered: colorbar.rs:168 has the same left-to-right skip ("each drawn only if it clears the one before") that [Bug]: A layout scale bar whose labels don't all fit can keep unevenly spaced ones #3490 fixes for scale bars. This PR does not touch it. A follow-up issue would be reasonable. Confidence: high that the pattern exists.
  • Judgement call: thinned labels are not reported through Session::notify. The PR calls this display thinning, not data loss, and the grid labels treat it the same way. I did not treat it as a violation.

AGENTS.md: no violations found. I checked:

  • No unwrap/expect in non-test code (test code uses expect, as the sibling tests do).
  • No command, saved-field, schema, asset or wasm-gated change.
  • The test names used (serif_available, measure_in, bar_number, bar_plan, ElementKind::scale_bar, FontFamily::Serif) all exist.

Earlier concerns: I left 4 alone. Three were answered or addressed (cramped wording, the sweep's weak assertion, the 7 + 1 call). The fourth, on the pinned test's name and doc, looks mostly addressed by the rename and doc change, with no further reply.

Change size

Compared against main (merge base 1fc44f9 to PR head c66c95a), the whole PR.

Measure Value
Files changed 4 (1 added, 3 modified, 0 removed, 0 renamed)
Size before 625.4 KB
Size after 632.3 KB
Total size change +6.9 KB
File Before After Change
crates/render/src/layout/tests/scale_bar_labels.rs 0 B 5.6 KB +5.6 KB
crates/render/src/layout/scale_bar.rs 34.7 KB 35.7 KB +1.0 KB
docs/user-guide/layouts.md 102.5 KB 102.7 KB +224 B
crates/render/src/layout/tests.rs 488.3 KB 488.4 KB +99 B

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-3499/
Web app https://preview.rustgis.app/pr-3499/app/
Commit c66c95a

GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the preview label (once per commit).

@jgregoriorincon
jgregoriorincon added this pull request to the merge queue Dec 1, 2026
Merged via the queue into opengeos:main with commit 44d7e19 Dec 1, 2026
30 checks passed
@jgregoriorincon
jgregoriorincon deleted the fix/3490-scale-bar-even-labels branch December 1, 2026 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: A layout scale bar whose labels don't all fit can keep unevenly spaced ones

1 participant