Skip to main content

cranpose_ui/text/
unit.rs

1/// A text measurement given in scaled pixels or as a multiple of the font size.
2/// Compose's `TextUnit`.
3#[derive(Clone, Copy, Debug, PartialEq, Default)]
4pub enum TextUnit {
5    /// No value is set. The property takes its value from the enclosing style or a default.
6    #[default]
7    Unspecified,
8    /// Scaled pixels: logical pixels that also follow the user's font scale setting.
9    Sp(f32),
10    /// A multiple of the font size, so `Em(1.5)` is one and a half times the font size.
11    Em(f32),
12}
13
14impl TextUnit {
15    /// Returns true for `Unspecified`.
16    pub fn is_unspecified(&self) -> bool {
17        matches!(self, TextUnit::Unspecified)
18    }
19
20    /// Returns true for `Sp` and `Em`.
21    pub fn is_specified(&self) -> bool {
22        !self.is_unspecified()
23    }
24
25    /// Returns the number inside `Sp` or `Em`, or `NaN` for `Unspecified`.
26    pub fn value(&self) -> f32 {
27        match self {
28            TextUnit::Unspecified => f32::NAN,
29            TextUnit::Sp(v) => *v,
30            TextUnit::Em(v) => *v,
31        }
32    }
33}