Skip to main content

freya_core/style/
text_height.rs

1use freya_engine::prelude::*;
2
3/// Controls whether the extra leading height of the first and last lines is kept or trimmed.
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Default)]
6pub enum TextHeightBehavior {
7    /// Keep the leading on both the first ascent and the last descent.
8    All = 0,
9    /// Trim the leading above the first line.
10    DisableFirstAscent = 1,
11    /// Trim the leading below the last line.
12    DisableLastDescent = 2,
13    /// Trim the leading on both the first and last lines. This is the default.
14    #[default]
15    DisableAll = 3,
16}
17
18impl TextHeightBehavior {
19    pub fn needs_custom_height(&self) -> bool {
20        matches!(
21            self,
22            Self::All | Self::DisableFirstAscent | Self::DisableLastDescent
23        )
24    }
25
26    pub fn pretty(&self) -> String {
27        match self {
28            Self::All => "All".to_string(),
29            Self::DisableFirstAscent => "DisableFirstAscent".to_string(),
30            Self::DisableLastDescent => "DisableLastDescent".to_string(),
31            Self::DisableAll => "DisableAll".to_string(),
32        }
33    }
34}
35
36impl From<TextHeightBehavior> for SkTextHeightBehavior {
37    fn from(value: TextHeightBehavior) -> Self {
38        match value {
39            TextHeightBehavior::All => SkTextHeightBehavior::All,
40            TextHeightBehavior::DisableAll => SkTextHeightBehavior::DisableAll,
41            TextHeightBehavior::DisableFirstAscent => SkTextHeightBehavior::DisableFirstAscent,
42            TextHeightBehavior::DisableLastDescent => SkTextHeightBehavior::DisableLastDescent,
43        }
44    }
45}