Box.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/Node.h>
  10. #include <LibWeb/Painting/BorderPainting.h>
  11. #include <LibWeb/Painting/StackingContext.h>
  12. namespace Web::Layout {
  13. class Box : public NodeWithStyleAndBoxModelMetrics {
  14. public:
  15. struct OverflowData {
  16. Gfx::FloatRect scrollable_overflow_rect;
  17. Gfx::FloatPoint scroll_offset;
  18. };
  19. const Gfx::FloatRect absolute_rect() const;
  20. Gfx::FloatPoint effective_offset() const;
  21. void set_offset(const Gfx::FloatPoint& offset);
  22. void set_offset(float x, float y) { set_offset({ x, y }); }
  23. const Gfx::FloatSize& size() const { return m_size; }
  24. void set_size(const Gfx::FloatSize&);
  25. void set_size(float width, float height) { set_size({ width, height }); }
  26. void set_width(float width) { set_size(width, height()); }
  27. void set_height(float height) { set_size(width(), height); }
  28. float width() const { return m_size.width(); }
  29. float height() const { return m_size.height(); }
  30. Gfx::FloatRect padded_rect() const
  31. {
  32. auto absolute_rect = this->absolute_rect();
  33. Gfx::FloatRect rect;
  34. rect.set_x(absolute_rect.x() - box_model().padding.left);
  35. rect.set_width(width() + box_model().padding.left + box_model().padding.right);
  36. rect.set_y(absolute_rect.y() - box_model().padding.top);
  37. rect.set_height(height() + box_model().padding.top + box_model().padding.bottom);
  38. return rect;
  39. }
  40. Gfx::FloatRect bordered_rect() const
  41. {
  42. auto padded_rect = this->padded_rect();
  43. Gfx::FloatRect rect;
  44. rect.set_x(padded_rect.x() - box_model().border.left);
  45. rect.set_width(padded_rect.width() + box_model().border.left + box_model().border.right);
  46. rect.set_y(padded_rect.y() - box_model().border.top);
  47. rect.set_height(padded_rect.height() + box_model().border.top + box_model().border.bottom);
  48. return rect;
  49. }
  50. float margin_box_width() const
  51. {
  52. auto margin_box = box_model().margin_box();
  53. return width() + margin_box.left + margin_box.right;
  54. }
  55. float margin_box_height() const
  56. {
  57. auto margin_box = box_model().margin_box();
  58. return height() + margin_box.top + margin_box.bottom;
  59. }
  60. float border_box_width() const
  61. {
  62. auto border_box = box_model().border_box();
  63. return width() + border_box.left + border_box.right;
  64. }
  65. float border_box_height() const
  66. {
  67. auto border_box = box_model().border_box();
  68. return height() + border_box.top + border_box.bottom;
  69. }
  70. Gfx::FloatRect content_box_as_relative_rect() const
  71. {
  72. return { m_offset, m_size };
  73. }
  74. Gfx::FloatRect border_box_as_relative_rect() const
  75. {
  76. auto rect = content_box_as_relative_rect();
  77. auto border_box = box_model().border_box();
  78. rect.set_x(rect.x() - border_box.left);
  79. rect.set_width(rect.width() + border_box.left + border_box.right);
  80. rect.set_y(rect.y() - border_box.top);
  81. rect.set_height(rect.height() + border_box.top + border_box.bottom);
  82. return rect;
  83. }
  84. Gfx::FloatRect margin_box_as_relative_rect() const
  85. {
  86. auto rect = content_box_as_relative_rect();
  87. auto margin_box = box_model().margin_box();
  88. rect.set_x(rect.x() - margin_box.left);
  89. rect.set_width(rect.width() + margin_box.left + margin_box.right);
  90. rect.set_y(rect.y() - margin_box.top);
  91. rect.set_height(rect.height() + margin_box.top + margin_box.bottom);
  92. return rect;
  93. }
  94. float absolute_x() const { return absolute_rect().x(); }
  95. float absolute_y() const { return absolute_rect().y(); }
  96. Gfx::FloatPoint absolute_position() const { return absolute_rect().location(); }
  97. bool is_out_of_flow(FormattingContext const&) const;
  98. virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const override;
  99. virtual void set_needs_display() override;
  100. bool is_body() const;
  101. void set_containing_line_box_fragment(LineBoxFragment&);
  102. StackingContext* stacking_context() { return m_stacking_context; }
  103. const StackingContext* stacking_context() const { return m_stacking_context; }
  104. void set_stacking_context(NonnullOwnPtr<StackingContext> context) { m_stacking_context = move(context); }
  105. StackingContext* enclosing_stacking_context();
  106. virtual void paint(PaintContext&, PaintPhase) override;
  107. virtual void paint_border(PaintContext& context);
  108. virtual void paint_box_shadow(PaintContext& context);
  109. virtual void paint_background(PaintContext& context);
  110. Painting::BorderRadiusData normalized_border_radius_data();
  111. virtual Optional<float> intrinsic_width() const { return {}; }
  112. virtual Optional<float> intrinsic_height() const { return {}; }
  113. virtual Optional<float> intrinsic_aspect_ratio() const { return {}; }
  114. bool has_intrinsic_width() const { return intrinsic_width().has_value(); }
  115. bool has_intrinsic_height() const { return intrinsic_height().has_value(); }
  116. bool has_intrinsic_aspect_ratio() const { return intrinsic_aspect_ratio().has_value(); }
  117. bool has_overflow() const { return m_overflow_data; }
  118. Optional<Gfx::FloatRect> scrollable_overflow_rect() const
  119. {
  120. if (!m_overflow_data)
  121. return {};
  122. return m_overflow_data->scrollable_overflow_rect;
  123. }
  124. OverflowData& ensure_overflow_data()
  125. {
  126. if (!m_overflow_data)
  127. m_overflow_data = make<OverflowData>();
  128. return *m_overflow_data;
  129. }
  130. void clear_overflow_data() { m_overflow_data = nullptr; }
  131. virtual void before_children_paint(PaintContext&, PaintPhase) override;
  132. virtual void after_children_paint(PaintContext&, PaintPhase) override;
  133. protected:
  134. Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  135. : NodeWithStyleAndBoxModelMetrics(document, node, move(style))
  136. {
  137. }
  138. Box(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  139. : NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
  140. {
  141. }
  142. virtual void did_set_rect() { }
  143. private:
  144. virtual bool is_box() const final { return true; }
  145. Gfx::FloatPoint m_offset;
  146. Gfx::FloatSize m_size;
  147. // Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
  148. WeakPtr<LineBoxFragment> m_containing_line_box_fragment;
  149. OwnPtr<StackingContext> m_stacking_context;
  150. OwnPtr<OverflowData> m_overflow_data;
  151. };
  152. template<>
  153. inline bool Node::fast_is<Box>() const { return is_box(); }
  154. }