DOMQuad.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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::NonnullGCPtr<DOMQuad> DOMQuad::construct_impl(JS::Realm& realm, DOMPointInit const& p1, DOMPointInit const& p2, DOMPointInit const& p3, DOMPointInit const& p4)
  10. {
  11. return realm.heap().allocate<DOMQuad>(realm, realm, p1, p2, p3, p4);
  12. }
  13. DOMQuad::DOMQuad(JS::Realm& realm, DOMPointInit const& p1, DOMPointInit const& p2, DOMPointInit const& p3, DOMPointInit const& p4)
  14. : PlatformObject(realm)
  15. , m_p1(DOMPoint::from_point(realm.vm(), p1))
  16. , m_p2(DOMPoint::from_point(realm.vm(), p2))
  17. , m_p3(DOMPoint::from_point(realm.vm(), p3))
  18. , m_p4(DOMPoint::from_point(realm.vm(), p4))
  19. {
  20. }
  21. DOMQuad::~DOMQuad() = default;
  22. // https://drafts.fxtf.org/geometry/#dom-domquad-fromrect
  23. JS::NonnullGCPtr<DOMQuad> DOMQuad::from_rect(JS::VM& vm, DOMRectInit const& other)
  24. {
  25. // The fromRect(other) static method on DOMQuad must create a DOMQuad from the DOMRectInit dictionary other.
  26. return construct_impl(*vm.current_realm(), { other.x, other.y },
  27. { other.x + other.width, other.y },
  28. { other.x + other.width, other.y + other.height },
  29. { other.x, other.y + other.height });
  30. }
  31. // https://drafts.fxtf.org/geometry/#dom-domquad-fromquad
  32. JS::NonnullGCPtr<DOMQuad> DOMQuad::from_quad(JS::VM& vm, DOMQuadInit const& other)
  33. {
  34. // The fromQuad(other) static method on DOMQuad must create a DOMQuad from the DOMQuadInit dictionary other.
  35. return construct_impl(*vm.current_realm(), other.p1, other.p2, other.p3, other.p4);
  36. }
  37. // https://drafts.fxtf.org/geometry/#dom-domquad-getbounds
  38. JS::NonnullGCPtr<DOMRect> DOMQuad::get_bounds() const
  39. {
  40. // 1. Let bounds be a DOMRect object.
  41. auto bounds = DOMRect::create(realm(), {});
  42. // 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.
  43. auto left = min(m_p1->x(), min(m_p2->x(), min(m_p3->x(), m_p4->x())));
  44. // 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.
  45. auto top = min(m_p1->y(), min(m_p2->y(), min(m_p3->y(), m_p4->y())));
  46. // 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.
  47. auto right = max(m_p1->x(), max(m_p2->x(), max(m_p3->x(), m_p4->x())));
  48. // 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.
  49. auto bottom = max(m_p1->y(), max(m_p2->y(), max(m_p3->y(), m_p4->y())));
  50. // 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.
  51. bounds->set_x(left);
  52. bounds->set_y(top);
  53. bounds->set_width(right - left);
  54. bounds->set_height(bottom - top);
  55. // 7. Return bounds.
  56. return bounds;
  57. }
  58. void DOMQuad::initialize(JS::Realm& realm)
  59. {
  60. Base::initialize(realm);
  61. set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMQuadPrototype>(realm, "DOMQuad"));
  62. }
  63. void DOMQuad::visit_edges(Cell::Visitor& visitor)
  64. {
  65. Base::visit_edges(visitor);
  66. visitor.visit(m_p1.ptr());
  67. visitor.visit(m_p2.ptr());
  68. visitor.visit(m_p3.ptr());
  69. visitor.visit(m_p4.ptr());
  70. }
  71. }