LayoutEngine.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "LayoutEngine.h"
  29. #include "LayoutBlockBoxSpace.h"
  30. #include "LayoutDetails.h"
  31. #include "LayoutInlineBoxText.h"
  32. #include "LayoutTable.h"
  33. #include "Pool.h"
  34. #include "../../Include/RmlUi/Core/Element.h"
  35. #include "../../Include/RmlUi/Core/Profiling.h"
  36. #include "../../Include/RmlUi/Core/Types.h"
  37. #include <cstddef>
  38. namespace Rml {
  39. #define MAX(a, b) (a > b ? a : b)
  40. struct LayoutChunk
  41. {
  42. static const unsigned int size = MAX(sizeof(LayoutBlockBox), MAX(sizeof(LayoutInlineBox), MAX(sizeof(LayoutInlineBoxText), MAX(sizeof(LayoutLineBox), sizeof(LayoutBlockBoxSpace)))));
  43. alignas(std::max_align_t) char buffer[size];
  44. };
  45. static Pool< LayoutChunk > layout_chunk_pool(200, true);
  46. // Formats the contents for a root-level element (usually a document or floating element).
  47. void LayoutEngine::FormatElement(Element* element, Vector2f containing_block, const Box* override_initial_box, Vector2f* visible_overflow_size)
  48. {
  49. RMLUI_ASSERT(element && containing_block.x >= 0 && containing_block.y >= 0);
  50. #ifdef RMLUI_ENABLE_PROFILING
  51. RMLUI_ZoneScopedC(0xB22222);
  52. auto name = CreateString(80, "%s %x", element->GetAddress(false, false).c_str(), element);
  53. RMLUI_ZoneName(name.c_str(), name.size());
  54. #endif
  55. LayoutBlockBox containing_block_box(nullptr, nullptr, Box(containing_block), 0.0f, FLT_MAX);
  56. Box box;
  57. if (override_initial_box)
  58. box = *override_initial_box;
  59. else
  60. LayoutDetails::BuildBox(box, containing_block, element, false);
  61. float min_height, max_height;
  62. LayoutDetails::GetMinMaxHeight(min_height, max_height, &element->GetComputedValues(), box, containing_block.y);
  63. LayoutBlockBox* block_context_box = containing_block_box.AddBlockElement(element, box, min_height, max_height);
  64. for (int layout_iteration = 0; layout_iteration < 2; layout_iteration++)
  65. {
  66. for (int i = 0; i < element->GetNumChildren(); i++)
  67. {
  68. if (!FormatElement(block_context_box, element->GetChild(i)))
  69. i = -1;
  70. }
  71. if (block_context_box->Close() == LayoutBlockBox::OK)
  72. break;
  73. }
  74. block_context_box->CloseAbsoluteElements();
  75. if (visible_overflow_size)
  76. *visible_overflow_size = block_context_box->GetVisibleOverflowSize();
  77. element->OnLayout();
  78. }
  79. void* LayoutEngine::AllocateLayoutChunk(size_t size)
  80. {
  81. RMLUI_ASSERT(size <= LayoutChunk::size);
  82. (void)size;
  83. return layout_chunk_pool.AllocateAndConstruct();
  84. }
  85. void LayoutEngine::DeallocateLayoutChunk(void* chunk)
  86. {
  87. layout_chunk_pool.DestroyAndDeallocate((LayoutChunk*) chunk);
  88. }
  89. // Positions a single element and its children within this layout.
  90. bool LayoutEngine::FormatElement(LayoutBlockBox* block_context_box, Element* element)
  91. {
  92. #ifdef RMLUI_ENABLE_PROFILING
  93. RMLUI_ZoneScoped;
  94. auto name = CreateString(80, ">%s %x", element->GetAddress(false, false).c_str(), element);
  95. RMLUI_ZoneName(name.c_str(), name.size());
  96. #endif
  97. auto& computed = element->GetComputedValues();
  98. // Check if we have to do any special formatting for any elements that don't fit into the standard layout scheme.
  99. if (FormatElementSpecial(block_context_box, element))
  100. return true;
  101. // Fetch the display property, and don't lay this element out if it is set to a display type of none.
  102. if (computed.display == Style::Display::None)
  103. return true;
  104. // Check for an absolute position; if this has been set, then we remove it from the flow and add it to the current
  105. // block box to be laid out and positioned once the block has been closed and sized.
  106. if (computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed)
  107. {
  108. // Display the element as a block element.
  109. block_context_box->AddAbsoluteElement(element);
  110. return true;
  111. }
  112. // If the element is floating, we remove it from the flow.
  113. if (computed.float_ != Style::Float::None)
  114. {
  115. LayoutEngine::FormatElement(element, LayoutDetails::GetContainingBlock(block_context_box));
  116. return block_context_box->AddFloatElement(element);
  117. }
  118. // The element is nothing exceptional, so we treat it as a normal block, inline or replaced element.
  119. switch (computed.display)
  120. {
  121. case Style::Display::Block: return FormatElementBlock(block_context_box, element);
  122. case Style::Display::Inline: return FormatElementInline(block_context_box, element);
  123. case Style::Display::InlineBlock: return FormatElementInlineBlock(block_context_box, element);
  124. case Style::Display::Table: return FormatElementTable(block_context_box, element);
  125. case Style::Display::TableRow:
  126. case Style::Display::TableColumn:
  127. case Style::Display::TableCell:
  128. {
  129. const Property* display_property = element->GetProperty(PropertyId::Display);
  130. Log::Message(Log::LT_WARNING, "Element has a display type '%s', but is not located in a table. It will not be formatted. In element %s",
  131. display_property ? display_property->ToString().c_str() : "*unknown*",
  132. element->GetAddress().c_str()
  133. );
  134. return true;
  135. }
  136. case Style::Display::None: RMLUI_ERROR; /* handled above */ break;
  137. }
  138. return true;
  139. }
  140. // Formats and positions an element as a block element.
  141. bool LayoutEngine::FormatElementBlock(LayoutBlockBox* block_context_box, Element* element)
  142. {
  143. RMLUI_ZoneScopedC(0x2F4F4F);
  144. Box box;
  145. float min_height, max_height;
  146. LayoutDetails::BuildBox(box, min_height, max_height, block_context_box, element, false);
  147. LayoutBlockBox* new_block_context_box = block_context_box->AddBlockElement(element, box, min_height, max_height);
  148. if (new_block_context_box == nullptr)
  149. return false;
  150. // Format the element's children.
  151. for (int i = 0; i < element->GetNumChildren(); i++)
  152. {
  153. if (!FormatElement(new_block_context_box, element->GetChild(i)))
  154. i = -1;
  155. }
  156. // Close the block box, and check the return code; we may have overflowed either this element or our parent.
  157. switch (new_block_context_box->Close())
  158. {
  159. // We need to reformat ourself; format all of our children again and close the box. No need to check for error
  160. // codes, as we already have our vertical slider bar.
  161. case LayoutBlockBox::LAYOUT_SELF:
  162. {
  163. for (int i = 0; i < element->GetNumChildren(); i++)
  164. FormatElement(new_block_context_box, element->GetChild(i));
  165. if (new_block_context_box->Close() == LayoutBlockBox::OK)
  166. {
  167. element->OnLayout();
  168. break;
  169. }
  170. }
  171. //-fallthrough
  172. // We caused our parent to add a vertical scrollbar; bail out!
  173. case LayoutBlockBox::LAYOUT_PARENT:
  174. {
  175. return false;
  176. }
  177. break;
  178. default:
  179. element->OnLayout();
  180. }
  181. return true;
  182. }
  183. // Formats and positions an element as an inline element.
  184. bool LayoutEngine::FormatElementInline(LayoutBlockBox* block_context_box, Element* element)
  185. {
  186. RMLUI_ZoneScopedC(0x3F6F6F);
  187. const Vector2f containing_block = LayoutDetails::GetContainingBlock(block_context_box);
  188. Box box;
  189. LayoutDetails::BuildBox(box, containing_block, element, true);
  190. LayoutInlineBox* inline_box = block_context_box->AddInlineElement(element, box);
  191. // Format the element's children.
  192. for (int i = 0; i < element->GetNumChildren(); i++)
  193. {
  194. if (!FormatElement(block_context_box, element->GetChild(i)))
  195. return false;
  196. }
  197. inline_box->Close();
  198. return true;
  199. }
  200. // Positions an element as a sized inline element, formatting its internal hierarchy as a block element.
  201. bool LayoutEngine::FormatElementInlineBlock(LayoutBlockBox* block_context_box, Element* element)
  202. {
  203. RMLUI_ZoneScopedC(0x1F2F2F);
  204. // Format the element separately as a block element, then position it inside our own layout as an inline element.
  205. Vector2f containing_block_size = LayoutDetails::GetContainingBlock(block_context_box);
  206. FormatElement(element, containing_block_size);
  207. block_context_box->AddInlineElement(element, element->GetBox())->Close();
  208. return true;
  209. }
  210. bool LayoutEngine::FormatElementTable(LayoutBlockBox* block_context_box, Element* element_table)
  211. {
  212. LayoutBlockBox* table_block_context_box = nullptr;
  213. {
  214. Box box;
  215. float min_height, max_height;
  216. LayoutDetails::BuildBox(box, min_height, max_height, block_context_box, element_table, false);
  217. table_block_context_box = block_context_box->AddBlockElement(element_table, box, min_height, max_height);
  218. }
  219. if (!table_block_context_box)
  220. return false;
  221. for (int i = 0; i < 2; i++)
  222. {
  223. LayoutBlockBox::CloseResult result = LayoutTable::FormatTable(table_block_context_box, element_table);
  224. // If the close failed, it probably means that the table or its parent produced scrollbars. Try again, but only once.
  225. if (result == LayoutBlockBox::LAYOUT_SELF)
  226. continue;
  227. else if (result == LayoutBlockBox::LAYOUT_PARENT)
  228. return false;
  229. else if (result == LayoutBlockBox::OK)
  230. break;
  231. }
  232. return true;
  233. }
  234. // Executes any special formatting for special elements.
  235. bool LayoutEngine::FormatElementSpecial(LayoutBlockBox* block_context_box, Element* element)
  236. {
  237. static const String br("br");
  238. // Check for a <br> tag.
  239. if (element->GetTagName() == br)
  240. {
  241. block_context_box->AddBreak();
  242. element->OnLayout();
  243. return true;
  244. }
  245. return false;
  246. }
  247. } // namespace Rml