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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! TrueType style outline to path conversion.

use super::pen::{OutlinePen, PathStyle};
use core::fmt;
use raw::{
    tables::glyf::{PointCoord, PointFlags},
    types::Point,
};

/// Errors that can occur when converting an outline to a path.
#[derive(Clone, Debug)]
pub enum ToPathError {
    /// Contour end point at this index was less than its preceding end point.
    ContourOrder(usize),
    /// Expected a quadratic off-curve point at this index.
    ExpectedQuad(usize),
    /// Expected a quadratic off-curve or on-curve point at this index.
    ExpectedQuadOrOnCurve(usize),
    /// Expected a cubic off-curve point at this index.
    ExpectedCubic(usize),
    /// Expected number of points to == number of flags
    PointFlagMismatch { num_points: usize, num_flags: usize },
}

impl fmt::Display for ToPathError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::ContourOrder(ix) => write!(
                f,
                "Contour end point at index {ix} was less than preceding end point"
            ),
            Self::ExpectedQuad(ix) => write!(f, "Expected quadatic off-curve point at index {ix}"),
            Self::ExpectedQuadOrOnCurve(ix) => write!(
                f,
                "Expected quadatic off-curve or on-curve point at index {ix}"
            ),
            Self::ExpectedCubic(ix) => write!(f, "Expected cubic off-curve point at index {ix}"),
            Self::PointFlagMismatch {
                num_points,
                num_flags,
            } => write!(
                f,
                "Number of points ({num_points}) and flags ({num_flags}) must match"
            ),
        }
    }
}

/// Converts a `glyf` outline described by points, flags and contour end points
/// to a sequence of path elements and invokes the appropriate callback on the
/// given pen for each.
///
/// The input points can have any coordinate type that implements
/// [`PointCoord`]. Output points are always generated in `f32`.
///
/// This is roughly equivalent to [`FT_Outline_Decompose`](https://freetype.org/freetype2/docs/reference/ft2-outline_processing.html#ft_outline_decompose).
///
/// See [`contour_to_path`] for a more general function that takes an iterator
/// if your outline data is in a different format.
pub(crate) fn to_path<C: PointCoord>(
    points: &[Point<C>],
    flags: &[PointFlags],
    contours: &[u16],
    path_style: PathStyle,
    pen: &mut impl OutlinePen,
) -> Result<(), ToPathError> {
    for contour_ix in 0..contours.len() {
        let start_ix = (contour_ix > 0)
            .then(|| contours[contour_ix - 1] as usize + 1)
            .unwrap_or_default();
        let end_ix = contours[contour_ix] as usize;
        if end_ix < start_ix || end_ix >= points.len() {
            return Err(ToPathError::ContourOrder(contour_ix));
        }
        let points = &points[start_ix..=end_ix];
        if points.is_empty() {
            continue;
        }
        let flags = flags
            .get(start_ix..=end_ix)
            .ok_or(ToPathError::PointFlagMismatch {
                num_points: points.len(),
                num_flags: flags.len(),
            })?;
        let last_point = points.last().unwrap();
        let last_flags = flags.last().unwrap();
        let last_point = ContourPoint {
            x: last_point.x,
            y: last_point.y,
            flags: *last_flags,
        };
        contour_to_path(
            points.iter().zip(flags).map(|(point, flags)| ContourPoint {
                x: point.x,
                y: point.y,
                flags: *flags,
            }),
            last_point,
            path_style,
            pen,
        )
        .map_err(|e| match &e {
            ToPathError::ExpectedCubic(ix) => ToPathError::ExpectedCubic(ix + start_ix),
            ToPathError::ExpectedQuad(ix) => ToPathError::ExpectedQuad(ix + start_ix),
            ToPathError::ExpectedQuadOrOnCurve(ix) => {
                ToPathError::ExpectedQuadOrOnCurve(ix + start_ix)
            }
            _ => e,
        })?
    }
    Ok(())
}

/// Combination of point coordinates and flags.
#[derive(Copy, Clone, Default, Debug)]
pub(crate) struct ContourPoint<T> {
    pub x: T,
    pub y: T,
    pub flags: PointFlags,
}

impl<T> ContourPoint<T>
where
    T: PointCoord,
{
    fn point_f32(&self) -> Point<f32> {
        Point::new(self.x.to_f32(), self.y.to_f32())
    }

    fn midpoint(&self, other: Self) -> ContourPoint<T> {
        let (x, y) = (self.x.midpoint(other.x), self.y.midpoint(other.y));
        Self {
            x,
            y,
            flags: other.flags,
        }
    }
}

