LayoutDetails.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 "LayoutDetails.h"
  29. #include "LayoutEngine.h"
  30. #include "../../Include/RmlUi/Core/Element.h"
  31. #include "../../Include/RmlUi/Core/ElementScroll.h"
  32. #include "../../Include/RmlUi/Core/Math.h"
  33. #include "../../Include/RmlUi/Core/Profiling.h"
  34. #include <float.h>
  35. namespace Rml {
  36. static inline float BorderWidthToContentWidth(float border_width, const Box& box)
  37. {
  38. const float border_padding_edges_width = box.GetSizeAcross(Box::HORIZONTAL, Box::BORDER, Box::PADDING);
  39. return Math::Max(0.0f, border_width - border_padding_edges_width);
  40. }
  41. static inline float BorderHeightToContentHeight(float border_height, const Box& box)
  42. {
  43. const float border_padding_edges_height = box.GetSizeAcross(Box::VERTICAL, Box::BORDER, Box::PADDING);
  44. return Math::Max(0.0f, border_height - border_padding_edges_height);
  45. }
  46. // Generates the box for an element.
  47. void LayoutDetails::BuildBox(Box& box, Vector2f containing_block, Element* element, bool inline_element, float override_shrink_to_fit_width)
  48. {
  49. if (!element)
  50. {
  51. box.SetContent(containing_block);
  52. return;
  53. }
  54. const ComputedValues& computed = element->GetComputedValues();
  55. // Calculate the padding area.
  56. box.SetEdge(Box::PADDING, Box::TOP, Math::Max(0.0f, ResolveValue(computed.padding_top, containing_block.x)));
  57. box.SetEdge(Box::PADDING, Box::RIGHT, Math::Max(0.0f, ResolveValue(computed.padding_right, containing_block.x)));
  58. box.SetEdge(Box::PADDING, Box::BOTTOM, Math::Max(0.0f, ResolveValue(computed.padding_bottom, containing_block.x)));
  59. box.SetEdge(Box::PADDING, Box::LEFT, Math::Max(0.0f, ResolveValue(computed.padding_left, containing_block.x)));
  60. // Calculate the border area.
  61. box.SetEdge(Box::BORDER, Box::TOP, Math::Max(0.0f, computed.border_top_width));
  62. box.SetEdge(Box::BORDER, Box::RIGHT, Math::Max(0.0f, computed.border_right_width));
  63. box.SetEdge(Box::BORDER, Box::BOTTOM, Math::Max(0.0f, computed.border_bottom_width));
  64. box.SetEdge(Box::BORDER, Box::LEFT, Math::Max(0.0f, computed.border_left_width));
  65. // Calculate the size of the content area.
  66. Vector2f content_area(-1, -1);
  67. float intrinsic_ratio = -1;
  68. bool replaced_element = false;
  69. // If the element has intrinsic dimensions, then we use those as the basis for the content area and only adjust
  70. // them if a non-auto style has been applied to them.
  71. if (element->GetIntrinsicDimensions(content_area, intrinsic_ratio))
  72. {
  73. replaced_element = true;
  74. bool auto_width = false, auto_height = false;
  75. if (computed.width.type == Style::Width::Auto)
  76. auto_width = true;
  77. else if (computed.box_sizing == Style::BoxSizing::ContentBox)
  78. content_area.x = ResolveValue(computed.width, containing_block.x);
  79. else
  80. content_area.x = BorderWidthToContentWidth(ResolveValue(computed.width, containing_block.x), box);
  81. if (computed.height.type == Style::Height::Auto)
  82. auto_height = true;
  83. else if (computed.box_sizing == Style::BoxSizing::ContentBox)
  84. content_area.y = ResolveValue(computed.height, containing_block.y);
  85. else
  86. content_area.y = BorderHeightToContentHeight(ResolveValue(computed.height, containing_block.y), box);
  87. // Use a fallback size if we still couldn't determine the size.
  88. if (content_area.x < 0)
  89. content_area.x = 300;
  90. if (content_area.y < 0)
  91. content_area.y = 150;
  92. // Resolve the size constraints.
  93. float min_width = ResolveValue(computed.min_width, containing_block.x);
  94. float max_width = (computed.max_width.value < 0.f ? FLT_MAX : ResolveValue(computed.max_width, containing_block.x));
  95. float min_height = ResolveValue(computed.min_height, containing_block.y);
  96. float max_height = (computed.max_height.value < 0.f ? FLT_MAX : ResolveValue(computed.max_height, containing_block.y));
  97. if (computed.box_sizing == Style::BoxSizing::BorderBox)
  98. {
  99. min_width = BorderWidthToContentWidth(min_width, box);
  100. max_width = BorderWidthToContentWidth(max_width, box);
  101. min_height = BorderHeightToContentHeight(min_height, box);
  102. max_height = BorderHeightToContentHeight(max_height, box);
  103. }
  104. // If we have an intrinsic ratio and one of the dimensions is 'auto', then scale it such that the ratio is preserved.
  105. if (intrinsic_ratio > 0)
  106. {
  107. if (auto_width && !auto_height)
  108. {
  109. content_area.x = content_area.y * intrinsic_ratio;
  110. }
  111. else if (auto_height && !auto_width)
  112. {
  113. content_area.y = content_area.x / intrinsic_ratio;
  114. }
  115. else if (auto_width && auto_height)
  116. {
  117. // If both width and height are auto, try to preserve the ratio under the respective min/max constraints.
  118. const float w = content_area.x;
  119. const float h = content_area.y;
  120. if ((w < min_width && h > max_height) || (w > max_width && h < min_height))
  121. {
  122. // Cannot preserve aspect ratio, let it be clamped.
  123. }
  124. else if (w < min_width && h < min_height)
  125. {
  126. // Increase the size such that both min-constraints are respected. The non-scaled axis will
  127. // be clamped below, preserving the aspect ratio.
  128. if (min_width <= min_height * intrinsic_ratio)
  129. content_area.x = min_height * intrinsic_ratio;
  130. else
  131. content_area.y = min_width / intrinsic_ratio;
  132. }
  133. else if (w > max_width && h > max_height)
  134. {
  135. // Shrink the size such that both max-constraints are respected. The non-scaled axis will
  136. // be clamped below, preserving the aspect ratio.
  137. if (max_width <= max_height * intrinsic_ratio)
  138. content_area.y = max_width / intrinsic_ratio;
  139. else
  140. content_area.x = max_height * intrinsic_ratio;
  141. }
  142. else
  143. {
  144. // Single constraint violations.
  145. if (w < min_width)
  146. content_area.y = min_width / intrinsic_ratio;
  147. else if (w > max_width)
  148. content_area.y = max_width / intrinsic_ratio;
  149. else if (h < min_height)
  150. content_area.x = min_height * intrinsic_ratio;
  151. else if (h > max_height)
  152. content_area.x = max_height * intrinsic_ratio;
  153. }
  154. }
  155. }
  156. content_area.x = Math::Clamp(content_area.x, min_width, max_width);
  157. content_area.y = Math::Clamp(content_area.y, min_height, max_height);
  158. }
  159. // If the element is inline, then its calculations are much more straightforward (no worrying about auto margins
  160. // and dimensions, etc). All we do is calculate the margins, set the content area and bail.
  161. if (inline_element)
  162. {
  163. // If the element was not replaced, then we leave its dimension as unsized (-1, -1) and ignore the width and
  164. // height properties.
  165. box.SetContent(content_area);
  166. // Evaluate the margins. Any declared as 'auto' will resolve to 0.
  167. box.SetEdge(Box::MARGIN, Box::TOP, ResolveValue(computed.margin_top, containing_block.x));
  168. box.SetEdge(Box::MARGIN, Box::RIGHT, ResolveValue(computed.margin_right, containing_block.x));
  169. box.SetEdge(Box::MARGIN, Box::BOTTOM, ResolveValue(computed.margin_bottom, containing_block.x));
  170. box.SetEdge(Box::MARGIN, Box::LEFT, ResolveValue(computed.margin_left, containing_block.x));
  171. }
  172. // The element is block, so we need to run the box through the ringer to potentially evaluate auto margins and
  173. // dimensions.
  174. else
  175. {
  176. box.SetContent(content_area);
  177. BuildBoxWidth(box, computed, containing_block, element, replaced_element, override_shrink_to_fit_width);
  178. BuildBoxHeight(box, computed, containing_block.y);
  179. }
  180. }
  181. // Generates the box for an element placed in a block box.
  182. void LayoutDetails::BuildBox(Box& box, float& min_height, float& max_height, LayoutBlockBox* containing_box, Element* element, bool inline_element, float override_shrink_to_fit_width)
  183. {
  184. Vector2f containing_block = LayoutDetails::GetContainingBlock(containing_box);
  185. BuildBox(box, containing_block, element, inline_element, override_shrink_to_fit_width);
  186. if (element)
  187. GetMinMaxHeight(min_height, max_height, element->GetComputedValues(), box, containing_block.y);
  188. else
  189. min_height = max_height = box.GetSize().y;
  190. }
  191. float LayoutDetails::ClampWidth(float width, const ComputedValues& computed, const Box& box, float containing_block_width)
  192. {
  193. float min_width = ResolveValue(computed.min_width, containing_block_width);
  194. float max_width = (computed.max_width.value < 0.f ? FLT_MAX : ResolveValue(computed.max_width, containing_block_width));
  195. if (computed.box_sizing == Style::BoxSizing::BorderBox)
  196. {
  197. min_width = BorderWidthToContentWidth(min_width, box);
  198. max_width = BorderWidthToContentWidth(max_width, box);
  199. }
  200. return Math::Clamp(width, min_width, max_width);
  201. }
  202. float LayoutDetails::ClampHeight(float height, const ComputedValues& computed, const Box& box, float containing_block_height)
  203. {
  204. float min_height = ResolveValue(computed.min_height, containing_block_height);
  205. float max_height = (computed.max_height.value < 0.f ? FLT_MAX : ResolveValue(computed.max_height, containing_block_height));
  206. if (computed.box_sizing == Style::BoxSizing::BorderBox)
  207. {
  208. min_height = BorderHeightToContentHeight(min_height, box);
  209. max_height = BorderHeightToContentHeight(max_height, box);
  210. }
  211. return Math::Clamp(height, min_height, max_height);
  212. }
  213. void LayoutDetails::GetMinMaxHeight(float& min_height, float& max_height, const ComputedValues& computed, const Box& box, float containing_block_height)
  214. {
  215. const float box_height = box.GetSize().y;
  216. if (box_height < 0)
  217. {
  218. min_height = ResolveValue(computed.min_height, containing_block_height);
  219. max_height = (computed.max_height.value < 0.f ? FLT_MAX : ResolveValue(computed.max_height, containing_block_height));
  220. if (computed.box_sizing == Style::BoxSizing::BorderBox)
  221. {
  222. min_height = BorderHeightToContentHeight(min_height, box);
  223. max_height = BorderHeightToContentHeight(max_height, box);
  224. }
  225. }
  226. else
  227. {
  228. min_height = box_height;
  229. max_height = box_height;
  230. }
  231. }
  232. // Returns the fully-resolved, fixed-width and -height containing block from a block box.
  233. Vector2f LayoutDetails::GetContainingBlock(const LayoutBlockBox* containing_box)
  234. {
  235. RMLUI_ASSERT(containing_box);
  236. Vector2f containing_block;
  237. containing_block.x = containing_box->GetBox().GetSize(Box::CONTENT).x;
  238. if (containing_box->GetElement() != nullptr)
  239. containing_block.x -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  240. while ((containing_block.y = containing_box->GetBox().GetSize(Box::CONTENT).y) < 0)
  241. {
  242. containing_box = containing_box->GetParent();
  243. if (containing_box == nullptr)
  244. {
  245. RMLUI_ERROR;
  246. containing_block.y = 0;
  247. }
  248. }
  249. if (containing_box != nullptr &&
  250. containing_box->GetElement() != nullptr)
  251. containing_block.y -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL);
  252. containing_block.x = Math::Max(0.0f, containing_block.x);
  253. containing_block.y = Math::Max(0.0f, containing_block.y);
  254. return containing_block;
  255. }
  256. float LayoutDetails::GetShrinkToFitWidth(Element* element, Vector2f containing_block)
  257. {
  258. RMLUI_ASSERT(element);
  259. Box box;
  260. float min_height, max_height;
  261. LayoutDetails::BuildBox(box, containing_block, element, false, containing_block.x);
  262. LayoutDetails::GetMinMaxHeight(min_height, max_height, element->GetComputedValues(), box, containing_block.y);
  263. // First we need to format the element, then we get the shrink-to-fit width based on the largest line or box.
  264. LayoutBlockBox containing_block_box(nullptr, nullptr, Box(containing_block), 0.0f, FLT_MAX);
  265. // Here we fix the element's width to its containing block so that any content is wrapped at this width.
  266. // We can consider to instead set this to infinity and clamp it to the available width later after formatting,
  267. // but right now the formatting procedure doesn't work well with such numbers.
  268. LayoutBlockBox* block_context_box = containing_block_box.AddBlockElement(element, box, min_height, max_height);
  269. // @performance. Some formatting can be simplified, eg. absolute elements do not contribute to the shrink-to-fit width.
  270. // Also, children of elements with a fixed width and height don't need to be formatted further.
  271. for (int i = 0; i < element->GetNumChildren(); i++)
  272. {
  273. if (!LayoutEngine::FormatElement(block_context_box, element->GetChild(i)))
  274. i = -1;
  275. }
  276. // We only do layouting to get the fit-to-shrink width here, and for this purpose we may get
  277. // away with not closing the boxes. This is avoided for performance reasons.
  278. //block_context_box->Close();
  279. return Math::Min(containing_block.x, block_context_box->GetShrinkToFitWidth());
  280. }
  281. // Builds the block-specific width and horizontal margins of a Box.
  282. void LayoutDetails::BuildBoxWidth(Box& box, const ComputedValues& computed, Vector2f containing_block, Element* element, bool replaced_element, float override_shrink_to_fit_width)
  283. {
  284. RMLUI_ZoneScoped;
  285. Vector2f content_area = box.GetSize();
  286. // Determine if the element has an automatic width, and if not calculate it.
  287. bool width_auto = false;
  288. if (content_area.x < 0)
  289. {
  290. if (computed.width.type == Style::Width::Auto)
  291. width_auto = true;
  292. else if (computed.box_sizing == Style::BoxSizing::ContentBox)
  293. content_area.x = ResolveValue(computed.width, containing_block.x);
  294. else
  295. content_area.x = BorderWidthToContentWidth(ResolveValue(computed.width, containing_block.x), box);
  296. }
  297. // Determine if the element has automatic margins.
  298. bool margins_auto[2];
  299. int num_auto_margins = 0;
  300. for (int i = 0; i < 2; ++i)
  301. {
  302. auto* margin_value = (i == 0 ? &computed.margin_left : &computed.margin_right);
  303. if (margin_value->type == Style::Margin::Auto)
  304. {
  305. margins_auto[i] = true;
  306. num_auto_margins++;
  307. }
  308. else
  309. {
  310. margins_auto[i] = false;
  311. box.SetEdge(Box::MARGIN, i == 0 ? Box::LEFT : Box::RIGHT, ResolveValue(*margin_value, containing_block.x));
  312. }
  313. }
  314. // If the width is set to auto, we need to calculate the width
  315. if (width_auto)
  316. {
  317. // Apply the shrink-to-fit algorithm here to find the width of the element.
  318. // See CSS 2.1 section 10.3.7 for when this should be applied.
  319. const bool shrink_to_fit = !replaced_element &&
  320. (
  321. (computed.float_ != Style::Float::None) ||
  322. ((computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed) && (computed.left.type == Style::Left::Auto || computed.right.type == Style::Right::Auto)) ||
  323. (computed.display == Style::Display::InlineBlock)
  324. );
  325. float left = 0.0f, right = 0.0f;
  326. // If we are dealing with an absolutely positioned element we need to
  327. // consider if the left and right properties are set, since the width can be affected.
  328. if (computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed)
  329. {
  330. if (computed.left.type != Style::Left::Auto)
  331. left = ResolveValue(computed.left, containing_block.x);
  332. if (computed.right.type != Style::Right::Auto)
  333. right = ResolveValue(computed.right, containing_block.x);
  334. }
  335. if (margins_auto[0])
  336. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  337. if (margins_auto[1])
  338. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  339. if (shrink_to_fit && override_shrink_to_fit_width < 0)
  340. {
  341. content_area.x = GetShrinkToFitWidth(element, containing_block);
  342. override_shrink_to_fit_width = content_area.x;
  343. }
  344. else if (shrink_to_fit)
  345. {
  346. content_area.x = override_shrink_to_fit_width;
  347. }
  348. else
  349. {
  350. // We resolve any auto margins to 0 and the width is set to whatever is left of the containing block.
  351. content_area.x = containing_block.x - (left +
  352. box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  353. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  354. right);
  355. content_area.x = Math::Max(0.0f, content_area.x);
  356. }
  357. }
  358. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  359. else if (num_auto_margins > 0)
  360. {
  361. float margin = (containing_block.x - (box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  362. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  363. content_area.x)) / num_auto_margins;
  364. if (margins_auto[0])
  365. box.SetEdge(Box::MARGIN, Box::LEFT, margin);
  366. if (margins_auto[1])
  367. box.SetEdge(Box::MARGIN, Box::RIGHT, margin);
  368. }
  369. // Clamp the calculated width; if the width is changed by the clamp, then the margins need to be recalculated if
  370. // they were set to auto.
  371. float clamped_width = ClampWidth(content_area.x, computed, box, containing_block.x);
  372. if (clamped_width != content_area.x)
  373. {
  374. content_area.x = clamped_width;
  375. box.SetContent(content_area);
  376. if (num_auto_margins > 0)
  377. {
  378. // Reset the automatic margins.
  379. if (margins_auto[0])
  380. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  381. if (margins_auto[1])
  382. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  383. BuildBoxWidth(box, computed, containing_block, element, replaced_element, override_shrink_to_fit_width);
  384. }
  385. }
  386. else
  387. box.SetContent(content_area);
  388. }
  389. // Builds the block-specific height and vertical margins of a Box.
  390. void LayoutDetails::BuildBoxHeight(Box& box, const ComputedValues& computed, float containing_block_height)
  391. {
  392. RMLUI_ZoneScoped;
  393. Vector2f content_area = box.GetSize();
  394. // Determine if the element has an automatic height, and if not calculate it.
  395. bool height_auto = false;
  396. if (content_area.y < 0)
  397. {
  398. if (computed.height.type == Style::Width::Auto)
  399. height_auto = true;
  400. else if (computed.box_sizing == Style::BoxSizing::ContentBox)
  401. content_area.y = ResolveValue(computed.height, containing_block_height);
  402. else
  403. content_area.y = BorderHeightToContentHeight(ResolveValue(computed.height, containing_block_height), box);
  404. }
  405. // Determine if the element has automatic margins.
  406. bool margins_auto[2];
  407. int num_auto_margins = 0;
  408. for (int i = 0; i < 2; ++i)
  409. {
  410. auto* margin_value = (i == 0 ? &computed.margin_top : &computed.margin_bottom);
  411. if (margin_value->type == Style::Margin::Auto)
  412. {
  413. margins_auto[i] = true;
  414. num_auto_margins++;
  415. }
  416. else
  417. {
  418. margins_auto[i] = false;
  419. box.SetEdge(Box::MARGIN, i == 0 ? Box::TOP : Box::BOTTOM, ResolveValue(*margin_value, containing_block_height));
  420. }
  421. }
  422. // If the height is set to auto, we need to calculate the height
  423. if (height_auto)
  424. {
  425. // We resolve any auto margins to 0
  426. if (margins_auto[0])
  427. box.SetEdge(Box::MARGIN, Box::TOP, 0);
  428. if (margins_auto[1])
  429. box.SetEdge(Box::MARGIN, Box::BOTTOM, 0);
  430. // If the height is set to auto for a box in normal flow, the height is set to -1.
  431. content_area.y = -1;
  432. // But if we are dealing with an absolutely positioned element we need to
  433. // consider if the top and bottom properties are set, since the height can be affected.
  434. if (computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed)
  435. {
  436. float top = 0.0f, bottom = 0.0f;
  437. if (computed.top.type != Style::Top::Auto && computed.bottom.type != Style::Bottom::Auto)
  438. {
  439. top = ResolveValue(computed.top, containing_block_height );
  440. bottom = ResolveValue(computed.bottom, containing_block_height );
  441. // The height gets resolved to whatever is left of the containing block
  442. content_area.y = containing_block_height - (top +
  443. box.GetCumulativeEdge(Box::CONTENT, Box::TOP) +
  444. box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM) +
  445. bottom);
  446. content_area.y = Math::Max(0.0f, content_area.y);
  447. }
  448. }
  449. }
  450. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  451. else if (num_auto_margins > 0)
  452. {
  453. float margin;
  454. if (content_area.y >= 0)
  455. {
  456. margin = (containing_block_height - (box.GetCumulativeEdge(Box::CONTENT, Box::TOP) +
  457. box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM) +
  458. content_area.y)) / num_auto_margins;
  459. }
  460. else
  461. margin = 0;
  462. if (margins_auto[0])
  463. box.SetEdge(Box::MARGIN, Box::TOP, margin);
  464. if (margins_auto[1])
  465. box.SetEdge(Box::MARGIN, Box::BOTTOM, margin);
  466. }
  467. if (content_area.y >= 0)
  468. {
  469. // Clamp the calculated height; if the height is changed by the clamp, then the margins need to be recalculated if
  470. // they were set to auto.
  471. float clamped_height = ClampHeight(content_area.y, computed, box, containing_block_height);
  472. if (clamped_height != content_area.y)
  473. {
  474. content_area.y = clamped_height;
  475. box.SetContent(content_area);
  476. if (num_auto_margins > 0)
  477. {
  478. // Reset the automatic margins.
  479. if (margins_auto[0])
  480. box.SetEdge(Box::MARGIN, Box::TOP, 0);
  481. if (margins_auto[1])
  482. box.SetEdge(Box::MARGIN, Box::BOTTOM, 0);
  483. BuildBoxHeight(box, computed, containing_block_height);
  484. }
  485. return;
  486. }
  487. }
  488. box.SetContent(content_area);
  489. }
  490. } // namespace Rml