From d0cf7f09ecb670f9edc30b9cda3b17e5155b6ab0 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 1 Dec 2026 02:05:29 -0800 Subject: [PATCH 1/2] Tests: format tests check exact values instead of wide tolerances The PDF, EPS, SVG mesh, metafile hatch and distorted-image tests now assert the values the input gives: the Letter page a page without area falls back to, the H glyph's box from its font metrics, the middle gray of each mesh piece, the 8 of 64 hatch pixels, and the bounds of the placement taken at the image's center. --- crates/doc/src/image_quad.rs | 2 +- crates/eps/src/import/tests_composite.rs | 14 ++++++++++---- crates/metafile/src/tests/import.rs | 6 ++++-- crates/pdf/src/tests/import_options.rs | 3 ++- crates/svg/tests/it/mesh_export.rs | 5 +++++ 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/crates/doc/src/image_quad.rs b/crates/doc/src/image_quad.rs index 8f514a3b71..f5b087125d 100644 --- a/crates/doc/src/image_quad.rs +++ b/crates/doc/src/image_quad.rs @@ -457,7 +457,7 @@ mod tests { n.transform(Affine::scale_non_uniform(1.0, 0.25), false); assert!(n.extras().image_quad.is_none()); let b = n.geometric_bounds().unwrap(); - assert!(b.height() < 1e-6 && (b.width() - 150.0).abs() < 60.0, "{b:?}"); + assert!(b.height() < 1e-6 && (b.x0 + 200.0 / 9.0).abs() < 1e-6 && (b.x1 - 1400.0 / 9.0).abs() < 1e-6, "{b:?}"); } /// A file's corners that the image cannot take are dropped as it is read, so a later scale diff --git a/crates/eps/src/import/tests_composite.rs b/crates/eps/src/import/tests_composite.rs index ff3b3382d3..6e3a63373d 100644 --- a/crates/eps/src/import/tests_composite.rs +++ b/crates/eps/src/import/tests_composite.rs @@ -64,8 +64,10 @@ fn type42_descendants_draw_their_own_glyphs() { } // The H, then the I one H's width along; both stand on the baseline, a cap height tall. let (bh, bi) = (bounds(paths[0]), bounds(paths[1])); - assert!((bh.x0 - 10.0).abs() < 8.0 && bh.width() > 20.0 && bh.width() < 50.0 * wh, "{bh:?}"); - assert!((bh.height() - bi.height()).abs() < 0.5 && bh.height() > 25.0 && bh.height() < 40.0, "{bh:?} {bi:?}"); + // The bundled font's H spans (90, 0)–(562, 656) of its 1000-unit em: at 50 pt from x = 10 that + // is 14.5 to 38.1, 32.8 tall. + assert!((bh.x0 - 14.5).abs() < 0.01 && (bh.width() - 23.6).abs() < 0.01 && (bh.height() - 32.8).abs() < 0.01, "{bh:?}"); + assert!((bh.height() - bi.height()).abs() < 0.5, "{bh:?} {bi:?}"); assert!(bi.x0 > 10.0 + 50.0 * wh - 1.0 && bi.x1 < 10.0 + 50.0 * (wh + wi), "{bi:?}"); } @@ -78,8 +80,12 @@ fn type42_descendants_measure_space_and_outline_their_glyphs() { check(format!("(\\000\\001\\000\\002) stringwidth pop {} sub abs 0.01 lt", 50.0 * (wh + wi))); // xyshow places each character where its numbers say. check("gsave nulldevice 10 20 moveto (\\000\\001\\000\\002) [30 0 7 0] xyshow currentpoint grestore pop 47 sub abs 0.01 lt".into()); - // charpath outlines them. - check("newpath 10 20 moveto (\\000\\001) true charpath pathbbox exch pop sub abs 25 gt exch 10 sub abs 8 lt and".into()); + // charpath outlines them: the H's box is (14.5, 20)–(38.1, 52.8). + check( + "newpath 10 20 moveto (\\000\\001) true charpath pathbbox 52.8 sub abs 0.01 lt exch 38.1 sub abs 0.01 lt and \ + exch 20 sub abs 0.01 lt and exch 14.5 sub abs 0.01 lt and" + .into(), + ); } /// A descendant that is a font by name only (its program isn't run) sets its characters as type, diff --git a/crates/metafile/src/tests/import.rs b/crates/metafile/src/tests/import.rs index 15eca240a9..42a66bc8d5 100644 --- a/crates/metafile/src/tests/import.rs +++ b/crates/metafile/src/tests/import.rs @@ -731,7 +731,8 @@ fn hatched_brushes_fill_with_their_average_color() { let p = paths(&back.document); assert_eq!(p.len(), 1); let [r, g, b] = fill_rgb(&p[0]).unwrap(); - assert!(b == 255 && r == g && r > 0 && r < 255, "{:?}", [r, g, b]); + // The horizontal hatch covers one row in eight of its 8 × 8 tile: 8 blue and 56 white pixels. + assert_eq!([r, g, b], [223, 223, 255]); // With a transparent background (SETBKMODE 1) the hatch is blue with gaps: a translucent fill. let mut records = records; records.insert(0, (18, le(&[1]))); @@ -739,7 +740,8 @@ fn hatched_brushes_fill_with_their_average_color() { let p = paths(&back.document); assert_eq!(fill_rgb(&p[0]), Some([0, 0, 255])); let AppearanceItem::Fill(f) = &p[0].appearance.items[0] else { panic!("a fill") }; - assert!(f.opacity > 0.0 && f.opacity < 1.0, "{}", f.opacity); + // One eighth of 255, truncated to 31. + assert!((f.opacity - 31.0 / 255.0).abs() < 1e-6, "{}", f.opacity); } #[test] diff --git a/crates/pdf/src/tests/import_options.rs b/crates/pdf/src/tests/import_options.rs index a419d64137..2205ae2a7f 100644 --- a/crates/pdf/src/tests/import_options.rs +++ b/crates/pdf/src/tests/import_options.rs @@ -272,7 +272,8 @@ fn a_page_without_area_gets_the_size_viewers_give_it() { let bytes = pdf(&[PdfPage { media: [0.0, 0.0, 0.0, 0.0], ..PdfPage::new(0.0, 0.0, "") }], None); let i = info(&bytes, None).unwrap(); let (w, h) = (i.pages[0].width, i.pages[0].height); - assert!(w > 0.0 && h > 0.0); + // US Letter, 8.5 × 11 in. + assert_eq!((w, h), (612.0, 792.0)); for crop in [CropTo::Crop, CropTo::Media, CropTo::Art] { let ab = open(&bytes, ImportOptions { crop, ..Default::default() }).artboards[0].rect; assert_eq!((ab.width(), ab.height()), (w, h), "{crop:?}"); diff --git a/crates/svg/tests/it/mesh_export.rs b/crates/svg/tests/it/mesh_export.rs index 78f30f50c9..c46b943734 100644 --- a/crates/svg/tests/it/mesh_export.rs +++ b/crates/svg/tests/it/mesh_export.rs @@ -84,6 +84,11 @@ fn a_patch_is_cut_no_finer_than_its_colour_changes() { let d = doc(2, 2, |_, c| Color::rgb8(100 + c as u8, 100 + c as u8, 100 + c as u8)); let svg = export(&d, &ExportOptions::default()); assert!(pieces(&svg) <= 4, "{} pieces", pieces(&svg)); + // Each piece takes its patch's middle color: 100.5 and 101.5 round to 101 and 102. + let mut fills: Vec<&str> = svg.split("fill=\"").skip(1).filter_map(|s| s.split('"').next()).filter(|f| f.starts_with('#')).collect(); + fills.sort_unstable(); + fills.dedup(); + assert_eq!(fills, ["#656565", "#666666"], "{svg}"); assert_looks_alike(&d, &svg, in_square, 2, 0.0); } From cdc355d4367dd431a738f33e80822e25d3502b1d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 1 Dec 2026 02:27:45 -0800 Subject: [PATCH 2/2] Tests: check the drawn H's vertical position and each mesh column's color The composite import test now checks the H's top and bottom as well as its width and height, so a shift of the drawn glyph fails it. The mesh export test samples each patch's center, so swapped column fills fail it. --- crates/eps/src/import/tests_composite.rs | 2 ++ crates/svg/tests/it/mesh_export.rs | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/crates/eps/src/import/tests_composite.rs b/crates/eps/src/import/tests_composite.rs index 6e3a63373d..4caf968271 100644 --- a/crates/eps/src/import/tests_composite.rs +++ b/crates/eps/src/import/tests_composite.rs @@ -67,6 +67,8 @@ fn type42_descendants_draw_their_own_glyphs() { // The bundled font's H spans (90, 0)–(562, 656) of its 1000-unit em: at 50 pt from x = 10 that // is 14.5 to 38.1, 32.8 tall. assert!((bh.x0 - 14.5).abs() < 0.01 && (bh.width() - 23.6).abs() < 0.01 && (bh.height() - 32.8).abs() < 0.01, "{bh:?}"); + // In the y-down document the baseline at y = 20 lands at 80, and the cap height 32.8 above it at 47.2. + assert!((bh.y0 - 47.2).abs() < 0.01 && (bh.y1 - 80.0).abs() < 0.01, "{bh:?}"); assert!((bh.height() - bi.height()).abs() < 0.5, "{bh:?} {bi:?}"); assert!(bi.x0 > 10.0 + 50.0 * wh - 1.0 && bi.x1 < 10.0 + 50.0 * (wh + wi), "{bi:?}"); } diff --git a/crates/svg/tests/it/mesh_export.rs b/crates/svg/tests/it/mesh_export.rs index c46b943734..281d3b1571 100644 --- a/crates/svg/tests/it/mesh_export.rs +++ b/crates/svg/tests/it/mesh_export.rs @@ -89,6 +89,11 @@ fn a_patch_is_cut_no_finer_than_its_colour_changes() { fills.sort_unstable(); fills.dedup(); assert_eq!(fills, ["#656565", "#666666"], "{svg}"); + // The left column's pieces take 101 and the right column's 102, in both rows. + let img = resvg_render(&svg); + for (x, y, want) in [(60, 60, 101), (60, 140, 101), (140, 60, 102), (140, 140, 102)] { + assert_eq!(&img.pixel(x, y)[..3], &[want; 3], "({x}, {y}) {svg}"); + } assert_looks_alike(&d, &svg, in_square, 2, 0.0); }