DOMPointReadOnly.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2024, Kenneth Myhra <kennethmyhra@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Geometry/DOMMatrix.h>
  10. #include <LibWeb/Geometry/DOMPointReadOnly.h>
  11. #include <LibWeb/HTML/StructuredSerialize.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. namespace Web::Geometry {
  14. JS_DEFINE_ALLOCATOR(DOMPointReadOnly);
  15. JS::NonnullGCPtr<DOMPointReadOnly> DOMPointReadOnly::construct_impl(JS::Realm& realm, double x, double y, double z, double w)
  16. {
  17. return realm.heap().allocate<DOMPointReadOnly>(realm, realm, x, y, z, w);
  18. }
  19. JS::NonnullGCPtr<DOMPointReadOnly> DOMPointReadOnly::create(JS::Realm& realm)
  20. {
  21. return realm.heap().allocate<DOMPointReadOnly>(realm, realm);
  22. }
  23. DOMPointReadOnly::DOMPointReadOnly(JS::Realm& realm, double x, double y, double z, double w)
  24. : PlatformObject(realm)
  25. , m_x(x)
  26. , m_y(y)
  27. , m_z(z)
  28. , m_w(w)
  29. {
  30. }
  31. DOMPointReadOnly::DOMPointReadOnly(JS::Realm& realm)
  32. : PlatformObject(realm)
  33. {
  34. }
  35. // https://drafts.fxtf.org/geometry/#dom-dompointreadonly-frompoint
  36. JS::NonnullGCPtr<DOMPointReadOnly> DOMPointReadOnly::from_point(JS::VM& vm, DOMPointInit const& other)
  37. {
  38. // The fromPoint(other) static method on DOMPointReadOnly must create a DOMPointReadOnly from the dictionary other.
  39. return construct_impl(*vm.current_realm(), other.x, other.y, other.z, other.w);
  40. }
  41. DOMPointReadOnly::~DOMPointReadOnly() = default;
  42. // https://drafts.fxtf.org/geometry/#dom-dompointreadonly-matrixtransform
  43. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMPoint>> DOMPointReadOnly::matrix_transform(DOMMatrixInit& matrix) const
  44. {
  45. // 1. Let matrixObject be the result of invoking create a DOMMatrix from the dictionary matrix.
  46. auto matrix_object = TRY(DOMMatrix::create_from_dom_matrix_init(realm(), matrix));
  47. // 2. Return the result of invoking transform a point with a matrix, given the current point and matrixObject. The current point does not get modified.
  48. return matrix_object->transform_point(*this);
  49. }
  50. void DOMPointReadOnly::initialize(JS::Realm& realm)
  51. {
  52. Base::initialize(realm);
  53. WEB_SET_PROTOTYPE_FOR_INTERFACE(DOMPointReadOnly);
  54. }
  55. WebIDL::ExceptionOr<void> DOMPointReadOnly::serialization_steps(HTML::SerializationRecord& serialized, bool, HTML::SerializationMemory&)
  56. {
  57. // 1. Set serialized.[[X]] to value’s x coordinate.
  58. HTML::serialize_primitive_type(serialized, m_x);
  59. // 2. Set serialized.[[Y]] to value’s y coordinate.
  60. HTML::serialize_primitive_type(serialized, m_y);
  61. // 3. Set serialized.[[Z]] to value’s z coordinate.
  62. HTML::serialize_primitive_type(serialized, m_z);
  63. // 4. Set serialized.[[W]] to value’s w coordinate.
  64. HTML::serialize_primitive_type(serialized, m_w);
  65. return {};
  66. }
  67. WebIDL::ExceptionOr<void> DOMPointReadOnly::deserialization_steps(ReadonlySpan<u32> const& serialized, size_t& position, HTML::DeserializationMemory&)
  68. {
  69. // 1. Set value’s x coordinate to serialized.[[X]].
  70. m_x = HTML::deserialize_primitive_type<double>(serialized, position);
  71. // 2. Set value’s y coordinate to serialized.[[Y]].
  72. m_y = HTML::deserialize_primitive_type<double>(serialized, position);
  73. // 3. Set value’s z coordinate to serialized.[[Z]].
  74. m_z = HTML::deserialize_primitive_type<double>(serialized, position);
  75. // 4. Set value’s w coordinate to serialized.[[W]].
  76. m_w = HTML::deserialize_primitive_type<double>(serialized, position);
  77. return {};
  78. }
  79. }