From 5de65d9f2c1ac7aa007978ec54d1d2fa5279ad19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Tue, 14 Jun 2022 08:38:11 +0000 Subject: [PATCH] style: Make viewport-relative units zoomable via "zoom text only" They're not font relative, so it we probably want them to be zoomed. Differential Revision: https://phabricator.services.mozilla.com/D148796 --- components/style/values/computed/length.rs | 19 +++++++++----- .../values/computed/length_percentage.rs | 14 +++++----- components/style/values/specified/font.rs | 26 +++++++++---------- components/style/values/specified/length.rs | 13 ++++++++++ 4 files changed, 47 insertions(+), 25 deletions(-) diff --git a/components/style/values/computed/length.rs b/components/style/values/computed/length.rs index 896001cbcd8..56a187dbbda 100644 --- a/components/style/values/computed/length.rs +++ b/components/style/values/computed/length.rs @@ -31,10 +31,22 @@ impl ToComputedValue for specified::NoCalcLength { #[inline] fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { + self.to_computed_value_with_base_size(context, FontBaseSize::CurrentStyle) + } + + #[inline] + fn from_computed_value(computed: &Self::ComputedValue) -> Self { + specified::NoCalcLength::Absolute(AbsoluteLength::Px(computed.px())) + } +} + +impl specified::NoCalcLength { + /// Computes a length with a given font-relative base size. + pub fn to_computed_value_with_base_size(&self, context: &Context, base_size: FontBaseSize) -> Length { match *self { specified::NoCalcLength::Absolute(length) => length.to_computed_value(context), specified::NoCalcLength::FontRelative(length) => { - length.to_computed_value(context, FontBaseSize::CurrentStyle) + length.to_computed_value(context, base_size) }, specified::NoCalcLength::ViewportPercentage(length) => { context @@ -47,11 +59,6 @@ impl ToComputedValue for specified::NoCalcLength { }, } } - - #[inline] - fn from_computed_value(computed: &Self::ComputedValue) -> Self { - specified::NoCalcLength::Absolute(AbsoluteLength::Px(computed.px())) - } } impl ToComputedValue for specified::Length { diff --git a/components/style/values/computed/length_percentage.rs b/components/style/values/computed/length_percentage.rs index cea2466ce6f..6ee9a07377a 100644 --- a/components/style/values/computed/length_percentage.rs +++ b/components/style/values/computed/length_percentage.rs @@ -730,14 +730,16 @@ impl specified::CalcLengthPercentage { F: Fn(Length) -> Length, { use crate::values::specified::calc::Leaf; - use crate::values::specified::length::NoCalcLength; let node = self.node.map_leaves(|leaf| match *leaf { Leaf::Percentage(p) => CalcLengthPercentageLeaf::Percentage(Percentage(p)), - Leaf::Length(l) => CalcLengthPercentageLeaf::Length(match l { - NoCalcLength::Absolute(ref abs) => zoom_fn(abs.to_computed_value(context)), - NoCalcLength::FontRelative(ref fr) => fr.to_computed_value(context, base_size), - other => other.to_computed_value(context), + Leaf::Length(l) => CalcLengthPercentageLeaf::Length({ + let result = l.to_computed_value_with_base_size(context, base_size); + if l.should_zoom_text() { + zoom_fn(result) + } else { + result + } }), Leaf::Number(..) | Leaf::Angle(..) | Leaf::Time(..) => { unreachable!("Shouldn't have parsed") @@ -755,7 +757,7 @@ impl specified::CalcLengthPercentage { ) -> LengthPercentage { self.to_computed_value_with_zoom( context, - |abs| context.maybe_zoom_text(abs.into()), + |abs| context.maybe_zoom_text(abs), base_size, ) } diff --git a/components/style/values/specified/font.rs b/components/style/values/specified/font.rs index 9701f3f5542..1ae42ac772d 100644 --- a/components/style/values/specified/font.rs +++ b/components/style/values/specified/font.rs @@ -916,21 +916,21 @@ impl FontSize { }; let mut info = KeywordInfo::none(); let size = match *self { - FontSize::Length(LengthPercentage::Length(NoCalcLength::FontRelative(value))) => { - if let FontRelativeLength::Em(em) = value { - // If the parent font was keyword-derived, this is too. - // Tack the em unit onto the factor - info = compose_keyword(em); + FontSize::Length(LengthPercentage::Length(ref l)) => { + if let NoCalcLength::FontRelative(ref value) = *l { + if let FontRelativeLength::Em(em) = *value { + // If the parent font was keyword-derived, this is + // too. Tack the em unit onto the factor + info = compose_keyword(em); + } + } + let result = l.to_computed_value_with_base_size(context, base_size); + if l.should_zoom_text() { + context.maybe_zoom_text(result) + } else { + result } - value.to_computed_value(context, base_size) }, - FontSize::Length(LengthPercentage::Length(NoCalcLength::ServoCharacterWidth( - value, - ))) => value.to_computed_value(base_size.resolve(context)), - FontSize::Length(LengthPercentage::Length(NoCalcLength::Absolute(ref l))) => { - context.maybe_zoom_text(l.to_computed_value(context)) - }, - FontSize::Length(LengthPercentage::Length(ref l)) => l.to_computed_value(context), FontSize::Length(LengthPercentage::Percentage(pc)) => { // If the parent font was keyword-derived, this is too. // Tack the % onto the factor diff --git a/components/style/values/specified/length.rs b/components/style/values/specified/length.rs index 159eb996364..237e59e21be 100644 --- a/components/style/values/specified/length.rs +++ b/components/style/values/specified/length.rs @@ -790,6 +790,19 @@ impl NoCalcLength { } } + /// Whether text-only zoom should be applied to this length. + /// + /// Generally, font-dependent/relative units don't get text-only-zoomed, + /// because the font they're relative to should be zoomed already. + pub fn should_zoom_text(&self) -> bool { + match *self { + Self::Absolute(..) | + Self::ViewportPercentage(..) => true, + Self::ServoCharacterWidth(..) | + Self::FontRelative(..) => false, + } + } + /// Parse a given absolute or relative dimension. pub fn parse_dimension( context: &ParserContext,