style: Implement CSS round() function

Co-Authored-By: Emilio Cobos Álvarez <emilio@crisal.io>

Differential Revision: https://phabricator.services.mozilla.com/D156742
This commit is contained in:
Connor Pearson 2022-11-05 21:25:16 +00:00 committed by Martin Robinson
parent 17d33183c2
commit 6a4a97ad7c
6 changed files with 649 additions and 117 deletions

View file

@ -17,7 +17,7 @@ use crate::values::{specified, CSSFloat};
use crate::Zero;
use app_units::Au;
use std::fmt::{self, Write};
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Rem, Sub, SubAssign};
use style_traits::{CSSPixel, CssWriter, ToCss};
pub use super::image::Image;
@ -402,6 +402,15 @@ impl Neg for CSSPixelLength {
}
}
impl Rem for CSSPixelLength {
type Output = Self;
#[inline]
fn rem(self, other: Self) -> Self {
CSSPixelLength::new(self.0 % other.0)
}
}
impl Sub for CSSPixelLength {
type Output = Self;

View file

@ -624,10 +624,10 @@ impl PartialOrd for CalcLengthPercentageLeaf {
}
impl calc::CalcNodeLeaf for CalcLengthPercentageLeaf {
fn is_negative(&self) -> bool {
fn unitless_value(&self) -> f32 {
match *self {
Self::Length(ref l) => l.px() < 0.,
Self::Percentage(ref p) => p.0 < 0.,
Self::Length(ref l) => l.px(),
Self::Percentage(ref p) => p.0,
}
}
@ -657,6 +657,28 @@ impl calc::CalcNodeLeaf for CalcLengthPercentageLeaf {
Ok(())
}
fn try_op<O>(&self, other: &Self, op: O) -> Result<Self, ()>
where
O: Fn(f32, f32) -> f32,
{
match (self, other) {
(
&CalcLengthPercentageLeaf::Length(ref one),
&CalcLengthPercentageLeaf::Length(ref other),
) => Ok(CalcLengthPercentageLeaf::Length(Length::new(op(
one.px(),
other.px(),
)))),
(
&CalcLengthPercentageLeaf::Percentage(one),
&CalcLengthPercentageLeaf::Percentage(other),
) => Ok(CalcLengthPercentageLeaf::Percentage(Percentage(op(
one.0, other.0,
)))),
_ => Err(()),
}
}
fn mul_by(&mut self, scalar: f32) {
match *self {
Self::Length(ref mut l) => *l = *l * scalar,

View file

@ -77,6 +77,30 @@ impl std::ops::AddAssign for Percentage {
}
}
impl std::ops::Add for Percentage {
type Output = Self;
fn add(self, other: Self) -> Self {
Percentage(self.0 + other.0)
}
}
impl std::ops::Sub for Percentage {
type Output = Self;
fn sub(self, other: Self) -> Self {
Percentage(self.0 - other.0)
}
}
impl std::ops::Rem for Percentage {
type Output = Self;
fn rem(self, other: Self) -> Self {
Percentage(self.0 % other.0)
}
}
impl ToCss for Percentage {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where