whisker_css/prop/
typography.rs1use crate::css::Css;
4use crate::data_type::{CssString, Length, LengthPercentage};
5use crate::keyword::{FontStyle, FontVariant, FontWeight};
6use crate::value::LineHeight;
7
8impl Css {
9 pub fn font_family(self, v: impl Into<String>) -> Self {
14 self.push("font-family", CssString::new(v))
17 }
18
19 pub fn font_size(self, v: impl Into<LengthPercentage>) -> Self {
22 self.push("font-size", v.into())
23 }
24
25 pub fn font_style(self, v: FontStyle) -> Self {
28 self.push("font-style", v)
29 }
30
31 pub fn font_weight(self, v: FontWeight) -> Self {
35 self.push("font-weight", v)
36 }
37
38 pub fn font_variant(self, v: FontVariant) -> Self {
41 self.push("font-variant", v)
42 }
43
44 pub fn letter_spacing(self, v: Length) -> Self {
47 self.push("letter-spacing", v)
48 }
49
50 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}