Drop initial values of properties other than font-size and font-family in serialization of canvas font attribute.

According to the canvas spec [1], we must drop initial values other than
font-size and font-family when we serialize canvas font attribute.
As for font-size and font-family, the default values, '10px sans-serif' [2]
are not dropped at all.

[1] https://html.spec.whatwg.org/multipage/scripting.html#dom-context-2d-font
[2] https://html.spec.whatwg.org/multipage/scripting.html#canvastextdrawingstyles
This commit is contained in:
Hiroyuki Ikezoe 2017-05-20 07:37:24 +09:00
parent 7330dab51f
commit e7b777384c
2 changed files with 91 additions and 38 deletions

View file

@ -122,8 +122,67 @@
SomeSystem,
None
}
% endif
enum SerializeFor {
Normal,
% if product == "gecko":
Canvas,
% endif
}
impl<'a> LonghandsToSerialize<'a> {
impl<'a> LonghandsToSerialize<'a> {
fn to_css_for<W>(&self,
serialize_for: SerializeFor,
dest: &mut W) -> fmt::Result where W: fmt::Write {
% if product == "gecko":
match self.check_system() {
CheckSystemResult::AllSystem(sys) => return sys.to_css(dest),
CheckSystemResult::SomeSystem => return Ok(()),
CheckSystemResult::None => ()
}
% endif
% if product == "gecko" or data.testing:
% for name in gecko_sub_properties:
if self.font_${name} != &font_${name}::get_initial_specified_value() {
return Ok(());
}
% endfor
% endif
// In case of serialization for canvas font, we need to drop
// initial values of properties other than size and family.
% for name in "style variant_caps weight stretch".split():
let needs_this_property = match serialize_for {
SerializeFor::Normal => true,
% if product == "gecko":
SerializeFor::Canvas =>
self.font_${name} != &font_${name}::get_initial_specified_value(),
% endif
};
if needs_this_property {
self.font_${name}.to_css(dest)?;
dest.write_str(" ")?;
}
% endfor
self.font_size.to_css(dest)?;
match *self.line_height {
line_height::SpecifiedValue::Normal => {},
_ => {
dest.write_str("/")?;
self.line_height.to_css(dest)?;
}
}
dest.write_str(" ")?;
self.font_family.to_css(dest)?;
Ok(())
}
% if product == "gecko":
/// Check if some or all members are system fonts
fn check_system(&self) -> CheckSystemResult {
let mut sys = None;
@ -148,47 +207,18 @@
CheckSystemResult::None
}
}
}
% endif
/// Serialize the shorthand value for canvas font attribute.
pub fn to_css_for_canvas<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.to_css_for(SerializeFor::Canvas, dest)
}
% endif
}
// This may be a bit off, unsure, possibly needs changes
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
% if product == "gecko":
match self.check_system() {
CheckSystemResult::AllSystem(sys) => return sys.to_css(dest),
CheckSystemResult::SomeSystem => return Ok(()),
CheckSystemResult::None => ()
}
% endif
% if product == "gecko" or data.testing:
% for name in gecko_sub_properties:
if self.font_${name} != &font_${name}::get_initial_specified_value() {
return Ok(());
}
% endfor
% endif
% for name in "style variant_caps weight stretch".split():
self.font_${name}.to_css(dest)?;
dest.write_str(" ")?;
% endfor
self.font_size.to_css(dest)?;
match *self.line_height {
line_height::SpecifiedValue::Normal => {},
_ => {
dest.write_str("/")?;
self.line_height.to_css(dest)?;
}
}
dest.write_str(" ")?;
self.font_family.to_css(dest)?;
Ok(())
self.to_css_for(SerializeFor::Normal, dest)
}
}
</%helpers:shorthand>

View file

@ -1434,6 +1434,29 @@ pub extern "C" fn Servo_DeclarationBlock_SerializeOneValue(
})
}
#[no_mangle]
pub extern "C" fn Servo_SerializeFontValueForCanvas(
declarations: RawServoDeclarationBlockBorrowed,
buffer: *mut nsAString) {
use style::properties::shorthands::font;
read_locked_arc(declarations, |decls: &PropertyDeclarationBlock| {
let longhands = match font::LonghandsToSerialize::from_iter(decls.declarations_iter()) {
Ok(l) => l,
Err(()) => {
warn!("Unexpected property!");
return;
}
};
let mut string = String::new();
let rv = longhands.to_css_for_canvas(&mut string);
debug_assert!(rv.is_ok());
write!(unsafe { &mut *buffer }, "{}", string).expect("Failed to copy string");
})
}
#[no_mangle]
pub extern "C" fn Servo_DeclarationBlock_Count(declarations: RawServoDeclarationBlockBorrowed) -> u32 {
read_locked_arc(declarations, |decls: &PropertyDeclarationBlock| {