DOMMatrix.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Geometry/DOMMatrix.h>
  9. #include <LibWeb/WebIDL/ExceptionOr.h>
  10. namespace Web::Geometry {
  11. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::construct_impl(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
  12. {
  13. auto& vm = realm.vm();
  14. // https://drafts.fxtf.org/geometry/#dom-dommatrix-dommatrix
  15. if (init.has_value()) {
  16. // -> Otherwise
  17. // Throw a TypeError exception.
  18. // The only condition where this can be met is with a sequence type which doesn't have exactly 6 or 16 elements.
  19. if (auto* double_sequence = init.value().get_pointer<Vector<double>>(); double_sequence && (double_sequence->size() != 6 && double_sequence->size() != 16))
  20. 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())) };
  21. }
  22. return realm.heap().allocate<DOMMatrix>(realm, realm, init);
  23. }
  24. // https://drafts.fxtf.org/geometry/#create-a-dommatrix-from-the-2d-dictionary
  25. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::create_from_dom_matrix_2d_init(JS::Realm& realm, DOMMatrix2DInit& init)
  26. {
  27. // 1. Validate and fixup (2D) other.
  28. TRY(validate_and_fixup_dom_matrix_2d_init(init));
  29. // These should all have values after calling `validate_and_fixup_dom_matrix_2d_init`
  30. VERIFY(init.m11.has_value());
  31. VERIFY(init.m12.has_value());
  32. VERIFY(init.m21.has_value());
  33. VERIFY(init.m22.has_value());
  34. VERIFY(init.m41.has_value());
  35. VERIFY(init.m42.has_value());
  36. // 2. Return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers,
  37. // the values being the 6 elements m11, m12, m21, m22, m41 and m42 of other in the given order.
  38. 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());
  39. }
  40. // https://drafts.fxtf.org/geometry/#create-a-dommatrix-from-the-dictionary
  41. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::create_from_dom_matrix_init(JS::Realm& realm, DOMMatrixInit& init)
  42. {
  43. // 1. Validate and fixup other.
  44. TRY(validate_and_fixup_dom_matrix_init(init));
  45. // 2. If the is2D dictionary member of other is true.
  46. if (init.is2d.has_value() && init.is2d.value()) {
  47. // 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.
  48. 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());
  49. }
  50. // 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.
  51. return realm.heap().allocate<DOMMatrix>(realm, realm, init.m11.value(), init.m12.value(), init.m13, init.m14,
  52. init.m21.value(), init.m22.value(), init.m23, init.m24,
  53. init.m31, init.m32, init.m33, init.m34,
  54. init.m41.value(), init.m42.value(), init.m43, init.m44);
  55. }
  56. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::create_from_dom_matrix_read_only(JS::Realm& realm, DOMMatrixReadOnly const& read_only_matrix)
  57. {
  58. return realm.heap().allocate<DOMMatrix>(realm, realm, read_only_matrix);
  59. }
  60. DOMMatrix::DOMMatrix(JS::Realm& realm, double m11, double m12, double m21, double m22, double m41, double m42)
  61. : DOMMatrixReadOnly(realm, m11, m12, m21, m22, m41, m42)
  62. {
  63. }
  64. 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)
  65. : DOMMatrixReadOnly(realm, m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44)
  66. {
  67. }
  68. DOMMatrix::DOMMatrix(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
  69. : DOMMatrixReadOnly(realm, init)
  70. {
  71. }
  72. DOMMatrix::DOMMatrix(JS::Realm& realm, DOMMatrixReadOnly const& read_only_matrix)
  73. : DOMMatrixReadOnly(realm, read_only_matrix)
  74. {
  75. }
  76. DOMMatrix::~DOMMatrix() = default;
  77. void DOMMatrix::initialize(JS::Realm& realm)
  78. {
  79. Base::initialize(realm);
  80. set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMMatrixPrototype>(realm, "DOMMatrix"));
  81. }
  82. // https://drafts.fxtf.org/geometry/#dom-dommatrix-frommatrix
  83. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::from_matrix(JS::VM& vm, DOMMatrixInit other)
  84. {
  85. return create_from_dom_matrix_init(*vm.current_realm(), other);
  86. }
  87. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m11
  88. void DOMMatrix::set_m11(double value)
  89. {
  90. // For the DOMMatrix interface, setting the m11 or the a attribute must set the m11 element to the new value.
  91. m_matrix.elements()[0][0] = value;
  92. }
  93. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m12
  94. void DOMMatrix::set_m12(double value)
  95. {
  96. // For the DOMMatrix interface, setting the m12 or the b attribute must set the m12 element to the new value.
  97. m_matrix.elements()[1][0] = value;
  98. }
  99. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m13
  100. void DOMMatrix::set_m13(double value)
  101. {
  102. // 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.
  103. m_matrix.elements()[2][0] = value;
  104. if (value != 0.0 && value != -0.0)
  105. m_is_2d = false;
  106. }
  107. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m14
  108. void DOMMatrix::set_m14(double value)
  109. {
  110. // 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.
  111. m_matrix.elements()[3][0] = value;
  112. if (value != 0.0 && value != -0.0)
  113. m_is_2d = false;
  114. }
  115. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m21
  116. void DOMMatrix::set_m21(double value)
  117. {
  118. // For the DOMMatrix interface, setting the m21 or the c attribute must set the m21 element to the new value.
  119. m_matrix.elements()[0][1] = value;
  120. }
  121. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m22
  122. void DOMMatrix::set_m22(double value)
  123. {
  124. // For the DOMMatrix interface, setting the m22 or the d attribute must set the m22 element to the new value.
  125. m_matrix.elements()[1][1] = value;
  126. }
  127. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m23
  128. void DOMMatrix::set_m23(double value)
  129. {
  130. // 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.
  131. m_matrix.elements()[2][1] = value;
  132. if (value != 0.0 && value != -0.0)
  133. m_is_2d = false;
  134. }
  135. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m24
  136. void DOMMatrix::set_m24(double value)
  137. {
  138. // 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.
  139. m_matrix.elements()[3][1] = value;
  140. if (value != 0.0 && value != -0.0)
  141. m_is_2d = false;
  142. }
  143. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m31
  144. void DOMMatrix::set_m31(double value)
  145. {
  146. // 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.
  147. m_matrix.elements()[0][2] = value;
  148. if (value != 0.0 && value != -0.0)
  149. m_is_2d = false;
  150. }
  151. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m32
  152. void DOMMatrix::set_m32(double value)
  153. {
  154. // 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.
  155. m_matrix.elements()[1][2] = value;
  156. if (value != 0.0 && value != -0.0)
  157. m_is_2d = false;
  158. }
  159. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m33
  160. void DOMMatrix::set_m33(double value)
  161. {
  162. // 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.
  163. m_matrix.elements()[2][2] = value;
  164. if (value != 1.0)
  165. m_is_2d = false;
  166. }
  167. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m34
  168. void DOMMatrix::set_m34(double value)
  169. {
  170. // 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.
  171. m_matrix.elements()[3][2] = value;
  172. if (value != 0.0 && value != -0.0)
  173. m_is_2d = false;
  174. }
  175. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m41
  176. void DOMMatrix::set_m41(double value)
  177. {
  178. // For the DOMMatrix interface, setting the m41 or the e attribute must set the m41 element to the new value.
  179. m_matrix.elements()[0][3] = value;
  180. }
  181. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m42
  182. void DOMMatrix::set_m42(double value)
  183. {
  184. // For the DOMMatrix interface, setting the m42 or the f attribute must set the m42 element to the new value.
  185. m_matrix.elements()[1][3] = value;
  186. }
  187. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m43
  188. void DOMMatrix::set_m43(double value)
  189. {
  190. // 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.
  191. m_matrix.elements()[2][3] = value;
  192. if (value != 0.0 && value != -0.0)
  193. m_is_2d = false;
  194. }
  195. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m44
  196. void DOMMatrix::set_m44(double value)
  197. {
  198. // 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.
  199. m_matrix.elements()[3][3] = value;
  200. if (value != 1.0)
  201. m_is_2d = false;
  202. }
  203. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-a
  204. void DOMMatrix::set_a(double value)
  205. {
  206. // For the DOMMatrix interface, setting the m11 or the a attribute must set the m11 element to the new value.
  207. set_m11(value);
  208. }
  209. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-b
  210. void DOMMatrix::set_b(double value)
  211. {
  212. // For the DOMMatrix interface, setting the m12 or the b attribute must set the m12 element to the new value.
  213. set_m12(value);
  214. }
  215. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-c
  216. void DOMMatrix::set_c(double value)
  217. {
  218. // For the DOMMatrix interface, setting the m21 or the c attribute must set the m21 element to the new value.
  219. set_m21(value);
  220. }
  221. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-d
  222. void DOMMatrix::set_d(double value)
  223. {
  224. // For the DOMMatrix interface, setting the m22 or the d attribute must set the m22 element to the new value.
  225. set_m22(value);
  226. }
  227. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-e
  228. void DOMMatrix::set_e(double value)
  229. {
  230. // For the DOMMatrix interface, setting the m41 or the e attribute must set the m41 element to the new value.
  231. set_m41(value);
  232. }
  233. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-f
  234. void DOMMatrix::set_f(double value)
  235. {
  236. // For the DOMMatrix interface, setting the m42 or the f attribute must set the m42 element to the new value.
  237. set_m42(value);
  238. }
  239. // https://drafts.fxtf.org/geometry/#dom-dommatrix-multiplyself
  240. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::multiply_self(DOMMatrixInit other)
  241. {
  242. // 1. Let otherObject be the result of invoking create a DOMMatrix from the dictionary other.
  243. auto other_object = TRY(DOMMatrix::create_from_dom_matrix_init(realm(), other));
  244. // 2. The otherObject matrix gets post-multiplied to the current matrix.
  245. m_matrix = m_matrix * other_object->m_matrix;
  246. // 3. If is 2D of otherObject is false, set is 2D of the current matrix to false.
  247. if (!other_object->m_is_2d)
  248. m_is_2d = false;
  249. // 4. Return the current matrix.
  250. return JS::NonnullGCPtr<DOMMatrix>(*this);
  251. }
  252. // https://drafts.fxtf.org/geometry/#dom-dommatrix-premultiplyself
  253. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::pre_multiply_self(DOMMatrixInit other)
  254. {
  255. // 1. Let otherObject be the result of invoking create a DOMMatrix from the dictionary other.
  256. auto other_object = TRY(DOMMatrix::create_from_dom_matrix_init(realm(), other));
  257. // 2. The otherObject matrix gets pre-multiplied to the current matrix.
  258. m_matrix = other_object->m_matrix * m_matrix;
  259. // 3. If is 2D of otherObject is false, set is 2D of the current matrix to false.
  260. if (!other_object->m_is_2d)
  261. m_is_2d = false;
  262. // 4. Return the current matrix.
  263. return JS::NonnullGCPtr<DOMMatrix>(*this);
  264. }
  265. // https://drafts.fxtf.org/geometry/#dom-dommatrix-translateself
  266. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::translate_self(Optional<double> tx, Optional<double> ty, Optional<double> tz)
  267. {
  268. // 1. Post-multiply a translation transformation on the current matrix. The 3D translation matrix is described in CSS Transforms.
  269. m_matrix = m_matrix * Gfx::translation_matrix(Vector3<double> { tx.value_or(0), ty.value_or(0), tz.value_or(0) });
  270. // 2. If tz is specified and not 0 or -0, set is 2D of the current matrix to false.
  271. if (tz.has_value() && (tz != 0 || tz != -0))
  272. m_is_2d = false;
  273. // 3. Return the current matrix.
  274. return *this;
  275. }
  276. // https://drafts.fxtf.org/geometry/#dom-dommatrix-skewxself
  277. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::skew_x_self(double sx)
  278. {
  279. // 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]
  280. // clang-format off
  281. Gfx::DoubleMatrix4x4 skew_matrix = { 1, tan(AK::to_radians(sx)), 0, 0,
  282. 0, 1, 0, 0,
  283. 0, 0, 1, 0,
  284. 0, 0, 0, 1 };
  285. // clang-format on
  286. m_matrix = m_matrix * skew_matrix;
  287. // 3. Return the current matrix.
  288. return *this;
  289. }
  290. // https://drafts.fxtf.org/geometry/#dom-dommatrix-skewyself
  291. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::skew_y_self(double sy)
  292. {
  293. // 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]
  294. // clang-format off
  295. Gfx::DoubleMatrix4x4 skew_matrix = { 1, 0, 0, 0,
  296. tan(AK::to_radians(sy)), 1, 0, 0,
  297. 0, 0, 1, 0,
  298. 0, 0, 0, 1 };
  299. // clang-format on
  300. m_matrix = m_matrix * skew_matrix;
  301. // 3. Return the current matrix.
  302. return *this;
  303. }
  304. // https://drafts.fxtf.org/geometry/#dom-dommatrix-invertself
  305. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::invert_self()
  306. {
  307. bool is_invertible = m_matrix.is_invertible();
  308. // 1. Invert the current matrix.
  309. if (is_invertible)
  310. m_matrix = m_matrix.inverse();
  311. // 2. If the current matrix is not invertible set all attributes to NaN and set is 2D to false.
  312. if (!is_invertible) {
  313. for (u8 i = 0; i < 4; i++) {
  314. for (u8 j = 0; j < 4; j++)
  315. m_matrix.elements()[j][i] = NAN;
  316. }
  317. m_is_2d = false;
  318. }
  319. // 3. Return the current matrix.
  320. return *this;
  321. }
  322. }