lightningcss/values/
resolution.rs1use super::length::serialize_dimension;
4use super::number::CSSNumber;
5use crate::compat::Feature;
6use crate::error::{ParserError, PrinterError};
7use crate::printer::Printer;
8use crate::traits::{Parse, ToCss};
9#[cfg(feature = "visitor")]
10use crate::visitor::Visit;
11use cssparser::*;
12
13#[derive(Debug, Clone, PartialEq)]
15#[cfg_attr(feature = "visitor", derive(Visit))]
16#[cfg_attr(feature = "visitor", visit(visit_resolution, RESOLUTIONS))]
17#[cfg_attr(
18 feature = "serde",
19 derive(serde::Serialize, serde::Deserialize),
20 serde(tag = "type", content = "value", rename_all = "kebab-case")
21)]
22#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
23#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
24pub enum Resolution {
25 Dpi(CSSNumber),
27 Dpcm(CSSNumber),
29 Dppx(CSSNumber),
31}
32
33impl<'i> Parse<'i> for Resolution {
34 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
35 let location = input.current_source_location();
37 match *input.next()? {
38 Token::Dimension { value, ref unit, .. } => {
39 match_ignore_ascii_case! { unit,
40 "dpi" => Ok(Resolution::Dpi(value)),
41 "dpcm" => Ok(Resolution::Dpcm(value)),
42 "dppx" | "x" => Ok(Resolution::Dppx(value)),
43 _ => Err(location.new_unexpected_token_error(Token::Ident(unit.clone())))
44 }
45 }
46 ref t => Err(location.new_unexpected_token_error(t.clone())),
47 }
48 }
49}
50
51impl<'i> TryFrom<&Token<'i>> for Resolution {
52 type Error = ();
53
54 fn try_from(token: &Token) -> Result<Self, Self::Error> {
55 match token {
56 Token::Dimension { value, ref unit, .. } => match_ignore_ascii_case! { unit,
57 "dpi" => Ok(Resolution::Dpi(*value)),
58 "dpcm" => Ok(Resolution::Dpcm(*value)),
59 "dppx" | "x" => Ok(Resolution::Dppx(*value)),
60 _ => Err(()),
61 },
62 _ => Err(()),
63 }
64 }
65}
66
67impl ToCss for Resolution {
68 fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
69 where
70 W: std::fmt::Write,
71 {
72 let (value, unit) = match self {
73 Resolution::Dpi(dpi) => (*dpi, "dpi"),
74 Resolution::Dpcm(dpcm) => (*dpcm, "dpcm"),
75 Resolution::Dppx(dppx) => {
76 if dest.targets.is_compatible(Feature::XResolutionUnit) {
77 (*dppx, "x")
78 } else {
79 (*dppx, "dppx")
80 }
81 }
82 };
83
84 serialize_dimension(value, unit, dest)
85 }
86}
87
88impl std::ops::Add<CSSNumber> for Resolution {
89 type Output = Self;
90
91 fn add(self, other: CSSNumber) -> Resolution {
92 match self {
93 Resolution::Dpi(dpi) => Resolution::Dpi(dpi + other),
94 Resolution::Dpcm(dpcm) => Resolution::Dpcm(dpcm + other),
95 Resolution::Dppx(dppx) => Resolution::Dppx(dppx + other),
96 }
97 }
98}