LayoutEngine.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "LayoutEngine.h"
  29. #include "../../../Include/RmlUi/Core/Element.h"
  30. #include "../../../Include/RmlUi/Core/ElementUtilities.h"
  31. #include "../../../Include/RmlUi/Core/Log.h"
  32. #include "../../../Include/RmlUi/Core/Profiling.h"
  33. #include "ContainerBox.h"
  34. #include "FormattingContext.h"
  35. #include "FormattingContextDebug.h"
  36. #include "LayoutDetails.h"
  37. #include "LayoutNode.h"
  38. namespace Rml {
  39. static void FormatElementImpl(Element* element, Vector2f containing_block, Vector2f absolutely_positioning_containing_block,
  40. const FormattingMode& formatting_mode)
  41. {
  42. RootBox absolute_root(Box(absolutely_positioning_containing_block), formatting_mode);
  43. RootBox root(Box(containing_block), &absolute_root);
  44. UniquePtr<LayoutBox> layout_box = FormattingContext::FormatIndependent(&root, element, nullptr, FormattingContextType::Block);
  45. if (!layout_box)
  46. {
  47. Log::Message(Log::LT_ERROR, "Error while formatting element: %s", element->GetAddress().c_str());
  48. }
  49. if (int num_absolute_boxes = absolute_root.CountAbsolutelyPositionedBoxes(); num_absolute_boxes != 0)
  50. {
  51. // In the end, this might be fine, but needs further investigation.
  52. Log::Message(Log::LT_ERROR, "%d absolutely positioned box(es) not closed, while formatting: %s", num_absolute_boxes,
  53. element->GetAddress().c_str());
  54. }
  55. }
  56. void LayoutEngine::FormatElement(Element* layout_root, Vector2f containing_block, bool allow_cache)
  57. {
  58. RMLUI_ZoneScoped;
  59. RMLUI_ASSERT(layout_root && containing_block.x >= 0 && containing_block.y >= 0);
  60. const FormattingMode formatting_mode{FormattingMode::Constraint::None, allow_cache, allow_cache};
  61. constexpr bool debug_logging = false;
  62. if (debug_logging)
  63. {
  64. Log::Message(Log::LT_INFO, "UpdateLayout start: %s", layout_root->GetAddress().c_str());
  65. DebugLogDirtyLayoutTree(layout_root);
  66. }
  67. struct ElementToFormat {
  68. Element* element;
  69. Vector2f containing_block_size;
  70. Vector2f absolutely_positioning_containing_block_size;
  71. };
  72. Vector<ElementToFormat> elements;
  73. bool force_full_document_layout = !allow_cache;
  74. if (!force_full_document_layout)
  75. {
  76. RMLUI_ZoneScopedNC("Search dirty elements", 0x66AACC);
  77. ElementUtilities::BreadthFirstSearch(layout_root, [&](Element* candidate) {
  78. if (candidate->GetDisplay() == Style::Display::None)
  79. return ElementUtilities::CallbackControlFlow::SkipChildren;
  80. LayoutNode* layout_node = candidate->GetLayoutNode();
  81. if (!layout_node->IsDirty())
  82. return ElementUtilities::CallbackControlFlow::Continue;
  83. if (!layout_node->IsLayoutBoundary())
  84. {
  85. // Normally the dirty layout should have propagated to the closest layout boundary during
  86. // Element::Update(), which then invokes formatting on that boundary element, and which again clears the
  87. // dirty layout on this element. This didn't happen here, which may be caused by modifying the layout
  88. // during layouting itself. For now, we simply skip this element and keep going. The element should
  89. // normally be properly layed-out on the next context update.
  90. // TODO: Might want to investigate this further.
  91. if (debug_logging)
  92. Log::Message(Log::LT_INFO, "Dirty layout on non-layout boundary element: %s", candidate->GetAddress().c_str());
  93. return ElementUtilities::CallbackControlFlow::Continue;
  94. }
  95. const Optional<CommittedLayout>& committed_layout = layout_node->GetCommittedLayout();
  96. if (!committed_layout)
  97. {
  98. if (candidate != layout_root && debug_logging)
  99. Log::Message(Log::LT_INFO, "Forcing full layout update due to missing committed layout on element: %s",
  100. candidate->GetAddress().c_str());
  101. force_full_document_layout = true;
  102. return ElementUtilities::CallbackControlFlow::Break;
  103. }
  104. elements.push_back(ElementToFormat{
  105. candidate,
  106. committed_layout->containing_block_size,
  107. committed_layout->absolutely_positioning_containing_block_size,
  108. });
  109. return ElementUtilities::CallbackControlFlow::SkipChildren;
  110. });
  111. }
  112. if (force_full_document_layout)
  113. {
  114. elements = {
  115. ElementToFormat{
  116. layout_root,
  117. containing_block,
  118. containing_block,
  119. },
  120. };
  121. }
  122. for (const ElementToFormat& element_to_format : elements)
  123. {
  124. if (element_to_format.element != layout_root && debug_logging)
  125. Log::Message(Log::LT_INFO, "Doing partial layout update on element: %s", element_to_format.element->GetAddress().c_str());
  126. // TODO: In some cases, we need to check if size changed, such that we need to do a layout update in its parent.
  127. FormatElementImpl(element_to_format.element, element_to_format.containing_block_size,
  128. element_to_format.absolutely_positioning_containing_block_size, formatting_mode);
  129. // TODO: A bit ugly
  130. element_to_format.element->UpdateRelativeOffsetFromInsetConstraints();
  131. }
  132. if (elements.empty() && debug_logging)
  133. Log::Message(Log::LT_INFO, "Didn't make any layout updates on dirty document: %s", layout_root->GetAddress().c_str());
  134. if (debug_logging)
  135. Log::Message(Log::LT_INFO, "Document layout: Clear dirty on %s", layout_root->GetAddress().c_str());
  136. {
  137. RMLUI_ZoneScopedN("CommitLayoutRecursive");
  138. layout_root->CommitLayoutRecursive();
  139. }
  140. }
  141. } // namespace Rml