diff --git a/crates/doc/src/node/ops.rs b/crates/doc/src/node/ops.rs index 0e15c90e94..ebb462ba35 100644 --- a/crates/doc/src/node/ops.rs +++ b/crates/doc/src/node/ops.rs @@ -463,14 +463,76 @@ impl Node { self.visual(Some(symbols)) } - /// The visual box of a group or layer without a clipping path, from the visual boxes of its - /// [`Node::editing_children`]: for a walk that measures the members as well. + /// A box round a group or layer without a clipping path, from boxes round its + /// [`Node::editing_children`], grown by the farthest its own strokes can reach from any path + /// ([`Appearance::outset`]): for boxes measured where the members' shapes are not at hand. + /// [`Node::visual_of_measured_members`] measures the strokes along the members. pub fn visual_of_members(&self, members: impl Iterator>) -> Option { // The container's own strokes paint around its members. let o = self.appearance.outset(); members.fold(None, vectorcraft_geom::union_opt).map(|b| b.inflate(o, o)) } + /// The visual box of a group or layer without a clipping path, from its + /// [`Node::editing_children`] paired with their visual boxes: for a walk that measures the + /// members as well. The container's own strokes are measured along the shape of each member + /// path and compound path, at any depth in its groups; they paint nothing round images, symbol + /// instances, guides and hidden members, and grow the box of any other member by the farthest + /// they can reach ([`Appearance::outset`]), measured with `symbols` as in + /// [`Node::visual_bounds_with`]. + pub fn visual_of_measured_members<'a>( + &self, + members: impl Iterator)>, + symbols: Option<&SymbolBoxes>, + ) -> Option { + let stroked = self.appearance.painted_strokes().next().is_some(); + members.fold(None, |acc, (c, b)| { + let own = if stroked { c.strokes_of_container(&self.appearance, Some(b), symbols) } else { None }; + vectorcraft_geom::union_opt(vectorcraft_geom::union_opt(acc, b), own) + }) + } + + /// The box the strokes of `appearance`, the appearance of a group or layer that holds this + /// object, paint along it, measured with `symbols` as in [`Node::visual_bounds_with`]: the box + /// [`Node::visual_of_measured_members`] takes in for this member besides its own visual box. + pub fn painted_by_container(&self, appearance: &Appearance, symbols: Option<&SymbolBoxes>) -> Option { + appearance.painted_strokes().next()?; + self.strokes_of_container(appearance, None, symbols) + } + + /// The box the strokes of `appearance`, a container's, paint along this member, whose visual + /// box is `measured` when known: along the shape of a path or compound path, as it measures its + /// own strokes ([`Appearance::stroked_bounds`]); along the members of a group or layer without + /// a clipping path that is not a compound shape; nothing for a hidden member, a guide, an image + /// or a symbol instance, which the strokes do not paint; and the member's visual box grown by + /// the farthest the strokes can reach ([`Appearance::outset`]) for anything else. + fn strokes_of_container(&self, appearance: &Appearance, measured: Option>, symbols: Option<&SymbolBoxes>) -> Option { + if !self.visible || self.is_guide() { + return None; + } + match &self.kind { + NodeKind::Path { .. } | NodeKind::Compound { .. } => self.stroked_by(appearance, symbols), + NodeKind::Group { clip: false, .. } | NodeKind::Layer { clip: false, .. } if !self.is_compound_shape() => self + .editing_children() + .iter() + .fold(None, |acc, c| vectorcraft_geom::union_opt(acc, c.strokes_of_container(appearance, None, symbols))), + NodeKind::Image(_) | NodeKind::SymbolInstance { .. } => None, + _ => { + let o = appearance.outset(); + measured.unwrap_or_else(|| self.visual(symbols)).map(|b| b.inflate(o, o)) + } + } + } + + /// The visual box of this path or compound path painted with the strokes of `appearance`, + /// measured along its shape ([`Appearance::stroked_bounds`]). + fn stroked_by(&self, appearance: &Appearance, symbols: Option<&SymbolBoxes>) -> Option { + // A lone anchor is drawn only as the dot of a round or projecting cap on a closed subpath. + let dots = 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(appearance.stroked_bounds(b, || self.stroke_path().unwrap_or_default())) + } + /// The box of [`Node::visual_bounds`], or of [`Node::visual_bounds_with`] when `symbols` is /// given. pub(crate) fn visual(&self, symbols: Option<&SymbolBoxes>) -> Option { @@ -485,7 +547,9 @@ impl Node { } } NodeKind::Intertwine { children } => children.iter().fold(None, |acc, c| vectorcraft_geom::union_opt(acc, c.visual(symbols))), - NodeKind::Layer { .. } | NodeKind::Group { .. } => self.visual_of_members(self.editing_children().iter().map(|c| c.visual(symbols))), + NodeKind::Layer { .. } | NodeKind::Group { .. } => { + self.visual_of_measured_members(self.editing_children().iter().map(|c| (c.as_ref(), c.visual(symbols))), symbols) + } NodeKind::Blend { children, spec } => { let b = children.iter().fold(None, |acc, c| vectorcraft_geom::union_opt(acc, c.visual(symbols))); let o = crate::live::max_outset(children, symbols); @@ -495,12 +559,7 @@ impl Node { let o = crate::live::max_outset(content, symbols); self.geometric(symbols).map(|b| b.inflate(o, o)) } - NodeKind::Path { .. } | NodeKind::Compound { .. } => { - // 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::Path { .. } | NodeKind::Compound { .. } => self.stroked_by(&self.appearance, symbols), NodeKind::SymbolInstance { symbol, xf } => { let b = match symbols.and_then(|s| Some((s, s.art(symbol)?))) { Some((s, art)) if !s.art_strokes() => xf.transform_rect_bbox(art.geometric), diff --git a/crates/doc/src/node/tests.rs b/crates/doc/src/node/tests.rs index a7cd7986c4..8d6520265a 100644 --- a/crates/doc/src/node/tests.rs +++ b/crates/doc/src/node/tests.rs @@ -537,6 +537,52 @@ fn visual_of_members_unions_the_members_and_grows_by_the_group_stroke() { assert_eq!(g.visual_of_members([].into_iter()), None); } +/// A group's or layer's own miter-joined stroke is measured along its members' shapes, as a +/// path's is: a 10 pt stroke round a square reaches 5 pt past it, not the 50 pt a miter spike +/// could reach, through nested groups and in a turned frame too. It paints nothing round a hidden +/// member or an image. +#[test] +fn a_containers_own_miter_stroke_is_measured_along_its_members() { + use vectorcraft_color::{Color, Paint}; + let path = |id, r: Rect| Arc::new(Node::path(NodeId(id), shapes::rectangle(r), Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0))); + let stroke = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 10.0); + assert_eq!(stroke.outset(), 50.0, "a 10 pt miter-joined stroke can reach 50 pt"); + let stroked = |mut n: Node| { + n.appearance = stroke.clone().into(); + n + }; + let square = Rect::new(100.0, 100.0, 200.0, 200.0); + let around = Some(Rect::new(95.0, 95.0, 205.0, 205.0)); + assert_eq!(stroked(Node::group(NodeId(1), vec![path(2, square)])).visual_bounds(), around, "a group"); + let nested = Node::group(NodeId(1), vec![Arc::new(Node::group(NodeId(3), vec![path(2, square)]))]); + assert_eq!(stroked(nested.clone()).visual_bounds(), around, "a nested group"); + // Measured in a turned frame, as a box square to a turned selection is. + let turned = stroked(nested).bounds_in(&crate::Document::new(400.0, 400.0).symbol_boxes(), Affine::rotate(std::f64::consts::FRAC_PI_2), true); + let want = Rect::new(-205.0, 95.0, -95.0, 205.0); + assert!( + turned.is_some_and(|b| (b.x0 - want.x0).abs() + (b.y0 - want.y0).abs() + (b.x1 - want.x1).abs() + (b.y1 - want.y1).abs() < 1e-9), + "{turned:?}" + ); + let mut layer = Node::layer(NodeId(1), "Layer 1", LayerColor::Preset(0)); + if let NodeKind::Layer { children, .. } = &mut layer.kind { + children.push(path(2, square)); + } + assert_eq!(stroked(layer).visual_bounds(), around, "a layer"); + let mut hidden = (*path(3, Rect::new(100.0, 300.0, 200.0, 400.0))).clone(); + hidden.visible = false; + let image = ImageObject { + key: "k".into(), + width: 10, + height: 10, + xf: Affine::translate((500.0, 100.0)), + link: None, + placement: Default::default(), + pixels: None, + }; + let kids = vec![path(2, square), Arc::new(hidden), Arc::new(Node::new(NodeId(4), NodeKind::Image(Box::new(image))))]; + assert_eq!(stroked(Node::group(NodeId(1), kids)).visual_bounds(), Some(Rect::new(95.0, 95.0, 510.0, 400.0)), "a hidden member and an image"); +} + /// Type's visual bounds grow by how far its character strokes paint past the glyphs: half the /// weight centred, the whole weight outside, nothing inside; scaled with the type. #[test] diff --git a/crates/doc/src/orient.rs b/crates/doc/src/orient.rs index a2ff76d83f..c3a87988bc 100644 --- a/crates/doc/src/orient.rs +++ b/crates/doc/src/orient.rs @@ -121,10 +121,16 @@ impl Node { pub fn bounds_in(&self, symbols: &SymbolBoxes, a: Affine, visual: bool) -> Option { match &self.kind { NodeKind::Group { clip: false, .. } => { - let b = self.editing_children().iter().fold(None, |acc, c| union_opt(acc, c.bounds_in(symbols, a, visual))); - // The group's own strokes paint around its members (as in `visual_bounds`). - let o = if visual { self.appearance.outset() } else { 0.0 }; - b.map(|b| b.inflate(o, o)) + // The group's own strokes paint along its members, placed by `a` (as in `visual_bounds`). + let stroked = visual && self.appearance.painted_strokes().next().is_some(); + self.editing_children().iter().fold(None, |acc, c| { + let own = stroked.then(|| { + let mut placed = (**c).clone(); + placed.transform(a, false); + placed.painted_by_container(&self.appearance, Some(symbols)) + }); + union_opt(union_opt(acc, c.bounds_in(symbols, a, visual)), own.flatten()) + }) } NodeKind::Group { children, clip: true } => { let clip = children.first()?; diff --git a/crates/doc/src/reach.rs b/crates/doc/src/reach.rs index f83b0e9329..9189302bc0 100644 --- a/crates/doc/src/reach.rs +++ b/crates/doc/src/reach.rs @@ -600,21 +600,40 @@ pub const SLANT_INSTANCES: usize = 4096; impl crate::Node { /// Does a visible stroke of this object, of an object in it or of an object in a symbol /// instance's art from `symbols`, placed by `a`, stop at the corners of a segment at a slant - /// ([`Appearance::has_slanted_stroke_corners`])? Past [`SLANT_INSTANCES`] instances it answers - /// yes. + /// ([`Appearance::has_slanted_stroke_corners`])? The strokes of a group or layer without a + /// clipping path count along the paths and compound paths in it, which they paint along + /// ([`crate::Node::painted_by_container`]). Past [`SLANT_INSTANCES`] instances it answers yes. pub fn has_slanted_stroke_corners(&self, symbols: &crate::SymbolBoxes, a: vectorcraft_geom::Affine) -> bool { - self.slanted_corners(symbols, a, &std::cell::Cell::new(SLANT_INSTANCES)) + self.slanted_corners(symbols, a, &std::cell::Cell::new(SLANT_INSTANCES), &[]) } - /// [`Self::has_slanted_stroke_corners`] with `left` instances still to look into. - fn slanted_corners(&self, symbols: &crate::SymbolBoxes, a: vectorcraft_geom::Affine, left: &std::cell::Cell) -> bool { + /// [`Self::has_slanted_stroke_corners`] with `left` instances still to look into, and the + /// appearances of the groups and layers around this object whose strokes paint along it in + /// `around`. + fn slanted_corners( + &self, + symbols: &crate::SymbolBoxes, + a: vectorcraft_geom::Affine, + left: &std::cell::Cell, + around: &[&Appearance], + ) -> bool { if let crate::NodeKind::SymbolInstance { symbol, xf } = &self.kind { let Some(l) = left.get().checked_sub(1) else { return true }; left.set(l); - return symbols.measure_art(symbol, |art| art.slanted_corners(symbols, a * *xf, left)).unwrap_or(false); + return symbols.measure_art(symbol, |art| art.slanted_corners(symbols, a * *xf, left, &[])).unwrap_or(false); } - (self.appearance.has_cornered_stroke() && self.stroke_path().is_some_and(|bp| self.appearance.has_slanted_stroke_corners(&bp, a))) - || self.children().is_some_and(|c| c.iter().any(|c| c.slanted_corners(symbols, a, left))) + let slanted = |ap: &Appearance| ap.has_cornered_stroke() && self.stroke_path().is_some_and(|bp| ap.has_slanted_stroke_corners(&bp, a)); + let shaped = matches!(self.kind, crate::NodeKind::Path { .. } | crate::NodeKind::Compound { .. }); + if slanted(&self.appearance) || shaped && around.iter().any(|ap| slanted(ap)) { + return true; + } + let plain = + matches!(self.kind, crate::NodeKind::Group { clip: false, .. } | crate::NodeKind::Layer { clip: false, .. }) && !self.is_compound_shape(); + let mut inner: Vec<&Appearance> = if plain { around.to_vec() } else { vec![] }; + if plain && self.appearance.has_cornered_stroke() { + inner.push(&self.appearance); + } + self.children().is_some_and(|c| c.iter().any(|c| c.slanted_corners(symbols, a, left, &inner))) } } diff --git a/crates/engine/src/cmd/object/spans.rs b/crates/engine/src/cmd/object/spans.rs index 57fe9aeb2c..88e9629a82 100644 --- a/crates/engine/src/cmd/object/spans.rs +++ b/crates/engine/src/cmd/object/spans.rs @@ -1,5 +1,5 @@ use super::shape::scale_across; -use super::{Affine, Document, Node, NodeId, NodeKind, OrientedBox, Point, Rect}; +use super::{Affine, Appearance, Document, Node, NodeId, NodeKind, OrientedBox, Point, Rect}; /// One axis of an object that [`set_bounds`] sizes, square to its frame: the geometric box runs /// from `lo` to `hi`, and the visual box reaches past each end (before, after) by a margin that @@ -47,27 +47,47 @@ impl Spans { (true, true) => n.visual_bounds_with(s), (false, _) => n.bounds_in(s, to_local, visual), }; + // The box the strokes of the plain groups around `n` paint along it, square to `frame` + // (`Node::painted_by_container`). + let painted = |n: &Node, groups: &[&Appearance], s: &vectorcraft_doc::SymbolBoxes| { + if groups.is_empty() { + return None; + } + let placed; + let n = if frame.angle == 0.0 { + n + } else { + let mut m = n.clone(); + m.transform(to_local, false); + placed = m; + &placed + }; + groups.iter().fold(None, |acc, a| vectorcraft_geom::union_opt(acc, n.painted_by_container(a, Some(s)))) + }; let roots: Vec<&Node> = ids.iter().filter_map(|id| d.node(*id)).collect(); let mut parts = vec![]; - if !roots.iter().all(|n| open(n, 0.0, d, &symbols, to_local, &mut parts)) { - parts = roots.iter().map(|n| Part::Object(n, 0.0)).collect(); + if !roots.iter().all(|n| open(n, &[], d, &symbols, to_local, &mut parts)) { + parts = roots.iter().map(|n| Part::Object(n, vec![])).collect(); } let mut out = Self { x: vec![], y: vec![], strokes }; for part in parts { let (g, v, vs) = match part { - Part::Object(n, grow) => { + Part::Object(n, groups) => { let Some(g) = measure(n, &symbols, false) else { continue }; - let v = measure(n, &symbols, true).unwrap_or(g).inflate(grow, grow); - let vs = if strokes { measure(n, &without, true).unwrap_or(g).inflate(grow, grow) } else { g }; + let v = vectorcraft_geom::union_opt(measure(n, &symbols, true), painted(n, &groups, &symbols)).unwrap_or(g); + let vs = + if strokes { vectorcraft_geom::union_opt(measure(n, &without, true), painted(n, &groups, &without)).unwrap_or(g) } else { g }; (g, v, vs) } // Measured as the instance's own box measures its art (`Node::visual_bounds_with`): - // the strokes at their own weight, and the instances in it by their art boxes. + // the strokes at their own weight, those of the plain groups around it in the art + // included, and the instances in it by their art boxes. Part::Art(n, xf, grow, kept) => { let mut placed = n.clone(); placed.transform(xf, false); let Some(g) = placed.geometric_bounds_with(&art) else { continue }; - let v = placed.visual_bounds_with(&art).map_or(g, |v| v.union(g)).inflate(kept + grow, kept + grow); + let v = placed.visual_bounds_with(&art).map_or(g, |v| v.union(g)); + let v = kept.iter().fold(v, |v, a| placed.painted_by_container(a, Some(&art)).map_or(v, |p| v.union(p))).inflate(grow, grow); let vs = if strokes { g.inflate(grow, grow) } else { g }; (g, v, vs) } @@ -155,29 +175,40 @@ const MAX_SPANS: usize = 4096; /// What [`Spans::measure`] measures as one span. enum Part<'a> { - /// An object of the selection, or a member of a plain group in it, and the outset of the - /// strokes of the plain groups around it, which scale with its own. - Object(&'a Node, f64), + /// An object of the selection, or a member of a plain group in it, and the appearances of the + /// plain groups around it, whose strokes paint along it and scale with its own. + Object(&'a Node, Vec<&'a Appearance>), /// An object in the art of a symbol instance and the transform that places it square to the - /// frame. The outset of the strokes of the instance and of the plain groups around the instance - /// scales with the strokes; the outset of the strokes of the plain groups around the object in - /// the art stays, as the strokes in the art do. - Art(&'a Node, Affine, f64, f64), + /// frame. The outset of the strokes of the instance scales with the strokes; the strokes of the + /// plain groups around the object in the art, given by their appearances, paint along it and + /// stay, as the strokes in the art do. The strokes of the plain groups around the instance paint + /// nothing round it. + Art(&'a Node, Affine, f64, Vec<&'a Appearance>), } -/// Adds the parts that [`Spans::measure`] measures `n` by to `parts`, `grow` being the outset of -/// the strokes of the plain groups around it ([`Part::Object`]). A plain group ([`opens`]) adds the -/// parts of each member. A symbol instance without effects or an opacity mask of its own adds the -/// objects of its art, with plain groups there opened too ([`Part::Art`]), when its symbol's art +/// Adds the parts that [`Spans::measure`] measures `n` by to `parts`, `groups` being the +/// appearances of the plain groups around it ([`Part::Object`]); hidden members and guides stop +/// those groups' strokes. A plain group ([`opens`]) adds the parts of each member. A symbol +/// instance without effects or an opacity mask of its own adds the objects of its art, with plain +/// groups there opened too ([`Part::Art`]), when its symbol's art /// measures it, it is not in perspective, and its transform keeps the art square to the frame (a /// turned or slanted instance is measured by the box around its turned art box). Anything else is /// one part. Returns false when the parts pass [`MAX_SPANS`]. -fn open<'a>(n: &'a Node, grow: f64, d: &'a Document, symbols: &vectorcraft_doc::SymbolBoxes, to_local: Affine, parts: &mut Vec>) -> bool { +fn open<'a>( + n: &'a Node, + groups: &[&'a Appearance], + d: &'a Document, + symbols: &vectorcraft_doc::SymbolBoxes, + to_local: Affine, + parts: &mut Vec>, +) -> bool { + let groups = if n.visible && !n.is_guide() { groups } else { &[] }; if opens(n) { match &n.kind { NodeKind::Group { children, .. } => { - let grow = grow + n.appearance.outset(); - return children.iter().all(|c| open(c, grow, d, symbols, to_local, parts)); + let mut groups = groups.to_vec(); + groups.push(&n.appearance); + return children.iter().all(|c| open(c, &groups, d, symbols, to_local, parts)); } NodeKind::SymbolInstance { symbol, xf } => { let xf = to_local * *xf; @@ -186,38 +217,41 @@ fn open<'a>(n: &'a Node, grow: f64, d: &'a Document, symbols: &vectorcraft_doc:: && symbols.art_bounds(symbol).is_some() && let Some(art) = d.symbols.iter().find(|s| s.name == *symbol) { - return open_art(&art.art, xf, grow + n.appearance.outset(), 0.0, parts); + return open_art(&art.art, xf, n.appearance.outset(), &[], parts); } } _ => {} } } - parts.push(Part::Object(n, grow)); + parts.push(Part::Object(n, groups.to_vec())); parts.len() <= MAX_SPANS } /// [`open`] for `n` in symbol art that `xf` places square to the frame: a plain group adds the parts -/// of each member, and anything else, a symbol instance included, is one part. `kept` is the outset -/// of the strokes of the plain groups around `n` in the art, and `grow` that of the instance and the -/// plain groups around it. -fn open_art<'a>(n: &'a Node, xf: Affine, grow: f64, kept: f64, parts: &mut Vec>) -> bool { +/// of each member, and anything else, a symbol instance included, is one part. `kept` holds the +/// appearances of the plain groups around `n` in the art, stopped at hidden members and guides, +/// and `grow` is the outset of the strokes of the instance. +fn open_art<'a>(n: &'a Node, xf: Affine, grow: f64, kept: &[&'a Appearance], parts: &mut Vec>) -> bool { + let kept = if n.visible && !n.is_guide() { kept } else { &[] }; if let NodeKind::Group { children, .. } = &n.kind && opens(n) { - let kept = kept + n.appearance.outset(); - return children.iter().all(|c| open_art(c, xf, grow, kept, parts)); + let mut kept = kept.to_vec(); + kept.push(&n.appearance); + return children.iter().all(|c| open_art(c, xf, grow, &kept, parts)); } - parts.push(Part::Art(n, xf, grow, kept)); + parts.push(Part::Art(n, xf, grow, kept.to_vec())); parts.len() <= MAX_SPANS } -/// Whether [`open`] opens `n`: a group without a clip, a Shaper construction, effects or an opacity -/// mask of its own, or a symbol instance without effects or an opacity mask of its own. +/// Whether [`open`] opens `n`: a group that is not a compound shape and has no clip, Shaper +/// construction, effects or opacity mask of its own, or a symbol instance without effects or an +/// opacity mask of its own. fn opens(n: &Node) -> bool { let visible = |effects: &[vectorcraft_doc::Effect]| effects.iter().any(|e| e.visible); let effects = visible(&n.appearance.effects) || n.appearance.items.iter().any(|i| visible(i.effects())); let kind = match n.kind { - NodeKind::Group { clip, .. } => !clip && n.extras().shaper.is_none(), + NodeKind::Group { clip, .. } => !clip && !n.is_compound_shape() && n.extras().shaper.is_none(), NodeKind::SymbolInstance { .. } => true, _ => false, }; diff --git a/crates/engine/src/tests/previewbounds.rs b/crates/engine/src/tests/previewbounds.rs index 53f344ceb8..af9602f3cd 100644 --- a/crates/engine/src/tests/previewbounds.rs +++ b/crates/engine/src/tests/previewbounds.rs @@ -975,6 +975,57 @@ fn set_bounds_gives_a_closed_path_with_beveled_or_mitered_corners_the_size_out_o assert!(wrong.is_empty(), "{wrong:?}"); } +#[test] +fn set_bounds_gives_a_group_whose_own_stroke_miters_a_slanted_corner_the_size_out_of_proportion() { + // The group's own beveled or mitered stroke paints along the unstroked triangle in it, so its + // margins at the tip follow the slanted segments' directions too. + let mut wrong = vec![]; + for (join, limit) in [("bevel", 10.0), ("miter", 2.0), ("miter", 10.0)] { + let mut s = session(); + let anchors = json!([{"x": 100, "y": 150}, {"x": 110, "y": 100}, {"x": 120, "y": 150}]); + let path = NodeId(run(&mut s, "path.create", json!({"anchors": anchors, "closed": true}))["id"].as_u64().unwrap()); + let group = NodeId(run(&mut s, "object.group", json!({}))["id"].as_u64().unwrap()); + run(&mut s, "appearance.addStroke", json!({"ids": [group.0]})); + run(&mut s, "stroke.set", json!({"weight": 2, "join": join, "miterLimit": limit})); + run(&mut s, "select.set", json!({"ids": [path.0]})); + run(&mut s, "paint.setStroke", json!({"none": true})); + let doc = &s.doc().unwrap().doc; + let painted = |id: NodeId| doc.node(id).unwrap().appearance.painted_strokes().count(); + assert_eq!((painted(path), painted(group)), (0, 1), "only the group has a stroke"); + preview(&mut s, true); + run(&mut s, "select.set", json!({"ids": [group.0]})); + run(&mut s, "object.setBounds", json!({"width": 60, "height": 20, "reference": 0, "strokes": false})); + let v = s.transform_bounds(&[group]).unwrap(); + if (v.width() - 60.0).abs() > 1e-6 || (v.height() - 20.0).abs() > 1e-6 { + wrong.push(format!("{join} {limit}: {v:?}")); + } + } + assert!(wrong.is_empty(), "{wrong:?}"); +} + +#[test] +fn set_bounds_gives_a_stroked_group_with_a_hidden_subgroup_the_requested_width() { + // The hidden subgroup supplies the rightmost geometry, but the outer group's stroke paints + // only along the visible rectangle. Square corners keep the resize in the span solver. + let mut s = session(); + let shown = rect(&mut s, 100.0, 50.0, 0.0); + rect(&mut s, 200.0, 50.0, 0.0); + let hidden = NodeId(run(&mut s, "object.group", json!({}))["id"].as_u64().unwrap()); + run(&mut s, "select.set", json!({"ids": [shown.0, hidden.0]})); + let group = NodeId(run(&mut s, "object.group", json!({}))["id"].as_u64().unwrap()); + run(&mut s, "appearance.addStroke", json!({"ids": [group.0]})); + run(&mut s, "paint.setStroke", json!({"ids": [group.0], "item": 0, "color": "#000000"})); + run(&mut s, "stroke.set", json!({"weight": 10, "join": "miter"})); + run(&mut s, "object.hide", json!({"ids": [hidden.0]})); + preview(&mut s, true); + run(&mut s, "select.set", json!({"ids": [group.0]})); + let before = s.transform_bounds(&[group]).unwrap(); + assert!(close(before, Rect::new(95.0, 95.0, 250.0, 155.0)), "{before:?}"); + run(&mut s, "object.setBounds", json!({"width": 300, "reference": 0, "strokes": false})); + let after = s.transform_bounds(&[group]).unwrap(); + assert!((after.width() - 300.0).abs() < 1e-6, "{after:?}"); +} + #[test] fn set_bounds_gives_a_symbol_instance_of_a_diagonal_butt_line_the_size_out_of_proportion() { let mut s = session(); @@ -1034,19 +1085,21 @@ fn set_bounds_measures_the_members_of_groups_one_by_one() { wrong.push(format!("nested {nested}, plain {plain}, strokes {strokes}: {v:?}")); } } - // The group with a 1 pt stroke of its own, which reaches 5 pt at its miter limit of 10, around - // each member: the visual box runs from (-7, 90) to (110, 160). Set to 224 wide from its left - // edge, it is 224 wide and its left edge stays at -7, with strokes kept or scaling. + // The group with a 20 pt miter-joined stroke of its own, which paints 10 pt past each member's + // corners, not the 100 pt its miter limit of 10 could reach: the visual box runs from (-10, 90) + // to (110, 160). Set to 224 wide from its left edge, it is 224 wide and its left edge stays at + // -10, with strokes kept or scaling. for strokes in [false, true] { let (mut s, _) = thin_and_thick(); let group = NodeId(run(&mut s, "object.group", json!({}))["id"].as_u64().unwrap()); run(&mut s, "appearance.addStroke", json!({"ids": [group.0]})); + run(&mut s, "stroke.set", json!({"weight": 20})); let ids = [group, rect(&mut s, 100.0, 10.0, 0.0)]; run(&mut s, "select.set", json!({"ids": [ids[0].0, ids[1].0]})); - assert!(close(s.transform_bounds(&ids).unwrap(), Rect::new(-7.0, 90.0, 110.0, 160.0)), "{:?}", s.transform_bounds(&ids)); + assert!(close(s.transform_bounds(&ids).unwrap(), Rect::new(-10.0, 90.0, 110.0, 160.0)), "{:?}", s.transform_bounds(&ids)); run(&mut s, "object.setBounds", json!({"width": 224, "reference": 0, "strokes": strokes})); let v = s.transform_bounds(&ids).unwrap(); - if !close(v, Rect::new(-7.0, 90.0, 217.0, 160.0)) { + if !close(v, Rect::new(-10.0, 90.0, 214.0, 160.0)) { wrong.push(format!("the group with a stroke, strokes {strokes}: {v:?}")); } } diff --git a/crates/engine/src/tests/symbolbounds.rs b/crates/engine/src/tests/symbolbounds.rs index 50adeededb..a6abff8193 100644 --- a/crates/engine/src/tests/symbolbounds.rs +++ b/crates/engine/src/tests/symbolbounds.rs @@ -185,8 +185,9 @@ fn set_bounds_measures_the_objects_in_an_instances_art_one_by_one() { // A symbol whose art is a group of a rectangle from x 0 to 10 with a 2 pt stroke and one from // x 3 to 10 with a 10 pt stroke, both 50 pt high, and an instance of it 100 pt down, in a group // when `grouped`, selected with an unstroked rectangle from x 100 to 110 when `plain`. The - // instance's visual box runs from (-2, 95) to (15, 155). With `own`, the art's group and the - // instance each have a 1 pt stroke of their own, which reaches 5 pt at its miter limit of 10. + // instance's visual box runs from (-2, 95) to (15, 155). With `own`, the art's group has a 20 pt + // miter-joined stroke of its own, which paints 10 pt past each rectangle's corners, and the + // instance a 1 pt stroke, which grows its box by the 5 pt it can reach at its miter limit of 10. let selection = |grouped: bool, plain: bool, own: bool| { let mut d = Document::new(400.0, 400.0); let stroked = |d: &mut Document, x0: f64, weight: f64| { @@ -199,7 +200,7 @@ fn set_bounds_measures_the_objects_in_an_instances_art_one_by_one() { let mut art = Node::group(d.alloc_id(), vec![Arc::new(stroked(&mut d, 0.0, 2.0)), Arc::new(stroked(&mut d, 3.0, 10.0))]); let mut instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Mixed".into(), xf: Affine::translate((0.0, 100.0)) }); if own { - *art.appearance = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 1.0); + *art.appearance = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 20.0); *instance.appearance = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 1.0); } d.symbols.push(Symbol { dynamic: false, name: "Mixed".into(), art: Arc::new(art) }); @@ -221,8 +222,8 @@ fn set_bounds_measures_the_objects_in_an_instances_art_one_by_one() { // With the unstroked rectangle, the visual box runs from (-2, 95) to (110, 155). Set to 224 wide // from its left edge, it is 224 wide and its left edge stays at -2, with strokes kept or // scaling, and with the instance in a group. The instance alone set to 37 wide is 37 wide. With - // the strokes of their own, which paint around each rectangle, the visual box runs from - // (-12, 85) to (110, 165): set to 224 wide, it is 224 wide and its left edge stays at -12. + // the strokes of their own, the visual box runs from (-15, 85) to (110, 165): set to 224 wide, + // it is 224 wide and its left edge stays at -15. let mut wrong = vec![]; for (grouped, plain, own, width, strokes) in [ (false, true, false, 224.0, false), @@ -233,7 +234,7 @@ fn set_bounds_measures_the_objects_in_an_instances_art_one_by_one() { (false, true, true, 224.0, true), ] { let (mut s, ids) = selection(grouped, plain, own); - let (left, top, bottom) = if own { (-12.0, 85.0, 165.0) } else { (-2.0, 95.0, 155.0) }; + let (left, top, bottom) = if own { (-15.0, 85.0, 165.0) } else { (-2.0, 95.0, 155.0) }; let before = s.transform_bounds(&ids); if !close(before, Rect::new(left, top, if plain { 110.0 } else { 15.0 }, bottom)) { wrong.push(format!("grouped {grouped}, plain {plain}, own {own}, before: {before:?}"));