Skip to content

Commit

Permalink
style: Remove StyleFontSize enum from nsStyleConsts.h.
Browse files Browse the repository at this point in the history
  • Loading branch information
lokalmatador authored and emilio committed Jun 18, 2020
1 parent 60f4fed commit 5af0d7c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 79 deletions.
7 changes: 4 additions & 3 deletions components/style/properties/cascade.rs
Expand Up @@ -854,12 +854,13 @@ impl<'a, 'b: 'a> Cascade<'a, 'b> {

let new_size = {
let font = self.context.builder.get_font();
let new_size = match font.clone_font_size().keyword_info {
Some(info) => {
let info = font.clone_font_size().keyword_info;
let new_size = match info.kw {
specified::FontSizeKeyword::None => return,
_ => {
self.context.for_non_inherited_property = None;
specified::FontSize::Keyword(info).to_computed_value(self.context)
}
None => return,
};

if font.gecko().mScriptUnconstrainedSize == Au::from(new_size.size()).0 {
Expand Down
52 changes: 8 additions & 44 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -942,62 +942,26 @@ fn static_assert() {
}

pub fn set_font_size(&mut self, v: FontSize) {
use crate::values::specified::font::KeywordSize;

let size = Au::from(v.size());
self.gecko.mScriptUnconstrainedSize = size.0;

// These two may be changed from Cascade::fixup_font_stuff.
self.gecko.mSize = size.0;
self.gecko.mFont.size = size.0;

if let Some(info) = v.keyword_info {
self.gecko.mFontSizeKeyword = match info.kw {
KeywordSize::XXSmall => structs::StyleFontSize::Xxsmall,
KeywordSize::XSmall => structs::StyleFontSize::Xsmall,
KeywordSize::Small => structs::StyleFontSize::Small,
KeywordSize::Medium => structs::StyleFontSize::Medium,
KeywordSize::Large => structs::StyleFontSize::Large,
KeywordSize::XLarge => structs::StyleFontSize::Xxlarge,
KeywordSize::XXLarge => structs::StyleFontSize::Xxlarge,
KeywordSize::XXXLarge => structs::StyleFontSize::Xxxlarge,
};
self.gecko.mFontSizeFactor = info.factor;
self.gecko.mFontSizeOffset = info.offset.to_i32_au();
} else {
self.gecko.mFontSizeKeyword = structs::StyleFontSize::NoKeyword;
self.gecko.mFontSizeFactor = 1.;
self.gecko.mFontSizeOffset = 0;
}
self.gecko.mFontSizeKeyword = v.keyword_info.kw;
self.gecko.mFontSizeFactor = v.keyword_info.factor;
self.gecko.mFontSizeOffset = v.keyword_info.offset.to_i32_au();
}

pub fn clone_font_size(&self) -> FontSize {
use crate::values::specified::font::{KeywordInfo, KeywordSize};
let size = Au(self.gecko.mSize).into();
let kw = match self.gecko.mFontSizeKeyword {
structs::StyleFontSize::Xxsmall => KeywordSize::XXSmall,
structs::StyleFontSize::Xsmall => KeywordSize::XSmall,
structs::StyleFontSize::Small => KeywordSize::Small,
structs::StyleFontSize::Medium => KeywordSize::Medium,
structs::StyleFontSize::Large => KeywordSize::Large,
structs::StyleFontSize::Xlarge => KeywordSize::XLarge,
structs::StyleFontSize::Xxlarge => KeywordSize::XXLarge,
structs::StyleFontSize::Xxxlarge => KeywordSize::XXXLarge,
structs::StyleFontSize::NoKeyword => {
return FontSize {
size,
keyword_info: None,
}
}
_ => unreachable!("mFontSizeKeyword should be an absolute keyword or NO_KEYWORD")
};
use crate::values::specified::font::KeywordInfo;
FontSize {
size,
keyword_info: Some(KeywordInfo {
kw,
size: Au(self.gecko.mSize).into(),
keyword_info: KeywordInfo {
kw: self.gecko.mFontSizeKeyword,
factor: self.gecko.mFontSizeFactor,
offset: Au(self.gecko.mFontSizeOffset).into()
})
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion components/style/properties/longhands/font.mako.rs
Expand Up @@ -362,6 +362,7 @@ ${helpers.predefined_type(
use crate::gecko_bindings::structs::{LookAndFeel_FontID, nsFont};
use std::mem;
use crate::values::computed::Percentage;
use crate::values::specified::font::KeywordInfo;
use crate::values::computed::font::{FontFamily, FontSize, FontStretch, FontStyle, FontFamilyList};
use crate::values::generics::NonNegative;

Expand Down Expand Up @@ -397,7 +398,7 @@ ${helpers.predefined_type(
},
font_size: FontSize {
size: NonNegative(cx.maybe_zoom_text(Au(system.size).into())),
keyword_info: None
keyword_info: KeywordInfo::none()
},
font_weight,
font_stretch,
Expand Down
6 changes: 3 additions & 3 deletions components/style/values/computed/font.rs
Expand Up @@ -101,7 +101,7 @@ pub struct FontSize {
pub size: NonNegativeLength,
/// If derived from a keyword, the keyword and additional transformations applied to it
#[css(skip)]
pub keyword_info: Option<KeywordInfo>,
pub keyword_info: KeywordInfo,
}

impl FontWeight {
Expand Down Expand Up @@ -170,7 +170,7 @@ impl FontSize {
pub fn medium() -> Self {
Self {
size: NonNegative(Length::new(specified::FONT_MEDIUM_PX as CSSFloat)),
keyword_info: Some(KeywordInfo::medium()),
keyword_info: KeywordInfo::medium(),
}
}
}
Expand All @@ -187,7 +187,7 @@ impl ToAnimatedValue for FontSize {
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
FontSize {
size: NonNegative(animated.clamp_to_non_negative()),
keyword_info: None,
keyword_info: KeywordInfo::none(),
}
}
}
Expand Down
65 changes: 38 additions & 27 deletions components/style/values/specified/font.rs
Expand Up @@ -511,19 +511,21 @@ pub enum KeywordSize {
XXLarge,
#[css(keyword = "xxx-large")]
XXXLarge,
#[css(skip)]
None,
}

impl KeywordSize {
impl FontSizeKeyword {
/// Convert to an HTML <font size> value
#[inline]
pub fn html_size(self) -> u8 {
self as u8
}
}

impl Default for KeywordSize {
impl Default for FontSizeKeyword {
fn default() -> Self {
KeywordSize::Medium
FontSizeKeyword::Medium
}
}

Expand All @@ -546,7 +548,7 @@ impl Default for KeywordSize {
/// Additional information for keyword-derived font sizes.
pub struct KeywordInfo {
/// The keyword used
pub kw: KeywordSize,
pub kw: FontSizeKeyword,
/// A factor to be multiplied by the computed size of the keyword
#[css(skip)]
pub factor: f32,
Expand All @@ -559,10 +561,15 @@ pub struct KeywordInfo {
impl KeywordInfo {
/// KeywordInfo value for font-size: medium
pub fn medium() -> Self {
Self::new(KeywordSize::Medium)
Self::new(FontSizeKeyword::Medium)
}

/// KeywordInfo value for font-size: none
pub fn none() -> Self {
Self::new(FontSizeKeyword::None)
}

fn new(kw: KeywordSize) -> Self {
fn new(kw: FontSizeKeyword) -> Self {
KeywordInfo {
kw,
factor: 1.,
Expand All @@ -573,13 +580,17 @@ impl KeywordInfo {
/// Computes the final size for this font-size keyword, accounting for
/// text-zoom.
fn to_computed_value(&self, context: &Context) -> CSSPixelLength {
debug_assert_ne!(self.kw, FontSizeKeyword::None);
let base = context.maybe_zoom_text(self.kw.to_length(context).0);
base * self.factor + context.maybe_zoom_text(self.offset)
}

/// Given a parent keyword info (self), apply an additional factor/offset to
/// it.
fn compose(self, factor: f32) -> Self {
if self.kw == FontSizeKeyword::None {
return self;
}
KeywordInfo {
kw: self.kw,
factor: self.factor * factor,
Expand All @@ -590,7 +601,7 @@ impl KeywordInfo {

impl SpecifiedValueInfo for KeywordInfo {
fn collect_completion_keywords(f: KeywordsCollectFn) {
<KeywordSize as SpecifiedValueInfo>::collect_completion_keywords(f);
<FontSizeKeyword as SpecifiedValueInfo>::collect_completion_keywords(f);
}
}

Expand Down Expand Up @@ -766,21 +777,21 @@ const LARGER_FONT_SIZE_RATIO: f32 = 1.2;
/// The default font size.
pub const FONT_MEDIUM_PX: i32 = 16;

impl KeywordSize {
impl FontSizeKeyword {
#[inline]
#[cfg(feature = "servo")]
fn to_length(&self, _: &Context) -> NonNegativeLength {
let medium = Length::new(FONT_MEDIUM_PX as f32);
// https://drafts.csswg.org/css-fonts-3/#font-size-prop
NonNegative(match *self {
KeywordSize::XXSmall => medium * 3.0 / 5.0,
KeywordSize::XSmall => medium * 3.0 / 4.0,
KeywordSize::Small => medium * 8.0 / 9.0,
KeywordSize::Medium => medium,
KeywordSize::Large => medium * 6.0 / 5.0,
KeywordSize::XLarge => medium * 3.0 / 2.0,
KeywordSize::XXLarge => medium * 2.0,
KeywordSize::XXXLarge => medium * 3.0,
FontSizeKeyword::XXSmall => medium * 3.0 / 5.0,
FontSizeKeyword::XSmall => medium * 3.0 / 4.0,
FontSizeKeyword::Small => medium * 8.0 / 9.0,
FontSizeKeyword::Medium => medium,
FontSizeKeyword::Large => medium * 6.0 / 5.0,
FontSizeKeyword::XLarge => medium * 3.0 / 2.0,
FontSizeKeyword::XXLarge => medium * 2.0,
FontSizeKeyword::XXXLarge => medium * 3.0,
})
}

Expand Down Expand Up @@ -861,14 +872,14 @@ impl FontSize {
pub fn from_html_size(size: u8) -> Self {
FontSize::Keyword(KeywordInfo::new(match size {
// If value is less than 1, let it be 1.
0 | 1 => KeywordSize::XSmall,
2 => KeywordSize::Small,
3 => KeywordSize::Medium,
4 => KeywordSize::Large,
5 => KeywordSize::XLarge,
6 => KeywordSize::XXLarge,
0 | 1 => FontSizeKeyword::XSmall,
2 => FontSizeKeyword::Small,
3 => FontSizeKeyword::Medium,
4 => FontSizeKeyword::Large,
5 => FontSizeKeyword::XLarge,
6 => FontSizeKeyword::XXLarge,
// If value is greater than 7, let it be 7.
_ => KeywordSize::XXXLarge,
_ => FontSizeKeyword::XXXLarge,
}))
}

Expand All @@ -886,9 +897,9 @@ impl FontSize {
.get_parent_font()
.clone_font_size()
.keyword_info
.map(|i| i.compose(factor))
.compose(factor)
};
let mut info = None;
let mut info = KeywordInfo::none();
let size = match *self {
FontSize::Length(LengthPercentage::Length(NoCalcLength::FontRelative(value))) => {
if let FontRelativeLength::Em(em) = value {
Expand Down Expand Up @@ -917,7 +928,7 @@ impl FontSize {
},
FontSize::Keyword(i) => {
// As a specified keyword, this is keyword derived
info = Some(i);
info = i;
i.to_computed_value(context).clamp_to_non_negative()
},
FontSize::Smaller => {
Expand Down Expand Up @@ -991,7 +1002,7 @@ impl FontSize {
return Ok(FontSize::Length(lp));
}

if let Ok(kw) = input.try(KeywordSize::parse) {
if let Ok(kw) = input.try(FontSizeKeyword::parse) {
return Ok(FontSize::Keyword(KeywordInfo::new(kw)));
}

Expand Down
2 changes: 1 addition & 1 deletion components/style/values/specified/mod.rs
Expand Up @@ -50,7 +50,7 @@ pub use self::effects::{BoxShadow, Filter, SimpleShadow};
pub use self::flex::FlexBasis;
pub use self::font::{FontFamily, FontLanguageOverride, FontStyle};
pub use self::font::{FontFeatureSettings, FontVariantLigatures, FontVariantNumeric};
pub use self::font::{FontSize, FontSizeAdjust, FontStretch, FontSynthesis};
pub use self::font::{FontSize, FontSizeAdjust, FontStretch, FontSynthesis, FontSizeKeyword};
pub use self::font::{FontVariantAlternates, FontWeight};
pub use self::font::{FontVariantEastAsian, FontVariationSettings};
pub use self::font::{MozScriptLevel, MozScriptMinSize, MozScriptSizeMultiplier, XLang, XTextZoom};
Expand Down

0 comments on commit 5af0d7c

Please sign in to comment.