InlineNode.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/BlockBox.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.context_box();
  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 background_data = Painting::BackgroundData {
  43. .color = computed_values().background_color(),
  44. .image = background_image() ? background_image()->bitmap() : nullptr,
  45. .repeat_x = computed_values().background_repeat_x(),
  46. .repeat_y = computed_values().background_repeat_y()
  47. };
  48. auto top_left_border_radius = computed_values().border_top_left_radius();
  49. auto top_right_border_radius = computed_values().border_top_right_radius();
  50. auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
  51. auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
  52. for_each_fragment([&](auto& fragment) {
  53. // FIXME: This recalculates our (InlineNode's) absolute_rect() for every single fragment!
  54. auto rect = fragment.absolute_rect();
  55. 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);
  56. Painting::paint_background(context, enclosing_int_rect(rect), background_data, border_radius_data);
  57. if (auto computed_box_shadow = computed_values().box_shadow(); computed_box_shadow.has_value()) {
  58. auto box_shadow_data = Painting::BoxShadowData {
  59. .offset_x = (int)computed_box_shadow->offset_x.resolved_or_zero(*this, rect.width()).to_px(*this),
  60. .offset_y = (int)computed_box_shadow->offset_y.resolved_or_zero(*this, rect.height()).to_px(*this),
  61. .blur_radius = (int)computed_box_shadow->blur_radius.resolved_or_zero(*this, rect.width()).to_px(*this),
  62. .color = computed_box_shadow->color
  63. };
  64. Painting::paint_box_shadow(context, enclosing_int_rect(rect), box_shadow_data);
  65. }
  66. return IterationDecision::Continue;
  67. });
  68. }
  69. if (phase == PaintPhase::Border) {
  70. auto top_left_border_radius = computed_values().border_top_left_radius();
  71. auto top_right_border_radius = computed_values().border_top_right_radius();
  72. auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
  73. auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
  74. auto borders_data = Painting::BordersData {
  75. .top = computed_values().border_top(),
  76. .right = computed_values().border_right(),
  77. .bottom = computed_values().border_bottom(),
  78. .left = computed_values().border_left(),
  79. };
  80. for_each_fragment([&](auto& fragment) {
  81. // FIXME: This recalculates our (InlineNode's) absolute_rect() for every single fragment!
  82. auto bordered_rect = fragment.absolute_rect();
  83. bordered_rect.inflate(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
  84. 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);
  85. Painting::paint_all_borders(context, bordered_rect, border_radius_data, borders_data);
  86. return IterationDecision::Continue;
  87. });
  88. }
  89. if (phase == PaintPhase::Foreground && document().inspected_node() == dom_node()) {
  90. // FIXME: This paints a double-thick border between adjacent fragments, where ideally there
  91. // would be none. Once we implement non-rectangular outlines for the `outline` CSS
  92. // property, we can use that here instead.
  93. for_each_fragment([&](auto& fragment) {
  94. painter.draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
  95. return IterationDecision::Continue;
  96. });
  97. }
  98. }
  99. template<typename Callback>
  100. void InlineNode::for_each_fragment(Callback callback)
  101. {
  102. // FIXME: This will be slow if the containing block has a lot of fragments!
  103. containing_block()->for_each_fragment([&](auto& fragment) {
  104. if (!is_inclusive_ancestor_of(fragment.layout_node()))
  105. return IterationDecision::Continue;
  106. // FIXME: This skips the 0-width fragments at the start and end of the InlineNode.
  107. // A better solution would be to not generate them in the first place.
  108. if (fragment.width() == 0 || fragment.height() == 0)
  109. return IterationDecision::Continue;
  110. return callback(fragment);
  111. });
  112. }
  113. }