mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 10:51:11 +01:00
Size arrowhead selection bounds from the painted outlines
This commit is contained in:
1 parent
567eacdc5b
commit
86a4421090
5 files changed
+62
-52
No files matched your search
@@ -6,16 +6,55 @@
|
||||
//! the inset; with [`ArrowAlign::Extend`] the path keeps its length and the tip sits the inset
|
||||
//! past the end point. Heads added after the first ten are drawn in local units ([`local`]), as are
|
||||
//! custom heads, whose stroke ends at their own stop or else halfway along their length
|
||||
//! ([`vectorcraft_doc::CustomArrowhead::stop`]).
|
||||
//! ([`crate::CustomArrowhead::stop`]).
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use std::f64::consts::{FRAC_1_SQRT_2, FRAC_PI_2, PI};
|
||||
|
||||
use kurbo::{Affine, BezPath, ParamCurve, ParamCurveArclen, PathSeg, Point, Shape, Vec2};
|
||||
use vectorcraft_doc::{ArrowAlign, Arrowhead, StrokeLayer};
|
||||
use crate::{ArrowAlign, Arrowhead, LineCap, StrokeLayer};
|
||||
|
||||
use vectorcraft_geom::bez::{segments, subpaths, tangent, unit};
|
||||
|
||||
const ARCLEN_ACCURACY: f64 = 1e-6;
|
||||
|
||||
/// The geometry one stroke paints, in the coordinate space of its path.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StrokePieces<'a> {
|
||||
/// The centre line to stroke: the path with its ends trimmed under the arrowheads (borrowed
|
||||
/// when nothing is trimmed). Empty when the heads cover the whole path.
|
||||
pub line: Cow<'a, BezPath>,
|
||||
/// The arrowheads, start first. Closed ends get none.
|
||||
pub heads: Vec<Arrow>,
|
||||
}
|
||||
|
||||
/// The centre line and arrowheads of stroke `st` along `bp`.
|
||||
pub fn stroke_pieces<'a>(bp: &'a BezPath, st: &StrokeLayer) -> StrokePieces<'a> {
|
||||
if st.start_arrow.is_none() && st.end_arrow.is_none() {
|
||||
return StrokePieces { line: Cow::Borrowed(bp), heads: vec![] };
|
||||
}
|
||||
pieces(bp, st)
|
||||
}
|
||||
|
||||
/// How far a cap reaches past the end of a stroke of `width`.
|
||||
pub fn cap_extent(cap: LineCap, width: f64) -> f64 {
|
||||
match cap {
|
||||
LineCap::Butt => 0.0,
|
||||
LineCap::Round | LineCap::Square => width / 2.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Append `seg` to `out` (which must already have a current point at its start).
|
||||
pub fn push_seg(out: &mut BezPath, seg: &PathSeg) {
|
||||
match seg {
|
||||
PathSeg::Line(l) => out.line_to(l.p1),
|
||||
PathSeg::Quad(q) => out.quad_to(q.p1, q.p2),
|
||||
PathSeg::Cubic(c) => out.curve_to(c.p1, c.p2, c.p3),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
use super::{ARCLEN_ACCURACY, StrokePieces, cap_extent, push_seg, segments, subpaths, tangent, unit};
|
||||
|
||||
/// One arrowhead.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@@ -30,8 +69,8 @@ pub struct Arrow {
|
||||
pub inset: f64,
|
||||
}
|
||||
|
||||
/// [`super::stroke_pieces`] for a stroke with at least one arrowhead.
|
||||
pub(super) fn pieces<'a>(bp: &'a BezPath, st: &StrokeLayer) -> StrokePieces<'a> {
|
||||
/// [`stroke_pieces`] for a stroke with at least one arrowhead.
|
||||
fn pieces<'a>(bp: &'a BezPath, st: &StrokeLayer) -> StrokePieces<'a> {
|
||||
let subs = subpaths(bp);
|
||||
let els = bp.elements();
|
||||
// Heads go on the start of the first subpath and the end of the last one, when open.
|
||||
@@ -182,10 +221,10 @@ fn bar(hw: f64) -> f64 {
|
||||
/// Where the notch of the (open) arrow sits, as a fraction of the length.
|
||||
const NOTCH: f64 = 0.7;
|
||||
|
||||
/// How far [`super::stroke_pieces`] trims the line under each end's head: the head's inset from
|
||||
/// How far [`stroke_pieces`] trims the line under each end's head: the head's inset from
|
||||
/// the tip, along the path. Both are 0 without a head at that end, and in [`ArrowAlign::Extend`],
|
||||
/// where the stroke keeps its full length.
|
||||
pub(super) fn trims(st: &StrokeLayer) -> (f64, f64) {
|
||||
pub fn trims(st: &StrokeLayer) -> (f64, f64) {
|
||||
if st.arrow_align != ArrowAlign::Tip {
|
||||
return (0.0, 0.0);
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
pub mod appearance;
|
||||
pub mod arrow;
|
||||
pub mod assets;
|
||||
pub mod blend;
|
||||
pub mod cancel;
|
||||
|
||||
+10
-6
@@ -316,9 +316,10 @@ impl StrokeLayer {
|
||||
}
|
||||
}
|
||||
}
|
||||
for (at, _, end) in head_ends(self, bp) {
|
||||
let r = self.head_reach(end);
|
||||
out = out.union(Rect::from_center_size(at, (2.0 * r, 2.0 * r)));
|
||||
for head in crate::arrow::stroke_pieces(bp, self).heads {
|
||||
if !head.outline.elements().is_empty() {
|
||||
out = out.union(head.outline.bounding_box());
|
||||
}
|
||||
}
|
||||
even.map_or(out, |e| out.union(e.inflate(side_max, side_max)))
|
||||
}
|
||||
@@ -681,9 +682,12 @@ mod tests {
|
||||
})
|
||||
};
|
||||
let (small, big) = (at(100.0).bounds_on(&l, b), at(300.0).bounds_on(&l, b));
|
||||
assert!(big.x1 > small.x1 + 15.0 && big.y1 > small.y1 + 7.0, "{small:?} → {big:?}");
|
||||
// A 300% head on a 2 pt stroke is 24 pt long and wide: its tip, past the end, is inside.
|
||||
assert!(big.x1 >= 100.0 + 24.0 && big.y1 >= 12.0 && big.x0 == 0.0, "{big:?}");
|
||||
// The triangle straddles its path end in Extend mode and ends exactly on it in Tip mode.
|
||||
assert_eq!(small, Rect::new(0.0, -4.0, 104.0, 4.0));
|
||||
assert_eq!(big, Rect::new(0.0, -12.0, 112.0, 12.0));
|
||||
let mut tip = at(300.0);
|
||||
tip.arrow_align = ArrowAlign::Tip;
|
||||
assert_eq!(tip.bounds_on(&l, b), Rect::new(0.0, -12.0, 100.0, 12.0));
|
||||
// The start has no head.
|
||||
assert_eq!(at(300.0).head_reach(false), 0.0);
|
||||
}
|
||||
|
||||
@@ -11,19 +11,18 @@
|
||||
//! - [`for_writer`] and [`outline_region`]: strokes for the SVG/PDF writers and Outline Stroke;
|
||||
//! - [`gradient_slices`] and [`gradient_meshes`]: gradients laid along or across a stroke.
|
||||
|
||||
mod arrow;
|
||||
use vectorcraft_doc::arrow;
|
||||
mod dash;
|
||||
mod gradient;
|
||||
mod outline;
|
||||
mod width;
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use kurbo::{BezPath, ParamCurve, ParamCurveNearest, PathEl, PathSeg, Rect, Shape};
|
||||
use vectorcraft_doc::{LineCap, LineJoin, StrokeAlign, StrokeLayer};
|
||||
pub(crate) use vectorcraft_geom::bez::{kink, segments, subpaths, tangent, unit};
|
||||
|
||||
pub use arrow::Arrow;
|
||||
pub use arrow::{Arrow, StrokePieces, cap_extent, stroke_pieces};
|
||||
pub(crate) use arrow::push_seg;
|
||||
pub use dash::{Dashed, Dot, dash, dash_kept, dot_outline};
|
||||
pub use gradient::{Slice, WrittenSlices, gradient_meshes, gradient_slices, written_slices};
|
||||
pub use outline::{OUTLINE_TOL, Written, WrittenShape, for_writer, is_plain, outline_region};
|
||||
@@ -32,24 +31,6 @@ pub use width::width_outline;
|
||||
/// Arc-length accuracy for dashing and trimming (document points).
|
||||
const ARCLEN_ACCURACY: f64 = 1e-6;
|
||||
|
||||
/// The geometry one stroke paints, in the coordinate space of its path.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StrokePieces<'a> {
|
||||
/// The centre line to stroke: the path with its ends trimmed under the arrowheads (borrowed
|
||||
/// when nothing is trimmed). Empty when the heads cover the whole path.
|
||||
pub line: Cow<'a, BezPath>,
|
||||
/// The arrowheads, start first. Closed ends get none.
|
||||
pub heads: Vec<Arrow>,
|
||||
}
|
||||
|
||||
/// The centre line and arrowheads of stroke `st` along `bp`.
|
||||
pub fn stroke_pieces<'a>(bp: &'a BezPath, st: &StrokeLayer) -> StrokePieces<'a> {
|
||||
if st.start_arrow.is_none() && st.end_arrow.is_none() {
|
||||
return StrokePieces { line: Cow::Borrowed(bp), heads: vec![] };
|
||||
}
|
||||
arrow::pieces(bp, st)
|
||||
}
|
||||
|
||||
/// Does `bp` end with a `ClosePath`?
|
||||
pub fn is_closed(bp: &BezPath) -> bool {
|
||||
bp.elements().last().is_some_and(|e| matches!(e, PathEl::ClosePath))
|
||||
@@ -64,14 +45,6 @@ pub fn aligned_width(st: &StrokeLayer, closed: bool) -> f64 {
|
||||
}
|
||||
}
|
||||
|
||||
/// How far a cap reaches past the end of a stroke of `width`.
|
||||
pub fn cap_extent(cap: LineCap, width: f64) -> f64 {
|
||||
match cap {
|
||||
LineCap::Butt => 0.0,
|
||||
LineCap::Round | LineCap::Square => width / 2.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// The stroke style (cap, join, miter limit) of `st` at `width`, without dashes.
|
||||
pub fn style(st: &StrokeLayer, width: f64) -> kurbo::Stroke {
|
||||
kurbo::Stroke::new(width)
|
||||
@@ -221,15 +194,6 @@ fn reaches_edge(contour: &[PathEl], path: &BezPath, reach: f64) -> bool {
|
||||
kurbo::segments(contour.iter().copied()).any(|s| far(s.eval(0.0)) || far(s.eval(0.5)))
|
||||
}
|
||||
|
||||
/// Append `seg` to `out` (which must already have a current point at its start).
|
||||
pub(crate) fn push_seg(out: &mut BezPath, seg: &PathSeg) {
|
||||
match seg {
|
||||
PathSeg::Line(l) => out.line_to(l.p1),
|
||||
PathSeg::Quad(q) => out.quad_to(q.p1, q.p2),
|
||||
PathSeg::Cubic(c) => out.curve_to(c.p1, c.p2, c.p3),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use kurbo::{BezPath, PathEl, Point, Rect, Shape, Vec2};
|
||||
use vectorcraft_color::{Color, Paint};
|
||||
use vectorcraft_doc::{ArrowAlign, Arrowhead, Dash, LineCap, StrokeLayer, WidthProfile};
|
||||
|
||||
Reference in new issue
Block a user