BlockFormattingContext.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "BlockFormattingContext.h"
  2. #include "../../../Include/RmlUi/Core/ComputedValues.h"
  3. #include "../../../Include/RmlUi/Core/Element.h"
  4. #include "../../../Include/RmlUi/Core/Profiling.h"
  5. #include "../../../Include/RmlUi/Core/PropertyDefinition.h"
  6. #include "../../../Include/RmlUi/Core/StyleSheetSpecification.h"
  7. #include "../../../Include/RmlUi/Core/SystemInterface.h"
  8. #include "BlockContainer.h"
  9. #include "FloatedBoxSpace.h"
  10. #include "LayoutDetails.h"
  11. namespace Rml {
  12. // Table elements should be handled within FormatElementTable, log a warning when it seems like we're encountering table parts in the wild.
  13. static void LogUnexpectedFlowElement(Element* element, Style::Display display)
  14. {
  15. RMLUI_ASSERT(element);
  16. String value = "*unknown";
  17. StyleSheetSpecification::GetPropertySpecification().GetProperty(PropertyId::Display)->GetValue(value, Property(display));
  18. Log::Message(Log::LT_WARNING, "Element has a display type '%s' which cannot be located in normal flow layout. Element will not be formatted: %s",
  19. value.c_str(), element->GetAddress().c_str());
  20. }
  21. #ifdef RMLUI_DEBUG
  22. static bool g_debug_dumping_layout_tree = false;
  23. struct DebugDumpLayoutTree {
  24. Element* element;
  25. BlockContainer* block_box;
  26. bool is_printing_tree_root = false;
  27. DebugDumpLayoutTree(Element* element, BlockContainer* block_box) : element(element), block_box(block_box)
  28. {
  29. // When an element with this ID is encountered, dump the formatted layout tree (including for all descendant formatting contexts).
  30. static const String debug_trigger_id = "rmlui-debug-layout";
  31. is_printing_tree_root = element->HasAttribute(debug_trigger_id);
  32. if (is_printing_tree_root)
  33. g_debug_dumping_layout_tree = true;
  34. }
  35. ~DebugDumpLayoutTree()
  36. {
  37. if (g_debug_dumping_layout_tree)
  38. {
  39. const String header = ":: " + LayoutDetails::GetDebugElementName(element) + " ::\n";
  40. const String layout_tree = header + block_box->DumpLayoutTree();
  41. if (SystemInterface* system_interface = GetSystemInterface())
  42. system_interface->LogMessage(Log::LT_INFO, layout_tree);
  43. if (is_printing_tree_root)
  44. g_debug_dumping_layout_tree = false;
  45. }
  46. }
  47. };
  48. #else
  49. struct DebugDumpLayoutTree {
  50. DebugDumpLayoutTree(Element* /*element*/, BlockContainer* /*block_box*/) {}
  51. };
  52. #endif
  53. enum class OuterDisplayType { BlockLevel, InlineLevel, Invalid };
  54. static OuterDisplayType GetOuterDisplayType(Style::Display display)
  55. {
  56. switch (display)
  57. {
  58. case Style::Display::Block:
  59. case Style::Display::FlowRoot:
  60. case Style::Display::Flex:
  61. case Style::Display::Table: return OuterDisplayType::BlockLevel;
  62. case Style::Display::Inline:
  63. case Style::Display::InlineBlock:
  64. case Style::Display::InlineFlex:
  65. case Style::Display::InlineTable: return OuterDisplayType::InlineLevel;
  66. case Style::Display::TableRow:
  67. case Style::Display::TableRowGroup:
  68. case Style::Display::TableColumn:
  69. case Style::Display::TableColumnGroup:
  70. case Style::Display::TableCell:
  71. case Style::Display::None: break;
  72. }
  73. return OuterDisplayType::Invalid;
  74. }
  75. UniquePtr<LayoutBox> BlockFormattingContext::Format(ContainerBox* parent_container, Element* element, const Box* override_initial_box)
  76. {
  77. RMLUI_ASSERT(parent_container && element);
  78. #ifdef RMLUI_TRACY_PROFILING
  79. RMLUI_ZoneScopedC(0xB22222);
  80. auto name = CreateString("%s %x", element->GetAddress(false, false).c_str(), element);
  81. RMLUI_ZoneName(name.c_str(), name.size());
  82. #endif
  83. const Vector2f containing_block = LayoutDetails::GetContainingBlock(parent_container, element->GetPosition()).size;
  84. Box box;
  85. if (override_initial_box)
  86. box = *override_initial_box;
  87. else
  88. LayoutDetails::BuildBox(box, containing_block, element);
  89. float min_height, max_height;
  90. LayoutDetails::GetDefiniteMinMaxHeight(min_height, max_height, element->GetComputedValues(), box, containing_block.y);
  91. UniquePtr<BlockContainer> container = MakeUnique<BlockContainer>(parent_container, nullptr, element, box, min_height, max_height);
  92. DebugDumpLayoutTree debug_dump_tree(element, container.get());
  93. container->ResetScrollbars(box);
  94. // Format the element's children. In rare cases, it is possible that we need three iterations: Once to enable the
  95. // horizontal scrollbar, then to enable the vertical scrollbar, and finally to format with both scrollbars enabled.
  96. for (int layout_iteration = 0; layout_iteration < 3; layout_iteration++)
  97. {
  98. bool all_children_formatted = true;
  99. for (int i = 0; i < element->GetNumChildren() && all_children_formatted; i++)
  100. {
  101. if (!FormatBlockContainerChild(container.get(), element->GetChild(i)))
  102. all_children_formatted = false;
  103. }
  104. if (all_children_formatted && container->Close(nullptr))
  105. // Success, break out of the loop.
  106. break;
  107. // Otherwise, restart formatting now that one or both scrollbars have been enabled.
  108. container->ResetContents();
  109. }
  110. return container;
  111. }
  112. bool BlockFormattingContext::FormatBlockBox(BlockContainer* parent_container, Element* element)
  113. {
  114. RMLUI_ZoneScopedC(0x2F4F4F);
  115. const Vector2f containing_block = LayoutDetails::GetContainingBlock(parent_container, element->GetPosition()).size;
  116. Box box;
  117. LayoutDetails::BuildBox(box, containing_block, element);
  118. float min_height, max_height;
  119. LayoutDetails::GetDefiniteMinMaxHeight(min_height, max_height, element->GetComputedValues(), box, containing_block.y);
  120. BlockContainer* container = parent_container->OpenBlockBox(element, box, min_height, max_height);
  121. if (!container)
  122. return false;
  123. // Format our children. This may result in scrollbars being added to our formatting context root, then we need to
  124. // bail out and restart formatting for the current block formatting context.
  125. for (int i = 0; i < element->GetNumChildren(); i++)
  126. {
  127. if (!FormatBlockContainerChild(container, element->GetChild(i)))
  128. return false;
  129. }
  130. if (!container->Close(parent_container))
  131. return false;
  132. return true;
  133. }
  134. bool BlockFormattingContext::FormatInlineBox(BlockContainer* parent_container, Element* element)
  135. {
  136. RMLUI_ZoneScopedC(0x3F6F6F);
  137. const Vector2f containing_block = LayoutDetails::GetContainingBlock(parent_container, element->GetPosition()).size;
  138. Box box;
  139. LayoutDetails::BuildBox(box, containing_block, element, BuildBoxMode::Inline);
  140. auto inline_box_handle = parent_container->AddInlineElement(element, box);
  141. // Format the element's children.
  142. for (int i = 0; i < element->GetNumChildren(); i++)
  143. {
  144. if (!FormatBlockContainerChild(parent_container, element->GetChild(i)))
  145. return false;
  146. }
  147. parent_container->CloseInlineElement(inline_box_handle);
  148. return true;
  149. }
  150. bool BlockFormattingContext::FormatBlockContainerChild(BlockContainer* parent_container, Element* element)
  151. {
  152. #ifdef RMLUI_TRACY_PROFILING
  153. RMLUI_ZoneScoped;
  154. auto name = CreateString(">%s %x", element->GetAddress(false, false).c_str(), element);
  155. RMLUI_ZoneName(name.c_str(), name.size());
  156. #endif
  157. // Check for special formatting tags.
  158. if (element->GetTagName() == "br")
  159. {
  160. parent_container->AddBreak();
  161. return true;
  162. }
  163. auto& computed = element->GetComputedValues();
  164. const Style::Display display = computed.display();
  165. // Don't lay this element out if it is set to a display type of none.
  166. if (display == Style::Display::None)
  167. return true;
  168. // Check for absolutely positioned elements: they are removed from the flow and added to the box representing their
  169. // containing block, to be layed out and positioned once that box has been closed and sized.
  170. const Style::Position position_property = computed.position();
  171. if (position_property == Style::Position::Absolute || position_property == Style::Position::Fixed)
  172. {
  173. const Vector2f static_position = parent_container->GetOpenStaticPosition(display) - parent_container->GetPosition();
  174. ContainingBlock containing_block = LayoutDetails::GetContainingBlock(parent_container, position_property);
  175. containing_block.container->AddAbsoluteElement(element, static_position, parent_container->GetElement());
  176. return true;
  177. }
  178. const OuterDisplayType outer_display = GetOuterDisplayType(display);
  179. if (outer_display == OuterDisplayType::Invalid)
  180. {
  181. LogUnexpectedFlowElement(element, display);
  182. return true;
  183. }
  184. // If the element creates an independent formatting context, then format it accordingly.
  185. if (UniquePtr<LayoutBox> layout_box = FormattingContext::FormatIndependent(parent_container, element, nullptr, FormattingContextType::None))
  186. {
  187. // If the element is floating, we remove it from the flow.
  188. if (computed.float_() != Style::Float::None)
  189. {
  190. parent_container->AddFloatElement(element, layout_box->GetVisibleOverflowSize());
  191. }
  192. // Otherwise, check if we have a sized block-level box.
  193. else if (layout_box && outer_display == OuterDisplayType::BlockLevel)
  194. {
  195. if (!parent_container->AddBlockLevelBox(std::move(layout_box), element, element->GetBox()))
  196. return false;
  197. }
  198. // Nope, then this must be an inline-level box.
  199. else
  200. {
  201. RMLUI_ASSERT(outer_display == OuterDisplayType::InlineLevel);
  202. auto inline_box_handle = parent_container->AddInlineElement(element, element->GetBox());
  203. parent_container->CloseInlineElement(inline_box_handle);
  204. }
  205. return true;
  206. }
  207. // The element is an in-flow box participating in this same block formatting context.
  208. switch (display)
  209. {
  210. case Style::Display::Block: return FormatBlockBox(parent_container, element);
  211. case Style::Display::Inline: return FormatInlineBox(parent_container, element);
  212. default:
  213. RMLUI_ERROR; // Should have been handled above.
  214. break;
  215. }
  216. return true;
  217. }
  218. } // namespace Rml