From 09f2c299aedf0d500249e21012189a29b9d99fe0 Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 27 Oct 2026 02:31:46 +0100 Subject: [PATCH] Integration: keep vector-mask edits on the mask for type on a path (#9321) The Type Path branch in edit_path caught every `"name":"layer"` edit of a type-on-a-path layer, including edits of its targeted vector mask: Direct Selection showed the mask, but a drag moved the Type Path by the mask's knot indices and left the mask as it was. On main the same command edits the mask, and path.info / path.get / path.transform still read the mask. `"name":"layer"` now keeps that meaning: it edits the vector mask when the layer has one, and the Type Path only when it has none or with `"typePath": true`, which Direct Selection sends for the Type Path (PathRef::TypePath). path.list reports the Type Path under its own `typePath` key, so `layerPath` still names the vector mask, and an edited Type Path keeps the knot cap type.attachToPath enforces. Both points were found by CodeRabbit. The canvas also draws the Type Path with the other tools, as it does a shape's path: the Pen's Auto Add/Delete edits it, and before this a Pen click on the baseline added or deleted anchors the user couldn't see. Co-authored-by: DuBearte <130190089+DuBearte@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/path_edit_cmds.rs | 13 ++++--- crates/engine/src/type_extra_cmds.rs | 3 ++ crates/engine/src/type_extra_cmds/tests.rs | 41 +++++++++++++++++++++- crates/engine/src/vector_cmds.rs | 14 +++++--- crates/ui-egui/src/direct_select.rs | 17 +++++---- crates/ui-egui/src/direct_select_tests.rs | 31 +++++++++++++++- crates/ui-egui/src/vector_ui.rs | 17 +++++++++ 7 files changed, 119 insertions(+), 17 deletions(-) diff --git a/crates/engine/src/path_edit_cmds.rs b/crates/engine/src/path_edit_cmds.rs index 2c3b7d83b9..d06b0fb29c 100644 --- a/crates/engine/src/path_edit_cmds.rs +++ b/crates/engine/src/path_edit_cmds.rs @@ -6,7 +6,8 @@ //! //! Every command targets a path like `path.transform`: `"name":"work"` (default), a saved path's //! name, or `"layer"` with `"layer":id?` for a shape layer's path (re-rendered; it stops being a -//! live shape) or a layer's vector mask. Knots are addressed by `subpath` and `knot` index, as +//! live shape), a layer's vector mask, or type on a path's Type Path (`"typePath":true` when the +//! type layer also has a vector mask). Knots are addressed by `subpath` and `knot` index, as //! listed by `path.info`, and the result is `{name, path}` in the compact JSON form. use photocraft_doc::{LayerContent, Path}; @@ -49,9 +50,13 @@ fn knot(cmd: &str, p: &Value) -> Result<[usize; 2]> { fn edit_path(s: &mut Session, cmd: &str, p: &Value, label: &str, f: impl FnOnce(&mut Path) -> std::result::Result<(), String>) -> Result { let name = p.get("name").and_then(Value::as_str).unwrap_or("work").to_owned(); let layer = if name == "layer" { Some(layer_id(s, p)?) } else { None }; - // Type on a path: its own path, the text laid along it again (#9114). + // Type on a path: its own path, the text laid along it again (#9114). A vector mask on the + // layer stays what `"layer"` edits, as with `path.info`, unless `"typePath": true`. + let type_path = p.get("typePath").and_then(Value::as_bool).unwrap_or(false); let typed = layer.filter(|id| { - s.active().and_then(|d| d.doc.layer(*id)).is_some_and(|l| matches!(&l.content, LayerContent::Text(t) if crate::type_extra_cmds::type_path(t).is_some())) + s.active().and_then(|d| d.doc.layer(*id)).is_some_and(|l| { + matches!(&l.content, LayerContent::Text(t) if crate::type_extra_cmds::type_path(t).is_some()) && (type_path || l.vector_mask.is_none()) + }) }); if let Some(id) = typed { let path = crate::type_extra_cmds::with_text(s, &json!({"layer": id.0}), label, |t, _| { @@ -162,7 +167,7 @@ fn delete_anchor(s: &mut Session, p: &Value) -> Result { } pub fn specs() -> Vec { - const TARGET: &str = r#""name":str|"work"|"layer"="work","layer":id? (with "layer": a shape layer's path or a vector mask)"#; + const TARGET: &str = r#""name":str|"work"|"layer"="work","layer":id? (with "layer": a shape layer's path, a vector mask, or else type on a path's Type Path),"typePath":bool=false (with "layer": the Type Path even when the type layer has a vector mask)"#; let spec = |id: &'static str, label: &'static str, params: String, run: fn(&mut Session, &Value) -> Result| CommandSpec { id, label, diff --git a/crates/engine/src/type_extra_cmds.rs b/crates/engine/src/type_extra_cmds.rs index 59ed4799b5..d8c11a52d6 100644 --- a/crates/engine/src/type_extra_cmds.rs +++ b/crates/engine/src/type_extra_cmds.rs @@ -433,6 +433,9 @@ pub(crate) fn set_type_path(t: &mut TextLayer, edited: &Path) -> Result<()> { if sub.knots.len() < 2 { return Err(EngineError::Other("type on a path needs a path of two or more points".into())); } + if sub.knots.len() > MAX_TYPE_PATH_KNOTS { + return Err(EngineError::Other("the type path has too many anchor points".into())); + } let inv = t.transform.inverse().ok_or_else(|| EngineError::Other("the type layer's transform cannot be inverted".into()))?; let map = |q: Point| inv.apply(q); let knots: Vec = diff --git a/crates/engine/src/type_extra_cmds/tests.rs b/crates/engine/src/type_extra_cmds/tests.rs index ecb3e01f10..8ae504cfa1 100644 --- a/crates/engine/src/type_extra_cmds/tests.rs +++ b/crates/engine/src/type_extra_cmds/tests.rs @@ -332,7 +332,7 @@ fn type_follows_its_path_when_the_path_is_edited() { let ends: Vec<(f64, f64)> = path.subpaths[0].knots.iter().map(|k| (k.anchor.x, k.anchor.y)).collect(); assert!(ends.iter().zip([(150.0, 10.0), (150.0, 110.0)]).all(|(a, b)| (a.0 - b.0).abs() < 1e-6 && (a.1 - b.1).abs() < 1e-6), "{ends:?}"); let listed = s.execute("path.list", json!({})).unwrap(); - assert_eq!(listed["layerPath"]["kind"], "typePath", "{listed}"); + assert_eq!(listed["typePath"]["kind"], "typePath", "{listed}"); // Swing the path's end to the right: the type now runs down and to the right. let before = ink(&s, id); let r = s.execute("path.moveAnchors", json!({"name": "layer", "layer": id.0, "anchors": [[0, 1]], "move": [120, 0]})).unwrap(); @@ -349,6 +349,45 @@ fn type_follows_its_path_when_the_path_is_edited() { assert_eq!(ink(&s, id), before); } +/// #9114: a type-on-a-path layer's vector mask is still what `"name": "layer"` edits, as on main +/// and as `path.info` reads it; `"typePath": true` edits the Type Path; `path.list` reports both. +#[test] +fn a_vector_mask_on_type_on_a_path_is_edited_not_the_type_path() { + let mut s = session(8); + let id = text_layer(&mut s, "Hello path"); + s.edit("path", |doc, _| { + doc.work_path = Some(Path::new(vec![Subpath::polyline(&[(150.0, 10.0), (150.0, 110.0)])])); + Ok(()) + }) + .unwrap(); + s.execute("type.attachToPath", json!({})).unwrap(); + let rect = json!({"subpaths": [{"closed": true, "knots": [[10, 10], [200, 10], [200, 100], [10, 100]]}]}); + s.execute("layer.vectorMask.add", json!({"layer": id.0, "path": rect})).unwrap(); + let listed = s.execute("path.list", json!({})).unwrap(); + assert_eq!((listed["layerPath"]["kind"].as_str(), listed["typePath"]["kind"].as_str()), (Some("vectorMask"), Some("typePath")), "{listed}"); + let type_path = |s: &Session| crate::type_extra_cmds::type_path(text(s, id)).unwrap(); + let before = type_path(&s); + let mask = |s: &Session| s.active().unwrap().doc.layer(id).unwrap().vector_mask.as_ref().unwrap().path.subpaths[0].knots[0].anchor; + s.execute("path.moveAnchors", json!({"name": "layer", "layer": id.0, "anchors": [[0, 0]], "move": [10, 0]})).unwrap(); + assert_eq!(mask(&s), photocraft_geom::Point::new(20.0, 10.0), "the mask's corner moved"); + assert_eq!(type_path(&s), before, "the Type Path stays"); + s.execute("path.moveAnchors", json!({"name": "layer", "layer": id.0, "typePath": true, "anchors": [[0, 1]], "move": [20, 0]})).unwrap(); + assert!((type_path(&s).subpaths[0].knots[1].anchor.x - before.subpaths[0].knots[1].anchor.x - 20.0).abs() < 1e-6, "the Type Path moved"); + assert_eq!(mask(&s), photocraft_geom::Point::new(20.0, 10.0), "the mask stays"); +} + +/// An edit can't grow the Type Path past the cap `type.attachToPath` keeps. +#[test] +fn an_edited_type_path_keeps_the_knot_cap() { + let mut s = session(8); + let id = text_layer(&mut s, "Hi"); + let knots: Vec<(f64, f64)> = (0..=MAX_TYPE_PATH_KNOTS).map(|i| (i as f64 * 0.001, 50.0)).collect(); + let mut t = text(&s, id).clone(); + t.shape = TextShape::Path { path: Subpath::polyline(&knots[..2]), start: 0.0, flip: false }; + assert!(set_type_path(&mut t, &Path::new(vec![Subpath::polyline(&knots[..MAX_TYPE_PATH_KNOTS])])).is_ok()); + assert!(set_type_path(&mut t, &Path::new(vec![Subpath::polyline(&knots)])).is_err()); +} + /// The Type tool's click on a path: `"at"` starts the type where the path passes nearest the /// click, and the paragraph alignment is kept about that point, as in Photoshop. #[test] diff --git a/crates/engine/src/vector_cmds.rs b/crates/engine/src/vector_cmds.rs index d90fb3595e..4f54c9f788 100644 --- a/crates/engine/src/vector_cmds.rs +++ b/crates/engine/src/vector_cmds.rs @@ -851,11 +851,9 @@ fn paths_list(s: &Session) -> Result { let d = s.active().ok_or(EngineError::NoDocument)?; let doc = &d.doc; let knots = |p: &Path| p.subpaths.iter().map(|s| s.knots.len()).sum::(); - let layer = d.active_layer.and_then(|id| doc.layer(id)).and_then(|l| match &l.content { + let active = d.active_layer.and_then(|id| doc.layer(id)); + let layer = active.and_then(|l| match &l.content { LayerContent::Shape(sh) => Some(json!({ "kind": "shape", "layer": l.id.0, "name": format!("{} Shape Path", l.name), "knots": knots(&sh.path) })), - // Type on a path: Photoshop's Type Path, which Direct Selection edits (#9114). - LayerContent::Text(t) if crate::type_extra_cmds::type_path(t).is_some() => crate::type_extra_cmds::type_path(t) - .map(|p| json!({ "kind": "typePath", "layer": l.id.0, "name": format!("{} Type Path", l.name), "knots": knots(&p) })), _ => { l.vector_mask.as_ref().map(|v| json!({ "kind": "vectorMask", "layer": l.id.0, "name": format!("{} Vector Mask", l.name), "knots": knots(&v.path) })) } @@ -865,6 +863,12 @@ fn paths_list(s: &Session) -> Result { "workPath": doc.work_path.as_ref().map(|p| json!({ "knots": knots(p), "subpaths": p.subpaths.len() })), "clippingPath": doc.clipping_path.as_ref().map(|c| json!({ "name": c.name, "flatness": c.flatness })), "layerPath": layer, + // Type on a path: Photoshop's Type Path, which Direct Selection edits (#9114). + "typePath": active.and_then(|l| match &l.content { + LayerContent::Text(t) => crate::type_extra_cmds::type_path(t) + .map(|p| json!({ "kind": "typePath", "layer": l.id.0, "name": format!("{} Type Path", l.name), "knots": knots(&p) })), + _ => None, + }), })) } @@ -1800,7 +1804,7 @@ pub fn specs() -> Vec { label: "List Paths", menu: &[], shortcut: None, - params: r##"{} → {paths:[{name,knots,subpaths}],workPath,clippingPath,layerPath (active layer's shape path / vector mask)}"##, + params: r##"{} → {paths:[{name,knots,subpaths}],workPath,clippingPath,layerPath (active layer's shape path / vector mask),typePath (active type layer's Type Path, if it is on a path)}"##, enabled: has_doc, run: |s, _| paths_list(s), journal: false, diff --git a/crates/ui-egui/src/direct_select.rs b/crates/ui-egui/src/direct_select.rs index 305e954339..46aae92d05 100644 --- a/crates/ui-egui/src/direct_select.rs +++ b/crates/ui-egui/src/direct_select.rs @@ -1,7 +1,7 @@ //! Direct Selection tool (A, #790), the Convert Point tool (#3204), the Pens' ⌘/Ctrl (Direct //! Selection: the Pen, Freeform Pen and Curvature Pen) and the Pen's ⌥/Alt (Convert Point) -//! modifiers, as in Photoshop. It edits the paths the canvas shows: the targeted vector mask or -//! the active shape layer's path, and the work path. +//! modifiers, as in Photoshop. It edits the paths the canvas shows: the targeted vector mask, the +//! active shape layer's path or the active type layer's Type Path, and the work path. //! //! * Click an anchor to select it (⇧ adds or removes, ⌥ selects its whole subpath), drag on empty //! canvas to marquee-select anchors, click empty canvas to deselect. @@ -39,6 +39,8 @@ pub enum PathRef { Work, /// A layer's shape path, or its vector mask. Layer(u64), + /// A type layer's Type Path (#9114), apart from a vector mask the layer may have. + TypePath(u64), /// The Pen path being drawn (`ui.pen`): edited in place, not through the engine. Pen, } @@ -49,6 +51,7 @@ impl PathRef { match self { PathRef::Work => json!({"name": "work"}), PathRef::Layer(id) => json!({"name": "layer", "layer": id}), + PathRef::TypePath(id) => json!({"name": "layer", "layer": id, "typePath": true}), // Never sent: edits to the Pen path being drawn don't go through the engine. PathRef::Pen => json!({}), } @@ -167,10 +170,12 @@ pub fn shows(app: &PhotocraftApp, mods: Modifiers) -> bool { /// active type layer's Type Path (#9114), then the work path. The Pen's Auto Add/Delete edits these /// (`anchor_tools::pen_auto`). pub(crate) fn active_paths(app: &PhotocraftApp) -> Vec<(PathRef, Path)> { - let layer = - crate::vector_ui::targeted_vector_mask(app).or_else(|| crate::vector_ui::active_shape_path(app)).or_else(|| crate::vector_ui::active_type_path(app)); + let layer = crate::vector_ui::targeted_vector_mask(app) + .or_else(|| crate::vector_ui::active_shape_path(app)) + .map(|(id, p)| (PathRef::Layer(id), p)) + .or_else(|| crate::vector_ui::active_type_path(app).map(|(id, p)| (PathRef::TypePath(id), p))); let work = app.session.active().and_then(|st| st.doc.work_path.clone()); - layer.map(|(id, p)| (PathRef::Layer(id), p)).into_iter().chain(work.map(|p| (PathRef::Work, p))).collect() + layer.into_iter().chain(work.map(|p| (PathRef::Work, p))).collect() } /// The paths Direct Selection edits, front first: the Pen path being drawn, the active paths, then @@ -303,7 +308,7 @@ fn press(app: &mut PhotocraftApp, p: [f64; 2], mods: Modifiers, convert: bool) - app.ui.direct_selection.drag = Some(Drag::Marquee { start: p, end: p, add: mods.shift }); return true; }; - if let PathRef::Layer(id) = target + if let PathRef::Layer(id) | PathRef::TypePath(id) = target && app.session.active().and_then(|st| st.active_layer).map(|l| l.0) != Some(id) { let _ = app.run("layer.select", json!({"layer": id})); diff --git a/crates/ui-egui/src/direct_select_tests.rs b/crates/ui-egui/src/direct_select_tests.rs index 38064a8c07..27a9fc59d8 100644 --- a/crates/ui-egui/src/direct_select_tests.rs +++ b/crates/ui-egui/src/direct_select_tests.rs @@ -532,7 +532,7 @@ fn dragging_an_anchor_of_type_on_a_path_moves_the_type() { }; let n = steps(&app); drag(&mut app, [100.2, 180.2], [160.2, 180.2], Modifiers::NONE); - assert_eq!(app.ui.direct_selection.target, Some(PathRef::Layer(id)), "the type path, in front of the work path"); + assert_eq!(app.ui.direct_selection.target, Some(PathRef::TypePath(id)), "the type path, in front of the work path"); let end = type_path(&app).subpaths[0].knots[1].anchor; assert!((end.x - 160.0).abs() < 1e-6 && (end.y - 180.0).abs() < 1e-6, "{end:?}"); assert_eq!(work(&app).subpaths[0].knots[1].anchor, Point::new(100.0, 180.0), "the work path stays"); @@ -540,3 +540,32 @@ fn dragging_an_anchor_of_type_on_a_path_moves_the_type() { assert!(app.session.undo()); assert_eq!(type_path(&app).subpaths[0].knots[1].anchor.x.round(), 100.0); } + +/// #9114: with a type-on-a-path layer's vector mask targeted, Direct Selection edits the mask, not +/// the Type Path; untargeted, it edits the Type Path and the mask stays. +#[test] +fn a_targeted_vector_mask_on_type_on_a_path_is_edited_not_the_type_path() { + let mut app = app(Tool::DirectSelection); + app.run("path.set", json!({"name": "work", "path": {"subpaths": [{"closed": false, "knots": [[100, 20], [100, 180]]}]}})).unwrap(); + let id = app.run("type.create", json!({"x": 20, "y": 70, "text": "On a path", "size": 24})).unwrap()["layer"].as_u64().unwrap(); + app.run("type.attachToPath", json!({})).unwrap(); + let rect = json!({"subpaths": [{"closed": true, "knots": [[20, 20], [180, 20], [180, 160], [20, 160]]}]}); + app.run("layer.vectorMask.add", json!({"layer": id, "path": rect})).unwrap(); + app.ui.vector_mask_target = true; + let layer = |app: &PhotocraftApp| app.session.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().clone(); + let type_path = |app: &PhotocraftApp| match &layer(app).content { + photocraft_doc::LayerContent::Text(t) => photocraft_engine::type_extra_cmds::type_path(t).unwrap(), + _ => panic!("not a type layer"), + }; + let before = type_path(&app); + drag(&mut app, [20.2, 20.2], [40.2, 30.2], Modifiers::NONE); + assert_eq!(app.ui.direct_selection.target, Some(PathRef::Layer(id))); + assert_eq!(layer(&app).vector_mask.unwrap().path.subpaths[0].knots[0].anchor, Point::new(40.0, 30.0), "the mask's corner moved"); + assert_eq!(type_path(&app), before, "the Type Path stays"); + // With the mask no longer targeted, the Type Path is in front again and the mask stays. + app.ui.vector_mask_target = false; + drag(&mut app, [100.2, 180.2], [160.2, 180.2], Modifiers::NONE); + assert_eq!(app.ui.direct_selection.target, Some(PathRef::TypePath(id))); + assert!((type_path(&app).subpaths[0].knots[1].anchor.x - 160.0).abs() < 1e-6); + assert_eq!(layer(&app).vector_mask.unwrap().path.subpaths[0].knots[0].anchor, Point::new(40.0, 30.0)); +} diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rs index a0da93d511..79442e9448 100644 --- a/crates/ui-egui/src/vector_ui.rs +++ b/crates/ui-egui/src/vector_ui.rs @@ -1229,6 +1229,10 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform if let Some((_, p)) = targeted_vector_mask(app) { draw_path(&p, anchors); } + // So does type on a path's Type Path, which the Pen's Auto Add/Delete edits (#9114). + if let Some((_, p)) = active_type_path(app) { + draw_path(&p, anchors); + } crate::corner_radius::draw(app, painter, &to_scr, accent); } // Pen path in progress, with handles of the last knot and a rubber band to the pointer. While @@ -2282,6 +2286,19 @@ mod tests { } } + /// #9114: the active type layer's Type Path shows with the Pen, whose Auto Add/Delete edits it. + #[test] + fn the_type_path_of_type_on_a_path_is_drawn_with_the_pen() { + let ctx = egui::Context::default(); + let mut app = app(); + app.run("path.set", json!({"name": "work", "path": {"subpaths": [{"closed": false, "knots": [[100, 20], [100, 180]]}]}})).unwrap(); + app.run("type.create", json!({"x": 20, "y": 70, "text": "On a path", "size": 24})).unwrap(); + app.run("type.attachToPath", json!({})).unwrap(); + app.run("path.delete", json!({"name": "work"})).unwrap(); + app.ui.tool = Tool::Pen; + assert!(overlay_shapes(&app, &ctx) > 0, "the Type Path is drawn"); + } + #[test] fn show_target_path_and_extras_hide_the_path_outlines() { // #1119: View › Show › Target Path only flipped its checkmark; the canvas kept drawing the