FormattingContext.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 (type == FormattingContextType::None)
  70. return nullptr;
  71. if (element->GetId() == "outer")
  72. int x = 0;
  73. #ifdef RMLUI_DEBUG
  74. auto* debug_tracker = FormatIndependentDebugTracker::GetIf();
  75. FormatIndependentDebugTracker::Entry* tracker_entry = nullptr;
  76. if (debug_tracker && type != FormattingContextType::None)
  77. {
  78. tracker_entry = &debug_tracker->entries.emplace_back(FormatIndependentDebugTracker::Entry{
  79. debug_tracker->current_stack_level,
  80. parent_container->GetElement() ? parent_container->GetElement()->GetAddress() : "",
  81. parent_container->GetAbsolutePositioningContainingBlockElementForDebug()
  82. ? parent_container->GetAbsolutePositioningContainingBlockElementForDebug()->GetAddress()
  83. : "",
  84. parent_container->GetContainingBlockSize(Style::Position::Static),
  85. parent_container->GetContainingBlockSize(Style::Position::Absolute),
  86. element->GetAddress(),
  87. override_initial_box ? Optional<Box>{*override_initial_box} : std::nullopt,
  88. type,
  89. });
  90. debug_tracker->current_stack_level += 1;
  91. }
  92. #endif
  93. UniquePtr<LayoutBox> layout_box;
  94. const FormattingMode& formatting_mode = parent_container->GetFormattingMode();
  95. if (type != FormattingContextType::None && formatting_mode.constraint == FormattingMode::Constraint::None)
  96. {
  97. LayoutNode* layout_node = element->GetLayoutNode();
  98. if (layout_node->CommittedLayoutMatches(parent_container->GetContainingBlockSize(Style::Position::Static),
  99. parent_container->GetContainingBlockSize(Style::Position::Static), override_initial_box))
  100. {
  101. Log::Message(Log::LT_INFO, "Layout cache match on element%s: %s",
  102. (formatting_mode.allow_format_independent_cache ? "" : " (skipping cache due to formatting mode)"), element->GetAddress().c_str());
  103. // TODO: How to deal with ShrinkToFitWidth, in particular for the returned box? Store it in the LayoutNode?
  104. // Maybe best not to use this committed layout at all during max-content layouting. Instead, skip this here,
  105. // return zero in the CacheContainerBox, and make a separate LayoutNode cache for shrink-to-fit width that
  106. // is fetched in LayoutDetails::ShrinkToFitWidth().
  107. if (formatting_mode.allow_format_independent_cache)
  108. {
  109. layout_box = MakeUnique<CachedContainer>(element, parent_container, element->GetBox(),
  110. layout_node->GetCommittedLayout()->visible_overflow_size, layout_node->GetCommittedLayout()->baseline_of_last_line);
  111. }
  112. }
  113. }
  114. if (!layout_box)
  115. {
  116. const Vector2f containing_block = parent_container->GetContainingBlockSize(element->GetPosition());
  117. Box box;
  118. if (override_initial_box)
  119. {
  120. box = *override_initial_box;
  121. }
  122. else if (formatting_mode.constraint == FormattingMode::Constraint::MaxContent && type == FormattingContextType::Flex)
  123. {
  124. LayoutDetails::BuildBox(box, containing_block, element, BuildBoxMode::UnalignedBlock);
  125. }
  126. else
  127. {
  128. LayoutDetails::BuildBox(box, containing_block, element, BuildBoxMode::ShrinkableBlock);
  129. // Check for shrink-to-fit width.
  130. if (box.GetSize().x < 0.f)
  131. {
  132. const float box_height = box.GetSize().y;
  133. // Max-content width should be calculated without any vertical constraint.
  134. box.SetContent(Vector2f(-1.f));
  135. FormattingMode shrink_formatting_mode = formatting_mode;
  136. shrink_formatting_mode.constraint = FormattingMode::Constraint::MaxContent;
  137. float max_content_width = -1.f;
  138. switch (type)
  139. {
  140. case FormattingContextType::Block:
  141. max_content_width = BlockFormattingContext::DetermineMaxContentWidth(element, box, shrink_formatting_mode);
  142. break;
  143. case FormattingContextType::Table:
  144. // Currently we don't support shrink-to-fit width for tables, just use a zero-sized width.
  145. max_content_width = 0.f;
  146. break;
  147. case FormattingContextType::Flex:
  148. max_content_width = FlexFormattingContext::DetermineMaxContentWidth(element, box, shrink_formatting_mode);
  149. break;
  150. case FormattingContextType::Replaced: RMLUI_ERRORMSG("Replaced elements are expected to have a positive intrinsice size."); break;
  151. case FormattingContextType::None: RMLUI_ERROR; break;
  152. }
  153. RMLUI_ASSERTMSG(max_content_width >= 0.f, "Max-content width should evaluate to a positive size.")
  154. // TODO: Cache max_content width. Remove LayoutDetails::ShrinkToFit (or re-use this function here).
  155. // Maybe split this out to a separate function that can be called also from e.g. FlexBoxFormatting.
  156. float fit_content_width = max_content_width;
  157. if (containing_block.x >= 0.f)
  158. {
  159. const float available_width =
  160. Math::Max(0.f, containing_block.x - box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin, BoxArea::Padding));
  161. fit_content_width = Math::Min(max_content_width, available_width);
  162. }
  163. box.SetContent(Vector2f(fit_content_width, box_height));
  164. LayoutDetails::ClampSizeAndBuildAutoMarginsForBlockWidth(box, containing_block, element);
  165. }
  166. }
  167. switch (type)
  168. {
  169. case FormattingContextType::Block: layout_box = BlockFormattingContext::Format(parent_container, element, containing_block, box); break;
  170. case FormattingContextType::Table: layout_box = TableFormattingContext::Format(parent_container, element, containing_block, box); break;
  171. case FormattingContextType::Flex: layout_box = FlexFormattingContext::Format(parent_container, element, containing_block, box); break;
  172. case FormattingContextType::Replaced: layout_box = ReplacedFormattingContext::Format(parent_container, element, box); break;
  173. case FormattingContextType::None: RMLUI_ERROR; break;
  174. }
  175. // TODO: If MaxContent constraint, and containing block = -1, -1, store resulting size as max-content size.
  176. }
  177. if (layout_box && formatting_mode.constraint == FormattingMode::Constraint::None)
  178. {
  179. Optional<float> baseline_of_last_line;
  180. float baseline_of_last_line_value = 0.f;
  181. if (layout_box->GetBaselineOfLastLine(baseline_of_last_line_value))
  182. baseline_of_last_line = baseline_of_last_line_value;
  183. LayoutNode* layout_node = element->GetLayoutNode();
  184. layout_node->CommitLayout(parent_container->GetContainingBlockSize(Style::Position::Static),
  185. parent_container->GetContainingBlockSize(Style::Position::Absolute), override_initial_box, layout_box->GetVisibleOverflowSize(),
  186. baseline_of_last_line);
  187. }
  188. #ifdef RMLUI_DEBUG
  189. if (tracker_entry)
  190. {
  191. debug_tracker->current_stack_level -= 1;
  192. if (layout_box)
  193. {
  194. const bool from_cache = (layout_box->GetType() == LayoutBox::Type::CachedContainer);
  195. tracker_entry->layout = Optional<FormatIndependentDebugTracker::Entry::LayoutResults>({
  196. from_cache,
  197. layout_box->GetVisibleOverflowSize(),
  198. layout_box->GetIfBox() ? Optional<Box>{*layout_box->GetIfBox()} : std::nullopt,
  199. });
  200. }
  201. }
  202. #endif
  203. return layout_box;
  204. }
  205. } // namespace Rml