mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
Scripts: pathItems.removeAll measures an effect's clip region under its fill rule
When a live geometry effect reshapes what stays in a clipping path, removeAll() now measures the area that outline fills under its fill rule, with overlaps resolved, instead of the area of each subpath. An image whose Transform effect adds a copy reflected onto itself fills nothing under nonzero, so deleting the other paths now throws and deletes nothing. A frame that an effect scales to a thin strip still counts as a clipping region.
This commit is contained in:
1 parent
04ea00461d
commit
f6de56e490
2 files changed
+35
-4
No files matched your search
@@ -655,8 +655,10 @@ fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer {
|
||||
// The live geometry effects of what stays in the clipping path reshape the region it
|
||||
// clips by, as the renderer draws it; deleting paths that lets an effect shrink the
|
||||
// region to nothing leaves no outline. A region that was empty before is left as it is.
|
||||
// The region is the area the outline fills under its fill rule, so outlines that run
|
||||
// opposite ways over the same area under nonzero fill nothing.
|
||||
let reshaped_empty = |m: &vectorcraft_doc::Node| {
|
||||
vectorcraft_render::effects::reshaped_clip_outline(m).is_some_and(|o| !o.is_some_and(|(bp, _)| encloses(&bp)))
|
||||
vectorcraft_render::effects::reshaped_clip_outline(m).is_some_and(|o| !o.is_some_and(|(bp, rule)| fills(&bp, rule)))
|
||||
};
|
||||
if n.clips()
|
||||
&& let Some(mask) = kids.first()
|
||||
@@ -711,6 +713,19 @@ fn encloses(outline: &kurbo::BezPath) -> bool {
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether `outline` fills an area under `rule`: a subpath encloses an area ([`encloses`]) and the
|
||||
/// outline with its overlaps resolved, thin strips included, has an area. An outline the path
|
||||
/// operations refuse is judged by [`encloses`] alone.
|
||||
fn fills(outline: &kurbo::BezPath, rule: vectorcraft_geom::FillRule) -> bool {
|
||||
if !encloses(outline) {
|
||||
return false;
|
||||
}
|
||||
match vectorcraft_pathops::try_area(&vectorcraft_geom::PathData::from_bezpath(outline), rule) {
|
||||
Ok(area) => area > 0.0,
|
||||
Err(_) => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// `n` without the members in `going`, at every depth. Only the members in `affected` (those that
|
||||
/// hold a member in `going`) are copied; the others are shared.
|
||||
fn without(
|
||||
|
||||
@@ -4060,7 +4060,7 @@ fn document_path_items_remove_all_refuses_to_empty_a_clipping_group_with_an_empt
|
||||
/// an image still has an outline after its paths are deleted.
|
||||
#[test]
|
||||
fn document_path_items_remove_all_keeps_a_clipping_group_whose_clipping_path_keeps_an_image() {
|
||||
for frame in [Frame::Square, Frame::Line, Frame::Far, Frame::Inset] {
|
||||
for frame in [Frame::Square, Frame::Line, Frame::Far, Frame::Inset, Frame::Mirrored, Frame::Strip] {
|
||||
remove_all_beside_an_image(frame);
|
||||
}
|
||||
}
|
||||
@@ -4076,12 +4076,18 @@ enum Frame {
|
||||
Far,
|
||||
/// 40 pt square with a live Offset Path of -30 pt, which leaves nothing.
|
||||
Inset,
|
||||
/// 40 pt square with a live Transform effect that adds one copy reflected about its center:
|
||||
/// the two outlines run opposite ways over the same area and fill nothing under nonzero.
|
||||
Mirrored,
|
||||
/// 40 pt square with a live Transform effect that scales it to 0.004 pt high, which still
|
||||
/// clips to a thin strip.
|
||||
Strip,
|
||||
}
|
||||
|
||||
/// A clipping group whose clipping path is a group of one path and an image with `frame`, then the
|
||||
/// document's `pathItems.removeAll()`.
|
||||
fn remove_all_beside_an_image(frame: Frame) {
|
||||
let flat = matches!(frame, Frame::Line | Frame::Inset);
|
||||
let flat = matches!(frame, Frame::Line | Frame::Inset | Frame::Mirrored);
|
||||
let (mut s, a, b) = doc();
|
||||
let t = NodeId(s.execute("text.create", &json!({"x": 10, "y": 250, "text": "hi"})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("select.set", &json!({"ids": [a.0]})).unwrap();
|
||||
@@ -4094,7 +4100,7 @@ fn remove_all_beside_an_image(frame: Frame) {
|
||||
width: 4,
|
||||
height: 4,
|
||||
xf: match frame {
|
||||
Frame::Square | Frame::Inset => kurbo::Affine::scale(10.0),
|
||||
Frame::Square | Frame::Inset | Frame::Mirrored | Frame::Strip => kurbo::Affine::scale(10.0),
|
||||
Frame::Line => kurbo::Affine::scale_non_uniform(10.0, 0.0),
|
||||
Frame::Far => kurbo::Affine::translate((3_500_000.0, 3_500_000.0)) * kurbo::Affine::scale(1.0 / 256.0),
|
||||
},
|
||||
@@ -4109,6 +4115,16 @@ fn remove_all_beside_an_image(frame: Frame) {
|
||||
if frame == Frame::Inset {
|
||||
s.execute("effect.apply", &json!({"effect": "path.offsetPath", "params": {"offset": -30}, "ids": [image.0], "item": null})).unwrap();
|
||||
}
|
||||
if frame == Frame::Mirrored {
|
||||
s.execute(
|
||||
"effect.apply",
|
||||
&json!({"effect": "distort.transform", "params": {"reflectX": true, "copies": 1, "reference": 4}, "ids": [image.0], "item": null}),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
if frame == Frame::Strip {
|
||||
s.execute("effect.apply", &json!({"effect": "distort.transform", "params": {"scaleV": 0.01}, "ids": [image.0], "item": null})).unwrap();
|
||||
}
|
||||
s.execute("select.set", &json!({"ids": [mask.0, t.0]})).unwrap();
|
||||
let outer = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
s.edit("Clip", |d, _| {
|
||||
|
||||
Reference in new issue
Block a user