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 #ifndef MOZILLA_DOM_DOMMATRIX_H_
8 #define MOZILLA_DOM_DOMMATRIX_H_
9 
10 #include "nsWrapperCache.h"
11 #include "nsISupports.h"
12 #include "nsCycleCollectionParticipant.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/ErrorResult.h"
15 #include "nsCOMPtr.h"
16 #include "mozilla/dom/BindingDeclarations.h"
17 #include "mozilla/dom/TypedArray.h"
18 #include "mozilla/gfx/Matrix.h" // for Matrix4x4
19 
20 namespace mozilla {
21 namespace dom {
22 
23 class GlobalObject;
24 class DOMMatrix;
25 class DOMPoint;
26 struct DOMPointInit;
27 
28 class DOMMatrixReadOnly : public nsWrapperCache
29 {
30 public:
DOMMatrixReadOnly(nsISupports * aParent)31   explicit DOMMatrixReadOnly(nsISupports* aParent)
32     : mParent(aParent), mMatrix2D(new gfx::Matrix())
33   {
34   }
35 
DOMMatrixReadOnly(nsISupports * aParent,const DOMMatrixReadOnly & other)36   DOMMatrixReadOnly(nsISupports* aParent, const DOMMatrixReadOnly& other)
37     : mParent(aParent)
38   {
39     if (other.mMatrix2D) {
40       mMatrix2D = new gfx::Matrix(*other.mMatrix2D);
41     } else {
42       mMatrix3D = new gfx::Matrix4x4(*other.mMatrix3D);
43     }
44   }
45 
46   NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DOMMatrixReadOnly)
47   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(DOMMatrixReadOnly)
48 
49 #define GetMatrixMember(entry2D, entry3D, default) \
50 { \
51   if (mMatrix3D) { \
52     return mMatrix3D->entry3D; \
53   } \
54   return mMatrix2D->entry2D; \
55 }
56 
57 #define Get3DMatrixMember(entry3D, default) \
58 { \
59   if (mMatrix3D) { \
60     return mMatrix3D->entry3D; \
61   } \
62   return default; \
63 }
64 
65   double A() const GetMatrixMember(_11, _11, 1.0)
66   double B() const GetMatrixMember(_12, _12, 0)
67   double C() const GetMatrixMember(_21, _21, 0)
68   double D() const GetMatrixMember(_22, _22, 1.0)
69   double E() const GetMatrixMember(_31, _41, 0)
70   double F() const GetMatrixMember(_32, _42, 0)
71 
72   double M11() const GetMatrixMember(_11, _11, 1.0)
73   double M12() const GetMatrixMember(_12, _12, 0)
74   double M13() const Get3DMatrixMember(_13, 0)
75   double M14() const Get3DMatrixMember(_14, 0)
76   double M21() const GetMatrixMember(_21, _21, 0)
77   double M22() const GetMatrixMember(_22, _22, 1.0)
78   double M23() const Get3DMatrixMember(_23, 0)
79   double M24() const Get3DMatrixMember(_24, 0)
80   double M31() const Get3DMatrixMember(_31, 0)
81   double M32() const Get3DMatrixMember(_32, 0)
82   double M33() const Get3DMatrixMember(_33, 1.0)
83   double M34() const Get3DMatrixMember(_34, 0)
84   double M41() const GetMatrixMember(_31, _41, 0)
85   double M42() const GetMatrixMember(_32, _42, 0)
86   double M43() const Get3DMatrixMember(_43, 0)
87   double M44() const Get3DMatrixMember(_44, 1.0)
88 
89 #undef GetMatrixMember
90 #undef Get3DMatrixMember
91 
92   already_AddRefed<DOMMatrix> Translate(double aTx,
93                                         double aTy,
94                                         double aTz = 0) const;
95   already_AddRefed<DOMMatrix> Scale(double aScale,
96                                     double aOriginX = 0,
97                                     double aOriginY = 0) const;
98   already_AddRefed<DOMMatrix> Scale3d(double aScale,
99                                       double aOriginX = 0,
100                                       double aOriginY = 0,
101                                       double aOriginZ = 0) const;
102   already_AddRefed<DOMMatrix> ScaleNonUniform(double aScaleX,
103                                               double aScaleY = 1.0,
104                                               double aScaleZ = 1.0,
105                                               double aOriginX = 0,
106                                               double aOriginY = 0,
107                                               double aOriginZ = 0) const;
108   already_AddRefed<DOMMatrix> Rotate(double aAngle,
109                                      double aOriginX = 0,
110                                      double aOriginY = 0) const;
111   already_AddRefed<DOMMatrix> RotateFromVector(double aX,
112                                                double aY) const;
113   already_AddRefed<DOMMatrix> RotateAxisAngle(double aX,
114                                               double aY,
115                                               double aZ,
116                                               double aAngle) const;
117   already_AddRefed<DOMMatrix> SkewX(double aSx) const;
118   already_AddRefed<DOMMatrix> SkewY(double aSy) const;
119   already_AddRefed<DOMMatrix> Multiply(const DOMMatrix& aOther) const;
120   already_AddRefed<DOMMatrix> FlipX() const;
121   already_AddRefed<DOMMatrix> FlipY() const;
122   already_AddRefed<DOMMatrix> Inverse() const;
123 
124   bool                        Is2D() const;
125   bool                        Identity() const;
126   already_AddRefed<DOMPoint>  TransformPoint(const DOMPointInit& aPoint) const;
127   void                        ToFloat32Array(JSContext* aCx,
128                                              JS::MutableHandle<JSObject*> aResult,
129                                              ErrorResult& aRv) const;
130   void                        ToFloat64Array(JSContext* aCx,
131                                              JS::MutableHandle<JSObject*> aResult,
132                                              ErrorResult& aRv) const;
133   void                        Stringify(nsAString& aResult);
134 protected:
135   nsCOMPtr<nsISupports>     mParent;
136   nsAutoPtr<gfx::Matrix>    mMatrix2D;
137   nsAutoPtr<gfx::Matrix4x4> mMatrix3D;
138 
~DOMMatrixReadOnly()139   virtual ~DOMMatrixReadOnly() {}
140 
141 private:
142   DOMMatrixReadOnly() = delete;
143   DOMMatrixReadOnly(const DOMMatrixReadOnly&) = delete;
144   DOMMatrixReadOnly& operator=(const DOMMatrixReadOnly&) = delete;
145 };
146 
147 class DOMMatrix : public DOMMatrixReadOnly
148 {
149 public:
DOMMatrix(nsISupports * aParent)150   explicit DOMMatrix(nsISupports* aParent)
151     : DOMMatrixReadOnly(aParent)
152   {}
153 
DOMMatrix(nsISupports * aParent,const DOMMatrixReadOnly & other)154   DOMMatrix(nsISupports* aParent, const DOMMatrixReadOnly& other)
155     : DOMMatrixReadOnly(aParent, other)
156   {}
157 
158   static already_AddRefed<DOMMatrix>
159   Constructor(const GlobalObject& aGlobal, ErrorResult& aRv);
160   static already_AddRefed<DOMMatrix>
161   Constructor(const GlobalObject& aGlobal, const nsAString& aTransformList, ErrorResult& aRv);
162   static already_AddRefed<DOMMatrix>
163   Constructor(const GlobalObject& aGlobal, const DOMMatrixReadOnly& aOther, ErrorResult& aRv);
164   static already_AddRefed<DOMMatrix>
165   Constructor(const GlobalObject& aGlobal, const Float32Array& aArray32, ErrorResult& aRv);
166   static already_AddRefed<DOMMatrix>
167   Constructor(const GlobalObject& aGlobal, const Float64Array& aArray64, ErrorResult& aRv);
168   static already_AddRefed<DOMMatrix>
169   Constructor(const GlobalObject& aGlobal, const Sequence<double>& aNumberSequence, ErrorResult& aRv);
170 
GetParentObject()171   nsISupports* GetParentObject() const { return mParent; }
172   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
173 
174 #define Set2DMatrixMember(entry2D, entry3D) \
175 { \
176   if (mMatrix3D) { \
177     mMatrix3D->entry3D = v; \
178   } else { \
179     mMatrix2D->entry2D = v; \
180   } \
181 }
182 
183 #define Set3DMatrixMember(entry3D, default) \
184 { \
185   if (mMatrix3D || (v != default)) { \
186     Ensure3DMatrix(); \
187     mMatrix3D->entry3D = v; \
188   } \
189 }
190 
191   void SetA(double v) Set2DMatrixMember(_11, _11)
192   void SetB(double v) Set2DMatrixMember(_12, _12)
193   void SetC(double v) Set2DMatrixMember(_21, _21)
194   void SetD(double v) Set2DMatrixMember(_22, _22)
195   void SetE(double v) Set2DMatrixMember(_31, _41)
196   void SetF(double v) Set2DMatrixMember(_32, _42)
197 
198   void SetM11(double v) Set2DMatrixMember(_11, _11)
199   void SetM12(double v) Set2DMatrixMember(_12, _12)
200   void SetM13(double v) Set3DMatrixMember(_13, 0)
201   void SetM14(double v) Set3DMatrixMember(_14, 0)
202   void SetM21(double v) Set2DMatrixMember(_21, _21)
203   void SetM22(double v) Set2DMatrixMember(_22, _22)
204   void SetM23(double v) Set3DMatrixMember(_23, 0)
205   void SetM24(double v) Set3DMatrixMember(_24, 0)
206   void SetM31(double v) Set3DMatrixMember(_31, 0)
207   void SetM32(double v) Set3DMatrixMember(_32, 0)
208   void SetM33(double v) Set3DMatrixMember(_33, 1.0)
209   void SetM34(double v) Set3DMatrixMember(_34, 0)
210   void SetM41(double v) Set2DMatrixMember(_31, _41)
211   void SetM42(double v) Set2DMatrixMember(_32, _42)
212   void SetM43(double v) Set3DMatrixMember(_43, 0)
213   void SetM44(double v) Set3DMatrixMember(_44, 1.0)
214 
215 #undef Set2DMatrixMember
216 #undef Set3DMatrixMember
217 
218   DOMMatrix* MultiplySelf(const DOMMatrix& aOther);
219   DOMMatrix* PreMultiplySelf(const DOMMatrix& aOther);
220   DOMMatrix* TranslateSelf(double aTx,
221                            double aTy,
222                            double aTz = 0);
223   DOMMatrix* ScaleSelf(double aScale,
224                        double aOriginX = 0,
225                        double aOriginY = 0);
226   DOMMatrix* Scale3dSelf(double aScale,
227                          double aOriginX = 0,
228                          double aOriginY = 0,
229                          double aOriginZ = 0);
230   DOMMatrix* ScaleNonUniformSelf(double aScaleX,
231                                  double aScaleY = 1,
232                                  double aScaleZ = 1,
233                                  double aOriginX = 0,
234                                  double aOriginY = 0,
235                                  double aOriginZ = 0);
236   DOMMatrix* RotateSelf(double aAngle,
237                         double aOriginX = 0,
238                         double aOriginY = 0);
239   DOMMatrix* RotateFromVectorSelf(double aX,
240                                   double aY);
241   DOMMatrix* RotateAxisAngleSelf(double aX,
242                                  double aY,
243                                  double aZ,
244                                  double aAngle);
245   DOMMatrix* SkewXSelf(double aSx);
246   DOMMatrix* SkewYSelf(double aSy);
247   DOMMatrix* InvertSelf();
248   DOMMatrix* SetMatrixValue(const nsAString& aTransformList, ErrorResult& aRv);
249 protected:
250   void Ensure3DMatrix();
251 
~DOMMatrix()252   virtual ~DOMMatrix() {}
253 };
254 
255 } // namespace dom
256 } // namespace mozilla
257 
258 #endif /*MOZILLA_DOM_DOMMATRIX_H_*/
259