From 2100c0a724266f384ce8d75f47ef782d3fcff154 Mon Sep 17 00:00:00 2001 From: superplay2021 Date: Fri, 20 Nov 2026 05:23:34 +0200 Subject: [PATCH] Preserve PSD alignment data and fix dashed rectangle corners --- crates/io/src/vector_map.rs | 11 ++++++++++- crates/vector/src/lib.rs | 7 +++++++ crates/vector/src/pixel_align.rs | 28 +++++++++++++++++++++++++++- crates/vector/src/tests.rs | 19 +++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/crates/io/src/vector_map.rs b/crates/io/src/vector_map.rs index 311b5e626d3..bad95fa68b3 100644 --- a/crates/io/src/vector_map.rs +++ b/crates/io/src/vector_map.rs @@ -559,7 +559,7 @@ pub fn shape_blocks(sh: &ShapeLayer, raw: &mut Vec<([u8; 4], Vec)>, w: u32, let find = |raw: &Vec<([u8; 4], Vec)>, k: &[u8; 4]| raw.iter().position(|(key, _)| key == k); // The layer's Align Edges setting, not the stroke's Stroke Adjust setting. match find(raw, b"sn2P") { - Some(i) if aligned_rendering(&raw[i].1) == Some(sh.use_aligned_rendering) => {} + Some(i) if aligned_rendering(&raw[i].1).unwrap_or(false) == sh.use_aligned_rendering => {} Some(i) => raw[i].1 = u32::from(sh.use_aligned_rendering).to_be_bytes().to_vec(), None if sh.use_aligned_rendering => raw.push((*b"sn2P", 1u32.to_be_bytes().to_vec())), None => {} @@ -884,6 +884,15 @@ mod align_edges_tests { fn align_edges_psd_layer_flag_accepts_nonzero_and_rejects_truncated_data() { for bytes in [vec![], vec![1], vec![0, 0, 1], vec![0, 0, 0, 1, 1]] { assert_eq!(aligned_rendering(&bytes), None); + let mut shape = ShapeLayer::default(); + shape_from_blocks(&mut shape, &|key| (key == b"sn2P").then(|| bytes.clone()), 64, 64, 72.0); + assert!(!shape.use_aligned_rendering); + let mut raw = vec![(*b"sn2P", bytes.clone())]; + shape_blocks(&shape, &mut raw, 64, 64, 72.0); + assert_eq!(raw[0].1, bytes); + shape.use_aligned_rendering = true; + shape_blocks(&shape, &mut raw, 64, 64, 72.0); + assert_eq!(raw[0].1, 1u32.to_be_bytes()); } for value in [1u32, 2, u32::MAX] { let data = value.to_be_bytes().to_vec(); diff --git a/crates/vector/src/lib.rs b/crates/vector/src/lib.rs index 20284950ac7..10b432ac7d2 100644 --- a/crates/vector/src/lib.rs +++ b/crates/vector/src/lib.rs @@ -741,6 +741,10 @@ impl CompiledShape { continue; } let clip = if *inside { *f } else { 1.0 - *f }; + if clip <= 0.0 { + *c = 0.0; + continue; + } let scale = self .pixel_rectangle .map_or(*scale, |bounds| pixel_align::rectangle_coverage_scale(bounds, rect.x0 + (i % w) as i32, rect.y0 + (i / w) as i32)); @@ -748,6 +752,9 @@ impl CompiledShape { } } self.pixel_dash_caps.sharpen_dash_caps(rect, &mut cov, self.pixel_dash_threshold, r, self.pixel_dash_bias.as_ref(), self.pixel_dash_centers); + if let Some(bounds) = self.pixel_rectangle.filter(|_| self.pixel_dash_bias.is_some()) { + pixel_align::restore_rectangle_dash_corners(bounds, rect, &mut cov, r); + } for (i, (o, c)) in out.iter_mut().zip(&cov).enumerate() { if *c <= 0.0 { continue; diff --git a/crates/vector/src/pixel_align.rs b/crates/vector/src/pixel_align.rs index 3192cc3d776..73015132fd7 100644 --- a/crates/vector/src/pixel_align.rs +++ b/crates/vector/src/pixel_align.rs @@ -402,6 +402,31 @@ pub(crate) fn rectangle_bounds(shape: &ShapeLayer, tolerance: (f64, f64)) -> Opt shape.path.control_bounds() } +/// At a half-pixel tie the inward row can have no source coverage near a guarded endpoint. +/// Sample the original dashed outline on its centerline so a dash gap still stays empty. +pub(crate) fn restore_rectangle_dash_corners((x0, y0, x1, y1): (f64, f64, f64, f64), rect: Rect, coverage: &mut [f32], stroke: &crate::raster::Rasterizer) { + let (left, top, right, bottom) = (snap_edge(x0) as i32, snap_edge(y0) as i32, snap_edge(x1) as i32, snap_edge(y1) as i32); + for (x, sx, dx) in [(left, x0, 1i32), (right.saturating_sub(1), x1, -1)] { + for (y, sy, dy) in [(top, y0, 1i32), (bottom.saturating_sub(1), y1, -1)] { + for (px, py, horizontal) in [(x, y, true), (x, y, false), (x.saturating_add(dx), y, true), (x, y.saturating_add(dy), false)] { + if px < left || px >= right || py < top || py >= bottom || px < rect.x0 || px >= rect.x1 || py < rect.y0 || py >= rect.y1 { + continue; + } + let (edge, row, side) = if horizontal { (sy, py, dy) } else { (sx, px, dx) }; + if !same(f64::from(row) + 0.5 - edge, f64::from(side)) { + continue; + } + let i = (py - rect.y0) as usize * rect.width() as usize + (px - rect.x0) as usize; + let Some(value) = coverage.get_mut(i).filter(|v| **v == 0.0) else { continue }; + let cell = Rect::new(px, py, px + 1, py + 1); + let probe = |pixel: i32, low: f64, high: f64| (f64::from(pixel) + 0.5).max(low).min(high) - f64::from(pixel); + let offset = if horizontal { (probe(px, x0, x1), sy - f64::from(py)) } else { (sx - f64::from(px), probe(py, y0, y1)) }; + *value = stroke.point_samples(cell, offset).first().copied().unwrap_or(0.0); + } + } + } +} + /// Normalize the selected interior row using its actual normal-axis coverage. This keeps /// quantized PSD edges opaque without rounding the path used for dash lengths and phase. pub(crate) fn rectangle_coverage_scale((x0, y0, x1, y1): (f64, f64, f64, f64), x: i32, y: i32) -> f32 { @@ -413,7 +438,8 @@ pub(crate) fn rectangle_coverage_scale((x0, y0, x1, y1): (f64, f64, f64, f64), x normal = normal.min(0.5 + (edge - rounded) * if inside_positive { 1.0 } else { -1.0 }); } } - (1.0 / normal) as f32 + let scale = normal.recip() as f32; + if scale.is_finite() { scale } else { 1.0 } } /// Bound actual tessellated joins, including sharp interior cusps. Source control diff --git a/crates/vector/src/tests.rs b/crates/vector/src/tests.rs index 1b31527f7b7..516ae4a784a 100644 --- a/crates/vector/src/tests.rs +++ b/crates/vector/src/tests.rs @@ -1790,3 +1790,22 @@ fn align_edges_half_ties_choose_increasing_coordinates() { let large_integer = 4_503_599_627_370_497.0; assert_eq!(super::pixel_align::snap_edge(large_integer), large_integer); } + +#[test] +fn pixel_aligned_half_pixel_dashes_keep_inner_corners_and_clip_outside() { + let rect = Rect::new(0, 0, 40, 40); + let shape = ShapeLayer { + path: shapes::rect(10.5, 10.5, 20.0, 20.0), + use_aligned_rendering: true, + stroke: Some(ShapeStroke { width: 1.0, dashes: vec![100.0, 1.0], ..Default::default() }), + ..Default::default() + }; + let pixels = CompiledShape::new(&shape, DEFAULT_TOLERANCE, rect).render_rgba(rect); + let alpha = |x: usize, y: usize| pixels[y * 40 + x][3]; + for (x, y) in [(11, 11), (12, 11), (11, 12)] { + assert_eq!(alpha(x, y), 1.0, "inside corner at {x},{y}"); + } + for (x, y) in [(10, 11), (11, 10), (10, 10)] { + assert_eq!(alpha(x, y), 0.0, "outside corner at {x},{y}"); + } +}