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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//! Tools to iterate over paths.
//!
//! # Lyon path iterators
//!
//! ## Overview
//!
//! This module provides a collection of traits to extend the `Iterator` trait when
//! iterating over paths.
//!
//! ## Examples
//!
//! ```
//! extern crate lyon_path;
//! use lyon_path::iterator::*;
//! use lyon_path::math::{point, vector};
//! use lyon_path::{PathEvent, SvgEvent, FlattenedEvent};
//!
//! fn main() {
//!     let events = vec![
//!         SvgEvent::MoveTo(point(1.0, 1.0)),
//!         SvgEvent::RelativeQuadraticTo(vector(4.0, 5.0), vector(-1.0, 4.0)),
//!         SvgEvent::CubicTo(point(3.0, 0.0), point(3.0, 1.0), point(10.0, -3.0)),
//!         SvgEvent::Close,
//!     ];
//!
//!     // A simple std::iter::Iterator<SvgEvent>,
//!     let simple_iter = events.iter().cloned();
//!
//!     // Make it a SvgIterator (keeps tracks of the path state).
//!     let svg_path_iter = SvgPathIter::new(simple_iter);
//!
//!     // Make it a PathEvent iterator.
//!     let path_iter = svg_path_iter.path_events();
//!
//!     // Make it an iterator over even simpler primitives: FlattenedEvent,
//!     // which do not contain any curve. To do so we approximate each curve
//!     // linear segments according to a tolerance threshold which controls
//!     // the tradeoff between fidelity of the approximation and amount of
//!     // generated events. Let's use a tolerance threshold of 0.01.
//!     // The beauty of this approach is that the flattening happens lazily
//!     // while iterating without allocating memory for the path.
//!     let flattened_iter = path_iter.flattened(0.01);
//!
//!     for evt in flattened_iter {
//!         match evt {
//!             FlattenedEvent::MoveTo(p) => { println!(" - move to {:?}", p); }
//!             FlattenedEvent::Line(segment) => { println!(" - line {:?}", segment); }
//!             FlattenedEvent::Close(segment) => { println!(" - close {:?}", segment); }
//!         }
//!     }
//! }
//! ```
//!
//! An equivalent (shorter) version of the above code takes advantage of the
//! fact you can get a flattening iterator directly from an `SvgIterator`:
//!
//! ```
//! extern crate lyon_path;
//! use lyon_path::iterator::*;
//! use lyon_path::math::{point, vector};
//! use lyon_path::SvgEvent;
//!
//! fn main() {
//!     let events = vec![
//!         SvgEvent::MoveTo(point(1.0, 1.0)),
//!         SvgEvent::RelativeQuadraticTo(vector(4.0, 5.0), vector(-1.0, 4.0)),
//!         SvgEvent::SmoothCubicTo(point(3.0, 1.0), point(10.0, -3.0)),
//!         SvgEvent::Close,
//!     ];
//!
//!     for evt in SvgPathIter::new(events.iter().cloned()).flattened(0.01) {
//!         // ...
//!     }
//! }
//! ```
//!
//! Sometimes, working with segments directly without dealing with MoveTo/Close events
//! can be more convenient:
//!
//! ```
//! extern crate lyon_path;
//! use lyon_path::iterator::*;
//! use lyon_path::math::{point, vector};
//! use lyon_path::geom::BezierSegment;
//! use lyon_path::Path;
//!
//! fn main() {
//!     // In practice it is more common to iterate over Path objects than vectors
//!     // of SVG commands (the former can be constructed from the latter).
//!     let mut builder = Path::builder();
//!     builder.move_to(point(1.0, 1.0));
//!     builder.line_to(point(2.0, 1.0));
//!     builder.quadratic_bezier_to(point(2.0, 2.0), point(1.0, 2.0));
//!     builder.cubic_bezier_to(point(0.0, 2.0), point(0.0, 0.0), point(1.0, 0.0));
//!     let path = builder.build();
//!
//!     // Iterate over bézier segments directly.
//!     for segment in path.iter().bezier_segments() {
//!         match segment {
//!             BezierSegment::Linear(segment) => { println!("{:?}", segment); }
//!             BezierSegment::Quadratic(segment) => { println!("{:?}", segment); }
//!             BezierSegment::Cubic(segment) => { println!("{:?}", segment); }
//!         }
//!     }
//!
//!     // It is also possible to iterate over line segments directly with flattened paths.
//!     for segment in path.iter().flattened(0.1).line_segments() {
//!         println!("line segment {:?} -> {:?}", segment.from, segment.to);
//!     }
//! }
//! ```
//!
//! Chaining the provided iterators allow performing some path manipulations lazily
//! without allocating actual path objects to hold the result of the transformations.
//!
//! ```
//! extern crate lyon_path;
//! use lyon_path::iterator::*;
//! use lyon_path::geom::euclid::{Angle, Transform2D};
//! use lyon_path::math::point;
//! use lyon_path::Path;
//!
//! fn main() {
//!     // In practice it is more common to iterate over Path objects than vectors
//!     // of SVG commands (the former can be constructed from the latter).
//!     let mut builder = Path::builder();
//!     builder.move_to(point(1.0, 1.0));
//!     builder.line_to(point(2.0, 1.0));
//!     builder.quadratic_bezier_to(point(2.0, 2.0), point(1.0, 2.0));
//!     builder.cubic_bezier_to(point(0.0, 2.0), point(0.0, 0.0), point(1.0, 0.0));
//!     builder.close();
//!     let path = builder.build();
//!
//!     let mut transform = Transform2D::create_rotation(Angle::radians(1.0));
//!
//!     for evt in path.iter().transformed(&transform).bezier_segments() {
//!         // ...
//!     }
//! }
//! ```
use std::iter;

