mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Introduce CSSPixelLength and update NonNegativeLength.
First, we define computed::CSSPixelLength which contains a CSSFloat, a pixel value, and then we replace computed::Length with CSSPixelLength. Therefore, the |ComputedValue| of NoCalcLength, AbsoluteLength, FontRelativeLength, ViewportPercentageLength, CharacterWidth, and PhysicalLength is CSSPixelLength. Besides, we drop NonNegativeAu, and replace computed::NonNegativeLength with NonNegative<computed::Length>. (i.e. NonNegative<CSSPixelLength>)
This commit is contained in:
parent
cad3aff508
commit
a949e2a057
40 changed files with 502 additions and 406 deletions
|
@ -19,7 +19,7 @@ use values::computed::ComputedUrl;
|
|||
use values::computed::GreaterThanOrEqualToOneNumber as ComputedGreaterThanOrEqualToOneNumber;
|
||||
use values::computed::MaxLength as ComputedMaxLength;
|
||||
use values::computed::MozLength as ComputedMozLength;
|
||||
use values::computed::NonNegativeAu;
|
||||
use values::computed::NonNegativeLength as ComputedNonNegativeLength;
|
||||
use values::computed::NonNegativeLengthOrPercentage as ComputedNonNegativeLengthOrPercentage;
|
||||
use values::computed::NonNegativeNumber as ComputedNonNegativeNumber;
|
||||
use values::computed::PositiveInteger as ComputedPositiveInteger;
|
||||
|
@ -296,7 +296,7 @@ impl ToAnimatedValue for ComputedGreaterThanOrEqualToOneNumber {
|
|||
}
|
||||
}
|
||||
|
||||
impl ToAnimatedValue for NonNegativeAu {
|
||||
impl ToAnimatedValue for ComputedNonNegativeLength {
|
||||
type AnimatedValue = Self;
|
||||
|
||||
#[inline]
|
||||
|
@ -306,7 +306,7 @@ impl ToAnimatedValue for NonNegativeAu {
|
|||
|
||||
#[inline]
|
||||
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
|
||||
max(animated.0, Au(0)).into()
|
||||
ComputedNonNegativeLength::new(animated.px().max(0.))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,12 +7,13 @@
|
|||
use app_units::{Au, AU_PER_PX};
|
||||
use ordered_float::NotNaN;
|
||||
use std::fmt;
|
||||
use std::ops::Add;
|
||||
use style_traits::ToCss;
|
||||
use style_traits::values::specified::AllowedLengthType;
|
||||
use super::{Number, ToComputedValue, Context, Percentage};
|
||||
use values::{Auto, CSSFloat, Either, ExtremumLength, None_, Normal, specified};
|
||||
use values::animated::{Animate, Procedure, ToAnimatedZero};
|
||||
use values::computed::{NonNegativeAu, NonNegativeNumber};
|
||||
use values::computed::NonNegativeNumber;
|
||||
use values::distance::{ComputeSquaredDistance, SquaredDistance};
|
||||
use values::generics::NonNegative;
|
||||
use values::specified::length::{AbsoluteLength, FontBaseSize, FontRelativeLength};
|
||||
|
@ -22,10 +23,10 @@ pub use super::image::Image;
|
|||
pub use values::specified::{Angle, BorderStyle, Time, UrlOrNone};
|
||||
|
||||
impl ToComputedValue for specified::NoCalcLength {
|
||||
type ComputedValue = Au;
|
||||
type ComputedValue = CSSPixelLength;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> Au {
|
||||
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
|
||||
match *self {
|
||||
specified::NoCalcLength::Absolute(length) =>
|
||||
length.to_computed_value(context),
|
||||
|
@ -34,7 +35,7 @@ impl ToComputedValue for specified::NoCalcLength {
|
|||
specified::NoCalcLength::ViewportPercentage(length) =>
|
||||
length.to_computed_value(context.viewport_size_for_viewport_unit_resolution()),
|
||||
specified::NoCalcLength::ServoCharacterWidth(length) =>
|
||||
length.to_computed_value(context.style().get_font().clone_font_size().0),
|
||||
length.to_computed_value(Au::from(context.style().get_font().clone_font_size())),
|
||||
#[cfg(feature = "gecko")]
|
||||
specified::NoCalcLength::Physical(length) =>
|
||||
length.to_computed_value(context),
|
||||
|
@ -42,24 +43,24 @@ impl ToComputedValue for specified::NoCalcLength {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Au) -> Self {
|
||||
specified::NoCalcLength::Absolute(AbsoluteLength::Px(computed.to_f32_px()))
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
specified::NoCalcLength::Absolute(AbsoluteLength::Px(computed.px()))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToComputedValue for specified::Length {
|
||||
type ComputedValue = Au;
|
||||
type ComputedValue = CSSPixelLength;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> Au {
|
||||
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
|
||||
match *self {
|
||||
specified::Length::NoCalc(l) => l.to_computed_value(context),
|
||||
specified::Length::Calc(ref calc) => calc.to_computed_value(context).length(),
|
||||
specified::Length::Calc(ref calc) => calc.to_computed_value(context).length().into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Au) -> Self {
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
specified::Length::NoCalc(specified::NoCalcLength::from_computed_value(computed))
|
||||
}
|
||||
}
|
||||
|
@ -229,7 +230,7 @@ impl specified::CalcLengthOrPercentage {
|
|||
let mut length = Au(0);
|
||||
|
||||
if let Some(absolute) = self.absolute {
|
||||
length += zoom_fn(absolute.to_computed_value(context));
|
||||
length += zoom_fn(Au::from(absolute.to_computed_value(context)));
|
||||
}
|
||||
|
||||
for val in &[self.vw.map(ViewportPercentageLength::Vw),
|
||||
|
@ -237,7 +238,8 @@ impl specified::CalcLengthOrPercentage {
|
|||
self.vmin.map(ViewportPercentageLength::Vmin),
|
||||
self.vmax.map(ViewportPercentageLength::Vmax)] {
|
||||
if let Some(val) = *val {
|
||||
length += val.to_computed_value(context.viewport_size_for_viewport_unit_resolution());
|
||||
let viewport_size = context.viewport_size_for_viewport_unit_resolution();
|
||||
length += Au::from(val.to_computed_value(viewport_size));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -246,7 +248,7 @@ impl specified::CalcLengthOrPercentage {
|
|||
self.ex.map(FontRelativeLength::Ex),
|
||||
self.rem.map(FontRelativeLength::Rem)] {
|
||||
if let Some(val) = *val {
|
||||
length += val.to_computed_value(context, base_size);
|
||||
length += Au::from(val.to_computed_value(context, base_size));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -259,7 +261,7 @@ impl specified::CalcLengthOrPercentage {
|
|||
|
||||
/// Compute font-size or line-height taking into account text-zoom if necessary.
|
||||
pub fn to_computed_value_zoomed(&self, context: &Context, base_size: FontBaseSize) -> CalcLengthOrPercentage {
|
||||
self.to_computed_value_with_zoom(context, |abs| context.maybe_zoom_text(abs.into()).0, base_size)
|
||||
self.to_computed_value_with_zoom(context, |abs| Au::from(context.maybe_zoom_text(abs.into())), base_size)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -275,7 +277,7 @@ impl ToComputedValue for specified::CalcLengthOrPercentage {
|
|||
fn from_computed_value(computed: &CalcLengthOrPercentage) -> Self {
|
||||
specified::CalcLengthOrPercentage {
|
||||
clamping_mode: computed.clamping_mode,
|
||||
absolute: Some(AbsoluteLength::from_computed_value(&computed.length)),
|
||||
absolute: Some(AbsoluteLength::from_computed_value(&computed.length.into())),
|
||||
percentage: computed.percentage,
|
||||
..Default::default()
|
||||
}
|
||||
|
@ -402,7 +404,7 @@ impl ToComputedValue for specified::LengthOrPercentage {
|
|||
fn to_computed_value(&self, context: &Context) -> LengthOrPercentage {
|
||||
match *self {
|
||||
specified::LengthOrPercentage::Length(ref value) => {
|
||||
LengthOrPercentage::Length(value.to_computed_value(context))
|
||||
LengthOrPercentage::Length(Au::from(value.to_computed_value(context)))
|
||||
}
|
||||
specified::LengthOrPercentage::Percentage(value) => {
|
||||
LengthOrPercentage::Percentage(value)
|
||||
|
@ -417,7 +419,7 @@ impl ToComputedValue for specified::LengthOrPercentage {
|
|||
match *computed {
|
||||
LengthOrPercentage::Length(value) => {
|
||||
specified::LengthOrPercentage::Length(
|
||||
ToComputedValue::from_computed_value(&value)
|
||||
ToComputedValue::from_computed_value(&value.into())
|
||||
)
|
||||
}
|
||||
LengthOrPercentage::Percentage(value) => {
|
||||
|
@ -493,7 +495,7 @@ impl ToComputedValue for specified::LengthOrPercentageOrAuto {
|
|||
fn to_computed_value(&self, context: &Context) -> LengthOrPercentageOrAuto {
|
||||
match *self {
|
||||
specified::LengthOrPercentageOrAuto::Length(ref value) => {
|
||||
LengthOrPercentageOrAuto::Length(value.to_computed_value(context))
|
||||
LengthOrPercentageOrAuto::Length(Au::from(value.to_computed_value(context)))
|
||||
}
|
||||
specified::LengthOrPercentageOrAuto::Percentage(value) => {
|
||||
LengthOrPercentageOrAuto::Percentage(value)
|
||||
|
@ -513,7 +515,7 @@ impl ToComputedValue for specified::LengthOrPercentageOrAuto {
|
|||
LengthOrPercentageOrAuto::Auto => specified::LengthOrPercentageOrAuto::Auto,
|
||||
LengthOrPercentageOrAuto::Length(value) => {
|
||||
specified::LengthOrPercentageOrAuto::Length(
|
||||
ToComputedValue::from_computed_value(&value)
|
||||
ToComputedValue::from_computed_value(&value.into())
|
||||
)
|
||||
}
|
||||
LengthOrPercentageOrAuto::Percentage(value) => {
|
||||
|
@ -585,7 +587,7 @@ impl ToComputedValue for specified::LengthOrPercentageOrNone {
|
|||
fn to_computed_value(&self, context: &Context) -> LengthOrPercentageOrNone {
|
||||
match *self {
|
||||
specified::LengthOrPercentageOrNone::Length(ref value) => {
|
||||
LengthOrPercentageOrNone::Length(value.to_computed_value(context))
|
||||
LengthOrPercentageOrNone::Length(Au::from(value.to_computed_value(context)))
|
||||
}
|
||||
specified::LengthOrPercentageOrNone::Percentage(value) => {
|
||||
LengthOrPercentageOrNone::Percentage(value)
|
||||
|
@ -605,7 +607,7 @@ impl ToComputedValue for specified::LengthOrPercentageOrNone {
|
|||
LengthOrPercentageOrNone::None => specified::LengthOrPercentageOrNone::None,
|
||||
LengthOrPercentageOrNone::Length(value) => {
|
||||
specified::LengthOrPercentageOrNone::Length(
|
||||
ToComputedValue::from_computed_value(&value)
|
||||
ToComputedValue::from_computed_value(&value.into())
|
||||
)
|
||||
}
|
||||
LengthOrPercentageOrNone::Percentage(value) => {
|
||||
|
@ -623,10 +625,10 @@ impl ToComputedValue for specified::LengthOrPercentageOrNone {
|
|||
/// A wrapper of LengthOrPercentage, whose value must be >= 0.
|
||||
pub type NonNegativeLengthOrPercentage = NonNegative<LengthOrPercentage>;
|
||||
|
||||
impl From<NonNegativeAu> for NonNegativeLengthOrPercentage {
|
||||
impl From<NonNegativeLength> for NonNegativeLengthOrPercentage {
|
||||
#[inline]
|
||||
fn from(length: NonNegativeAu) -> Self {
|
||||
LengthOrPercentage::Length(length.0).into()
|
||||
fn from(length: NonNegativeLength) -> Self {
|
||||
LengthOrPercentage::Length(Au::from(length.0)).into()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -664,8 +666,56 @@ impl NonNegativeLengthOrPercentage {
|
|||
}
|
||||
}
|
||||
|
||||
/// A computed `<length>` value.
|
||||
pub type Length = Au;
|
||||
/// The computed `<length>` value.
|
||||
#[cfg_attr(feature = "servo", derive(Deserialize, HeapSizeOf, Serialize))]
|
||||
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, PartialEq, PartialOrd)]
|
||||
#[derive(ToAnimatedValue, ToAnimatedZero)]
|
||||
pub struct CSSPixelLength(CSSFloat);
|
||||
|
||||
impl CSSPixelLength {
|
||||
/// Return a new CSSPixelLength.
|
||||
#[inline]
|
||||
pub fn new(px: CSSFloat) -> Self {
|
||||
CSSPixelLength(px)
|
||||
}
|
||||
|
||||
/// Return the containing pixel value.
|
||||
#[inline]
|
||||
pub fn px(&self) -> CSSFloat {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Return the length with app_unit i32 type.
|
||||
#[inline]
|
||||
pub fn to_i32_au(&self) -> i32 {
|
||||
Au::from(*self).0
|
||||
}
|
||||
}
|
||||
|
||||
impl ToCss for CSSPixelLength {
|
||||
#[inline]
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
self.0.to_css(dest)?;
|
||||
dest.write_str("px")
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CSSPixelLength> for Au {
|
||||
#[inline]
|
||||
fn from(len: CSSPixelLength) -> Self {
|
||||
Au::from_f32_px(len.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Au> for CSSPixelLength {
|
||||
#[inline]
|
||||
fn from(len: Au) -> Self {
|
||||
CSSPixelLength::new(len.to_f32_px())
|
||||
}
|
||||
}
|
||||
|
||||
/// An alias of computed `<length>` value.
|
||||
pub type Length = CSSPixelLength;
|
||||
|
||||
/// Either a computed `<length>` or the `none` keyword.
|
||||
pub type LengthOrNone = Either<Length, None_>;
|
||||
|
@ -688,7 +738,63 @@ impl LengthOrNumber {
|
|||
pub type LengthOrNormal = Either<Length, Normal>;
|
||||
|
||||
/// A wrapper of Length, whose value must be >= 0.
|
||||
pub type NonNegativeLength = NonNegativeAu;
|
||||
pub type NonNegativeLength = NonNegative<Length>;
|
||||
|
||||
impl NonNegativeLength {
|
||||
/// Create a NonNegativeLength.
|
||||
#[inline]
|
||||
pub fn new(px: CSSFloat) -> Self {
|
||||
NonNegative(Length::new(px.max(0.)))
|
||||
}
|
||||
|
||||
/// Return a zero value.
|
||||
#[inline]
|
||||
pub fn zero() -> Self {
|
||||
Self::new(0.)
|
||||
}
|
||||
|
||||
/// Return the pixel value of |NonNegativeLength|.
|
||||
#[inline]
|
||||
pub fn px(&self) -> CSSFloat {
|
||||
self.0.px()
|
||||
}
|
||||
|
||||
/// Scale this NonNegativeLength.
|
||||
/// We scale NonNegativeLength by zero if the factor is negative because it doesn't
|
||||
/// make sense to scale a negative factor on a non-negative length.
|
||||
#[inline]
|
||||
pub fn scale_by(&self, factor: f32) -> Self {
|
||||
Self::new(self.0.px() * factor.max(0.))
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<NonNegativeLength> for NonNegativeLength {
|
||||
type Output = Self;
|
||||
fn add(self, other: Self) -> Self {
|
||||
NonNegativeLength::new(self.px() + other.px())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Length> for NonNegativeLength {
|
||||
#[inline]
|
||||
fn from(len: Length) -> Self {
|
||||
NonNegative(len)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Au> for NonNegativeLength {
|
||||
#[inline]
|
||||
fn from(au: Au) -> Self {
|
||||
NonNegative(au.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NonNegativeLength> for Au {
|
||||
#[inline]
|
||||
fn from(non_negative_len: NonNegativeLength) -> Self {
|
||||
Au::from(non_negative_len.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Either a computed NonNegativeLength or the `auto` keyword.
|
||||
pub type NonNegativeLengthOrAuto = Either<NonNegativeLength, Auto>;
|
||||
|
|
|
@ -14,7 +14,7 @@ use properties;
|
|||
use properties::{ComputedValues, StyleBuilder};
|
||||
#[cfg(feature = "servo")]
|
||||
use servo_url::ServoUrl;
|
||||
use std::{f32, fmt, ops};
|
||||
use std::{f32, fmt};
|
||||
#[cfg(feature = "servo")]
|
||||
use std::sync::Arc;
|
||||
use style_traits::ToCss;
|
||||
|
@ -46,7 +46,7 @@ pub use super::{Auto, Either, None_};
|
|||
pub use super::specified::BorderStyle;
|
||||
pub use self::length::{CalcLengthOrPercentage, Length, LengthOrNone, LengthOrNumber, LengthOrPercentage};
|
||||
pub use self::length::{LengthOrPercentageOrAuto, LengthOrPercentageOrNone, MaxLength, MozLength};
|
||||
pub use self::length::NonNegativeLengthOrPercentage;
|
||||
pub use self::length::{CSSPixelLength, NonNegativeLength, NonNegativeLengthOrPercentage};
|
||||
pub use self::percentage::Percentage;
|
||||
pub use self::position::Position;
|
||||
pub use self::svg::{SVGLength, SVGOpacity, SVGPaint, SVGPaintKind, SVGStrokeDashArray, SVGWidth};
|
||||
|
@ -146,12 +146,12 @@ impl<'a> Context<'a> {
|
|||
|
||||
/// Apply text-zoom if enabled.
|
||||
#[cfg(feature = "gecko")]
|
||||
pub fn maybe_zoom_text(&self, size: NonNegativeAu) -> NonNegativeAu {
|
||||
pub fn maybe_zoom_text(&self, size: CSSPixelLength) -> CSSPixelLength {
|
||||
// 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.0).into()
|
||||
self.device().zoom_text(Au::from(size)).into()
|
||||
} else {
|
||||
size
|
||||
}
|
||||
|
@ -159,7 +159,7 @@ impl<'a> Context<'a> {
|
|||
|
||||
/// (Servo doesn't do text-zoom)
|
||||
#[cfg(feature = "servo")]
|
||||
pub fn maybe_zoom_text(&self, size: NonNegativeAu) -> NonNegativeAu {
|
||||
pub fn maybe_zoom_text(&self, size: CSSPixelLength) -> CSSPixelLength {
|
||||
size
|
||||
}
|
||||
}
|
||||
|
@ -450,13 +450,13 @@ pub type NonNegativeLengthOrPercentageOrNumber = Either<NonNegativeNumber, NonNe
|
|||
|
||||
#[allow(missing_docs)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, Eq, PartialEq)]
|
||||
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, PartialEq)]
|
||||
/// A computed cliprect for clip and image-region
|
||||
pub struct ClipRect {
|
||||
pub top: Option<Au>,
|
||||
pub right: Option<Au>,
|
||||
pub bottom: Option<Au>,
|
||||
pub left: Option<Au>,
|
||||
pub top: Option<Length>,
|
||||
pub right: Option<Length>,
|
||||
pub bottom: Option<Length>,
|
||||
pub left: Option<Length>,
|
||||
}
|
||||
|
||||
impl ToCss for ClipRect {
|
||||
|
@ -529,50 +529,6 @@ impl ClipRectOrAuto {
|
|||
/// <color> | auto
|
||||
pub type ColorOrAuto = Either<Color, Auto>;
|
||||
|
||||
/// A wrapper of Au, but the value >= 0.
|
||||
pub type NonNegativeAu = NonNegative<Au>;
|
||||
|
||||
impl NonNegativeAu {
|
||||
/// Return a zero value.
|
||||
#[inline]
|
||||
pub fn zero() -> Self {
|
||||
NonNegative::<Au>(Au(0))
|
||||
}
|
||||
|
||||
/// Return a NonNegativeAu from pixel.
|
||||
#[inline]
|
||||
pub fn from_px(px: i32) -> Self {
|
||||
NonNegative::<Au>(Au::from_px(::std::cmp::max(px, 0)))
|
||||
}
|
||||
|
||||
/// Get the inner value of |NonNegativeAu.0|.
|
||||
#[inline]
|
||||
pub fn value(self) -> i32 {
|
||||
(self.0).0
|
||||
}
|
||||
|
||||
/// Scale this NonNegativeAu.
|
||||
#[inline]
|
||||
pub fn scale_by(self, factor: f32) -> Self {
|
||||
// scale this by zero if factor is negative.
|
||||
NonNegative::<Au>(self.0.scale_by(factor.max(0.)))
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Add<NonNegativeAu> for NonNegativeAu {
|
||||
type Output = NonNegativeAu;
|
||||
fn add(self, other: Self) -> Self {
|
||||
(self.0 + other.0).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Au> for NonNegativeAu {
|
||||
#[inline]
|
||||
fn from(au: Au) -> NonNegativeAu {
|
||||
NonNegative::<Au>(au)
|
||||
}
|
||||
}
|
||||
|
||||
/// The computed value of a CSS `url()`, resolved relative to the stylesheet URL.
|
||||
#[cfg(feature = "servo")]
|
||||
#[derive(Clone, Debug, Deserialize, HeapSizeOf, PartialEq, Serialize)]
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
use app_units::Au;
|
||||
use values::RGBA;
|
||||
use values::computed::{ComputedUrl, LengthOrPercentage, NonNegativeAu};
|
||||
use values::computed::{ComputedUrl, LengthOrPercentage, NonNegativeLength};
|
||||
use values::computed::{NonNegativeNumber, NonNegativeLengthOrPercentage, Number};
|
||||
use values::computed::Opacity;
|
||||
use values::generics::svg as generic;
|
||||
|
@ -72,8 +72,8 @@ impl Into<NonNegativeSvgLengthOrPercentageOrNumber> for SvgLengthOrPercentageOrN
|
|||
/// An non-negative wrapper of SVGLength.
|
||||
pub type SVGWidth = generic::SVGLength<NonNegativeSvgLengthOrPercentageOrNumber>;
|
||||
|
||||
impl From<NonNegativeAu> for SVGWidth {
|
||||
fn from(length: NonNegativeAu) -> Self {
|
||||
impl From<NonNegativeLength> for SVGWidth {
|
||||
fn from(length: NonNegativeLength) -> Self {
|
||||
generic::SVGLength::Length(
|
||||
generic::SvgLengthOrPercentageOrNumber::LengthOrPercentage(length.into()))
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
use values::{CSSInteger, CSSFloat};
|
||||
use values::animated::ToAnimatedZero;
|
||||
use values::computed::{NonNegativeAu, NonNegativeNumber};
|
||||
use values::computed::{NonNegativeLength, NonNegativeNumber};
|
||||
use values::computed::length::{Length, LengthOrPercentage};
|
||||
use values::generics::text::InitialLetter as GenericInitialLetter;
|
||||
use values::generics::text::LineHeight as GenericLineHeight;
|
||||
|
@ -22,7 +22,7 @@ pub type LetterSpacing = Spacing<Length>;
|
|||
pub type WordSpacing = Spacing<LengthOrPercentage>;
|
||||
|
||||
/// A computed value for the `line-height` property.
|
||||
pub type LineHeight = GenericLineHeight<NonNegativeNumber, NonNegativeAu>;
|
||||
pub type LineHeight = GenericLineHeight<NonNegativeNumber, NonNegativeLength>;
|
||||
|
||||
impl ToAnimatedZero for LineHeight {
|
||||
#[inline]
|
||||
|
|
|
@ -30,7 +30,7 @@ impl TransformOrigin {
|
|||
Self::new(
|
||||
LengthOrPercentage::Percentage(Percentage(0.5)),
|
||||
LengthOrPercentage::Percentage(Percentage(0.5)),
|
||||
Length::from_px(0),
|
||||
Length::new(0.),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ impl TransformList {
|
|||
Transform3D::create_rotation(ax, ay, az, theta.into())
|
||||
}
|
||||
ComputedOperation::Perspective(d) => {
|
||||
Self::create_perspective_matrix(d)
|
||||
Self::create_perspective_matrix(d.px())
|
||||
}
|
||||
ComputedOperation::Scale(sx, sy, sz) => {
|
||||
Transform3D::create_scale(sx, sy, sz)
|
||||
|
@ -105,7 +105,7 @@ impl TransformList {
|
|||
(extract_pixel_length(&tx), extract_pixel_length(&ty))
|
||||
}
|
||||
};
|
||||
let tz = tz.to_f32_px();
|
||||
let tz = tz.px();
|
||||
Transform3D::create_translation(tx, ty, tz)
|
||||
}
|
||||
ComputedOperation::Matrix(m) => {
|
||||
|
@ -137,7 +137,7 @@ impl TransformList {
|
|||
|
||||
/// Return the transform matrix from a perspective length.
|
||||
#[inline]
|
||||
pub fn create_perspective_matrix(d: Au) -> Transform3D<f32> {
|
||||
pub fn create_perspective_matrix(d: CSSFloat) -> Transform3D<f32> {
|
||||
// TODO(gw): The transforms spec says that perspective length must
|
||||
// be positive. However, there is some confusion between the spec
|
||||
// and browser implementations as to handling the case of 0 for the
|
||||
|
@ -145,7 +145,6 @@ impl TransformList {
|
|||
// that a provided perspective value of <= 0.0 doesn't cause panics
|
||||
// and behaves as it does in other browsers.
|
||||
// See https://lists.w3.org/Archives/Public/www-style/2016Jan/0020.html for more details.
|
||||
let d = d.to_f32_px();
|
||||
if d <= 0.0 {
|
||||
Transform3D::identity()
|
||||
} else {
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
use cssparser::Parser;
|
||||
use parser::{Parse, ParserContext};
|
||||
use style_traits::ParseError;
|
||||
use values::computed::{Context, NonNegativeAu, ToComputedValue};
|
||||
use values::computed::{Context, NonNegativeLength, ToComputedValue};
|
||||
use values::generics::border::BorderCornerRadius as GenericBorderCornerRadius;
|
||||
use values::generics::border::BorderImageSideWidth as GenericBorderImageSideWidth;
|
||||
use values::generics::border::BorderImageSlice as GenericBorderImageSlice;
|
||||
|
@ -71,7 +71,7 @@ impl Parse for BorderSideWidth {
|
|||
}
|
||||
|
||||
impl ToComputedValue for BorderSideWidth {
|
||||
type ComputedValue = NonNegativeAu;
|
||||
type ComputedValue = NonNegativeLength;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
|
||||
|
@ -82,7 +82,7 @@ impl ToComputedValue for BorderSideWidth {
|
|||
BorderSideWidth::Thin => Length::from_px(1.).to_computed_value(context),
|
||||
BorderSideWidth::Medium => Length::from_px(3.).to_computed_value(context),
|
||||
BorderSideWidth::Thick => Length::from_px(5.).to_computed_value(context),
|
||||
BorderSideWidth::Length(ref length) => length.to_computed_value(context)
|
||||
BorderSideWidth::Length(ref length) => length.to_computed_value(context),
|
||||
}.into()
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ use stylesheets::CssRuleType;
|
|||
use super::{AllowQuirks, Number, ToComputedValue, Percentage};
|
||||
use values::{Auto, CSSFloat, Either, FONT_MEDIUM_PX, None_, Normal};
|
||||
use values::{ExtremumLength, serialize_dimension};
|
||||
use values::computed::{self, Context};
|
||||
use values::computed::{self, CSSPixelLength, Context};
|
||||
use values::generics::NonNegative;
|
||||
use values::specified::NonNegativeNumber;
|
||||
use values::specified::calc::CalcNode;
|
||||
|
@ -95,16 +95,30 @@ impl FontBaseSize {
|
|||
pub fn resolve(&self, context: &Context) -> Au {
|
||||
match *self {
|
||||
FontBaseSize::Custom(size) => size,
|
||||
FontBaseSize::CurrentStyle => context.style().get_font().clone_font_size().0,
|
||||
FontBaseSize::InheritedStyle => context.style().get_parent_font().clone_font_size().0,
|
||||
FontBaseSize::CurrentStyle => Au::from(context.style().get_font().clone_font_size()),
|
||||
FontBaseSize::InheritedStyle => Au::from(context.style().get_parent_font().clone_font_size()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FontRelativeLength {
|
||||
/// Computes the font-relative length. We use the base_size
|
||||
/// flag to pass a different size for computing font-size and unconstrained font-size
|
||||
pub fn to_computed_value(&self, context: &Context, base_size: FontBaseSize) -> Au {
|
||||
/// Computes the font-relative length.
|
||||
pub fn to_computed_value(&self, context: &Context, base_size: FontBaseSize) -> CSSPixelLength {
|
||||
use std::f32;
|
||||
let (reference_size, length) = self.reference_font_size_and_length(context, base_size);
|
||||
let pixel = (length * reference_size.to_f32_px()).min(f32::MAX).max(f32::MIN);
|
||||
CSSPixelLength::new(pixel)
|
||||
}
|
||||
|
||||
/// Return reference font size. We use the base_size flag to pass a different size
|
||||
/// for computing font-size and unconstrained font-size.
|
||||
/// This returns a pair, the first one is the reference font size, and the second one is the
|
||||
/// unpacked relative length.
|
||||
fn reference_font_size_and_length(
|
||||
&self,
|
||||
context: &Context,
|
||||
base_size: FontBaseSize,
|
||||
) -> (Au, CSSFloat) {
|
||||
fn query_font_metrics(context: &Context, reference_font_size: Au) -> FontMetricsQueryResult {
|
||||
context.font_metrics_provider.query(context.style().get_font(),
|
||||
reference_font_size,
|
||||
|
@ -116,22 +130,31 @@ impl FontRelativeLength {
|
|||
let reference_font_size = base_size.resolve(context);
|
||||
|
||||
match *self {
|
||||
FontRelativeLength::Em(length) => reference_font_size.scale_by(length),
|
||||
FontRelativeLength::Em(length) => {
|
||||
(reference_font_size, length)
|
||||
},
|
||||
FontRelativeLength::Ex(length) => {
|
||||
match query_font_metrics(context, reference_font_size) {
|
||||
FontMetricsQueryResult::Available(metrics) => metrics.x_height.scale_by(length),
|
||||
let reference_size = match query_font_metrics(context, reference_font_size) {
|
||||
FontMetricsQueryResult::Available(metrics) => {
|
||||
metrics.x_height
|
||||
},
|
||||
// https://drafts.csswg.org/css-values/#ex
|
||||
//
|
||||
// In the cases where it is impossible or impractical to
|
||||
// determine the x-height, a value of 0.5em must be
|
||||
// assumed.
|
||||
//
|
||||
FontMetricsQueryResult::NotAvailable => reference_font_size.scale_by(0.5 * length),
|
||||
}
|
||||
FontMetricsQueryResult::NotAvailable => {
|
||||
reference_font_size.scale_by(0.5)
|
||||
},
|
||||
};
|
||||
(reference_size, length)
|
||||
},
|
||||
FontRelativeLength::Ch(length) => {
|
||||
match query_font_metrics(context, reference_font_size) {
|
||||
FontMetricsQueryResult::Available(metrics) => metrics.zero_advance_measure.scale_by(length),
|
||||
let reference_size = match query_font_metrics(context, reference_font_size) {
|
||||
FontMetricsQueryResult::Available(metrics) => {
|
||||
metrics.zero_advance_measure
|
||||
},
|
||||
// https://drafts.csswg.org/css-values/#ch
|
||||
//
|
||||
// In the cases where it is impossible or impractical to
|
||||
|
@ -144,12 +167,13 @@ impl FontRelativeLength {
|
|||
//
|
||||
FontMetricsQueryResult::NotAvailable => {
|
||||
if context.style().writing_mode.is_vertical() {
|
||||
reference_font_size.scale_by(length)
|
||||
reference_font_size
|
||||
} else {
|
||||
reference_font_size.scale_by(0.5 * length)
|
||||
reference_font_size.scale_by(0.5)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
(reference_size, length)
|
||||
}
|
||||
FontRelativeLength::Rem(length) => {
|
||||
// https://drafts.csswg.org/css-values/#rem:
|
||||
|
@ -158,11 +182,12 @@ impl FontRelativeLength {
|
|||
// element, the rem units refer to the property’s initial
|
||||
// value.
|
||||
//
|
||||
if context.is_root_element {
|
||||
reference_font_size.scale_by(length)
|
||||
let reference_size = if context.is_root_element {
|
||||
reference_font_size
|
||||
} else {
|
||||
context.device().root_font_size().scale_by(length)
|
||||
}
|
||||
context.device().root_font_size()
|
||||
};
|
||||
(reference_size, length)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -197,7 +222,7 @@ impl ToCss for ViewportPercentageLength {
|
|||
|
||||
impl ViewportPercentageLength {
|
||||
/// Computes the given viewport-relative length for the given viewport size.
|
||||
pub fn to_computed_value(&self, viewport_size: Size2D<Au>) -> Au {
|
||||
pub fn to_computed_value(&self, viewport_size: Size2D<Au>) -> CSSPixelLength {
|
||||
let (factor, length) = match *self {
|
||||
ViewportPercentageLength::Vw(length) =>
|
||||
(length, viewport_size.width),
|
||||
|
@ -209,10 +234,11 @@ impl ViewportPercentageLength {
|
|||
(length, cmp::max(viewport_size.width, viewport_size.height)),
|
||||
};
|
||||
|
||||
// FIXME: Bug 1396535, we need to fix the extremely small viewport length for transform.
|
||||
// See bug 989802. We truncate so that adding multiple viewport units
|
||||
// that add up to 100 does not overflow due to rounding differences
|
||||
let trunc_scaled = ((length.0 as f64) * factor as f64 / 100.).trunc();
|
||||
Au::from_f64_au(trunc_scaled)
|
||||
Au::from_f64_au(trunc_scaled).into()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -223,14 +249,15 @@ pub struct CharacterWidth(pub i32);
|
|||
|
||||
impl CharacterWidth {
|
||||
/// Computes the given character width.
|
||||
pub fn to_computed_value(&self, reference_font_size: Au) -> Au {
|
||||
pub fn to_computed_value(&self, reference_font_size: Au) -> CSSPixelLength {
|
||||
// This applies the *converting a character width to pixels* algorithm as specified
|
||||
// in HTML5 § 14.5.4.
|
||||
//
|
||||
// TODO(pcwalton): Find these from the font.
|
||||
let average_advance = reference_font_size.scale_by(0.5);
|
||||
let max_advance = reference_font_size;
|
||||
average_advance.scale_by(self.0 as CSSFloat - 1.0) + max_advance
|
||||
let au = average_advance.scale_by(self.0 as CSSFloat - 1.0) + max_advance;
|
||||
au.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -286,32 +313,14 @@ impl AbsoluteLength {
|
|||
}
|
||||
|
||||
impl ToComputedValue for AbsoluteLength {
|
||||
type ComputedValue = Au;
|
||||
type ComputedValue = CSSPixelLength;
|
||||
|
||||
fn to_computed_value(&self, _: &Context) -> Au {
|
||||
Au::from(*self)
|
||||
fn to_computed_value(&self, _: &Context) -> Self::ComputedValue {
|
||||
CSSPixelLength::new(self.to_px())
|
||||
}
|
||||
|
||||
fn from_computed_value(computed: &Au) -> AbsoluteLength {
|
||||
AbsoluteLength::Px(computed.to_f32_px())
|
||||
}
|
||||
}
|
||||
|
||||
fn au_from_f32_round(x: f32) -> Au {
|
||||
Au::from_f64_au((x as f64).round())
|
||||
}
|
||||
|
||||
impl From<AbsoluteLength> for Au {
|
||||
fn from(length: AbsoluteLength) -> Au {
|
||||
match length {
|
||||
AbsoluteLength::Px(value) => au_from_f32_round((value * AU_PER_PX)),
|
||||
AbsoluteLength::In(value) => au_from_f32_round((value * AU_PER_IN)),
|
||||
AbsoluteLength::Cm(value) => au_from_f32_round((value * AU_PER_CM)),
|
||||
AbsoluteLength::Mm(value) => au_from_f32_round((value * AU_PER_MM)),
|
||||
AbsoluteLength::Q(value) => au_from_f32_round((value * AU_PER_Q)),
|
||||
AbsoluteLength::Pt(value) => au_from_f32_round((value * AU_PER_PT)),
|
||||
AbsoluteLength::Pc(value) => au_from_f32_round((value * AU_PER_PC)),
|
||||
}
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
AbsoluteLength::Px(computed.px())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -376,18 +385,20 @@ impl PhysicalLength {
|
|||
}
|
||||
|
||||
/// Computes the given character width.
|
||||
pub fn to_computed_value(&self, context: &Context) -> Au {
|
||||
pub fn to_computed_value(&self, context: &Context) -> CSSPixelLength {
|
||||
use gecko_bindings::bindings;
|
||||
// Same as Gecko
|
||||
const MM_PER_INCH: f32 = 25.4;
|
||||
use std::f32;
|
||||
|
||||
let physical_inch = unsafe {
|
||||
bindings::Gecko_GetAppUnitsPerPhysicalInch(context.device().pres_context())
|
||||
// Same as Gecko
|
||||
const INCH_PER_MM: f32 = 1. / 25.4;
|
||||
|
||||
let au_per_physical_inch = unsafe {
|
||||
bindings::Gecko_GetAppUnitsPerPhysicalInch(context.device().pres_context()) as f32
|
||||
};
|
||||
|
||||
let inch = self.0 / MM_PER_INCH;
|
||||
|
||||
au_from_f32_round(inch * physical_inch as f32)
|
||||
let px_per_physical_inch = au_per_physical_inch / AU_PER_PX;
|
||||
let pixel = self.0 * px_per_physical_inch * INCH_PER_MM;
|
||||
CSSPixelLength::new(pixel.min(f32::MAX).max(f32::MIN))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -84,6 +84,7 @@ impl ToComputedValue for LineHeight {
|
|||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
|
||||
use app_units::Au;
|
||||
use values::specified::length::FontBaseSize;
|
||||
match *self {
|
||||
GenericLineHeight::Normal => {
|
||||
|
@ -99,7 +100,7 @@ impl ToComputedValue for LineHeight {
|
|||
GenericLineHeight::Length(ref non_negative_lop) => {
|
||||
let result = match non_negative_lop.0 {
|
||||
LengthOrPercentage::Length(NoCalcLength::Absolute(ref abs)) => {
|
||||
context.maybe_zoom_text(abs.to_computed_value(context).into())
|
||||
context.maybe_zoom_text(abs.to_computed_value(context)).into()
|
||||
}
|
||||
LengthOrPercentage::Length(ref length) => {
|
||||
length.to_computed_value(context).into()
|
||||
|
@ -123,7 +124,7 @@ impl ToComputedValue for LineHeight {
|
|||
let absolute_length = computed_calc.unclamped_length();
|
||||
computed_calc
|
||||
.clamping_mode
|
||||
.clamp(absolute_length + font_relative_length)
|
||||
.clamp(absolute_length + Au::from(font_relative_length))
|
||||
.into()
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue