Paintable.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/Paintable.h>
  11. #include <LibWeb/Painting/ShadowPainting.h>
  12. namespace Web::Painting {
  13. NonnullRefPtr<PaintableBox> PaintableBox::create(Layout::Box const& layout_box)
  14. {
  15. return adopt_ref(*new PaintableBox(layout_box));
  16. }
  17. PaintableBox::PaintableBox(Layout::Box const& layout_box)
  18. : Paintable(layout_box)
  19. {
  20. }
  21. PaintableBox::~PaintableBox()
  22. {
  23. }
  24. PaintableWithLines::PaintableWithLines(Layout::BlockContainer const& layout_box)
  25. : PaintableBox(layout_box)
  26. {
  27. }
  28. PaintableWithLines::~PaintableWithLines()
  29. {
  30. }
  31. void PaintableBox::set_offset(const Gfx::FloatPoint& offset)
  32. {
  33. if (m_offset == offset)
  34. return;
  35. m_offset = offset;
  36. // FIXME: This const_cast is gross.
  37. const_cast<Layout::Box&>(layout_box()).did_set_rect();
  38. }
  39. void PaintableBox::set_content_size(Gfx::FloatSize const& size)
  40. {
  41. if (m_content_size == size)
  42. return;
  43. m_content_size = size;
  44. // FIXME: This const_cast is gross.
  45. const_cast<Layout::Box&>(layout_box()).did_set_rect();
  46. }
  47. Gfx::FloatPoint PaintableBox::effective_offset() const
  48. {
  49. if (m_containing_line_box_fragment.has_value()) {
  50. auto const& fragment = layout_box().containing_block()->paint_box()->line_boxes()[m_containing_line_box_fragment->line_box_index].fragments()[m_containing_line_box_fragment->fragment_index];
  51. return fragment.offset();
  52. }
  53. return m_offset;
  54. }
  55. Gfx::FloatRect PaintableBox::absolute_rect() const
  56. {
  57. Gfx::FloatRect rect { effective_offset(), content_size() };
  58. for (auto* block = layout_box().containing_block(); block; block = block->containing_block())
  59. rect.translate_by(block->paint_box()->effective_offset());
  60. return rect;
  61. }
  62. void PaintableBox::set_containing_line_box_fragment(Optional<Layout::LineBoxFragmentCoordinate> fragment_coordinate)
  63. {
  64. m_containing_line_box_fragment = fragment_coordinate;
  65. }
  66. Painting::StackingContext* PaintableBox::enclosing_stacking_context()
  67. {
  68. for (auto* ancestor = layout_box().parent(); ancestor; ancestor = ancestor->parent()) {
  69. if (!is<Layout::Box>(ancestor))
  70. continue;
  71. auto& ancestor_box = static_cast<Layout::Box&>(const_cast<Layout::NodeWithStyle&>(*ancestor));
  72. if (!ancestor_box.establishes_stacking_context())
  73. continue;
  74. VERIFY(ancestor_box.paint_box()->stacking_context());
  75. return const_cast<StackingContext*>(ancestor_box.paint_box()->stacking_context());
  76. }
  77. // We should always reach the Layout::InitialContainingBlock stacking context.
  78. VERIFY_NOT_REACHED();
  79. }
  80. void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
  81. {
  82. if (!is_visible())
  83. return;
  84. if (phase == PaintPhase::Background) {
  85. paint_background(context);
  86. paint_box_shadow(context);
  87. }
  88. if (phase == PaintPhase::Border) {
  89. paint_border(context);
  90. }
  91. if (phase == PaintPhase::Overlay && layout_box().dom_node() && layout_box().document().inspected_node() == layout_box().dom_node()) {
  92. auto content_rect = absolute_rect();
  93. auto margin_box = box_model().margin_box();
  94. Gfx::FloatRect margin_rect;
  95. margin_rect.set_x(absolute_x() - margin_box.left);
  96. margin_rect.set_width(content_width() + margin_box.left + margin_box.right);
  97. margin_rect.set_y(absolute_y() - margin_box.top);
  98. margin_rect.set_height(content_height() + margin_box.top + margin_box.bottom);
  99. auto border_rect = absolute_border_box_rect();
  100. auto padding_rect = absolute_padding_box_rect();
  101. auto paint_inspector_rect = [&](Gfx::FloatRect const& rect, Color color) {
  102. context.painter().fill_rect(enclosing_int_rect(rect), Color(color).with_alpha(100));
  103. context.painter().draw_rect(enclosing_int_rect(rect), Color(color));
  104. };
  105. paint_inspector_rect(margin_rect, Color::Yellow);
  106. paint_inspector_rect(padding_rect, Color::Cyan);
  107. paint_inspector_rect(border_rect, Color::Green);
  108. paint_inspector_rect(content_rect, Color::Magenta);
  109. StringBuilder builder;
  110. if (layout_box().dom_node())
  111. builder.append(layout_box().dom_node()->debug_description());
  112. else
  113. builder.append(layout_box().debug_description());
  114. builder.appendff(" {}x{} @ {},{}", border_rect.width(), border_rect.height(), border_rect.x(), border_rect.y());
  115. auto size_text = builder.to_string();
  116. auto size_text_rect = border_rect;
  117. size_text_rect.set_y(border_rect.y() + border_rect.height());
  118. size_text_rect.set_top(size_text_rect.top());
  119. size_text_rect.set_width((float)context.painter().font().width(size_text) + 4);
  120. size_text_rect.set_height(context.painter().font().glyph_height() + 4);
  121. context.painter().fill_rect(enclosing_int_rect(size_text_rect), context.palette().color(Gfx::ColorRole::Tooltip));
  122. context.painter().draw_rect(enclosing_int_rect(size_text_rect), context.palette().threed_shadow1());
  123. context.painter().draw_text(enclosing_int_rect(size_text_rect), size_text, Gfx::TextAlignment::Center, context.palette().color(Gfx::ColorRole::TooltipText));
  124. }
  125. if (phase == PaintPhase::FocusOutline && layout_box().dom_node() && layout_box().dom_node()->is_element() && verify_cast<DOM::Element>(*layout_box().dom_node()).is_focused()) {
  126. context.painter().draw_rect(enclosing_int_rect(absolute_rect()), context.palette().focus_outline());
  127. }
  128. }
  129. void PaintableBox::paint_border(PaintContext& context) const
  130. {
  131. auto borders_data = BordersData {
  132. .top = computed_values().border_top(),
  133. .right = computed_values().border_right(),
  134. .bottom = computed_values().border_bottom(),
  135. .left = computed_values().border_left(),
  136. };
  137. paint_all_borders(context, absolute_border_box_rect(), normalized_border_radius_data(), borders_data);
  138. }
  139. void PaintableBox::paint_background(PaintContext& context) const
  140. {
  141. // If the body's background properties were propagated to the root element, do no re-paint the body's background.
  142. if (layout_box().is_body() && document().html_element()->should_use_body_background_properties())
  143. return;
  144. Gfx::IntRect background_rect;
  145. Color background_color = computed_values().background_color();
  146. auto* background_layers = &computed_values().background_layers();
  147. if (layout_box().is_root_element()) {
  148. // CSS 2.1 Appendix E.2: If the element is a root element, paint the background over the entire canvas.
  149. background_rect = context.viewport_rect();
  150. // Section 2.11.2: If the computed value of background-image on the root element is none and its background-color is transparent,
  151. // user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY child element.
  152. if (document().html_element()->should_use_body_background_properties()) {
  153. background_layers = document().background_layers();
  154. background_color = document().background_color(context.palette());
  155. }
  156. } else {
  157. background_rect = enclosing_int_rect(absolute_padding_box_rect());
  158. }
  159. // HACK: If the Box has a border, use the bordered_rect to paint the background.
  160. // This way if we have a border-radius there will be no gap between the filling and actual border.
  161. if (computed_values().border_top().width || computed_values().border_right().width || computed_values().border_bottom().width || computed_values().border_left().width)
  162. background_rect = enclosing_int_rect(absolute_border_box_rect());
  163. Painting::paint_background(context, layout_box(), background_rect, background_color, background_layers, normalized_border_radius_data());
  164. }
  165. void PaintableBox::paint_box_shadow(PaintContext& context) const
  166. {
  167. auto box_shadow_data = computed_values().box_shadow();
  168. if (box_shadow_data.is_empty())
  169. return;
  170. Vector<BoxShadowData> resolved_box_shadow_data;
  171. resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
  172. for (auto const& layer : box_shadow_data) {
  173. resolved_box_shadow_data.empend(
  174. layer.color,
  175. static_cast<int>(layer.offset_x.to_px(layout_box())),
  176. static_cast<int>(layer.offset_y.to_px(layout_box())),
  177. static_cast<int>(layer.blur_radius.to_px(layout_box())),
  178. static_cast<int>(layer.spread_distance.to_px(layout_box())),
  179. layer.placement == CSS::BoxShadowPlacement::Outer ? BoxShadowPlacement::Outer : BoxShadowPlacement::Inner);
  180. }
  181. Painting::paint_box_shadow(context, enclosing_int_rect(absolute_border_box_rect()), resolved_box_shadow_data);
  182. }
  183. BorderRadiusData PaintableBox::normalized_border_radius_data() const
  184. {
  185. return Painting::normalized_border_radius_data(layout_box(), absolute_border_box_rect(),
  186. computed_values().border_top_left_radius(),
  187. computed_values().border_top_right_radius(),
  188. computed_values().border_bottom_right_radius(),
  189. computed_values().border_bottom_left_radius());
  190. }
  191. void PaintableBox::before_children_paint(PaintContext& context, PaintPhase) const
  192. {
  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(absolute_border_box_rect()));
  197. }
  198. }
  199. void PaintableBox::after_children_paint(PaintContext& context, PaintPhase) const
  200. {
  201. // FIXME: Support more overflow variations.
  202. if (computed_values().overflow_x() == CSS::Overflow::Hidden && computed_values().overflow_y() == CSS::Overflow::Hidden)
  203. context.painter().restore();
  204. }
  205. void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
  206. {
  207. if (!is_visible())
  208. return;
  209. PaintableBox::paint(context, phase);
  210. if (m_line_boxes.is_empty())
  211. return;
  212. bool should_clip_overflow = computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible;
  213. if (should_clip_overflow) {
  214. context.painter().save();
  215. // FIXME: Handle overflow-x and overflow-y being different values.
  216. context.painter().add_clip_rect(enclosing_int_rect(absolute_padding_box_rect()));
  217. auto scroll_offset = static_cast<Layout::BlockContainer const&>(layout_box()).scroll_offset();
  218. context.painter().translate(-scroll_offset.to_type<int>());
  219. }
  220. for (auto& line_box : m_line_boxes) {
  221. for (auto& fragment : line_box.fragments()) {
  222. if (context.should_show_line_box_borders())
  223. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Green);
  224. const_cast<Layout::LineBoxFragment&>(fragment).paint(context, phase);
  225. }
  226. }
  227. if (should_clip_overflow) {
  228. context.painter().restore();
  229. }
  230. // FIXME: Merge this loop with the above somehow..
  231. if (phase == PaintPhase::FocusOutline) {
  232. for (auto& line_box : m_line_boxes) {
  233. for (auto& fragment : line_box.fragments()) {
  234. auto* node = fragment.layout_node().dom_node();
  235. if (!node)
  236. continue;
  237. auto* parent = node->parent_element();
  238. if (!parent)
  239. continue;
  240. if (parent->is_focused())
  241. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline());
  242. }
  243. }
  244. }
  245. }
  246. void Paintable::handle_mousedown(Badge<EventHandler>, Gfx::IntPoint const&, unsigned, unsigned)
  247. {
  248. }
  249. void Paintable::handle_mouseup(Badge<EventHandler>, Gfx::IntPoint const&, unsigned, unsigned)
  250. {
  251. }
  252. void Paintable::handle_mousemove(Badge<EventHandler>, Gfx::IntPoint const&, unsigned, unsigned)
  253. {
  254. }
  255. bool Paintable::handle_mousewheel(Badge<EventHandler>, Gfx::IntPoint const&, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y)
  256. {
  257. if (auto* containing_block = layout_node().containing_block()) {
  258. if (!containing_block->is_scrollable())
  259. return false;
  260. auto new_offset = containing_block->scroll_offset();
  261. new_offset.translate_by(wheel_delta_x, wheel_delta_y);
  262. // FIXME: This const_cast is gross.
  263. // FIXME: Scroll offset shouldn't live in the layout tree.
  264. const_cast<Layout::BlockContainer*>(containing_block)->set_scroll_offset(new_offset);
  265. return true;
  266. }
  267. return false;
  268. }
  269. bool PaintableWithLines::handle_mousewheel(Badge<EventHandler>, Gfx::IntPoint const&, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y)
  270. {
  271. if (!layout_box().is_scrollable())
  272. return false;
  273. auto new_offset = layout_box().scroll_offset();
  274. new_offset.translate_by(wheel_delta_x, wheel_delta_y);
  275. const_cast<Layout::BlockContainer&>(layout_box()).set_scroll_offset(new_offset);
  276. return true;
  277. }
  278. Layout::BlockContainer const& PaintableWithLines::layout_box() const
  279. {
  280. return static_cast<Layout::BlockContainer const&>(PaintableBox::layout_box());
  281. }
  282. Layout::BlockContainer& PaintableWithLines::layout_box()
  283. {
  284. return static_cast<Layout::BlockContainer&>(PaintableBox::layout_box());
  285. }
  286. }