LayoutEngine.cpp 25 KB

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