Skip to content

Commit 9eff2ea

Browse files
fontique: Move creation of attributes from fontconfig
This exposes `from_fontconfig` functions on `FontStretch`, `FontStyle`, and `FontWeight` rather than only defining them locally within the fontconfig code. This is in preparation for a change similar to linebender#211 where these types and others get pulled into a shared vocabulary crate and this sort of constructor helper would be useful to others.
1 parent ffea1e6 commit 9eff2ea

File tree

2 files changed

+80
-80
lines changed

2 files changed

+80
-80
lines changed

fontique/src/attributes.rs

+77
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,28 @@ impl FontStretch {
191191
}
192192
}
193193

194+
impl FontStretch {
195+
/// Creates a new stretch attribute with the given value from Fontconfig.
196+
///
197+
/// The values are determined based on the [fonts.conf documentation].
198+
///
199+
/// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
200+
pub fn from_fontconfig(width: i32) -> Self {
201+
match width {
202+
50 => Self::ULTRA_CONDENSED,
203+
63 => Self::EXTRA_CONDENSED,
204+
75 => Self::CONDENSED,
205+
87 => Self::SEMI_CONDENSED,
206+
100 => Self::NORMAL,
207+
113 => Self::SEMI_EXPANDED,
208+
125 => Self::EXPANDED,
209+
150 => Self::EXTRA_EXPANDED,
210+
200 => Self::ULTRA_EXPANDED,
211+
_ => Self::from_ratio(width as f32 / 100.0),
212+
}
213+
}
214+
}
215+
194216
impl fmt::Display for FontStretch {
195217
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
196218
let value = self.0 * 1000.0;
@@ -304,6 +326,46 @@ impl FontWeight {
304326
}
305327
}
306328

