diff --git a/crates/render/src/layout/colorbar.rs b/crates/render/src/layout/colorbar.rs index 8b6c71fe54..2b5ac5c85f 100644 --- a/crates/render/src/layout/colorbar.rs +++ b/crates/render/src/layout/colorbar.rs @@ -7,7 +7,7 @@ const LABEL_LINE: f32 = 1.2; /// A tick's length past the bar, in pixels at 96 dpi. const TICK_PX: f32 = 2.5; /// The gap between a tick and its label, in pixels at 96 dpi. -const GAP_PX: f32 = 1.5; +pub(super) const GAP_PX: f32 = 1.5; /// The thinnest horizontal bar drawn with its labels, in pixels at 96 dpi. const MIN_BAR_PX: f32 = 3.0; @@ -65,15 +65,19 @@ pub(super) struct ColorbarPlan { /// The labels drawn: each one's text, its tick's two ends, where it is drawn, and whether /// centred on it (else from its left). pub(super) labels: Vec<(String, [(f32, f32); 2], (f32, f32), bool)>, - /// Labels left out because they would spill out of the element (#2638), not those thinned - /// for clearing the one before: a short or narrow element, or one with no room for a bar. + /// How many labels a short or narrow element leaves out that the page's cut-off check says: + /// those that would spill out of it, or all of them with no room for a bar (#2638), and the + /// last one where the first and last would touch (#3501). Labels thinned between the two + /// aren't counted. pub(super) spilled: usize, } /// A colorbar in `r` at `ppp` (#2638): its title's lines that fit over the bar's own room /// ([`colorbar_title_drawn`]), the bar, leaving room for the labels on their side and for the end -/// labels to stand past its ends, and each label drawn only if it clears the one before and stays -/// inside the element. +/// labels to stand past its ends, and the labels that stay inside the element, thinned evenly where +/// they would touch, as a scale bar's are ([`scale_bar_kept`], #3501): the first and last of those +/// always, unless the two would touch, and then only the first along the bar, the last counted +/// in `spilled`. pub(super) fn colorbar_plan(r: Rect, scale: &ColorScale, bar: &Bar, ppp: f32) -> ColorbarPlan { let px = label_px(bar.text_pt, ppp); let width = |s: &str| text::measure(s, px / 1.33, false); @@ -158,16 +162,14 @@ pub(super) fn colorbar_plan(r: Rect, scale: &ColorScale, bar: &Bar, ppp: f32) -> // A label (and its tick) is left out where it would spill out of a short or narrow element. let inside = |x: f32, y: f32, w: f32, h: f32| x >= r.x() - 1.0 && y >= r.y() - 1.0 && x + w <= r.right() + 1.0 && y + h <= r.bottom() + 1.0; - // Labels in order along the bar, each drawn only if it clears the one before. - let mut last_end = f32::NEG_INFINITY; let mut ordered: Vec<(f32, String)> = labels.into_iter().map(|(t, s)| (along(t.clamp(0.0, 1.0)), s)).collect(); ordered.sort_by(|a, b| a.0.total_cmp(&b.0)); + // Each label that stays inside the element, with the room it takes along the bar. One left out + // takes none, so it doesn't thin the others. + let (mut spans, mut placed) = (Vec::new(), Vec::new()); for (d, label) in ordered { let lw = width(&label); let extent = if vertical { px } else { lw }; - if d - extent / 2.0 < last_end + gap { - continue; - } let (tick_ends, at, centred, fits) = if vertical { let (y, x0) = (by + d, if bar.side == TickSide::Start { bx } else { bx + bw }); let x1 = if bar.side == TickSide::Start { x0 - tick } else { x0 + tick }; @@ -179,14 +181,23 @@ pub(super) fn colorbar_plan(r: Rect, scale: &ColorScale, bar: &Bar, ppp: f32) -> let ly = if bar.side == TickSide::Start { y1 - gap - px * 0.5 } else { y1 + gap + px * 0.5 }; ([(x, y0), (x, y1)], (x, ly), true, inside(x - lw / 2.0, ly - px / 2.0, lw, px)) }; - // Only a label drawn takes room: one left out doesn't thin the next. if fits { - last_end = d + extent / 2.0; - plan.labels.push((label, tick_ends, at, centred)); + spans.push((d - extent / 2.0, d + extent / 2.0)); + placed.push((label, tick_ends, at, centred)); } else { plan.spilled += 1; } } + // Where even the first and last would touch, only the first is drawn, never one label over + // another, and the last is said with the labels that don't fit. + let ends_touch = matches!((spans.first(), spans.last()), (Some(a), Some(b)) if spans.len() > 1 && a.1 + gap > b.0); + let kept = if ends_touch { + plan.spilled += 1; + vec![0] + } else { + scale_bar_kept(&spans, 0, gap) + }; + plan.labels = placed.into_iter().enumerate().filter(|(i, _)| kept.contains(i)).map(|(_, l)| l).collect(); plan } @@ -284,7 +295,7 @@ pub(super) fn colorbar_title_drawn(lines: usize, top: f32, limit: f32, px: f32) } /// A colorbar's labels' size in pixels at `ppp`, `pt` points brought into range as a file's is. -fn label_px(pt: f64, ppp: f32) -> f32 { +pub(super) fn label_px(pt: f64, ppp: f32) -> f32 { layout_text_in_range(pt, COLORBAR_TEXT_PT) as f32 * 1.33 * ppp } diff --git a/crates/render/src/layout/losses.rs b/crates/render/src/layout/losses.rs index ae604200e9..edd2a25c4a 100644 --- a/crates/render/src/layout/losses.rs +++ b/crates/render/src/layout/losses.rs @@ -27,9 +27,9 @@ pub struct TextLoss { /// 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. For a colorbar, also set, with no line counted, when its labels would - /// spill out of its box ([`colorbar_plan`]). For a profile, also set, with no line counted, - /// when its title is shortened or left out between its section's names - /// ([`profile_title_cut`]). + /// spill out of its box, or an end is left out for touching the other ([`colorbar_plan`]). For + /// a profile, also set, with no line counted, when its title is shortened or left out between + /// its section's names ([`profile_title_cut`]). pub below_box: bool, /// Some of them come past the 200 lines a text draws, whatever its box: only splitting the /// text draws them. diff --git a/crates/render/src/layout/tests.rs b/crates/render/src/layout/tests.rs index 2153593a5e..f57f2d8042 100644 --- a/crates/render/src/layout/tests.rs +++ b/crates/render/src/layout/tests.rs @@ -1,5 +1,6 @@ use super::*; +mod colorbar_labels; mod legend_breaks; mod scale_bar_labels; @@ -5283,7 +5284,8 @@ fn a_colorbars_long_title_leaves_its_bar_room_for_labels() { /// one-line title back and says so, its bar keeping both end labels, and one 12 mm high draws /// the title and says nothing. A vertical colorbar too short for its end labels, or too narrow /// for its labels beside the bar, says so even with no title, and a horizontal one too short -/// for its labels under its bar, too. +/// for its labels under its bar, too. So does one whose two end labels fit but would touch +/// (#3501): 22 px high holds both, a line apart, but not their gap. #[test] fn a_colorbars_labels_left_out_are_said() { let map = ramp_map( @@ -5298,6 +5300,7 @@ fn a_colorbars_labels_left_out_are_said() { (Orientation::Vertical, "Elevation (m)", 25.0, 12.0, false, 0), (Orientation::Vertical, "", 25.0, 12.0 * mm, true, 0), (Orientation::Vertical, "", 25.0, 25.0 * mm, false, 0), + (Orientation::Vertical, "", 25.0, 22.0 * mm, true, 0), // Too narrow for its labels beside the bar: they're left out, and said. (Orientation::Vertical, "", 2.0, 25.0, true, 0), (Orientation::Horizontal, "", 25.0, 15.0 * mm, true, 0), diff --git a/crates/render/src/layout/tests/colorbar_labels.rs b/crates/render/src/layout/tests/colorbar_labels.rs new file mode 100644 index 0000000000..12f308b7bd --- /dev/null +++ b/crates/render/src/layout/tests/colorbar_labels.rs @@ -0,0 +1,120 @@ +//! Which labels a colorbar keeps when they don't all fit (#3501). + +use super::*; + +const PT: f64 = 16.0; + +/// A colorbar of `scale` in a `w` by `h` element at one pixel per point of its own: the indices, +/// low to high, of the labels it draws among all of its labels, how many it has, and how many +/// spilled. Panics if two drawn labels stand closer than the plan's gap, or two of the bar's labels +/// have the same text, which would not tell them apart. +fn kept(scale: &ColorScale, ticks: usize, orientation: Orientation, reverse: bool, w: f32, h: f32) -> (Vec, usize, usize) { + let bar = Bar { title: "", orientation, ticks, side: TickSide::End, reverse, comma: false, ink: INK, text_pt: PT, decimals: Some(2) }; + let plan = |w, h| colorbar_plan(Rect::from_xywh(0.0, 0.0, w, h).unwrap(), scale, &bar, 1.0); + let mut all: Vec = plan(3000.0, 3000.0).labels.into_iter().map(|l| l.0).collect(); + if reverse != (orientation == Orientation::Vertical) { + all.reverse(); + } + assert_eq!(all.iter().collect::>().len(), all.len(), "a repeated label: {all:?}"); + let drawn = plan(w, h); + let px = label_px(PT, 1.0); + let spans: Vec<(f32, f32)> = drawn + .labels + .iter() + .map(|(s, _, at, _)| { + let (middle, extent) = + if orientation == Orientation::Vertical { (at.1, px) } else { (at.0, text::measure(s, px / 1.33, false)) }; + (middle - extent / 2.0, middle + extent / 2.0) + }) + .collect(); + assert!(spans.windows(2).all(|p| p[0].1 + GAP_PX <= p[1].0), "{w}x{h}: labels touch: {spans:?}"); + let mut indices: Vec = drawn.labels.iter().map(|l| all.iter().position(|s| *s == l.0).unwrap()).collect(); + indices.sort_unstable(); + (indices, all.len(), drawn.spilled) +} + +/// Every set of labels kept over elements from narrow (or short) to roomy where nothing is said to +/// be cut off; each one holds both ends and is evenly spaced between them. +fn outcomes(scale: &ColorScale, ticks: usize, orientation: Orientation, reverse: bool) -> std::collections::BTreeSet> { + let mut seen = std::collections::BTreeSet::new(); + for size in (20..900).step_by(2) { + let (w, h) = if orientation == Orientation::Vertical { (300.0, size as f32) } else { (size as f32, 80.0) }; + let (indices, n, spilled) = kept(scale, ticks, orientation, reverse, w, h); + if spilled > 0 || indices.is_empty() { + continue; + } + assert_eq!((indices.first(), indices.last()), (Some(&0), Some(&(n - 1))), "{w}x{h}: the ends: {indices:?}"); + let step = indices[1] - indices[0]; + assert!(indices.windows(2).all(|p| p[1] - p[0] == step), "{w}x{h}: evenly spaced: {indices:?}"); + seen.insert(indices); + } + seen +} + +fn ramp() -> ColorScale { + let grey = Rgba::rgb(200, 200, 200); + ColorScale::Ramp { lo: 415.82, hi: 569.18, colors: vec![grey, grey] } +} + +fn sets(of: &[&[usize]]) -> std::collections::BTreeSet> { + of.iter().map(|s| s.to_vec()).collect() +} + +/// The case of the issue and its family: all five where they fit, then every second one, then +/// only the ends, never the ends' neighbours without the maximum. +#[test] +fn a_colorbar_keeps_its_ends_and_thins_the_rest_evenly() { + let expected = sets(&[&[0, 1, 2, 3, 4], &[0, 2, 4], &[0, 4]]); + for orientation in [Orientation::Horizontal, Orientation::Vertical] { + for reverse in [false, true] { + assert_eq!(outcomes(&ramp(), 5, orientation, reverse), expected, "{orientation:?}, reverse {reverse}"); + } + } +} + +/// Five gaps have no even step between all of them and the ends alone. +#[test] +fn a_colorbar_with_a_prime_number_of_gaps_keeps_all_or_its_ends() { + for orientation in [Orientation::Horizontal, Orientation::Vertical] { + assert_eq!(outcomes(&ramp(), 6, orientation, false), sets(&[&[0, 1, 2, 3, 4, 5], &[0, 5]]), "{orientation:?}"); + } +} + +#[test] +fn a_classified_or_unique_colorbar_thins_its_labels_the_same_way() { + let grey = Rgba::rgb(200, 200, 200); + let steps = ColorScale::Steps { edges: vec![415.82, 420.0, 500.0, 501.0, 569.18], colors: vec![grey; 4] }; + let values = ColorScale::Values([415.82, 420.0, 500.0, 501.0, 569.18].map(|v| (v, grey)).to_vec()); + let expected = sets(&[&[0, 1, 2, 3, 4], &[0, 2, 4], &[0, 4]]); + for scale in [&steps, &values] { + for orientation in [Orientation::Horizontal, Orientation::Vertical] { + assert_eq!(outcomes(scale, 5, orientation, false), expected, "{orientation:?}"); + } + } +} + +#[test] +fn a_colorbar_of_one_value_labels_it_once() { + let grey = Rgba::rgb(200, 200, 200); + let flat = ColorScale::Ramp { lo: 7.0, hi: 7.0, colors: vec![grey] }; + assert_eq!(kept(&flat, 5, Orientation::Horizontal, false, 300.0, 80.0), (vec![0], 1, 0)); +} + +/// Where even the first and last label would touch, only the one at the bar's start (left, or +/// top) is drawn, and the other end is said to be cut off. +#[test] +fn a_colorbar_whose_ends_would_touch_keeps_one_and_says_so() { + // Twice the wider end label: both just fit, a label's width apart. Measured, as fonts differ. + let end = |s: &str| text::measure(s, label_px(PT, 1.0) / 1.33, false); + let narrow = 2.0 * end("415.82").max(end("569.18")); + assert_eq!(kept(&ramp(), 5, Orientation::Horizontal, false, narrow, 80.0), (vec![0], 5, 1)); + assert_eq!(kept(&ramp(), 5, Orientation::Vertical, false, 300.0, 43.0), (vec![4], 5, 1)); +} + +/// A label that spills out of the element is counted though it would also touch its neighbour. +#[test] +fn a_colorbar_counts_a_label_that_spills_and_would_touch() { + let grey = Rgba::rgb(200, 200, 200); + let values = ColorScale::Values(vec![(1.0, grey), (1e12, grey)]); + assert_eq!(kept(&values, 5, Orientation::Vertical, true, 80.0, 43.0), (vec![0], 2, 1)); +} diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 25d3b41528..29c3bf41cd 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -682,7 +682,12 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu 21, the ends included), the labels' **Text size** (4 to 100 points, 8 unless set; the bar makes room for them), their **Decimals** (Automatic, or 0 to 6 with the thousands set apart and the layout's decimal mark, never shortened to `K` or `M`; too few for the ticks' step repeats a - label) and **Reverse** (high values first). An RGB raster or a hillshade + label) and **Reverse** (high values first). Where labels would touch, the first and last + that fit in the box are drawn and those between are thinned evenly, as on the scale bar, so an + end label that doesn't fit gives way to the next one that does; where even those two would + touch, only the one nearer the left, or the top, is drawn. Labels that don't fit in the box, + and an end left out because the two would touch, are said with the page's cut-off texts. An RGB + raster or a hillshade without a tint has no colour scale and shows a placeholder, as does a picked layer that was renamed or removed (it is kept by name, like a table or chart frame's layer) until another is picked. From a command: