1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "mozilla/dom/DOMMatrix.h"
8
9 #include "mozilla/dom/BindingUtils.h"
10 #include "mozilla/dom/DOMMatrixBinding.h"
11 #include "mozilla/dom/DOMPoint.h"
12 #include "mozilla/dom/DOMPointBinding.h"
13 #include "mozilla/dom/BindingDeclarations.h"
14 #include "mozilla/dom/ToJSValue.h"
15 #include "mozilla/ServoCSSParser.h"
16 #include "nsCSSParser.h"
17 #include "nsStyleTransformMatrix.h"
18
19 #include <math.h>
20
21 namespace mozilla {
22 namespace dom {
23
24 static const double radPerDegree = 2.0 * M_PI / 360.0;
25
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMMatrixReadOnly,mParent)26 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMMatrixReadOnly, mParent)
27
28 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DOMMatrixReadOnly, AddRef)
29 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DOMMatrixReadOnly, Release)
30
31 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::Translate(double aTx, double aTy,
32 double aTz) const {
33 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
34 retval->TranslateSelf(aTx, aTy, aTz);
35
36 return retval.forget();
37 }
38
Scale(double aScale,double aOriginX,double aOriginY) const39 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::Scale(double aScale,
40 double aOriginX,
41 double aOriginY) const {
42 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
43 retval->ScaleSelf(aScale, aOriginX, aOriginY);
44
45 return retval.forget();
46 }
47
Scale3d(double aScale,double aOriginX,double aOriginY,double aOriginZ) const48 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::Scale3d(double aScale,
49 double aOriginX,
50 double aOriginY,
51 double aOriginZ) const {
52 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
53 retval->Scale3dSelf(aScale, aOriginX, aOriginY, aOriginZ);
54
55 return retval.forget();
56 }
57
ScaleNonUniform(double aScaleX,double aScaleY,double aScaleZ,double aOriginX,double aOriginY,double aOriginZ) const58 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::ScaleNonUniform(
59 double aScaleX, double aScaleY, double aScaleZ, double aOriginX,
60 double aOriginY, double aOriginZ) const {
61 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
62 retval->ScaleNonUniformSelf(aScaleX, aScaleY, aScaleZ, aOriginX, aOriginY,
63 aOriginZ);
64
65 return retval.forget();
66 }
67
Rotate(double aAngle,double aOriginX,double aOriginY) const68 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::Rotate(double aAngle,
69 double aOriginX,
70 double aOriginY) const {
71 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
72 retval->RotateSelf(aAngle, aOriginX, aOriginY);
73
74 return retval.forget();
75 }
76
RotateFromVector(double x,double y) const77 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::RotateFromVector(
78 double x, double y) const {
79 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
80 retval->RotateFromVectorSelf(x, y);
81
82 return retval.forget();
83 }
84
RotateAxisAngle(double aX,double aY,double aZ,double aAngle) const85 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::RotateAxisAngle(
86 double aX, double aY, double aZ, double aAngle) const {
87 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
88 retval->RotateAxisAngleSelf(aX, aY, aZ, aAngle);
89
90 return retval.forget();
91 }
92
SkewX(double aSx) const93 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::SkewX(double aSx) const {
94 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
95 retval->SkewXSelf(aSx);
96
97 return retval.forget();
98 }
99
SkewY(double aSy) const100 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::SkewY(double aSy) const {
101 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
102 retval->SkewYSelf(aSy);
103
104 return retval.forget();
105 }
106
Multiply(const DOMMatrix & other) const107 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::Multiply(
108 const DOMMatrix& other) const {
109 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
110 retval->MultiplySelf(other);
111
112 return retval.forget();
113 }
114
FlipX() const115 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::FlipX() const {
116 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
117 if (mMatrix3D) {
118 gfx::Matrix4x4 m;
119 m._11 = -1;
120 retval->mMatrix3D = new gfx::Matrix4x4(m * *mMatrix3D);
121 } else {
122 gfx::Matrix m;
123 m._11 = -1;
124 retval->mMatrix2D = new gfx::Matrix(mMatrix2D ? m * *mMatrix2D : m);
125 }
126
127 return retval.forget();
128 }
129
FlipY() const130 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::FlipY() const {
131 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
132 if (mMatrix3D) {
133 gfx::Matrix4x4 m;
134 m._22 = -1;
135 retval->mMatrix3D = new gfx::Matrix4x4(m * *mMatrix3D);
136 } else {
137 gfx::Matrix m;
138 m._22 = -1;
139 retval->mMatrix2D = new gfx::Matrix(mMatrix2D ? m * *mMatrix2D : m);
140 }
141
142 return retval.forget();
143 }
144
Inverse() const145 already_AddRefed<DOMMatrix> DOMMatrixReadOnly::Inverse() const {
146 RefPtr<DOMMatrix> retval = new DOMMatrix(mParent, *this);
147 retval->InvertSelf();
148
149 return retval.forget();
150 }
151
Is2D() const152 bool DOMMatrixReadOnly::Is2D() const { return !mMatrix3D; }
153
IsIdentity() const154 bool DOMMatrixReadOnly::IsIdentity() const {
155 if (mMatrix3D) {
156 return mMatrix3D->IsIdentity();
157 }
158
159 return mMatrix2D->IsIdentity();
160 }
161
TransformPoint(const DOMPointInit & point) const162 already_AddRefed<DOMPoint> DOMMatrixReadOnly::TransformPoint(
163 const DOMPointInit& point) const {
164 RefPtr<DOMPoint> retval = new DOMPoint(mParent);
165
166 if (mMatrix3D) {
167 gfx::Point4D transformedPoint;
168 transformedPoint.x = point.mX;
169 transformedPoint.y = point.mY;
170 transformedPoint.z = point.mZ;
171 transformedPoint.w = point.mW;
172
173 transformedPoint = mMatrix3D->TransformPoint(transformedPoint);
174
175 retval->SetX(transformedPoint.x);
176 retval->SetY(transformedPoint.y);
177 retval->SetZ(transformedPoint.z);
178 retval->SetW(transformedPoint.w);
179 } else if (point.mZ != 0 || point.mW != 1.0) {
180 gfx::Matrix4x4 tempMatrix(gfx::Matrix4x4::From2D(*mMatrix2D));
181
182 gfx::Point4D transformedPoint;
183 transformedPoint.x = point.mX;
184 transformedPoint.y = point.mY;
185 transformedPoint.z = point.mZ;
186 transformedPoint.w = point.mW;
187
188 transformedPoint = tempMatrix.TransformPoint(transformedPoint);
189
190 retval->SetX(transformedPoint.x);
191 retval->SetY(transformedPoint.y);
192 retval->SetZ(transformedPoint.z);
193 retval->SetW(transformedPoint.w);
194 } else {
195 gfx::Point transformedPoint;
196 transformedPoint.x = point.mX;
197 transformedPoint.y = point.mY;
198
199 transformedPoint = mMatrix2D->TransformPoint(transformedPoint);
200
201 retval->SetX(transformedPoint.x);
202 retval->SetY(transformedPoint.y);
203 retval->SetZ(point.mZ);
204 retval->SetW(point.mW);
205 }
206 return retval.forget();
207 }
208
209 template <typename T>
GetDataFromMatrix(const DOMMatrixReadOnly * aMatrix,T * aData)210 void GetDataFromMatrix(const DOMMatrixReadOnly* aMatrix, T* aData) {
211 aData[0] = static_cast<T>(aMatrix->M11());
212 aData[1] = static_cast<T>(aMatrix->M12());
213 aData[2] = static_cast<T>(aMatrix->M13());
214 aData[3] = static_cast<T>(aMatrix->M14());
215 aData[4] = static_cast<T>(aMatrix->M21());
216 aData[5] = static_cast<T>(aMatrix->M22());
217 aData[6] = static_cast<T>(aMatrix->M23());
218 aData[7] = static_cast<T>(aMatrix->M24());
219 aData[8] = static_cast<T>(aMatrix->M31());
220 aData[9] = static_cast<T>(aMatrix->M32());
221 aData[10] = static_cast<T>(aMatrix->M33());
222 aData[11] = static_cast<T>(aMatrix->M34());
223 aData[12] = static_cast<T>(aMatrix->M41());
224 aData[13] = static_cast<T>(aMatrix->M42());
225 aData[14] = static_cast<T>(aMatrix->M43());
226 aData[15] = static_cast<T>(aMatrix->M44());
227 }
228
ToFloat32Array(JSContext * aCx,JS::MutableHandle<JSObject * > aResult,ErrorResult & aRv) const229 void DOMMatrixReadOnly::ToFloat32Array(JSContext* aCx,
230 JS::MutableHandle<JSObject*> aResult,
231 ErrorResult& aRv) const {
232 AutoTArray<float, 16> arr;
233 arr.SetLength(16);
234 GetDataFromMatrix(this, arr.Elements());
235 JS::Rooted<JS::Value> value(aCx);
236 if (!ToJSValue(aCx, TypedArrayCreator<Float32Array>(arr), &value)) {
237 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
238 return;
239 }
240 aResult.set(&value.toObject());
241 }
242
ToFloat64Array(JSContext * aCx,JS::MutableHandle<JSObject * > aResult,ErrorResult & aRv) const243 void DOMMatrixReadOnly::ToFloat64Array(JSContext* aCx,
244 JS::MutableHandle<JSObject*> aResult,
245 ErrorResult& aRv) const {
246 AutoTArray<double, 16> arr;
247 arr.SetLength(16);
248 GetDataFromMatrix(this, arr.Elements());
249 JS::Rooted<JS::Value> value(aCx);
250 if (!ToJSValue(aCx, TypedArrayCreator<Float64Array>(arr), &value)) {
251 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
252 return;
253 }
254 aResult.set(&value.toObject());
255 }
256
257 // Convenient way to append things as floats, not doubles. We use this because
258 // we only want to output about 6 digits of precision for our matrix()
259 // functions, to preserve the behavior we used to have when we used
260 // AppendPrintf.
AppendFloat(nsAString & aStr,float f)261 static void AppendFloat(nsAString& aStr, float f) { aStr.AppendFloat(f); }
262
Stringify(nsAString & aResult)263 void DOMMatrixReadOnly::Stringify(nsAString& aResult) {
264 nsAutoString matrixStr;
265 if (mMatrix3D) {
266 // We can't use AppendPrintf here, because it does locale-specific
267 // formatting of floating-point values.
268 matrixStr.AssignLiteral("matrix3d(");
269 AppendFloat(matrixStr, M11());
270 matrixStr.AppendLiteral(", ");
271 AppendFloat(matrixStr, M12());
272 matrixStr.AppendLiteral(", ");
273 AppendFloat(matrixStr, M13());
274 matrixStr.AppendLiteral(", ");
275 AppendFloat(matrixStr, M14());
276 matrixStr.AppendLiteral(", ");
277 AppendFloat(matrixStr, M21());
278 matrixStr.AppendLiteral(", ");
279 AppendFloat(matrixStr, M22());
280 matrixStr.AppendLiteral(", ");
281 AppendFloat(matrixStr, M23());
282 matrixStr.AppendLiteral(", ");
283 AppendFloat(matrixStr, M24());
284 matrixStr.AppendLiteral(", ");
285 AppendFloat(matrixStr, M31());
286 matrixStr.AppendLiteral(", ");
287 AppendFloat(matrixStr, M32());
288 matrixStr.AppendLiteral(", ");
289 AppendFloat(matrixStr, M33());
290 matrixStr.AppendLiteral(", ");
291 AppendFloat(matrixStr, M34());
292 matrixStr.AppendLiteral(", ");
293 AppendFloat(matrixStr, M41());
294 matrixStr.AppendLiteral(", ");
295 AppendFloat(matrixStr, M42());
296 matrixStr.AppendLiteral(", ");
297 AppendFloat(matrixStr, M43());
298 matrixStr.AppendLiteral(", ");
299 AppendFloat(matrixStr, M44());
300 matrixStr.AppendLiteral(")");
301 } else {
302 // We can't use AppendPrintf here, because it does locale-specific
303 // formatting of floating-point values.
304 matrixStr.AssignLiteral("matrix(");
305 AppendFloat(matrixStr, A());
306 matrixStr.AppendLiteral(", ");
307 AppendFloat(matrixStr, B());
308 matrixStr.AppendLiteral(", ");
309 AppendFloat(matrixStr, C());
310 matrixStr.AppendLiteral(", ");
311 AppendFloat(matrixStr, D());
312 matrixStr.AppendLiteral(", ");
313 AppendFloat(matrixStr, E());
314 matrixStr.AppendLiteral(", ");
315 AppendFloat(matrixStr, F());
316 matrixStr.AppendLiteral(")");
317 }
318
319 aResult = matrixStr;
320 }
321
IsStyledByServo(JSContext * aContext)322 static bool IsStyledByServo(JSContext* aContext) {
323 nsGlobalWindowInner* win = xpc::CurrentWindowOrNull(aContext);
324 nsIDocument* doc = win ? win->GetDoc() : nullptr;
325 return doc ? doc->IsStyledByServo() : false;
326 }
327
Constructor(const GlobalObject & aGlobal,ErrorResult & aRv)328 already_AddRefed<DOMMatrix> DOMMatrix::Constructor(const GlobalObject& aGlobal,
329 ErrorResult& aRv) {
330 RefPtr<DOMMatrix> obj = new DOMMatrix(aGlobal.GetAsSupports(),
331 IsStyledByServo(aGlobal.Context()));
332 return obj.forget();
333 }
334
Constructor(const GlobalObject & aGlobal,const nsAString & aTransformList,ErrorResult & aRv)335 already_AddRefed<DOMMatrix> DOMMatrix::Constructor(
336 const GlobalObject& aGlobal, const nsAString& aTransformList,
337 ErrorResult& aRv) {
338 RefPtr<DOMMatrix> obj = new DOMMatrix(aGlobal.GetAsSupports(),
339 IsStyledByServo(aGlobal.Context()));
340
341 obj = obj->SetMatrixValue(aTransformList, aRv);
342 return obj.forget();
343 }
344
Constructor(const GlobalObject & aGlobal,const DOMMatrixReadOnly & aOther,ErrorResult & aRv)345 already_AddRefed<DOMMatrix> DOMMatrix::Constructor(
346 const GlobalObject& aGlobal, const DOMMatrixReadOnly& aOther,
347 ErrorResult& aRv) {
348 RefPtr<DOMMatrix> obj = new DOMMatrix(aGlobal.GetAsSupports(), aOther);
349 return obj.forget();
350 }
351
352 template <typename T>
SetDataInMatrix(DOMMatrix * aMatrix,const T * aData,int aLength,ErrorResult & aRv)353 void SetDataInMatrix(DOMMatrix* aMatrix, const T* aData, int aLength,
354 ErrorResult& aRv) {
355 if (aLength == 16) {
356 aMatrix->SetM11(aData[0]);
357 aMatrix->SetM12(aData[1]);
358 aMatrix->SetM13(aData[2]);
359 aMatrix->SetM14(aData[3]);
360 aMatrix->SetM21(aData[4]);
361 aMatrix->SetM22(aData[5]);
362 aMatrix->SetM23(aData[6]);
363 aMatrix->SetM24(aData[7]);
364 aMatrix->SetM31(aData[8]);
365 aMatrix->SetM32(aData[9]);
366 aMatrix->SetM33(aData[10]);
367 aMatrix->SetM34(aData[11]);
368 aMatrix->SetM41(aData[12]);
369 aMatrix->SetM42(aData[13]);
370 aMatrix->SetM43(aData[14]);
371 aMatrix->SetM44(aData[15]);
372 } else if (aLength == 6) {
373 aMatrix->SetA(aData[0]);
374 aMatrix->SetB(aData[1]);
375 aMatrix->SetC(aData[2]);
376 aMatrix->SetD(aData[3]);
377 aMatrix->SetE(aData[4]);
378 aMatrix->SetF(aData[5]);
379 } else {
380 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
381 }
382 }
383
Constructor(const GlobalObject & aGlobal,const Float32Array & aArray32,ErrorResult & aRv)384 already_AddRefed<DOMMatrix> DOMMatrix::Constructor(const GlobalObject& aGlobal,
385 const Float32Array& aArray32,
386 ErrorResult& aRv) {
387 RefPtr<DOMMatrix> obj = new DOMMatrix(aGlobal.GetAsSupports(),
388 IsStyledByServo(aGlobal.Context()));
389 aArray32.ComputeLengthAndData();
390 SetDataInMatrix(obj, aArray32.Data(), aArray32.Length(), aRv);
391
392 return obj.forget();
393 }
394
Constructor(const GlobalObject & aGlobal,const Float64Array & aArray64,ErrorResult & aRv)395 already_AddRefed<DOMMatrix> DOMMatrix::Constructor(const GlobalObject& aGlobal,
396 const Float64Array& aArray64,
397 ErrorResult& aRv) {
398 RefPtr<DOMMatrix> obj = new DOMMatrix(aGlobal.GetAsSupports(),
399 IsStyledByServo(aGlobal.Context()));
400 aArray64.ComputeLengthAndData();
401 SetDataInMatrix(obj, aArray64.Data(), aArray64.Length(), aRv);
402
403 return obj.forget();
404 }
405
Constructor(const GlobalObject & aGlobal,const Sequence<double> & aNumberSequence,ErrorResult & aRv)406 already_AddRefed<DOMMatrix> DOMMatrix::Constructor(
407 const GlobalObject& aGlobal, const Sequence<double>& aNumberSequence,
408 ErrorResult& aRv) {
409 RefPtr<DOMMatrix> obj = new DOMMatrix(aGlobal.GetAsSupports(),
410 IsStyledByServo(aGlobal.Context()));
411 SetDataInMatrix(obj, aNumberSequence.Elements(), aNumberSequence.Length(),
412 aRv);
413
414 return obj.forget();
415 }
416
Ensure3DMatrix()417 void DOMMatrix::Ensure3DMatrix() {
418 if (!mMatrix3D) {
419 mMatrix3D = new gfx::Matrix4x4(gfx::Matrix4x4::From2D(*mMatrix2D));
420 mMatrix2D = nullptr;
421 }
422 }
423
MultiplySelf(const DOMMatrix & aOther)424 DOMMatrix* DOMMatrix::MultiplySelf(const DOMMatrix& aOther) {
425 if (aOther.IsIdentity()) {
426 return this;
427 }
428
429 if (aOther.Is2D()) {
430 if (mMatrix3D) {
431 *mMatrix3D = gfx::Matrix4x4::From2D(*aOther.mMatrix2D) * *mMatrix3D;
432 } else {
433 *mMatrix2D = *aOther.mMatrix2D * *mMatrix2D;
434 }
435 } else {
436 Ensure3DMatrix();
437 *mMatrix3D = *aOther.mMatrix3D * *mMatrix3D;
438 }
439
440 return this;
441 }
442
PreMultiplySelf(const DOMMatrix & aOther)443 DOMMatrix* DOMMatrix::PreMultiplySelf(const DOMMatrix& aOther) {
444 if (aOther.IsIdentity()) {
445 return this;
446 }
447
448 if (aOther.Is2D()) {
449 if (mMatrix3D) {
450 *mMatrix3D = *mMatrix3D * gfx::Matrix4x4::From2D(*aOther.mMatrix2D);
451 } else {
452 *mMatrix2D = *mMatrix2D * *aOther.mMatrix2D;
453 }
454 } else {
455 Ensure3DMatrix();
456 *mMatrix3D = *mMatrix3D * *aOther.mMatrix3D;
457 }
458
459 return this;
460 }
461
TranslateSelf(double aTx,double aTy,double aTz)462 DOMMatrix* DOMMatrix::TranslateSelf(double aTx, double aTy, double aTz) {
463 if (aTx == 0 && aTy == 0 && aTz == 0) {
464 return this;
465 }
466
467 if (mMatrix3D || aTz != 0) {
468 Ensure3DMatrix();
469 mMatrix3D->PreTranslate(aTx, aTy, aTz);
470 } else {
471 mMatrix2D->PreTranslate(aTx, aTy);
472 }
473
474 return this;
475 }
476
ScaleSelf(double aScale,double aOriginX,double aOriginY)477 DOMMatrix* DOMMatrix::ScaleSelf(double aScale, double aOriginX,
478 double aOriginY) {
479 ScaleNonUniformSelf(aScale, aScale, 1.0, aOriginX, aOriginY, 0);
480
481 return this;
482 }
483
Scale3dSelf(double aScale,double aOriginX,double aOriginY,double aOriginZ)484 DOMMatrix* DOMMatrix::Scale3dSelf(double aScale, double aOriginX,
485 double aOriginY, double aOriginZ) {
486 ScaleNonUniformSelf(aScale, aScale, aScale, aOriginX, aOriginY, aOriginZ);
487
488 return this;
489 }
490
ScaleNonUniformSelf(double aScaleX,double aScaleY,double aScaleZ,double aOriginX,double aOriginY,double aOriginZ)491 DOMMatrix* DOMMatrix::ScaleNonUniformSelf(double aScaleX, double aScaleY,
492 double aScaleZ, double aOriginX,
493 double aOriginY, double aOriginZ) {
494 if (aScaleX == 1.0 && aScaleY == 1.0 && aScaleZ == 1.0) {
495 return this;
496 }
497
498 TranslateSelf(aOriginX, aOriginY, aOriginZ);
499
500 if (mMatrix3D || aScaleZ != 1.0 || aOriginZ != 0) {
501 Ensure3DMatrix();
502 gfx::Matrix4x4 m;
503 m._11 = aScaleX;
504 m._22 = aScaleY;
505 m._33 = aScaleZ;
506 *mMatrix3D = m * *mMatrix3D;
507 } else {
508 gfx::Matrix m;
509 m._11 = aScaleX;
510 m._22 = aScaleY;
511 *mMatrix2D = m * *mMatrix2D;
512 }
513
514 TranslateSelf(-aOriginX, -aOriginY, -aOriginZ);
515
516 return this;
517 }
518
RotateFromVectorSelf(double aX,double aY)519 DOMMatrix* DOMMatrix::RotateFromVectorSelf(double aX, double aY) {
520 if (aX == 0.0 || aY == 0.0) {
521 return this;
522 }
523
524 RotateSelf(atan2(aY, aX) / radPerDegree);
525
526 return this;
527 }
528
RotateSelf(double aAngle,double aOriginX,double aOriginY)529 DOMMatrix* DOMMatrix::RotateSelf(double aAngle, double aOriginX,
530 double aOriginY) {
531 if (fmod(aAngle, 360) == 0) {
532 return this;
533 }
534
535 TranslateSelf(aOriginX, aOriginY);
536
537 if (mMatrix3D) {
538 RotateAxisAngleSelf(0, 0, 1, aAngle);
539 } else {
540 *mMatrix2D = mMatrix2D->PreRotate(aAngle * radPerDegree);
541 }
542
543 TranslateSelf(-aOriginX, -aOriginY);
544
545 return this;
546 }
547
RotateAxisAngleSelf(double aX,double aY,double aZ,double aAngle)548 DOMMatrix* DOMMatrix::RotateAxisAngleSelf(double aX, double aY, double aZ,
549 double aAngle) {
550 if (fmod(aAngle, 360) == 0) {
551 return this;
552 }
553
554 aAngle *= radPerDegree;
555
556 Ensure3DMatrix();
557 gfx::Matrix4x4 m;
558 m.SetRotateAxisAngle(aX, aY, aZ, aAngle);
559
560 *mMatrix3D = m * *mMatrix3D;
561
562 return this;
563 }
564
SkewXSelf(double aSx)565 DOMMatrix* DOMMatrix::SkewXSelf(double aSx) {
566 if (fmod(aSx, 360) == 0) {
567 return this;
568 }
569
570 if (mMatrix3D) {
571 gfx::Matrix4x4 m;
572 m._21 = tan(aSx * radPerDegree);
573 *mMatrix3D = m * *mMatrix3D;
574 } else {
575 gfx::Matrix m;
576 m._21 = tan(aSx * radPerDegree);
577 *mMatrix2D = m * *mMatrix2D;
578 }
579
580 return this;
581 }
582
SkewYSelf(double aSy)583 DOMMatrix* DOMMatrix::SkewYSelf(double aSy) {
584 if (fmod(aSy, 360) == 0) {
585 return this;
586 }
587
588 if (mMatrix3D) {
589 gfx::Matrix4x4 m;
590 m._12 = tan(aSy * radPerDegree);
591 *mMatrix3D = m * *mMatrix3D;
592 } else {
593 gfx::Matrix m;
594 m._12 = tan(aSy * radPerDegree);
595 *mMatrix2D = m * *mMatrix2D;
596 }
597
598 return this;
599 }
600
InvertSelf()601 DOMMatrix* DOMMatrix::InvertSelf() {
602 if (mMatrix3D) {
603 if (!mMatrix3D->Invert()) {
604 mMatrix3D->SetNAN();
605 }
606 } else if (!mMatrix2D->Invert()) {
607 mMatrix2D = nullptr;
608
609 mMatrix3D = new gfx::Matrix4x4();
610 mMatrix3D->SetNAN();
611 }
612
613 return this;
614 }
615
SetMatrixValue(const nsAString & aTransformList,ErrorResult & aRv)616 DOMMatrix* DOMMatrix::SetMatrixValue(const nsAString& aTransformList,
617 ErrorResult& aRv) {
618 // An empty string is a no-op.
619 if (aTransformList.IsEmpty()) {
620 return this;
621 }
622
623 gfx::Matrix4x4 transform;
624 bool contains3dTransform = false;
625 if (mIsServo) {
626 if (!ServoCSSParser::ParseTransformIntoMatrix(
627 aTransformList, contains3dTransform, transform.components)) {
628 aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
629 return nullptr;
630 }
631 } else {
632 #ifdef MOZ_OLD_STYLE
633 nsCSSValue value;
634 nsCSSParser parser;
635 bool parseSuccess =
636 parser.ParseTransformProperty(aTransformList, true, value);
637 if (!parseSuccess) {
638 aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
639 return nullptr;
640 }
641
642 // A value of "none" results in a 2D identity matrix.
643 if (value.GetUnit() == eCSSUnit_None) {
644 mMatrix3D = nullptr;
645 mMatrix2D = new gfx::Matrix();
646 return this;
647 }
648
649 // A value other than a transform-list is a syntax error.
650 if (value.GetUnit() != eCSSUnit_SharedList) {
651 aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
652 return nullptr;
653 }
654
655 RuleNodeCacheConditions dummy;
656 nsStyleTransformMatrix::TransformReferenceBox dummyBox;
657 transform = nsStyleTransformMatrix::ReadTransforms(
658 value.GetSharedListValue()->mHead, nullptr, nullptr, dummy, dummyBox,
659 nsPresContext::AppUnitsPerCSSPixel(), &contains3dTransform);
660 #else
661 MOZ_CRASH("old style system disabled");
662 #endif
663 }
664
665 if (!contains3dTransform) {
666 mMatrix3D = nullptr;
667 mMatrix2D = new gfx::Matrix();
668
669 SetA(transform._11);
670 SetB(transform._12);
671 SetC(transform._21);
672 SetD(transform._22);
673 SetE(transform._41);
674 SetF(transform._42);
675 } else {
676 mMatrix3D = new gfx::Matrix4x4(transform);
677 mMatrix2D = nullptr;
678 }
679
680 return this;
681 }
682
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)683 JSObject* DOMMatrix::WrapObject(JSContext* aCx,
684 JS::Handle<JSObject*> aGivenProto) {
685 return DOMMatrixBinding::Wrap(aCx, this, aGivenProto);
686 }
687
688 } // namespace dom
689 } // namespace mozilla
690