|
1 | 1 | // Copyright 2024 the Parley Authors |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT |
3 | 3 |
|
4 | | -use super::{Stretch, Style, Weight}; |
| 4 | +use styled_text::{Stretch, Style, Weight}; |
5 | 5 | use fontconfig_cache_parser::{Cache, CharSetLeaf, Object, Pattern, Value}; |
6 | 6 | use std::io::Read; |
7 | 7 | use std::path::PathBuf; |
8 | 8 |
|
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 | +// FIXME(style): There's an argument for putting this into styled_text |
| 10 | +fn stretch_from_fc(width: i32) -> Stretch { |
| 11 | + match width { |
| 12 | + 63 => Stretch::EXTRA_CONDENSED, |
| 13 | + 87 => Stretch::SEMI_CONDENSED, |
| 14 | + 113 => Stretch::SEMI_EXPANDED, |
| 15 | + _ => Stretch::from_ratio(width as f32 / 100.0), |
17 | 16 | } |
18 | 17 | } |
19 | 18 |
|
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 | | - } |
| 19 | +// FIXME(style): There's an argument for putting this into styled_text |
| 20 | +fn style_from_fc(slant: i32) -> Style { |
| 21 | + match slant { |
| 22 | + 100 => Style::Italic, |
| 23 | + 110 => Style::Oblique(None), |
| 24 | + _ => Style::Normal, |
27 | 25 | } |
28 | 26 | } |
29 | 27 |
|
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 | | - } |
| 28 | +// FIXME(style): There's an argument for putting this into styled_text |
| 29 | +fn weight_from_fc(weight: i32) -> Weight { |
| 30 | + const MAP: &[(i32, i32)] = &[ |
| 31 | + (0, 0), |
| 32 | + (100, 0), |
| 33 | + (200, 40), |
| 34 | + (300, 50), |
| 35 | + (350, 55), |
| 36 | + (380, 75), |
| 37 | + (400, 80), |
| 38 | + (500, 100), |
| 39 | + (600, 180), |
| 40 | + (700, 200), |
| 41 | + (800, 205), |
| 42 | + (900, 210), |
| 43 | + (950, 215), |
| 44 | + ]; |
| 45 | + for (i, (ot, fc)) in MAP.iter().skip(1).enumerate() { |
| 46 | + if weight == *fc { |
| 47 | + return Weight::new(*ot as f32); |
| 48 | + } |
| 49 | + if weight < *fc { |
| 50 | + let weight = weight as f32; |
| 51 | + let fc_a = MAP[i - 1].1 as f32; |
| 52 | + let fc_b = *fc as f32; |
| 53 | + let ot_a = MAP[i - 1].1 as f32; |
| 54 | + let ot_b = *ot as f32; |
| 55 | + let t = (fc_a - fc_b) / (weight - fc_a); |
| 56 | + return Weight::new(ot_a + (ot_b - ot_a) * t); |
60 | 57 | } |
61 | | - Weight::EXTRA_BLACK |
62 | 58 | } |
| 59 | + Weight::EXTRA_BLACK |
63 | 60 | } |
64 | 61 |
|
65 | 62 | #[derive(Default)] |
@@ -156,21 +153,21 @@ fn parse_font( |
156 | 153 | Object::Slant => { |
157 | 154 | for val in elt.values().ok()? { |
158 | 155 | if let Value::Int(i) = val.ok()? { |
159 | | - font.style = Style::from_fc(i as _); |
| 156 | + font.style = style_from_fc(i as _); |
160 | 157 | } |
161 | 158 | } |
162 | 159 | } |
163 | 160 | Object::Weight => { |
164 | 161 | for val in elt.values().ok()? { |
165 | 162 | if let Value::Int(i) = val.ok()? { |
166 | | - font.weight = Weight::from_fc(i as _); |
| 163 | + font.weight = weight_from_fc(i as _); |
167 | 164 | } |
168 | 165 | } |
169 | 166 | } |
170 | 167 | Object::Width => { |
171 | 168 | for val in elt.values().ok()? { |
172 | 169 | if let Value::Int(i) = val.ok()? { |
173 | | - font.stretch = Stretch::from_fc(i as _); |
| 170 | + font.stretch = stretch_from_fc(i as _); |
174 | 171 | } |
175 | 172 | } |
176 | 173 | } |
|
0 commit comments