Merge pull request #4348 from Terra854/fix/shape-stroke-composite-4317

Shapes: render Stroke Composite within its group
This commit is contained in:
FredApps authored and GitHub committed 2026-12-02 19:51:43 +01:00
commit 10b1512c20
2 files changed
+203 -7

No files matched your search

+22 -7
View File
@@ -6,7 +6,8 @@
//! same group, including the paths of nested groups;
//! - paint items (fills, strokes, gradient fills/strokes) paint the paths above them, *as modified
//! by every operator in the stack* — a Trim Paths below a Stroke still trims it;
//! - items higher in the list draw on top;
//! - items higher in the list draw on top, unless a paint's Composite override places it
//! above previous paints in its own group;
//! - surface items (Front, Bevel, Side, Back) colour those surfaces of the Advanced 3D
//! extrusion of every paint in their group and its nested groups (the innermost wins).
@@ -65,6 +66,17 @@ struct Draw {
local: bool,
}
fn push_paint(draws: &mut Vec<Draw>, draw: Draw, above: bool) {
// Only previous paints owned by this group are overridden. A nested group's
// paint is not a previous local paint; keep existing draws in relative order,
// including when a nested group separates two local paints.
if above && let Some(index) = draws.iter().position(|d| d.local) {
draws.insert(index, draw);
} else {
draws.push(draw);
}
}
/// Straight RGB colours of an extrusion's surfaces, by [`effectcraft_model::extrude::Surface`]
/// index (Front, Bevel, Side, Back); `None` keeps the paint's colour.
pub type SurfaceColors = [Option<[f32; 3]>; 4];
@@ -312,7 +324,7 @@ fn collect_in(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, arena: &mut Ve
"fill" => {
let mut c = ctx.color(layer, g, "color");
c[3] = 1.0;
draws.push(Draw {
let draw = Draw {
paint: Paint::Fill { color: c, rule: rule(ctx, layer, g) },
slots: live.clone(),
xf: Mat3::IDENTITY,
@@ -320,13 +332,14 @@ fn collect_in(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, arena: &mut Ve
blend: blend_mode(ctx, layer, g),
surfaces: [None; 4],
local: true,
});
};
push_paint(&mut draws, draw, ctx.e(layer, g, "composite") == 1);
}
"stroke" => {
let mut c = ctx.color(layer, g, "color");
c[3] = 1.0;
let style = stroke_style(ctx, layer, g);
draws.push(Draw {
let draw = Draw {
paint: Paint::Stroke { color: c, style },
slots: live.clone(),
xf: Mat3::IDENTITY,
@@ -334,7 +347,8 @@ fn collect_in(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, arena: &mut Ve
blend: blend_mode(ctx, layer, g),
surfaces: [None; 4],
local: true,
});
};
push_paint(&mut draws, draw, ctx.e(layer, g, "composite") == 1);
}
"gfill" | "gstroke" => {
let gr = match ctx.group_value(layer, g, "colors") {
@@ -342,7 +356,7 @@ fn collect_in(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, arena: &mut Ve
_ => Gradient::default(),
};
let stroke = (g.match_id == "gstroke").then(|| stroke_style(ctx, layer, g));
draws.push(Draw {
let draw = Draw {
paint: Paint::Gradient {
g: gr,
radial: ctx.e(layer, g, "type") == 1,
@@ -358,7 +372,8 @@ fn collect_in(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, arena: &mut Ve
blend: blend_mode(ctx, layer, g),
surfaces: [None; 4],
local: true,
});
};
push_paint(&mut draws, draw, g.match_id == "gstroke" && ctx.e(layer, g, "composite") == 1);
}
"trim" => {
let (s, e, o) = (ctx.f(layer, g, "start", 0.0), ctx.f(layer, g, "end", 100.0), ctx.f(layer, g, "offset", 0.0));
+181
View File
@@ -58,6 +58,187 @@ fn op(ids: &mut Ids, kind: &str, params: &[(&str, Value)]) -> PropGroup {
const RED: [f64; 4] = [1.0, 0.0, 0.0, 1.0];
const GREEN: [f64; 4] = [0.0, 1.0, 0.0, 1.0];
#[test]
fn stroke_composite_changes_the_render_with_fixed_contents_order() {
let draw = |composite: Option<u32>, move_stroke: bool| {
let (p, cid) = shape_comp_sized(100, 100, |ids| {
let rect = build::shape_rect(ids, [60.0, 60.0], [0.0; 2], 0.0);
let fill = build::shape_fill(ids, [0.0, 0.0, 1.0, 1.0]);
let mut stroke = build::shape_stroke(ids, RED, 16.0);
if let Some(value) = composite {
set(&mut stroke, "composite", Value::Enum(value));
}
if move_stroke { vec![rect, stroke, fill] } else { vec![rect, fill, stroke] }
});
frame(&p, cid)
};
let default = draw(None, false);
let below = draw(Some(0), false);
let above = draw(Some(1), false);
assert!(default.data == below.data, "the default remains Below Previous");
assert!(below.get(24, 50)[2] > 0.99, "Fill covers the inner half of a Below stroke");
assert!(above.get(24, 50)[0] > 0.99, "Above puts the stroke over the fill: {:?}", above.get(24, 50));
assert!(above.data == draw(None, true).data, "Composite is the paint-order override, without moving Contents");
for img in [&below, &above] {
assert!(img.get(50, 50)[2] > 0.99, "the centre stays filled");
assert!(img.get(16, 50)[0] > 0.99, "the outer half stays stroked");
}
}
#[test]
fn composite_is_shared_by_fill_and_gradient_stroke_paint() {
for gradient in [false, true] {
let draw = |above: bool, moved: bool| {
let (p, cid) = shape_comp_sized(100, 100, |ids| {
let rect = build::shape_rect(ids, [60.0, 60.0], [0.0; 2], 0.0);
let fill = build::shape_fill(ids, [0.0, 0.0, 1.0, 1.0]);
let stroke = if gradient {
let colors =
Gradient { colors: vec![(0.0, RED.map(|v| v as f32)), (1.0, GREEN.map(|v| v as f32))], opacities: vec![(0.0, 1.0), (1.0, 1.0)] };
build::shape_gradient_stroke(ids, false, [-30.0, 0.0], [30.0, 0.0], colors, 16.0)
} else {
build::shape_stroke(ids, RED, 16.0)
};
let (previous, mut paint) = if gradient { (fill, stroke) } else { (stroke, fill) };
set(&mut paint, "composite", Value::Enum(u32::from(above)));
if moved { vec![rect, paint, previous] } else { vec![rect, previous, paint] }
});
frame(&p, cid)
};
let (below, above) = (draw(false, false), draw(true, false));
assert!(below.data != above.data, "gradient stroke={gradient}: Composite must change the frame");
assert!(above.data == draw(false, true).data, "the same override applies to the exposed paint property");
}
}
#[test]
fn composite_does_not_promote_parent_paint_over_a_nested_group() {
for own_fill in [false, true] {
let draw = |above: bool| {
let (p, cid) = shape_comp_sized(100, 100, |ids| {
let rect = build::shape_rect(ids, if own_fill { [12.0, 12.0] } else { [60.0, 60.0] }, if own_fill { [-26.0, 0.0] } else { [0.0; 2] }, 0.0);
let fill = build::shape_fill(ids, GREEN);
let mut items = vec![build::shape_group(ids, "Nested paint", vec![rect, fill])];
if own_fill {
items.push(build::shape_rect(ids, [60.0, 60.0], [0.0; 2], 0.0));
items.push(build::shape_fill(ids, [0.0, 0.0, 1.0, 1.0]));
}
let mut stroke = build::shape_stroke(ids, RED, 16.0);
set(&mut stroke, "composite", Value::Enum(u32::from(above)));
items.push(stroke);
items
});
frame(&p, cid)
};
let (below, above) = (draw(false), draw(true));
assert!(above.get(24, 50)[1] > 0.99, "nested paint retains its stacking position");
assert_eq!(below.get(24, 50), above.get(24, 50));
if own_fill {
assert!(below.get(76, 50)[2] > 0.99 && above.get(76, 50)[0] > 0.99, "only the parent's previous paint is overridden");
} else {
assert!(below.data == above.data, "there is no previous paint in the parent group");
}
}
}
#[test]
fn stroke_composite_finds_noncontiguous_previous_paint_in_its_group() {
let draw = |above: bool| {
let (p, cid) = shape_comp(|ids| {
let rect = build::shape_rect(ids, [40.0, 40.0], [-40.0, 0.0], 0.0);
let fill = build::shape_fill(ids, [0.0, 0.0, 1.0, 1.0]);
let other_rect = build::shape_rect(ids, [20.0, 20.0], [50.0, 0.0], 0.0);
let other_fill = build::shape_fill(ids, GREEN);
let group = build::shape_group(ids, "Disjoint nested group", vec![other_rect, other_fill]);
let mut stroke = build::shape_stroke(ids, RED, 8.0);
set(&mut stroke, "composite", Value::Enum(u32::from(above)));
vec![rect, fill, group, stroke]
});
frame(&p, cid)
};
let (below, above) = (draw(false), draw(true));
assert!(below.get(42, 50)[2] > 0.99 && above.get(42, 50)[0] > 0.99, "the earlier local Fill remains Previous across a nested group");
assert_eq!(below.get(150, 50), above.get(150, 50));
assert!(above.get(150, 50)[1] > 0.99, "the nested group's disjoint interior stays painted");
}
#[test]
fn stroke_composite_changes_paint_inside_and_after_repeaters() {
let draw = |above: bool, stroke_first: bool, copy_composite: u32| {
let (p, cid) = shape_comp_sized(100, 100, |ids| {
let mut items = vec![build::shape_rect(ids, [20.0, 40.0], [-15.0, 0.0], 0.0), build::shape_fill(ids, [0.0, 0.0, 1.0, 1.0])];
let mut stroke = build::shape_stroke(ids, RED, 8.0);
set(&mut stroke, "composite", Value::Enum(u32::from(above)));
let mut repeater = build::shape_repeater(ids, 2.0, [30.0, 0.0]);
set(&mut repeater, "composite", Value::Enum(copy_composite));
if stroke_first {
items.extend([stroke, repeater]);
} else {
items.extend([repeater, stroke]);
}
items
});
frame(&p, cid)
};
for copies in [0, 1] {
for before in [false, true] {
let (below, above) = (draw(false, before, copies), draw(true, before, copies));
for x in [27, 57] {
assert!(below.get(x, 50)[2] > 0.99 && above.get(x, 50)[0] > 0.99, "stroke before repeater={before}, copy Composite={copies}, x={x}");
}
assert!(below.data.iter().zip(&above.data).all(|(a, b)| (a[3] - b[3]).abs() < 1e-6), "paint order preserves the repeated geometry");
}
}
}
#[test]
fn stroke_composite_preserves_existing_repeater_copy_order() {
let draw = |copies: u32, stroke_above: bool| {
let (p, cid) = shape_comp_sized(100, 100, |ids| {
let rect = build::shape_rect(ids, [40.0, 40.0], [-10.0, 0.0], 0.0);
let colors = Gradient { colors: vec![(0.0, RED.map(|v| v as f32)), (1.0, GREEN.map(|v| v as f32))], opacities: vec![(0.0, 1.0), (1.0, 1.0)] };
let fill = build::shape_gradient_fill(ids, false, [-30.0, 0.0], [10.0, 0.0], colors);
let mut stroke = build::shape_stroke(ids, [0.0, 0.0, 1.0, 1.0], 8.0);
set(&mut stroke, "composite", Value::Enum(u32::from(stroke_above)));
let mut repeater = build::shape_repeater(ids, 2.0, [20.0, 0.0]);
set(&mut repeater, "composite", Value::Enum(copies));
vec![rect, fill, stroke, repeater]
});
frame(&p, cid).get(50, 50)
};
let first = draw(0, false);
let second = draw(1, false);
// This point lies inside both copies, away from either stroke. The shifted
// gradient makes their order visible independently of Stroke Composite.
assert!((first[0] - second[0]).abs() > 0.2);
assert_eq!(first, draw(0, true));
assert_eq!(second, draw(1, true));
}
#[test]
fn stroke_composite_keeps_group_and_layer_transforms() {
let draw = |above: bool, moved: bool| {
let (mut p, cid) = shape_comp_sized(100, 100, |ids| {
let rect = build::shape_rect(ids, [60.0, 40.0], [0.0; 2], 0.0);
let fill = build::shape_fill(ids, [0.0, 0.0, 1.0, 1.0]);
let mut stroke = build::shape_stroke(ids, RED, 12.0);
set(&mut stroke, "composite", Value::Enum(u32::from(above)));
let mut group = build::shape_group(ids, "Transformed", if moved { vec![rect, stroke, fill] } else { vec![rect, fill, stroke] });
set(&mut group, "transform/position", Value::Vec2([12.0, -3.0]));
set(&mut group, "transform/scale", Value::Vec2([120.0, 80.0]));
set(&mut group, "transform/rotation", Value::Scalar(19.0));
vec![group]
});
let layer = &mut p.comp_mut(cid).unwrap().layers[0];
set(&mut layer.props, "transform/position", Value::Vec2([45.0, 55.0]));
set(&mut layer.props, "transform/rotation", Value::Scalar(-7.0));
frame(&p, cid)
};
let above = draw(true, false);
assert!(above.data != draw(false, false).data, "Composite changes the transformed paint overlap");
assert!(above.data == draw(false, true).data, "only stacking changes in the transformed group");
}
#[test]
fn merge_add_feeds_fill_below_and_drops_paint_above() {
let (p, cid) = shape_comp(|ids| {