1use crate::{Attrs, Font, FontMatchAttrs, HashMap, ShapeBuffer};
2use alloc::boxed::Box;
3use alloc::collections::BTreeSet;
4use alloc::string::String;
5use alloc::sync::Arc;
6use alloc::vec::Vec;
7use core::fmt;
8use core::ops::{Deref, DerefMut};
9use fontdb::Query;
10
11pub use fontdb;
13pub use rustybuzz;
14
15use super::fallback::{Fallback, Fallbacks, MonospaceFallbackInfo, PlatformFallback};
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
18pub struct FontMatchKey {
19 pub(crate) font_weight_diff: u16,
20 pub(crate) font_weight: u16,
21 pub(crate) id: fontdb::ID,
22}
23
24struct FontCachedCodepointSupportInfo {
25 supported: Vec<u32>,
26 not_supported: Vec<u32>,
27}
28
29impl FontCachedCodepointSupportInfo {
30 const SUPPORTED_MAX_SZ: usize = 512;
31 const NOT_SUPPORTED_MAX_SZ: usize = 1024;
32
33 fn new() -> Self {
34 Self {
35 supported: Vec::with_capacity(Self::SUPPORTED_MAX_SZ),
36 not_supported: Vec::with_capacity(Self::NOT_SUPPORTED_MAX_SZ),
37 }
38 }
39
40 #[inline(always)]
41 fn unknown_has_codepoint(
42 &mut self,
43 font_codepoints: &[u32],
44 codepoint: u32,
45 supported_insert_pos: usize,
46 not_supported_insert_pos: usize,
47 ) -> bool {
48 let ret = font_codepoints.contains(&codepoint);
49 if ret {
50 if supported_insert_pos != Self::SUPPORTED_MAX_SZ {
52 self.supported.insert(supported_insert_pos, codepoint);
53 self.supported.truncate(Self::SUPPORTED_MAX_SZ);
54 }
55 } else {
56 if not_supported_insert_pos != Self::NOT_SUPPORTED_MAX_SZ {
58 self.not_supported
59 .insert(not_supported_insert_pos, codepoint);
60 self.not_supported.truncate(Self::NOT_SUPPORTED_MAX_SZ);
61 }
62 }
63 ret
64 }
65
66 #[inline(always)]
67 fn has_codepoint(&mut self, font_codepoints: &[u32], codepoint: u32) -> bool {
68 match self.supported.binary_search(&codepoint) {
69 Ok(_) => true,
70 Err(supported_insert_pos) => match self.not_supported.binary_search(&codepoint) {
71 Ok(_) => false,
72 Err(not_supported_insert_pos) => self.unknown_has_codepoint(
73 font_codepoints,
74 codepoint,
75 supported_insert_pos,
76 not_supported_insert_pos,
77 ),
78 },
79 }
80 }
81}
82
83pub struct FontSystem {
85 locale: String,
87
88 db: fontdb::Database,
90
91 font_cache: HashMap<(fontdb::ID, fontdb::Weight), Option<Arc<Font>>>,
93
94 monospace_font_ids: Vec<fontdb::ID>,
96
97 per_script_monospace_font_ids: HashMap<[u8; 4], Vec<fontdb::ID>>,
101
102 font_codepoint_support_info_cache: HashMap<fontdb::ID, FontCachedCodepointSupportInfo>,
104
105 font_matches_cache: HashMap<FontMatchAttrs, Arc<Vec<FontMatchKey>>>,
107
108 pub(crate) shape_buffer: ShapeBuffer,
110
111 pub(crate) monospace_fallbacks_buffer: BTreeSet<MonospaceFallbackInfo>,
113
114 #[cfg(feature = "shape-run-cache")]
116 pub shape_run_cache: crate::ShapeRunCache,
117
118 pub(crate) dyn_fallback: Box<dyn Fallback>,
120
121 pub(crate) fallbacks: Fallbacks,
123}
124
125impl fmt::Debug for FontSystem {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127 f.debug_struct("FontSystem")
128 .field("locale", &self.locale)
129 .field("db", &self.db)
130 .finish_non_exhaustive()
131 }
132}
133
134impl FontSystem {
135 const FONT_MATCHES_CACHE_SIZE_LIMIT: usize = 256;
136 pub fn new() -> Self {
144 Self::new_with_fonts(core::iter::empty())
145 }
146
147 pub fn new_with_fonts(fonts: impl IntoIterator<Item = fontdb::Source>) -> Self {
149 let locale = Self::get_locale();
150 log::debug!("Locale: {locale}");
151
152 let mut db = fontdb::Database::new();
153
154 Self::load_fonts(&mut db, fonts.into_iter());
155
156 db.set_monospace_family("Noto Sans Mono");
158 db.set_sans_serif_family("Open Sans");
159 db.set_serif_family("DejaVu Serif");
160
161 Self::new_with_locale_and_db_and_fallback(locale, db, PlatformFallback)
162 }
163
164 pub fn new_with_locale_and_db_and_fallback(
166 locale: String,
167 db: fontdb::Database,
168 impl_fallback: impl Fallback + 'static,
169 ) -> Self {
170 let mut monospace_font_ids = db
171 .faces()
172 .filter(|face_info| {
173 face_info.monospaced && !face_info.post_script_name.contains("Emoji")
174 })
175 .map(|face_info| face_info.id)
176 .collect::<Vec<_>>();
177 monospace_font_ids.sort();
178
179 let mut per_script_monospace_font_ids: HashMap<[u8; 4], BTreeSet<fontdb::ID>> =
180 HashMap::default();
181
182 if cfg!(feature = "monospace_fallback") {
183 for &id in &monospace_font_ids {
184 db.with_face_data(id, |font_data, face_index| {
185 let _ = ttf_parser::Face::parse(font_data, face_index).map(|face| {
186 face.tables()
187 .gpos
188 .into_iter()
189 .chain(face.tables().gsub)
190 .flat_map(|table| table.scripts)
191 .inspect(|script| {
192 per_script_monospace_font_ids
193 .entry(script.tag.to_bytes())
194 .or_default()
195 .insert(id);
196 })
197 });
198 });
199 }
200 }
201
202 let per_script_monospace_font_ids = per_script_monospace_font_ids
203 .into_iter()
204 .map(|(k, v)| (k, Vec::from_iter(v)))
205 .collect();
206
207 let fallbacks = Fallbacks::new(&impl_fallback, &[], &locale);
208
209 Self {
210 locale,
211 db,
212 monospace_font_ids,
213 per_script_monospace_font_ids,
214 font_cache: HashMap::default(),
215 font_matches_cache: HashMap::default(),
216 font_codepoint_support_info_cache: HashMap::default(),
217 monospace_fallbacks_buffer: BTreeSet::default(),
218 #[cfg(feature = "shape-run-cache")]
219 shape_run_cache: crate::ShapeRunCache::default(),
220 shape_buffer: ShapeBuffer::default(),
221 dyn_fallback: Box::new(impl_fallback),
222 fallbacks,
223 }
224 }
225
226 pub fn new_with_locale_and_db(locale: String, db: fontdb::Database) -> Self {
228 Self::new_with_locale_and_db_and_fallback(locale, db, PlatformFallback)
229 }
230
231 pub fn locale(&self) -> &str {
233 &self.locale
234 }
235
236 pub const fn db(&self) -> &fontdb::Database {
238 &self.db
239 }
240
241 pub fn db_mut(&mut self) -> &mut fontdb::Database {
243 self.font_matches_cache.clear();
244 &mut self.db
245 }
246
247 pub fn into_locale_and_db(self) -> (String, fontdb::Database) {
249 (self.locale, self.db)
250 }
251
252 pub fn get_font(&mut self, id: fontdb::ID, weight: fontdb::Weight) -> Option<Arc<Font>> {
254 self.font_cache
255 .entry((id, weight))
256 .or_insert_with(|| {
257 #[cfg(feature = "std")]
258 unsafe {
259 self.db.make_shared_face_data(id);
260 }
261 if let Some(font) = Font::new(&self.db, id, weight) {
262 Some(Arc::new(font))
263 } else {
264 log::warn!(
265 "failed to load font '{}'",
266 self.db.face(id)?.post_script_name
267 );
268 None
269 }
270 })
271 .clone()
272 }
273
274 pub fn is_monospace(&self, id: fontdb::ID) -> bool {
275 self.monospace_font_ids.binary_search(&id).is_ok()
276 }
277
278 pub fn get_monospace_ids_for_scripts(
279 &self,
280 scripts: impl Iterator<Item = [u8; 4]>,
281 ) -> Vec<fontdb::ID> {
282 let mut ret = scripts
283 .filter_map(|script| self.per_script_monospace_font_ids.get(&script))
284 .flat_map(|ids| ids.iter().copied())
285 .collect::<Vec<_>>();
286 ret.sort();
287 ret.dedup();
288 ret
289 }
290
291 #[inline(always)]
292 pub fn get_font_supported_codepoints_in_word(
293 &mut self,
294 id: fontdb::ID,
295 weight: fontdb::Weight,
296 word: &str,
297 ) -> Option<usize> {
298 self.get_font(id, weight).map(|font| {
299 let code_points = font.unicode_codepoints();
300 let cache = self
301 .font_codepoint_support_info_cache
302 .entry(id)
303 .or_insert_with(FontCachedCodepointSupportInfo::new);
304 word.chars()
305 .filter(|ch| cache.has_codepoint(code_points, u32::from(*ch)))
306 .count()
307 })
308 }
309
310 pub fn get_font_matches(&mut self, attrs: &Attrs<'_>) -> Arc<Vec<FontMatchKey>> {
311 if self.font_matches_cache.len() >= Self::FONT_MATCHES_CACHE_SIZE_LIMIT {
313 log::trace!("clear font mache cache");
314 self.font_matches_cache.clear();
315 }
316
317 self.font_matches_cache
318 .entry(attrs.into())
320 .or_insert_with(|| {
321 #[cfg(all(feature = "std", not(target_arch = "wasm32")))]
322 let now = std::time::Instant::now();
323
324 let mut font_match_keys = self
325 .db
326 .faces()
327 .filter(|face| attrs.matches(face))
328 .map(|face| FontMatchKey {
329 font_weight_diff: attrs.weight.0.abs_diff(face.weight.0),
330 font_weight: face.weight.0,
331 id: face.id,
332 })
333 .collect::<Vec<_>>();
334
335 font_match_keys.sort();
337
338 let query = Query {
340 families: &[attrs.family],
341 weight: attrs.weight,
342 stretch: attrs.stretch,
343 style: attrs.style,
344 };
345
346 if let Some(id) = self.db.query(&query) {
347 if let Some(i) = font_match_keys
348 .iter()
349 .enumerate()
350 .find(|(_i, key)| key.id == id)
351 .map(|(i, _)| i)
352 {
353 let match_key = font_match_keys.remove(i);
355 font_match_keys.insert(0, match_key);
356 } else if let Some(face) = self.db.face(id) {
357 let match_key = FontMatchKey {
359 font_weight_diff: attrs.weight.0.abs_diff(face.weight.0),
360 font_weight: face.weight.0,
361 id,
362 };
363 font_match_keys.insert(0, match_key);
364 } else {
365 log::error!("Could not get face from db, that should've been there.");
366 }
367 }
368
369 #[cfg(all(feature = "std", not(target_arch = "wasm32")))]
370 {
371 let elapsed = now.elapsed();
372 log::debug!("font matches for {attrs:?} in {elapsed:?}");
373 }
374
375 Arc::new(font_match_keys)
376 })
377 .clone()
378 }
379
380 #[cfg(feature = "std")]
381 fn get_locale() -> String {
382 sys_locale::get_locale().unwrap_or_else(|| {
383 log::warn!("failed to get system locale, falling back to en-US");
384 String::from("en-US")
385 })
386 }
387
388 #[cfg(not(feature = "std"))]
389 fn get_locale() -> String {
390 String::from("en-US")
391 }
392
393 #[cfg(feature = "std")]
394 fn load_fonts(db: &mut fontdb::Database, fonts: impl Iterator<Item = fontdb::Source>) {
395 #[cfg(not(target_arch = "wasm32"))]
396 let now = std::time::Instant::now();
397
398 db.load_system_fonts();
399
400 for source in fonts {
401 db.load_font_source(source);
402 }
403
404 #[cfg(not(target_arch = "wasm32"))]
405 log::debug!(
406 "Parsed {} font faces in {}ms.",
407 db.len(),
408 now.elapsed().as_millis()
409 );
410 }
411
412 #[cfg(not(feature = "std"))]
413 fn load_fonts(db: &mut fontdb::Database, fonts: impl Iterator<Item = fontdb::Source>) {
414 for source in fonts {
415 db.load_font_source(source);
416 }
417 }
418}
419
420#[derive(Debug)]
422pub struct BorrowedWithFontSystem<'a, T> {
423 pub(crate) inner: &'a mut T,
424 pub(crate) font_system: &'a mut FontSystem,
425}
426
427impl<T> Deref for BorrowedWithFontSystem<'_, T> {
428 type Target = T;
429
430 fn deref(&self) -> &Self::Target {
431 self.inner
432 }
433}
434
435impl<T> DerefMut for BorrowedWithFontSystem<'_, T> {
436 fn deref_mut(&mut self) -> &mut Self::Target {
437 self.inner
438 }
439}