LayoutEngine.cpp 23 KB

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