LayoutEngine.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "LayoutDetails.h"
  36. #include "LayoutNode.h"
  37. namespace Rml {
  38. static void FormatElementImpl(Element* element, Vector2f containing_block, Vector2f absolutely_positioning_containing_block,
  39. const FormattingMode& formatting_mode)
  40. {
  41. RMLUI_ZoneScoped;
  42. RootBox absolute_root(Box(absolutely_positioning_containing_block), formatting_mode);
  43. RootBox root(Box(containing_block), &absolute_root);
  44. auto 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_ASSERT(layout_root && containing_block.x >= 0 && containing_block.y >= 0);
  59. const FormattingMode formatting_mode{FormattingMode::Constraint::None, allow_cache, allow_cache};
  60. constexpr bool debug_logging = false;
  61. if (debug_logging)
  62. Log::Message(Log::LT_INFO, "UpdateLayout start: %s", layout_root->GetAddress().c_str());
  63. struct ElementToFormat {
  64. Element* element;
  65. Vector2f containing_block_size;
  66. Vector2f absolutely_positioning_containing_block_size;
  67. };
  68. Vector<ElementToFormat> elements;
  69. bool force_full_document_layout = !allow_cache;
  70. if (!force_full_document_layout)
  71. {
  72. ElementUtilities::BreadthFirstSearch(layout_root, [&](Element* candidate) {
  73. if (candidate->GetDisplay() == Style::Display::None)
  74. return ElementUtilities::CallbackControlFlow::SkipChildren;
  75. LayoutNode* layout_node = candidate->GetLayoutNode();
  76. if (!layout_node->IsDirty())
  77. return ElementUtilities::CallbackControlFlow::Continue;
  78. if (!layout_node->IsLayoutBoundary())
  79. {
  80. // Normally the dirty layout should have propagated to the closest layout boundary during
  81. // Element::Update(), which then invokes formatting on that boundary element, and which again clears the
  82. // dirty layout on this element. This didn't happen here, which may be caused by modifying the layout
  83. // during layouting itself. For now, we simply skip this element and keep going. The element should
  84. // normally be properly layed-out on the next context update.
  85. // TODO: Might want to investigate this further.
  86. if (debug_logging)
  87. Log::Message(Log::LT_INFO, "Dirty layout on non-layout boundary element: %s", candidate->GetAddress().c_str());
  88. return ElementUtilities::CallbackControlFlow::Continue;
  89. }
  90. const Optional<CommittedLayout>& committed_layout = layout_node->GetCommittedLayout();
  91. if (!committed_layout)
  92. {
  93. if (candidate != layout_root && debug_logging)
  94. Log::Message(Log::LT_INFO, "Forcing full layout update due to missing committed layout on element: %s",
  95. candidate->GetAddress().c_str());
  96. force_full_document_layout = true;
  97. return ElementUtilities::CallbackControlFlow::Break;
  98. }
  99. elements.push_back(ElementToFormat{
  100. candidate,
  101. committed_layout->containing_block_size,
  102. committed_layout->absolutely_positioning_containing_block_size,
  103. });
  104. return ElementUtilities::CallbackControlFlow::SkipChildren;
  105. });
  106. }
  107. if (force_full_document_layout)
  108. {
  109. elements = {
  110. ElementToFormat{
  111. layout_root,
  112. containing_block,
  113. containing_block,
  114. },
  115. };
  116. }
  117. for (const ElementToFormat& element_to_format : elements)
  118. {
  119. if (element_to_format.element != layout_root && debug_logging)
  120. Log::Message(Log::LT_INFO, "Doing partial layout update on element: %s", element_to_format.element->GetAddress().c_str());
  121. // TODO: In some cases, we need to check if size changed, such that we need to do a layout update in its parent.
  122. FormatElementImpl(element_to_format.element, element_to_format.containing_block_size,
  123. element_to_format.absolutely_positioning_containing_block_size, formatting_mode);
  124. // TODO: A bit ugly
  125. element_to_format.element->UpdateRelativeOffsetFromInsetConstraints();
  126. }
  127. if (elements.empty() && debug_logging)
  128. Log::Message(Log::LT_INFO, "Didn't make any layout updates on dirty document: %s", layout_root->GetAddress().c_str());
  129. if (debug_logging)
  130. Log::Message(Log::LT_INFO, "Document layout: Clear dirty on %s", layout_root->GetAddress().c_str());
  131. {
  132. RMLUI_ZoneScopedN("CommitLayoutRecursive");
  133. layout_root->CommitLayoutRecursive();
  134. }
  135. }
  136. } // namespace Rml