BlockFormattingContext.cpp 11 KB

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