InlinePaintable.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/AntiAliasingPainter.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/Layout/BlockContainer.h>
  9. #include <LibWeb/Painting/BackgroundPainting.h>
  10. #include <LibWeb/Painting/InlinePaintable.h>
  11. #include <LibWeb/Painting/TextPaintable.h>
  12. namespace Web::Painting {
  13. JS::NonnullGCPtr<InlinePaintable> InlinePaintable::create(Layout::InlineNode const& layout_node)
  14. {
  15. return layout_node.heap().allocate_without_realm<InlinePaintable>(layout_node);
  16. }
  17. InlinePaintable::InlinePaintable(Layout::InlineNode const& layout_node)
  18. : Paintable(layout_node)
  19. {
  20. }
  21. Layout::InlineNode const& InlinePaintable::layout_node() const
  22. {
  23. return static_cast<Layout::InlineNode const&>(Paintable::layout_node());
  24. }
  25. Optional<int> InlinePaintable::scroll_frame_id() const
  26. {
  27. if (m_enclosing_scroll_frame)
  28. return m_enclosing_scroll_frame->id;
  29. return {};
  30. }
  31. Optional<CSSPixelPoint> InlinePaintable::enclosing_scroll_frame_offset() const
  32. {
  33. if (m_enclosing_scroll_frame)
  34. return m_enclosing_scroll_frame->offset;
  35. return {};
  36. }
  37. Optional<CSSPixelRect> InlinePaintable::clip_rect() const
  38. {
  39. if (m_enclosing_clip_frame)
  40. return m_enclosing_clip_frame->rect();
  41. return {};
  42. }
  43. void InlinePaintable::before_paint(PaintContext& context, PaintPhase) const
  44. {
  45. if (scroll_frame_id().has_value()) {
  46. context.recording_painter().save();
  47. context.recording_painter().set_scroll_frame_id(scroll_frame_id().value());
  48. }
  49. if (clip_rect().has_value()) {
  50. context.recording_painter().save();
  51. context.recording_painter().add_clip_rect(context.enclosing_device_rect(*clip_rect()).to_type<int>());
  52. }
  53. }
  54. void InlinePaintable::after_paint(PaintContext& context, PaintPhase) const
  55. {
  56. if (clip_rect().has_value())
  57. context.recording_painter().restore();
  58. if (scroll_frame_id().has_value())
  59. context.recording_painter().restore();
  60. }
  61. void InlinePaintable::paint(PaintContext& context, PaintPhase phase) const
  62. {
  63. auto& painter = context.recording_painter();
  64. if (phase == PaintPhase::Background) {
  65. auto containing_block_position_in_absolute_coordinates = containing_block()->absolute_position();
  66. for_each_fragment([&](auto const& fragment, bool is_first_fragment, bool is_last_fragment) {
  67. CSSPixelRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
  68. if (is_first_fragment) {
  69. auto extra_start_width = box_model().padding.left;
  70. absolute_fragment_rect.translate_by(-extra_start_width, 0);
  71. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
  72. }
  73. if (is_last_fragment) {
  74. auto extra_end_width = box_model().padding.right;
  75. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
  76. }
  77. auto const& border_radii_data = fragment.border_radii_data();
  78. paint_background(context, layout_node(), absolute_fragment_rect, computed_values().background_color(), computed_values().image_rendering(), &computed_values().background_layers(), border_radii_data);
  79. if (!box_shadow_data().is_empty()) {
  80. auto borders_data = BordersData {
  81. .top = computed_values().border_top(),
  82. .right = computed_values().border_right(),
  83. .bottom = computed_values().border_bottom(),
  84. .left = computed_values().border_left(),
  85. };
  86. auto absolute_fragment_rect_bordered = absolute_fragment_rect.inflated(
  87. borders_data.top.width, borders_data.right.width,
  88. borders_data.bottom.width, borders_data.left.width);
  89. paint_box_shadow(context, absolute_fragment_rect_bordered, absolute_fragment_rect,
  90. borders_data, border_radii_data, box_shadow_data());
  91. }
  92. return IterationDecision::Continue;
  93. });
  94. }
  95. auto paint_border_or_outline = [&](Optional<BordersData> outline_data = {}, CSSPixels outline_offset = 0) {
  96. auto borders_data = BordersData {
  97. .top = computed_values().border_top(),
  98. .right = computed_values().border_right(),
  99. .bottom = computed_values().border_bottom(),
  100. .left = computed_values().border_left(),
  101. };
  102. auto containing_block_position_in_absolute_coordinates = containing_block()->absolute_position();
  103. for_each_fragment([&](auto const& fragment, bool is_first_fragment, bool is_last_fragment) {
  104. CSSPixelRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
  105. if (is_first_fragment) {
  106. auto extra_start_width = box_model().padding.left;
  107. absolute_fragment_rect.translate_by(-extra_start_width, 0);
  108. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
  109. }
  110. if (is_last_fragment) {
  111. auto extra_end_width = box_model().padding.right;
  112. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
  113. }
  114. auto borders_rect = absolute_fragment_rect.inflated(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
  115. auto border_radii_data = fragment.border_radii_data();
  116. if (outline_data.has_value()) {
  117. auto outline_offset_x = outline_offset;
  118. auto outline_offset_y = outline_offset;
  119. // "Both the height and the width of the outside of the shape drawn by the outline should not
  120. // become smaller than twice the computed value of the outline-width property to make sure
  121. // that an outline can be rendered even with large negative values."
  122. // https://www.w3.org/TR/css-ui-4/#outline-offset
  123. // So, if the horizontal outline offset is > half the borders_rect's width then we set it to that.
  124. // (And the same for y)
  125. if ((borders_rect.width() / 2) + outline_offset_x < 0)
  126. outline_offset_x = -borders_rect.width() / 2;
  127. if ((borders_rect.height() / 2) + outline_offset_y < 0)
  128. outline_offset_y = -borders_rect.height() / 2;
  129. border_radii_data.inflate(outline_data->top.width + outline_offset_y, outline_data->right.width + outline_offset_x, outline_data->bottom.width + outline_offset_y, outline_data->left.width + outline_offset_x);
  130. borders_rect.inflate(outline_data->top.width + outline_offset_y, outline_data->right.width + outline_offset_x, outline_data->bottom.width + outline_offset_y, outline_data->left.width + outline_offset_x);
  131. context.recording_painter().paint_borders(context.rounded_device_rect(borders_rect), border_radii_data.as_corners(context), outline_data->to_device_pixels(context));
  132. } else {
  133. context.recording_painter().paint_borders(context.rounded_device_rect(borders_rect), border_radii_data.as_corners(context), borders_data.to_device_pixels(context));
  134. }
  135. return IterationDecision::Continue;
  136. });
  137. };
  138. if (phase == PaintPhase::Border) {
  139. paint_border_or_outline();
  140. }
  141. if (phase == PaintPhase::Outline) {
  142. auto maybe_outline_data = this->outline_data();
  143. if (maybe_outline_data.has_value())
  144. paint_border_or_outline(maybe_outline_data.value(), computed_values().outline_offset().to_px(layout_node()));
  145. }
  146. if (phase == PaintPhase::Foreground) {
  147. for_each_fragment([&](auto const& fragment, bool, bool) {
  148. if (is<TextPaintable>(fragment.paintable()))
  149. paint_text_fragment(context, static_cast<TextPaintable const&>(fragment.paintable()), fragment, phase);
  150. });
  151. }
  152. if (phase == PaintPhase::Overlay && layout_node().document().inspected_layout_node() == &layout_node()) {
  153. // FIXME: This paints a double-thick border between adjacent fragments, where ideally there
  154. // would be none. Once we implement non-rectangular outlines for the `outline` CSS
  155. // property, we can use that here instead.
  156. for_each_fragment([&](auto const& fragment, bool, bool) {
  157. painter.draw_rect(context.enclosing_device_rect(fragment.absolute_rect()).template to_type<int>(), Color::Magenta);
  158. return IterationDecision::Continue;
  159. });
  160. }
  161. }
  162. template<typename Callback>
  163. void InlinePaintable::for_each_fragment(Callback callback) const
  164. {
  165. for (size_t i = 0; i < m_fragments.size(); ++i) {
  166. auto const& fragment = m_fragments[i];
  167. callback(fragment, i == 0, i == m_fragments.size() - 1);
  168. }
  169. }
  170. TraversalDecision InlinePaintable::hit_test(CSSPixelPoint position, HitTestType type, Function<TraversalDecision(HitTestResult)> const& callback) const
  171. {
  172. if (m_clip_rect.has_value() && !m_clip_rect.value().contains(position))
  173. return TraversalDecision::Continue;
  174. auto position_adjusted_by_scroll_offset = position;
  175. if (enclosing_scroll_frame_offset().has_value())
  176. position_adjusted_by_scroll_offset.translate_by(-enclosing_scroll_frame_offset().value());
  177. for (auto const& fragment : m_fragments) {
  178. if (fragment.paintable().stacking_context())
  179. continue;
  180. auto fragment_absolute_rect = fragment.absolute_rect();
  181. if (fragment_absolute_rect.contains(position_adjusted_by_scroll_offset)) {
  182. if (fragment.paintable().hit_test(position, type, callback) == TraversalDecision::Break)
  183. return TraversalDecision::Break;
  184. auto hit_test_result = HitTestResult { const_cast<Paintable&>(fragment.paintable()), fragment.text_index_at(position_adjusted_by_scroll_offset.x()) };
  185. if (callback(hit_test_result) == TraversalDecision::Break)
  186. return TraversalDecision::Break;
  187. }
  188. }
  189. bool should_exit = false;
  190. for_each_child([&](Paintable const& child) {
  191. if (should_exit)
  192. return;
  193. if (child.stacking_context())
  194. return;
  195. if (child.hit_test(position, type, callback) == TraversalDecision::Break)
  196. should_exit = true;
  197. });
  198. if (should_exit)
  199. return TraversalDecision::Break;
  200. return TraversalDecision::Continue;
  201. }
  202. CSSPixelRect InlinePaintable::bounding_rect() const
  203. {
  204. CSSPixelRect bounding_rect;
  205. for_each_fragment([&](auto const& fragment, bool, bool) {
  206. auto fragment_absolute_rect = fragment.absolute_rect();
  207. bounding_rect = bounding_rect.united(fragment_absolute_rect);
  208. });
  209. if (bounding_rect.is_empty()) {
  210. // FIXME: This is adhoc, and we should return rect of empty fragment instead.
  211. auto containing_block_position_in_absolute_coordinates = containing_block()->absolute_position();
  212. return { containing_block_position_in_absolute_coordinates, { 0, 0 } };
  213. }
  214. return bounding_rect;
  215. }
  216. }