LayoutDetails.cpp 24 KB

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