css_style/
font.rs

1use crate::{unit::*, StyleUpdater};
2use derive_rich::Rich;
3use std::borrow::Cow;
4
5/// ```
6/// use css_style::{prelude::*, Color, unit::em};
7/// use palette::rgb::Rgb;
8///
9/// style()
10///     .and_font(|conf| {
11///         // set the font size to xx-large
12///         conf.xx_large()
13///             // we can set the font size with unit functions too
14///             .size(em(1.5))
15///             // set font variant to smal-caps
16///             .small_caps()
17///             // set font to be bold
18///             .bold()
19///             // we can pick specific weight (e.g. 200)
20///             .weight_200()
21///     });
22/// ```
23#[derive(Rich, Clone, Debug, PartialEq, Default)]
24pub struct Font {
25    #[rich(write, write(option))]
26    pub family: Option<Family>,
27    #[rich(write(rename = size), write(option, rename = try_size), value_fns = {
28        medium = Size::Medium,
29        xx_small = Size::XXSmall,
30        x_small = Size::XSmall,
31        small = Size::Small,
32        large = Size::Large,
33        x_large = Size::XLarge,
34        xx_large = Size::XXLarge,
35        smaller = Size::Smaller,
36        larger = Size::Larger,
37    })]
38    pub size: Option<Size>,
39    #[rich(write(rename = style), write(option, rename = try_style), value_fns = {
40        normal_style = Style::Normal,
41        italic = Style::Italic,
42        oblique = Style::Oblique,
43    })]
44    pub style: Option<Style>,
45    #[rich(write(rename = variant), write(option, rename = try_variant), value_fns = {
46        normal_variant = Variant::Normal,
47        small_caps = Variant::SmallCaps,
48    })]
49    pub variant: Option<Variant>,
50    #[rich(write(rename = weight), write(option, rename = try_weight), value_fns = {
51        normal_weight = Weight::Normal,
52        bold = Weight::Bold,
53        bolder = Weight::Bolder,
54        lighter = Weight::Lighter,
55        weight_100 = Weight::L100,
56        weight_200 = Weight::L200,
57        weight_300 = Weight::L300,
58        weight_400 = Weight::L400,
59        weight_500 = Weight::L500,
60        weight_600 = Weight::L600,
61        weight_700 = Weight::L700,
62        weight_800 = Weight::L800,
63        weight_900 = Weight::L900,
64    })]
65    pub weight: Option<Weight>,
66}
67
68impl<T> From<T> for Font
69where
70    T: Into<Size>,
71{
72    fn from(source: T) -> Self {
73        Font::default().size(source)
74    }
75}
76
77impl StyleUpdater for Font {
78    fn update_style(self, style: crate::Style) -> crate::Style {
79        style
80            .try_insert("font-family", self.family.clone())
81            .try_insert("font-size", self.size)
82            .try_insert("font-style", self.style)
83            .try_insert("font-variant", self.variant)
84            .try_insert("font-weight", self.weight)
85    }
86}
87
88#[derive(Clone, Debug, PartialEq, Display, From)]
89pub enum Family {
90    #[display(fmt = "{}", "_0.join(\" \")")]
91    Family(Vec<Cow<'static, str>>),
92    #[display(fmt = "initial")]
93    Initial,
94    #[display(fmt = "inherit")]
95    Inherit,
96}
97
98impl From<Cow<'static, str>> for Family {
99    fn from(source: Cow<'static, str>) -> Self {
100        Family::Family(vec![source])
101    }
102}
103
104impl From<String> for Family {
105    fn from(source: String) -> Self {
106        Family::Family(vec![source.into()])
107    }
108}
109
110impl From<&'static str> for Family {
111    fn from(source: &'static str) -> Self {
112        Family::Family(vec![source.into()])
113    }
114}
115
116impl From<Vec<String>> for Family {
117    fn from(source: Vec<String>) -> Self {
118        Family::Family(source.into_iter().map(Into::into).collect())
119    }
120}
121
122impl From<Vec<&'static str>> for Family {
123    fn from(source: Vec<&'static str>) -> Self {
124        Family::Family(source.into_iter().map(Into::into).collect())
125    }
126}
127
128#[derive(Clone, Debug, PartialEq, Display, From)]
129pub enum Size {
130    #[display(fmt = "medium")]
131    Medium,
132    #[display(fmt = "xx-small")]
133    XXSmall,
134    #[display(fmt = "x-small")]
135    XSmall,
136    #[display(fmt = "small")]
137    Small,
138    #[display(fmt = "large")]
139    Large,
140    #[display(fmt = "x-large")]
141    XLarge,
142    #[display(fmt = "xx-large")]
143    XXLarge,
144    #[display(fmt = "smaller")]
145    Smaller,
146    #[display(fmt = "larger")]
147    Larger,
148    Length(Length),
149    Percent(Percent),
150    #[display(fmt = "initial")]
151    Initial,
152    #[display(fmt = "inherit")]
153    Inherit,
154}
155
156#[derive(Clone, Copy, Debug, PartialEq, Display, From)]
157pub enum Style {
158    #[display(fmt = "normal")]
159    Normal,
160    #[display(fmt = "italic")]
161    Italic,
162    #[display(fmt = "oblique")]
163    Oblique,
164    #[display(fmt = "initial")]
165    Initial,
166    #[display(fmt = "inherit")]
167    Inherit,
168}
169
170#[derive(Clone, Copy, Debug, PartialEq, Display, From)]
171pub enum Variant {
172    #[display(fmt = "normal")]
173    Normal,
174    #[display(fmt = "small-caps")]
175    SmallCaps,
176    #[display(fmt = "initial")]
177    Initial,
178    #[display(fmt = "inherit")]
179    Inherit,
180}
181
182#[derive(Clone, Copy, Debug, PartialEq, Display, From)]
183pub enum Weight {
184    #[display(fmt = "normal")]
185    Normal,
186    #[display(fmt = "bold")]
187    Bold,
188    #[display(fmt = "bolder")]
189    Bolder,
190    #[display(fmt = "lighter")]
191    Lighter,
192    #[display(fmt = "100")]
193    L100,
194    #[display(fmt = "200")]
195    L200,
196    #[display(fmt = "300")]
197    L300,
198    #[display(fmt = "400")]
199    L400,
200    #[display(fmt = "500")]
201    L500,
202    #[display(fmt = "600")]
203    L600,
204    #[display(fmt = "700")]
205    L700,
206    #[display(fmt = "800")]
207    L800,
208    #[display(fmt = "900")]
209    L900,
210    #[display(fmt = "initial")]
211    Initial,
212    #[display(fmt = "inherit")]
213    Inherit,
214}