1 /*
2 * Copyright (C) 1999 Antti Koivisto (koivisto@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22 #include "config.h"
23 #include "platform/transforms/TransformOperations.h"
24
25 #include "platform/transforms/IdentityTransformOperation.h"
26 #include "platform/transforms/InterpolatedTransformOperation.h"
27 #include <algorithm>
28
29 using namespace std;
30
31 namespace WebCore {
32
TransformOperations(bool makeIdentity)33 TransformOperations::TransformOperations(bool makeIdentity)
34 {
35 if (makeIdentity)
36 m_operations.append(IdentityTransformOperation::create());
37 }
38
operator ==(const TransformOperations & o) const39 bool TransformOperations::operator==(const TransformOperations& o) const
40 {
41 if (m_operations.size() != o.m_operations.size())
42 return false;
43
44 unsigned s = m_operations.size();
45 for (unsigned i = 0; i < s; i++) {
46 if (*m_operations[i] != *o.m_operations[i])
47 return false;
48 }
49
50 return true;
51 }
52
operationsMatch(const TransformOperations & other) const53 bool TransformOperations::operationsMatch(const TransformOperations& other) const
54 {
55 size_t numOperations = operations().size();
56 // If the sizes of the function lists don't match, the lists don't match
57 if (numOperations != other.operations().size())
58 return false;
59
60 // If the types of each function are not the same, the lists don't match
61 for (size_t i = 0; i < numOperations; ++i) {
62 if (!operations()[i]->isSameType(*other.operations()[i]))
63 return false;
64 }
65 return true;
66 }
67
blendByMatchingOperations(const TransformOperations & from,const double & progress) const68 TransformOperations TransformOperations::blendByMatchingOperations(const TransformOperations& from, const double& progress) const
69 {
70 TransformOperations result;
71
72 unsigned fromSize = from.operations().size();
73 unsigned toSize = operations().size();
74 unsigned size = max(fromSize, toSize);
75 for (unsigned i = 0; i < size; i++) {
76 RefPtr<TransformOperation> fromOperation = (i < fromSize) ? from.operations()[i].get() : 0;
77 RefPtr<TransformOperation> toOperation = (i < toSize) ? operations()[i].get() : 0;
78 RefPtr<TransformOperation> blendedOperation = toOperation ? toOperation->blend(fromOperation.get(), progress) : (fromOperation ? fromOperation->blend(0, progress, true) : 0);
79 if (blendedOperation)
80 result.operations().append(blendedOperation);
81 else {
82 RefPtr<TransformOperation> identityOperation = IdentityTransformOperation::create();
83 if (progress > 0.5)
84 result.operations().append(toOperation ? toOperation : identityOperation);
85 else
86 result.operations().append(fromOperation ? fromOperation : identityOperation);
87 }
88 }
89
90 return result;
91 }
92
blendByUsingMatrixInterpolation(const TransformOperations & from,double progress) const93 TransformOperations TransformOperations::blendByUsingMatrixInterpolation(const TransformOperations& from, double progress) const
94 {
95 TransformOperations result;
96 result.operations().append(InterpolatedTransformOperation::create(from, *this, progress));
97 return result;
98 }
99
blend(const TransformOperations & from,double progress) const100 TransformOperations TransformOperations::blend(const TransformOperations& from, double progress) const
101 {
102 if (from == *this || (!from.size() && !size()))
103 return *this;
104
105 // If either list is empty, use blendByMatchingOperations which has special logic for this case.
106 if (!from.size() || !size() || from.operationsMatch(*this))
107 return blendByMatchingOperations(from, progress);
108
109 return blendByUsingMatrixInterpolation(from, progress);
110 }
111
add(const TransformOperations & addend) const112 TransformOperations TransformOperations::add(const TransformOperations& addend) const
113 {
114 TransformOperations result;
115 result.m_operations = operations();
116 result.m_operations.append(addend.operations());
117 return result;
118 }
119
120 } // namespace WebCore
121