DOMMatrix.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Geometry/DOMMatrix.h>
  8. #include <LibWeb/WebIDL/ExceptionOr.h>
  9. namespace Web::Geometry {
  10. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::construct_impl(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
  11. {
  12. auto& vm = realm.vm();
  13. // https://drafts.fxtf.org/geometry/#dom-dommatrix-dommatrix
  14. if (init.has_value()) {
  15. // -> Otherwise
  16. // Throw a TypeError exception.
  17. // The only condition where this can be met is with a sequence type which doesn't have exactly 6 or 16 elements.
  18. if (auto* double_sequence = init.value().get_pointer<Vector<double>>(); double_sequence && (double_sequence->size() != 6 && double_sequence->size() != 16))
  19. 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())) };
  20. }
  21. return realm.heap().allocate<DOMMatrix>(realm, realm, init).release_allocated_value_but_fixme_should_propagate_errors();
  22. }
  23. // https://drafts.fxtf.org/geometry/#create-a-dommatrix-from-the-2d-dictionary
  24. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrix::create_from_dom_matrix_2d_init(JS::Realm& realm, DOMMatrix2DInit& init)
  25. {
  26. // 1. Validate and fixup (2D) other.
  27. TRY(validate_and_fixup_dom_matrix_2d_init(init));
  28. // These should all have values after calling `validate_and_fixup_dom_matrix_2d_init`
  29. VERIFY(init.m11.has_value());
  30. VERIFY(init.m12.has_value());
  31. VERIFY(init.m21.has_value());
  32. VERIFY(init.m22.has_value());
  33. VERIFY(init.m41.has_value());
  34. VERIFY(init.m42.has_value());
  35. // 2. Return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers,
  36. // the values being the 6 elements m11, m12, m21, m22, m41 and m42 of other in the given order.
  37. 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()).release_allocated_value_but_fixme_should_propagate_errors();
  38. }
  39. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::create_from_dom_matrix_read_only(JS::Realm& realm, DOMMatrixReadOnly const& read_only_matrix)
  40. {
  41. return realm.heap().allocate<DOMMatrix>(realm, realm, read_only_matrix).release_allocated_value_but_fixme_should_propagate_errors();
  42. }
  43. DOMMatrix::DOMMatrix(JS::Realm& realm, double m11, double m12, double m21, double m22, double m41, double m42)
  44. : DOMMatrixReadOnly(realm, m11, m12, m21, m22, m41, m42)
  45. {
  46. }
  47. DOMMatrix::DOMMatrix(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
  48. : DOMMatrixReadOnly(realm, init)
  49. {
  50. }
  51. DOMMatrix::DOMMatrix(JS::Realm& realm, DOMMatrixReadOnly const& read_only_matrix)
  52. : DOMMatrixReadOnly(realm, read_only_matrix)
  53. {
  54. }
  55. DOMMatrix::~DOMMatrix() = default;
  56. JS::ThrowCompletionOr<void> DOMMatrix::initialize(JS::Realm& realm)
  57. {
  58. MUST_OR_THROW_OOM(Base::initialize(realm));
  59. set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMMatrixPrototype>(realm, "DOMMatrix"));
  60. return {};
  61. }
  62. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m11
  63. void DOMMatrix::set_m11(double value)
  64. {
  65. // For the DOMMatrix interface, setting the m11 or the a attribute must set the m11 element to the new value.
  66. m_matrix.elements()[0][0] = value;
  67. }
  68. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m12
  69. void DOMMatrix::set_m12(double value)
  70. {
  71. // For the DOMMatrix interface, setting the m12 or the b attribute must set the m12 element to the new value.
  72. m_matrix.elements()[0][1] = value;
  73. }
  74. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m13
  75. void DOMMatrix::set_m13(double value)
  76. {
  77. // 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.
  78. m_matrix.elements()[0][2] = value;
  79. if (value != 0.0 && value != -0.0)
  80. m_is_2d = false;
  81. }
  82. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m14
  83. void DOMMatrix::set_m14(double value)
  84. {
  85. // 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.
  86. m_matrix.elements()[0][3] = value;
  87. if (value != 0.0 && value != -0.0)
  88. m_is_2d = false;
  89. }
  90. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m21
  91. void DOMMatrix::set_m21(double value)
  92. {
  93. // For the DOMMatrix interface, setting the m21 or the c attribute must set the m21 element to the new value.
  94. m_matrix.elements()[1][0] = value;
  95. }
  96. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m22
  97. void DOMMatrix::set_m22(double value)
  98. {
  99. // For the DOMMatrix interface, setting the m22 or the d attribute must set the m22 element to the new value.
  100. m_matrix.elements()[1][1] = value;
  101. }
  102. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m23
  103. void DOMMatrix::set_m23(double value)
  104. {
  105. // 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.
  106. m_matrix.elements()[1][2] = value;
  107. if (value != 0.0 && value != -0.0)
  108. m_is_2d = false;
  109. }
  110. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m24
  111. void DOMMatrix::set_m24(double value)
  112. {
  113. // 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.
  114. m_matrix.elements()[1][3] = value;
  115. if (value != 0.0 && value != -0.0)
  116. m_is_2d = false;
  117. }
  118. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m31
  119. void DOMMatrix::set_m31(double value)
  120. {
  121. // 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.
  122. m_matrix.elements()[2][0] = value;
  123. if (value != 0.0 && value != -0.0)
  124. m_is_2d = false;
  125. }
  126. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m32
  127. void DOMMatrix::set_m32(double value)
  128. {
  129. // 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.
  130. m_matrix.elements()[2][1] = value;
  131. if (value != 0.0 && value != -0.0)
  132. m_is_2d = false;
  133. }
  134. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m33
  135. void DOMMatrix::set_m33(double value)
  136. {
  137. // 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.
  138. m_matrix.elements()[2][2] = value;
  139. if (value != 1.0)
  140. m_is_2d = false;
  141. }
  142. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m34
  143. void DOMMatrix::set_m34(double value)
  144. {
  145. // 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.
  146. m_matrix.elements()[2][3] = value;
  147. if (value != 0.0 && value != -0.0)
  148. m_is_2d = false;
  149. }
  150. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m41
  151. void DOMMatrix::set_m41(double value)
  152. {
  153. // For the DOMMatrix interface, setting the m41 or the e attribute must set the m41 element to the new value.
  154. m_matrix.elements()[3][0] = value;
  155. }
  156. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m42
  157. void DOMMatrix::set_m42(double value)
  158. {
  159. // For the DOMMatrix interface, setting the m42 or the f attribute must set the m42 element to the new value.
  160. m_matrix.elements()[3][1] = value;
  161. }
  162. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m43
  163. void DOMMatrix::set_m43(double value)
  164. {
  165. // 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.
  166. m_matrix.elements()[3][2] = value;
  167. if (value != 0.0 && value != -0.0)
  168. m_is_2d = false;
  169. }
  170. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-m44
  171. void DOMMatrix::set_m44(double value)
  172. {
  173. // 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.
  174. m_matrix.elements()[3][3] = value;
  175. if (value != 1.0)
  176. m_is_2d = false;
  177. }
  178. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-a
  179. void DOMMatrix::set_a(double value)
  180. {
  181. // For the DOMMatrix interface, setting the m11 or the a attribute must set the m11 element to the new value.
  182. set_m11(value);
  183. }
  184. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-b
  185. void DOMMatrix::set_b(double value)
  186. {
  187. // For the DOMMatrix interface, setting the m12 or the b attribute must set the m12 element to the new value.
  188. set_m12(value);
  189. }
  190. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-c
  191. void DOMMatrix::set_c(double value)
  192. {
  193. // For the DOMMatrix interface, setting the m21 or the c attribute must set the m21 element to the new value.
  194. set_m21(value);
  195. }
  196. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-d
  197. void DOMMatrix::set_d(double value)
  198. {
  199. // For the DOMMatrix interface, setting the m22 or the d attribute must set the m22 element to the new value.
  200. set_m22(value);
  201. }
  202. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-e
  203. void DOMMatrix::set_e(double value)
  204. {
  205. // For the DOMMatrix interface, setting the m41 or the e attribute must set the m41 element to the new value.
  206. set_m41(value);
  207. }
  208. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-f
  209. void DOMMatrix::set_f(double value)
  210. {
  211. // For the DOMMatrix interface, setting the m42 or the f attribute must set the m42 element to the new value.
  212. set_m42(value);
  213. }
  214. // https://drafts.fxtf.org/geometry/#dom-dommatrix-invertself
  215. JS::NonnullGCPtr<DOMMatrix> DOMMatrix::invert_self()
  216. {
  217. // 1. Invert the current matrix.
  218. m_matrix = m_matrix.inverse();
  219. // 2. If the current matrix is not invertible set all attributes to NaN and set is 2D to false.
  220. if (!m_matrix.is_invertible()) {
  221. for (u8 i = 0; i < 4; i++) {
  222. for (u8 j = 0; j < 4; j++)
  223. m_matrix.elements()[i][j] = NAN;
  224. }
  225. m_is_2d = false;
  226. }
  227. // 3. Return the current matrix.
  228. return *this;
  229. }
  230. }