Box.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGfx/Painter.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/HTML/HTMLBodyElement.h>
  10. #include <LibWeb/HTML/HTMLHtmlElement.h>
  11. #include <LibWeb/Layout/BlockContainer.h>
  12. #include <LibWeb/Layout/Box.h>
  13. #include <LibWeb/Layout/FormattingContext.h>
  14. #include <LibWeb/Page/BrowsingContext.h>
  15. #include <LibWeb/Painting/BackgroundPainting.h>
  16. #include <LibWeb/Painting/BorderPainting.h>
  17. #include <LibWeb/Painting/ShadowPainting.h>
  18. namespace Web::Layout {
  19. void Box::paint(PaintContext& context, PaintPhase phase)
  20. {
  21. if (!is_visible())
  22. return;
  23. Gfx::PainterStateSaver saver(context.painter());
  24. if (is_fixed_position())
  25. context.painter().translate(context.scroll_offset());
  26. if (phase == PaintPhase::Background) {
  27. paint_background(context);
  28. paint_box_shadow(context);
  29. }
  30. if (phase == PaintPhase::Border) {
  31. paint_border(context);
  32. }
  33. if (phase == PaintPhase::Overlay && dom_node() && document().inspected_node() == dom_node()) {
  34. auto content_rect = absolute_rect();
  35. auto margin_box = box_model().margin_box();
  36. Gfx::FloatRect margin_rect;
  37. margin_rect.set_x(absolute_x() - margin_box.left);
  38. margin_rect.set_width(width() + margin_box.left + margin_box.right);
  39. margin_rect.set_y(absolute_y() - margin_box.top);
  40. margin_rect.set_height(height() + margin_box.top + margin_box.bottom);
  41. context.painter().draw_rect(enclosing_int_rect(margin_rect), Color::Yellow);
  42. context.painter().draw_rect(enclosing_int_rect(padded_rect()), Color::Cyan);
  43. context.painter().draw_rect(enclosing_int_rect(content_rect), Color::Magenta);
  44. }
  45. if (phase == PaintPhase::FocusOutline && dom_node() && dom_node()->is_element() && verify_cast<DOM::Element>(*dom_node()).is_focused()) {
  46. context.painter().draw_rect(enclosing_int_rect(absolute_rect()), context.palette().focus_outline());
  47. }
  48. }
  49. void Box::paint_border(PaintContext& context)
  50. {
  51. auto borders_data = Painting::BordersData {
  52. .top = computed_values().border_top(),
  53. .right = computed_values().border_right(),
  54. .bottom = computed_values().border_bottom(),
  55. .left = computed_values().border_left(),
  56. };
  57. Painting::paint_all_borders(context, bordered_rect(), normalized_border_radius_data(), borders_data);
  58. }
  59. void Box::paint_background(PaintContext& context)
  60. {
  61. // If the body's background properties were propagated to the root element, do no re-paint the body's background.
  62. if (is_body() && document().html_element()->should_use_body_background_properties())
  63. return;
  64. Gfx::IntRect background_rect;
  65. Color background_color = computed_values().background_color();
  66. const Gfx::Bitmap* background_image = this->background_image() ? this->background_image()->bitmap() : nullptr;
  67. CSS::Repeat background_repeat_x = computed_values().background_repeat_x();
  68. CSS::Repeat background_repeat_y = computed_values().background_repeat_y();
  69. if (is_root_element()) {
  70. // CSS 2.1 Appendix E.2: If the element is a root element, paint the background over the entire canvas.
  71. background_rect = context.viewport_rect();
  72. // Section 2.11.2: If the computed value of background-image on the root element is none and its background-color is transparent,
  73. // user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY child element.
  74. if (document().html_element()->should_use_body_background_properties()) {
  75. background_color = document().background_color(context.palette());
  76. background_image = document().background_image();
  77. background_repeat_x = document().background_repeat_x();
  78. background_repeat_y = document().background_repeat_y();
  79. }
  80. } else {
  81. background_rect = enclosing_int_rect(padded_rect());
  82. }
  83. // HACK: If the Box has a border, use the bordered_rect to paint the background.
  84. // This way if we have a border-radius there will be no gap between the filling and actual border.
  85. if (computed_values().border_top().width || computed_values().border_right().width || computed_values().border_bottom().width || computed_values().border_left().width)
  86. background_rect = enclosing_int_rect(bordered_rect());
  87. auto background_data = Painting::BackgroundData {
  88. .color = background_color,
  89. .image = background_image,
  90. .repeat_x = background_repeat_x,
  91. .repeat_y = background_repeat_y
  92. };
  93. Painting::paint_background(context, background_rect, background_data, normalized_border_radius_data());
  94. }
  95. void Box::paint_box_shadow(PaintContext& context)
  96. {
  97. auto box_shadow_data = computed_values().box_shadow();
  98. if (!box_shadow_data.has_value())
  99. return;
  100. auto resolved_box_shadow_data = Painting::BoxShadowData {
  101. .offset_x = (int)box_shadow_data->offset_x.resolved_or_zero(*this, width()).to_px(*this),
  102. .offset_y = (int)box_shadow_data->offset_y.resolved_or_zero(*this, width()).to_px(*this),
  103. .blur_radius = (int)box_shadow_data->blur_radius.resolved_or_zero(*this, width()).to_px(*this),
  104. .color = box_shadow_data->color
  105. };
  106. Painting::paint_box_shadow(context, enclosing_int_rect(bordered_rect()), resolved_box_shadow_data);
  107. }
  108. Painting::BorderRadiusData Box::normalized_border_radius_data()
  109. {
  110. return Painting::normalized_border_radius_data(*this, bordered_rect(),
  111. computed_values().border_top_left_radius(),
  112. computed_values().border_top_right_radius(),
  113. computed_values().border_bottom_right_radius(),
  114. computed_values().border_bottom_left_radius());
  115. }
  116. // https://www.w3.org/TR/css-display-3/#out-of-flow
  117. bool Box::is_out_of_flow(FormattingContext const& formatting_context) const
  118. {
  119. // A box is out of flow if either:
  120. // 1. It is floated (which requires that floating is not inhibited).
  121. if (!formatting_context.inhibits_floating() && computed_values().float_() != CSS::Float::None)
  122. return true;
  123. // 2. It is "absolutely positioned".
  124. switch (computed_values().position()) {
  125. case CSS::Position::Absolute:
  126. case CSS::Position::Fixed:
  127. return true;
  128. case CSS::Position::Static:
  129. case CSS::Position::Relative:
  130. case CSS::Position::Sticky:
  131. break;
  132. }
  133. return false;
  134. }
  135. HitTestResult Box::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  136. {
  137. // FIXME: It would be nice if we could confidently skip over hit testing
  138. // parts of the layout tree, but currently we can't just check
  139. // m_rect.contains() since inline text rects can't be trusted..
  140. HitTestResult result { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  141. for_each_child_in_paint_order([&](auto& child) {
  142. auto child_result = child.hit_test(position, type);
  143. if (child_result.layout_node)
  144. result = child_result;
  145. });
  146. return result;
  147. }
  148. void Box::set_needs_display()
  149. {
  150. if (!is_inline()) {
  151. browsing_context().set_needs_display(enclosing_int_rect(absolute_rect()));
  152. return;
  153. }
  154. Node::set_needs_display();
  155. }
  156. bool Box::is_body() const
  157. {
  158. return dom_node() && dom_node() == document().body();
  159. }
  160. void Box::set_offset(const Gfx::FloatPoint& offset)
  161. {
  162. if (m_offset == offset)
  163. return;
  164. m_offset = offset;
  165. did_set_rect();
  166. }
  167. void Box::set_size(const Gfx::FloatSize& size)
  168. {
  169. if (m_size == size)
  170. return;
  171. m_size = size;
  172. did_set_rect();
  173. }
  174. Gfx::FloatPoint Box::effective_offset() const
  175. {
  176. if (m_containing_line_box_fragment)
  177. return m_containing_line_box_fragment->offset();
  178. return m_offset;
  179. }
  180. const Gfx::FloatRect Box::absolute_rect() const
  181. {
  182. Gfx::FloatRect rect { effective_offset(), size() };
  183. for (auto* block = containing_block(); block; block = block->containing_block()) {
  184. rect.translate_by(block->effective_offset());
  185. }
  186. return rect;
  187. }
  188. void Box::set_containing_line_box_fragment(LineBoxFragment& fragment)
  189. {
  190. m_containing_line_box_fragment = fragment.make_weak_ptr();
  191. }
  192. StackingContext* Box::enclosing_stacking_context()
  193. {
  194. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  195. if (!is<Box>(ancestor))
  196. continue;
  197. auto& ancestor_box = verify_cast<Box>(*ancestor);
  198. if (!ancestor_box.establishes_stacking_context())
  199. continue;
  200. VERIFY(ancestor_box.stacking_context());
  201. return ancestor_box.stacking_context();
  202. }
  203. // We should always reach the Layout::InitialContainingBlock stacking context.
  204. VERIFY_NOT_REACHED();
  205. }
  206. LineBox& Box::ensure_last_line_box()
  207. {
  208. if (m_line_boxes.is_empty())
  209. return add_line_box();
  210. return m_line_boxes.last();
  211. }
  212. LineBox& Box::add_line_box()
  213. {
  214. m_line_boxes.append(LineBox());
  215. return m_line_boxes.last();
  216. }
  217. float Box::width_of_logical_containing_block() const
  218. {
  219. auto* containing_block = this->containing_block();
  220. VERIFY(containing_block);
  221. return containing_block->width();
  222. }
  223. }