InlineNode.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGfx/Painter.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Element.h>
  10. #include <LibWeb/Layout/BlockContainer.h>
  11. #include <LibWeb/Layout/InlineFormattingContext.h>
  12. #include <LibWeb/Layout/InlineNode.h>
  13. #include <LibWeb/Painting/BackgroundPainting.h>
  14. #include <LibWeb/Painting/BorderPainting.h>
  15. #include <LibWeb/Painting/ShadowPainting.h>
  16. namespace Web::Layout {
  17. InlineNode::InlineNode(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  18. : Layout::NodeWithStyleAndBoxModelMetrics(document, &element, move(style))
  19. {
  20. set_inline(true);
  21. }
  22. InlineNode::~InlineNode()
  23. {
  24. }
  25. void InlineNode::paint(PaintContext& context, PaintPhase phase)
  26. {
  27. auto& painter = context.painter();
  28. if (phase == PaintPhase::Background) {
  29. auto top_left_border_radius = computed_values().border_top_left_radius();
  30. auto top_right_border_radius = computed_values().border_top_right_radius();
  31. auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
  32. auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
  33. auto containing_block_position_in_absolute_coordinates = containing_block()->absolute_position();
  34. for_each_fragment([&](auto const& fragment) {
  35. Gfx::FloatRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
  36. auto border_radius_data = Painting::normalized_border_radius_data(*this, absolute_fragment_rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
  37. Painting::paint_background(context, *this, enclosing_int_rect(absolute_fragment_rect), computed_values().background_color(), &computed_values().background_layers(), border_radius_data);
  38. if (auto computed_box_shadow = computed_values().box_shadow(); !computed_box_shadow.is_empty()) {
  39. Vector<Painting::BoxShadowData> resolved_box_shadow_data;
  40. resolved_box_shadow_data.ensure_capacity(computed_box_shadow.size());
  41. for (auto const& layer : computed_box_shadow) {
  42. resolved_box_shadow_data.empend(
  43. layer.color,
  44. static_cast<int>(layer.offset_x.resolved_or_zero(*this).to_px(*this)),
  45. static_cast<int>(layer.offset_y.resolved_or_zero(*this).to_px(*this)),
  46. static_cast<int>(layer.blur_radius.resolved_or_zero(*this).to_px(*this)),
  47. static_cast<int>(layer.spread_distance.resolved_or_zero(*this).to_px(*this)),
  48. layer.placement == CSS::BoxShadowPlacement::Outer ? Painting::BoxShadowPlacement::Outer : Painting::BoxShadowPlacement::Inner);
  49. }
  50. Painting::paint_box_shadow(context, enclosing_int_rect(absolute_fragment_rect), resolved_box_shadow_data);
  51. }
  52. return IterationDecision::Continue;
  53. });
  54. }
  55. if (phase == PaintPhase::Border) {
  56. auto top_left_border_radius = computed_values().border_top_left_radius();
  57. auto top_right_border_radius = computed_values().border_top_right_radius();
  58. auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
  59. auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
  60. auto borders_data = Painting::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 containing_block_position_in_absolute_coordinates = containing_block()->absolute_position();
  67. for_each_fragment([&](auto& fragment) {
  68. Gfx::FloatRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
  69. auto bordered_rect = absolute_fragment_rect.inflated(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
  70. auto border_radius_data = Painting::normalized_border_radius_data(*this, bordered_rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
  71. Painting::paint_all_borders(context, bordered_rect, border_radius_data, borders_data);
  72. return IterationDecision::Continue;
  73. });
  74. }
  75. if (phase == PaintPhase::Foreground && document().inspected_node() == dom_node()) {
  76. // FIXME: This paints a double-thick border between adjacent fragments, where ideally there
  77. // would be none. Once we implement non-rectangular outlines for the `outline` CSS
  78. // property, we can use that here instead.
  79. for_each_fragment([&](auto& fragment) {
  80. painter.draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
  81. return IterationDecision::Continue;
  82. });
  83. }
  84. }
  85. template<typename Callback>
  86. void InlineNode::for_each_fragment(Callback callback)
  87. {
  88. // FIXME: This will be slow if the containing block has a lot of fragments!
  89. containing_block()->for_each_fragment([&](auto& fragment) {
  90. if (!is_inclusive_ancestor_of(fragment.layout_node()))
  91. return IterationDecision::Continue;
  92. return callback(fragment);
  93. });
  94. }
  95. }