CSS: Use the actual Au type for lengths

This commit is contained in:
Simon Sapin 2013-10-15 17:11:12 +01:00
parent a325905fa6
commit f38b4ab9bb
3 changed files with 40 additions and 40 deletions

View file

@ -2,19 +2,20 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
pub use servo_util::geometry::Au;
pub type Float = f64; pub type Float = f64;
pub type Integer = i64;
pub mod specified { pub mod specified {
use std::ascii::StrAsciiExt; use std::ascii::StrAsciiExt;
use cssparser::*; use cssparser::*;
use super::{Integer, Float}; use super::{Au, Float};
pub use CSSColor = cssparser::Color; pub use CSSColor = cssparser::Color;
#[deriving(Clone)] #[deriving(Clone)]
pub enum Length { pub enum Length {
Au(Integer), // application units Au_(Au), // application units
Em(Float), Em(Float),
Ex(Float), Ex(Float),
// XXX uncomment when supported: // XXX uncomment when supported:
@ -37,7 +38,7 @@ pub mod specified {
match input { match input {
&Dimension(ref value, ref unit) if negative_ok || value.value >= 0. &Dimension(ref value, ref unit) if negative_ok || value.value >= 0.
=> Length::parse_dimension(value.value, unit.as_slice()), => Length::parse_dimension(value.value, unit.as_slice()),
&Number(ref value) if value.value == 0. => Some(Au(0)), &Number(ref value) if value.value == 0. => Some(Au_(Au(0))),
_ => None _ => None
} }
} }
@ -50,11 +51,11 @@ pub mod specified {
pub fn parse_dimension(value: Float, unit: &str) -> Option<Length> { pub fn parse_dimension(value: Float, unit: &str) -> Option<Length> {
match unit.to_ascii_lower().as_slice() { match unit.to_ascii_lower().as_slice() {
"px" => Some(Length::from_px(value)), "px" => Some(Length::from_px(value)),
"in" => Some(Au((value * AU_PER_IN) as Integer)), "in" => Some(Au_(Au((value * AU_PER_IN) as i32))),
"cm" => Some(Au((value * AU_PER_CM) as Integer)), "cm" => Some(Au_(Au((value * AU_PER_CM) as i32))),
"mm" => Some(Au((value * AU_PER_MM) as Integer)), "mm" => Some(Au_(Au((value * AU_PER_MM) as i32))),
"pt" => Some(Au((value * AU_PER_PT) as Integer)), "pt" => Some(Au_(Au((value * AU_PER_PT) as i32))),
"pc" => Some(Au((value * AU_PER_PC) as Integer)), "pc" => Some(Au_(Au((value * AU_PER_PC) as i32))),
"em" => Some(Em(value)), "em" => Some(Em(value)),
"ex" => Some(Ex(value)), "ex" => Some(Ex(value)),
_ => None _ => None
@ -62,7 +63,7 @@ pub mod specified {
} }
#[inline] #[inline]
pub fn from_px(px_value: Float) -> Length { pub fn from_px(px_value: Float) -> Length {
Au((px_value * AU_PER_PX) as Integer) Au_(Au((px_value * AU_PER_PX) as i32))
} }
} }
@ -79,7 +80,7 @@ pub mod specified {
=> Length::parse_dimension(value.value, unit.as_slice()).map_move(LP_Length), => Length::parse_dimension(value.value, unit.as_slice()).map_move(LP_Length),
&ast::Percentage(ref value) if negative_ok || value.value >= 0. &ast::Percentage(ref value) if negative_ok || value.value >= 0.
=> Some(LP_Percentage(value.value / 100.)), => Some(LP_Percentage(value.value / 100.)),
&Number(ref value) if value.value == 0. => Some(LP_Length(Au(0))), &Number(ref value) if value.value == 0. => Some(LP_Length(Au_(Au(0)))),
_ => None _ => None
} }
} }
@ -107,7 +108,7 @@ pub mod specified {
=> Length::parse_dimension(value.value, unit.as_slice()).map_move(LPA_Length), => Length::parse_dimension(value.value, unit.as_slice()).map_move(LPA_Length),
&ast::Percentage(ref value) if negative_ok || value.value >= 0. &ast::Percentage(ref value) if negative_ok || value.value >= 0.
=> Some(LPA_Percentage(value.value / 100.)), => Some(LPA_Percentage(value.value / 100.)),
&Number(ref value) if value.value == 0. => Some(LPA_Length(Au(0))), &Number(ref value) if value.value == 0. => Some(LPA_Length(Au_(Au(0)))),
&Ident(ref value) if value.eq_ignore_ascii_case("auto") => Some(LPA_Auto), &Ident(ref value) if value.eq_ignore_ascii_case("auto") => Some(LPA_Auto),
_ => None _ => None
} }
@ -129,9 +130,11 @@ pub mod computed {
pub use compute_CSSColor = super::super::longhands::computed_as_specified; pub use compute_CSSColor = super::super::longhands::computed_as_specified;
use super::*; use super::*;
use super::super::longhands; use super::super::longhands;
pub use servo_util::geometry::Au;
pub struct Context { pub struct Context {
current_color: cssparser::RGBA, current_color: cssparser::RGBA,
font_size: Length, font_size: Au,
font_weight: longhands::font_weight::ComputedValue, font_weight: longhands::font_weight::ComputedValue,
position: longhands::position::SpecifiedValue, position: longhands::position::SpecifiedValue,
float: longhands::float::SpecifiedValue, float: longhands::float::SpecifiedValue,
@ -142,48 +145,44 @@ pub mod computed {
has_border_left: bool, has_border_left: bool,
// TODO, as needed: root font size, viewport size, etc. // TODO, as needed: root font size, viewport size, etc.
} }
#[deriving(Clone)]
pub struct Length(Integer); // in application units
impl Length {
pub fn times(self, factor: Float) -> Length {
Length(((*self as Float) * factor) as Integer)
}
}
pub fn compute_Length(value: specified::Length, context: &Context) -> Length { #[inline]
fn mul(a: Au, b: Float) -> Au { Au(((*a as Float) * b) as i32) }
pub fn compute_Au(value: specified::Length, context: &Context) -> Au {
match value { match value {
specified::Au(value) => Length(value), specified::Au_(value) => value,
specified::Em(value) => context.font_size.times(value), specified::Em(value) => mul(context.font_size, value),
specified::Ex(value) => { specified::Ex(value) => {
let x_height = 0.5; // TODO: find that from the font let x_height = 0.5; // TODO: find that from the font
context.font_size.times(value * x_height) mul(context.font_size, value * x_height)
}, },
} }
} }
#[deriving(Clone)] #[deriving(Clone)]
pub enum LengthOrPercentage { pub enum LengthOrPercentage {
LP_Length(Length), LP_Length(Au),
LP_Percentage(Float), LP_Percentage(Float),
} }
pub fn compute_LengthOrPercentage(value: specified::LengthOrPercentage, context: &Context) pub fn compute_LengthOrPercentage(value: specified::LengthOrPercentage, context: &Context)
-> LengthOrPercentage { -> LengthOrPercentage {
match value { match value {
specified::LP_Length(value) => LP_Length(compute_Length(value, context)), specified::LP_Length(value) => LP_Length(compute_Au(value, context)),
specified::LP_Percentage(value) => LP_Percentage(value), specified::LP_Percentage(value) => LP_Percentage(value),
} }
} }
#[deriving(Clone)] #[deriving(Clone)]
pub enum LengthOrPercentageOrAuto { pub enum LengthOrPercentageOrAuto {
LPA_Length(Length), LPA_Length(Au),
LPA_Percentage(Float), LPA_Percentage(Float),
LPA_Auto, LPA_Auto,
} }
pub fn compute_LengthOrPercentageOrAuto(value: specified::LengthOrPercentageOrAuto, pub fn compute_LengthOrPercentageOrAuto(value: specified::LengthOrPercentageOrAuto,
context: &Context) -> LengthOrPercentageOrAuto { context: &Context) -> LengthOrPercentageOrAuto {
match value { match value {
specified::LPA_Length(value) => LPA_Length(compute_Length(value, context)), specified::LPA_Length(value) => LPA_Length(compute_Au(value, context)),
specified::LPA_Percentage(value) => LPA_Percentage(value), specified::LPA_Percentage(value) => LPA_Percentage(value),
specified::LPA_Auto => LPA_Auto, specified::LPA_Auto => LPA_Auto,
} }

View file

@ -146,14 +146,14 @@ pub mod longhands {
% for side in ["top", "right", "bottom", "left"]: % for side in ["top", "right", "bottom", "left"]:
${predefined_type("margin-" + side, "LengthOrPercentageOrAuto", ${predefined_type("margin-" + side, "LengthOrPercentageOrAuto",
"computed::LPA_Length(computed::Length(0))")} "computed::LPA_Length(Au(0))")}
% endfor % endfor
${new_style_struct("Padding")} ${new_style_struct("Padding")}
% for side in ["top", "right", "bottom", "left"]: % for side in ["top", "right", "bottom", "left"]:
${predefined_type("padding-" + side, "LengthOrPercentage", ${predefined_type("padding-" + side, "LengthOrPercentage",
"computed::LP_Length(computed::Length(0))", "computed::LP_Length(Au(0))",
"parse_non_negative")} "parse_non_negative")}
% endfor % endfor
@ -187,17 +187,17 @@ pub mod longhands {
% for side in ["top", "right", "bottom", "left"]: % for side in ["top", "right", "bottom", "left"]:
<%self:longhand name="border-${side}-width"> <%self:longhand name="border-${side}-width">
pub type SpecifiedValue = specified::Length; pub type SpecifiedValue = specified::Length;
pub type ComputedValue = computed::Length; pub type ComputedValue = Au;
#[inline] pub fn get_initial_value() -> ComputedValue { #[inline] pub fn get_initial_value() -> ComputedValue {
computed::Length(3 * 60) // medium Au::from_px(3) // medium
} }
pub fn parse(input: &[ComponentValue]) -> Option<SpecifiedValue> { pub fn parse(input: &[ComponentValue]) -> Option<SpecifiedValue> {
one_component_value(input).chain(parse_border_width) one_component_value(input).chain(parse_border_width)
} }
pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context) pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> ComputedValue { -> ComputedValue {
if context.has_border_${side} { computed::compute_Length(value, context) } if context.has_border_${side} { computed::compute_Au(value, context) }
else { computed::Length(0) } else { Au(0) }
} }
</%self:longhand> </%self:longhand>
% endfor % endfor
@ -252,7 +252,7 @@ pub mod longhands {
#[deriving(Clone)] #[deriving(Clone)]
pub enum ComputedValue { pub enum ComputedValue {
Normal, Normal,
Length(computed::Length), Length(Au),
Number(Float), Number(Float),
} }
#[inline] pub fn get_initial_value() -> ComputedValue { Normal } #[inline] pub fn get_initial_value() -> ComputedValue { Normal }
@ -260,7 +260,7 @@ pub mod longhands {
-> ComputedValue { -> ComputedValue {
match value { match value {
SpecifiedNormal => Normal, SpecifiedNormal => Normal,
SpecifiedLength(value) => Length(computed::compute_Length(value, context)), SpecifiedLength(value) => Length(computed::compute_Au(value, context)),
SpecifiedNumber(value) => Number(value), SpecifiedNumber(value) => Number(value),
} }
} }
@ -295,7 +295,7 @@ pub mod longhands {
% for keyword in vertical_align_keywords: % for keyword in vertical_align_keywords:
${to_rust_ident(keyword)}, ${to_rust_ident(keyword)},
% endfor % endfor
Length(computed::Length), Length(Au),
Percentage(Float), Percentage(Float),
} }
#[inline] pub fn get_initial_value() -> ComputedValue { baseline } #[inline] pub fn get_initial_value() -> ComputedValue { baseline }
@ -502,11 +502,11 @@ pub mod longhands {
</%self:single_component_value> </%self:single_component_value>
<%self:single_component_value name="font-size" inherited="True"> <%self:single_component_value name="font-size" inherited="True">
pub use to_computed_value = super::super::common_types::computed::compute_Length; pub use to_computed_value = super::super::common_types::computed::compute_Au;
pub type SpecifiedValue = specified::Length; // Percentages are the same as em. pub type SpecifiedValue = specified::Length; // Percentages are the same as em.
pub type ComputedValue = computed::Length; pub type ComputedValue = Au;
#[inline] pub fn get_initial_value() -> ComputedValue { #[inline] pub fn get_initial_value() -> ComputedValue {
computed::Length(16 * 60) // medium Au::from_px(16) // medium
} }
/// <length> | <percentage> /// <length> | <percentage>
/// TODO: support <absolute-size> and <relative-size> /// TODO: support <absolute-size> and <relative-size>

View file

@ -14,6 +14,7 @@
extern mod extra; extern mod extra;
extern mod cssparser; extern mod cssparser;
extern mod servo_util (name = "util");
extern mod script; extern mod script;