LayoutEngine.cpp 21 KB

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