From 245d848508a299f4c8eafb45256df808b8d1b04b Mon Sep 17 00:00:00 2001 From: Jonathan Watt Date: Fri, 13 Apr 2018 20:34:37 +0100 Subject: [PATCH] style: Use a user defined type for font weight everywhere. Bug: 1436048 Reviewed-by: emilio --- components/style/gecko/rules.rs | 4 ++-- components/style/gecko_bindings/sugar/ns_css_value.rs | 5 +++++ components/style/properties/gecko.mako.rs | 10 +++++++--- components/style/values/computed/font.rs | 6 ++++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/components/style/gecko/rules.rs b/components/style/gecko/rules.rs index ccf49f82970..2f144279656 100644 --- a/components/style/gecko/rules.rs +++ b/components/style/gecko/rules.rs @@ -25,7 +25,7 @@ impl<'a> ToNsCssValue for &'a FamilyName { impl ToNsCssValue for font_weight::T { fn convert(self, nscssvalue: &mut nsCSSValue) { - nscssvalue.set_integer(self.0 as i32) + nscssvalue.set_font_weight(self.0) } } @@ -34,7 +34,7 @@ impl<'a> ToNsCssValue for &'a FontWeight { match *self { FontWeight::Normal => nscssvalue.set_enum(structs::NS_FONT_WEIGHT_NORMAL as i32), FontWeight::Bold => nscssvalue.set_enum(structs::NS_FONT_WEIGHT_BOLD as i32), - FontWeight::Weight(weight) => nscssvalue.set_integer(weight.0 as i32), + FontWeight::Weight(weight) => nscssvalue.set_font_weight(weight.0), } } } diff --git a/components/style/gecko_bindings/sugar/ns_css_value.rs b/components/style/gecko_bindings/sugar/ns_css_value.rs index 4507e3fbd6b..45693561510 100644 --- a/components/style/gecko_bindings/sugar/ns_css_value.rs +++ b/components/style/gecko_bindings/sugar/ns_css_value.rs @@ -177,6 +177,11 @@ impl nsCSSValue { self.set_string_from_atom_internal(s, nsCSSUnit::eCSSUnit_Local_Font); } + /// Set to a font weight + pub fn set_font_weight(&mut self, w: u16) { + unsafe { bindings::Gecko_CSSValue_SetFontWeight(self, w as f32) } + } + fn set_int_internal(&mut self, value: i32, unit: nsCSSUnit) { unsafe { bindings::Gecko_CSSValue_SetInt(self, value, unit) } } diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 16c75025f41..067d014442d 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -25,6 +25,8 @@ use gecko_bindings::bindings::Gecko_CopyFontFamilyFrom; use gecko_bindings::bindings::Gecko_CopyImageValueFrom; use gecko_bindings::bindings::Gecko_CopyListStyleImageFrom; use gecko_bindings::bindings::Gecko_EnsureImageLayersLength; +use gecko_bindings::bindings::Gecko_FontWeight_SetFloat; +use gecko_bindings::bindings::Gecko_FontWeight_ToFloat; use gecko_bindings::bindings::Gecko_SetCursorArrayLength; use gecko_bindings::bindings::Gecko_SetCursorImageValue; use gecko_bindings::bindings::Gecko_StyleTransition_SetUnsupportedProperty; @@ -2599,13 +2601,15 @@ fn static_assert() { } pub fn set_font_weight(&mut self, v: longhands::font_weight::computed_value::T) { - self.gecko.mFont.weight = v.0; + unsafe { Gecko_FontWeight_SetFloat(&mut self.gecko.mFont.weight, v.0 as f32) }; } ${impl_simple_copy('font_weight', 'mFont.weight')} pub fn clone_font_weight(&self) -> longhands::font_weight::computed_value::T { - debug_assert!(self.gecko.mFont.weight <= ::std::u16::MAX); - longhands::font_weight::computed_value::T(self.gecko.mFont.weight) + let weight: f32 = unsafe { Gecko_FontWeight_ToFloat(self.gecko.mFont.weight) }; + debug_assert!(weight >= 0.0 && + weight <= ::std::u16::MAX as f32); + longhands::font_weight::computed_value::T(weight as u16) } ${impl_simple_type_with_conversion("font_synthesis", "mFont.synthesis")} diff --git a/components/style/values/computed/font.rs b/components/style/values/computed/font.rs index 0863311fe17..c270969a52f 100644 --- a/components/style/values/computed/font.rs +++ b/components/style/values/computed/font.rs @@ -75,10 +75,12 @@ impl FontWeight { } /// Convert from an Gecko weight - pub fn from_gecko_weight(weight: u16) -> Self { + #[cfg(feature = "gecko")] + pub fn from_gecko_weight(weight: structs::FontWeight) -> Self { // we allow a wider range of weights than is parseable // because system fonts may provide custom values - FontWeight(weight) + let weight = unsafe { bindings::Gecko_FontWeight_ToFloat(weight) }; + FontWeight(weight as u16) } /// Weither this weight is bold