LayoutEngine.cpp 8.4 KB

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