DOMQuad.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Geometry/DOMQuad.h>
  8. namespace Web::Geometry {
  9. JS_DEFINE_ALLOCATOR(DOMQuad);
  10. JS::NonnullGCPtr<DOMQuad> DOMQuad::construct_impl(JS::Realm& realm, DOMPointInit const& p1, DOMPointInit const& p2, DOMPointInit const& p3, DOMPointInit const& p4)
  11. {
  12. return realm.heap().allocate<DOMQuad>(realm, realm, p1, p2, p3, p4);
  13. }
  14. JS::NonnullGCPtr<DOMQuad> DOMQuad::create(JS::Realm& realm)
  15. {
  16. return realm.heap().allocate<DOMQuad>(realm, realm);
  17. }
  18. DOMQuad::DOMQuad(JS::Realm& realm, DOMPointInit const& p1, DOMPointInit const& p2, DOMPointInit const& p3, DOMPointInit const& p4)
  19. : PlatformObject(realm)
  20. , m_p1(DOMPoint::from_point(realm.vm(), p1))
  21. , m_p2(DOMPoint::from_point(realm.vm(), p2))
  22. , m_p3(DOMPoint::from_point(realm.vm(), p3))
  23. , m_p4(DOMPoint::from_point(realm.vm(), p4))
  24. {
  25. }
  26. DOMQuad::DOMQuad(JS::Realm& realm)
  27. : PlatformObject(realm)
  28. , m_p1(DOMPoint::create(realm))
  29. , m_p2(DOMPoint::create(realm))
  30. , m_p3(DOMPoint::create(realm))
  31. , m_p4(DOMPoint::create(realm))
  32. {
  33. }
  34. DOMQuad::~DOMQuad() = default;
  35. // https://drafts.fxtf.org/geometry/#dom-domquad-fromrect
  36. JS::NonnullGCPtr<DOMQuad> DOMQuad::from_rect(JS::VM& vm, DOMRectInit const& other)
  37. {
  38. // The fromRect(other) static method on DOMQuad must create a DOMQuad from the DOMRectInit dictionary other.
  39. return construct_impl(*vm.current_realm(), { other.x, other.y },
  40. { other.x + other.width, other.y },
  41. { other.x + other.width, other.y + other.height },
  42. { other.x, other.y + other.height });
  43. }
  44. // https://drafts.fxtf.org/geometry/#dom-domquad-fromquad
  45. JS::NonnullGCPtr<DOMQuad> DOMQuad::from_quad(JS::VM& vm, DOMQuadInit const& other)
  46. {
  47. // The fromQuad(other) static method on DOMQuad must create a DOMQuad from the DOMQuadInit dictionary other.
  48. return construct_impl(*vm.current_realm(), other.p1, other.p2, other.p3, other.p4);
  49. }
  50. // https://drafts.fxtf.org/geometry/#dom-domquad-getbounds
  51. JS::NonnullGCPtr<DOMRect> DOMQuad::get_bounds() const
  52. {
  53. auto nan_safe_minimum = [](double a, double b, double c, double d) -> double {
  54. if (isnan(a) || isnan(b) || isnan(c) || isnan(d))
  55. return NAN;
  56. return min(a, min(b, min(c, d)));
  57. };
  58. auto nan_safe_maximum = [](double a, double b, double c, double d) -> double {
  59. if (isnan(a) || isnan(b) || isnan(c) || isnan(d))
  60. return NAN;
  61. return max(a, max(b, max(c, d)));
  62. };
  63. // 1. Let bounds be a DOMRect object.
  64. auto bounds = DOMRect::create(realm(), {});
  65. // 2. Let left be the NaN-safe minimum of point 1’s x coordinate, point 2’s x coordinate, point 3’s x coordinate and point 4’s x coordinate.
  66. auto left = nan_safe_minimum(m_p1->x(), m_p2->x(), m_p3->x(), m_p4->x());
  67. // 3. Let top be the NaN-safe minimum of point 1’s y coordinate, point 2’s y coordinate, point 3’s y coordinate and point 4’s y coordinate.
  68. auto top = nan_safe_minimum(m_p1->y(), m_p2->y(), m_p3->y(), m_p4->y());
  69. // 4. Let right be the NaN-safe maximum of point 1’s x coordinate, point 2’s x coordinate, point 3’s x coordinate and point 4’s x coordinate.
  70. auto right = nan_safe_maximum(m_p1->x(), m_p2->x(), m_p3->x(), m_p4->x());
  71. // 5. Let bottom be the NaN-safe maximum of point 1’s y coordinate, point 2’s y coordinate, point 3’s y coordinate and point 4’s y coordinate.
  72. auto bottom = nan_safe_maximum(m_p1->y(), m_p2->y(), m_p3->y(), m_p4->y());
  73. // 6. Set x coordinate of bounds to left, y coordinate of bounds to top, width dimension of bounds to right - left and height dimension of bounds to bottom - top.
  74. bounds->set_x(left);
  75. bounds->set_y(top);
  76. bounds->set_width(right - left);
  77. bounds->set_height(bottom - top);
  78. // 7. Return bounds.
  79. return bounds;
  80. }
  81. // https://drafts.fxtf.org/geometry/#structured-serialization
  82. WebIDL::ExceptionOr<void> DOMQuad::serialization_steps(HTML::SerializationRecord& serialzied, bool for_storage, HTML::SerializationMemory& memory)
  83. {
  84. auto& vm = this->vm();
  85. // 1. Set serialized.[[P1]] to the sub-serialization of value’s point 1.
  86. serialzied.extend(TRY(HTML::structured_serialize_internal(vm, m_p1, for_storage, memory)));
  87. // 2. Set serialized.[[P2]] to the sub-serialization of value’s point 2.
  88. serialzied.extend(TRY(HTML::structured_serialize_internal(vm, m_p2, for_storage, memory)));
  89. // 3. Set serialized.[[P3]] to the sub-serialization of value’s point 3.
  90. serialzied.extend(TRY(HTML::structured_serialize_internal(vm, m_p3, for_storage, memory)));
  91. // 4. Set serialized.[[P4]] to the sub-serialization of value’s point 4.
  92. serialzied.extend(TRY(HTML::structured_serialize_internal(vm, m_p4, for_storage, memory)));
  93. return {};
  94. }
  95. // https://drafts.fxtf.org/geometry/#structured-serialization
  96. WebIDL::ExceptionOr<void> DOMQuad::deserialization_steps(ReadonlySpan<u32> const& serialized, size_t& position, HTML::DeserializationMemory& memory)
  97. {
  98. auto& realm = this->realm();
  99. // 1. Set value’s point 1 to the sub-deserialization of serialized.[[P1]].
  100. auto deserialized_record = TRY(HTML::structured_deserialize_internal(vm(), serialized, realm, memory, position));
  101. if (deserialized_record.value.has_value() && is<DOMPoint>(deserialized_record.value.value().as_object()))
  102. m_p1 = dynamic_cast<DOMPoint&>(deserialized_record.value.release_value().as_object());
  103. position = deserialized_record.position;
  104. // 2. Set value’s point 2 to the sub-deserialization of serialized.[[P2]].
  105. deserialized_record = TRY(HTML::structured_deserialize_internal(vm(), serialized, realm, memory, position));
  106. if (deserialized_record.value.has_value() && is<DOMPoint>(deserialized_record.value.value().as_object()))
  107. m_p2 = dynamic_cast<DOMPoint&>(deserialized_record.value.release_value().as_object());
  108. position = deserialized_record.position;
  109. // 3. Set value’s point 3 to the sub-deserialization of serialized.[[P3]].
  110. deserialized_record = TRY(HTML::structured_deserialize_internal(vm(), serialized, realm, memory, position));
  111. if (deserialized_record.value.has_value() && is<DOMPoint>(deserialized_record.value.value().as_object()))
  112. m_p3 = dynamic_cast<DOMPoint&>(deserialized_record.value.release_value().as_object());
  113. position = deserialized_record.position;
  114. // 4. Set value’s point 4 to the sub-deserialization of serialized.[[P4]].
  115. deserialized_record = TRY(HTML::structured_deserialize_internal(vm(), serialized, realm, memory, position));
  116. if (deserialized_record.value.has_value() && is<DOMPoint>(deserialized_record.value.value().as_object()))
  117. m_p4 = dynamic_cast<DOMPoint&>(deserialized_record.value.release_value().as_object());
  118. position = deserialized_record.position;
  119. return {};
  120. }
  121. void DOMQuad::initialize(JS::Realm& realm)
  122. {
  123. Base::initialize(realm);
  124. WEB_SET_PROTOTYPE_FOR_INTERFACE(DOMQuad);
  125. }
  126. void DOMQuad::visit_edges(Cell::Visitor& visitor)
  127. {
  128. Base::visit_edges(visitor);
  129. visitor.visit(m_p1);
  130. visitor.visit(m_p2);
  131. visitor.visit(m_p3);
  132. visitor.visit(m_p4);
  133. }
  134. }