2017-09-12 01:28:14 -04:00
|
|
|
// pathfinder/path-utils/src/monotonic.rs
|
|
|
|
//
|
|
|
|
// Copyright © 2017 The Pathfinder Project Developers.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use arrayvec::ArrayVec;
|
|
|
|
use euclid::Point2D;
|
|
|
|
use std::mem;
|
|
|
|
|
2017-10-04 14:06:41 -04:00
|
|
|
use PathCommand;
|
2017-09-12 01:28:14 -04:00
|
|
|
use curve::Curve;
|
|
|
|
|
2017-10-04 14:06:41 -04:00
|
|
|
pub struct MonotonicPathCommandStream<I> {
|
2017-09-12 01:28:14 -04:00
|
|
|
inner: I,
|
2017-10-04 14:06:41 -04:00
|
|
|
queue: ArrayVec<[PathCommand; 2]>,
|
2017-09-12 01:28:14 -04:00
|
|
|
prev_point: Point2D<f32>,
|
|
|
|
}
|
|
|
|
|
2017-10-04 14:06:41 -04:00
|
|
|
impl<I> MonotonicPathCommandStream<I> where I: Iterator<Item = PathCommand> {
|
|
|
|
pub fn new(inner: I) -> MonotonicPathCommandStream<I> {
|
|
|
|
MonotonicPathCommandStream {
|
2017-09-12 01:28:14 -04:00
|
|
|
inner: inner,
|
|
|
|
queue: ArrayVec::new(),
|
|
|
|
prev_point: Point2D::zero(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 14:06:41 -04:00
|
|
|
impl<I> Iterator for MonotonicPathCommandStream<I> where I: Iterator<Item = PathCommand> {
|
|
|
|
type Item = PathCommand;
|
2017-09-12 01:28:14 -04:00
|
|
|
|
2017-10-04 14:06:41 -04:00
|
|
|
fn next(&mut self) -> Option<PathCommand> {
|
2017-09-12 01:28:14 -04:00
|
|
|
if !self.queue.is_empty() {
|
|
|
|
return Some(self.queue.remove(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.inner.next() {
|
|
|
|
None => None,
|
2017-10-04 14:06:41 -04:00
|
|
|
Some(PathCommand::ClosePath) => Some(PathCommand::ClosePath),
|
|
|
|
Some(PathCommand::MoveTo(point)) => {
|
2017-09-12 01:28:14 -04:00
|
|
|
self.prev_point = point;
|
2017-10-04 14:06:41 -04:00
|
|
|
Some(PathCommand::MoveTo(point))
|
2017-09-12 01:28:14 -04:00
|
|
|
}
|
2017-10-04 14:06:41 -04:00
|
|
|
Some(PathCommand::LineTo(point)) => {
|
2017-09-12 01:28:14 -04:00
|
|
|
self.prev_point = point;
|
2017-10-04 14:06:41 -04:00
|
|
|
Some(PathCommand::LineTo(point))
|
2017-09-12 01:28:14 -04:00
|
|
|
}
|
2017-10-04 14:06:41 -04:00
|
|
|
Some(PathCommand::CurveTo(control_point, endpoint)) => {
|
2017-09-12 01:28:14 -04:00
|
|
|
let curve = Curve::new(&self.prev_point, &control_point, &endpoint);
|
2017-09-22 16:40:41 -04:00
|
|
|
self.prev_point = endpoint;
|
2017-09-12 01:28:14 -04:00
|
|
|
match curve.inflection_points() {
|
2017-10-04 14:06:41 -04:00
|
|
|
(None, None) => Some(PathCommand::CurveTo(control_point, endpoint)),
|
2017-09-12 01:28:14 -04:00
|
|
|
(Some(t), None) | (None, Some(t)) => {
|
|
|
|
let (prev_curve, next_curve) = curve.subdivide(t);
|
|
|
|
self.queue.push(next_curve.to_path_segment());
|
|
|
|
Some(prev_curve.to_path_segment())
|
|
|
|
}
|
|
|
|
(Some(mut t0), Some(mut t1)) => {
|
|
|
|
if t1 < t0 {
|
|
|
|
mem::swap(&mut t0, &mut t1)
|
|
|
|
}
|
|
|
|
|
|
|
|
let (curve_0, curve_12) = curve.subdivide(t0);
|
2017-09-22 16:40:41 -04:00
|
|
|
let (curve_1, curve_2) = curve_12.subdivide((t1 - t0) / (1.0 - t0));
|
2017-09-12 01:28:14 -04:00
|
|
|
self.queue.push(curve_1.to_path_segment());
|
|
|
|
self.queue.push(curve_2.to_path_segment());
|
|
|
|
|
|
|
|
Some(curve_0.to_path_segment())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|