use math::*;
use {PathEvent, SvgEvent, FlattenedEvent, QuadraticEvent, PathState};
use geom::{BezierSegment, QuadraticBezierSegment, CubicBezierSegment, LineSegment, quadratic_bezier, cubic_bezier};
use geom::arc::*;
use geom::arrayvec::ArrayVec;
use builder::SvgBuilder;

/// An extension trait for `PathEvent` iterators.
pub trait PathIterator: Iterator<Item = PathEvent> + Sized {

    /// Returns an iterator that turns curves into line segments.
    fn flattened(self, tolerance: f32) -> Flattened<Self> {
        Flattened::new(tolerance, self)
    }

    /// Returns an iterator applying a 2D transform to all of its events.
    fn transformed(self, mat: &Transform2D) -> Transformed<Self> {
        Transformed::new(mat, self)
    }

    /// Returns an iterator of segments.
    fn bezier_segments(self) -> BezierSegments<Self> {
        BezierSegments { iter: self }
    }
}

impl<Iter> PathIterator for Iter
where
    Iter: Iterator<Item = PathEvent>,
{}

/// An extension to the common Iterator interface, that adds information which is useful when
/// chaining path-specific iterators.
pub trait SvgIterator: Iterator<Item = SvgEvent> + Sized {
    /// The returned structure exposes the current position, the first position in the current
    /// sub-path, and the position of the last control point.
    fn path_state(&self) -> &PathState;

    /// Returns an iterator of FlattenedEvents, turning curves into sequences of line segments.
    fn flattened(self, tolerance: f32) -> Flattened<PathEvents<Self>> {
        self.path_events().flattened(tolerance)
    }

    /// Returns an iterator of path events.
    fn path_events(self) -> PathEvents<Self> { PathEvents::new(self) }
}

/// An extension to the common Iterator interface, that adds information which is useful when
/// chaining path-specific iterators.
pub trait FlattenedIterator: Iterator<Item = FlattenedEvent> + Sized {

    /// Returns an iterator of path events.
    fn path_events(self) -> iter::Map<Self, fn(FlattenedEvent) -> PathEvent> {
        self.map(flattened_to_path_event)
    }

    /// Returns an iterator of svg events.
    fn svg_events(self) -> iter::Map<Self, fn(FlattenedEvent) -> SvgEvent> {
        self.map(flattened_to_svg_event)
    }

    /// Returns an iterator applying a 2D transform to all of its events.
    fn transformed(self, mat: &Transform2D) -> Transformed<Self> {
        Transformed::new(mat, self)
    }

    /// Consumes the iterator and returns the length of the path.
    fn length(self) -> f32 {
        flattened_path_length(self)
    }

    /// Returns an iterator of line segments.
    fn line_segments(self) -> LineSegments<Self> {
        LineSegments { iter: self }
    }
}

impl<Iter> FlattenedIterator for Iter
where
    Iter: Iterator<Item = FlattenedEvent>,
{}

/// An extension to the common Iterator interface, that adds information which is useful when
/// chaining path-specific iterators.
pub trait QuadraticPathIterator: Iterator<Item = QuadraticEvent> + Sized {

