Skip to main content

freya_core/style/
font_feature.rs

1use std::borrow::Cow;
2
3/// An OpenType feature of the font, enabled with a value.
4///
5/// Implements `From<&'static str>`, which enables the feature with a value of `1`,
6/// and `From<(&'static str, i32)>`. Common features are available as constants,
7/// like [`FontFeature::TABULAR_NUMBERS`].
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[derive(Debug, PartialEq, Eq, Clone, Hash)]
10pub struct FontFeature {
11    pub name: Cow<'static, str>,
12    pub value: i32,
13}
14
15impl FontFeature {
16    pub const fn new(name: &'static str, value: i32) -> Self {
17        FontFeature {
18            name: Cow::Borrowed(name),
19            value,
20        }
21    }
22
23    /// Every digit shares the same width, so changing numbers do not shift the layout.
24    pub const TABULAR_NUMBERS: Self = Self::new("tnum", 1);
25    /// Lowercase letters render as small capitals.
26    pub const SMALL_CAPS: Self = Self::new("smcp", 1);
27    /// Zero renders with a slash or dot to tell it apart from the letter O.
28    pub const SLASHED_ZERO: Self = Self::new("zero", 1);
29    /// Sequences like `1/2` render as fractions.
30    pub const FRACTIONS: Self = Self::new("frac", 1);
31    /// Standard ligatures like `fi` are disabled.
32    pub const NO_LIGATURES: Self = Self::new("liga", 0);
33}
34
35impl From<&'static str> for FontFeature {
36    fn from(name: &'static str) -> Self {
37        Self::new(name, 1)
38    }
39}
40
41impl<T: Into<Cow<'static, str>>> From<(T, i32)> for FontFeature {
42    fn from((name, value): (T, i32)) -> Self {
43        FontFeature {
44            name: name.into(),
45            value,
46        }
47    }
48}