InlineNode.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2018-2020, 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::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  26. {
  27. auto& containing_block = context.containing_block();
  28. if (!computed_values().padding().left.is_undefined_or_auto()) {
  29. float padding_left = computed_values().padding().left.resolved(CSS::Length::make_px(0), *this, containing_block.width()).to_px(*this);
  30. containing_block.ensure_last_line_box().add_fragment(*this, 0, 0, padding_left, 0, LineBoxFragment::Type::Leading);
  31. }
  32. NodeWithStyleAndBoxModelMetrics::split_into_lines(context, layout_mode);
  33. if (!computed_values().padding().right.is_undefined_or_auto()) {
  34. float padding_right = computed_values().padding().right.resolved(CSS::Length::make_px(0), *this, containing_block.width()).to_px(*this);
  35. containing_block.ensure_last_line_box().add_fragment(*this, 0, 0, padding_right, 0, LineBoxFragment::Type::Trailing);
  36. }
  37. }
  38. void InlineNode::paint(PaintContext& context, PaintPhase phase)
  39. {
  40. auto& painter = context.painter();
  41. if (phase == PaintPhase::Background) {
  42. auto top_left_border_radius = computed_values().border_top_left_radius();
  43. auto top_right_border_radius = computed_values().border_top_right_radius();
  44. auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
  45. auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
  46. for_each_fragment([&](auto& fragment) {
  47. // FIXME: This recalculates our (InlineNode's) absolute_rect() for every single fragment!
  48. auto rect = fragment.absolute_rect();
  49. auto border_radius_data = Painting::normalized_border_radius_data(*this, rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
  50. Painting::paint_background(context, *this, enclosing_int_rect(rect), computed_values().background_color(), &computed_values().background_layers(), border_radius_data);
  51. if (auto computed_box_shadow = computed_values().box_shadow(); computed_box_shadow.has_value()) {
  52. auto box_shadow_data = Painting::BoxShadowData {
  53. .offset_x = (int)computed_box_shadow->offset_x.resolved_or_zero(*this, rect.width()).to_px(*this),
  54. .offset_y = (int)computed_box_shadow->offset_y.resolved_or_zero(*this, rect.height()).to_px(*this),
  55. .blur_radius = (int)computed_box_shadow->blur_radius.resolved_or_zero(*this, rect.width()).to_px(*this),
  56. .color = computed_box_shadow->color
  57. };
  58. Painting::paint_box_shadow(context, enclosing_int_rect(rect), box_shadow_data);
  59. }
  60. return IterationDecision::Continue;
  61. });
  62. }
  63. if (phase == PaintPhase::Border) {
  64. auto top_left_border_radius = computed_values().border_top_left_radius();
  65. auto top_right_border_radius = computed_values().border_top_right_radius();
  66. auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
  67. auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
  68. auto borders_data = Painting::BordersData {
  69. .top = computed_values().border_top(),
  70. .right = computed_values().border_right(),
  71. .bottom = computed_values().border_bottom(),
  72. .left = computed_values().border_left(),
  73. };
  74. for_each_fragment([&](auto& fragment) {
  75. // FIXME: This recalculates our (InlineNode's) absolute_rect() for every single fragment!
  76. auto bordered_rect = fragment.absolute_rect();
  77. bordered_rect.inflate(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
  78. 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);
  79. Painting::paint_all_borders(context, bordered_rect, border_radius_data, borders_data);
  80. return IterationDecision::Continue;
  81. });
  82. }
  83. if (phase == PaintPhase::Foreground && document().inspected_node() == dom_node()) {
  84. // FIXME: This paints a double-thick border between adjacent fragments, where ideally there
  85. // would be none. Once we implement non-rectangular outlines for the `outline` CSS
  86. // property, we can use that here instead.
  87. for_each_fragment([&](auto& fragment) {
  88. painter.draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
  89. return IterationDecision::Continue;
  90. });
  91. }
  92. }
  93. template<typename Callback>
  94. void InlineNode::for_each_fragment(Callback callback)
  95. {
  96. // FIXME: This will be slow if the containing block has a lot of fragments!
  97. containing_block()->for_each_fragment([&](auto& fragment) {
  98. if (!is_inclusive_ancestor_of(fragment.layout_node()))
  99. return IterationDecision::Continue;
  100. // FIXME: This skips the 0-width fragments at the start and end of the InlineNode.
  101. // A better solution would be to not generate them in the first place.
  102. if (fragment.width() == 0 || fragment.height() == 0)
  103. return IterationDecision::Continue;
  104. return callback(fragment);
  105. });
  106. }
  107. }