LayoutEngine.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 "LayoutInlineBoxText.h"
  33. #include "../../Include/RmlUi/Core/Element.h"
  34. #include "../../Include/RmlUi/Core/ElementScroll.h"
  35. #include "../../Include/RmlUi/Core/ElementText.h"
  36. #include "../../Include/RmlUi/Core/Property.h"
  37. #include "../../Include/RmlUi/Core/Profiling.h"
  38. #include "../../Include/RmlUi/Core/Types.h"
  39. #include <float.h>
  40. #include <cstddef>
  41. namespace Rml {
  42. #define MAX(a, b) (a > b ? a : b)
  43. struct LayoutChunk
  44. {
  45. static const unsigned int size = MAX(sizeof(LayoutBlockBox), MAX(sizeof(LayoutInlineBox), MAX(sizeof(LayoutInlineBoxText), MAX(sizeof(LayoutLineBox), sizeof(LayoutBlockBoxSpace)))));
  46. alignas(std::max_align_t) char buffer[size];
  47. };
  48. static Pool< LayoutChunk > layout_chunk_pool(200, true);
  49. LayoutEngine::LayoutEngine()
  50. {
  51. block_box = nullptr;
  52. block_context_box = nullptr;
  53. }
  54. LayoutEngine::~LayoutEngine()
  55. {
  56. }
  57. // Formats the contents for a root-level element (usually a document or floating element).
  58. bool LayoutEngine::FormatElement(Element* element, Vector2f containing_block, bool shrink_to_fit)
  59. {
  60. #ifdef RMLUI_ENABLE_PROFILING
  61. RMLUI_ZoneScopedC(0xB22222);
  62. auto name = CreateString(80, "%s %x", element->GetAddress(false, false).c_str(), element);
  63. RMLUI_ZoneName(name.c_str(), name.size());
  64. #endif
  65. block_box = new LayoutBlockBox(this, nullptr, nullptr);
  66. block_box->GetBox().SetContent(containing_block);
  67. block_context_box = block_box->AddBlockElement(element);
  68. for (int i = 0; i < element->GetNumChildren(); i++)
  69. {
  70. if (!FormatElement(element->GetChild(i)))
  71. i = -1;
  72. }
  73. if (shrink_to_fit)
  74. {
  75. // For inline blocks with 'auto' width, we want to shrink the box back to its inner content width, recreating the LayoutBlockBox.
  76. const float content_width = block_context_box->InternalContentWidth();
  77. if (content_width < containing_block.x)
  78. {
  79. RMLUI_ZoneScopedNC("shrink_to_fit", 0xB27222);
  80. const Vector2f shrinked_block_size(content_width, containing_block.y);
  81. delete block_box;
  82. block_box = new LayoutBlockBox(this, nullptr, nullptr);
  83. block_box->GetBox().SetContent(shrinked_block_size);
  84. block_context_box = block_box->AddBlockElement(element);
  85. for (int i = 0; i < element->GetNumChildren(); i++)
  86. {
  87. if (!FormatElement(element->GetChild(i)))
  88. i = -1;
  89. }
  90. }
  91. }
  92. block_context_box->Close();
  93. block_context_box->CloseAbsoluteElements();
  94. element->OnLayout();
  95. delete block_box;
  96. return true;
  97. }
  98. // Generates the box for an element.
  99. void LayoutEngine::BuildBox(Box& box, const Vector2f& containing_block, Element* element, bool inline_element)
  100. {
  101. if (element == nullptr)
  102. {
  103. box.SetContent(containing_block);
  104. return;
  105. }
  106. const ComputedValues& computed = element->GetComputedValues();
  107. // Calculate the padding area.
  108. float padding = ResolveValue(computed.padding_top, containing_block.x);
  109. box.SetEdge(Box::PADDING, Box::TOP, Math::Max(0.0f, padding));
  110. padding = ResolveValue(computed.padding_right, containing_block.x);
  111. box.SetEdge(Box::PADDING, Box::RIGHT, Math::Max(0.0f, padding));
  112. padding = ResolveValue(computed.padding_bottom, containing_block.x);
  113. box.SetEdge(Box::PADDING, Box::BOTTOM, Math::Max(0.0f, padding));
  114. padding = ResolveValue(computed.padding_left, containing_block.x);
  115. box.SetEdge(Box::PADDING, Box::LEFT, Math::Max(0.0f, padding));
  116. // Calculate the border area.
  117. box.SetEdge(Box::BORDER, Box::TOP, Math::Max(0.0f, computed.border_top_width));
  118. box.SetEdge(Box::BORDER, Box::RIGHT, Math::Max(0.0f, computed.border_right_width));
  119. box.SetEdge(Box::BORDER, Box::BOTTOM, Math::Max(0.0f, computed.border_bottom_width));
  120. box.SetEdge(Box::BORDER, Box::LEFT, Math::Max(0.0f, computed.border_left_width));
  121. // Calculate the size of the content area.
  122. Vector2f content_area(-1, -1);
  123. bool replaced_element = false;
  124. // If the element has intrinsic dimensions, then we use those as the basis for the content area and only adjust
  125. // them if a non-auto style has been applied to them.
  126. if (element->GetIntrinsicDimensions(content_area))
  127. {
  128. replaced_element = true;
  129. Vector2f original_content_area = content_area;
  130. // The element has resized itself, so we only resize it if a RCSS width or height was set explicitly. A value of
  131. // 'auto' (or 'auto-fit', ie, both keywords) means keep (or adjust) the intrinsic dimensions.
  132. bool auto_width = false, auto_height = false;
  133. if (computed.width.type != Style::Width::Auto)
  134. content_area.x = ResolveValue(computed.width, containing_block.x);
  135. else
  136. auto_width = true;
  137. if (computed.height.type != Style::Height::Auto)
  138. content_area.y = ResolveValue(computed.height, containing_block.y);
  139. else
  140. auto_height = true;
  141. // If one of the dimensions is 'auto' then we need to scale it such that the original ratio is preserved.
  142. if (auto_width && !auto_height)
  143. content_area.x = (content_area.y / original_content_area.y) * original_content_area.x;
  144. else if (auto_height && !auto_width)
  145. content_area.y = (content_area.x / original_content_area.x) * original_content_area.y;
  146. // Reduce the width and height to make up for borders and padding.
  147. content_area.x -= (box.GetEdge(Box::BORDER, Box::LEFT) +
  148. box.GetEdge(Box::PADDING, Box::LEFT) +
  149. box.GetEdge(Box::BORDER, Box::RIGHT) +
  150. box.GetEdge(Box::PADDING, Box::RIGHT));
  151. content_area.y -= (box.GetEdge(Box::BORDER, Box::TOP) +
  152. box.GetEdge(Box::PADDING, Box::TOP) +
  153. box.GetEdge(Box::BORDER, Box::BOTTOM) +
  154. box.GetEdge(Box::PADDING, Box::BOTTOM));
  155. content_area.x = Math::Max(content_area.x, 0.0f);
  156. content_area.y = Math::Max(content_area.y, 0.0f);
  157. }
  158. // If the element is inline, then its calculations are much more straightforward (no worrying about auto margins
  159. // and dimensions, etc). All we do is calculate the margins, set the content area and bail.
  160. if (inline_element)
  161. {
  162. if (replaced_element)
  163. {
  164. content_area.x = ClampWidth(content_area.x, computed, containing_block.x);
  165. content_area.y = ClampHeight(content_area.y, computed, containing_block.y);
  166. }
  167. // If the element was not replaced, then we leave its dimension as unsized (-1, -1) and ignore the width and
  168. // height properties.
  169. box.SetContent(content_area);
  170. // Evaluate the margins. Any declared as 'auto' will resolve to 0.
  171. box.SetEdge(Box::MARGIN, Box::TOP, ResolveValue(computed.margin_top, containing_block.x));
  172. box.SetEdge(Box::MARGIN, Box::RIGHT, ResolveValue(computed.margin_right, containing_block.x));
  173. box.SetEdge(Box::MARGIN, Box::BOTTOM, ResolveValue(computed.margin_bottom, containing_block.x));
  174. box.SetEdge(Box::MARGIN, Box::LEFT, ResolveValue(computed.margin_left, containing_block.x));
  175. }
  176. // The element is block, so we need to run the box through the ringer to potentially evaluate auto margins and
  177. // dimensions.
  178. else
  179. {
  180. box.SetContent(content_area);
  181. BuildBoxWidth(box, computed, containing_block.x);
  182. BuildBoxHeight(box, computed, containing_block.y);
  183. }
  184. }
  185. // Generates the box for an element placed in a block box.
  186. void LayoutEngine::BuildBox(Box& box, float& min_height, float& max_height, LayoutBlockBox* containing_box, Element* element, bool inline_element)
  187. {
  188. Vector2f containing_block = GetContainingBlock(containing_box);
  189. BuildBox(box, containing_block, element, inline_element);
  190. float box_height = box.GetSize().y;
  191. if (box_height < 0)
  192. {
  193. auto& computed = element->GetComputedValues();
  194. min_height = ResolveValue(computed.min_height, containing_block.y);
  195. max_height = (computed.max_height.value < 0.f ? FLT_MAX : ResolveValue(computed.max_height, containing_block.y));
  196. }
  197. else
  198. {
  199. min_height = box_height;
  200. max_height = box_height;
  201. }
  202. }
  203. // Clamps the width of an element based from its min-width and max-width properties.
  204. float LayoutEngine::ClampWidth(float width, const ComputedValues& computed, float containing_block_width)
  205. {
  206. float min_width = ResolveValue(computed.min_width, containing_block_width);
  207. float max_width = (computed.max_width.value < 0.f ? FLT_MAX : ResolveValue(computed.max_width, containing_block_width));
  208. return Math::Clamp(width, min_width, max_width);
  209. }
  210. // Clamps the height of an element based from its min-height and max-height properties.
  211. float LayoutEngine::ClampHeight(float height, const ComputedValues& computed, float containing_block_height)
  212. {
  213. float min_height = ResolveValue(computed.min_height, containing_block_height);
  214. float max_height = (computed.max_height.value < 0.f ? FLT_MAX : ResolveValue(computed.max_height, containing_block_height));
  215. return Math::Clamp(height, min_height, max_height);
  216. }
  217. void* LayoutEngine::AllocateLayoutChunk(size_t size)
  218. {
  219. RMLUI_ASSERT(size <= LayoutChunk::size);
  220. (void)size;
  221. return layout_chunk_pool.AllocateAndConstruct();
  222. }
  223. void LayoutEngine::DeallocateLayoutChunk(void* chunk)
  224. {
  225. layout_chunk_pool.DestroyAndDeallocate((LayoutChunk*) chunk);
  226. }
  227. // Positions a single element and its children within this layout.
  228. bool LayoutEngine::FormatElement(Element* element)
  229. {
  230. #ifdef RMLUI_ENABLE_PROFILING
  231. RMLUI_ZoneScoped;
  232. auto name = CreateString(80, ">%s %x", element->GetAddress(false, false).c_str(), element);
  233. RMLUI_ZoneName(name.c_str(), name.size());
  234. #endif
  235. auto& computed = element->GetComputedValues();
  236. // Check if we have to do any special formatting for any elements that don't fit into the standard layout scheme.
  237. if (FormatElementSpecial(element))
  238. return true;
  239. // Fetch the display property, and don't lay this element out if it is set to a display type of none.
  240. if (computed.display == Style::Display::None)
  241. return true;
  242. // Check for an absolute position; if this has been set, then we remove it from the flow and add it to the current
  243. // block box to be laid out and positioned once the block has been closed and sized.
  244. if (computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed)
  245. {
  246. // Display the element as a block element.
  247. block_context_box->AddAbsoluteElement(element);
  248. return true;
  249. }
  250. // If the element is floating, we remove it from the flow.
  251. if (computed.float_ != Style::Float::None)
  252. {
  253. // Format the element as a block element.
  254. LayoutEngine layout_engine;
  255. bool shrink_to_fit = (computed.width.type == Style::Width::Auto);
  256. // Don't shrink replaced elements.
  257. if (shrink_to_fit)
  258. {
  259. Vector2f unused_intrinsic_dimensions;
  260. shrink_to_fit = !element->GetIntrinsicDimensions(unused_intrinsic_dimensions);
  261. }
  262. layout_engine.FormatElement(element, GetContainingBlock(block_context_box), shrink_to_fit);
  263. return block_context_box->AddFloatElement(element);
  264. }
  265. // The element is nothing exceptional, so we treat it as a normal block, inline or replaced element.
  266. switch (computed.display)
  267. {
  268. case Style::Display::Block: return FormatElementBlock(element); break;
  269. case Style::Display::Inline: return FormatElementInline(element); break;
  270. case Style::Display::InlineBlock: return FormatElementInlineBlock(element); break;
  271. default: RMLUI_ERROR;
  272. }
  273. return true;
  274. }
  275. // Formats and positions an element as a block element.
  276. bool LayoutEngine::FormatElementBlock(Element* element)
  277. {
  278. RMLUI_ZoneScopedC(0x2F4F4F);
  279. LayoutBlockBox* new_block_context_box = block_context_box->AddBlockElement(element);
  280. if (new_block_context_box == nullptr)
  281. return false;
  282. block_context_box = new_block_context_box;
  283. // Format the element's children.
  284. for (int i = 0; i < element->GetNumChildren(); i++)
  285. {
  286. if (!FormatElement(element->GetChild(i)))
  287. i = -1;
  288. }
  289. // Close the block box, and check the return code; we may have overflowed either this element or our parent.
  290. new_block_context_box = block_context_box->GetParent();
  291. switch (block_context_box->Close())
  292. {
  293. // We need to reformat ourself; format all of our children again and close the box. No need to check for error
  294. // codes, as we already have our vertical slider bar.
  295. case LayoutBlockBox::LAYOUT_SELF:
  296. {
  297. for (int i = 0; i < element->GetNumChildren(); i++)
  298. FormatElement(element->GetChild(i));
  299. if (block_context_box->Close() == LayoutBlockBox::OK)
  300. {
  301. element->OnLayout();
  302. break;
  303. }
  304. }
  305. //-fallthrough
  306. // We caused our parent to add a vertical scrollbar; bail out!
  307. case LayoutBlockBox::LAYOUT_PARENT:
  308. {
  309. block_context_box = new_block_context_box;
  310. return false;
  311. }
  312. break;
  313. default:
  314. element->OnLayout();
  315. }
  316. block_context_box = new_block_context_box;
  317. return true;
  318. }
  319. // Formats and positions an element as an inline element.
  320. bool LayoutEngine::FormatElementInline(Element* element)
  321. {
  322. RMLUI_ZoneScopedC(0x3F6F6F);
  323. Box box;
  324. float min_height, max_height;
  325. BuildBox(box, min_height, max_height, block_context_box, element, true);
  326. LayoutInlineBox* inline_box = block_context_box->AddInlineElement(element, box);
  327. // Format the element's children.
  328. for (int i = 0; i < element->GetNumChildren(); i++)
  329. {
  330. if (!FormatElement(element->GetChild(i)))
  331. return false;
  332. }
  333. inline_box->Close();
  334. return true;
  335. }
  336. // Positions an element as a sized inline element, formatting its internal hierarchy as a block element.
  337. bool LayoutEngine::FormatElementInlineBlock(Element* element)
  338. {
  339. RMLUI_ZoneScopedC(0x1F2F2F);
  340. // Format the element separately as a block element, then position it inside our own layout as an inline element.
  341. Vector2f containing_block_size = GetContainingBlock(block_context_box);
  342. LayoutEngine layout_engine;
  343. bool shrink_to_fit = element->GetComputedValues().width.type == Style::Width::Auto;
  344. // Don't shrink replaced elements.
  345. if (shrink_to_fit)
  346. {
  347. Vector2f unused_intrinsic_dimensions;
  348. shrink_to_fit = !element->GetIntrinsicDimensions(unused_intrinsic_dimensions);
  349. }
  350. layout_engine.FormatElement(element, containing_block_size, shrink_to_fit);
  351. block_context_box->AddInlineElement(element, element->GetBox())->Close();
  352. return true;
  353. }
  354. // Executes any special formatting for special elements.
  355. bool LayoutEngine::FormatElementSpecial(Element* element)
  356. {
  357. static const String br("br");
  358. // Check for a <br> tag.
  359. if (element->GetTagName() == br)
  360. {
  361. block_context_box->AddBreak();
  362. element->OnLayout();
  363. return true;
  364. }
  365. return false;
  366. }
  367. // Returns the fully-resolved, fixed-width and -height containing block from a block box.
  368. Vector2f LayoutEngine::GetContainingBlock(const LayoutBlockBox* containing_box)
  369. {
  370. Vector2f containing_block;
  371. containing_block.x = containing_box->GetBox().GetSize(Box::CONTENT).x;
  372. if (containing_box->GetElement() != nullptr)
  373. containing_block.x -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  374. while ((containing_block.y = containing_box->GetBox().GetSize(Box::CONTENT).y) < 0)
  375. {
  376. containing_box = containing_box->GetParent();
  377. if (containing_box == nullptr)
  378. {
  379. RMLUI_ERROR;
  380. containing_block.y = 0;
  381. }
  382. }
  383. if (containing_box != nullptr &&
  384. containing_box->GetElement() != nullptr)
  385. containing_block.y -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL);
  386. containing_block.x = Math::Max(0.0f, containing_block.x);
  387. containing_block.y = Math::Max(0.0f, containing_block.y);
  388. return containing_block;
  389. }
  390. // Builds the block-specific width and horizontal margins of a Box.
  391. void LayoutEngine::BuildBoxWidth(Box& box, const ComputedValues& computed, float containing_block_width)
  392. {
  393. RMLUI_ZoneScoped;
  394. Vector2f content_area = box.GetSize();
  395. // Determine if the element has an automatic width, and if not calculate it.
  396. bool width_auto;
  397. if (content_area.x >= 0)
  398. {
  399. width_auto = false;
  400. }
  401. else
  402. {
  403. if (computed.width.type == Style::Width::Auto)
  404. {
  405. width_auto = true;
  406. }
  407. else
  408. {
  409. width_auto = false;
  410. content_area.x = ResolveValue(computed.width, containing_block_width);
  411. }
  412. }
  413. // Determine if the element has automatic margins.
  414. bool margins_auto[2];
  415. int num_auto_margins = 0;
  416. for (int i = 0; i < 2; ++i)
  417. {
  418. auto* margin_value = (i == 0 ? &computed.margin_left : &computed.margin_right);
  419. if (margin_value->type == Style::Margin::Auto)
  420. {
  421. margins_auto[i] = true;
  422. num_auto_margins++;
  423. }
  424. else
  425. {
  426. margins_auto[i] = false;
  427. box.SetEdge(Box::MARGIN, i == 0 ? Box::LEFT : Box::RIGHT, ResolveValue(*margin_value, containing_block_width));
  428. }
  429. }
  430. // If the width is set to auto, we need to calculate the width
  431. if (width_auto)
  432. {
  433. float left = 0.0f, right = 0.0f;
  434. // If we are dealing with an absolutely positioned element we need to
  435. // consider if the left and right properties are set, since the width can be affected.
  436. if (computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed)
  437. {
  438. if (computed.left.type != Style::Left::Auto)
  439. left = ResolveValue(computed.left, containing_block_width );
  440. if (computed.right.type != Style::Right::Auto)
  441. right = ResolveValue(computed.right, containing_block_width);
  442. }
  443. // We resolve any auto margins to 0 and the width is set to whatever is left of the containing block.
  444. if (margins_auto[0])
  445. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  446. if (margins_auto[1])
  447. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  448. content_area.x = containing_block_width - (left +
  449. box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  450. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  451. right);
  452. content_area.x = Math::Max(0.0f, content_area.x);
  453. }
  454. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  455. else if (num_auto_margins > 0)
  456. {
  457. float margin = (containing_block_width - (box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  458. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  459. content_area.x)) / num_auto_margins;
  460. if (margins_auto[0])
  461. box.SetEdge(Box::MARGIN, Box::LEFT, margin);
  462. if (margins_auto[1])
  463. box.SetEdge(Box::MARGIN, Box::RIGHT, margin);
  464. }
  465. // Clamp the calculated width; if the width is changed by the clamp, then the margins need to be recalculated if
  466. // they were set to auto.
  467. float clamped_width = ClampWidth(content_area.x, computed, containing_block_width);
  468. if (clamped_width != content_area.x)
  469. {
  470. content_area.x = clamped_width;
  471. box.SetContent(content_area);
  472. if (num_auto_margins > 0)
  473. {
  474. // Reset the automatic margins.
  475. if (margins_auto[0])
  476. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  477. if (margins_auto[1])
  478. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  479. BuildBoxWidth(box, computed, containing_block_width);
  480. }
  481. }
  482. else
  483. box.SetContent(content_area);
  484. }
  485. // Builds the block-specific height and vertical margins of a Box.
  486. void LayoutEngine::BuildBoxHeight(Box& box, const ComputedValues& computed, float containing_block_height)
  487. {
  488. RMLUI_ZoneScoped;
  489. Vector2f content_area = box.GetSize();
  490. // Determine if the element has an automatic height, and if not calculate it.
  491. bool height_auto;
  492. if (content_area.y >= 0)
  493. {
  494. height_auto = false;
  495. }
  496. else
  497. {
  498. if (computed.height.type == Style::Height::Auto)
  499. {
  500. height_auto = true;
  501. }
  502. else
  503. {
  504. height_auto = false;
  505. content_area.y = ResolveValue(computed.height, containing_block_height);
  506. }
  507. }
  508. // Determine if the element has automatic margins.
  509. bool margins_auto[2];
  510. int num_auto_margins = 0;
  511. for (int i = 0; i < 2; ++i)
  512. {
  513. auto* margin_value = (i == 0 ? &computed.margin_top : &computed.margin_bottom);
  514. if (margin_value->type == Style::Margin::Auto)
  515. {
  516. margins_auto[i] = true;
  517. num_auto_margins++;
  518. }
  519. else
  520. {
  521. margins_auto[i] = false;
  522. box.SetEdge(Box::MARGIN, i == 0 ? Box::TOP : Box::BOTTOM, ResolveValue(*margin_value, containing_block_height));
  523. }
  524. }
  525. // If the height is set to auto, we need to calculate the height
  526. if (height_auto)
  527. {
  528. // We resolve any auto margins to 0
  529. if (margins_auto[0])
  530. box.SetEdge(Box::MARGIN, Box::TOP, 0);
  531. if (margins_auto[1])
  532. box.SetEdge(Box::MARGIN, Box::BOTTOM, 0);
  533. // If the height is set to auto for a box in normal flow, the height is set to -1.
  534. content_area.y = -1;
  535. // But if we are dealing with an absolutely positioned element we need to
  536. // consider if the top and bottom properties are set, since the height can be affected.
  537. if (computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed)
  538. {
  539. float top = 0.0f, bottom = 0.0f;
  540. if (computed.top.type != Style::Top::Auto && computed.bottom.type != Style::Bottom::Auto)
  541. {
  542. top = ResolveValue(computed.top, containing_block_height );
  543. bottom = ResolveValue(computed.bottom, containing_block_height );
  544. // The height gets resolved to whatever is left of the containing block
  545. content_area.y = containing_block_height - (top +
  546. box.GetCumulativeEdge(Box::CONTENT, Box::TOP) +
  547. box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM) +
  548. bottom);
  549. content_area.y = Math::Max(0.0f, content_area.y);
  550. }
  551. }
  552. }
  553. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  554. else if (num_auto_margins > 0)
  555. {
  556. float margin;
  557. if (content_area.y >= 0)
  558. {
  559. margin = (containing_block_height - (box.GetCumulativeEdge(Box::CONTENT, Box::TOP) +
  560. box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM) +
  561. content_area.y)) / num_auto_margins;
  562. }
  563. else
  564. margin = 0;
  565. if (margins_auto[0])
  566. box.SetEdge(Box::MARGIN, Box::TOP, margin);
  567. if (margins_auto[1])
  568. box.SetEdge(Box::MARGIN, Box::BOTTOM, margin);
  569. }
  570. if (content_area.y >= 0)
  571. {
  572. // Clamp the calculated height; if the height is changed by the clamp, then the margins need to be recalculated if
  573. // they were set to auto.
  574. float clamped_height = ClampHeight(content_area.y, computed, containing_block_height);
  575. if (clamped_height != content_area.y)
  576. {
  577. content_area.y = clamped_height;
  578. box.SetContent(content_area);
  579. if (num_auto_margins > 0)
  580. {
  581. // Reset the automatic margins.
  582. if (margins_auto[0])
  583. box.SetEdge(Box::MARGIN, Box::TOP, 0);
  584. if (margins_auto[1])
  585. box.SetEdge(Box::MARGIN, Box::BOTTOM, 0);
  586. BuildBoxHeight(box, computed, containing_block_height);
  587. }
  588. return;
  589. }
  590. }
  591. box.SetContent(content_area);
  592. }
  593. } // namespace Rml