style: Use user defined types for font-stretch / font-style.

Co-authored-by: Emilio Cobos Álvarez <emilio@crisal.io>
Bug: 1436048
Reviewed-by: jfkthame,jwatt
MozReview-Commit-ID: 7ONYtICeAqb
This commit is contained in:
Jonathan Watt 2018-04-23 16:52:20 +02:00 committed by Emilio Cobos Álvarez
parent b2722e965c
commit 36cef8ec68
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
6 changed files with 62 additions and 40 deletions

View file

@ -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);
}
}
@ -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);
}

View file

@ -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) }

View file

@ -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;
@ -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)
}
@ -2618,7 +2618,8 @@ 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))
@ -2626,12 +2627,16 @@ fn static_assert() {
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 {

View file

@ -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(

View file

@ -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)))
}
}