Skip to content

Commit

Permalink
stylo: Handle text-zoom for font-size
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Aug 2, 2017
1 parent de5f6f1 commit 16d46ea
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
5 changes: 5 additions & 0 deletions components/style/gecko/media_queries.rs
Expand Up @@ -186,6 +186,11 @@ impl Device {
pub fn default_background_color(&self) -> RGBA {
convert_nscolor_to_rgba(self.pres_context().mBackgroundColor)
}

/// Applies text zoom to a font-size or line-height value (see nsStyleFont::ZoomText).
pub fn zoom_text(&self, size: Au) -> Au {
size.scale_by(self.pres_context().mEffectiveTextZoom)
}
}

/// A expression for gecko contains a reference to the media feature, the value
Expand Down
3 changes: 0 additions & 3 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -2231,9 +2231,6 @@ fn static_assert() {
)
}

// FIXME(bholley): Gecko has two different sizes, one of which (mSize) is the
// actual computed size, and the other of which (mFont.size) is the 'display
// size' which takes font zooming into account. We don't handle font zooming yet.
pub fn set_font_size(&mut self, v: longhands::font_size::computed_value::T) {
self.gecko.mSize = v.0;
self.gecko.mScriptUnconstrainedSize = v.0;
Expand Down
16 changes: 9 additions & 7 deletions components/style/properties/longhand/font.mako.rs
Expand Up @@ -830,17 +830,17 @@ ${helpers.single_keyword_system("font-variant-caps",
value.to_computed_value(base_size.resolve(context))
}
SpecifiedValue::Length(LengthOrPercentage::Length(ref l)) => {
l.to_computed_value(context)
context.maybe_zoom_text(l.to_computed_value(context))
}
SpecifiedValue::Length(LengthOrPercentage::Percentage(pc)) => {
base_size.resolve(context).scale_by(pc.0)
}
SpecifiedValue::Length(LengthOrPercentage::Calc(ref calc)) => {
let calc = calc.to_computed_value(context);
let calc = calc.to_computed_value_zoomed(context);
calc.to_used_value(Some(base_size.resolve(context))).unwrap()
}
SpecifiedValue::Keyword(ref key, fraction) => {
key.to_computed_value(context).scale_by(fraction)
context.maybe_zoom_text(key.to_computed_value(context).scale_by(fraction))
}
SpecifiedValue::Smaller => {
FontRelativeLength::Em(1. / LARGER_FONT_SIZE_RATIO)
Expand Down Expand Up @@ -960,7 +960,7 @@ ${helpers.single_keyword_system("font-variant-caps",
context.builder.get_font().gecko().mGenericID !=
context.builder.get_parent_font().gecko().mGenericID {
if let Some((kw, ratio)) = context.builder.font_size_keyword {
computed = kw.to_computed_value(context).scale_by(ratio);
computed = context.maybe_zoom_text(kw.to_computed_value(context).scale_by(ratio));
}
}
% endif
Expand Down Expand Up @@ -990,7 +990,7 @@ ${helpers.single_keyword_system("font-variant-caps",
// changes using the font_size_keyword. We also need to do this to
// handle mathml scriptlevel changes
let kw_inherited_size = context.builder.font_size_keyword.map(|(kw, ratio)| {
SpecifiedValue::Keyword(kw, ratio).to_computed_value(context)
context.maybe_zoom_text(SpecifiedValue::Keyword(kw, ratio).to_computed_value(context))
});
let parent_kw;
let device = context.builder.device;
Expand All @@ -1015,8 +1015,10 @@ ${helpers.single_keyword_system("font-variant-caps",
pub fn cascade_initial_font_size(context: &mut Context) {
// font-size's default ("medium") does not always
// compute to the same value and depends on the font
let computed = longhands::font_size::get_initial_specified_value()
.to_computed_value(context);
let computed = context.maybe_zoom_text(
longhands::font_size::get_initial_specified_value()
.to_computed_value(context)
);
context.builder.mutate_font().set_font_size(computed);
% if product == "gecko":
let device = context.builder.device;
Expand Down
23 changes: 18 additions & 5 deletions components/style/values/computed/length.rs
Expand Up @@ -228,14 +228,14 @@ impl ToCss for CalcLengthOrPercentage {
}
}

impl ToComputedValue for specified::CalcLengthOrPercentage {
type ComputedValue = CalcLengthOrPercentage;

fn to_computed_value(&self, context: &Context) -> CalcLengthOrPercentage {
impl specified::CalcLengthOrPercentage {
/// Compute the value, zooming any absolute units by the zoom function.
fn to_computed_value_with_zoom<F>(&self, context: &Context, zoom_fn: F) -> CalcLengthOrPercentage
where F: Fn(Au) -> Au {
let mut length = Au(0);

if let Some(absolute) = self.absolute {
length += absolute;
length += zoom_fn(absolute);
}

for val in &[self.vw.map(ViewportPercentageLength::Vw),
Expand Down Expand Up @@ -263,6 +263,19 @@ impl ToComputedValue for specified::CalcLengthOrPercentage {
}
}

/// Compute font-size or line-height taking into account text-zoom if necessary.
pub fn to_computed_value_zoomed(&self, context: &Context) -> CalcLengthOrPercentage {
self.to_computed_value_with_zoom(context, |abs| context.maybe_zoom_text(abs))
}
}

impl ToComputedValue for specified::CalcLengthOrPercentage {
type ComputedValue = CalcLengthOrPercentage;

fn to_computed_value(&self, context: &Context) -> CalcLengthOrPercentage {
self.to_computed_value_with_zoom(context, |abs| abs)
}

#[inline]
fn from_computed_value(computed: &CalcLengthOrPercentage) -> Self {
specified::CalcLengthOrPercentage {
Expand Down
20 changes: 20 additions & 0 deletions components/style/values/computed/mod.rs
Expand Up @@ -132,6 +132,26 @@ impl<'a> Context<'a> {
pub fn style(&self) -> &StyleBuilder {
&self.builder
}


/// Apply text-zoom if enabled
#[cfg(feature = "gecko")]
pub fn maybe_zoom_text(&self, size: Au) -> Au {
// We disable zoom for <svg:text> by unsetting the
// -x-text-zoom property, which leads to a false value
// in mAllowZoom
if self.style().get_font().gecko.mAllowZoom {
self.device().zoom_text(size)
} else {
size
}
}

/// (Servo doesn't do text-zoom)
#[cfg(feature = "servo")]
pub fn maybe_zoom_text(&self, size: Au) -> Au {
size
}
}

/// An iterator over a slice of computed values
Expand Down

0 comments on commit 16d46ea

Please sign in to comment.