/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ //! Generic types for text properties. use app_units::Au; use cssparser::Parser; use parser::ParserContext; use style_traits::ParseError; use values::animated::{Animate, Procedure, ToAnimatedZero}; use values::distance::{ComputeSquaredDistance, SquaredDistance}; /// A generic value for the `initial-letter` property. #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToCss)] pub enum InitialLetter { /// `normal` Normal, /// ` ?` Specified(Number, Option), } impl InitialLetter { /// Returns `normal`. #[inline] pub fn normal() -> Self { InitialLetter::Normal } } /// A generic spacing value for the `letter-spacing` and `word-spacing` properties. #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToCss)] pub enum Spacing { /// `normal` Normal, /// `` Value(Value), } impl Spacing { /// Returns `normal`. #[inline] pub fn normal() -> Self { Spacing::Normal } /// Parses. #[inline] pub fn parse_with<'i, 't, F>( context: &ParserContext, input: &mut Parser<'i, 't>, parse: F) -> Result> where F: FnOnce(&ParserContext, &mut Parser<'i, 't>) -> Result> { if input.try(|i| i.expect_ident_matching("normal")).is_ok() { return Ok(Spacing::Normal); } parse(context, input).map(Spacing::Value) } /// Returns the spacing value, if not `normal`. #[inline] pub fn value(&self) -> Option<&Value> { match *self { Spacing::Normal => None, Spacing::Value(ref value) => Some(value), } } } impl Animate for Spacing where Value: Animate + From, { #[inline] fn animate(&self, other: &Self, procedure: Procedure) -> Result { if let (&Spacing::Normal, &Spacing::Normal) = (self, other) { return Ok(Spacing::Normal); } let zero = Value::from(Au(0)); let this = self.value().unwrap_or(&zero); let other = other.value().unwrap_or(&zero); Ok(Spacing::Value(this.animate(other, procedure)?)) } } impl ComputeSquaredDistance for Spacing where V: ComputeSquaredDistance + From, { #[inline] fn compute_squared_distance(&self, other: &Self) -> Result { let zero = V::from(Au(0)); let this = self.value().unwrap_or(&zero); let other = other.value().unwrap_or(&zero); this.compute_squared_distance(other) } } impl ToAnimatedZero for Spacing where V: From, { #[inline] fn to_animated_zero(&self) -> Result { Err(()) } } /// A generic value for the `line-height` property. #[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug)] #[derive(MallocSizeOf, PartialEq, ToAnimatedValue, ToCss)] pub enum LineHeight { /// `normal` Normal, /// `-moz-block-height` #[cfg(feature = "gecko")] MozBlockHeight, /// `` Number(Number), /// `` Length(LengthOrPercentage), } impl ToAnimatedZero for LineHeight { #[inline] fn to_animated_zero(&self) -> Result { Err(()) } } impl LineHeight { /// Returns `normal`. #[inline] pub fn normal() -> Self { LineHeight::Normal } } /// A generic value for the `-moz-tab-size` property. #[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf)] #[derive(PartialEq, ToAnimatedValue, ToAnimatedZero, ToComputedValue, ToCss)] pub enum MozTabSize { /// A number. Number(Number), /// A length. Length(Length), }