Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include "config.h" |
| 32 | #include "core/animation/AnimatableRepeatable.h" |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | size_t greatestCommonDivisor(size_t a, size_t b) |
| 37 | { |
| 38 | return b ? greatestCommonDivisor(b, a % b) : a; |
| 39 | } |
| 40 | |
| 41 | size_t lowestCommonMultiple(size_t a, size_t b) |
| 42 | { |
| 43 | ASSERT(a && b); |
| 44 | return a / greatestCommonDivisor(a, b) * b; |
| 45 | } |
| 46 | |
| 47 | } // namespace |
| 48 | |
| 49 | namespace WebCore { |
| 50 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 51 | bool AnimatableRepeatable::usesDefaultInterpolationWith(const AnimatableValue* value) const |
| 52 | { |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 53 | const WillBeHeapVector<RefPtrWillBeMember<AnimatableValue> >& fromValues = m_values; |
| 54 | const WillBeHeapVector<RefPtrWillBeMember<AnimatableValue> >& toValues = toAnimatableRepeatable(value)->m_values; |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 55 | ASSERT(!fromValues.isEmpty() && !toValues.isEmpty()); |
| 56 | size_t size = lowestCommonMultiple(fromValues.size(), toValues.size()); |
| 57 | for (size_t i = 0; i < size; ++i) { |
| 58 | const AnimatableValue* from = fromValues[i % fromValues.size()].get(); |
| 59 | const AnimatableValue* to = toValues[i % toValues.size()].get(); |
| 60 | // Spec: If a pair of values cannot be interpolated, then the lists are not interpolable. |
| 61 | if (AnimatableValue::usesDefaultInterpolation(from, to)) |
| 62 | return true; |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 67 | bool AnimatableRepeatable::interpolateLists(const WillBeHeapVector<RefPtrWillBeMember<AnimatableValue> >& fromValues, const WillBeHeapVector<RefPtrWillBeMember<AnimatableValue> >& toValues, double fraction, WillBeHeapVector<RefPtrWillBeMember<AnimatableValue> >& interpolatedValues) |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 68 | { |
| 69 | // Interpolation behaviour spec: http://www.w3.org/TR/css3-transitions/#animtype-repeatable-list |
| 70 | ASSERT(interpolatedValues.isEmpty()); |
| 71 | ASSERT(!fromValues.isEmpty() && !toValues.isEmpty()); |
| 72 | size_t size = lowestCommonMultiple(fromValues.size(), toValues.size()); |
| 73 | for (size_t i = 0; i < size; ++i) { |
| 74 | const AnimatableValue* from = fromValues[i % fromValues.size()].get(); |
| 75 | const AnimatableValue* to = toValues[i % toValues.size()].get(); |
| 76 | // Spec: If a pair of values cannot be interpolated, then the lists are not interpolable. |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 77 | if (AnimatableValue::usesDefaultInterpolation(from, to)) |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 78 | return false; |
| 79 | interpolatedValues.append(interpolate(from, to, fraction)); |
| 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 84 | PassRefPtrWillBeRawPtr<AnimatableValue> AnimatableRepeatable::interpolateTo(const AnimatableValue* value, double fraction) const |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 85 | { |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 86 | WillBeHeapVector<RefPtrWillBeMember<AnimatableValue> > interpolatedValues; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 87 | bool success = interpolateLists(m_values, toAnimatableRepeatable(value)->m_values, fraction, interpolatedValues); |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 88 | if (success) |
| 89 | return create(interpolatedValues); |
| 90 | return defaultInterpolateTo(this, value, fraction); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 93 | bool AnimatableRepeatable::equalTo(const AnimatableValue* value) const |
| 94 | { |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 95 | const WillBeHeapVector<RefPtrWillBeMember<AnimatableValue> >& otherValues = toAnimatableRepeatable(value)->m_values; |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 96 | if (m_values.size() != otherValues.size()) |
| 97 | return false; |
| 98 | for (size_t i = 0; i < m_values.size(); ++i) { |
| 99 | if (!m_values[i]->equals(otherValues[i].get())) |
| 100 | return false; |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 105 | void AnimatableRepeatable::trace(Visitor* visitor) |
| 106 | { |
| 107 | visitor->trace(m_values); |
| 108 | } |
| 109 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 110 | } // namespace WebCore |