Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Layer style: an Outside stroke at Fill below 100 % leaves the interior clear (#906)

llduyll10f985b81300ef
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs
index 273851893..c4f7d93ca 100644
--- a/crates/compose/src/effects.rs
+++ b/crates/compose/src/effects.rs
@@ -1377,7 +1377,17 @@ pub(crate) fn composite_with_effects_prepared(
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 { 1.0 }
+ 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
};
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index 6a62d5b2c..f6e143c1e 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -422,6 +422,29 @@ fn outside_stroke_width() {
assert!(close4(px(&d, 20, 20), [1.0, 0.0, 0.0, 1.0]), "interior untouched");
}
+#[test]
+fn outside_stroke_at_zero_fill_leaves_the_interior_clear() {
+ // Fill 0 % + an Outside stroke is the classic "outline only" look: the stroke lies outside
+ // the layer's pixels, so the interior shows the backdrop, not the stroke colour.
+ for (depth, fmt) in [(SampleType::U8, PixelFormat::RGBA8), (SampleType::U16, PixelFormat::RGBA16), (SampleType::F32, PixelFormat::RGBA32F)] {
+ let mut d = Document::with_background("t", Size::new(40, 40), ColorMode::Rgb, depth, Color::WHITE);
+ let mut l = Layer::raster("sq", fmt);
+ l.surface_mut().unwrap().fill_rect(Rect::new(10, 10, 30, 30), &[1.0, 0.0, 0.0, 1.0]);
+ l.effects.items = vec![stroke(3.0, StrokePosition::Outside)];
+ l.fill_opacity = 0.0;
+ d.layers.push(l);
+ assert!(close4(px(&d, 20, 20), [1.0; 4]), "{depth:?}: interior shows the white backdrop: {:?}", px(&d, 20, 20));
+ assert!(close4(px(&d, 11, 20), [1.0; 4]), "{depth:?}: just inside the edge too: {:?}", px(&d, 11, 20));
+ assert!(close4(px(&d, 8, 20), [0.0, 0.0, 1.0, 1.0]), "{depth:?}: the stroke itself stays: {:?}", px(&d, 8, 20));
+ // Partial fill: the interior is the layer at that fill over the backdrop, still no stroke.
+ d.layers[1].fill_opacity = 0.5;
+ assert!(close4(px(&d, 20, 20), [1.0, 0.5, 0.5, 1.0]), "{depth:?}: {:?}", px(&d, 20, 20));
+ // 100 % fill is unchanged.
+ d.layers[1].fill_opacity = 1.0;
+ assert!(close4(px(&d, 20, 20), [1.0, 0.0, 0.0, 1.0]), "{depth:?}: {:?}", px(&d, 20, 20));
+ }
+}
+
#[test]
fn inside_and_center_strokes() {
let d = fx_doc(vec![stroke(2.0, StrokePosition::Inside)]);
diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl
index 506f5f725..3e514171d 100644
--- a/crates/gpu/src/compose.wgsl
+++ b/crates/gpu/src/compose.wgsl
@@ -894,7 +894,8 @@ fn fs_fxstroke(in: VOut) -> @location(0) vec4<f32> {
let first = (op.flags & F_FIRST) != 0u;
var cover = 0.0;
var lay_a = 0.0;
- if (!first || outline) {
+ let vector = (op.flags & F_VECTOR) != 0u;
+ if (!first || outline || !vector) {
let cv = textureLoad(tex_c, p, 0);
cover = cv.r;
lay_a = cv.g;
@@ -903,7 +904,15 @@ fn fs_fxstroke(in: VOut) -> @location(0) vec4<f32> {
if (outline) {
k = k * outline_share(a, lay_a);
} else if (a > INSIDE_EPS) {
- k = select(1.0, 0.0, (op.flags & F_VECTOR) != 0u);
+ // Pixel layers: the stroke shows beneath only through the part the shape leaves
+ // (`effects.rs`): (1 - a) / (1 - layer alpha), 1 under an opaque layer.
+ if (vector) {
+ k = 0.0;
+ } else if (lay_a >= 1.0) {
+ k = 1.0;
+ } else {
+ k = clamp((1.0 - clamp(a, 0.0, 1.0)) / (1.0 - lay_a), 0.0, 1.0);
+ }
}
let share = k * op.opacity * (1.0 - cover);
if (op.kind == 1) { return vec4(cover + share, lay_a, 0.0, 0.0); }
diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rs
index aafcf28b6..9acc6df4f 100644
--- a/crates/gpu/src/plan.rs
+++ b/crates/gpu/src/plan.rs
@@ -342,7 +342,7 @@ enum Cov {
/// Shader flags for effect passes (keep in sync with compose.wgsl). Coverage `m` of a paint, with
/// `a` the layer's alpha and `inside` = `a > 0.5/255`:
/// knockout `m × (1 − a × k)` (k in `p4.w`); gate `inside ? m : 0`; rel `inside ? min(m / a, 1) : 0`;
-/// stroke-out `inside ? (vector ? 0 : 1) : m`.
+/// stroke-out `inside ? (vector ? 0 : (1 − a) / (1 − layer alpha), 1 at an opaque layer) : m`.
pub const F_KNOCKOUT: u32 = 16;
/// Effect merge: A already holds the layer; only mix with the backdrop by the layer's opacity.
pub const F_NO_LAYER: u32 = 32;
@@ -1110,8 +1110,9 @@ impl<'a> Planner<'a> {
.collect();
if !outs.is_empty() {
let (mut acc, mut cover): (Option<Slot>, Option<Slot>) = (None, None);
- if outline {
- // The layer's alpha rides along the coverage (`effects::outline_share`).
+ if outline || !vector_shape {
+ // The layer's alpha rides along the coverage: `effects::outline_share`, and for
+ // pixel layers the stroke's share beneath a partly covering layer (fill < 100 %).
let ll = self.retain(l);
cover = Some(init(self, ll, None, INIT_COVER, 1.0));
}
diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs
index cd4c77aa6..f78417dfc 100644
--- a/crates/gpu/tests/parity.rs
+++ b/crates/gpu/tests/parity.rs
@@ -874,6 +874,16 @@ fn layer_effects_opacity_fill_blend_and_off_canvas() {
d.layers.push(l);
fx_check(&mut g, &d, &format!("opacity {opacity} fill {fill} {blend:?}"));
}
+ // Fill below 100 % with an Outside stroke: the interior stays clear of the stroke (the
+ // "outline only" look of Fill 0 %).
+ for fill in [0.0, 0.5] {
+ let mut d = fx_doc(80, 80, SampleType::U8);
+ let mut l = blob("ring", d.pixel_format(), 40.0, 40.0, 20.0, [0.2, 0.6, 0.9]);
+ l.effects.items = vec![Effect::Stroke(stroke(4.0, StrokePosition::Outside, FxPaint::Color(Color::rgb(0.95, 0.85, 0.1))))];
+ l.fill_opacity = fill;
+ d.layers.push(l);
+ fx_check(&mut g, &d, &format!("outside stroke at fill {fill}"));
+ }
// Shapes partly off the canvas (their effects reach back in) and a masked effect layer.
let mut d = fx_doc(90, 70, SampleType::U8);
let mut l = blob("edge", d.pixel_format(), 4.0, 66.0, 18.0, [0.9, 0.9, 0.2]);