logo
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use super::*;

#[derive(Debug, Clone)]
enum FontSmoothing {
    Antialias,
    Subpixel,
    Standard(String),
    Length(LengthUnit),
}

#[doc = include_str!("readme.md")]
#[derive(Debug, Clone)]
pub struct TailwindFontSmoothing {
    kind: FontSmoothing,
}

impl<T> From<T> for TailwindFontSmoothing
where
    T: Into<String>,
{
    fn from(kind: T) -> Self {
        Self { kind: FontSmoothing::Standard(kind.into()) }
    }
}

impl Display for TailwindFontSmoothing {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            FontSmoothing::Antialias => write!(f, "antialiased"),
            FontSmoothing::Subpixel => write!(f, "subpixel-antialiased"),
            FontSmoothing::Standard(s) => write!(f, "font-smoothing-{}", s),
            FontSmoothing::Length(s) => write!(f, "font-smoothing-[{}]", s),
        }
    }
}

impl TailwindInstance for TailwindFontSmoothing {
    fn attributes(&self, _: &TailwindBuilder) -> BTreeSet<CssAttribute> {
        match &self.kind {
            FontSmoothing::Antialias => css_attributes! {
                "-webkit-font-smoothing" => "antialiased",
                "-moz-osx-font-smoothing" => "grayscale",
            },
            FontSmoothing::Subpixel => css_attributes! {
                "-webkit-font-smoothing" => "auto",
                "-moz-osx-font-smoothing" => "auto",
            },
            FontSmoothing::Standard(s) => css_attributes! {
                "font-smooth" => s,
            },
            FontSmoothing::Length(s) => css_attributes! {
                "font-smooth" => s.get_properties(),
            },
        }
    }
}

impl TailwindFontSmoothing {
    /// https://tailwindcss.com/docs/font-smoothing
    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
        let kind = match pattern {
            ["antialiased"] => FontSmoothing::Antialias,
            ["subpixel", "antialiased"] | ["subpixel"] => FontSmoothing::Subpixel,
            [n] if Self::check_valid(n) => FontSmoothing::Standard(n.to_string()),
            [n] => {
                let l = TailwindArbitrary::from(*n).as_length()?;
                FontSmoothing::Length(l)
            },
            [] => {
                debug_assert!(arbitrary.is_some());
                FontSmoothing::Length(arbitrary.as_length()?)
            },
            _ => return syntax_error!("Unknown font-smoothing instructions: {}", pattern.join("-")),
        };
        Ok(Self { kind })
    }
    /// https://developer.mozilla.org/en-US/docs/Web/CSS/font-smooth#syntax
    pub fn check_valid(mode: &str) -> bool {
        let set = BTreeSet::from_iter(vec!["auto", "never", "always", "inherit", "initial", "revert", "unset"]);
        set.contains(mode)
    }
}