DOMMatrixReadOnly.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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 <LibJS/Runtime/TypedArray.h>
  8. #include <LibWeb/CSS/Parser/Parser.h>
  9. #include <LibWeb/CSS/StyleProperties.h>
  10. #include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
  11. #include <LibWeb/Geometry/DOMMatrix.h>
  12. #include <LibWeb/Geometry/DOMMatrixReadOnly.h>
  13. #include <LibWeb/Geometry/DOMPoint.h>
  14. #include <LibWeb/HTML/Window.h>
  15. #include <LibWeb/WebIDL/Buffers.h>
  16. #include <LibWeb/WebIDL/ExceptionOr.h>
  17. namespace Web::Geometry {
  18. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-dommatrixreadonly
  19. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::construct_impl(JS::Realm& realm, Optional<Variant<String, Vector<double>>> const& init)
  20. {
  21. auto& vm = realm.vm();
  22. // -> If init is omitted
  23. if (!init.has_value()) {
  24. // 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].
  25. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm, 1, 0, 0, 1, 0, 0);
  26. }
  27. auto const& init_value = init.value();
  28. // -> If init is a DOMString
  29. if (init_value.has<String>()) {
  30. // 1. If current global object is not a Window object, then throw a TypeError exception.
  31. if (!is<HTML::Window>(realm.global_object()))
  32. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "This can only be used in a Window context"_string };
  33. // 2. Parse init into an abstract matrix, and let matrix and 2dTransform be the result. If the result is failure, then throw a "SyntaxError" DOMException.
  34. auto result = TRY(parse_dom_matrix_init_string(realm, init_value.get<String>()));
  35. auto* elements = result.matrix.elements();
  36. // If 2dTransform is true
  37. if (result.is_2d_transform) {
  38. // 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 elements m11, m12, m21, m22, m41 and m42 of matrix.
  39. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm, elements[0][0], elements[1][0], elements[0][1], elements[1][1], elements[0][3], elements[1][3]);
  40. }
  41. // 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 of matrix.
  42. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm,
  43. elements[0][0], elements[1][0], elements[2][0], elements[3][0],
  44. elements[0][1], elements[1][1], elements[2][1], elements[3][1],
  45. elements[0][2], elements[1][2], elements[2][2], elements[3][2],
  46. elements[0][3], elements[1][3], elements[2][3], elements[3][3]);
  47. }
  48. auto const& double_sequence = init_value.get<Vector<double>>();
  49. // -> If init is a sequence with 6 elements
  50. if (double_sequence.size() == 6) {
  51. // Return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with the sequence init.
  52. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm, double_sequence[0], double_sequence[1], double_sequence[2], double_sequence[3], double_sequence[4], double_sequence[5]);
  53. }
  54. // -> If init is a sequence with 16 elements
  55. if (double_sequence.size() == 16) {
  56. // Return the result of invoking create a 3d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with the sequence init.
  57. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm,
  58. double_sequence[0], double_sequence[1], double_sequence[2], double_sequence[3],
  59. double_sequence[4], double_sequence[5], double_sequence[6], double_sequence[7],
  60. double_sequence[8], double_sequence[9], double_sequence[10], double_sequence[11],
  61. double_sequence[12], double_sequence[13], double_sequence[14], double_sequence[15]);
  62. }
  63. // -> Otherwise, throw a TypeError exception.
  64. 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())) };
  65. }
  66. // https://drafts.fxtf.org/geometry/#create-a-dommatrixreadonly-from-the-2d-dictionary
  67. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::create_from_dom_matrix_2d_init(JS::Realm& realm, DOMMatrix2DInit& init)
  68. {
  69. // 1. Validate and fixup (2D) other.
  70. TRY(validate_and_fixup_dom_matrix_2d_init(init));
  71. // These should all have values after calling `validate_and_fixup_dom_matrix_2d_init`
  72. VERIFY(init.m11.has_value());
  73. VERIFY(init.m12.has_value());
  74. VERIFY(init.m21.has_value());
  75. VERIFY(init.m22.has_value());
  76. VERIFY(init.m41.has_value());
  77. VERIFY(init.m42.has_value());
  78. // 2. Return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers,
  79. // the values being the 6 elements m11, m12, m21, m22, m41 and m42 of other in the given order.
  80. 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());
  81. }
  82. // https://drafts.fxtf.org/geometry/#create-a-dommatrixreadonly-from-the-dictionary
  83. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::create_from_dom_matrix_init(JS::Realm& realm, DOMMatrixInit& init)
  84. {
  85. // 1. Validate and fixup other.
  86. TRY(validate_and_fixup_dom_matrix_init(init));
  87. // 2. If the is2D dictionary member of other is true.
  88. if (init.is2d.has_value() && init.is2d.value()) {
  89. // 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.
  90. 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());
  91. }
  92. // 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.
  93. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm, init.m11.value(), init.m12.value(), init.m13, init.m14,
  94. init.m21.value(), init.m22.value(), init.m23, init.m24,
  95. init.m31, init.m32, init.m33, init.m34,
  96. init.m41.value(), init.m42.value(), init.m43, init.m44);
  97. }
  98. DOMMatrixReadOnly::DOMMatrixReadOnly(JS::Realm& realm, double m11, double m12, double m21, double m22, double m41, double m42)
  99. : Bindings::PlatformObject(realm)
  100. {
  101. initialize_from_create_2d_matrix(m11, m12, m21, m22, m41, m42);
  102. }
  103. 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)
  104. : Bindings::PlatformObject(realm)
  105. {
  106. initialize_from_create_3d_matrix(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
  107. }
  108. DOMMatrixReadOnly::DOMMatrixReadOnly(JS::Realm& realm, DOMMatrixReadOnly const& other)
  109. : Bindings::PlatformObject(realm)
  110. , m_matrix(other.m_matrix)
  111. , m_is_2d(other.m_is_2d)
  112. {
  113. }
  114. DOMMatrixReadOnly::~DOMMatrixReadOnly() = default;
  115. void DOMMatrixReadOnly::initialize(JS::Realm& realm)
  116. {
  117. Base::initialize(realm);
  118. WEB_SET_PROTOTYPE_FOR_INTERFACE(DOMMatrixReadOnly);
  119. }
  120. // https://drafts.fxtf.org/geometry/#create-a-2d-matrix
  121. void DOMMatrixReadOnly::initialize_from_create_2d_matrix(double m11, double m12, double m21, double m22, double m41, double m42)
  122. {
  123. // 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.
  124. // 1. Let matrix be a new instance of type.
  125. // 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.
  126. auto* elements = m_matrix.elements();
  127. elements[0][0] = m11;
  128. elements[1][0] = m12;
  129. elements[0][1] = m21;
  130. elements[1][1] = m22;
  131. elements[0][3] = m41;
  132. elements[1][3] = m42;
  133. // 3. Set m13 element, m14 element, m23 element, m24 element, m31 element, m32 element, m34 element, and m43 element to 0.
  134. elements[2][0] = 0.0;
  135. elements[3][0] = 0.0;
  136. elements[2][1] = 0.0;
  137. elements[3][1] = 0.0;
  138. elements[0][2] = 0.0;
  139. elements[1][2] = 0.0;
  140. elements[3][2] = 0.0;
  141. elements[2][3] = 0.0;
  142. // 4. Set m33 element and m44 element to 1.
  143. elements[2][2] = 1.0;
  144. elements[3][3] = 1.0;
  145. // 5. Set is 2D to true.
  146. m_is_2d = true;
  147. // 6. Return matrix
  148. }
  149. // https://drafts.fxtf.org/geometry/#create-a-3d-matrix
  150. 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)
  151. {
  152. // 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.
  153. // 1. Let matrix be a new instance of type.
  154. // 2. Set m11 element to m44 element to the values of init in column-major order.
  155. auto* elements = m_matrix.elements();
  156. elements[0][0] = m11;
  157. elements[1][0] = m12;
  158. elements[2][0] = m13;
  159. elements[3][0] = m14;
  160. elements[0][1] = m21;
  161. elements[1][1] = m22;
  162. elements[2][1] = m23;
  163. elements[3][1] = m24;
  164. elements[0][2] = m31;
  165. elements[1][2] = m32;
  166. elements[2][2] = m33;
  167. elements[3][2] = m34;
  168. elements[0][3] = m41;
  169. elements[1][3] = m42;
  170. elements[2][3] = m43;
  171. elements[3][3] = m44;
  172. // 3. Set is 2D to false.
  173. m_is_2d = false;
  174. // 4. Return matrix
  175. }
  176. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-frommatrix
  177. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::from_matrix(JS::VM& vm, DOMMatrixInit& other)
  178. {
  179. return create_from_dom_matrix_init(*vm.current_realm(), other);
  180. }
  181. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-fromfloat32array
  182. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::from_float32_array(JS::VM& vm, JS::Handle<WebIDL::BufferSource> const& array32)
  183. {
  184. if (!is<JS::Float32Array>(*array32))
  185. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Float32Array");
  186. auto& realm = *vm.current_realm();
  187. auto& float32_array = static_cast<JS::Float32Array&>(*array32->raw_object());
  188. ReadonlySpan<float> elements = float32_array.data();
  189. // If array32 has 6 elements, return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers taking the values from array32 in the provided order.
  190. if (elements.size() == 6)
  191. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm, elements.at(0), elements.at(1), elements.at(2), elements.at(3), elements.at(4), elements.at(5));
  192. // If array32 has 16 elements, return the result of invoking create a 3d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers taking the values from array32 in the provided order.
  193. if (elements.size() == 16)
  194. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm, elements.at(0), elements.at(1), elements.at(2), elements.at(3),
  195. elements.at(4), elements.at(5), elements.at(6), elements.at(7),
  196. elements.at(8), elements.at(9), elements.at(10), elements.at(11),
  197. elements.at(12), elements.at(13), elements.at(14), elements.at(15));
  198. // Otherwise, throw a TypeError exception.
  199. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Expected a Float32Array argument with 6 or 16 elements"_string };
  200. }
  201. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-fromfloat64array
  202. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrixReadOnly>> DOMMatrixReadOnly::from_float64_array(JS::VM& vm, JS::Handle<WebIDL::BufferSource> const& array64)
  203. {
  204. if (!is<JS::Float64Array>(*array64))
  205. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Float64Array");
  206. auto& realm = *vm.current_realm();
  207. auto& float64_array = static_cast<JS::Float64Array&>(*array64->raw_object());
  208. ReadonlySpan<double> elements = float64_array.data();
  209. // If array64 has 6 elements, return the result of invoking create a 2d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers taking the values from array64 in the provided order.
  210. if (elements.size() == 6)
  211. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm, elements.at(0), elements.at(1), elements.at(2), elements.at(3), elements.at(4), elements.at(5));
  212. // If array64 has 16 elements, return the result of invoking create a 3d matrix of type DOMMatrixReadOnly or DOMMatrix as appropriate, with a sequence of numbers taking the values from array64 in the provided order.
  213. if (elements.size() == 16)
  214. return realm.heap().allocate<DOMMatrixReadOnly>(realm, realm, elements.at(0), elements.at(1), elements.at(2), elements.at(3),
  215. elements.at(4), elements.at(5), elements.at(6), elements.at(7),
  216. elements.at(8), elements.at(9), elements.at(10), elements.at(11),
  217. elements.at(12), elements.at(13), elements.at(14), elements.at(15));
  218. // Otherwise, throw a TypeError exception.
  219. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Expected a Float64Array argument with 6 or 16 elements"_string };
  220. }
  221. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-isidentity
  222. bool DOMMatrixReadOnly::is_identity() const
  223. {
  224. // The isIdentity attribute must return true if
  225. // m12 element, m13 element, m14 element,
  226. // m21 element, m23 element, m24 element,
  227. // m31 element, m32 element, m34 element
  228. // m41 element, m42 element, m43 element
  229. // are 0 or -0 and
  230. // m11 element, m22 element, m33 element, m44 element are 1.
  231. // Otherwise it must return false.
  232. if (m12() != 0.0 && m12() != -0.0)
  233. return false;
  234. if (m13() != 0.0 && m13() != -0.0)
  235. return false;
  236. if (m14() != 0.0 && m14() != -0.0)
  237. return false;
  238. if (m21() != 0.0 && m21() != -0.0)
  239. return false;
  240. if (m23() != 0.0 && m24() != -0.0)
  241. return false;
  242. if (m31() != 0.0 && m32() != -0.0)
  243. return false;
  244. if (m34() != 0.0 && m34() != -0.0)
  245. return false;
  246. if (m41() != 0.0 && m42() != -0.0)
  247. return false;
  248. if (m43() != 0.0 && m43() != -0.0)
  249. return false;
  250. if (m11() != 1.0)
  251. return false;
  252. if (m22() != 1.0)
  253. return false;
  254. if (m33() != 1.0)
  255. return false;
  256. if (m44() != 1.0)
  257. return false;
  258. return true;
  259. }
  260. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-translate
  261. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::translate(Optional<double> const& tx, Optional<double> const& ty, Optional<double> const& tz) const
  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 translateSelf() transformation on result with the arguments tx, ty, tz.
  266. // 3. Return result.
  267. return result->translate_self(tx, ty, tz);
  268. }
  269. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-scale
  270. 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)
  271. {
  272. // 1. If scaleY is missing, set scaleY to the value of scaleX.
  273. if (!scale_y.has_value())
  274. scale_y = scale_x;
  275. // 2. Let result be the resulting matrix initialized to the values of the current matrix.
  276. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  277. // 3. Perform a scaleSelf() transformation on result with the arguments scaleX, scaleY, scaleZ, originX, originY, originZ.
  278. // 4. Return result.
  279. return result->scale_self(scale_x, scale_y, scale_z, origin_x, origin_y, origin_z);
  280. }
  281. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-scalenonuniform
  282. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::scale_non_uniform(Optional<double> scale_x, Optional<double> scale_y)
  283. {
  284. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  285. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  286. // 2. Perform a scaleSelf() transformation on result with the arguments scaleX, scaleY, 1, 0, 0, 0.
  287. // 3. Return result.
  288. return result->scale_self(scale_x, scale_y, 1, 0, 0, 0);
  289. }
  290. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-scale3d
  291. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::scale3d(Optional<double> scale, Optional<double> origin_x, Optional<double> origin_y, Optional<double> origin_z)
  292. {
  293. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  294. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  295. // 2. Perform a scale3dSelf() transformation on result with the arguments scale, originX, originY, originZ.
  296. // 3. Return result.
  297. return result->scale3d_self(scale, origin_x, origin_y, origin_z);
  298. }
  299. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::rotate(Optional<double> rot_x, Optional<double> rot_y, Optional<double> rot_z)
  300. {
  301. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  302. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  303. // 2. Perform a rotateSelf() transformation on result with the arguments rotX, rotY, rotZ.
  304. // 3. Return result.
  305. return result->rotate_self(rot_x, rot_y, rot_z);
  306. }
  307. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::rotate_from_vector(Optional<double> x, Optional<double> y)
  308. {
  309. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  310. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  311. // 2. Perform a rotateFromVectorSelf() transformation on result with the arguments x, y.
  312. // 3. Return result.
  313. return result->rotate_from_vector_self(x, y);
  314. }
  315. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::rotate_axis_angle(Optional<double> x, Optional<double> y, Optional<double> z, Optional<double> angle)
  316. {
  317. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  318. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  319. // 2. Perform a rotateAxisAngleSelf() transformation on result with the arguments x, y, z, angle.
  320. // 3. Return result.
  321. return result->rotate_axis_angle_self(x, y, z, angle);
  322. }
  323. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-skewx
  324. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::skew_x(double sx) const
  325. {
  326. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  327. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  328. // 2. Perform a skewXSelf() transformation on result with the argument sx.
  329. // 3. Return result.
  330. return result->skew_x_self(sx);
  331. }
  332. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-skewy
  333. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::skew_y(double sy) const
  334. {
  335. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  336. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  337. // 2. Perform a skewYSelf() transformation on result with the argument sy.
  338. // 3. Return result.
  339. return result->skew_y_self(sy);
  340. }
  341. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-multiply
  342. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMMatrix>> DOMMatrixReadOnly::multiply(DOMMatrixInit other)
  343. {
  344. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  345. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  346. // 2. Perform a multiplySelf() transformation on result with the argument other.
  347. // 3. Return result.
  348. return result->multiply_self(other);
  349. }
  350. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-flipx
  351. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::flip_x()
  352. {
  353. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  354. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  355. // 2. Post-multiply result with new DOMMatrix([-1, 0, 0, 1, 0, 0]).
  356. // clang-format off
  357. Gfx::DoubleMatrix4x4 flip_matrix = { -1, 0, 0, 0,
  358. 0, 1, 0, 0,
  359. 0, 0, 1, 0,
  360. 0, 0, 0, 1 };
  361. // clang-format on
  362. result->m_matrix = result->m_matrix * flip_matrix;
  363. // 3. Return result.
  364. return result;
  365. }
  366. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-flipy
  367. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::flip_y()
  368. {
  369. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  370. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  371. // 2. Post-multiply result with new DOMMatrix([1, 0, 0, -1, 0, 0]).
  372. // clang-format off
  373. Gfx::DoubleMatrix4x4 flip_matrix = { 1, 0, 0, 0,
  374. 0, -1, 0, 0,
  375. 0, 0, 1, 0,
  376. 0, 0, 0, 1 };
  377. // clang-format on
  378. result->m_matrix = result->m_matrix * flip_matrix;
  379. // 3. Return result.
  380. return result;
  381. }
  382. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-inverse
  383. JS::NonnullGCPtr<DOMMatrix> DOMMatrixReadOnly::inverse() const
  384. {
  385. // 1. Let result be the resulting matrix initialized to the values of the current matrix.
  386. auto result = DOMMatrix::create_from_dom_matrix_read_only(realm(), *this);
  387. // 2. Perform a invertSelf() transformation on result.
  388. // 3. Return result.
  389. // The current matrix is not modified.
  390. return result->invert_self();
  391. }
  392. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-transformpoint
  393. JS::NonnullGCPtr<DOMPoint> DOMMatrixReadOnly::transform_point(DOMPointInit const& point) const
  394. {
  395. // Let pointObject be the result of invoking create a DOMPoint from the dictionary point.
  396. auto point_object = DOMPoint::from_point(realm().vm(), point);
  397. // Return the result of invoking transform a point with a matrix, given pointObject and the current matrix. The passed argument does not get modified.
  398. return transform_point(point_object);
  399. }
  400. // https://drafts.fxtf.org/geometry/#transform-a-point-with-a-matrix
  401. JS::NonnullGCPtr<DOMPoint> DOMMatrixReadOnly::transform_point(DOMPointReadOnly const& point) const
  402. {
  403. // 1. Let x be point’s x coordinate.
  404. // 2. Let y be point’s y coordinate.
  405. // 3. Let z be point’s z coordinate.
  406. // 4. Let w be point’s w perspective.
  407. // 5. Let pointVector be a new column vector with the elements being x, y, z, and w, respectively.
  408. Vector4<double> point_vector { point.x(), point.y(), point.z(), point.w() };
  409. // 6. Set pointVector to pointVector pre-multiplied by matrix.
  410. // This is really a post multiply because of the transposed m_matrix.
  411. point_vector = m_matrix * point_vector;
  412. // 7. Let transformedPoint be a new DOMPoint object.
  413. // 8. Set transformedPoint’s x coordinate to pointVector’s first element.
  414. // 9. Set transformedPoint’s y coordinate to pointVector’s second element.
  415. // 10. Set transformedPoint’s z coordinate to pointVector’s third element.
  416. // 11. Set transformedPoint’s w perspective to pointVector’s fourth element.
  417. // 12. Return transformedPoint.
  418. return DOMPoint::construct_impl(realm(), point_vector.x(), point_vector.y(), point_vector.z(), point_vector.w());
  419. }
  420. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-tofloat32array
  421. JS::NonnullGCPtr<JS::Float32Array> DOMMatrixReadOnly::to_float32_array() const
  422. {
  423. // Returns the serialized 16 elements m11 to m44 of the current matrix in column-major order as Float32Array.
  424. float elements[16] = { static_cast<float>(m11()), static_cast<float>(m12()), static_cast<float>(m13()), static_cast<float>(m14()),
  425. static_cast<float>(m21()), static_cast<float>(m22()), static_cast<float>(m23()), static_cast<float>(m24()),
  426. static_cast<float>(m31()), static_cast<float>(m32()), static_cast<float>(m33()), static_cast<float>(m34()),
  427. static_cast<float>(m41()), static_cast<float>(m42()), static_cast<float>(m43()), static_cast<float>(m44()) };
  428. auto bytes = MUST(ByteBuffer::copy(elements, sizeof(elements)));
  429. auto array_buffer = JS::ArrayBuffer::create(realm(), move(bytes));
  430. return JS::Float32Array::create(realm(), sizeof(elements) / sizeof(float), array_buffer);
  431. }
  432. // https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-tofloat64array
  433. JS::NonnullGCPtr<JS::Float64Array> DOMMatrixReadOnly::to_float64_array() const
  434. {
  435. // Returns the serialized 16 elements m11 to m44 of the current matrix in column-major order as Float64Array.
  436. double elements[16] = { m11(), m12(), m13(), m14(),
  437. m21(), m22(), m23(), m24(),
  438. m31(), m32(), m33(), m34(),
  439. m41(), m42(), m43(), m44() };
  440. auto bytes = MUST(ByteBuffer::copy(elements, sizeof(elements)));
  441. auto array_buffer = JS::ArrayBuffer::create(realm(), move(bytes));
  442. return JS::Float64Array::create(realm(), sizeof(elements) / sizeof(double), array_buffer);
  443. }
  444. // https://drafts.fxtf.org/geometry/#dommatrixreadonly-stringification-behavior
  445. WebIDL::ExceptionOr<String> DOMMatrixReadOnly::to_string() const
  446. {
  447. auto& vm = this->vm();
  448. // 1. If one or more of m11 element through m44 element are a non-finite value, then throw an "InvalidStateError" DOMException.
  449. // Spec Note: The CSS syntax cannot represent NaN or Infinity values.
  450. if (!isfinite(m11()) || !isfinite(m12()) || !isfinite(m13()) || !isfinite(m14())
  451. || !isfinite(m21()) || !isfinite(m22()) || !isfinite(m23()) || !isfinite(m24())
  452. || !isfinite(m31()) || !isfinite(m32()) || !isfinite(m33()) || !isfinite(m34())
  453. || !isfinite(m41()) || !isfinite(m42()) || !isfinite(m43()) || !isfinite(m44())) {
  454. return WebIDL::InvalidStateError::create(realm(), "Cannot stringify non-finite matrix values"_fly_string);
  455. }
  456. // 2. Let string be the empty string.
  457. StringBuilder builder;
  458. // 3. If is 2D is true, then:
  459. if (m_is_2d) {
  460. // 1. Append "matrix(" to string.
  461. TRY_OR_THROW_OOM(vm, builder.try_append("matrix("sv));
  462. // 2. Append ! ToString(m11 element) to string.
  463. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m11()).to_string_without_side_effects()));
  464. // 3. Append ", " to string.
  465. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  466. // 4. Append ! ToString(m12 element) to string.
  467. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m12()).to_string_without_side_effects()));
  468. // 5. Append ", " to string.
  469. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  470. // 6. Append ! ToString(m21 element) to string.
  471. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m21()).to_string_without_side_effects()));
  472. // 7. Append ", " to string.
  473. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  474. // 8. Append ! ToString(m22 element) to string.
  475. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m22()).to_string_without_side_effects()));
  476. // 9. Append ", " to string.
  477. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  478. // 10. Append ! ToString(m41 element) to string.
  479. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m41()).to_string_without_side_effects()));
  480. // 11. Append ", " to string.
  481. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  482. // 12. Append ! ToString(m42 element) to string.
  483. TRY_OR_THROW_OOM(vm, builder.try_append(JS::Value(m42()).to_string_without_side_effects()));
  484. // 13. Append ")" to string.
  485. TRY_OR_THROW_OOM(vm, builder.try_append(")"sv));
  486. } else {
  487. // 1. Append "matrix3d(" to string.
  488. TRY_OR_THROW_OOM(vm, builder.try_append("matrix3d("sv));
  489. // 2. Append ! ToString(m11 element) to string.
  490. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m11())));
  491. // 3. Append ", " to string.
  492. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  493. // 4. Append ! ToString(m12 element) to string.
  494. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m12())));
  495. // 5. Append ", " to string.
  496. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  497. // 6. Append ! ToString(m13 element) to string.
  498. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m13())));
  499. // 7. Append ", " to string.
  500. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  501. // 8. Append ! ToString(m14 element) to string.
  502. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m14())));
  503. // 9. Append ", " to string.
  504. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  505. // 10. Append ! ToString(m21 element) to string.
  506. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m21())));
  507. // 11. Append ", " to string.
  508. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  509. // 12. Append ! ToString(m22 element) to string.
  510. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m22())));
  511. // 13. Append ", " to string.
  512. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  513. // 14. Append ! ToString(m23 element) to string.
  514. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m23())));
  515. // 15. Append ", " to string.
  516. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  517. // 16. Append ! ToString(m24 element) to string.
  518. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m24())));
  519. // 17. Append ", " to string.
  520. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  521. // NOTE: The spec doesn't include the steps to append m31 to m34, but they are required as matrix3d requires 16 elements.
  522. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m31())));
  523. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  524. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m32())));
  525. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  526. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m33())));
  527. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  528. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m34())));
  529. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  530. // 18. Append ! ToString(m41 element) to string.
  531. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m41())));
  532. // 19. Append ", " to string.
  533. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  534. // 20. Append ! ToString(m42 element) to string.
  535. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m42())));
  536. // 21. Append ", " to string.
  537. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  538. // 22. Append ! ToString(m43 element) to string.
  539. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m43())));
  540. // 23. Append ", " to string.
  541. TRY_OR_THROW_OOM(vm, builder.try_append(", "sv));
  542. // 24. Append ! ToString(m44 element) to string.
  543. TRY_OR_THROW_OOM(vm, builder.try_append(JS::number_to_string(m44())));
  544. // 25. Append ")" to string.
  545. TRY_OR_THROW_OOM(vm, builder.try_append(")"sv));
  546. }
  547. // 5. Return string.
  548. return TRY_OR_THROW_OOM(vm, builder.to_string());
  549. }
  550. // https://drafts.fxtf.org/geometry/#matrix-validate-and-fixup-2d
  551. WebIDL::ExceptionOr<void> validate_and_fixup_dom_matrix_2d_init(DOMMatrix2DInit& init)
  552. {
  553. // 1. If at least one of the following conditions are true for dict, then throw a TypeError exception and abort these steps.
  554. // - a and m11 are both present and SameValueZero(a, m11) is false.
  555. if (init.a.has_value() && init.m11.has_value() && !JS::same_value_zero(JS::Value(init.a.value()), JS::Value(init.m11.value())))
  556. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.a and DOMMatrix2DInit.m11 must have the same value if they are both present"sv };
  557. // - b and m12 are both present and SameValueZero(b, m12) is false.
  558. if (init.b.has_value() && init.m12.has_value() && !JS::same_value_zero(JS::Value(init.b.value()), JS::Value(init.m12.value())))
  559. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.b and DOMMatrix2DInit.m12 must have the same value if they are both present"sv };
  560. // - c and m21 are both present and SameValueZero(c, m21) is false.
  561. if (init.c.has_value() && init.m21.has_value() && !JS::same_value_zero(JS::Value(init.c.value()), JS::Value(init.m21.value())))
  562. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.c and DOMMatrix2DInit.m21 must have the same value if they are both present"sv };
  563. // - d and m22 are both present and SameValueZero(d, m22) is false.
  564. if (init.d.has_value() && init.m22.has_value() && !JS::same_value_zero(JS::Value(init.d.value()), JS::Value(init.m22.value())))
  565. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.d and DOMMatrix2DInit.m22 must have the same value if they are both present"sv };
  566. // - e and m41 are both present and SameValueZero(e, m41) is false.
  567. if (init.e.has_value() && init.m41.has_value() && !JS::same_value_zero(JS::Value(init.e.value()), JS::Value(init.m41.value())))
  568. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.e and DOMMatrix2DInit.m41 must have the same value if they are both present"sv };
  569. // - f and m42 are both present and SameValueZero(f, m42) is false.
  570. if (init.f.has_value() && init.m42.has_value() && !JS::same_value_zero(JS::Value(init.f.value()), JS::Value(init.m42.value())))
  571. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrix2DInit.f and DOMMatrix2DInit.m42 must have the same value if they are both present"sv };
  572. // 2. If m11 is not present then set it to the value of member a, or value 1 if a is also not present.
  573. if (!init.m11.has_value())
  574. init.m11 = init.a.value_or(1.0);
  575. // 3. If m12 is not present then set it to the value of member b, or value 0 if b is also not present.
  576. if (!init.m12.has_value())
  577. init.m12 = init.b.value_or(0.0);
  578. // 4. If m21 is not present then set it to the value of member c, or value 0 if c is also not present.
  579. if (!init.m21.has_value())
  580. init.m21 = init.c.value_or(0.0);
  581. // 5. If m22 is not present then set it to the value of member d, or value 1 if d is also not present.
  582. if (!init.m22.has_value())
  583. init.m22 = init.d.value_or(1.0);
  584. // 6. If m41 is not present then set it to the value of member e, or value 0 if e is also not present.
  585. if (!init.m41.has_value())
  586. init.m41 = init.e.value_or(0.0);
  587. // 7. If m42 is not present then set it to the value of member f, or value 0 if f is also not present.
  588. if (!init.m42.has_value())
  589. init.m42 = init.f.value_or(0.0);
  590. return {};
  591. }
  592. // https://drafts.fxtf.org/geometry/#matrix-validate-and-fixup
  593. WebIDL::ExceptionOr<void> validate_and_fixup_dom_matrix_init(DOMMatrixInit& init)
  594. {
  595. // 1. Validate and fixup (2D) dict.
  596. TRY(validate_and_fixup_dom_matrix_2d_init(init));
  597. // 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,
  598. // or at least one of m33, m44 are present with a value other than 1, then throw a TypeError exception and abort these steps.
  599. if (init.is2d.has_value() && init.is2d.value()) {
  600. if ((init.m13 != 0.0 && init.m13 != -0.0)
  601. || (init.m14 != 0.0 && init.m14 != -0.0)
  602. || (init.m23 != 0.0 && init.m23 != -0.0)
  603. || (init.m24 != 0.0 && init.m24 != -0.0)
  604. || (init.m31 != 0.0 && init.m31 != -0.0)
  605. || (init.m32 != 0.0 && init.m32 != -0.0)
  606. || (init.m34 != 0.0 && init.m34 != -0.0)
  607. || (init.m43 != 0.0 && init.m43 != -0.0)
  608. || init.m33 != 1.0
  609. || init.m44 != 1.0) {
  610. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "DOMMatrixInit.is2D is true, but the given matrix is not a 2D matrix"sv };
  611. }
  612. }
  613. // 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,
  614. // or at least one of m33, m44 are present with a value other than 1, set is2D to false.
  615. if (!init.is2d.has_value()) {
  616. if ((init.m13 != 0.0 && init.m13 != -0.0)
  617. || (init.m14 != 0.0 && init.m14 != -0.0)
  618. || (init.m23 != 0.0 && init.m23 != -0.0)
  619. || (init.m24 != 0.0 && init.m24 != -0.0)
  620. || (init.m31 != 0.0 && init.m31 != -0.0)
  621. || (init.m32 != 0.0 && init.m32 != -0.0)
  622. || (init.m34 != 0.0 && init.m34 != -0.0)
  623. || (init.m43 != 0.0 && init.m43 != -0.0)
  624. || init.m33 != 1.0
  625. || init.m44 != 1.0) {
  626. init.is2d = false;
  627. }
  628. }
  629. // 4. If is2D is still not present, set it to true.
  630. if (!init.is2d.has_value())
  631. init.is2d = true;
  632. return {};
  633. }
  634. // https://drafts.fxtf.org/geometry/#parse-a-string-into-an-abstract-matrix
  635. WebIDL::ExceptionOr<ParsedMatrix> parse_dom_matrix_init_string(JS::Realm& realm, StringView transform_list)
  636. {
  637. // 1. If transformList is the empty string, set it to the string "matrix(1, 0, 0, 1, 0, 0)".
  638. if (transform_list.is_empty())
  639. transform_list = "matrix(1, 0, 0, 1, 0, 0)"sv;
  640. // 2. Parse transformList into parsedValue given the grammar for the CSS transform property.
  641. // The result will be a <transform-list>, the keyword none, or failure.
  642. // If parsedValue is failure, or any <transform-function> has <length> values without absolute length units, or any keyword other than none is used, then return failure. [CSS3-SYNTAX] [CSS3-TRANSFORMS]
  643. auto parsing_context = CSS::Parser::ParsingContext { realm };
  644. auto transform_style_value = parse_css_value(parsing_context, transform_list, CSS::PropertyID::Transform);
  645. if (!transform_style_value)
  646. return WebIDL::SyntaxError::create(realm, "Failed to parse CSS transform string."_fly_string);
  647. auto parsed_value = CSS::StyleProperties::transformations_for_style_value(*transform_style_value);
  648. // 3. If parsedValue is none, set parsedValue to a <transform-list> containing a single identity matrix.
  649. // NOTE: parsed_value is empty on none so for loop in 6 won't modify matrix
  650. auto matrix = Gfx::FloatMatrix4x4::identity();
  651. // 4. Let 2dTransform track the 2D/3D dimension status of parsedValue.
  652. // -> If parsedValue consists of any three-dimensional transform functions, set 2dTransform to false.
  653. // -> Otherwise, set 2dTransform to true.
  654. bool is_2d_transform = true;
  655. for (auto const& transform : parsed_value) {
  656. // https://www.w3.org/TR/css-transforms-1/#two-d-transform-functions
  657. if (transform.function() != CSS::TransformFunction::Matrix
  658. && transform.function() != CSS::TransformFunction::Translate
  659. && transform.function() != CSS::TransformFunction::TranslateX
  660. && transform.function() != CSS::TransformFunction::TranslateY
  661. && transform.function() != CSS::TransformFunction::Scale
  662. && transform.function() != CSS::TransformFunction::ScaleX
  663. && transform.function() != CSS::TransformFunction::ScaleY
  664. && transform.function() != CSS::TransformFunction::Rotate
  665. && transform.function() != CSS::TransformFunction::Skew
  666. && transform.function() != CSS::TransformFunction::SkewX
  667. && transform.function() != CSS::TransformFunction::SkewY)
  668. is_2d_transform = false;
  669. }
  670. // 5. Transform all <transform-function>s to 4x4 abstract matrices by following the “Mathematical Description of Transform Functions”. [CSS3-TRANSFORMS]
  671. // 6. Let matrix be a 4x4 abstract matrix as shown in the initial figure of this section. Post-multiply all matrices from left to right and set matrix to this product.
  672. for (auto const& transform : parsed_value) {
  673. auto const& transform_matrix = transform.to_matrix({});
  674. if (transform_matrix.is_error())
  675. return WebIDL::SyntaxError::create(realm, "Failed to parse CSS transform string."_fly_string);
  676. matrix = matrix * transform_matrix.value();
  677. }
  678. // 7. Return matrix and 2dTransform.
  679. auto* elements = matrix.elements();
  680. Gfx::DoubleMatrix4x4 double_matrix {
  681. static_cast<double>(elements[0][0]), static_cast<double>(elements[0][1]), static_cast<double>(elements[0][2]), static_cast<double>(elements[0][3]),
  682. static_cast<double>(elements[1][0]), static_cast<double>(elements[1][1]), static_cast<double>(elements[1][2]), static_cast<double>(elements[1][3]),
  683. static_cast<double>(elements[2][0]), static_cast<double>(elements[2][1]), static_cast<double>(elements[2][2]), static_cast<double>(elements[2][3]),
  684. static_cast<double>(elements[3][0]), static_cast<double>(elements[3][1]), static_cast<double>(elements[3][2]), static_cast<float>(elements[3][3])
  685. };
  686. return ParsedMatrix { double_matrix, is_2d_transform };
  687. }
  688. }