InlineNode.cpp 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. namespace Web::Layout {
  16. InlineNode::InlineNode(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  17. : Layout::NodeWithStyleAndBoxModelMetrics(document, &element, move(style))
  18. {
  19. set_inline(true);
  20. }
  21. InlineNode::~InlineNode()
  22. {
  23. }
  24. void InlineNode::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  25. {
  26. auto& containing_block = context.context_box();
  27. if (!computed_values().padding().left.is_undefined_or_auto()) {
  28. float padding_left = computed_values().padding().left.resolved(CSS::Length::make_px(0), *this, containing_block.width()).to_px(*this);
  29. containing_block.ensure_last_line_box().add_fragment(*this, 0, 0, padding_left, 0, LineBoxFragment::Type::Leading);
  30. }
  31. NodeWithStyleAndBoxModelMetrics::split_into_lines(context, layout_mode);
  32. if (!computed_values().padding().right.is_undefined_or_auto()) {
  33. float padding_right = computed_values().padding().right.resolved(CSS::Length::make_px(0), *this, containing_block.width()).to_px(*this);
  34. containing_block.ensure_last_line_box().add_fragment(*this, 0, 0, padding_right, 0, LineBoxFragment::Type::Trailing);
  35. }
  36. }
  37. void InlineNode::paint(PaintContext& context, PaintPhase phase)
  38. {
  39. auto& painter = context.painter();
  40. if (phase == PaintPhase::Background) {
  41. auto background_data = Painting::BackgroundData {
  42. .color = computed_values().background_color(),
  43. .image = background_image() ? background_image()->bitmap() : nullptr,
  44. .repeat_x = computed_values().background_repeat_x(),
  45. .repeat_y = computed_values().background_repeat_y()
  46. };
  47. auto top_left_border_radius = computed_values().border_top_left_radius();
  48. auto top_right_border_radius = computed_values().border_top_right_radius();
  49. auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
  50. auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
  51. for_each_fragment([&](auto& fragment) {
  52. // FIXME: This recalculates our (InlineNode's) absolute_rect() for every single fragment!
  53. auto rect = fragment.absolute_rect();
  54. 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);
  55. Painting::paint_background(context, enclosing_int_rect(rect), background_data, border_radius_data);
  56. return IterationDecision::Continue;
  57. });
  58. }
  59. if (phase == PaintPhase::Foreground && document().inspected_node() == dom_node()) {
  60. // FIXME: This paints a double-thick border between adjacent fragments, where ideally there
  61. // would be none. Once we implement non-rectangular outlines for the `outline` CSS
  62. // property, we can use that here instead.
  63. for_each_fragment([&](auto& fragment) {
  64. painter.draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
  65. return IterationDecision::Continue;
  66. });
  67. }
  68. }
  69. template<typename Callback>
  70. void InlineNode::for_each_fragment(Callback callback)
  71. {
  72. // FIXME: This will be slow if the containing block has a lot of fragments!
  73. containing_block()->for_each_fragment([&](auto& fragment) {
  74. if (!is_inclusive_ancestor_of(fragment.layout_node()))
  75. return IterationDecision::Continue;
  76. // FIXME: This skips the 0-width fragments at the start and end of the InlineNode.
  77. // A better solution would be to not generate them in the first place.
  78. if (fragment.width() == 0 || fragment.height() == 0)
  79. return IterationDecision::Continue;
  80. return callback(fragment);
  81. });
  82. }
  83. }