LayoutEngine.cpp 23 KB

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