mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 16:42:52 +00:00
Tool overlays: draw every dash of a dashed piece as line segments
dashes_from drew the cut first dash and a line shorter than the rest of a dash as one path. The tessellator snaps horizontal and vertical line segments to pixels but not paths, so those dashes could look softer than the rest. Each line of them is now its own line segment, as Shape::dashed_line draws a dash, and a line of no length is skipped.
This commit is contained in:
@@ -428,7 +428,7 @@ pub(super) fn dashes_from(line: &[Pos2], s: Stroke, from: f64, out: &mut Vec<Sha
|
||||
let cut = w[0] + (w[1] - w[0]) * (left / len);
|
||||
let mut dash = line[..=i].to_vec();
|
||||
dash.push(cut);
|
||||
out.push(Shape::line(dash, s));
|
||||
segments(&dash, s, out);
|
||||
let mut rest = vec![cut];
|
||||
rest.extend_from_slice(&line[i + 1..]);
|
||||
Shape::dashed_line_many_with_offset(&rest, s, &[DASH], &[GAP], GAP, out);
|
||||
@@ -436,7 +436,13 @@ pub(super) fn dashes_from(line: &[Pos2], s: Stroke, from: f64, out: &mut Vec<Sha
|
||||
}
|
||||
left -= len;
|
||||
}
|
||||
out.push(Shape::line(line.to_vec(), s));
|
||||
segments(line, s, out);
|
||||
}
|
||||
|
||||
/// Add to `out` each line of `line` that has a length as its own [`Shape::LineSegment`], as
|
||||
/// [`Shape::dashed_line`] draws a dash.
|
||||
fn segments(line: &[Pos2], s: Stroke, out: &mut Vec<Shape>) {
|
||||
out.extend(line.windows(2).filter(|w| w[0] != w[1]).map(|w| Shape::line_segment([w[0], w[1]], s)));
|
||||
}
|
||||
|
||||
impl SharedPaths {
|
||||
|
||||
@@ -201,6 +201,14 @@ fn a_line_dashed_in_pieces_dashes_as_the_whole_line_does() {
|
||||
let (want, got) = (covered(&one_go), covered(&pieces));
|
||||
assert_eq!(want.len(), got.len(), "{want:?} {got:?}");
|
||||
assert!(want.iter().zip(&got).all(|(w, g)| (w.0 - g.0).abs() < 1e-3 && (w.1 - g.1).abs() < 1e-3), "{want:?} {got:?}");
|
||||
// Every dash is drawn as line segments with a length, as `Shape::dashed_line` draws them: the
|
||||
// cut first dash, a dash cut on a vertex (at 4) and a line shorter than the rest of a dash too.
|
||||
let mut ends = vec![];
|
||||
overlays::dashes_from(&along(&[0.0, 4.0, 10.0]), s, 0.0, &mut ends);
|
||||
overlays::dashes_from(&along(&[0.0, 1.0, 2.0]), s, 1.0, &mut ends);
|
||||
for shape in one_go.iter().chain(&pieces).chain(&ends) {
|
||||
assert!(matches!(shape, Shape::LineSegment { points: [a, b], .. } if a != b), "{shape:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user