FormattingContextDebug.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. namespace Rml {
  31. #ifdef RMLUI_DEBUG
  32. static FormatIndependentDebugTracker* g_debug_format_independent_tracker = nullptr;
  33. static std::string repeat(const std::string& input, int count)
  34. {
  35. if (count <= 0)
  36. return {};
  37. std::string result;
  38. result.reserve(input.size() * size_t(count));
  39. for (size_t i = 0; i < size_t(count); ++i)
  40. result += input;
  41. return result;
  42. }
  43. FormatIndependentDebugTracker::FormatIndependentDebugTracker()
  44. {
  45. RMLUI_ASSERTMSG(!g_debug_format_independent_tracker, "An instance of FormatIndependentDebugTracker already exists");
  46. g_debug_format_independent_tracker = this;
  47. }
  48. FormatIndependentDebugTracker::~FormatIndependentDebugTracker()
  49. {
  50. RMLUI_ASSERT(g_debug_format_independent_tracker == this);
  51. RMLUI_ASSERT(current_stack_level == 0);
  52. g_debug_format_independent_tracker = nullptr;
  53. }
  54. FormatIndependentDebugTracker* FormatIndependentDebugTracker::GetIf()
  55. {
  56. return g_debug_format_independent_tracker;
  57. }
  58. void FormatIndependentDebugTracker::Reset()
  59. {
  60. *this = {};
  61. }
  62. String FormatIndependentDebugTracker::ToString() const
  63. {
  64. String result;
  65. for (const auto& entry : entries)
  66. {
  67. const std::string newline = '\n' + repeat("| ", entry.level);
  68. result += '\n' + repeat("+ - ", entry.level) + entry.element;
  69. result += newline + "| Containing block: " + Rml::ToString(entry.containing_block.x) + " x " + Rml::ToString(entry.containing_block.y) +
  70. ". " + entry.parent_container_element;
  71. result += newline + "| Absolute positioning containing block: " + Rml::ToString(entry.absolute_positioning_containing_block.x) + " x " +
  72. Rml::ToString(entry.absolute_positioning_containing_block.y) + ". " + entry.absolute_positioning_containing_block_element;
  73. result += newline + "| Formatting context: ";
  74. switch (entry.type)
  75. {
  76. case FormattingContextType::Block: result += "Block"; break;
  77. case FormattingContextType::Table: result += "Table"; break;
  78. case FormattingContextType::Flex: result += "Flex"; break;
  79. case FormattingContextType::Replaced: result += "Replaced"; break;
  80. case FormattingContextType::None: result += "None"; break;
  81. default: result += "Unknown"; break;
  82. }
  83. if (entry.override_box)
  84. {
  85. const Box& box = *entry.override_box;
  86. result += newline + "| Override box: ";
  87. result += Rml::ToString(box.GetSize().x) + " x " + Rml::ToString(box.GetSize().y);
  88. result += " (outer size: " + Rml::ToString(box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin)) + " x " +
  89. Rml::ToString(box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin)) + ")";
  90. }
  91. else
  92. {
  93. result += newline + "| Override box: none";
  94. }
  95. if (entry.layout)
  96. {
  97. result += newline + "| Layout results:";
  98. result += newline + "| Visible overflow: " + Rml::ToString(entry.layout->visible_overflow_size.x) + " x " +
  99. Rml::ToString(entry.layout->visible_overflow_size.y);
  100. if (entry.layout->box)
  101. {
  102. const Box& box = *entry.layout->box;
  103. result += newline + "| Layout box: ";
  104. result += Rml::ToString(box.GetSize().x) + " x " + Rml::ToString(box.GetSize().y);
  105. result += " (outer size: " + Rml::ToString(box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin)) + " x " +
  106. Rml::ToString(box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin)) + ")";
  107. }
  108. else
  109. {
  110. result += newline + "| Layout box: none";
  111. }
  112. }
  113. else
  114. {
  115. result += newline + "| Layout results: none";
  116. }
  117. }
  118. return result;
  119. }
  120. void FormatIndependentDebugTracker::LogMessage() const
  121. {
  122. Log::Message(Log::LT_INFO, "%s", ToString().c_str());
  123. }
  124. int FormatIndependentDebugTracker::CountCachedEntries() const
  125. {
  126. return (int)std::count_if(entries.begin(), entries.end(), [](const auto& entry) { return entry.layout.has_value(); });
  127. }
  128. int FormatIndependentDebugTracker::CountFormattedEntries() const
  129. {
  130. return (int)entries.size() - CountCachedEntries();
  131. }
  132. int FormatIndependentDebugTracker::CountEntries() const
  133. {
  134. return (int)entries.size();
  135. }
  136. #endif // RMLUI_DEBUG
  137. } // namespace Rml