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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use super::{
    Path,
    PathSegment,
};

/// A builder for [`Path`].
///
/// # Examples
///
/// Ellipse to path:
///
/// ```
/// use svgtypes::PathBuilder;
///
/// let (cx, cy, rx, ry) = (10.0, 20.0, 5.0, 8.0);
///
/// let path = PathBuilder::with_capacity(6)
///     .move_to(cx + rx, cy)
///     .arc_to(rx, ry, 0.0, false, true, cx,      cy + ry)
///     .arc_to(rx, ry, 0.0, false, true, cx - rx, cy)
///     .arc_to(rx, ry, 0.0, false, true, cx,      cy - ry)
///     .arc_to(rx, ry, 0.0, false, true, cx + rx, cy)
///     .close_path()
///     .finalize();
///
/// assert_eq!(path.to_string(), "M 15 20 A 5 8 0 0 1 10 28 A 5 8 0 0 1 5 20 \
///                               A 5 8 0 0 1 10 12 A 5 8 0 0 1 15 20 Z");
/// ```
///
/// [`Path`]: struct.Path.html
#[allow(missing_debug_implementations)]
pub struct PathBuilder {
    path: Path,
}

impl PathBuilder {
    /// Constructs a new builder.
    pub fn new() -> PathBuilder {
        PathBuilder { path: Path::new() }
    }

    /// Constructs a new builder with a specified capacity.
    pub fn with_capacity(capacity: usize) -> PathBuilder {
        PathBuilder { path: Path::with_capacity(capacity) }
    }

    // TODO: from existing Path

    /// Appends a new absolute MoveTo segment.
    pub fn move_to(mut self, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::MoveTo { abs: true, x, y });
        self
    }

    /// Appends a new relative MoveTo segment.
    pub fn rel_move_to(mut self, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::MoveTo { abs: false, x, y });
        self
    }

    /// Appends a new absolute ClosePath segment.
    pub fn close_path(mut self) -> PathBuilder {
        self.path.push(PathSegment::ClosePath { abs: true });
        self
    }

    /// Appends a new relative ClosePath segment.
    pub fn rel_close_path(mut self) -> PathBuilder {
        self.path.push(PathSegment::ClosePath { abs: false });
        self
    }

    /// Appends a new absolute LineTo segment.
    pub fn line_to(mut self, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::LineTo { abs: true, x, y });
        self
    }

    /// Appends a new relative LineTo segment.
    pub fn rel_line_to(mut self, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::LineTo { abs: false, x, y });
        self
    }

    /// Appends a new absolute HorizontalLineTo segment.
    pub fn hline_to(mut self, x: f64) -> PathBuilder {
        self.path.push(PathSegment::HorizontalLineTo { abs: true, x });
        self
    }

    /// Appends a new relative HorizontalLineTo segment.
    pub fn rel_hline_to(mut self, x: f64) -> PathBuilder {
        self.path.push(PathSegment::HorizontalLineTo { abs: false, x });
        self
    }

    /// Appends a new absolute VerticalLineTo segment.
    pub fn vline_to(mut self, y: f64) -> PathBuilder {
        self.path.push(PathSegment::VerticalLineTo { abs: true, y });
        self
    }

    /// Appends a new relative VerticalLineTo segment.
    pub fn rel_vline_to(mut self, y: f64) -> PathBuilder {
        self.path.push(PathSegment::VerticalLineTo { abs: false, y });
        self
    }

    /// Appends a new absolute CurveTo segment.
    pub fn curve_to(mut self, x1: f64, y1: f64, x2: f64, y2: f64, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::CurveTo { abs: true, x1, y1, x2, y2, x, y });
        self
    }

    /// Appends a new relative CurveTo segment.
    pub fn rel_curve_to(mut self, x1: f64, y1: f64, x2: f64, y2: f64, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::CurveTo { abs: false, x1, y1, x2, y2, x, y });
        self
    }

    /// Appends a new absolute SmoothCurveTo segment.
    pub fn smooth_curve_to(mut self, x2: f64, y2: f64, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::SmoothCurveTo { abs: true, x2, y2, x, y });
        self
    }

    /// Appends a new relative SmoothCurveTo segment.
    pub fn rel_smooth_curve_to(mut self, x2: f64, y2: f64, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::SmoothCurveTo { abs: false, x2, y2, x, y });
        self
    }

    /// Appends a new absolute QuadTo segment.
    pub fn quad_to(mut self, x1: f64, y1: f64, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::Quadratic { abs: true, x1, y1, x, y });
        self
    }

    /// Appends a new relative QuadTo segment.
    pub fn rel_quad_to(mut self, x1: f64, y1: f64, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::Quadratic { abs: false, x1, y1, x, y });
        self
    }

    /// Appends a new absolute SmoothQuadTo segment.
    pub fn smooth_quad_to(mut self, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::SmoothQuadratic { abs: true, x, y });
        self
    }

    /// Appends a new relative SmoothQuadTo segment.
    pub fn rel_smooth_quad_to(mut self, x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::SmoothQuadratic { abs: false, x, y });
        self
    }

    /// Appends a new absolute ArcTo segment.
    pub fn arc_to(mut self, rx: f64, ry: f64, x_axis_rotation: f64, large_arc: bool, sweep: bool,
                  x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::EllipticalArc { abs: true, rx, ry, x_axis_rotation,
                                                    large_arc, sweep, x, y });
        self
    }

    /// Appends a new relative ArcTo segment.
    pub fn rel_arc_to(mut self, rx: f64, ry: f64, x_axis_rotation: f64, large_arc: bool, sweep: bool,
                      x: f64, y: f64) -> PathBuilder {
        self.path.push(PathSegment::EllipticalArc { abs: false, rx, ry, x_axis_rotation,
                                                    large_arc, sweep, x, y });
        self
    }

    /// Finalizes the build.
    pub fn finalize(self) -> Path {
        self.path
    }
}