From 2a1bb8663ab2ea138ebd63949d6bcb78744c580d Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Mon, 9 Dec 2024 12:19:10 +0700 Subject: [PATCH 1/3] 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 #209 where these types and others get pulled into a shared vocabulary crate and this sort of constructor helper would be useful to others. --- CHANGELOG.md | 4 ++ fontique/src/attributes.rs | 77 ++++++++++++++++++++++ fontique/src/backend/fontconfig/cache.rs | 83 +----------------------- 3 files changed, 84 insertions(+), 80 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47e2914d..21bfe4d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ This release has an [MSRV] of 1.75. ### Added +#### Fontique + +- `FontStretch`, `FontStyle`, and `FontWeight` get helper functions `from_fontconfig` ([#212] by [@waywardmonkeys][]) + #### Parley - `Generation` on `PlainEditor` to help implement lazy drawing. ([#143] by [@xorgy]) diff --git a/fontique/src/attributes.rs b/fontique/src/attributes.rs index 0bad9e65..8f807bbf 100644 --- a/fontique/src/attributes.rs +++ b/fontique/src/attributes.rs @@ -191,6 +191,28 @@ impl FontStretch { } } +impl FontStretch { + /// Creates a new stretch attribute with the given value from Fontconfig. + /// + /// The values are determined based on the [fonts.conf documentation]. + /// + /// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html + pub fn from_fontconfig(width: i32) -> Self { + match width { + 50 => Self::ULTRA_CONDENSED, + 63 => Self::EXTRA_CONDENSED, + 75 => Self::CONDENSED, + 87 => Self::SEMI_CONDENSED, + 100 => Self::NORMAL, + 113 => Self::SEMI_EXPANDED, + 125 => Self::EXPANDED, + 150 => Self::EXTRA_EXPANDED, + 200 => Self::ULTRA_EXPANDED, + _ => Self::from_ratio(width as f32 / 100.0), + } + } +} + impl fmt::Display for FontStretch { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let value = self.0 * 1000.0; @@ -304,6 +326,46 @@ impl FontWeight { } } +impl FontWeight { + /// Creates a new weight attribute with the given value from Fontconfig. + /// + /// The values are determined based on the [fonts.conf documentation]. + /// + /// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html + pub fn from_fontconfig(weight: i32) -> Self { + const MAP: &[(i32, i32)] = &[ + (0, 0), + (100, 0), + (200, 40), + (300, 50), + (350, 55), + (380, 75), + (400, 80), + (500, 100), + (600, 180), + (700, 200), + (800, 205), + (900, 210), + (950, 215), + ]; + for (i, (ot, fc)) in MAP.iter().skip(1).enumerate() { + if weight == *fc { + return Self::new(*ot as f32); + } + if weight < *fc { + let weight = weight as f32; + let fc_a = MAP[i - 1].1 as f32; + let fc_b = *fc as f32; + let ot_a = MAP[i - 1].1 as f32; + let ot_b = *ot as f32; + let t = (fc_a - fc_b) / (weight - fc_a); + return Self::new(ot_a + (ot_b - ot_a) * t); + } + } + Self::EXTRA_BLACK + } +} + impl Default for FontWeight { fn default() -> Self { Self::NORMAL @@ -399,6 +461,21 @@ impl FontStyle { } } +impl FontStyle { + /// Creates a new style attribute with the given value from Fontconfig. + /// + /// The values are determined based on the [fonts.conf documentation]. + /// + /// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html + pub fn from_fontconfig(slant: i32) -> Self { + match slant { + 100 => Self::Italic, + 110 => Self::Oblique(None), + _ => Self::Normal, + } + } +} + impl fmt::Display for FontStyle { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let value = match self { diff --git a/fontique/src/backend/fontconfig/cache.rs b/fontique/src/backend/fontconfig/cache.rs index 9eba2aea..79e13d33 100644 --- a/fontique/src/backend/fontconfig/cache.rs +++ b/fontique/src/backend/fontconfig/cache.rs @@ -6,83 +6,6 @@ use fontconfig_cache_parser::{Cache, CharSetLeaf, Object, Pattern, Value}; use std::io::Read; use std::path::PathBuf; -impl FontStretch { - /// Creates a new stretch attribute with the given value from Fontconfig. - /// - /// The values are determined based on the [fonts.conf documentation]. - /// - /// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html - pub fn from_fc(width: i32) -> Self { - match width { - 50 => Self::ULTRA_CONDENSED, - 63 => Self::EXTRA_CONDENSED, - 75 => Self::CONDENSED, - 87 => Self::SEMI_CONDENSED, - 100 => Self::NORMAL, - 113 => Self::SEMI_EXPANDED, - 125 => Self::EXPANDED, - 150 => Self::EXTRA_EXPANDED, - 200 => Self::ULTRA_EXPANDED, - _ => Self::from_ratio(width as f32 / 100.0), - } - } -} - -impl FontStyle { - /// Creates a new style attribute with the given value from Fontconfig. - /// - /// The values are determined based on the [fonts.conf documentation]. - /// - /// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html - fn from_fc(slant: i32) -> Self { - match slant { - 100 => Self::Italic, - 110 => Self::Oblique(None), - _ => Self::Normal, - } - } -} - -impl FontWeight { - /// Creates a new weight attribute with the given value from Fontconfig. - /// - /// The values are determined based on the [fonts.conf documentation]. - /// - /// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html - fn from_fc(weight: i32) -> Self { - const MAP: &[(i32, i32)] = &[ - (0, 0), - (100, 0), - (200, 40), - (300, 50), - (350, 55), - (380, 75), - (400, 80), - (500, 100), - (600, 180), - (700, 200), - (800, 205), - (900, 210), - (950, 215), - ]; - for (i, (ot, fc)) in MAP.iter().skip(1).enumerate() { - if weight == *fc { - return Self::new(*ot as f32); - } - if weight < *fc { - let weight = weight as f32; - let fc_a = MAP[i - 1].1 as f32; - let fc_b = *fc as f32; - let ot_a = MAP[i - 1].1 as f32; - let ot_b = *ot as f32; - let t = (fc_a - fc_b) / (weight - fc_a); - return Self::new(ot_a + (ot_b - ot_a) * t); - } - } - Self::EXTRA_BLACK - } -} - #[derive(Default)] pub struct CachedFont { pub family: Vec, @@ -177,21 +100,21 @@ fn parse_font( Object::Slant => { for val in elt.values().ok()? { if let Value::Int(i) = val.ok()? { - font.style = FontStyle::from_fc(i as _); + font.style = FontStyle::from_fontconfig(i as _); } } } Object::Weight => { for val in elt.values().ok()? { if let Value::Int(i) = val.ok()? { - font.weight = FontWeight::from_fc(i as _); + font.weight = FontWeight::from_fontconfig(i as _); } } } Object::Width => { for val in elt.values().ok()? { if let Value::Int(i) = val.ok()? { - font.stretch = FontStretch::from_fc(i as _); + font.stretch = FontStretch::from_fontconfig(i as _); } } } From 3ee432787b24a2965e671c0a08ffe2cc2f4936e3 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Mon, 9 Dec 2024 18:46:18 +0700 Subject: [PATCH 2/3] Update fontique/src/attributes.rs Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> --- fontique/src/attributes.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fontique/src/attributes.rs b/fontique/src/attributes.rs index 8f807bbf..1e9a4ccf 100644 --- a/fontique/src/attributes.rs +++ b/fontique/src/attributes.rs @@ -333,6 +333,8 @@ impl FontWeight { /// /// [fonts.conf documentation]: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html pub fn from_fontconfig(weight: i32) -> Self { + // A selection of OpenType weights (first) and their corresponding fontconfig value (second) + // Invariant: The fontconfig values are sorted const MAP: &[(i32, i32)] = &[ (0, 0), (100, 0), From b0e2617c91a5b8ec7a593bb7703b34193d5d662d Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Mon, 9 Dec 2024 18:46:26 +0700 Subject: [PATCH 3/3] Update fontique/src/attributes.rs Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> --- fontique/src/attributes.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/fontique/src/attributes.rs b/fontique/src/attributes.rs index 1e9a4ccf..d57768e5 100644 --- a/fontique/src/attributes.rs +++ b/fontique/src/attributes.rs @@ -354,6 +354,7 @@ impl FontWeight { if weight == *fc { return Self::new(*ot as f32); } + // Linear interpolation if not an exact match if weight < *fc { let weight = weight as f32; let fc_a = MAP[i - 1].1 as f32;