    /// Returns an iterator of path events.
    fn path_events(self) -> iter::Map<Self, fn(QuadraticEvent) -> PathEvent> {
        self.map(quadratic_to_path_event)
    }

    /// Returns an iterator of svg events.
    fn svg_events(self) -> iter::Map<Self, fn(QuadraticEvent) -> SvgEvent> {
        self.map(quadratic_to_svg_event)
    }

    /// Returns an iterator applying a 2D transform to all of its events.
    fn transformed(self, mat: &Transform2D) -> Transformed<Self> {
        Transformed::new(mat, self)
    }
}

impl<Iter> QuadraticPathIterator for Iter
where
    Iter: Iterator<Item = QuadraticEvent>,
{}

/// Turns an iterator of SVG path commands into an iterator of `PathEvent`.
pub struct PathEvents<SvgIter> {
    it: SvgIter,
    arc_to_cubics: Vec<CubicBezierSegment<f32>>,
}

impl<SvgIter> PathEvents<SvgIter> {
    pub fn new(it: SvgIter) -> Self {
        PathEvents {
            it,
            arc_to_cubics: Vec::new(),
        }
    }
}

impl<SvgIter> Iterator for PathEvents<SvgIter>
where
    SvgIter: SvgIterator,
{
    type Item = PathEvent;
    fn next(&mut self) -> Option<PathEvent> {
        if let Some(segment) = self.arc_to_cubics.pop() {
            return Some(PathEvent::Cubic(segment));
        }
        match self.it.next() {
            Some(svg_evt) => Some(
                svg_to_path_event(
                    svg_evt,
                    &self.it.path_state().clone(),
                    &mut self.arc_to_cubics
                )
            ),
            None => None,
        }
    }
}

fn svg_to_path_event(
    event: SvgEvent,
    ps: &PathState,
    arcs_to_cubic: &mut Vec<CubicBezierSegment<f32>>
) -> PathEvent {
    let from = ps.current_position();
    match event {
        SvgEvent::MoveTo(to) => PathEvent::MoveTo(to),
        SvgEvent::LineTo(to) => PathEvent::Line(LineSegment { from, to }),
        SvgEvent::QuadraticTo(ctrl, to) => PathEvent::Quadratic(QuadraticBezierSegment {
            from, ctrl, to
        }),
        SvgEvent::CubicTo(ctrl1, ctrl2, to) => PathEvent::Cubic(CubicBezierSegment {
            from, ctrl1, ctrl2, to
        }),
        SvgEvent::Close => PathEvent::Close(LineSegment {
            from: ps.current_position(),
            to: ps.start_position(),
        }),
        SvgEvent::RelativeMoveTo(to) => PathEvent::MoveTo(ps.relative_to_absolute(to)),
        SvgEvent::RelativeLineTo(to) => PathEvent::Line(LineSegment {
            from,
            to: ps.relative_to_absolute(to)
        }),
        SvgEvent::RelativeQuadraticTo(ctrl, to) => {
            PathEvent::Quadratic(QuadraticBezierSegment {
                from,
                ctrl: ps.relative_to_absolute(ctrl),
                to: ps.relative_to_absolute(to),
            })
        }
        SvgEvent::RelativeCubicTo(ctrl1, ctrl2, to) => {
            PathEvent::Cubic(CubicBezierSegment {
                from,
                ctrl1: ps.relative_to_absolute(ctrl1),
                ctrl2: ps.relative_to_absolute(ctrl2),
                to: ps.relative_to_absolute(to),
            })
        }
        SvgEvent::HorizontalLineTo(x) => {
            PathEvent::Line(LineSegment {
                from,
                to: point(x, ps.current_position().y)
            })
        }
        SvgEvent::VerticalLineTo(y) => {
            PathEvent::Line(LineSegment {
                from,
                to: point(ps.current_position().x, y)
            })
        }
        SvgEvent::RelativeHorizontalLineTo(x) => {
            PathEvent::Line(LineSegment {
                from,
                to: point(ps.current_position().x + x, ps.current_position().y)
            })
        }
        SvgEvent::RelativeVerticalLineTo(y) => {
            PathEvent::Line(LineSegment {
                from,
                to: point(ps.current_position().x, ps.current_position().y + y)
            })
        }
        SvgEvent::SmoothQuadraticTo(to) => {
            PathEvent::Quadratic(QuadraticBezierSegment {
                from,
                ctrl: ps.get_smooth_quadratic_ctrl(),
                to
            })
        }
        SvgEvent::SmoothCubicTo(ctrl2, to) => {
            PathEvent::Cubic(CubicBezierSegment {
                from,
                ctrl1: ps.get_smooth_cubic_ctrl(),
                ctrl2,
                to
            })
        }
        SvgEvent::SmoothRelativeQuadraticTo(to) => {
            PathEvent::Quadratic(QuadraticBezierSegment {
                from,
                ctrl: ps.get_smooth_quadratic_ctrl(),
                to: ps.relative_to_absolute(to),
            })
        }
        SvgEvent::SmoothRelativeCubicTo(ctrl2, to) => {
            PathEvent::Cubic(CubicBezierSegment {
                from,
                ctrl1: ps.get_smooth_cubic_ctrl(),
                ctrl2: ps.relative_to_absolute(ctrl2),
                to: ps.relative_to_absolute(to),
            })
        }
        SvgEvent::ArcTo(radii, x_rotation, flags, to) => {
            arc_to_path_events(
                &Arc::from_svg_arc(&SvgArc {
                    from: ps.current_position(),
                    to,
                    radii,
                    x_rotation,
                    flags,
                }),
                arcs_to_cubic,
            )
        }
        SvgEvent::RelativeArcTo(radii, x_rotation, flags, to) => {
            arc_to_path_events(
                &Arc::from_svg_arc(&SvgArc {
                    from: ps.current_position(),
                    to: ps.current_position() + to,
                    radii,
                    x_rotation,
                    flags,
                }),
                arcs_to_cubic,
            )
        }
    }
}

