From 528fb9abb2d7d91cc6f85c98218673c0ee06a319 Mon Sep 17 00:00:00 2001 From: superplay2021 Date: Sat, 14 Nov 2026 15:35:30 +0200 Subject: [PATCH] Fix aligned dash phase and clarify rendering limits --- crates/vector/src/lib.rs | 25 +++++++++++++---- crates/vector/src/pixel_align.rs | 39 +++++++++++++++++++++++--- crates/vector/src/tests.rs | 48 ++++++++++++++++++++++++++++++++ docs/control-protocol.md | 6 ++-- docs/pixel-aligned-strokes.md | 7 +++-- 5 files changed, 112 insertions(+), 13 deletions(-) diff --git a/crates/vector/src/lib.rs b/crates/vector/src/lib.rs index d5fc72fc32b..20284950ac7 100644 --- a/crates/vector/src/lib.rs +++ b/crates/vector/src/lib.rs @@ -633,6 +633,9 @@ impl CompiledShape { /// out in `gradient_frame`: measuring a shape beyond the canvas it is drawn in. pub fn with_frames(shape: &ShapeLayer, tol: f64, coverage: Rect, gradient_frame: Rect) -> Self { let pb = shape.path.control_bounds().unwrap_or((0.0, 0.0, 0.0, 0.0)); + // Dash arc lengths and phase belong to the stored geometry. Snap the + // resulting outline/coverage, never the path fed to dash generation. + let dashed_path = shape.stroke.as_ref().filter(|s| !s.dashes.is_empty()).map(|_| &shape.path); let aligned_shape = pixel_align::aligned_edge_path(shape).map(|path| ShapeLayer { path, live: None, ..shape.clone() }); let shape = aligned_shape.as_ref().unwrap_or(shape); let g = gradient_frame; @@ -648,18 +651,22 @@ impl CompiledShape { let all_closed = shape.path.subpaths.iter().all(|s| s.closed); let mut align = None; let coordinate_tolerance = pixel_align::coordinate_tolerance(shape, gradient_frame); - let pixel_rectangle = pixel_align::rectangle_bounds(shape, coordinate_tolerance); + let pixel_rectangle = + pixel_align::rectangle_bounds(shape, coordinate_tolerance).map(|bounds| dashed_path.and_then(Path::control_bounds).unwrap_or(bounds)); let mut pixel_stroke = pixel_align::PixelRuns::stroke(shape, tol, coordinate_tolerance); // The stroker's round caps preserve circular area. Their half-circle // outside an integer dash boundary has area pi/8 at width 1. Use that // tie instead of 1/2, retaining the caps' extra pixel of raster length. let pixel_dash_threshold = if shape.stroke.as_ref().is_some_and(|s| s.cap == photocraft_doc::LineCap::Round) { std::f32::consts::PI / 8.0 } else { 0.5 }; - let pixel_dash_caps = if shape.stroke.as_ref().is_some_and(|s| !s.dashes.is_empty()) { + let mut pixel_dash_caps = if shape.stroke.as_ref().is_some_and(|s| !s.dashes.is_empty()) { pixel_align::PixelRuns::stroke(shape, tol, coordinate_tolerance) } else { pixel_align::PixelRuns::default() }; + if let Some(path) = dashed_path { + pixel_dash_caps.use_source_coordinates(path); + } let mut pixel_dash_bias = None; let stroke = shape.stroke.as_ref().filter(|s| s.width > 0.0 && s.opacity > 0.0).map(|s| { let mut st = stroke_style(s); @@ -669,13 +676,21 @@ impl CompiledShape { align = Some((area.clone(), s.align == StrokeAlign::Inside, 1.0)); } if let Some((x0, y0, x1, y1)) = pixel_rectangle { - pixel_stroke = pixel_align::PixelRuns::default(); + if dashed_path.is_some() { + // The original stroke can sit entirely outside the selected row + // at a half-pixel tie. Move its normal coverage before clipping. + pixel_stroke.select_rectangle_interior((x0, y0, x1, y1)); + pixel_dash_caps.select_rectangle_interior((x0, y0, x1, y1)); + } else { + pixel_stroke = pixel_align::PixelRuns::default(); + } // Preserve dash positions from the original vector path. // Select the interior pixel row for crisp 1px strokes. - let mask = shapes::rect(x0.round(), y0.round(), x1.round() - x0.round(), y1.round() - y0.round()); + let (x0, y0, x1, y1) = (pixel_align::snap_edge(x0), pixel_align::snap_edge(y0), pixel_align::snap_edge(x1), pixel_align::snap_edge(y1)); + let mask = shapes::rect(x0, y0, x1 - x0, y1 - y0); align = Some((fill_rasterizer(&mask, tol), true, 2.0)); } - let r = stroke_rasterizer_with_alignment(&shape.path, &st, tol, shape.use_aligned_rendering); + let r = stroke_rasterizer_with_alignment(dashed_path.unwrap_or(&shape.path), &st, tol, shape.use_aligned_rendering); if !pixel_dash_caps.is_empty() { // A tiny positive translation resolves only otherwise indistinguishable cap // ties. Reuse the compiled outline so dash lengths, phase and budgets agree. diff --git a/crates/vector/src/pixel_align.rs b/crates/vector/src/pixel_align.rs index 94d9a9ed4eb..3192cc3d776 100644 --- a/crates/vector/src/pixel_align.rs +++ b/crates/vector/src/pixel_align.rs @@ -36,6 +36,8 @@ pub(crate) struct PixelRuns(Vec); struct Run { horizontal: bool, coordinate: i32, + negative_side: bool, + sample_offset: f64, start: f64, end: f64, } @@ -69,7 +71,7 @@ impl PixelRuns { // Probe the original outline at the centerline and along-axis pixel centers. // The small negative bias preserves the existing positive-coordinate tie policy. let centers_rect = if run.horizontal { Rect::new(visible.x0, c, visible.x1, c + 1) } else { Rect::new(c, visible.y0, c + 1, visible.y1) }; - let offset = if run.horizontal { (0.5 - 1e-6, 0.0) } else { (0.0, 0.5 - 1e-6) }; + let offset = if run.horizontal { (0.5 - 1e-6, run.sample_offset) } else { (run.sample_offset, 0.5 - 1e-6) }; if sample_centers { let centers = rasterizer.point_samples(centers_rect, offset); for y in visible.y0..visible.y1 { @@ -172,7 +174,7 @@ impl PixelRuns { { return Self::default(); } - Self(vec![Run { horizontal, coordinate: coordinate.round() as i32, start: start + 2.0, end: end - 2.0 }]) + Self(vec![Run { horizontal, coordinate: coordinate.round() as i32, negative_side: false, sample_offset: 0.0, start: start + 2.0, end: end - 2.0 }]) } /// Find straight source segments with bounded overlap checks; curves retain normal coverage. fn path(path: &Path, coordinate_tolerance: (f64, f64)) -> Self { @@ -199,6 +201,35 @@ impl PixelRuns { } runs } + /// Probe dash centers on the stored segment even when its normal coordinate snapped. + /// Run discovery already caps the source to 256 knots and excludes overlaps. + pub(crate) fn use_source_coordinates(&mut self, path: &Path) { + if self.is_empty() { + return; + } + let segments: Vec<_> = path.subpaths.iter().flat_map(|s| s.segments()).collect(); + for run in &mut self.0 { + for points in &segments { + let coordinate = if run.horizontal { points[0].y } else { points[0].x }; + let on_axis = points.iter().all(|p| same(if run.horizontal { p.y } else { p.x }, coordinate)); + let (a, b) = if run.horizontal { (points[0].x, points[3].x) } else { (points[0].y, points[3].y) }; + if on_axis && snap_edge(coordinate) == f64::from(run.coordinate) && a.min(b) <= run.start && a.max(b) >= run.end { + run.sample_offset = coordinate - f64::from(run.coordinate); + break; + } + } + } + } + + /// Rectangle strokes select the interior side on the right and bottom edges. + pub(crate) fn select_rectangle_interior(&mut self, (x0, y0, x1, y1): (f64, f64, f64, f64)) { + for run in &mut self.0 { + let (low, high) = if run.horizontal { (y0, y1) } else { (x0, x1) }; + run.negative_side = f64::from(run.coordinate) == snap_edge(high); + run.sample_offset = if run.negative_side { high } else { low } - f64::from(run.coordinate); + } + } + /// Combine normal-axis coverage in the selected pixel, sampling beyond tile boundaries. pub(crate) fn apply(&self, rect: Rect, coverage: &mut [f32], sample: impl Fn(Rect) -> Vec) { let width = rect.width() as usize; @@ -221,7 +252,7 @@ impl PixelRuns { let k = if run.horizontal { (x - source.x0) as usize } else { (y - source.y0) as usize * sw }; let adjacent = if run.horizontal { k + sw } else { k + 1 }; if let (Some(out), Some(a), Some(b)) = (coverage.get_mut(index), values.get(k), values.get(adjacent)) { - *out = if (if run.horizontal { y } else { x }) == c { (*a + *b).min(1.0) } else { 0.0 }; + *out = if (if run.horizontal { y } else { x }) == c - i32::from(run.negative_side) { (*a + *b).min(1.0) } else { 0.0 }; } } } @@ -376,7 +407,7 @@ pub(crate) fn rectangle_bounds(shape: &ShapeLayer, tolerance: (f64, f64)) -> Opt pub(crate) fn rectangle_coverage_scale((x0, y0, x1, y1): (f64, f64, f64, f64), x: i32, y: i32) -> f32 { let mut normal: f64 = 1.0; for (pixel, edge, inside_positive) in [(x, x0, true), (x, x1, false), (y, y0, true), (y, y1, false)] { - let rounded = edge.round(); + let rounded = snap_edge(edge); let row = (rounded as i32).saturating_sub(i32::from(!inside_positive)); if pixel == row { normal = normal.min(0.5 + (edge - rounded) * if inside_positive { 1.0 } else { -1.0 }); diff --git a/crates/vector/src/tests.rs b/crates/vector/src/tests.rs index 527a6b9e5f1..1b31527f7b7 100644 --- a/crates/vector/src/tests.rs +++ b/crates/vector/src/tests.rs @@ -1324,6 +1324,54 @@ fn pixel_aligned_dashes_have_uniform_opaque_lengths_with_half_pixel_periods() { } } +/// Edge alignment must not measure later sides using the snapped rectangle's perimeter. +#[test] +fn pixel_aligned_fractional_rectangle_preserves_dash_phase_on_later_sides() { + let clip = Rect::new(-32, -32, 48, 48); + for (start, end) in [(10.2, 30.6), (10.5, 30.5), (-10.5, 9.5)] { + let length = end - start; + let low = crate::pixel_align::snap_edge(start) as i32; + let high = crate::pixel_align::snap_edge(end) as i32 - 1; + for (width, offset) in [1.0, 2.0, 3.0].into_iter().flat_map(|width| [-0.75, 0.0, 0.5].map(|offset| (width, offset))) { + let shape = ShapeLayer { + path: shapes::rect(start, start, length, length), + use_aligned_rendering: true, + stroke: Some(ShapeStroke { width, dashes: vec![4.0, 2.0], dash_offset: offset, ..Default::default() }), + ..Default::default() + }; + let original = shape.path.clone(); + let compiled = CompiledShape::new(&shape, DEFAULT_TOLERANCE, clip); + for p in low + 4..high - 2 { + let probe = f64::from(p) + 0.5 - 1e-6; + for (x, y, distance) in + [(p, low, probe - start), (high, p, length + probe - start), (p, high, 2.0 * length + end - probe), (low, p, 3.0 * length + end - probe)] + { + let expected = f32::from((distance / f64::from(width) + f64::from(offset)).rem_euclid(6.0) < 4.0); + let actual = compiled.render_rgba(Rect::from_xywh(x, y, 1, 1))[0][3]; + assert_eq!(actual, expected, "dash phase at ({x}, {y}), width={width}, offset={offset}"); + } + } + assert_eq!(shape.path, original); + } + } +} + +#[test] +fn pixel_aligned_fractional_open_dashes_probe_the_original_centerline() { + let clip = Rect::new(0, 0, 80, 40); + let shape = ShapeLayer { + path: Path::new(vec![Subpath::polyline(&[(10.2, 20.5), (70.6, 20.5)])]), + use_aligned_rendering: true, + stroke: Some(ShapeStroke { width: 1.0, dashes: vec![4.0, 2.0], ..Default::default() }), + ..Default::default() + }; + let compiled = CompiledShape::new(&shape, DEFAULT_TOLERANCE, clip); + for x in 14..67 { + let expected = f32::from((f64::from(x) + 0.5 - 1e-6 - 10.2).rem_euclid(6.0) < 4.0); + assert_eq!(compiled.render_rgba(Rect::from_xywh(x, 21, 1, 1))[0][3], expected, "x={x}"); + } +} + /// Fractional butt dashes are selected by their centers, not their integrated area. #[test] fn pixel_aligned_fractional_butt_dashes_select_pixel_centers() { diff --git a/docs/control-protocol.md b/docs/control-protocol.md index d6c8f20a7ea..d8ee6a231f9 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -2,8 +2,10 @@ `ui.set {useAlignedRendering: true}` changes the Align Edges tool preference for subsequent shape creation. `ui.inspect.toolOptions.useAlignedRendering` reports -that preference, not the selected shape's setting. With an existing shape selected, -the options-bar checkbox displays the layer's value. Use +that preference, not the selected shape's setting. Shape-tool options display the +selected shape's value. The Pen displays the target layer's value when appending +to it, the captured value during a valid draft, or the tool preference for New Layer. +Use `shape.edit {layer: id, useAlignedRendering: true}` to change an existing layer and `shape.info {layer: id}` to read its `useAlignedRendering` value (engine commands are invoked through `engine.execute` on this control channel). diff --git a/docs/pixel-aligned-strokes.md b/docs/pixel-aligned-strokes.md index b8d0e317d9c..8ead69d0f99 100644 --- a/docs/pixel-aligned-strokes.md +++ b/docs/pixel-aligned-strokes.md @@ -12,13 +12,16 @@ path; it does not toggle render-time Align Edges or edit an existing layer. Alignment affects both fill and stroke. Straight horizontal and vertical source edges at fractional coordinates snap to document pixel boundaries on a render-only -copy. Axis-aligned curve extrema snap too, including ellipse boundaries. Handles +copy. Curve extrema at anchors with axis-aligned handles snap too, including +ellipse boundaries; extrema inside a Bézier segment are not detected. Handles move with their anchors, preserving adjoining curve tangents. Curves and diagonals retain antialiasing; flattened curve chords are never snapped. Stored paths, live parameters, stroke widths and dash settings are unchanged. Turning the option off restores the original coverage. Existing one-pixel stroke and dash-cap coverage rules remain in use after edge alignment. Straight outline -edges of wider integer-width strokes also snap. Centered fractional-width strokes +edges of wider integer-width strokes also snap when every source knot has +coincident handles. Paths with curved segments, including rounded rectangles, +retain outline antialiasing on their straight sides too. Centered fractional-width strokes keep their area coverage so a subpixel outline cannot collapse to zero width. Inside/outside strokes use a doubled outline before clipping to the fill; some fractional requested widths therefore produce a snapped integral-width outline