Box.cpp 9.4 KB

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