New nodes: 'Twirl', 'Pinch', 'Spherize', and 'Polar Coordinates' (#5042)

Add Twirl, Pinch, Spherize, and Polar Coordinates effects
This commit is contained in:
Keavon Chambers authored and GitHub committed 2026-12-01 16:32:11 -08:00
1 parent d6ee55b86a
commit 738ea6ac20
22 files changed
+3129 -216

No files matched your search

+190 -22
View File
@@ -30,7 +30,7 @@ use graphic_types::filters::is_random_field_filter;
use graphic_types::generator::GeneratorPattern;
use graphic_types::masks::{MaskMode, masked_radius_blur_levels};
use graphic_types::raster_types::{CPU, GPU, Raster};
use graphic_types::resampling::frame_of_bounds;
use graphic_types::resampling::{FrameWarp, frame_of_bounds};
use graphic_types::vector_types::gradient::{Gradient, GradientForm};
use graphic_types::vector_types::vector::click_target::{ClickTarget, FreePoint, TargetPath};
use graphic_types::vector_types::vector::misc::dvec2_to_point;
@@ -1109,8 +1109,13 @@ fn finish_deferred_markup(
fn svg_expresses(operation: &EffectOperation) -> bool {
match operation {
EffectOperation::Adjustment(adjustment) => svg_expresses_adjustment(adjustment),
// No primitive blurs along a line or arc
EffectOperation::MotionBlur { .. } | EffectOperation::RadialBlur { .. } => false,
// Browsers displace with nearest-neighbor sampling, which can't match the GPU's interpolation, and no primitive blurs along a line or arc
EffectOperation::MotionBlur { .. }
| EffectOperation::RadialBlur { .. }
| EffectOperation::Twirl { .. }
| EffectOperation::Pinch { .. }
| EffectOperation::Spherize { .. }
| EffectOperation::PolarCoordinates { .. } => false,
_ => true,
}
}
@@ -1850,7 +1855,12 @@ fn write_svg_effects_filter_on_lattice(
continue;
}
// The whole item becomes an image the GPU renders, which replaces this filter
EffectOperation::MotionBlur { .. } | EffectOperation::RadialBlur { .. } => continue,
EffectOperation::MotionBlur { .. }
| EffectOperation::RadialBlur { .. }
| EffectOperation::Twirl { .. }
| EffectOperation::Pinch { .. }
| EffectOperation::Spherize { .. }
| EffectOperation::PolarCoordinates { .. } => continue,
};
for (plane, within, color, blend_mode) in planes {
@@ -2840,10 +2850,20 @@ fn render_graphic_item_svg(item: ItemRef<'_, Graphic>, next_clips: bool, mask_st
bounds => bounds,
};
// Warps change even content without edges within their frames, so its effects, masks included, are carried out over the area in view, as for an endless color or gradient
let endless_area = match effects {
Some(effects) if warps_change_endless_content(effects) && matches!(bounds(), RenderBoundingBox::Infinite) => filtered_endless_area(effects, &compositing, render, render_params),
_ => None,
};
let effects_bounds = || match (bounds(), endless_area) {
(RenderBoundingBox::Infinite, Some(area)) => RenderBoundingBox::Rectangle(area),
(bounds, _) => bounds,
};
// Masks that leave it without edges wrap it in a group of their own, which takes the blend mode so it blends with what lies beneath
let has_mask = effects.is_some_and(|effects| effects.operations().any(|operation| matches!(operation, EffectOperation::Mask { .. })));
let endless_mask = match effects {
Some(effects) if has_mask && matches!(bounds(), RenderBoundingBox::Infinite) => masks::write_endless_content_mask(render, effects, transform, render_params),
Some(effects) if has_mask && matches!(effects_bounds(), RenderBoundingBox::Infinite) => masks::write_endless_content_mask(render, effects, transform, render_params),
_ => None,
};
let endless_masked = endless_mask.is_some();
@@ -2854,8 +2874,8 @@ fn render_graphic_item_svg(item: ItemRef<'_, Graphic>, next_clips: bool, mask_st
Some(effects) if effects.any_takes_frame() => nested_effects_reach(element, transform, |_| true),
_ => DVec2::ZERO,
};
let deferred = start_deferred_markup(render, render_params, &compositing, bounds, nested_reach.max_element());
let filters = compositing.svg_filters(render, render_params, bounds, |keeps| Graphic::Graphic(Box::new(effects_content(item, element.clone(), keeps))));
let deferred = start_deferred_markup(render, render_params, &compositing, effects_bounds, nested_reach.max_element());
let filters = compositing.svg_filters(render, render_params, effects_bounds, |keeps| Graphic::Graphic(Box::new(effects_content(item, element.clone(), keeps))));
let filtered = filters.is_some();
render_svg_through_filters(render, filters, compositing.opacity, render_params.to_canvas(), |render| {
@@ -5107,7 +5127,7 @@ fn render_color_item_svg(item: ItemRef<'_, Color>, render: &mut SvgRender, rende
let Some(&color) = item.element() else { return };
let compositing = item.compositing(render_params, item.attribute_cloned_or_default(ATTR_TRANSFORM));
// Leading masks that give the color edges let its effects be carried out within them like any other content's, and filters, which change even a flat color, carry them out over the area in view
// Leading masks that give the color edges let its effects be carried out within them like any other content's, and filters and warps, which change even a flat color, carry them out over the area in view
let extent = compositing
.effects
.and_then(|(effects, _)| masks::masked_extent(effects, compositing.placement).or_else(|| filtered_endless_area(effects, &compositing, render, render_params)));
@@ -5132,9 +5152,19 @@ fn render_color_item_svg(item: ItemRef<'_, Color>, render: &mut SvgRender, rende
render_edgeless_color_svg(item, render, &compositing, color, render_params);
}
/// The area in view in the current user space, grown past it by how far the `effects` and the groups around them read, for endless content whose filters read it there, if it has any.
/// Whether the effects change endless content (like a color or gradient filling everything) over the area in view, as filters reading its details do, and warps within their frames.
fn changes_endless_content_in_view(effects: &Effects) -> bool {
graphic_types::filters::has_filters(effects) || warps_change_endless_content(effects)
}
/// Whether any of the effects is a warp that changes something, which it does within its frame even to endless content.
fn warps_change_endless_content(effects: &Effects) -> bool {
effects.operations().any(|operation| FrameWarp::of(&operation).is_some() && !operation.changes_nothing())
}
/// The area in view in the current user space, grown past it by how far the `effects` and the groups around them read, for endless content that its filters or warps change there, if it has any.
fn filtered_endless_area(effects: &Effects, compositing: &ItemCompositing, render: &SvgRender, render_params: &RenderParams) -> Option<[DVec2; 2]> {
if !graphic_types::filters::has_filters(effects) {
if !changes_endless_content_in_view(effects) {
return None;
}
let own_reach = effects
@@ -5347,8 +5377,8 @@ fn render_color_item_to_vello(item: ItemRef<'_, Color>, scene: &mut VelloSceneBu
let blend_mode: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE);
// Leading masks that give the color edges let its effects be carried out within them like any other content's, and filters, which change even a flat color, carry them out over the area in view, as in SVG
let carried_out = |effects: &Effects| masks::masked_extent(effects, compositing.placement).is_some() || graphic_types::filters::has_filters(effects);
// Leading masks that give the color edges let its effects be carried out within them like any other content's, and filters and warps, which change even a flat color, carry them out over the area in view, as in SVG
let carried_out = |effects: &Effects| masks::masked_extent(effects, compositing.placement).is_some() || changes_endless_content_in_view(effects);
if compositing.effects.is_some_and(|(effects, _)| carried_out(effects)) {
let vello_color = SRGBA8::from(color).to_peniko_color();
return draw_endless_item_with_effects(scene, &compositing, blend_mode, parent_transform, render_params, |scene| scene.fill_viewport(vello_color, None));
@@ -5461,7 +5491,7 @@ fn render_gradient_item_svg_with_thumbnail_rect(item: ItemRef<'_, Gradient>, thu
let compositing = item.compositing(render_params, item.attribute_cloned_or_default(ATTR_TRANSFORM));
let settings = gradient_settings_from_item(item);
// Leading masks that give the gradient edges let its effects be carried out within them like any other content's, and filters reading its details carry them out over the area in view
// Leading masks that give the gradient edges let its effects be carried out within them like any other content's, and filters reading its details and warps carry them out over the area in view
let extent = compositing
.effects
.and_then(|(effects, _)| masks::masked_extent(effects, compositing.placement).or_else(|| filtered_endless_area(effects, &compositing, render, render_params)));
@@ -5629,8 +5659,8 @@ fn render_gradient_item_to_vello(item: ItemRef<'_, Gradient>, scene: &mut VelloS
let blend_mode: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE);
// Leading masks that give the gradient edges let its effects be carried out within them like any other content's, and filters reading its details carry them out over the area in view, as in SVG
let carried_out = |effects: &Effects| masks::masked_extent(effects, compositing.placement).is_some() || graphic_types::filters::has_filters(effects);
// Leading masks that give the gradient edges let its effects be carried out within them like any other content's, and filters reading its details and warps carry them out over the area in view, as in SVG
let carried_out = |effects: &Effects| masks::masked_extent(effects, compositing.placement).is_some() || changes_endless_content_in_view(effects);
if compositing.effects.is_some_and(|(effects, _)| carried_out(effects)) {
let (brush, brush_transform) = gradient_brush(item, source_gradient, settings, parent_transform);
return draw_endless_item_with_effects(scene, &compositing, blend_mode, parent_transform, render_params, |scene| {
@@ -6961,7 +6991,7 @@ mod tests {
let zoom = graphic_types::EffectOperation::RadialBlur {
method: graphic_types::RadialBlurMethod::Zoom,
amount: 100.,
center: DVec2::splat(5.),
transform: DAffine2::from_translation(DVec2::splat(5.)),
gamma: false,
};
let group = Item::new_from_element(Graphic::VectorList(orange_square(std::slice::from_ref(&zoom), 1., 1.))).with_attribute(ATTR_TRANSFORM, DAffine2::from_scale(DVec2::splat(2.)));
@@ -6994,7 +7024,7 @@ mod tests {
let zoom = graphic_types::EffectOperation::RadialBlur {
method: graphic_types::RadialBlurMethod::Zoom,
amount: 200.,
center: DVec2::ZERO,
transform: DAffine2::IDENTITY,
gamma: false,
};
let effects = Effects::from_operations([zoom]);
@@ -7056,15 +7086,21 @@ mod tests {
let radial_blur = graphic_types::EffectOperation::RadialBlur {
method: graphic_types::RadialBlurMethod::Spin,
amount: 30.,
center: DVec2::splat(5.),
transform: DAffine2::from_translation(DVec2::splat(5.)),
gamma: false,
};
// Nor does a warp, which acts within the frame its own transform places
let twirl = graphic_types::EffectOperation::Twirl {
angle: 90.,
transform: DAffine2::from_scale(DVec2::splat(10.)),
};
let shadow = drop_shadow(180., 3., 2.);
let cases = [
(vec![shadow.clone()], false),
(vec![shadow.clone(), motion_blur(4.)], false),
(vec![shadow.clone(), motion_blur(0.)], false),
(vec![shadow, radial_blur], false),
(vec![shadow.clone(), radial_blur], false),
(vec![shadow, twirl], false),
];
for (effects, takes_frame) in cases {
@@ -7147,6 +7183,47 @@ mod tests {
);
}
#[test]
fn thumbnails_and_exports_hold_the_corners_of_a_turned_square_that_warps_paint_in() {
use graphic_types::EffectOperation;
use kurbo::Shape;
// A disc 30 in radius in a group turned an eighth and unwrapped across the square around it, which reaches 30√2 from the middle along each axis, past the disc's 30
let mut disc = Vector::item_from_bezpath(&kurbo::Circle::new((0., 0.), 30.).to_path(0.1));
disc.set_attribute(ATTR_APPEARANCE, Appearance::new_single(Coverage::new_fill(), Graphic::ColorList(List::new_from_element(Color::RED))));
let unwrap = EffectOperation::PolarCoordinates {
inverse: true,
transform: DAffine2::from_scale(DVec2::splat(60.)),
};
let group = Item::new_from_element(Graphic::Vector(Box::new(disc)))
.with_attribute(ATTR_TRANSFORM, DAffine2::from_translation(DVec2::splat(64.)) * DAffine2::from_angle(std::f64::consts::FRAC_PI_4))
.with_attribute(ATTR_EFFECTS, Effects::from_operations([unwrap]))
.with_attribute(ATTR_EDITOR_LAYER_PATH, NodeIdPath::from(vec![NodeId(1)]));
// The group's layer inside another one, which the square reaches past too
let outer = Item::new_from_element(Graphic::GraphicList(List::new_from_item(group))).with_attribute(ATTR_EDITOR_LAYER_PATH, NodeIdPath::from(vec![NodeId(2)]));
let document = List::new_from_item(outer);
let spans = |distance: f64, [min, max]: [DVec2; 2]| (min - DVec2::splat(64. - distance)).abs().max_element() < 0.05 && (max - DVec2::splat(64. + distance)).abs().max_element() < 0.05;
let corner = 30. * std::f64::consts::SQRT_2;
// The thumbnail frames the corners, unless SVG draws it without the GPU's renders, which leave the warp out
let thumbnail = |maps_resolved: bool| match graphic_list_painted_bounds(&document, maps_resolved) {
RenderBoundingBox::Rectangle(bounds) => bounds,
bounds => panic!("the disc is bounded by {bounds:?}"),
};
assert!(spans(corner, thumbnail(true)), "the thumbnail frames {:?}", thumbnail(true));
assert!(spans(30., thumbnail(false)), "the thumbnail without the GPU frames {:?}", thumbnail(false));
// Exports hold the corners for both layers by how far the square reaches past the disc
let mut metadata = RenderMetadata::default();
document.collect_metadata(&mut metadata, Footprint::default(), None, None);
for layer in [NodeId(1), NodeId(2)] {
let reach = metadata.effects_reach.get(&layer).copied();
assert!(reach.is_some_and(|reach| (reach - (corner - 30.)).abs() < 0.05), "the layer {layer:?} reaches {reach:?}");
}
}
#[test]
fn svg_maps_cover_past_the_view_as_far_as_the_filters_reading_them_reach() {
let blur = graphic_types::EffectOperation::Blur {
@@ -7721,10 +7798,11 @@ mod tests {
// A spin about the middle of an image 10 units square inside a group scaled 5 times, so the image spans 50 units of the document
let mut image = Item::new_from_element(Raster::new_cpu(Image::new(2, 2, Color::WHITE))).with_attribute(ATTR_TRANSFORM, DAffine2::from_scale(DVec2::splat(10.)));
let center = DAffine2::from_translation(DVec2::splat(5.));
let spin = EffectOperation::RadialBlur {
method: RadialBlurMethod::Spin,
amount: 90.,
center: DVec2::splat(5.),
transform: center,
gamma: false,
};
stamp_effect(&mut image, Effect::new(&spin));
@@ -7739,11 +7817,12 @@ mod tests {
},
);
// The spin carries the corners as far as its size in the document says, a fifth of that in the group's units
// The spin carries the corners as far as it does in its effect space, which the group scales into the document and back into its own units
let [deferred] = &render.deferred_images[..] else {
panic!("expected one deferred image, got {}", render.deferred_images.len())
};
let reach = RadialBlur::new(RadialBlurMethod::Spin, 90., DVec2::splat(5.)).reach(25. * std::f64::consts::SQRT_2) / 5.;
let corners = [DVec2::ZERO, DVec2::new(10., 0.), DVec2::splat(10.), DVec2::new(0., 10.)];
let reach = RadialBlur::new(RadialBlurMethod::Spin, 90., center).reach_around(corners, 0.);
let expected = [DVec2::splat(-reach), DVec2::splat(10. + reach)];
assert!(
(deferred.bounds[0] - expected[0]).length() < 1e-9 && (deferred.bounds[1] - expected[1]).length() < 1e-9,
@@ -7752,6 +7831,32 @@ mod tests {
);
}
#[test]
fn effects_svg_cannot_express_that_change_nothing_leave_the_content_live() {
use graphic_types::raster_types::Image;
use graphic_types::{Effect, EffectKind, EffectOperation, stamp_effect};
let mut item = Item::new_from_element(Raster::new_cpu(Image::new(2, 2, Color::WHITE))).with_attribute(ATTR_TRANSFORM, DAffine2::from_scale(DVec2::splat(10.)));
stamp_effect(&mut item, Effect::new(&EffectOperation::default_for(EffectKind::DropShadow)));
stamp_effect(
&mut item,
Effect::new(&EffectOperation::Twirl {
angle: 0.,
transform: DAffine2::from_scale(DVec2::splat(10.)),
}),
);
let mut render = SvgRender::new();
List::new_from_item(item).render_svg(
&mut render,
&RenderParams {
for_export: true,
..Default::default()
},
);
assert!(render.deferred_images.is_empty(), "{}", render.svg.to_svg_string());
}
#[test]
fn svg_paint_overlays_blend_with_the_content_in_gamma() {
let mut primitives = SvgFilterPrimitives::default();
@@ -8393,4 +8498,67 @@ mod tests {
assert!(region.iter().zip(expected).all(|(actual, expected)| (actual - expected).abs() < 1e-9), "{region:?} in {defs}");
assert!(!defs_of(Vec::new()).contains("<filter"));
}
#[test]
fn svg_endless_colors_and_gradients_draw_their_warps_as_the_gpu_renders_them_in_view() {
use glam::UVec2;
use graphic_types::EffectOperation;
let render_params = RenderParams {
view: Some(Footprint {
resolution: UVec2::new(96, 64),
..Default::default()
}),
for_export: true,
..Default::default()
};
let images_of = |graphic: Graphic| {
let mut render = SvgRender::new();
List::new_from_element(graphic).render_svg(&mut render, &render_params);
render.deferred_images.len()
};
// Each warp's circle or square bounds where it changes the content, so it's carried out over the view, unless its transform collapses
let in_view = DAffine2::from_translation(DVec2::new(48., 32.)) * DAffine2::from_scale(DVec2::splat(40.));
let twirl = |transform| Effects::from_operations([EffectOperation::Twirl { angle: 90., transform }]);
let wrap = Effects::from_operations([EffectOperation::PolarCoordinates { inverse: false, transform: in_view }]);
let gradient = |effects| Graphic::Gradient(Item::new_from_element(Gradient::from(vec![Color::BLACK, Color::WHITE])).with_attribute(ATTR_EFFECTS, effects));
let color = |effects| Graphic::Color(Item::new_from_element(Color::BLUE).with_attribute(ATTR_EFFECTS, effects));
assert_eq!(images_of(gradient(twirl(in_view))), 1);
assert_eq!(images_of(color(wrap)), 1);
assert_eq!(images_of(gradient(twirl(DAffine2::from_scale(DVec2::new(40., 0.))))), 0);
// Likewise for a warp on a group holding an endless gradient
let plain_gradient = Graphic::Gradient(Item::new_from_element(Gradient::from(vec![Color::BLACK, Color::WHITE])));
let group = Graphic::Graphic(Box::new(Item::new_from_element(plain_gradient).with_attribute(ATTR_EFFECTS, twirl(in_view))));
assert_eq!(images_of(group), 1);
}
#[test]
fn warps_reach_their_turned_square_past_what_nested_effects_paint() {
use graphic_types::EffectOperation;
// A square 10 across blurred inside a group unwrapped across a square twice as large turned an eighth, with its corners 10√2 from the middle
let blur = EffectOperation::Blur {
radius_mask: Default::default(),
radius: 1.,
box_blur: false,
gamma: false,
};
let unwrap = EffectOperation::PolarCoordinates {
inverse: true,
transform: DAffine2::from_translation(DVec2::splat(5.)) * DAffine2::from_angle(std::f64::consts::FRAC_PI_4) * DAffine2::from_scale(DVec2::splat(20.)),
};
let group = Item::new_from_element(Graphic::VectorList(orange_square(&[blur], 1., 1.))).with_attribute(ATTR_EFFECTS, Effects::from_operations([unwrap]));
let RenderBoundingBox::Rectangle([min, max]) = graphic_list_painted_bounds(&List::new_from_item(group), true) else {
panic!("the group should have bounds");
};
let corner = 10. * std::f64::consts::SQRT_2;
assert!(
min.cmple(DVec2::splat(5. - corner)).all() && max.cmpge(DVec2::splat(5. + corner)).all(),
"the bounds {min} to {max} cut off the square's corners"
);
}
}