use alloc::vec;
use alloc::vec::Vec;
use crate::affine::Affine2D;
use crate::error::ShapeError;
use crate::stroke::{LineCap, LineJoin, StrokeStyle};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PathCommand {
MoveTo(f32, f32),
LineTo(f32, f32),
QuadTo(f32, f32, f32, f32),
CubicTo(f32, f32, f32, f32, f32, f32),
Close,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Path2D {
commands: Vec<PathCommand>,
}
impl Path2D {
#[must_use]
pub fn new() -> Self {
Path2D {
commands: Vec::new(),
}
}
#[must_use]
pub fn commands(&self) -> &[PathCommand] {
&self.commands
}
#[must_use]
pub fn len(&self) -> usize {
self.commands.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.commands.is_empty()
}
pub fn push(&mut self, cmd: PathCommand) -> &mut Self {
self.commands.push(cmd);
self
}
#[must_use]
pub fn bbox(&self) -> Option<Rect> {
let mut b = Rect::empty();
let mut cur = (0.0f32, 0.0f32);
let mut any = false;
for cmd in &self.commands {
match *cmd {
PathCommand::MoveTo(x, y) => {
b.extend(x, y);
cur = (x, y);
any = true;
}
PathCommand::LineTo(x, y) => {
b.extend(x, y);
cur = (x, y);
any = true;
}
PathCommand::QuadTo(cx, cy, x, y) => {
b.extend(x, y);
let p = [cur, (cx, cy), (x, y)];
for t in quad_roots(p[0].0, p[1].0, p[2].0) {
b.extend(quad_x(p, t), quad_y(p, t));
}
for t in quad_roots(p[0].1, p[1].1, p[2].1) {
b.extend(quad_x(p, t), quad_y(p, t));
}
cur = (x, y);
any = true;
}
PathCommand::CubicTo(c1x, c1y, c2x, c2y, x, y) => {
b.extend(x, y);
let p = [cur, (c1x, c1y), (c2x, c2y), (x, y)];
for t in cubic_roots(p[0].0, p[1].0, p[2].0, p[3].0) {
b.extend(cubic_x(p, t), cubic_y(p, t));
}
for t in cubic_roots(p[0].1, p[1].1, p[2].1, p[3].1) {
b.extend(cubic_x(p, t), cubic_y(p, t));
}
cur = (x, y);
any = true;
}
PathCommand::Close => {
cur = (0.0, 0.0);
any = true;
}
}
}
if any {
Some(b)
} else {
None
}
}
#[must_use]
pub fn transformed(&self, m: &Affine2D) -> Path2D {
let mut out = Path2D::new();
out.commands.reserve(self.commands.len());
for cmd in &self.commands {
let t = match *cmd {
PathCommand::MoveTo(x, y) => {
let (x, y) = m.transform_point(x, y);
PathCommand::MoveTo(x, y)
}
PathCommand::LineTo(x, y) => {
let (x, y) = m.transform_point(x, y);
PathCommand::LineTo(x, y)
}
PathCommand::QuadTo(cx, cy, x, y) => {
let (cx, cy) = m.transform_point(cx, cy);
let (x, y) = m.transform_point(x, y);
PathCommand::QuadTo(cx, cy, x, y)
}
PathCommand::CubicTo(c1x, c1y, c2x, c2y, x, y) => {
let (c1x, c1y) = m.transform_point(c1x, c1y);
let (c2x, c2y) = m.transform_point(c2x, c2y);
let (x, y) = m.transform_point(x, y);
PathCommand::CubicTo(c1x, c1y, c2x, c2y, x, y)
}
PathCommand::Close => PathCommand::Close,
};
out.push(t);
}
out
}
#[must_use]
pub fn translated(&self, dx: f32, dy: f32) -> Path2D {
self.transformed(&Affine2D::translate(dx, dy))
}
pub fn reverse(&mut self) {
let reversed = crate::stroke::reverse_commands(&self.commands);
self.commands = reversed;
}
#[must_use]
pub fn subpath_count(&self) -> usize {
self.commands
.iter()
.filter(|c| matches!(c, PathCommand::MoveTo(..)))
.count()
}
#[must_use]
pub fn is_closed(&self) -> bool {
matches!(self.commands.last(), Some(PathCommand::Close))
}
#[must_use]
pub fn flatten(&self, tolerance: f32) -> Vec<Vec<(f32, f32)>> {
self.flatten_polylines(tolerance)
.into_iter()
.map(|p| p.points)
.collect()
}
#[must_use]
pub(crate) fn flatten_polylines(&self, tolerance: f32) -> Vec<Polyline> {
let tol = if tolerance > 0.0 {
tolerance
} else {
self.bbox().map_or(0.25, |b| {
(b.width().max(b.height()).max(1.0)) * 0.01
})
};
let mut out: Vec<Polyline> = Vec::new();
let mut cur: Vec<(f32, f32)> = Vec::new();
let mut pt = (0.0f32, 0.0f32);
let mut start = (0.0f32, 0.0f32);
let mut drew = false;
let mut closed = false;
for cmd in &self.commands {
match *cmd {
PathCommand::MoveTo(x, y) => {
if cur.len() > 1 {
if drew {
cur.push(start);
}
if !is_degenerate(&cur) {
out.push(Polyline {
points: core::mem::take(&mut cur),
closed: false,
});
}
}
cur.clear();
pt = (x, y);
start = pt;
cur.push(pt);
drew = false;
closed = false;
}
PathCommand::LineTo(x, y) => {
if cur.is_empty() {
cur.push(pt);
start = pt;
}
cur.push((x, y));
pt = (x, y);
drew = true;
}
PathCommand::QuadTo(cx, cy, x, y) => {
if cur.is_empty() {
cur.push(pt);
start = pt;
}
flatten_quad(&mut cur, pt, (cx, cy), (x, y), tol);
pt = (x, y);
drew = true;
}
PathCommand::CubicTo(c1x, c1y, c2x, c2y, x, y) => {
if cur.is_empty() {
cur.push(pt);
start = pt;
}
flatten_cubic(&mut cur, pt, (c1x, c1y), (c2x, c2y), (x, y), tol);
pt = (x, y);
drew = true;
}
PathCommand::Close => {
if cur.len() > 1 {
cur.push(start);
if !is_degenerate(&cur) {
out.push(Polyline {
points: core::mem::take(&mut cur),
closed: true,
});
}
}
cur.clear();
pt = start;
drew = false;
closed = true;
}
}
}
let _ = closed;
if cur.len() > 1 {
if drew {
cur.push(start);
}
if is_degenerate(&cur) {
cur.clear();
}
if cur.len() > 1 {
out.push(Polyline {
points: cur,
closed: false,
});
}
}
out
}
#[must_use]
pub fn polygon_area(&self) -> f32 {
self.flatten(0.0)
.iter()
.map(|poly| {
let mut a = 0.0f32;
for (i, p) in poly.iter().enumerate() {
let q = poly.get(i + 1).copied().unwrap_or(*p);
let finite =
p.0.is_finite() && p.1.is_finite() && q.0.is_finite() && q.1.is_finite();
if finite {
a += p.0 * q.1 - q.0 * p.1;
}
}
a.abs() * 0.5
})
.sum()
}
}
#[derive(Debug, Clone, Default)]
pub struct PathBuilder {
commands: Vec<PathCommand>,
stack: Vec<Affine2D>,
current: Affine2D,
cursor: (f32, f32),
subpath_start: (f32, f32),
started: bool,
}
impl PathBuilder {
#[must_use]
pub fn new() -> Self {
PathBuilder {
commands: Vec::new(),
stack: Vec::new(),
current: Affine2D::IDENTITY,
cursor: (0.0, 0.0),
subpath_start: (0.0, 0.0),
started: false,
}
}
#[must_use]
pub fn from_path(path: &Path2D) -> Self {
let mut b = PathBuilder::new();
b.commands.extend_from_slice(path.commands());
if let Some(last) = path.commands().last() {
b.cursor = last.end_point().unwrap_or((0.0, 0.0));
}
b
}
#[must_use]
pub const fn current_transform(&self) -> Affine2D {
self.current
}
#[must_use]
pub fn depth(&self) -> usize {
self.stack.len()
}
pub fn push_transform(&mut self, m: Affine2D) -> &mut Self {
self.current = m.then(&self.current);
self.stack.push(m);
self
}
#[allow(clippy::result_large_err)]
pub fn pop_transform(&mut self) -> Result<Affine2D, ShapeError> {
let popped = self
.stack
.pop()
.ok_or(ShapeError::UnbalancedPop { depth: 0 })?;
let mut m = self.stack.last().copied().unwrap_or(Affine2D::IDENTITY);
for below in self.stack.iter().rev().skip(1) {
m = m.then(below);
}
self.current = m;
Ok(popped)
}
pub fn reset_transforms(&mut self) -> &mut Self {
self.stack.clear();
self.current = Affine2D::IDENTITY;
self
}
#[must_use]
pub const fn current_point(&self) -> (f32, f32) {
self.cursor
}
pub fn move_to(&mut self, x: f32, y: f32) -> &mut Self {
let (x, y) = self.current.transform_point(x, y);
self.cursor = (x, y);
self.subpath_start = (x, y);
self.started = true;
self.commands.push(PathCommand::MoveTo(x, y));
self
}
pub fn move_by(&mut self, dx: f32, dy: f32) -> &mut Self {
let (dx, dy) = self.current.transform_vector(dx, dy);
let (cx, cy) = self.cursor;
self.move_to_device(cx + dx, cy + dy)
}
fn move_to_device(&mut self, x: f32, y: f32) -> &mut Self {
self.cursor = (x, y);
self.subpath_start = (x, y);
self.started = true;
self.commands.push(PathCommand::MoveTo(x, y));
self
}
pub fn line_to(&mut self, x: f32, y: f32) -> &mut Self {
let (x, y) = self.current.transform_point(x, y);
self.cursor = (x, y);
self.commands.push(PathCommand::LineTo(x, y));
self
}
pub fn line_by(&mut self, dx: f32, dy: f32) -> &mut Self {
let (dx, dy) = self.current.transform_vector(dx, dy);
let (cx, cy) = self.cursor;
let x = cx + dx;
let y = cy + dy;
self.cursor = (x, y);
self.commands.push(PathCommand::LineTo(x, y));
self
}
pub fn quad_to(&mut self, cx: f32, cy: f32, x: f32, y: f32) -> &mut Self {
let (cx, cy) = self.current.transform_point(cx, cy);
let (x, y) = self.current.transform_point(x, y);
self.cursor = (x, y);
self.commands.push(PathCommand::QuadTo(cx, cy, x, y));
self
}
pub fn cubic_to(
&mut self,
c1x: f32,
c1y: f32,
c2x: f32,
c2y: f32,
x: f32,
y: f32,
) -> &mut Self {
let (c1x, c1y) = self.current.transform_point(c1x, c1y);
let (c2x, c2y) = self.current.transform_point(c2x, c2y);
let (x, y) = self.current.transform_point(x, y);
self.cursor = (x, y);
self.commands
.push(PathCommand::CubicTo(c1x, c1y, c2x, c2y, x, y));
self
}
pub fn close(&mut self) -> &mut Self {
self.commands.push(PathCommand::Close);
self.cursor = self.subpath_start;
self
}
pub fn add_polygon(&mut self, points: &[(f32, f32)], closed: bool) -> &mut Self {
let Some(first) = points.first() else {
return self;
};
self.move_to(first.0, first.1);
for p in points.iter().skip(1) {
self.line_to(p.0, p.1);
}
if closed {
self.close();
}
self
}
pub fn rect(&mut self, x: f32, y: f32, w: f32, h: f32) -> &mut Self {
self.move_to(x, y);
self.line_to(x + w, y);
self.line_to(x + w, y + h);
self.line_to(x, y + h);
self.close()
}
pub fn circle(&mut self, cx: f32, cy: f32, r: f32) -> &mut Self {
let k = r * 0.552_284_8;
self.move_to(cx + r, cy);
self.cubic_to(cx + r, cy + k, cx + k, cy + r, cx, cy + r);
self.cubic_to(cx - k, cy + r, cx - r, cy + k, cx - r, cy);
self.cubic_to(cx - r, cy - k, cx - k, cy - r, cx, cy - r);
self.cubic_to(cx + k, cy - r, cx + r, cy - k, cx + r, cy);
self.close()
}
pub fn extend(&mut self, path: &Path2D) -> &mut Self {
let mut pt = (0.0f32, 0.0f32);
for cmd in path.commands() {
match *cmd {
PathCommand::MoveTo(x, y) => {
self.move_to(x, y);
pt = self.cursor;
}
PathCommand::LineTo(x, y) => {
self.line_to(x, y);
pt = self.cursor;
}
PathCommand::QuadTo(cx, cy, x, y) => {
self.quad_to(cx, cy, x, y);
pt = self.cursor;
}
PathCommand::CubicTo(c1x, c1y, c2x, c2y, x, y) => {
self.cubic_to(c1x, c1y, c2x, c2y, x, y);
pt = self.cursor;
}
PathCommand::Close => {
self.close();
pt = self.subpath_start;
}
}
}
let _ = pt;
self
}
pub fn add_stroke(&mut self, path: &Path2D, width: f32, style: StrokeStyle) -> &mut Self {
let expanded = crate::stroke::stroke_path(path, width, style);
self.extend(&expanded)
}
#[must_use]
pub fn commands(&self) -> &[PathCommand] {
&self.commands
}
#[must_use]
pub fn len(&self) -> usize {
self.commands.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.commands.is_empty()
}
#[must_use]
pub fn build(&self) -> Path2D {
Path2D {
commands: self.commands.clone(),
}
}
pub const DEFAULT_CAP: LineCap = LineCap::Butt;
pub const DEFAULT_JOIN: LineJoin = LineJoin::Miter;
}
impl PathCommand {
#[must_use]
pub fn end_point(&self) -> Option<(f32, f32)> {
match *self {
PathCommand::MoveTo(x, y) | PathCommand::LineTo(x, y) => Some((x, y)),
PathCommand::QuadTo(_, _, x, y) | PathCommand::CubicTo(_, _, _, _, x, y) => {
Some((x, y))
}
PathCommand::Close => None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Polyline {
pub points: Vec<(f32, f32)>,
pub closed: bool,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
pub min_x: f32,
pub min_y: f32,
pub max_x: f32,
pub max_y: f32,
}
impl Default for Rect {
fn default() -> Self {
Rect::empty()
}
}
impl Rect {
#[must_use]
pub const fn empty() -> Self {
Rect {
min_x: f32::INFINITY,
min_y: f32::INFINITY,
max_x: f32::NEG_INFINITY,
max_y: f32::NEG_INFINITY,
}
}
#[must_use]
pub fn from_corners(x0: f32, y0: f32, x1: f32, y1: f32) -> Self {
Rect {
min_x: if x0 < x1 { x0 } else { x1 },
min_y: if y0 < y1 { y0 } else { y1 },
max_x: if x0 > x1 { x0 } else { x1 },
max_y: if y0 > y1 { y0 } else { y1 },
}
}
pub fn extend(&mut self, x: f32, y: f32) {
if x < self.min_x {
self.min_x = x;
}
if y < self.min_y {
self.min_y = y;
}
if x > self.max_x {
self.max_x = x;
}
if y > self.max_y {
self.max_y = y;
}
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.min_x > self.max_x
}
#[must_use]
pub fn width(&self) -> f32 {
if self.is_empty() {
0.0
} else {
self.max_x - self.min_x
}
}
#[must_use]
pub fn height(&self) -> f32 {
if self.is_empty() {
0.0
} else {
self.max_y - self.min_y
}
}
#[must_use]
pub fn contains(&self, x: f32, y: f32) -> bool {
!self.is_empty() && x >= self.min_x && x <= self.max_x && y >= self.min_y && y <= self.max_y
}
#[must_use]
pub fn translated(&self, dx: f32, dy: f32) -> Rect {
if self.is_empty() {
return Rect::empty();
}
Rect::from_corners(
self.min_x + dx,
self.min_y + dy,
self.max_x + dx,
self.max_y + dy,
)
}
}
fn quad_roots(a0: f32, a1: f32, a2: f32) -> Vec<f32> {
let qa = 2.0 * (a0 - 2.0 * a1 + a2);
let qb = 2.0 * (a1 - a0);
if libm::fabsf(qa) < 1e-9 {
return Vec::new();
}
let t = -qb / qa;
if t > 0.0 && t < 1.0 {
vec![t]
} else {
Vec::new()
}
}
fn cubic_roots(a0: f32, a1: f32, a2: f32, a3: f32) -> Vec<f32> {
let qa = 3.0 * (-a0 + 3.0 * a1 - 3.0 * a2 + a3);
let qb = 6.0 * (a0 - 2.0 * a1 + a2);
let qc = 3.0 * (a1 - a0);
let mut out = Vec::new();
let mut push = |t: f32| {
if t > 0.0 && t < 1.0 {
out.push(t);
}
};
if libm::fabsf(qa) < 1e-9 {
if libm::fabsf(qb) >= 1e-9 {
push(-qc / qb);
}
return out;
}
let disc = qb * qb - 4.0 * qa * qc;
if disc >= 0.0 {
let s = libm::sqrtf(disc);
push((-qb + s) / (2.0 * qa));
push((-qb - s) / (2.0 * qa));
}
out
}
fn quad_x(p: [(f32, f32); 3], t: f32) -> f32 {
let mt = 1.0 - t;
mt * mt * p[0].0 + 2.0 * mt * t * p[1].0 + t * t * p[2].0
}
fn quad_y(p: [(f32, f32); 3], t: f32) -> f32 {
let mt = 1.0 - t;
mt * mt * p[0].1 + 2.0 * mt * t * p[1].1 + t * t * p[2].1
}
fn cubic_x(p: [(f32, f32); 4], t: f32) -> f32 {
let mt = 1.0 - t;
mt * mt * mt * p[0].0
+ 3.0 * mt * mt * t * p[1].0
+ 3.0 * mt * t * t * p[2].0
+ t * t * t * p[3].0
}
fn cubic_y(p: [(f32, f32); 4], t: f32) -> f32 {
let mt = 1.0 - t;
mt * mt * mt * p[0].1
+ 3.0 * mt * mt * t * p[1].1
+ 3.0 * mt * t * t * p[2].1
+ t * t * t * p[3].1
}
fn is_degenerate(pts: &[(f32, f32)]) -> bool {
let Some(first) = pts.first() else {
return true;
};
pts.iter().all(|p| p.0 == first.0 && p.1 == first.1)
}
fn segment_count(p0: (f32, f32), p1: (f32, f32), p2: (f32, f32), cubic: bool, tol: f32) -> usize {
let tol = if tol > 1e-6 { tol } else { 1e-6 };
let d2 = if cubic {
let ax = libm::fabsf(p2.0 - 2.0 * p1.0 + p0.0);
let ay = libm::fabsf(p2.1 - 2.0 * p1.1 + p0.1);
libm::sqrtf(ax * ax + ay * ay)
} else {
let ax = libm::fabsf(p1.0 - p0.0);
let ay = libm::fabsf(p1.1 - p0.1);
libm::sqrtf(ax * ax + ay * ay)
};
if d2 <= tol {
return 1;
}
let n = libm::sqrtf(d2 / tol) * 2.0;
let n = if n.is_finite() { n as usize } else { 64 };
n.clamp(1, 64) }
fn flatten_quad(
out: &mut Vec<(f32, f32)>,
p0: (f32, f32),
c: (f32, f32),
p1: (f32, f32),
tol: f32,
) {
let n = segment_count(p0, c, p1, false, tol);
for i in 1..=n {
let t = i as f32 / n as f32;
out.push((quad_x([p0, c, p1], t), quad_y([p0, c, p1], t)));
}
}
fn flatten_cubic(
out: &mut Vec<(f32, f32)>,
p0: (f32, f32),
c1: (f32, f32),
c2: (f32, f32),
p1: (f32, f32),
tol: f32,
) {
let n = segment_count(p0, c1, c2, true, tol);
for i in 1..=n {
let t = i as f32 / n as f32;
out.push((cubic_x([p0, c1, c2, p1], t), cubic_y([p0, c1, c2, p1], t)));
}
}
#[cfg(test)]
mod tests {
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing,
clippy::float_cmp
)]
use super::{Path2D, PathBuilder, PathCommand, Rect};
use crate::Affine2D;
fn close(a: f32, b: f32) -> bool {
(a - b).abs() < 1e-4
}
fn rect_path(x: f32, y: f32, w: f32, h: f32) -> Path2D {
let mut b = PathBuilder::new();
b.rect(x, y, w, h);
b.build()
}
#[test]
fn empty_path_has_no_bbox() {
let p = Path2D::new();
assert!(p.is_empty());
assert_eq!(p.len(), 0);
assert!(p.bbox().is_none());
assert!(p.flatten(0.0).is_empty());
assert_eq!(p.subpath_count(), 0);
assert!(!p.is_closed());
assert_eq!(p.polygon_area(), 0.0);
}
#[test]
fn bbox_of_a_rectangle_is_exact() {
let b = rect_path(10.0, 20.0, 30.0, 40.0).bbox().expect("bbox");
assert_eq!(b.min_x, 10.0);
assert_eq!(b.min_y, 20.0);
assert_eq!(b.max_x, 40.0);
assert_eq!(b.max_y, 60.0);
assert_eq!(b.width(), 30.0);
assert_eq!(b.height(), 40.0);
assert!(b.contains(10.0, 20.0));
}
#[test]
fn bbox_holds_a_cubics_true_extremes() {
let mut b = PathBuilder::new();
b.move_to(0.0, 0.0);
b.cubic_to(3.0, 0.0, 3.0, 3.0, 0.0, 3.0);
b.close();
let bbox = b.build().bbox().expect("bbox");
assert!(close(bbox.max_x, 2.25), "max_x {}", bbox.max_x);
let mut q = PathBuilder::new();
q.move_to(0.0, 0.0);
q.quad_to(2.0, 4.0, 4.0, 0.0);
q.close();
let bbox = q.build().bbox().expect("bbox");
assert!(close(bbox.max_y, 2.0), "max_y {}", bbox.max_y);
}
#[test]
fn rect_and_polygon_helpers_agree() {
let mut a = PathBuilder::new();
a.rect(0.0, 0.0, 10.0, 10.0);
let mut b = PathBuilder::new();
b.add_polygon(&[(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)], true);
assert_eq!(a.build().bbox(), b.build().bbox());
assert_eq!(a.build().commands().len(), 5);
let mut c = PathBuilder::new();
c.add_polygon(&[], true);
assert!(c.is_empty());
let mut d = PathBuilder::new();
d.add_polygon(&[(1.0, 2.0)], true);
assert_eq!(d.len(), 2);
}
#[test]
fn circle_is_roughly_circular() {
let mut b = PathBuilder::new();
b.circle(50.0, 50.0, 20.0);
let p = b.build();
let bbox = p.bbox().expect("bbox");
assert!(
close(bbox.min_x, 30.0) && close(bbox.max_x, 70.0),
"{bbox:?}"
);
assert!(close(bbox.width(), bbox.height()), "{bbox:?}");
let area = p.polygon_area();
let want = core::f32::consts::PI * 400.0;
assert!(
(area - want).abs() < want * 0.01,
"circle area {area} vs {want}"
);
}
#[test]
fn flatten_respects_the_tolerance() {
let mut b = PathBuilder::new();
b.move_to(0.0, 0.0);
b.cubic_to(0.0, 100.0, 100.0, 100.0, 100.0, 0.0);
b.close();
let p = b.build();
let coarse = p.flatten(10.0);
let fine = p.flatten(0.01);
assert!(
fine[0].len() > coarse[0].len(),
"finer tolerance: {} vs {}",
fine[0].len(),
coarse[0].len()
);
assert!(fine[0].len() <= 66, "clamped: {}", fine[0].len());
let mut f = PathBuilder::new();
f.move_to(0.0, 0.0);
f.cubic_to(1.0, 0.0, 2.0, 0.0, 3.0, 0.0);
f.close();
assert_eq!(f.build().flatten(1.0)[0].len(), 3);
}
#[test]
fn flatten_closes_a_closed_subpath_and_leaves_an_open_one() {
let closed = rect_path(0.0, 0.0, 5.0, 5.0);
let polys = closed.flatten(0.0);
assert_eq!(polys.len(), 1);
assert_eq!(polys[0][0], polys[0][polys[0].len() - 1]);
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);
let open = b.build();
let polys = open.flatten(0.0);
assert_eq!(polys.len(), 1);
assert_eq!(polys[0].last(), Some(&(0.0, 0.0)));
assert!(!open.is_closed());
}
#[test]
fn flatten_of_a_lone_point_is_dropped() {
let mut b = PathBuilder::new();
b.move_to(5.0, 5.0);
assert!(b.build().flatten(0.0).is_empty());
let mut c = PathBuilder::new();
c.move_to(5.0, 5.0);
c.line_to(5.0, 5.0);
c.close();
assert!(c.build().flatten(0.0).is_empty() || c.build().flatten(0.0)[0].len() <= 2);
}
#[test]
fn transform_moves_every_point() {
let p = rect_path(0.0, 0.0, 10.0, 10.0);
let t = p.transformed(&Affine2D::translate(5.0, 7.0));
let b = t.bbox().expect("bbox");
assert_eq!(b.min_x, 5.0);
assert_eq!(b.min_y, 7.0);
assert_eq!(b.width(), 10.0);
assert_eq!(p.bbox().expect("bbox").min_x, 0.0);
assert_eq!(p.translated(5.0, 7.0).bbox(), Some(b));
}
#[test]
fn transform_of_a_curve_is_the_curve_of_the_transformed_points() {
let mut b = PathBuilder::new();
b.move_to(0.0, 0.0);
b.cubic_to(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
b.close();
let p = b.build();
let m = Affine2D::new(2.0, 0.0, 0.0, 3.0, 1.0, -1.0);
let t = p.transformed(&m);
for (a, b2) in p.commands().iter().zip(t.commands().iter()) {
let want = |x: f32, y: f32| -> (f32, f32) { m.transform_point(x, y) };
match (*a, *b2) {
(PathCommand::MoveTo(x1, y1), PathCommand::MoveTo(x2, y2))
| (PathCommand::LineTo(x1, y1), PathCommand::LineTo(x2, y2)) => {
let w = want(x1, y1);
assert!(close(w.0, x2) && close(w.1, y2));
}
(PathCommand::QuadTo(a1, a2, a3, a4), PathCommand::QuadTo(b1, b2, b3, b4)) => {
for (src, dst) in [((a1, a2), (b1, b2)), ((a3, a4), (b3, b4))] {
let w = want(src.0, src.1);
assert!(close(w.0, dst.0) && close(w.1, dst.1));
}
}
(
PathCommand::CubicTo(a1, a2, a3, a4, a5, a6),
PathCommand::CubicTo(b1, b2, b3, b4, b5, b6),
) => {
let pairs = [
((a1, a2), (b1, b2)),
((a3, a4), (b3, b4)),
((a5, a6), (b5, b6)),
];
for (src, dst) in pairs {
let w = want(src.0, src.1);
assert!(close(w.0, dst.0) && close(w.1, dst.1));
}
}
(PathCommand::Close, PathCommand::Close) => {}
_ => panic!("command shape changed: {a:?} -> {b2:?}"),
}
}
}
#[test]
fn transform_stack_composes_and_pops() {
let mut b = PathBuilder::new();
assert_eq!(b.depth(), 0);
assert!(b.current_transform().is_identity());
b.push_transform(Affine2D::translate(10.0, 0.0));
assert_eq!(b.depth(), 1);
b.push_transform(Affine2D::scale(2.0, 2.0));
assert_eq!(b.depth(), 2);
b.rect(0.0, 0.0, 5.0, 5.0);
assert_eq!(
b.pop_transform().expect("balanced"),
Affine2D::scale(2.0, 2.0)
);
b.rect(0.0, 0.0, 5.0, 5.0);
b.pop_transform().expect("balanced");
assert_eq!(b.depth(), 0);
assert!(b.current_transform().is_identity());
let bbox = b.build().bbox().expect("bbox");
assert_eq!(bbox.max_x, 20.0);
assert_eq!(bbox.max_y, 10.0);
}
#[test]
fn unbalanced_pop_is_refused() {
let mut b = PathBuilder::new();
match b.pop_transform().unwrap_err() {
crate::ShapeError::UnbalancedPop { depth } => assert_eq!(depth, 0),
other => panic!("expected UnbalancedPop, got {other:?}"),
}
b.push_transform(Affine2D::scale(0.0, 0.0));
assert!(b.pop_transform().is_ok());
b.reset_transforms();
assert_eq!(b.depth(), 0);
}
#[test]
fn transform_is_applied_at_add_time() {
let mut b = PathBuilder::new();
b.push_transform(Affine2D::translate(100.0, 100.0));
b.move_to(1.0, 2.0);
assert_eq!(b.current_point(), (101.0, 102.0));
b.line_to(3.0, 4.0);
match b.commands()[1] {
PathCommand::LineTo(x, y) => assert_eq!((x, y), (103.0, 104.0)),
other => panic!("{other:?}"),
}
b.line_by(1.0, 1.0);
assert_eq!(b.current_point(), (104.0, 105.0));
b.move_by(-104.0, -105.0);
assert_eq!(b.current_point(), (0.0, 0.0));
}
#[test]
fn from_path_keeps_the_commands_and_cursor() {
let p = rect_path(1.0, 2.0, 3.0, 4.0);
let b = PathBuilder::from_path(&p);
assert_eq!(b.len(), p.len());
assert!(b.current_point() == (0.0, 0.0) || b.current_point() == (0.0, 0.0));
}
#[test]
fn extend_transforms_the_added_path() {
let inner = rect_path(0.0, 0.0, 10.0, 10.0);
let mut b = PathBuilder::new();
b.push_transform(Affine2D::translate(50.0, 0.0));
b.extend(&inner);
let bbox = b.build().bbox().expect("bbox");
assert_eq!(bbox.min_x, 50.0);
assert_eq!(bbox.max_x, 60.0);
}
#[test]
fn reverse_flips_direction_but_keeps_geometry() {
let mut p = rect_path(0.0, 0.0, 10.0, 10.0);
let before = p.bbox().expect("bbox");
let area = p.polygon_area();
p.reverse();
assert_eq!(p.bbox(), Some(before));
assert!(close(p.polygon_area(), area));
assert!(p.is_closed());
}
#[test]
fn subpath_count_and_close_tracking() {
let mut b = PathBuilder::new();
b.rect(0.0, 0.0, 1.0, 1.0);
b.rect(2.0, 0.0, 1.0, 1.0);
b.move_to(5.0, 5.0);
let p = b.build();
assert_eq!(p.subpath_count(), 3);
assert!(!p.is_closed(), "the last subpath is open");
b.close();
assert!(b.build().is_closed());
}
#[test]
fn rect_helpers() {
let r = Rect::empty();
assert!(r.is_empty());
assert_eq!(r.width(), 0.0);
assert_eq!(r.height(), 0.0);
assert!(!r.contains(0.0, 0.0));
let r = Rect::from_corners(10.0, 20.0, 0.0, 5.0);
assert_eq!(r.min_x, 0.0);
assert_eq!(r.max_y, 20.0);
let t = r.translated(1.0, 1.0);
assert_eq!(t.min_x, 1.0);
assert!(Rect::empty().translated(1.0, 1.0).is_empty());
let mut g = r;
g.extend(-5.0, -100.0);
assert_eq!(g.min_x, -5.0);
assert_eq!(g.min_y, -100.0);
assert_eq!(g.max_x, 10.0);
assert_eq!(g.max_y, 20.0);
assert_eq!(Rect::default(), Rect::empty());
}
#[test]
fn command_end_points() {
assert_eq!(PathCommand::MoveTo(1.0, 2.0).end_point(), Some((1.0, 2.0)));
assert_eq!(PathCommand::LineTo(1.0, 2.0).end_point(), Some((1.0, 2.0)));
assert_eq!(
PathCommand::QuadTo(0.0, 0.0, 1.0, 2.0).end_point(),
Some((1.0, 2.0))
);
assert_eq!(
PathCommand::CubicTo(0.0, 0.0, 0.0, 0.0, 1.0, 2.0).end_point(),
Some((1.0, 2.0))
);
assert_eq!(PathCommand::Close.end_point(), None);
}
#[test]
fn zero_tolerance_uses_a_default() {
let mut b = PathBuilder::new();
b.circle(0.0, 0.0, 50.0);
let p = b.build();
let a = p.flatten(0.0);
let b2 = p.flatten(1.0);
assert!(!a.is_empty() && !b2.is_empty());
}
#[test]
fn polygon_area_of_a_square() {
let p = rect_path(0.0, 0.0, 10.0, 10.0);
assert!(close(p.polygon_area(), 100.0), "{}", p.polygon_area());
let mut b = PathBuilder::new();
b.rect(0.0, 0.0, 10.0, 10.0);
b.rect(20.0, 0.0, 10.0, 10.0);
assert!(close(b.build().polygon_area(), 200.0));
}
#[test]
fn build_is_a_snapshot() {
let mut b = PathBuilder::new();
b.rect(0.0, 0.0, 1.0, 1.0);
let snapshot = b.build();
b.rect(10.0, 10.0, 1.0, 1.0);
assert_eq!(snapshot.len(), 5, "the snapshot did not grow");
assert_eq!(b.len(), 10);
}
}