mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
Scripts: pathItems.removeAll judges a kept clipping region by its live effects
The check that an image or placed-document frame still encloses an area now measures each subpath from its own first point, so a tiny frame far from the origin is not lost to roundoff and a collapsed frame is not counted through roundoff. When a live geometry effect, such as an Offset Path inset, would leave no clipping region once the paths are deleted, removeAll() now throws and deletes nothing. The preflight copies only the members that hold a path being deleted.
This commit is contained in:
1 parent
761f99fc5a
commit
07c33b9f2a
4 files changed
+75
-12
No files matched your search
@@ -625,9 +625,7 @@ fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer {
|
||||
&& match &n.kind {
|
||||
NodeKind::Path { .. } => true,
|
||||
// A frame collapsed to a line or a point encloses nothing to clip by.
|
||||
NodeKind::Image(_) | NodeKind::PlacedDocument(_) => {
|
||||
n.clip_shapes(None).iter().any(|(frame, _)| kurbo::Shape::area(frame).abs() > 0.0)
|
||||
}
|
||||
NodeKind::Image(_) | NodeKind::PlacedDocument(_) => n.clip_shapes(None).iter().any(|(frame, _)| encloses(frame)),
|
||||
NodeKind::Text(t) => {
|
||||
in_mask.contains(&n.id) && !vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t).to_bezpath().elements().is_empty()
|
||||
}
|
||||
@@ -639,10 +637,16 @@ fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer {
|
||||
if n.visible && ((shape && !going.contains(&n.id)) || inner) {
|
||||
geometry.insert(n.id);
|
||||
}
|
||||
// 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.
|
||||
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)))
|
||||
};
|
||||
if n.clips()
|
||||
&& let Some(mask) = kids.first()
|
||||
&& affected.contains(&mask.id)
|
||||
&& !geometry.contains(&mask.id)
|
||||
&& (!geometry.contains(&mask.id) || (reshaped_empty(&without(mask, &going, &affected)) && !reshaped_empty(mask)))
|
||||
&& kids.iter().skip(1).any(|k| !going.contains(&k.id) && !matches!(k.kind, NodeKind::RulerGuide { .. }))
|
||||
&& masks.len() < 20
|
||||
{
|
||||
@@ -671,6 +675,44 @@ fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer {
|
||||
Ok(Data::Undefined)
|
||||
}
|
||||
|
||||
/// Whether a subpath of `outline` encloses an area. Each subpath's area is measured from its first
|
||||
/// point, so its position does not change the result, and an area within roundoff of its own
|
||||
/// bounding box's is counted as none.
|
||||
fn encloses(outline: &kurbo::BezPath) -> bool {
|
||||
let mut subpaths: Vec<kurbo::BezPath> = Vec::new();
|
||||
for el in outline.elements() {
|
||||
if matches!(el, kurbo::PathEl::MoveTo(_)) || subpaths.is_empty() {
|
||||
subpaths.push(kurbo::BezPath::new());
|
||||
}
|
||||
if let Some(last) = subpaths.last_mut() {
|
||||
last.push(*el);
|
||||
}
|
||||
}
|
||||
subpaths.into_iter().any(|mut sub| {
|
||||
let Some(start) = sub.elements().iter().find_map(|e| e.end_point()) else { return false };
|
||||
sub.apply_affine(kurbo::Affine::translate(-start.to_vec2()));
|
||||
let area = kurbo::Shape::area(&sub).abs();
|
||||
area > 0.0 && area > 64.0 * f64::EPSILON * kurbo::Shape::bounding_box(&sub).area()
|
||||
})
|
||||
}
|
||||
|
||||
/// `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(
|
||||
n: &vectorcraft_doc::Node,
|
||||
going: &std::collections::HashSet<NodeId>,
|
||||
affected: &std::collections::HashSet<NodeId>,
|
||||
) -> vectorcraft_doc::Node {
|
||||
let mut m = n.clone();
|
||||
if let Some(kids) = m.children_mut() {
|
||||
kids.retain(|k| !going.contains(&k.id));
|
||||
for k in kids.iter_mut().filter(|k| affected.contains(&k.id)) {
|
||||
*k = Arc::new(without(k, going, affected));
|
||||
}
|
||||
}
|
||||
m
|
||||
}
|
||||
|
||||
/// The id in the JSON result of a command that made an object, read exactly, as the text creators
|
||||
/// read it.
|
||||
fn made_id(r: &(Data, Value)) -> std::result::Result<NodeId, HostError> {
|
||||
|
||||
Reference in new issue
Block a user