Box.cpp 7.9 KB

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