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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use crate::scalar::{Scalar, One};
use crate::generic_math::{Point, Vector, Rect};
use crate::{LineSegment, QuadraticBezierSegment, CubicBezierSegment};

use std::ops::Range;

/// Common APIs to segment types.
pub trait Segment: Copy + Sized {
    type Scalar: Scalar;

    /// Start of the curve.
    fn from(&self) -> Point<Self::Scalar>;

    /// End of the curve.
    fn to(&self) -> Point<Self::Scalar>;

    /// Sample the curve at t (expecting t between 0 and 1).
    fn sample(&self, t: Self::Scalar) -> Point<Self::Scalar>;

    /// Sample x at t (expecting t between 0 and 1).
    fn x(&self, t: Self::Scalar) -> Self::Scalar { self.sample(t).x }

    /// Sample y at t (expecting t between 0 and 1).
    fn y(&self, t: Self::Scalar) -> Self::Scalar { self.sample(t).y }

    /// Sample the derivative at t (expecting t between 0 and 1).
    fn derivative(&self, t: Self::Scalar) -> Vector<Self::Scalar>;

    /// Sample x derivative at t (expecting t between 0 and 1).
    fn dx(&self, t: Self::Scalar) -> Self::Scalar { self.derivative(t).x }

    /// Sample y derivative at t (expecting t between 0 and 1).
    fn dy(&self, t: Self::Scalar) -> Self::Scalar { self.derivative(t).y }

    /// Split this curve into two sub-curves.
    fn split(&self, t: Self::Scalar) -> (Self, Self);

    /// Return the curve before the split point.
    fn before_split(&self, t: Self::Scalar) -> Self;

    /// Return the curve after the split point.
    fn after_split(&self, t: Self::Scalar) -> Self;

    /// Return the curve inside a given range of t.
    ///
    /// This is equivalent splitting at the range's end points.
    fn split_range(&self, t_range: Range<Self::Scalar>) -> Self;

    /// Swap the direction of the segment.
    fn flip(&self) -> Self;

    /// Compute the length of the segment using a flattened approximation.
    fn approximate_length(&self, tolerance: Self::Scalar) -> Self::Scalar;
}

pub trait BoundingRect {
    type Scalar: Scalar;

    /// Returns a rectangle that contains the curve.
    fn bounding_rect(&self) -> Rect<Self::Scalar>;

    /// Returns a rectangle that contains the curve.
    ///
    /// This does not necessarily return the smallest possible bounding rectangle.
    fn fast_bounding_rect(&self) -> Rect<Self::Scalar> { self.bounding_rect() }

    /// Returns a range of x values that contains the curve.
    fn bounding_range_x(&self) -> (Self::Scalar, Self::Scalar);

    /// Returns a range of y values that contains the curve.
    fn bounding_range_y(&self) -> (Self::Scalar, Self::Scalar);

    /// Returns a range of x values that contains the curve.
    fn fast_bounding_range_x(&self) -> (Self::Scalar, Self::Scalar);

    /// Returns a range of y values that contains the curve.
    fn fast_bounding_range_y(&self) -> (Self::Scalar, Self::Scalar);
}

/// Types that implement call-back based iteration
pub trait FlattenedForEach: Segment {
    /// Iterates through the curve invoking a callback at each point.
    fn for_each_flattened<F: FnMut(Point<Self::Scalar>)>(&self, tolerance: Self::Scalar, call_back: &mut F);
}

/// Types that implement local flattening approximation at the start of the curve.
pub trait FlatteningStep: FlattenedForEach {
    /// Find the interval of the begining of the curve that can be approximated with a
    /// line segment.
    fn flattening_step(&self, tolerance: Self::Scalar) -> Self::Scalar;

    /// Returns the flattened representation of the curve as an iterator, starting *after* the
    /// current point.
    fn flattened(self, tolerance: Self::Scalar) -> Flattened<Self::Scalar, Self> {
        Flattened::new(self, tolerance)
    }
}

impl<T> FlattenedForEach for T
where T: FlatteningStep
{
    fn for_each_flattened<F: FnMut(Point<Self::Scalar>)>(&self, tolerance: Self::Scalar, call_back: &mut F) {
        let mut iter = *self;
        loop {
            let t = iter.flattening_step(tolerance);
            if t >= Self::Scalar::one() {
                call_back(iter.to());
                break;
            }
            iter = iter.after_split(t);
            call_back(iter.from());
        }
    }
}

/// An iterator over a generic curve segment that yields line segments approximating the
/// curve for a given approximation threshold.
///
/// The iterator starts at the first point *after* the origin of the curve and ends at the
/// destination.
pub struct Flattened<S, T> {
    curve: T,
    tolerance: S,
    done: bool,
}

impl<S: Scalar, T: FlatteningStep> Flattened<S, T> {
    pub fn new(curve: T, tolerance: S) -> Self {
        assert!(tolerance > S::ZERO);
        Flattened {
            curve: curve,
            tolerance: tolerance,
            done: false,
        }
    }
}

