Box.h 5.4 KB

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