Skip to main content

gpui/text_system/
font_fallbacks.rs

1use std::sync::Arc;
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6/// Font families to try, in order, when a [`Font`](crate::Font) lacks a glyph.
7///
8/// Set the list on [`Font::fallbacks`](crate::Font::fallbacks). The text system
9/// uses these families in order when the primary family cannot supply the needed
10/// glyphs. Shaping chooses fonts for clusters, which may contain combining marks
11/// or an emoji sequence; it does not choose a font independently for every code
12/// point. Names use the same [`TextSystem`](crate::TextSystem) font database as
13/// the primary family. See the [text guide](crate::_documentation::guides::text)
14/// for loading fonts and working with cluster boundaries.
15///
16/// # Examples
17///
18/// ```
19/// # use gpui::{FontFallbacks, font};
20/// let mut body = font("Lilex");
21/// body.fallbacks = Some(FontFallbacks::from_fonts(vec![
22///     "Noto Sans Arabic".to_string(),
23/// ]));
24/// assert_eq!(body.fallbacks.unwrap().fallback_list(), ["Noto Sans Arabic"]);
25/// ```
26#[derive(Default, Clone, Eq, PartialEq, Hash, Debug, Deserialize, Serialize, JsonSchema)]
27pub struct FontFallbacks(pub Arc<Vec<String>>);
28
29impl FontFallbacks {
30    /// Returns the fallback families in the order the text system tries them.
31    pub fn fallback_list(&self) -> &[String] {
32        self.0.as_slice()
33    }
34
35    /// Creates a fallback list from family names, tried in the order given.
36    pub fn from_fonts(fonts: Vec<String>) -> Self {
37        FontFallbacks(Arc::new(fonts))
38    }
39}