1 | /*
|
---|
2 | * Copyright (C) 2017 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. AND ITS CONTRIBUTORS ``AS IS''
|
---|
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
|
---|
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
---|
23 | * THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "config.h"
|
---|
27 | #include "DOMMatrix.h"
|
---|
28 |
|
---|
29 | #include "ScriptExecutionContext.h"
|
---|
30 | #include <JavaScriptCore/Float32Array.h>
|
---|
31 | #include <cmath>
|
---|
32 | #include <limits>
|
---|
33 |
|
---|
34 | namespace WebCore {
|
---|
35 |
|
---|
36 | // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-dommatrixreadonly
|
---|
37 | ExceptionOr<Ref<DOMMatrix>> DOMMatrix::create(ScriptExecutionContext& scriptExecutionContext, std::optional<std::variant<String, Vector<double>>>&& init)
|
---|
38 | {
|
---|
39 | if (!init)
|
---|
40 | return adoptRef(*new DOMMatrix);
|
---|
41 |
|
---|
42 | return WTF::switchOn(init.value(),
|
---|
43 | [&scriptExecutionContext](const String& init) -> ExceptionOr<Ref<DOMMatrix>> {
|
---|
44 | if (!scriptExecutionContext.isDocument())
|
---|
45 | return Exception { TypeError };
|
---|
46 |
|
---|
47 | auto parseResult = parseStringIntoAbstractMatrix(init);
|
---|
48 | if (parseResult.hasException())
|
---|
49 | return parseResult.releaseException();
|
---|
50 |
|
---|
51 | return adoptRef(*new DOMMatrix(parseResult.returnValue().matrix, parseResult.returnValue().is2D ? Is2D::Yes : Is2D::No));
|
---|
52 | },
|
---|
53 | [](const Vector<double>& init) -> ExceptionOr<Ref<DOMMatrix>> {
|
---|
54 | if (init.size() == 6) {
|
---|
55 | return adoptRef(*new DOMMatrix(TransformationMatrix {
|
---|
56 | init[0], init[1], init[2], init[3], init[4], init[5]
|
---|
57 | }, Is2D::Yes));
|
---|
58 | }
|
---|
59 | if (init.size() == 16) {
|
---|
60 | return adoptRef(*new DOMMatrix(TransformationMatrix {
|
---|
61 | init[0], init[1], init[2], init[3],
|
---|
62 | init[4], init[5], init[6], init[7],
|
---|
63 | init[8], init[9], init[10], init[11],
|
---|
64 | init[12], init[13], init[14], init[15]
|
---|
65 | }, Is2D::No));
|
---|
66 | }
|
---|
67 | return Exception { TypeError };
|
---|
68 | }
|
---|
69 | );
|
---|
70 | }
|
---|
71 |
|
---|
72 | DOMMatrix::DOMMatrix(const TransformationMatrix& matrix, Is2D is2D)
|
---|
73 | : DOMMatrixReadOnly(matrix, is2D)
|
---|
74 | {
|
---|
75 | }
|
---|
76 |
|
---|
77 | DOMMatrix::DOMMatrix(TransformationMatrix&& matrix, Is2D is2D)
|
---|
78 | : DOMMatrixReadOnly(WTFMove(matrix), is2D)
|
---|
79 | {
|
---|
80 | }
|
---|
81 |
|
---|
82 | // https://drafts.fxtf.org/geometry/#create-a-dommatrix-from-the-dictionary
|
---|
83 | ExceptionOr<Ref<DOMMatrix>> DOMMatrix::fromMatrix(DOMMatrixInit&& init)
|
---|
84 | {
|
---|
85 | return fromMatrixHelper<DOMMatrix>(WTFMove(init));
|
---|
86 | }
|
---|
87 |
|
---|
88 | ExceptionOr<Ref<DOMMatrix>> DOMMatrix::fromFloat32Array(Ref<Float32Array>&& array32)
|
---|
89 | {
|
---|
90 | if (array32->length() == 6)
|
---|
91 | return DOMMatrix::create(TransformationMatrix(array32->item(0), array32->item(1), array32->item(2), array32->item(3), array32->item(4), array32->item(5)), Is2D::Yes);
|
---|
92 |
|
---|
93 | if (array32->length() == 16) {
|
---|
94 | return DOMMatrix::create(TransformationMatrix(
|
---|
95 | array32->item(0), array32->item(1), array32->item(2), array32->item(3),
|
---|
96 | array32->item(4), array32->item(5), array32->item(6), array32->item(7),
|
---|
97 | array32->item(8), array32->item(9), array32->item(10), array32->item(11),
|
---|
98 | array32->item(12), array32->item(13), array32->item(14), array32->item(15)
|
---|
99 | ), Is2D::No);
|
---|
100 | }
|
---|
101 |
|
---|
102 | return Exception { TypeError };
|
---|
103 | }
|
---|
104 |
|
---|
105 | ExceptionOr<Ref<DOMMatrix>> DOMMatrix::fromFloat64Array(Ref<Float64Array>&& array64)
|
---|
106 | {
|
---|
107 | if (array64->length() == 6)
|
---|
108 | return DOMMatrix::create(TransformationMatrix(array64->item(0), array64->item(1), array64->item(2), array64->item(3), array64->item(4), array64->item(5)), Is2D::Yes);
|
---|
109 |
|
---|
110 | if (array64->length() == 16) {
|
---|
111 | return DOMMatrix::create(TransformationMatrix(
|
---|
112 | array64->item(0), array64->item(1), array64->item(2), array64->item(3),
|
---|
113 | array64->item(4), array64->item(5), array64->item(6), array64->item(7),
|
---|
114 | array64->item(8), array64->item(9), array64->item(10), array64->item(11),
|
---|
115 | array64->item(12), array64->item(13), array64->item(14), array64->item(15)
|
---|
116 | ), Is2D::No);
|
---|
117 | }
|
---|
118 |
|
---|
119 | return Exception { TypeError };
|
---|
120 | }
|
---|
121 |
|
---|
122 | // https://drafts.fxtf.org/geometry/#dom-dommatrix-multiplyself
|
---|
123 | ExceptionOr<Ref<DOMMatrix>> DOMMatrix::multiplySelf(DOMMatrixInit&& other)
|
---|
124 | {
|
---|
125 | auto fromMatrixResult = DOMMatrix::fromMatrix(WTFMove(other));
|
---|
126 | if (fromMatrixResult.hasException())
|
---|
127 | return fromMatrixResult.releaseException();
|
---|
128 | auto otherObject = fromMatrixResult.releaseReturnValue();
|
---|
129 | m_matrix.multiply(otherObject->m_matrix);
|
---|
130 | if (!otherObject->is2D())
|
---|
131 | m_is2D = false;
|
---|
132 | return Ref<DOMMatrix> { *this };
|
---|
133 | }
|
---|
134 |
|
---|
135 | // https://drafts.fxtf.org/geometry/#dom-dommatrix-premultiplyself
|
---|
136 | ExceptionOr<Ref<DOMMatrix>> DOMMatrix::preMultiplySelf(DOMMatrixInit&& other)
|
---|
137 | {
|
---|
138 | auto fromMatrixResult = DOMMatrix::fromMatrix(WTFMove(other));
|
---|
139 | if (fromMatrixResult.hasException())
|
---|
140 | return fromMatrixResult.releaseException();
|
---|
141 | auto otherObject = fromMatrixResult.releaseReturnValue();
|
---|
142 | m_matrix = otherObject->m_matrix * m_matrix;
|
---|
143 | if (!otherObject->is2D())
|
---|
144 | m_is2D = false;
|
---|
145 | return Ref<DOMMatrix> { *this };
|
---|
146 | }
|
---|
147 |
|
---|
148 | // https://drafts.fxtf.org/geometry/#dom-dommatrix-translateself
|
---|
149 | Ref<DOMMatrix> DOMMatrix::translateSelf(double tx, double ty, double tz)
|
---|
150 | {
|
---|
151 | m_matrix.translate3d(tx, ty, tz);
|
---|
152 | if (tz)
|
---|
153 | m_is2D = false;
|
---|
154 | return *this;
|
---|
155 | }
|
---|
156 |
|
---|
157 | // https://drafts.fxtf.org/geometry/#dom-dommatrix-scaleself
|
---|
158 | Ref<DOMMatrix> DOMMatrix::scaleSelf(double scaleX, std::optional<double> scaleY, double scaleZ, double originX, double originY, double originZ)
|
---|
159 | {
|
---|
160 | if (!scaleY)
|
---|
161 | scaleY = scaleX;
|
---|
162 | translateSelf(originX, originY, originZ);
|
---|
163 | // Post-multiply a non-uniform scale transformation on the current matrix.
|
---|
164 | // The 3D scale matrix is described in CSS Transforms with sx = scaleX, sy = scaleY and sz = scaleZ.
|
---|
165 | m_matrix.scale3d(scaleX, scaleY.value(), scaleZ);
|
---|
166 | translateSelf(-originX, -originY, -originZ);
|
---|
167 | if (scaleZ != 1 || originZ)
|
---|
168 | m_is2D = false;
|
---|
169 | return *this;
|
---|
170 | }
|
---|
171 |
|
---|
172 | // https://drafts.fxtf.org/geometry/#dom-dommatrix-scale3dself
|
---|
173 | Ref<DOMMatrix> DOMMatrix::scale3dSelf(double scale, double originX, double originY, double originZ)
|
---|
174 | {
|
---|
175 | translateSelf(originX, originY, originZ);
|
---|
176 | // Post-multiply a uniform 3D scale transformation (m11 = m22 = m33 = scale) on the current matrix.
|
---|
177 | // The 3D scale matrix is described in CSS Transforms with sx = sy = sz = scale. [CSS3-TRANSFORMS]
|
---|
178 | m_matrix.scale3d(scale, scale, scale);
|
---|
179 | translateSelf(-originX, -originY, -originZ);
|
---|
180 | if (scale != 1)
|
---|
181 | m_is2D = false;
|
---|
182 | return *this;
|
---|
183 | }
|
---|
184 |
|
---|
185 | // https://drafts.fxtf.org/geometry/#dom-dommatrix-rotateself
|
---|
186 | Ref<DOMMatrix> DOMMatrix::rotateSelf(double rotX, std::optional<double> rotY, std::optional<double> rotZ)
|
---|
187 | {
|
---|
188 | if (!rotY && !rotZ) {
|
---|
189 | rotZ = rotX;
|
---|
190 | rotX = 0;
|
---|
191 | rotY = 0;
|
---|
192 | }
|
---|
193 | m_matrix.rotate3d(rotX, rotY.value_or(0), rotZ.value_or(0));
|
---|
194 | if (rotX || rotY.value_or(0))
|
---|
195 | m_is2D = false;
|
---|
196 | return *this;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // https://drafts.fxtf.org/geometry/#dom-dommatrix-rotatefromvectorself
|
---|
200 | Ref<DOMMatrix> DOMMatrix::rotateFromVectorSelf(double x, double y)
|
---|
201 | {
|
---|
202 | m_matrix.rotateFromVector(x, y);
|
---|
203 | return *this;
|
---|
204 | }
|
---|
205 |
|
---|
206 | // https://drafts.fxtf.org/geometry/#dom-dommatrix-rotateaxisangleself
|
---|
207 | Ref<DOMMatrix> DOMMatrix::rotateAxisAngleSelf(double x, double y, double z, double angle)
|
---|
208 | {
|
---|
209 | m_matrix.rotate3d(x, y, z, angle);
|
---|
210 | if (x || y)
|
---|
211 | m_is2D = false;
|
---|
212 | return *this;
|
---|
213 | }
|
---|
214 |
|
---|
215 | // https://drafts.fxtf.org/geometry/#dom-dommatrix-skewxself
|
---|
216 | Ref<DOMMatrix> DOMMatrix::skewXSelf(double sx)
|
---|
217 | {
|
---|
218 | m_matrix.skewX(sx);
|
---|
219 | return *this;
|
---|
220 | }
|
---|
221 |
|
---|
222 | // https://drafts.fxtf.org/geometry/#dom-dommatrix-skewyself
|
---|
223 | Ref<DOMMatrix> DOMMatrix::skewYSelf(double sy)
|
---|
224 | {
|
---|
225 | m_matrix.skewY(sy);
|
---|
226 | return *this;
|
---|
227 | }
|
---|
228 |
|
---|
229 | // https://drafts.fxtf.org/geometry/#dom-dommatrix-invertself
|
---|
230 | Ref<DOMMatrix> DOMMatrix::invertSelf()
|
---|
231 | {
|
---|
232 | auto inverse = m_matrix.inverse();
|
---|
233 | if (inverse)
|
---|
234 | m_matrix = *inverse;
|
---|
235 | else {
|
---|
236 | m_matrix.setMatrix(
|
---|
237 | std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(),
|
---|
238 | std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(),
|
---|
239 | std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(),
|
---|
240 | std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN()
|
---|
241 | );
|
---|
242 | m_is2D = false;
|
---|
243 | }
|
---|
244 | return Ref<DOMMatrix> { *this };
|
---|
245 | }
|
---|
246 |
|
---|
247 | ExceptionOr<Ref<DOMMatrix>> DOMMatrix::setMatrixValueForBindings(const String& string)
|
---|
248 | {
|
---|
249 | auto result = setMatrixValue(string);
|
---|
250 | if (result.hasException())
|
---|
251 | return result.releaseException();
|
---|
252 | return Ref<DOMMatrix> { *this };
|
---|
253 | }
|
---|
254 |
|
---|
255 | } // namespace WebCore
|
---|