fn arc_to_path_events(arc: &Arc<f32>, arcs_to_cubic: &mut Vec<CubicBezierSegment<f32>>) -> PathEvent {
    let mut curves: ArrayVec<[CubicBezierSegment<f32>; 4]> = ArrayVec::new();
    arc.for_each_cubic_bezier(&mut|curve: &CubicBezierSegment<f32>| {
        curves.push(*curve);
    });
    while curves.len() > 1 {
        // Append in reverse order.
        arcs_to_cubic.push(curves.pop().unwrap());
    }
    PathEvent::Cubic(curves[0])
}

/// An iterator that consumes `PathEvent` iterator and yields FlattenedEvents.
pub struct Flattened<Iter> {
    it: Iter,
    current_position: Point,
    current_curve: TmpFlatteningIter,
    tolerance: f32,
}

enum TmpFlatteningIter {
    Quadratic(quadratic_bezier::Flattened<f32>),
    Cubic(cubic_bezier::Flattened<f32>),
    None,
}

impl<Iter: Iterator<Item = PathEvent>> Flattened<Iter> {
    /// Create the iterator.
    pub fn new(tolerance: f32, it: Iter) -> Self {
        Flattened {
            it,
            current_position: point(0.0, 0.0),
            current_curve: TmpFlatteningIter::None,
            tolerance,
        }
    }
}

impl<Iter> Iterator for Flattened<Iter>
where
    Iter: Iterator<Item = PathEvent>,
{
    type Item = FlattenedEvent;
    fn next(&mut self) -> Option<FlattenedEvent> {
        match self.current_curve {
            TmpFlatteningIter::Quadratic(ref mut it) => {
                if let Some(to) = it.next() {
                    let from = self.current_position;
                    self.current_position = to;
                    return Some(FlattenedEvent::Line(LineSegment { from, to }));
                }
            }
            TmpFlatteningIter::Cubic(ref mut it) => {
                if let Some(to) = it.next() {
                    let from = self.current_position;
                    self.current_position = to;
                    return Some(FlattenedEvent::Line(LineSegment { from, to }));
                }
            }
            _ => {}
        }
        self.current_curve = TmpFlatteningIter::None;
        match self.it.next() {
            Some(PathEvent::MoveTo(to)) => Some(FlattenedEvent::MoveTo(to)),
            Some(PathEvent::Line(segment)) => Some(FlattenedEvent::Line(segment)),
            Some(PathEvent::Close(segment)) => Some(FlattenedEvent::Close(segment)),
            Some(PathEvent::Quadratic(segment)) => {
                self.current_position = segment.from;
                self.current_curve = TmpFlatteningIter::Quadratic(
                    segment.flattened(self.tolerance)
                );
                self.next()
            }
            Some(PathEvent::Cubic(segment)) => {
                self.current_position = segment.from;
                self.current_curve = TmpFlatteningIter::Cubic(
                    segment.flattened(self.tolerance)
                );
                self.next()
            }
            None => None,
        }
    }
}

