FormattingContextDebug.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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-2025 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 "FormattingContextDebug.h"
  29. #include "../../../Include/RmlUi/Core/Element.h"
  30. #include "../../../Include/RmlUi/Core/ElementUtilities.h"
  31. #include "../../../Include/RmlUi/Core/StringUtilities.h"
  32. #include "LayoutNode.h"
  33. namespace Rml {
  34. #ifdef RMLUI_DEBUG
  35. static FormattingContextDebugTracker* g_debug_format_independent_tracker = nullptr;
  36. FormattingContextDebugTracker::FormattingContextDebugTracker()
  37. {
  38. RMLUI_ASSERTMSG(!g_debug_format_independent_tracker, "An instance of FormattingContextDebugTracker already exists");
  39. g_debug_format_independent_tracker = this;
  40. }
  41. FormattingContextDebugTracker::~FormattingContextDebugTracker()
  42. {
  43. RMLUI_ASSERT(g_debug_format_independent_tracker == this);
  44. RMLUI_ASSERT(current_stack_level == 0);
  45. g_debug_format_independent_tracker = nullptr;
  46. }
  47. FormattingContextDebugTracker* FormattingContextDebugTracker::GetIf()
  48. {
  49. return g_debug_format_independent_tracker;
  50. }
  51. FormattingContextDebugTracker::Entry& FormattingContextDebugTracker::PushEntry(FormatType format_type, ContainerBox* parent_container,
  52. Element* element, const Box* override_initial_box, FormattingContextType type)
  53. {
  54. Entry& result = entries.emplace_back(FormattingContextDebugTracker::Entry{
  55. current_stack_level,
  56. format_type,
  57. parent_container && parent_container->GetElement() ? parent_container->GetElement()->GetAddress() : "",
  58. parent_container && parent_container->GetAbsolutePositioningContainingBlockElementForDebug()
  59. ? parent_container->GetAbsolutePositioningContainingBlockElementForDebug()->GetAddress()
  60. : "",
  61. parent_container ? parent_container->GetContainingBlockSize(Style::Position::Static) : Optional<Vector2f>{},
  62. parent_container ? parent_container->GetContainingBlockSize(Style::Position::Absolute) : Optional<Vector2f>{},
  63. element->GetAddress(),
  64. override_initial_box ? Optional<Box>{*override_initial_box} : std::nullopt,
  65. type,
  66. });
  67. current_stack_level += 1;
  68. return result;
  69. }
  70. void FormattingContextDebugTracker::CloseEntry(Entry& entry, LayoutBox* layout_box)
  71. {
  72. current_stack_level -= 1;
  73. if (layout_box)
  74. {
  75. entry.from_cache = (layout_box->GetType() == LayoutBox::Type::CachedContainer);
  76. entry.layout_result = Optional<FormattingContextDebugTracker::Entry::LayoutResult>({
  77. layout_box->GetVisibleOverflowSize(),
  78. layout_box->GetIfBox() ? Optional<Box>{*layout_box->GetIfBox()} : std::nullopt,
  79. });
  80. }
  81. }
  82. void FormattingContextDebugTracker::CloseEntry(Entry& entry, float max_content_width, bool from_cache)
  83. {
  84. current_stack_level -= 1;
  85. entry.from_cache = from_cache;
  86. entry.fit_width_result = Optional<FormattingContextDebugTracker::Entry::FitWidthResult>({
  87. max_content_width,
  88. });
  89. }
  90. void FormattingContextDebugTracker::Reset()
  91. {
  92. *this = {};
  93. }
  94. String FormattingContextDebugTracker::ToString() const
  95. {
  96. String result;
  97. for (const auto& entry : entries)
  98. {
  99. auto OptionalVec2ToString = [](const Optional<Vector2f>& value) -> String {
  100. if (value.has_value())
  101. return Rml::ToString(value->x) + " x " + Rml::ToString(value->y);
  102. return "none";
  103. };
  104. const std::string newline = '\n' + StringUtilities::RepeatString("| ", entry.level);
  105. result += '\n' + StringUtilities::RepeatString("+ - ", entry.level) + entry.element;
  106. result +=
  107. newline + "| Format type: " + (entry.format_type == FormatType::FormatIndependent ? "FormatIndependent" : "FormatFitContentWidth");
  108. result += newline + "| Containing block: " + OptionalVec2ToString(entry.containing_block) + ". " + entry.parent_container_element;
  109. result += newline + "| Absolute positioning containing block: " + OptionalVec2ToString(entry.absolute_positioning_containing_block) + ". " +
  110. entry.absolute_positioning_containing_block_element;
  111. result += newline + "| Formatting context: ";
  112. switch (entry.context_type)
  113. {
  114. case FormattingContextType::Block: result += "Block"; break;
  115. case FormattingContextType::Table: result += "Table"; break;
  116. case FormattingContextType::Flex: result += "Flex"; break;
  117. case FormattingContextType::Replaced: result += "Replaced"; break;
  118. case FormattingContextType::None: result += "None"; break;
  119. default: result += "Unknown"; break;
  120. }
  121. if (entry.override_box)
  122. {
  123. const Box& box = *entry.override_box;
  124. result += newline + "| Override box: ";
  125. result += Rml::ToString(box.GetSize().x) + " x " + Rml::ToString(box.GetSize().y);
  126. result += " (outer size: " + Rml::ToString(box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin)) + " x " +
  127. Rml::ToString(box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin)) + ")";
  128. }
  129. else
  130. {
  131. result += newline + "| Override box: none";
  132. }
  133. if (entry.layout_result)
  134. {
  135. result += newline + "| Layout result" + (entry.from_cache ? " (cached)" : "") + ":";
  136. result += newline + "| Visible overflow: " + Rml::ToString(entry.layout_result->visible_overflow_size.x) + " x " +
  137. Rml::ToString(entry.layout_result->visible_overflow_size.y);
  138. if (entry.layout_result->box)
  139. {
  140. const Box& box = *entry.layout_result->box;
  141. result += newline + "| Layout box: ";
  142. result += Rml::ToString(box.GetSize().x) + " x " + Rml::ToString(box.GetSize().y);
  143. result += " (outer size: " + Rml::ToString(box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin)) + " x " +
  144. Rml::ToString(box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin)) + ")";
  145. }
  146. else
  147. {
  148. result += newline + "| Layout box: none";
  149. }
  150. }
  151. else if (entry.fit_width_result)
  152. {
  153. result += newline + "| Fit-width result" + (entry.from_cache ? " (cached)" : "") + ":";
  154. result += newline + "| Max-content width: " + Rml::ToString(entry.fit_width_result->max_content_width);
  155. }
  156. else
  157. {
  158. result += newline + "| Layout result: none";
  159. }
  160. }
  161. return result;
  162. }
  163. void FormattingContextDebugTracker::LogMessage() const
  164. {
  165. Log::Message(Log::LT_INFO, "%s", ToString().c_str());
  166. }
  167. int FormattingContextDebugTracker::CountCachedEntries() const
  168. {
  169. return (int)std::count_if(entries.begin(), entries.end(), [](const auto& entry) { return entry.from_cache; });
  170. }
  171. int FormattingContextDebugTracker::CountFormattedEntries() const
  172. {
  173. return (int)entries.size() - CountCachedEntries();
  174. }
  175. int FormattingContextDebugTracker::CountEntries() const
  176. {
  177. return (int)entries.size();
  178. }
  179. void DebugLogDirtyLayoutTree(Element* root_element)
  180. {
  181. String tree_dirty_state;
  182. ElementUtilities::VisitElementsDepthOrder(root_element, [&](Element* element, int tree_depth) {
  183. tree_dirty_state += String(size_t(4 * tree_depth), ' ');
  184. tree_dirty_state += CreateString("%s. Self: %d Child: %d", element->GetAddress(false, tree_depth == 0).c_str(),
  185. element->GetLayoutNode()->IsSelfDirty(), element->GetLayoutNode()->IsChildDirty());
  186. tree_dirty_state += '\n';
  187. });
  188. Log::Message(Log::LT_INFO, "Dirty layout tree:\n%s\n", tree_dirty_state.c_str());
  189. }
  190. #endif // RMLUI_DEBUG
  191. } // namespace Rml