Skip to main content

whisker_css/prop/
typography.rs

1//! Typography properties: font, letter spacing, line height.
2
3use crate::css::Css;
4use crate::data_type::{CssString, Length, LengthPercentage};
5use crate::keyword::{FontStyle, FontVariant, FontWeight};
6use crate::value::LineHeight;
7
8impl Css {
9    /// Sets `font-family`. Pass a single family name; for multiple
10    /// families, call this method once per family or use the
11    /// [`Css::raw`] escape hatch with a comma-separated list.
12    /// <https://lynxjs.org/api/css/properties/font-family>
13    pub fn font_family(self, v: impl Into<String>) -> Self {
14        // Lynx accepts either bare identifiers or quoted strings; quoting
15        // unconditionally is always safe.
16        self.push("font-family", CssString::new(v))
17    }
18
19    /// Sets `font-size`. Lynx default: `14px`.
20    /// <https://lynxjs.org/api/css/properties/font-size>
21    pub fn font_size(self, v: impl Into<LengthPercentage>) -> Self {
22        self.push("font-size", v.into())
23    }
24
25    /// Sets `font-style`. Lynx default: `normal`.
26    /// <https://lynxjs.org/api/css/properties/font-style>
27    pub fn font_style(self, v: FontStyle) -> Self {
28        self.push("font-style", v)
29    }
30
31    /// Sets `font-weight`. Lynx default: `normal`. `bolder`/`lighter`
32    /// are not supported.
33    /// <https://lynxjs.org/api/css/properties/font-weight>
34    pub fn font_weight(self, v: FontWeight) -> Self {
35        self.push("font-weight", v)
36    }
37
38    /// Sets `font-variant`.
39    /// <https://lynxjs.org/api/css/properties/font-variant>
40    pub fn font_variant(self, v: FontVariant) -> Self {
41        self.push("font-variant", v)
42    }
43
44    /// Sets `letter-spacing`. Accepts `<length>`.
45    /// <https://lynxjs.org/api/css/properties/letter-spacing>
46    pub fn letter_spacing(self, v: Length) -> Self {
47        self.push("letter-spacing", v)
48    }
49
50    /// Sets `line-height`. Lynx default: `normal`.
51    /// <https://lynxjs.org/api/css/properties/line-height>
52    pub fn line_height(self, v: impl Into<LineHeight>) -> Self {
53        self.push("line-height", v.into())
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use crate::ext::*;
60    use crate::keyword::*;
61    use crate::value::LineHeight;
62    use crate::Css;
63
64    #[test]
65    fn font_family_quotes_the_value() {
66        let s = Css::new().font_family("Helvetica Neue");
67        assert_eq!(s.to_string(), "font-family: \"Helvetica Neue\";");
68    }
69
70    #[test]
71    fn font_size_length_or_percentage() {
72        assert_eq!(Css::new().font_size(px(16)).to_string(), "font-size: 16px;");
73        assert_eq!(
74            Css::new().font_size(percent(120)).to_string(),
75            "font-size: 120%;"
76        );
77    }
78
79    #[test]
80    fn font_style_keywords() {
81        assert_eq!(
82            Css::new().font_style(FontStyle::Italic).to_string(),
83            "font-style: italic;"
84        );
85    }
86
87    #[test]
88    fn font_weight_keyword_and_numeric() {
89        assert_eq!(
90            Css::new().font_weight(FontWeight::Bold).to_string(),
91            "font-weight: bold;"
92        );
93        assert_eq!(
94            Css::new().font_weight(FontWeight::Numeric(600)).to_string(),
95            "font-weight: 600;"
96        );
97    }
98
99    #[test]
100    fn font_variant_small_caps() {
101        assert_eq!(
102            Css::new().font_variant(FontVariant::SmallCaps).to_string(),
103            "font-variant: small-caps;"
104        );
105    }
106
107    #[test]
108    fn letter_spacing_length() {
109        let s = Css::new().letter_spacing(px(2));
110        assert_eq!(s.to_string(), "letter-spacing: 2px;");
111    }
112
113    #[test]
114    fn line_height_variants() {
115        assert_eq!(
116            Css::new().line_height(LineHeight::Normal).to_string(),
117            "line-height: normal;"
118        );
119        assert_eq!(
120            Css::new().line_height(1.5_f32).to_string(),
121            "line-height: 1.5;"
122        );
123        assert_eq!(
124            Css::new().line_height(px(24)).to_string(),
125            "line-height: 24px;"
126        );
127        assert_eq!(
128            Css::new().line_height(percent(150)).to_string(),
129            "line-height: 150%;"
130        );
131    }
132}