// TODO: SvgPathIter and PathIter should be merged into a single struct using
// specialization to implement the Iterator trait depending on the type of
// event but specialization isn't stable in rust yet.

/// An adapter iterator that implements SvgIterator on top of an Iterator<Item=SvgEvent>.
pub struct SvgPathIter<Iter> {
    it: Iter,
    state: PathState,
}

impl<E, Iter> SvgPathIter<Iter>
where
    E: Into<SvgEvent>,
    Iter: Iterator<Item = E>
{
    pub fn new(it: Iter) -> Self {
        SvgPathIter {
            it,
            state: PathState::new(),
        }
    }
}

impl<E, Iter> SvgIterator for SvgPathIter<Iter>
where
    E: Into<SvgEvent>,
    Iter: Iterator<Item = E>
{
    fn path_state(&self) -> &PathState { &self.state }
}

impl<E, Iter> Iterator for SvgPathIter<Iter>
where
    E: Into<SvgEvent>,
    Iter: Iterator<Item = E>
{
    type Item = SvgEvent;
    fn next(&mut self) -> Option<SvgEvent> {
        if let Some(evt) = self.it.next() {
            let svg_evt = evt.into();
            self.state.svg_event(svg_evt);
            return Some(svg_evt);
        }

        None
    }
}

#[inline]
fn quadratic_to_path_event(evt: QuadraticEvent) -> PathEvent { evt.to_path_event() }
#[inline]
fn quadratic_to_svg_event(evt: QuadraticEvent) -> SvgEvent { evt.to_svg_event() }
#[inline]
fn flattened_to_path_event(evt: FlattenedEvent) -> PathEvent { evt.to_path_event() }
#[inline]
fn flattened_to_svg_event(evt: FlattenedEvent) -> SvgEvent { evt.to_svg_event() }

/// Applies a 2D transform to a path iterator and yields the resulting path iterator.
pub struct Transformed<I> {
    it: I,
    transform: Transform2D,
}

impl<I, Event> Transformed<I>
where
    I: Iterator<Item = Event>,
    Event: Transform
{
    /// Creates a new transformed path iterator from a path iterator.
    #[inline]
    pub fn new(transform: &Transform2D, it: I) -> Transformed<I> {
        Transformed {
            it,
            transform: *transform,
        }
    }
}

impl<I, Event> Iterator for Transformed<I>
where
    I: Iterator<Item = Event>,
    Event: Transform
{
    type Item = Event;
    fn next(&mut self) -> Option<Event> {
        match self.it.next() {
            None => None,
            Some(ref evt) => Some(evt.transform(&self.transform)),
        }
    }
}


/// An iterator that consumes an iterator of `Point`s and produces `FlattenedEvent`s.
///
/// # Example
///
/// ```
/// # extern crate lyon_path;
/// # use lyon_path::iterator::FromPolyline;
/// # use lyon_path::math::point;
/// # fn main() {
/// let points = [
///     point(1.0, 1.0),
///     point(2.0, 1.0),
///     point(1.0, 2.0)
/// ];
/// let iter = FromPolyline::closed(points.iter().cloned());
/// # }
/// ```
pub struct FromPolyline<Iter> {
    iter: Iter,
    current: Point,
    first: Point,
    is_first: bool,
    done: bool,
    close: bool,
}

impl<Iter: Iterator<Item = Point>> FromPolyline<Iter> {
    pub fn new(close: bool, iter: Iter) -> Self {
        FromPolyline {
            iter,
            current: point(0.0, 0.0),
            first: point(0.0, 0.0),
            is_first: true,
            done: false,
            close,
        }
    }

    pub fn closed(iter: Iter) -> Self { FromPolyline::new(true, iter) }

    pub fn open(iter: Iter) -> Self { FromPolyline::new(false, iter) }
}

