LayoutEngine.cpp 23 KB

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