InlinePaintable.cpp 11 KB

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