style: Move font-size-adjust outside of mako

This commit is contained in:
CYBAI 2017-11-08 23:08:00 +08:00
parent cbf2ac35f7
commit e73f2377cf
5 changed files with 135 additions and 121 deletions

View file

@ -7,7 +7,8 @@
use app_units::Au;
use std::fmt;
use style_traits::ToCss;
use values::animated::ToAnimatedValue;
use values::CSSFloat;
use values::animated::{ToAnimatedValue, ToAnimatedZero};
use values::computed::{Context, NonNegativeLength, ToComputedValue};
use values::specified::font as specified;
use values::specified::length::{FontBaseSize, NoCalcLength};
@ -212,6 +213,58 @@ impl ToAnimatedValue for FontSize {
}
}
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf, PartialEq, ToCss)]
/// Preserve the readability of text when font fallback occurs
pub enum FontSizeAdjust {
#[animation(error)]
/// None variant
None,
/// Number variant
Number(CSSFloat),
}
impl FontSizeAdjust {
#[inline]
/// Default value of font-size-adjust
pub fn none() -> Self {
FontSizeAdjust::None
}
/// Get font-size-adjust with float number
pub fn from_gecko_adjust(gecko: f32) -> Self {
if gecko == -1.0 {
FontSizeAdjust::None
} else {
FontSizeAdjust::Number(gecko)
}
}
}
impl ToAnimatedZero for FontSizeAdjust {
#[inline]
// FIXME(emilio): why?
fn to_animated_zero(&self) -> Result<Self, ()> {
Err(())
}
}
impl ToAnimatedValue for FontSizeAdjust {
type AnimatedValue = Self;
#[inline]
fn to_animated_value(self) -> Self {
self
}
#[inline]
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
match animated {
FontSizeAdjust::Number(number) => FontSizeAdjust::Number(number.max(0.)),
_ => animated
}
}
}
impl ToComputedValue for specified::MozScriptMinSize {
type ComputedValue = MozScriptMinSize;

View file

@ -36,7 +36,7 @@ pub use self::angle::Angle;
pub use self::background::{BackgroundSize, BackgroundRepeat};
pub use self::border::{BorderImageSlice, BorderImageWidth, BorderImageSideWidth};
pub use self::border::{BorderRadius, BorderCornerRadius, BorderSpacing};
pub use self::font::{FontSize, FontWeight, MozScriptLevel, MozScriptMinSize, XTextZoom};
pub use self::font::{FontSize, FontSizeAdjust, FontWeight, MozScriptLevel, MozScriptMinSize, XTextZoom};
pub use self::box_::{AnimationIterationCount, AnimationName, ScrollSnapType, VerticalAlign};
pub use self::color::{Color, ColorPropertyValue, RGBAColor};
pub use self::effects::{BoxShadow, Filter, SimpleShadow};

View file

@ -13,7 +13,7 @@ use properties::longhands::system_font::SystemFont;
use std::fmt;
use style_traits::{ToCss, StyleParseErrorKind, ParseError};
use values::computed::{font as computed, Context, Length, NonNegativeLength, ToComputedValue};
use values::specified::{AllowQuirks, LengthOrPercentage, NoCalcLength};
use values::specified::{AllowQuirks, LengthOrPercentage, NoCalcLength, Number};
use values::specified::length::{AU_PER_PT, AU_PER_PX, FontBaseSize};
const DEFAULT_SCRIPT_MIN_SIZE_PT: u32 = 8;
@ -151,6 +151,76 @@ impl From<LengthOrPercentage> for FontSize {
}
}
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToCss)]
/// Preserve the readability of text when font fallback occurs
pub enum FontSizeAdjust {
/// None variant
None,
/// Number variant
Number(Number),
/// system font
System(SystemFont),
}
impl FontSizeAdjust {
#[inline]
/// Default value of font-size-adjust
pub fn none() -> Self {
FontSizeAdjust::None
}
/// Get font-size-adjust with SystemFont
pub fn system_font(f: SystemFont) -> Self {
FontSizeAdjust::System(f)
}
/// Get SystemFont variant
pub fn get_system(&self) -> Option<SystemFont> {
if let FontSizeAdjust::System(s) = *self {
Some(s)
} else {
None
}
}
}
impl ToComputedValue for FontSizeAdjust {
type ComputedValue = computed::FontSizeAdjust;
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
match *self {
FontSizeAdjust::None => computed::FontSizeAdjust::None,
FontSizeAdjust::Number(ref n) => computed::FontSizeAdjust::Number(n.to_computed_value(context)),
FontSizeAdjust::System(_) => {
#[cfg(feature = "gecko")] {
context.cached_system_font.as_ref().unwrap().font_size_adjust
}
#[cfg(feature = "servo")] {
unreachable!()
}
}
}
}
fn from_computed_value(computed: &computed::FontSizeAdjust) -> Self {
match *computed {
computed::FontSizeAdjust::None => FontSizeAdjust::None,
computed::FontSizeAdjust::Number(ref v) => FontSizeAdjust::Number(Number::from_computed_value(v)),
}
}
}
impl Parse for FontSizeAdjust {
/// none | <number>
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<FontSizeAdjust, ParseError<'i>> {
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(FontSizeAdjust::None);
}
Ok(FontSizeAdjust::Number(Number::parse_non_negative(context, input)?))
}
}
/// CSS font keywords
#[derive(Animate, ComputeSquaredDistance, MallocSizeOf, ToAnimatedValue, ToAnimatedZero)]
#[derive(Clone, Copy, Debug, PartialEq)]

View file

@ -30,7 +30,7 @@ pub use self::align::{AlignItems, AlignJustifyContent, AlignJustifySelf, Justify
pub use self::background::{BackgroundRepeat, BackgroundSize};
pub use self::border::{BorderCornerRadius, BorderImageSlice, BorderImageWidth};
pub use self::border::{BorderImageSideWidth, BorderRadius, BorderSideWidth, BorderSpacing};
pub use self::font::{FontSize, FontWeight, MozScriptLevel, MozScriptMinSize, XTextZoom};
pub use self::font::{FontSize, FontSizeAdjust, FontWeight, MozScriptLevel, MozScriptMinSize, XTextZoom};
pub use self::box_::{AnimationIterationCount, AnimationName, ScrollSnapType, VerticalAlign};
pub use self::color::{Color, ColorPropertyValue, RGBAColor};
pub use self::effects::{BoxShadow, Filter, SimpleShadow};