@@ -156,6 +156,28 @@ impl Stretch {
156
156
}
157
157
}
158
158
159
+ impl Stretch {
160
+ /// Creates a new stretch attribute with the given value from Fontconfig.
161
+ ///
162
+ /// The values are determined based on the [fonts.conf documentation].
163
+ ///
164
+ /// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
165
+ pub fn from_fontconfig ( width : i32 ) -> Stretch {
166
+ match width {
167
+ 50 => Self :: ULTRA_CONDENSED ,
168
+ 63 => Self :: EXTRA_CONDENSED ,
169
+ 75 => Self :: CONDENSED ,
170
+ 87 => Self :: SEMI_CONDENSED ,
171
+ 100 => Self :: NORMAL ,
172
+ 113 => Self :: SEMI_EXPANDED ,
173
+ 125 => Self :: EXPANDED ,
174
+ 150 => Self :: EXTRA_EXPANDED ,
175
+ 200 => Self :: ULTRA_EXPANDED ,
176
+ _ => Self :: from_ratio ( width as f32 / 100.0 ) ,
177
+ }
178
+ }
179
+ }
180
+
159
181
impl fmt:: Display for Stretch {
160
182
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
161
183
let value = self . 0 * 1000.0 ;
@@ -268,6 +290,46 @@ impl Weight {
268
290
}
269
291
}
270
292
293
+ impl Weight {
294
+ /// Creates a new weight attribute with the given value from Fontconfig.
295
+ ///
296
+ /// The values are determined based on the [fonts.conf documentation].
297
+ ///
298
+ /// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
299
+ pub fn from_fontconfig ( weight : i32 ) -> Self {
300
+ const MAP : & [ ( i32 , i32 ) ] = & [
301
+ ( 0 , 0 ) ,
302
+ ( 100 , 0 ) ,
303
+ ( 200 , 40 ) ,
304
+ ( 300 , 50 ) ,
305
+ ( 350 , 55 ) ,
306
+ ( 380 , 75 ) ,
307
+ ( 400 , 80 ) ,
308
+ ( 500 , 100 ) ,
309
+ ( 600 , 180 ) ,
310
+ ( 700 , 200 ) ,
311
+ ( 800 , 205 ) ,
312
+ ( 900 , 210 ) ,
313
+ ( 950 , 215 ) ,
314
+ ] ;
315
+ for ( i, ( ot, fc) ) in MAP . iter ( ) . skip ( 1 ) . enumerate ( ) {
316
+ if weight == * fc {
317
+ return Self :: new ( * ot as f32 ) ;
318
+ }
319
+ if weight < * fc {
320
+ let weight = weight as f32 ;
321
+ let fc_a = MAP [ i - 1 ] . 1 as f32 ;
322
+ let fc_b = * fc as f32 ;
323
+ let ot_a = MAP [ i - 1 ] . 1 as f32 ;
324
+ let ot_b = * ot as f32 ;
325
+ let t = ( fc_a - fc_b) / ( weight - fc_a) ;
326
+ return Self :: new ( ot_a + ( ot_b - ot_a) * t) ;
327
+ }
328
+ }
329
+ Self :: EXTRA_BLACK
330
+ }
331
+ }
332
+
271
333
impl Default for Weight {
272
334
fn default ( ) -> Self {
273
335
Self :: NORMAL
@@ -362,6 +424,21 @@ impl Style {
362
424
}
363
425
}
364
426
427
+ impl Style {
428
+ /// Creates a new style attribute with the given value from Fontconfig.
429
+ ///
430
+ /// The values are determined based on the [fonts.conf documentation].
431
+ ///
432
+ /// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
433
+ pub fn from_fontconfig ( slant : i32 ) -> Self {
434
+ match slant {
435
+ 100 => Self :: Italic ,
436
+ 110 => Self :: Oblique ( None ) ,
437
+ _ => Self :: Normal ,
438
+ }
439
+ }
440
+ }
441
+
365
442
impl fmt:: Display for Style {
366
443
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
367
444
let value = match self {
0 commit comments