Box.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/OwnPtr.h>
  8. #include <LibGfx/Rect.h>
  9. #include <LibWeb/Layout/LineBox.h>
  10. #include <LibWeb/Layout/Node.h>
  11. #include <LibWeb/Painting/BorderPainting.h>
  12. #include <LibWeb/Painting/StackingContext.h>
  13. namespace Web::Layout {
  14. class Box : public NodeWithStyleAndBoxModelMetrics {
  15. public:
  16. const Gfx::FloatRect absolute_rect() const;
  17. Gfx::FloatPoint effective_offset() const;
  18. void set_offset(const Gfx::FloatPoint& offset);
  19. void set_offset(float x, float y) { set_offset({ x, y }); }
  20. const Gfx::FloatSize& size() const { return m_size; }
  21. void set_size(const Gfx::FloatSize&);
  22. void set_size(float width, float height) { set_size({ width, height }); }
  23. void set_width(float width) { set_size(width, height()); }
  24. void set_height(float height) { set_size(width(), height); }
  25. float width() const { return m_size.width(); }
  26. float height() const { return m_size.height(); }
  27. Gfx::FloatRect padded_rect() const
  28. {
  29. auto absolute_rect = this->absolute_rect();
  30. Gfx::FloatRect rect;
  31. rect.set_x(absolute_rect.x() - box_model().padding.left);
  32. rect.set_width(width() + box_model().padding.left + box_model().padding.right);
  33. rect.set_y(absolute_rect.y() - box_model().padding.top);
  34. rect.set_height(height() + box_model().padding.top + box_model().padding.bottom);
  35. return rect;
  36. }
  37. Gfx::FloatRect bordered_rect() const
  38. {
  39. auto padded_rect = this->padded_rect();
  40. Gfx::FloatRect rect;
  41. rect.set_x(padded_rect.x() - box_model().border.left);
  42. rect.set_width(padded_rect.width() + box_model().border.left + box_model().border.right);
  43. rect.set_y(padded_rect.y() - box_model().border.top);
  44. rect.set_height(padded_rect.height() + box_model().border.top + box_model().border.bottom);
  45. return rect;
  46. }
  47. float margin_box_width() const
  48. {
  49. auto margin_box = box_model().margin_box();
  50. return width() + margin_box.left + margin_box.right;
  51. }
  52. float margin_box_height() const
  53. {
  54. auto margin_box = box_model().margin_box();
  55. return height() + margin_box.top + margin_box.bottom;
  56. }
  57. float border_box_width() const
  58. {
  59. auto border_box = box_model().border_box();
  60. return width() + border_box.left + border_box.right;
  61. }
  62. float border_box_height() const
  63. {
  64. auto border_box = box_model().border_box();
  65. return height() + border_box.top + border_box.bottom;
  66. }
  67. Gfx::FloatRect content_box_as_relative_rect() const
  68. {
  69. return { m_offset, m_size };
  70. }
  71. Gfx::FloatRect margin_box_as_relative_rect() const
  72. {
  73. auto rect = content_box_as_relative_rect();
  74. auto margin_box = box_model().margin_box();
  75. rect.set_x(rect.x() - margin_box.left);
  76. rect.set_width(rect.width() + margin_box.left + margin_box.right);
  77. rect.set_y(rect.y() - margin_box.top);
  78. rect.set_height(rect.height() + margin_box.top + margin_box.bottom);
  79. return rect;
  80. }
  81. float absolute_x() const { return absolute_rect().x(); }
  82. float absolute_y() const { return absolute_rect().y(); }
  83. Gfx::FloatPoint absolute_position() const { return absolute_rect().location(); }
  84. bool is_out_of_flow(FormattingContext const&) const;
  85. virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const override;
  86. virtual void set_needs_display() override;
  87. bool is_body() const;
  88. void set_containing_line_box_fragment(LineBoxFragment&);
  89. StackingContext* stacking_context() { return m_stacking_context; }
  90. const StackingContext* stacking_context() const { return m_stacking_context; }
  91. void set_stacking_context(NonnullOwnPtr<StackingContext> context) { m_stacking_context = move(context); }
  92. StackingContext* enclosing_stacking_context();
  93. virtual void paint(PaintContext&, PaintPhase) override;
  94. virtual void paint_border(PaintContext& context);
  95. virtual void paint_box_shadow(PaintContext& context);
  96. virtual void paint_background(PaintContext& context);
  97. Vector<LineBox>& line_boxes() { return m_line_boxes; }
  98. const Vector<LineBox>& line_boxes() const { return m_line_boxes; }
  99. LineBox& ensure_last_line_box();
  100. LineBox& add_line_box();
  101. virtual float width_of_logical_containing_block() const;
  102. Painting::BorderRadiusData normalized_border_radius_data();
  103. protected:
  104. Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  105. : NodeWithStyleAndBoxModelMetrics(document, node, move(style))
  106. {
  107. }
  108. Box(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  109. : NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
  110. {
  111. }
  112. virtual void did_set_rect() { }
  113. Vector<LineBox> m_line_boxes;
  114. private:
  115. virtual bool is_box() const final { return true; }
  116. Gfx::FloatPoint m_offset;
  117. Gfx::FloatSize m_size;
  118. // Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
  119. WeakPtr<LineBoxFragment> m_containing_line_box_fragment;
  120. OwnPtr<StackingContext> m_stacking_context;
  121. };
  122. template<>
  123. inline bool Node::fast_is<Box>() const { return is_box(); }
  124. }