From 2e122b16e470b30e11183f0c5a2b0f8fda43b51c Mon Sep 17 00:00:00 2001 From: Bastiaan van der Plaat Date: Mon, 25 Sep 2023 18:44:10 +0200 Subject: [PATCH] LibWeb: Add DOMMatrix toFloat32Array and toFloat64Array --- .../BindingsGenerator/IDLGenerators.cpp | 2 ++ .../expected/geometry/dommatrix-to-array.txt | 4 +++ .../input/geometry/dommatrix-to-array.html | 21 ++++++++++++++ .../LibWeb/Geometry/DOMMatrixReadOnly.cpp | 28 +++++++++++++++++++ .../LibWeb/Geometry/DOMMatrixReadOnly.h | 2 ++ .../LibWeb/Geometry/DOMMatrixReadOnly.idl | 4 +-- 6 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/geometry/dommatrix-to-array.txt create mode 100644 Tests/LibWeb/Text/input/geometry/dommatrix-to-array.html diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 96e6b8301cc..d8922a0c600 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -84,6 +84,8 @@ static bool is_javascript_builtin(Type const& type) // might simply need to add another type here. static constexpr Array types = { "ArrayBuffer"sv, + "Float32Array"sv, + "Float64Array"sv, }; return types.span().contains_slow(type.name()); diff --git a/Tests/LibWeb/Text/expected/geometry/dommatrix-to-array.txt b/Tests/LibWeb/Text/expected/geometry/dommatrix-to-array.txt new file mode 100644 index 00000000000..4a9f0186b4e --- /dev/null +++ b/Tests/LibWeb/Text/expected/geometry/dommatrix-to-array.txt @@ -0,0 +1,4 @@ +1. {"0":10,"1":20,"2":0,"3":0,"4":30,"5":40,"6":0,"7":0,"8":0,"9":0,"10":1,"11":0,"12":50,"13":60,"14":0,"15":1} +2. {"0":10,"1":20,"2":30,"3":40,"4":50,"5":60,"6":70,"7":80,"8":90,"9":100,"10":110,"11":120,"12":130,"13":140,"14":150,"15":160} +3. {"0":10,"1":20,"2":0,"3":0,"4":30,"5":40,"6":0,"7":0,"8":0,"9":0,"10":1,"11":0,"12":50,"13":60,"14":0,"15":1} +4. {"0":10,"1":20,"2":30,"3":40,"4":50,"5":60,"6":70,"7":80,"8":90,"9":100,"10":110,"11":120,"12":130,"13":140,"14":150,"15":160} diff --git a/Tests/LibWeb/Text/input/geometry/dommatrix-to-array.html b/Tests/LibWeb/Text/input/geometry/dommatrix-to-array.html new file mode 100644 index 00000000000..8c17d634eb4 --- /dev/null +++ b/Tests/LibWeb/Text/input/geometry/dommatrix-to-array.html @@ -0,0 +1,21 @@ + + diff --git a/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp b/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp index 82df8aff535..70cc3d0a244 100644 --- a/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp +++ b/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp @@ -5,6 +5,8 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include +#include #include #include #include @@ -453,6 +455,32 @@ JS::NonnullGCPtr DOMMatrixReadOnly::transform_point(DOMPointReadOnly c return DOMPoint::construct_impl(realm(), point_vector.x(), point_vector.y(), point_vector.z(), point_vector.w()); } +// https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-tofloat32array +JS::NonnullGCPtr DOMMatrixReadOnly::to_float32_array() const +{ + // Returns the serialized 16 elements m11 to m44 of the current matrix in column-major order as Float32Array. + float elements[16] = { static_cast(m11()), static_cast(m12()), static_cast(m13()), static_cast(m14()), + static_cast(m21()), static_cast(m22()), static_cast(m23()), static_cast(m24()), + static_cast(m31()), static_cast(m32()), static_cast(m33()), static_cast(m34()), + static_cast(m41()), static_cast(m42()), static_cast(m43()), static_cast(m44()) }; + auto bytes = MUST(ByteBuffer::copy(elements, sizeof(elements))); + auto array_buffer = JS::ArrayBuffer::create(realm(), move(bytes)); + return JS::Float32Array::create(realm(), sizeof(elements) / sizeof(float), array_buffer); +} + +// https://drafts.fxtf.org/geometry/#dom-dommatrixreadonly-tofloat64array +JS::NonnullGCPtr DOMMatrixReadOnly::to_float64_array() const +{ + // Returns the serialized 16 elements m11 to m44 of the current matrix in column-major order as Float64Array. + double elements[16] = { m11(), m12(), m13(), m14(), + m21(), m22(), m23(), m24(), + m31(), m32(), m33(), m34(), + m41(), m42(), m43(), m44() }; + auto bytes = MUST(ByteBuffer::copy(elements, sizeof(elements))); + auto array_buffer = JS::ArrayBuffer::create(realm(), move(bytes)); + return JS::Float64Array::create(realm(), sizeof(elements) / sizeof(double), array_buffer); +} + // https://drafts.fxtf.org/geometry/#dommatrixreadonly-stringification-behavior WebIDL::ExceptionOr DOMMatrixReadOnly::to_string() const { diff --git a/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.h b/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.h index cf93290898e..a0239a93e6b 100644 --- a/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.h +++ b/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.h @@ -101,6 +101,8 @@ public: JS::NonnullGCPtr transform_point(DOMPointInit const&) const; JS::NonnullGCPtr transform_point(DOMPointReadOnly const&) const; + JS::NonnullGCPtr to_float32_array() const; + JS::NonnullGCPtr to_float64_array() const; WebIDL::ExceptionOr to_string() const; diff --git a/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.idl b/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.idl index d56ecbf5bd4..212ae3c6baf 100644 --- a/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.idl +++ b/Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.idl @@ -54,8 +54,8 @@ interface DOMMatrixReadOnly { [NewObject] DOMMatrix inverse(); [NewObject] DOMPoint transformPoint(optional DOMPointInit point = {}); - // FIXME: [NewObject] Float32Array toFloat32Array(); - // FIXME: [NewObject] Float64Array toFloat64Array(); + [NewObject] Float32Array toFloat32Array(); + [NewObject] Float64Array toFloat64Array(); [Exposed=Window] stringifier; [Default] object toJSON();