2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/animation/AnimatableRepeatable.h"
36 size_t greatestCommonDivisor(size_t a, size_t b)
38 return b ? greatestCommonDivisor(b, a % b) : a;
41 size_t lowestCommonMultiple(size_t a, size_t b)
44 return a / greatestCommonDivisor(a, b) * b;
51 bool AnimatableRepeatable::usesDefaultInterpolationWith(const AnimatableValue* value) const
53 const Vector<RefPtr<AnimatableValue> >& fromValues = m_values;
54 const Vector<RefPtr<AnimatableValue> >& toValues = toAnimatableRepeatable(value)->m_values;
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))
67 bool AnimatableRepeatable::interpolateLists(const Vector<RefPtr<AnimatableValue> >& fromValues, const Vector<RefPtr<AnimatableValue> >& toValues, double fraction, Vector<RefPtr<AnimatableValue> >& interpolatedValues)
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.
77 if (AnimatableValue::usesDefaultInterpolation(from, to))
79 interpolatedValues.append(interpolate(from, to, fraction));
84 PassRefPtr<AnimatableValue> AnimatableRepeatable::interpolateTo(const AnimatableValue* value, double fraction) const
86 Vector<RefPtr<AnimatableValue> > interpolatedValues;
87 bool success = interpolateLists(m_values, toAnimatableRepeatable(value)->m_values, fraction, interpolatedValues);
88 return success ? create(interpolatedValues) : defaultInterpolateTo(this, value, fraction);
91 PassRefPtr<AnimatableValue> AnimatableRepeatable::addWith(const AnimatableValue* value) const
93 const Vector<RefPtr<AnimatableValue> >& otherValues = toAnimatableRepeatable(value)->m_values;
94 ASSERT(!m_values.isEmpty() && !otherValues.isEmpty());
95 Vector<RefPtr<AnimatableValue> > addedValues(lowestCommonMultiple(m_values.size(), otherValues.size()));
96 for (size_t i = 0; i < addedValues.size(); ++i) {
97 const AnimatableValue* left = m_values[i % m_values.size()].get();
98 const AnimatableValue* right = otherValues[i % otherValues.size()].get();
99 addedValues[i] = add(left, right);
101 return create(addedValues);
104 bool AnimatableRepeatable::equalTo(const AnimatableValue* value) const
106 const Vector<RefPtr<AnimatableValue> >& otherValues = toAnimatableRepeatable(value)->m_values;
107 if (m_values.size() != otherValues.size())
109 for (size_t i = 0; i < m_values.size(); ++i) {
110 if (!m_values[i]->equals(otherValues[i].get()))
116 } // namespace WebCore