/src/mozilla-central/dom/base/WebKitCSSMatrix.cpp
Line | Count | Source (jump to first uncovered line) |
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/WebKitCSSMatrix.h" |
8 | | |
9 | | #include "mozilla/dom/BindingUtils.h" |
10 | | #include "mozilla/dom/WebKitCSSMatrixBinding.h" |
11 | | #include "mozilla/Preferences.h" |
12 | | #include "nsPresContext.h" |
13 | | #include "nsGlobalWindowInner.h" |
14 | | |
15 | | namespace mozilla { |
16 | | namespace dom { |
17 | | |
18 | | static const double sRadPerDegree = 2.0 * M_PI / 360.0; |
19 | | |
20 | | bool |
21 | | WebKitCSSMatrix::FeatureEnabled(JSContext* aCx, JSObject* aObj) |
22 | 0 | { |
23 | 0 | return Preferences::GetBool("layout.css.DOMMatrix.enabled", false) && |
24 | 0 | Preferences::GetBool("layout.css.prefixes.webkit", false); |
25 | 0 | } |
26 | | |
27 | | already_AddRefed<WebKitCSSMatrix> |
28 | | WebKitCSSMatrix::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv) |
29 | 0 | { |
30 | 0 | RefPtr<WebKitCSSMatrix> obj = new WebKitCSSMatrix(aGlobal.GetAsSupports()); |
31 | 0 | return obj.forget(); |
32 | 0 | } |
33 | | |
34 | | already_AddRefed<WebKitCSSMatrix> |
35 | | WebKitCSSMatrix::Constructor(const GlobalObject& aGlobal, |
36 | | const nsAString& aTransformList, ErrorResult& aRv) |
37 | 0 | { |
38 | 0 | RefPtr<WebKitCSSMatrix> obj = new WebKitCSSMatrix(aGlobal.GetAsSupports()); |
39 | 0 | obj = obj->SetMatrixValue(aTransformList, aRv); |
40 | 0 | return obj.forget(); |
41 | 0 | } |
42 | | |
43 | | already_AddRefed<WebKitCSSMatrix> |
44 | | WebKitCSSMatrix::Constructor(const GlobalObject& aGlobal, |
45 | | const DOMMatrixReadOnly& aOther, ErrorResult& aRv) |
46 | 0 | { |
47 | 0 | RefPtr<WebKitCSSMatrix> obj = new WebKitCSSMatrix(aGlobal.GetAsSupports(), |
48 | 0 | aOther); |
49 | 0 | return obj.forget(); |
50 | 0 | } |
51 | | |
52 | | JSObject* |
53 | | WebKitCSSMatrix::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
54 | 0 | { |
55 | 0 | return WebKitCSSMatrix_Binding::Wrap(aCx, this, aGivenProto); |
56 | 0 | } |
57 | | |
58 | | WebKitCSSMatrix* |
59 | | WebKitCSSMatrix::SetMatrixValue(const nsAString& aTransformList, |
60 | | ErrorResult& aRv) |
61 | 0 | { |
62 | 0 | DOMMatrix::SetMatrixValue(aTransformList, aRv); |
63 | 0 | return this; |
64 | 0 | } |
65 | | |
66 | | already_AddRefed<WebKitCSSMatrix> |
67 | | WebKitCSSMatrix::Multiply(const WebKitCSSMatrix& other) const |
68 | 0 | { |
69 | 0 | RefPtr<WebKitCSSMatrix> retval = new WebKitCSSMatrix(mParent, *this); |
70 | 0 | retval->MultiplySelf(other); |
71 | 0 |
|
72 | 0 | return retval.forget(); |
73 | 0 | } |
74 | | |
75 | | already_AddRefed<WebKitCSSMatrix> |
76 | | WebKitCSSMatrix::Inverse(ErrorResult& aRv) const |
77 | 0 | { |
78 | 0 | RefPtr<WebKitCSSMatrix> retval = new WebKitCSSMatrix(mParent, *this); |
79 | 0 | retval->InvertSelfThrow(aRv); |
80 | 0 | if (NS_WARN_IF(aRv.Failed())) { |
81 | 0 | return nullptr; |
82 | 0 | } |
83 | 0 | |
84 | 0 | return retval.forget(); |
85 | 0 | } |
86 | | |
87 | | WebKitCSSMatrix* |
88 | | WebKitCSSMatrix::InvertSelfThrow(ErrorResult& aRv) |
89 | 0 | { |
90 | 0 | if (mMatrix3D) { |
91 | 0 | if (!mMatrix3D->Invert()) { |
92 | 0 | aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
93 | 0 | return nullptr; |
94 | 0 | } |
95 | 0 | } else if (!mMatrix2D->Invert()) { |
96 | 0 | aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
97 | 0 | return nullptr; |
98 | 0 | } |
99 | 0 | |
100 | 0 | return this; |
101 | 0 | } |
102 | | |
103 | | already_AddRefed<WebKitCSSMatrix> |
104 | | WebKitCSSMatrix::Translate(double aTx, |
105 | | double aTy, |
106 | | double aTz) const |
107 | 0 | { |
108 | 0 | RefPtr<WebKitCSSMatrix> retval = new WebKitCSSMatrix(mParent, *this); |
109 | 0 | retval->TranslateSelf(aTx, aTy, aTz); |
110 | 0 |
|
111 | 0 | return retval.forget(); |
112 | 0 | } |
113 | | |
114 | | already_AddRefed<WebKitCSSMatrix> |
115 | | WebKitCSSMatrix::Scale(double aScaleX, |
116 | | const Optional<double>& aScaleY, |
117 | | double aScaleZ) const |
118 | 0 | { |
119 | 0 | double scaleX = aScaleX; |
120 | 0 | double scaleY = aScaleY.WasPassed() ? aScaleY.Value() : scaleX; |
121 | 0 | double scaleZ = aScaleZ; |
122 | 0 |
|
123 | 0 | RefPtr<WebKitCSSMatrix> retval = new WebKitCSSMatrix(mParent, *this); |
124 | 0 | retval->ScaleNonUniformSelf(scaleX, scaleY, scaleZ); |
125 | 0 |
|
126 | 0 | return retval.forget(); |
127 | 0 | } |
128 | | |
129 | | already_AddRefed<WebKitCSSMatrix> |
130 | | WebKitCSSMatrix::Rotate(double aRotX, |
131 | | const Optional<double>& aRotY, |
132 | | const Optional<double>& aRotZ) const |
133 | 0 | { |
134 | 0 | double rotX = aRotX; |
135 | 0 | double rotY; |
136 | 0 | double rotZ; |
137 | 0 |
|
138 | 0 | if (!aRotY.WasPassed() && !aRotZ.WasPassed()) { |
139 | 0 | rotZ = rotX; |
140 | 0 | rotX = 0; |
141 | 0 | rotY = 0; |
142 | 0 | } else { |
143 | 0 | rotY = aRotY.WasPassed() ? aRotY.Value() : 0; |
144 | 0 | rotZ = aRotZ.WasPassed() ? aRotZ.Value() : 0; |
145 | 0 | } |
146 | 0 |
|
147 | 0 | RefPtr<WebKitCSSMatrix> retval = new WebKitCSSMatrix(mParent, *this); |
148 | 0 | retval->Rotate3dSelf(rotX, rotY, rotZ); |
149 | 0 |
|
150 | 0 | return retval.forget(); |
151 | 0 | } |
152 | | |
153 | | WebKitCSSMatrix* |
154 | | WebKitCSSMatrix::Rotate3dSelf(double aRotX, |
155 | | double aRotY, |
156 | | double aRotZ) |
157 | 0 | { |
158 | 0 | if (aRotX != 0 || aRotY != 0) { |
159 | 0 | Ensure3DMatrix(); |
160 | 0 | } |
161 | 0 |
|
162 | 0 | if (mMatrix3D) { |
163 | 0 | if (fmod(aRotZ, 360) != 0) { |
164 | 0 | mMatrix3D->RotateZ(aRotZ * sRadPerDegree); |
165 | 0 | } |
166 | 0 | if (fmod(aRotY, 360) != 0) { |
167 | 0 | mMatrix3D->RotateY(aRotY * sRadPerDegree); |
168 | 0 | } |
169 | 0 | if (fmod(aRotX, 360) != 0) { |
170 | 0 | mMatrix3D->RotateX(aRotX * sRadPerDegree); |
171 | 0 | } |
172 | 0 | } else if (fmod(aRotZ, 360) != 0) { |
173 | 0 | mMatrix2D->PreRotate(aRotZ * sRadPerDegree); |
174 | 0 | } |
175 | 0 |
|
176 | 0 | return this; |
177 | 0 | } |
178 | | |
179 | | already_AddRefed<WebKitCSSMatrix> |
180 | | WebKitCSSMatrix::RotateAxisAngle(double aX, |
181 | | double aY, |
182 | | double aZ, |
183 | | double aAngle) const |
184 | 0 | { |
185 | 0 | RefPtr<WebKitCSSMatrix> retval = new WebKitCSSMatrix(mParent, *this); |
186 | 0 | retval->RotateAxisAngleSelf(aX, aY, aZ, aAngle); |
187 | 0 |
|
188 | 0 | return retval.forget(); |
189 | 0 | } |
190 | | |
191 | | already_AddRefed<WebKitCSSMatrix> |
192 | | WebKitCSSMatrix::SkewX(double aSx) const |
193 | 0 | { |
194 | 0 | RefPtr<WebKitCSSMatrix> retval = new WebKitCSSMatrix(mParent, *this); |
195 | 0 | retval->SkewXSelf(aSx); |
196 | 0 |
|
197 | 0 | return retval.forget(); |
198 | 0 | } |
199 | | |
200 | | already_AddRefed<WebKitCSSMatrix> |
201 | | WebKitCSSMatrix::SkewY(double aSy) const |
202 | 0 | { |
203 | 0 | RefPtr<WebKitCSSMatrix> retval = new WebKitCSSMatrix(mParent, *this); |
204 | 0 | retval->SkewYSelf(aSy); |
205 | 0 |
|
206 | 0 | return retval.forget(); |
207 | 0 | } |
208 | | |
209 | | } // namespace dom |
210 | | } // namespace mozilla |