Bug 1374233 - Part 3: Use NonNegativeAu as computed values for font-size related properties.

For font-size and font-size-adjust.

MozReview-Commit-ID: 5rrfVSzB7WF
This commit is contained in:
Boris Chiou 2017-07-21 15:38:55 +08:00
parent 191c2a282b
commit 234d2c1b32
13 changed files with 87 additions and 57 deletions

View file

@ -62,7 +62,7 @@ use std::mem::{forget, uninitialized, transmute, zeroed};
use std::{cmp, ops, ptr};
use stylesheets::{MallocSizeOfWithRepeats, SizeOfState};
use values::{self, Auto, CustomIdent, Either, KeyframesName};
use values::computed::ToComputedValue;
use values::computed::{NonNegativeAu, ToComputedValue};
use values::computed::effects::{BoxShadow, Filter, SimpleShadow};
use values::computed::length::Percentage;
use computed_values::border_style;
@ -2259,15 +2259,15 @@ fn static_assert() {
}
pub fn set_font_size(&mut self, v: longhands::font_size::computed_value::T) {
self.gecko.mSize = v.0;
self.gecko.mScriptUnconstrainedSize = v.0;
self.gecko.mSize = v.value();
self.gecko.mScriptUnconstrainedSize = v.value();
}
/// Set font size, taking into account scriptminsize and scriptlevel
/// Returns Some(size) if we have to recompute the script unconstrained size
pub fn apply_font_size(&mut self, v: longhands::font_size::computed_value::T,
parent: &Self,
device: &Device) -> Option<Au> {
device: &Device) -> Option<NonNegativeAu> {
let (adjusted_size, adjusted_unconstrained_size) =
self.calculate_script_level_size(parent, device);
// In this case, we have been unaffected by scriptminsize, ignore it
@ -2277,9 +2277,9 @@ fn static_assert() {
self.fixup_font_min_size(device);
None
} else {
self.gecko.mSize = v.0;
self.gecko.mSize = v.value();
self.fixup_font_min_size(device);
Some(Au(parent.gecko.mScriptUnconstrainedSize))
Some(Au(parent.gecko.mScriptUnconstrainedSize).into())
}
}
@ -2287,8 +2287,8 @@ fn static_assert() {
unsafe { bindings::Gecko_nsStyleFont_FixupMinFontSize(&mut self.gecko, device.pres_context()) }
}
pub fn apply_unconstrained_font_size(&mut self, v: Au) {
self.gecko.mScriptUnconstrainedSize = v.0;
pub fn apply_unconstrained_font_size(&mut self, v: NonNegativeAu) {
self.gecko.mScriptUnconstrainedSize = v.value();
}
/// Calculates the constrained and unconstrained font sizes to be inherited
@ -2398,7 +2398,7 @@ fn static_assert() {
///
/// Returns true if the inherited keyword size was actually used
pub fn inherit_font_size_from(&mut self, parent: &Self,
kw_inherited_size: Option<Au>,
kw_inherited_size: Option<NonNegativeAu>,
device: &Device) -> bool {
let (adjusted_size, adjusted_unconstrained_size)
= self.calculate_script_level_size(parent, device);
@ -2424,9 +2424,9 @@ fn static_assert() {
false
} else if let Some(size) = kw_inherited_size {
// Parent element was a keyword-derived size.
self.gecko.mSize = size.0;
self.gecko.mSize = size.value();
// MathML constraints didn't apply here, so we can ignore this.
self.gecko.mScriptUnconstrainedSize = size.0;
self.gecko.mScriptUnconstrainedSize = size.value();
self.fixup_font_min_size(device);
true
} else {
@ -2440,7 +2440,7 @@ fn static_assert() {
}
pub fn clone_font_size(&self) -> longhands::font_size::computed_value::T {
Au(self.gecko.mSize)
Au(self.gecko.mSize).into()
}
pub fn set_font_weight(&mut self, v: longhands::font_weight::computed_value::T) {

View file

@ -566,7 +566,7 @@ ${helpers.single_keyword_system("font-variant-caps",
}
</%helpers:longhand>
<%helpers:longhand name="font-size" need_clone="True" animation_value_type="ComputedValue"
<%helpers:longhand name="font-size" need_clone="True" animation_value_type="NonNegativeAu"
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER"
allow_quirks="True" spec="https://drafts.csswg.org/css-fonts/#propdef-font-size">
use app_units::Au;
@ -574,6 +574,7 @@ ${helpers.single_keyword_system("font-variant-caps",
use std::fmt;
use style_traits::{HasViewportPercentage, ToCss};
use values::FONT_MEDIUM_PX;
use values::computed::NonNegativeAu;
use values::specified::{AllowQuirks, FontRelativeLength, LengthOrPercentage, NoCalcLength};
use values::specified::length::FontBaseSize;
@ -621,8 +622,8 @@ ${helpers.single_keyword_system("font-variant-caps",
}
pub mod computed_value {
use app_units::Au;
pub type T = Au;
use values::computed::NonNegativeAu;
pub type T = NonNegativeAu;
}
/// CSS font keywords
@ -697,7 +698,7 @@ ${helpers.single_keyword_system("font-variant-caps",
% if product == "servo":
impl ToComputedValue for KeywordSize {
type ComputedValue = Au;
type ComputedValue = NonNegativeAu;
#[inline]
fn to_computed_value(&self, _: &Context) -> computed_value::T {
// https://drafts.csswg.org/css-fonts-3/#font-size-prop
@ -711,7 +712,7 @@ ${helpers.single_keyword_system("font-variant-caps",
XLarge => Au::from_px(FONT_MEDIUM_PX) * 3 / 2,
XXLarge => Au::from_px(FONT_MEDIUM_PX) * 2,
XXXLarge => Au::from_px(FONT_MEDIUM_PX) * 3,
}
}.into()
}
#[inline]
@ -721,7 +722,7 @@ ${helpers.single_keyword_system("font-variant-caps",
}
% else:
impl ToComputedValue for KeywordSize {
type ComputedValue = Au;
type ComputedValue = NonNegativeAu;
#[inline]
fn to_computed_value(&self, cx: &Context) -> computed_value::T {
use gecko_bindings::structs::nsIAtom;
@ -757,9 +758,9 @@ ${helpers.single_keyword_system("font-variant-caps",
let base_size_px = au_to_int_px(base_size as f32);
let html_size = self.html_size() as usize;
if base_size_px >= 9 && base_size_px <= 16 {
Au::from_px(FONT_SIZE_MAPPING[(base_size_px - 9) as usize][html_size])
NonNegativeAu::from_px(FONT_SIZE_MAPPING[(base_size_px - 9) as usize][html_size])
} else {
Au(FONT_SIZE_FACTORS[html_size] * base_size / 100)
Au(FONT_SIZE_FACTORS[html_size] * base_size / 100).into()
}
}
@ -818,37 +819,38 @@ ${helpers.single_keyword_system("font-variant-caps",
}
/// Compute it against a given base font size
pub fn to_computed_value_against(&self, context: &Context, base_size: FontBaseSize) -> Au {
pub fn to_computed_value_against(&self, context: &Context, base_size: FontBaseSize)
-> NonNegativeAu {
use values::specified::length::FontRelativeLength;
match *self {
SpecifiedValue::Length(LengthOrPercentage::Length(
NoCalcLength::FontRelative(value))) => {
value.to_computed_value(context, base_size)
value.to_computed_value(context, base_size).into()
}
SpecifiedValue::Length(LengthOrPercentage::Length(
NoCalcLength::ServoCharacterWidth(value))) => {
value.to_computed_value(base_size.resolve(context))
value.to_computed_value(base_size.resolve(context)).into()
}
SpecifiedValue::Length(LengthOrPercentage::Length(ref l)) => {
context.maybe_zoom_text(l.to_computed_value(context))
context.maybe_zoom_text(l.to_computed_value(context).into())
}
SpecifiedValue::Length(LengthOrPercentage::Percentage(pc)) => {
base_size.resolve(context).scale_by(pc.0)
base_size.resolve(context).scale_by(pc.0).into()
}
SpecifiedValue::Length(LengthOrPercentage::Calc(ref calc)) => {
let calc = calc.to_computed_value_zoomed(context);
calc.to_used_value(Some(base_size.resolve(context))).unwrap()
calc.to_used_value(Some(base_size.resolve(context))).unwrap().into()
}
SpecifiedValue::Keyword(ref key, fraction) => {
context.maybe_zoom_text(key.to_computed_value(context).scale_by(fraction))
}
SpecifiedValue::Smaller => {
FontRelativeLength::Em(1. / LARGER_FONT_SIZE_RATIO)
.to_computed_value(context, base_size)
.to_computed_value(context, base_size).into()
}
SpecifiedValue::Larger => {
FontRelativeLength::Em(LARGER_FONT_SIZE_RATIO)
.to_computed_value(context, base_size)
.to_computed_value(context, base_size).into()
}
SpecifiedValue::System(_) => {
@ -863,7 +865,7 @@ ${helpers.single_keyword_system("font-variant-caps",
#[inline]
#[allow(missing_docs)]
pub fn get_initial_value() -> computed_value::T {
Au::from_px(FONT_MEDIUM_PX)
NonNegativeAu::from_px(FONT_MEDIUM_PX)
}
#[inline]
@ -883,7 +885,7 @@ ${helpers.single_keyword_system("font-variant-caps",
#[inline]
fn from_computed_value(computed: &computed_value::T) -> Self {
SpecifiedValue::Length(LengthOrPercentage::Length(
ToComputedValue::from_computed_value(computed)
ToComputedValue::from_computed_value(&computed.0)
))
}
}
@ -930,7 +932,7 @@ ${helpers.single_keyword_system("font-variant-caps",
#[allow(unused_mut)]
pub fn cascade_specified_font_size(context: &mut Context,
specified_value: &SpecifiedValue,
mut computed: Au) {
mut computed: NonNegativeAu) {
if let SpecifiedValue::Keyword(kw, fraction) = *specified_value {
context.builder.font_size_keyword = Some((kw, fraction));
} else if let Some(ratio) = specified_value.as_font_ratio() {
@ -976,7 +978,7 @@ ${helpers.single_keyword_system("font-variant-caps",
if let Some(parent) = parent_unconstrained {
let new_unconstrained =
specified_value
.to_computed_value_against(context, FontBaseSize::Custom(parent));
.to_computed_value_against(context, FontBaseSize::Custom(parent.0));
context.builder
.mutate_font()
.apply_unconstrained_font_size(new_unconstrained);
@ -1028,7 +1030,8 @@ ${helpers.single_keyword_system("font-variant-caps",
}
</%helpers:longhand>
<%helpers:longhand products="gecko" name="font-size-adjust" animation_value_type="ComputedValue"
<%helpers:longhand products="gecko" name="font-size-adjust"
animation_value_type="longhands::font_size_adjust::computed_value::T"
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER"
spec="https://drafts.csswg.org/css-fonts/#propdef-font-size-adjust">
use properties::longhands::system_font::SystemFont;
@ -1082,7 +1085,7 @@ ${helpers.single_keyword_system("font-variant-caps",
pub mod computed_value {
use properties::animated_properties::Animatable;
use values::CSSFloat;
use values::animated::ToAnimatedZero;
use values::animated::{ToAnimatedValue, ToAnimatedZero};
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Copy, Clone, Debug, PartialEq, ToCss)]
@ -1125,6 +1128,23 @@ ${helpers.single_keyword_system("font-variant-caps",
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> { Err(()) }
}
impl ToAnimatedValue for T {
type AnimatedValue = Self;
#[inline]
fn to_animated_value(self) -> Self {
self
}
#[inline]
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
match animated {
T::Number(number) => T::Number(number.max(0.)),
_ => animated
}
}
}
}
#[inline]
@ -2514,7 +2534,7 @@ ${helpers.single_keyword("-moz-math-variant",
let weight = longhands::font_weight::computed_value::T::from_gecko_weight(system.weight);
let ret = ComputedSystemFont {
font_family: longhands::font_family::computed_value::T(family),
font_size: Au(system.size),
font_size: Au(system.size).into(),
font_weight: weight,
font_size_adjust: longhands::font_size_adjust::computed_value
::T::from_gecko_adjust(system.sizeAdjust),

View file

@ -1648,12 +1648,12 @@ pub use gecko_properties::style_structs;
/// The module where all the style structs are defined.
#[cfg(feature = "servo")]
pub mod style_structs {
use app_units::Au;
use fnv::FnvHasher;
use super::longhands;
use std::hash::{Hash, Hasher};
use logical_geometry::WritingMode;
use media_queries::Device;
use values::computed::NonNegativeAu;
% for style_struct in data.active_style_structs():
% if style_struct.name == "Font":
@ -1771,7 +1771,8 @@ pub mod style_structs {
/// (Servo does not handle MathML, so this just calls copy_font_size_from)
pub fn inherit_font_size_from(&mut self, parent: &Self,
_: Option<Au>, _: &Device) -> bool {
_: Option<NonNegativeAu>,
_: &Device) -> bool {
self.copy_font_size_from(parent);
false
}
@ -1779,12 +1780,12 @@ pub mod style_structs {
pub fn apply_font_size(&mut self,
v: longhands::font_size::computed_value::T,
_: &Self,
_: &Device) -> Option<Au> {
_: &Device) -> Option<NonNegativeAu> {
self.set_font_size(v);
None
}
/// (Servo does not handle MathML, so this does nothing)
pub fn apply_unconstrained_font_size(&mut self, _: Au) {
pub fn apply_unconstrained_font_size(&mut self, _: NonNegativeAu) {
}
% elif style_struct.name == "Outline":