Skip to main content

vectordraw/
backend.rs

1pub struct DrawOptions {
2    fill_color: Option<(u8, u8, u8)>,
3    stroke_color: Option<(u8, u8, u8)>,
4    stroke_size: f64,
5}
6
7impl DrawOptions {
8    pub fn default() -> DrawOptions {
9        DrawOptions {
10            fill_color: None,
11            stroke_color: Some((0, 0, 0)),
12            stroke_size: 2.0,
13        }
14    }
15
16    pub fn stroked((r, g, b): (u8, u8, u8), size: f64) -> DrawOptions {
17        DrawOptions {
18            fill_color: None,
19            stroke_color: Some((r, g, b)),
20            stroke_size: size,
21        }
22    }
23
24    pub fn filled((r, g, b): (u8, u8, u8)) -> DrawOptions {
25        DrawOptions {
26            fill_color: Some((r, g, b)),
27            stroke_color: None,
28            stroke_size: 0.0,
29        }
30    }
31}
32
33pub enum Command {
34    StartShape(DrawOptions),
35    MoveTo {
36        x: f64,
37        y: f64
38    },
39    LineTo {
40        x: f64,
41        y: f64
42    },
43    CubicCurveTo {
44        // Control Point 1
45        cx1: f64,
46        cy1: f64,
47        // Control Point 2
48        cx2: f64,
49        cy2: f64,
50        // End Point
51        x: f64,
52        y: f64,
53    },
54    QuadraticCurveTo {
55        // Control Point
56        cx: f64,
57        cy: f64,
58        // End Point
59        x: f64,
60        y: f64,
61    },
62    // https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
63    ArcTo {
64        // Radius of of Elipse
65        rx: f64,
66        ry: f64,
67        // x axis rotation
68        rotation: f64,
69        // Which path to use
70        large_arc: bool,
71        sweep: bool,
72        // End Point
73        x: f64,
74        y: f64,
75    },
76    CloseShape,
77    EndShape,
78}
79
80pub trait DrawBackend {
81    type Error;
82    fn apply(&mut self, command: Command) -> Result<(), Self::Error>;
83    fn close(self) -> Result<(), Self::Error>;
84}