FormattingContext.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. if (element->GetId() == "outer")
  70. int x = 0;
  71. #ifdef RMLUI_DEBUG
  72. auto* debug_tracker = FormatIndependentDebugTracker::GetIf();
  73. FormatIndependentDebugTracker::Entry* tracker_entry = nullptr;
  74. if (debug_tracker && type != FormattingContextType::None)
  75. {
  76. tracker_entry = &debug_tracker->entries.emplace_back(FormatIndependentDebugTracker::Entry{
  77. debug_tracker->current_stack_level,
  78. parent_container->GetElement() ? parent_container->GetElement()->GetAddress() : "",
  79. parent_container->GetAbsolutePositioningContainingBlockElementForDebug()
  80. ? parent_container->GetAbsolutePositioningContainingBlockElementForDebug()->GetAddress()
  81. : "",
  82. parent_container->GetContainingBlockSize(Style::Position::Static),
  83. parent_container->GetContainingBlockSize(Style::Position::Absolute),
  84. element->GetAddress(),
  85. override_initial_box ? Optional<Box>{*override_initial_box} : std::nullopt,
  86. type,
  87. });
  88. debug_tracker->current_stack_level += 1;
  89. }
  90. #endif
  91. UniquePtr<LayoutBox> layout_box;
  92. const FormattingMode& formatting_mode = parent_container->GetFormattingMode();
  93. if (type != FormattingContextType::None && formatting_mode.constraint == FormattingMode::Constraint::None)
  94. {
  95. LayoutNode* layout_node = element->GetLayoutNode();
  96. if (layout_node->CommittedLayoutMatches(parent_container->GetContainingBlockSize(Style::Position::Static),
  97. parent_container->GetContainingBlockSize(Style::Position::Static), override_initial_box))
  98. {
  99. Log::Message(Log::LT_INFO, "Layout cache match on element%s: %s",
  100. (formatting_mode.allow_format_independent_cache ? "" : " (skipping cache due to formatting mode)"), element->GetAddress().c_str());
  101. // TODO: How to deal with ShrinkToFitWidth, in particular for the returned box? Store it in the LayoutNode?
  102. // Maybe best not to use this committed layout at all during max-content layouting. Instead, skip this here,
  103. // return zero in the CacheContainerBox, and make a separate LayoutNode cache for shrink-to-fit width that
  104. // is fetched in LayoutDetails::ShrinkToFitWidth().
  105. if (formatting_mode.allow_format_independent_cache)
  106. {
  107. layout_box = MakeUnique<CachedContainer>(element, parent_container, element->GetBox(),
  108. layout_node->GetCommittedLayout()->visible_overflow_size, layout_node->GetCommittedLayout()->baseline_of_last_line);
  109. }
  110. }
  111. }
  112. if (!layout_box)
  113. {
  114. const Vector2f containing_block = parent_container->GetContainingBlockSize(element->GetPosition());
  115. Box box;
  116. if (override_initial_box)
  117. box = *override_initial_box;
  118. else if (formatting_mode.constraint == FormattingMode::Constraint::MaxContent && type == FormattingContextType::Flex)
  119. LayoutDetails::BuildBox(box, containing_block, element, BuildBoxMode::UnalignedBlock);
  120. else
  121. LayoutDetails::BuildBox(box, containing_block, element, BuildBoxMode::ShrinkableBlock, &parent_container->GetFormattingMode());
  122. switch (type)
  123. {
  124. case FormattingContextType::Block: layout_box = BlockFormattingContext::Format(parent_container, element, containing_block, box); break;
  125. case FormattingContextType::Table: layout_box = TableFormattingContext::Format(parent_container, element, containing_block, box); break;
  126. case FormattingContextType::Flex: layout_box = FlexFormattingContext::Format(parent_container, element, containing_block, box); break;
  127. case FormattingContextType::Replaced: layout_box = ReplacedFormattingContext::Format(parent_container, element, box); break;
  128. case FormattingContextType::None: break;
  129. }
  130. // TODO: If MaxContent constraint, and containing block = -1, -1, store resulting size as max-content size.
  131. }
  132. if (layout_box && formatting_mode.constraint == FormattingMode::Constraint::None)
  133. {
  134. Optional<float> baseline_of_last_line;
  135. float baseline_of_last_line_value = 0.f;
  136. if (layout_box->GetBaselineOfLastLine(baseline_of_last_line_value))
  137. baseline_of_last_line = baseline_of_last_line_value;
  138. LayoutNode* layout_node = element->GetLayoutNode();
  139. layout_node->CommitLayout(parent_container->GetContainingBlockSize(Style::Position::Static),
  140. parent_container->GetContainingBlockSize(Style::Position::Absolute), override_initial_box, layout_box->GetVisibleOverflowSize(),
  141. baseline_of_last_line);
  142. }
  143. #ifdef RMLUI_DEBUG
  144. if (tracker_entry)
  145. {
  146. debug_tracker->current_stack_level -= 1;
  147. if (layout_box)
  148. {
  149. const bool from_cache = (layout_box->GetType() == LayoutBox::Type::CachedContainer);
  150. tracker_entry->layout = Optional<FormatIndependentDebugTracker::Entry::LayoutResults>({
  151. from_cache,
  152. layout_box->GetVisibleOverflowSize(),
  153. layout_box->GetIfBox() ? Optional<Box>{*layout_box->GetIfBox()} : std::nullopt,
  154. });
  155. }
  156. }
  157. #endif
  158. return layout_box;
  159. }
  160. } // namespace Rml