InlinePaintable.cpp 12 KB

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