2
0

LayoutDetails.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #include "LayoutDetails.h"
  2. #include "../../../Include/RmlUi/Core/ComputedValues.h"
  3. #include "../../../Include/RmlUi/Core/Element.h"
  4. #include "../../../Include/RmlUi/Core/ElementScroll.h"
  5. #include "../../../Include/RmlUi/Core/ElementText.h"
  6. #include "../../../Include/RmlUi/Core/Math.h"
  7. #include "../../../Include/RmlUi/Core/Profiling.h"
  8. #include "ContainerBox.h"
  9. #include "FormattingContext.h"
  10. #include "LayoutEngine.h"
  11. #include <float.h>
  12. namespace Rml {
  13. // Convert width or height of a border box to the width or height of its corresponding content box.
  14. static inline float BorderSizeToContentSize(float border_size, float border_padding_edges_size)
  15. {
  16. if (border_size < 0.0f || border_size >= FLT_MAX)
  17. return border_size;
  18. return Math::Max(0.0f, border_size - border_padding_edges_size);
  19. }
  20. void LayoutDetails::BuildBox(Box& box, Vector2f containing_block, Element* element, BuildBoxMode box_context)
  21. {
  22. if (!element)
  23. {
  24. box.SetContent(containing_block);
  25. return;
  26. }
  27. const ComputedValues& computed = element->GetComputedValues();
  28. // Calculate the padding area.
  29. box.SetEdge(BoxArea::Padding, BoxEdge::Top, Math::Max(0.0f, ResolveValue(computed.padding_top(), containing_block.x)));
  30. box.SetEdge(BoxArea::Padding, BoxEdge::Right, Math::Max(0.0f, ResolveValue(computed.padding_right(), containing_block.x)));
  31. box.SetEdge(BoxArea::Padding, BoxEdge::Bottom, Math::Max(0.0f, ResolveValue(computed.padding_bottom(), containing_block.x)));
  32. box.SetEdge(BoxArea::Padding, BoxEdge::Left, Math::Max(0.0f, ResolveValue(computed.padding_left(), containing_block.x)));
  33. // Calculate the border area.
  34. box.SetEdge(BoxArea::Border, BoxEdge::Top, Math::Max(0.0f, computed.border_top_width()));
  35. box.SetEdge(BoxArea::Border, BoxEdge::Right, Math::Max(0.0f, computed.border_right_width()));
  36. box.SetEdge(BoxArea::Border, BoxEdge::Bottom, Math::Max(0.0f, computed.border_bottom_width()));
  37. box.SetEdge(BoxArea::Border, BoxEdge::Left, Math::Max(0.0f, computed.border_left_width()));
  38. // Prepare sizing of the content area.
  39. Vector2f content_area(-1, -1);
  40. Vector2f min_size = Vector2f(0, 0);
  41. Vector2f max_size = Vector2f(FLT_MAX, FLT_MAX);
  42. // Intrinsic size for replaced elements.
  43. Vector2f intrinsic_size(-1, -1);
  44. float intrinsic_ratio = -1;
  45. const bool replaced_element = element->GetIntrinsicDimensions(intrinsic_size, intrinsic_ratio);
  46. // Calculate the content area and constraints. 'auto' width and height are handled later.
  47. // For inline non-replaced elements, width and height are ignored, so we can skip the calculations.
  48. if (box_context == BuildBoxMode::Block || box_context == BuildBoxMode::UnalignedBlock || replaced_element)
  49. {
  50. content_area.x = ResolveValueOr(computed.width(), containing_block.x, -1.f);
  51. content_area.y = ResolveValueOr(computed.height(), containing_block.y, -1.f);
  52. min_size = Vector2f{
  53. ResolveValueOr(computed.min_width(), containing_block.x, 0.f),
  54. ResolveValueOr(computed.min_height(), containing_block.y, 0.f),
  55. };
  56. max_size = Vector2f{
  57. ResolveValueOr(computed.max_width(), containing_block.x, FLT_MAX),
  58. ResolveValueOr(computed.max_height(), containing_block.y, FLT_MAX),
  59. };
  60. // Adjust sizes for the given box sizing model.
  61. if (computed.box_sizing() == Style::BoxSizing::BorderBox)
  62. {
  63. const float border_padding_width = box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Border, BoxArea::Padding);
  64. const float border_padding_height = box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Border, BoxArea::Padding);
  65. min_size.x = BorderSizeToContentSize(min_size.x, border_padding_width);
  66. max_size.x = BorderSizeToContentSize(max_size.x, border_padding_width);
  67. content_area.x = BorderSizeToContentSize(content_area.x, border_padding_width);
  68. min_size.y = BorderSizeToContentSize(min_size.y, border_padding_height);
  69. max_size.y = BorderSizeToContentSize(max_size.y, border_padding_height);
  70. content_area.y = BorderSizeToContentSize(content_area.y, border_padding_height);
  71. }
  72. if (content_area.x >= 0)
  73. content_area.x = Math::Clamp(content_area.x, min_size.x, max_size.x);
  74. if (content_area.y >= 0)
  75. content_area.y = Math::Clamp(content_area.y, min_size.y, max_size.y);
  76. if (replaced_element)
  77. content_area = CalculateSizeForReplacedElement(content_area, min_size, max_size, intrinsic_size, intrinsic_ratio);
  78. }
  79. box.SetContent(content_area);
  80. // Evaluate the margins, and width and height if they are auto.
  81. BuildBoxSizeAndMargins(box, min_size, max_size, containing_block, element, box_context, replaced_element);
  82. }
  83. void LayoutDetails::GetMinMaxWidth(float& min_width, float& max_width, const ComputedValues& computed, const Box& box, float containing_block_width)
  84. {
  85. min_width = ResolveValueOr(computed.min_width(), containing_block_width, 0.f);
  86. max_width = ResolveValueOr(computed.max_width(), containing_block_width, FLT_MAX);
  87. if (computed.box_sizing() == Style::BoxSizing::BorderBox)
  88. {
  89. const float border_padding_width = box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Border, BoxArea::Padding);
  90. min_width = BorderSizeToContentSize(min_width, border_padding_width);
  91. max_width = BorderSizeToContentSize(max_width, border_padding_width);
  92. }
  93. }
  94. void LayoutDetails::GetMinMaxHeight(float& min_height, float& max_height, const ComputedValues& computed, const Box& box,
  95. float containing_block_height)
  96. {
  97. min_height = ResolveValueOr(computed.min_height(), containing_block_height, 0.f);
  98. max_height = ResolveValueOr(computed.max_height(), containing_block_height, FLT_MAX);
  99. if (computed.box_sizing() == Style::BoxSizing::BorderBox)
  100. {
  101. const float border_padding_height = box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Border, BoxArea::Padding);
  102. min_height = BorderSizeToContentSize(min_height, border_padding_height);
  103. max_height = BorderSizeToContentSize(max_height, border_padding_height);
  104. }
  105. }
  106. void LayoutDetails::GetDefiniteMinMaxHeight(float& min_height, float& max_height, const ComputedValues& computed, const Box& box,
  107. float containing_block_height)
  108. {
  109. const float box_height = box.GetSize().y;
  110. if (box_height < 0)
  111. {
  112. GetMinMaxHeight(min_height, max_height, computed, box, containing_block_height);
  113. }
  114. else
  115. {
  116. min_height = box_height;
  117. max_height = box_height;
  118. }
  119. }
  120. ContainingBlock LayoutDetails::GetContainingBlock(ContainerBox* parent_container, const Style::Position position)
  121. {
  122. RMLUI_ASSERT(parent_container);
  123. using Style::Position;
  124. ContainerBox* container = parent_container;
  125. BoxArea area = BoxArea::Content;
  126. // For absolutely positioned boxes we look for the first positioned ancestor. We deviate from the CSS specs by using
  127. // the same rules for fixed boxes, as that is particularly helpful on handles and other widgets that should not
  128. // scroll with the window. This is a common design pattern in target applications for this library, although this
  129. // behavior may be reconsidered in the future.
  130. if (position == Position::Absolute || position == Position::Fixed)
  131. {
  132. area = BoxArea::Padding;
  133. while (container->GetParent() && !container->IsAbsolutePositioningContainingBlock())
  134. container = container->GetParent();
  135. }
  136. const Box* box = container->GetIfBox();
  137. if (!box)
  138. {
  139. RMLUI_ERROR;
  140. return {container, {}};
  141. }
  142. Vector2f containing_block = box->GetSize(area);
  143. if (position == Position::Static || position == Position::Relative)
  144. {
  145. // For static elements we subtract the scrollbar size so that elements normally don't overlap their parent's
  146. // scrollbars. In CSS, this would also be done for absolutely positioned elements, we might want to copy that
  147. // behavior in the future. If so, we would also need to change the element offset behavior, and ideally also
  148. // make positioned boxes contribute to the scrollable area.
  149. if (Element* element = container->GetElement())
  150. {
  151. ElementScroll* element_scroll = element->GetElementScroll();
  152. if (containing_block.x >= 0.f)
  153. containing_block.x = Math::Max(containing_block.x - element_scroll->GetScrollbarSize(ElementScroll::VERTICAL), 0.f);
  154. if (containing_block.y >= 0.f)
  155. containing_block.y = Math::Max(containing_block.y - element_scroll->GetScrollbarSize(ElementScroll::HORIZONTAL), 0.f);
  156. }
  157. }
  158. return {container, containing_block};
  159. }
  160. void LayoutDetails::BuildBoxSizeAndMargins(Box& box, Vector2f min_size, Vector2f max_size, Vector2f containing_block, Element* element,
  161. BuildBoxMode box_context, bool replaced_element)
  162. {
  163. const ComputedValues& computed = element->GetComputedValues();
  164. if (box_context == BuildBoxMode::Inline || box_context == BuildBoxMode::UnalignedBlock)
  165. {
  166. // For inline elements, their calculations are straightforward. No worrying about auto margins and dimensions, etc.
  167. // Evaluate the margins. Any declared as 'auto' will resolve to 0.
  168. box.SetEdge(BoxArea::Margin, BoxEdge::Top, ResolveValue(computed.margin_top(), containing_block.x));
  169. box.SetEdge(BoxArea::Margin, BoxEdge::Right, ResolveValue(computed.margin_right(), containing_block.x));
  170. box.SetEdge(BoxArea::Margin, BoxEdge::Bottom, ResolveValue(computed.margin_bottom(), containing_block.x));
  171. box.SetEdge(BoxArea::Margin, BoxEdge::Left, ResolveValue(computed.margin_left(), containing_block.x));
  172. }
  173. else
  174. {
  175. // The element is block, so we need to run the box through the ringer to potentially evaluate auto margins and dimensions.
  176. BuildBoxWidth(box, computed, min_size.x, max_size.x, containing_block, element, replaced_element);
  177. BuildBoxHeight(box, computed, min_size.y, max_size.y, containing_block.y);
  178. }
  179. }
  180. float LayoutDetails::GetShrinkToFitWidth(Element* element, Vector2f containing_block)
  181. {
  182. RMLUI_ASSERT(element);
  183. // @performance Can we lay out the elements directly using a fit-content size mode, instead of fetching the
  184. // shrink-to-fit width first? Use a non-definite placeholder for the box content width, and available width as a
  185. // maximum constraint.
  186. Box box;
  187. float min_height, max_height;
  188. LayoutDetails::BuildBox(box, containing_block, element, BuildBoxMode::UnalignedBlock);
  189. LayoutDetails::GetDefiniteMinMaxHeight(min_height, max_height, element->GetComputedValues(), box, containing_block.y);
  190. if (box.GetSize().x >= 0.f)
  191. {
  192. return box.GetSize().x;
  193. }
  194. // Currently we don't support shrink-to-fit width for tables. Just return a zero-sized width.
  195. const Style::Display display = element->GetDisplay();
  196. if (display == Style::Display::Table || display == Style::Display::InlineTable)
  197. {
  198. return 0.f;
  199. }
  200. // Use a large size for the box content width, so that it is practically unconstrained. This makes the formatting
  201. // procedure act as if under a maximum content constraint. Children with percentage sizing values may be scaled
  202. // based on this width (such as 'width' or 'margin'), if so, the layout is considered undefined like in CSS 2.
  203. const float max_content_constraint_width = containing_block.x + 10000.f;
  204. box.SetContent({max_content_constraint_width, box.GetSize().y});
  205. // First, format the element under the above generated box. Then we ask the resulting box for its shrink-to-fit
  206. // width. For block containers, this is essentially its largest line or child box.
  207. // @performance. Some formatting can be simplified, e.g. absolute elements do not contribute to the shrink-to-fit
  208. // width. Also, children of elements with a fixed width and height don't need to be formatted further.
  209. RootBox root(Math::Max(containing_block, Vector2f(0.f)));
  210. UniquePtr<LayoutBox> layout_box = FormattingContext::FormatIndependent(&root, element, &box, FormattingContextType::Block);
  211. float shrink_to_fit_width = layout_box->GetShrinkToFitWidth();
  212. if (containing_block.x >= 0)
  213. {
  214. const float available_width =
  215. Math::Max(0.f, containing_block.x - box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin, BoxArea::Padding));
  216. shrink_to_fit_width = Math::Min(shrink_to_fit_width, available_width);
  217. }
  218. return shrink_to_fit_width;
  219. }
  220. ComputedAxisSize LayoutDetails::BuildComputedHorizontalSize(const ComputedValues& computed)
  221. {
  222. return ComputedAxisSize{computed.width(), computed.min_width(), computed.max_width(), computed.padding_left(), computed.padding_right(),
  223. computed.margin_left(), computed.margin_right(), computed.border_left_width(), computed.border_right_width(), computed.box_sizing()};
  224. }
  225. ComputedAxisSize LayoutDetails::BuildComputedVerticalSize(const ComputedValues& computed)
  226. {
  227. return ComputedAxisSize{computed.height(), computed.min_height(), computed.max_height(), computed.padding_top(), computed.padding_bottom(),
  228. computed.margin_top(), computed.margin_bottom(), computed.border_top_width(), computed.border_bottom_width(), computed.box_sizing()};
  229. }
  230. void LayoutDetails::GetEdgeSizes(float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b,
  231. const ComputedAxisSize& computed_size, const float base_value)
  232. {
  233. margin_a = ResolveValue(computed_size.margin_a, base_value);
  234. margin_b = ResolveValue(computed_size.margin_b, base_value);
  235. padding_border_a = Math::Max(0.0f, ResolveValue(computed_size.padding_a, base_value)) + Math::Max(0.0f, computed_size.border_a);
  236. padding_border_b = Math::Max(0.0f, ResolveValue(computed_size.padding_b, base_value)) + Math::Max(0.0f, computed_size.border_b);
  237. }
  238. String LayoutDetails::GetDebugElementName(Element* element)
  239. {
  240. if (!element)
  241. return "nullptr";
  242. if (!element->GetId().empty())
  243. return '#' + element->GetId();
  244. if (auto element_text = rmlui_dynamic_cast<ElementText*>(element))
  245. return '\"' + StringUtilities::StripWhitespace(element_text->GetText()).substr(0, 20) + '\"';
  246. return element->GetAddress(false, false);
  247. }
  248. Vector2f LayoutDetails::CalculateSizeForReplacedElement(const Vector2f specified_content_size, const Vector2f min_size, const Vector2f max_size,
  249. const Vector2f intrinsic_size, const float intrinsic_ratio)
  250. {
  251. // Start with the element's specified width and height. If any of them are auto, use the element's intrinsic
  252. // dimensions and ratio to find a suitable content size.
  253. Vector2f content_size = specified_content_size;
  254. const bool auto_width = (content_size.x < 0);
  255. const bool auto_height = (content_size.y < 0);
  256. if (auto_width)
  257. content_size.x = intrinsic_size.x;
  258. if (auto_height)
  259. content_size.y = intrinsic_size.y;
  260. // Use a fallback size if we still couldn't determine the size.
  261. if (content_size.x < 0)
  262. content_size.x = 300;
  263. if (content_size.y < 0)
  264. content_size.y = 150;
  265. // Resolve the size constraints.
  266. const float min_width = min_size.x;
  267. const float max_width = max_size.x;
  268. const float min_height = min_size.y;
  269. const float max_height = max_size.y;
  270. // If we have an intrinsic ratio and one of the dimensions is 'auto', then scale it such that the ratio is preserved.
  271. if (intrinsic_ratio > 0)
  272. {
  273. if (auto_width && !auto_height)
  274. {
  275. content_size.x = content_size.y * intrinsic_ratio;
  276. }
  277. else if (auto_height && !auto_width)
  278. {
  279. content_size.y = content_size.x / intrinsic_ratio;
  280. }
  281. else if (auto_width && auto_height)
  282. {
  283. // If both width and height are auto, try to preserve the ratio under the respective min/max constraints.
  284. const float w = content_size.x;
  285. const float h = content_size.y;
  286. if ((w < min_width && h > max_height) || (w > max_width && h < min_height))
  287. {
  288. // Cannot preserve aspect ratio, let it be clamped.
  289. }
  290. else if (w < min_width && h < min_height)
  291. {
  292. // Increase the size such that both min-constraints are respected. The non-scaled axis will
  293. // be clamped below, preserving the aspect ratio.
  294. if (min_width <= min_height * intrinsic_ratio)
  295. content_size.x = min_height * intrinsic_ratio;
  296. else
  297. content_size.y = min_width / intrinsic_ratio;
  298. }
  299. else if (w > max_width && h > max_height)
  300. {
  301. // Shrink the size such that both max-constraints are respected. The non-scaled axis will
  302. // be clamped below, preserving the aspect ratio.
  303. if (max_width <= max_height * intrinsic_ratio)
  304. content_size.y = max_width / intrinsic_ratio;
  305. else
  306. content_size.x = max_height * intrinsic_ratio;
  307. }
  308. else
  309. {
  310. // Single constraint violations.
  311. if (w < min_width)
  312. content_size.y = min_width / intrinsic_ratio;
  313. else if (w > max_width)
  314. content_size.y = max_width / intrinsic_ratio;
  315. else if (h < min_height)
  316. content_size.x = min_height * intrinsic_ratio;
  317. else if (h > max_height)
  318. content_size.x = max_height * intrinsic_ratio;
  319. }
  320. }
  321. }
  322. content_size.x = Math::Clamp(content_size.x, min_width, max_width);
  323. content_size.y = Math::Clamp(content_size.y, min_height, max_height);
  324. return content_size;
  325. }
  326. void LayoutDetails::BuildBoxWidth(Box& box, const ComputedValues& computed, float min_width, float max_width, Vector2f containing_block,
  327. Element* element, bool replaced_element, float override_shrink_to_fit_width)
  328. {
  329. RMLUI_ZoneScoped;
  330. Vector2f content_area = box.GetSize();
  331. // Determine if the element has automatic margins.
  332. bool margins_auto[2];
  333. int num_auto_margins = 0;
  334. for (int i = 0; i < 2; ++i)
  335. {
  336. const Style::Margin margin_value = (i == 0 ? computed.margin_left() : computed.margin_right());
  337. if (margin_value.type == Style::Margin::Auto)
  338. {
  339. margins_auto[i] = true;
  340. num_auto_margins++;
  341. box.SetEdge(BoxArea::Margin, i == 0 ? BoxEdge::Left : BoxEdge::Right, 0);
  342. }
  343. else
  344. {
  345. margins_auto[i] = false;
  346. box.SetEdge(BoxArea::Margin, i == 0 ? BoxEdge::Left : BoxEdge::Right, ResolveValue(margin_value, containing_block.x));
  347. }
  348. }
  349. const bool absolutely_positioned = (computed.position() == Style::Position::Absolute || computed.position() == Style::Position::Fixed);
  350. const bool inset_auto = (computed.left().type == Style::Left::Auto || computed.right().type == Style::Right::Auto);
  351. const bool width_auto = (content_area.x < 0);
  352. auto GetInsetWidth = [&] {
  353. // For absolutely positioned elements (and only those), the 'left' and 'right' values are part of the box's width constraint.
  354. if (absolutely_positioned)
  355. return ResolveValue(computed.left(), containing_block.x) + ResolveValue(computed.right(), containing_block.x);
  356. return 0.f;
  357. };
  358. // If the width is set to auto, we need to calculate the width.
  359. if (width_auto)
  360. {
  361. // Apply the shrink-to-fit algorithm here to find the width of the element.
  362. // See CSS 2.1 section 10.3.7 for when this should be applied.
  363. const bool shrink_to_fit = !replaced_element &&
  364. ((computed.float_() != Style::Float::None) || (absolutely_positioned && inset_auto) ||
  365. (computed.display() == Style::Display::InlineBlock || computed.display() == Style::Display::InlineFlex));
  366. if (!shrink_to_fit)
  367. {
  368. // The width is set to whatever remains of the containing block.
  369. content_area.x = containing_block.x - (GetInsetWidth() + box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin, BoxArea::Padding));
  370. content_area.x = Math::Max(0.0f, content_area.x);
  371. }
  372. else if (override_shrink_to_fit_width >= 0)
  373. {
  374. content_area.x = override_shrink_to_fit_width;
  375. }
  376. else
  377. {
  378. content_area.x = GetShrinkToFitWidth(element, containing_block);
  379. override_shrink_to_fit_width = content_area.x;
  380. }
  381. }
  382. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  383. else if (num_auto_margins > 0)
  384. {
  385. const float margin =
  386. (containing_block.x - (GetInsetWidth() + box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin))) / float(num_auto_margins);
  387. if (margins_auto[0])
  388. box.SetEdge(BoxArea::Margin, BoxEdge::Left, margin);
  389. if (margins_auto[1])
  390. box.SetEdge(BoxArea::Margin, BoxEdge::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, clamped_width);
  401. }
  402. else
  403. box.SetContent(content_area);
  404. }
  405. void LayoutDetails::BuildBoxHeight(Box& box, const ComputedValues& computed, float min_height, float max_height, float containing_block_height)
  406. {
  407. RMLUI_ZoneScoped;
  408. Vector2f content_area = box.GetSize();
  409. // Determine if the element has automatic margins.
  410. bool margins_auto[2];
  411. int num_auto_margins = 0;
  412. for (int i = 0; i < 2; ++i)
  413. {
  414. const Style::Margin margin_value = (i == 0 ? computed.margin_top() : computed.margin_bottom());
  415. if (margin_value.type == Style::Margin::Auto)
  416. {
  417. margins_auto[i] = true;
  418. num_auto_margins++;
  419. box.SetEdge(BoxArea::Margin, i == 0 ? BoxEdge::Top : BoxEdge::Bottom, 0);
  420. }
  421. else
  422. {
  423. margins_auto[i] = false;
  424. box.SetEdge(BoxArea::Margin, i == 0 ? BoxEdge::Top : BoxEdge::Bottom, ResolveValue(margin_value, containing_block_height));
  425. }
  426. }
  427. const bool absolutely_positioned = (computed.position() == Style::Position::Absolute || computed.position() == Style::Position::Fixed);
  428. const bool inset_auto = (computed.top().type == Style::Top::Auto || computed.bottom().type == Style::Bottom::Auto);
  429. const bool height_auto = (content_area.y < 0);
  430. auto GetInsetHeight = [&] {
  431. // For absolutely positioned elements (and only those), the 'top' and 'bottom' values are part of the box's height constraint.
  432. if (absolutely_positioned)
  433. return ResolveValue(computed.top(), containing_block_height) + ResolveValue(computed.bottom(), containing_block_height);
  434. return 0.f;
  435. };
  436. // If the height is set to auto, we need to calculate the height.
  437. if (height_auto)
  438. {
  439. // If the height is set to auto for a box in normal flow, the height is set to -1, representing indefinite height.
  440. content_area.y = -1;
  441. // But if we are dealing with an absolutely positioned element we need to consider if the top and bottom
  442. // properties are set, since the height can be affected.
  443. if (absolutely_positioned && !inset_auto)
  444. {
  445. // The height is set to whatever remains of the containing block.
  446. content_area.y =
  447. containing_block_height - (GetInsetHeight() + box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin, BoxArea::Padding));
  448. content_area.y = Math::Max(0.0f, content_area.y);
  449. }
  450. }
  451. // Otherwise, the margins that are set to auto will pick up the remaining height of the containing block.
  452. else if (num_auto_margins > 0)
  453. {
  454. const float margin =
  455. (containing_block_height - (GetInsetHeight() + box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin))) / float(num_auto_margins);
  456. if (margins_auto[0])
  457. box.SetEdge(BoxArea::Margin, BoxEdge::Top, margin);
  458. if (margins_auto[1])
  459. box.SetEdge(BoxArea::Margin, BoxEdge::Bottom, margin);
  460. }
  461. if (content_area.y >= 0)
  462. {
  463. // Clamp the calculated height; if the height is changed by the clamp, then the margins need to be recalculated if
  464. // they were set to auto.
  465. float clamped_height = Math::Clamp(content_area.y, min_height, max_height);
  466. if (clamped_height != content_area.y)
  467. {
  468. content_area.y = clamped_height;
  469. box.SetContent(content_area);
  470. if (num_auto_margins > 0)
  471. BuildBoxHeight(box, computed, min_height, max_height, containing_block_height);
  472. return;
  473. }
  474. }
  475. box.SetContent(content_area);
  476. }
  477. } // namespace Rml