LibWeb: Add DOMMatrix toFloat32Array and toFloat64Array

This commit is contained in:
Bastiaan van der Plaat 2023-09-25 18:44:10 +02:00 committed by Andrew Kaster
parent 7b4b5b735b
commit 2e122b16e4
Notes: sideshowbarker 2024-07-17 02:14:39 +09:00
6 changed files with 59 additions and 2 deletions

View file

@ -5,6 +5,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Geometry/DOMMatrix.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());
}
// 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
WebIDL::ExceptionOr<String> DOMMatrixReadOnly::to_string() const
{