Paths: a lone anchor widens the visual box only where a stroke draws it

The previous commit made PathData::bounds hold the point of a subpath of
one anchor. Node::visual_bounds measures a path from that box, so a lone
anchor that draws nothing (an open one, or a closed one under a butt cap
or with no stroke) widened the visual box by the stroke's reach. The
renderer draws a lone anchor only as the dot of a round or projecting
cap on a closed subpath (point_dots in crates/effects/src/stroke.rs).

- PathData::drawn_bounds measures the segments, and the lone anchors of
  closed subpaths when a cap draws dots. Node::visual_bounds measures
  paths and compound paths from it, falling back to the geometric box
  when nothing is drawn. Geometric bounds still hold every lone anchor.
- The .ai envelope import's box of what type's outlines paint relied on
  PathData::bounds leaving lone anchors out. It now removes contours of
  zero length from the baked pieces before measuring them, so
  a_contour_of_zero_length_paints_nothing passes unchanged.
This commit is contained in:
dotCooCoo committed 2026-11-27 15:33:20 -08:00
1 parent 7ee80289fc
commit 9d54c7ccec
4 files changed
+103 -6

No files matched your search

+14 -1
View File
@@ -431,6 +431,17 @@ impl Node {
}
}
/// The box of what a path, or the paths of a compound path, draw before the strokes' reach
/// ([`PathData::drawn_bounds`]): lone anchors of closed subpaths count only with `dots`. Other
/// members of a compound path count by their geometric box. `None` when nothing is drawn.
fn drawn(&self, symbols: Option<&SymbolBoxes>, dots: bool) -> Option<Rect> {
match &self.kind {
NodeKind::Path { path, .. } => path.drawn_bounds(dots),
NodeKind::Compound { .. } => self.editing_children().iter().fold(None, |acc, c| vectorcraft_geom::union_opt(acc, c.drawn(symbols, dots))),
_ => self.geometric(symbols),
}
}
/// Visual bounds: what the object paints, its strokes included (paths and compound paths
/// measure theirs along their own shape, see [`Appearance::stroked_bounds`]). An image is
/// measured by its frame. A symbol instance is measured as in [`Node::geometric_bounds`],
@@ -485,7 +496,9 @@ impl Node {
self.geometric(symbols).map(|b| b.inflate(o, o))
}
NodeKind::Path { .. } | NodeKind::Compound { .. } => {
let b = self.geometric(symbols)?;
// A lone anchor is drawn only as the dot of a round or projecting cap on a closed subpath.
let dots = self.appearance.painted_strokes().any(|s| matches!(s.cap, crate::LineCap::Round | crate::LineCap::Square));
let b = self.drawn(symbols, dots).or_else(|| self.geometric(symbols))?;
Some(self.appearance.stroked_bounds(b, || self.stroke_path().unwrap_or_default()))
}
NodeKind::SymbolInstance { symbol, xf } => {
+29
View File
@@ -469,6 +469,35 @@ fn visual_bounds_include_stroke() {
assert_eq!(p.visual_bounds(), Some(Rect::new(-0.5, -0.5, 10.5, 10.5)));
}
/// A lone anchor 20 points right of a stroked square widens the path's geometric box, and its
/// visual box only where the stroke draws it: as the dot of a round or projecting cap on a closed
/// subpath. An open lone anchor, or a closed one under a butt cap, draws nothing.
#[test]
fn a_lone_anchor_widens_the_visual_box_only_where_its_cap_draws_a_dot() {
use crate::appearance::LineCap;
use vectorcraft_geom::{Point, SubPath};
let square = shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0));
let node = |closed: bool, cap: LineCap| {
let mut subpaths = square.subpaths.clone();
subpaths.push(SubPath::polyline(&[Point::new(30.0, 5.0)], closed));
let mut p = Node::path(NodeId(2), PathData::new(subpaths), Appearance::default_art());
let s = p.appearance.stroke_mut().unwrap();
(s.cap, s.join) = (cap, crate::appearance::LineJoin::Round);
p
};
for closed in [false, true] {
for cap in [LineCap::Butt, LineCap::Round, LineCap::Square] {
let p = node(closed, cap);
let geometric = p.geometric_bounds().unwrap();
let visual = p.visual_bounds().unwrap();
assert!(geometric.x1 >= 30.0, "{closed} {cap:?}: the geometric box {geometric:?} leaves out the lone anchor");
let dot = closed && cap != LineCap::Butt;
assert_eq!(visual.x1 > 30.0, dot, "{closed} {cap:?}: the visual box {visual:?}");
assert!(dot || visual.x1 < 11.0, "{closed} {cap:?}: the visual box {visual:?} reaches toward the lone anchor");
}
}
}
/// A group's box from its members' boxes grows by the group's own strokes, leaves out members
/// without a box, and is `None` when none has one.
#[test]
+41 -4
View File
@@ -220,16 +220,53 @@ const PAINTED_SLACK: f64 = 1e-4;
/// applied, so a stroke a Transform effect moves counts where it is moved to), measured by its
/// visual bounds. Those are exact for miter joins (a tip counts where the miter limit keeps it) and
/// round joins, and an upper bound for bevel joins, miters the limit cuts, width profiles and
/// square-capped dashes; a contour of zero length, which paints nothing, doesn't count. Effects on
/// the type object itself are left out: VectorCraft applies them after the envelope bends the type.
/// `None` when nothing in it has bounds.
/// square-capped dashes; a contour of zero length, which paints nothing in the files' drawn art,
/// doesn't count, even where VectorCraft draws it as the dot of a round or projecting cap. Effects
/// on the type object itself are left out: VectorCraft applies them after the envelope bends the
/// type. `None` when nothing in it has bounds.
fn painted_outlines(outlines: &Node) -> Option<Rect> {
outlines.children().into_iter().flatten().fold(None, |acc, piece| {
let baked = vectorcraft_effects::bake_type_appearance(None, piece);
vectorcraft_geom::union_opt(acc, baked.as_ref().unwrap_or(piece).visual_bounds())
let baked = baked.as_ref().unwrap_or(piece);
let painted = without_zero_length_contours(baked);
vectorcraft_geom::union_opt(acc, painted.as_ref().unwrap_or(baked).visual_bounds())
})
}
/// `n` with its paths, and the paths in its groups and compound paths, without their subpaths of
/// one anchor (contours of zero length, which have no segment); a path with nothing else keeps
/// them. `None` when nothing changes.
fn without_zero_length_contours(n: &Node) -> Option<Node> {
if let Some(path) = n.path_data() {
let lone = |s: &vectorcraft_geom::SubPath| s.segment_count() == 0;
if !path.subpaths.iter().any(lone) || path.subpaths.iter().all(lone) {
return None;
}
let mut kept = n.clone();
if let Some(p) = kept.path_data_mut() {
p.subpaths.retain(|s| !lone(s));
}
return Some(kept);
}
if !matches!(n.kind, NodeKind::Group { .. } | NodeKind::Layer { .. } | NodeKind::Compound { .. }) {
return None;
}
let members = n.children()?;
let changed: Vec<Option<Node>> = members.iter().map(|c| without_zero_length_contours(c)).collect();
if changed.iter().all(Option::is_none) {
return None;
}
let mut kept = n.clone();
if let Some(children) = kept.children_mut() {
for (c, new) in children.iter_mut().zip(changed) {
if let Some(new) = new {
*c = Arc::new(new);
}
}
}
Some(kept)
}
/// The box of `content` an envelope of type with Distort Appearance may map from in a file besides
/// [`half_stroked_box`]: what it paints, type by its outlines ([`painted_outlines`]), groups by
/// their members grown by as far as their own strokes paint along a side, other objects by their
+19 -1
View File
@@ -685,9 +685,21 @@ impl PathData {
{
return Some(Rect::from_points(a.p, a.p));
}
Some(self.measured(|_| true).unwrap_or_default())
}
/// The box of what a stroke along the path draws, before the stroke's own reach: its segments,
/// as in [`PathData::bounds`], and the lone anchors a stroke draws. A lone anchor of an open
/// subpath draws nothing. A lone anchor of a closed subpath draws a dot when `dots` is set, as
/// a round or projecting cap does, and nothing otherwise. `None` when the path draws nothing.
pub fn drawn_bounds(&self, dots: bool) -> Option<Rect> {
self.measured(|sp| dots && sp.closed)
}
/// The box of the segments and of the lone anchors of the subpaths `lone` keeps.
fn measured(&self, lone: impl Fn(&SubPath) -> bool) -> Option<Rect> {
let mut bbox: Option<Rect> = None;
for sp in &self.subpaths {
if sp.segment_count() == 0
&& lone(sp)
&& let Some(a) = sp.anchors.first()
{
let lone = Rect::from_points(a.p, a.p);
@@ -710,7 +722,7 @@ impl PathData {
bbox = Some(bbox.map_or(seg, |bb| bb.union(seg)));
}
}
Some(bbox.unwrap_or_default())
bbox
}
/// Bounds including control handles (used for quick culling).
pub fn control_bounds(&self) -> Option<Rect> {
@@ -916,6 +928,12 @@ mod tests {
let both = beside.bounds().unwrap_or_default();
assert!(both.x1 >= 20.0 && both.y0 <= -3.0, "{both:?} leaves out the lone anchor at (20, -3)");
assert_eq!(both.union(segment_only), both, "{both:?} leaves out part of the segment's box {segment_only:?}");
// What a stroke draws leaves out the open lone anchor, and a closed one without dots.
assert_eq!(beside.drawn_bounds(true), Some(segment_only), "an open lone anchor draws nothing");
let dot = PathData::new(vec![beside.subpaths[0].clone(), SubPath::polyline(&[Point::new(20.0, -3.0)], true)]);
assert_eq!(dot.drawn_bounds(true), dot.bounds(), "a closed lone anchor draws a dot");
assert_eq!(dot.drawn_bounds(false), Some(segment_only), "without dots, a closed lone anchor draws nothing");
assert_eq!(lone.drawn_bounds(false), None, "lone anchors alone draw nothing");
// A NaN anchor differs from itself: kurbo closes such a subpath with a line of its own.
let nan = Point::new(f64::NAN, 1.0);
let closed = |pts: &[Point]| SubPath::polyline(pts, true);