use pltrs_core::path::convert::SketchParams;
use pltrs_core::{Affine2D, Bbox, Path, Rgba};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum CapStyle {
#[default]
Butt,
Projecting,
Round,
}
pltrs_core::archive_enum!(CapStyle {
Butt,
Projecting,
Round,
});
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum JoinStyle {
Miter,
#[default]
Round,
Bevel,
}
pltrs_core::archive_enum!(JoinStyle {
Miter,
Round,
Bevel,
});
impl CapStyle {
pub fn from_name(name: &str) -> Option<Self> {
Some(match name {
"butt" => Self::Butt,
"projecting" => Self::Projecting,
"round" => Self::Round,
_ => return None,
})
}
pub const fn name(self) -> &'static str {
match self {
Self::Butt => "butt",
Self::Projecting => "projecting",
Self::Round => "round",
}
}
}
impl JoinStyle {
pub fn from_name(name: &str) -> Option<Self> {
Some(match name {
"miter" => Self::Miter,
"round" => Self::Round,
"bevel" => Self::Bevel,
_ => return None,
})
}
pub const fn name(self) -> &'static str {
match self {
Self::Miter => "miter",
Self::Round => "round",
Self::Bevel => "bevel",
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Dashes {
pub offset: f64,
pub pattern: Option<Vec<f64>>,
}
pltrs_core::archive_struct!(Dashes { offset, pattern });
impl Dashes {
pub const fn solid() -> Self {
Self {
offset: 0.0,
pattern: None,
}
}
pub const fn new(offset: f64, pattern: Vec<f64>) -> Self {
Self {
offset,
pattern: Some(pattern),
}
}
pub fn lengths(&self) -> &[f64] {
self.pattern.as_deref().unwrap_or_default()
}
pub fn is_solid(&self) -> bool {
self.lengths().iter().all(|&len| len <= 0.0)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Hatch {
pub pattern: String,
pub color: Rgba,
pub linewidth: f64,
}
pltrs_core::archive_struct!(Hatch {
pattern,
color,
linewidth
});
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Sketch {
pub scale: f64,
pub length: f64,
pub randomness: f64,
}
pltrs_core::archive_struct!(Sketch {
scale,
length,
randomness
});
impl Sketch {
pub(crate) fn params(self) -> SketchParams {
SketchParams::new(self.scale, Some(self.length), Some(self.randomness))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ClipPath {
pub path: Path,
pub transform: Affine2D,
pub rebuilt: bool,
}
impl ClipPath {
#[must_use]
pub fn new(path: Path, transform: Affine2D) -> Self {
Self {
path,
transform,
rebuilt: false,
}
}
#[must_use]
pub fn with_rebuilt(self, rebuilt: bool) -> Self {
Self { rebuilt, ..self }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum SnapMode {
#[default]
Auto,
Always,
Never,
}
pltrs_core::archive_enum!(SnapMode {
Auto,
Always,
Never,
});
#[derive(Clone, Debug, PartialEq)]
pub struct GraphicsContext {
pub foreground: Rgba,
pub alpha: Option<f64>,
pub antialiased: bool,
pub cap: CapStyle,
pub join: JoinStyle,
pub linewidth: f64,
pub dashes: Dashes,
pub clip_rect: Option<Bbox>,
pub clip_path: Option<ClipPath>,
pub hatch: Option<Hatch>,
pub snap: SnapMode,
pub sketch: Option<Sketch>,
pub url: Option<String>,
pub gid: Option<String>,
}
impl Default for GraphicsContext {
fn default() -> Self {
Self {
foreground: Rgba::BLACK,
alpha: None,
antialiased: true,
cap: CapStyle::Butt,
join: JoinStyle::Round,
linewidth: 1.0,
dashes: Dashes::solid(),
clip_rect: None,
clip_path: None,
hatch: None,
snap: SnapMode::Auto,
sketch: None,
url: None,
gid: None,
}
}
}
impl GraphicsContext {
pub fn stroke_color(&self) -> Rgba {
self.apply_alpha(self.foreground)
}
pub fn fill_color(&self, face: Rgba) -> Rgba {
self.apply_alpha(face)
}
pub fn strokes(&self) -> bool {
self.linewidth > 0.0 && !self.stroke_color().is_transparent()
}
fn apply_alpha(&self, color: Rgba) -> Rgba {
self.alpha.map_or(color, |a| color.with_alpha(a))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn alpha_override_applies_to_stroke_and_fill() {
let gc = GraphicsContext {
foreground: Rgba::new(1.0, 0.0, 0.0, 0.2),
alpha: Some(0.5),
..GraphicsContext::default()
};
assert_eq!(gc.stroke_color().a, 0.5);
assert_eq!(gc.fill_color(Rgba::WHITE).a, 0.5);
assert!(gc.strokes());
}
#[test]
fn style_names_round_trip() {
for cap in [CapStyle::Butt, CapStyle::Projecting, CapStyle::Round] {
assert_eq!(CapStyle::from_name(cap.name()), Some(cap));
}
for join in [JoinStyle::Miter, JoinStyle::Round, JoinStyle::Bevel] {
assert_eq!(JoinStyle::from_name(join.name()), Some(join));
}
assert_eq!(CapStyle::from_name("square"), None);
}
#[test]
fn zero_length_dash_patterns_are_solid() {
assert!(Dashes::solid().is_solid());
assert!(Dashes::new(0.0, vec![0.0, 0.0]).is_solid());
assert!(Dashes::new(2.0, Vec::new()).is_solid());
assert!(!Dashes::new(0.0, vec![3.0, 1.0]).is_solid());
assert_eq!(Dashes::solid().lengths(), []);
}
}