use alloc::{string::String, vec::Vec};
use core::fmt::{self, Write};
pub trait OutlinePen {
fn move_to(&mut self, x: f32, y: f32);
fn line_to(&mut self, x: f32, y: f32);
fn quad_to(&mut self, cx0: f32, cy0: f32, x: f32, y: f32);
fn curve_to(&mut self, cx0: f32, cy0: f32, cx1: f32, cy1: f32, x: f32, y: f32);
fn close(&mut self);
}
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
pub enum PathElement {
MoveTo { x: f32, y: f32 },
LineTo { x: f32, y: f32 },
QuadTo { cx0: f32, cy0: f32, x: f32, y: f32 },
CurveTo {
cx0: f32,
cy0: f32,
cx1: f32,
cy1: f32,
x: f32,
y: f32,
},
Close,
}
#[derive(Debug, Default, Copy, Clone)]
pub enum PathStyle {
#[default]
FreeType,
HarfBuzz,
}
impl OutlinePen for Vec<PathElement> {
fn move_to(&mut self, x: f32, y: f32) {
self.push(PathElement::MoveTo { x, y })
}
fn line_to(&mut self, x: f32, y: f32) {
self.push(PathElement::LineTo { x, y })
}
fn quad_to(&mut self, cx0: f32, cy0: f32, x: f32, y: f32) {
self.push(PathElement::QuadTo { cx0, cy0, x, y })
}
fn curve_to(&mut self, cx0: f32, cy0: f32, cx1: f32, cy1: f32, x: f32, y: f32) {
self.push(PathElement::CurveTo {
cx0,
cy0,
cx1,
cy1,
x,
y,
})
}
fn close(&mut self) {
self.push(PathElement::Close)
}
}
pub struct NullPen;
impl OutlinePen for NullPen {
fn move_to(&mut self, _x: f32, _y: f32) {}
fn line_to(&mut self, _x: f32, _y: f32) {}
fn quad_to(&mut self, _cx0: f32, _cy0: f32, _x: f32, _y: f32) {}
fn curve_to(&mut self, _cx0: f32, _cy0: f32, _cx1: f32, _cy1: f32, _x: f32, _y: f32) {}
fn close(&mut self) {}
}
#[derive(Clone, Default, Debug)]
pub struct SvgPen(String);
impl SvgPen {
pub fn clear(&mut self) {
self.0.clear();
}
fn maybe_push_space(&mut self) {
if !self.0.is_empty() {
self.0.push(' ');
}
}
}
impl core::ops::Deref for SvgPen {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0.as_str()
}
}
impl OutlinePen for SvgPen {
fn move_to(&mut self, x: f32, y: f32) {
self.maybe_push_space();
let _ = write!(self.0, "M{x:.1},{y:.1}");
}
fn line_to(&mut self, x: f32, y: f32) {
self.maybe_push_space();
let _ = write!(self.0, "L{x:.1},{y:.1}");
}
fn quad_to(&mut self, cx0: f32, cy0: f32, x: f32, y: f32) {
self.maybe_push_space();
let _ = write!(self.0, "Q{cx0:.1},{cy0:.1} {x:.1},{y:.1}");
}
fn curve_to(&mut self, cx0: f32, cy0: f32, cx1: f32, cy1: f32, x: f32, y: f32) {
self.maybe_push_space();
let _ = write!(self.0, "C{cx0:.1},{cy0:.1} {cx1:.1},{cy1:.1} {x:.1},{y:.1}");
}
fn close(&mut self) {
self.maybe_push_space();
self.0.push('Z');
}
}
impl AsRef<str> for SvgPen {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl From<String> for SvgPen {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<SvgPen> for String {
fn from(value: SvgPen) -> Self {
value.0
}
}
impl fmt::Display for SvgPen {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}