DOMMatrix.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/TypedArray.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Geometry/DOMMatrix.h>
  10. #include <LibWeb/WebIDL/ExceptionOr.h>
  11. namespace Web::Geometry {
  12. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::construct_impl(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
  13. {
  14. auto& vm = realm.vm();
  15. // https://drafts.fxtf.org/geometry/#dom-dommatrix-dommatrix
  16. if (init.has_value()) {
  17. // -> Otherwise
  18. // Throw a TypeError exception.
  19. // The only condition where this can be met is with a sequence type which doesn't have exactly 6 or 16 elements.
  20. if (auto* double_sequence = init.value().get_pointer<Vector<double>>(); double_sequence && (double_sequence->size() != 6 && double_sequence->size() != 16))
  21. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, TRY_OR_THROW_OOM(vm, String::formatted("Sequence must contain exactly 6 or 16 elements, got {} element(s)", double_sequence->size())) };
  22. }
  23. return realm.heap().allocate<DOMMatrix>(realm, realm, init);
  24. }
  25. // https://drafts.fxtf.org/geometry/#create-a-dommatrix-from-the-2d-dictionary
  26. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::create_from_dom_matrix_2d_init(JS::Realm& realm, DOMMatrix2DInit& init)
  27. {
  28. // 1. Validate and fixup (2D) other.
  29. TRY(validate_and_fixup_dom_matrix_2d_init(init));
  30. // These should all have values after calling `validate_and_fixup_dom_matrix_2d_init`
  31. VERIFY(init.m11.has_value());
  32. VERIFY(init.m12.has_value());
  33. VERIFY(init.m21.has_value());
  34. VERIFY(init.m22.has_value());
  35. VERIFY(init.m41.has_value());
  36. VERIFY(init.m42.has_value());
  37. // 2. Return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers,
  38. // the values being the 6 elements m11, m12, m21, m22, m41 and m42 of other in the given order.
  39. return realm.heap().allocate<DOMMatrix>(realm, realm, init.m11.value(), init.m12.value(), init.m21.value(), init.m22.value(), init.m41.value(), init.m42.value());
  40. }
  41. // https://drafts.fxtf.org/geometry/#create-a-dommatrix-from-the-dictionary
  42. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::create_from_dom_matrix_init(JS::Realm& realm, DOMMatrixInit& init)
  43. {
  44. // 1. Validate and fixup other.
  45. TRY(validate_and_fixup_dom_matrix_init(init));
  46. // 2. If the is2D dictionary member of other is true.
  47. if (init.is2d.has_value() && init.is2d.value()) {
  48. // Return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers, the values being the 6 elements m11, m12, m21, m22, m41 and m42 of other in the given order.
  49. return realm.heap().allocate<DOMMatrix>(realm, realm, init.m11.value(), init.m12.value(), init.m21.value(), init.m22.value(), init.m41.value(), init.m42.value());
  50. }
  51. // Otherwise, Return the result of invoking create a 3d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers, the values being the 16 elements m11, m12, m13, ..., m44 of other in the given order.
  52. return realm.heap().allocate<DOMMatrix>(realm, realm, init.m11.value(), init.m12.value(), init.m13, init.m14,
  53. init.m21.value(), init.m22.value(), init.m23, init.m24,
  54. init.m31, init.m32, init.m33, init.m34,
  55. init.m41.value(), init.m42.value(), init.m43, init.m44);
  56. }
  57. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::create_from_dom_matrix_read_only(JS::Realm& realm, DOMMatrixReadOnly const& read_only_matrix)
  58. {
  59. return realm.heap().allocate<DOMMatrix>(realm, realm, read_only_matrix);
  60. }
  61. DOMMatrix::DOMMatrix(JS::Realm& realm, double m11, double m12, double m21, double m22, double m41, double m42)
  62. : DOMMatrixReadOnly(realm, m11, m12, m21, m22, m41, m42)
  63. {
  64. }
  65. DOMMatrix::DOMMatrix(JS::Realm& realm, double m11, double m12, double m13, double m14, double m21, double m22, double m23, double m24, double m31, double m32, double m33, double m34, double m41, double m42, double m43, double m44)
  66. : DOMMatrixReadOnly(realm, m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44)
  67. {
  68. }
  69. DOMMatrix::DOMMatrix(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
  70. : DOMMatrixReadOnly(realm, init)
  71. {
  72. }
  73. DOMMatrix::DOMMatrix(JS::Realm& realm, DOMMatrixReadOnly const& read_only_matrix)
  74. : DOMMatrixReadOnly(realm, read_only_matrix)
  75. {
  76. }
  77. DOMMatrix::~DOMMatrix() = default;
  78. void DOMMatrix::initialize(JS::Realm& realm)
  79. {
  80. Base::initialize(realm);
  81. set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMMatrixPrototype>(realm, "DOMMatrix"));
  82. }
  83. // https://drafts.fxtf.org/geometry/#dom-dommatrix-frommatrix
  84. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::from_matrix(JS::VM& vm, DOMMatrixInit other)
  85. {
  86. return create_from_dom_matrix_init(*vm.current_realm(), other);
  87. }
  88. // https://drafts.fxtf.org/geometry/#dom-dommatrix-fromfloat32array
  89. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::from_float32_array(JS::VM& vm, JS::Handle<JS::Object> const& array32)
  90. {
  91. if (!is<JS::Float32Array>(*array32))
  92. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Float32Array");
  93. auto& realm = *vm.current_realm();
  94. auto& float32_array = static_cast<JS::Float32Array&>(*array32);
  95. ReadonlySpan<float> elements = float32_array.data();
  96. // If array32 has 6 elements, return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers taking the values from array32 in the provided order.
  97. if (elements.size() == 6)
  98. return realm.heap().allocate<DOMMatrix>(realm, realm, elements.at(0), elements.at(1), elements.at(2), elements.at(3), elements.at(4), elements.at(5));
  99. // If array32 has 16 elements, return the result of invoking create a 3d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers taking the values from array32 in the provided order.
  100. if (elements.size() == 16)
  101. return realm.heap().allocate<DOMMatrix>(realm, realm, elements.at(0), elements.at(1), elements.at(2), elements.at(3),
  102. elements.at(4), elements.at(5), elements.at(6), elements.at(7),
  103. elements.at(8), elements.at(9), elements.at(10), elements.at(11),
  104. elements.at(12), elements.at(13), elements.at(14), elements.at(15));
  105. // Otherwise, throw a TypeError exception.
  106. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Expected a Float32Array argument with 6 or 16 elements"_string };
  107. }
  108. // https://drafts.fxtf.org/geometry/#dom-dommatrix-fromfloat64array
  109. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::from_float64_array(JS::VM& vm, JS::Handle<JS::Object> const& array64)
  110. {
  111. if (!is<JS::Float64Array>(*array64))
  112. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Float64Array");
  113. auto& realm = *vm.current_realm();
  114. auto& float64_array = static_cast<JS::Float64Array&>(*array64);
  115. ReadonlySpan<double> elements = float64_array.data();
  116. // If array64 has 6 elements, return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers taking the values from array64 in the provided order.
  117. if (elements.size() == 6)
  118. return realm.heap().allocate<DOMMatrix>(realm, realm, elements.at(0), elements.at(1), elements.at(2), elements.at(3), elements.at(4), elements.at(5));
  119. // If array64 has 16 elements, return the result of invoking create a 3d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers taking the values from array64 in the provided order.
  120. if (elements.size() == 16)
  121. return realm.heap().allocate<DOMMatrix>(realm, realm, elements.at(0), elements.at(1), elements.at(2), elements.at(3),
  122. elements.at(4), elements.at(5), elements.at(6), elements.at(7),
  123. elements.at(8), elements.at(9), elements.at(10), elements.at(11),
  124. elements.at(12), elements.at(13), elements.at(14), elements.at(15));
  125. // Otherwise, throw a TypeError exception.
  126. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Expected a Float64Array argument with 6 or 16 elements"_string };
  127. }
  128. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m11
  129. void DOMMatrix::set_m11(double value)
  130. {
  131. // For the DOMMatrix interface, setting the m11 or the a attribute must set the m11 element to the new value.
  132. m_matrix.elements()[0][0] = value;
  133. }
  134. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m12
  135. void DOMMatrix::set_m12(double value)
  136. {
  137. // For the DOMMatrix interface, setting the m12 or the b attribute must set the m12 element to the new value.
  138. m_matrix.elements()[1][0] = value;
  139. }
  140. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m13
  141. void DOMMatrix::set_m13(double value)
  142. {
  143. // For the DOMMatrix interface, setting the m13 attribute must set the m13 element to the new value and, if the new value is not 0 or -0, set is 2D to false.
  144. m_matrix.elements()[2][0] = value;
  145. if (value != 0.0 && value != -0.0)
  146. m_is_2d = false;
  147. }
  148. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m14
  149. void DOMMatrix::set_m14(double value)
  150. {
  151. // For the DOMMatrix interface, setting the m14 attribute must set the m14 element to the new value and, if the new value is not 0 or -0, set is 2D to false.
  152. m_matrix.elements()[3][0] = value;
  153. if (value != 0.0 && value != -0.0)
  154. m_is_2d = false;
  155. }
  156. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m21
  157. void DOMMatrix::set_m21(double value)
  158. {
  159. // For the DOMMatrix interface, setting the m21 or the c attribute must set the m21 element to the new value.
  160. m_matrix.elements()[0][1] = value;
  161. }
  162. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m22
  163. void DOMMatrix::set_m22(double value)
  164. {
  165. // For the DOMMatrix interface, setting the m22 or the d attribute must set the m22 element to the new value.
  166. m_matrix.elements()[1][1] = value;
  167. }
  168. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m23
  169. void DOMMatrix::set_m23(double value)
  170. {
  171. // For the DOMMatrix interface, setting the m23 attribute must set the m23 element to the new value and, if the new value is not 0 or -0, set is 2D to false.
  172. m_matrix.elements()[2][1] = value;
  173. if (value != 0.0 && value != -0.0)
  174. m_is_2d = false;
  175. }
  176. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m24
  177. void DOMMatrix::set_m24(double value)
  178. {
  179. // For the DOMMatrix interface, setting the m24 attribute must set the m24 element to the new value and, if the new value is not 0 or -0, set is 2D to false.
  180. m_matrix.elements()[3][1] = value;
  181. if (value != 0.0 && value != -0.0)
  182. m_is_2d = false;
  183. }
  184. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m31
  185. void DOMMatrix::set_m31(double value)
  186. {
  187. // For the DOMMatrix interface, setting the m31 attribute must set the m31 element to the new value and, if the new value is not 0 or -0, set is 2D to false.
  188. m_matrix.elements()[0][2] = value;
  189. if (value != 0.0 && value != -0.0)
  190. m_is_2d = false;
  191. }
  192. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m32
  193. void DOMMatrix::set_m32(double value)
  194. {
  195. // For the DOMMatrix interface, setting the m32 attribute must set the m32 element to the new value and, if the new value is not 0 or -0, set is 2D to false.
  196. m_matrix.elements()[1][2] = value;
  197. if (value != 0.0 && value != -0.0)
  198. m_is_2d = false;
  199. }
  200. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m33
  201. void DOMMatrix::set_m33(double value)
  202. {
  203. // For the DOMMatrix interface, setting the m33 attribute must set the m33 element to the new value and, if the new value is not 1, set is 2D to false.
  204. m_matrix.elements()[2][2] = value;
  205. if (value != 1.0)
  206. m_is_2d = false;
  207. }
  208. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m34
  209. void DOMMatrix::set_m34(double value)
  210. {
  211. // For the DOMMatrix interface, setting the m34 attribute must set the m34 element to the new value and, if the new value is not 0 or -0, set is 2D to false.
  212. m_matrix.elements()[3][2] = value;
  213. if (value != 0.0 && value != -0.0)
  214. m_is_2d = false;
  215. }
  216. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m41
  217. void DOMMatrix::set_m41(double value)
  218. {
  219. // For the DOMMatrix interface, setting the m41 or the e attribute must set the m41 element to the new value.
  220. m_matrix.elements()[0][3] = value;
  221. }
  222. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m42
  223. void DOMMatrix::set_m42(double value)
  224. {
  225. // For the DOMMatrix interface, setting the m42 or the f attribute must set the m42 element to the new value.
  226. m_matrix.elements()[1][3] = value;
  227. }
  228. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m43
  229. void DOMMatrix::set_m43(double value)
  230. {
  231. // For the DOMMatrix interface, setting the m43 attribute must set the m43 element to the new value and, if the new value is not 0 or -0, set is 2D to false.
  232. m_matrix.elements()[2][3] = value;
  233. if (value != 0.0 && value != -0.0)
  234. m_is_2d = false;
  235. }
  236. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m44
  237. void DOMMatrix::set_m44(double value)
  238. {
  239. // For the DOMMatrix interface, setting the m44 attribute must set the m44 element to the new value and, if the new value is not 1, set is 2D to false.
  240. m_matrix.elements()[3][3] = value;
  241. if (value != 1.0)
  242. m_is_2d = false;
  243. }
  244. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-a
  245. void DOMMatrix::set_a(double value)
  246. {
  247. // For the DOMMatrix interface, setting the m11 or the a attribute must set the m11 element to the new value.
  248. set_m11(value);
  249. }
  250. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-b
  251. void DOMMatrix::set_b(double value)
  252. {
  253. // For the DOMMatrix interface, setting the m12 or the b attribute must set the m12 element to the new value.
  254. set_m12(value);
  255. }
  256. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-c
  257. void DOMMatrix::set_c(double value)
  258. {
  259. // For the DOMMatrix interface, setting the m21 or the c attribute must set the m21 element to the new value.
  260. set_m21(value);
  261. }
  262. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-d
  263. void DOMMatrix::set_d(double value)
  264. {
  265. // For the DOMMatrix interface, setting the m22 or the d attribute must set the m22 element to the new value.
  266. set_m22(value);
  267. }
  268. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-e
  269. void DOMMatrix::set_e(double value)
  270. {
  271. // For the DOMMatrix interface, setting the m41 or the e attribute must set the m41 element to the new value.
  272. set_m41(value);
  273. }
  274. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-f
  275. void DOMMatrix::set_f(double value)
  276. {
  277. // For the DOMMatrix interface, setting the m42 or the f attribute must set the m42 element to the new value.
  278. set_m42(value);
  279. }
  280. // https://drafts.fxtf.org/geometry/#dom-dommatrix-multiplyself
  281. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::multiply_self(DOMMatrixInit other)
  282. {
  283. // 1. Let otherObject be the result of invoking create a DOMMatrix from the dictionary other.
  284. auto other_object = TRY(DOMMatrix::create_from_dom_matrix_init(realm(), other));
  285. // 2. The otherObject matrix gets post-multiplied to the current matrix.
  286. m_matrix = m_matrix * other_object->m_matrix;
  287. // 3. If is 2D of otherObject is false, set is 2D of the current matrix to false.
  288. if (!other_object->m_is_2d)
  289. m_is_2d = false;
  290. // 4. Return the current matrix.
  291. return JS::NonnullGCPtr<DOMMatrix>(*this);
  292. }
  293. // https://drafts.fxtf.org/geometry/#dom-dommatrix-premultiplyself
  294. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::pre_multiply_self(DOMMatrixInit other)
  295. {
  296. // 1. Let otherObject be the result of invoking create a DOMMatrix from the dictionary other.
  297. auto other_object = TRY(DOMMatrix::create_from_dom_matrix_init(realm(), other));
  298. // 2. The otherObject matrix gets pre-multiplied to the current matrix.
  299. m_matrix = other_object->m_matrix * m_matrix;
  300. // 3. If is 2D of otherObject is false, set is 2D of the current matrix to false.
  301. if (!other_object->m_is_2d)
  302. m_is_2d = false;
  303. // 4. Return the current matrix.
  304. return JS::NonnullGCPtr<DOMMatrix>(*this);
  305. }
  306. // https://drafts.fxtf.org/geometry/#dom-dommatrix-translateself
  307. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::translate_self(Optional<double> tx, Optional<double> ty, Optional<double> tz)
  308. {
  309. // 1. Post-multiply a translation transformation on the current matrix. The 3D translation matrix is described in CSS Transforms.
  310. m_matrix = m_matrix * Gfx::translation_matrix(Vector3<double> { tx.value_or(0), ty.value_or(0), tz.value_or(0) });
  311. // 2. If tz is specified and not 0 or -0, set is 2D of the current matrix to false.
  312. if (tz.has_value() && (tz != 0 || tz != -0))
  313. m_is_2d = false;
  314. // 3. Return the current matrix.
  315. return *this;
  316. }
  317. // https://drafts.fxtf.org/geometry/#dom-dommatrix-scaleself
  318. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::scale_self(Optional<double> scale_x, Optional<double> scale_y, Optional<double> scale_z, Optional<double> origin_x, Optional<double> origin_y, Optional<double> origin_z)
  319. {
  320. // 1. Perform a translateSelf() transformation on the current matrix with the arguments originX, originY, originZ.
  321. translate_self(origin_x, origin_y, origin_z);
  322. // 2. If scaleY is missing, set scaleY to the value of scaleX.
  323. if (!scale_y.has_value())
  324. scale_y = scale_x.value_or(1);
  325. // 3. Post-multiply a non-uniform scale transformation on the current matrix. The 3D scale matrix is described in CSS Transforms with sx = scaleX, sy = scaleY and sz = scaleZ. [CSS3-TRANSFORMS]
  326. m_matrix = m_matrix * Gfx::scale_matrix(Vector3<double> { scale_x.value_or(1), scale_y.value(), scale_z.value_or(1) });
  327. // 4. Negate originX, originY and originZ.
  328. // 5. Perform a translateSelf() transformation on the current matrix with the arguments originX, originY, originZ.
  329. translate_self(-origin_x.value_or(0), -origin_y.value_or(0), -origin_z.value_or(0));
  330. // 6. If scaleZ is not 1, set is 2D of the current matrix to false.
  331. if (scale_z != 1)
  332. m_is_2d = false;
  333. // 7. Return the current matrix.
  334. return *this;
  335. }
  336. // https://drafts.fxtf.org/geometry/#dom-dommatrix-scale3dself
  337. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::scale3d_self(Optional<double> scale, Optional<double> origin_x, Optional<double> origin_y, Optional<double> origin_z)
  338. {
  339. // 1. Apply a translateSelf() transformation to the current matrix with the arguments originX, originY, originZ.
  340. translate_self(origin_x, origin_y, origin_z);
  341. // 2. Post-multiply a uniform 3D scale transformation (m11 = m22 = m33 = scale) on the current matrix. The 3D scale matrix is described in CSS Transforms with sx = sy = sz = scale. [CSS3-TRANSFORMS]
  342. m_matrix = m_matrix * Gfx::scale_matrix(Vector3<double> { scale.value_or(1), scale.value_or(1), scale.value_or(1) });
  343. // 3. Apply a translateSelf() transformation to the current matrix with the arguments -originX, -originY, -originZ.
  344. translate_self(-origin_x.value_or(0), -origin_y.value_or(0), -origin_z.value_or(0));
  345. // 4. If scale is not 1, set is 2D of the current matrix to false.
  346. if (scale != 1)
  347. m_is_2d = false;
  348. // 5. Return the current matrix.
  349. return *this;
  350. }
  351. // https://drafts.fxtf.org/geometry/#dom-dommatrix-rotateself
  352. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::rotate_self(Optional<double> rot_x, Optional<double> rot_y, Optional<double> rot_z)
  353. {
  354. // 1. If rotY and rotZ are both missing, set rotZ to the value of rotX and set rotX and rotY to 0.
  355. if (!rot_y.has_value() && !rot_z.has_value()) {
  356. rot_z = rot_x;
  357. rot_x = 0;
  358. rot_y = 0;
  359. }
  360. // 2. If rotY is still missing, set rotY to 0.
  361. if (!rot_y.has_value())
  362. rot_y = 0;
  363. // 3. If rotZ is still missing, set rotZ to 0.
  364. if (!rot_z.has_value())
  365. rot_z = 0;
  366. // 4. If rotX or rotY are not 0 or -0, set is 2D of the current matrix to false.
  367. if (rot_x != 0 || rot_x != -0 || rot_y != 0 || rot_y != -0)
  368. m_is_2d = false;
  369. // 5. Post-multiply a rotation transformation on the current matrix around the vector 0, 0, 1 by the specified rotation rotZ in degrees. The 3D rotation matrix is described in CSS Transforms with alpha = rotZ in degrees. [CSS3-TRANSFORMS]
  370. m_matrix = m_matrix * Gfx::rotation_matrix<double>(Vector3<double> { 0.0, 0.0, 1.0 }, AK::to_radians(rot_z.value()));
  371. // 6. Post-multiply a rotation transformation on the current matrix around the vector 0, 1, 0 by the specified rotation rotY in degrees. The 3D rotation matrix is described in CSS Transforms with alpha = rotY in degrees. [CSS3-TRANSFORMS]
  372. m_matrix = m_matrix * Gfx::rotation_matrix<double>(Vector3<double> { 0.0, 1.0, 0.0 }, AK::to_radians(rot_y.value()));
  373. // 7. Post-multiply a rotation transformation on the current matrix around the vector 1, 0, 0 by the specified rotation rotX in degrees. The 3D rotation matrix is described in CSS Transforms with alpha = rotX in degrees. [CSS3-TRANSFORMS]
  374. m_matrix = m_matrix * Gfx::rotation_matrix<double>(Vector3<double> { 1.0, 0.0, 0.0 }, AK::to_radians(rot_x.value()));
  375. // 8. Return the current matrix.
  376. return *this;
  377. }
  378. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::rotate_from_vector_self(Optional<double> x, Optional<double> y)
  379. {
  380. // 1. Post-multiply a rotation transformation on the current matrix.
  381. // The rotation angle is determined by the angle between the vector (1,0)T and (x,y)T in the clockwise direction. If x and y should both be 0 or -0, the angle is specified as 0.
  382. double angle = (x == 0 || x == -0) && (y == 0 || y == -0) ? 0.0 : atan2(y.value_or(0), x.value_or(0));
  383. // The 2D rotation matrix is described in CSS Transforms where alpha is the angle between the vector (1,0)T and (x,y)T in degrees. [CSS3-TRANSFORMS]
  384. m_matrix = m_matrix * Gfx::rotation_matrix<double>(Vector3<double> { 0.0, 0.0, 1.0 }, angle);
  385. // 2. Return the current matrix.
  386. return *this;
  387. }
  388. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::rotate_axis_angle_self(Optional<double> x, Optional<double> y, Optional<double> z, Optional<double> angle)
  389. {
  390. // 1. Post-multiply a rotation transformation on the current matrix around the specified vector x, y, z by the specified rotation angle in degrees. The 3D rotation matrix is described in CSS Transforms with alpha = angle in degrees. [CSS3-TRANSFORMS]
  391. m_matrix = m_matrix * Gfx::rotation_matrix<double>(Vector3<double> { x.value_or(0), y.value_or(0), z.value_or(0) }.normalized(), AK::to_radians(angle.value()));
  392. // 2. If x or y are not 0 or -0, set is 2D of the current matrix to false.
  393. if (x != 0 || x != -0 || y != 0 || y != -0)
  394. m_is_2d = false;
  395. // 3. Return the current matrix.
  396. return *this;
  397. }
  398. // https://drafts.fxtf.org/geometry/#dom-dommatrix-skewxself
  399. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::skew_x_self(double sx)
  400. {
  401. // 1. Post-multiply a skewX transformation on the current matrix by the specified angle sx in degrees. The 2D skewX matrix is described in CSS Transforms with alpha = sx in degrees. [CSS3-TRANSFORMS]
  402. // clang-format off
  403. Gfx::DoubleMatrix4x4 skew_matrix = { 1, tan(AK::to_radians(sx)), 0, 0,
  404. 0, 1, 0, 0,
  405. 0, 0, 1, 0,
  406. 0, 0, 0, 1 };
  407. // clang-format on
  408. m_matrix = m_matrix * skew_matrix;
  409. // 3. Return the current matrix.
  410. return *this;
  411. }
  412. // https://drafts.fxtf.org/geometry/#dom-dommatrix-skewyself
  413. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::skew_y_self(double sy)
  414. {
  415. // 1. Post-multiply a skewX transformation on the current matrix by the specified angle sy in degrees. The 2D skewY matrix is described in CSS Transforms with beta = sy in degrees. [CSS3-TRANSFORMS]
  416. // clang-format off
  417. Gfx::DoubleMatrix4x4 skew_matrix = { 1, 0, 0, 0,
  418. tan(AK::to_radians(sy)), 1, 0, 0,
  419. 0, 0, 1, 0,
  420. 0, 0, 0, 1 };
  421. // clang-format on
  422. m_matrix = m_matrix * skew_matrix;
  423. // 3. Return the current matrix.
  424. return *this;
  425. }
  426. // https://drafts.fxtf.org/geometry/#dom-dommatrix-invertself
  427. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::invert_self()
  428. {
  429. bool is_invertible = m_matrix.is_invertible();
  430. // 1. Invert the current matrix.
  431. if (is_invertible)
  432. m_matrix = m_matrix.inverse();
  433. // 2. If the current matrix is not invertible set all attributes to NaN and set is 2D to false.
  434. if (!is_invertible) {
  435. for (u8 i = 0; i < 4; i++) {
  436. for (u8 j = 0; j < 4; j++)
  437. m_matrix.elements()[j][i] = NAN;
  438. }
  439. m_is_2d = false;
  440. }
  441. // 3. Return the current matrix.
  442. return *this;
  443. }
  444. }