@@ -6,60 +6,54 @@ use fontconfig_cache_parser::{Cache, CharSetLeaf, Object, Pattern, Value};
66use std:: io:: Read ;
77use std:: path:: PathBuf ;
88
9- impl Stretch {
10- fn from_fc ( width : i32 ) -> Self {
11- match width {
12- 63 => Self :: EXTRA_CONDENSED ,
13- 87 => Self :: SEMI_CONDENSED ,
14- 113 => Self :: SEMI_EXPANDED ,
15- _ => Self :: from_ratio ( width as f32 / 100.0 ) ,
16- }
9+ fn stretch_from_fc ( width : i32 ) -> Stretch {
10+ match width {
11+ 63 => Stretch :: EXTRA_CONDENSED ,
12+ 87 => Stretch :: SEMI_CONDENSED ,
13+ 113 => Stretch :: SEMI_EXPANDED ,
14+ _ => Stretch :: from_ratio ( width as f32 / 100.0 ) ,
1715 }
1816}
1917
20- impl Style {
21- fn from_fc ( slant : i32 ) -> Self {
22- match slant {
23- 100 => Self :: Italic ,
24- 110 => Self :: Oblique ( None ) ,
25- _ => Self :: Normal ,
26- }
18+ fn style_from_fc ( slant : i32 ) -> Style {
19+ match slant {
20+ 100 => Style :: Italic ,
21+ 110 => Style :: Oblique ( None ) ,
22+ _ => Style :: Normal ,
2723 }
2824}
2925
30- impl Weight {
31- fn from_fc ( weight : i32 ) -> Self {
32- const MAP : & [ ( i32 , i32 ) ] = & [
33- ( 0 , 0 ) ,
34- ( 100 , 0 ) ,
35- ( 200 , 40 ) ,
36- ( 300 , 50 ) ,
37- ( 350 , 55 ) ,
38- ( 380 , 75 ) ,
39- ( 400 , 80 ) ,
40- ( 500 , 100 ) ,
41- ( 600 , 180 ) ,
42- ( 700 , 200 ) ,
43- ( 800 , 205 ) ,
44- ( 900 , 210 ) ,
45- ( 950 , 215 ) ,
46- ] ;
47- for ( i, ( ot, fc) ) in MAP . iter ( ) . skip ( 1 ) . enumerate ( ) {
48- if weight == * fc {
49- return Self :: new ( * ot as f32 ) ;
50- }
51- if weight < * fc {
52- let weight = weight as f32 ;
53- let fc_a = MAP [ i - 1 ] . 1 as f32 ;
54- let fc_b = * fc as f32 ;
55- let ot_a = MAP [ i - 1 ] . 1 as f32 ;
56- let ot_b = * ot as f32 ;
57- let t = ( fc_a - fc_b) / ( weight - fc_a) ;
58- return Self :: new ( ot_a + ( ot_b - ot_a) * t) ;
59- }
26+ fn weight_from_fc ( weight : i32 ) -> Weight {
27+ const MAP : & [ ( i32 , i32 ) ] = & [
28+ ( 0 , 0 ) ,
29+ ( 100 , 0 ) ,
30+ ( 200 , 40 ) ,
31+ ( 300 , 50 ) ,
32+ ( 350 , 55 ) ,
33+ ( 380 , 75 ) ,
34+ ( 400 , 80 ) ,
35+ ( 500 , 100 ) ,
36+ ( 600 , 180 ) ,
37+ ( 700 , 200 ) ,
38+ ( 800 , 205 ) ,
39+ ( 900 , 210 ) ,
40+ ( 950 , 215 ) ,
41+ ] ;
42+ for ( i, ( ot, fc) ) in MAP . iter ( ) . skip ( 1 ) . enumerate ( ) {
43+ if weight == * fc {
44+ return Weight :: new ( * ot as f32 ) ;
45+ }
46+ if weight < * fc {
47+ let weight = weight as f32 ;
48+ let fc_a = MAP [ i - 1 ] . 1 as f32 ;
49+ let fc_b = * fc as f32 ;
50+ let ot_a = MAP [ i - 1 ] . 1 as f32 ;
51+ let ot_b = * ot as f32 ;
52+ let t = ( fc_a - fc_b) / ( weight - fc_a) ;
53+ return Weight :: new ( ot_a + ( ot_b - ot_a) * t) ;
6054 }
61- Weight :: EXTRA_BLACK
6255 }
56+ Weight :: EXTRA_BLACK
6357}
6458
6559#[ derive( Default ) ]
@@ -156,21 +150,21 @@ fn parse_font(
156150 Object :: Slant => {
157151 for val in elt. values ( ) . ok ( ) ? {
158152 if let Value :: Int ( i) = val. ok ( ) ? {
159- font. style = Style :: from_fc ( i as _ ) ;
153+ font. style = style_from_fc ( i as _ ) ;
160154 }
161155 }
162156 }
163157 Object :: Weight => {
164158 for val in elt. values ( ) . ok ( ) ? {
165159 if let Value :: Int ( i) = val. ok ( ) ? {
166- font. weight = Weight :: from_fc ( i as _ ) ;
160+ font. weight = weight_from_fc ( i as _ ) ;
167161 }
168162 }
169163 }
170164 Object :: Width => {
171165 for val in elt. values ( ) . ok ( ) ? {
172166 if let Value :: Int ( i) = val. ok ( ) ? {
173- font. stretch = Stretch :: from_fc ( i as _ ) ;
167+ font. stretch = stretch_from_fc ( i as _ ) ;
174168 }
175169 }
176170 }
0 commit comments