tailwind_css_fixes/modules/typography/font/font_smoothing/
mod.rs

1use super::*;
2
3#[derive(Debug, Clone)]
4enum FontSmoothing {
5    Antialias,
6    Subpixel,
7    Standard(String),
8    Length(LengthUnit),
9}
10
11#[doc=include_str!("readme.md")]
12#[derive(Debug, Clone)]
13pub struct TailwindFontSmoothing {
14    kind: FontSmoothing,
15}
16
17impl<T> From<T> for TailwindFontSmoothing
18where
19    T: Into<String>,
20{
21    fn from(kind: T) -> Self {
22        Self { kind: FontSmoothing::Standard(kind.into()) }
23    }
24}
25
26impl Display for TailwindFontSmoothing {
27    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28        match &self.kind {
29            FontSmoothing::Antialias => write!(f, "antialiased"),
30            FontSmoothing::Subpixel => write!(f, "subpixel-antialiased"),
31            FontSmoothing::Standard(s) => write!(f, "font-smoothing-{}", s),
32            FontSmoothing::Length(s) => write!(f, "font-smoothing-[{}]", s),
33        }
34    }
35}
36
37impl TailwindInstance for TailwindFontSmoothing {
38    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
39        match &self.kind {
40            FontSmoothing::Antialias => css_attributes! {
41                "-webkit-font-smoothing" => "antialiased",
42                "-moz-osx-font-smoothing" => "grayscale",
43            },
44            FontSmoothing::Subpixel => css_attributes! {
45                "-webkit-font-smoothing" => "auto",
46                "-moz-osx-font-smoothing" => "auto",
47            },
48            FontSmoothing::Standard(s) => css_attributes! {
49                "font-smooth" => s,
50            },
51            FontSmoothing::Length(s) => css_attributes! {
52                "font-smooth" => s.get_properties(),
53            },
54        }
55    }
56}
57
58impl TailwindFontSmoothing {
59    /// https://tailwindcss.com/docs/font-smoothing
60    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
61        let kind = match pattern {
62            ["antialiased"] => FontSmoothing::Antialias,
63            ["subpixel", "antialiased"] | ["subpixel"] => FontSmoothing::Subpixel,
64            [n] if Self::check_valid(n) => FontSmoothing::Standard(n.to_string()),
65            [n] => {
66                let l = TailwindArbitrary::from(*n).as_length_or_fraction()?;
67                FontSmoothing::Length(l)
68            },
69            [] => FontSmoothing::Length(arbitrary.as_length_or_fraction()?),
70            _ => return syntax_error!("Unknown font-smoothing instructions: {}", pattern.join("-")),
71        };
72        Ok(Self { kind })
73    }
74    /// https://developer.mozilla.org/en-US/docs/Web/CSS/font-smooth#syntax
75    pub fn check_valid(mode: &str) -> bool {
76        let set = BTreeSet::from_iter(vec!["auto", "never", "always", "inherit", "initial", "revert", "unset"]);
77        set.contains(mode)
78    }
79}