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 "TransformOperations.h"
24
25#include "IdentityTransformOperation.h"
26#include "Matrix3DTransformOperation.h"
27#include <algorithm>
28
29namespace WebCore {
30
31TransformOperations::TransformOperations(bool makeIdentity)
32{
33    if (makeIdentity)
34        m_operations.append(IdentityTransformOperation::create());
35}
36
37bool TransformOperations::operator==(const TransformOperations& o) const
38{
39    if (m_operations.size() != o.m_operations.size())
40        return false;
41
42    unsigned s = m_operations.size();
43    for (unsigned i = 0; i < s; i++) {
44        if (*m_operations[i] != *o.m_operations[i])
45            return false;
46    }
47
48    return true;
49}
50
51bool TransformOperations::operationsMatch(const TransformOperations& other) const
52{
53    size_t numOperations = operations().size();
54    // If the sizes of the function lists don't match, the lists don't match
55    if (numOperations != other.operations().size())
56        return false;
57
58    // If the types of each function are not the same, the lists don't match
59    for (size_t i = 0; i < numOperations; ++i) {
60        if (!operations()[i]->isSameType(*other.operations()[i]))
61            return false;
62    }
63    return true;
64}
65
66TransformOperations TransformOperations::blendByMatchingOperations(const TransformOperations& from, const double& progress) const
67{
68    TransformOperations result;
69
70    unsigned fromSize = from.operations().size();
71    unsigned toSize = operations().size();
72    unsigned size = std::max(fromSize, toSize);
73    for (unsigned i = 0; i < size; i++) {
74        RefPtr<TransformOperation> fromOperation = (i < fromSize) ? from.operations()[i].get() : 0;
75        RefPtr<TransformOperation> toOperation = (i < toSize) ? operations()[i].get() : 0;
76        RefPtr<TransformOperation> blendedOperation = toOperation ? toOperation->blend(fromOperation.get(), progress) : (fromOperation ? fromOperation->blend(0, progress, true) : 0);
77        if (blendedOperation)
78            result.operations().append(blendedOperation);
79        else {
80            RefPtr<TransformOperation> identityOperation = IdentityTransformOperation::create();
81            if (progress > 0.5)
82                result.operations().append(toOperation ? toOperation : identityOperation);
83            else
84                result.operations().append(fromOperation ? fromOperation : identityOperation);
85        }
86    }
87
88    return result;
89}
90
91TransformOperations TransformOperations::blendByUsingMatrixInterpolation(const TransformOperations& from, double progress, const LayoutSize& size) const
92{
93    TransformOperations result;
94
95    // Convert the TransformOperations into matrices
96    TransformationMatrix fromTransform;
97    TransformationMatrix toTransform;
98    from.apply(size, fromTransform);
99    apply(size, toTransform);
100
101    toTransform.blend(fromTransform, progress);
102
103    // Append the result
104    result.operations().append(Matrix3DTransformOperation::create(toTransform));
105
106    return result;
107}
108
109TransformOperations TransformOperations::blend(const TransformOperations& from, double progress, const LayoutSize& size) const
110{
111    if (from == *this)
112        return *this;
113
114    if (from.size() && from.operationsMatch(*this))
115        return blendByMatchingOperations(from, progress);
116
117    return blendByUsingMatrixInterpolation(from, progress, size);
118}
119
120} // namespace WebCore
121