mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 10:51:11 +01:00
Render: a thin stroke takes the corners of its own subpath
A corner in one subpath gave the stroke's join to the lines of a curve in another subpath that met at the same point, so a miter could be drawn there instead of a round join. The corners are now kept per subpath and each subpath of the outline reads only its own. Test: a curve's outline is the same with and without a second subpath that has a corner at the curve's start.
This commit is contained in:
1 parent
69ff74a63b
commit
f1ef1a4e39
2 files changed
+40
-13
No files matched your search
+19
-13
@@ -50,10 +50,10 @@ fn thin_line(line: &BezPath, st: &StrokeLayer, width: f64) -> Option<BezPath> {
|
||||
}
|
||||
|
||||
/// The points where `line` has a corner: where two of its segments meet with tangents that are not
|
||||
/// in line ([`SMOOTH`]), or where a segment has no direction there. Sorted by [`point_order`].
|
||||
/// [`draw_sub`] leaves these points where they are, so the lines a curve is drawn with have each
|
||||
/// corner at the same point.
|
||||
fn corners(line: &BezPath) -> Vec<Point> {
|
||||
/// in line ([`SMOOTH`]), or where a segment has no direction there. One list for each subpath, in
|
||||
/// order, each sorted by [`point_order`]. [`draw_sub`] leaves these points where they are, so the
|
||||
/// lines a curve is drawn with have each corner at the same point, and draws one subpath for each.
|
||||
fn corners(line: &BezPath) -> Vec<Vec<Point>> {
|
||||
let tangents = |seg: PathSeg| {
|
||||
let c = match seg {
|
||||
PathSeg::Line(l) => CubicBez::new(l.p0, l.p0, l.p1, l.p1),
|
||||
@@ -64,6 +64,7 @@ fn corners(line: &BezPath) -> Vec<Point> {
|
||||
let mut out = Vec::new();
|
||||
// A span of `f64::INFINITY` gives every segment no steps: only the segments are wanted.
|
||||
for sub in subpaths(line.elements(), f64::INFINITY, 1.0).unwrap_or_default() {
|
||||
let mut here = Vec::new();
|
||||
let n = sub.segs.len();
|
||||
let first = if sub.closed { 0 } else { 1 };
|
||||
for i in first..n {
|
||||
@@ -74,11 +75,12 @@ fn corners(line: &BezPath) -> Vec<Point> {
|
||||
_ => false,
|
||||
};
|
||||
if !smooth {
|
||||
out.push(b.start());
|
||||
here.push(b.start());
|
||||
}
|
||||
}
|
||||
here.sort_by(point_order);
|
||||
out.push(here);
|
||||
}
|
||||
out.sort_by(point_order);
|
||||
out
|
||||
}
|
||||
|
||||
@@ -90,19 +92,22 @@ fn point_order(a: &Point, b: &Point) -> std::cmp::Ordering {
|
||||
/// The outline of stroke `st` at `width` along `lines`, a path of lines only ([`thin_line`]), to fill
|
||||
/// with the non-zero rule. Each side of each line is offset by half the width. Where two lines meet
|
||||
/// at a small angle ([`THIN_SMOOTH`]) both sides go to where the offsets cross. Elsewhere the
|
||||
/// stroke's join is drawn at the path's `corners` ([`corners`]) and a round join where the lines a
|
||||
/// stroke's join is drawn at the corners of the same subpath of the path (`corners`, one list for
|
||||
/// each subpath of `lines`, [`corners`]) and a round join where the lines a
|
||||
/// curve is drawn with meet. An open subpath ends in the stroke's caps, with round parts drawn as
|
||||
/// lines within `fine` of their arcs. `None` when a subpath has fewer than two distinct points or a
|
||||
/// point that isn't finite, or is closed and no wider or taller than the stroke: those take
|
||||
/// [`effects::stroke::line_outline`].
|
||||
fn thin_band(lines: &BezPath, corners: &[Point], st: &StrokeLayer, width: f64, fine: f64) -> Option<BezPath> {
|
||||
fn thin_band(lines: &BezPath, corners: &[Vec<Point>], st: &StrokeLayer, width: f64, fine: f64) -> Option<BezPath> {
|
||||
let h = width / 2.0;
|
||||
// The angle between two points of an arc of radius `h` drawn as lines within `fine` of it.
|
||||
let step = 2.0 * (1.0 - (fine / h).min(1.0)).acos().max(1e-3);
|
||||
let mut out = BezPath::new();
|
||||
let mut pts: Vec<Point> = Vec::new();
|
||||
let mut closed = false;
|
||||
let flush = |pts: &mut Vec<Point>, closed: bool, out: &mut BezPath| -> Option<()> {
|
||||
// The subpath being read; `None` before the first move.
|
||||
let mut sub: Option<usize> = None;
|
||||
let flush = |pts: &mut Vec<Point>, closed: bool, sub: Option<usize>, out: &mut BezPath| -> Option<()> {
|
||||
if pts.is_empty() {
|
||||
return Some(());
|
||||
}
|
||||
@@ -119,14 +124,15 @@ fn thin_band(lines: &BezPath, corners: &[Point], st: &StrokeLayer, width: f64, f
|
||||
return None;
|
||||
}
|
||||
}
|
||||
band_sub(out, pts, corners, closed, st, h, step);
|
||||
band_sub(out, pts, corners.get(sub?)?, closed, st, h, step);
|
||||
pts.clear();
|
||||
Some(())
|
||||
};
|
||||
for el in lines.elements() {
|
||||
match *el {
|
||||
PathEl::MoveTo(p) => {
|
||||
flush(&mut pts, closed, &mut out)?;
|
||||
flush(&mut pts, closed, sub, &mut out)?;
|
||||
sub = Some(sub.map_or(0, |s| s + 1));
|
||||
closed = false;
|
||||
pts.push(p);
|
||||
}
|
||||
@@ -139,7 +145,7 @@ fn thin_band(lines: &BezPath, corners: &[Point], st: &StrokeLayer, width: f64, f
|
||||
PathEl::QuadTo(..) | PathEl::CurveTo(..) => return None,
|
||||
}
|
||||
}
|
||||
flush(&mut pts, closed, &mut out)?;
|
||||
flush(&mut pts, closed, sub, &mut out)?;
|
||||
Some(out)
|
||||
}
|
||||
|
||||
@@ -147,7 +153,7 @@ fn thin_band(lines: &BezPath, corners: &[Point], st: &StrokeLayer, width: f64, f
|
||||
/// repeating the first when `closed`) to `out`: for an open subpath one contour, its left side,
|
||||
/// the end cap, its right side backward and the start cap; for a closed one its left side and its
|
||||
/// right side backward, as two contours. `h` is half the width and `step` the angle between the
|
||||
/// points of a round join or cap. A sharp turn takes the stroke's join at one of the path's
|
||||
/// points of a round join or cap. A sharp turn takes the stroke's join at one of this subpath's
|
||||
/// `corners` (sorted by [`point_order`]) and a round join elsewhere.
|
||||
fn band_sub(out: &mut BezPath, pts: &[Point], corners: &[Point], closed: bool, st: &StrokeLayer, h: f64, step: f64) {
|
||||
use vectorcraft_doc::{LineCap, LineJoin};
|
||||
|
||||
@@ -254,6 +254,27 @@ fn thin_strokes_along_long_handles_cover_the_curve_outline() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A corner of one subpath does not change the join where the lines of another subpath's curve
|
||||
/// meet at the same point: the first subpath's outline is the same with or without the second.
|
||||
#[test]
|
||||
fn thin_strokes_take_corners_from_their_own_subpath() {
|
||||
let w = 0.5;
|
||||
let mut st = StrokeLayer::new(Paint::solid(Color::BLACK), w);
|
||||
st.join = LineJoin::Miter;
|
||||
st.miter_limit = 10.0;
|
||||
let mut alone = BezPath::new();
|
||||
alone.move_to((-1.0, 0.0));
|
||||
alone.line_to((0.0, 0.0));
|
||||
alone.curve_to((0.000001, 0.0), (-1.0, 0.5), (-2.0, 1.0));
|
||||
let mut both = alone.clone();
|
||||
both.move_to((-10.0, -10.0));
|
||||
both.line_to((0.0, 0.0));
|
||||
both.line_to((-10.0, 10.0));
|
||||
let one = raster_stroke_outline(&alone, &st, w, 0.25);
|
||||
let two = raster_stroke_outline(&both, &st, w, 0.25);
|
||||
assert_eq!(two.elements().get(..one.elements().len()), Some(one.elements()), "the second subpath changed the first one's outline");
|
||||
}
|
||||
|
||||
/// Independently integrate coverage in radial/angle bins of an original notched ring. Checking
|
||||
/// only the outline geometry misses raster flattening that cancels its two nearby contours.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user