mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 12:21:08 +01:00
Merge pull request #9335 from dotCooCoo/fix/copy-region-ids-isolated-ancestor
Fill Area: ids that name what holds the isolated group take its paths
This commit is contained in:
5 files changed
+130
-10
No files matched your search
@@ -77,7 +77,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Fill Area",
|
||||
[],
|
||||
None,
|
||||
"{point: [x,y], ids?|id?: [id…] (the objects or layers whose paths bound areas; default: every layer, nothing needs to be selected), gap?: pt (0–72; 0: Gap Detection off; out of range is an error), openFilledAsClosed?: true (as shapeBuilder.merge), fill?, stroke?: \"#rrggbb\" | {color|swatch|gradient|none} (default: the current fill and stroke, with the current stroke weight)} add a new closed path of the area under point that the visible, unlocked paths enclose (the region of shapeBuilder.regions holding it: areas a shape already fills and areas open paths bound count too; a path a clipping mask clips bounds areas only where every mask around it shows it), directly above the front-most object along the area (its outline touches the area's outline, or it covers the area; when a group, Live Paint group, Shaper group or clipping group holds that path, above the outermost group in its layer, never inside it), and select it; in isolation mode only the paths of the isolated layer or group bound areas (with ids, only the paths of the listed objects that are in it), and the new path goes inside it, directly above the object in it that holds that path; no area is found while the isolated object is not a layer or group (a path or compound path), is a Live Paint group, compound shape, Shaper group, graph, Image Trace result, dimension, flare or Objects on Path group, is a layer or group with a live effect or a clipping mask, or lies inside one of these or inside an envelope; the paths around the area are not changed; a point in no enclosed area is an error and changes nothing (no undo step); when the paths together have more than 65,536 segments, only an area the paths near the point enclose is found → {id, above (the object the new path went above)}",
|
||||
"{point: [x,y], ids?|id?: [id…] (the objects or layers whose paths bound areas; default: every layer, nothing needs to be selected), gap?: pt (0–72; 0: Gap Detection off; out of range is an error), openFilledAsClosed?: true (as shapeBuilder.merge), fill?, stroke?: \"#rrggbb\" | {color|swatch|gradient|none} (default: the current fill and stroke, with the current stroke weight)} add a new closed path of the area under point that the visible, unlocked paths enclose (the region of shapeBuilder.regions holding it: areas a shape already fills and areas open paths bound count too; a path a clipping mask clips bounds areas only where every mask around it shows it), directly above the front-most object along the area (its outline touches the area's outline, or it covers the area; when a group, Live Paint group, Shaper group or clipping group holds that path, above the outermost group in its layer, never inside it), and select it; in isolation mode only the paths of the isolated layer or group bound areas (with ids, the paths of the listed objects that are in it, and all of its paths when a listed layer or group holds it or is it; other listed objects are ignored), and the new path goes inside it, directly above the object in it that holds that path; no area is found while the isolated object is not a layer or group (a path or compound path), is a Live Paint group, compound shape, Shaper group, graph, Image Trace result, dimension, flare or Objects on Path group, is a layer or group with a live effect or a clipping mask, or lies inside one of these or inside an envelope; the paths around the area are not changed; a point in no enclosed area is an error and changes nothing (no undo step); when the paths together have more than 65,536 segments, only an area the paths near the point enclose is found → {id, above (the object the new path went above)}",
|
||||
has_doc,
|
||||
sb_copy_region
|
||||
),
|
||||
|
||||
@@ -232,6 +232,29 @@ fn fill_area_takes_clipped_art_only_where_its_mask_shows_it() {
|
||||
assert_eq!((undo_len(&s), stack(&s)), (undo, kids));
|
||||
}
|
||||
|
||||
/// Parts of an outline that lie on the mask's edge show: a square without a fill, clipped by a mask
|
||||
/// of the same square, keeps all four sides and bounds its whole area. A square just outside the
|
||||
/// mask is hidden and bounds nothing.
|
||||
#[test]
|
||||
fn fill_area_keeps_outline_parts_on_the_mask_edge() {
|
||||
let mut s = session();
|
||||
let square = rect(&mut s, 0.0, 0.0, 100.0, 100.0);
|
||||
s.execute("paint.setFill", &json!({"none": true})).unwrap();
|
||||
clip_by(&mut s, &[square], 0.0, 0.0, 100.0, 100.0);
|
||||
deselect(&mut s);
|
||||
let r = s.execute("shapeBuilder.copyRegion", &json!({"point": [50, 50]})).unwrap();
|
||||
assert!((area(&s, id_of(&r)) - 10000.0).abs() < 1.0, "{}", area(&s, id_of(&r)));
|
||||
|
||||
let mut s = session();
|
||||
let square = rect(&mut s, -0.005, -0.005, 100.01, 100.01);
|
||||
s.execute("paint.setFill", &json!({"none": true})).unwrap();
|
||||
clip_by(&mut s, &[square], 0.0, 0.0, 100.0, 100.0);
|
||||
deselect(&mut s);
|
||||
let (undo, kids) = (undo_len(&s), stack(&s));
|
||||
assert!(s.execute("shapeBuilder.copyRegion", &json!({"point": [50, 50]})).is_err());
|
||||
assert_eq!((undo_len(&s), stack(&s)), (undo, kids));
|
||||
}
|
||||
|
||||
/// In a clipping group inside another, clipped art shows only where both masks show it.
|
||||
#[test]
|
||||
fn fill_area_takes_art_in_nested_clipping_groups_only_where_every_mask_shows_it() {
|
||||
@@ -305,6 +328,39 @@ fn fill_area_stays_inside_the_isolated_group() {
|
||||
assert_eq!(s.doc().unwrap().doc.parent_of(new), s.doc().unwrap().doc.default_layer());
|
||||
}
|
||||
|
||||
/// In isolation mode, `ids` naming the isolated group, or the layer or a group that holds it, takes
|
||||
/// every path of the isolated group. The other paths of that layer or group, and other listed
|
||||
/// objects outside the isolated group, do not bound areas.
|
||||
#[test]
|
||||
fn fill_area_ids_naming_what_holds_the_isolated_group_take_its_paths() {
|
||||
let mut s = session();
|
||||
let outside =
|
||||
[line(&mut s, (200.0, 0.0), (300.0, 100.0)), line(&mut s, (300.0, 0.0), (200.0, 100.0)), line(&mut s, (190.0, 80.0), (310.0, 80.0))];
|
||||
let lines = triangle(&mut s);
|
||||
let ids: Vec<u64> = lines.iter().map(|l| l.0).collect();
|
||||
s.execute("select.set", &json!({"ids": ids})).unwrap();
|
||||
let g = id_of(&s.execute("object.group", &json!({})).unwrap());
|
||||
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
|
||||
let outer = id_of(&s.execute("object.group", &json!({})).unwrap());
|
||||
s.execute("object.isolate", &json!({"id": g.0})).unwrap();
|
||||
deselect(&mut s);
|
||||
let layer = s.doc().unwrap().doc.default_layer().unwrap();
|
||||
let (undo, kids) = (undo_len(&s), stack(&s));
|
||||
for named in [vec![layer.0], vec![outer.0], vec![g.0], vec![layer.0, outside[0].0]] {
|
||||
let r = s.execute("shapeBuilder.copyRegion", &json!({"point": [250, 70], "ids": named}));
|
||||
assert!(r.is_err(), "the area outside the isolated group, ids {named:?}");
|
||||
assert_eq!((undo_len(&s), stack(&s)), (undo, kids.clone()));
|
||||
let r = s.execute("shapeBuilder.copyRegion", &json!({"point": [50, 70], "ids": named})).unwrap();
|
||||
let new = id_of(&r);
|
||||
assert_eq!(s.doc().unwrap().doc.parent_of(new), Some(g), "ids {named:?}");
|
||||
assert!((area(&s, new) - 900.0).abs() < 1.0, "{}", area(&s, new));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
}
|
||||
let named: Vec<u64> = outside.iter().map(|l| l.0).collect();
|
||||
assert!(s.execute("shapeBuilder.copyRegion", &json!({"point": [50, 70], "ids": named})).is_err(), "only art outside it listed");
|
||||
assert_eq!((undo_len(&s), stack(&s)), (undo, kids));
|
||||
}
|
||||
|
||||
/// While a compound path is isolated, Fill Area finds no area, for the command and the tool, and
|
||||
/// changes nothing: the compound path cannot hold the new path. The same goes for an isolated
|
||||
/// Objects on Path group, a group with a Transform effect and a group that is a clipping mask.
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//! closed path of that area with the current fill and stroke (`shapeBuilder.copyRegion`). The
|
||||
//! paths around the area stay as they are, and nothing needs to be selected.
|
||||
|
||||
use vectorcraft_geom::kurbo::ParamCurveDeriv;
|
||||
use vectorcraft_geom::path::segment_bounds;
|
||||
use vectorcraft_geom::{ParamCurve, PathSeg};
|
||||
|
||||
@@ -171,13 +172,39 @@ fn unmasked(doc: &Document, leaf: &Node, mut shape: po::Shape, masks: &mut Masks
|
||||
Some(shape)
|
||||
}
|
||||
|
||||
/// How far (pt) the middle of a piece of an outline may lie from a mask's outline and still count
|
||||
/// as on the mask's edge, which [`clip_to`] keeps. It covers rounding only, so a piece just outside
|
||||
/// the mask is still left out.
|
||||
const ON_MASK: f64 = 1e-6;
|
||||
|
||||
/// How far (pt) to each side of a piece [`shows_through`] looks for the mask's filled area: past
|
||||
/// [`ON_MASK`], and within a filled strip of the mask as narrow as this.
|
||||
const MASK_SIDE: f64 = 1e-4;
|
||||
|
||||
/// Does the piece of an outline whose middle is `p`, running in direction `dir` there, show
|
||||
/// through `mask` filled with `rule`? A piece off the mask's outline shows when the filled area
|
||||
/// holds `p`. A piece on the outline ([`ON_MASK`]) shows when the filled area lies on one side of
|
||||
/// it ([`MASK_SIDE`]), so every side of a square clipped by the same square shows, and an outline
|
||||
/// of the mask that bounds no filled area (two copies of one outline under even-odd) hides it.
|
||||
fn shows_through(mask: &PathData, rule: FillRule, p: Point, dir: vectorcraft_geom::Vec2) -> bool {
|
||||
if !mask.nearest(p).is_some_and(|(.., d)| d <= ON_MASK) {
|
||||
return path_contains(mask, rule, p);
|
||||
}
|
||||
let len = dir.hypot();
|
||||
if !(len > 0.0 && len.is_finite()) {
|
||||
return false;
|
||||
}
|
||||
let side = dir.turn_90() * (MASK_SIDE / len);
|
||||
path_contains(mask, rule, p + side) || path_contains(mask, rule, p - side)
|
||||
}
|
||||
|
||||
/// `shape` cut to `mask` (filled with `rule`). With `filled` (the path's fill draws), its closed
|
||||
/// subpaths are cut to their overlap with the mask, whose edge then bounds the fill. Its other
|
||||
/// subpaths, and every subpath of a path without a fill, are cut to their pieces inside the mask,
|
||||
/// so a hidden part of an outline does not close an area. A subpath with more segments or
|
||||
/// crossings than [`po::addons::pathtools::crossings_on`] takes is left out. None when nothing of
|
||||
/// it is inside.
|
||||
fn clip_to(shape: po::Shape, filled: bool, mask: &PathData, rule: FillRule) -> Option<po::Shape> {
|
||||
/// subpaths, and every subpath of a path without a fill, are cut to their pieces inside the mask or
|
||||
/// on its edge ([`shows_through`]), so a hidden part of an outline does not close an area. A
|
||||
/// subpath with more segments or crossings than [`po::addons::pathtools::crossings_on`] takes is
|
||||
/// left out. None when nothing of it is inside.
|
||||
pub(super) fn clip_to(shape: po::Shape, filled: bool, mask: &PathData, rule: FillRule) -> Option<po::Shape> {
|
||||
let (b, mb) = (shape.path.bounds()?, mask.bounds()?);
|
||||
if !meet(&b, &mb) {
|
||||
return None;
|
||||
@@ -195,7 +222,17 @@ fn clip_to(shape: po::Shape, filled: bool, mask: &PathData, rule: FillRule) -> O
|
||||
let (cut, at) = po::addons::pathtools::insert_at(sp, &places);
|
||||
subs.extend(cut.cut_at(&at).into_iter().filter(|piece| {
|
||||
let n = piece.segment_count();
|
||||
n > 0 && path_contains(mask, rule, piece.segment(n / 2).eval(0.5))
|
||||
n > 0 && {
|
||||
let seg = piece.segment(n / 2);
|
||||
// Where the tangent vanishes (a cusp at the middle), the curve runs along its second
|
||||
// derivative there, and a straight segment with that degenerate form along its chord.
|
||||
let d1 = seg.deriv();
|
||||
let dir = [d1.eval(0.5).to_vec2(), d1.deriv().eval(0.5).to_vec2(), seg.p3 - seg.p0]
|
||||
.into_iter()
|
||||
.find(|v| v.hypot2() > 0.0)
|
||||
.unwrap_or_default();
|
||||
shows_through(mask, rule, seg.eval(0.5), dir)
|
||||
}
|
||||
}));
|
||||
}
|
||||
if subs.is_empty() {
|
||||
|
||||
@@ -3796,6 +3796,32 @@ fn fill_area_press_does_not_search_a_hover_position_it_passed() {
|
||||
assert_eq!(super::fill_area::hover_searches_started() - started, 1, "only the search that was running");
|
||||
}
|
||||
|
||||
/// An outline without a fill, cut to a clipping mask, keeps the pieces on the mask's edge: a square
|
||||
/// clipped by the same square keeps all four sides. A square just outside the mask, and a square on
|
||||
/// an outline of the mask that bounds no filled area (two copies of the square under even-odd),
|
||||
/// keep nothing.
|
||||
#[test]
|
||||
fn fill_area_clip_keeps_outline_pieces_on_the_mask_edge() {
|
||||
let unit = Rect::new(0.0, 0.0, 100.0, 100.0);
|
||||
let clip =
|
||||
|shape: PathData, mask: &PathData, rule: FillRule| super::fill_area::clip_to(po::Shape::new(shape, FillRule::NonZero, 1), false, mask, rule);
|
||||
let kept = clip(shapes::rectangle(unit), &shapes::rectangle(unit), FillRule::NonZero).expect("the sides on the mask's edge");
|
||||
assert_eq!(kept.path.subpaths.iter().map(|sp| sp.segment_count()).sum::<usize>(), 4, "{:?}", kept.path);
|
||||
let outside = shapes::rectangle(Rect::new(-0.005, -0.005, 100.005, 100.005));
|
||||
assert!(clip(outside, &shapes::rectangle(unit), FillRule::NonZero).is_none());
|
||||
let mut twice = shapes::rectangle(unit);
|
||||
let mut back = shapes::rectangle(unit).subpaths;
|
||||
back[0].anchors.reverse();
|
||||
twice.subpaths.extend(back);
|
||||
assert!(clip(shapes::rectangle(unit), &twice, FillRule::EvenOdd).is_none());
|
||||
// A top side drawn as a cubic whose tangent vanishes at its middle shows as well.
|
||||
let a = |p: Point, h_in: Point, h_out: Point| vectorcraft_geom::Anchor { p, h_in, h_out, kind: vectorcraft_geom::AnchorKind::Corner };
|
||||
let (o, x, xy, y) = (Point::new(0.0, 0.0), Point::new(100.0, 0.0), Point::new(100.0, 100.0), Point::new(0.0, 100.0));
|
||||
let cusp = SubPath::new(vec![a(o, o, x), a(x, o, x), a(xy, xy, xy), a(y, y, y)], true);
|
||||
let kept = clip(PathData::single(cusp), &shapes::rectangle(unit), FillRule::NonZero).expect("the sides on the mask's edge");
|
||||
assert_eq!(kept.path.subpaths.iter().map(|sp| sp.segment_count()).sum::<usize>(), 4, "{:?}", kept.path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_builds_the_three_tools() {
|
||||
for id in ["shapeBuilder", "livePaintBucket", "livePaintSelection"] {
|
||||
|
||||
+4
-3
@@ -14353,9 +14353,10 @@ The new path takes the current fill, stroke and stroke weight (`fill` and `strok
|
||||
above the front-most object along the area (one whose outline touches the area's outline, or that covers it) and is
|
||||
selected. When a group, Live Paint group, Shaper group or clipping group holds that object, the new path goes above the
|
||||
outermost group in its layer, not inside it, and the group keeps its members. In isolation mode only the paths in
|
||||
the isolated layer or group bound areas, for the tool's hover and click and for the command (with `ids`, only the
|
||||
paths of the listed objects that are in it), and the new path goes inside the isolated layer or group, directly above
|
||||
the object in it that holds that path. No area is found while the isolated object is not a layer or group (a path
|
||||
the isolated layer or group bound areas, for the tool's hover and click and for the command. With `ids`, the paths
|
||||
of the listed objects that are in it bound areas, and all of its paths do when `ids` lists the isolated layer or group
|
||||
or a layer or group that holds it; the other listed objects are ignored. The new path goes inside the isolated layer
|
||||
or group, directly above the object in it that holds that path. No area is found while the isolated object is not a layer or group (a path
|
||||
or compound path), is a Live Paint group, compound shape, Shaper group, graph, Image Trace result, dimension, flare or
|
||||
Objects on Path group, is a layer or group with a live effect or a clipping mask, or lies inside one of these or
|
||||
inside an envelope. A point in no enclosed area is the error "Fill Area: no enclosed area under the pointer" and records no undo
|
||||
|
||||
Reference in new issue
Block a user