style: Convert the font-stretch descriptor to use NonNegativePercentage

Depends on D109285

Differential Revision: https://phabricator.services.mozilla.com/D109286
This commit is contained in:
Oriol Brufau 2023-05-16 10:56:02 +02:00
parent 4d4fb4b414
commit 5ef832b778
3 changed files with 7 additions and 28 deletions

View file

@ -194,7 +194,7 @@ impl FontStretchRange {
fn compute_stretch(s: &FontStretch) -> f32 { fn compute_stretch(s: &FontStretch) -> f32 {
match *s { match *s {
FontStretch::Keyword(ref kw) => kw.compute().0, FontStretch::Keyword(ref kw) => kw.compute().0,
FontStretch::Stretch(ref p) => p.get(), FontStretch::Stretch(ref p) => p.0.get(),
FontStretch::System(..) => unreachable!(), FontStretch::System(..) => unreachable!(),
} }
} }

View file

@ -197,7 +197,7 @@
let font_stretch = match *self.font_stretch { let font_stretch = match *self.font_stretch {
FontStretch::Keyword(kw) => kw, FontStretch::Keyword(kw) => kw,
FontStretch::Stretch(percentage) => { FontStretch::Stretch(percentage) => {
match FontStretchKeyword::from_percentage(percentage.get()) { match FontStretchKeyword::from_percentage(percentage.0.get()) {
Some(kw) => kw, Some(kw) => kw,
None => return Ok(()), None => return Ok(()),
} }

View file

@ -19,7 +19,7 @@ use crate::values::generics::font::{self as generics, FeatureTagValue, FontSetti
use crate::values::generics::NonNegative; use crate::values::generics::NonNegative;
use crate::values::specified::length::{FontBaseSize, PX_PER_PT}; use crate::values::specified::length::{FontBaseSize, PX_PER_PT};
use crate::values::specified::{AllowQuirks, Angle, Integer, LengthPercentage}; use crate::values::specified::{AllowQuirks, Angle, Integer, LengthPercentage};
use crate::values::specified::{NoCalcLength, NonNegativeNumber, Number, Percentage}; use crate::values::specified::{NoCalcLength, NonNegativeNumber, Number, NonNegativePercentage};
use crate::values::CustomIdent; use crate::values::CustomIdent;
use crate::Atom; use crate::Atom;
use cssparser::{Parser, Token}; use cssparser::{Parser, Token};
@ -359,13 +359,11 @@ impl ToComputedValue for FontStyle {
/// A value for the `font-stretch` property. /// A value for the `font-stretch` property.
/// ///
/// https://drafts.csswg.org/css-fonts-4/#font-stretch-prop /// https://drafts.csswg.org/css-fonts-4/#font-stretch-prop
///
/// TODO(emilio): We could derive Parse if we had NonNegativePercentage.
#[allow(missing_docs)] #[allow(missing_docs)]
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem)] #[derive(Clone, Copy, Debug, MallocSizeOf, Parse, PartialEq, SpecifiedValueInfo, ToCss, ToShmem)]
#[repr(u8)] #[repr(u8)]
pub enum FontStretch { pub enum FontStretch {
Stretch(Percentage), Stretch(NonNegativePercentage),
Keyword(FontStretchKeyword), Keyword(FontStretchKeyword),
#[css(skip)] #[css(skip)]
System(SystemFont), System(SystemFont),
@ -452,32 +450,13 @@ impl FontStretch {
system_font_methods!(FontStretch, font_stretch); system_font_methods!(FontStretch, font_stretch);
} }
impl Parse for FontStretch {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
// From https://drafts.csswg.org/css-fonts-4/#font-stretch-prop:
//
// Values less than 0% are not allowed and are treated as parse
// errors.
if let Ok(percentage) =
input.try_parse(|input| Percentage::parse_non_negative(context, input))
{
return Ok(FontStretch::Stretch(percentage));
}
Ok(FontStretch::Keyword(FontStretchKeyword::parse(input)?))
}
}
impl ToComputedValue for FontStretch { impl ToComputedValue for FontStretch {
type ComputedValue = computed::FontStretch; type ComputedValue = computed::FontStretch;
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
match *self { match *self {
FontStretch::Stretch(ref percentage) => { FontStretch::Stretch(ref percentage) => {
computed::FontStretch(NonNegative(percentage.to_computed_value(context))) computed::FontStretch(percentage.to_computed_value(context))
}, },
FontStretch::Keyword(ref kw) => computed::FontStretch(NonNegative(kw.compute())), FontStretch::Keyword(ref kw) => computed::FontStretch(NonNegative(kw.compute())),
FontStretch::System(_) => self.compute_system(context), FontStretch::System(_) => self.compute_system(context),
@ -485,7 +464,7 @@ impl ToComputedValue for FontStretch {
} }
fn from_computed_value(computed: &Self::ComputedValue) -> Self { fn from_computed_value(computed: &Self::ComputedValue) -> Self {
FontStretch::Stretch(Percentage::from_computed_value(&(computed.0).0)) FontStretch::Stretch(NonNegativePercentage::from_computed_value(&NonNegative((computed.0).0)))
} }
} }