1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use super::Parameters;
use super::Position;

/// A command of a data attribute.
#[derive(Clone)]
pub enum Command {
    /// [Establish][1] a new current point.
    ///
    /// [1]: https://www.w3.org/TR/SVG/paths.html#PathDataMovetoCommands
    Move(Position, Parameters),

    /// [Draw][1] straight lines.
    ///
    /// [1]: https://www.w3.org/TR/SVG/paths.html#PathDataLinetoCommands
    Line(Position, Parameters),

    /// [Draw][1] horizontal lines.
    ///
    /// [1]: https://www.w3.org/TR/SVG/paths.html#PathDataLinetoCommands
    HorizontalLine(Position, Parameters),

    /// [Draw][1] vertical lines.
    ///
    /// [1]: https://www.w3.org/TR/SVG/paths.html#PathDataLinetoCommands
    VerticalLine(Position, Parameters),

    /// [Draw][1] a quadratic Bézier curve.
    ///
    /// [1]: https://www.w3.org/TR/SVG/paths.html#PathDataQuadraticBezierCommands
    QuadraticCurve(Position, Parameters),

    /// [Draw][1] a quadratic Bézier curve assuming the control point to be the
    /// reflection of the control point on the previous command relative to the
    /// current point.
    ///
    /// [1]: https://www.w3.org/TR/SVG/paths.html#PathDataQuadraticBezierCommands
    SmoothQuadraticCurve(Position, Parameters),

    /// [Draw][1] a cubic Bézier curve.
    ///
    /// [1]: https://www.w3.org/TR/SVG/paths.html#PathDataCubicBezierCommands
    CubicCurve(Position, Parameters),

    /// [Draw][1] a cubic Bézier curve assuming the first control point to be
    /// the reflection of the second control point on the previous command
    /// relative to the current point.
    ///
    /// [1]: https://www.w3.org/TR/SVG/paths.html#PathDataCubicBezierCommands
    SmoothCubicCurve(Position, Parameters),

    /// [Draw][1] an elliptical arc.
    ///
    /// [1]: https://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands
    EllipticalArc(Position, Parameters),

    /// [End][1] the current subpath.
    ///
    /// [1]: https://www.w3.org/TR/SVG/paths.html#PathDataClosePathCommand
    Close,
}

macro_rules! implement {
    ($($command:ident($position:ident) => $letter:expr,)*) => (
        impl From<Command> for String {
            fn from(command: Command) -> Self {
                use self::Command::*;
                use super::Position::*;
                match command {
                    $($command($position, parameters) => {
                        format!(concat!($letter, "{}"), String::from(parameters))
                    })*
                    Close => String::from("z"),
                }
            }
        }
    );
}

implement! {
    Move(Absolute) => "M",
    Move(Relative) => "m",
    Line(Absolute) => "L",
    Line(Relative) => "l",
    HorizontalLine(Absolute) => "H",
    HorizontalLine(Relative) => "h",
    VerticalLine(Absolute) => "V",
    VerticalLine(Relative) => "v",
    QuadraticCurve(Absolute) => "Q",
    QuadraticCurve(Relative) => "q",
    SmoothQuadraticCurve(Absolute) => "T",
    SmoothQuadraticCurve(Relative) => "t",
    CubicCurve(Absolute) => "C",
    CubicCurve(Relative) => "c",
    SmoothCubicCurve(Absolute) => "S",
    SmoothCubicCurve(Relative) => "s",
    EllipticalArc(Absolute) => "A",
    EllipticalArc(Relative) => "a",
}