Skip to content

Commit

Permalink
Make style system support mozmm unit and compute it correctly.
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: hCUs8xuNd1
  • Loading branch information
kuoe0 committed May 2, 2017
1 parent fc1e4e9 commit 507c90d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
5 changes: 4 additions & 1 deletion components/style/values/computed/length.rs
Expand Up @@ -29,7 +29,10 @@ impl ToComputedValue for specified::NoCalcLength {
specified::NoCalcLength::ViewportPercentage(length) =>
length.to_computed_value(context.viewport_size()),
specified::NoCalcLength::ServoCharacterWidth(length) =>
length.to_computed_value(context.style().get_font().clone_font_size())
length.to_computed_value(context.style().get_font().clone_font_size()),
#[cfg(feature = "gecko")]
specified::NoCalcLength::Physical(length) =>
length.to_computed_value(context),
}
}

Expand Down
57 changes: 57 additions & 0 deletions components/style/values/specified/length.rs
Expand Up @@ -333,6 +333,51 @@ impl Mul<CSSFloat> for AbsoluteLength {
}
}

/// Represents a physical length (mozmm) based on DPI
#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg(feature = "gecko")]
pub struct PhysicalLength(pub CSSFloat);

#[cfg(feature = "gecko")]
impl PhysicalLength {
fn is_zero(&self) -> bool {
self.0 == 0.
}

/// Computes the given character width.
pub fn to_computed_value(&self, context: &Context) -> Au {
use gecko_bindings::bindings;
// Same as Gecko
const MM_PER_INCH: f32 = 25.4;

let physical_inch = unsafe {
let pres_context = &*context.device.pres_context;
bindings::Gecko_GetAppUnitsPerPhysicalInch(&pres_context)
};

let inch = self.0 / MM_PER_INCH;

to_au_round(inch, physical_inch as f32)
}
}

#[cfg(feature = "gecko")]
impl ToCss for PhysicalLength {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
write!(dest, "{}mozmm", self.0)
}
}

#[cfg(feature = "gecko")]
impl Mul<CSSFloat> for PhysicalLength {
type Output = PhysicalLength;

#[inline]
fn mul(self, scalar: CSSFloat) -> PhysicalLength {
PhysicalLength(self.0 * scalar)
}
}

/// A `<length>` without taking `calc` expressions into account
///
/// https://drafts.csswg.org/css-values/#lengths
Expand All @@ -359,6 +404,10 @@ pub enum NoCalcLength {
/// This cannot be specified by the user directly and is only generated by
/// `Stylist::synthesize_rules_for_legacy_attributes()`.
ServoCharacterWidth(CharacterWidth),

/// A physical length (mozmm) based on DPI
#[cfg(feature = "gecko")]
Physical(PhysicalLength),
}

impl HasViewportPercentage for NoCalcLength {
Expand All @@ -378,6 +427,8 @@ impl ToCss for NoCalcLength {
NoCalcLength::ViewportPercentage(length) => length.to_css(dest),
/* This should only be reached from style dumping code */
NoCalcLength::ServoCharacterWidth(CharacterWidth(i)) => write!(dest, "CharWidth({})", i),
#[cfg(feature = "gecko")]
NoCalcLength::Physical(length) => length.to_css(dest),
}
}
}
Expand All @@ -392,6 +443,8 @@ impl Mul<CSSFloat> for NoCalcLength {
NoCalcLength::FontRelative(v) => NoCalcLength::FontRelative(v * scalar),
NoCalcLength::ViewportPercentage(v) => NoCalcLength::ViewportPercentage(v * scalar),
NoCalcLength::ServoCharacterWidth(_) => panic!("Can't multiply ServoCharacterWidth!"),
#[cfg(feature = "gecko")]
NoCalcLength::Physical(v) => NoCalcLength::Physical(v * scalar),
}
}
}
Expand Down Expand Up @@ -438,6 +491,8 @@ impl NoCalcLength {
}
Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vmax(value)))
},
#[cfg(feature = "gecko")]
"mozmm" => Ok(NoCalcLength::Physical(PhysicalLength(value))),
_ => Err(())
}
}
Expand All @@ -453,6 +508,8 @@ impl NoCalcLength {
pub fn is_zero(&self) -> bool {
match *self {
NoCalcLength::Absolute(length) => length.is_zero(),
#[cfg(feature = "gecko")]
NoCalcLength::Physical(length) => length.is_zero(),
_ => false
}
}
Expand Down
4 changes: 2 additions & 2 deletions ports/geckolib/glue.rs
Expand Up @@ -1539,7 +1539,7 @@ pub extern "C" fn Servo_DeclarationBlock_SetLengthValue(declarations:
unit: structs::nsCSSUnit) {
use style::properties::{PropertyDeclaration, LonghandId};
use style::properties::longhands::_moz_script_min_size::SpecifiedValue as MozScriptMinSize;
use style::values::specified::length::{AbsoluteLength, FontRelativeLength};
use style::values::specified::length::{AbsoluteLength, FontRelativeLength, PhysicalLength};
use style::values::specified::length::{LengthOrPercentage, NoCalcLength};

let long = get_longhand_from_id!(property);
Expand All @@ -1550,6 +1550,7 @@ pub extern "C" fn Servo_DeclarationBlock_SetLengthValue(declarations:
structs::nsCSSUnit::eCSSUnit_Inch => NoCalcLength::Absolute(AbsoluteLength::In(value)),
structs::nsCSSUnit::eCSSUnit_Centimeter => NoCalcLength::Absolute(AbsoluteLength::Cm(value)),
structs::nsCSSUnit::eCSSUnit_Millimeter => NoCalcLength::Absolute(AbsoluteLength::Mm(value)),
structs::nsCSSUnit::eCSSUnit_PhysicalMillimeter => NoCalcLength::Physical(PhysicalLength(value)),
structs::nsCSSUnit::eCSSUnit_Point => NoCalcLength::Absolute(AbsoluteLength::Pt(value)),
structs::nsCSSUnit::eCSSUnit_Pica => NoCalcLength::Absolute(AbsoluteLength::Pc(value)),
structs::nsCSSUnit::eCSSUnit_Quarter => NoCalcLength::Absolute(AbsoluteLength::Q(value)),
Expand Down Expand Up @@ -2220,4 +2221,3 @@ pub extern "C" fn Servo_StyleSet_ResolveForDeclarations(raw_data: RawServoStyleS
parent_style,
declarations.clone()).into_strong()
}

0 comments on commit 507c90d

Please sign in to comment.