Box.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Painter.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/HTMLBodyElement.h>
  9. #include <LibWeb/Layout/BlockBox.h>
  10. #include <LibWeb/Layout/Box.h>
  11. #include <LibWeb/Page/Frame.h>
  12. #include <LibWeb/Painting/BorderPainting.h>
  13. namespace Web::Layout {
  14. void Box::paint(PaintContext& context, PaintPhase phase)
  15. {
  16. if (!is_visible())
  17. return;
  18. Gfx::PainterStateSaver saver(context.painter());
  19. if (is_fixed_position())
  20. context.painter().translate(context.scroll_offset());
  21. auto padded_rect = this->padded_rect();
  22. if (phase == PaintPhase::Background && !is_body()) {
  23. auto background_rect = enclosing_int_rect(padded_rect);
  24. context.painter().fill_rect(background_rect, computed_values().background_color());
  25. if (background_image() && background_image()->bitmap()) {
  26. paint_background_image(context, *background_image()->bitmap(), computed_values().background_repeat_x(), computed_values().background_repeat_y(), move(background_rect));
  27. }
  28. }
  29. if (phase == PaintPhase::Border) {
  30. auto bordered_rect = this->bordered_rect();
  31. Painting::paint_border(context, Painting::BorderEdge::Left, bordered_rect, computed_values());
  32. Painting::paint_border(context, Painting::BorderEdge::Right, bordered_rect, computed_values());
  33. Painting::paint_border(context, Painting::BorderEdge::Top, bordered_rect, computed_values());
  34. Painting::paint_border(context, Painting::BorderEdge::Bottom, bordered_rect, computed_values());
  35. }
  36. Layout::NodeWithStyleAndBoxModelMetrics::paint(context, phase);
  37. if (phase == PaintPhase::Overlay && dom_node() && document().inspected_node() == dom_node()) {
  38. auto content_rect = absolute_rect();
  39. auto margin_box = box_model().margin_box();
  40. Gfx::FloatRect margin_rect;
  41. margin_rect.set_x(absolute_x() - margin_box.left);
  42. margin_rect.set_width(width() + margin_box.left + margin_box.right);
  43. margin_rect.set_y(absolute_y() - margin_box.top);
  44. margin_rect.set_height(height() + margin_box.top + margin_box.bottom);
  45. context.painter().draw_rect(enclosing_int_rect(margin_rect), Color::Yellow);
  46. context.painter().draw_rect(enclosing_int_rect(padded_rect), Color::Cyan);
  47. context.painter().draw_rect(enclosing_int_rect(content_rect), Color::Magenta);
  48. }
  49. if (phase == PaintPhase::FocusOutline && dom_node() && dom_node()->is_element() && downcast<DOM::Element>(*dom_node()).is_focused()) {
  50. context.painter().draw_rect(enclosing_int_rect(absolute_rect()), context.palette().focus_outline());
  51. }
  52. }
  53. void Box::paint_background_image(
  54. PaintContext& context,
  55. const Gfx::Bitmap& background_image,
  56. CSS::Repeat background_repeat_x,
  57. CSS::Repeat background_repeat_y,
  58. Gfx::IntRect background_rect)
  59. {
  60. switch (background_repeat_x) {
  61. case CSS::Repeat::Round:
  62. case CSS::Repeat::Space:
  63. // FIXME: Support 'round' and 'space'. Fall through to 'repeat' since that most closely resembles these.
  64. case CSS::Repeat::Repeat:
  65. // The background rect is already sized to align with 'repeat'.
  66. break;
  67. case CSS::Repeat::NoRepeat:
  68. background_rect.set_width(background_image.width());
  69. break;
  70. }
  71. switch (background_repeat_y) {
  72. case CSS::Repeat::Round:
  73. case CSS::Repeat::Space:
  74. // FIXME: Support 'round' and 'space'. Fall through to 'repeat' since that most closely resembles these.
  75. case CSS::Repeat::Repeat:
  76. // The background rect is already sized to align with 'repeat'.
  77. break;
  78. case CSS::Repeat::NoRepeat:
  79. background_rect.set_height(background_image.height());
  80. break;
  81. }
  82. context.painter().blit_tiled(background_rect, background_image, background_image.rect());
  83. }
  84. HitTestResult Box::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  85. {
  86. // FIXME: It would be nice if we could confidently skip over hit testing
  87. // parts of the layout tree, but currently we can't just check
  88. // m_rect.contains() since inline text rects can't be trusted..
  89. HitTestResult result { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  90. for_each_child_in_paint_order([&](auto& child) {
  91. auto child_result = child.hit_test(position, type);
  92. if (child_result.layout_node)
  93. result = child_result;
  94. });
  95. return result;
  96. }
  97. void Box::set_needs_display()
  98. {
  99. if (!is_inline()) {
  100. frame().set_needs_display(enclosing_int_rect(absolute_rect()));
  101. return;
  102. }
  103. Node::set_needs_display();
  104. }
  105. bool Box::is_body() const
  106. {
  107. return dom_node() && dom_node() == document().body();
  108. }
  109. void Box::set_offset(const Gfx::FloatPoint& offset)
  110. {
  111. if (m_offset == offset)
  112. return;
  113. m_offset = offset;
  114. did_set_rect();
  115. }
  116. void Box::set_size(const Gfx::FloatSize& size)
  117. {
  118. if (m_size == size)
  119. return;
  120. m_size = size;
  121. did_set_rect();
  122. }
  123. Gfx::FloatPoint Box::effective_offset() const
  124. {
  125. if (m_containing_line_box_fragment)
  126. return m_containing_line_box_fragment->offset();
  127. return m_offset;
  128. }
  129. const Gfx::FloatRect Box::absolute_rect() const
  130. {
  131. Gfx::FloatRect rect { effective_offset(), size() };
  132. for (auto* block = containing_block(); block; block = block->containing_block()) {
  133. rect.translate_by(block->effective_offset());
  134. }
  135. return rect;
  136. }
  137. void Box::set_containing_line_box_fragment(LineBoxFragment& fragment)
  138. {
  139. m_containing_line_box_fragment = fragment.make_weak_ptr();
  140. }
  141. StackingContext* Box::enclosing_stacking_context()
  142. {
  143. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  144. if (!is<Box>(ancestor))
  145. continue;
  146. auto& ancestor_box = downcast<Box>(*ancestor);
  147. if (!ancestor_box.establishes_stacking_context())
  148. continue;
  149. VERIFY(ancestor_box.stacking_context());
  150. return ancestor_box.stacking_context();
  151. }
  152. // We should always reach the Layout::InitialContainingBlockBox stacking context.
  153. VERIFY_NOT_REACHED();
  154. }
  155. bool Box::establishes_stacking_context() const
  156. {
  157. if (!has_style())
  158. return false;
  159. if (dom_node() == document().root())
  160. return true;
  161. auto position = computed_values().position();
  162. auto z_index = computed_values().z_index();
  163. if (position == CSS::Position::Absolute || position == CSS::Position::Relative) {
  164. if (z_index.has_value())
  165. return true;
  166. }
  167. if (position == CSS::Position::Fixed || position == CSS::Position::Sticky)
  168. return true;
  169. return false;
  170. }
  171. LineBox& Box::ensure_last_line_box()
  172. {
  173. if (m_line_boxes.is_empty())
  174. return add_line_box();
  175. return m_line_boxes.last();
  176. }
  177. LineBox& Box::add_line_box()
  178. {
  179. m_line_boxes.append(LineBox());
  180. return m_line_boxes.last();
  181. }
  182. float Box::width_of_logical_containing_block() const
  183. {
  184. auto* containing_block = this->containing_block();
  185. VERIFY(containing_block);
  186. return containing_block->width();
  187. }
  188. }