Skip to content

Commit

Permalink
Introduce a FamilyName type, like FontFamily but not a generic family.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Feb 2, 2017
1 parent 44f6c60 commit 52aa243
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion components/style/properties/gecko.mako.rs
Expand Up @@ -944,7 +944,7 @@ fn static_assert() {
for family in &v.0 {
match *family {
FontFamily::FamilyName(ref name) => {
unsafe { Gecko_FontFamilyList_AppendNamed(list, name.as_ptr()); }
unsafe { Gecko_FontFamilyList_AppendNamed(list, name.0.as_ptr()); }
}
FontFamily::Generic(ref name) => {
let family_type =
Expand Down
42 changes: 31 additions & 11 deletions components/style/properties/longhand/font.mako.rs
Expand Up @@ -10,7 +10,7 @@
additional_methods=[Method("compute_font_hash", is_mut=True)]) %>
<%helpers:longhand name="font-family" animatable="False" need_index="True"
spec="https://drafts.csswg.org/css-fonts/#propdef-font-family">
use self::computed_value::FontFamily;
use self::computed_value::{FontFamily, FamilyName};
use values::NoViewportPercentage;
use values::computed::ComputedValueAsSpecified;
pub use self::computed_value::T as SpecifiedValue;
Expand All @@ -28,15 +28,19 @@
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
pub enum FontFamily {
FamilyName(Atom),
FamilyName(FamilyName),
Generic(Atom),
}

#[derive(Debug, PartialEq, Eq, Clone, Hash)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct FamilyName(pub Atom);

impl FontFamily {
#[inline]
pub fn atom(&self) -> &Atom {
match *self {
FontFamily::FamilyName(ref name) => name,
FontFamily::FamilyName(ref name) => &name.0,
FontFamily::Generic(ref name) => name,
}
}
Expand Down Expand Up @@ -67,18 +71,22 @@
"monospace" => return FontFamily::Generic(atom!("monospace")),
_ => {}
}
FontFamily::FamilyName(input)
FontFamily::FamilyName(FamilyName(input))
}
}

impl ToCss for FamilyName {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
dest.write_char('"')?;
write!(CssStringWriter::new(dest), "{}", self.0)?;
dest.write_char('"')
}
}

impl ToCss for FontFamily {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
FontFamily::FamilyName(ref name) => {
dest.write_char('"')?;
write!(CssStringWriter::new(dest), "{}", name)?;
dest.write_char('"')
}
FontFamily::FamilyName(ref name) => name.to_css(dest),

// All generic values accepted by the parser are known to not require escaping.
FontFamily::Generic(ref name) => write!(dest, "{}", name),
Expand Down Expand Up @@ -121,10 +129,22 @@
}
}

/// `FamilyName::parse` is based on `FontFamily::parse` and not the other way around
/// because we want the former to exclude generic family keywords.
impl Parse for FamilyName {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
match FontFamily::parse(context, input) {
Ok(FontFamily::FamilyName(name)) => Ok(name),
Ok(FontFamily::Generic(_)) |
Err(()) => Err(())
}
}
}

impl Parse for FontFamily {
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
if let Ok(value) = input.try(|input| input.expect_string()) {
return Ok(FontFamily::FamilyName(Atom::from(&*value)))
return Ok(FontFamily::FamilyName(FamilyName(Atom::from(&*value))))
}
let first_ident = try!(input.expect_ident());

Expand Down Expand Up @@ -165,7 +185,7 @@
value.push_str(" ");
value.push_str(&ident);
}
Ok(FontFamily::FamilyName(Atom::from(value)))
Ok(FontFamily::FamilyName(FamilyName(Atom::from(value))))
}
}
</%helpers:longhand>
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/gfx/font_cache_thread.rs
Expand Up @@ -4,16 +4,16 @@

use gfx::font_cache_thread::FontCacheThread;
use ipc_channel::ipc;
use style::computed_values::font_family::FontFamily;
use style::computed_values::font_family::{FontFamily, FamilyName};
use style::font_face::{FontFaceRule, Source};

#[test]
fn test_local_web_font() {
let (inp_chan, _) = ipc::channel().unwrap();
let (out_chan, out_receiver) = ipc::channel().unwrap();
let font_cache_thread = FontCacheThread::new(inp_chan, None);
let family_name = FontFamily::FamilyName(From::from("test family"));
let variant_name = FontFamily::FamilyName(From::from("test font face"));
let family_name = FontFamily::FamilyName(FamilyName(From::from("test family")));
let variant_name = FontFamily::FamilyName(FamilyName(From::from("test font face")));
let font_face_rule = FontFaceRule {
family: family_name.clone(),
sources: vec![Source::Local(variant_name)],
Expand Down

0 comments on commit 52aa243

Please sign in to comment.