LayoutEngine.cpp 21 KB

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