2019-01-10 13:01:21 -05:00
|
|
|
// pathfinder/geometry/src/orientation.rs
|
2018-03-07 14:21:08 -05:00
|
|
|
//
|
2019-01-10 13:01:21 -05:00
|
|
|
// Copyright © 2019 The Pathfinder Project Developers.
|
2018-03-07 14:21:08 -05:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-02-06 16:12:53 -05:00
|
|
|
use crate::outline::Outline;
|
2018-03-07 14:21:08 -05:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
pub enum Orientation {
|
|
|
|
Ccw = -1,
|
|
|
|
Cw = 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Orientation {
|
|
|
|
/// This follows the FreeType algorithm.
|
2019-02-06 16:12:53 -05:00
|
|
|
pub fn from_outline(outline: &Outline) -> Orientation {
|
|
|
|
let mut area = 0.0;
|
|
|
|
for contour in &outline.contours {
|
|
|
|
let mut prev_position = match contour.last_position() {
|
|
|
|
None => continue,
|
|
|
|
Some(position) => position,
|
|
|
|
};
|
|
|
|
for &next_position in &contour.points {
|
|
|
|
area += prev_position.det(next_position);
|
|
|
|
prev_position = next_position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Orientation::from_area(area)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_area(area: f32) -> Orientation {
|
2018-03-07 14:21:08 -05:00
|
|
|
if area <= 0.0 {
|
|
|
|
Orientation::Ccw
|
|
|
|
} else {
|
|
|
|
Orientation::Cw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|