InlinePaintable.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/Layout/ImageBox.h>
  10. #include <LibWeb/Painting/BackgroundPainting.h>
  11. #include <LibWeb/Painting/InlinePaintable.h>
  12. #include <LibWeb/Painting/ShadowPainting.h>
  13. namespace Web::Painting {
  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::paint(PaintContext& context, PaintPhase phase) const
  27. {
  28. auto& painter = context.painter();
  29. if (phase == PaintPhase::Background) {
  30. auto top_left_border_radius = computed_values().border_top_left_radius();
  31. auto top_right_border_radius = computed_values().border_top_right_radius();
  32. auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
  33. auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
  34. auto containing_block_position_in_absolute_coordinates = containing_block()->paintable_box()->absolute_position();
  35. for_each_fragment([&](auto const& fragment, bool is_first_fragment, bool is_last_fragment) {
  36. CSSPixelRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
  37. if (is_first_fragment) {
  38. auto extra_start_width = box_model().padding.left;
  39. absolute_fragment_rect.translate_by(-extra_start_width, 0);
  40. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
  41. }
  42. if (is_last_fragment) {
  43. auto extra_end_width = box_model().padding.right;
  44. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
  45. }
  46. auto border_radii_data = normalized_border_radii_data(layout_node(), absolute_fragment_rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
  47. paint_background(context, layout_node(), absolute_fragment_rect, computed_values().background_color(), computed_values().image_rendering(), &computed_values().background_layers(), border_radii_data);
  48. if (auto computed_box_shadow = computed_values().box_shadow(); !computed_box_shadow.is_empty()) {
  49. Vector<ShadowData> resolved_box_shadow_data;
  50. resolved_box_shadow_data.ensure_capacity(computed_box_shadow.size());
  51. for (auto const& layer : computed_box_shadow) {
  52. resolved_box_shadow_data.empend(
  53. layer.color,
  54. layer.offset_x.to_px(layout_node()),
  55. layer.offset_y.to_px(layout_node()),
  56. layer.blur_radius.to_px(layout_node()),
  57. layer.spread_distance.to_px(layout_node()),
  58. layer.placement == CSS::ShadowPlacement::Outer ? ShadowPlacement::Outer : ShadowPlacement::Inner);
  59. }
  60. auto borders_data = BordersData {
  61. .top = computed_values().border_top(),
  62. .right = computed_values().border_right(),
  63. .bottom = computed_values().border_bottom(),
  64. .left = computed_values().border_left(),
  65. };
  66. auto absolute_fragment_rect_bordered = absolute_fragment_rect.inflated(
  67. borders_data.top.width, borders_data.right.width,
  68. borders_data.bottom.width, borders_data.left.width);
  69. paint_box_shadow(context, absolute_fragment_rect_bordered, absolute_fragment_rect,
  70. borders_data, border_radii_data, resolved_box_shadow_data);
  71. }
  72. return IterationDecision::Continue;
  73. });
  74. }
  75. auto paint_border_or_outline = [&](Optional<BordersData> outline_data = {}, CSSPixels outline_offset = 0) {
  76. auto top_left_border_radius = computed_values().border_top_left_radius();
  77. auto top_right_border_radius = computed_values().border_top_right_radius();
  78. auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
  79. auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
  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 containing_block_position_in_absolute_coordinates = containing_block()->paintable_box()->absolute_position();
  87. for_each_fragment([&](auto const& fragment, bool is_first_fragment, bool is_last_fragment) {
  88. CSSPixelRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
  89. if (is_first_fragment) {
  90. auto extra_start_width = box_model().padding.left;
  91. absolute_fragment_rect.translate_by(-extra_start_width, 0);
  92. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
  93. }
  94. if (is_last_fragment) {
  95. auto extra_end_width = box_model().padding.right;
  96. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
  97. }
  98. auto borders_rect = absolute_fragment_rect.inflated(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
  99. auto border_radii_data = normalized_border_radii_data(layout_node(), borders_rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
  100. if (outline_data.has_value()) {
  101. auto outline_offset_x = outline_offset;
  102. auto outline_offset_y = outline_offset;
  103. // "Both the height and the width of the outside of the shape drawn by the outline should not
  104. // become smaller than twice the computed value of the outline-width property to make sure
  105. // that an outline can be rendered even with large negative values."
  106. // https://www.w3.org/TR/css-ui-4/#outline-offset
  107. // So, if the horizontal outline offset is > half the borders_rect's width then we set it to that.
  108. // (And the same for y)
  109. if ((borders_rect.width() / 2) + outline_offset_x < 0)
  110. outline_offset_x = -borders_rect.width() / 2;
  111. if ((borders_rect.height() / 2) + outline_offset_y < 0)
  112. outline_offset_y = -borders_rect.height() / 2;
  113. 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);
  114. 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);
  115. paint_all_borders(context.painter(), context.rounded_device_rect(borders_rect), border_radii_data.as_corners(context), outline_data->to_device_pixels(context));
  116. } else {
  117. paint_all_borders(context.painter(), context.rounded_device_rect(borders_rect), border_radii_data.as_corners(context), borders_data.to_device_pixels(context));
  118. }
  119. return IterationDecision::Continue;
  120. });
  121. };
  122. if (phase == PaintPhase::Border) {
  123. paint_border_or_outline();
  124. }
  125. if (phase == PaintPhase::Outline) {
  126. auto outline_width = computed_values().outline_width().to_px(layout_node());
  127. auto maybe_outline_data = borders_data_for_outline(layout_node(), computed_values().outline_color(), computed_values().outline_style(), outline_width);
  128. if (maybe_outline_data.has_value()) {
  129. paint_border_or_outline(maybe_outline_data.value(), computed_values().outline_offset().to_px(layout_node()));
  130. }
  131. }
  132. if (phase == PaintPhase::Overlay && layout_node().document().inspected_layout_node() == &layout_node()) {
  133. // FIXME: This paints a double-thick border between adjacent fragments, where ideally there
  134. // would be none. Once we implement non-rectangular outlines for the `outline` CSS
  135. // property, we can use that here instead.
  136. for_each_fragment([&](auto const& fragment, bool, bool) {
  137. painter.draw_rect(context.enclosing_device_rect(fragment.absolute_rect()).template to_type<int>(), Color::Magenta);
  138. return IterationDecision::Continue;
  139. });
  140. }
  141. }
  142. template<typename Callback>
  143. void InlinePaintable::for_each_fragment(Callback callback) const
  144. {
  145. // FIXME: This will be slow if the containing block has a lot of fragments!
  146. Vector<Layout::LineBoxFragment const&> fragments;
  147. verify_cast<PaintableWithLines>(*containing_block()->paintable_box()).for_each_fragment([&](auto& fragment) {
  148. if (layout_node().is_inclusive_ancestor_of(fragment.layout_node()))
  149. fragments.append(fragment);
  150. return IterationDecision::Continue;
  151. });
  152. for (size_t i = 0; i < fragments.size(); ++i) {
  153. auto const& fragment = fragments[i];
  154. callback(fragment, i == 0, i == fragments.size() - 1);
  155. }
  156. }
  157. }