Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions crates/render/src/layout/extent_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The numbers on a map frame's extent indicators (#3394).

use ab_glyph::Font;
use serde_json::json;

use super::*;
Expand All @@ -9,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.
Expand Down Expand Up @@ -36,13 +40,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 {
Expand All @@ -51,7 +57,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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run.glyphs.last().unwrap() panics without saying which run was empty. Test code may unwrap (clippy.toml allows it), so this is not a rule break. Skipping an empty run keeps the failure in the assertion instead:

Suggested change
let last = run.glyphs.last().unwrap();
let Some(last) = run.glyphs.last() else { continue };
let width = last.x + run.font.h_advance_unscaled(last.id) * run.size / run.font.units_per_em().unwrap();

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left for the release quality pass: this improves the diagnostic for an unexpected empty glyph run in the test helper. It does not affect the centre measurement fix.

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)));
}
Expand Down Expand Up @@ -81,7 +91,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
}
Expand Down Expand Up @@ -362,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
Expand All @@ -373,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.
Expand Down Expand Up @@ -436,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
Expand All @@ -461,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:?}");
}
Loading