Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer auto-generation for some keyword props #13902

Merged
merged 4 commits into from Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/gfx/paint_context.rs
Expand Up @@ -223,7 +223,7 @@ impl<'a> PaintContext<'a> {
// Something like Scale2x would be ideal.
let draw_surface_filter = match image_rendering {
image_rendering::T::auto => Filter::Linear,
image_rendering::T::crispedges | image_rendering::T::pixelated => Filter::Point,
image_rendering::T::crisp_edges | image_rendering::T::pixelated => Filter::Point,
};

let draw_surface_options = DrawSurfaceOptions::new(draw_surface_filter, true);
Expand Down
2 changes: 1 addition & 1 deletion components/layout/webrender_helpers.rs
Expand Up @@ -213,7 +213,7 @@ trait ToImageRendering {
impl ToImageRendering for image_rendering::T {
fn to_image_rendering(&self) -> webrender_traits::ImageRendering {
match *self {
image_rendering::T::crispedges => webrender_traits::ImageRendering::CrispEdges,
image_rendering::T::crisp_edges => webrender_traits::ImageRendering::CrispEdges,
image_rendering::T::auto => webrender_traits::ImageRendering::Auto,
image_rendering::T::pixelated => webrender_traits::ImageRendering::Pixelated,
}
Expand Down
13 changes: 9 additions & 4 deletions components/style/properties/data.py
Expand Up @@ -18,7 +18,7 @@ def to_camel_case(ident):

class Keyword(object):
def __init__(self, name, values, gecko_constant_prefix=None,
gecko_enum_prefix=None,
gecko_enum_prefix=None, custom_consts=None,
extra_gecko_values=None, extra_servo_values=None):
self.name = name
self.values = values.split()
Expand All @@ -29,6 +29,7 @@ def __init__(self, name, values, gecko_constant_prefix=None,
self.gecko_enum_prefix = gecko_enum_prefix
self.extra_gecko_values = (extra_gecko_values or "").split()
self.extra_servo_values = (extra_servo_values or "").split()
self.consts_map = {} if custom_consts is None else custom_consts

def gecko_values(self):
return self.values + self.extra_gecko_values
Expand All @@ -45,12 +46,15 @@ def values_for(self, product):
raise Exception("Bad product: " + product)

def gecko_constant(self, value):
moz_stripped = value.replace("-moz-", '')
parts = moz_stripped.split('-')
if self.gecko_enum_prefix:
parts = value.replace("-moz-", "").split("-")
parts = [p.title() for p in parts]
return self.gecko_enum_prefix + "::" + "".join(parts)
else:
return self.gecko_constant_prefix + "_" + value.replace("-moz-", "").replace("-", "_").upper()
mapped = self.consts_map.get(value)
suffix = mapped if mapped else moz_stripped.replace("-", "_")
return self.gecko_constant_prefix + "_" + suffix.upper()

def needs_cast(self):
return self.gecko_enum_prefix is None
Expand All @@ -63,7 +67,7 @@ class Longhand(object):
def __init__(self, style_struct, name, animatable=None, derived_from=None, keyword=None,
predefined_type=None, custom_cascade=False, experimental=False, internal=False,
need_clone=False, need_index=False, gecko_ffi_name=None, depend_on_viewport_size=False,
allowed_in_keyframe_block=True, complex_color=False):
allowed_in_keyframe_block=True, complex_color=False, cast_type='u8'):
self.name = name
self.keyword = keyword
self.predefined_type = predefined_type
Expand All @@ -78,6 +82,7 @@ def __init__(self, style_struct, name, animatable=None, derived_from=None, keywo
self.depend_on_viewport_size = depend_on_viewport_size
self.derived_from = (derived_from or "").split()
self.complex_color = complex_color
self.cast_type = cast_type

# https://drafts.csswg.org/css-animations/#keyframes
# > The <declaration-list> inside of <keyframe-block> accepts any CSS property
Expand Down
62 changes: 21 additions & 41 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -490,21 +490,29 @@ impl Debug for ${style_struct.gecko_struct_name} {
"CSSColor": impl_color,
}

def predefined_type_method(longhand):
def longhand_method(longhand):
args = dict(ident=longhand.ident, gecko_ffi_name=longhand.gecko_ffi_name,
need_clone=longhand.need_clone)
method = predefined_types[longhand.predefined_type]

# additional type-specific arguments
if longhand.predefined_type in ["CSSColor"]:
args.update(complex_color=longhand.complex_color)
# get the method and pass additional keyword or type-specific arguments
if longhand.keyword:
method = impl_keyword
args.update(keyword=longhand.keyword)
if "font" in longhand.ident:
args.update(cast_type=longhand.cast_type)
else:
method = predefined_types[longhand.predefined_type]
if longhand.predefined_type in ["CSSColor"]:
args.update(complex_color=longhand.complex_color)

method(**args)

keyword_longhands = [x for x in longhands if x.keyword and x.name not in force_stub]
predefined_longhands = [x for x in longhands
if x.predefined_type in predefined_types and x.name not in force_stub]
stub_longhands = [x for x in longhands if x not in keyword_longhands + predefined_longhands]
picked_longhands, stub_longhands = [], []
for x in longhands:
if (x.keyword or x.predefined_type in predefined_types) and x.name not in force_stub:
picked_longhands.append(x)
else:
stub_longhands.append(x)

# If one of the longhands is not handled
# by either:
Expand All @@ -518,6 +526,7 @@ impl Debug for ${style_struct.gecko_struct_name} {
# If you hit this error, please add `product="servo"` to the longhand.
# In case the longhand is used in a shorthand, add it to the force_stub
# list above.

for stub in stub_longhands:
if stub.name not in force_stub:
raise Exception("Don't know what to do with longhand %s in style struct %s"
Expand All @@ -533,10 +542,8 @@ impl ${style_struct.gecko_struct_name} {
* Auto-Generated Methods.
*/
<%
for longhand in keyword_longhands:
impl_keyword(longhand.ident, longhand.gecko_ffi_name, longhand.keyword, longhand.need_clone)
for longhand in predefined_longhands:
predefined_type_method(longhand)
for longhand in picked_longhands:
longhand_method(longhand)
%>

/*
Expand Down Expand Up @@ -758,7 +765,7 @@ fn static_assert() {
</%self:impl_trait>

<%self:impl_trait style_struct_name="Font"
skip_longhands="font-family font-kerning font-stretch font-style font-size font-weight"
skip_longhands="font-family font-size font-weight"
skip_additionals="*">

pub fn set_font_family(&mut self, v: longhands::font_family::computed_value::T) {
Expand Down Expand Up @@ -791,9 +798,6 @@ fn static_assert() {
unsafe { Gecko_CopyFontFamilyFrom(&mut self.gecko.mFont, &other.gecko.mFont); }
}

<%call expr="impl_keyword('font_style', 'mFont.style',
data.longhands_by_name['font-style'].keyword, need_clone=False)"></%call>

// FIXME(bholley): Gecko has two different sizes, one of which (mSize) is the
// actual computed size, and the other of which (mFont.size) is the 'display
// size' which takes font zooming into account. We don't handle font zooming yet.
Expand All @@ -809,19 +813,6 @@ fn static_assert() {
Au(self.gecko.mSize)
}

<% kerning_keyword = Keyword("font-kerning", "auto normal none",
gecko_constant_prefix='NS_FONT_KERNING') %>

${impl_keyword('font_kerning', 'mFont.kerning', kerning_keyword, need_clone=False)}

<% stretch_keyword = Keyword("font-stretch",
"normal ultra-condensed extra-condensed condensed " +
"semi-condensed semi-expanded expanded " +
"extra-expanded ultra-expanded",
gecko_constant_prefix='NS_FONT_STRETCH') %>

${impl_keyword('font_stretch', 'mFont.stretch', stretch_keyword, need_clone=False, cast_type='i16')}

pub fn set_font_weight(&mut self, v: longhands::font_weight::computed_value::T) {
self.gecko.mFont.weight = v as u16;
}
Expand Down Expand Up @@ -1634,17 +1625,6 @@ fn static_assert() {
</%self:impl_trait>


<%self:impl_trait style_struct_name="InheritedBox"
skip_longhands="image-rendering">

<% render_keyword = Keyword("image-rendering",
"auto optimizequality optimizespeed crispedges") %>

${impl_keyword('image_rendering', 'mImageRendering', render_keyword, need_clone=False)}

</%self:impl_trait>


<%self:impl_trait style_struct_name="InheritedText"
skip_longhands="text-align text-emphasis-style text-shadow line-height letter-spacing word-spacing">

Expand Down
1 change: 1 addition & 0 deletions components/style/properties/helpers.mako.rs
Expand Up @@ -312,6 +312,7 @@
keyword_kwargs = {a: kwargs.pop(a, None) for a in [
'gecko_constant_prefix', 'gecko_enum_prefix',
'extra_gecko_values', 'extra_servo_values',
'custom_consts',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another instance of this same list below (line 359)

]}
%>

Expand Down
7 changes: 7 additions & 0 deletions components/style/properties/longhand/font.mako.rs
Expand Up @@ -123,7 +123,9 @@
${helpers.single_keyword("font-style",
"normal italic oblique",
gecko_constant_prefix="NS_FONT_STYLE",
gecko_ffi_name="mFont.style",
animatable=False)}

${helpers.single_keyword("font-variant",
"normal small-caps",
animatable=False)}
Expand Down Expand Up @@ -347,11 +349,16 @@ ${helpers.single_keyword("font-stretch",
"normal ultra-condensed extra-condensed condensed \
semi-condensed semi-expanded expanded extra-expanded \
ultra-expanded",
gecko_ffi_name="mFont.stretch",
gecko_constant_prefix="NS_FONT_STRETCH",
cast_type='i16',
animatable=False)}

${helpers.single_keyword("font-kerning",
"auto none normal",
products="gecko",
gecko_ffi_name="mFont.kerning",
gecko_constant_prefix="NS_FONT_KERNING",
animatable=False)}

${helpers.single_keyword("font-variant-position",
Expand Down
76 changes: 9 additions & 67 deletions components/style/properties/longhand/inherited_box.mako.rs
Expand Up @@ -40,73 +40,15 @@ ${helpers.single_keyword("color-adjust",
"economy exact", products="gecko",
animatable=False)}

<%helpers:longhand name="image-rendering" animatable="False">
pub mod computed_value {
use cssparser::ToCss;
use std::fmt;

#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
pub enum T {
auto,
crispedges,
% if product == "gecko":
optimizequality,
optimizespeed,
% else:
pixelated, // firefox doesn't support it (https://bugzilla.mozilla.org/show_bug.cgi?id=856337)
% endif
}

impl ToCss for T {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
T::auto => dest.write_str("auto"),
T::crispedges => dest.write_str("crisp-edges"),
% if product == "gecko":
T::optimizequality => dest.write_str("optimizeQuality"),
T::optimizespeed => dest.write_str("optimizeSpeed"),
% else:
T::pixelated => dest.write_str("pixelated"),
% endif
}
}
}
}

use values::NoViewportPercentage;
impl NoViewportPercentage for SpecifiedValue {}

pub type SpecifiedValue = computed_value::T;

#[inline]
pub fn get_initial_value() -> computed_value::T {
computed_value::T::auto
}

pub fn parse(_: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
// According to to CSS-IMAGES-3, `optimizespeed` and `optimizequality` are synonyms for
// `auto`.
match_ignore_ascii_case! {
try!(input.expect_ident()),
"auto" => Ok(computed_value::T::auto),
"crisp-edges" => Ok(computed_value::T::crispedges),
% if product == "gecko":
"optimizequality" => Ok(computed_value::T::optimizequality),
"optimizespeed" => Ok(computed_value::T::optimizespeed),
% else:
"optimizequality" => Ok(computed_value::T::auto),
"optimizespeed" => Ok(computed_value::T::auto),
"pixelated" => Ok(computed_value::T::pixelated),
% endif
_ => Err(())
}
}

use values::computed::ComputedValueAsSpecified;
impl ComputedValueAsSpecified for SpecifiedValue { }
</%helpers:longhand>
<% image_rendering_custom_consts = { "crisp-edges": "CRISPEDGES" } %>
// According to to CSS-IMAGES-3, `optimizespeed` and `optimizequality` are synonyms for `auto`
// And, firefox doesn't support `pixelated` yet (https://bugzilla.mozilla.org/show_bug.cgi?id=856337)
${helpers.single_keyword("image-rendering",
"auto crisp-edges",
extra_gecko_values="optimizespeed optimizequality",
extra_servo_values="pixelated",
custom_consts=image_rendering_custom_consts,
animatable=False)}

// Used in the bottom-up flow construction traversal to avoid constructing flows for
// descendants of nodes with `display: none`.
Expand Down