mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
Merge pull request #10879 from dotCooCoo/fix/reverse-path-keeps-anchor-selection
Paths: Reverse Path Direction keeps the same anchors direct-selected
This commit is contained in:
3 files changed
+225
-52
No files matched your search
@@ -1089,45 +1089,82 @@ fn reverse(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
None => path.subpaths.iter().map(|sp| want.is_none_or(|ccw| (sp.signed_area() < 0.0) != ccw)).collect(),
|
||||
}
|
||||
};
|
||||
let changed = super::overprint::edit_counted(s, "Reverse Path Direction", |d| {
|
||||
// A compound path's own strokes run along its members' subpaths in order, so they turn
|
||||
// with them, read before any member turns.
|
||||
let compounds: BTreeSet<NodeId> = ids.iter().map(|id| compound_or_self(d, *id)).filter(|c| ids.binary_search(c).is_err()).collect();
|
||||
for c in compounds {
|
||||
let Some(NodeKind::Compound { children, .. }) = d.node(c).map(|n| &n.kind) else { continue };
|
||||
let flip: Vec<bool> = children
|
||||
.iter()
|
||||
.filter_map(|m| m.path_data().map(|pd| if ids.binary_search(&m.id).is_ok() { flip_of(pd) } else { vec![false; pd.subpaths.len()] }))
|
||||
.flatten()
|
||||
.collect();
|
||||
if flip.contains(&true)
|
||||
&& let Some(n) = d.node_mut(c)
|
||||
{
|
||||
vectorcraft_doc::blend::reverse_stroke_records(n, &flip);
|
||||
let mut d = (*s.doc()?.doc).clone();
|
||||
// A compound path's own strokes run along its members' subpaths in order, so they turn
|
||||
// with them, read before any member turns.
|
||||
let compounds: BTreeSet<NodeId> = ids.iter().map(|id| compound_or_self(&d, *id)).filter(|c| ids.binary_search(c).is_err()).collect();
|
||||
for c in compounds {
|
||||
let Some(NodeKind::Compound { children, .. }) = d.node(c).map(|n| &n.kind) else { continue };
|
||||
let flip: Vec<bool> = children
|
||||
.iter()
|
||||
.filter_map(|m| m.path_data().map(|pd| if ids.binary_search(&m.id).is_ok() { flip_of(pd) } else { vec![false; pd.subpaths.len()] }))
|
||||
.flatten()
|
||||
.collect();
|
||||
if flip.contains(&true)
|
||||
&& let Some(n) = d.node_mut(c)
|
||||
{
|
||||
vectorcraft_doc::blend::reverse_stroke_records(n, &flip);
|
||||
}
|
||||
}
|
||||
let mut turned = Vec::new();
|
||||
for id in &ids {
|
||||
let path = path_mut(&mut d, *id)?;
|
||||
let flip = flip_of(path);
|
||||
if !flip.contains(&true) {
|
||||
continue;
|
||||
}
|
||||
// The stroke's width profile and pen recordings turn with the path, read before it turns.
|
||||
if let Some(n) = d.node_mut(*id) {
|
||||
vectorcraft_doc::blend::reverse_stroke_records(n, &flip);
|
||||
}
|
||||
let path = path_mut(&mut d, *id)?;
|
||||
for (sp, f) in path.subpaths.iter_mut().zip(&flip) {
|
||||
if *f {
|
||||
sp.reverse();
|
||||
}
|
||||
}
|
||||
let mut changed = 0;
|
||||
for id in &ids {
|
||||
let path = path_mut(d, *id)?;
|
||||
let flip = flip_of(path);
|
||||
if !flip.contains(&true) {
|
||||
continue;
|
||||
}
|
||||
// The stroke's width profile and pen recordings turn with the path, read before it turns.
|
||||
if let Some(n) = d.node_mut(*id) {
|
||||
vectorcraft_doc::blend::reverse_stroke_records(n, &flip);
|
||||
}
|
||||
let path = path_mut(d, *id)?;
|
||||
for (sp, f) in path.subpaths.iter_mut().zip(flip) {
|
||||
if f {
|
||||
sp.reverse();
|
||||
turned.push((*id, flip));
|
||||
}
|
||||
if !turned.is_empty() {
|
||||
s.edit("Reverse Path Direction", |doc, sel| {
|
||||
*doc = d;
|
||||
for (id, flip) in &turned {
|
||||
if let Some(path) = doc.node(*id).and_then(Node::path_data) {
|
||||
reverse_selected_anchors(sel, *id, path, flip);
|
||||
}
|
||||
}
|
||||
changed += 1;
|
||||
}
|
||||
Ok(changed)
|
||||
})?;
|
||||
Ok(json!({ "changed": changed }))
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
Ok(json!({ "changed": turned.len() }))
|
||||
}
|
||||
|
||||
/// Point the selected anchors, selected segments and key anchor of `sel` on path `id` at the same
|
||||
/// points after the subpaths of `path` marked in `flip` were reversed ([`SubPath::reverse`]). In a
|
||||
/// reversed subpath of `n` anchors, anchor `i` becomes anchor `n - 1 - i` and segment `j` becomes
|
||||
/// segment `n - 2 - j`; a closed subpath's closing segment (`n - 1`) stays its closing segment.
|
||||
fn reverse_selected_anchors(sel: &mut Selection, id: NodeId, path: &PathData, flip: &[bool]) {
|
||||
let reversed = |si: usize| path.subpaths.get(si).filter(|_| flip.get(si) == Some(&true)).map(|sp| (sp.anchors.len(), sp.closed));
|
||||
let anchor = |(si, ai): AnchorRef| match reversed(si) {
|
||||
Some((n, _)) if ai < n => (si, n - 1 - ai),
|
||||
_ => (si, ai),
|
||||
};
|
||||
let segment = |(si, j): SegmentRef| match reversed(si) {
|
||||
Some((n, true)) if j < n => (si, (2 * n - 2 - j) % n),
|
||||
Some((n, false)) if j < n.saturating_sub(1) => (si, n - 2 - j),
|
||||
_ => (si, j),
|
||||
};
|
||||
if let Some(set) = sel.anchors.get_mut(&id) {
|
||||
*set = set.iter().map(|r| anchor(*r)).collect();
|
||||
}
|
||||
if let Some(set) = sel.segments.get_mut(&id) {
|
||||
*set = set.iter().map(|r| segment(*r)).collect();
|
||||
}
|
||||
if let Some((key, r)) = sel.key_anchor
|
||||
&& key == id
|
||||
{
|
||||
sel.key_anchor = Some((id, anchor(r)));
|
||||
}
|
||||
}
|
||||
|
||||
/// First and last anchor points of a subpath.
|
||||
@@ -2409,8 +2446,9 @@ fn stretch_weights(anchors: &Anchors, closed: bool, roles: &[Stretch]) -> Vec<[f
|
||||
shares
|
||||
}
|
||||
|
||||
/// After an anchor is added at `(si, added)` of path `id`, number the direct-selected anchors and
|
||||
/// selected segments after it on that subpath one higher, so the selection keeps the same points.
|
||||
/// After an anchor is added at `(si, added)` of path `id`, number the direct-selected anchors,
|
||||
/// selected segments and key anchor after it on that subpath one higher, so the selection keeps
|
||||
/// the same points.
|
||||
/// A selected segment that the new anchor splits stays selected as its two halves, and the new
|
||||
/// anchor joins its ends in the anchors that move with it.
|
||||
fn renumber_after_insert(sel: &mut Selection, id: NodeId, si: usize, added: usize) {
|
||||
@@ -2419,6 +2457,14 @@ fn renumber_after_insert(sel: &mut Selection, id: NodeId, si: usize, added: usiz
|
||||
// renumbering saturates instead of overflowing.
|
||||
*set = set.iter().map(|&(s, a)| if s == si && a >= added { (s, a.saturating_add(1)) } else { (s, a) }).collect();
|
||||
}
|
||||
// The key anchor moves along with the anchors after the new one.
|
||||
if let Some((key, (s, a))) = sel.key_anchor
|
||||
&& key == id
|
||||
&& s == si
|
||||
&& a >= added
|
||||
{
|
||||
sel.key_anchor = Some((id, (s, a.saturating_add(1))));
|
||||
}
|
||||
let split = added.checked_sub(1);
|
||||
if sel.segments.get(&id).is_some_and(|set| split.is_some_and(|g| set.contains(&(si, g)))) {
|
||||
sel.anchors.entry(id).or_default().insert((si, added));
|
||||
|
||||
@@ -180,6 +180,106 @@ fn reverse_path_direction_on_and_off_set_the_winding() {
|
||||
assert!(s.execute("path.reverse", &json!({})).is_err(), "nothing to reverse");
|
||||
}
|
||||
|
||||
/// Points of what is direct-selected on one path, rounded to whole points.
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct Picked {
|
||||
/// The selected anchors' points, sorted.
|
||||
anchors: Vec<(i64, i64)>,
|
||||
/// The end points of the selected segments, each pair and the list sorted.
|
||||
segments: Vec<[(i64, i64); 2]>,
|
||||
/// The key anchor's point, when the key anchor is on the path.
|
||||
key: Option<(i64, i64)>,
|
||||
}
|
||||
|
||||
/// What is direct-selected on path `id`, as points.
|
||||
fn picked_points(s: &Session, id: NodeId) -> Picked {
|
||||
let st = s.doc().unwrap();
|
||||
let sp = &st.doc.node(id).unwrap().path_data().unwrap().subpaths;
|
||||
let at = |(si, ai): (usize, usize)| {
|
||||
let p = sp[si].anchors[ai].p;
|
||||
(p.x.round() as i64, p.y.round() as i64)
|
||||
};
|
||||
let mut anchors: Vec<_> = st.selection.anchors.get(&id).into_iter().flatten().map(|r| at(*r)).collect();
|
||||
anchors.sort_unstable();
|
||||
let mut segments: Vec<_> = st
|
||||
.selection
|
||||
.segments
|
||||
.get(&id)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(|&(si, j)| {
|
||||
let mut ends = [at((si, j)), at((si, (j + 1) % sp[si].anchors.len()))];
|
||||
ends.sort_unstable();
|
||||
ends
|
||||
})
|
||||
.collect();
|
||||
segments.sort_unstable();
|
||||
let key = st.selection.key_anchor.filter(|(k, _)| *k == id).map(|(_, r)| at(r));
|
||||
Picked { anchors, segments, key }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reverse_path_direction_keeps_the_same_anchors_and_segments_direct_selected() {
|
||||
let mut s = session();
|
||||
let path = |s: &mut Session, anchors: Value, closed: bool| {
|
||||
NodeId(run(s, "path.create", json!({"anchors": anchors, "closed": closed}))["id"].as_u64().unwrap())
|
||||
};
|
||||
let open = path(&mut s, json!([{"x": 0, "y": 50}, {"x": 40, "y": 50}, {"x": 100, "y": 50}]), false);
|
||||
let closed = path(&mut s, json!([{"x": 10, "y": 10}, {"x": 90, "y": 10}, {"x": 90, "y": 90}, {"x": 10, "y": 90}]), true);
|
||||
run(&mut s, "select.anchorsMany", json!({"items": [{"id": open.0, "anchors": [[0, 0]]}, {"id": closed.0, "anchors": [[0, 1], [0, 2]]}]}));
|
||||
run(&mut s, "select.key", json!({"id": closed.0, "anchor": [0, 1]}));
|
||||
let before = [picked_points(&s, open), picked_points(&s, closed)];
|
||||
assert_eq!(before[0].anchors, [(0, 50)]);
|
||||
assert_eq!((before[1].anchors.clone(), before[1].key), (vec![(90, 10), (90, 90)], Some((90, 10))));
|
||||
assert_eq!(run(&mut s, "path.reverse", json!({}))["changed"], 2);
|
||||
let first = |s: &Session, id: NodeId| s.doc().unwrap().doc.node(id).unwrap().path_data().unwrap().subpaths[0].anchors[0].p;
|
||||
assert_eq!((first(&s, open).x, first(&s, closed).y), (100.0, 90.0), "both paths turned");
|
||||
assert_eq!([picked_points(&s, open), picked_points(&s, closed)], before, "the same points stay direct-selected after Reverse Path Direction");
|
||||
// Undo turns the paths back with the selection they had.
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert_eq!(first(&s, open).x, 0.0);
|
||||
assert_eq!([picked_points(&s, open), picked_points(&s, closed)], before, "undo");
|
||||
|
||||
// Selected segments, the closing segment of a closed path included, stay the same segments.
|
||||
run(&mut s, "select.segments", json!({"id": closed.0, "segments": [[0, 0], [0, 3]], "mode": "set"}));
|
||||
run(&mut s, "select.segments", json!({"id": open.0, "segments": [[0, 0]], "mode": "add"}));
|
||||
let before = [picked_points(&s, open), picked_points(&s, closed)];
|
||||
assert_eq!(before[0].segments, [[(0, 50), (40, 50)]]);
|
||||
assert_eq!(before[1].segments, [[(10, 10), (10, 90)], [(10, 10), (90, 10)]]);
|
||||
assert_eq!(run(&mut s, "path.reverse", json!({}))["changed"], 2);
|
||||
assert_eq!([picked_points(&s, open), picked_points(&s, closed)], before, "the same segments stay selected after Reverse Path Direction");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reverse_path_direction_of_one_subpath_leaves_the_others_selection_as_it_is() {
|
||||
let mut s = session();
|
||||
let id = NodeId(run(&mut s, "path.create", json!({"d": "M0 0 L40 0 L100 0 M0 200 L40 200 L100 200"}))["id"].as_u64().unwrap());
|
||||
run(&mut s, "select.anchorsMany", json!({"items": [{"id": id.0, "anchors": [[0, 0], [1, 0]]}]}));
|
||||
run(&mut s, "select.key", json!({"id": id.0, "anchor": [0, 0]}));
|
||||
let before = picked_points(&s, id);
|
||||
assert_eq!((before.anchors.clone(), before.key), (vec![(0, 0), (0, 200)], Some((0, 0))));
|
||||
assert_eq!(run(&mut s, "path.reverse", json!({"subpath": 1}))["changed"], 1);
|
||||
let first = |s: &Session, si: usize| s.doc().unwrap().doc.node(id).unwrap().path_data().unwrap().subpaths[si].anchors[0].p;
|
||||
assert_eq!((first(&s, 0).x, first(&s, 1).x), (0.0, 100.0), "only the second subpath turned");
|
||||
assert_eq!(picked_points(&s, id), before, "the same points stay direct-selected on both subpaths");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_anchor_point_keeps_the_key_anchor_on_its_point() {
|
||||
let mut s = session();
|
||||
let anchors = json!([{"x": 10, "y": 10}, {"x": 90, "y": 10}, {"x": 90, "y": 90}, {"x": 10, "y": 90}]);
|
||||
let id = NodeId(run(&mut s, "path.create", json!({"anchors": anchors, "closed": true}))["id"].as_u64().unwrap());
|
||||
run(&mut s, "select.anchorsMany", json!({"items": [{"id": id.0, "anchors": [[0, 1], [0, 2]]}]}));
|
||||
run(&mut s, "select.key", json!({"id": id.0, "anchor": [0, 2]}));
|
||||
let before = picked_points(&s, id);
|
||||
assert_eq!((before.anchors.clone(), before.key), (vec![(90, 10), (90, 90)], Some((90, 90))));
|
||||
run(&mut s, "path.insertAnchor", json!({"id": id.0, "segment": 0}));
|
||||
assert_eq!(s.doc().unwrap().doc.node(id).unwrap().path_data().unwrap().subpaths[0].anchors.len(), 5);
|
||||
assert_eq!(picked_points(&s, id), before, "the key anchor and the selected anchors stay on their points after Add Anchor Point");
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert_eq!(picked_points(&s, id), before, "undo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reverse_path_direction_turns_the_width_profile_and_pen_records_with_the_path() {
|
||||
use vectorcraft_doc::{AngleProfile, PressureProfile, WidthProfile};
|
||||
|
||||
@@ -1616,11 +1616,13 @@ fn two_corner_radii(panel: &Panel, a: u64, b: u64) -> (Vec<f64>, Vec<f64>) {
|
||||
(corner_radii(&app, a), corner_radii(&app, b))
|
||||
}
|
||||
|
||||
/// The radius of each corner of live-cornered path `id`, in document lengths.
|
||||
/// The radius at each anchor of live-cornered path `id`, by anchor index counted across subpaths,
|
||||
/// in document lengths. An anchor that is not a corner, such as an open subpath's end, reads 0.
|
||||
fn corner_radii(app: &VectorcraftApp, id: u64) -> Vec<f64> {
|
||||
let n = app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().clone();
|
||||
let lc = vectorcraft_doc::LiveCorners::of(&n).unwrap();
|
||||
(0..lc.all().len()).map(|k| crate::panels::corner_length(&lc, k)).collect()
|
||||
let anchors = lc.base.subpaths.iter().map(|sp| sp.anchors.len()).sum();
|
||||
(0..anchors).map(|k| if lc.corner(k).is_some() { crate::panels::corner_length(&lc, k) } else { 0.0 }).collect()
|
||||
}
|
||||
|
||||
/// A corner radius typed and not yet entered is dropped when the selection moves to other corners
|
||||
@@ -1729,11 +1731,12 @@ fn rounding_a_direct_selected_corner_keeps_the_corner_fields_target() {
|
||||
assert_eq!(target(&app), before, "the same corner is the target");
|
||||
}
|
||||
|
||||
/// Reversing a path drops an unfinished radius when the picked index names a different corner:
|
||||
/// an expanded rectangle, a path whose first corner and last corner have sides of the same
|
||||
/// lengths and turn the same way, a crossing path whose corners share a position, a path whose
|
||||
/// reversed corner has sides in the same directions, a path with two corners at the same point
|
||||
/// whose sides lean the same ways, and a small subpath beside a very large one.
|
||||
/// Reversing a path drops an unfinished radius, both while the picked anchor follows its point to
|
||||
/// its new index and when the old index is picked again and names a different corner: an expanded
|
||||
/// rectangle, a path whose first corner and last corner have sides of the same lengths and turn
|
||||
/// the same way, a crossing path whose corners share a position, a path whose reversed corner has
|
||||
/// sides in the same directions, a path with two corners at the same point whose sides lean the
|
||||
/// same ways, and a small subpath beside a very large one.
|
||||
#[test]
|
||||
fn an_unfinished_corner_radius_is_dropped_when_the_path_is_reversed() {
|
||||
let mut app = new_app();
|
||||
@@ -1757,24 +1760,48 @@ fn an_unfinished_corner_radius_is_dropped_when_the_path_is_reversed() {
|
||||
("coincident corners", coincident, 1),
|
||||
("small subpath", small, 1),
|
||||
] {
|
||||
// Where the corner at `index` is and the directions its sides leave it in.
|
||||
let corner = |app: &VectorcraftApp| {
|
||||
// Where corner `k` is and the directions its sides leave it in.
|
||||
let corner = |app: &VectorcraftApp, k: usize| {
|
||||
let node = app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap();
|
||||
let c = *vectorcraft_doc::LiveCorners::of(node).unwrap().corner(index).unwrap();
|
||||
let c = *vectorcraft_doc::LiveCorners::of(node).unwrap().corner(k).unwrap();
|
||||
(c.at, c.u, c.v)
|
||||
};
|
||||
// The anchors of the first subpath, where `index` is.
|
||||
let first_subpath = |app: &VectorcraftApp| {
|
||||
app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().path_data().unwrap().subpaths[0].anchors.len()
|
||||
};
|
||||
|
||||
// The picked anchor follows its point to its new index, and the draft is dropped.
|
||||
panel.app().run("select.anchors", json!({"id": id, "anchors": [[0, index]], "mode": "set"})).unwrap();
|
||||
panel.frame();
|
||||
let before = corner(&panel.app());
|
||||
let before = corner(&panel.app(), index);
|
||||
let reversed = first_subpath(&panel.app()) - 1 - index;
|
||||
finish_after_change(&mut panel, case, "Corner Radius", "0 pt", |app| {
|
||||
app.run("path.reverse", json!({"ids": [id]})).unwrap();
|
||||
});
|
||||
let app = panel.app();
|
||||
assert_ne!(corner(&app), before, "{case}: index {index} names a different corner");
|
||||
let mut app = panel.app();
|
||||
let picked = picked_corner(&app);
|
||||
assert_eq!(picked, index, "{case}: the picked corner index stays the same");
|
||||
assert_eq!(picked, reversed, "{case}: the picked anchor follows its point");
|
||||
assert_eq!(corner(&app, picked), (before.0, before.2, before.1), "{case}: the same corner, its sides swapped");
|
||||
let radii = corner_radii(&app, id);
|
||||
assert_eq!(radii[picked], 1.0, "{case}: the corner now picked takes 1 pt");
|
||||
assert_eq!(radii[picked], 1.0, "{case}: the picked corner takes 1 pt");
|
||||
assert!(radii.iter().all(|&radius| radius != 4.0), "{case}: no corner takes the unfinished 3+1 draft");
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
drop(app);
|
||||
|
||||
// Picking `index` again after the reversal names a different corner and drops the draft.
|
||||
panel.app().run("select.anchors", json!({"id": id, "anchors": [[0, index]], "mode": "set"})).unwrap();
|
||||
panel.frame();
|
||||
let before = corner(&panel.app(), index);
|
||||
finish_after_change(&mut panel, case, "Corner Radius", "0 pt", |app| {
|
||||
app.run("path.reverse", json!({"ids": [id]})).unwrap();
|
||||
app.run("select.anchors", json!({"id": id, "anchors": [[0, index]], "mode": "set"})).unwrap();
|
||||
});
|
||||
let app = panel.app();
|
||||
assert_ne!(corner(&app, index), before, "{case}: index {index} names a different corner");
|
||||
assert_eq!(picked_corner(&app), index, "{case}: index {index} is picked");
|
||||
let radii = corner_radii(&app, id);
|
||||
assert_eq!(radii[index], 1.0, "{case}: the corner now picked takes 1 pt");
|
||||
assert!(radii.iter().all(|&radius| radius != 4.0), "{case}: no corner takes the unfinished 3+1 draft");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user