mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-28 09:21:55 +01:00
Effects: bounds hold the dot a closed lone anchor draws
A round or projecting cap draws a dot for a closed subpath of one anchor. The bounds the effects are measured by left that dot out: the visual and cull bounds, the box Rasterize makes its image over, and the reach of an effected path's raster effects. A path of only lone anchors was measured at the origin. - `vectorcraft_geom::path::drawn_curve_bounds` measures a kurbo path as `PathData::drawn_bounds` measures the anchors it stands for. - `crates/render/src/fx` measures effected paths with it, and a path that draws nothing by its anchors, as `Node::visual_bounds` does. - A stroke measured along its segments (miter and bevel joins, projecting caps) gives the dot of a closed lone anchor its reach.
This commit is contained in:
1 parent
5c709a1a64
commit
b73f28dc71
6 files changed
+166
-13
No files matched your search
@@ -498,6 +498,31 @@ fn a_lone_anchor_widens_the_visual_box_only_where_its_cap_draws_a_dot() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The dot a round or projecting cap draws for a closed lone anchor reaches half the stroke's
|
||||
/// width round the anchor under every join, miter and bevel joins included, which measure the
|
||||
/// stroke along its segments.
|
||||
#[test]
|
||||
fn a_lone_anchors_dot_reaches_half_the_stroke_width_under_every_join() {
|
||||
use crate::appearance::{LineCap, LineJoin};
|
||||
use vectorcraft_geom::{Point, SubPath};
|
||||
let square = shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0));
|
||||
for join in [LineJoin::Miter, LineJoin::Bevel, LineJoin::Round] {
|
||||
for cap in [LineCap::Butt, LineCap::Round, LineCap::Square] {
|
||||
let mut subpaths = square.subpaths.clone();
|
||||
subpaths.push(SubPath::polyline(&[Point::new(30.0, 5.0)], true));
|
||||
let mut p = Node::path(NodeId(2), PathData::new(subpaths), Appearance::default_art());
|
||||
let s = p.appearance.stroke_mut().unwrap();
|
||||
(s.cap, s.join, s.width) = (cap, join, 4.0);
|
||||
let visual = p.visual_bounds().unwrap();
|
||||
if cap == LineCap::Butt {
|
||||
assert!(visual.x1 < 13.0, "{join:?} {cap:?}: the visual box {visual:?} reaches toward a lone anchor that draws nothing");
|
||||
} else {
|
||||
assert!(visual.x1 >= 32.0 - 1e-9 && visual.y1 >= 10.0, "{join:?} {cap:?}: the visual box {visual:?} leaves out the dot's reach");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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]
|
||||
|
||||
@@ -291,9 +291,12 @@ impl StrokeLayer {
|
||||
// past them are measured.
|
||||
match self.body_along(&segs, side, out) {
|
||||
Some(body) => out = out.union(body),
|
||||
// A subpath with no length paints at most its caps' dot.
|
||||
// A subpath with no length paints at most its caps' dot. A closed subpath with
|
||||
// no segment, a lone anchor, has its dot at that anchor under a round or
|
||||
// projecting cap.
|
||||
None if !butt => {
|
||||
let dot = segments(&els[r]).iter().map(ParamCurveExtrema::bounding_box).reduce(|a, b| a.union(b));
|
||||
let lone = (self.cap != LineCap::Butt).then(|| els[r.start].end_point()).flatten().map(|p| Rect::from_points(p, p));
|
||||
let dot = segments(&els[r]).iter().map(ParamCurveExtrema::bounding_box).reduce(|a, b| a.union(b)).or(lone);
|
||||
out = dot.map_or(out, |d| out.union(d.inflate(side, side)));
|
||||
}
|
||||
None => {}
|
||||
|
||||
@@ -861,6 +861,28 @@ pub fn curve_bounds(path: &BezPath) -> Rect {
|
||||
path.segments().map(segment_bounds).reduce(|bb, seg_bb| bb.union(seg_bb)).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The box of what a stroke along `path` draws before the stroke's own reach, as
|
||||
/// [`PathData::drawn_bounds`] measures the path it stands for: the box of its segments, as in
|
||||
/// [`curve_bounds`], and with `dots` the point of each closed subpath that has no segment, which a
|
||||
/// round or projecting cap draws as a dot. `None` when the path draws nothing.
|
||||
pub fn drawn_curve_bounds(path: &BezPath, dots: bool) -> Option<Rect> {
|
||||
let mut bbox = path.segments().map(segment_bounds).reduce(|bb, seg_bb| bb.union(seg_bb));
|
||||
if !dots {
|
||||
return bbox;
|
||||
}
|
||||
let els = path.elements();
|
||||
for (r, closed) in crate::bez::subpaths(path) {
|
||||
if closed
|
||||
&& kurbo::segments(els[r.clone()].iter().copied()).next().is_none()
|
||||
&& let Some(p) = els[r.start].end_point()
|
||||
{
|
||||
let dot = Rect::from_points(p, p);
|
||||
bbox = Some(bbox.map_or(dot, |bb| bb.union(dot)));
|
||||
}
|
||||
}
|
||||
bbox
|
||||
}
|
||||
|
||||
/// The box of `seg` as kurbo's `bounding_box` measures it (its start, end and extrema), except
|
||||
/// that a segment with a control point whose x or y is not finite or is larger in size than
|
||||
/// `f64::MAX / 16` is measured by its control points. The box then holds every point the segment
|
||||
@@ -916,6 +938,10 @@ mod tests {
|
||||
let path = proptest::collection::vec(sub, 0..4).prop_map(PathData::new);
|
||||
proptest!(ProptestConfig::with_cases(2000), |(pd in path)| {
|
||||
prop_assert_eq!(bits(pd.bounds()), bits(reference(&pd)));
|
||||
// The kurbo path the anchors stand for draws the same box.
|
||||
for dots in [false, true] {
|
||||
prop_assert_eq!(bits(drawn_curve_bounds(&pd.to_bezpath(), dots)), bits(pd.drawn_bounds(dots)));
|
||||
}
|
||||
});
|
||||
// Two lone anchors: the path has no segment, and the box spans the two points.
|
||||
let lone = PathData::new(vec![SubPath::polyline(&[Point::new(5.0, 5.0)], false), SubPath::polyline(&[Point::new(9.0, 1.0)], true)]);
|
||||
|
||||
@@ -124,7 +124,7 @@ pub(crate) fn visual_bounds(symbols: &SymbolBoxes, n: &Node) -> Option<Rect> {
|
||||
if carry.skipped() {
|
||||
carry.hide_skipped(&mut ran.to_mut().appearance.effects);
|
||||
}
|
||||
let mut r: Option<Rect> = (!g.elements().is_empty()).then(|| g.bounding_box());
|
||||
let mut r: Option<Rect> = drawn_box(&g, n.appearance.painted_strokes().any(dotted));
|
||||
for (j, item) in n.appearance.items.iter().enumerate() {
|
||||
let fx = item_effects(item);
|
||||
if effects::has_geometry(fx) {
|
||||
@@ -134,10 +134,7 @@ pub(crate) fn visual_bounds(symbols: &SymbolBoxes, n: &Node) -> Option<Rect> {
|
||||
{
|
||||
c.hide_skipped(it.effects_mut());
|
||||
}
|
||||
if !ig.elements().is_empty() {
|
||||
let b = ig.bounding_box();
|
||||
r = Some(r.map_or(b, |r| r.union(b)));
|
||||
}
|
||||
r = vectorcraft_geom::union_opt(r, drawn_box(&ig, item_dots(item)));
|
||||
}
|
||||
}
|
||||
let s = n.appearance.outset();
|
||||
@@ -324,10 +321,29 @@ fn item_bounds(doc: &Document, n: &Node, item: &AppearanceItem) -> Option<Rect>
|
||||
if effects::has_geometry(fx) {
|
||||
let ig = apply(fx, &g, &GeomContext { doc: Some(doc), ..GeomContext::of(n) }.item(item));
|
||||
let o = if let AppearanceItem::Stroke(s) = item { s.reach() } else { 0.0 };
|
||||
return (!ig.elements().is_empty()).then(|| ig.bounding_box().inflate(o, o));
|
||||
return drawn_box(&ig, item_dots(item)).map(|b| b.inflate(o, o));
|
||||
}
|
||||
let one = vectorcraft_doc::Appearance { items: vec![item.clone()], ..Default::default() };
|
||||
(!g.elements().is_empty()).then(|| one.stroked_bounds(g.bounding_box(), || g.clone()))
|
||||
drawn_box(&g, item_dots(item)).map(|b| one.stroked_bounds(b, || g.clone()))
|
||||
}
|
||||
|
||||
/// The box of what `g` draws before the strokes' reach ([`vectorcraft_geom::path::drawn_curve_bounds`]),
|
||||
/// with the lone anchors of closed subpaths when `dots`; a path that draws nothing is measured by
|
||||
/// its anchors, as [`Node::visual_bounds`] measures one. `None` for a path with no elements.
|
||||
pub(super) fn drawn_box(g: &BezPath, dots: bool) -> Option<Rect> {
|
||||
vectorcraft_geom::path::drawn_curve_bounds(g, dots)
|
||||
.or_else(|| g.elements().iter().filter_map(|e| e.end_point()).map(|p| Rect::from_points(p, p)).reduce(|a, b| a.union(b)))
|
||||
}
|
||||
|
||||
/// Does `s`, a painted stroke, draw a lone anchor of a closed subpath as a dot (a round or
|
||||
/// projecting cap)?
|
||||
pub(super) fn dotted(s: &vectorcraft_doc::StrokeLayer) -> bool {
|
||||
matches!(s.cap, vectorcraft_doc::LineCap::Round | vectorcraft_doc::LineCap::Square)
|
||||
}
|
||||
|
||||
/// Does `item` draw lone anchors as dots: a painted stroke with a round or projecting cap?
|
||||
fn item_dots(item: &AppearanceItem) -> bool {
|
||||
matches!(item, AppearanceItem::Stroke(s) if paints(item) && dotted(s))
|
||||
}
|
||||
|
||||
/// `b` grown by how far raster effects `fx` paint past it, each spreading what the ones before it
|
||||
@@ -382,12 +398,13 @@ fn shape_bounds(symbols: &SymbolBoxes, n: &Node) -> Option<Rect> {
|
||||
let mut plain = n.appearance.clone();
|
||||
plain.items.retain(|i| !(paints(i) && effects::has_geometry(item_effects(i))));
|
||||
let on_path = !n.appearance.items.iter().any(paints) || plain.items.iter().any(paints);
|
||||
let mut r = (on_path && !g.elements().is_empty()).then(|| plain.stroked_bounds(g.bounding_box(), || g.clone()));
|
||||
let dots = plain.painted_strokes().any(dotted);
|
||||
let mut r = on_path.then(|| drawn_box(&g, dots)).flatten().map(|b| plain.stroked_bounds(b, || g.clone()));
|
||||
let ctx = GeomContext { doc: Some(symbols.document()), ..GeomContext::of(n) };
|
||||
for item in n.appearance.items.iter().filter(|i| paints(i) && effects::has_geometry(item_effects(i))) {
|
||||
let ig = apply(item_effects(item), &g, &ctx.item(item));
|
||||
let o = if let AppearanceItem::Stroke(s) = item { s.reach() } else { 0.0 };
|
||||
r = vectorcraft_geom::union_opt(r, (!ig.elements().is_empty()).then(|| ig.bounding_box().inflate(o, o)));
|
||||
r = vectorcraft_geom::union_opt(r, drawn_box(&ig, item_dots(item)).map(|b| b.inflate(o, o)));
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::bounds::{cull_bounds, effected_path_carrying, node_bezpath, rasterize_stage};
|
||||
use super::bounds::{cull_bounds, dotted, drawn_box, effected_path_carrying, node_bezpath, rasterize_stage};
|
||||
use super::entries::{Content, Drawn, FX_RASTER_BUDGET, FxGeom, Inputs, ItemGeom, Kept, KeptImage, Made, Reads, Shadow, ShadowEntry};
|
||||
use super::pixels::FilterReach;
|
||||
use super::predicates::{evaluates_live, is_container, rasterize_bounds, reads_outside, shifted};
|
||||
@@ -453,7 +453,7 @@ impl Renderer {
|
||||
let rfx = effects::raster_effects(&paints.appearance.effects);
|
||||
// Document-space reach of the painted geometry (strokes, arrowheads…).
|
||||
let outset = n.appearance.outset();
|
||||
let reach = g.bounding_box().inflate(outset, outset);
|
||||
let reach = drawn_box(g, paints.appearance.painted_strokes().any(dotted)).unwrap_or_default().inflate(outset, outset);
|
||||
let bounds = rasterize_bounds(f.symbols, n, &rfx);
|
||||
// Under Rasterize the content reaches as far as its fills' and strokes' raster effects paint.
|
||||
let reach = bounds.map_or(reach, |b| reach.union(b));
|
||||
|
||||
@@ -99,6 +99,88 @@ fn doc_with(r: Rect, fill: Color, effects: Vec<(&str, serde_json::Value)>) -> Do
|
||||
d
|
||||
}
|
||||
|
||||
/// A stroked line from (10, 50) to (60, 50) beside a lone anchor at (90, 90), closed or open
|
||||
/// (`line` false: the lone anchor alone), its stroke 4 pt wide with `cap`, round joins and the
|
||||
/// object-level `object` and stroke-level `stroke` effects.
|
||||
fn lone_anchor_doc(line: bool, closed: bool, cap: vectorcraft_doc::LineCap, object: &[Effect], stroke: &[Effect]) -> Document {
|
||||
use vectorcraft_geom::{PathData, SubPath};
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let lone = SubPath::polyline(&[Point::new(90.0, 90.0)], closed);
|
||||
let subpaths = if line { vec![SubPath::polyline(&[Point::new(10.0, 50.0), Point::new(60.0, 50.0)], false), lone] } else { vec![lone] };
|
||||
let mut ap = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 4.0);
|
||||
let s = ap.stroke_mut().unwrap();
|
||||
(s.cap, s.join) = (cap, vectorcraft_doc::LineJoin::Round);
|
||||
s.effects = stroke.to_vec();
|
||||
ap.effects = object.to_vec();
|
||||
let n = Node::path(d.alloc_id(), PathData::new(subpaths), ap);
|
||||
let l = d.layers[0].id;
|
||||
d.insert(Some(l), 0, n).unwrap();
|
||||
d
|
||||
}
|
||||
|
||||
/// The bounds the effects are measured by hold the dot a round or projecting cap draws for a
|
||||
/// lone anchor of a closed subpath, wherever the effected path is measured: the visual and cull
|
||||
/// bounds (with the object's or the stroke's own geometry effect) and what Rasterize makes its image
|
||||
/// over (the path as drawn, and a stroke with a raster effect, with and without a geometry effect
|
||||
/// of its own). A lone anchor that draws no dot (open, or under a butt cap) leaves them as the line
|
||||
/// alone has them.
|
||||
#[test]
|
||||
fn effect_bounds_hold_a_lone_anchors_dot_only_where_its_cap_draws_one() {
|
||||
use vectorcraft_doc::LineCap;
|
||||
let fx = |id: &str, p: serde_json::Value| Effect { id: id.into(), params: p, visible: true };
|
||||
// The object's effect moves the dot to (91, 90), the stroke's own effect to (130, 90), and the
|
||||
// stroke's shadow paints a copy 30 pt below.
|
||||
let moved = fx("distort.transform", json!({"moveH": 1}));
|
||||
let far = fx("distort.transform", json!({"moveH": 40}));
|
||||
let shadow = fx("stylize.dropShadow", json!({"x": 0, "y": 30, "blur": 0}));
|
||||
// (what, object effects, stroke effects, measure, the center of the lowest dot painted)
|
||||
type Measure = fn(&Document, &Node) -> Option<Rect>;
|
||||
let visual: Measure = |d, n| super::visual_bounds(&d.symbol_boxes(), n);
|
||||
let cull: Measure = |d, n| super::cull_bounds(&d.symbol_boxes(), n);
|
||||
let drawn: Measure = |d, n| crate::drawn_bounds(d, n);
|
||||
type Case = (&'static str, Vec<Effect>, Vec<Effect>, Measure, Point);
|
||||
let cases: [Case; 7] = [
|
||||
("visual, object effect", vec![moved.clone()], vec![], visual, Point::new(91.0, 90.0)),
|
||||
("visual, stroke effect", vec![], vec![far.clone()], visual, Point::new(130.0, 90.0)),
|
||||
("cull, object effect", vec![moved.clone()], vec![], cull, Point::new(91.0, 90.0)),
|
||||
("drawn, object effect", vec![moved.clone()], vec![], drawn, Point::new(91.0, 90.0)),
|
||||
("drawn, stroke effect", vec![], vec![far.clone()], drawn, Point::new(130.0, 90.0)),
|
||||
("drawn, stroke raster effect", vec![moved.clone()], vec![shadow.clone()], drawn, Point::new(91.0, 120.0)),
|
||||
("drawn, stroke geometry and raster effects", vec![], vec![far.clone(), shadow.clone()], drawn, Point::new(130.0, 120.0)),
|
||||
];
|
||||
for (what, object, stroke, measure, dot) in cases {
|
||||
for (closed, cap) in [(true, LineCap::Round), (true, LineCap::Square), (true, LineCap::Butt), (false, LineCap::Round)] {
|
||||
let d = lone_anchor_doc(true, closed, cap, &object, &stroke);
|
||||
let b = measure(&d, &d.layers[0].children().unwrap()[0]).unwrap();
|
||||
if closed && cap != LineCap::Butt {
|
||||
// The dot is 4 pt across.
|
||||
assert!(b.x1 >= dot.x + 2.0 - 1e-6 && b.y1 >= dot.y + 2.0 - 1e-6, "{what}, {cap:?}: {b:?} leaves out the dot at {dot:?}");
|
||||
} else {
|
||||
assert!(b.y1 < dot.y - 2.0, "{what}, closed {closed}, {cap:?}: {b:?} reaches toward a lone anchor that draws nothing");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A path of only a lone anchor is measured at that anchor, not at the origin, and an effect that
|
||||
/// paints around what the path draws (a drop shadow) is drawn over the dot a round cap draws there.
|
||||
#[test]
|
||||
fn a_path_of_a_lone_anchor_is_measured_at_its_dot() {
|
||||
use vectorcraft_doc::LineCap;
|
||||
let moved = Effect { id: "distort.transform".into(), params: json!({"moveH": 1}), visible: true };
|
||||
let d = lone_anchor_doc(false, true, LineCap::Round, std::slice::from_ref(&moved), &[]);
|
||||
let n = &d.layers[0].children().unwrap()[0];
|
||||
for (what, b) in [("visual", super::visual_bounds(&d.symbol_boxes(), n)), ("drawn", crate::drawn_bounds(&d, n))] {
|
||||
let b = b.unwrap();
|
||||
assert!(b.contains(Point::new(91.0, 90.0)) && !b.contains(Point::ZERO), "{what}: {b:?}");
|
||||
}
|
||||
let shadow = Effect { id: "stylize.dropShadow".into(), params: json!({"x": 0, "y": 6, "blur": 0}), visible: true };
|
||||
let d = lone_anchor_doc(true, true, LineCap::Round, &[moved, shadow], &[]);
|
||||
let img = render(&d);
|
||||
assert!(lum(img.pixel(91, 90)) < 200, "the dot is drawn");
|
||||
assert!(lum(img.pixel(91, 96)) < 600, "the dot's shadow is drawn: {:?}", img.pixel(91, 96));
|
||||
}
|
||||
|
||||
fn render(d: &Document) -> Rendered {
|
||||
Renderer::new().render(d, 100, 100, Affine::IDENTITY, &RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() })
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user