use core::borrow::Borrow;
use smallvec::SmallVec;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Fill {
NonZero,
EvenOdd,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Join {
Bevel,
Miter,
Round,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Cap {
Butt,
Square,
Round,
}
#[derive(Clone, Debug)]
pub struct Stroke {
pub width: f32,
pub join: Join,
pub miter_limit: f32,
pub start_cap: Cap,
pub end_cap: Cap,
pub dash_pattern: Dashes,
pub dash_offset: f32,
pub scale: bool,
}
impl Default for Stroke {
fn default() -> Self {
Self {
width: 1.0,
join: Join::Round,
miter_limit: 4.0,
start_cap: Cap::Round,
end_cap: Cap::Round,
dash_pattern: Default::default(),
dash_offset: 0.0,
scale: true,
}
}
}
impl Stroke {
pub fn new(width: f32) -> Self {
Self {
width,
..Default::default()
}
}
pub fn with_join(mut self, join: Join) -> Self {
self.join = join;
self
}
pub fn with_miter_limit(mut self, limit: f32) -> Self {
self.miter_limit = limit;
self
}
pub fn with_start_cap(mut self, cap: Cap) -> Self {
self.start_cap = cap;
self
}
pub fn with_end_cap(mut self, cap: Cap) -> Self {
self.end_cap = cap;
self
}
pub fn with_caps(mut self, cap: Cap) -> Self {
self.start_cap = cap;
self.end_cap = cap;
self
}
pub fn with_dashes<P>(mut self, offset: f32, pattern: P) -> Self
where
P: IntoIterator,
P::Item: Borrow<f32>,
{
self.dash_offset = offset;
self.dash_pattern.clear();
self.dash_pattern
.extend(pattern.into_iter().map(|dash| *dash.borrow()));
self
}
pub fn with_scale(mut self, yes: bool) -> Self {
self.scale = yes;
self
}
}
pub type Dashes = SmallVec<[f32; 4]>;
#[derive(Clone, Debug)]
pub enum Style {
Fill(Fill),
Stroke(Stroke),
}
impl From<Fill> for Style {
fn from(fill: Fill) -> Self {
Self::Fill(fill)
}
}
impl From<Stroke> for Style {
fn from(stroke: Stroke) -> Self {
Self::Stroke(stroke)
}
}
pub enum StyleRef<'a> {
Fill(Fill),
Stroke(&'a Stroke),
}
impl<'a> StyleRef<'a> {
pub fn to_owned(&self) -> Style {
match self {
Self::Fill(fill) => Style::Fill(*fill),
Self::Stroke(stroke) => Style::Stroke((*stroke).clone()),
}
}
}
impl From<Fill> for StyleRef<'_> {
fn from(fill: Fill) -> Self {
Self::Fill(fill)
}
}
impl<'a> From<&'a Stroke> for StyleRef<'a> {
fn from(stroke: &'a Stroke) -> Self {
Self::Stroke(stroke)
}
}
impl<'a> From<&'a Style> for StyleRef<'a> {
fn from(draw: &'a Style) -> Self {
match draw {
Style::Fill(fill) => Self::Fill(*fill),
Style::Stroke(stroke) => Self::Stroke(stroke),
}
}
}