• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/css/CSSMatrix.h"
28 
29 #include "CSSPropertyNames.h"
30 #include "CSSValueKeywords.h"
31 #include "bindings/v8/ExceptionState.h"
32 #include "core/css/CSSParser.h"
33 #include "core/css/CSSToLengthConversionData.h"
34 #include "core/css/StylePropertySet.h"
35 #include "core/css/resolver/TransformBuilder.h"
36 #include "core/dom/ExceptionCode.h"
37 #include "core/rendering/style/RenderStyle.h"
38 #include "core/rendering/style/StyleInheritedData.h"
39 #include "wtf/MathExtras.h"
40 
41 namespace WebCore {
42 
CSSMatrix(const TransformationMatrix & m)43 CSSMatrix::CSSMatrix(const TransformationMatrix& m)
44     : m_matrix(m)
45 {
46     ScriptWrappable::init(this);
47 }
48 
CSSMatrix(const String & s,ExceptionState & exceptionState)49 CSSMatrix::CSSMatrix(const String& s, ExceptionState& exceptionState)
50 {
51     ScriptWrappable::init(this);
52     setMatrixValue(s, exceptionState);
53 }
54 
setMatrixValue(const String & string,ExceptionState & exceptionState)55 void CSSMatrix::setMatrixValue(const String& string, ExceptionState& exceptionState)
56 {
57     if (string.isEmpty())
58         return;
59 
60     RefPtr<MutableStylePropertySet> styleDeclaration = MutableStylePropertySet::create();
61     if (CSSParser::parseValue(styleDeclaration.get(), CSSPropertyWebkitTransform, string, true, HTMLStandardMode, 0)) {
62         // Convert to TransformOperations. This can fail if a property
63         // requires style (i.e., param uses 'ems' or 'exs')
64         RefPtr<CSSValue> value = styleDeclaration->getPropertyCSSValue(CSSPropertyWebkitTransform);
65 
66         // Check for a "none" or empty transform. In these cases we can use the default identity matrix.
67         if (!value || (value->isPrimitiveValue() && (toCSSPrimitiveValue(value.get()))->getValueID() == CSSValueNone))
68             return;
69 
70         DEFINE_STATIC_REF(RenderStyle, defaultStyle, RenderStyle::createDefaultStyle());
71         TransformOperations operations;
72         if (!TransformBuilder::createTransformOperations(value.get(), CSSToLengthConversionData(defaultStyle, defaultStyle), operations)) {
73             exceptionState.throwUninformativeAndGenericDOMException(SyntaxError);
74             return;
75         }
76 
77         // Convert transform operations to a TransformationMatrix. This can fail
78         // if a param has a percentage ('%')
79         if (operations.dependsOnBoxSize())
80             exceptionState.throwUninformativeAndGenericDOMException(SyntaxError);
81         TransformationMatrix t;
82         operations.apply(FloatSize(0, 0), t);
83 
84         // set the matrix
85         m_matrix = t;
86     } else { // There is something there but parsing failed.
87         exceptionState.throwUninformativeAndGenericDOMException(SyntaxError);
88     }
89 }
90 
91 // Perform a concatenation of the matrices (this * secondMatrix)
multiply(CSSMatrix * secondMatrix) const92 PassRefPtr<CSSMatrix> CSSMatrix::multiply(CSSMatrix* secondMatrix) const
93 {
94     if (!secondMatrix)
95         return 0;
96 
97     return CSSMatrix::create(TransformationMatrix(m_matrix).multiply(secondMatrix->m_matrix));
98 }
99 
inverse(ExceptionState & exceptionState) const100 PassRefPtr<CSSMatrix> CSSMatrix::inverse(ExceptionState& exceptionState) const
101 {
102     if (!m_matrix.isInvertible()) {
103         exceptionState.throwUninformativeAndGenericDOMException(NotSupportedError);
104         return 0;
105     }
106 
107     return CSSMatrix::create(m_matrix.inverse());
108 }
109 
translate(double x,double y,double z) const110 PassRefPtr<CSSMatrix> CSSMatrix::translate(double x, double y, double z) const
111 {
112     if (std::isnan(x))
113         x = 0;
114     if (std::isnan(y))
115         y = 0;
116     if (std::isnan(z))
117         z = 0;
118     return CSSMatrix::create(TransformationMatrix(m_matrix).translate3d(x, y, z));
119 }
120 
scale(double scaleX,double scaleY,double scaleZ) const121 PassRefPtr<CSSMatrix> CSSMatrix::scale(double scaleX, double scaleY, double scaleZ) const
122 {
123     if (std::isnan(scaleX))
124         scaleX = 1;
125     if (std::isnan(scaleY))
126         scaleY = scaleX;
127     if (std::isnan(scaleZ))
128         scaleZ = 1;
129     return CSSMatrix::create(TransformationMatrix(m_matrix).scale3d(scaleX, scaleY, scaleZ));
130 }
131 
rotate(double rotX,double rotY,double rotZ) const132 PassRefPtr<CSSMatrix> CSSMatrix::rotate(double rotX, double rotY, double rotZ) const
133 {
134     if (std::isnan(rotX))
135         rotX = 0;
136 
137     if (std::isnan(rotY) && std::isnan(rotZ)) {
138         rotZ = rotX;
139         rotX = 0;
140         rotY = 0;
141     }
142 
143     if (std::isnan(rotY))
144         rotY = 0;
145     if (std::isnan(rotZ))
146         rotZ = 0;
147     return CSSMatrix::create(TransformationMatrix(m_matrix).rotate3d(rotX, rotY, rotZ));
148 }
149 
rotateAxisAngle(double x,double y,double z,double angle) const150 PassRefPtr<CSSMatrix> CSSMatrix::rotateAxisAngle(double x, double y, double z, double angle) const
151 {
152     if (std::isnan(x))
153         x = 0;
154     if (std::isnan(y))
155         y = 0;
156     if (std::isnan(z))
157         z = 0;
158     if (std::isnan(angle))
159         angle = 0;
160     if (!x && !y && !z)
161         z = 1;
162     return CSSMatrix::create(TransformationMatrix(m_matrix).rotate3d(x, y, z, angle));
163 }
164 
skewX(double angle) const165 PassRefPtr<CSSMatrix> CSSMatrix::skewX(double angle) const
166 {
167     if (std::isnan(angle))
168         angle = 0;
169     return CSSMatrix::create(TransformationMatrix(m_matrix).skewX(angle));
170 }
171 
skewY(double angle) const172 PassRefPtr<CSSMatrix> CSSMatrix::skewY(double angle) const
173 {
174     if (std::isnan(angle))
175         angle = 0;
176     return CSSMatrix::create(TransformationMatrix(m_matrix).skewY(angle));
177 }
178 
toString() const179 String CSSMatrix::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)", m_matrix.a(), m_matrix.b(), m_matrix.c(), m_matrix.d(), m_matrix.e(), m_matrix.f());
184     return String::format("matrix3d(%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)",
185     m_matrix.m11(), m_matrix.m12(), m_matrix.m13(), m_matrix.m14(),
186     m_matrix.m21(), m_matrix.m22(), m_matrix.m23(), m_matrix.m24(),
187     m_matrix.m31(), m_matrix.m32(), m_matrix.m33(), m_matrix.m34(),
188     m_matrix.m41(), m_matrix.m42(), m_matrix.m43(), m_matrix.m44());
189 }
190 
191 } // namespace WebCore
192