Box.cpp 11 KB

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