impl<S: Scalar, T: FlatteningStep<Scalar=S>> Iterator for Flattened<S, T>
{
    type Item = Point<S>;
    fn next(&mut self) -> Option<Point<S>> {
        if self.done {
            return None;
        }
        let t = self.curve.flattening_step(self.tolerance);
        if t >= S::ONE {
            self.done = true;
            return Some(self.curve.to());
        }
        self.curve = self.curve.after_split(t);
        return Some(self.curve.from());
    }
}

pub(crate) fn approximate_length_from_flattening<S: Scalar, T>(curve: &T, tolerance: S) -> S
where T: FlattenedForEach<Scalar=S>
{
    let mut start = curve.from();
    let mut len = S::ZERO;
    curve.for_each_flattened(tolerance, &mut|p| {
        len = len + (p - start).length();
        start = p;
    });
    return len;
}

macro_rules! impl_segment {
    ($S:ty) => (
        type Scalar = $S;
        fn from(&self) -> Point<$S> { self.from() }
        fn to(&self) -> Point<$S> { self.to() }
        fn sample(&self, t: $S) -> Point<$S> { self.sample(t) }
        fn x(&self, t: $S) -> $S { self.x(t) }
        fn y(&self, t: $S) -> $S { self.y(t) }
        fn derivative(&self, t: $S) -> Vector<$S> { self.derivative(t) }
        fn dx(&self, t: $S) -> $S { self.dx(t) }
        fn dy(&self, t: $S) -> $S { self.dy(t) }
        fn split(&self, t: $S) -> (Self, Self) { self.split(t) }
        fn before_split(&self, t: $S) -> Self { self.before_split(t) }
        fn after_split(&self, t: $S) -> Self { self.after_split(t) }
        fn split_range(&self, t_range: Range<$S>) -> Self { self.split_range(t_range) }
        fn flip(&self) -> Self { self.flip() }
        fn approximate_length(&self, tolerance: $S) -> $S {
            self.approximate_length(tolerance)
        }
    )
}

/// Either a cubic, quadratic or linear bézier segment.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum BezierSegment<S> {
    Linear(LineSegment<S>),
    Quadratic(QuadraticBezierSegment<S>),
    Cubic(CubicBezierSegment<S>),
}

impl<S: Scalar> BezierSegment<S> {
    #[inline]
    pub fn sample(&self, t: S) -> Point<S> {
        match self {
            BezierSegment::Linear(segment) => segment.sample(t),
            BezierSegment::Quadratic(segment) => segment.sample(t),
            BezierSegment::Cubic(segment) => segment.sample(t),
        }
    }

    #[inline]
    pub fn from(&self) -> Point<S> {
        match self {
            BezierSegment::Linear(segment) => segment.from,
            BezierSegment::Quadratic(segment) => segment.from,
            BezierSegment::Cubic(segment) => segment.from,
        }
    }

    #[inline]
    pub fn to(&self) -> Point<S> {
        match self {
            BezierSegment::Linear(segment) => segment.to,
            BezierSegment::Quadratic(segment) => segment.to,
            BezierSegment::Cubic(segment) => segment.to,
        }
    }

    #[inline]
    pub fn is_linear(&self, tolerance: S) -> bool {
        match self {
            BezierSegment::Linear(..) => true,
            BezierSegment::Quadratic(segment) => segment.is_linear(tolerance),
            BezierSegment::Cubic(segment) => segment.is_linear(tolerance),
        }
    }

    #[inline]
    pub fn baseline(&self) -> LineSegment<S> {
        match self {
            BezierSegment::Linear(segment) => *segment,
            BezierSegment::Quadratic(segment) => segment.baseline(),
            BezierSegment::Cubic(segment) => segment.baseline(),
        }
    }

    /// Split this segment into two sub-segments.
    pub fn split(&self, t: S) -> (BezierSegment<S>, BezierSegment<S>) {
        match self {
            BezierSegment::Linear(segment) => {
                let (a, b) = segment.split(t);
                (BezierSegment::Linear(a), BezierSegment::Linear(b))
            }
            BezierSegment::Quadratic(segment) => {
                let (a, b) = segment.split(t);
                (BezierSegment::Quadratic(a), BezierSegment::Quadratic(b))
            }
            BezierSegment::Cubic(segment) => {
                let (a, b) = segment.split(t);
                (BezierSegment::Cubic(a), BezierSegment::Cubic(b))
            }
        }
    }
}

impl<S> From<LineSegment<S>> for BezierSegment<S> {
    fn from(s: LineSegment<S>) -> Self {
        BezierSegment::Linear(s)
    }
}

impl<S> From<QuadraticBezierSegment<S>> for BezierSegment<S> {
    fn from(s: QuadraticBezierSegment<S>) -> Self {
        BezierSegment::Quadratic(s)
    }
}

impl<S> From<CubicBezierSegment<S>> for BezierSegment<S> {
    fn from(s: CubicBezierSegment<S>) -> Self {
        BezierSegment::Cubic(s)
    }
}