LayoutEngine.cpp 24 KB

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