Skip to main content

rust_motion_paths/
types.rs

1//! SVG path types and data structures.
2
3use serde::{Deserialize, Serialize};
4
5/// 2D point coordinate.
6#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
7pub struct Point {
8    pub x: f64,
9    pub y: f64,
10}
11
12impl Point {
13    pub fn new(x: f64, y: f64) -> Self {
14        Self { x, y }
15    }
16}
17
18/// Represents an individual SVG path command instruction.
19#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
20pub enum Instruction {
21    /// `M x y` or `m x y`
22    MoveTo { x: f64, y: f64 },
23    /// `L x y` or `l x y`
24    LineTo { x: f64, y: f64 },
25    /// `C x1 y1 x2 y2 x y` or `c x1 y1 x2 y2 x y` (Cubic Bezier)
26    CubicCurveTo {
27        x1: f64,
28        y1: f64,
29        x2: f64,
30        y2: f64,
31        x: f64,
32        y: f64,
33    },
34    /// `Q x1 y1 x y` or `q x1 y1 x y` (Quadratic Bezier)
35    QuadCurveTo { x1: f64, y1: f64, x: f64, y: f64 },
36    /// `A rx ry x_axis_rotation large_arc_flag sweep_flag x y` or `a ...` (Elliptical Arc)
37    ArcTo {
38        rx: f64,
39        ry: f64,
40        x_axis_rotation: f64,
41        large_arc_flag: bool,
42        sweep_flag: bool,
43        x: f64,
44        y: f64,
45    },
46    /// `Z` or `z`
47    ClosePath,
48}
49
50/// Calculated SVG stroke properties for line drawing animation.
51#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
52pub struct EvolvedPath {
53    /// `stroke-dasharray` CSS property value.
54    pub stroke_dasharray: String,
55    /// `stroke-dashoffset` CSS property value.
56    pub stroke_dashoffset: f64,
57}
58
59/// Axis-aligned 2D bounding box of an SVG path.
60#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
61pub struct BoundingBox {
62    pub x: f64,
63    pub y: f64,
64    pub width: f64,
65    pub height: f64,
66}
67
68impl BoundingBox {
69    pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
70        Self {
71            x,
72            y,
73            width,
74            height,
75        }
76    }
77}