LayoutEngine.cpp 8.1 KB

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