Files
figcraft/crates/pdf/src/effect_order_tests.rs
T
BFlatandClaude Opus 5.5 70b2666c90 M3: progressive blur, inner shadows under strokes, noise and texture in SVG (#86)
- Inner shadows paint over the fills and under the children and strokes, as documented (canvas,
  PNG, PDF and SVG alike; text fills every run before the inner shadow and the strokes).
- Layer and background blur gain a progressive type: the radius ramps linearly from a start radius
  at a start point to the end radius at an end point (unit-box points, like gradient handles).
  Rendered by blending a few geometric blur levels, each blurred only around the pixels that use
  it; bands and partial renders match one piece. Design panel type menu and Start/End fields,
  on-canvas handles while the settings are open, effect.add/effect.set params, startRadius
  variable bindings, scale tool, REST import (blurType, startRadius, startOffset, endOffset),
  Inspect row; CSS leaves progressive blurs out.
- SVG export embeds renders for what filters can't match: noise grains over the vector layer
  (crisp, blended), texture and progressive layer blur whole, background blur and glass from what
  lies under the layer among the exported layers. PDF and SVG share the renderer's helpers.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 17:20:07 +07:00

69 lines
3.0 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Effects in the PDF: inner shadow images go over the fills and under the children and strokes
//! (the renderer's order); progressive blurs are drawn by the renderer like the PNG export.
use super::*;
use crate::backdrop_tests::{pdf_vs_png, scene};
use crate::tests::inflated;
use figcraft_color::{Color, Paint};
use figcraft_doc::{Blur, BlurType, Effect, EffectKind, NodeKind, Shadow, StrokeAlign};
const FILL: &str = "0.2 0.4 0.8 rg";
const CHILD: &str = "1 0 0 rg";
const STROKE: &str = "0 1 0 rg";
/// A blue 100 × 100 `kind` with a green inside stroke and an inner shadow (and a red child when a
/// frame); the page content stream.
fn content(kind: NodeKind) -> String {
let mut d = Document::new("pdf order");
let p = d.first_page().unwrap();
let r = d.create(p, kind, "R", 0.0, 0.0, 100.0, 100.0).unwrap();
let n = d.get_mut(r).unwrap();
n.fills = vec![Paint::solid(Color::from_hex("3366CC").unwrap())];
n.strokes = vec![Paint::solid(Color::from_hex("00FF00").unwrap())];
n.stroke.weight = 10.0;
n.stroke.align = StrokeAlign::Inside;
n.effects = vec![Effect { kind: EffectKind::InnerShadow(Shadow::default()), visible: true }];
if n.kind.is_frame_like() {
let c = d.create(r, NodeKind::Rectangle, "C", 20.0, 20.0, 40.0, 40.0).unwrap();
d.get_mut(c).unwrap().fills = vec![Paint::solid(Color::from_hex("FF0000").unwrap())];
}
inflated(&export_pdf(&d, &[r], &PdfOptions::default()).unwrap())
}
#[test]
fn inner_shadows_go_over_fills_and_under_strokes() {
let c = content(NodeKind::Rectangle);
let (fill, shadow, stroke) = (c.find(FILL).unwrap(), c.find(" Do").unwrap(), c.find(STROKE).unwrap());
assert!(fill < shadow && shadow < stroke, "{c}");
}
#[test]
fn a_frames_children_go_over_its_inner_shadow() {
let c = content(NodeKind::Frame);
let (fill, shadow, child, stroke) = (c.find(FILL).unwrap(), c.find(" Do").unwrap(), c.find(CHILD).unwrap(), c.find(STROKE).unwrap());
assert!(fill < shadow && shadow < child && child < stroke, "{c}");
}
#[test]
fn pdf_progressive_background_blur_matches_the_png() {
let b = Blur { radius: 16.0, blur_type: BlurType::Progressive, start_radius: 2.0, start: (0.0, 0.5), end: (1.0, 0.5) };
let (d, f, pane) = scene(EffectKind::BackgroundBlur(b));
let diff = pdf_vs_png(&d, f, pane);
assert!(diff <= 3, "{diff}");
}
#[test]
fn progressive_layer_blur_is_rasterised() {
let images = |d: &Document, id| count_images(&export_pdf(d, &[id], &PdfOptions::default()).unwrap());
let (mut d, f, pane) = scene(EffectKind::LayerBlur(Blur::uniform(0.0)));
d.get_mut(pane).unwrap().fills = vec![Paint::solid(Color::WHITE)];
let plain = images(&d, f);
let b = Blur { radius: 12.0, blur_type: BlurType::Progressive, ..Blur::default() };
d.get_mut(pane).unwrap().effects = vec![Effect { kind: EffectKind::LayerBlur(b), visible: true }];
assert!(images(&d, f) > plain);
}
fn count_images(pdf: &[u8]) -> usize {
String::from_utf8_lossy(pdf).matches("/Subtype/Image").count()
}