/// Generates a path from an iterator of contour points.
///
/// Note that this requires the last point of the contour to be passed
/// separately to support FreeType style path conversion when the contour
/// begins with an off curve point. The points iterator should still
/// yield the last point as well.
///
/// This is more general than [`to_path`] and exists to support cases (such as
/// autohinting) where the source outline data is in a different format.
pub(crate) fn contour_to_path<C: PointCoord>(
    points: impl Iterator<Item = ContourPoint<C>>,
    last_point: ContourPoint<C>,
    style: PathStyle,
    pen: &mut impl OutlinePen,
) -> Result<(), ToPathError> {
    let mut points = points.enumerate().peekable();
    let Some((_, first_point)) = points.peek().copied() else {
        // This is an empty contour
        return Ok(());
    };
    // We don't accept an off curve cubic as the first point
    if first_point.flags.is_off_curve_cubic() {
        return Err(ToPathError::ExpectedQuadOrOnCurve(0));
    }
    // For FreeType style, we may need to omit the last point if we find the
    // first on curve there
    let mut omit_last = false;
    // For HarfBuzz style, may skip up to two points in finding the start, so
    // process these at the end
    let mut trailing_points = [None; 2];
    // Find our starting point
    let start_point = if first_point.flags.is_off_curve_quad() {
        // We're starting with an off curve, so select our first move based on
        // the path style
        match style {
            PathStyle::FreeType => {
                if last_point.flags.is_on_curve() {
                    // The last point is an on curve, so let's start there
                    omit_last = true;
                    last_point
                } else {
                    // It's also an off curve, so take implicit midpoint
                    last_point.midpoint(first_point)
                }
            }
            PathStyle::HarfBuzz => {
                // Always consume the first point
                points.next();
                // Then check the next point
                let Some((_, next_point)) = points.peek().copied() else {
                    // This is a single point contour
                    return Ok(());
                };
                if next_point.flags.is_on_curve() {
                    points.next();
                    trailing_points = [Some((0, first_point)), Some((1, next_point))];
                    // Next is on curve, so let's start there
                    next_point
                } else {
                    // It's also an off curve, so take the implicit midpoint
                    trailing_points = [Some((0, first_point)), None];
                    first_point.midpoint(next_point)
                }
            }
        }
    } else {
        // We're starting with an on curve, so consume the point
        points.next();
        first_point
    };
    let point = start_point.point_f32();
    pen.move_to(point.x, point.y);
    let mut state = PendingState::default();
    if omit_last {
        while let Some((ix, point)) = points.next() {
            if points.peek().is_none() {
                break;
            }
            state.emit(ix, point, pen)?;
        }
    } else {
        for (ix, point) in points {
            state.emit(ix, point, pen)?;
        }
    }
    for (ix, point) in trailing_points.iter().filter_map(|x| *x) {
        state.emit(ix, point, pen)?;
    }
    state.finish(0, start_point, pen)?;
    Ok(())
}

#[derive(Copy, Clone, Default)]
enum PendingState<C> {
    /// No pending points.
    #[default]
    Empty,
    /// Pending off-curve quad point.
    PendingQuad(ContourPoint<C>),
    /// Single pending off-curve cubic point.
    PendingCubic(ContourPoint<C>),
    /// Two pending off-curve cubic points.
    TwoPendingCubics(ContourPoint<C>, ContourPoint<C>),
}

