Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a13aa93

Browse files
committedDec 8, 2024·
Fix fontique's fontconfig conversion code.
1 parent 40819b7 commit a13aa93

File tree

1 file changed

+47
-50
lines changed

1 file changed

+47
-50
lines changed
 

‎fontique/src/backend/fontconfig/cache.rs

+47-50
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,62 @@
11
// Copyright 2024 the Parley Authors
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

4-
use super::{Stretch, Style, Weight};
54
use fontconfig_cache_parser::{Cache, CharSetLeaf, Object, Pattern, Value};
65
use std::io::Read;
76
use std::path::PathBuf;
7+
use styled_text::{Stretch, Style, Weight};
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+
// 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),
1716
}
1817
}
1918

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,
2725
}
2826
}
2927

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);
6057
}
61-
Weight::EXTRA_BLACK
6258
}
59+
Weight::EXTRA_BLACK
6360
}
6461

6562
#[derive(Default)]
@@ -156,21 +153,21 @@ fn parse_font(
156153
Object::Slant => {
157154
for val in elt.values().ok()? {
158155
if let Value::Int(i) = val.ok()? {
159-
font.style = Style::from_fc(i as _);
156+
font.style = style_from_fc(i as _);
160157
}
161158
}
162159
}
163160
Object::Weight => {
164161
for val in elt.values().ok()? {
165162
if let Value::Int(i) = val.ok()? {
166-
font.weight = Weight::from_fc(i as _);
163+
font.weight = weight_from_fc(i as _);
167164
}
168165
}
169166
}
170167
Object::Width => {
171168
for val in elt.values().ok()? {
172169
if let Value::Int(i) = val.ok()? {
173-
font.stretch = Stretch::from_fc(i as _);
170+
font.stretch = stretch_from_fc(i as _);
174171
}
175172
}
176173
}

0 commit comments

Comments
 (0)