Box.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/OwnPtr.h>
  28. #include <LibGfx/Rect.h>
  29. #include <LibWeb/Layout/LineBox.h>
  30. #include <LibWeb/Layout/Node.h>
  31. #include <LibWeb/Painting/StackingContext.h>
  32. namespace Web::Layout {
  33. class Box : public NodeWithStyleAndBoxModelMetrics {
  34. public:
  35. const Gfx::FloatRect absolute_rect() const;
  36. Gfx::FloatPoint effective_offset() const;
  37. void set_offset(const Gfx::FloatPoint& offset);
  38. void set_offset(float x, float y) { set_offset({ x, y }); }
  39. const Gfx::FloatSize& size() const { return m_size; }
  40. void set_size(const Gfx::FloatSize&);
  41. void set_size(float width, float height) { set_size({ width, height }); }
  42. void set_width(float width) { set_size(width, height()); }
  43. void set_height(float height) { set_size(width(), height); }
  44. float width() const { return m_size.width(); }
  45. float height() const { return m_size.height(); }
  46. Gfx::FloatRect padded_rect() const
  47. {
  48. Gfx::FloatRect rect;
  49. rect.set_x(absolute_x() - box_model().padding.left);
  50. rect.set_width(width() + box_model().padding.left + box_model().padding.right);
  51. rect.set_y(absolute_y() - box_model().padding.top);
  52. rect.set_height(height() + box_model().padding.top + box_model().padding.bottom);
  53. return rect;
  54. }
  55. Gfx::FloatRect bordered_rect() const
  56. {
  57. auto padded_rect = this->padded_rect();
  58. Gfx::FloatRect rect;
  59. rect.set_x(padded_rect.x() - box_model().border.left);
  60. rect.set_width(padded_rect.width() + box_model().border.left + box_model().border.right);
  61. rect.set_y(padded_rect.y() - box_model().border.top);
  62. rect.set_height(padded_rect.height() + box_model().border.top + box_model().border.bottom);
  63. return rect;
  64. }
  65. float margin_box_width() const
  66. {
  67. auto margin_box = box_model().margin_box();
  68. return width() + margin_box.left + margin_box.right;
  69. }
  70. float margin_box_height() const
  71. {
  72. auto margin_box = box_model().margin_box();
  73. return height() + margin_box.top + margin_box.bottom;
  74. }
  75. float border_box_width() const
  76. {
  77. auto border_box = box_model().border_box();
  78. return width() + border_box.left + border_box.right;
  79. }
  80. float border_box_height() const
  81. {
  82. auto border_box = box_model().border_box();
  83. return height() + border_box.top + border_box.bottom;
  84. }
  85. Gfx::FloatRect content_box_as_relative_rect() const
  86. {
  87. return { m_offset, m_size };
  88. }
  89. Gfx::FloatRect margin_box_as_relative_rect() const
  90. {
  91. auto rect = content_box_as_relative_rect();
  92. auto margin_box = box_model().margin_box();
  93. rect.set_x(rect.x() - margin_box.left);
  94. rect.set_width(rect.width() + margin_box.left + margin_box.right);
  95. rect.set_y(rect.y() - margin_box.top);
  96. rect.set_height(rect.height() + margin_box.top + margin_box.bottom);
  97. return rect;
  98. }
  99. float absolute_x() const { return absolute_rect().x(); }
  100. float absolute_y() const { return absolute_rect().y(); }
  101. Gfx::FloatPoint absolute_position() const { return absolute_rect().location(); }
  102. virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const override;
  103. virtual void set_needs_display() override;
  104. bool is_body() const;
  105. void set_containing_line_box_fragment(LineBoxFragment&);
  106. bool establishes_stacking_context() const;
  107. StackingContext* stacking_context() { return m_stacking_context; }
  108. const StackingContext* stacking_context() const { return m_stacking_context; }
  109. void set_stacking_context(NonnullOwnPtr<StackingContext> context) { m_stacking_context = move(context); }
  110. StackingContext* enclosing_stacking_context();
  111. virtual void paint(PaintContext&, PaintPhase) override;
  112. Vector<LineBox>& line_boxes() { return m_line_boxes; }
  113. const Vector<LineBox>& line_boxes() const { return m_line_boxes; }
  114. LineBox& ensure_last_line_box();
  115. LineBox& add_line_box();
  116. virtual float width_of_logical_containing_block() const;
  117. protected:
  118. Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  119. : NodeWithStyleAndBoxModelMetrics(document, node, move(style))
  120. {
  121. }
  122. Box(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  123. : NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
  124. {
  125. }
  126. virtual void did_set_rect() { }
  127. void paint_background_image(PaintContext&, const Gfx::Bitmap&, CSS::Repeat, CSS::Repeat, Gfx::IntRect);
  128. Vector<LineBox> m_line_boxes;
  129. private:
  130. virtual bool is_box() const final { return true; }
  131. Gfx::FloatPoint m_offset;
  132. Gfx::FloatSize m_size;
  133. // Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
  134. WeakPtr<LineBoxFragment> m_containing_line_box_fragment;
  135. OwnPtr<StackingContext> m_stacking_context;
  136. };
  137. template<>
  138. inline bool Node::fast_is<Box>() const { return is_box(); }
  139. }