OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "core/dom/DOMMatrix.h" | |
6 | |
7 namespace blink { | |
8 | |
9 DOMMatrix* DOMMatrix::create(ExceptionState& exceptionState) { | |
10 return new DOMMatrix(TransformationMatrix()); | |
11 } | |
12 | |
13 DOMMatrix* DOMMatrix::create(DOMMatrixReadOnly* other, | |
14 ExceptionState& exceptionState) { | |
15 return new DOMMatrix(other->matrix(), other->is2D()); | |
16 } | |
17 | |
18 DOMMatrix* DOMMatrix::create(const SkMatrix44& matrix, | |
19 ExceptionState& exceptionState) { | |
20 TransformationMatrix transformationMatrix(matrix); | |
21 return new DOMMatrix(transformationMatrix, transformationMatrix.isAffine()); | |
22 } | |
23 | |
24 DOMMatrix* DOMMatrix::create(const String& transformList, | |
25 ExceptionState& exceptionState) { | |
26 DOMMatrix* matrix = new DOMMatrix(TransformationMatrix()); | |
27 matrix->setMatrixValueFromString(transformList, exceptionState); | |
28 return matrix; | |
29 } | |
30 | |
31 DOMMatrix* DOMMatrix::create(Vector<double> sequence, | |
32 ExceptionState& exceptionState) { | |
33 if (sequence.size() != 6 && sequence.size() != 16) { | |
34 exceptionState.throwTypeError( | |
35 "The sequence must contain 6 elements for a 2D matrix or 16 elements " | |
36 "for a 3D matrix."); | |
37 return nullptr; | |
38 } | |
39 return new DOMMatrix(sequence, sequence.size()); | |
40 } | |
41 | |
42 DOMMatrix* DOMMatrix::fromFloat32Array(DOMFloat32Array* float32Array, | |
43 ExceptionState& exceptionState) { | |
44 if (float32Array->length() != 6 && float32Array->length() != 16) { | |
45 exceptionState.throwTypeError( | |
46 "The sequence must contain 6 elements for a 2D matrix or 16 elements " | |
47 "for a 3D matrix."); | |
48 return nullptr; | |
49 } | |
50 return new DOMMatrix(float32Array->data(), float32Array->length()); | |
51 } | |
52 | |
53 DOMMatrix* DOMMatrix::fromFloat64Array(DOMFloat64Array* float64Array, | |
54 ExceptionState& exceptionState) { | |
55 if (float64Array->length() != 6 && float64Array->length() != 16) { | |
56 exceptionState.throwTypeError( | |
57 "The sequence must contain 6 elements for a 2D matrix or 16 elements " | |
58 "for a 3D matrix."); | |
59 return nullptr; | |
60 } | |
61 return new DOMMatrix(float64Array->data(), float64Array->length()); | |
62 } | |
63 | |
64 template <typename T> | |
65 DOMMatrix::DOMMatrix(T sequence, int size) | |
66 : DOMMatrixReadOnly(sequence, size) {} | |
67 | |
68 DOMMatrix::DOMMatrix(const TransformationMatrix& matrix, bool is2D) | |
69 : DOMMatrixReadOnly(matrix, is2D) {} | |
70 | |
71 DOMMatrix* DOMMatrix::fromMatrix(DOMMatrixInit& other, | |
72 ExceptionState& exceptionState) { | |
73 if (!validateAndFixup(other, exceptionState)) { | |
74 DCHECK(exceptionState.hadException()); | |
75 return nullptr; | |
76 } | |
77 if (other.is2D()) { | |
78 return new DOMMatrix({other.m11(), other.m12(), other.m21(), other.m22(), | |
79 other.m41(), other.m42()}, | |
80 other.is2D()); | |
81 } | |
82 | |
83 return new DOMMatrix({other.m11(), other.m12(), other.m13(), other.m14(), | |
84 other.m21(), other.m22(), other.m23(), other.m24(), | |
85 other.m31(), other.m32(), other.m33(), other.m34(), | |
86 other.m41(), other.m42(), other.m43(), other.m44()}, | |
87 other.is2D()); | |
88 } | |
89 | |
90 void DOMMatrix::setIs2D(bool value) { | |
91 if (m_is2D) | |
92 m_is2D = value; | |
93 } | |
94 | |
95 void DOMMatrix::setNAN() { | |
96 m_matrix->setM11(NAN); | |
97 m_matrix->setM12(NAN); | |
98 m_matrix->setM13(NAN); | |
99 m_matrix->setM14(NAN); | |
100 m_matrix->setM21(NAN); | |
101 m_matrix->setM22(NAN); | |
102 m_matrix->setM23(NAN); | |
103 m_matrix->setM24(NAN); | |
104 m_matrix->setM31(NAN); | |
105 m_matrix->setM32(NAN); | |
106 m_matrix->setM33(NAN); | |
107 m_matrix->setM34(NAN); | |
108 m_matrix->setM41(NAN); | |
109 m_matrix->setM42(NAN); | |
110 m_matrix->setM43(NAN); | |
111 m_matrix->setM44(NAN); | |
112 } | |
113 | |
114 DOMMatrix* DOMMatrix::multiplySelf(DOMMatrixInit& other, | |
115 ExceptionState& exceptionState) { | |
116 DOMMatrix* otherMatrix = DOMMatrix::fromMatrix(other, exceptionState); | |
117 if (!otherMatrix) { | |
118 DCHECK(exceptionState.hadException()); | |
119 return nullptr; | |
120 } | |
121 if (!otherMatrix->is2D()) | |
122 m_is2D = false; | |
123 | |
124 *m_matrix *= otherMatrix->matrix(); | |
125 | |
126 return this; | |
127 } | |
128 | |
129 DOMMatrix* DOMMatrix::preMultiplySelf(DOMMatrixInit& other, | |
130 ExceptionState& exceptionState) { | |
131 DOMMatrix* otherMatrix = DOMMatrix::fromMatrix(other, exceptionState); | |
132 if (!otherMatrix) { | |
133 DCHECK(exceptionState.hadException()); | |
134 return nullptr; | |
135 } | |
136 if (!otherMatrix->is2D()) | |
137 m_is2D = false; | |
138 | |
139 TransformationMatrix& matrix = *m_matrix; | |
140 *m_matrix = otherMatrix->matrix() * matrix; | |
141 | |
142 return this; | |
143 } | |
144 | |
145 DOMMatrix* DOMMatrix::translateSelf(double tx, double ty, double tz) { | |
146 if (!tx && !ty && !tz) | |
147 return this; | |
148 | |
149 if (tz) | |
150 m_is2D = false; | |
151 | |
152 if (m_is2D) | |
153 m_matrix->translate(tx, ty); | |
154 else | |
155 m_matrix->translate3d(tx, ty, tz); | |
156 | |
157 return this; | |
158 } | |
159 | |
160 DOMMatrix* DOMMatrix::scaleSelf(double sx) { | |
161 return scaleSelf(sx, sx); | |
162 } | |
163 | |
164 DOMMatrix* DOMMatrix::scaleSelf(double sx, | |
165 double sy, | |
166 double sz, | |
167 double ox, | |
168 double oy, | |
169 double oz) { | |
170 if (sz != 1 || oz) | |
171 m_is2D = false; | |
172 | |
173 if (sx == 1 && sy == 1 && sz == 1) | |
174 return this; | |
175 | |
176 bool hasTranslation = (ox || oy || oz); | |
177 | |
178 if (hasTranslation) | |
179 translateSelf(ox, oy, oz); | |
180 | |
181 if (m_is2D) | |
182 m_matrix->scaleNonUniform(sx, sy); | |
183 else | |
184 m_matrix->scale3d(sx, sy, sz); | |
185 | |
186 if (hasTranslation) | |
187 translateSelf(-ox, -oy, -oz); | |
188 | |
189 return this; | |
190 } | |
191 | |
192 DOMMatrix* DOMMatrix::scale3dSelf(double scale, | |
193 double ox, | |
194 double oy, | |
195 double oz) { | |
196 return scaleSelf(scale, scale, scale, ox, oy, oz); | |
197 } | |
198 | |
199 DOMMatrix* DOMMatrix::rotateSelf(double rotX) { | |
200 return rotateSelf(0, 0, rotX); | |
201 } | |
202 | |
203 DOMMatrix* DOMMatrix::rotateSelf(double rotX, double rotY) { | |
204 return rotateSelf(rotX, rotY, 0); | |
205 } | |
206 | |
207 DOMMatrix* DOMMatrix::rotateSelf(double rotX, double rotY, double rotZ) { | |
208 if (rotZ) | |
209 m_matrix->rotate3d(0, 0, 1, rotZ); | |
210 | |
211 if (rotY) { | |
212 m_matrix->rotate3d(0, 1, 0, rotY); | |
213 m_is2D = false; | |
214 } | |
215 | |
216 if (rotX) { | |
217 m_matrix->rotate3d(1, 0, 0, rotX); | |
218 m_is2D = false; | |
219 } | |
220 | |
221 return this; | |
222 } | |
223 | |
224 DOMMatrix* DOMMatrix::rotateFromVectorSelf(double x, double y) { | |
225 m_matrix->rotate(rad2deg(atan2(y, x))); | |
226 return this; | |
227 } | |
228 | |
229 DOMMatrix* DOMMatrix::rotateAxisAngleSelf(double x, | |
230 double y, | |
231 double z, | |
232 double angle) { | |
233 m_matrix->rotate3d(x, y, z, angle); | |
234 | |
235 if (x != 0 || y != 0) | |
236 m_is2D = false; | |
237 | |
238 return this; | |
239 } | |
240 | |
241 DOMMatrix* DOMMatrix::skewXSelf(double sx) { | |
242 m_matrix->skewX(sx); | |
243 return this; | |
244 } | |
245 | |
246 DOMMatrix* DOMMatrix::skewYSelf(double sy) { | |
247 m_matrix->skewY(sy); | |
248 return this; | |
249 } | |
250 | |
251 DOMMatrix* DOMMatrix::invertSelf() { | |
252 if (m_matrix->isInvertible()) { | |
253 m_matrix = TransformationMatrix::create(m_matrix->inverse()); | |
254 } else { | |
255 setNAN(); | |
256 setIs2D(false); | |
257 } | |
258 return this; | |
259 } | |
260 | |
261 DOMMatrix* DOMMatrix::setMatrixValue(const String& inputString, | |
262 ExceptionState& exceptionState) { | |
263 setMatrixValueFromString(inputString, exceptionState); | |
264 return this; | |
265 } | |
266 | |
267 } // namespace blink | |
OLD | NEW |