LayoutEngine.cpp 22 KB

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