-
Notifications
You must be signed in to change notification settings - Fork 503
/
sharedfontdb.rs
208 lines (189 loc) · 6.98 KB
/
sharedfontdb.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
use std::cell::RefCell;
pub use fontdb;
#[derive(derive_more::Deref, derive_more::DerefMut)]
pub struct FontDatabase {
#[deref]
#[deref_mut]
db: fontdb::Database,
#[cfg(not(any(
target_family = "windows",
target_os = "macos",
target_os = "ios",
target_arch = "wasm32"
)))]
pub fontconfig_fallback_families: Vec<String>,
// Default font families to use instead of SansSerif when SLINT_DEFAULT_FONT env var is set.
pub default_font_family_ids: Vec<fontdb::ID>,
// Same as default_font_families but reduced to unique family names
default_font_family_names: Vec<String>,
}
impl FontDatabase {
pub fn query_with_family(
&self,
query: fontdb::Query<'_>,
family: Option<&'_ str>,
) -> Option<fontdb::ID> {
let mut query = query;
if let Some(specified_family) = family {
let single_family = [fontdb::Family::Name(specified_family)];
query.families = &single_family;
self.db.query(&query)
} else if self.default_font_family_ids.is_empty() {
query.families = &[fontdb::Family::SansSerif];
self.db.query(&query)
} else {
let family_storage = self
.default_font_family_names
.iter()
.map(|name| fontdb::Family::Name(name))
.collect::<Vec<_>>();
query.families = &family_storage;
self.db.query(&query)
}
}
}
thread_local! {
pub static FONT_DB: RefCell<FontDatabase> = RefCell::new(init_fontdb())
}
#[cfg(not(any(
target_family = "windows",
target_os = "macos",
target_os = "ios",
target_arch = "wasm32"
)))]
mod fontconfig;
fn init_fontdb() -> FontDatabase {
let mut font_db = fontdb::Database::new();
#[cfg(not(target_arch = "wasm32"))]
let (default_font_family_ids, default_font_family_names) =
std::env::var_os("SLINT_DEFAULT_FONT")
.and_then(|maybe_font_path| {
let path = std::path::Path::new(&maybe_font_path);
match if path.extension().is_some() {
font_db.load_font_file(path)
} else {
font_db.load_fonts_dir(path);
Ok(())
} {
Ok(_) => {
let mut family_ids = Vec::new();
let mut family_names = Vec::new();
for face_info in font_db.faces() {
family_ids.push(face_info.id);
let family_name = &face_info.families[0].0;
if let Err(insert_pos) = family_names.binary_search(family_name) {
family_names.insert(insert_pos, family_name.clone());
}
}
Some((family_ids, family_names))
}
Err(err) => {
eprintln!(
"Could not load the font set via `SLINT_DEFAULT_FONT`: {}: {}",
path.display(),
err,
);
None
}
}
})
.unwrap_or_default();
#[cfg(target_arch = "wasm32")]
let (default_font_family_ids, default_font_family_names) =
(Default::default(), Default::default());
#[cfg(not(any(
target_family = "windows",
target_os = "macos",
target_os = "ios",
target_arch = "wasm32"
)))]
let mut fontconfig_fallback_families = Vec::new();
#[cfg(target_arch = "wasm32")]
{
let data = include_bytes!("sharedfontdb/DejaVuSans.ttf");
font_db.load_font_data(data.to_vec());
font_db.set_sans_serif_family("DejaVu Sans");
}
#[cfg(not(target_arch = "wasm32"))]
{
font_db.load_system_fonts();
cfg_if::cfg_if! {
if #[cfg(not(any(
target_family = "windows",
target_os = "macos",
target_os = "ios",
target_arch = "wasm32"
)))] {
match fontconfig::find_families("sans-serif") {
Ok(mut fallback_families) => {
if !fallback_families.is_empty() {
let default_sans_serif_family = fallback_families.remove(0);
font_db.set_sans_serif_family(default_sans_serif_family);
}
fontconfig_fallback_families = fallback_families;
}
Err(e) => {
eprintln!("Error opening libfontconfig.so.1: {}", e);
}
}
}
}
if font_db
.query(&fontdb::Query { families: &[fontdb::Family::SansSerif], ..Default::default() })
.is_none()
{
panic!(
"Unable to determine default font. Failed to locate font for family {}",
font_db.family_name(&fontdb::Family::SansSerif)
)
}
}
FontDatabase {
db: font_db,
#[cfg(not(any(
target_family = "windows",
target_os = "macos",
target_os = "ios",
target_arch = "wasm32"
)))]
fontconfig_fallback_families,
default_font_family_ids,
default_font_family_names,
}
}
/// This function can be used to register a custom TrueType font with Slint,
/// for use with the `font-family` property. The provided slice must be a valid TrueType
/// font.
pub fn register_font_from_memory(data: &'static [u8]) -> Result<(), Box<dyn std::error::Error>> {
FONT_DB.with(|db| {
db.borrow_mut().load_font_source(fontdb::Source::Binary(std::sync::Arc::new(data)))
});
Ok(())
}
#[cfg(not(target_arch = "wasm32"))]
pub fn register_font_from_path(path: &std::path::Path) -> Result<(), Box<dyn std::error::Error>> {
let requested_path = path.canonicalize().unwrap_or_else(|_| path.to_owned());
FONT_DB.with(|db| {
for face_info in db.borrow().faces() {
match &face_info.source {
fontdb::Source::Binary(_) => {}
fontdb::Source::File(loaded_path) | fontdb::Source::SharedFile(loaded_path, ..) => {
if *loaded_path == requested_path {
return Ok(());
}
}
}
}
db.borrow_mut().load_font_file(requested_path).map_err(|e| e.into())
})
}
#[cfg(target_arch = "wasm32")]
pub fn register_font_from_path(_path: &std::path::Path) -> Result<(), Box<dyn std::error::Error>> {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Registering fonts from paths is not supported in WASM builds",
)
.into());
}