diff --git a/crates/doc/src/hit.rs b/crates/doc/src/hit.rs index fd907a26cc..eeedd15c74 100644 --- a/crates/doc/src/hit.rs +++ b/crates/doc/src/hit.rs @@ -74,11 +74,24 @@ pub struct HitOptions { /// its edge. Off for the tools that act on what is drawn (sampling, type, gradients…); /// [`hit_test_visible`] never does it. pub clip_outlines: bool, + /// The Type tool: area type is hit anywhere inside its frame, where a click places the caret. + /// Off, the empty part of the frame (away from its edge and its lines of type) takes no + /// clicks, so they reach the art behind (#7274). + pub area_type_interior: bool, } impl Default for HitOptions { fn default() -> Self { - Self { tol: 3.0, outline: false, path_only: false, type_path_only: false, skip_guides: false, clip_outlines: false, scope: None } + Self { + tol: 3.0, + outline: false, + path_only: false, + type_path_only: false, + skip_guides: false, + clip_outlines: false, + area_type_interior: false, + scope: None, + } } } @@ -897,7 +910,11 @@ fn hit_leaf(n: &Node, p: Point, opt: HitOptions, symbols: &SymbolBoxes) -> Optio }) } NodeKind::Text(_) | NodeKind::PlacedDocument(_) | NodeKind::SymbolInstance { .. } => { - n.geometric_bounds_with(symbols).filter(|b| b.inflate(opt.tol, opt.tol).contains(p)).map(|_| HitKind::Bounds) + n.geometric_bounds_with(symbols).filter(|b| b.inflate(opt.tol, opt.tol).contains(p))?; + match &n.kind { + NodeKind::Text(t) if !opt.area_type_interior => in_area_type_art(n, t, p, opt.tol).unwrap_or(true).then_some(HitKind::Bounds), + _ => Some(HitKind::Bounds), + } } // A blend or an envelope is hit within its bounds, and outside them where its evaluated // art paints: a blend's keys and steps on a spine can reach past the keys and the spine, @@ -946,6 +963,30 @@ fn on_type_path(n: &Node, t: &crate::TextObject, p: Point, tol: f64) -> bool { stroke_contains(&path.to_bezpath(), 0.0, tol, p) } +/// Is `p` on area type `t` (in `n`) where it is drawn: within `tol` of its frame's edge or inside +/// its laid-out lines ([`crate::TextObject::cached_bounds`], with the characters' strokes)? The +/// rest of the frame is empty and takes no clicks (#7274). `None` when that is not known: other +/// type, type in perspective, or area type not laid out yet, which are hit within their bounds. +fn in_area_type_art(n: &Node, t: &crate::TextObject, p: Point, tol: f64) -> Option { + use vectorcraft_geom::kurbo::Shape; + let crate::TextKind::Area { frame } = &t.kind else { return None }; + if n.projection().is_some() { + return None; + } + if stroke_contains(&frame.transformed(t.xf).to_bezpath(), 0.0, tol, p) { + return Some(true); + } + let lines = t.cached_bounds?; + // No lines (empty type): only the frame's edge. + if lines.width() <= 0.0 && lines.height() <= 0.0 { + return Some(false); + } + let mut lines = lines.to_path(0.1); + lines.apply_affine(t.xf); + let reach = tol + t.char_stroke_outset(); + Some(fill_contains(&lines, FillRule::NonZero, p) || stroke_contains(&lines, 0.0, reach, p)) +} + /// Hit anywhere in an evaluated subtree (groups recurse; leaves use [`hit_leaf`]). fn hit_any(n: &Node, p: Point, opt: HitOptions, symbols: &SymbolBoxes) -> Option { // Hidden art does not hit. diff --git a/crates/doc/src/hit/tests.rs b/crates/doc/src/hit/tests.rs index 3eac4202b6..d020bf0fb3 100644 --- a/crates/doc/src/hit/tests.rs +++ b/crates/doc/src/hit/tests.rs @@ -663,8 +663,16 @@ fn masked_square(layer: bool) -> (Document, NodeId, NodeId, NodeId) { } } -const SELECTING: HitOptions = - HitOptions { tol: 3.0, outline: false, path_only: false, type_path_only: false, skip_guides: false, clip_outlines: true, scope: None }; +const SELECTING: HitOptions = HitOptions { + tol: 3.0, + outline: false, + path_only: false, + type_path_only: false, + skip_guides: false, + clip_outlines: true, + area_type_interior: false, + scope: None, +}; /// For the selection tools a clip group's clipping path is hit on its outline, within the /// tolerance, before the art it clips and just outside it too; inside, the art is hit. @@ -958,6 +966,41 @@ fn type_path_only_picks_type_on_its_path() { assert_eq!(hit(Point::new(60.0, 339.5), path_only), Some((on_path, HitKind::Outline)), "type on a path: its path"); } +/// Area type is hit on its frame's edge and where its lines are laid out, not in the empty rest of +/// its frame: a click there reaches the art behind, in Preview and in Outline mode (#7274). The +/// Type tool still finds the type anywhere inside its frame. +#[test] +fn area_type_passes_clicks_through_its_empty_frame() { + use crate::{CharStyle, TextKind, TextObject}; + let mut d = Document::new(400.0, 400.0); + let l = d.layers[0].id; + // A filled square behind the type, its edge at x = 150 inside the frame. + let square = Node::path(d.alloc_id(), shapes::rectangle(Rect::new(150.0, 150.0, 350.0, 350.0)), Appearance::default_art()); + let square_id = square.id; + d.insert(Some(l), 9, square).unwrap(); + // Area type at (100, 100): a 200 × 200 frame with one 80 wide line at its top. + let mut area = TextObject::point(Point::new(100.0, 100.0), "Area", CharStyle { size: 20.0, ..CharStyle::default() }); + area.kind = TextKind::Area { frame: shapes::rectangle(Rect::new(0.0, 0.0, 200.0, 200.0)) }; + area.cached_bounds = Some(Rect::new(0.0, 0.0, 80.0, 24.0)); + area.cached_baselines = vec![(Point::new(0.0, 18.0), Point::new(80.0, 18.0))]; + let area_id = d.alloc_id(); + d.insert(Some(l), 9, Node::new(area_id, NodeKind::Text(Box::new(area)))).unwrap(); + let outline = HitOptions { outline: true, ..Default::default() }; + for (opt, mode) in [(HitOptions::default(), "preview"), (outline, "outline")] { + let hit = |p: Point| hit_test(&d, p, opt).map(|h| h.leaf); + assert_eq!(hit(Point::new(140.0, 110.0)), Some(area_id), "{mode}: on the line of type"); + assert_eq!(hit(Point::new(200.0, 100.5)), Some(area_id), "{mode}: on the frame's top edge"); + assert_eq!(hit(Point::new(299.0, 250.0)), Some(area_id), "{mode}: on the frame's right edge"); + assert_eq!(hit(Point::new(220.0, 130.0)), None, "{mode}: the empty frame, nothing behind"); + assert_eq!(hit(Point::new(120.0, 250.0)), None, "{mode}: the empty frame, nothing behind"); + assert_eq!(hit(Point::new(150.0, 250.0)), Some(square_id), "{mode}: the square's edge behind the empty frame"); + } + assert_eq!(hit_test(&d, Point::new(250.0, 250.0), HitOptions::default()).map(|h| h.leaf), Some(square_id), "the square's fill"); + // The Type tool: anywhere inside the frame. + let typing = HitOptions { area_type_interior: true, ..Default::default() }; + assert_eq!(hit_test(&d, Point::new(250.0, 250.0), typing).map(|h| h.leaf), Some(area_id), "type tool: inside the frame"); +} + /// Type Object Selection by Path Only with the layout's baselines: every line's for point type, /// area type's besides its frame, a vertical column's centre line, and type in perspective /// where it is drawn. diff --git a/crates/tools/src/context.rs b/crates/tools/src/context.rs index e6db6069c7..dc38a0f7de 100644 --- a/crates/tools/src/context.rs +++ b/crates/tools/src/context.rs @@ -427,6 +427,7 @@ impl ToolContext<'_> { scope: self.isolation, skip_guides: !self.guides, clip_outlines: false, + area_type_interior: false, } } diff --git a/crates/tools/src/text.rs b/crates/tools/src/text.rs index 763325f90e..0541adadeb 100644 --- a/crates/tools/src/text.rs +++ b/crates/tools/src/text.rs @@ -781,9 +781,11 @@ impl TypeTool { /// Start editing the type under `p` with the caret there → the actions (ending the previous /// edit, selecting the type); None when no type is under `p`. A click among the characters - /// edits them, whatever Type Object Selection by Path Only says. + /// edits them, whatever Type Object Selection by Path Only says, and so does a click anywhere + /// inside area type's frame. fn edit_at(&mut self, cx: &ToolContext, p: Point) -> Option> { - let h = hit_test(cx.doc, p, vectorcraft_doc::hit::HitOptions { type_path_only: false, ..cx.hit_options() })?; + let opt = vectorcraft_doc::hit::HitOptions { type_path_only: false, area_type_interior: true, ..cx.hit_options() }; + let h = hit_test(cx.doc, p, opt)?; let Some(NodeKind::Text(t)) = cx.doc.node(h.leaf).map(|n| &n.kind) else { return None }; let lay = self.layout(t); let byte = vectorcraft_text::hit_byte(&lay, t.xf.inverse() * p); diff --git a/crates/tools/src/text/tests.rs b/crates/tools/src/text/tests.rs index 94063166bc..1075018b52 100644 --- a/crates/tools/src/text/tests.rs +++ b/crates/tools/src/text/tests.rs @@ -1400,6 +1400,26 @@ fn creates(acts: &[Action]) -> bool { acts.iter().any(|a| matches!(a, Action::Exec(cmd, _) if cmd == "text.create" || cmd == "text.createInPath")) } +/// A click in the empty part of an area type frame, below its one line, edits that type: the +/// selection tools pass such clicks through to the art behind (#7274), the Type tool does not. +#[test] +fn a_click_in_an_area_frame_below_its_text_edits_it() { + let mut d = Document::new(500.0, 500.0); + let l = d.layers[0].id; + let id = d.alloc_id(); + let mut t = TextObject::point(Point::new(100.0, 100.0), "Hi", CharStyle { size: 20.0, ..Default::default() }); + t.kind = TextKind::Area { frame: vectorcraft_geom::PathData::from_bezpath(&Rect::new(0.0, 0.0, 200.0, 200.0).to_path(0.1)) }; + t.cached_bounds = Some(Rect::new(0.0, 0.0, 30.0, 24.0)); + d.insert(Some(l), 0, Node::new(id, NodeKind::Text(Box::new(t)))).unwrap(); + let (sel, p) = (Selection::default(), paint()); + let c = cx(&d, &sel, &p); + assert!(vectorcraft_doc::hit::hit_test(&d, Point::new(250.0, 250.0), c.selection_hit_options()).is_none(), "selection tools: passes through"); + let mut tool = TypeTool::new("type"); + let acts = click(&mut tool, &c, 250.0, 250.0); + assert!(!creates(&acts), "{acts:?}"); + assert_eq!(tool.editing, Some(id)); +} + /// While type is being edited, a click on blank canvas ends the editing and makes no new type; /// the Type tool stays, and the next click makes point type (#2871, #2854). The same holds when /// the editing began with a double-click from a selection tool.