style: Clean up list interpolation code

Factor out duplicated / common code to its own module, add / fix spec
links, and clean-up callers.

Differential Revision: https://phabricator.services.mozilla.com/D167253
This commit is contained in:
Emilio Cobos Álvarez 2023-01-20 00:23:11 +00:00 committed by Martin Robinson
parent d54d9ec25a
commit 6ce64abe7e
10 changed files with 162 additions and 221 deletions

View file

@ -7,7 +7,6 @@
use super::{Animate, Procedure, ToAnimatedZero};
use crate::values::computed::font::FontVariationSettings;
use crate::values::distance::{ComputeSquaredDistance, SquaredDistance};
use crate::values::generics::font::FontSettings as GenericFontSettings;
/// <https://drafts.csswg.org/css-fonts-4/#font-variation-settings-def>
///
@ -17,31 +16,15 @@ use crate::values::generics::font::FontSettings as GenericFontSettings;
impl Animate for FontVariationSettings {
#[inline]
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
if self.0.len() == other.0.len() {
self.0
.iter()
.zip(other.0.iter())
.map(|(st, ot)| st.animate(&ot, procedure))
.collect::<Result<Vec<_>, ()>>()
.map(|v| GenericFontSettings(v.into_boxed_slice()))
} else {
Err(())
}
let result: Vec<_> = super::lists::by_computed_value::animate(&self.0, &other.0, procedure)?;
Ok(Self(result.into_boxed_slice()))
}
}
impl ComputeSquaredDistance for FontVariationSettings {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
if self.0.len() == other.0.len() {
self.0
.iter()
.zip(other.0.iter())
.map(|(st, ot)| st.compute_squared_distance(&ot))
.sum()
} else {
Err(())
}
super::lists::by_computed_value::squared_distance(&self.0, &other.0)
}
}

View file

@ -81,18 +81,8 @@ impl Animate for generics::TrackRepeat<LengthPercentage, Integer> {
(_, _) => return Err(()),
}
// The length of track_sizes should be matched.
if self.track_sizes.len() != other.track_sizes.len() {
return Err(());
}
let count = self.count;
let track_sizes = self
.track_sizes
.iter()
.zip(other.track_sizes.iter())
.map(|(a, b)| a.animate(b, procedure))
.collect::<Result<Vec<_>, _>>()?;
let track_sizes = super::lists::by_computed_value::animate(&self.track_sizes, &other.track_sizes, procedure)?;
// The length of |line_names| is always 0 or N+1, where N is the length
// of |track_sizes|. Besides, <line-names> is always discrete.
@ -101,7 +91,7 @@ impl Animate for generics::TrackRepeat<LengthPercentage, Integer> {
Ok(generics::TrackRepeat {
count,
line_names,
track_sizes: track_sizes.into(),
track_sizes,
})
}
}
@ -130,18 +120,14 @@ impl Animate for TrackList {
return Err(());
}
let values = self
.values
.iter()
.zip(other.values.iter())
.map(|(a, b)| a.animate(b, procedure))
.collect::<Result<Vec<_>, _>>()?;
let values = super::lists::by_computed_value::animate(&self.values, &other.values, procedure)?;
// The length of |line_names| is always 0 or N+1, where N is the length
// of |track_sizes|. Besides, <line-names> is always discrete.
let line_names = discrete(&self.line_names, &other.line_names, procedure)?;
Ok(TrackList {
values: values.into(),
values,
line_names,
auto_repeat_index: self.auto_repeat_index,
})

View file

