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
use super::{Bezier, Piecewise, Vector};
use glifparser::{Contour, Outline, Handle, PointType};
#[cfg(feature="default")]
use glifparser::glif::{MFEKContour, MFEKOutline};
impl<T: glifparser::PointData> From<&Outline<T>> for Piecewise<Piecewise<Bezier>>
{
fn from(outline: &Outline<T>) -> Self {
let mut new_segs = Vec::new();
for contour in outline
{
new_segs.push(Piecewise::from(contour));
}
return Piecewise::new(new_segs, None);
}
}
#[cfg(feature="default")]
impl<T: glifparser::PointData> From<&MFEKOutline<T>> for Piecewise<Piecewise<Bezier>>
{
fn from(outline: &MFEKOutline<T>) -> Self {
let mut new_segs = Vec::new();
for contour in outline
{
new_segs.push(Piecewise::from(contour));
}
return Piecewise::new(new_segs, None);
}
}
#[cfg(feature="default")]
impl<T: glifparser::PointData> From<MFEKOutline<T>> for Piecewise<Piecewise<Bezier>>
{
fn from(outline: MFEKOutline<T>) -> Self {
return outline.into();
}
}
impl Piecewise<Piecewise<Bezier>> {
pub fn to_outline<T: glifparser::PointData>(&self) -> Outline<T> {
let mut output_outline: Outline<T> = Outline::new();
for contour in &self.segs
{
output_outline.push(contour.to_contour());
}
return output_outline;
}
}
impl<T: glifparser::PointData> From<&Contour<T>> for Piecewise<Bezier>
{
fn from(contour: &Contour<T>) -> Self {
let mut new_segs = Vec::new();
let mut lastpoint: Option<&glifparser::Point<T>> = None;
for point in contour
{
match lastpoint
{
None => {},
Some(lastpoint) => {
new_segs.push(Bezier::from(&lastpoint, point));
}
}
lastpoint = Some(point);
}
let firstpoint = contour.first().unwrap();
if firstpoint.ptype != PointType::Move {
new_segs.push(Bezier::from(&lastpoint.unwrap(), firstpoint));
}
return Piecewise::new(new_segs, None);
}
}
#[cfg(feature="default")]
impl<T: glifparser::PointData> From<&MFEKContour<T>> for Piecewise<Bezier>
{
fn from(contour: &MFEKContour<T>) -> Self {
return Piecewise::from(&contour.inner);
}
}
#[cfg(feature="default")]
impl<T: glifparser::PointData> From<MFEKContour<T>> for Piecewise<Bezier>
{
fn from(contour: MFEKContour<T>) -> Self {
return Piecewise::from(&contour.inner);
}
}
impl Piecewise<Bezier> {
pub fn to_contour<T: glifparser::PointData>(&self) -> Contour<T> {
let mut output_contour: Contour<T> = Vec::new();
let mut last_curve: Option<[Vector; 4]> = None;
let mut first_point = true;
for curve in &self.segs {
let control_points = curve.to_control_points();
let point_type = if first_point && !self.is_closed() { PointType::Move } else { PointType::Curve };
let mut new_point = control_points[0].to_point(control_points[1].to_handle(), Handle::Colocated, point_type);
match last_curve {
Some(lc) => {
new_point.b = lc[2].to_handle();
}
None => {}
}
output_contour.push(new_point);
last_curve = Some(control_points);
first_point = false;
}
if output_contour.len() <= 1 { return output_contour }
if self.is_closed() {
let fp = output_contour.first_mut().unwrap();
fp.b = Vector::to_handle(last_curve.unwrap()[2]);
} else {
let control_points = last_curve.unwrap();
let new_point = control_points[3].to_point(control_points[2].to_handle(), Handle::Colocated, PointType::Curve);
output_contour.push(new_point);
}
output_contour
}
}