LayoutDetails.cpp 22 KB

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