LayoutEngine.cpp 11 KB

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