Bug 1370734 - Part 1: stylo: Refactor generic font handling into a method; r?heycam
MozReview-Commit-ID: Lva9FIPeHP2
--- a/servo/components/style/properties/gecko.mako.rs
+++ b/servo/components/style/properties/gecko.mako.rs
@@ -216,16 +216,19 @@ impl ComputedValues {
<%def name="declare_style_struct(style_struct)">
pub struct ${style_struct.gecko_struct_name} {
gecko: ${style_struct.gecko_ffi_name},
}
impl ${style_struct.gecko_struct_name} {
pub fn gecko(&self) -> &${style_struct.gecko_ffi_name} {
&self.gecko
}
+ pub fn gecko_mut(&mut self) -> &mut ${style_struct.gecko_ffi_name} {
+ &mut self.gecko
+ }
}
</%def>
<%def name="impl_simple_setter(ident, gecko_ffi_name)">
#[allow(non_snake_case)]
pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) {
${set_gecko_property(gecko_ffi_name, "v")}
}
@@ -1582,51 +1585,29 @@ fn static_assert() {
pub fn fixup_none_generic(&mut self, device: &Device) {
unsafe {
bindings::Gecko_nsStyleFont_FixupNoneGeneric(&mut self.gecko, &*device.pres_context)
}
}
pub fn set_font_family(&mut self, v: longhands::font_family::computed_value::T) {
use properties::longhands::font_family::computed_value::FontFamily;
- use gecko_bindings::structs::FontFamilyType;
let list = &mut self.gecko.mFont.fontlist;
unsafe { Gecko_FontFamilyList_Clear(list); }
self.gecko.mGenericID = structs::kGenericFont_NONE;
for family in &v.0 {
match *family {
FontFamily::FamilyName(ref f) => {
unsafe { Gecko_FontFamilyList_AppendNamed(list, f.name.as_ptr(), f.quoted); }
}
FontFamily::Generic(ref name) => {
- let (family_type, generic) =
- if name == &atom!("serif") {
- (FontFamilyType::eFamily_serif,
- structs::kGenericFont_serif)
- } else if name == &atom!("sans-serif") {
- (FontFamilyType::eFamily_sans_serif,
- structs::kGenericFont_sans_serif)
- } else if name == &atom!("cursive") {
- (FontFamilyType::eFamily_cursive,
- structs::kGenericFont_cursive)
- } else if name == &atom!("fantasy") {
- (FontFamilyType::eFamily_fantasy,
- structs::kGenericFont_fantasy)
- } else if name == &atom!("monospace") {
- (FontFamilyType::eFamily_monospace,
- structs::kGenericFont_monospace)
- } else if name == &atom!("-moz-fixed") {
- (FontFamilyType::eFamily_moz_fixed,
- structs::kGenericFont_moz_fixed)
- } else {
- panic!("Unknown generic font family")
- };
+ let (family_type, generic) = FontFamily::generic(name);
if v.0.len() == 1 {
self.gecko.mGenericID = generic;
}
unsafe { Gecko_FontFamilyList_AppendGeneric(list, family_type); }
}
}
}
}
--- a/servo/components/style/properties/longhand/font.mako.rs
+++ b/servo/components/style/properties/longhand/font.mako.rs
@@ -186,16 +186,43 @@
value.push_str(" ");
value.push_str(&ident);
}
Ok(FontFamily::FamilyName(FamilyName {
name: Atom::from(value),
quoted: false,
}))
}
+
+ #[cfg(feature = "gecko")]
+ /// Return the generic ID for a given generic font name
+ pub fn generic(name: &Atom) -> (::gecko_bindings::structs::FontFamilyType, u8) {
+ use gecko_bindings::structs::{self, FontFamilyType};
+ if *name == atom!("serif") {
+ (FontFamilyType::eFamily_serif,
+ structs::kGenericFont_serif)
+ } else if *name == atom!("sans-serif") {
+ (FontFamilyType::eFamily_sans_serif,
+ structs::kGenericFont_sans_serif)
+ } else if *name == atom!("cursive") {
+ (FontFamilyType::eFamily_cursive,
+ structs::kGenericFont_cursive)
+ } else if *name == atom!("fantasy") {
+ (FontFamilyType::eFamily_fantasy,
+ structs::kGenericFont_fantasy)
+ } else if *name == atom!("monospace") {
+ (FontFamilyType::eFamily_monospace,
+ structs::kGenericFont_monospace)
+ } else if *name == atom!("-moz-fixed") {
+ (FontFamilyType::eFamily_moz_fixed,
+ structs::kGenericFont_moz_fixed)
+ } else {
+ panic!("Unknown generic {}", name);
+ }
+ }
}
impl ToCss for FamilyName {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
if self.quoted {
dest.write_char('"')?;
write!(CssStringWriter::new(dest), "{}", self.name)?;
dest.write_char('"')
@@ -255,16 +282,31 @@
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SpecifiedValue {
Values(Vec<FontFamily>),
System(SystemFont),
}
+ #[cfg(feature = "gecko")]
+ impl SpecifiedValue {
+ /// Return the generic ID if it is a single generic font
+ pub fn single_generic(&self) -> Option<u8> {
+ if let SpecifiedValue::Values(ref values) = *self {
+ if values.len() == 1 {
+ if let FontFamily::Generic(ref name) = values[0] {
+ return Some(FontFamily::generic(name).1);
+ }
+ }
+ }
+ None
+ }
+ }
+
impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T;
fn to_computed_value(&self, _cx: &Context) -> Self::ComputedValue {
match *self {
SpecifiedValue::Values(ref v) => computed_value::T(v.clone()),
SpecifiedValue::System(_) => {
<%self:nongecko_unreachable>
_cx.cached_system_font.as_ref().unwrap().font_family.clone()