Merge main into fix/a-part-with-nan-opacity-neither-casts-a-shadow

This commit is contained in:
dotCooCoo
2026-11-14 18:44:29 -08:00
141 changed files with 5841 additions and 679 deletions
+49 -12
View File
@@ -199,8 +199,19 @@ fn placed_fits(baked: &Node, spent: u64, dropped: u64) -> Option<u64> {
}
/// The names of the symbols of `d` whose art needs baking ([`needs_bake`]), and of those whose art
/// holds an instance of one of them at any depth.
/// holds an instance of one of them at any depth, in the art of enabled opacity masks too.
fn drawn_symbols(d: &Document) -> HashSet<String> {
fn used(n: &Node, names: &mut Vec<String>) {
if let NodeKind::SymbolInstance { symbol, .. } = &n.kind {
names.push(symbol.clone());
}
if let Some(m) = n.extras().mask.as_ref().filter(|m| !m.disabled) {
used(&m.art, names);
}
for c in n.children().into_iter().flatten() {
used(c, names);
}
}
let mut out: HashSet<String> = d.symbols.iter().filter(|s| needs_bake(&s.art)).map(|s| s.name.clone()).collect();
if out.is_empty() {
return out;
@@ -211,11 +222,7 @@ fn drawn_symbols(d: &Document) -> HashSet<String> {
.filter(|s| !out.contains(&s.name))
.map(|s| {
let mut names = vec![];
s.art.walk(&mut |c| {
if let NodeKind::SymbolInstance { symbol, .. } = &c.kind {
names.push(symbol.clone());
}
});
used(&s.art, &mut names);
(s.name.as_str(), names)
})
.collect();
@@ -233,14 +240,22 @@ fn drawn_symbols(d: &Document) -> HashSet<String> {
}
/// Does `n`'s subtree hold an instance of a symbol that [`bake_document_placing_symbols`] replaces
/// by its placed art? False outside it, and for objects the export does not write ([`unwritten`]).
/// by its placed art, in the art of its enabled opacity masks too? False outside it, and for
/// objects the export does not write ([`unwritten`]).
fn holds_placed_symbol(n: &Node) -> bool {
fn walk(n: &Node, symbols: &HashSet<String>) -> bool {
placed_symbol_in(n, true)
}
/// [`holds_placed_symbol`], with the art of `n`'s own opacity mask left out unless `own_mask`
/// (the art of the masks inside `n` is always looked into).
fn placed_symbol_in(n: &Node, own_mask: bool) -> bool {
fn walk(n: &Node, symbols: &HashSet<String>, own_mask: bool) -> bool {
!unwritten(n)
&& (matches!(&n.kind, NodeKind::SymbolInstance { symbol, .. } if symbols.contains(symbol))
|| n.children().is_some_and(|ch| ch.iter().any(|c| walk(c, symbols))))
|| (own_mask && !n.is_ruler_guide() && n.extras().mask.as_ref().is_some_and(|m| !m.disabled && walk(&m.art, symbols, true)))
|| n.children().is_some_and(|ch| ch.iter().any(|c| walk(c, symbols, true))))
}
!UNWRITTEN.with(|c| c.get()) && PLACING.with(|p| p.borrow().as_ref().is_some_and(|p| walk(n, &p.symbols)))
!UNWRITTEN.with(|c| c.get()) && PLACING.with(|p| p.borrow().as_ref().is_some_and(|p| walk(n, &p.symbols, own_mask)))
}
/// Symbol instance `n`, while [`bake_document_placing_symbols`] bakes and its symbol is one it
@@ -278,8 +293,29 @@ fn placed_symbol_art(d: &Document, n: &Node) -> Option<(Node, u64)> {
Some((g, most))
}
/// `n` with its geometry effects evaluated (`None` = unchanged).
/// `n` with its geometry effects evaluated (`None` = unchanged), and the art of its enabled opacity
/// mask baked one level deeper when it holds instances that [`bake_document_placing_symbols`]
/// places ([`holds_placed_symbol`]), so the mask masks with the art the instances draw. The art is
/// baked only while the object baked from `n` still carries `n`'s mask art; art already baked
/// with `n`'s recolored or placed form is not baked again. A ruler guide's mask stays as it is.
fn bake_node(d: &mut Document, n: &Node) -> Option<Node> {
let out = bake_object(d, n);
let Some(mask) = n.extras().mask.as_ref().filter(|m| !m.disabled && !n.is_ruler_guide() && !unwritten(n) && holds_placed_symbol(&m.art)) else {
return out;
};
let changed = out.is_some();
let mut out = out.unwrap_or_else(|| n.clone());
let Some(slot) = out.extras_if_any().and_then(|e| e.mask.as_mut()).filter(|s| Arc::ptr_eq(&s.art, &mask.art)) else {
return changed.then_some(out);
};
let Some(art) = one_deeper(|| bake_node(d, &mask.art)) else { return changed.then_some(out) };
slot.art = Arc::new(art);
Some(out)
}
/// `n` with its geometry effects evaluated (`None` = unchanged); its opacity mask is left to
/// [`bake_node`].
fn bake_object(d: &mut Document, n: &Node) -> Option<Node> {
// Inside an object the export leaves out, effects are baked but no instance is placed.
if !UNWRITTEN.with(|c| c.get()) && unwritten(n) {
UNWRITTEN.with(|c| c.set(true));
@@ -289,7 +325,8 @@ fn bake_node(d: &mut Document, n: &Node) -> Option<Node> {
}
// A cancelled export leaves the rest of the document as it is: the caller drops the result.
// A ruler guide draws nothing and stays as it is, last in its layer.
if n.is_ruler_guide() || !(needs_bake(n) || holds_placed_symbol(n)) || vectorcraft_doc::cancel::is_cancelled() {
// An object whose own mask's art alone holds placed instances stays as it is here.
if n.is_ruler_guide() || !(needs_bake(n) || placed_symbol_in(n, false)) || vectorcraft_doc::cancel::is_cancelled() {
return None;
}
// Colour adjustments: the object recoloured (inside it too), then baked as it is.
+68
View File
@@ -1169,6 +1169,74 @@ fn placing_symbols_reaches_the_art_of_nested_instances() {
assert!(near_rect(bounds(&out, 900), Rect::new(110.0, 140.0, 120.0, 150.0)), "{:?}", bounds(&out, 900));
}
/// A 10 pt square with id `id`, masked by `art` (the mask enabled unless `disabled`).
fn masked_square(id: u64, art: Node, disabled: bool) -> Node {
let mut n = Node::path(NodeId(id), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default_art());
let mut mask = vectorcraft_doc::OpacityMask::new(art, false);
mask.disabled = disabled;
n.extras_mut().mask = Some(Box::new(mask));
n
}
/// Whether the art of `n`'s opacity mask is still a symbol instance, and its bounds when it is not.
fn mask_art(n: &Node) -> (bool, Option<Rect>) {
let art = &n.extras().mask.as_ref().unwrap_or_else(|| panic!("node {:?} has a mask", n.id)).art;
(matches!(art.kind, NodeKind::SymbolInstance { .. }), art.geometric_bounds())
}
/// An instance in the art of an enabled opacity mask is placed and baked like any other, so the
/// mask masks with the art the instance draws, and the masked object stays as it is (a clipping
/// path stays one); one in a disabled mask, in the mask of an object the export leaves out, or in
/// a ruler guide's mask, stays as it is.
#[test]
fn placing_symbols_reaches_instances_in_opacity_masks() {
use vectorcraft_geom::Affine;
let mut d = doc_with(masked_square(700, instance(900, "S", Affine::IDENTITY), false));
let layer = d.layers[0].id;
d.insert(Some(layer), 1, masked_square(701, instance(901, "S", Affine::IDENTITY), true)).unwrap();
let mut hidden = masked_square(702, instance(902, "S", Affine::IDENTITY), false);
hidden.visible = false;
d.insert(Some(layer), 2, hidden).unwrap();
let mut clipping = masked_square(703, instance(903, "S", Affine::IDENTITY), false);
if let NodeKind::Path { clipping: c, .. } = &mut clipping.kind {
*c = true;
}
d.insert(Some(layer), 3, Node::group(NodeId(704), vec![Arc::new(clipping)])).unwrap();
let mut guide = Node::new(NodeId(705), NodeKind::RulerGuide { vertical: false, pos: 5.0, artboard: None });
guide.extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(instance(905, "S", Affine::IDENTITY), false)));
d.insert(Some(layer), 4, guide).unwrap();
add_symbol(&mut d, "S", moved_square(800));
let out = bake_document_placing_symbols(&d, true, ALL).unwrap();
for id in [700, 703] {
let (still, b) = mask_art(out.node(NodeId(id)).unwrap());
assert!(!still && b.is_some_and(|b| near_rect(b, Rect::new(110.0, 40.0, 120.0, 50.0))), "{id}: {b:?}");
}
assert!(matches!(out.node(NodeId(703)).unwrap().kind, NodeKind::Path { clipping: true, .. }), "the clipping path stays one");
assert!(mask_art(out.node(NodeId(701)).unwrap()).0, "a disabled mask's instance stays");
assert!(mask_art(out.node(NodeId(702)).unwrap()).0, "a hidden object's mask's instance stays");
assert!(mask_art(out.node(NodeId(705)).unwrap()).0, "a ruler guide's mask's instance stays");
}
/// A symbol whose art holds an instance only in an opacity mask's art is placed too, and the
/// instance in its mask is placed with it.
#[test]
fn placing_symbols_reaches_instances_in_the_masks_of_symbol_art() {
use vectorcraft_geom::Affine;
let mut d = doc_with(instance(900, "Outer", Affine::IDENTITY));
add_symbol(&mut d, "Inner", moved_square(800));
add_symbol(&mut d, "Outer", masked_square(803, instance(802, "Inner", Affine::IDENTITY), false));
let out = bake_document_placing_symbols(&d, true, ALL).unwrap();
assert!(!is_instance(&out, 900), "the outer instance is placed");
let mut found = None;
out.node(NodeId(900)).unwrap().walk(&mut |n| {
if n.id == NodeId(803) {
found = Some(mask_art(n));
}
});
let (still, b) = found.unwrap_or_else(|| panic!("the masked square is in the placed art"));
assert!(!still && b.is_some_and(|b| near_rect(b, Rect::new(110.0, 40.0, 120.0, 50.0))), "{b:?}");
}
/// A symbol whose art holds an instance of itself is placed until the depth limit, and the export
/// ends.
#[test]
+2 -2
View File
@@ -468,8 +468,8 @@ pub fn effect_catalog() -> Vec<EffectInfo> {
"blur.radial",
"Radial Blur…",
BLUR,
"{amount: 1..100 (10; spin: an arc of `amount`°, zoom: the content scaled by up to about ±amount/2 %), method: \"spin\"|\"zoom\" (\"spin\"), quality: \"draft\"|\"good\"|\"best\" (\"good\"; 16, 64 or 256 samples)} blurs around the object's centre",
json!({"amount": 10.0, "method": "spin", "quality": "good"}),
"{amount: 1..100 (10; spin: an arc of `amount`°, zoom: the content scaled by up to about ±amount/2 %), method: \"spin\"|\"zoom\" (\"spin\"), quality: \"draft\"|\"good\"|\"best\" (\"good\"; 16, 64 or 256 samples), centerX: 0..1 (0.5; the Blur Center across the object's bounds, 0 their left edge), centerY: 0..1 (0.5; the Blur Center down the object's bounds, 0 their top edge)} blurs around the Blur Center, which moves and scales with the object. A centerX or centerY outside 0..1 is refused",
json!({"amount": 10.0, "method": "spin", "quality": "good", "centerX": 0.5, "centerY": 0.5}),
),
r(
"blur.smart",
+18 -13
View File
@@ -30,7 +30,7 @@ mod texture;
mod video;
use serde_json::Value;
use vectorcraft_geom::{Affine, Point, Rect};
use vectorcraft_geom::{Affine, Point, Rect, Vec2};
use crate::util::{flag, num, text};
@@ -158,9 +158,10 @@ pub enum PixelFx {
/// (1..20), then a [`Texture`] surface scaled by `scaling` (0.5..2), in relief as strong as
/// `relief` (0..50) and lit from `light`; `invert` turns the surface's heights over.
RoughPastels { length: f64, detail: f64, texture: Texture, scaling: f64, relief: f64, light: Light, invert: bool },
/// Blur › Radial Blur around the object's centre: along arcs (spin, an arc of `amount`°) or
/// along rays (zoom, the content scaled by up to about ±`amount`/2 %), averaging 2^`passes` samples.
RadialBlur { amount: f64, zoom: bool, passes: u32 },
/// Blur › Radial Blur around its Blur Center, `center` (0..1 across and down the object's
/// bounds; (0.5, 0.5) the object's centre): along arcs (spin, an arc of `amount`°) or along
/// rays (zoom, the content scaled by up to about ±`amount`/2 %), averaging 2^`passes` samples.
RadialBlur { amount: f64, zoom: bool, passes: u32, center: [f64; 2] },
/// Blur › Smart Blur (Normal mode): each pixel averaged with the neighbours within `radius`
/// whose colour differs from its own by no more than `threshold` levels, over a grid of
/// `samples` × `samples` neighbours at most.
@@ -314,9 +315,12 @@ pub struct PixelSpace {
pub to_doc: Affine,
/// The size of a pixel in document units.
pub px: f64,
/// The object's centre (document coordinates): where Radial Blur blurs around, and where the
/// Pixelate filters' patterns are anchored (so they move with the object).
/// The object's centre (document coordinates): where Radial Blur blurs around by default, and
/// where the Pixelate filters' patterns are anchored (so they move with the object).
pub center: Point,
/// The width and height of the object's bounds (document units), centred on `center`: Radial
/// Blur's Blur Center lies at a share of them.
pub size: Vec2,
/// What the colour channels hold.
pub channels: Channels,
/// The height of a line of the document's raster grid (document units: 72 / Document Raster
@@ -421,6 +425,7 @@ pub(crate) fn parse(id: &str, p: &Value) -> Option<PixelFx> {
amount: num(p, "amount", 10.0).clamp(1.0, 100.0),
zoom: text(p, "method", "spin").eq_ignore_ascii_case("zoom"),
passes: quality([("draft", 4), ("good", 6), ("best", 8)], 6),
center: [num(p, "centerX", 0.5).clamp(0.0, 1.0), num(p, "centerY", 0.5).clamp(0.0, 1.0)],
},
"blur.smart" => PixelFx::SmartBlur {
radius: num(p, "radius", 3.0).clamp(0.1, 100.0),
@@ -608,13 +613,13 @@ pub(crate) fn parse(id: &str, p: &Value) -> Option<PixelFx> {
impl PixelFx {
/// How far beyond `bounds` (the content's, document units) the effect can paint.
pub fn outset(&self, bounds: Rect) -> f64 {
// From the centre to the farthest corner, and to the nearest side.
let far = 0.5 * bounds.width().hypot(bounds.height());
let near = 0.5 * bounds.width().min(bounds.height());
match *self {
// A rotated corner reaches the circle through the corners.
PixelFx::RadialBlur { zoom: false, .. } => (far - near).max(0.0),
PixelFx::RadialBlur { amount, zoom: true, .. } => far * (blur::zoom_extent(amount).exp() - 1.0),
// A rotated corner reaches the circle about the Blur Center through the farthest corner.
PixelFx::RadialBlur { zoom: false, center, .. } => {
let (far, near) = blur::center_reach(bounds, center);
(far - near).max(0.0)
}
PixelFx::RadialBlur { amount, zoom: true, center, .. } => blur::center_reach(bounds, center).0 * (blur::zoom_extent(amount).exp() - 1.0),
PixelFx::SmartBlur { radius, .. } => radius,
PixelFx::AccentedEdges { .. } => 0.0,
// Strokes reach out of the object as far as they reach into it.
@@ -786,7 +791,7 @@ impl PixelFx {
PixelFx::RoughPastels { length, detail, ref texture, scaling, relief, light, invert } => {
artistic::rough_pastels(px, w, h, space, length, detail, texture, scaling, relief, light, invert)
}
PixelFx::RadialBlur { amount, zoom, passes } => blur::radial(px, w, h, space, amount, zoom, passes),
PixelFx::RadialBlur { amount, zoom, passes, center } => blur::radial(px, w, h, space, amount, zoom, passes, center),
PixelFx::SmartBlur { radius, threshold, samples, mode: SmartBlurMode::Normal } => {
blur::smart(px, w, h, to_px(radius), threshold, samples)
}
+25 -7
View File
@@ -1,6 +1,6 @@
//! Blur › Radial Blur and Smart Blur (Normal, Edge Only and Overlay Edge).
use vectorcraft_geom::{Affine, Point, Vec2};
use vectorcraft_geom::{Affine, Point, Rect, Vec2};
use super::{PixelSpace, Px16, box_blur, pick, to8, to16};
@@ -29,13 +29,31 @@ fn bilinear(buf: &[Px16], w: usize, h: usize, x: f64, y: f64) -> [f32; 4] {
std::array::from_fn(|i| a[i] * wa + b[i] * wb + c[i] * wc + d[i] * wd)
}
/// Radial Blur: the average of the content turned (spin) or scaled (zoom) about the centre over
/// evenly spaced steps spanning `amount`. Each pass averages the previous result with itself
/// turned both ways by half the last step, so `passes` passes average 2^`passes` samples at two
/// lookups a pixel each.
pub(super) fn radial(px: &mut [[u8; 4]], w: usize, h: usize, space: &PixelSpace, amount: f64, zoom: bool, passes: u32) {
/// How far Radial Blur's Blur Center `center` (0..1 across and down `bounds`) lies from the
/// farthest corner of `bounds`, and from its nearest side.
pub(super) fn center_reach(bounds: Rect, center: [f64; 2]) -> (f64, f64) {
// The share of the width (height) on the far side of the center, doubled: 1 at the middle.
let far = |f: f64| 1.0 + (2.0 * f - 1.0).abs();
let (w, h) = (bounds.width(), bounds.height());
let near = ((2.0 - far(center[0])) * w).min((2.0 - far(center[1])) * h);
(0.5 * (far(center[0]) * w).hypot(far(center[1]) * h), 0.5 * near)
}
/// Where Radial Blur's Blur Center `center` (0..1 across and down the object's bounds) lies in the
/// document: the object's centre at (0.5, 0.5).
pub(super) fn blur_center(space: &PixelSpace, center: [f64; 2]) -> Point {
let shift = Vec2::new((center[0] - 0.5) * space.size.x, (center[1] - 0.5) * space.size.y);
if shift.x.is_finite() && shift.y.is_finite() { space.center + shift } else { space.center }
}
/// Radial Blur: the average of the content turned (spin) or scaled (zoom) about the Blur Center
/// (`center`, [`blur_center`]) over evenly spaced steps spanning `amount`. Each pass averages the
/// previous result with itself turned both ways by half the last step, so `passes` passes average
/// 2^`passes` samples at two lookups a pixel each.
#[allow(clippy::too_many_arguments)] // the effect's four options
pub(super) fn radial(px: &mut [[u8; 4]], w: usize, h: usize, space: &PixelSpace, amount: f64, zoom: bool, passes: u32, center: [f64; 2]) {
// The centre in pixel index coordinates (pixel centres at whole numbers).
let c = space.to_doc.inverse() * space.center - Vec2::new(0.5, 0.5);
let c = space.to_doc.inverse() * blur_center(space, center) - Vec2::new(0.5, 0.5);
if !(c.x.is_finite() && c.y.is_finite()) {
return;
}
+58 -5
View File
@@ -12,6 +12,7 @@ fn space(w: usize, h: usize) -> PixelSpace {
to_doc: Affine::IDENTITY,
px: 1.0,
center: Point::new(w / 2.0, h / 2.0),
size: vectorcraft_geom::Vec2::new(w, h),
channels: Channels::Rgb,
line: 1.0,
extent: 0.5 * w.hypot(h),
@@ -479,12 +480,16 @@ fn film_grain_leaves_invalid_coordinate_spaces_alone() {
#[test]
fn params_take_defaults_and_stay_in_range() {
assert_eq!(fx("blur.radial", json!({})), PixelFx::RadialBlur { amount: 10.0, zoom: false, passes: 6 });
assert_eq!(fx("blur.radial", json!({})), PixelFx::RadialBlur { amount: 10.0, zoom: false, passes: 6, center: [0.5, 0.5] });
assert_eq!(
fx("blur.radial", json!({"amount": 1e308, "method": "ZOOM", "quality": "best"})),
PixelFx::RadialBlur { amount: 100.0, zoom: true, passes: 8 }
fx("blur.radial", json!({"amount": 1e308, "method": "ZOOM", "quality": "best", "centerX": 0.25, "centerY": 1})),
PixelFx::RadialBlur { amount: 100.0, zoom: true, passes: 8, center: [0.25, 1.0] }
);
// A saved center outside the bounds (the commands refuse one) draws at the nearest edge.
assert_eq!(
fx("blur.radial", json!({"amount": -5, "quality": "nonsense", "centerX": -3, "centerY": 7})),
PixelFx::RadialBlur { amount: 1.0, zoom: false, passes: 6, center: [0.0, 1.0] }
);
assert_eq!(fx("blur.radial", json!({"amount": -5, "quality": "nonsense"})), PixelFx::RadialBlur { amount: 1.0, zoom: false, passes: 6 });
assert_eq!(fx("blur.smart", json!({})), PixelFx::SmartBlur { radius: 3.0, threshold: 25.0, samples: 7, mode: SmartBlurMode::Normal });
assert_eq!(
fx("blur.smart", json!({"radius": "1e999", "threshold": -1, "quality": "low", "blurMode": "EDGEONLY"})),
@@ -519,6 +524,11 @@ fn outsets_follow_the_object() {
let far = 0.5 * 100f64.hypot(50.0);
assert!((spin.outset(b) - (far - 25.0)).abs() < 1e-9);
assert!(fx("blur.radial", json!({"method": "zoom", "amount": 100})).outset(b) > 0.4 * far);
// About a corner the spin reaches the circle through the opposite corner, the whole diagonal out.
let corner = fx("blur.radial", json!({"centerX": 0, "centerY": 0}));
assert!((corner.outset(b) - 100f64.hypot(50.0)).abs() < 1e-9, "{}", corner.outset(b));
let zoom = |center: f64| fx("blur.radial", json!({"method": "zoom", "amount": 100, "centerX": center, "centerY": center})).outset(b);
assert!((zoom(1.0) - 2.0 * zoom(0.5)).abs() < 1e-9, "a zoom about a corner reaches twice as far");
assert_eq!(fx("blur.smart", json!({"radius": 4})).outset(b), 4.0);
assert_eq!(fx("blur.smart", json!({"radius": 4})).reach(1.0), Some(4.0));
// The edge modes also read the copy blurred over a wide radius's samples.
@@ -633,6 +643,40 @@ fn spin_blur_smears_along_arcs_and_keeps_the_centre() {
assert_eq!(at(&z, w, 32, 16)[3], 0, "not across it");
}
/// The Blur Center moves the point the blur turns about: a dot on it stays sharp, a dot on the
/// object's centre then spreads, and the center follows the object when it moves or is scaled.
#[test]
fn radial_blur_turns_about_its_blur_center() {
let (w, h) = (41, 41);
// A dot on pixel (10, 30), whose centre (10.5, 30.5) is the Blur Center below, and one on the
// object's centre.
let src = image(w, h, |x, y| if (x == 10 && y == 30) || (x == 20 && y == 20) { grey(255) } else { [0, 0, 0, 0] });
let blur = |p: serde_json::Value, s: &PixelSpace, src: &[u8]| {
let mut d = src.to_vec();
fx("blur.radial", p).apply(&mut d, w, h, s);
d
};
let off = json!({"amount": 60, "quality": "best", "centerX": 10.5 / 41.0, "centerY": 30.5 / 41.0});
let d = blur(off.clone(), &space(w, h), &src);
assert!(at(&d, w, 10, 30)[3] > 100, "the dot on the Blur Center barely moves: {:?}", at(&d, w, 10, 30));
assert!(at(&d, w, 20, 20)[3] < 100, "the dot on the object's centre spreads: {:?}", at(&d, w, 20, 20));
// The default center is the object's centre, the same as no center at all.
assert_eq!(blur(json!({"amount": 60, "centerX": 0.5, "centerY": 0.5}), &space(w, h), &src), blur(json!({"amount": 60}), &space(w, h), &src));
// Moved 7 pt right and 3 pt down, the object draws the same pixels.
let moved =
PixelSpace { to_doc: Affine::translate((7.0, 3.0)), center: space(w, h).center + vectorcraft_geom::Vec2::new(7.0, 3.0), ..space(w, h) };
assert_eq!(blur(off.clone(), &moved, &src), d, "the center moves with the object");
// Drawn at twice the size (two points a pixel), the center lies on the same pixels.
let big = PixelSpace {
to_doc: Affine::scale(2.0),
px: 2.0,
center: Point::new(41.0, 41.0),
size: vectorcraft_geom::Vec2::new(82.0, 82.0),
..space(w, h)
};
assert_eq!(blur(off, &big, &src), d, "the center scales with the object");
}
#[test]
fn smart_blur_smooths_noise_and_keeps_edges() {
let (w, h) = (40, 20);
@@ -862,6 +906,7 @@ fn lengths_are_document_units() {
to_doc: Affine::scale(1.0 / scale as f64),
px: 1.0 / scale as f64,
center: Point::ZERO,
size: vectorcraft_geom::Vec2::ZERO,
channels: Channels::Rgb,
line: 1.0,
extent: 0.0,
@@ -916,7 +961,15 @@ fn linear_light_blur_mixes_light_not_values() {
fn pixel_timings() {
let (w, h) = (2000, 2000);
let src = image(w, h, |x, y| [(x % 256) as u8, (y % 256) as u8, ((x ^ y) % 256) as u8, 255]);
let s = PixelSpace { to_doc: Affine::IDENTITY, px: 1.0, center: Point::new(1000.0, 1000.0), channels: Channels::Rgb, line: 1.0, extent: 0.0 };
let s = PixelSpace {
to_doc: Affine::IDENTITY,
px: 1.0,
center: Point::new(1000.0, 1000.0),
size: vectorcraft_geom::Vec2::new(2000.0, 2000.0),
channels: Channels::Rgb,
line: 1.0,
extent: 0.0,
};
let cases = [
("blur.radial", json!({"quality": "draft"})),
("blur.radial", json!({"quality": "good"})),
+1
View File
@@ -14,6 +14,7 @@ fn space(w: usize, h: usize) -> PixelSpace {
to_doc: Affine::IDENTITY,
px: 1.0,
center: Point::new(w / 2.0, h / 2.0),
size: vectorcraft_geom::Vec2::new(w, h),
channels: Channels::Rgb,
line: 1.0,
extent: 0.5 * w.hypot(h),
+1
View File
@@ -14,6 +14,7 @@ fn space(w: usize, h: usize) -> PixelSpace {
to_doc: Affine::IDENTITY,
px: 1.0,
center: Point::new(w / 2.0, h / 2.0),
size: vectorcraft_geom::Vec2::new(w, h),
channels: Channels::Rgb,
line: 1.0,
extent: 0.5 * w.hypot(h),
+38
View File
@@ -1115,6 +1115,44 @@ fn the_check_builds_the_profile_of_a_shape_with_no_fill() {
}
}
/// A filled shape is checked about the bounds the effect draws its solid about, which take in the
/// strokes beside it. A shape of many small curves is too complex for Extrude & Bevel about the
/// shapes' own bounds, where its curves are sampled more finely, and passes about the bounds of a
/// wide stroke in its group, as its solid draws there.
#[test]
fn the_check_builds_a_filled_shape_about_the_bounds_its_solid_draws_about() {
// 900 curves about (100, 100), each bulging 1 pt out from a side of a polygon 80 pt across.
let corners = 900;
let center = Point::new(100.0, 100.0);
let corner = |k: usize| {
let (s, c) = (k as f64 * std::f64::consts::TAU / corners as f64).sin_cos();
Point::new(center.x + 40.0 * c, center.y + 40.0 * s)
};
let mut b = BezPath::new();
b.move_to(corner(0));
for k in 0..corners {
let (p0, p3) = (corner(k), corner(k + 1));
let out = p0.midpoint(p3) - center;
let out = out / out.hypot();
b.curve_to(p0 + (p3 - p0) / 3.0 + out, p0 + (p3 - p0) * (2.0 / 3.0) + out, p3);
}
b.close_path();
let filled = Node::path(NodeId(1), PathData::from_bezpath(&b), Appearance::basic(Paint::solid(RED), Paint::None, 0.0));
let mut line = BezPath::new();
line.move_to((80.0, 100.0));
line.line_to((120.0, 100.0));
let mut wide = Node::path(NodeId(2), PathData::from_bezpath(&line), Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 2000.0));
wide.appearance.items.retain(|i| matches!(i, AppearanceItem::Stroke(_)));
let p = json!({"bevel": "rounded", "bevelRepeat": 6});
let (path, rule) = profile(&filled).unwrap();
let own = path.bounds().unwrap().union(wide.path_data().unwrap().bounds().unwrap());
assert_eq!(facets(EXTRUDE, &path, rule, own, &p).map(|_| ()), Err(vectorcraft_three_d::TOO_COMPLEX_SOLID.to_string()), "about its own bounds");
let mut g = Node::group(NodeId(9999), vec![Arc::new(filled), Arc::new(wide)]);
assert_eq!(validate_solid(&g, EXTRUDE, &p), Ok(()));
g.appearance.effects.push(fx(EXTRUDE, p));
assert!(solid_art(&g).is_some(), "the solid draws");
}
/// A convex polygon of `corners` corners about (20,000, 20,000), filled red, with no stroke.
fn polygon_node(id: u64, corners: usize) -> Node {
let points: Vec<Point> = (0..corners)
@@ -128,6 +128,7 @@ mod tests {
to_doc: Affine::scale(1.0),
px: 1.0,
center: Point::new(wd / 2.0, wd / 2.0),
size: vectorcraft_geom::Vec2::new(wd, wd),
channels: Channels::Rgb,
line: 1.0,
extent: 0.5 * wd.hypot(wd),
+9 -1
View File
@@ -10,7 +10,15 @@ use crate::{PixelFx, RasterFx, raster_effects};
/// resolution whose pixels are `line` points wide.
fn space(w: usize, h: usize, px: f64, line: f64) -> PixelSpace {
let (wd, hd) = (w as f64 * px, h as f64 * px);
PixelSpace { to_doc: Affine::scale(px), px, center: Point::new(wd / 2.0, hd / 2.0), channels: Channels::Rgb, line, extent: 0.5 * wd.hypot(hd) }
PixelSpace {
to_doc: Affine::scale(px),
px,
center: Point::new(wd / 2.0, hd / 2.0),
size: vectorcraft_geom::Vec2::new(wd, hd),
channels: Channels::Rgb,
line,
extent: 0.5 * wd.hypot(hd),
}
}
/// `w` × `h` premultiplied pixels: `f(x, y)` each.
+1
View File
@@ -1850,6 +1850,7 @@ fn rasterize_compose_leaves_the_raster_alone_when_the_sizes_do_not_fit() {
to_doc: Affine::IDENTITY,
px: 1.0,
center: Point::new(1.0, 1.0),
size: vectorcraft_geom::Vec2::new(2.0, 2.0),
channels: pixel::Channels::Rgb,
line: 1.0,
extent: std::f64::consts::SQRT_2,