diff options
-rw-r--r-- | components/style/gecko_bindings/bindings.rs | 4 | ||||
-rw-r--r-- | components/style/values/computed/length.rs | 5 | ||||
-rw-r--r-- | components/style/values/specified/length.rs | 57 | ||||
-rw-r--r-- | ports/geckolib/glue.rs | 4 |
4 files changed, 67 insertions, 3 deletions
diff --git a/components/style/gecko_bindings/bindings.rs b/components/style/gecko_bindings/bindings.rs index 1d5a0909913..36075707b5b 100644 --- a/components/style/gecko_bindings/bindings.rs +++ b/components/style/gecko_bindings/bindings.rs @@ -1075,6 +1075,10 @@ extern "C" { -> nscoord; } extern "C" { + pub fn Gecko_GetAppUnitsPerPhysicalInch(pres_context: RawGeckoPresContextBorrowed) + -> i32; +} +extern "C" { pub fn Gecko_CSSValue_GetKeyword(aCSSValue: nsCSSValueBorrowed) -> nsCSSKeyword; } diff --git a/components/style/values/computed/length.rs b/components/style/values/computed/length.rs index d636390c021..0312c601df9 100644 --- a/components/style/values/computed/length.rs +++ b/components/style/values/computed/length.rs @@ -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), } } diff --git a/components/style/values/specified/length.rs b/components/style/values/specified/length.rs index d3ebba44726..7e41fa80585 100644 --- a/components/style/values/specified/length.rs +++ b/components/style/values/specified/length.rs @@ -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 @@ -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 { @@ -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), } } } @@ -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), } } } @@ -438,6 +491,8 @@ impl NoCalcLength { } Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vmax(value))) }, + #[cfg(feature = "gecko")] + "mozmm" => Ok(NoCalcLength::Physical(PhysicalLength(value))), _ => Err(()) } } @@ -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 } } diff --git a/ports/geckolib/glue.rs b/ports/geckolib/glue.rs index 6940c4346dd..92ca8b8b68c 100644 --- a/ports/geckolib/glue.rs +++ b/ports/geckolib/glue.rs @@ -1541,7 +1541,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); @@ -1552,6 +1552,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)), @@ -2222,4 +2223,3 @@ pub extern "C" fn Servo_StyleSet_ResolveForDeclarations(raw_data: RawServoStyleS parent_style, declarations.clone()).into_strong() } - |