cosmic_text/font/
system.rs

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::{FaceInfo, Query, Style};
10use skrifa::raw::{ReadError, TableProvider as _};
11use skrifa::MetadataProvider;
12
13// re-export fontdb and harfrust
14pub use fontdb;
15pub use harfrust;
16
17use super::fallback::{Fallback, Fallbacks, MonospaceFallbackInfo, PlatformFallback};
18
19// The fields are used in the derived Ord implementation for sorting fallback candidates.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
21pub struct FontMatchKey {
22    pub(crate) not_emoji: bool,
23    pub(crate) font_weight_diff: u16,
24    pub(crate) font_stretch_diff: u16,
25    pub(crate) font_style_diff: u8,
26    pub(crate) font_weight: u16,
27    pub(crate) font_stretch: u16,
28    pub(crate) id: fontdb::ID,
29    pub(crate) variable_weight_match: bool,
30}
31
32impl FontMatchKey {
33    fn new(attrs: &Attrs, face: &FaceInfo, db: &fontdb::Database) -> FontMatchKey {
34        // TODO: smarter way of detecting emoji
35        let not_emoji = !face.post_script_name.contains("Emoji");
36        let font_weight_diff = attrs.weight.0.abs_diff(face.weight.0);
37
38        let variable_weight_match = font_weight_diff != 0
39            && db.with_face_data(face.id, |font_data, face_index| {
40                let font_ref = skrifa::FontRef::from_index(font_data, face_index).ok()?;
41                let axis = font_ref.axes().get_by_tag(skrifa::Tag::new(b"wght"))?;
42                let w = attrs.weight.0 as f32;
43                Some(w >= axis.min_value() && w <= axis.max_value())
44            }) == Some(Some(true));
45        let font_weight = face.weight.0;
46        let font_stretch_diff = attrs.stretch.to_number().abs_diff(face.stretch.to_number());
47        let font_stretch = face.stretch.to_number();
48        let font_style_diff = match (attrs.style, face.style) {
49            (Style::Normal, Style::Normal)
50            | (Style::Italic, Style::Italic)
51            | (Style::Oblique, Style::Oblique) => 0,
52            (Style::Italic, Style::Oblique) | (Style::Oblique, Style::Italic) => 1,
53            (Style::Normal, Style::Italic)
54            | (Style::Normal, Style::Oblique)
55            | (Style::Italic, Style::Normal)
56            | (Style::Oblique, Style::Normal) => 2,
57        };
58        let id = face.id;
59        FontMatchKey {
60            not_emoji,
61            font_weight_diff,
62            font_stretch_diff,
63            font_style_diff,
64            font_weight,
65            font_stretch,
66            id,
67            variable_weight_match,
68        }
69    }
70}
71
72struct FontCachedCodepointSupportInfo {
73    supported: Vec<u32>,
74    not_supported: Vec<u32>,
75}
76
77impl FontCachedCodepointSupportInfo {
78    const SUPPORTED_MAX_SZ: usize = 512;
79    const NOT_SUPPORTED_MAX_SZ: usize = 1024;
80
81    fn new() -> Self {
82        Self {
83            supported: Vec::with_capacity(Self::SUPPORTED_MAX_SZ),
84            not_supported: Vec::with_capacity(Self::NOT_SUPPORTED_MAX_SZ),
85        }
86    }
87
88    #[inline(always)]
89    fn unknown_has_codepoint(
90        &mut self,
91        font_codepoints: &[u32],
92        codepoint: u32,
93        supported_insert_pos: usize,
94        not_supported_insert_pos: usize,
95    ) -> bool {
96        let ret = font_codepoints.contains(&codepoint);
97        if ret {
98            // don't bother inserting if we are going to truncate the entry away
99            if supported_insert_pos != Self::SUPPORTED_MAX_SZ {
100                self.supported.insert(supported_insert_pos, codepoint);
101                self.supported.truncate(Self::SUPPORTED_MAX_SZ);
102            }
103        } else {
104            // don't bother inserting if we are going to truncate the entry away
105            if not_supported_insert_pos != Self::NOT_SUPPORTED_MAX_SZ {
106                self.not_supported
107                    .insert(not_supported_insert_pos, codepoint);
108                self.not_supported.truncate(Self::NOT_SUPPORTED_MAX_SZ);
109            }
110        }
111        ret
112    }
113
114    #[inline(always)]
115    fn has_codepoint(&mut self, font_codepoints: &[u32], codepoint: u32) -> bool {
116        match self.supported.binary_search(&codepoint) {
117            Ok(_) => true,
118            Err(supported_insert_pos) => match self.not_supported.binary_search(&codepoint) {
119                Ok(_) => false,
120                Err(not_supported_insert_pos) => self.unknown_has_codepoint(
121                    font_codepoints,
122                    codepoint,
123                    supported_insert_pos,
124                    not_supported_insert_pos,
125                ),
126            },
127        }
128    }
129}
130
131/// Access to the system fonts.
132pub struct FontSystem {
133    /// The locale of the system.
134    locale: String,
135
136    /// The underlying font database.
137    db: fontdb::Database,
138
139    /// Cache for loaded fonts from the database.
140    font_cache: HashMap<(fontdb::ID, fontdb::Weight), Option<Arc<Font>>>,
141
142    /// Sorted unique ID's of all Monospace fonts in DB
143    monospace_font_ids: Vec<fontdb::ID>,
144
145    /// Sorted unique ID's of all Monospace fonts in DB per script.
146    /// A font may support multiple scripts of course, so the same ID
147    /// may appear in multiple map value vecs.
148    per_script_monospace_font_ids: HashMap<[u8; 4], Vec<fontdb::ID>>,
149
150    /// Cache for font codepoint support info
151    font_codepoint_support_info_cache: HashMap<fontdb::ID, FontCachedCodepointSupportInfo>,
152
153    /// Cache for font matches.
154    font_matches_cache: HashMap<FontMatchAttrs, Arc<Vec<FontMatchKey>>>,
155
156    /// Scratch buffer for shaping and laying out.
157    pub(crate) shape_buffer: ShapeBuffer,
158
159    /// Buffer for use in `FontFallbackIter`.
160    pub(crate) monospace_fallbacks_buffer: BTreeSet<MonospaceFallbackInfo>,
161
162    /// Cache for shaped runs
163    #[cfg(feature = "shape-run-cache")]
164    pub shape_run_cache: crate::ShapeRunCache,
165
166    /// List of fallbacks
167    pub(crate) dyn_fallback: Box<dyn Fallback>,
168
169    /// List of fallbacks
170    pub(crate) fallbacks: Fallbacks,
171}
172
173impl fmt::Debug for FontSystem {
174    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175        f.debug_struct("FontSystem")
176            .field("locale", &self.locale)
177            .field("db", &self.db)
178            .finish_non_exhaustive()
179    }
180}
181
182impl FontSystem {
183    const FONT_MATCHES_CACHE_SIZE_LIMIT: usize = 256;
184    /// Create a new [`FontSystem`], that allows access to any installed system fonts
185    ///
186    /// # Timing
187    ///
188    /// This function takes some time to run. On the release build, it can take up to a second,
189    /// while debug builds can take up to ten times longer. For this reason, it should only be
190    /// called once, and the resulting [`FontSystem`] should be shared.
191    pub fn new() -> Self {
192        Self::new_with_fonts(core::iter::empty())
193    }
194
195    /// Create a new [`FontSystem`] with a pre-specified set of fonts.
196    pub fn new_with_fonts(fonts: impl IntoIterator<Item = fontdb::Source>) -> Self {
197        let locale = Self::get_locale();
198        log::debug!("Locale: {locale}");
199
200        let mut db = fontdb::Database::new();
201
202        Self::load_fonts(&mut db, fonts.into_iter());
203
204        //TODO: configurable default fonts
205        db.set_monospace_family("Noto Sans Mono");
206        db.set_sans_serif_family("Open Sans");
207        db.set_serif_family("DejaVu Serif");
208
209        Self::new_with_locale_and_db_and_fallback(locale, db, PlatformFallback)
210    }
211
212    /// Create a new [`FontSystem`] with a pre-specified locale, font database and font fallback list.
213    pub fn new_with_locale_and_db_and_fallback(
214        locale: String,
215        db: fontdb::Database,
216        impl_fallback: impl Fallback + 'static,
217    ) -> Self {
218        let mut monospace_font_ids = db
219            .faces()
220            .filter(|face_info| {
221                face_info.monospaced && !face_info.post_script_name.contains("Emoji")
222            })
223            .map(|face_info| face_info.id)
224            .collect::<Vec<_>>();
225        monospace_font_ids.sort();
226
227        let mut per_script_monospace_font_ids: HashMap<[u8; 4], BTreeSet<fontdb::ID>> =
228            HashMap::default();
229
230        if cfg!(feature = "monospace_fallback") {
231            for &id in &monospace_font_ids {
232                db.with_face_data(id, |font_data, face_index| {
233                    let face = skrifa::FontRef::from_index(font_data, face_index)?;
234                    for script in face
235                        .gpos()?
236                        .script_list()?
237                        .script_records()
238                        .iter()
239                        .chain(face.gsub()?.script_list()?.script_records().iter())
240                    {
241                        per_script_monospace_font_ids
242                            .entry(script.script_tag().into_bytes())
243                            .or_default()
244                            .insert(id);
245                    }
246                    Ok::<_, ReadError>(())
247                });
248            }
249        }
250
251        let per_script_monospace_font_ids = per_script_monospace_font_ids
252            .into_iter()
253            .map(|(k, v)| (k, Vec::from_iter(v)))
254            .collect();
255
256        let fallbacks = Fallbacks::new(&impl_fallback, &[], &locale);
257
258        Self {
259            locale,
260            db,
261            monospace_font_ids,
262            per_script_monospace_font_ids,
263            font_cache: HashMap::default(),
264            font_matches_cache: HashMap::default(),
265            font_codepoint_support_info_cache: HashMap::default(),
266            monospace_fallbacks_buffer: BTreeSet::default(),
267            #[cfg(feature = "shape-run-cache")]
268            shape_run_cache: crate::ShapeRunCache::default(),
269            shape_buffer: ShapeBuffer::default(),
270            dyn_fallback: Box::new(impl_fallback),
271            fallbacks,
272        }
273    }
274
275    /// Create a new [`FontSystem`] with a pre-specified locale and font database.
276    pub fn new_with_locale_and_db(locale: String, db: fontdb::Database) -> Self {
277        Self::new_with_locale_and_db_and_fallback(locale, db, PlatformFallback)
278    }
279
280    /// Get the locale.
281    pub fn locale(&self) -> &str {
282        &self.locale
283    }
284
285    /// Get the database.
286    pub const fn db(&self) -> &fontdb::Database {
287        &self.db
288    }
289
290    /// Get a mutable reference to the database.
291    pub fn db_mut(&mut self) -> &mut fontdb::Database {
292        self.font_matches_cache.clear();
293        &mut self.db
294    }
295
296    /// Consume this [`FontSystem`] and return the locale and database.
297    pub fn into_locale_and_db(self) -> (String, fontdb::Database) {
298        (self.locale, self.db)
299    }
300
301    /// Get a font by its ID and weight.
302    pub fn get_font(&mut self, id: fontdb::ID, weight: fontdb::Weight) -> Option<Arc<Font>> {
303        self.font_cache
304            .entry((id, weight))
305            .or_insert_with(|| {
306                #[cfg(feature = "std")]
307                unsafe {
308                    self.db.make_shared_face_data(id);
309                }
310                if let Some(font) = Font::new(&self.db, id, weight) {
311                    Some(Arc::new(font))
312                } else {
313                    log::warn!(
314                        "failed to load font '{}'",
315                        self.db.face(id)?.post_script_name
316                    );
317                    None
318                }
319            })
320            .clone()
321    }
322
323    pub fn is_monospace(&self, id: fontdb::ID) -> bool {
324        self.monospace_font_ids.binary_search(&id).is_ok()
325    }
326
327    pub fn get_monospace_ids_for_scripts(
328        &self,
329        scripts: impl Iterator<Item = [u8; 4]>,
330    ) -> Vec<fontdb::ID> {
331        let mut ret = scripts
332            .filter_map(|script| self.per_script_monospace_font_ids.get(&script))
333            .flat_map(|ids| ids.iter().copied())
334            .collect::<Vec<_>>();
335        ret.sort();
336        ret.dedup();
337        ret
338    }
339
340    #[inline(always)]
341    pub fn get_font_supported_codepoints_in_word(
342        &mut self,
343        id: fontdb::ID,
344        weight: fontdb::Weight,
345        word: &str,
346    ) -> Option<usize> {
347        self.get_font(id, weight).map(|font| {
348            let code_points = font.unicode_codepoints();
349            let cache = self
350                .font_codepoint_support_info_cache
351                .entry(id)
352                .or_insert_with(FontCachedCodepointSupportInfo::new);
353            word.chars()
354                .filter(|ch| cache.has_codepoint(code_points, u32::from(*ch)))
355                .count()
356        })
357    }
358
359    pub fn get_font_matches(&mut self, attrs: &Attrs<'_>) -> Arc<Vec<FontMatchKey>> {
360        // Clear the cache first if it reached the size limit
361        if self.font_matches_cache.len() >= Self::FONT_MATCHES_CACHE_SIZE_LIMIT {
362            log::trace!("clear font mache cache");
363            self.font_matches_cache.clear();
364        }
365
366        self.font_matches_cache
367            //TODO: do not create AttrsOwned unless entry does not already exist
368            .entry(attrs.into())
369            .or_insert_with(|| {
370                #[cfg(all(feature = "std", not(target_arch = "wasm32")))]
371                let now = std::time::Instant::now();
372
373                let mut font_match_keys = self
374                    .db
375                    .faces()
376                    .map(|face| FontMatchKey::new(attrs, face, &self.db))
377                    .collect::<Vec<_>>();
378
379                // Sort so we get the keys with weight_offset=0 first
380                font_match_keys.sort();
381
382                // db.query is better than above, but returns just one font
383                let query = Query {
384                    families: &[attrs.family],
385                    weight: attrs.weight,
386                    stretch: attrs.stretch,
387                    style: attrs.style,
388                };
389
390                if let Some(id) = self.db.query(&query) {
391                    if let Some(i) = font_match_keys
392                        .iter()
393                        .enumerate()
394                        .find(|(_i, key)| key.id == id)
395                        .map(|(i, _)| i)
396                    {
397                        // if exists move to front
398                        let match_key = font_match_keys.remove(i);
399                        font_match_keys.insert(0, match_key);
400                    } else if let Some(face) = self.db.face(id) {
401                        // else insert in front
402                        let match_key = FontMatchKey::new(attrs, face, &self.db);
403                        font_match_keys.insert(0, match_key);
404                    } else {
405                        log::error!("Could not get face from db, that should've been there.");
406                    }
407                }
408
409                #[cfg(all(feature = "std", not(target_arch = "wasm32")))]
410                {
411                    let elapsed = now.elapsed();
412                    log::debug!("font matches for {attrs:?} in {elapsed:?}");
413                }
414
415                Arc::new(font_match_keys)
416            })
417            .clone()
418    }
419
420    #[cfg(feature = "std")]
421    fn get_locale() -> String {
422        sys_locale::get_locale().unwrap_or_else(|| {
423            log::warn!("failed to get system locale, falling back to en-US");
424            String::from("en-US")
425        })
426    }
427
428    #[cfg(not(feature = "std"))]
429    fn get_locale() -> String {
430        String::from("en-US")
431    }
432
433    #[cfg(feature = "std")]
434    fn load_fonts(db: &mut fontdb::Database, fonts: impl Iterator<Item = fontdb::Source>) {
435        #[cfg(not(target_arch = "wasm32"))]
436        let now = std::time::Instant::now();
437
438        db.load_system_fonts();
439
440        for source in fonts {
441            db.load_font_source(source);
442        }
443
444        #[cfg(not(target_arch = "wasm32"))]
445        log::debug!(
446            "Parsed {} font faces in {}ms.",
447            db.len(),
448            now.elapsed().as_millis()
449        );
450    }
451
452    #[cfg(not(feature = "std"))]
453    fn load_fonts(db: &mut fontdb::Database, fonts: impl Iterator<Item = fontdb::Source>) {
454        for source in fonts {
455            db.load_font_source(source);
456        }
457    }
458}
459
460/// A value borrowed together with an [`FontSystem`]
461#[derive(Debug)]
462pub struct BorrowedWithFontSystem<'a, T> {
463    pub(crate) inner: &'a mut T,
464    pub(crate) font_system: &'a mut FontSystem,
465}
466
467impl<T> Deref for BorrowedWithFontSystem<'_, T> {
468    type Target = T;
469
470    fn deref(&self) -> &Self::Target {
471        self.inner
472    }
473}
474
475impl<T> DerefMut for BorrowedWithFontSystem<'_, T> {
476    fn deref_mut(&mut self) -> &mut Self::Target {
477        self.inner
478    }
479}