LayoutEngine.cpp 24 KB

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