mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
CSS: Use the actual Au type for lengths
This commit is contained in:
parent
a325905fa6
commit
f38b4ab9bb
3 changed files with 40 additions and 40 deletions
|
@ -2,19 +2,20 @@
|
|||
* 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/. */
|
||||
|
||||
pub use servo_util::geometry::Au;
|
||||
|
||||
pub type Float = f64;
|
||||
pub type Integer = i64;
|
||||
|
||||
|
||||
pub mod specified {
|
||||
use std::ascii::StrAsciiExt;
|
||||
use cssparser::*;
|
||||
use super::{Integer, Float};
|
||||
use super::{Au, Float};
|
||||
pub use CSSColor = cssparser::Color;
|
||||
|
||||
#[deriving(Clone)]
|
||||
pub enum Length {
|
||||
Au(Integer), // application units
|
||||
Au_(Au), // application units
|
||||
Em(Float),
|
||||
Ex(Float),
|
||||
// XXX uncomment when supported:
|
||||
|
@ -37,7 +38,7 @@ pub mod specified {
|
|||
match input {
|
||||
&Dimension(ref value, ref unit) if negative_ok || value.value >= 0.
|
||||
=> 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
|
||||
}
|
||||
}
|
||||
|
@ -50,11 +51,11 @@ pub mod specified {
|
|||
pub fn parse_dimension(value: Float, unit: &str) -> Option<Length> {
|
||||
match unit.to_ascii_lower().as_slice() {
|
||||
"px" => Some(Length::from_px(value)),
|
||||
"in" => Some(Au((value * AU_PER_IN) as Integer)),
|
||||
"cm" => Some(Au((value * AU_PER_CM) as Integer)),
|
||||
"mm" => Some(Au((value * AU_PER_MM) as Integer)),
|
||||
"pt" => Some(Au((value * AU_PER_PT) as Integer)),
|
||||
"pc" => Some(Au((value * AU_PER_PC) as Integer)),
|
||||
"in" => Some(Au_(Au((value * AU_PER_IN) as i32))),
|
||||
"cm" => Some(Au_(Au((value * AU_PER_CM) as i32))),
|
||||
"mm" => Some(Au_(Au((value * AU_PER_MM) as i32))),
|
||||
"pt" => Some(Au_(Au((value * AU_PER_PT) as i32))),
|
||||
"pc" => Some(Au_(Au((value * AU_PER_PC) as i32))),
|
||||
"em" => Some(Em(value)),
|
||||
"ex" => Some(Ex(value)),
|
||||
_ => None
|
||||
|
@ -62,7 +63,7 @@ pub mod specified {
|
|||
}
|
||||
#[inline]
|
||||
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),
|
||||
&ast::Percentage(ref value) if negative_ok || value.value >= 0.
|
||||
=> 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
|
||||
}
|
||||
}
|
||||
|
@ -107,7 +108,7 @@ pub mod specified {
|
|||
=> Length::parse_dimension(value.value, unit.as_slice()).map_move(LPA_Length),
|
||||
&ast::Percentage(ref value) if negative_ok || value.value >= 0.
|
||||
=> 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),
|
||||
_ => None
|
||||
}
|
||||
|
@ -129,9 +130,11 @@ pub mod computed {
|
|||
pub use compute_CSSColor = super::super::longhands::computed_as_specified;
|
||||
use super::*;
|
||||
use super::super::longhands;
|
||||
pub use servo_util::geometry::Au;
|
||||
|
||||
pub struct Context {
|
||||
current_color: cssparser::RGBA,
|
||||
font_size: Length,
|
||||
font_size: Au,
|
||||
font_weight: longhands::font_weight::ComputedValue,
|
||||
position: longhands::position::SpecifiedValue,
|
||||
float: longhands::float::SpecifiedValue,
|
||||
|
@ -142,48 +145,44 @@ pub mod computed {
|
|||
has_border_left: bool,
|
||||
// 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 {
|
||||
specified::Au(value) => Length(value),
|
||||
specified::Em(value) => context.font_size.times(value),
|
||||
specified::Au_(value) => value,
|
||||
specified::Em(value) => mul(context.font_size, value),
|
||||
specified::Ex(value) => {
|
||||
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)]
|
||||
pub enum LengthOrPercentage {
|
||||
LP_Length(Length),
|
||||
LP_Length(Au),
|
||||
LP_Percentage(Float),
|
||||
}
|
||||
pub fn compute_LengthOrPercentage(value: specified::LengthOrPercentage, context: &Context)
|
||||
-> LengthOrPercentage {
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Clone)]
|
||||
pub enum LengthOrPercentageOrAuto {
|
||||
LPA_Length(Length),
|
||||
LPA_Length(Au),
|
||||
LPA_Percentage(Float),
|
||||
LPA_Auto,
|
||||
}
|
||||
pub fn compute_LengthOrPercentageOrAuto(value: specified::LengthOrPercentageOrAuto,
|
||||
context: &Context) -> LengthOrPercentageOrAuto {
|
||||
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_Auto => LPA_Auto,
|
||||
}
|
||||
|
|
|
@ -146,14 +146,14 @@ pub mod longhands {
|
|||
|
||||
% for side in ["top", "right", "bottom", "left"]:
|
||||
${predefined_type("margin-" + side, "LengthOrPercentageOrAuto",
|
||||
"computed::LPA_Length(computed::Length(0))")}
|
||||
"computed::LPA_Length(Au(0))")}
|
||||
% endfor
|
||||
|
||||
${new_style_struct("Padding")}
|
||||
|
||||
% for side in ["top", "right", "bottom", "left"]:
|
||||
${predefined_type("padding-" + side, "LengthOrPercentage",
|
||||
"computed::LP_Length(computed::Length(0))",
|
||||
"computed::LP_Length(Au(0))",
|
||||
"parse_non_negative")}
|
||||
% endfor
|
||||
|
||||
|
@ -187,17 +187,17 @@ pub mod longhands {
|
|||
% for side in ["top", "right", "bottom", "left"]:
|
||||
<%self:longhand name="border-${side}-width">
|
||||
pub type SpecifiedValue = specified::Length;
|
||||
pub type ComputedValue = computed::Length;
|
||||
pub type ComputedValue = Au;
|
||||
#[inline] pub fn get_initial_value() -> ComputedValue {
|
||||
computed::Length(3 * 60) // medium
|
||||
Au::from_px(3) // medium
|
||||
}
|
||||
pub fn parse(input: &[ComponentValue]) -> Option<SpecifiedValue> {
|
||||
one_component_value(input).chain(parse_border_width)
|
||||
}
|
||||
pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
|
||||
-> ComputedValue {
|
||||
if context.has_border_${side} { computed::compute_Length(value, context) }
|
||||
else { computed::Length(0) }
|
||||
if context.has_border_${side} { computed::compute_Au(value, context) }
|
||||
else { Au(0) }
|
||||
}
|
||||
</%self:longhand>
|
||||
% endfor
|
||||
|
@ -252,7 +252,7 @@ pub mod longhands {
|
|||
#[deriving(Clone)]
|
||||
pub enum ComputedValue {
|
||||
Normal,
|
||||
Length(computed::Length),
|
||||
Length(Au),
|
||||
Number(Float),
|
||||
}
|
||||
#[inline] pub fn get_initial_value() -> ComputedValue { Normal }
|
||||
|
@ -260,7 +260,7 @@ pub mod longhands {
|
|||
-> ComputedValue {
|
||||
match value {
|
||||
SpecifiedNormal => Normal,
|
||||
SpecifiedLength(value) => Length(computed::compute_Length(value, context)),
|
||||
SpecifiedLength(value) => Length(computed::compute_Au(value, context)),
|
||||
SpecifiedNumber(value) => Number(value),
|
||||
}
|
||||
}
|
||||
|
@ -295,7 +295,7 @@ pub mod longhands {
|
|||
% for keyword in vertical_align_keywords:
|
||||
${to_rust_ident(keyword)},
|
||||
% endfor
|
||||
Length(computed::Length),
|
||||
Length(Au),
|
||||
Percentage(Float),
|
||||
}
|
||||
#[inline] pub fn get_initial_value() -> ComputedValue { baseline }
|
||||
|
@ -502,11 +502,11 @@ pub mod longhands {
|
|||
</%self:single_component_value>
|
||||
|
||||
<%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 ComputedValue = computed::Length;
|
||||
pub type ComputedValue = Au;
|
||||
#[inline] pub fn get_initial_value() -> ComputedValue {
|
||||
computed::Length(16 * 60) // medium
|
||||
Au::from_px(16) // medium
|
||||
}
|
||||
/// <length> | <percentage>
|
||||
/// TODO: support <absolute-size> and <relative-size>
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
extern mod extra;
|
||||
extern mod cssparser;
|
||||
extern mod servo_util (name = "util");
|
||||
extern mod script;
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue