/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
usecrate::derives::*; usecrate::parser::{Parse, ParserContext}; usecrate::values::computed::resolution::Resolution as ComputedResolution; usecrate::values::computed::{Context, ToComputedValue}; usecrate::values::specified::calc::{CalcNode, CalcNumeric, Leaf}; usecrate::values::tagged_numeric::{NumericUnion, Unpacked}; usecrate::values::CSSFloat; use cssparser::{match_ignore_ascii_case, Parser, Token}; use std::fmt::{self, Write}; use style_traits::{CssWriter, ParseError, SpecifiedValueInfo, StyleParseErrorKind, ToCss};
/// The unit of a `<resolution>` value. #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem)] #[repr(u8)] pubenum ResolutionUnit { /// Dots per inch.
Dpi, /// An alias unit for dots per pixel.
X, /// Dots per pixel.
Dppx, /// Dots per centimeter.
Dpcm,
}
impl ResolutionUnit { /// Returns the resolution unit for the given string. #[inline] pubfn from_str(unit: &str) -> Result<Self, ()> {
Ok(match_ignore_ascii_case! { &unit, "dpi" => Self::Dpi, "dppx" => Self::Dppx, "dpcm" => Self::Dpcm, "x" => Self::X,
_ => return Err(())
})
}
/// Returns this unit as a string. #[inline] pubfn as_str(self) -> &'static str { matchself { Self::Dpi => "dpi", Self::X => "x", Self::Dppx => "dppx", Self::Dpcm => "dpcm",
}
}
}
impl NoCalcResolution { /// Creates a resolution with the given unit and value. #[inline] pubfn new(unit: ResolutionUnit, value: CSSFloat) -> Self { Self { unit, value }
}
/// Returns a resolution value from dppx units. #[inline] pubfn from_dppx(value: CSSFloat) -> Self { Self::new(ResolutionUnit::Dppx, value)
}
/// Returns a resolution value from x units. #[inline] pubfn from_x(value: CSSFloat) -> Self { Self::new(ResolutionUnit::X, value)
}
/// Convert this resolution value to dppx units. pubfn dppx(&self) -> CSSFloat { matchself.unit {
ResolutionUnit::X | ResolutionUnit::Dppx => self.value,
_ => self.dpi() / 96.0,
}
}
/// Returns the unit of the resolution. #[inline] pubfn resolution_unit(&self) -> ResolutionUnit { self.unit
}
/// Parse a resolution given a value and unit. pubfn parse_dimension(value: CSSFloat, unit: &str) -> Result<Self, ()> { let unit = ResolutionUnit::from_str(unit)?;
Ok(Self::new(unit, value))
}
}
/// A specified resolution value, either a plain value or a `calc()` expression. /// /// https://drafts.csswg.org/css-values/#resolution-value #[derive(Clone, Debug, MallocSizeOf, PartialEq, ToShmem)] pubstruct Resolution(NumericUnion<ResolutionUnit, f32, CalcNumeric>);
impl Resolution { /// Creates a resolution from a non-calc `NoCalcResolution`. #[inline] pubfn new(resolution: NoCalcResolution) -> Self { Self(NumericUnion::inline(resolution.unit, resolution.value))
}
/// Creates a resolution from a `calc()` expression. #[inline] pubfn new_calc(calc: Box<CalcNumeric>) -> Self { Self(NumericUnion::boxed(calc))
}
/// Returns a resolution value from dppx units. #[inline] pubfn from_dppx(value: CSSFloat) -> Self { Self::new(NoCalcResolution::from_dppx(value))
}
/// Returns a resolution value from x units. #[inline] pubfn from_x(value: CSSFloat) -> Self { Self::new(NoCalcResolution::from_x(value))
}
/// Returns true if this is a `calc()` expression. #[inline] pubfn is_calc(&self) -> bool { self.0.is_boxed()
}
/// Parse a resolution given a value and unit. pubfn parse_dimension(value: CSSFloat, unit: &str) -> Result<Self, ()> {
NoCalcResolution::parse_dimension(value, unit).map(Self::new)
}
}
impl ToComputedValue for Resolution { type ComputedValue = ComputedResolution;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.