@@ -16,6 +16,7 @@ pub enum End {
1616 Tail ,
1717}
1818
19+ /// Generic prev/next given an index for [`Vec<T>`].
1920pub trait GenericPrevNext {
2021 type Error ;
2122 /// Return the previous and next index, given an index.
@@ -50,42 +51,37 @@ pub trait GenericPrevNext {
5051 }
5152}
5253
54+ /// Return the previous and next index, given an index. As opposed to ``GenericPrevNext``, this
55+ /// always considers a contour's open/closed state (`assert!(self[0].ptype == PointType::Move)`).
5356pub trait PrevNext {
5457 type Error ;
55- /// Return the previous and next index, given an index. As opposed to ``GenericPrevNext``, this always
56- /// considers a contour's open/closed state (`assert!(self[0].ptype == PointType::Move)`).
5758 fn contour_prev_next ( & self , idx : usize ) -> Result < ( Option < usize > , Option < usize > ) , GlifParserError > ;
58- fn contour_prev_next_handles ( & self , idx : usize ) -> Result < ( ( Handle , Handle ) , ( Handle , Handle ) ) , GlifParserError > ;
59+ fn contour_prev_next_handles (
60+ & self ,
61+ idx : usize ,
62+ ) -> Result < ( ( Handle , Handle ) , ( Handle , Handle ) ) , GlifParserError > ;
5963}
6064
6165impl < T > GenericPrevNext for Vec < T > {
6266 type Error = GlifParserError ;
6367
6468 fn idx_sane ( & self , idx : usize ) -> Result < ( ) , GlifParserError > {
6569 if idx >= self . len ( ) {
66- return Err ( GlifParserError :: PointIdxOutOfBounds { idx, len : self . len ( ) } )
70+ return Err ( GlifParserError :: PointIdxOutOfBounds { idx, len : self . len ( ) } ) ;
6771 } else if self . len ( ) == 1 {
68- return Err ( GlifParserError :: ContourLenOneUnexpected )
72+ return Err ( GlifParserError :: ContourLenOneUnexpected ) ;
6973 } else if self . len ( ) == 0 {
70- return Err ( GlifParserError :: ContourLenZeroUnexpected )
74+ return Err ( GlifParserError :: ContourLenZeroUnexpected ) ;
7175 }
7276 Ok ( ( ) )
7377 }
7478
7579 fn prev_next ( & self , idx : usize ) -> Result < ( usize , usize ) , GlifParserError > {
7680 self . idx_sane ( idx) ?;
7781
78- let prev = if idx == 0 {
79- self . len ( ) - 1
80- } else {
81- idx - 1
82- } ;
82+ let prev = if idx == 0 { self . len ( ) - 1 } else { idx - 1 } ;
8383
84- let next = if idx == self . len ( ) - 1 {
85- 0
86- } else {
87- idx + 1
88- } ;
84+ let next = if idx == self . len ( ) - 1 { 0 } else { idx + 1 } ;
8985
9086 Ok ( ( prev, next) )
9187 }
@@ -102,6 +98,7 @@ impl<T> GenericPrevNext for Vec<T> {
10298 }
10399}
104100
101+ /// Is contour open or closed?
105102pub trait State {
106103 fn is_open ( & self ) -> bool ;
107104 fn is_closed ( & self ) -> bool {
@@ -113,18 +110,20 @@ impl<PD: PointData> State for Contour<PD> {
113110 fn is_open ( & self ) -> bool {
114111 match self . len ( ) {
115112 0 | 1 => true ,
116- _ => self [ 0 ] . ptype == PointType :: Move
113+ _ => self [ 0 ] . ptype == PointType :: Move ,
117114 }
118115 }
119116}
120117
118+ /// Error will always be GlifParserError::PointIdxOutOfBounds
121119impl < PD : PointData > PrevNext for Contour < PD > {
122120 type Error = GlifParserError ;
123- /// Error will always be GlifParserError::PointIdxOutOfBounds
124121 fn contour_prev_next ( & self , idx : usize ) -> Result < ( Option < usize > , Option < usize > ) , GlifParserError > {
125122 let ( prev, next) = self . prev_next ( idx) ?;
126123 if self . is_open ( ) && self . idx_at_start_or_end ( idx) ? {
127- let end = self . idx_which_end ( idx) ?. expect ( "self.idx_at_start_or_end true but self.idx_which_end returned None???" ) ;
124+ let end = self
125+ . idx_which_end ( idx) ?
126+ . expect ( "self.idx_at_start_or_end true but self.idx_which_end returned None???" ) ;
128127 match end {
129128 End :: Head => Ok ( ( None , Some ( next) ) ) ,
130129 End :: Tail => Ok ( ( Some ( prev) , None ) ) ,
@@ -133,14 +132,22 @@ impl<PD: PointData> PrevNext for Contour<PD> {
133132 Ok ( ( Some ( self . prev ( idx) ?) , Some ( self . next ( idx) ?) ) )
134133 }
135134 }
136- fn contour_prev_next_handles ( & self , idx : usize ) -> Result < ( ( Handle , Handle ) , ( Handle , Handle ) ) , GlifParserError > {
135+ fn contour_prev_next_handles (
136+ & self ,
137+ idx : usize ,
138+ ) -> Result < ( ( Handle , Handle ) , ( Handle , Handle ) ) , GlifParserError > {
137139 let ( prev, next) = self . contour_prev_next ( idx) ?;
138- let prev = prev. map ( |idx| ( self [ idx] . a , self [ idx] . b ) ) . unwrap_or ( ( Handle :: Colocated , Handle :: Colocated ) ) ;
139- let next = next. map ( |idx| ( self [ idx] . a , self [ idx] . b ) ) . unwrap_or ( ( Handle :: Colocated , Handle :: Colocated ) ) ;
140+ let prev = prev
141+ . map ( |idx| ( self [ idx] . a , self [ idx] . b ) )
142+ . unwrap_or ( ( Handle :: Colocated , Handle :: Colocated ) ) ;
143+ let next = next
144+ . map ( |idx| ( self [ idx] . a , self [ idx] . b ) )
145+ . unwrap_or ( ( Handle :: Colocated , Handle :: Colocated ) ) ;
140146 Ok ( ( prev, next) )
141147 }
142148}
143149
150+ /// This validates the UFO `.glif` point `smooth` attribute.
144151pub trait CheckSmooth {
145152 fn is_point_smooth_within ( & self , idx : usize , within : f32 ) -> Result < bool , GlifParserError > ;
146153 fn is_point_smooth ( & self , idx : usize ) -> Result < bool , GlifParserError > {
@@ -161,21 +168,24 @@ impl<PD: PointData> CheckSmooth for Contour<PD> {
161168 if let Some ( prev) = prev {
162169 ( p. a , self [ prev] . b )
163170 } else {
164- return Ok ( false )
171+ return Ok ( false ) ;
165172 }
166173 }
167174 Some ( End :: Tail ) => {
168175 if let Some ( next) = next {
169176 ( self [ next] . a , p. b )
170177 } else {
171- return Ok ( false )
178+ return Ok ( false ) ;
172179 }
173180 }
174181 } ;
175182 let kp0 = kurbo:: Point :: new ( p. x as f64 , p. y as f64 ) ;
176183 let ( kp1, kp2) = match ( a, b) {
177- ( Handle :: At ( x1, y1) , Handle :: At ( x2, y2) ) => ( kurbo:: Point :: new ( x1 as f64 , y1 as f64 ) , kurbo:: Point :: new ( x2 as f64 , y2 as f64 ) ) ,
178- _ => return Ok ( false )
184+ ( Handle :: At ( x1, y1) , Handle :: At ( x2, y2) ) => (
185+ kurbo:: Point :: new ( x1 as f64 , y1 as f64 ) ,
186+ kurbo:: Point :: new ( x2 as f64 , y2 as f64 ) ,
187+ ) ,
188+ _ => return Ok ( false ) ,
179189 } ;
180190 let line = kurbo:: Line :: new ( kp1, kp2) ;
181191 let nearest = line. nearest ( kp0, 0.000001 ) ;
0 commit comments