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, we want to shrink the box back to its inner content width, recreating the LayoutBlockBox.
  75. // There is an issue where resize events are not propagated correctly, which affects e.g. DataGridCells.
  76. float content_width = block_box->InternalContentWidth();
  77. if (content_width < containing_block.x)
  78. {
  79. Vector2f shrinked_block_size(content_width, containing_block.y);
  80. delete block_box;
  81. block_box = new LayoutBlockBox(this, NULL, NULL);
  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 == NULL)
  101. {
  102. box.SetContent(containing_block);
  103. return;
  104. }
  105. const ComputedValues& computed = element->GetComputedValues();
  106. // Calculate the padding area.
  107. float padding = ResolveProperty(computed.padding_top, containing_block.x);
  108. box.SetEdge(Box::PADDING, Box::TOP, Math::Max(0.0f, padding));
  109. padding = ResolveProperty(computed.padding_right, containing_block.x);
  110. box.SetEdge(Box::PADDING, Box::RIGHT, Math::Max(0.0f, padding));
  111. padding = ResolveProperty(computed.padding_bottom, containing_block.x);
  112. box.SetEdge(Box::PADDING, Box::BOTTOM, Math::Max(0.0f, padding));
  113. padding = ResolveProperty(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 = ResolveProperty(computed.width, containing_block.x);
  134. else
  135. auto_width = true;
  136. if (computed.height.type != Style::Height::Auto)
  137. content_area.y = ResolveProperty(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, ResolveProperty(computed.margin_top, containing_block.x));
  171. box.SetEdge(Box::MARGIN, Box::RIGHT, ResolveProperty(computed.margin_right, containing_block.x));
  172. box.SetEdge(Box::MARGIN, Box::BOTTOM, ResolveProperty(computed.margin_bottom, containing_block.x));
  173. box.SetEdge(Box::MARGIN, Box::LEFT, ResolveProperty(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 = ResolveProperty(computed.min_height, containing_block.y);
  194. max_height = (computed.max_height.value < 0.f ? FLT_MAX : ResolveProperty(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 = ResolveProperty(computed.min_width, containing_block_width);
  206. float max_width = (computed.max_width.value < 0.f ? FLT_MAX : ResolveProperty(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 = ResolveProperty(computed.min_height, containing_block_height);
  213. float max_height = (computed.max_height.value < 0.f ? FLT_MAX : ResolveProperty(computed.max_height, containing_block_height));
  214. return Math::Clamp(height, min_height, max_height);
  215. }
  216. void* LayoutEngine::AllocateLayoutChunk(size_t ROCKET_UNUSED_ASSERT_PARAMETER(size))
  217. {
  218. ROCKET_UNUSED_ASSERT(size);
  219. ROCKET_ASSERT(size <= LayoutChunk::size);
  220. return layout_chunk_pool.AllocateObject();
  221. }
  222. void LayoutEngine::DeallocateLayoutChunk(void* chunk)
  223. {
  224. layout_chunk_pool.DeallocateObject((LayoutChunk*) chunk);
  225. }
  226. // Positions a single element and its children within this layout.
  227. bool LayoutEngine::FormatElement(Element* element)
  228. {
  229. auto& computed = element->GetComputedValues();
  230. // Check if we have to do any special formatting for any elements that don't fit into the standard layout scheme.
  231. if (FormatElementSpecial(element))
  232. return true;
  233. // Fetch the display property, and don't lay this element out if it is set to a display type of none.
  234. if (computed.display == Style::Display::None)
  235. return true;
  236. // Check for an absolute position; if this has been set, then we remove it from the flow and add it to the current
  237. // block box to be laid out and positioned once the block has been closed and sized.
  238. if (computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed)
  239. {
  240. // Display the element as a block element.
  241. block_context_box->AddAbsoluteElement(element);
  242. return true;
  243. }
  244. // If the element is floating, we remove it from the flow.
  245. Style::Float float_property = element->GetFloat();
  246. if (float_property != Style::Float::None)
  247. {
  248. // Format the element as a block element.
  249. LayoutEngine layout_engine;
  250. layout_engine.FormatElement(element, GetContainingBlock(block_context_box));
  251. return block_context_box->AddFloatElement(element);
  252. }
  253. // The element is nothing exceptional, so we treat it as a normal block, inline or replaced element.
  254. switch (computed.display)
  255. {
  256. case Style::Display::Block: return FormatElementBlock(element); break;
  257. case Style::Display::Inline: return FormatElementInline(element); break;
  258. case Style::Display::InlineBlock: return FormatElementReplaced(element); break;
  259. default: ROCKET_ERROR;
  260. }
  261. return true;
  262. }
  263. // Formats and positions an element as a block element.
  264. bool LayoutEngine::FormatElementBlock(Element* element)
  265. {
  266. LayoutBlockBox* new_block_context_box = block_context_box->AddBlockElement(element);
  267. if (new_block_context_box == NULL)
  268. return false;
  269. block_context_box = new_block_context_box;
  270. // Format the element's children.
  271. for (int i = 0; i < element->GetNumChildren(); i++)
  272. {
  273. if (!FormatElement(element->GetChild(i)))
  274. i = -1;
  275. }
  276. // Close the block box, and check the return code; we may have overflowed either this element or our parent.
  277. new_block_context_box = block_context_box->GetParent();
  278. switch (block_context_box->Close())
  279. {
  280. // We need to reformat ourself; format all of our children again and close the box. No need to check for error
  281. // codes, as we already have our vertical slider bar.
  282. case LayoutBlockBox::LAYOUT_SELF:
  283. {
  284. for (int i = 0; i < element->GetNumChildren(); i++)
  285. FormatElement(element->GetChild(i));
  286. if (block_context_box->Close() == LayoutBlockBox::OK)
  287. {
  288. element->OnLayout();
  289. break;
  290. }
  291. }
  292. // We caused our parent to add a vertical scrollbar; bail out!
  293. case LayoutBlockBox::LAYOUT_PARENT:
  294. {
  295. block_context_box = new_block_context_box;
  296. return false;
  297. }
  298. break;
  299. default:
  300. element->OnLayout();
  301. }
  302. block_context_box = new_block_context_box;
  303. return true;
  304. }
  305. // Formats and positions an element as an inline element.
  306. bool LayoutEngine::FormatElementInline(Element* element)
  307. {
  308. Box box;
  309. float min_height, max_height;
  310. BuildBox(box, min_height, max_height, block_context_box, element, true);
  311. LayoutInlineBox* inline_box = block_context_box->AddInlineElement(element, box);
  312. // Format the element's children.
  313. for (int i = 0; i < element->GetNumChildren(); i++)
  314. {
  315. if (!FormatElement(element->GetChild(i)))
  316. return false;
  317. }
  318. inline_box->Close();
  319. // element->OnLayout();
  320. return true;
  321. }
  322. // Positions an element as a sized inline element, formatting its internal hierarchy as a block element.
  323. bool LayoutEngine::FormatElementReplaced(Element* element)
  324. {
  325. // Format the element separately as a block element, then position it inside our own layout as an inline element.
  326. Vector2f containing_block_size = GetContainingBlock(block_context_box);
  327. LayoutEngine layout_engine;
  328. layout_engine.FormatElement(element, containing_block_size, true);
  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 = ResolveProperty(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, ResolveProperty(*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 = ResolveProperty(computed.left, containing_block_width );
  417. if (computed.right.type != Style::Right::Auto)
  418. right = ResolveProperty(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 = ResolveProperty(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, ResolveProperty(*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 = ResolveProperty(computed.top, containing_block_height );
  519. bottom = ResolveProperty(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. }