Use consistent half-pixel rounding across zero

This commit is contained in:
Gabriel Heuser committed 2026-11-10 00:32:53 +02:00
1 parent 2ea658d587
commit 345fe9c01c
3 files changed
+73 -10

No files matched your search

+4 -4
View File
@@ -94,12 +94,12 @@ fn stroke_rasterizer_with_alignment(path: &Path, style: &StrokeStyle, tol: f64,
let next = (index + 1) % original.len();
let (a, b) = (original[index], original[next]);
if a.0 == b.0 {
polygon[index].0 = a.0.round();
polygon[next].0 = b.0.round();
polygon[index].0 = pixel_align::snap_edge(a.0);
polygon[next].0 = pixel_align::snap_edge(b.0);
}
if a.1 == b.1 {
polygon[index].1 = a.1.round();
polygon[next].1 = b.1.round();
polygon[index].1 = pixel_align::snap_edge(a.1);
polygon[next].1 = pixel_align::snap_edge(b.1);
}
}
}
+15 -6
View File
@@ -1,10 +1,17 @@
//! Coverage-only alignment of one-pixel straight runs. Geometry and dash arc lengths are
//! never rounded. Curve tangencies and joins retain their original area antialiasing.
//! Render-only alignment of shape edges and straight one-pixel stroke runs. Stored
//! geometry and dash settings remain unchanged; curved interiors retain area antialiasing.
use photocraft_doc::{LiveShape, Path, ShapeLayer, StrokeAlign};
use photocraft_geom::Rect;
// Trigonometric live shapes and quarter-turn transforms carry tiny rounding errors.
const ALIGN_EPSILON: f64 = 1e-7;
/// Round an edge to the nearest pixel, choosing increasing coordinates at half ties.
/// Comparing the fractional part avoids addition rounding just below a half pixel.
pub(crate) fn snap_edge(coordinate: f64) -> f64 {
let lower = coordinate.floor();
if coordinate - lower >= 0.5 { lower + 1.0 } else { lower }
}
type CleanLine = (Vec<(f64, f64)>, Vec<bool>);
/// Compare source coordinates without treating flattened curve chords as straight segments.
fn same(a: f64, b: f64) -> bool {
@@ -482,14 +489,16 @@ pub(crate) fn aligned_edge_path(shape: &ShapeLayer) -> Option<Path> {
}
for ((target, original), (vertical, horizontal)) in subpath.knots.iter_mut().zip(source).zip(axes) {
if vertical && original.anchor.x.is_finite() {
let delta = original.anchor.x.round() - original.anchor.x;
target.anchor.x = original.anchor.x.round();
let snapped = snap_edge(original.anchor.x);
let delta = snapped - original.anchor.x;
target.anchor.x = snapped;
target.in_ctrl.x = original.in_ctrl.x + delta;
target.out_ctrl.x = original.out_ctrl.x + delta;
}
if horizontal && original.anchor.y.is_finite() {
let delta = original.anchor.y.round() - original.anchor.y;
target.anchor.y = original.anchor.y.round();
let snapped = snap_edge(original.anchor.y);
let delta = snapped - original.anchor.y;
target.anchor.y = snapped;
target.in_ctrl.y = original.in_ctrl.y + delta;
target.out_ctrl.y = original.out_ctrl.y + delta;
}
+54
View File
@@ -1688,3 +1688,57 @@ fn align_edges_fractional_stroke_width_keeps_area_coverage() {
assert_eq!(aligned[20 * 80 + 35][3], width.min(2.0) / 2.0);
}
}
/// Render the same document pixels after an integer translation of both geometry and clip.
fn aligned_pixels_after_translation(shape: &ShapeLayer, offset: [i32; 2]) -> Vec<[f32; 4]> {
let [dx, dy] = offset;
let shifted = ShapeLayer { path: shape.path.transform(&photocraft_geom::Affine::translate(f64::from(dx), f64::from(dy))), ..shape.clone() };
let clip = Rect::new(-30 + dx, -30 + dy, 50 + dx, 50 + dy);
CompiledShape::new(&shifted, DEFAULT_TOLERANCE, clip).render_rgba(clip)
}
/// A half-pixel rectangle spanning the origin keeps its dimensions and occupied pixels.
#[test]
fn align_edges_fill_half_ties_preserve_extent_across_zero() {
let shape =
ShapeLayer { path: shapes::rect(-10.5, -5.5, 31.0, 21.0), fill: Some(Fill::Solid(Color::BLACK)), use_aligned_rendering: true, ..Default::default() };
let reference = aligned_pixels_after_translation(&shape, [40, 30]);
assert!(reference.iter().all(|pixel| pixel[3] == 0.0 || pixel[3] == 1.0));
assert_eq!(reference.iter().filter(|pixel| pixel[3] == 1.0).count(), 31 * 21);
for offset in [[-40, -30], [0, 0], [40, 30]] {
let pixels = aligned_pixels_after_translation(&shape, offset);
let mismatch = pixels.iter().zip(&reference).position(|(actual, expected)| actual != expected);
assert_eq!(mismatch, None, "offset={offset:?}");
}
}
/// Odd-width outlines have half-pixel boundaries even when their source path is integral.
#[test]
fn align_edges_three_pixel_stroke_translates_across_zero() {
let shape = ShapeLayer {
path: shapes::rect(-10.0, -5.0, 31.0, 21.0),
stroke: Some(ShapeStroke { width: 3.0, ..Default::default() }),
use_aligned_rendering: true,
..Default::default()
};
let reference = aligned_pixels_after_translation(&shape, [40, 30]);
assert!(reference.iter().all(|pixel| pixel[3] == 0.0 || pixel[3] == 1.0));
assert_eq!(reference.iter().filter(|pixel| pixel[3] == 1.0).count(), 2 * (31 + 21) * 3);
for offset in [[-40, -30], [0, 0], [40, 30]] {
let pixels = aligned_pixels_after_translation(&shape, offset);
let mismatch = pixels.iter().zip(&reference).position(|(actual, expected)| actual != expected);
assert_eq!(mismatch, None, "offset={offset:?}");
}
}
/// The common edge rule is half-up on both sides of zero and preserves nearby non-ties.
#[test]
fn align_edges_half_ties_choose_increasing_coordinates() {
for (source, expected) in [(-10.5, -10.0), (-0.5, 0.0), (0.5, 1.0), (10.5, 11.0), (-10.75, -11.0), (10.25, 10.0)] {
assert_eq!(super::pixel_align::snap_edge(source), expected, "source={source}");
}
let below_half = f64::from_bits(0.5_f64.to_bits() - 1);
assert_eq!(super::pixel_align::snap_edge(below_half), 0.0);
let large_integer = 4_503_599_627_370_497.0;
assert_eq!(super::pixel_align::snap_edge(large_integer), large_integer);
}