LayoutDetails.cpp 24 KB

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