Skip to content

Commit 2b7c9c7

Browse files
feature/fontique - switch femtovg over and use a default fontdb for usvg (#9426)
* Use default fontdb for usvg * Start adapting femtovg * Use DesignFontMetrics from sharedfontique * Restore fallback fonts * [autofix.ci] apply automated fixes * Add allow(dead_code) tag * [autofix.ci] apply automated fixes * Tiny comment change --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent aee9609 commit 2b7c9c7

File tree

8 files changed

+109
-161
lines changed

8 files changed

+109
-161
lines changed

internal/common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ path = "lib.rs"
1919
[features]
2020
default = []
2121
shared-fontdb = ["dep:fontdb", "dep:libloading", "derive_more", "cfg-if", "dep:ttf-parser"]
22-
shared-fontique = ["dep:fontique"]
22+
shared-fontique = ["dep:fontique", "dep:ttf-parser"]
2323

2424
[dependencies]
2525
fontdb = { workspace = true, optional = true }

internal/common/sharedfontique.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
33

44
pub use fontique;
5+
pub use ttf_parser;
56

67
static COLLECTION: std::sync::LazyLock<Collection> = std::sync::LazyLock::new(|| Collection {
78
inner: fontique::Collection::new(fontique::CollectionOptions {
@@ -30,3 +31,41 @@ impl Collection {
3031
self.inner.register_fonts(data.into(), None).len()
3132
}
3233
}
34+
35+
impl std::ops::Deref for Collection {
36+
type Target = fontique::Collection;
37+
38+
fn deref(&self) -> &Self::Target {
39+
&self.inner
40+
}
41+
}
42+
43+
impl std::ops::DerefMut for Collection {
44+
fn deref_mut(&mut self) -> &mut Self::Target {
45+
&mut self.inner
46+
}
47+
}
48+
49+
/// Font metrics in design space. Scale with desired pixel size and divided by units_per_em
50+
/// to obtain pixel metrics.
51+
#[derive(Clone)]
52+
pub struct DesignFontMetrics {
53+
pub ascent: f32,
54+
pub descent: f32,
55+
pub x_height: f32,
56+
pub cap_height: f32,
57+
pub units_per_em: f32,
58+
}
59+
60+
impl DesignFontMetrics {
61+
pub fn new(font: &fontique::QueryFont) -> Self {
62+
let face = ttf_parser::Face::parse(font.blob.data(), font.index).unwrap();
63+
Self {
64+
ascent: face.ascender() as f32,
65+
descent: face.descender() as f32,
66+
x_height: face.x_height().unwrap_or_default() as f32,
67+
cap_height: face.capital_height().unwrap_or_default() as f32,
68+
units_per_em: face.units_per_em() as f32,
69+
}
70+
}
71+
}

internal/compiler/passes/embed_images.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ fn load_image(
372372
if file.canon_path.extension() == Some(OsStr::new("svg"))
373373
|| file.canon_path.extension() == Some(OsStr::new("svgz"))
374374
{
375-
let tree = i_slint_common::sharedfontdb::FONT_DB.with_borrow(|db| {
376-
let option = usvg::Options { fontdb: (*db).clone(), ..Default::default() };
375+
let tree = {
376+
let option = usvg::Options::default();
377377
match file.builtin_contents {
378378
Some(data) => usvg::Tree::from_data(data, &option),
379379
None => usvg::Tree::from_data(
@@ -387,7 +387,7 @@ fn load_image(
387387
e,
388388
))
389389
})
390-
})?;
390+
}?;
391391
let scale_factor = scale_factor as f32;
392392
// TODO: ideally we should find the size used for that `Image`
393393
let original_size = tree.size();

internal/core/graphics.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,6 @@ pub struct FontRequest {
155155
pub italic: bool,
156156
}
157157

158-
#[cfg(feature = "shared-fontdb")]
159-
impl FontRequest {
160-
/// Returns the relevant properties of this FontRequest propagated into a fontdb Query.
161-
pub fn to_fontdb_query(&self) -> i_slint_common::sharedfontdb::fontdb::Query<'_> {
162-
use i_slint_common::sharedfontdb::fontdb::{Query, Style, Weight};
163-
Query {
164-
style: if self.italic { Style::Italic } else { Style::Normal },
165-
weight: Weight(self.weight.unwrap_or(/* CSS normal*/ 400) as _),
166-
..Default::default()
167-
}
168-
}
169-
}
170-
171158
#[cfg(feature = "shared-fontique")]
172159
impl FontRequest {
173160
/// Attempts to query the fontique font collection for a matching font.

internal/core/graphics/image/svg.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,13 @@ pub fn load_from_path(
8181
) -> Result<ParsedSVG, std::io::Error> {
8282
let svg_data = std::fs::read(std::path::Path::new(&path.as_str()))?;
8383

84-
i_slint_common::sharedfontdb::FONT_DB.with_borrow(|db| {
85-
let option = usvg::Options { fontdb: (*db).clone(), ..Default::default() };
86-
usvg::Tree::from_data(&svg_data, &option)
87-
.map(|svg| ParsedSVG { svg_tree: svg, cache_key })
88-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
89-
})
84+
let option = usvg::Options::default();
85+
usvg::Tree::from_data(&svg_data, &option)
86+
.map(|svg| ParsedSVG { svg_tree: svg, cache_key })
87+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
9088
}
9189

9290
pub fn load_from_data(slice: &[u8], cache_key: ImageCacheKey) -> Result<ParsedSVG, usvg::Error> {
93-
i_slint_common::sharedfontdb::FONT_DB.with_borrow(|db| {
94-
let option = usvg::Options { fontdb: (*db).clone(), ..Default::default() };
95-
usvg::Tree::from_data(slice, &option).map(|svg| ParsedSVG { svg_tree: svg, cache_key })
96-
})
91+
let option = usvg::Options::default();
92+
usvg::Tree::from_data(slice, &option).map(|svg| ParsedSVG { svg_tree: svg, cache_key })
9793
}

internal/renderers/femtovg/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ wgpu-26 = ["dep:wgpu-26", "femtovg/wgpu", "i-slint-core/unstable-wgpu-26"]
2323
unstable-wgpu-26 = ["wgpu-26"]
2424

2525
[dependencies]
26-
i-slint-core = { workspace = true, features = ["default", "box-shadow-cache", "shared-fontdb"] }
26+
i-slint-core = { workspace = true, features = ["default", "box-shadow-cache", "shared-fontique"] }
2727
i-slint-core-macros = { workspace = true, features = ["default"] }
2828
i-slint-common = { workspace = true, features = ["default", "shared-fontique"] }
2929

0 commit comments

Comments
 (0)