Skip to content

Commit

Permalink
style: Use user defined types for font-stretch / font-style.
Browse files Browse the repository at this point in the history
Co-authored-by: Emilio Cobos Álvarez <emilio@crisal.io>
Bug: 1436048
Reviewed-by: jfkthame,jwatt
MozReview-Commit-ID: 7ONYtICeAqb
  • Loading branch information
jwatt and emilio committed Apr 28, 2018
1 parent b2722e9 commit 36cef8e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 40 deletions.
21 changes: 7 additions & 14 deletions components/style/gecko/rules.rs
Expand Up @@ -26,19 +26,12 @@ impl<'a> ToNsCssValue for &'a FamilyName {

impl<'a> ToNsCssValue for &'a SpecifiedFontStretch {
fn convert(self, nscssvalue: &mut nsCSSValue) {
use values::specified::font::FontStretchKeyword;
match *self {
SpecifiedFontStretch::Stretch(ref p) => nscssvalue.set_number(p.get()),
SpecifiedFontStretch::Keyword(ref kw) => {
// TODO(emilio): Use this branch instead.
if false {
nscssvalue.set_number(kw.compute().0)
} else {
nscssvalue.set_enum(FontStretchKeyword::gecko_keyword(kw.compute().0) as i32)
}
}
let number = match *self {
SpecifiedFontStretch::Stretch(ref p) => p.get(),
SpecifiedFontStretch::Keyword(ref kw) => kw.compute().0,
SpecifiedFontStretch::System(..) => unreachable!(),
}
};
nscssvalue.set_font_stretch(number);
}
}

Expand Down Expand Up @@ -123,8 +116,8 @@ impl<'a> ToNsCssValue for &'a FontStyle {
let mut a = nsCSSValue::null();
let mut b = nsCSSValue::null();

a.set_angle(SpecifiedFontStyle::compute_angle(first));
b.set_angle(SpecifiedFontStyle::compute_angle(second));
a.set_font_style(SpecifiedFontStyle::compute_angle(first).degrees());
b.set_font_style(SpecifiedFontStyle::compute_angle(second).degrees());

nscssvalue.set_pair(&a, &b);
}
Expand Down
14 changes: 12 additions & 2 deletions components/style/gecko_bindings/sugar/ns_css_value.rs
Expand Up @@ -167,16 +167,26 @@ impl nsCSSValue {
unsafe { bindings::Gecko_CSSValue_SetAtomIdent(self, s.into_addrefed()) }
}

/// Set to a font format
/// Set to a font format.
pub fn set_font_format(&mut self, s: &str) {
self.set_string_internal(s, nsCSSUnit::eCSSUnit_Font_Format);
}

/// Set to a local font value
/// Set to a local font value.
pub fn set_local_font(&mut self, s: &Atom) {
self.set_string_from_atom_internal(s, nsCSSUnit::eCSSUnit_Local_Font);
}

/// Set to a font stretch.
pub fn set_font_stretch(&mut self, s: f32) {
unsafe { bindings::Gecko_CSSValue_SetFontStretch(self, s) }
}

/// Set to a font style
pub fn set_font_style(&mut self, s: f32) {
unsafe { bindings::Gecko_CSSValue_SetFontSlantStyle(self, s) }
}

/// Set to a font weight
pub fn set_font_weight(&mut self, w: f32) {
unsafe { bindings::Gecko_CSSValue_SetFontWeight(self, w) }
Expand Down
27 changes: 16 additions & 11 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -25,8 +25,6 @@ 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;
Expand Down Expand Up @@ -2601,12 +2599,14 @@ fn static_assert() {
}

pub fn set_font_weight(&mut self, v: longhands::font_weight::computed_value::T) {
unsafe { Gecko_FontWeight_SetFloat(&mut self.gecko.mFont.weight, v.0) };
unsafe { bindings::Gecko_FontWeight_SetFloat(&mut self.gecko.mFont.weight, v.0) };
}
${impl_simple_copy('font_weight', 'mFont.weight')}

pub fn clone_font_weight(&self) -> longhands::font_weight::computed_value::T {
let weight: f32 = unsafe { Gecko_FontWeight_ToFloat(self.gecko.mFont.weight) };
let weight: f32 = unsafe {
bindings::Gecko_FontWeight_ToFloat(self.gecko.mFont.weight)
};
longhands::font_weight::computed_value::T(weight)
}

Expand All @@ -2618,20 +2618,25 @@ fn static_assert() {
use values::computed::Percentage;
use values::generics::NonNegative;

let stretch = self.gecko.mFont.stretch as f32 / 100.;
let stretch =
unsafe { bindings::Gecko_FontStretch_ToFloat(self.gecko.mFont.stretch) };
debug_assert!(stretch >= 0.);

NonNegative(Percentage(stretch))
}

pub fn set_font_style(&mut self, v: longhands::font_style::computed_value::T) {
use values::generics::font::FontStyle;
self.gecko.mFont.style = match v {
FontStyle::Normal => structs::NS_STYLE_FONT_STYLE_NORMAL,
FontStyle::Italic => structs::NS_STYLE_FONT_STYLE_ITALIC,
// FIXME(emilio): Honor the angle.
FontStyle::Oblique(ref _angle) => structs::NS_STYLE_FONT_STYLE_OBLIQUE,
} as u8;
let s = &mut self.gecko.mFont.style;
unsafe {
match v {
FontStyle::Normal => bindings::Gecko_FontSlantStyle_SetNormal(s),
FontStyle::Italic => bindings::Gecko_FontSlantStyle_SetItalic(s),
FontStyle::Oblique(ref angle) => {
bindings::Gecko_FontSlantStyle_SetOblique(s, angle.0.degrees())
}
}
}
}
${impl_simple_copy('font_style', 'mFont.style')}
pub fn clone_font_style(&self) -> longhands::font_style::computed_value::T {
Expand Down
4 changes: 3 additions & 1 deletion components/style/properties/longhand/font.mako.rs
Expand Up @@ -350,7 +350,9 @@ ${helpers.predefined_type("-x-text-zoom",
)
}
let font_weight = longhands::font_weight::computed_value::T::from_gecko_weight(system.weight);
let font_stretch = NonNegative(Percentage(system.stretch as f32));
let font_stretch = NonNegative(Percentage(unsafe {
bindings::Gecko_FontStretch_ToFloat(system.stretch)
}));
let font_style = FontStyle::from_gecko(system.style);
let ret = ComputedSystemFont {
font_family: longhands::font_family::computed_value::T(
Expand Down
22 changes: 13 additions & 9 deletions components/style/values/computed/font.rs
Expand Up @@ -885,16 +885,20 @@ impl FontStyle {

/// Get the font style from Gecko's nsFont struct.
#[cfg(feature = "gecko")]
pub fn from_gecko(kw: u8) -> Self {
use gecko_bindings::structs;

match kw as u32 {
structs::NS_STYLE_FONT_STYLE_NORMAL => generics::FontStyle::Normal,
structs::NS_STYLE_FONT_STYLE_ITALIC => generics::FontStyle::Italic,
// FIXME(emilio): Grab the angle when we honor it :)
structs::NS_STYLE_FONT_STYLE_OBLIQUE => generics::FontStyle::Oblique(Self::default_angle()),
_ => unreachable!("Unknown font style"),
pub fn from_gecko(style: structs::FontSlantStyle) -> Self {
let mut angle = 0.;
let mut italic = false;
let mut normal = false;
unsafe {
bindings::Gecko_FontSlantStyle_Get(style, &mut normal, &mut italic, &mut angle);
}
if normal {
return generics::FontStyle::Normal;
}
if italic {
return generics::FontStyle::Italic;
}
generics::FontStyle::Oblique(FontStyleAngle(Angle::Deg(angle)))
}
}

Expand Down
14 changes: 11 additions & 3 deletions ports/geckolib/glue.rs
Expand Up @@ -3801,6 +3801,7 @@ pub extern "C" fn Servo_DeclarationBlock_SetKeywordValue(
use style::properties::{PropertyDeclaration, LonghandId};
use style::properties::longhands;
use style::values::specified::BorderStyle;
use style::values::generics::font::FontStyle;

let long = get_longhand_from_id!(property);
let value = value as u32;
Expand All @@ -3820,7 +3821,14 @@ pub extern "C" fn Servo_DeclarationBlock_SetKeywordValue(
longhands::font_size::SpecifiedValue::from_html_size(value as u8)
},
FontStyle => {
ToComputedValue::from_computed_value(&longhands::font_style::computed_value::T::from_gecko(value as u8))
let val = if value == structs::NS_FONT_STYLE_ITALIC {
FontStyle::Italic
} else {
debug_assert_eq!(value, structs::NS_FONT_STYLE_NORMAL);
FontStyle::Normal
};

ToComputedValue::from_computed_value(&val)
},
FontWeight => longhands::font_weight::SpecifiedValue::from_gecko_keyword(value),
ListStyleType => Box::new(longhands::list_style_type::SpecifiedValue::from_gecko_keyword(value)),
Expand Down Expand Up @@ -5434,16 +5442,16 @@ pub extern "C" fn Servo_ParseFontShorthandForMatching(
FontFamily::Values(list) => family.set_move(list.0),
FontFamily::System(_) => return false,
}

let specified_font_style = match font.font_style {
FontStyle::Specified(ref s) => s,
FontStyle::System(_) => return false,
};

match *specified_font_style {
GenericFontStyle::Normal => style.set_normal(),
GenericFontStyle::Italic => style.set_enum(structs::NS_FONT_STYLE_ITALIC as i32),
GenericFontStyle::Oblique(ref angle) => {
style.set_angle(SpecifiedFontStyle::compute_angle(angle))
style.set_font_style(SpecifiedFontStyle::compute_angle(angle).degrees())
}
}

Expand Down

0 comments on commit 36cef8e

Please sign in to comment.