WebKitCSSMatrix.cpp revision 65f03d4f644ce73618e5f4f50dd694b26f55ae12
1/*
2 * Copyright (C) 2008 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebKitCSSMatrix.h"
28
29#include "CSSParser.h"
30#include "CSSStyleSelector.h"
31#include "CSSMutableStyleDeclaration.h"
32#include "CSSPropertyNames.h"
33#include "CSSValueKeywords.h"
34#include "ExceptionCode.h"
35#include "RenderStyle.h"
36#include <wtf/MathExtras.h>
37
38namespace WebCore {
39
40WebKitCSSMatrix::WebKitCSSMatrix(const TransformationMatrix& m)
41    : m_matrix(m)
42{
43}
44
45WebKitCSSMatrix::WebKitCSSMatrix(const String& s, ExceptionCode& ec)
46{
47    setMatrixValue(s, ec);
48}
49
50WebKitCSSMatrix::~WebKitCSSMatrix()
51{
52}
53
54void WebKitCSSMatrix::setMatrixValue(const String& string, ExceptionCode& ec)
55{
56    CSSParser p(true);
57    RefPtr<CSSMutableStyleDeclaration> styleDeclaration = CSSMutableStyleDeclaration::create();
58    if (p.parseValue(styleDeclaration.get(), CSSPropertyWebkitTransform, string, true)) {
59        // Convert to TransformOperations. This can fail if a property
60        // requires style (i.e., param uses 'ems' or 'exs')
61        RefPtr<CSSValue> value = styleDeclaration->getPropertyCSSValue(CSSPropertyWebkitTransform);
62
63        // Check for a "none" or empty transform. In these cases we can use the default identity matrix.
64        if (!value || (value->isPrimitiveValue() && (static_cast<CSSPrimitiveValue*>(value.get()))->getIdent() == CSSValueNone))
65            return;
66
67        TransformOperations operations;
68        if (!CSSStyleSelector::createTransformOperations(value.get(), 0, 0, operations)) {
69            ec = SYNTAX_ERR;
70            return;
71        }
72
73        // Convert transform operations to a TransformationMatrix. This can fail
74        // if a param has a percentage ('%')
75        TransformationMatrix t;
76        for (unsigned i = 0; i < operations.operations().size(); ++i) {
77            if (operations.operations()[i].get()->apply(t, IntSize(0, 0))) {
78                ec = SYNTAX_ERR;
79                return;
80            }
81        }
82
83        // set the matrix
84        m_matrix = t;
85    } else if (!string.isEmpty()) // There is something there but parsing failed
86        ec = SYNTAX_ERR;
87}
88
89// Perform a concatenation of the matrices (this * secondMatrix)
90PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::multiply(WebKitCSSMatrix* secondMatrix) const
91{
92    if (!secondMatrix)
93        return 0;
94
95    TransformationMatrix tmp(secondMatrix->m_matrix);
96    tmp.multiply(m_matrix);
97    return WebKitCSSMatrix::create(tmp);
98}
99
100PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::inverse(ExceptionCode& ec) const
101{
102    if (!m_matrix.isInvertible()) {
103        ec = NOT_SUPPORTED_ERR;
104        return 0;
105    }
106
107    return WebKitCSSMatrix::create(m_matrix.inverse());
108}
109
110PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::translate(double x, double y, double z) const
111{
112    if (isnan(x))
113        x = 0;
114    if (isnan(y))
115        y = 0;
116    if (isnan(z))
117        z = 0;
118    return WebKitCSSMatrix::create(TransformationMatrix(m_matrix).translate3d(x, y, z));
119}
120
121PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::scale(double scaleX, double scaleY, double scaleZ) const
122{
123    if (isnan(scaleX))
124        scaleX = 1;
125    if (isnan(scaleY))
126        scaleY = scaleX;
127    if (isnan(scaleZ))
128        scaleZ = 1;
129    return WebKitCSSMatrix::create(TransformationMatrix(m_matrix).scale3d(scaleX, scaleY, scaleZ));
130}
131
132PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::rotate(double rotX, double rotY, double rotZ) const
133{
134    if (isnan(rotX))
135        rotX = 0;
136
137    if (isnan(rotY) && isnan(rotZ)) {
138        rotZ = rotX;
139        rotX = 0;
140        rotY = 0;
141    }
142
143    if (isnan(rotY))
144        rotY = 0;
145    if (isnan(rotZ))
146        rotZ = 0;
147    return WebKitCSSMatrix::create(TransformationMatrix(m_matrix).rotate3d(rotX, rotY, rotZ));
148}
149
150PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::rotateAxisAngle(double x, double y, double z, double angle) const
151{
152    if (isnan(x))
153        x = 0;
154    if (isnan(y))
155        y = 0;
156    if (isnan(z))
157        z = 0;
158    if (isnan(angle))
159        angle = 0;
160    if (x == 0 && y == 0 && z == 0)
161        z = 1;
162    return WebKitCSSMatrix::create(TransformationMatrix(m_matrix).rotate3d(x, y, z, angle));
163}
164
165PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::skewX(double angle) const
166{
167    if (isnan(angle))
168        angle = 0;
169    return WebKitCSSMatrix::create(TransformationMatrix(m_matrix).skewX(angle));
170}
171
172PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::skewY(double angle) const
173{
174    if (isnan(angle))
175        angle = 0;
176    return WebKitCSSMatrix::create(TransformationMatrix(m_matrix).skewY(angle));
177}
178
179String WebKitCSSMatrix::toString() const
180{
181    // FIXME - Need to ensure valid CSS floating point values (https://bugs.webkit.org/show_bug.cgi?id=20674)
182    if (m_matrix.isAffine())
183        return String::format("matrix(%f, %f, %f, %f, %f, %f)",
184                                m_matrix.a(), m_matrix.b(), m_matrix.c(), m_matrix.d(), m_matrix.e(), m_matrix.f());
185    return String::format("matrix3d(%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)",
186                            m_matrix.m11(), m_matrix.m12(), m_matrix.m13(), m_matrix.m14(),
187                            m_matrix.m21(), m_matrix.m22(), m_matrix.m23(), m_matrix.m24(),
188                            m_matrix.m31(), m_matrix.m32(), m_matrix.m33(), m_matrix.m34(),
189                            m_matrix.m41(), m_matrix.m42(), m_matrix.m43(), m_matrix.m44());
190}
191
192} // namespace WebCore
193