From 9cb4ee70e2982c2d985a4ab1d6105d8dda69ba43 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 10:02:47 -0800 Subject: [PATCH] PICT: curved fills that coarsen to one line keep their area When a fill holds more points than one polygon, its curves are flattened more coarsely. A small figure could then come out as points that all lie on one line, which encloses nothing, and the figure lost its filled area. Such a figure now keeps its finer outline, as one that coarsens to two points already did. --- crates/metafile/src/pict.rs | 16 +++++++++++++--- crates/metafile/src/tests/pict_export.rs | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/metafile/src/pict.rs b/crates/metafile/src/pict.rs index 4b208210f7..b01c06c8ca 100644 --- a/crates/metafile/src/pict.rs +++ b/crates/metafile/src/pict.rs @@ -79,6 +79,15 @@ fn i16_of(v: f64) -> i16 { v.round().clamp(-MAX_I16, MAX_I16) as i16 } +/// Whether every point of `fig` lies on one line, so that it encloses no area. +fn collinear(fig: &[[i16; 2]]) -> bool { + let Some(&[x0, y0]) = fig.first() else { return true }; + let (x0, y0) = (i64::from(x0), i64::from(y0)); + let Some(&[x1, y1]) = fig.iter().find(|p| **p != fig[0]) else { return true }; + let (dx, dy) = (i64::from(x1) - x0, i64::from(y1) - y0); + fig.iter().all(|&[x, y]| dx * (i64::from(y) - y0) == dy * (i64::from(x) - x0)) +} + struct Writer { pic: PictWriter, /// Document space to device units. @@ -229,9 +238,10 @@ impl Writer { .flat_map(|(range, _)| { let subpath = BezPath::from_vec(path.elements()[range].to_vec()); let coarse = self.figures(&subpath, flatness); - // A small curved figure can collapse to two points. Keep its finer outline - // rather than leave it out; splitting and thinning still bound polygon size. - if coarse.is_empty() { + // A small curved figure can collapse to two points, or to points on one line. + // Keep its finer outline rather than lose its area; splitting and thinning + // still bound polygon size. + if coarse.is_empty() || coarse.iter().any(|fig| collinear(fig)) { self.figures(&subpath, FLATNESS) } else { coarsened |= subpath.elements().iter().any(|el| matches!(el, PathEl::QuadTo(..) | PathEl::CurveTo(..))); diff --git a/crates/metafile/src/tests/pict_export.rs b/crates/metafile/src/tests/pict_export.rs index 47d904fb2f..e7bb605606 100644 --- a/crates/metafile/src/tests/pict_export.rs +++ b/crates/metafile/src/tests/pict_export.rs @@ -377,6 +377,28 @@ fn pict_coarse_flattening_keeps_small_curved_figures() { assert!(out.warnings.iter().any(|w| w.contains("several polygons")), "{:?}", out.warnings); } +#[test] +fn pict_coarse_flattening_keeps_figures_that_become_one_line() { + // At the coarsest flatness the curve becomes a line, and its three points lie on one line. + let bumps: Vec = (0..2200) + .map(|i| { + let (x, y) = (f64::from(i % 50) * 1.5 + 1.0, f64::from(i / 50) * 1.5 + 1.0); + let mut bp = BezPath::new(); + bp.move_to((x, y)); + bp.quad_to((x + 0.5, y - 0.5), (x + 1.0, y)); + bp.line_to((x + 0.5, y)); + bp.close_path(); + bp + }) + .collect(); + let out = pict(&doc_with(100.0, 100.0, vec![compound(&bumps, FillRule::EvenOdd)])); + let pic = read(&out); + for i in [0_u32, 1100, 2199] { + let (x, y) = (f64::from(i % 50) * 1.5 + 1.0, f64::from(i / 50) * 1.5 + 1.0); + assert!(painted(&pic, (x + 0.5, y - 0.1)), "bump {i} was lost"); + } +} + #[test] fn a_fill_past_one_polygon_is_split_or_thinned() { // 3,000 separate squares: more points than one polygon holds.