impl<Iter> Iterator for FromPolyline<Iter>
where
    Iter: Iterator<Item = Point>,
{
    type Item = FlattenedEvent;

    fn next(&mut self) -> Option<FlattenedEvent> {
        if self.done {
            return None;
        }

        if let Some(next) = self.iter.next() {
            debug_assert!(next.x.is_finite());
            debug_assert!(next.y.is_finite());
            let from = self.current;
            self.current = next;
            return if self.is_first {
                self.is_first = false;
                self.first = next;
                Some(FlattenedEvent::MoveTo(next))
            } else {
                Some(FlattenedEvent::Line(LineSegment { from, to: next }))
            }
        }

        self.done = true;
        if self.close {
            return Some(FlattenedEvent::Close(LineSegment {
                from: self.current,
                to: self.first,
            }));
        }

        None
    }
}

/// Turns an iterator of `PathEvent` into an iterator of `BezierSegment<f32>`.
pub struct BezierSegments<Iter> {
    iter: Iter
}

impl<Iter> Iterator for BezierSegments<Iter>
where Iter: Iterator<Item = PathEvent> {
    type Item = BezierSegment<f32>;
    fn next(&mut self) -> Option<BezierSegment<f32>> {
        match self.iter.next() {
            Some(PathEvent::Line(segment))
            | Some(PathEvent::Close(segment))
            => Some(BezierSegment::Linear(segment)),
            Some(PathEvent::Quadratic(segment)) => Some(BezierSegment::Quadratic(segment)),
            Some(PathEvent::Cubic(segment)) => Some(BezierSegment::Cubic(segment)),
            Some(PathEvent::MoveTo(..)) => self.next(),
            None => None,
        }
    }
}

/// Turns an iterator of `FlattenedEvent` into an iterator of `LineSegment<f32>`.
pub struct LineSegments<Iter> {
    iter: Iter
}

impl<Iter> Iterator for LineSegments<Iter>
where Iter: Iterator<Item = FlattenedEvent> {
    type Item = LineSegment<f32>;
    fn next(&mut self) -> Option<LineSegment<f32>> {
        match self.iter.next() {
            Some(FlattenedEvent::Line(segment))
            | Some(FlattenedEvent::Close(segment))
            => Some(segment),
            Some(FlattenedEvent::MoveTo(..)) => self.next(),
            None => None,
        }
    }
}

/// Computes the length of a flattened path.
fn flattened_path_length<T>(iter: T) -> f32
where T: Iterator<Item = FlattenedEvent> {
    let mut length = 0.0;
    for evt in iter {
        match evt {
            FlattenedEvent::MoveTo(..) => {}
            FlattenedEvent::Line(segment) => { length += segment.length(); }
            FlattenedEvent::Close(segment) => { length += segment.length(); }
        }
    }

    length
}

#[test]
fn test_from_polyline_open() {
    let points = &[
        point(1.0, 1.0),
        point(3.0, 1.0),
        point(4.0, 5.0),
        point(5.0, 2.0),
    ];

    let mut evts = FromPolyline::open(points.iter().cloned());

    assert_eq!(evts.next(), Some(FlattenedEvent::MoveTo(point(1.0, 1.0))));
    assert_eq!(evts.next(), Some(FlattenedEvent::Line(LineSegment { from: point(1.0, 1.0), to: point(3.0, 1.0) })));
    assert_eq!(evts.next(), Some(FlattenedEvent::Line(LineSegment { from: point(3.0, 1.0), to: point(4.0, 5.0) })));
    assert_eq!(evts.next(), Some(FlattenedEvent::Line(LineSegment { from: point(4.0, 5.0), to: point(5.0, 2.0) })));
    assert_eq!(evts.next(), None);
}

#[test]
fn test_from_polyline_closed() {
    let points = &[
        point(1.0, 1.0),
        point(3.0, 1.0),
        point(4.0, 5.0),
        point(5.0, 2.0),
    ];

    let mut evts = FromPolyline::closed(points.iter().cloned());

    assert_eq!(evts.next(), Some(FlattenedEvent::MoveTo(point(1.0, 1.0))));
    assert_eq!(evts.next(), Some(FlattenedEvent::Line(LineSegment { from: point(1.0, 1.0), to: point(3.0, 1.0) })));
    assert_eq!(evts.next(), Some(FlattenedEvent::Line(LineSegment { from: point(3.0, 1.0), to: point(4.0, 5.0) })));
    assert_eq!(evts.next(), Some(FlattenedEvent::Line(LineSegment { from: point(4.0, 5.0), to: point(5.0, 2.0) })));
    assert_eq!(evts.next(), Some(FlattenedEvent::Close(LineSegment { from: point(5.0, 2.0), to: point(1.0, 1.0) })));
}