InlineNode.cpp 6.3 KB

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