PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(compose): prevent outside stroke from bleeding into opaque character interior (#5861)
seotarekac1bc012ef09
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rsindex 5acdd7931..550cb398a 100644--- a/crates/compose/src/effects.rs+++ b/crates/compose/src/effects.rs@@ -2093,23 +2093,8 @@ pub(crate) fn composite_with_effects_prepared(let mut share = vec![0.0f32; w * h];for (i, sh) in share.iter_mut().enumerate() {let band = (out_w + 0.5 - d[i]).clamp(0.0, 1.0);- let k = if maps.outline {- band * outline_share(shape.v[i], lay.px[i][3])- } else if inside(shape.v[i]) {- if vector_shape {- 0.0- } else {- // The stroke lies outside the layer's pixels. Beneath the layer it may show- // only through the part of the pixel the shape doesn't cover (1 - a); the- // layer composited on top covers c = a × fill, so the share beneath is- // (1 - a) / (1 - c). At 100 % fill that is 1 (the layer hides the rest, as- // before); at 0 % fill the interior stays clear (Fill 0 % + Outside stroke).- let c = lay.px[i][3].clamp(0.0, 1.0);- if c >= 1.0 { 1.0 } else { ((1.0 - shape.v[i].clamp(0.0, 1.0)) / (1.0 - c)).clamp(0.0, 1.0) }- }- } else {- band- };+ let k =+ if inside(shape.v[i]) { if vector_shape && !maps.outline { 0.0 } else { band * outline_share(shape.v[i], lay.px[i][3]) } } else { band };*sh = k * st.common.opacity * (1.0 - cover[i]);cover[i] += *sh;}@@ -2185,6 +2170,9 @@ pub(crate) fn composite_with_effects_prepared(/// layer, `share + l (1 - share) = (1 - cov) + l`.pub fn outline_share(cov: f32, l: f32) -> f32 {let outside = (1.0 - cov.clamp(0.0, 1.0)).max(0.0);+ if outside <= 1e-6 {+ return 0.0;+ }let rest = 1.0 - l.clamp(0.0, 1.0);if rest <= 1e-6 { 1.0 } else { (outside / rest).min(1.0) }}diff --git a/crates/compose/src/stroke_tests.rs b/crates/compose/src/stroke_tests.rsindex ab265fa20..97a0ee8c0 100644--- a/crates/compose/src/stroke_tests.rs+++ b/crates/compose/src/stroke_tests.rs@@ -91,6 +91,7 @@ fn outline_share_treats_stroke_and_shape_as_disjoint_areas() {// Outside the shape the stroke covers the pixel; inside it nothing.assert_eq!(effects::outline_share(0.0, 0.0), 1.0);assert_eq!(effects::outline_share(1.0, 0.3), 0.0);+ assert_eq!(effects::outline_share(1.0, 1.0), 0.0);// Beneath an opaque edge pixel the result ends opaque: share + l (1 - share) = 1 - cov + l.let (cov, l) = (0.6, 0.6);let s = effects::outline_share(cov, l);@@ -247,3 +248,47 @@ fn hairline_vector_stroke_keeps_the_files_edge_pixel_colour() {assert!(close4(px(&d, 20, 20), [fill, fill, fill, 1.0]), "{sample:?} {:?}", px(&d, 20, 20));}}++#[test]+fn outside_stroke_on_raster_layer_does_not_paint_inside_opaque_pixels() {+ let mut d = doc(40, 40);+ let mut pixels = photocraft_raster::Surface::new(PixelFormat::RGBA8);+ pixels.fill_rect(Rect::new(15, 15, 25, 25), &[1.0, 0.0, 0.0, 1.0]);+ let mut l = Layer::new("raster", LayerContent::Raster(pixels));+ l.effects.items = vec![stroke(3.0, StrokePosition::Outside, blue())];+ d.layers.push(l);+ let center = px(&d, 20, 20);+ assert!(close4(center, [1.0, 0.0, 0.0, 1.0]), "interior must stay red, got: {center:?}");+ let stroke_pixel = px(&d, 13, 20);+ assert!(close4(stroke_pixel, [0.0, 0.0, 1.0, 1.0]), "outside stroke must be blue, got: {stroke_pixel:?}");+}++#[test]+fn outside_stroke_on_zero_fill_raster_layer_keeps_interior_clear() {+ let mut d = Document::new("t", Size::new(40, 40), ColorMode::Rgb, SampleType::U8);+ let mut pixels = photocraft_raster::Surface::new(PixelFormat::RGBA8);+ pixels.fill_rect(Rect::new(15, 15, 25, 25), &[1.0, 0.0, 0.0, 1.0]);+ let mut l = Layer::new("raster", LayerContent::Raster(pixels));+ l.fill_opacity = 0.0;+ l.effects.items = vec![stroke(3.0, StrokePosition::Outside, blue())];+ d.layers.push(l);+ let center = px(&d, 20, 20);+ assert_eq!(center[3], 0.0, "interior with 0% fill must stay clear: {center:?}");+ let stroke_pixel = px(&d, 13, 20);+ assert!(close4(stroke_pixel, [0.0, 0.0, 1.0, 1.0]), "outside stroke must be blue: {stroke_pixel:?}");+}++#[test]+fn outside_stroke_on_partial_fill_raster_layer_keeps_interior_free_of_stroke() {+ let mut d = Document::new("t", Size::new(40, 40), ColorMode::Rgb, SampleType::U8);+ let mut pixels = photocraft_raster::Surface::new(PixelFormat::RGBA8);+ pixels.fill_rect(Rect::new(15, 15, 25, 25), &[1.0, 0.0, 0.0, 1.0]);+ let mut l = Layer::new("raster", LayerContent::Raster(pixels));+ l.fill_opacity = 0.5;+ l.effects.items = vec![stroke(3.0, StrokePosition::Outside, blue())];+ d.layers.push(l);+ let center = px(&d, 20, 20);+ assert!(close4(center, [1.0, 0.0, 0.0, 0.5]), "interior must be 50% red: {center:?}");+ let stroke_pixel = px(&d, 13, 20);+ assert!(close4(stroke_pixel, [0.0, 0.0, 1.0, 1.0]), "outside stroke must be blue: {stroke_pixel:?}");+}