Skip to main content

incular_text/style/
icons.rs

1use super::fonts::{FontFamily, FontVariation};
2use super::text_style::TextStyle;
3use std::sync::Arc;
4
5/// Immutable icon font metadata.
6///
7/// Icon glyphs use the normal text shaping pipeline: this value only carries
8/// the code point and font selection metadata, then [`Self::text_style`]
9/// produces the same [`TextStyle`] used by ordinary text. There is no second
10/// icon renderer or path model to keep synchronized with text.
11#[derive(Clone, Debug, PartialEq)]
12pub struct IconData {
13    code_point: u32,
14    font_family: Option<FontFamily>,
15    font_package: Option<String>,
16    match_text_direction: bool,
17    font_variations: Arc<[FontVariation]>,
18}
19
20impl IconData {
21    #[must_use]
22    pub fn new(code_point: u32) -> Self {
23        Self {
24            code_point,
25            font_family: None,
26            font_package: None,
27            match_text_direction: false,
28            font_variations: Arc::from([]),
29        }
30    }
31
32    #[must_use]
33    pub const fn code_point(&self) -> u32 {
34        self.code_point
35    }
36
37    #[must_use]
38    pub fn glyph(&self) -> Option<char> {
39        char::from_u32(self.code_point)
40    }
41
42    #[must_use]
43    pub fn glyph_text(&self) -> Option<String> {
44        self.glyph().map(|glyph| glyph.to_string())
45    }
46
47    #[must_use]
48    pub fn font_family_value(&self) -> Option<&FontFamily> {
49        self.font_family.as_ref()
50    }
51
52    #[must_use]
53    pub fn font_package_value(&self) -> Option<&str> {
54        self.font_package.as_deref()
55    }
56
57    #[must_use]
58    pub const fn match_text_direction_value(&self) -> bool {
59        self.match_text_direction
60    }
61
62    #[must_use]
63    pub fn font_variations(&self) -> &[FontVariation] {
64        &self.font_variations
65    }
66
67    #[must_use]
68    pub fn family(mut self, family: FontFamily) -> Self {
69        self.font_family = Some(family);
70        self
71    }
72
73    #[must_use]
74    pub fn font_family(mut self, family: impl Into<String>) -> Self {
75        self.font_family = Some(FontFamily::named(family));
76        self
77    }
78
79    #[must_use]
80    pub fn font_package(mut self, package: impl Into<String>) -> Self {
81        self.font_package = Some(package.into());
82        self
83    }
84
85    #[must_use]
86    pub fn match_text_direction(mut self, match_text_direction: bool) -> Self {
87        self.match_text_direction = match_text_direction;
88        self
89    }
90
91    #[must_use]
92    pub fn variations<I>(mut self, variations: I) -> Self
93    where
94        I: IntoIterator<Item = FontVariation>,
95    {
96        self.font_variations = variations.into_iter().collect::<Vec<_>>().into();
97        self
98    }
99
100    /// Builds the ordinary text style used to shape this glyph.
101    #[must_use]
102    pub fn text_style(&self, size: f32) -> TextStyle {
103        let mut style = TextStyle::default().font_size(size);
104        if let Some(family) = &self.font_family {
105            style = style.family(family.clone());
106        }
107        if !self.font_variations.is_empty() {
108            style = style.font_variation_settings(&self.font_variations);
109        }
110        style
111    }
112}