PaintableBox.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/HTML/HTMLHtmlElement.h>
  8. #include <LibWeb/Layout/BlockContainer.h>
  9. #include <LibWeb/Painting/BackgroundPainting.h>
  10. #include <LibWeb/Painting/PaintableBox.h>
  11. #include <LibWeb/Painting/ShadowPainting.h>
  12. #include <LibWeb/Painting/StackingContext.h>
  13. namespace Web::Painting {
  14. NonnullRefPtr<PaintableBox> PaintableBox::create(Layout::Box const& layout_box)
  15. {
  16. return adopt_ref(*new PaintableBox(layout_box));
  17. }
  18. PaintableBox::PaintableBox(Layout::Box const& layout_box)
  19. : Paintable(layout_box)
  20. {
  21. }
  22. PaintableBox::~PaintableBox()
  23. {
  24. }
  25. PaintableWithLines::PaintableWithLines(Layout::BlockContainer const& layout_box)
  26. : PaintableBox(layout_box)
  27. {
  28. }
  29. PaintableWithLines::~PaintableWithLines()
  30. {
  31. }
  32. void PaintableBox::set_offset(const Gfx::FloatPoint& offset)
  33. {
  34. if (m_offset == offset)
  35. return;
  36. m_offset = offset;
  37. // FIXME: This const_cast is gross.
  38. const_cast<Layout::Box&>(layout_box()).did_set_rect();
  39. }
  40. void PaintableBox::set_content_size(Gfx::FloatSize const& size)
  41. {
  42. if (m_content_size == size)
  43. return;
  44. m_content_size = size;
  45. // FIXME: This const_cast is gross.
  46. const_cast<Layout::Box&>(layout_box()).did_set_rect();
  47. }
  48. Gfx::FloatPoint PaintableBox::effective_offset() const
  49. {
  50. if (m_containing_line_box_fragment.has_value()) {
  51. auto const& fragment = containing_block()->paint_box()->line_boxes()[m_containing_line_box_fragment->line_box_index].fragments()[m_containing_line_box_fragment->fragment_index];
  52. return fragment.offset();
  53. }
  54. return m_offset;
  55. }
  56. Gfx::FloatRect PaintableBox::absolute_rect() const
  57. {
  58. if (!m_absolute_rect.has_value()) {
  59. Gfx::FloatRect rect { effective_offset(), content_size() };
  60. for (auto const* block = containing_block(); block && block->paintable(); block = block->paintable()->containing_block())
  61. rect.translate_by(block->paint_box()->effective_offset());
  62. m_absolute_rect = rect;
  63. }
  64. return *m_absolute_rect;
  65. }
  66. void PaintableBox::set_containing_line_box_fragment(Optional<Layout::LineBoxFragmentCoordinate> fragment_coordinate)
  67. {
  68. m_containing_line_box_fragment = fragment_coordinate;
  69. }
  70. Painting::StackingContext* PaintableBox::enclosing_stacking_context()
  71. {
  72. for (auto* ancestor = layout_box().parent(); ancestor; ancestor = ancestor->parent()) {
  73. if (!is<Layout::Box>(ancestor))
  74. continue;
  75. auto& ancestor_box = static_cast<Layout::Box&>(const_cast<Layout::NodeWithStyle&>(*ancestor));
  76. if (!ancestor_box.establishes_stacking_context())
  77. continue;
  78. VERIFY(ancestor_box.paint_box()->stacking_context());
  79. return const_cast<StackingContext*>(ancestor_box.paint_box()->stacking_context());
  80. }
  81. // We should always reach the Layout::InitialContainingBlock stacking context.
  82. VERIFY_NOT_REACHED();
  83. }
  84. void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
  85. {
  86. if (!is_visible())
  87. return;
  88. if (phase == PaintPhase::Background) {
  89. paint_background(context);
  90. paint_box_shadow(context);
  91. }
  92. if (phase == PaintPhase::Border) {
  93. paint_border(context);
  94. }
  95. if (phase == PaintPhase::Overlay && layout_box().dom_node() && layout_box().document().inspected_node() == layout_box().dom_node()) {
  96. auto content_rect = absolute_rect();
  97. auto margin_box = box_model().margin_box();
  98. Gfx::FloatRect margin_rect;
  99. margin_rect.set_x(absolute_x() - margin_box.left);
  100. margin_rect.set_width(content_width() + margin_box.left + margin_box.right);
  101. margin_rect.set_y(absolute_y() - margin_box.top);
  102. margin_rect.set_height(content_height() + margin_box.top + margin_box.bottom);
  103. auto border_rect = absolute_border_box_rect();
  104. auto padding_rect = absolute_padding_box_rect();
  105. auto paint_inspector_rect = [&](Gfx::FloatRect const& rect, Color color) {
  106. context.painter().fill_rect(enclosing_int_rect(rect), Color(color).with_alpha(100));
  107. context.painter().draw_rect(enclosing_int_rect(rect), Color(color));
  108. };
  109. paint_inspector_rect(margin_rect, Color::Yellow);
  110. paint_inspector_rect(padding_rect, Color::Cyan);
  111. paint_inspector_rect(border_rect, Color::Green);
  112. paint_inspector_rect(content_rect, Color::Magenta);
  113. StringBuilder builder;
  114. if (layout_box().dom_node())
  115. builder.append(layout_box().dom_node()->debug_description());
  116. else
  117. builder.append(layout_box().debug_description());
  118. builder.appendff(" {}x{} @ {},{}", border_rect.width(), border_rect.height(), border_rect.x(), border_rect.y());
  119. auto size_text = builder.to_string();
  120. auto size_text_rect = border_rect;
  121. size_text_rect.set_y(border_rect.y() + border_rect.height());
  122. size_text_rect.set_top(size_text_rect.top());
  123. size_text_rect.set_width((float)context.painter().font().width(size_text) + 4);
  124. size_text_rect.set_height(context.painter().font().glyph_height() + 4);
  125. context.painter().fill_rect(enclosing_int_rect(size_text_rect), context.palette().color(Gfx::ColorRole::Tooltip));
  126. context.painter().draw_rect(enclosing_int_rect(size_text_rect), context.palette().threed_shadow1());
  127. context.painter().draw_text(enclosing_int_rect(size_text_rect), size_text, Gfx::TextAlignment::Center, context.palette().color(Gfx::ColorRole::TooltipText));
  128. }
  129. if (phase == PaintPhase::FocusOutline && layout_box().dom_node() && layout_box().dom_node()->is_element() && verify_cast<DOM::Element>(*layout_box().dom_node()).is_focused()) {
  130. context.painter().draw_rect(enclosing_int_rect(absolute_rect()), context.palette().focus_outline());
  131. }
  132. }
  133. void PaintableBox::paint_border(PaintContext& context) const
  134. {
  135. auto borders_data = BordersData {
  136. .top = computed_values().border_top(),
  137. .right = computed_values().border_right(),
  138. .bottom = computed_values().border_bottom(),
  139. .left = computed_values().border_left(),
  140. };
  141. paint_all_borders(context, absolute_border_box_rect(), normalized_border_radius_data(), borders_data);
  142. }
  143. void PaintableBox::paint_background(PaintContext& context) const
  144. {
  145. // If the body's background properties were propagated to the root element, do no re-paint the body's background.
  146. if (layout_box().is_body() && document().html_element()->should_use_body_background_properties())
  147. return;
  148. Gfx::IntRect background_rect;
  149. Color background_color = computed_values().background_color();
  150. auto* background_layers = &computed_values().background_layers();
  151. if (layout_box().is_root_element()) {
  152. // CSS 2.1 Appendix E.2: If the element is a root element, paint the background over the entire canvas.
  153. background_rect = context.viewport_rect();
  154. // Section 2.11.2: If the computed value of background-image on the root element is none and its background-color is transparent,
  155. // user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY child element.
  156. if (document().html_element()->should_use_body_background_properties()) {
  157. background_layers = document().background_layers();
  158. background_color = document().background_color(context.palette());
  159. }
  160. } else {
  161. background_rect = enclosing_int_rect(absolute_padding_box_rect());
  162. }
  163. // HACK: If the Box has a border, use the bordered_rect to paint the background.
  164. // This way if we have a border-radius there will be no gap between the filling and actual border.
  165. if (computed_values().border_top().width || computed_values().border_right().width || computed_values().border_bottom().width || computed_values().border_left().width)
  166. background_rect = enclosing_int_rect(absolute_border_box_rect());
  167. Painting::paint_background(context, layout_box(), background_rect, background_color, background_layers, normalized_border_radius_data());
  168. }
  169. void PaintableBox::paint_box_shadow(PaintContext& context) const
  170. {
  171. auto box_shadow_data = computed_values().box_shadow();
  172. if (box_shadow_data.is_empty())
  173. return;
  174. Vector<BoxShadowData> resolved_box_shadow_data;
  175. resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
  176. for (auto const& layer : box_shadow_data) {
  177. resolved_box_shadow_data.empend(
  178. layer.color,
  179. static_cast<int>(layer.offset_x.to_px(layout_box())),
  180. static_cast<int>(layer.offset_y.to_px(layout_box())),
  181. static_cast<int>(layer.blur_radius.to_px(layout_box())),
  182. static_cast<int>(layer.spread_distance.to_px(layout_box())),
  183. layer.placement == CSS::BoxShadowPlacement::Outer ? BoxShadowPlacement::Outer : BoxShadowPlacement::Inner);
  184. }
  185. Painting::paint_box_shadow(context, enclosing_int_rect(absolute_border_box_rect()), resolved_box_shadow_data);
  186. }
  187. BorderRadiusData PaintableBox::normalized_border_radius_data() const
  188. {
  189. return Painting::normalized_border_radius_data(layout_box(), absolute_border_box_rect(),
  190. computed_values().border_top_left_radius(),
  191. computed_values().border_top_right_radius(),
  192. computed_values().border_bottom_right_radius(),
  193. computed_values().border_bottom_left_radius());
  194. }
  195. void PaintableBox::before_children_paint(PaintContext& context, PaintPhase) const
  196. {
  197. // FIXME: Support more overflow variations.
  198. if (computed_values().overflow_x() == CSS::Overflow::Hidden && computed_values().overflow_y() == CSS::Overflow::Hidden) {
  199. context.painter().save();
  200. context.painter().add_clip_rect(enclosing_int_rect(absolute_border_box_rect()));
  201. }
  202. }
  203. void PaintableBox::after_children_paint(PaintContext& context, PaintPhase) const
  204. {
  205. // FIXME: Support more overflow variations.
  206. if (computed_values().overflow_x() == CSS::Overflow::Hidden && computed_values().overflow_y() == CSS::Overflow::Hidden)
  207. context.painter().restore();
  208. }
  209. void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
  210. {
  211. if (!is_visible())
  212. return;
  213. PaintableBox::paint(context, phase);
  214. if (m_line_boxes.is_empty())
  215. return;
  216. bool should_clip_overflow = computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible;
  217. if (should_clip_overflow) {
  218. context.painter().save();
  219. // FIXME: Handle overflow-x and overflow-y being different values.
  220. context.painter().add_clip_rect(enclosing_int_rect(absolute_padding_box_rect()));
  221. auto scroll_offset = static_cast<Layout::BlockContainer const&>(layout_box()).scroll_offset();
  222. context.painter().translate(-scroll_offset.to_type<int>());
  223. }
  224. for (auto& line_box : m_line_boxes) {
  225. for (auto& fragment : line_box.fragments()) {
  226. if (context.should_show_line_box_borders())
  227. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Green);
  228. const_cast<Layout::LineBoxFragment&>(fragment).paint(context, phase);
  229. }
  230. }
  231. if (should_clip_overflow) {
  232. context.painter().restore();
  233. }
  234. // FIXME: Merge this loop with the above somehow..
  235. if (phase == PaintPhase::FocusOutline) {
  236. for (auto& line_box : m_line_boxes) {
  237. for (auto& fragment : line_box.fragments()) {
  238. auto* node = fragment.layout_node().dom_node();
  239. if (!node)
  240. continue;
  241. auto* parent = node->parent_element();
  242. if (!parent)
  243. continue;
  244. if (parent->is_focused())
  245. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline());
  246. }
  247. }
  248. }
  249. }
  250. bool PaintableWithLines::handle_mousewheel(Badge<EventHandler>, Gfx::IntPoint const&, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y)
  251. {
  252. if (!layout_box().is_scrollable())
  253. return false;
  254. auto new_offset = layout_box().scroll_offset();
  255. new_offset.translate_by(wheel_delta_x, wheel_delta_y);
  256. const_cast<Layout::BlockContainer&>(layout_box()).set_scroll_offset(new_offset);
  257. return true;
  258. }
  259. Layout::BlockContainer const& PaintableWithLines::layout_box() const
  260. {
  261. return static_cast<Layout::BlockContainer const&>(PaintableBox::layout_box());
  262. }
  263. Layout::BlockContainer& PaintableWithLines::layout_box()
  264. {
  265. return static_cast<Layout::BlockContainer&>(PaintableBox::layout_box());
  266. }
  267. void PaintableBox::set_stacking_context(NonnullOwnPtr<StackingContext> stacking_context)
  268. {
  269. m_stacking_context = move(stacking_context);
  270. }
  271. template<typename Callback>
  272. void PaintableBox::for_each_child_in_paint_order(Callback callback) const
  273. {
  274. // Element traversal using the order defined in https://www.w3.org/TR/CSS2/zindex.html#painting-order.
  275. // Note: Some steps are skipped because they are not relevant to node traversal.
  276. // 3. Stacking contexts formed by positioned descendants with negative z-indices (excluding 0) in z-index order
  277. // (most negative first) then tree order.
  278. // FIXME: This does not retrieve elements in the z-index order.
  279. layout_box().for_each_child([&](auto& child) {
  280. if (!child.is_positioned() || !is<Layout::Box>(child))
  281. return;
  282. auto& box_child = verify_cast<Layout::Box>(child);
  283. auto* stacking_context = box_child.paint_box()->stacking_context();
  284. if (stacking_context && box_child.computed_values().z_index().has_value() && box_child.computed_values().z_index().value() < 0)
  285. callback(child);
  286. });
  287. // 4. For all its in-flow, non-positioned, block-level descendants in tree order: If the element is a block, list-item,
  288. // or other block equivalent:
  289. layout_box().for_each_child([&](auto& child) {
  290. if (is<Layout::Box>(child) && verify_cast<Layout::Box>(child).paint_box()->stacking_context())
  291. return;
  292. if (!child.is_positioned())
  293. callback(child);
  294. });
  295. // 5. All non-positioned floating descendants, in tree order. For each one of these, treat the element as if it created
  296. // a new stacking context, but any positioned descendants and descendants which actually create a new stacking context
  297. // should be considered part of the parent stacking context, not this new one.
  298. layout_box().for_each_child([&](auto& child) {
  299. if (is<Layout::Box>(child) && verify_cast<Layout::Box>(child).paint_box()->stacking_context())
  300. return;
  301. if (child.is_positioned())
  302. callback(child);
  303. });
  304. // 8. All positioned descendants with 'z-index: auto' or 'z-index: 0', in tree order. For those with 'z-index: auto', treat
  305. // the element as if it created a new stacking context, but any positioned descendants and descendants which actually
  306. // create a new stacking context should be considered part of the parent stacking context, not this new one. For those
  307. // with 'z-index: 0', treat the stacking context generated atomically.
  308. layout_box().for_each_child([&](auto& child) {
  309. if (!child.is_positioned() || !is<Layout::Box>(child))
  310. return;
  311. auto& box_child = verify_cast<Layout::Box>(child);
  312. auto* stacking_context = box_child.paint_box()->stacking_context();
  313. if (stacking_context && box_child.computed_values().z_index().has_value() && box_child.computed_values().z_index().value() == 0)
  314. callback(child);
  315. });
  316. // 9. Stacking contexts formed by positioned descendants with z-indices greater than or equal to 1 in z-index order
  317. // (smallest first) then tree order.
  318. // FIXME: This does not retrieve elements in the z-index order.
  319. layout_box().for_each_child([&](auto& child) {
  320. if (!child.is_positioned() || !is<Layout::Box>(child))
  321. return;
  322. auto& box_child = verify_cast<Layout::Box>(child);
  323. auto* stacking_context = box_child.paint_box()->stacking_context();
  324. if (stacking_context && box_child.computed_values().z_index().has_value() && box_child.computed_values().z_index().value() > 0)
  325. callback(child);
  326. });
  327. }
  328. HitTestResult PaintableBox::hit_test(Gfx::IntPoint const& position, HitTestType type) const
  329. {
  330. if (layout_box().is_initial_containing_block_box())
  331. return stacking_context()->hit_test(position, type);
  332. HitTestResult result { absolute_border_box_rect().contains(position.x(), position.y()) ? this : nullptr };
  333. for_each_child_in_paint_order([&](auto& child) {
  334. if (child.paintable()) {
  335. auto child_result = child.paintable()->hit_test(position, type);
  336. if (child_result.paintable)
  337. result = child_result;
  338. }
  339. });
  340. return result;
  341. }
  342. HitTestResult PaintableWithLines::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  343. {
  344. if (!layout_box().children_are_inline())
  345. return PaintableBox::hit_test(position, type);
  346. HitTestResult last_good_candidate;
  347. for (auto& line_box : m_line_boxes) {
  348. for (auto& fragment : line_box.fragments()) {
  349. if (is<Layout::Box>(fragment.layout_node()) && static_cast<Layout::Box const&>(fragment.layout_node()).paint_box()->stacking_context())
  350. continue;
  351. if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
  352. if (is<Layout::BlockContainer>(fragment.layout_node()) && fragment.layout_node().paintable())
  353. return fragment.layout_node().paintable()->hit_test(position, type);
  354. return { fragment.layout_node().paintable(), fragment.text_index_at(position.x()) };
  355. }
  356. if (fragment.absolute_rect().top() <= position.y())
  357. last_good_candidate = { fragment.layout_node().paintable(), fragment.text_index_at(position.x()) };
  358. }
  359. }
  360. if (type == HitTestType::TextCursor && last_good_candidate.paintable)
  361. return last_good_candidate;
  362. return { absolute_border_box_rect().contains(position.x(), position.y()) ? this : nullptr };
  363. }
  364. }