Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

test(text): per-colour passes match full-size passes (#8716)

FinnNkb8ee65e03089
diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs
index 83f699fc8..c34172270 100644
--- a/crates/text/src/tests.rs
+++ b/crates/text/src/tests.rs
@@ -2666,3 +2666,52 @@ fn a_word_wider_than_the_paragraph_box_breaks_at_the_box_edge() {
assert_eq!(l.lines.len(), 2, "{:?}", l.lines);
assert_eq!(l.lines[1].range.start, 3, "{:?}", l.lines);
}
+
+/// #8602: each colour is rasterised only over its own glyphs instead of the whole text, and the
+/// pixels match a full-size pass per colour (plain, faux bold, underlined, rotated and warped
+/// text; RGB and CMYK), up to float rounding in the coverage sums.
+#[test]
+fn per_colour_passes_match_full_size_passes() {
+ use photocraft_doc::text::TextWarp;
+ let mut e = TextEngine::new();
+ let text = "Many coloured runs of text, one colour a letter, across two lines\nof a point text layer";
+ let colours = [Color::rgb(0.9, 0.1, 0.1), Color::rgb(0.1, 0.7, 0.2), Color::rgb(0.1, 0.2, 0.9), Color::rgb(0.6, 0.6, 0.1)];
+ let runs = |bold: bool| {
+ text.char_indices()
+ .map(|(i, ch)| {
+ let colour = colours[i % colours.len()];
+ let style = CharStyle { size_pt: 28.0, color: colour, faux_bold: bold && i % 3 == 0, underline: i % 5 == 0, ..Default::default() };
+ TextRun { len: ch.len_utf8(), style }
+ })
+ .collect::<Vec<_>>()
+ };
+ let cases = [
+ TextLayer { text: text.into(), runs: runs(false), transform: Affine::translate(10.0, 40.0), ..Default::default() },
+ TextLayer { text: text.into(), runs: runs(true), transform: Affine::rotate(0.3).then(&Affine::translate(30.0, 90.0)), ..Default::default() },
+ TextLayer {
+ text: text.into(),
+ runs: runs(true),
+ transform: Affine::translate(10.0, 60.0),
+ warp: Some(TextWarp { style: "warpArc".into(), value: 40.0, horizontal_distortion: 0.0, vertical_distortion: 0.0, horizontal: true }),
+ ..Default::default()
+ },
+ ];
+ for (k, t) in cases.iter().enumerate() {
+ let l = e.layout(t, 72.0);
+ let warp = crate::render::layout_deform(&l, t);
+ for format in [PixelFormat::RGBA8, PixelFormat::CMYKA8] {
+ let fast = crate::render::rasterize_passes(&l, &t.transform, format, t.antialias, warp.as_ref(), true);
+ let full = crate::render::rasterize_passes(&l, &t.transform, format, t.antialias, warp.as_ref(), false);
+ assert_eq!(fast.rect, full.rect, "case {k}");
+ let (a, b) = (fast.surface.read_region(fast.rect), full.surface.read_region(full.rect));
+ assert!(!a.is_empty());
+ // Compare what shows: colour weighted by alpha (a fully transparent pixel's colour is moot).
+ let ch = format.channels();
+ let shown = |px: &[f32]| {
+ px.chunks_exact(ch).flat_map(|p| p[..ch - 1].iter().map(|c| c * p[ch - 1]).chain([p[ch - 1]]).collect::<Vec<_>>()).collect::<Vec<_>>()
+ };
+ let worst = shown(&a).iter().zip(&shown(&b)).map(|(x, y)| (x - y).abs()).fold(0.0f32, f32::max);
+ assert!(worst <= 1.0 / 255.0, "case {k} {format:?}: largest difference {worst}");
+ }
+ }
+}