Skip to content

Commit

Permalink
Factor out CSS length to Au conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
sanxiyn committed Aug 2, 2013
1 parent 1d04d5f commit 018e220
Showing 1 changed file with 20 additions and 47 deletions.
67 changes: 20 additions & 47 deletions src/components/main/layout/model.rs
Expand Up @@ -8,7 +8,7 @@ use std::num::Zero;
use geom::side_offsets::SideOffsets2D;
use gfx::geometry::Au;
use newcss::complete::CompleteStyle;
use newcss::units::{Em, Pt, Px};
use newcss::units::{Length, Em, Pt, Px};
use newcss::values::{CSSBorderWidth, CSSBorderWidthLength, CSSBorderWidthMedium};
use newcss::values::{CSSBorderWidthThick, CSSBorderWidthThin, CSSFontSize, CSSFontSizeLength};
use newcss::values::{CSSWidth, CSSWidthLength, CSSWidthPercentage, CSSWidthAuto};
Expand All @@ -24,6 +24,20 @@ pub struct BoxModel {
content_box_width: Au,
}

fn from_length(length: Length, font_size: CSSFontSize) -> Au {
match length {
Px(v) => Au::from_frac_px(v),
Pt(v) => Au::from_pt(v),
Em(em) => {
match font_size {
CSSFontSizeLength(Px(v)) => Au::from_frac_px(em * v),
CSSFontSizeLength(Pt(v)) => Au::from_pt(em * v),
_ => fail!("expected non-relative font size")
}
}
}
}

/// Useful helper data type when computing values for blocks and positioned elements.
pub enum MaybeAuto {
Auto,
Expand All @@ -35,48 +49,23 @@ impl MaybeAuto {
match margin {
CSSMarginAuto => Auto,
CSSMarginPercentage(percent) => Specified(containing_width.scale_by(percent/100.0)),
CSSMarginLength(Px(v)) => Specified(Au::from_frac_px(v)),
CSSMarginLength(Pt(v)) => Specified(Au::from_pt(v)),
CSSMarginLength(Em(em)) => {
match font_size {
CSSFontSizeLength(Px(v)) => Specified(Au::from_frac_px(em * v)),
CSSFontSizeLength(Pt(v)) => Specified(Au::from_pt(em * v)),
_ => fail!(~"expected non-relative font size"),
}
}
CSSMarginLength(length) => Specified(from_length(length, font_size))
}
}

pub fn from_width(width: CSSWidth, containing_width: Au, font_size: CSSFontSize) -> MaybeAuto {
match width {
CSSWidthAuto => Auto,
CSSWidthPercentage(percent) => Specified(containing_width.scale_by(percent/100.0)),
CSSWidthLength(Px(v)) => Specified(Au::from_frac_px(v)),
CSSWidthLength(Pt(v)) => Specified(Au::from_pt(v)),
CSSWidthLength(Em(em)) => {
match font_size {
CSSFontSizeLength(Px(v)) => Specified(Au::from_frac_px(em * v)),
CSSFontSizeLength(Pt(v)) => Specified(Au::from_pt(em * v)),
_ => fail!(~"expected non-relative font size"),
}
}
CSSWidthLength(length) => Specified(from_length(length, font_size))
}
}

pub fn from_height(height: CSSHeight, cb_height: Au, font_size: CSSFontSize) -> MaybeAuto {
match height {
CSSHeightAuto => Auto,
CSSHeightPercentage(percent) => Specified(cb_height.scale_by(percent/100.0)),
CSSHeightLength(Px(v)) => Specified(Au::from_frac_px(v)),
CSSHeightLength(Pt(v)) => Specified(Au::from_pt(v)),
CSSHeightLength(Em(em)) => {
match font_size {
CSSFontSizeLength(Px(v)) => Specified(Au::from_frac_px(em * v)),
CSSFontSizeLength(Pt(v)) => Specified(Au::from_pt(em * v)),
_ => fail!(~"expected non-relative font size"),
}
}

CSSHeightLength(length) => Specified(from_length(length, font_size))
}
}

Expand Down Expand Up @@ -137,15 +126,7 @@ impl BoxModel {
/// Helper function to compute the border width in app units from the CSS border width.
pub fn compute_border_width(&self, width: CSSBorderWidth, font_size: CSSFontSize) -> Au {
match width {
CSSBorderWidthLength(Px(v)) => Au::from_frac_px(v),
CSSBorderWidthLength(Pt(v)) => Au::from_pt(v),
CSSBorderWidthLength(Em(em)) => {
match font_size {
CSSFontSizeLength(Px(v)) => Au::from_frac_px(em * v),
CSSFontSizeLength(Pt(v)) => Au::from_pt(em * v),
_ => fail!(~"expected non-relative font size"),
}
},
CSSBorderWidthLength(length) => from_length(length, font_size),
CSSBorderWidthThin => Au::from_px(1),
CSSBorderWidthMedium => Au::from_px(5),
CSSBorderWidthThick => Au::from_px(10),
Expand All @@ -154,15 +135,7 @@ impl BoxModel {

pub fn compute_padding_length(&self, padding: CSSPadding, content_box_width: Au, font_size: CSSFontSize) -> Au {
match padding {
CSSPaddingLength(Px(v)) => Au::from_frac_px(v),
CSSPaddingLength(Pt(v)) => Au::from_pt(v),
CSSPaddingLength(Em(em)) => {
match font_size {
CSSFontSizeLength(Px(v)) => Au::from_frac_px(em * v),
CSSFontSizeLength(Pt(v)) => Au::from_pt(em * v),
_ => fail!(~"expected non-relative font size"),
}
},
CSSPaddingLength(length) => from_length(length, font_size),
CSSPaddingPercentage(p) => content_box_width.scale_by(p/100.0)
}
}
Expand Down

5 comments on commit 018e220

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

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

saw approval from metajack
at sanxiyn@018e220

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

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

merging sanxiyn/servo/from-length = 018e220 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

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

sanxiyn/servo/from-length = 018e220 merged ok, testing candidate = 2239436

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

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

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

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

fast-forwarding master to auto = 2239436

Please sign in to comment.