diff --git a/crates/engine/src/cmd/effectcmd.rs b/crates/engine/src/cmd/effectcmd.rs index cafe5daa23..2c4fad2e81 100644 --- a/crates/engine/src/cmd/effectcmd.rs +++ b/crates/engine/src/cmd/effectcmd.rs @@ -957,9 +957,9 @@ fn is_container(n: &Node) -> bool { /// The raster effects of `n` (its own, its fills' and strokes') as an embedded image rendered at /// the document's raster effects resolution or a Rasterize effect's finer one, with whether they -/// all paint below it (shadows and outer glows: the image then holds just them). A group's or -/// layer's image leaves out its members' raster effects, which are expanded with them. `None` -/// when it has none. +/// all paint below it (shadows and outer glows: the image then holds just them, and paints with +/// their blend mode when they all use one). A group's or layer's image leaves out its members' +/// raster effects, which are expanded with them. `None` when it has none. /// `Err` when that image would take `budget` past its limits: the object then stays as it is. fn raster_image(d: &mut Document, n: &Node, budget: &mut RasterBudget) -> std::result::Result, OverBudget> { let fx = rasterfx::raster_fx(n); @@ -968,12 +968,21 @@ fn raster_image(d: &mut Document, n: &Node, budget: &mut RasterBudget) -> std::r } let below = fx.iter().all(effects::RasterFx::is_below); let pixels = budget.take(rasterfx::raster_pixels(d, n, below, true))?; - let image = rasterfx::raster_image(d, n, below, true); + let mut image = rasterfx::raster_image(d, n, below, true); if image.is_none() { // No image was made: it takes nothing from the budget. budget.images += 1; budget.pixels += pixels; } + // Rendered alone, over nothing, the shadows and glows can't blend with the art below the + // object, so the image does, as the canvas draws them. + let mut modes = fx.iter().filter_map(effects::RasterFx::mode); + if below + && let (Some(mode), Some(image)) = (modes.next(), image.as_mut()) + && modes.all(|m| m == mode) + { + image.blend = mode; + } Ok(image.map(|image| (image, below))) } diff --git a/crates/engine/src/tests/expand.rs b/crates/engine/src/tests/expand.rs index 1039dab493..287cbc11da 100644 --- a/crates/engine/src/tests/expand.rs +++ b/crates/engine/src/tests/expand.rs @@ -612,6 +612,68 @@ fn a_nested_instance_fill_blur_becomes_an_image_when_the_outer_art_expands() { assert!(differing(&before, &after, 24) < 400, "expanding changed {} pixels", differing(&before, &after, 24)); } +/// A blue rectangle from x 140 to 200, drawn first, so a shadow right of the square falls on it. +fn blue_backdrop(s: &mut Session) { + run(s, "shape.rectangle", json!({"x": 140, "y": 0, "width": 60, "height": 200})); + run(s, "paint.setFill", json!({"color": "#0000ff"})); +} + +/// A red Drop Shadow 20 pt to the right on row `row` of instance `id`, with no blur, painting with `mode`. +fn red_shadow_row(s: &mut Session, id: NodeId, row: usize, mode: &str) { + let params = json!({"x": 20, "y": 0, "blur": 0, "opacity": 100, "mode": mode, "color": "#ff0000"}); + run(s, "effect.apply", json!({"ids": [id.0], "item": row, "effect": "stylize.dropShadow", "params": params})); +} + +/// The blend modes of the images in `n`, in the order the walk visits them. +fn image_modes(n: &Node) -> Vec { + let mut modes = vec![]; + n.walk(&mut |part| { + if matches!(part.kind, NodeKind::Image(_)) { + modes.push(part.blend); + } + }); + modes +} + +/// A Multiply shadow on the nested instance's fill: its image multiplies with the blue art below +/// as the shadow did, rather than painting red over it. +#[test] +fn a_nested_instance_multiply_shadow_image_keeps_its_mode() { + let mut s = session(); + blue_backdrop(&mut s); + let (id, row) = nested_with_row(&mut s, |s, id, row| red_shadow_row(s, id, row, "multiply")); + offset_row(&mut s, id, row, 10.0); + let before = render(&s); + assert!(dark(before.pixel(170, 100)), "red multiplied with blue is dark: {:?}", before.pixel(170, 100)); + + run(&mut s, "effect.expandAppearance", json!({})); + let n = node(&s, id); + assert_eq!(instances_and_images(&n).0, 0, "the outer and inner instances become art"); + assert_eq!(image_modes(&n), [BlendMode::Multiply]); + no_effects_left(&n); + let after = render(&s); + assert!(dark(after.pixel(170, 100)), "the image multiplies with the blue art: {:?}", after.pixel(170, 100)); + assert!(differing(&before, &after, 24) < 400, "expanding changed {} pixels", differing(&before, &after, 24)); +} + +/// A Multiply shadow on an instance's own fill: the image under its art paints with Multiply. +#[test] +fn an_instance_multiply_shadow_image_keeps_its_mode() { + let mut s = session(); + blue_backdrop(&mut s); + let (id, row) = instance_with_row(&mut s, "Square", true); + red_shadow_row(&mut s, id, row, "multiply"); + let before = render(&s); + assert!(dark(before.pixel(160, 100)), "red multiplied with blue is dark: {:?}", before.pixel(160, 100)); + + run(&mut s, "effect.expandAppearance", json!({})); + let n = node(&s, id); + assert_eq!(image_modes(&n), [BlendMode::Multiply]); + let after = render(&s); + assert!(dark(after.pixel(160, 100)), "the image multiplies with the blue art: {:?}", after.pixel(160, 100)); + assert!(differing(&before, &after, 24) < 400, "expanding changed {} pixels", differing(&before, &after, 24)); +} + /// A shadow on a hidden fill of the nested instance draws nothing, and expanding makes no image of it. #[test] fn a_hidden_nested_instance_fill_shadow_makes_no_image() {