InlineNode.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.has_value()) {
  39. auto box_shadow_data = Painting::BoxShadowData {
  40. .offset_x = (int)computed_box_shadow->offset_x.resolved_or_zero(*this).to_px(*this),
  41. .offset_y = (int)computed_box_shadow->offset_y.resolved_or_zero(*this).to_px(*this),
  42. .blur_radius = (int)computed_box_shadow->blur_radius.resolved_or_zero(*this).to_px(*this),
  43. .color = computed_box_shadow->color
  44. };
  45. Painting::paint_box_shadow(context, enclosing_int_rect(absolute_fragment_rect), box_shadow_data);
  46. }
  47. return IterationDecision::Continue;
  48. });
  49. }
  50. if (phase == PaintPhase::Border) {
  51. auto top_left_border_radius = computed_values().border_top_left_radius();
  52. auto top_right_border_radius = computed_values().border_top_right_radius();
  53. auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
  54. auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
  55. auto borders_data = Painting::BordersData {
  56. .top = computed_values().border_top(),
  57. .right = computed_values().border_right(),
  58. .bottom = computed_values().border_bottom(),
  59. .left = computed_values().border_left(),
  60. };
  61. auto containing_block_position_in_absolute_coordinates = containing_block()->absolute_position();
  62. for_each_fragment([&](auto& fragment) {
  63. Gfx::FloatRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
  64. auto bordered_rect = absolute_fragment_rect.inflated(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
  65. 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);
  66. Painting::paint_all_borders(context, bordered_rect, border_radius_data, borders_data);
  67. return IterationDecision::Continue;
  68. });
  69. }
  70. if (phase == PaintPhase::Foreground && document().inspected_node() == dom_node()) {
  71. // FIXME: This paints a double-thick border between adjacent fragments, where ideally there
  72. // would be none. Once we implement non-rectangular outlines for the `outline` CSS
  73. // property, we can use that here instead.
  74. for_each_fragment([&](auto& fragment) {
  75. painter.draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
  76. return IterationDecision::Continue;
  77. });
  78. }
  79. }
  80. template<typename Callback>
  81. void InlineNode::for_each_fragment(Callback callback)
  82. {
  83. // FIXME: This will be slow if the containing block has a lot of fragments!
  84. containing_block()->for_each_fragment([&](auto& fragment) {
  85. if (!is_inclusive_ancestor_of(fragment.layout_node()))
  86. return IterationDecision::Continue;
  87. return callback(fragment);
  88. });
  89. }
  90. }