LayoutEngine.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 alignas(LayoutBlockBox) 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. void* LayoutEngine::AllocateLayoutChunk(size_t ROCKET_UNUSED_ASSERT_PARAMETER(size))
  247. {
  248. ROCKET_UNUSED_ASSERT(size);
  249. ROCKET_ASSERT(size <= LayoutChunk::size);
  250. return layout_chunk_pool.AllocateObject();
  251. }
  252. void LayoutEngine::DeallocateLayoutChunk(void* chunk)
  253. {
  254. layout_chunk_pool.DeallocateObject((LayoutChunk*) chunk);
  255. }
  256. // Positions a single element and its children within this layout.
  257. bool LayoutEngine::FormatElement(Element* element)
  258. {
  259. // Check if we have to do any special formatting for any elements that don't fit into the standard layout scheme.
  260. if (FormatElementSpecial(element))
  261. return true;
  262. // Fetch the display property, and don't lay this element out if it is set to a display type of none.
  263. int display_property = element->GetDisplay();
  264. if (display_property == DISPLAY_NONE)
  265. return true;
  266. // Check for an absolute position; if this has been set, then we remove it from the flow and add it to the current
  267. // block box to be laid out and positioned once the block has been closed and sized.
  268. int position_property = element->GetPosition();
  269. if (position_property == POSITION_ABSOLUTE ||
  270. position_property == POSITION_FIXED)
  271. {
  272. // Display the element as a block element.
  273. block_context_box->AddAbsoluteElement(element);
  274. return true;
  275. }
  276. // If the element is floating, we remove it from the flow.
  277. int float_property = element->GetFloat();
  278. if (float_property != FLOAT_NONE)
  279. {
  280. // Format the element as a block element.
  281. LayoutEngine layout_engine;
  282. layout_engine.FormatElement(element, GetContainingBlock(block_context_box));
  283. return block_context_box->AddFloatElement(element);
  284. }
  285. // The element is nothing exceptional, so we treat it as a normal block, inline or replaced element.
  286. switch (display_property)
  287. {
  288. case DISPLAY_BLOCK: return FormatElementBlock(element); break;
  289. case DISPLAY_INLINE: return FormatElementInline(element); break;
  290. case DISPLAY_INLINE_BLOCK: return FormatElementReplaced(element); break;
  291. default: ROCKET_ERROR;
  292. }
  293. return true;
  294. }
  295. // Formats and positions an element as a block element.
  296. bool LayoutEngine::FormatElementBlock(Element* element)
  297. {
  298. LayoutBlockBox* new_block_context_box = block_context_box->AddBlockElement(element);
  299. if (new_block_context_box == NULL)
  300. return false;
  301. block_context_box = new_block_context_box;
  302. // Format the element's children.
  303. for (int i = 0; i < element->GetNumChildren(); i++)
  304. {
  305. if (!FormatElement(element->GetChild(i)))
  306. i = -1;
  307. }
  308. // Close the block box, and check the return code; we may have overflowed either this element or our parent.
  309. new_block_context_box = block_context_box->GetParent();
  310. switch (block_context_box->Close())
  311. {
  312. // We need to reformat ourself; format all of our children again and close the box. No need to check for error
  313. // codes, as we already have our vertical slider bar.
  314. case LayoutBlockBox::LAYOUT_SELF:
  315. {
  316. for (int i = 0; i < element->GetNumChildren(); i++)
  317. FormatElement(element->GetChild(i));
  318. if (block_context_box->Close() == LayoutBlockBox::OK)
  319. {
  320. element->OnLayout();
  321. break;
  322. }
  323. }
  324. // We caused our parent to add a vertical scrollbar; bail out!
  325. case LayoutBlockBox::LAYOUT_PARENT:
  326. {
  327. block_context_box = new_block_context_box;
  328. return false;
  329. }
  330. break;
  331. default:
  332. element->OnLayout();
  333. }
  334. block_context_box = new_block_context_box;
  335. return true;
  336. }
  337. // Formats and positions an element as an inline element.
  338. bool LayoutEngine::FormatElementInline(Element* element)
  339. {
  340. Box box;
  341. float min_height, max_height;
  342. BuildBox(box, min_height, max_height, block_context_box, element, true);
  343. LayoutInlineBox* inline_box = block_context_box->AddInlineElement(element, box);
  344. // Format the element's children.
  345. for (int i = 0; i < element->GetNumChildren(); i++)
  346. {
  347. if (!FormatElement(element->GetChild(i)))
  348. return false;
  349. }
  350. inline_box->Close();
  351. // element->OnLayout();
  352. return true;
  353. }
  354. // Positions an element as a sized inline element, formatting its internal hierarchy as a block element.
  355. bool LayoutEngine::FormatElementReplaced(Element* element)
  356. {
  357. // Format the element separately as a block element, then position it inside our own layout as an inline element.
  358. Vector2f containing_block_size = GetContainingBlock(block_context_box);
  359. LayoutEngine layout_engine;
  360. layout_engine.FormatElement(element, containing_block_size, true);
  361. block_context_box->AddInlineElement(element, element->GetBox())->Close();
  362. return true;
  363. }
  364. // Executes any special formatting for special elements.
  365. bool LayoutEngine::FormatElementSpecial(Element* element)
  366. {
  367. static String br("br");
  368. // Check for a <br> tag.
  369. if (element->GetTagName() == br)
  370. {
  371. block_context_box->AddBreak();
  372. element->OnLayout();
  373. return true;
  374. }
  375. return false;
  376. }
  377. // Returns the fully-resolved, fixed-width and -height containing block from a block box.
  378. Vector2f LayoutEngine::GetContainingBlock(const LayoutBlockBox* containing_box)
  379. {
  380. Vector2f containing_block;
  381. containing_block.x = containing_box->GetBox().GetSize(Box::CONTENT).x;
  382. if (containing_box->GetElement() != NULL)
  383. containing_block.x -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  384. while ((containing_block.y = containing_box->GetBox().GetSize(Box::CONTENT).y) < 0)
  385. {
  386. containing_box = containing_box->GetParent();
  387. if (containing_box == NULL)
  388. {
  389. ROCKET_ERROR;
  390. containing_block.y = 0;
  391. }
  392. }
  393. if (containing_box != NULL &&
  394. containing_box->GetElement() != NULL)
  395. containing_block.y -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL);
  396. containing_block.x = Math::Max(0.0f, containing_block.x);
  397. containing_block.y = Math::Max(0.0f, containing_block.y);
  398. return containing_block;
  399. }
  400. // Builds the block-specific width and horizontal margins of a Box.
  401. void LayoutEngine::BuildBoxWidth(Box& box, Element* element, float containing_block_width)
  402. {
  403. Vector2f content_area = box.GetSize();
  404. // Determine if the element has an automatic width, and if not calculate it.
  405. bool width_auto;
  406. if (content_area.x >= 0)
  407. width_auto = false;
  408. else
  409. {
  410. const Property* width_property;
  411. element->GetDimensionProperties(&width_property, NULL);
  412. if (width_property->unit == Property::KEYWORD)
  413. {
  414. width_auto = true;
  415. }
  416. else
  417. {
  418. width_auto = false;
  419. content_area.x = element->ResolveProperty(width_property, containing_block_width);
  420. }
  421. }
  422. // Determine if the element has automatic margins.
  423. bool margins_auto[2];
  424. int num_auto_margins = 0;
  425. const Property *margin_left, *margin_right;
  426. element->GetMarginProperties(NULL, NULL, &margin_left, &margin_right);
  427. for (int i = 0; i < 2; ++i)
  428. {
  429. const Property* margin_property = i == 0 ? margin_left : margin_right;
  430. if (margin_property != NULL &&
  431. margin_property->unit == Property::KEYWORD)
  432. {
  433. margins_auto[i] = true;
  434. num_auto_margins++;
  435. }
  436. else
  437. {
  438. margins_auto[i] = false;
  439. box.SetEdge(Box::MARGIN, i == 0 ? Box::LEFT : Box::RIGHT, element->ResolveProperty(margin_property, containing_block_width));
  440. }
  441. }
  442. // If the width is set to auto, we need to calculate the width
  443. if (width_auto)
  444. {
  445. float left = 0.0f, right = 0.0f;
  446. // If we are dealing with an absolutely positioned element we need to
  447. // consider if the left and right properties are set, since the width can be affected.
  448. if (element->GetPosition() == POSITION_ABSOLUTE ||
  449. element->GetPosition() == POSITION_FIXED)
  450. {
  451. Property const *left_property, *right_property;
  452. element->GetOffsetProperties( NULL, NULL, &left_property, &right_property );
  453. if (left_property->unit != Property::KEYWORD)
  454. left = element->ResolveProperty(left_property, containing_block_width );
  455. if (right_property->unit != Property::KEYWORD)
  456. right = element->ResolveProperty(right_property, containing_block_width );
  457. }
  458. // We resolve any auto margins to 0 and the width is set to whatever is left of the containing block.
  459. if (margins_auto[0])
  460. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  461. if (margins_auto[1])
  462. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  463. content_area.x = containing_block_width - (left +
  464. box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  465. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  466. right);
  467. content_area.x = Math::Max(0.0f, content_area.x);
  468. }
  469. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  470. else if (num_auto_margins > 0)
  471. {
  472. float margin = (containing_block_width - (box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  473. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  474. content_area.x)) / num_auto_margins;
  475. if (margins_auto[0])
  476. box.SetEdge(Box::MARGIN, Box::LEFT, margin);
  477. if (margins_auto[1])
  478. box.SetEdge(Box::MARGIN, Box::RIGHT, margin);
  479. }
  480. // Clamp the calculated width; if the width is changed by the clamp, then the margins need to be recalculated if
  481. // they were set to auto.
  482. float clamped_width = ClampWidth(content_area.x, element, containing_block_width);
  483. if (clamped_width != content_area.x)
  484. {
  485. content_area.x = clamped_width;
  486. box.SetContent(content_area);
  487. if (num_auto_margins > 0)
  488. {
  489. // Reset the automatic margins.
  490. if (margins_auto[0])
  491. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  492. if (margins_auto[1])
  493. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  494. BuildBoxWidth(box, element, containing_block_width);
  495. }
  496. }
  497. else
  498. box.SetContent(content_area);
  499. }
  500. // Builds the block-specific height and vertical margins of a Box.
  501. void LayoutEngine::BuildBoxHeight(Box& box, Element* element, float containing_block_height)
  502. {
  503. Vector2f content_area = box.GetSize();
  504. // Determine if the element has an automatic height, and if not calculate it.
  505. bool height_auto;
  506. if (content_area.y >= 0)
  507. height_auto = false;
  508. else
  509. {
  510. const Property* height_property;
  511. element->GetDimensionProperties(NULL, &height_property);
  512. if (height_property == NULL)
  513. {
  514. height_auto = false;
  515. }
  516. else if (height_property->unit == Property::KEYWORD)
  517. {
  518. height_auto = true;
  519. }
  520. else
  521. {
  522. height_auto = false;
  523. content_area.y = element->ResolveProperty(height_property, containing_block_height);
  524. }
  525. }
  526. // Determine if the element has automatic margins.
  527. bool margins_auto[2];
  528. int num_auto_margins = 0;
  529. const Property *margin_top, *margin_bottom;
  530. element->GetMarginProperties(&margin_top, &margin_bottom, NULL, NULL);
  531. for (int i = 0; i < 2; ++i)
  532. {
  533. const Property* margin_property = i == 0 ? margin_top : margin_bottom;
  534. if (margin_property != NULL &&
  535. margin_property->unit == Property::KEYWORD)
  536. {
  537. margins_auto[i] = true;
  538. num_auto_margins++;
  539. }
  540. else
  541. {
  542. margins_auto[i] = false;
  543. box.SetEdge(Box::MARGIN, i == 0 ? Box::TOP : Box::BOTTOM, element->ResolveProperty(margin_property, containing_block_height));
  544. }
  545. }
  546. // If the height is set to auto, we need to calculate the height
  547. if (height_auto)
  548. {
  549. // We resolve any auto margins to 0
  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. // If the height is set to auto for a box in normal flow, the height is set to -1.
  555. content_area.y = -1;
  556. // But if we are dealing with an absolutely positioned element we need to
  557. // consider if the top and bottom properties are set, since the height can be affected.
  558. if (element->GetPosition() == POSITION_ABSOLUTE ||
  559. element->GetPosition() == POSITION_FIXED)
  560. {
  561. float top = 0.0f, bottom = 0.0f;
  562. Property const *top_property, *bottom_property;
  563. element->GetOffsetProperties( &top_property, &bottom_property, NULL, NULL );
  564. if (top_property->unit != Property::KEYWORD && bottom_property->unit != Property::KEYWORD )
  565. {
  566. top = element->ResolveProperty(top_property, containing_block_height );
  567. bottom = element->ResolveProperty(bottom_property, containing_block_height );
  568. // The height gets resolved to whatever is left of the containing block
  569. content_area.y = containing_block_height - (top +
  570. box.GetCumulativeEdge(Box::CONTENT, Box::TOP) +
  571. box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM) +
  572. bottom);
  573. content_area.y = Math::Max(0.0f, content_area.y);
  574. }
  575. }
  576. }
  577. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  578. else if (num_auto_margins > 0)
  579. {
  580. float margin;
  581. if (content_area.y >= 0)
  582. {
  583. margin = (containing_block_height - (box.GetCumulativeEdge(Box::CONTENT, Box::TOP) +
  584. box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM) +
  585. content_area.y)) / num_auto_margins;
  586. }
  587. else
  588. margin = 0;
  589. if (margins_auto[0])
  590. box.SetEdge(Box::MARGIN, Box::TOP, margin);
  591. if (margins_auto[1])
  592. box.SetEdge(Box::MARGIN, Box::BOTTOM, margin);
  593. }
  594. if (content_area.y >= 0)
  595. {
  596. // Clamp the calculated height; if the height is changed by the clamp, then the margins need to be recalculated if
  597. // they were set to auto.
  598. float clamped_height = ClampHeight(content_area.y, element, containing_block_height);
  599. if (clamped_height != content_area.y)
  600. {
  601. content_area.y = clamped_height;
  602. box.SetContent(content_area);
  603. if (num_auto_margins > 0)
  604. {
  605. // Reset the automatic margins.
  606. if (margins_auto[0])
  607. box.SetEdge(Box::MARGIN, Box::TOP, 0);
  608. if (margins_auto[1])
  609. box.SetEdge(Box::MARGIN, Box::BOTTOM, 0);
  610. BuildBoxHeight(box, element, containing_block_height);
  611. }
  612. return;
  613. }
  614. }
  615. box.SetContent(content_area);
  616. }
  617. }
  618. }