impl<C> PendingState<C>
where
    C: PointCoord,
{
    #[inline(always)]
    fn emit(
        &mut self,
        ix: usize,
        point: ContourPoint<C>,
        pen: &mut impl OutlinePen,
    ) -> Result<(), ToPathError> {
        let flags = point.flags;
        match *self {
            Self::Empty => {
                if flags.is_off_curve_quad() {
                    *self = Self::PendingQuad(point);
                } else if flags.is_off_curve_cubic() {
                    *self = Self::PendingCubic(point);
                } else {
                    let p = point.point_f32();
                    pen.line_to(p.x, p.y);
                }
            }
            Self::PendingQuad(quad) => {
                if flags.is_off_curve_quad() {
                    let c0 = quad.point_f32();
                    let p = quad.midpoint(point).point_f32();
                    pen.quad_to(c0.x, c0.y, p.x, p.y);
                    *self = Self::PendingQuad(point);
                } else if flags.is_off_curve_cubic() {
                    return Err(ToPathError::ExpectedQuadOrOnCurve(ix));
                } else {
                    let c0 = quad.point_f32();
                    let p = point.point_f32();
                    pen.quad_to(c0.x, c0.y, p.x, p.y);
                    *self = Self::Empty;
                }
            }
            Self::PendingCubic(cubic) => {
                if flags.is_off_curve_cubic() {
                    *self = Self::TwoPendingCubics(cubic, point);
                } else {
                    return Err(ToPathError::ExpectedCubic(ix));
                }
            }
            Self::TwoPendingCubics(cubic0, cubic1) => {
                if flags.is_off_curve_quad() {
                    return Err(ToPathError::ExpectedCubic(ix));
                } else if flags.is_off_curve_cubic() {
                    let c0 = cubic0.point_f32();
                    let c1 = cubic1.point_f32();
                    let p = cubic1.midpoint(point).point_f32();
                    pen.curve_to(c0.x, c0.y, c1.x, c1.y, p.x, p.y);
                    *self = Self::PendingCubic(point);
                } else {
                    let c0 = cubic0.point_f32();
                    let c1 = cubic1.point_f32();
                    let p = point.point_f32();
                    pen.curve_to(c0.x, c0.y, c1.x, c1.y, p.x, p.y);
                    *self = Self::Empty;
                }
            }
        }
        Ok(())
    }

    fn finish(
        mut self,
        start_ix: usize,
        mut start_point: ContourPoint<C>,
        pen: &mut impl OutlinePen,
    ) -> Result<(), ToPathError> {
        match self {
            Self::Empty => {}
            _ => {
                // We always want to end with an explicit on-curve
                start_point.flags = PointFlags::on_curve();
                self.emit(start_ix, start_point, pen)?;
            }
        }
        pen.close();
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::{super::pen::SvgPen, *};
    use raw::types::F26Dot6;

    fn assert_off_curve_path_to_svg(expected: &str, path_style: PathStyle, all_off_curve: bool) {
        fn pt(x: i32, y: i32) -> Point<F26Dot6> {
            Point::new(x, y).map(F26Dot6::from_bits)
        }
        let mut flags = [PointFlags::off_curve_quad(); 4];
        if !all_off_curve {
            flags[1] = PointFlags::on_curve();
        }
        let contours = [3];
        // This test is meant to prevent a bug where the first move-to was computed improperly
        // for a contour consisting of all off curve points.
        // In this case, the start of the path should be the midpoint between the first and last points.
        // For this test case (in 26.6 fixed point): [(640, 128) + (128, 128)] / 2 = (384, 128)
        // which becomes (6.0, 2.0) when converted to floating point.
        let points = [pt(640, 128), pt(256, 64), pt(640, 64), pt(128, 128)];
        let mut pen = SvgPen::with_precision(1);
        to_path(&points, &flags, &contours, path_style, &mut pen).unwrap();
        assert_eq!(pen.as_ref(), expected);
    }

    #[test]
    fn all_off_curve_to_path_scan_backward() {
        assert_off_curve_path_to_svg(
            "M6.0,2.0 Q10.0,2.0 7.0,1.5 Q4.0,1.0 7.0,1.0 Q10.0,1.0 6.0,1.5 Q2.0,2.0 6.0,2.0 Z",
            PathStyle::FreeType,
            true,
        );
    }

    #[test]
    fn all_off_curve_to_path_scan_forward() {
        assert_off_curve_path_to_svg(
            "M7.0,1.5 Q4.0,1.0 7.0,1.0 Q10.0,1.0 6.0,1.5 Q2.0,2.0 6.0,2.0 Q10.0,2.0 7.0,1.5 Z",
            PathStyle::HarfBuzz,
            true,
        );
    }

    #[test]
    fn start_off_curve_to_path_scan_backward() {
        assert_off_curve_path_to_svg(
            "M6.0,2.0 Q10.0,2.0 4.0,1.0 Q10.0,1.0 6.0,1.5 Q2.0,2.0 6.0,2.0 Z",
            PathStyle::FreeType,
            false,
        );
    }

    #[test]
    fn start_off_curve_to_path_scan_forward() {
        assert_off_curve_path_to_svg(
            "M4.0,1.0 Q10.0,1.0 6.0,1.5 Q2.0,2.0 6.0,2.0 Q10.0,2.0 4.0,1.0 Z",
            PathStyle::HarfBuzz,
            false,
        );
    }
}