DOMMatrix.cpp 14 KB

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