use alloc::vec;
use alloc::vec::Vec;
use crate::path::{Path2D, PathCommand};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineCap {
#[default]
Butt,
Square,
Round,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineJoin {
#[default]
Miter,
Bevel,
Round,
}
pub const DEFAULT_MITER_LIMIT: f32 = 4.0;
const ARC_STEPS: usize = 12;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StrokeStyle {
pub width: f32,
pub cap: LineCap,
pub join: LineJoin,
pub miter_limit: f32,
}
impl Default for StrokeStyle {
fn default() -> Self {
StrokeStyle {
width: 1.0,
cap: LineCap::Butt,
join: LineJoin::Miter,
miter_limit: DEFAULT_MITER_LIMIT,
}
}
}
impl StrokeStyle {
#[must_use]
pub const fn new(width: f32) -> Self {
StrokeStyle {
width,
cap: LineCap::Butt,
join: LineJoin::Miter,
miter_limit: DEFAULT_MITER_LIMIT,
}
}
#[must_use]
pub const fn with_cap(mut self, cap: LineCap) -> Self {
self.cap = cap;
self
}
#[must_use]
pub const fn with_join(mut self, join: LineJoin) -> Self {
self.join = join;
self
}
#[must_use]
pub const fn with_miter_limit(mut self, limit: f32) -> Self {
self.miter_limit = limit;
self
}
}
#[must_use]
pub fn stroke_path(path: &Path2D, width: f32, style: StrokeStyle) -> Path2D {
let mut out = Path2D::new();
let hw = width * 0.5;
if !hw.is_finite() || hw <= 0.0 {
return out;
}
for poly in path.flatten_polylines(hw * 0.05) {
let pts: Vec<(f32, f32)> = match poly.points.split_last() {
Some((last, head)) if head.len() > 1 && poly.points.first() == Some(last) => {
head.to_vec()
}
_ => poly.points,
};
if pts.len() < 2 {
continue;
}
let mut seg = StrokeEmitter {
out: &mut out,
hw,
style,
};
seg.emit(&pts, poly.closed);
}
out
}
struct StrokeEmitter<'a> {
out: &'a mut Path2D,
hw: f32,
style: StrokeStyle,
}
impl StrokeEmitter<'_> {
fn emit(&mut self, pts: &[(f32, f32)], closed: bool) {
let n = pts.len();
let last = if closed { n } else { n - 1 };
let at = |i: usize| -> Option<(f32, f32)> { pts.get(i % n).copied() };
for i in 0..last {
let (Some(a), Some(b)) = (at(i), at(i + 1)) else {
continue;
};
if a == b {
continue;
}
let (na, nb) = (normal(a, b, self.hw), normal(a, b, -self.hw));
self.quad(
(a.0 + na.0, a.1 + na.1),
(b.0 + na.0, b.1 + na.1),
(b.0 + nb.0, b.1 + nb.1),
(a.0 + nb.0, a.1 + nb.1),
);
}
let first_join = if closed { 0 } else { 1 };
let last_join = if closed { n } else { n - 1 };
for i in first_join..last_join {
let (Some(prev), Some(here), Some(next)) = (at(i + n - 1), at(i), at(i + 1)) else {
continue;
};
self.join(prev, here, next);
}
if !closed {
match self.style.cap {
LineCap::Butt => {}
LineCap::Square | LineCap::Round => {
let round = matches!(self.style.cap, LineCap::Round);
let (Some(first), Some(second)) = (pts.first().copied(), pts.get(1).copied())
else {
return;
};
let (Some(last), Some(prev)) = (pts.last().copied(), pts.get(n - 2).copied())
else {
return;
};
if round {
self.cap_round(first, second);
self.cap_round(last, prev);
} else {
self.cap_square(first, second);
self.cap_square(last, prev);
}
}
}
}
}
fn quad(&mut self, a: (f32, f32), b: (f32, f32), c: (f32, f32), d: (f32, f32)) {
self.out.push(PathCommand::MoveTo(a.0, a.1));
self.out.push(PathCommand::LineTo(b.0, b.1));
self.out.push(PathCommand::LineTo(c.0, c.1));
self.out.push(PathCommand::LineTo(d.0, d.1));
self.out.push(PathCommand::Close);
}
fn triangle(&mut self, a: (f32, f32), b: (f32, f32), c: (f32, f32)) {
self.out.push(PathCommand::MoveTo(a.0, a.1));
self.out.push(PathCommand::LineTo(b.0, b.1));
self.out.push(PathCommand::LineTo(c.0, c.1));
self.out.push(PathCommand::Close);
}
fn join(&mut self, prev: (f32, f32), here: (f32, f32), next: (f32, f32)) {
let (Some(in_dir), Some(out_dir)) = (
norm((here.0 - prev.0, here.1 - prev.1)),
norm((next.0 - here.0, next.1 - here.1)),
) else {
return;
};
let cross = in_dir.0 * out_dir.1 - in_dir.1 * out_dir.0;
if cross.abs() < 1e-9 {
return;
}
let n_in = (in_dir.1 * self.hw, -in_dir.0 * self.hw);
let n_out = (out_dir.1 * self.hw, -out_dir.0 * self.hw);
let side_in = (here.0 + n_in.0, here.1 + n_in.1);
let side_out = (here.0 + n_out.0, here.1 + n_out.1);
match self.style.join {
LineJoin::Bevel => self.triangle(here, side_in, side_out),
LineJoin::Round => self.fan(here, side_in, side_out, cross),
LineJoin::Miter => {
let dot = in_dir.0 * out_dir.0 + in_dir.1 * out_dir.1;
let half_cos = libm::sqrtf((1.0 + dot) * 0.5).max(1e-6);
let ratio = 1.0 / half_cos;
if ratio > self.style.miter_limit.max(1.0) {
self.triangle(here, side_in, side_out);
} else {
let Some(n) = norm((n_in.0 + n_out.0, n_in.1 + n_out.1)) else {
self.triangle(here, side_in, side_out);
return;
};
let miter = (
here.0 + n.0 * self.hw * ratio,
here.1 + n.1 * self.hw * ratio,
);
self.quad(here, side_in, miter, side_out);
}
}
}
}
fn fan(&mut self, centre: (f32, f32), from: (f32, f32), to: (f32, f32), sign: f32) {
let a0 = libm::atan2f(from.1 - centre.1, from.0 - centre.0);
let a1 = libm::atan2f(to.1 - centre.1, to.0 - centre.0);
let mut delta = a1 - a0;
while delta <= -core::f32::consts::PI {
delta += 2.0 * core::f32::consts::PI;
}
while delta > core::f32::consts::PI {
delta -= 2.0 * core::f32::consts::PI;
}
let steps = ARC_STEPS.max(2);
self.out.push(PathCommand::MoveTo(centre.0, centre.1));
for i in 0..=steps {
let t = a0 + delta * (i as f32) / (steps as f32);
let (s, c) = (libm::sinf(t), libm::cosf(t));
let p = (centre.0 + c * self.hw, centre.1 + s * self.hw);
self.out.push(PathCommand::LineTo(p.0, p.1));
}
self.out.push(PathCommand::Close);
let _ = sign;
}
fn cap_square(&mut self, end: (f32, f32), toward: (f32, f32)) {
let Some(d) = norm((end.0 - toward.0, end.1 - toward.1)) else {
return;
};
let n = (d.1 * self.hw, -d.0 * self.hw);
let a = (end.0 + n.0, end.1 + n.1);
let b = (end.0 - n.0, end.1 - n.1);
let far_a = (a.0 + d.0 * self.hw, a.1 + d.1 * self.hw);
let far_b = (b.0 + d.0 * self.hw, b.1 + d.1 * self.hw);
self.quad(a, far_a, far_b, b);
}
fn cap_round(&mut self, end: (f32, f32), toward: (f32, f32)) {
let Some(d) = norm((end.0 - toward.0, end.1 - toward.1)) else {
return;
};
let n = (d.1 * self.hw, -d.0 * self.hw);
let a = (end.0 + n.0, end.1 + n.1);
let b = (end.0 - n.0, end.1 - n.1);
self.arc(end, a, b);
}
fn arc(&mut self, centre: (f32, f32), from: (f32, f32), to: (f32, f32)) {
let a0 = libm::atan2f(from.1 - centre.1, from.0 - centre.0);
let a1 = libm::atan2f(to.1 - centre.1, to.0 - centre.0);
let mut delta = a1 - a0;
while delta <= 0.0 {
delta += 2.0 * core::f32::consts::PI;
}
while delta > 2.0 * core::f32::consts::PI {
delta -= 2.0 * core::f32::consts::PI;
}
let steps = ARC_STEPS.max(2);
self.out.push(PathCommand::MoveTo(centre.0, centre.1));
for i in 0..=steps {
let t = a0 + delta * (i as f32) / (steps as f32);
let (s, c) = (libm::sinf(t), libm::cosf(t));
let p = (centre.0 + c * self.hw, centre.1 + s * self.hw);
self.out.push(PathCommand::LineTo(p.0, p.1));
}
self.out.push(PathCommand::Close);
}
}
fn normal(from: (f32, f32), to: (f32, f32), side: f32) -> (f32, f32) {
let dx = to.0 - from.0;
let dy = to.1 - from.1;
let len = libm::sqrtf(dx * dx + dy * dy);
if len <= 1e-9 {
return (0.0, 0.0);
}
let ux = dx / len;
let uy = dy / len;
(uy * side, -ux * side)
}
fn norm(d: (f32, f32)) -> Option<(f32, f32)> {
let len = libm::sqrtf(d.0 * d.0 + d.1 * d.1);
if len <= 1e-9 || !len.is_finite() {
return None;
}
Some((d.0 / len, d.1 / len))
}
#[must_use]
pub(crate) fn reverse_commands(commands: &[PathCommand]) -> Vec<PathCommand> {
struct Seg {
kind: u8,
c1: (f32, f32),
c2: (f32, f32),
to: (f32, f32),
}
type Sub = (Vec<(f32, f32)>, Vec<Seg>, bool);
type Open = (Vec<(f32, f32)>, Vec<Seg>);
let mut out: Vec<PathCommand> = Vec::with_capacity(commands.len());
let mut subs: Vec<Sub> = Vec::new();
let mut cur: Option<Open> = None;
for cmd in commands {
match *cmd {
PathCommand::MoveTo(x, y) => {
if let Some(sp) = cur.take() {
subs.push((sp.0, sp.1, false));
}
cur = Some((vec![(x, y)], Vec::new()));
}
PathCommand::LineTo(x, y) => {
if let Some(sp) = cur.as_mut() {
sp.1.push(Seg {
kind: 1,
c1: (0.0, 0.0),
c2: (0.0, 0.0),
to: (x, y),
});
sp.0.push((x, y));
}
}
PathCommand::QuadTo(cx, cy, x, y) => {
if let Some(sp) = cur.as_mut() {
sp.1.push(Seg {
kind: 2,
c1: (cx, cy),
c2: (0.0, 0.0),
to: (x, y),
});
sp.0.push((x, y));
}
}
PathCommand::CubicTo(c1x, c1y, c2x, c2y, x, y) => {
if let Some(sp) = cur.as_mut() {
sp.1.push(Seg {
kind: 3,
c1: (c1x, c1y),
c2: (c2x, c2y),
to: (x, y),
});
sp.0.push((x, y));
}
}
PathCommand::Close => {
if let Some(sp) = cur.take() {
subs.push((sp.0, sp.1, true));
}
}
}
}
if let Some(sp) = cur.take() {
subs.push((sp.0, sp.1, false));
}
for (points, segs, closed) in subs {
let Some(first) = points.first().copied() else {
continue;
};
if segs.is_empty() {
out.push(PathCommand::MoveTo(first.0, first.1));
if closed {
out.push(PathCommand::Close);
}
continue;
}
let begin = segs.last().map_or(first, |s| s.to);
out.push(PathCommand::MoveTo(begin.0, begin.1));
for (i, s) in segs.iter().enumerate().rev() {
let to = match i.checked_sub(1).and_then(|k| segs.get(k)) {
Some(prev) => prev.to,
None => first,
};
match s.kind {
1 => out.push(PathCommand::LineTo(to.0, to.1)),
2 => out.push(PathCommand::QuadTo(s.c1.0, s.c1.1, to.0, to.1)),
_ => out.push(PathCommand::CubicTo(
s.c2.0, s.c2.1, s.c1.0, s.c1.1, to.0, to.1,
)),
}
}
if closed {
out.push(PathCommand::Close);
}
}
out
}
#[cfg(test)]
mod tests {
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing,
clippy::float_cmp
)]
use super::{stroke_path, LineCap, LineJoin, StrokeStyle};
use crate::path::{PathBuilder, PathCommand};
use crate::FillRule;
fn close(a: f32, b: f32) -> bool {
(a - b).abs() < 1e-3
}
fn ink(path: &crate::Path2D, size: u32) -> f32 {
let mask = crate::fill_path(path, FillRule::NonZero, size, size).expect("fills");
mask.iter().map(|&v| f32::from(v)).sum::<f32>() / (255.0 * (size * size) as f32)
}
fn line(from: (f32, f32), to: (f32, f32)) -> crate::Path2D {
let mut b = PathBuilder::new();
b.move_to(from.0, from.1);
b.line_to(to.0, to.1);
b.build()
}
#[test]
fn zero_width_stroke_has_no_region() {
let p = line((10.0, 10.0), (50.0, 10.0));
for w in [0.0, -1.0, -0.001] {
let s = stroke_path(&p, w, StrokeStyle::new(w));
assert!(s.is_empty(), "width {w} produced {} commands", s.len());
}
assert!(stroke_path(&p, f32::NAN, StrokeStyle::default()).is_empty());
assert!(stroke_path(&p, f32::INFINITY, StrokeStyle::default()).is_empty());
}
#[test]
fn hairline_is_a_thin_but_nonzero_region() {
let p = line((10.0, 10.0), (50.0, 10.0));
let s = stroke_path(&p, 0.01, StrokeStyle::new(0.01));
assert!(!s.is_empty());
let b = s.bbox().expect("bbox");
assert!(close(b.height(), 0.01), "height {}", b.height());
assert!(close(b.width(), 40.0));
}
#[test]
fn butt_cap_stops_at_the_endpoint() {
let p = line((20.0, 50.0), (80.0, 50.0));
let s = stroke_path(&p, 10.0, StrokeStyle::new(10.0).with_cap(LineCap::Butt));
let b = s.bbox().expect("bbox");
assert_eq!(b.min_x, 20.0);
assert_eq!(b.max_x, 80.0);
assert_eq!(b.min_y, 45.0);
assert_eq!(b.max_y, 55.0);
}
#[test]
fn square_cap_extends_by_half_the_width() {
let p = line((20.0, 50.0), (80.0, 50.0));
let s = stroke_path(&p, 10.0, StrokeStyle::new(10.0).with_cap(LineCap::Square));
let b = s.bbox().expect("bbox");
assert_eq!(b.min_x, 15.0);
assert_eq!(b.max_x, 85.0);
assert_eq!(b.height(), 10.0);
}
#[test]
fn round_cap_extends_by_exactly_half_the_width() {
let p = line((30.0, 50.0), (70.0, 50.0));
let s = stroke_path(&p, 10.0, StrokeStyle::new(10.0).with_cap(LineCap::Round));
let b = s.bbox().expect("bbox");
assert!(close(b.min_x, 25.0), "min_x {}", b.min_x);
assert!(close(b.max_x, 75.0), "max_x {}", b.max_x);
assert_eq!(b.height(), 10.0, "the cap is as wide as the stroke");
let sq = stroke_path(&p, 10.0, StrokeStyle::new(10.0).with_cap(LineCap::Square));
assert_eq!(sq.bbox(), Some(b));
}
#[test]
fn diagonal_stroke_thickness_is_the_width() {
let p = line((0.0, 0.0), (100.0, 100.0));
let s = stroke_path(&p, 10.0, StrokeStyle::new(10.0));
let b = s.bbox().expect("bbox");
let expected = 5.0 / core::f32::consts::SQRT_2;
assert!(
close(b.min_x, -expected) && close(b.min_y, -expected),
"{b:?}"
);
assert!(close(b.max_x, 100.0 + expected), "{b:?}");
assert!(close(b.max_y, 100.0 + expected), "{b:?}");
}
#[test]
fn miter_join_extends_a_right_angle_to_the_miter_limit() {
let mut b = PathBuilder::new();
b.move_to(0.0, 0.0);
b.line_to(50.0, 0.0);
b.line_to(50.0, 50.0);
let p = b.build();
let s = stroke_path(&p, 10.0, StrokeStyle::new(10.0).with_join(LineJoin::Miter));
let bb = s.bbox().expect("bbox");
assert!(close(bb.max_x, 55.0), "{bb:?}");
assert!(close(bb.min_y, -5.0), "{bb:?}");
assert!(close(bb.min_x, 0.0) && close(bb.max_y, 50.0), "{bb:?}");
let bevel = stroke_path(&p, 10.0, StrokeStyle::new(10.0).with_join(LineJoin::Bevel));
let bvb = bevel.bbox().expect("bbox");
assert!(close(bvb.max_x, 55.0) && close(bvb.min_y, -5.0), "{bvb:?}");
let miter_area = s.polygon_area();
let bevel_area = bevel.polygon_area();
assert!(
miter_area > bevel_area,
"miter {miter_area} vs bevel {bevel_area}"
);
assert!(
(miter_area - bevel_area - 12.5).abs() < 0.5,
"gap {}",
miter_area - bevel_area
);
}
#[test]
fn miter_limit_exceeded_falls_back_to_a_bevel() {
let mut b = PathBuilder::new();
b.move_to(0.0, 0.0);
b.line_to(100.0, 0.0);
b.line_to(50.0, 1.0); let p = b.build();
let bevel = stroke_path(&p, 4.0, StrokeStyle::new(4.0).with_join(LineJoin::Bevel));
let tight = stroke_path(
&p,
4.0,
StrokeStyle::new(4.0)
.with_join(LineJoin::Miter)
.with_miter_limit(1.5),
);
assert_eq!(
tight.commands(),
bevel.commands(),
"a 1.5 limit bevels this spike"
);
let loose = stroke_path(&p, 4.0, StrokeStyle::new(4.0).with_miter_limit(1000.0));
assert_ne!(loose.commands(), bevel.commands());
}
#[test]
fn bevel_join_matches_miter_below_the_limit() {
let mut b = PathBuilder::new();
b.move_to(0.0, 0.0);
b.line_to(50.0, 0.0);
b.line_to(100.0, 20.0);
let p = b.build();
let miter = stroke_path(&p, 6.0, StrokeStyle::new(6.0).with_join(LineJoin::Miter));
let bevel = stroke_path(&p, 6.0, StrokeStyle::new(6.0).with_join(LineJoin::Bevel));
let a = ink(&miter, 128);
let c = ink(&bevel, 128);
assert!((a - c).abs() < 0.005, "miter {a} vs bevel {c}");
}
#[test]
fn joins_cover_different_amounts_at_a_corner() {
let mut b = PathBuilder::new();
b.move_to(30.0, 30.0);
b.line_to(80.0, 30.0);
b.line_to(80.0, 80.0);
let p = b.build();
let miter = stroke_path(&p, 10.0, StrokeStyle::new(10.0).with_join(LineJoin::Miter));
let round = stroke_path(&p, 10.0, StrokeStyle::new(10.0).with_join(LineJoin::Round));
let bevel = stroke_path(&p, 10.0, StrokeStyle::new(10.0).with_join(LineJoin::Bevel));
assert!(ink(&round, 256) > ink(&bevel, 256), "round vs bevel");
assert!(ink(&miter, 256) > ink(&bevel, 256), "miter vs bevel");
let miter_area = miter.polygon_area();
assert!(
(round.polygon_area() - miter_area).abs() > 1.0,
"round and miter differ"
);
let round_join = round.polygon_area() - 1000.0;
let quarter_disc = core::f32::consts::FRAC_PI_4 * 25.0;
assert!(
(round_join - quarter_disc).abs() < 0.5,
"round join area {round_join} vs quarter-disc {quarter_disc}"
);
let bevel_join = bevel.polygon_area() - 1000.0;
assert!(
(bevel_join - 12.5).abs() < 0.5,
"bevel join area {bevel_join} vs triangle 12.5"
);
}
#[test]
fn collinear_join_adds_nothing() {
let mut b = PathBuilder::new();
b.move_to(0.0, 0.0);
b.line_to(50.0, 0.0);
b.line_to(100.0, 0.0);
let p = b.build();
let s = stroke_path(&p, 10.0, StrokeStyle::new(10.0));
let bb = s.bbox().expect("bbox");
assert_eq!(bb.min_y, -5.0);
assert_eq!(bb.max_y, 5.0);
}
#[test]
fn closed_subpath_strokes_the_whole_ring() {
let mut b = PathBuilder::new();
b.rect(20.0, 20.0, 60.0, 60.0);
let p = b.build();
let s = stroke_path(&p, 6.0, StrokeStyle::new(6.0));
let bb = s.bbox().expect("bbox");
assert!(bb.min_x <= 17.0 && bb.max_x >= 83.0, "{bb:?}");
let round = stroke_path(&p, 6.0, StrokeStyle::new(6.0).with_join(LineJoin::Round));
let rb = round.bbox().expect("bbox");
assert!(close(rb.min_x, 17.0) && close(rb.max_x, 83.0), "{rb:?}");
}
#[test]
fn stroke_ink_is_length_times_width() {
let p = line((20.0, 50.0), (120.0, 50.0));
let s = stroke_path(&p, 10.0, StrokeStyle::new(10.0));
let area = s.polygon_area();
assert!((area - 1000.0).abs() < 2.0, "polygon area {area} vs 1000");
let mut b = PathBuilder::new();
b.extend(&s.translated(0.0, 0.0));
let total = ink(&b.build(), 512);
let expect = 1000.0 / (512.0 * 512.0);
assert!(
(total - expect).abs() < expect * 0.01,
"mask ink {total} vs {expect}"
);
}
#[test]
fn zero_length_segment_contributes_nothing() {
let mut b = PathBuilder::new();
b.move_to(10.0, 10.0);
b.line_to(10.0, 10.0);
b.line_to(50.0, 10.0);
let p = b.build();
let s = stroke_path(&p, 10.0, StrokeStyle::new(10.0));
let bb = s.bbox().expect("bbox");
assert_eq!(bb.min_x, 10.0, "the zero segment adds no cap");
assert_eq!(bb.max_x, 50.0);
}
#[test]
fn degenerate_paths_do_not_panic() {
assert!(stroke_path(&crate::Path2D::new(), 5.0, StrokeStyle::default()).is_empty());
let mut b = PathBuilder::new();
b.move_to(5.0, 5.0);
let p = b.build();
assert!(stroke_path(&p, 5.0, StrokeStyle::default()).is_empty());
let mut b = PathBuilder::new();
b.move_to(5.0, 5.0);
b.line_to(5.0, 5.0);
b.close();
assert!(stroke_path(&b.build(), 5.0, StrokeStyle::default()).is_empty());
}
#[test]
fn style_builders() {
let s = StrokeStyle::default();
assert_eq!(s.width, 1.0);
assert_eq!(s.cap, LineCap::Butt);
assert_eq!(s.join, LineJoin::Miter);
assert_eq!(s.miter_limit, super::DEFAULT_MITER_LIMIT);
let s = StrokeStyle::new(3.0)
.with_cap(LineCap::Round)
.with_join(LineJoin::Round)
.with_miter_limit(2.0);
assert_eq!(s.width, 3.0);
assert_eq!(s.cap, LineCap::Round);
assert_eq!(s.join, LineJoin::Round);
assert_eq!(s.miter_limit, 2.0);
}
#[test]
fn reverse_commands_helper() {
let mut b = PathBuilder::new();
b.move_to(0.0, 0.0);
b.line_to(10.0, 0.0);
b.line_to(10.0, 10.0);
b.close();
let before = b.build();
let reversed = super::reverse_commands(before.commands());
let mut after = before.clone();
after.reverse();
assert_eq!(reversed, after.commands());
assert!(super::reverse_commands(&[]).is_empty());
}
#[test]
fn emitted_contours_all_close() {
let mut b = PathBuilder::new();
b.move_to(10.0, 10.0);
b.line_to(50.0, 10.0);
b.line_to(50.0, 50.0);
let s = stroke_path(&b.build(), 8.0, StrokeStyle::new(8.0));
assert!(!s.commands().is_empty());
assert!(
matches!(s.commands().last(), Some(PathCommand::Close)),
"last command {:?}",
s.commands().last()
);
let mut starts = 0usize;
let mut closes = 0usize;
for c in s.commands() {
match c {
PathCommand::MoveTo(..) => starts += 1,
PathCommand::Close => closes += 1,
_ => {}
}
}
assert_eq!(starts, closes, "every MoveTo has a Close");
}
#[test]
fn many_segment_path_strokes_without_panicking() {
let mut b = PathBuilder::new();
b.move_to(0.0, 0.0);
for i in 1..200 {
b.line_to(
f32::from(i as u16) * 0.5,
if i % 3 == 0 { 5.0 } else { 0.0 },
);
}
let s = stroke_path(&b.build(), 2.0, StrokeStyle::new(2.0));
assert!(!s.is_empty());
assert!(s.bbox().is_some());
}
#[test]
fn non_finite_coordinates_do_not_panic() {
let mut b = PathBuilder::new();
b.move_to(0.0, 0.0);
b.line_to(f32::NAN, 10.0);
b.line_to(50.0, 10.0);
let s = stroke_path(&b.build(), 4.0, StrokeStyle::new(4.0));
if let Some(bb) = s.bbox() {
assert!(bb.min_x.is_finite() || bb.min_x == f32::INFINITY);
}
}
}