@ -0,0 +1,132 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! Lists have various ways of being animated, this module implements them.
//!
//! See https://drafts.csswg.org/web-animations-1/#animating-properties
/// https://drafts.csswg.org/web-animations-1/#by-computed-value
pub mod by_computed_value {
use crate::values::{animated::{Animate, Procedure}, distance::{ComputeSquaredDistance, SquaredDistance}};
use std::iter::FromIterator;
#[allow(missing_docs)]
pub fn animate<T, C>(left: &[T], right: &[T], procedure: Procedure) -> Result<C, ()>
where
T: Animate,
C: FromIterator<T>,
{
if left.len() != right.len() {
return Err(())
}
left.iter().zip(right.iter()).map(|(left, right)| {
left.animate(right, procedure)
}).collect()
}
#[allow(missing_docs)]
pub fn squared_distance<T>(left: &[T], right: &[T]) -> Result<SquaredDistance, ()>
where
T: ComputeSquaredDistance,
{
if left.len() != right.len() {
return Err(())
}
left.iter().zip(right.iter()).map(|(left, right)| {
left.compute_squared_distance(right)
}).sum()
}
}
/// This is the animation used for some of the types like shadows and filters, where the
/// interpolation happens with the zero value if one of the sides is not present.
///
/// https://drafts.csswg.org/web-animations-1/#animating-shadow-lists
pub mod with_zero {
use crate::values::{animated::{Animate, Procedure}, distance::{ComputeSquaredDistance, SquaredDistance}};
use crate::values::animated::ToAnimatedZero;
use itertools::{EitherOrBoth, Itertools};
use std::iter::FromIterator;
#[allow(missing_docs)]
pub fn animate<T, C>(left: &[T], right: &[T], procedure: Procedure) -> Result<C, ()>
where
T: Animate + Clone + ToAnimatedZero,
C: FromIterator<T>,
{
if procedure == Procedure::Add {
return Ok(
left.iter().chain(right.iter()).cloned().collect()
);
}
left.iter().zip_longest(right.iter()).map(|it| {
match it {
EitherOrBoth::Both(left, right) => {
left.animate(right, procedure)
},
EitherOrBoth::Left(left) => {
left.animate(&left.to_animated_zero()?, procedure)
},
EitherOrBoth::Right(right) => {
right.to_animated_zero()?.animate(right, procedure)
}
}
}).collect()
}
#[allow(missing_docs)]
pub fn squared_distance<T>(left: &[T], right: &[T]) -> Result<SquaredDistance, ()>
where
T: ToAnimatedZero + ComputeSquaredDistance,
{
left.iter().zip_longest(right.iter()).map(|it| {
match it {
EitherOrBoth::Both(left, right) => {
left.compute_squared_distance(right)
},
EitherOrBoth::Left(item) | EitherOrBoth::Right(item) => {
item.to_animated_zero()?.compute_squared_distance(item)
},
}
}).sum()
}
}
/// https://drafts.csswg.org/web-animations-1/#repeatable-list
pub mod repeatable_list {
use crate::values::{animated::{Animate, Procedure}, distance::{ComputeSquaredDistance, SquaredDistance}};
use std::iter::FromIterator;
#[allow(missing_docs)]
pub fn animate<T, C>(left: &[T], right: &[T], procedure: Procedure) -> Result<C, ()>
where
T: Animate,
C: FromIterator<T>,
{
use num_integer::lcm;
// If the length of either list is zero, the least common multiple is undefined.
if left.is_empty() || right.is_empty() {
return Err(());
}
let len = lcm(left.len(), right.len());
left.iter().cycle().zip(right.iter().cycle()).take(len).map(|(left, right)| {
left.animate(right, procedure)
}).collect()
}
#[allow(missing_docs)]
pub fn squared_distance<T>(left: &[T], right: &[T]) -> Result<SquaredDistance, ()>
where
T: ComputeSquaredDistance,
{
use num_integer::lcm;
if left.is_empty() || right.is_empty() {
return Err(());
}
let len = lcm(left.len(), right.len());
left.iter().cycle().zip(right.iter().cycle()).take(len).map(|(left, right)| {
left.compute_squared_distance(right)
}).sum()
}
}

View file

@ -21,6 +21,7 @@ use std::cmp;
pub mod color;
pub mod effects;
pub mod lists;
mod font;
mod grid;
mod svg;

View file

@ -5,7 +5,6 @@
//! Animation implementations for various SVG-related types.
use super::{Animate, Procedure};
use crate::properties::animated_properties::ListAnimation;
use crate::values::distance::{ComputeSquaredDistance, SquaredDistance};
use crate::values::generics::svg::SVGStrokeDashArray;
@ -22,7 +21,7 @@ where
}
match (self, other) {
(&SVGStrokeDashArray::Values(ref this), &SVGStrokeDashArray::Values(ref other)) => Ok(
SVGStrokeDashArray::Values(this.animate_repeatable_list(other, procedure)?),
SVGStrokeDashArray::Values(super::lists::repeatable_list::animate(this, other, procedure)?),
),
_ => Err(()),
}
@ -37,7 +36,7 @@ where
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
match (self, other) {
(&SVGStrokeDashArray::Values(ref this), &SVGStrokeDashArray::Values(ref other)) => {
this.squared_distance_repeatable_list(other)
super::lists::repeatable_list::squared_distance(this, other)
},
_ => Err(()),
}

View file

@ -8,7 +8,6 @@
use super::animate_multiplicative_factor;
use super::{Animate, Procedure, ToAnimatedZero};
use crate::properties::animated_properties::ListAnimation;
use crate::values::computed::transform::Rotate as ComputedRotate;
use crate::values::computed::transform::Scale as ComputedScale;
use crate::values::computed::transform::Transform as ComputedTransform;
@ -947,7 +946,7 @@ impl Animate for ComputedTransform {
impl ComputeSquaredDistance for ComputedTransform {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
let squared_dist = self.0.squared_distance_with_zero(&other.0);
let squared_dist = super::lists::with_zero::squared_distance(&self.0, &other.0);
// Roll back to matrix interpolation if there is any Err(()) in the
// transform lists, such as mismatched transform functions.