use std::f32::consts::TAU;
pub(super) mod command {
pub const MOVE_TO: u8 = 0;
pub const LINE_TO: u8 = 1;
pub const QUAD_TO: u8 = 2;
pub const CUBIC_TO: u8 = 3;
pub const ARC_TO: u8 = 4;
pub const ELLIPSE: u8 = 5;
pub const CLOSE: u8 = 6;
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Path2D {
svg: String,
commands: Vec<u8>,
count: u32,
}
impl Path2D {
pub fn new() -> Self {
Self::default()
}
pub fn svg(data: &str) -> Self {
Self {
svg: data.into(),
..Self::default()
}
}
pub fn move_to(&mut self, x: f32, y: f32) -> &mut Self {
self.command(command::MOVE_TO, &[x, y])
}
pub fn line_to(&mut self, x: f32, y: f32) -> &mut Self {
self.command(command::LINE_TO, &[x, y])
}
pub fn quad_to(&mut self, cx: f32, cy: f32, x: f32, y: f32) -> &mut Self {
self.command(command::QUAD_TO, &[cx, cy, x, y])
}
pub fn cubic_to(
&mut self,
c1x: f32,
c1y: f32,
c2x: f32,
c2y: f32,
x: f32,
y: f32,
) -> &mut Self {
self.command(command::CUBIC_TO, &[c1x, c1y, c2x, c2y, x, y])
}
pub fn arc_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, radius: f32) -> &mut Self {
self.command(command::ARC_TO, &[x1, y1, x2, y2, radius])
}
pub fn arc(
&mut self,
x: f32,
y: f32,
radius: f32,
start: f32,
end: f32,
counterclockwise: bool,
) -> &mut Self {
self.ellipse((x, y), (radius, radius), 0.0, start, end, counterclockwise)
}
pub fn ellipse(
&mut self,
center: (f32, f32),
radii: (f32, f32),
rotation: f32,
start: f32,
end: f32,
counterclockwise: bool,
) -> &mut Self {
let (x, y) = center;
let (radius_x, radius_y) = radii;
self.command(
command::ELLIPSE,
&[x, y, radius_x, radius_y, rotation, start, end],
);
self.commands.push(u8::from(counterclockwise));
self
}
pub fn close(&mut self) -> &mut Self {
self.command(command::CLOSE, &[])
}
pub fn rect(&mut self, x: f32, y: f32, width: f32, height: f32) -> &mut Self {
self.move_to(x, y)
.line_to(x + width, y)
.line_to(x + width, y + height)
.line_to(x, y + height)
.close()
}
pub fn round_rect(
&mut self,
x: f32,
y: f32,
width: f32,
height: f32,
radius: f32,
) -> &mut Self {
let radius = radius
.max(0.0)
.min(width.abs() / 2.0)
.min(height.abs() / 2.0);
let (right, bottom) = (x + width, y + height);
self.move_to(x + radius.copysign(width), y)
.arc_to(right, y, right, bottom, radius)
.arc_to(right, bottom, x, bottom, radius)
.arc_to(x, bottom, x, y, radius)
.arc_to(x, y, right, y, radius)
.close()
}
pub fn circle(&mut self, x: f32, y: f32, radius: f32) -> &mut Self {
self.move_to(x + radius, y)
.arc(x, y, radius, 0.0, TAU, false)
.close()
}
pub fn polygon(&mut self, points: impl IntoIterator<Item = (f32, f32)>) -> &mut Self {
let mut points = points.into_iter();
if let Some((x, y)) = points.next() {
self.move_to(x, y);
for (x, y) in points {
self.line_to(x, y);
}
self.close();
}
self
}
pub(super) fn write(&self, bytes: &mut Vec<u8>) {
bytes.extend_from_slice(&(self.svg.len() as u32).to_le_bytes());
bytes.extend_from_slice(self.svg.as_bytes());
bytes.extend_from_slice(&self.count.to_le_bytes());
bytes.extend_from_slice(&self.commands);
}
fn command(&mut self, command: u8, args: &[f32]) -> &mut Self {
self.commands.push(command);
for arg in args {
self.commands.extend_from_slice(&arg.to_le_bytes());
}
self.count += 1;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
fn written(path: &Path2D) -> Vec<u8> {
let mut bytes = Vec::new();
path.write(&mut bytes);
bytes
}
#[test]
fn a_path_writes_its_svg_then_its_commands() {
let mut path = Path2D::svg("M0 0");
path.line_to(1.0, 2.0).close();
let bytes = written(&path);
assert_eq!(&bytes[..4], 4u32.to_le_bytes());
assert_eq!(&bytes[4..8], b"M0 0");
assert_eq!(&bytes[8..12], 2u32.to_le_bytes());
assert_eq!(bytes[12], command::LINE_TO);
assert_eq!(&bytes[13..21], [1.0f32, 2.0].map(f32::to_le_bytes).concat());
assert_eq!(bytes[21..], [command::CLOSE]);
}
#[test]
fn an_ellipse_says_which_way_it_turns() {
let mut path = Path2D::new();
path.ellipse((1.0, 2.0), (3.0, 4.0), 0.5, 0.0, 1.0, true);
let bytes = written(&path);
assert_eq!(bytes[8], command::ELLIPSE);
assert_eq!(
&bytes[9..37],
[1.0f32, 2.0, 3.0, 4.0, 0.5, 0.0, 1.0]
.map(f32::to_le_bytes)
.concat()
);
assert_eq!(bytes[37..], [1]);
}
#[test]
fn a_round_rect_keeps_its_radius_within_half_its_shorter_side() {
let mut path = Path2D::new();
path.round_rect(0.0, 0.0, 10.0, 4.0, 5.0);
let mut expected = Path2D::new();
expected
.move_to(2.0, 0.0)
.arc_to(10.0, 0.0, 10.0, 4.0, 2.0)
.arc_to(10.0, 4.0, 0.0, 4.0, 2.0)
.arc_to(0.0, 4.0, 0.0, 0.0, 2.0)
.arc_to(0.0, 0.0, 10.0, 0.0, 2.0)
.close();
assert_eq!(path, expected);
}
#[test]
fn a_polygon_of_no_points_adds_nothing() {
let mut path = Path2D::new();
path.polygon([]);
assert_eq!(path, Path2D::new());
}
}