LayoutNode.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Painter.h>
  27. #include <LibHTML/DOM/Document.h>
  28. #include <LibHTML/DOM/Element.h>
  29. #include <LibHTML/Frame.h>
  30. #include <LibHTML/Layout/LayoutBlock.h>
  31. #include <LibHTML/Layout/LayoutNode.h>
  32. LayoutNode::LayoutNode(const Node* node)
  33. : m_node(node)
  34. {
  35. if (m_node)
  36. m_node->set_layout_node({}, this);
  37. }
  38. LayoutNode::~LayoutNode()
  39. {
  40. if (m_node && m_node->layout_node() == this)
  41. m_node->set_layout_node({}, nullptr);
  42. }
  43. void LayoutNode::layout()
  44. {
  45. for_each_child([](auto& child) {
  46. child.layout();
  47. });
  48. }
  49. const LayoutBlock* LayoutNode::containing_block() const
  50. {
  51. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  52. if (is<LayoutBlock>(*ancestor))
  53. return to<LayoutBlock>(ancestor);
  54. }
  55. return nullptr;
  56. }
  57. void LayoutNode::render(RenderingContext& context)
  58. {
  59. if (!is_visible())
  60. return;
  61. // TODO: render our border
  62. for_each_child([&](auto& child) {
  63. child.render(context);
  64. });
  65. }
  66. HitTestResult LayoutNode::hit_test(const Gfx::Point& position) const
  67. {
  68. HitTestResult result;
  69. for_each_child([&](auto& child) {
  70. auto child_result = child.hit_test(position);
  71. if (child_result.layout_node)
  72. result = child_result;
  73. });
  74. return result;
  75. }
  76. const Document& LayoutNode::document() const
  77. {
  78. if (is_anonymous())
  79. return parent()->document();
  80. return node()->document();
  81. }
  82. Document& LayoutNode::document()
  83. {
  84. if (is_anonymous())
  85. return parent()->document();
  86. // FIXME: Remove this const_cast once we give up on the idea of a const link from layout tree to DOM tree.
  87. return const_cast<Node*>(node())->document();
  88. }
  89. const LayoutDocument& LayoutNode::root() const
  90. {
  91. ASSERT(document().layout_node());
  92. return *document().layout_node();
  93. }
  94. LayoutDocument& LayoutNode::root()
  95. {
  96. ASSERT(document().layout_node());
  97. return *document().layout_node();
  98. }
  99. void LayoutNode::split_into_lines(LayoutBlock& container)
  100. {
  101. for_each_child([&](auto& child) {
  102. if (child.is_inline()) {
  103. child.split_into_lines(container);
  104. } else {
  105. // FIXME: Support block children of inlines.
  106. }
  107. });
  108. }
  109. void LayoutNode::set_needs_display()
  110. {
  111. auto* frame = document().frame();
  112. ASSERT(frame);
  113. if (auto* block = containing_block()) {
  114. block->for_each_fragment([&](auto& fragment) {
  115. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  116. const_cast<Frame*>(frame)->set_needs_display(enclosing_int_rect(fragment.rect()));
  117. }
  118. return IterationDecision::Continue;
  119. });
  120. }
  121. }
  122. Gfx::FloatPoint LayoutNode::box_type_agnostic_position() const
  123. {
  124. if (is_box())
  125. return to<LayoutBox>(*this).position();
  126. ASSERT(is_inline());
  127. Gfx::FloatPoint position;
  128. if (auto* block = containing_block()) {
  129. block->for_each_fragment([&](auto& fragment) {
  130. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  131. position = fragment.rect().location();
  132. return IterationDecision::Break;
  133. }
  134. return IterationDecision::Continue;
  135. });
  136. }
  137. return position;
  138. }