DOMPointReadOnly.cpp 3.5 KB

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