mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-03 00:39:44 -06:00
- Background blur sees the whole backdrop under the layer: the renderer keeps the stack of isolated surfaces it draws into (clipping frames, rounded clips, faded groups, masks) and flattens it for the layer's area, reading past the layer's own box. A blur inside a clipping frame now shows the frame's fill instead of the sharp layers below, on the canvas and in PNG exports. The PDF export draws background blur and glass through the same code from a render of what lies under the node (`backdrop::render_beneath`, which the PDF's backdrop now shares), and its images match the PNG's pixels. - Shadow spread offsets the outline: the shape filled and stroked twice the spread wide with mitred joins (grow) or cut away (shrink), the stroked bands cached per outline. Ellipses stay ellipses, stars keep sharp points, text keeps its letterforms, rounded corners grow their radius. Inner shadows shrink the outline the same way. Canvas, PNG and PDF share the shadow code; SVG writes spread shadows as the same geometry and now exports inner shadows. - Glass effect (light angle and intensity, refraction, depth, dispersion, frost, splay): the frosted backdrop bent towards the inside within `depth` of the edge, colour fringes, light on the rim, from an exact distance transform with smoothed normals. - Noise: mono, duo and multi colour modes, grain size, density, opacity and blend mode; grains live in the layer's coordinates (stable when panning and zooming). Texture is now drawn (pixels scattered by the radius, grain size, clip to shape). - Design panel rows for noise mode/colours/opacity, texture clip to shape and every glass value; `effect.add` takes `glass`; REST import of NOISE, TEXTURE and GLASS; Inspect rows and CSS; the scale tool scales grain sizes, texture radius, glass depth and frost. - Tests in new modules: render backdrop/spread/grain tests, PDF backdrop tests, SVG, doc, engine and REST effect tests. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
3.8 KiB
Rust
84 lines
3.8 KiB
Rust
//! Shadows in SVG: spread offsets the outline like the renderer (mitred stroke bands, not a
|
|
//! morphology filter), negative spread and inner shadows cut with masks; layers without one outline
|
|
//! keep the morphology filter. Every export stays valid SVG.
|
|
|
|
use figcraft_color::{Color, Paint};
|
|
use figcraft_doc::{Document, Effect, EffectKind, NodeId, NodeKind, Shadow};
|
|
|
|
use super::*;
|
|
|
|
fn with_effects(kind: NodeKind, effects: Vec<EffectKind>) -> (Document, NodeId) {
|
|
let mut d = Document::new("svg fx");
|
|
let p = d.first_page().unwrap();
|
|
let n = d.create(p, kind, "S", 10.0, 10.0, 100.0, 80.0).unwrap();
|
|
let node = d.get_mut(n).unwrap();
|
|
node.fills = vec![Paint::solid(Color::from_hex("3366CC").unwrap())];
|
|
node.effects = effects.into_iter().map(|kind| Effect { kind, visible: true }).collect();
|
|
(d, n)
|
|
}
|
|
|
|
fn shadow(spread: f64) -> Shadow {
|
|
Shadow { color: Color::from_hex("00000080").unwrap(), offset: (3.0, 5.0), radius: 8.0, spread, ..Default::default() }
|
|
}
|
|
|
|
/// Export and check the result parses as SVG.
|
|
fn export(d: &Document, id: NodeId) -> String {
|
|
let svg = export_svg(d, &[id], &SvgOptions::default()).unwrap();
|
|
let mut d2 = Document::new("back");
|
|
let p2 = d2.first_page().unwrap();
|
|
import_svg(&mut d2, svg.as_bytes(), p2, 0.0, 0.0, "Imported").unwrap();
|
|
svg
|
|
}
|
|
|
|
#[test]
|
|
fn drop_shadow_spread_offsets_the_outline() {
|
|
let star = NodeKind::Star { count: 5, inner_ratio: 0.4 };
|
|
let (d, n) = with_effects(star, vec![EffectKind::DropShadow(shadow(6.0))]);
|
|
let svg = export(&d, n);
|
|
assert!(!svg.contains("feMorphology"), "{svg}");
|
|
assert!(svg.contains("stroke-width=\"12\"") && svg.contains("stroke-linejoin=\"miter\"") && svg.contains("stroke-miterlimit=\"4\""), "{svg}");
|
|
assert!(svg.contains("<feGaussianBlur stdDeviation=\"4\"/>"), "{svg}");
|
|
assert!(svg.contains("opacity=\"0.502\""), "{svg}");
|
|
}
|
|
|
|
#[test]
|
|
fn negative_spread_and_inner_shadows_cut_with_masks() {
|
|
let ellipse = NodeKind::Ellipse { start: 0.0, sweep: std::f64::consts::TAU, inner: 0.0 };
|
|
let (d, n) = with_effects(ellipse.clone(), vec![EffectKind::DropShadow(shadow(-4.0))]);
|
|
let svg = export(&d, n);
|
|
assert!(svg.contains("<mask") && svg.contains("stroke=\"black\" stroke-width=\"8\""), "{svg}");
|
|
let (d, n) = with_effects(ellipse, vec![EffectKind::InnerShadow(shadow(4.0))]);
|
|
let svg = export(&d, n);
|
|
assert!(svg.contains("<clipPath") && svg.contains("<mask"), "{svg}");
|
|
assert!(svg.contains("stroke=\"black\" stroke-width=\"8\""), "the shrunk hole: {svg}");
|
|
}
|
|
|
|
#[test]
|
|
fn spread_without_an_outline_keeps_the_filter() {
|
|
let mut d = Document::new("g");
|
|
let p = d.first_page().unwrap();
|
|
let g = d.create(p, NodeKind::Group, "G", 0.0, 0.0, 50.0, 50.0).unwrap();
|
|
let r = d.create(g, NodeKind::Rectangle, "R", 0.0, 0.0, 50.0, 50.0).unwrap();
|
|
d.get_mut(r).unwrap().fills = vec![Paint::solid(Color::BLACK)];
|
|
d.get_mut(g).unwrap().effects = vec![Effect { kind: EffectKind::DropShadow(shadow(5.0)), visible: true }];
|
|
let svg = export(&d, g);
|
|
assert!(svg.contains("feMorphology") && svg.contains("operator=\"dilate\" radius=\"5\""), "{svg}");
|
|
// No spread: no morphology at all.
|
|
d.get_mut(g).unwrap().effects = vec![Effect { kind: EffectKind::DropShadow(shadow(0.0)), visible: true }];
|
|
assert!(!export(&d, g).contains("feMorphology"));
|
|
}
|
|
|
|
#[test]
|
|
fn hostile_shadow_values_still_export() {
|
|
for s in [f64::NAN, f64::INFINITY, -1e12, 1e12] {
|
|
let (d, n) = with_effects(
|
|
NodeKind::Rectangle,
|
|
vec![
|
|
EffectKind::DropShadow(Shadow { spread: s, radius: s, offset: (s, 0.0), ..Shadow::default() }),
|
|
EffectKind::InnerShadow(Shadow { spread: s, radius: s, offset: (0.0, s), ..Shadow::default() }),
|
|
],
|
|
);
|
|
assert!(export_svg(&d, &[n], &SvgOptions::default()).is_some());
|
|
}
|
|
}
|