InlineNode.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Painter.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/Element.h>
  9. #include <LibWeb/Layout/BlockBox.h>
  10. #include <LibWeb/Layout/InlineFormattingContext.h>
  11. #include <LibWeb/Layout/InlineNode.h>
  12. namespace Web::Layout {
  13. InlineNode::InlineNode(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  14. : Layout::NodeWithStyleAndBoxModelMetrics(document, &element, move(style))
  15. {
  16. set_inline(true);
  17. }
  18. InlineNode::~InlineNode()
  19. {
  20. }
  21. void InlineNode::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  22. {
  23. auto& containing_block = context.context_box();
  24. if (!computed_values().padding().left.is_undefined_or_auto()) {
  25. float padding_left = computed_values().padding().left.resolved(CSS::Length::make_px(0), *this, containing_block.width()).to_px(*this);
  26. containing_block.ensure_last_line_box().add_fragment(*this, 0, 0, padding_left, 0, LineBoxFragment::Type::Leading);
  27. }
  28. NodeWithStyleAndBoxModelMetrics::split_into_lines(context, layout_mode);
  29. if (!computed_values().padding().right.is_undefined_or_auto()) {
  30. float padding_right = computed_values().padding().right.resolved(CSS::Length::make_px(0), *this, containing_block.width()).to_px(*this);
  31. containing_block.ensure_last_line_box().add_fragment(*this, 0, 0, padding_right, 0, LineBoxFragment::Type::Trailing);
  32. }
  33. }
  34. void InlineNode::paint_fragment(PaintContext& context, const LineBoxFragment& fragment, PaintPhase phase) const
  35. {
  36. auto& painter = context.painter();
  37. if (phase == PaintPhase::Background) {
  38. painter.fill_rect(enclosing_int_rect(fragment.absolute_rect()), computed_values().background_color());
  39. }
  40. }
  41. void InlineNode::paint(PaintContext& context, PaintPhase phase)
  42. {
  43. auto& painter = context.painter();
  44. if (phase == PaintPhase::Foreground && document().inspected_node() == dom_node()) {
  45. // FIXME: This paints a double-thick border between adjacent fragments, where ideally there
  46. // would be none. Once we implement non-rectangular outlines for the `outline` CSS
  47. // property, we can use that here instead.
  48. containing_block()->for_each_fragment([&](auto& fragment) {
  49. if (is_inclusive_ancestor_of(fragment.layout_node()))
  50. painter.draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
  51. return IterationDecision::Continue;
  52. });
  53. }
  54. }
  55. }