329+
impl FontWeight {
330+
/// Creates a new weight attribute with the given value from Fontconfig.
331+
///
332+
/// The values are determined based on the [fonts.conf documentation].
333+
///
334+
/// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
335+
pub fn from_fontconfig(weight: i32) -> Self {
336+
const MAP: &[(i32, i32)] = &[
337+
(0, 0),
338+
(100, 0),
339+
(200, 40),
340+
(300, 50),
341+
(350, 55),
342+
(380, 75),
343+
(400, 80),
344+
(500, 100),
345+
(600, 180),
346+
(700, 200),
347+
(800, 205),
348+
(900, 210),
349+
(950, 215),
350+
];
351+
for (i, (ot, fc)) in MAP.iter().skip(1).enumerate() {
352+
if weight == *fc {
353+
return Self::new(*ot as f32);
354+
}
355+
if weight < *fc {
356+
let weight = weight as f32;
357+
let fc_a = MAP[i - 1].1 as f32;
358+
let fc_b = *fc as f32;
359+
let ot_a = MAP[i - 1].1 as f32;
360+
let ot_b = *ot as f32;
361+
let t = (fc_a - fc_b) / (weight - fc_a);
362+
return Self::new(ot_a + (ot_b - ot_a) * t);
363+
}
364+
}
365+
Self::EXTRA_BLACK
366+
}
367+
}
368+
307369
impl Default for FontWeight {
308370
fn default() -> Self {
309371
Self::NORMAL
@@ -399,6 +461,21 @@ impl FontStyle {
399461
}
400462
}
401463

464+
impl FontStyle {
465+
/// Creates a new style attribute with the given value from Fontconfig.
466+
///
467+
/// The values are determined based on the [fonts.conf documentation].
468+
///
469+
/// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
470+
pub fn from_fontconfig(slant: i32) -> Self {
471+
match slant {
472+
100 => Self::Italic,
473+
110 => Self::Oblique(None),
474+
_ => Self::Normal,
475+
}
476+
}
477+
}
478+
402479
impl fmt::Display for FontStyle {
403480
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
404481
let value = match self {

fontique/src/backend/fontconfig/cache.rs

+3-80
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,6 @@ use fontconfig_cache_parser::{Cache, CharSetLeaf, Object, Pattern, Value};
66
use std::io::Read;
77
use std::path::PathBuf;
88

9-
impl FontStretch {
10-
/// Creates a new stretch attribute with the given value from Fontconfig.
11-
///
12-
/// The values are determined based on the [fonts.conf documentation].
13-
///
14-
/// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
15-
pub fn from_fc(width: i32) -> Self {
16-
match width {
17-
50 => Self::ULTRA_CONDENSED,
18-
63 => Self::EXTRA_CONDENSED,
19-
75 => Self::CONDENSED,
20-
87 => Self::SEMI_CONDENSED,
21-
100 => Self::NORMAL,
22-
113 => Self::SEMI_EXPANDED,
23-
125 => Self::EXPANDED,
24-
150 => Self::EXTRA_EXPANDED,
25-
200 => Self::ULTRA_EXPANDED,
26-
_ => Self::from_ratio(width as f32 / 100.0),
27-
}
28-
}
29-
}
30-
31-
impl FontStyle {
32-
/// Creates a new style attribute with the given value from Fontconfig.
33-
///
34-
/// The values are determined based on the [fonts.conf documentation].
35-
///
36-
/// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
37-
fn from_fc(slant: i32) -> Self {
38-
match slant {
39-
100 => Self::Italic,
40-
110 => Self::Oblique(None),
41-
_ => Self::Normal,
42-
}
43-
}
44-
}
45-
46-
impl FontWeight {
47-
/// Creates a new weight attribute with the given value from Fontconfig.
48-
///
49-
/// The values are determined based on the [fonts.conf documentation].
50-
///
51-
/// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
52-
fn from_fc(weight: i32) -> Self {
53-
const MAP: &[(i32, i32)] = &[
54-
(0, 0),
55-
(100, 0),
56-
(200, 40),
57-
(300, 50),
58-
(350, 55),
59-
(380, 75),
60-
(400, 80),
61-
(500, 100),
62-
(600, 180),
63-
(700, 200),
64-
(800, 205),
65-
(900, 210),
66-
(950, 215),
67-
];
68-
for (i, (ot, fc)) in MAP.iter().skip(1).enumerate() {
69-
if weight == *fc {
70-
return Self::new(*ot as f32);
71-
}
72-
if weight < *fc {
73-
let weight = weight as f32;
74-
let fc_a = MAP[i - 1].1 as f32;
75-
let fc_b = *fc as f32;
76-
let ot_a = MAP[i - 1].1 as f32;
77-
let ot_b = *ot as f32;
78-
let t = (fc_a - fc_b) / (weight - fc_a);
79-
return Self::new(ot_a + (ot_b - ot_a) * t);
80-
}
81-
}
82-
Self::EXTRA_BLACK
83-
}
84-
}
85-
869
#[derive(Default)]
8710
pub struct CachedFont {
8811
pub family: Vec<String>,
@@ -177,21 +100,21 @@ fn parse_font(
177100
Object::Slant => {
178101
for val in elt.values().ok()? {
179102
if let Value::Int(i) = val.ok()? {
180-
font.style = FontStyle::from_fc(i as _);
103+
font.style = FontStyle::from_fontconfig(i as _);
181104
}
182105
}
183106
}
184107
Object::Weight => {
185108
for val in elt.values().ok()? {
186109
if let Value::Int(i) = val.ok()? {
187-
font.weight = FontWeight::from_fc(i as _);
110+
font.weight = FontWeight::from_fontconfig(i as _);
188111
}
189112
}
190113
}
191114
Object::Width => {
192115
for val in elt.values().ok()? {
193116
if let Value::Int(i) = val.ok()? {
194-
font.stretch = FontStretch::from_fc(i as _);
117+
font.stretch = FontStretch::from_fontconfig(i as _);
195118
}
196119
}
197120
}

0 commit comments

Comments
 (0)