Auto merge of #17898 - BorisChiou:stylo/animation/filter_distance, r=birtles

stylo: Fix compute_squared_distance for AnimatedFilterList

We implement compute_distance for Angle to avoid returning Err(()) from it, and then rewrite compute_squared_distance of AnimatedFilterLIst to avoid using unwrap() and make it simpler.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix [Bug 1384014](https://bugzilla.mozilla.org/show_bug.cgi?id=1384014).
- [X] These changes do not require tests because there is a DevTools test for this already.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17898)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-07-28 02:08:31 -05:00 committed by GitHub
commit 3ab6b1cf8f
4 changed files with 23 additions and 29 deletions

1
Cargo.lock generated
View file

@ -3011,6 +3011,7 @@ dependencies = [
"heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"heapsize_derive 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"html5ever 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -44,6 +44,7 @@ euclid = "0.15"
fnv = "1.0"
heapsize = {version = "0.4", optional = true}
heapsize_derive = {version = "0.1", optional = true}
itertools = "0.5"
itoa = "0.3"
html5ever = {version = "0.18", optional = true}
lazy_static = "0.2"

View file

@ -51,6 +51,7 @@ extern crate fnv;
#[cfg(feature = "gecko")] #[macro_use] pub mod gecko_string_cache;
#[cfg(feature = "servo")] extern crate heapsize;
#[cfg(feature = "servo")] #[macro_use] extern crate heapsize_derive;
extern crate itertools;
extern crate itoa;
#[cfg(feature = "servo")] #[macro_use] extern crate html5ever;
#[macro_use]

View file

@ -925,6 +925,13 @@ impl Animatable for Angle {
}
}
}
#[inline]
fn compute_distance(&self, other: &Self) -> Result<f64, ()> {
// Use the formula for calculating the distance between angles defined in SVG:
// https://www.w3.org/TR/SVG/animate.html#complexDistances
Ok((self.radians64() - other.radians64()).abs())
}
}
/// https://drafts.csswg.org/css-transitions/#animtype-percentage
@ -3172,37 +3179,21 @@ impl Animatable for AnimatedFilterList {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<f64, ()> {
use itertools::{EitherOrBoth, Itertools};
let mut square_distance: f64 = 0.0;
let mut from_iter = self.0.iter();
let mut to_iter = other.0.iter();
let mut from = from_iter.next();
let mut to = to_iter.next();
while from.is_some() || to.is_some() {
let current_square_distance: f64 ;
if from.is_none() {
let none = try!(add_weighted_filter_function(to, to, 0.0, 0.0));
current_square_distance =
compute_filter_square_distance(&none, &(to.unwrap())).unwrap();
to = to_iter.next();
} else if to.is_none() {
let none = try!(add_weighted_filter_function(from, from, 0.0, 0.0));
current_square_distance =
compute_filter_square_distance(&none, &(from.unwrap())).unwrap();
from = from_iter.next();
} else {
current_square_distance =
compute_filter_square_distance(&(from.unwrap()),
&(to.unwrap())).unwrap();
from = from_iter.next();
to = to_iter.next();
for it in self.0.iter().zip_longest(other.0.iter()) {
square_distance += match it {
EitherOrBoth::Both(from, to) => {
compute_filter_square_distance(&from, &to)?
},
EitherOrBoth::Left(list) | EitherOrBoth::Right(list)=> {
let none = add_weighted_filter_function(Some(list), Some(list), 0.0, 0.0)?;
compute_filter_square_distance(&none, &list)?
},
};
}
square_distance += current_square_distance;
}
Ok(square_distance.sqrt())
Ok(square_distance)
}
}