2
0

FormattingContext.cpp 6.9 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 "FormattingContext.h"
  29. #include "../../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../../Include/RmlUi/Core/Element.h"
  31. #include "../../../Include/RmlUi/Core/Profiling.h"
  32. #include "BlockFormattingContext.h"
  33. #include "ContainerBox.h"
  34. #include "FlexFormattingContext.h"
  35. #include "FormattingContextDebug.h"
  36. #include "LayoutBox.h"
  37. #include "LayoutDetails.h"
  38. #include "LayoutNode.h"
  39. #include "ReplacedFormattingContext.h"
  40. #include "TableFormattingContext.h"
  41. namespace Rml {
  42. FormattingContextType FormattingContext::GetFormattingContextType(Element* element)
  43. {
  44. if (element->IsReplaced())
  45. return FormattingContextType::Replaced;
  46. using namespace Style;
  47. auto& computed = element->GetComputedValues();
  48. const Display display = computed.display();
  49. if (display == Display::Flex || display == Display::InlineFlex)
  50. return FormattingContextType::Flex;
  51. if (display == Display::Table || display == Display::InlineTable)
  52. return FormattingContextType::Table;
  53. if (display == Display::InlineBlock || display == Display::FlowRoot || display == Display::TableCell || computed.float_() != Float::None ||
  54. computed.position() == Position::Absolute || computed.position() == Position::Fixed || computed.overflow_x() != Overflow::Visible ||
  55. computed.overflow_y() != Overflow::Visible || !element->GetParentNode() || element->GetParentNode()->GetDisplay() == Display::Flex)
  56. {
  57. return FormattingContextType::Block;
  58. }
  59. return FormattingContextType::None;
  60. }
  61. UniquePtr<LayoutBox> FormattingContext::FormatIndependent(ContainerBox* parent_container, Element* element, const Box* override_initial_box,
  62. FormattingContextType default_context)
  63. {
  64. RMLUI_ASSERT(parent_container && element);
  65. RMLUI_ZoneScopedC(0xAFAFAF);
  66. FormattingContextType type = GetFormattingContextType(element);
  67. if (type == FormattingContextType::None)
  68. type = default_context;
  69. #ifdef RMLUI_DEBUG
  70. auto* debug_tracker = FormatIndependentDebugTracker::GetIf();
  71. FormatIndependentDebugTracker::Entry* tracker_entry = nullptr;
  72. if (debug_tracker && type != FormattingContextType::None)
  73. {
  74. tracker_entry = &debug_tracker->entries.emplace_back(FormatIndependentDebugTracker::Entry{
  75. debug_tracker->current_stack_level,
  76. parent_container->GetElement() ? parent_container->GetElement()->GetAddress() : "",
  77. parent_container->GetAbsolutePositioningContainingBlockElementForDebug()
  78. ? parent_container->GetAbsolutePositioningContainingBlockElementForDebug()->GetAddress()
  79. : "",
  80. parent_container->GetContainingBlockSize(Style::Position::Static),
  81. parent_container->GetContainingBlockSize(Style::Position::Absolute),
  82. element->GetAddress(),
  83. override_initial_box ? Optional<Box>{*override_initial_box} : std::nullopt,
  84. type,
  85. });
  86. debug_tracker->current_stack_level += 1;
  87. }
  88. #endif
  89. UniquePtr<LayoutBox> layout_box;
  90. const bool is_under_max_content_constraint = parent_container->IsUnderMaxContentConstraint();
  91. if (type != FormattingContextType::None && !is_under_max_content_constraint)
  92. {
  93. LayoutNode* layout_node = element->GetLayoutNode();
  94. if (layout_node->CommittedLayoutMatches(parent_container->GetContainingBlockSize(Style::Position::Static),
  95. parent_container->GetContainingBlockSize(Style::Position::Static), override_initial_box))
  96. {
  97. Log::Message(Log::LT_INFO, "Layout cache match on element: %s", element->GetAddress().c_str());
  98. // TODO: How to deal with ShrinkToFitWidth, in particular for the returned box? Store it in the LayoutNode?
  99. // Maybe best not to use this committed layout at all during max-content layouting. Instead, skip this here,
  100. // return zero in the CacheContainerBox, and make a separate LayoutNode cache for shrink-to-fit width that
  101. // is fetched in LayoutDetails::ShrinkToFitWidth().
  102. layout_box = MakeUnique<CachedContainer>(element, parent_container, element->GetBox(),
  103. layout_node->GetCommittedLayout()->visible_overflow_size, layout_node->GetCommittedLayout()->baseline_of_last_line);
  104. }
  105. }
  106. if (!layout_box)
  107. {
  108. switch (type)
  109. {
  110. case FormattingContextType::Block: layout_box = BlockFormattingContext::Format(parent_container, element, override_initial_box); break;
  111. case FormattingContextType::Table: layout_box = TableFormattingContext::Format(parent_container, element, override_initial_box); break;
  112. case FormattingContextType::Flex: layout_box = FlexFormattingContext::Format(parent_container, element, override_initial_box); break;
  113. case FormattingContextType::Replaced: layout_box = ReplacedFormattingContext::Format(parent_container, element, override_initial_box); break;
  114. case FormattingContextType::None: break;
  115. }
  116. }
  117. if (layout_box && !is_under_max_content_constraint)
  118. {
  119. Optional<float> baseline_of_last_line;
  120. float baseline_of_last_line_value = 0.f;
  121. if (layout_box->GetBaselineOfLastLine(baseline_of_last_line_value))
  122. baseline_of_last_line = baseline_of_last_line_value;
  123. LayoutNode* layout_node = element->GetLayoutNode();
  124. layout_node->CommitLayout(parent_container->GetContainingBlockSize(Style::Position::Static),
  125. parent_container->GetContainingBlockSize(Style::Position::Absolute), override_initial_box, layout_box->GetVisibleOverflowSize(),
  126. baseline_of_last_line);
  127. }
  128. #ifdef RMLUI_DEBUG
  129. if (tracker_entry)
  130. {
  131. debug_tracker->current_stack_level -= 1;
  132. if (layout_box)
  133. {
  134. const bool from_cache = (layout_box->GetType() == LayoutBox::Type::CachedContainer);
  135. tracker_entry->layout = Optional<FormatIndependentDebugTracker::Entry::LayoutResults>({
  136. from_cache,
  137. layout_box->GetVisibleOverflowSize(),
  138. layout_box->GetIfBox() ? Optional<Box>{*layout_box->GetIfBox()} : std::nullopt,
  139. });
  140. }
  141. }
  142. #endif
  143. return layout_box;
  144. }
  145. } // namespace Rml