From bedc11ac21fea3900b7e1a70d7a4584031386981 Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 2 Dec 2026 07:00:15 -0600 Subject: [PATCH 1/2] test(render): compare extent number centres across fonts --- crates/render/src/layout/extent_tests.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/render/src/layout/extent_tests.rs b/crates/render/src/layout/extent_tests.rs index cc3b501fa7..4b0e20e925 100644 --- a/crates/render/src/layout/extent_tests.rs +++ b/crates/render/src/layout/extent_tests.rs @@ -1,5 +1,6 @@ //! The numbers on a map frame's extent indicators (#3394). +use ab_glyph::Font; use serde_json::json; use super::*; @@ -36,13 +37,15 @@ fn page_at(frames: &[((f64, f64), f64)], numbered: bool, colors: &[[u8; 4]]) -> } /// The text drawn in one of `colors` (not its halo), in the order drawn: what it says, its colour -/// and where its pen starts on the page, in pixels at 96 dpi (layout points). +/// and its horizontal centre and baseline on the page, in pixels at 96 dpi (layout points). fn written(l: &Layout, over: Map, colors: &[[u8; 4]]) -> Vec<(String, [u8; 4], (f32, f32))> { written_from(l, [Map::new("m"), over], colors) } /// [`written`] with the map the outlined frames show given too. fn written_from(l: &Layout, mut maps: [Map; 2], colors: &[[u8; 4]]) -> Vec<(String, [u8; 4], (f32, f32))> { + /// Find number runs through nested page transforms, measuring horizontal text centres + /// rather than font-dependent pen starts; exclude the halo's duplicate glyphs. fn walk(items: &[Item], at: Transform, colors: &[[u8; 4]], out: &mut Vec<(String, [u8; 4], (f32, f32))>) { for item in items { match item { @@ -51,7 +54,11 @@ fn written_from(l: &Layout, mut maps: [Map; 2], colors: &[[u8; 4]]) -> Vec<(Stri let c = run.color.to_color_u8(); let rgba = [c.red(), c.green(), c.blue(), c.alpha()]; if colors.contains(&rgba) { - let mut p = tiny_skia::Point::zero(); + // Different digits need not have the same advance (notably in Windows' + // default font). Their pen starts differ even when their centres coincide. + let last = run.glyphs.last().unwrap(); + let width = last.x + run.font.h_advance_unscaled(last.id) * run.size / run.font.units_per_em().unwrap(); + let mut p = tiny_skia::Point::from_xy(width / 2.0, 0.0); at.pre_concat(run.transform).map_point(&mut p); out.push((run.glyphs.iter().map(|g| g.ch).collect(), rgba, (p.x, p.y))); } @@ -81,7 +88,7 @@ fn on_page(lon: f64, lat: f64) -> (f32, f32) { ((x * PT_PER_MM + at.0) as f32, (y * PT_PER_MM + at.1) as f32) } -/// A digit's pen starts half its width left of the centre, on a baseline below it. +/// The number's horizontal centre is at the outline's centre, its baseline a little below it. fn centred(drawn: (f32, f32), centre: (f32, f32), within: f32) -> bool { (drawn.0 - centre.0).abs() < within && (drawn.1 - centre.1).abs() < within } From 24b81c249a80764b722f556b5c19f92a2491fbef Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 2 Dec 2026 08:18:07 -0600 Subject: [PATCH 2/2] test(render): allow float round-off in recovered label centres --- crates/render/src/layout/extent_tests.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/render/src/layout/extent_tests.rs b/crates/render/src/layout/extent_tests.rs index 4b0e20e925..5e46085461 100644 --- a/crates/render/src/layout/extent_tests.rs +++ b/crates/render/src/layout/extent_tests.rs @@ -10,6 +10,9 @@ const COLORS: [[u8; 4]; 3] = [[255, 0, 255, 255], [0, 160, 160, 255], [200, 120, /// The overview's box on the page (x, y, w, h in mm). const OVERVIEW_MM: (f64, f64, f64, f64) = (10.0, 10.0, 80.0, 60.0); const SCALE: f64 = 2e8; +/// Recovering a centre from a glyph run and page transform rounds at f32 precision. One +/// thousandth of a page pixel allows that round-off, but not a different placement. +const SAME_CENTRE: f32 = 0.001; /// 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. @@ -369,7 +372,7 @@ fn a_row_too_wide_for_the_frame_keeps_each_number_at_its_centre() { 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:?}"); + assert!(centred(drawn[0].2, drawn[1].2, SAME_CENTRE), "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 @@ -380,7 +383,7 @@ fn a_row_too_wide_for_the_frame_is_not_used_by_halves() { 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:?}"); + assert!(drawn.iter().all(|d| centred(d.2, drawn[0].2, SAME_CENTRE)), "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. @@ -443,7 +446,7 @@ fn a_row_reaching_past_a_shaped_frames_edge_is_not_used() { } 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:?}"); + assert!(centred(drawn[0].2, drawn[1].2, SAME_CENTRE), "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 @@ -468,5 +471,5 @@ fn a_row_whose_numbers_would_be_clipped_at_the_frames_edge_is_not_used() { 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:?}"); + assert!(centred(drawn[0].2, drawn[1].2, SAME_CENTRE), "both stay at the one centre: {drawn:?}"); }