OLD | NEW |
| (Empty) |
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 "core/css/CSSMatrix.h" | |
27 | |
28 #include "bindings/core/v8/ExceptionState.h" | |
29 #include "core/CSSPropertyNames.h" | |
30 #include "core/CSSValueKeywords.h" | |
31 #include "core/css/CSSIdentifierValue.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/frame/UseCounter.h" | |
38 #include "core/layout/api/LayoutViewItem.h" | |
39 #include "core/style/ComputedStyle.h" | |
40 #include "core/style/StyleInheritedData.h" | |
41 #include "wtf/MathExtras.h" | |
42 | |
43 namespace blink { | |
44 | |
45 CSSMatrix* CSSMatrix::create(ExecutionContext* executionContext, | |
46 const String& s, | |
47 ExceptionState& exceptionState) { | |
48 UseCounter::count(executionContext, UseCounter::WebKitCSSMatrix); | |
49 return new CSSMatrix(s, exceptionState); | |
50 } | |
51 | |
52 CSSMatrix::CSSMatrix(const TransformationMatrix& m) | |
53 : m_matrix(TransformationMatrix::create(m)) {} | |
54 | |
55 CSSMatrix::CSSMatrix(const String& s, ExceptionState& exceptionState) | |
56 : m_matrix(TransformationMatrix::create()) { | |
57 setMatrixValue(s, exceptionState); | |
58 } | |
59 | |
60 static inline PassRefPtr<ComputedStyle> createInitialStyle() { | |
61 RefPtr<ComputedStyle> initialStyle = ComputedStyle::create(); | |
62 initialStyle->font().update(nullptr); | |
63 return initialStyle; | |
64 } | |
65 | |
66 void CSSMatrix::setMatrixValue(const String& string, | |
67 ExceptionState& exceptionState) { | |
68 if (string.isEmpty()) | |
69 return; | |
70 | |
71 if (const CSSValue* value = | |
72 CSSParser::parseSingleValue(CSSPropertyTransform, string)) { | |
73 // Check for a "none" transform. In these cases we can use the default | |
74 // identity matrix. | |
75 if (value->isIdentifierValue() && | |
76 (toCSSIdentifierValue(value))->getValueID() == CSSValueNone) | |
77 return; | |
78 | |
79 DEFINE_STATIC_REF(ComputedStyle, initialStyle, createInitialStyle()); | |
80 TransformOperations operations = | |
81 TransformBuilder::createTransformOperations( | |
82 *value, CSSToLengthConversionData(initialStyle, initialStyle, | |
83 LayoutViewItem(nullptr), 1.0f)); | |
84 | |
85 // Convert transform operations to a TransformationMatrix. This can fail | |
86 // if a param has a percentage ('%') | |
87 if (operations.dependsOnBoxSize()) | |
88 exceptionState.throwDOMException(SyntaxError, | |
89 "The transformation depends on the box " | |
90 "size, which is not supported."); | |
91 m_matrix = TransformationMatrix::create(); | |
92 operations.apply(FloatSize(0, 0), *m_matrix); | |
93 } else { // There is something there but parsing failed. | |
94 exceptionState.throwDOMException(SyntaxError, | |
95 "Failed to parse '" + string + "'."); | |
96 } | |
97 } | |
98 | |
99 // Perform a concatenation of the matrices (this * secondMatrix) | |
100 CSSMatrix* CSSMatrix::multiply(CSSMatrix* secondMatrix) const { | |
101 if (!secondMatrix) | |
102 return nullptr; | |
103 | |
104 return CSSMatrix::create( | |
105 TransformationMatrix(*m_matrix).multiply(*secondMatrix->m_matrix)); | |
106 } | |
107 | |
108 CSSMatrix* CSSMatrix::inverse(ExceptionState& exceptionState) const { | |
109 if (!m_matrix->isInvertible()) { | |
110 exceptionState.throwDOMException(NotSupportedError, | |
111 "The matrix is not invertable."); | |
112 return nullptr; | |
113 } | |
114 | |
115 return CSSMatrix::create(m_matrix->inverse()); | |
116 } | |
117 | |
118 CSSMatrix* CSSMatrix::translate(double x, double y, double z) const { | |
119 if (std::isnan(x)) | |
120 x = 0; | |
121 if (std::isnan(y)) | |
122 y = 0; | |
123 if (std::isnan(z)) | |
124 z = 0; | |
125 return CSSMatrix::create( | |
126 TransformationMatrix(*m_matrix).translate3d(x, y, z)); | |
127 } | |
128 | |
129 CSSMatrix* CSSMatrix::scale(double scaleX, double scaleY, double scaleZ) const { | |
130 if (std::isnan(scaleX)) | |
131 scaleX = 1; | |
132 if (std::isnan(scaleY)) | |
133 scaleY = scaleX; | |
134 if (std::isnan(scaleZ)) | |
135 scaleZ = 1; | |
136 return CSSMatrix::create( | |
137 TransformationMatrix(*m_matrix).scale3d(scaleX, scaleY, scaleZ)); | |
138 } | |
139 | |
140 CSSMatrix* CSSMatrix::rotate(double rotX, double rotY, double rotZ) const { | |
141 if (std::isnan(rotX)) | |
142 rotX = 0; | |
143 | |
144 if (std::isnan(rotY) && std::isnan(rotZ)) { | |
145 rotZ = rotX; | |
146 rotX = 0; | |
147 rotY = 0; | |
148 } | |
149 | |
150 if (std::isnan(rotY)) | |
151 rotY = 0; | |
152 if (std::isnan(rotZ)) | |
153 rotZ = 0; | |
154 return CSSMatrix::create( | |
155 TransformationMatrix(*m_matrix).rotate3d(rotX, rotY, rotZ)); | |
156 } | |
157 | |
158 CSSMatrix* CSSMatrix::rotateAxisAngle(double x, | |
159 double y, | |
160 double z, | |
161 double angle) const { | |
162 if (std::isnan(x)) | |
163 x = 0; | |
164 if (std::isnan(y)) | |
165 y = 0; | |
166 if (std::isnan(z)) | |
167 z = 0; | |
168 if (std::isnan(angle)) | |
169 angle = 0; | |
170 if (!x && !y && !z) | |
171 z = 1; | |
172 return CSSMatrix::create( | |
173 TransformationMatrix(*m_matrix).rotate3d(x, y, z, angle)); | |
174 } | |
175 | |
176 CSSMatrix* CSSMatrix::skewX(double angle) const { | |
177 if (std::isnan(angle)) | |
178 angle = 0; | |
179 return CSSMatrix::create(TransformationMatrix(*m_matrix).skewX(angle)); | |
180 } | |
181 | |
182 CSSMatrix* CSSMatrix::skewY(double angle) const { | |
183 if (std::isnan(angle)) | |
184 angle = 0; | |
185 return CSSMatrix::create(TransformationMatrix(*m_matrix).skewY(angle)); | |
186 } | |
187 | |
188 String CSSMatrix::toString() const { | |
189 // FIXME - Need to ensure valid CSS floating point values | |
190 // (https://bugs.webkit.org/show_bug.cgi?id=20674) | |
191 if (m_matrix->isAffine()) | |
192 return String::format("matrix(%f, %f, %f, %f, %f, %f)", m_matrix->a(), | |
193 m_matrix->b(), m_matrix->c(), m_matrix->d(), | |
194 m_matrix->e(), m_matrix->f()); | |
195 return String::format( | |
196 "matrix3d(%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, " | |
197 "%f)", | |
198 m_matrix->m11(), m_matrix->m12(), m_matrix->m13(), m_matrix->m14(), | |
199 m_matrix->m21(), m_matrix->m22(), m_matrix->m23(), m_matrix->m24(), | |
200 m_matrix->m31(), m_matrix->m32(), m_matrix->m33(), m_matrix->m34(), | |
201 m_matrix->m41(), m_matrix->m42(), m_matrix->m43(), m_matrix->m44()); | |
202 } | |
203 | |
204 } // namespace blink | |
OLD | NEW |