diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index c7177a609e..ccfaaa6b06 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -538,10 +538,17 @@ fn draw_page( // colour draws none, as it draws no outline, rather than a halo alone. let (cw, ch) = (frame_pm.width(), frame_pm.height()); let size = EXTENT_NUMBER_SIZE * 1.33 * fppp as f32; - for (n, color, (nx, ny)) in numbers { - if color.a() > 0 && on_frame(mask.as_ref(), (cw, ch), (nx, ny)) { - text::text_at(&mut frame_pm, &n.to_string(), nx, ny, size, color, true, text::Align::Center, true); - } + // Numbers that would fall on one another (outlines with one centre) are set apart + // in a row there, so each outline keeps one that can be told apart (#3528); a row + // the frame can't hold whole isn't used. A clear outline, or one whose middle is + // off the frame (inside its shape), has no number and no place in a row. + let on = |at| on_frame(mask.as_ref(), (cw, ch), at); + let drawn: Vec<_> = numbers.into_iter().filter(|&(_, color, was)| color.a() > 0 && on(was)).collect(); + // A row place fits where the number's whole width is on the frame, not only its centre. + let fits = |(x, y): (f32, f32)| on((x, y)) && on((x - 0.6 * size, y)) && on((x + 0.6 * size, y)); + let apart = set_apart(&drawn.iter().map(|n| n.2).collect::>(), size, fits); + for ((n, color, _), (nx, ny)) in drawn.into_iter().zip(apart) { + text::text_at(&mut frame_pm, &n.to_string(), nx, ny, size, color, true, text::Align::Center, true); } if !frame_pm.is_recording() && let Some(m) = &mask diff --git a/crates/render/src/layout/extent_tests.rs b/crates/render/src/layout/extent_tests.rs index 5edf576c24..cc3b501fa7 100644 --- a/crates/render/src/layout/extent_tests.rs +++ b/crates/render/src/layout/extent_tests.rs @@ -13,11 +13,16 @@ const SCALE: f64 = 2e8; /// A page with an overview of the map "Over" outlining one frame of the map "m" per centre in /// `centers` (longitude, latitude), numbered when `numbered`; only the overview is drawn. fn page(centers: &[(f64, f64)], numbered: bool, colors: &[[u8; 4]]) -> Layout { + page_at(¢ers.iter().map(|&c| (c, 100_000_000.0)).collect::>(), numbered, colors) +} + +/// [`page`] with each outlined frame's scale given too. +fn page_at(frames: &[((f64, f64), f64)], numbered: bool, colors: &[[u8; 4]]) -> Layout { let mut l = Layout::new("Layout", 100.0, 100.0); - let of: Vec = centers + let of: Vec = frames .iter() - .map(|&(x, y)| { - let kind = json!({"MapFrame": {"map": "m", "center": {"x": x, "y": y}, "scale": 100_000_000}}); + .map(|&((x, y), scale)| { + let kind = json!({"MapFrame": {"map": "m", "center": {"x": x, "y": y}, "scale": scale}}); let id = l.add("Detail", (60.0, 75.0, 30.0, 20.0), serde_json::from_value(kind).unwrap()); l.elements.iter_mut().filter(|e| e.id == id).for_each(|e| e.visible = false); id @@ -293,3 +298,168 @@ fn a_globe_overview_numbers_the_outline_where_the_globe_shows_it() { let centre = ((x * PT_PER_MM + at.0) as f32, (y * PT_PER_MM + at.1) as f32); assert!(changed.iter().all(|&p| centred(p, centre, 14.0)), "{changed:?}, expected about {centre:?}"); } + +/// Numbers on one point (outlines with one centre) are set apart in a row centred there, in list +/// order, each in its outline's colour, so every outline keeps a number that can be told apart +/// (#3528). +#[test] +fn outlines_with_one_centre_are_numbered_side_by_side() { + let l = page_at(&[((0.0, 0.0), 2e7), ((0.0, 0.0), 5e7)], true, &COLORS); + let drawn = written(&l, Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0]), ("2", COLORS[1])]); + let (a, b) = (drawn[0].2, drawn[1].2); + assert!(b.0 - a.0 >= 10.0 && b.0 - a.0 <= 30.0 && (a.1 - b.1).abs() < 0.5, "{a:?} {b:?}"); + let middle = ((a.0 + b.0) / 2.0, (a.1 + b.1) / 2.0); + assert!(centred(middle, on_page(0.0, 0.0), 8.0), "the row is centred on {:?}, not {middle:?}", on_page(0.0, 0.0)); +} + +#[test] +fn three_outlines_with_one_centre_are_three_numbers_in_a_row() { + let l = page_at(&[((0.0, 0.0), 2e7), ((0.0, 0.0), 5e7), ((0.0, 0.0), 8e7)], true, &COLORS); + let drawn = written(&l, Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0]), ("2", COLORS[1]), ("3", COLORS[2])]); + let step = drawn[1].2.0 - drawn[0].2.0; + assert!(step >= 10.0 && (drawn[2].2.0 - drawn[1].2.0 - step).abs() < 0.5, "{drawn:?}"); + assert!(centred(drawn[1].2, on_page(0.0, 0.0), 8.0), "{drawn:?}"); +} + +/// Outlines of the same size and centre are one outline twice: their numbers are apart too. +#[test] +fn outlines_the_same_size_with_one_centre_are_numbered_apart() { + let l = page_at(&[((0.0, 0.0), 5e7), ((0.0, 0.0), 5e7)], true, &COLORS); + let drawn = written(&l, Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0]), ("2", COLORS[1])]); + assert!(drawn[1].2.0 - drawn[0].2.0 >= 10.0, "{drawn:?}"); +} + +/// Only the numbers that fall on one another move: another outline keeps its own centre. +#[test] +fn an_outline_away_from_a_shared_centre_keeps_its_number_where_it_was() { + let l = page_at(&[((0.0, 0.0), 2e7), ((0.0, 0.0), 5e7), ((35.0, -10.0), 1e8)], true, &COLORS); + let drawn = written(&l, Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0]), ("2", COLORS[1]), ("3", COLORS[2])]); + assert!(centred(drawn[2].2, on_page(35.0, -10.0), 8.0), "{drawn:?}"); +} + +/// A clear outline draws no number, so it takes no place in the row either. +#[test] +fn a_clear_outline_with_the_same_centre_takes_no_place_in_the_row() { + let colors = [COLORS[0], [0, 0, 0, 0], COLORS[2]]; + let l = page_at(&[((0.0, 0.0), 2e7), ((0.0, 0.0), 5e7), ((0.0, 0.0), 8e7)], true, &colors); + let drawn = written(&l, Map::new("Over"), &colors); + assert_eq!(said(&drawn), [("1", colors[0]), ("3", colors[2])]); + let middle = (drawn[0].2.0 + drawn[1].2.0) / 2.0; + assert!(drawn[1].2.0 - drawn[0].2.0 >= 10.0 && (middle - on_page(0.0, 0.0).0).abs() < 8.0, "{drawn:?}"); +} + +/// A row never costs a number its place: on a frame too narrow for the row, the group keeps its +/// places and each number is drawn at the one centre. +#[test] +fn a_row_too_wide_for_the_frame_keeps_each_number_at_its_centre() { + // A frame narrower than the row: each number's own centre is on it, the row's places are not. + let mut l = page_at(&[((0.0, 0.0), 2e7), ((0.0, 0.0), 5e7)], true, &COLORS); + let overview = l.elements.last_mut().unwrap(); + overview.w = 3.0; + let drawn = written(&l, Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0]), ("2", COLORS[1])]); + assert_eq!(drawn[0].2, drawn[1].2, "both stay at the one centre: {drawn:?}"); +} + +/// A row that doesn't fit the frame whole isn't used: three outlines with one centre on a frame too +/// narrow for it keep one place, not a middle number in the row's middle with the others on it. +#[test] +fn a_row_too_wide_for_the_frame_is_not_used_by_halves() { + let mut l = page_at(&[((0.0, 0.0), 2e7), ((0.0, 0.0), 5e7), ((0.0, 0.0), 8e7)], true, &COLORS); + l.elements.last_mut().unwrap().w = 3.0; + let drawn = written(&l, Map::new("Over"), &COLORS); + assert_eq!(drawn.len(), 3, "{drawn:?}"); + assert!(drawn.iter().all(|d| d.2 == drawn[0].2), "all at the one centre, as without the row: {drawn:?}"); +} + +/// Numbers linked by a chain of near pairs are one row, not the ones near the first alone. +#[test] +fn numbers_in_a_chain_of_near_places_are_one_row() { + let size = 13.3; + let chain = [(100.0, 50.0), (100.0 + 0.9 * size, 50.0), (100.0 + 1.8 * size, 50.0)]; + let apart = set_apart(&chain, size, |_| true); + assert!(apart.windows(2).all(|w| w[1].0 - w[0].0 >= size), "{apart:?}"); + // Places far from one another keep theirs. + let far = [(10.0, 10.0), (200.0, 10.0)]; + assert_eq!(set_apart(&far, size, |_| true), far); + // A row that doesn't fit leaves the group where it was. + assert_eq!(set_apart(&chain, size, |at| at.0 >= 100.0 && at.0 <= 101.0), chain); +} + +/// A number well away from a group keeps its place beside the group's row. +#[test] +fn a_number_far_from_a_group_keeps_its_place_beside_its_row() { + let size = 13.3; + let away = [(100.0, 50.0), (100.0, 50.0), (300.0, 50.0)]; + let apart = set_apart(&away, size, |_| true); + assert!(apart[1].0 - apart[0].0 >= size && apart[2] == away[2], "{apart:?}"); +} + +/// A number whose own centre is off the frame takes no place in the row of the others. +#[test] +fn a_number_with_its_centre_off_the_frame_takes_no_place_in_the_row() { + // The overview shows about 72° either side; the third outline's centre (80°) is off it. + let l = page_at(&[((0.0, 0.0), 2e7), ((0.0, 0.0), 5e7), ((80.0, 0.0), 8e7)], true, &COLORS); + let drawn = written(&l, Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0]), ("2", COLORS[1])]); + let middle = (drawn[0].2.0 + drawn[1].2.0) / 2.0; + assert!(drawn[1].2.0 - drawn[0].2.0 >= 10.0 && (middle - on_page(0.0, 0.0).0).abs() < 8.0, "{drawn:?}"); +} + +/// A row isn't laid on a row laid before it: two pairs 2 sizes apart would meet at 1.35 sizes. The +/// first pair is spread; the second, which would land on it, keeps its place, as before the rows. +#[test] +fn a_row_is_not_laid_on_another_row() { + let size = 13.3; + let places = [(100.0, 50.0), (100.0, 50.0), (100.0 + 2.0 * size, 50.0), (100.0 + 2.0 * size, 50.0)]; + let apart = set_apart(&places, size, |_| true); + assert!(apart[1].0 - apart[0].0 >= size, "the first pair is a row: {apart:?}"); + for (a, b) in [(0, 2), (0, 3), (1, 2), (1, 3)] { + assert!((apart[a].0 - apart[b].0).abs() >= size, "numbers of two groups within a size of each other: {apart:?}"); + } +} + +/// The row is checked against a shaped frame's mask: two outlines with one centre a little inside a +/// circle's edge would have their row's outer place outside it, so the group keeps its place. +#[test] +fn a_row_reaching_past_a_shaped_frames_edge_is_not_used() { + // The circle's edge is at about 30 mm: 52° east is some 29 mm out, and a row is 4 mm wide. + let mut l = page_at(&[((52.0, 0.0), 2e7), ((52.0, 0.0), 5e7)], true, &COLORS); + let drawn = written(&l, Map::new("Over"), &COLORS); + assert!(drawn[1].2.0 - drawn[0].2.0 >= 10.0, "on the rectangle the row is used: {drawn:?}"); + if let Some(ElementKind::MapFrame { shape, .. }) = l.elements.last_mut().map(|e| &mut e.kind) { + *shape = FrameShape::Circle; + } + let drawn = written(&l, Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0]), ("2", COLORS[1])]); + assert_eq!(drawn[0].2, drawn[1].2, "on the circle the row doesn't fit: {drawn:?}"); +} + +/// A number a row would land on joins the row instead of stopping it: two coincident outlines and a +/// third about one size away are three numbers in a row. +#[test] +fn a_number_near_a_group_joins_its_row() { + let size = 13.3; + let places = [(100.0, 50.0), (100.0, 50.0), (100.0 + size, 50.0)]; + let apart = set_apart(&places, size, |_| true); + let mut xs: Vec = apart.iter().map(|p| p.0).collect(); + xs.sort_by(f32::total_cmp); + assert!(xs.windows(2).all(|w| w[1] - w[0] >= size), "{apart:?}"); + // A row that then doesn't fit leaves all three where they were. + assert_eq!(set_apart(&places, size, |at| at.0 < 100.0 + size), places); +} + +/// A row is checked with its numbers' width: one whose centre is on the frame with its glyphs past +/// the edge isn't used, the numbers staying at the one centre. +#[test] +fn a_row_whose_numbers_would_be_clipped_at_the_frames_edge_is_not_used() { + // 67° west is about 10 px from the overview's left edge: the two centres of a row are 1 and 19. + let l = page_at(&[((-67.0, 0.0), 2e7), ((-67.0, 0.0), 5e7)], true, &COLORS); + let drawn = written(&l, Map::new("Over"), &COLORS); + assert_eq!(said(&drawn), [("1", COLORS[0]), ("2", COLORS[1])]); + assert_eq!(drawn[0].2, drawn[1].2, "both stay at the one centre: {drawn:?}"); +} diff --git a/crates/render/src/layout/frame.rs b/crates/render/src/layout/frame.rs index 9984999954..05c71b6a55 100644 --- a/crates/render/src/layout/frame.rs +++ b/crates/render/src/layout/frame.rs @@ -692,6 +692,65 @@ pub fn extent_number_mm( ((0.0..w).contains(&x) && (0.0..h).contains(&y)).then_some((x, y)) } +/// The places of numbers of `size` pixels (their font size), with the ones that fall on one +/// another (outlines with one centre, or close ones: within `size` each way, linked to the first +/// by a chain of such pairs) spread in a row across their middle, in list order, 1.3 × `size` +/// apart (room for two-digit numbers), each keeping its own height. A number the row would land +/// on joins it. A row is used only where `fits` says every place of it is usable and it is near +/// no number laid before; else the group keeps its places, as without a row. Numbers nowhere near +/// another keep theirs (#3528). +pub(super) fn set_apart(places: &[(f32, f32)], size: f32, fits: impl Fn((f32, f32)) -> bool) -> Vec<(f32, f32)> { + let near = |a: (f32, f32), b: (f32, f32)| (a.0 - b.0).abs() < size && (a.1 - b.1).abs() < size; + let mut out = places.to_vec(); + let mut placed = vec![false; places.len()]; + for first in 0..places.len() { + if placed[first] { + continue; + } + placed[first] = true; + let mut group = vec![first]; + let mut at = 0; + while at < group.len() { + for other in 0..places.len() { + if !placed[other] && near(places[group[at]], places[other]) { + placed[other] = true; + group.push(other); + } + } + at += 1; + } + if group.len() < 2 { + continue; + } + loop { + group.sort_unstable(); + let middle = group.iter().map(|&i| places[i].0).sum::() / group.len() as f32; + let row: Vec<(f32, f32)> = group + .iter() + .enumerate() + .map(|(k, &i)| (middle + (k as f32 - (group.len() - 1) as f32 / 2.0) * size * 1.3, places[i].1)) + .collect(); + // A number not yet placed that the row would land on joins it, and the row is laid again. + let joining: Vec = (0..places.len()).filter(|&i| !placed[i] && row.iter().any(|&p| near(p, places[i]))).collect(); + if joining.is_empty() { + // A row on a number outside the group (one already laid) would only move the overlap there. + let clear = out.iter().enumerate().all(|(i, &p)| group.contains(&i) || row.iter().all(|&at| !near(at, p))); + if clear && row.iter().all(|&at| fits(at)) { + for (&i, at) in group.iter().zip(row) { + out[i] = at; + } + } + break; + } + for i in joining { + placed[i] = true; + group.push(i); + } + } + } + out +} + /// Where a frame's map shows `c` (longitude, latitude), the middle of the frame outlined by /// `ring`, in pixels of its drawing: moved and cut as [`outline_lonlat`] moves and cuts that ring, /// so it falls with the outline, on the piece holding it. `None` where the map can't show it. diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index d943b2ab51..f22fb5f946 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -346,12 +346,14 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu `layout.update` with `"extentIndicator": {"color": "#555555", "width": 1}`. With several indicators, `of` says which: `{"of": 3, "width": 1}`, or `{"of": 3, "remove": true}`; `false` removes them all. **Number them**, in the same section, draws 1, 2, 3… in their order at the - centre of each outline, in its colour, so a key map points to numbered detail maps; removing one - renumbers the rest, and an outline centred off the frame has no number (`layout.update` with - `"extentNumbers": true`). The frame keeps the setting when all its indicators are removed, so - the next one added is numbered. An outline breaks - where the other frame's edge leaves the Earth (off a globe's disc, or past the edge of its - projection's world) and runs up to it there, and where the overview's own projection can't show + centre of each outline, in its colour, so a key map points to numbered detail maps; the numbers of + outlines that would fall on one another (one centre, or close ones) go side by side in a row + there (where the whole row fits the frame and clears the other numbers; otherwise they keep + their places). Removing one renumbers the rest, and an outline centred off the frame has no + number (`layout.update` with `"extentNumbers": true`). The frame keeps the setting when all its + indicators are removed, so the next one added is numbered. An outline breaks where the other + frame's edge leaves the Earth (off a globe's disc, or past the edge of its projection's world) + and runs up to it there, and where the overview's own projection can't show the place. An outline across the overview map's edge (on a world projection, the meridian opposite its central one) is cut there, each piece on its side of the map, and so is a frame on the globe around a pole outlined on Web Mercator, at ±180°.