DOMMatrixReadOnly.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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/Geometry/DOMMatrixReadOnly.h>
  10. #include <LibWeb/Geometry/DOMPoint.h>
  11. #include <LibWeb/WebIDL/ExceptionOr.h>
  12. namespace Web::Geometry {
  13. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::construct_impl(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
  14. {
  15. auto& vm = realm.vm();
  16. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-dommatrixreadonly
  17. if (init.has_value()) {
  18. // -> Otherwise
  19. // Throw a TypeError exception.
  20. // The only condition where this can be met is with a sequence type which doesn't have exactly 6 or 16 elements.
  21. if (auto* double_sequence = init.value().get_pointer<Vector<double>>(); double_sequence && (double_sequence->size() != 6 && double_sequence->size() != 16))
  22. 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())) };
  23. }
  24. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm, init);
  25. }
  26. // https://drafts.fxtf.org/geometry/#create-a-dommatrixreadonly-from-the-2d-dictionary
  27. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::create_from_dom_matrix_2d_init(JS::Realm& realm, DOMMatrix2DInit& init)
  28. {
  29. // 1. Validate and fixup (2D) other.
  30. TRY(validate_and_fixup_dom_matrix_2d_init(init));
  31. // These should all have values after calling `validate_and_fixup_dom_matrix_2d_init`
  32. VERIFY(init.m11.has_value());
  33. VERIFY(init.m12.has_value());
  34. VERIFY(init.m21.has_value());
  35. VERIFY(init.m22.has_value());
  36. VERIFY(init.m41.has_value());
  37. VERIFY(init.m42.has_value());
  38. // 2. Return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers,
  39. // the values being the 6 elements m11, m12, m21, m22, m41 and m42 of other in the given order.
  40. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm, init.m11.value(), init.m12.value(), init.m21.value(), init.m22.value(), init.m41.value(), init.m42.value());
  41. }
  42. // https://drafts.fxtf.org/geometry/#create-a-dommatrixreadonly-from-the-dictionary
  43. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::create_from_dom_matrix_init(JS::Realm& realm, DOMMatrixInit& init)
  44. {
  45. // 1. Validate and fixup other.
  46. TRY(validate_and_fixup_dom_matrix_init(init));
  47. // 2. If the is2D dictionary member of other is true.
  48. if (init.is2d.has_value() && init.is2d.value()) {
  49. // 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.
  50. 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());
  51. }
  52. // 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.
  53. return realm.heap().allocate<DOMMatrix>(realm, realm, init.m11.value(), init.m12.value(), init.m13, init.m14,
  54. init.m21.value(), init.m22.value(), init.m23, init.m24,
  55. init.m31, init.m32, init.m33, init.m34,
  56. init.m41.value(), init.m42.value(), init.m43, init.m44);
  57. }
  58. DOMMatrixReadOnly::DOMMatrixReadOnly(JS::Realm& realm, double m11, double m12, double m21, double m22, double m41, double m42)
  59. : Bindings::PlatformObject(realm)
  60. {
  61. initialize_from_create_2d_matrix(m11, m12, m21, m22, m41, m42);
  62. }
  63. DOMMatrixReadOnly::DOMMatrixReadOnly(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)
  64. : Bindings::PlatformObject(realm)
  65. {
  66. initialize_from_create_3d_matrix(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
  67. }
  68. DOMMatrixReadOnly::DOMMatrixReadOnly(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
  69. : Bindings::PlatformObject(realm)
  70. {
  71. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-dommatrixreadonly
  72. // -> If init is omitted
  73. if (!init.has_value()) {
  74. // Return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with the sequence [1, 0, 0, 1, 0, 0].
  75. initialize_from_create_2d_matrix(1, 0, 0, 1, 0, 0);
  76. return;
  77. }
  78. auto const& init_value = init.value();
  79. // -> If init is a DOMString
  80. if (init_value.has<String>()) {
  81. dbgln("FIXME: Implement initializing DOMMatrix(ReadOnly) from DOMString: '{}'", init_value.get<String>());
  82. // NOTE: This will result in an identity matrix for now.
  83. return;
  84. }
  85. auto const& double_sequence = init_value.get<Vector<double>>();
  86. // -> If init is a sequence with 6 elements
  87. if (double_sequence.size() == 6) {
  88. // Return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with the sequence init.
  89. initialize_from_create_2d_matrix(double_sequence[0], double_sequence[1], double_sequence[2], double_sequence[3], double_sequence[4], double_sequence[5]);
  90. return;
  91. }
  92. // -> If init is a sequence with 16 elements
  93. // NOTE: The "otherwise" case should be handled in construct_impl, leaving the only other possible condition here to be 16 elements.
  94. VERIFY(double_sequence.size() == 16);
  95. // Return the result of invoking create a 3d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with the sequence init.
  96. initialize_from_create_3d_matrix(
  97. double_sequence[0], double_sequence[1], double_sequence[2], double_sequence[3],
  98. double_sequence[4], double_sequence[5], double_sequence[6], double_sequence[7],
  99. double_sequence[8], double_sequence[9], double_sequence[10], double_sequence[11],
  100. double_sequence[12], double_sequence[13], double_sequence[14], double_sequence[15]);
  101. }
  102. DOMMatrixReadOnly::DOMMatrixReadOnly(JS::Realm& realm, DOMMatrixReadOnly const& other)
  103. : Bindings::PlatformObject(realm)
  104. , m_matrix(other.m_matrix)
  105. , m_is_2d(other.m_is_2d)
  106. {
  107. }
  108. DOMMatrixReadOnly::~DOMMatrixReadOnly() = default;
  109. void DOMMatrixReadOnly::initialize(JS::Realm& realm)
  110. {
  111. Base::initialize(realm);
  112. set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMMatrixReadOnlyPrototype>(realm, "DOMMatrixReadOnly"));
  113. }
  114. // https://drafts.fxtf.org/geometry/#create-a-2d-matrix
  115. void DOMMatrixReadOnly::initialize_from_create_2d_matrix(double m11, double m12, double m21, double m22, double m41, double m42)
  116. {
  117. // NOTE: The matrix used in the spec is column-major (https://drafts.fxtf.org/geometry/#4x4-abstract-matrix) but Gfx::Matrix4x4 is row-major so we need to transpose the values.
  118. // 1. Let matrix be a new instance of type.
  119. // 2. Set m11 element, m12 element, m21 element, m22 element, m41 element and m42 element to the values of init in order starting with the first value.
  120. auto* elements = m_matrix.elements();
  121. elements[0][0] = m11;
  122. elements[1][0] = m12;
  123. elements[0][1] = m21;
  124. elements[1][1] = m22;
  125. elements[0][3] = m41;
  126. elements[1][3] = m42;
  127. // 3. Set m13 element, m14 element, m23 element, m24 element, m31 element, m32 element, m34 element, and m43 element to 0.
  128. elements[2][0] = 0.0;
  129. elements[3][0] = 0.0;
  130. elements[2][1] = 0.0;
  131. elements[3][1] = 0.0;
  132. elements[0][2] = 0.0;
  133. elements[1][2] = 0.0;
  134. elements[3][2] = 0.0;
  135. elements[2][3] = 0.0;
  136. // 4. Set m33 element and m44 element to 1.
  137. elements[2][2] = 1.0;
  138. elements[3][3] = 1.0;
  139. // 5. Set is 2D to true.
  140. m_is_2d = true;
  141. // 6. Return matrix
  142. }
  143. // https://drafts.fxtf.org/geometry/#create-a-3d-matrix
  144. void DOMMatrixReadOnly::initialize_from_create_3d_matrix(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)
  145. {
  146. // NOTE: The matrix used in the spec is column-major (https://drafts.fxtf.org/geometry/#4x4-abstract-matrix) but Gfx::Matrix4x4 is row-major so we need to transpose the values.
  147. // 1. Let matrix be a new instance of type.
  148. // 2. Set m11 element to m44 element to the values of init in column-major order.
  149. auto* elements = m_matrix.elements();
  150. elements[0][0] = m11;
  151. elements[1][0] = m12;
  152. elements[2][0] = m13;
  153. elements[3][0] = m14;
  154. elements[0][1] = m21;
  155. elements[1][1] = m22;
  156. elements[2][1] = m23;
  157. elements[3][1] = m24;
  158. elements[0][2] = m31;
  159. elements[1][2] = m32;
  160. elements[2][2] = m33;
  161. elements[3][2] = m34;
  162. elements[0][3] = m41;
  163. elements[1][3] = m42;
  164. elements[2][3] = m43;
  165. elements[3][3] = m44;
  166. // 3. Set is 2D to false.
  167. m_is_2d = false;
  168. // 4. Return matrix
  169. }
  170. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-frommatrix
  171. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::from_matrix(JS::VM& vm, DOMMatrixInit& other)
  172. {
  173. return create_from_dom_matrix_init(*vm.current_realm(), other);
  174. }
  175. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-isidentity
  176. bool DOMMatrixReadOnly::is_identity() const
  177. {
  178. // The isIdentity attribute must return true if
  179. // m12 element, m13 element, m14 element,
  180. // m21 element, m23 element, m24 element,
  181. // m31 element, m32 element, m34 element
  182. // m41 element, m42 element, m43 element
  183. // are 0 or -0 and
  184. // m11 element, m22 element, m33 element, m44 element are 1.
  185. // Otherwise it must return false.
  186. if (m12() != 0.0 && m12() != -0.0)
  187. return false;
  188. if (m13() != 0.0 && m13() != -0.0)
  189. return false;
  190. if (m14() != 0.0 && m14() != -0.0)
  191. return false;
  192. if (m21() != 0.0 && m21() != -0.0)
  193. return false;
  194. if (m23() != 0.0 && m24() != -0.0)
  195. return false;
  196. if (m31() != 0.0 && m32() != -0.0)
  197. return false;
  198. if (m34() != 0.0 && m34() != -0.0)
  199. return false;
  200. if (m41() != 0.0 && m42() != -0.0)
  201. return false;
  202. if (m43() != 0.0 && m43() != -0.0)
  203. return false;
  204. if (m11() != 1.0)
  205. return false;
  206. if (m22() != 1.0)
  207. return false;
  208. if (m33() != 1.0)
  209. return false;
  210. if (m44() != 1.0)
  211. return false;
  212. return true;
  213. }
  214. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-translate
  215. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::translate(Optional<double> const& tx, Optional<double> const& ty, Optional<double> const& tz) const
  216. {
  217. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  218. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  219. // 2. Perform a translateSelf() transformation on result with the arguments tx, ty, tz.
  220. // 3. Return result.
  221. return result->translate_self(tx, ty, tz);
  222. }
  223. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-scale
  224. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::scale(Optional<double> scale_x, Optional<double> scale_y, Optional<double> scale_z, Optional<double> origin_x, Optional<double> origin_y, Optional<double> origin_z)
  225. {
  226. // 1. If scaleY is missing, set scaleY to the value of scaleX.
  227. if (!scale_y.has_value())
  228. scale_y = scale_x;
  229. // 2. Let result be the resulting matrix initialized to the values of the current matrix.
  230. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  231. // 3. Perform a scaleSelf() transformation on result with the arguments scaleX, scaleY, scaleZ, originX, originY, originZ.
  232. // 4. Return result.
  233. return result->scale_self(scale_x, scale_y, scale_z, origin_x, origin_y, origin_z);
  234. }
  235. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-scalenonuniform
  236. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::scale_non_uniform(Optional<double> scale_x, Optional<double> scale_y)
  237. {
  238. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  239. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  240. // 2. Perform a scaleSelf() transformation on result with the arguments scaleX, scaleY, 1, 0, 0, 0.
  241. // 3. Return result.
  242. return result->scale_self(scale_x, scale_y, 1, 0, 0, 0);
  243. }
  244. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-scale3d
  245. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::scale3d(Optional<double> scale, Optional<double> origin_x, Optional<double> origin_y, Optional<double> origin_z)
  246. {
  247. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  248. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  249. // 2. Perform a scale3dSelf() transformation on result with the arguments scale, originX, originY, originZ.
  250. // 3. Return result.
  251. return result->scale3d_self(scale, origin_x, origin_y, origin_z);
  252. }
  253. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::rotate(Optional<double> rot_x, Optional<double> rot_y, Optional<double> rot_z)
  254. {
  255. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  256. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  257. // 2. Perform a rotateSelf() transformation on result with the arguments rotX, rotY, rotZ.
  258. // 3. Return result.
  259. return result->rotate_self(rot_x, rot_y, rot_z);
  260. }
  261. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::rotate_from_vector(Optional<double> x, Optional<double> y)
  262. {
  263. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  264. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  265. // 2. Perform a rotateFromVectorSelf() transformation on result with the arguments x, y.
  266. // 3. Return result.
  267. return result->rotate_from_vector_self(x, y);
  268. }
  269. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::rotate_axis_angle(Optional<double> x, Optional<double> y, Optional<double> z, Optional<double> angle)
  270. {
  271. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  272. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  273. // 2. Perform a rotateAxisAngleSelf() transformation on result with the arguments x, y, z, angle.
  274. // 3. Return result.
  275. return result->rotate_axis_angle_self(x, y, z, angle);
  276. }
  277. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-skewx
  278. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::skew_x(double sx) const
  279. {
  280. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  281. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  282. // 2. Perform a skewXSelf() transformation on result with the argument sx.
  283. // 3. Return result.
  284. return result->skew_x_self(sx);
  285. }
  286. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-skewy
  287. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::skew_y(double sy) const
  288. {
  289. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  290. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  291. // 2. Perform a skewYSelf() transformation on result with the argument sy.
  292. // 3. Return result.
  293. return result->skew_y_self(sy);
  294. }
  295. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-multiply
  296. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrixReadOnly::multiply(DOMMatrixInit other)
  297. {
  298. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  299. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  300. // 2. Perform a multiplySelf() transformation on result with the argument other.
  301. // 3. Return result.
  302. return result->multiply_self(other);
  303. }
  304. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-flipx
  305. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::flip_x()
  306. {
  307. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  308. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  309. // 2. Post-multiply result with new DOMMatrix([-1, 0, 0, 1, 0, 0]).
  310. // clang-format off
  311. Gfx::DoubleMatrix4x4 flip_matrix = { -1, 0, 0, 0,
  312. 0, 1, 0, 0,
  313. 0, 0, 1, 0,
  314. 0, 0, 0, 1 };
  315. // clang-format on
  316. result->m_matrix = result->m_matrix * flip_matrix;
  317. // 3. Return result.
  318. return result;
  319. }
  320. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-flipy
  321. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::flip_y()
  322. {
  323. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  324. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  325. // 2. Post-multiply result with new DOMMatrix([1, 0, 0, -1, 0, 0]).
  326. // clang-format off
  327. Gfx::DoubleMatrix4x4 flip_matrix = { 1, 0, 0, 0,
  328. 0, -1, 0, 0,
  329. 0, 0, 1, 0,
  330. 0, 0, 0, 1 };
  331. // clang-format on
  332. result->m_matrix = result->m_matrix * flip_matrix;
  333. // 3. Return result.
  334. return result;
  335. }
  336. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-inverse
  337. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::inverse() const
  338. {
  339. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  340. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  341. // 2. Perform a invertSelf() transformation on result.
  342. // 3. Return result.
  343. // The current matrix is not modified.
  344. return result->invert_self();
  345. }
  346. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-transformpoint
  347. JS::NonnullGCPtr<DOMPoint> DOMMatrixReadOnly::transform_point(DOMPointInit const& point) const
  348. {
  349. // Let pointObject be the result of invoking create a DOMPoint from the dictionary point.
  350. auto point_object = DOMPoint::from_point(realm().vm(), point);
  351. // Return the result of invoking transform a point with a matrix, given pointObject and the current matrix. The passed argument does not get modified.
  352. return transform_point(point_object);
  353. }
  354. // https://drafts.fxtf.org/geometry/#transform-a-point-with-a-matrix
  355. JS::NonnullGCPtr<DOMPoint> DOMMatrixReadOnly::transform_point(DOMPointReadOnly const& point) const
  356. {
  357. // 1. Let x be point’s x coordinate.
  358. // 2. Let y be point’s y coordinate.
  359. // 3. Let z be point’s z coordinate.
  360. // 4. Let w be point’s w perspective.
  361. // 5. Let pointVector be a new column vector with the elements being x, y, z, and w, respectively.
  362. Vector4<double> point_vector { point.x(), point.y(), point.z(), point.w() };
  363. // 6. Set pointVector to pointVector pre-multiplied by matrix.
  364. // This is really a post multiply because of the transposed m_matrix.
  365. point_vector = m_matrix * point_vector;
  366. // 7. Let transformedPoint be a new DOMPoint object.
  367. // 8. Set transformedPoint’s x coordinate to pointVector’s first element.
  368. // 9. Set transformedPoint’s y coordinate to pointVector’s second element.
  369. // 10. Set transformedPoint’s z coordinate to pointVector’s third element.
  370. // 11. Set transformedPoint’s w perspective to pointVector’s fourth element.
  371. // 12. Return transformedPoint.
  372. return DOMPoint::construct_impl(realm(), point_vector.x(), point_vector.y(), point_vector.z(), point_vector.w());
  373. }
  374. // https://drafts.fxtf.org/geometry/#dommatrixreadonly-stringification-behavior
  375. WebIDL::ExceptionOr<String> DOMMatrixReadOnly::to_string() const
  376. {
  377. auto& vm = this->vm();
  378. // 1. If one or more of m11 element through m44 element are a non-finite value, then throw an "InvalidStateError" DOMException.
  379. // Spec Note: The CSS syntax cannot represent NaN or Infinity values.
  380. if (!isfinite(m11()) || !isfinite(m12()) || !isfinite(m13()) || !isfinite(m14())
  381. || !isfinite(m21()) || !isfinite(m22()) || !isfinite(m23()) || !isfinite(m24())
  382. || !isfinite(m31()) || !isfinite(m32()) || !isfinite(m33()) || !isfinite(m34())
  383. || !isfinite(m41()) || !isfinite(m42()) || !isfinite(m43()) || !isfinite(m44())) {
  384. return WebIDL::InvalidStateError::create(realm(), "Cannot stringify non-finite matrix values"_fly_string);
  385. }
  386. // 2. Let string be the empty string.
  387. StringBuilder builder;
  388. // 3. If is 2D is true, then:
  389. if (m_is_2d) {
  390. // 1. Append "matrix(" to string.
  391. TRY_OR_THROW_OOM(vm, builder.try_append("matrix("sv));
  392. // 2. Append ! ToString(m11 element) to string.
  393. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m11()).to_string_without_side_effects()));
  394. // 3. Append ", " to string.
  395. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  396. // 4. Append ! ToString(m12 element) to string.
  397. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m12()).to_string_without_side_effects()));
  398. // 5. Append ", " to string.
  399. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  400. // 6. Append ! ToString(m21 element) to string.
  401. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m21()).to_string_without_side_effects()));
  402. // 7. Append ", " to string.
  403. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  404. // 8. Append ! ToString(m22 element) to string.
  405. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m22()).to_string_without_side_effects()));
  406. // 9. Append ", " to string.
  407. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  408. // 10. Append ! ToString(m41 element) to string.
  409. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m41()).to_string_without_side_effects()));
  410. // 11. Append ", " to string.
  411. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  412. // 12. Append ! ToString(m42 element) to string.
  413. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m42()).to_string_without_side_effects()));
  414. // 13. Append ")" to string.
  415. TRY_OR_THROW_OOM(vm, builder.try_append(")"sv));
  416. } else {
  417. // 1. Append "matrix3d(" to string.
  418. TRY_OR_THROW_OOM(vm, builder.try_append("matrix3d("sv));
  419. // 2. Append ! ToString(m11 element) to string.
  420. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m11())));
  421. // 3. Append ", " to string.
  422. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  423. // 4. Append ! ToString(m12 element) to string.
  424. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m12())));
  425. // 5. Append ", " to string.
  426. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  427. // 6. Append ! ToString(m13 element) to string.
  428. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m13())));
  429. // 7. Append ", " to string.
  430. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  431. // 8. Append ! ToString(m14 element) to string.
  432. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m14())));
  433. // 9. Append ", " to string.
  434. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  435. // 10. Append ! ToString(m21 element) to string.
  436. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m21())));
  437. // 11. Append ", " to string.
  438. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  439. // 12. Append ! ToString(m22 element) to string.
  440. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m22())));
  441. // 13. Append ", " to string.
  442. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  443. // 14. Append ! ToString(m23 element) to string.
  444. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m23())));
  445. // 15. Append ", " to string.
  446. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  447. // 16. Append ! ToString(m24 element) to string.
  448. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m24())));
  449. // 17. Append ", " to string.
  450. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  451. // NOTE: The spec doesn't include the steps to append m31 to m34, but they are required as matrix3d requires 16 elements.
  452. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m31())));
  453. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  454. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m32())));
  455. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  456. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m33())));
  457. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  458. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m34())));
  459. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  460. // 18. Append ! ToString(m41 element) to string.
  461. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m41())));
  462. // 19. Append ", " to string.
  463. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  464. // 20. Append ! ToString(m42 element) to string.
  465. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m42())));
  466. // 21. Append ", " to string.
  467. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  468. // 22. Append ! ToString(m43 element) to string.
  469. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m43())));
  470. // 23. Append ", " to string.
  471. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  472. // 24. Append ! ToString(m44 element) to string.
  473. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m44())));
  474. // 25. Append ")" to string.
  475. TRY_OR_THROW_OOM(vm, builder.try_append(")"sv));
  476. }
  477. // 5. Return string.
  478. return TRY_OR_THROW_OOM(vm, builder.to_string());
  479. }
  480. // https://drafts.fxtf.org/geometry/#matrix-validate-and-fixup-2d
  481. WebIDL::ExceptionOr<void> validate_and_fixup_dom_matrix_2d_init(DOMMatrix2DInit& init)
  482. {
  483. // 1. If at least one of the following conditions are true for dict, then throw a TypeError exception and abort these steps.
  484. // - a and m11 are both present and SameValueZero(a, m11) is false.
  485. if (init.a.has_value() && init.m11.has_value() && !JS::same_value_zero(JS::Value(init.a.value()), JS::Value(init.m11.value())))
  486. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.a and DOMMatrix2DInit.m11 must have the same value if they are both present"sv };
  487. // - b and m12 are both present and SameValueZero(b, m12) is false.
  488. if (init.b.has_value() && init.m12.has_value() && !JS::same_value_zero(JS::Value(init.b.value()), JS::Value(init.m12.value())))
  489. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.b and DOMMatrix2DInit.m12 must have the same value if they are both present"sv };
  490. // - c and m21 are both present and SameValueZero(c, m21) is false.
  491. if (init.c.has_value() && init.m21.has_value() && !JS::same_value_zero(JS::Value(init.c.value()), JS::Value(init.m21.value())))
  492. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.c and DOMMatrix2DInit.m21 must have the same value if they are both present"sv };
  493. // - d and m22 are both present and SameValueZero(d, m22) is false.
  494. if (init.d.has_value() && init.m22.has_value() && !JS::same_value_zero(JS::Value(init.d.value()), JS::Value(init.m22.value())))
  495. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.d and DOMMatrix2DInit.m22 must have the same value if they are both present"sv };
  496. // - e and m41 are both present and SameValueZero(e, m41) is false.
  497. if (init.e.has_value() && init.m41.has_value() && !JS::same_value_zero(JS::Value(init.e.value()), JS::Value(init.m41.value())))
  498. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.e and DOMMatrix2DInit.m41 must have the same value if they are both present"sv };
  499. // - f and m42 are both present and SameValueZero(f, m42) is false.
  500. if (init.f.has_value() && init.m42.has_value() && !JS::same_value_zero(JS::Value(init.f.value()), JS::Value(init.m42.value())))
  501. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.f and DOMMatrix2DInit.m42 must have the same value if they are both present"sv };
  502. // 2. If m11 is not present then set it to the value of member a, or value 1 if a is also not present.
  503. if (!init.m11.has_value())
  504. init.m11 = init.a.value_or(1.0);
  505. // 3. If m12 is not present then set it to the value of member b, or value 0 if b is also not present.
  506. if (!init.m12.has_value())
  507. init.m12 = init.b.value_or(0.0);
  508. // 4. If m21 is not present then set it to the value of member c, or value 0 if c is also not present.
  509. if (!init.m21.has_value())
  510. init.m21 = init.c.value_or(0.0);
  511. // 5. If m22 is not present then set it to the value of member d, or value 1 if d is also not present.
  512. if (!init.m22.has_value())
  513. init.m22 = init.d.value_or(1.0);
  514. // 6. If m41 is not present then set it to the value of member e, or value 0 if e is also not present.
  515. if (!init.m41.has_value())
  516. init.m41 = init.e.value_or(0.0);
  517. // 7. If m42 is not present then set it to the value of member f, or value 0 if f is also not present.
  518. if (!init.m42.has_value())
  519. init.m42 = init.f.value_or(0.0);
  520. return {};
  521. }
  522. // https://drafts.fxtf.org/geometry/#matrix-validate-and-fixup
  523. WebIDL::ExceptionOr<void> validate_and_fixup_dom_matrix_init(DOMMatrixInit& init)
  524. {
  525. // 1. Validate and fixup (2D) dict.
  526. TRY(validate_and_fixup_dom_matrix_2d_init(init));
  527. // 2. If is2D is true and: at least one of m13, m14, m23, m24, m31, m32, m34, m43 are present with a value other than 0 or -0,
  528. // or at least one of m33, m44 are present with a value other than 1, then throw a TypeError exception and abort these steps.
  529. if (init.is2d.has_value() && init.is2d.value()) {
  530. if ((init.m13 != 0.0 && init.m13 != -0.0)
  531. || (init.m14 != 0.0 && init.m14 != -0.0)
  532. || (init.m23 != 0.0 && init.m23 != -0.0)
  533. || (init.m24 != 0.0 && init.m24 != -0.0)
  534. || (init.m31 != 0.0 && init.m31 != -0.0)
  535. || (init.m32 != 0.0 && init.m32 != -0.0)
  536. || (init.m34 != 0.0 && init.m34 != -0.0)
  537. || (init.m43 != 0.0 && init.m43 != -0.0)
  538. || init.m33 != 1.0
  539. || init.m44 != 1.0) {
  540. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrixInit.is2D is true, but the given matrix is not a 2D matrix"sv };
  541. }
  542. }
  543. // If is2D is not present and at least one of m13, m14, m23, m24, m31, m32, m34, m43 are present with a value other than 0 or -0,
  544. // or at least one of m33, m44 are present with a value other than 1, set is2D to false.
  545. if (!init.is2d.has_value()) {
  546. if ((init.m13 != 0.0 && init.m13 != -0.0)
  547. || (init.m14 != 0.0 && init.m14 != -0.0)
  548. || (init.m23 != 0.0 && init.m23 != -0.0)
  549. || (init.m24 != 0.0 && init.m24 != -0.0)
  550. || (init.m31 != 0.0 && init.m31 != -0.0)
  551. || (init.m32 != 0.0 && init.m32 != -0.0)
  552. || (init.m34 != 0.0 && init.m34 != -0.0)
  553. || (init.m43 != 0.0 && init.m43 != -0.0)
  554. || init.m33 != 1.0
  555. || init.m44 != 1.0) {
  556. init.is2d = false;
  557. }
  558. }
  559. // 4. If is2D is still not present, set it to true.
  560. if (!init.is2d.has_value())
  561. init.is2d = true;
  562. return {};
  563. }
  564. }