pub trait Pen {
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(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PenCommand {
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,
}
impl PenCommand {
pub fn apply_to<T: Pen>(&self, pen: &mut T) {
match *self {
PenCommand::MoveTo { x, y } => pen.move_to(x, y),
PenCommand::LineTo { x, y } => pen.line_to(x, y),
PenCommand::QuadTo { cx0, cy0, x, y } => pen.quad_to(cx0, cy0, x, y),
PenCommand::CurveTo {
cx0,
cy0,
cx1,
cy1,
x,
y,
} => pen.curve_to(cx0, cy0, cx1, cy1, x, y),
PenCommand::Close => pen.close(),
}
}
pub fn end_point(&self) -> Option<(f32, f32)> {
match *self {
PenCommand::MoveTo { x, y }
| PenCommand::LineTo { x, y }
| PenCommand::QuadTo { x, y, .. }
| PenCommand::CurveTo { x, y, .. } => Some((x, y)),
PenCommand::Close => None,
}
}
}