Browse Source

LibWeb: Add DOMMatrix toFloat32Array and toFloat64Array

Bastiaan van der Plaat 1 year ago
parent
commit
2e122b16e4

+ 2 - 0
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.
     // might simply need to add another type here.
     static constexpr Array types = {
     static constexpr Array types = {
         "ArrayBuffer"sv,
         "ArrayBuffer"sv,
+        "Float32Array"sv,
+        "Float64Array"sv,
     };
     };
 
 
     return types.span().contains_slow(type.name());
     return types.span().contains_slow(type.name());

+ 4 - 0
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}

+ 21 - 0
Tests/LibWeb/Text/input/geometry/dommatrix-to-array.html

@@ -0,0 +1,21 @@
+<script src="../include.js"></script>
+<script>
+    test(() => {
+        let testCounter = 1;
+        function testPart(part) {
+            println(`${testCounter++}. ${JSON.stringify(part())}`);
+        }
+
+        // 1. DOMMatrix to Float32Array
+        testPart(() => new DOMMatrix([10, 20, 30, 40, 50, 60]).toFloat32Array());
+
+        // 2. DOMMatrix to Float32Array
+        testPart(() => new DOMMatrix([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]).toFloat32Array());
+
+        // 3. DOMMatrix to Float64Array
+        testPart(() => new DOMMatrix([10, 20, 30, 40, 50, 60]).toFloat64Array());
+
+        // 4. DOMMatrix to Float64Array
+        testPart(() => new DOMMatrix([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]).toFloat64Array());
+    });
+</script>

+ 28 - 0
Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp

@@ -5,6 +5,8 @@
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
  */
  */
 
 
+#include <LibJS/Runtime/ArrayBuffer.h>
+#include <LibJS/Runtime/TypedArray.h>
 #include <LibWeb/Bindings/Intrinsics.h>
 #include <LibWeb/Bindings/Intrinsics.h>
 #include <LibWeb/Geometry/DOMMatrix.h>
 #include <LibWeb/Geometry/DOMMatrix.h>
 #include <LibWeb/Geometry/DOMMatrixReadOnly.h>
 #include <LibWeb/Geometry/DOMMatrixReadOnly.h>
@@ -453,6 +455,32 @@ JS::NonnullGCPtr<DOMPoint> DOMMatrixReadOnly::transform_point(DOMPointReadOnly c
     return DOMPoint::construct_impl(realm(), point_vector.x(), point_vector.y(), point_vector.z(), point_vector.w());
     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<JS::Float32Array> 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<float>(m11()), static_cast<float>(m12()), static_cast<float>(m13()), static_cast<float>(m14()),
+        static_cast<float>(m21()), static_cast<float>(m22()), static_cast<float>(m23()), static_cast<float>(m24()),
+        static_cast<float>(m31()), static_cast<float>(m32()), static_cast<float>(m33()), static_cast<float>(m34()),
+        static_cast<float>(m41()), static_cast<float>(m42()), static_cast<float>(m43()), static_cast<float>(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<JS::Float64Array> 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
 // https://drafts.fxtf.org/geometry/#dommatrixreadonly-stringification-behavior
 WebIDL::ExceptionOr<String> DOMMatrixReadOnly::to_string() const
 WebIDL::ExceptionOr<String> DOMMatrixReadOnly::to_string() const
 {
 {

+ 2 - 0
Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.h

@@ -101,6 +101,8 @@ public:
 
 
     JS::NonnullGCPtr<DOMPoint> transform_point(DOMPointInit const&) const;
     JS::NonnullGCPtr<DOMPoint> transform_point(DOMPointInit const&) const;
     JS::NonnullGCPtr<DOMPoint> transform_point(DOMPointReadOnly const&) const;
     JS::NonnullGCPtr<DOMPoint> transform_point(DOMPointReadOnly const&) const;
+    JS::NonnullGCPtr<JS::Float32Array> to_float32_array() const;
+    JS::NonnullGCPtr<JS::Float64Array> to_float64_array() const;
 
 
     WebIDL::ExceptionOr<String> to_string() const;
     WebIDL::ExceptionOr<String> to_string() const;
 
 

+ 2 - 2
Userland/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.idl

@@ -54,8 +54,8 @@ interface DOMMatrixReadOnly {
     [NewObject] DOMMatrix inverse();
     [NewObject] DOMMatrix inverse();
 
 
     [NewObject] DOMPoint transformPoint(optional DOMPointInit point = {});
     [NewObject] DOMPoint transformPoint(optional DOMPointInit point = {});
-    // FIXME: [NewObject] Float32Array toFloat32Array();
-    // FIXME: [NewObject] Float64Array toFloat64Array();
+    [NewObject] Float32Array toFloat32Array();
+    [NewObject] Float64Array toFloat64Array();
 
 
     [Exposed=Window] stringifier;
     [Exposed=Window] stringifier;
     [Default] object toJSON();
     [Default] object toJSON();