DOMMatrixReadOnly.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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/Geometry/DOMMatrixReadOnly.h>
  9. #include <LibWeb/Geometry/DOMPoint.h>
  10. #include <LibWeb/WebIDL/ExceptionOr.h>
  11. namespace Web::Geometry {
  12. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::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-dommatrixreadonly-dommatrixreadonly
  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<DOMMatrixReadOnly>(realm, realm, init);
  24. }
  25. // https://drafts.fxtf.org/geometry/#create-a-dommatrixreadonly-from-the-2d-dictionary
  26. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::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<DOMMatrixReadOnly>(realm, realm, init.m11.value(), init.m12.value(), init.m21.value(), init.m22.value(), init.m41.value(), init.m42.value());
  40. }
  41. DOMMatrixReadOnly::DOMMatrixReadOnly(JS::Realm& realm, double m11, double m12, double m21, double m22, double m41, double m42)
  42. : Bindings::PlatformObject(realm)
  43. {
  44. initialize_from_create_2d_matrix(m11, m12, m21, m22, m41, m42);
  45. }
  46. 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)
  47. : Bindings::PlatformObject(realm)
  48. {
  49. initialize_from_create_3d_matrix(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
  50. }
  51. DOMMatrixReadOnly::DOMMatrixReadOnly(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
  52. : Bindings::PlatformObject(realm)
  53. {
  54. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-dommatrixreadonly
  55. // -> If init is omitted
  56. if (!init.has_value()) {
  57. // 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].
  58. initialize_from_create_2d_matrix(1, 0, 0, 1, 0, 0);
  59. return;
  60. }
  61. auto const& init_value = init.value();
  62. // -> If init is a DOMString
  63. if (init_value.has<String>()) {
  64. dbgln("FIXME: Implement initializing DOMMatrix(ReadOnly) from DOMString: '{}'", init_value.get<String>());
  65. // NOTE: This will result in an identity matrix for now.
  66. return;
  67. }
  68. auto const& double_sequence = init_value.get<Vector<double>>();
  69. // -> If init is a sequence with 6 elements
  70. if (double_sequence.size() == 6) {
  71. // Return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with the sequence init.
  72. initialize_from_create_2d_matrix(double_sequence[0], double_sequence[1], double_sequence[2], double_sequence[3], double_sequence[4], double_sequence[5]);
  73. return;
  74. }
  75. // -> If init is a sequence with 16 elements
  76. // NOTE: The "otherwise" case should be handled in construct_impl, leaving the only other possible condition here to be 16 elements.
  77. VERIFY(double_sequence.size() == 16);
  78. // Return the result of invoking create a 3d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with the sequence init.
  79. initialize_from_create_3d_matrix(
  80. double_sequence[0], double_sequence[1], double_sequence[2], double_sequence[3],
  81. double_sequence[4], double_sequence[5], double_sequence[6], double_sequence[7],
  82. double_sequence[8], double_sequence[9], double_sequence[10], double_sequence[11],
  83. double_sequence[12], double_sequence[13], double_sequence[14], double_sequence[15]);
  84. }
  85. DOMMatrixReadOnly::DOMMatrixReadOnly(JS::Realm& realm, DOMMatrixReadOnly const& other)
  86. : Bindings::PlatformObject(realm)
  87. , m_matrix(other.m_matrix)
  88. , m_is_2d(other.m_is_2d)
  89. {
  90. }
  91. DOMMatrixReadOnly::~DOMMatrixReadOnly() = default;
  92. void DOMMatrixReadOnly::initialize(JS::Realm& realm)
  93. {
  94. Base::initialize(realm);
  95. set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMMatrixReadOnlyPrototype>(realm, "DOMMatrixReadOnly"));
  96. }
  97. // https://drafts.fxtf.org/geometry/#create-a-2d-matrix
  98. void DOMMatrixReadOnly::initialize_from_create_2d_matrix(double m11, double m12, double m21, double m22, double m41, double m42)
  99. {
  100. // 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.
  101. // 1. Let matrix be a new instance of type.
  102. // 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.
  103. auto* elements = m_matrix.elements();
  104. elements[0][0] = m11;
  105. elements[1][0] = m12;
  106. elements[0][1] = m21;
  107. elements[1][1] = m22;
  108. elements[0][3] = m41;
  109. elements[1][3] = m42;
  110. // 3. Set m13 element, m14 element, m23 element, m24 element, m31 element, m32 element, m34 element, and m43 element to 0.
  111. elements[2][0] = 0.0;
  112. elements[3][0] = 0.0;
  113. elements[2][1] = 0.0;
  114. elements[3][1] = 0.0;
  115. elements[0][2] = 0.0;
  116. elements[1][2] = 0.0;
  117. elements[3][2] = 0.0;
  118. elements[2][3] = 0.0;
  119. // 4. Set m33 element and m44 element to 1.
  120. elements[2][2] = 1.0;
  121. elements[3][3] = 1.0;
  122. // 5. Set is 2D to true.
  123. m_is_2d = true;
  124. // 6. Return matrix
  125. }
  126. // https://drafts.fxtf.org/geometry/#create-a-3d-matrix
  127. 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)
  128. {
  129. // 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.
  130. // 1. Let matrix be a new instance of type.
  131. // 2. Set m11 element to m44 element to the values of init in column-major order.
  132. auto* elements = m_matrix.elements();
  133. elements[0][0] = m11;
  134. elements[1][0] = m12;
  135. elements[2][0] = m13;
  136. elements[3][0] = m14;
  137. elements[0][1] = m21;
  138. elements[1][1] = m22;
  139. elements[2][1] = m23;
  140. elements[3][1] = m24;
  141. elements[0][2] = m31;
  142. elements[1][2] = m32;
  143. elements[2][2] = m33;
  144. elements[3][2] = m34;
  145. elements[0][3] = m41;
  146. elements[1][3] = m42;
  147. elements[2][3] = m43;
  148. elements[3][3] = m44;
  149. // 3. Set is 2D to false.
  150. m_is_2d = false;
  151. // 4. Return matrix
  152. }
  153. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-frommatrix
  154. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::from_matrix(JS::VM& vm, DOMMatrixInit& other)
  155. {
  156. return create_from_dom_matrix_2d_init(*vm.current_realm(), other);
  157. }
  158. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-isidentity
  159. bool DOMMatrixReadOnly::is_identity() const
  160. {
  161. // The isIdentity attribute must return true if
  162. // m12 element, m13 element, m14 element,
  163. // m21 element, m23 element, m24 element,
  164. // m31 element, m32 element, m34 element
  165. // m41 element, m42 element, m43 element
  166. // are 0 or -0 and
  167. // m11 element, m22 element, m33 element, m44 element are 1.
  168. // Otherwise it must return false.
  169. if (m12() != 0.0 && m12() != -0.0)
  170. return false;
  171. if (m13() != 0.0 && m13() != -0.0)
  172. return false;
  173. if (m14() != 0.0 && m14() != -0.0)
  174. return false;
  175. if (m21() != 0.0 && m21() != -0.0)
  176. return false;
  177. if (m23() != 0.0 && m24() != -0.0)
  178. return false;
  179. if (m31() != 0.0 && m32() != -0.0)
  180. return false;
  181. if (m34() != 0.0 && m34() != -0.0)
  182. return false;
  183. if (m41() != 0.0 && m42() != -0.0)
  184. return false;
  185. if (m43() != 0.0 && m43() != -0.0)
  186. return false;
  187. if (m11() != 1.0)
  188. return false;
  189. if (m22() != 1.0)
  190. return false;
  191. if (m33() != 1.0)
  192. return false;
  193. if (m44() != 1.0)
  194. return false;
  195. return true;
  196. }
  197. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-inverse
  198. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::inverse() const
  199. {
  200. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  201. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  202. // 2. Perform a invertSelf() transformation on result.
  203. // 3. Return result.
  204. // The current matrix is not modified.
  205. return result->invert_self();
  206. }
  207. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-transformpoint
  208. JS::NonnullGCPtr<DOMPoint> DOMMatrixReadOnly::transform_point(DOMPointInit const& point) const
  209. {
  210. // Let pointObject be the result of invoking create a DOMPoint from the dictionary point.
  211. auto point_object = DOMPoint::from_point(realm().vm(), point);
  212. // Return the result of invoking transform a point with a matrix, given pointObject and the current matrix. The passed argument does not get modified.
  213. return transform_point(point_object);
  214. }
  215. // https://drafts.fxtf.org/geometry/#transform-a-point-with-a-matrix
  216. JS::NonnullGCPtr<DOMPoint> DOMMatrixReadOnly::transform_point(DOMPointReadOnly const& point) const
  217. {
  218. // 1. Let x be point’s x coordinate.
  219. // 2. Let y be point’s y coordinate.
  220. // 3. Let z be point’s z coordinate.
  221. // 4. Let w be point’s w perspective.
  222. // 5. Let pointVector be a new column vector with the elements being x, y, z, and w, respectively.
  223. Vector4<double> point_vector { point.x(), point.y(), point.z(), point.w() };
  224. // 6. Set pointVector to pointVector pre-multiplied by matrix.
  225. // This is really a post multiply because of the transposed m_matrix.
  226. point_vector = m_matrix * point_vector;
  227. // 7. Let transformedPoint be a new DOMPoint object.
  228. // 8. Set transformedPoint’s x coordinate to pointVector’s first element.
  229. // 9. Set transformedPoint’s y coordinate to pointVector’s second element.
  230. // 10. Set transformedPoint’s z coordinate to pointVector’s third element.
  231. // 11. Set transformedPoint’s w perspective to pointVector’s fourth element.
  232. // 12. Return transformedPoint.
  233. return DOMPoint::construct_impl(realm(), point_vector.x(), point_vector.y(), point_vector.z(), point_vector.w());
  234. }
  235. // https://drafts.fxtf.org/geometry/#dommatrixreadonly-stringification-behavior
  236. WebIDL::ExceptionOr<String> DOMMatrixReadOnly::to_string() const
  237. {
  238. auto& vm = this->vm();
  239. // 1. If one or more of m11 element through m44 element are a non-finite value, then throw an "InvalidStateError" DOMException.
  240. // Spec Note: The CSS syntax cannot represent NaN or Infinity values.
  241. if (!isfinite(m11()) || !isfinite(m12()) || !isfinite(m13()) || !isfinite(m14())
  242. || !isfinite(m21()) || !isfinite(m22()) || !isfinite(m23()) || !isfinite(m24())
  243. || !isfinite(m31()) || !isfinite(m32()) || !isfinite(m33()) || !isfinite(m34())
  244. || !isfinite(m41()) || !isfinite(m42()) || !isfinite(m43()) || !isfinite(m44())) {
  245. return WebIDL::InvalidStateError::create(realm(), "Cannot stringify non-finite matrix values");
  246. }
  247. // 2. Let string be the empty string.
  248. StringBuilder builder;
  249. // 3. If is 2D is true, then:
  250. if (m_is_2d) {
  251. // 1. Append "matrix(" to string.
  252. TRY_OR_THROW_OOM(vm, builder.try_append("matrix("sv));
  253. // 2. Append ! ToString(m11 element) to string.
  254. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m11()).to_string_without_side_effects()));
  255. // 3. Append ", " to string.
  256. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  257. // 4. Append ! ToString(m12 element) to string.
  258. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m12()).to_string_without_side_effects()));
  259. // 5. Append ", " to string.
  260. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  261. // 6. Append ! ToString(m21 element) to string.
  262. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m21()).to_string_without_side_effects()));
  263. // 7. Append ", " to string.
  264. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  265. // 8. Append ! ToString(m22 element) to string.
  266. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m22()).to_string_without_side_effects()));
  267. // 9. Append ", " to string.
  268. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  269. // 10. Append ! ToString(m41 element) to string.
  270. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m41()).to_string_without_side_effects()));
  271. // 11. Append ", " to string.
  272. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  273. // 12. Append ! ToString(m42 element) to string.
  274. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m42()).to_string_without_side_effects()));
  275. // 13. Append ")" to string.
  276. TRY_OR_THROW_OOM(vm, builder.try_append(")"sv));
  277. } else {
  278. // 1. Append "matrix3d(" to string.
  279. TRY_OR_THROW_OOM(vm, builder.try_append("matrix3d("sv));
  280. // 2. Append ! ToString(m11 element) to string.
  281. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m11())));
  282. // 3. Append ", " to string.
  283. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  284. // 4. Append ! ToString(m12 element) to string.
  285. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m12())));
  286. // 5. Append ", " to string.
  287. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  288. // 6. Append ! ToString(m13 element) to string.
  289. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m13())));
  290. // 7. Append ", " to string.
  291. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  292. // 8. Append ! ToString(m14 element) to string.
  293. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m14())));
  294. // 9. Append ", " to string.
  295. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  296. // 10. Append ! ToString(m21 element) to string.
  297. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m21())));
  298. // 11. Append ", " to string.
  299. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  300. // 12. Append ! ToString(m22 element) to string.
  301. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m22())));
  302. // 13. Append ", " to string.
  303. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  304. // 14. Append ! ToString(m23 element) to string.
  305. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m23())));
  306. // 15. Append ", " to string.
  307. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  308. // 16. Append ! ToString(m24 element) to string.
  309. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m24())));
  310. // 17. Append ", " to string.
  311. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  312. // NOTE: The spec doesn't include the steps to append m31 to m34, but they are required as matrix3d requires 16 elements.
  313. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m31())));
  314. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  315. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m32())));
  316. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  317. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m33())));
  318. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  319. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m34())));
  320. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  321. // 18. Append ! ToString(m41 element) to string.
  322. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m41())));
  323. // 19. Append ", " to string.
  324. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  325. // 20. Append ! ToString(m42 element) to string.
  326. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m42())));
  327. // 21. Append ", " to string.
  328. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  329. // 22. Append ! ToString(m43 element) to string.
  330. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m43())));
  331. // 23. Append ", " to string.
  332. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  333. // 24. Append ! ToString(m44 element) to string.
  334. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m44())));
  335. // 25. Append ")" to string.
  336. TRY_OR_THROW_OOM(vm, builder.try_append(")"sv));
  337. }
  338. // 5. Return string.
  339. return TRY_OR_THROW_OOM(vm, builder.to_string());
  340. }
  341. // https://drafts.fxtf.org/geometry/#matrix-validate-and-fixup-2d
  342. WebIDL::ExceptionOr<void> validate_and_fixup_dom_matrix_2d_init(DOMMatrix2DInit& init)
  343. {
  344. // 1. If at least one of the following conditions are true for dict, then throw a TypeError exception and abort these steps.
  345. // - a and m11 are both present and SameValueZero(a, m11) is false.
  346. if (init.a.has_value() && init.m11.has_value() && !JS::same_value_zero(JS::Value(init.a.value()), JS::Value(init.m11.value())))
  347. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.a and DOMMatrix2DInit.m11 must have the same value if they are both present"sv };
  348. // - b and m12 are both present and SameValueZero(b, m12) is false.
  349. if (init.b.has_value() && init.m12.has_value() && !JS::same_value_zero(JS::Value(init.b.value()), JS::Value(init.m12.value())))
  350. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.b and DOMMatrix2DInit.m12 must have the same value if they are both present"sv };
  351. // - c and m21 are both present and SameValueZero(c, m21) is false.
  352. if (init.c.has_value() && init.m21.has_value() && !JS::same_value_zero(JS::Value(init.c.value()), JS::Value(init.m21.value())))
  353. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.c and DOMMatrix2DInit.m21 must have the same value if they are both present"sv };
  354. // - d and m22 are both present and SameValueZero(d, m22) is false.
  355. if (init.d.has_value() && init.m22.has_value() && !JS::same_value_zero(JS::Value(init.d.value()), JS::Value(init.m22.value())))
  356. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.d and DOMMatrix2DInit.m22 must have the same value if they are both present"sv };
  357. // - e and m41 are both present and SameValueZero(e, m41) is false.
  358. if (init.e.has_value() && init.m41.has_value() && !JS::same_value_zero(JS::Value(init.e.value()), JS::Value(init.m41.value())))
  359. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.e and DOMMatrix2DInit.m41 must have the same value if they are both present"sv };
  360. // - f and m42 are both present and SameValueZero(f, m42) is false.
  361. if (init.f.has_value() && init.m42.has_value() && !JS::same_value_zero(JS::Value(init.f.value()), JS::Value(init.m42.value())))
  362. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.f and DOMMatrix2DInit.m42 must have the same value if they are both present"sv };
  363. // 2. If m11 is not present then set it to the value of member a, or value 1 if a is also not present.
  364. if (!init.m11.has_value())
  365. init.m11 = init.a.value_or(1.0);
  366. // 3. If m12 is not present then set it to the value of member b, or value 0 if b is also not present.
  367. if (!init.m12.has_value())
  368. init.m12 = init.b.value_or(0.0);
  369. // 4. If m21 is not present then set it to the value of member c, or value 0 if c is also not present.
  370. if (!init.m21.has_value())
  371. init.m21 = init.c.value_or(0.0);
  372. // 5. If m22 is not present then set it to the value of member d, or value 1 if d is also not present.
  373. if (!init.m22.has_value())
  374. init.m22 = init.d.value_or(1.0);
  375. // 6. If m41 is not present then set it to the value of member e, or value 0 if e is also not present.
  376. if (!init.m41.has_value())
  377. init.m41 = init.e.value_or(0.0);
  378. // 7. If m42 is not present then set it to the value of member f, or value 0 if f is also not present.
  379. if (!init.m42.has_value())
  380. init.m42 = init.f.value_or(0.0);
  381. return {};
  382. }
  383. // https://drafts.fxtf.org/geometry/#matrix-validate-and-fixup
  384. WebIDL::ExceptionOr<void> validate_and_fixup_dom_matrix_init(DOMMatrixInit& init)
  385. {
  386. // 1. Validate and fixup (2D) dict.
  387. TRY(validate_and_fixup_dom_matrix_2d_init(init));
  388. // 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,
  389. // or at least one of m33, m44 are present with a value other than 1, then throw a TypeError exception and abort these steps.
  390. if (init.is2d.has_value() && init.is2d.value()) {
  391. if ((init.m13 != 0.0 && init.m13 != -0.0)
  392. || (init.m14 != 0.0 && init.m14 != -0.0)
  393. || (init.m23 != 0.0 && init.m23 != -0.0)
  394. || (init.m24 != 0.0 && init.m24 != -0.0)
  395. || (init.m31 != 0.0 && init.m31 != -0.0)
  396. || (init.m32 != 0.0 && init.m32 != -0.0)
  397. || (init.m34 != 0.0 && init.m34 != -0.0)
  398. || (init.m43 != 0.0 && init.m43 != -0.0)
  399. || init.m33 != 1.0
  400. || init.m44 != 1.0) {
  401. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrixInit.is2D is true, but the given matrix is not a 2D matrix"sv };
  402. }
  403. }
  404. // 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,
  405. // or at least one of m33, m44 are present with a value other than 1, set is2D to false.
  406. if (!init.is2d.has_value()) {
  407. if ((init.m13 != 0.0 && init.m13 != -0.0)
  408. || (init.m14 != 0.0 && init.m14 != -0.0)
  409. || (init.m23 != 0.0 && init.m23 != -0.0)
  410. || (init.m24 != 0.0 && init.m24 != -0.0)
  411. || (init.m31 != 0.0 && init.m31 != -0.0)
  412. || (init.m32 != 0.0 && init.m32 != -0.0)
  413. || (init.m34 != 0.0 && init.m34 != -0.0)
  414. || (init.m43 != 0.0 && init.m43 != -0.0)
  415. || init.m33 != 1.0
  416. || init.m44 != 1.0) {
  417. init.is2d = false;
  418. }
  419. }
  420. // 4. If is2D is still not present, set it to true.
  421. if (!init.is2d.has_value())
  422. init.is2d = true;
  423. return {};
  424. }
  425. }