use crate::ast::{Color, PixelFilter};
use crate::svg::emit_svg;
use std::ops::Mul;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Transform2D {
pub a: f64,
pub b: f64,
pub c: f64,
pub d: f64,
pub tx: f64,
pub ty: f64,
}
impl Transform2D {
pub const fn identity() -> Self {
Self {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
tx: 0.0,
ty: 0.0,
}
}
pub fn from_translation(tx: f64, ty: f64) -> Self {
Self {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
tx,
ty,
}
}
pub fn from_rotation(angle_rad: f64) -> Self {
let (sin, cos) = angle_rad.sin_cos();
Self {
a: cos,
b: sin,
c: -sin,
d: cos,
tx: 0.0,
ty: 0.0,
}
}
pub fn from_scale(sx: f64, sy: f64) -> Self {
Self {
a: sx,
b: 0.0,
c: 0.0,
d: sy,
tx: 0.0,
ty: 0.0,
}
}
pub fn mul(&self, o: &Transform2D) -> Self {
Self {
a: self.a * o.a + self.c * o.b,
b: self.b * o.a + self.d * o.b,
c: self.a * o.c + self.c * o.d,
d: self.b * o.c + self.d * o.d,
tx: self.a * o.tx + self.c * o.ty + self.tx,
ty: self.b * o.tx + self.d * o.ty + self.ty,
}
}
pub fn transform_point(&self, p: (f64, f64)) -> (f64, f64) {
(
self.a * p.0 + self.c * p.1 + self.tx,
self.b * p.0 + self.d * p.1 + self.ty,
)
}
}
impl Default for Transform2D {
fn default() -> Self {
Self::identity()
}
}
impl Mul for Transform2D {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Transform2D::mul(&self, &rhs)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TextAlign {
#[default]
Left,
Center,
Right,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineCap {
#[default]
Butt,
Round,
Square,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineJoin {
#[default]
Miter,
Round,
Bevel,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StrokeAlign {
#[default]
Center,
Inside,
Outside,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BlendMode {
#[default]
Normal,
Add,
Multiply,
Screen,
Overlay,
}
#[derive(Debug, Clone, PartialEq)]
pub struct GradientStop {
pub offset: f64,
pub color: Color,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Paint {
Color(Color),
Linear {
start: (f64, f64),
end: (f64, f64),
stops: Vec<GradientStop>,
},
Radial {
center: (f64, f64),
radius: f64,
focal: Option<(f64, f64)>,
stops: Vec<GradientStop>,
},
Angular {
center: (f64, f64),
start_angle: f64,
stops: Vec<GradientStop>,
},
Pattern(String),
}
impl Default for Paint {
fn default() -> Self {
Paint::Color(Color::BLACK)
}
}
impl From<Color> for Paint {
fn from(c: Color) -> Self {
Paint::Color(c)
}
}
impl Paint {
pub fn as_color(&self) -> Option<&Color> {
match self {
Paint::Color(c) => Some(c),
_ => None,
}
}
pub fn is_none(&self) -> bool {
match self {
Paint::Color(c) => c.is_none(),
Paint::Pattern(_) => false,
_ => false,
}
}
pub fn to_svg_string(&self) -> String {
match self {
Paint::Color(c) => c.to_svg_string(),
Paint::Pattern(name) => format!("url(#pvg-pat-{})", name),
_ => "url(#pvg-grad)".to_string(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Shadow {
pub offset: (f64, f64),
pub radius: f64,
pub color: Color,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Glow {
pub radius: f64,
pub color: Color,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DrawStyle {
pub fill: Paint,
pub stroke: Paint,
pub width: f64,
pub opacity: f64,
pub cap: LineCap,
pub join: LineJoin,
pub miter: f64,
pub dash: Vec<f64>,
pub stroke_align: StrokeAlign,
pub blend: BlendMode,
pub blur: f64,
pub shadow: Option<Shadow>,
pub glow: Option<Glow>,
}
impl Default for DrawStyle {
fn default() -> Self {
Self {
fill: Paint::Color(Color::BLACK),
stroke: Paint::Color(Color::None),
width: 1.0,
opacity: 1.0,
cap: LineCap::Butt,
join: LineJoin::Miter,
miter: 4.0,
dash: Vec::new(),
stroke_align: StrokeAlign::Center,
blend: BlendMode::Normal,
blur: 0.0,
shadow: None,
glow: None,
}
}
}
impl DrawStyle {
pub fn with_fill(mut self, fill: Color) -> Self {
self.fill = Paint::Color(fill);
self
}
pub fn with_paint(mut self, fill: Paint) -> Self {
self.fill = fill;
self
}
pub fn with_stroke(mut self, stroke: Color) -> Self {
self.stroke = Paint::Color(stroke);
self
}
pub fn with_stroke_paint(mut self, stroke: Paint) -> Self {
self.stroke = stroke;
self
}
pub fn with_width(mut self, width: f64) -> Self {
self.width = width;
self
}
pub fn with_opacity(mut self, opacity: f64) -> Self {
self.opacity = opacity;
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum DrawCmd {
Circle {
center: (f64, f64),
radius: f64,
style: DrawStyle,
},
Ellipse {
center: (f64, f64),
radius: (f64, f64),
style: DrawStyle,
},
Rectangle {
pos: (f64, f64),
size: (f64, f64),
corner_radius: f64,
style: DrawStyle,
},
Line {
from: (f64, f64),
to: (f64, f64),
style: DrawStyle,
},
Polygon {
points: Vec<(f64, f64)>,
style: DrawStyle,
},
Path {
commands: Vec<DrawPathCommand>,
style: DrawStyle,
},
Text {
pos: (f64, f64),
content: String,
size: f64,
font_family: String,
align: TextAlign,
style: DrawStyle,
},
Sprite {
pos: (f64, f64),
palette: Vec<Color>,
rows: Vec<String>,
scale: f64,
style: DrawStyle,
},
Spline {
points: Vec<(f64, f64)>,
style: DrawStyle,
},
Clip {
mask: Box<DrawCmd>,
content: Vec<DrawCmd>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct DrawPattern {
pub name: String,
pub width: f64,
pub height: f64,
pub tiles: Vec<DrawCmd>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DrawPathCommand {
Start((f64, f64)),
Line((f64, f64)),
Quad { cp: (f64, f64), ep: (f64, f64) },
Curve { c1: (f64, f64), c2: (f64, f64), ep: (f64, f64) },
Arc {
center: (f64, f64),
radius: f64,
start_angle: f64,
end_angle: f64,
},
Close,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DrawList {
pub canvas_width: f64,
pub canvas_height: f64,
pub background: Option<Color>,
pub snap: f64,
pub pixel_filter: PixelFilter,
pub patterns: Vec<DrawPattern>,
pub items: Vec<DrawCmd>,
}
impl DrawList {
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn to_svg(&self) -> String {
emit_svg(self)
}
}