FormattingContextDebug.cpp 7.4 KB

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