InlinePaintable.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. NonnullRefPtr<InlinePaintable> InlinePaintable::create(Layout::InlineNode const& layout_node)
  15. {
  16. return adopt_ref(*new 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, Painting::PaintPhase phase) const
  27. {
  28. auto& painter = context.painter();
  29. if (phase == Painting::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()->paint_box()->absolute_position();
  35. for_each_fragment([&](auto const& fragment, bool is_first_fragment, bool is_last_fragment) {
  36. Gfx::FloatRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
  37. if (is_first_fragment) {
  38. float 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. float 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 = Painting::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. Painting::paint_background(context, layout_node(), absolute_fragment_rect, computed_values().background_color(), &computed_values().background_layers(), border_radii_data);
  48. if (auto computed_box_shadow = computed_values().box_shadow(); !computed_box_shadow.is_empty()) {
  49. Vector<Painting::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. static_cast<int>(layer.offset_x.to_px(layout_node())),
  55. static_cast<int>(layer.offset_y.to_px(layout_node())),
  56. static_cast<int>(layer.blur_radius.to_px(layout_node())),
  57. static_cast<int>(layer.spread_distance.to_px(layout_node())),
  58. layer.placement == CSS::ShadowPlacement::Outer ? Painting::ShadowPlacement::Outer : Painting::ShadowPlacement::Inner);
  59. }
  60. Painting::paint_box_shadow(context, enclosing_int_rect(absolute_fragment_rect), resolved_box_shadow_data);
  61. }
  62. return IterationDecision::Continue;
  63. });
  64. }
  65. if (phase == Painting::PaintPhase::Border) {
  66. auto top_left_border_radius = computed_values().border_top_left_radius();
  67. auto top_right_border_radius = computed_values().border_top_right_radius();
  68. auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
  69. auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
  70. auto borders_data = Painting::BordersData {
  71. .top = computed_values().border_top(),
  72. .right = computed_values().border_right(),
  73. .bottom = computed_values().border_bottom(),
  74. .left = computed_values().border_left(),
  75. };
  76. auto containing_block_position_in_absolute_coordinates = containing_block()->paint_box()->absolute_position();
  77. for_each_fragment([&](auto const& fragment, bool is_first_fragment, bool is_last_fragment) {
  78. Gfx::FloatRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
  79. if (is_first_fragment) {
  80. float extra_start_width = box_model().padding.left;
  81. absolute_fragment_rect.translate_by(-extra_start_width, 0);
  82. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
  83. }
  84. if (is_last_fragment) {
  85. float extra_end_width = box_model().padding.right;
  86. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
  87. }
  88. auto bordered_rect = absolute_fragment_rect.inflated(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
  89. auto border_radii_data = Painting::normalized_border_radii_data(layout_node(), bordered_rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius, Painting::RelativeToWidthOnly::Yes);
  90. Painting::paint_all_borders(context, bordered_rect, border_radii_data, borders_data);
  91. return IterationDecision::Continue;
  92. });
  93. }
  94. // FIXME: We check for a non-null dom_node(), since pseudo-elements have a null one and were getting
  95. // highlighted incorrectly. A better solution will be needed if we want to inspect them too.
  96. if (phase == Painting::PaintPhase::Overlay && layout_node().dom_node() && layout_node().document().inspected_node() == layout_node().dom_node()) {
  97. // FIXME: This paints a double-thick border between adjacent fragments, where ideally there
  98. // would be none. Once we implement non-rectangular outlines for the `outline` CSS
  99. // property, we can use that here instead.
  100. for_each_fragment([&](auto const& fragment, bool, bool) {
  101. painter.draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
  102. return IterationDecision::Continue;
  103. });
  104. }
  105. }
  106. template<typename Callback>
  107. void InlinePaintable::for_each_fragment(Callback callback) const
  108. {
  109. // FIXME: This will be slow if the containing block has a lot of fragments!
  110. Vector<Layout::LineBoxFragment const&> fragments;
  111. containing_block()->paint_box()->for_each_fragment([&](auto& fragment) {
  112. if (layout_node().is_inclusive_ancestor_of(fragment.layout_node()))
  113. fragments.append(fragment);
  114. return IterationDecision::Continue;
  115. });
  116. for (size_t i = 0; i < fragments.size(); ++i) {
  117. auto const& fragment = fragments[i];
  118. callback(fragment, i == 0, i == fragments.size() - 1);
  119. }
  120. }
  121. }