LayoutEngine.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 <Rocket/Core/Math.h>
  30. #include "Pool.h"
  31. #include "LayoutBlockBoxSpace.h"
  32. #include "LayoutInlineBoxText.h"
  33. #include <Rocket/Core/Element.h>
  34. #include <Rocket/Core/ElementScroll.h>
  35. #include <Rocket/Core/ElementText.h>
  36. #include <Rocket/Core/Property.h>
  37. #include <Rocket/Core/Types.h>
  38. #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 LayoutChunk
  44. {
  45. LayoutChunk()
  46. {
  47. }
  48. static const unsigned int size = MAX(sizeof(LayoutBlockBox), MAX(sizeof(LayoutInlineBox), MAX(sizeof(LayoutInlineBoxText), MAX(sizeof(LayoutLineBox), sizeof(LayoutBlockBoxSpace)))));
  49. char buffer[size];
  50. };
  51. static Pool< LayoutChunk > layout_chunk_pool(200, true);
  52. LayoutEngine::LayoutEngine()
  53. {
  54. block_box = NULL;
  55. block_context_box = NULL;
  56. }
  57. LayoutEngine::~LayoutEngine()
  58. {
  59. }
  60. // Formats the contents for a root-level element (usually a document or floating element).
  61. bool LayoutEngine::FormatElement(Element* element, const Vector2f& containing_block)
  62. {
  63. block_box = new LayoutBlockBox(this, NULL, NULL);
  64. block_box->GetBox().SetContent(containing_block);
  65. block_context_box = block_box->AddBlockElement(element);
  66. for (int i = 0; i < element->GetNumChildren(); i++)
  67. {
  68. if (!FormatElement(element->GetChild(i)))
  69. i = -1;
  70. }
  71. block_context_box->Close();
  72. block_context_box->CloseAbsoluteElements();
  73. element->OnLayout();
  74. delete block_box;
  75. return true;
  76. }
  77. // Generates the box for an element.
  78. void LayoutEngine::BuildBox(Box& box, const Vector2f& containing_block, Element* element, bool inline_element)
  79. {
  80. if (element == NULL)
  81. {
  82. box.SetContent(containing_block);
  83. return;
  84. }
  85. // Calculate the padding area.
  86. float padding = element->ResolveProperty(PADDING_TOP, containing_block.x);
  87. box.SetEdge(Box::PADDING, Box::TOP, Math::Max(0.0f, padding));
  88. padding = element->ResolveProperty(PADDING_RIGHT, containing_block.x);
  89. box.SetEdge(Box::PADDING, Box::RIGHT, Math::Max(0.0f, padding));
  90. padding = element->ResolveProperty(PADDING_BOTTOM, containing_block.x);
  91. box.SetEdge(Box::PADDING, Box::BOTTOM, Math::Max(0.0f, padding));
  92. padding = element->ResolveProperty(PADDING_LEFT, containing_block.x);
  93. box.SetEdge(Box::PADDING, Box::LEFT, Math::Max(0.0f, padding));
  94. // Calculate the border area.
  95. float border = element->ResolveProperty(BORDER_TOP_WIDTH, containing_block.x);
  96. box.SetEdge(Box::BORDER, Box::TOP, Math::Max(0.0f, border));
  97. border = element->ResolveProperty(BORDER_RIGHT_WIDTH, containing_block.x);
  98. box.SetEdge(Box::BORDER, Box::RIGHT, Math::Max(0.0f, border));
  99. border = element->ResolveProperty(BORDER_BOTTOM_WIDTH, containing_block.x);
  100. box.SetEdge(Box::BORDER, Box::BOTTOM, Math::Max(0.0f, border));
  101. border = element->ResolveProperty(BORDER_LEFT_WIDTH, containing_block.x);
  102. box.SetEdge(Box::BORDER, Box::LEFT, Math::Max(0.0f, border));
  103. // Calculate the size of the content area.
  104. Vector2f content_area(-1, -1);
  105. bool replaced_element = false;
  106. // If the element has intrinsic dimensions, then we use those as the basis for the content area and only adjust
  107. // them if a non-auto style has been applied to them.
  108. if (element->GetIntrinsicDimensions(content_area))
  109. {
  110. replaced_element = true;
  111. // The element has resized itself, so we only resize it if a RCSS width or height was set explicitly. A value of
  112. // 'auto' (or 'auto-fit', ie, both keywords) means keep the intrinsic dimensions.
  113. const Property* width_property = element->GetProperty(WIDTH);
  114. if (width_property->unit != Property::KEYWORD)
  115. content_area.x = element->ResolveProperty(WIDTH, containing_block.x);
  116. const Property* height_property = element->GetProperty(HEIGHT);
  117. if (height_property->unit != Property::KEYWORD)
  118. content_area.y = element->ResolveProperty(HEIGHT, containing_block.y);
  119. // Reduce the width and height to make up for borders and padding.
  120. content_area.x -= (box.GetEdge(Box::BORDER, Box::LEFT) +
  121. box.GetEdge(Box::PADDING, Box::LEFT) +
  122. box.GetEdge(Box::BORDER, Box::RIGHT) +
  123. box.GetEdge(Box::PADDING, Box::RIGHT));
  124. content_area.y -= (box.GetEdge(Box::BORDER, Box::TOP) +
  125. box.GetEdge(Box::PADDING, Box::TOP) +
  126. box.GetEdge(Box::BORDER, Box::BOTTOM) +
  127. box.GetEdge(Box::PADDING, Box::BOTTOM));
  128. content_area.x = Math::Max(content_area.x, 0.0f);
  129. content_area.y = Math::Max(content_area.y, 0.0f);
  130. }
  131. // If the element is inline, then its calculations are much more straightforward (no worrying about auto margins
  132. // and dimensions, etc). All we do is calculate the margins, set the content area and bail.
  133. if (inline_element)
  134. {
  135. if (replaced_element)
  136. {
  137. content_area.x = ClampWidth(content_area.x, element, containing_block.x);
  138. content_area.y = ClampWidth(content_area.y, element, containing_block.y);
  139. }
  140. // If the element was not replaced, then we leave its dimension as unsized (-1, -1) and ignore the width and
  141. // height properties.
  142. box.SetContent(content_area);
  143. // Evaluate the margins. Any declared as 'auto' will resolve to 0.
  144. box.SetEdge(Box::MARGIN, Box::TOP, element->ResolveProperty(MARGIN_TOP, containing_block.x));
  145. box.SetEdge(Box::MARGIN, Box::RIGHT, element->ResolveProperty(MARGIN_RIGHT, containing_block.x));
  146. box.SetEdge(Box::MARGIN, Box::BOTTOM, element->ResolveProperty(MARGIN_BOTTOM, containing_block.x));
  147. box.SetEdge(Box::MARGIN, Box::LEFT, element->ResolveProperty(MARGIN_LEFT, containing_block.x));
  148. }
  149. // The element is block, so we need to run the box through the ringer to potentially evaluate auto margins and
  150. // dimensions.
  151. else
  152. {
  153. box.SetContent(content_area);
  154. BuildBoxWidth(box, element, containing_block.x);
  155. BuildBoxHeight(box, element, containing_block.y);
  156. }
  157. }
  158. // Generates the box for an element placed in a block box.
  159. void LayoutEngine::BuildBox(Box& box, float& min_height, float& max_height, LayoutBlockBox* containing_box, Element* element, bool inline_element)
  160. {
  161. Vector2f containing_block = GetContainingBlock(containing_box);
  162. BuildBox(box, GetContainingBlock(containing_box), element, inline_element);
  163. float box_height = box.GetSize().y;
  164. if (box_height < 0)
  165. {
  166. if (element->GetLocalProperty(MIN_HEIGHT) != NULL)
  167. min_height = element->ResolveProperty(MIN_HEIGHT, containing_block.y);
  168. else
  169. min_height = 0;
  170. if (element->GetLocalProperty(MAX_HEIGHT) != NULL)
  171. max_height = element->ResolveProperty(MAX_HEIGHT, containing_block.y);
  172. else
  173. max_height = FLT_MAX;
  174. }
  175. else
  176. {
  177. min_height = box_height;
  178. max_height = box_height;
  179. }
  180. }
  181. // Clamps the width of an element based from its min-width and max-width properties.
  182. float LayoutEngine::ClampWidth(float width, Element* element, float containing_block_width)
  183. {
  184. float min_width, max_width;
  185. if (element->GetLocalProperty(MIN_WIDTH) != NULL)
  186. min_width = element->ResolveProperty(MIN_WIDTH, containing_block_width);
  187. else
  188. min_width = 0;
  189. if (element->GetLocalProperty(MAX_WIDTH) != NULL)
  190. max_width = element->ResolveProperty(MAX_WIDTH, containing_block_width);
  191. else
  192. max_width = FLT_MAX;
  193. return Math::Clamp(width, min_width, max_width);
  194. }
  195. // Clamps the height of an element based from its min-height and max-height properties.
  196. float LayoutEngine::ClampHeight(float height, Element* element, float containing_block_height)
  197. {
  198. float min_height, max_height;
  199. if (element->GetLocalProperty(MIN_HEIGHT) != NULL)
  200. min_height = element->ResolveProperty(MIN_HEIGHT, containing_block_height);
  201. else
  202. min_height = 0;
  203. if (element->GetLocalProperty(MAX_HEIGHT) != NULL)
  204. max_height = element->ResolveProperty(MAX_HEIGHT, containing_block_height);
  205. else
  206. max_height = FLT_MAX;
  207. return Math::Clamp(height, min_height, max_height);
  208. }
  209. // Rounds a vector of two floating-point values to integral values.
  210. Vector2f& LayoutEngine::Round(Vector2f& value)
  211. {
  212. value.x = Round(value.x);
  213. value.y = Round(value.y);
  214. return value;
  215. }
  216. // Rounds a floating-point value to an integral value.
  217. float LayoutEngine::Round(float value)
  218. {
  219. return ceilf(value);
  220. }
  221. void* LayoutEngine::AllocateLayoutChunk(size_t size)
  222. {
  223. (size);
  224. ROCKET_ASSERT(size <= LayoutChunk::size);
  225. return layout_chunk_pool.AllocateObject();
  226. }
  227. void LayoutEngine::DeallocateLayoutChunk(void* chunk)
  228. {
  229. layout_chunk_pool.DeallocateObject((LayoutChunk*) chunk);
  230. }
  231. // Positions a single element and its children within this layout.
  232. bool LayoutEngine::FormatElement(Element* element)
  233. {
  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. int display_property = element->GetProperty< int >(DISPLAY);
  239. if (display_property == DISPLAY_NONE)
  240. return true;
  241. // Check for an absolute position; if this has been set, then we remove it from the flow and add it to the current
  242. // block box to be laid out and positioned once the block has been closed and sized.
  243. int position_property = element->GetProperty< int >(POSITION);
  244. if (position_property == POSITION_ABSOLUTE ||
  245. position_property == POSITION_FIXED)
  246. {
  247. // Display the element as a block element.
  248. block_context_box->AddAbsoluteElement(element);
  249. return true;
  250. }
  251. // If the element is floating, we remove it from the flow.
  252. int float_property = element->GetProperty< int >(FLOAT);
  253. if (float_property != FLOAT_NONE)
  254. {
  255. // Format the element as a block element.
  256. LayoutEngine layout_engine;
  257. layout_engine.FormatElement(element, GetContainingBlock(block_context_box));
  258. return block_context_box->AddFloatElement(element);
  259. }
  260. // The element is nothing exceptional, so we treat it as a normal block, inline or replaced element.
  261. switch (display_property)
  262. {
  263. case DISPLAY_BLOCK: return FormatElementBlock(element); break;
  264. case DISPLAY_INLINE: return FormatElementInline(element); break;
  265. case DISPLAY_INLINE_BLOCK: FormatElementReplaced(element); break;
  266. default: ROCKET_ERROR;
  267. }
  268. return true;
  269. }
  270. // Formats and positions an element as a block element.
  271. bool LayoutEngine::FormatElementBlock(Element* element)
  272. {
  273. LayoutBlockBox* new_block_context_box = block_context_box->AddBlockElement(element);
  274. if (new_block_context_box == NULL)
  275. return false;
  276. block_context_box = new_block_context_box;
  277. // Format the element's children.
  278. for (int i = 0; i < element->GetNumChildren(); i++)
  279. {
  280. if (!FormatElement(element->GetChild(i)))
  281. i = -1;
  282. }
  283. // Close the block box, and check the return code; we may have overflowed either this element or our parent.
  284. new_block_context_box = block_context_box->GetParent();
  285. switch (block_context_box->Close())
  286. {
  287. // We need to reformat ourself; format all of our children again and close the box. No need to check for error
  288. // codes, as we already have our vertical slider bar.
  289. case LayoutBlockBox::LAYOUT_SELF:
  290. {
  291. for (int i = 0; i < element->GetNumChildren(); i++)
  292. FormatElement(element->GetChild(i));
  293. if (block_context_box->Close() == LayoutBlockBox::OK)
  294. {
  295. element->OnLayout();
  296. break;
  297. }
  298. }
  299. // We caused our parent to add a vertical scrollbar; bail out!
  300. case LayoutBlockBox::LAYOUT_PARENT:
  301. {
  302. block_context_box = new_block_context_box;
  303. return false;
  304. }
  305. break;
  306. default:
  307. element->OnLayout();
  308. }
  309. block_context_box = new_block_context_box;
  310. return true;
  311. }
  312. // Formats and positions an element as an inline element.
  313. bool LayoutEngine::FormatElementInline(Element* element)
  314. {
  315. Box box;
  316. float min_height, max_height;
  317. BuildBox(box, min_height, max_height, block_context_box, element, true);
  318. LayoutInlineBox* inline_box = block_context_box->AddInlineElement(element, box);
  319. // Format the element's children.
  320. for (int i = 0; i < element->GetNumChildren(); i++)
  321. {
  322. if (!FormatElement(element->GetChild(i)))
  323. return false;
  324. }
  325. inline_box->Close();
  326. // element->OnLayout();
  327. return true;
  328. }
  329. // Positions an element as a sized inline element, formatting its internal hierarchy as a block element.
  330. void LayoutEngine::FormatElementReplaced(Element* element)
  331. {
  332. // Format the element separately as a block element, then position it inside our own layout as an inline element.
  333. LayoutEngine layout_engine;
  334. layout_engine.FormatElement(element, GetContainingBlock(block_context_box));
  335. block_context_box->AddInlineElement(element, element->GetBox())->Close();
  336. }
  337. // Executes any special formatting for special elements.
  338. bool LayoutEngine::FormatElementSpecial(Element* element)
  339. {
  340. static String br("br");
  341. // Check for a <br> tag.
  342. if (element->GetTagName() == br)
  343. {
  344. block_context_box->AddBreak();
  345. element->OnLayout();
  346. return true;
  347. }
  348. return false;
  349. }
  350. // Returns the fully-resolved, fixed-width and -height containing block from a block box.
  351. Vector2f LayoutEngine::GetContainingBlock(const LayoutBlockBox* containing_box)
  352. {
  353. Vector2f containing_block;
  354. containing_block.x = containing_box->GetBox().GetSize(Box::CONTENT).x;
  355. if (containing_box->GetElement() != NULL)
  356. containing_block.x -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  357. while ((containing_block.y = containing_box->GetBox().GetSize(Box::CONTENT).y) < 0)
  358. {
  359. containing_box = containing_box->GetParent();
  360. if (containing_box == NULL)
  361. {
  362. ROCKET_ERROR;
  363. containing_block.y = 0;
  364. }
  365. }
  366. if (containing_box != NULL &&
  367. containing_box->GetElement() != NULL)
  368. containing_block.y -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL);
  369. containing_block.x = Math::Max(0.0f, containing_block.x);
  370. containing_block.y = Math::Max(0.0f, containing_block.y);
  371. return containing_block;
  372. }
  373. // Builds the block-specific width and horizontal margins of a Box.
  374. void LayoutEngine::BuildBoxWidth(Box& box, Element* element, float containing_block_width)
  375. {
  376. Vector2f content_area = box.GetSize();
  377. // Determine if the element has an automatic width, and if not calculate it.
  378. bool width_auto;
  379. if (content_area.x >= 0)
  380. width_auto = false;
  381. else
  382. {
  383. const Property* width_property = element->GetProperty(WIDTH);
  384. if (width_property->unit == Property::KEYWORD)
  385. {
  386. width_auto = true;
  387. }
  388. else
  389. {
  390. width_auto = false;
  391. content_area.x = element->ResolveProperty(WIDTH, containing_block_width);
  392. }
  393. }
  394. // Determine if the element has automatic margins.
  395. bool margins_auto[2];
  396. int num_auto_margins = 0;
  397. for (int i = 0; i < 2; ++i)
  398. {
  399. const String& property_name = i == 0 ? MARGIN_LEFT : MARGIN_RIGHT;
  400. const Property* margin_property = element->GetLocalProperty(property_name);
  401. if (margin_property != NULL &&
  402. margin_property->unit == Property::KEYWORD)
  403. {
  404. margins_auto[i] = true;
  405. num_auto_margins++;
  406. }
  407. else
  408. {
  409. margins_auto[i] = false;
  410. box.SetEdge(Box::MARGIN, i == 0 ? Box::LEFT : Box::RIGHT, element->ResolveProperty(property_name, containing_block_width));
  411. }
  412. }
  413. // If the width is set to auto, then any margins also set to auto are resolved to 0 and the width is set to the
  414. // whatever if left of the containing block.
  415. if (width_auto)
  416. {
  417. if (margins_auto[0])
  418. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  419. if (margins_auto[1])
  420. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  421. content_area.x = containing_block_width - (box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  422. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT));
  423. content_area.x = Math::Max(0.0f, content_area.x);
  424. }
  425. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  426. else if (num_auto_margins > 0)
  427. {
  428. float margin = (containing_block_width - (box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  429. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  430. content_area.x)) / num_auto_margins;
  431. if (margins_auto[0])
  432. box.SetEdge(Box::MARGIN, Box::LEFT, margin);
  433. if (margins_auto[1])
  434. box.SetEdge(Box::MARGIN, Box::RIGHT, margin);
  435. }
  436. // Clamp the calculated width; if the width is changed by the clamp, then the margins need to be recalculated if
  437. // they were set to auto.
  438. float clamped_width = ClampWidth(content_area.x, element, containing_block_width);
  439. if (clamped_width != content_area.x)
  440. {
  441. content_area.x = clamped_width;
  442. box.SetContent(content_area);
  443. if (num_auto_margins > 0)
  444. {
  445. // Reset the automatic margins.
  446. if (margins_auto[0])
  447. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  448. if (margins_auto[1])
  449. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  450. BuildBoxWidth(box, element, containing_block_width);
  451. }
  452. }
  453. else
  454. box.SetContent(content_area);
  455. }
  456. // Builds the block-specific height and vertical margins of a Box.
  457. void LayoutEngine::BuildBoxHeight(Box& box, Element* element, float containing_block_height)
  458. {
  459. Vector2f content_area = box.GetSize();
  460. // Determine if the element has an automatic height, and if not calculate it.
  461. bool height_auto;
  462. if (content_area.y >= 0)
  463. height_auto = false;
  464. else
  465. {
  466. const Property* height_property = element->GetProperty(HEIGHT);
  467. if (height_property->unit == Property::KEYWORD)
  468. {
  469. height_auto = true;
  470. }
  471. else
  472. {
  473. height_auto = false;
  474. if (height_property != NULL)
  475. content_area.y = element->ResolveProperty(HEIGHT, containing_block_height);
  476. }
  477. }
  478. // Determine if the element has automatic margins.
  479. bool margins_auto[2];
  480. int num_auto_margins = 0;
  481. for (int i = 0; i < 2; ++i)
  482. {
  483. const String& property_name = i == 0 ? MARGIN_TOP : MARGIN_BOTTOM;
  484. const Property* margin_property = element->GetLocalProperty(property_name);
  485. if (margin_property != NULL &&
  486. margin_property->unit == Property::KEYWORD)
  487. {
  488. margins_auto[i] = true;
  489. num_auto_margins++;
  490. }
  491. else
  492. {
  493. margins_auto[i] = false;
  494. box.SetEdge(Box::MARGIN, i == 0 ? Box::TOP : Box::BOTTOM, element->ResolveProperty(property_name, containing_block_height));
  495. }
  496. }
  497. // If the height is set to auto, then any margins also set to auto are resolved to 0 and the height is set to -1.
  498. if (height_auto)
  499. {
  500. if (margins_auto[0])
  501. box.SetEdge(Box::MARGIN, Box::TOP, 0);
  502. if (margins_auto[1])
  503. box.SetEdge(Box::MARGIN, Box::BOTTOM, 0);
  504. content_area.y = -1;
  505. }
  506. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  507. else if (num_auto_margins > 0)
  508. {
  509. float margin;
  510. if (content_area.y >= 0)
  511. {
  512. margin = (containing_block_height - (box.GetCumulativeEdge(Box::CONTENT, Box::TOP) +
  513. box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM) +
  514. content_area.y)) / num_auto_margins;
  515. }
  516. else
  517. margin = 0;
  518. if (margins_auto[0])
  519. box.SetEdge(Box::MARGIN, Box::TOP, margin);
  520. if (margins_auto[1])
  521. box.SetEdge(Box::MARGIN, Box::BOTTOM, margin);
  522. }
  523. if (content_area.y >= 0)
  524. {
  525. // Clamp the calculated height; if the height is changed by the clamp, then the margins need to be recalculated if
  526. // they were set to auto.
  527. float clamped_height = ClampHeight(content_area.y, element, containing_block_height);
  528. if (clamped_height != content_area.y)
  529. {
  530. content_area.y = clamped_height;
  531. box.SetContent(content_area);
  532. if (num_auto_margins > 0)
  533. {
  534. // Reset the automatic margins.
  535. if (margins_auto[0])
  536. box.SetEdge(Box::MARGIN, Box::TOP, 0);
  537. if (margins_auto[1])
  538. box.SetEdge(Box::MARGIN, Box::BOTTOM, 0);
  539. BuildBoxHeight(box, element, containing_block_height);
  540. }
  541. return;
  542. }
  543. }
  544. box.SetContent(content_area);
  545. }
  546. }
  547. }