Box.cpp 9.9 KB

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