Box.cpp 8.0 KB

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