• Home
  • History
  • Annotate
  • 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 "bindings/core/v8/ExceptionState.h"
30 #include "core/CSSPropertyNames.h"
31 #include "core/CSSValueKeywords.h"
32 #include "core/css/CSSToLengthConversionData.h"
33 #include "core/css/StylePropertySet.h"
34 #include "core/css/parser/CSSParser.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 blink {
42 
CSSMatrix(const TransformationMatrix & m)43 CSSMatrix::CSSMatrix(const TransformationMatrix& m)
44     : m_matrix(m)
45 {
46 }
47 
CSSMatrix(const String & s,ExceptionState & exceptionState)48 CSSMatrix::CSSMatrix(const String& s, ExceptionState& exceptionState)
49 {
50     setMatrixValue(s, exceptionState);
51 }
52 
setMatrixValue(const String & string,ExceptionState & exceptionState)53 void CSSMatrix::setMatrixValue(const String& string, ExceptionState& exceptionState)
54 {
55     if (string.isEmpty())
56         return;
57 
58     // FIXME: crbug.com/154772 - should this continue to use legacy style parsing?
59     if (RefPtrWillBeRawPtr<CSSValue> value = CSSParser::parseSingleValue(CSSPropertyWebkitTransform, string)) {
60         // Check for a "none" transform. In these cases we can use the default identity matrix.
61         if (value->isPrimitiveValue() && (toCSSPrimitiveValue(value.get()))->getValueID() == CSSValueNone)
62             return;
63 
64         // FIXME: This has a null pointer crash if we use ex units (crbug.com/414145)
65         DEFINE_STATIC_REF(RenderStyle, defaultStyle, RenderStyle::createDefaultStyle());
66         TransformOperations operations;
67         if (!TransformBuilder::createTransformOperations(value.get(), CSSToLengthConversionData(defaultStyle, defaultStyle, 0, 0, 1.0f), operations)) {
68             exceptionState.throwDOMException(SyntaxError, "Failed to interpret '" + string + "' as a transformation operation.");
69             return;
70         }
71 
72         // Convert transform operations to a TransformationMatrix. This can fail
73         // if a param has a percentage ('%')
74         if (operations.dependsOnBoxSize())
75             exceptionState.throwDOMException(SyntaxError, "The transformation depends on the box size, which is not supported.");
76         TransformationMatrix t;
77         operations.apply(FloatSize(0, 0), t);
78 
79         // set the matrix
80         m_matrix = t;
81     } else { // There is something there but parsing failed.
82         exceptionState.throwDOMException(SyntaxError, "Failed to parse '" + string + "'.");
83     }
84 }
85 
86 // Perform a concatenation of the matrices (this * secondMatrix)
multiply(CSSMatrix * secondMatrix) const87 PassRefPtrWillBeRawPtr<CSSMatrix> CSSMatrix::multiply(CSSMatrix* secondMatrix) const
88 {
89     if (!secondMatrix)
90         return nullptr;
91 
92     return CSSMatrix::create(TransformationMatrix(m_matrix).multiply(secondMatrix->m_matrix));
93 }
94 
inverse(ExceptionState & exceptionState) const95 PassRefPtrWillBeRawPtr<CSSMatrix> CSSMatrix::inverse(ExceptionState& exceptionState) const
96 {
97     if (!m_matrix.isInvertible()) {
98         exceptionState.throwDOMException(NotSupportedError, "The matrix is not invertable.");
99         return nullptr;
100     }
101 
102     return CSSMatrix::create(m_matrix.inverse());
103 }
104 
translate(double x,double y,double z) const105 PassRefPtrWillBeRawPtr<CSSMatrix> CSSMatrix::translate(double x, double y, double z) const
106 {
107     if (std::isnan(x))
108         x = 0;
109     if (std::isnan(y))
110         y = 0;
111     if (std::isnan(z))
112         z = 0;
113     return CSSMatrix::create(TransformationMatrix(m_matrix).translate3d(x, y, z));
114 }
115 
scale(double scaleX,double scaleY,double scaleZ) const116 PassRefPtrWillBeRawPtr<CSSMatrix> CSSMatrix::scale(double scaleX, double scaleY, double scaleZ) const
117 {
118     if (std::isnan(scaleX))
119         scaleX = 1;
120     if (std::isnan(scaleY))
121         scaleY = scaleX;
122     if (std::isnan(scaleZ))
123         scaleZ = 1;
124     return CSSMatrix::create(TransformationMatrix(m_matrix).scale3d(scaleX, scaleY, scaleZ));
125 }
126 
rotate(double rotX,double rotY,double rotZ) const127 PassRefPtrWillBeRawPtr<CSSMatrix> CSSMatrix::rotate(double rotX, double rotY, double rotZ) const
128 {
129     if (std::isnan(rotX))
130         rotX = 0;
131 
132     if (std::isnan(rotY) && std::isnan(rotZ)) {
133         rotZ = rotX;
134         rotX = 0;
135         rotY = 0;
136     }
137 
138     if (std::isnan(rotY))
139         rotY = 0;
140     if (std::isnan(rotZ))
141         rotZ = 0;
142     return CSSMatrix::create(TransformationMatrix(m_matrix).rotate3d(rotX, rotY, rotZ));
143 }
144 
rotateAxisAngle(double x,double y,double z,double angle) const145 PassRefPtrWillBeRawPtr<CSSMatrix> CSSMatrix::rotateAxisAngle(double x, double y, double z, double angle) const
146 {
147     if (std::isnan(x))
148         x = 0;
149     if (std::isnan(y))
150         y = 0;
151     if (std::isnan(z))
152         z = 0;
153     if (std::isnan(angle))
154         angle = 0;
155     if (!x && !y && !z)
156         z = 1;
157     return CSSMatrix::create(TransformationMatrix(m_matrix).rotate3d(x, y, z, angle));
158 }
159 
skewX(double angle) const160 PassRefPtrWillBeRawPtr<CSSMatrix> CSSMatrix::skewX(double angle) const
161 {
162     if (std::isnan(angle))
163         angle = 0;
164     return CSSMatrix::create(TransformationMatrix(m_matrix).skewX(angle));
165 }
166 
skewY(double angle) const167 PassRefPtrWillBeRawPtr<CSSMatrix> CSSMatrix::skewY(double angle) const
168 {
169     if (std::isnan(angle))
170         angle = 0;
171     return CSSMatrix::create(TransformationMatrix(m_matrix).skewY(angle));
172 }
173 
toString() const174 String CSSMatrix::toString() const
175 {
176     // FIXME - Need to ensure valid CSS floating point values (https://bugs.webkit.org/show_bug.cgi?id=20674)
177     if (m_matrix.isAffine())
178         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());
179     return String::format("matrix3d(%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)",
180     m_matrix.m11(), m_matrix.m12(), m_matrix.m13(), m_matrix.m14(),
181     m_matrix.m21(), m_matrix.m22(), m_matrix.m23(), m_matrix.m24(),
182     m_matrix.m31(), m_matrix.m32(), m_matrix.m33(), m_matrix.m34(),
183     m_matrix.m41(), m_matrix.m42(), m_matrix.m43(), m_matrix.m44());
184 }
185 
186 } // namespace blink
187