diff --git a/crates/engine/src/vector_cmds/tests.rs b/crates/engine/src/vector_cmds/tests.rs index e0abe8e77b..6f41eb03c1 100644 --- a/crates/engine/src/vector_cmds/tests.rs +++ b/crates/engine/src/vector_cmds/tests.rs @@ -648,3 +648,21 @@ fn path_stroke_rejects_oversized_brush_before_rendering() { let result = s.execute("path.stroke", json!({"size": 1e30})); assert!(result.is_err(), "path strokes share the bounded paint renderer"); } + +#[test] +fn shape_info_bounds_of_curved_shapes_are_exact() { + // Issue #2537: an exact 600×600 ellipse reported width 601. + for depth in [8, 16, 32] { + let mut s = session(1920, 1080, depth); + for (params, want) in [ + (json!({"kind": "ellipse", "rect": [0, 0, 100, 100], "fill": "#0000ff"}), json!([0, 0, 100, 100])), + (json!({"kind": "ellipse", "rect": [660, 240, 600, 600], "fill": "#0000ff"}), json!([660, 240, 600, 600])), + (json!({"kind": "ellipse", "rect": [10.5, 10, 100, 100], "fill": "#0000ff"}), json!([10, 10, 101, 100])), + (json!({"kind": "rect", "rect": [0, 0, 100, 100], "fill": "#0000ff"}), json!([0, 0, 100, 100])), + ] { + let id = s.execute("shape.create", params.clone()).unwrap()["layer"].as_u64().unwrap(); + let info = s.execute("shape.info", json!({"layer": id})).unwrap(); + assert_eq!(info["bounds"], want, "{params} at {depth}-bit"); + } + } +} diff --git a/crates/vector/src/raster.rs b/crates/vector/src/raster.rs index 77af7f75e4..ba2cdd21ff 100644 --- a/crates/vector/src/raster.rs +++ b/crates/vector/src/raster.rs @@ -15,6 +15,10 @@ use photocraft_doc::{FillRule, PathOp}; use photocraft_geom::Rect; +/// Coverage below this is f32 round-off in the cell prefix sum, not area: it is well under half +/// a 16-bit step (7.6e-6), so 8- and 16-bit output is unchanged by dropping it. +const RESIDUE: f32 = 1e-6; + #[derive(Clone, Copy, Debug)] struct Edge { x0: f64, @@ -216,7 +220,10 @@ impl Rasterizer { let mut sum = 0.0f32; for (o, a) in row_out.iter_mut().zip(st.acc.iter()) { sum += *a; - *o = sum.clamp(0.0, 1.0); + // Opposite boundaries don't cancel exactly in f32, leaving ~1e-7 just past a + // shape's right edge; that residue would count as content (#2537). Snap it (and + // a NaN) to 0. + *o = if sum >= RESIDUE { sum.min(1.0) } else { 0.0 }; } } } diff --git a/crates/vector/src/tests.rs b/crates/vector/src/tests.rs index 0a49cbeac9..18b2f6f18a 100644 --- a/crates/vector/src/tests.rs +++ b/crates/vector/src/tests.rs @@ -514,3 +514,59 @@ fn joined_subpaths_fill_as_one_component() { let lone = Path::new(vec![Subpath::polygon(&[(2.0, 2.0), (8.0, 2.0), (8.0, 8.0)]).with_op(PathOp::Join)]); assert_eq!(cov(&lone, r)[3 * 20 + 6], 1.0); } + +/// Content bounds of a filled shape rendered as a layer (every depth) and as a selection +/// (every depth); they must all equal `want`. +fn assert_rendered_bounds(path: &Path, want: Rect, what: &str) { + let canvas = Rect::new(0, 0, 1920, 1080); + let sh = ShapeLayer { path: path.clone(), fill: Some(Fill::Solid(Color::rgb(0.0, 0.0, 1.0))), ..Default::default() }; + for sample in SampleType::ALL { + let fmt = PixelFormat::new(photocraft_color::ColorMode::Rgb, sample, true); + assert_eq!(render_shape(&sh, fmt, canvas).content_bounds(), want, "{what}: layer {sample:?}"); + let sel = coverage_surface(&fill_rasterizer(path, DEFAULT_TOLERANCE), PixelFormat::new(photocraft_color::ColorMode::Grayscale, sample, false), canvas); + assert_eq!(sel.content_bounds(), want, "{what}: selection {sample:?}"); + } +} + +#[test] +fn curved_shape_bounds_are_exact() { + // Issue #2537: f32 round-off left ~1e-7 coverage just right of curved edges, so curved + // shapes reported bounds one pixel too wide. Rectangles never showed it. + let p = |x: f64, y: f64| photocraft_geom::Point::new(x, y); + // An asymmetric Bézier blob whose extremes lie on its anchors (axis-parallel handles). + let blob = Path::new(vec![Subpath { + closed: true, + knots: vec![ + photocraft_doc::Knot::smooth(p(60.0, 0.0), p(30.0, 0.0), p(80.0, 0.0)), + photocraft_doc::Knot::smooth(p(100.0, 45.0), p(100.0, 20.0), p(100.0, 70.0)), + photocraft_doc::Knot::smooth(p(40.0, 100.0), p(75.0, 100.0), p(15.0, 100.0)), + photocraft_doc::Knot::smooth(p(0.0, 55.0), p(0.0, 85.0), p(0.0, 25.0)), + ], + op: Default::default(), + }]); + let cases = [ + ("ellipse", shapes::ellipse(0.0, 0.0, 100.0, 100.0), Rect::new(0, 0, 100, 100)), + ("ellipse 600", shapes::ellipse(660.0, 240.0, 600.0, 600.0), Rect::new(660, 240, 1260, 840)), + ("ellipse at x.5", shapes::ellipse(10.5, 10.0, 100.0, 100.0), Rect::new(10, 10, 111, 110)), + ("rounded rect", shapes::rounded_rect(0.0, 0.0, 100.0, 100.0, [20.0; 4]), Rect::new(0, 0, 100, 100)), + // Outer vertices at x = 50 ± 50·cos 18° (2.45, 97.55), lowest at y = 50 + 50·sin 54° (90.45). + ("star", shapes::polygon(0.0, 0.0, 100.0, 100.0, 5, 0.5), Rect::new(2, 0, 98, 91)), + ("custom path", blob, Rect::new(0, 0, 100, 100)), + ("rect", shapes::rect(0.0, 0.0, 100.0, 100.0), Rect::new(0, 0, 100, 100)), + ]; + for (what, path, want) in &cases { + assert_rendered_bounds(path, *want, what); + } +} + +#[test] +fn thin_antialiased_edges_still_count_toward_bounds() { + // A quarter-pixel overhang and a 1-px bar straddling two columns are real coverage. + assert_rendered_bounds(&shapes::rect(0.0, 0.0, 100.25, 100.0), Rect::new(0, 0, 101, 100), "quarter overhang"); + assert_rendered_bounds(&shapes::rect(10.5, 0.0, 1.0, 50.0), Rect::new(10, 0, 12, 50), "1-px bar"); + assert_rendered_bounds(&shapes::rect(0.0, 0.0, 100.01, 10.0), Rect::new(0, 0, 101, 10), "1% sliver"); + // At 32-bit even a 1e-4 sliver is kept: only round-off is dropped. + let r = fill_rasterizer(&shapes::rect(0.0, 0.0, 100.0001, 10.0), DEFAULT_TOLERANCE); + let c = r.render(Rect::new(0, 0, 102, 10)); + assert!(c[100] > 5e-5 && c[101] == 0.0, "{} {}", c[100], c[101]); +}