LayoutDetails.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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/Math.h"
  31. #include <float.h>
  32. namespace Rml {
  33. // Generates the box for an element.
  34. void LayoutDetails::BuildBox(Box& box, const Vector2f& containing_block, Element* element, bool inline_element, bool allow_shrink)
  35. {
  36. if (element == nullptr)
  37. {
  38. box.SetContent(containing_block);
  39. return;
  40. }
  41. const ComputedValues& computed = element->GetComputedValues();
  42. // Calculate the padding area.
  43. float padding = ResolveValue(computed.padding_top, containing_block.x);
  44. box.SetEdge(Box::PADDING, Box::TOP, Math::Max(0.0f, padding));
  45. padding = ResolveValue(computed.padding_right, containing_block.x);
  46. box.SetEdge(Box::PADDING, Box::RIGHT, Math::Max(0.0f, padding));
  47. padding = ResolveValue(computed.padding_bottom, containing_block.x);
  48. box.SetEdge(Box::PADDING, Box::BOTTOM, Math::Max(0.0f, padding));
  49. padding = ResolveValue(computed.padding_left, containing_block.x);
  50. box.SetEdge(Box::PADDING, Box::LEFT, Math::Max(0.0f, padding));
  51. // Calculate the border area.
  52. box.SetEdge(Box::BORDER, Box::TOP, Math::Max(0.0f, computed.border_top_width));
  53. box.SetEdge(Box::BORDER, Box::RIGHT, Math::Max(0.0f, computed.border_right_width));
  54. box.SetEdge(Box::BORDER, Box::BOTTOM, Math::Max(0.0f, computed.border_bottom_width));
  55. box.SetEdge(Box::BORDER, Box::LEFT, Math::Max(0.0f, computed.border_left_width));
  56. // Calculate the size of the content area.
  57. Vector2f content_area(-1, -1);
  58. bool replaced_element = false;
  59. // If the element has intrinsic dimensions, then we use those as the basis for the content area and only adjust
  60. // them if a non-auto style has been applied to them.
  61. if (element->GetIntrinsicDimensions(content_area))
  62. {
  63. replaced_element = true;
  64. Vector2f original_content_area = content_area;
  65. // The element has resized itself, so we only resize it if a RCSS width or height was set explicitly. A value of
  66. // 'auto' (or 'auto-fit', ie, both keywords) means keep (or adjust) the intrinsic dimensions.
  67. bool auto_width = false, auto_height = false;
  68. if (computed.width.type != Style::Width::Auto)
  69. content_area.x = ResolveValue(computed.width, containing_block.x);
  70. else
  71. auto_width = true;
  72. if (computed.height.type != Style::Height::Auto)
  73. content_area.y = ResolveValue(computed.height, containing_block.y);
  74. else
  75. auto_height = true;
  76. // If one of the dimensions is 'auto' then we need to scale it such that the original ratio is preserved.
  77. if (auto_width && !auto_height)
  78. content_area.x = (content_area.y / original_content_area.y) * original_content_area.x;
  79. else if (auto_height && !auto_width)
  80. content_area.y = (content_area.x / original_content_area.x) * original_content_area.y;
  81. // Reduce the width and height to make up for borders and padding.
  82. content_area.x -= (box.GetEdge(Box::BORDER, Box::LEFT) +
  83. box.GetEdge(Box::PADDING, Box::LEFT) +
  84. box.GetEdge(Box::BORDER, Box::RIGHT) +
  85. box.GetEdge(Box::PADDING, Box::RIGHT));
  86. content_area.y -= (box.GetEdge(Box::BORDER, Box::TOP) +
  87. box.GetEdge(Box::PADDING, Box::TOP) +
  88. box.GetEdge(Box::BORDER, Box::BOTTOM) +
  89. box.GetEdge(Box::PADDING, Box::BOTTOM));
  90. content_area.x = Math::Max(content_area.x, 0.0f);
  91. content_area.y = Math::Max(content_area.y, 0.0f);
  92. }
  93. // If the element is inline, then its calculations are much more straightforward (no worrying about auto margins
  94. // and dimensions, etc). All we do is calculate the margins, set the content area and bail.
  95. if (inline_element)
  96. {
  97. if (replaced_element)
  98. {
  99. content_area.x = ClampWidth(content_area.x, computed, containing_block.x);
  100. content_area.y = ClampHeight(content_area.y, computed, containing_block.y);
  101. }
  102. // If the element was not replaced, then we leave its dimension as unsized (-1, -1) and ignore the width and
  103. // height properties.
  104. box.SetContent(content_area);
  105. // Evaluate the margins. Any declared as 'auto' will resolve to 0.
  106. box.SetEdge(Box::MARGIN, Box::TOP, ResolveValue(computed.margin_top, containing_block.x));
  107. box.SetEdge(Box::MARGIN, Box::RIGHT, ResolveValue(computed.margin_right, containing_block.x));
  108. box.SetEdge(Box::MARGIN, Box::BOTTOM, ResolveValue(computed.margin_bottom, containing_block.x));
  109. box.SetEdge(Box::MARGIN, Box::LEFT, ResolveValue(computed.margin_left, containing_block.x));
  110. }
  111. // The element is block, so we need to run the box through the ringer to potentially evaluate auto margins and
  112. // dimensions.
  113. else
  114. {
  115. box.SetContent(content_area);
  116. BuildBoxWidth(box, computed, containing_block, element, allow_shrink, replaced_element);
  117. BuildBoxHeight(box, computed, containing_block.y);
  118. }
  119. }
  120. // Generates the box for an element placed in a block box.
  121. void LayoutDetails::BuildBox(Box& box, float& min_height, float& max_height, LayoutBlockBox* containing_box, Element* element, bool inline_element, bool allow_shrink)
  122. {
  123. Vector2f containing_block = GetContainingBlock(containing_box);
  124. BuildBox(box, containing_block, element, inline_element, allow_shrink);
  125. float box_height = box.GetSize().y;
  126. if (box_height < 0)
  127. {
  128. auto& computed = element->GetComputedValues();
  129. min_height = ResolveValue(computed.min_height, containing_block.y);
  130. max_height = (computed.max_height.value < 0.f ? FLT_MAX : ResolveValue(computed.max_height, containing_block.y));
  131. }
  132. else
  133. {
  134. min_height = box_height;
  135. max_height = box_height;
  136. }
  137. }
  138. // Clamps the width of an element based from its min-width and max-width properties.
  139. float LayoutDetails::ClampWidth(float width, const ComputedValues& computed, float containing_block_width)
  140. {
  141. float min_width = ResolveValue(computed.min_width, containing_block_width);
  142. float max_width = (computed.max_width.value < 0.f ? FLT_MAX : ResolveValue(computed.max_width, containing_block_width));
  143. return Math::Clamp(width, min_width, max_width);
  144. }
  145. // Clamps the height of an element based from its min-height and max-height properties.
  146. float LayoutDetails::ClampHeight(float height, const ComputedValues& computed, float containing_block_height)
  147. {
  148. float min_height = ResolveValue(computed.min_height, containing_block_height);
  149. float max_height = (computed.max_height.value < 0.f ? FLT_MAX : ResolveValue(computed.max_height, containing_block_height));
  150. return Math::Clamp(height, min_height, max_height);
  151. }
  152. // Returns the fully-resolved, fixed-width and -height containing block from a block box.
  153. Vector2f LayoutDetails::GetContainingBlock(const LayoutBlockBox* containing_box)
  154. {
  155. Vector2f containing_block;
  156. containing_block.x = containing_box->GetBox().GetSize(Box::CONTENT).x;
  157. if (containing_box->GetElement() != nullptr)
  158. containing_block.x -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  159. while ((containing_block.y = containing_box->GetBox().GetSize(Box::CONTENT).y) < 0)
  160. {
  161. containing_box = containing_box->GetParent();
  162. if (containing_box == nullptr)
  163. {
  164. RMLUI_ERROR;
  165. containing_block.y = 0;
  166. }
  167. }
  168. if (containing_box != nullptr &&
  169. containing_box->GetElement() != nullptr)
  170. containing_block.y -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL);
  171. containing_block.x = Math::Max(0.0f, containing_block.x);
  172. containing_block.y = Math::Max(0.0f, containing_block.y);
  173. return containing_block;
  174. }
  175. float LayoutDetails::GetShrinkToFitWidth(Element* element, Vector2f containing_block)
  176. {
  177. LayoutBlockBox containing_block_box(nullptr, nullptr);
  178. containing_block_box.GetBox().SetContent(containing_block);
  179. LayoutBlockBox* block_context_box = containing_block_box.AddBlockElement(element, false);
  180. for (int i = 0; i < element->GetNumChildren(); i++)
  181. {
  182. if (!LayoutEngine::FormatElement(block_context_box, element->GetChild(i)))
  183. i = -1;
  184. }
  185. // We only do layouting to get the fit-to-shrink width here, and for this purpose we may get
  186. // away with not closing the boxes. This is avoided for performance reasons.
  187. //block_context_box->Close();
  188. //block_context_box->CloseAbsoluteElements();
  189. return Math::Min(containing_block.x, block_context_box->GetShrinkToFitWidth());
  190. }
  191. // Builds the block-specific width and horizontal margins of a Box.
  192. void LayoutDetails::BuildBoxWidth(Box& box, const ComputedValues& computed, Vector2f containing_block, Element* element, bool allow_shrink, bool replaced_element)
  193. {
  194. RMLUI_ZoneScoped;
  195. Vector2f content_area = box.GetSize();
  196. // Determine if the element has an automatic width, and if not calculate it.
  197. bool width_auto;
  198. if (content_area.x >= 0)
  199. {
  200. width_auto = false;
  201. }
  202. else
  203. {
  204. if (computed.width.type == Style::Width::Auto)
  205. {
  206. width_auto = true;
  207. }
  208. else
  209. {
  210. width_auto = false;
  211. content_area.x = ResolveValue(computed.width, containing_block.x);
  212. }
  213. }
  214. // Determine if the element has automatic margins.
  215. bool margins_auto[2];
  216. int num_auto_margins = 0;
  217. for (int i = 0; i < 2; ++i)
  218. {
  219. auto* margin_value = (i == 0 ? &computed.margin_left : &computed.margin_right);
  220. if (margin_value->type == Style::Margin::Auto)
  221. {
  222. margins_auto[i] = true;
  223. num_auto_margins++;
  224. }
  225. else
  226. {
  227. margins_auto[i] = false;
  228. box.SetEdge(Box::MARGIN, i == 0 ? Box::LEFT : Box::RIGHT, ResolveValue(*margin_value, containing_block.x));
  229. }
  230. }
  231. // If the width is set to auto, we need to calculate the width
  232. if (width_auto)
  233. {
  234. // Apply the shrink-to-fit algorithm here to find the width of the element.
  235. // See CSS 2.1 section 10.3.7 for when this should be applied.
  236. const bool shrink_to_fit = allow_shrink && !replaced_element &&
  237. (
  238. (computed.float_ != Style::Float::None) ||
  239. ((computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed) && (computed.left.type == Style::Left::Auto || computed.right.type == Style::Right::Auto)) ||
  240. (computed.display == Style::Display::InlineBlock)
  241. );
  242. float left = 0.0f, right = 0.0f;
  243. // If we are dealing with an absolutely positioned element we need to
  244. // consider if the left and right properties are set, since the width can be affected.
  245. if (computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed)
  246. {
  247. if (computed.left.type != Style::Left::Auto)
  248. left = ResolveValue(computed.left, containing_block.x);
  249. if (computed.right.type != Style::Right::Auto)
  250. right = ResolveValue(computed.right, containing_block.x);
  251. }
  252. if (margins_auto[0])
  253. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  254. if (margins_auto[1])
  255. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  256. if (shrink_to_fit)
  257. {
  258. content_area.x = GetShrinkToFitWidth(element, containing_block);
  259. }
  260. else
  261. {
  262. // We resolve any auto margins to 0 and the width is set to whatever is left of the containing block.
  263. content_area.x = containing_block.x - (left +
  264. box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  265. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  266. right);
  267. content_area.x = Math::Max(0.0f, content_area.x);
  268. }
  269. }
  270. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  271. else if (num_auto_margins > 0)
  272. {
  273. float margin = (containing_block.x - (box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  274. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  275. content_area.x)) / num_auto_margins;
  276. if (margins_auto[0])
  277. box.SetEdge(Box::MARGIN, Box::LEFT, margin);
  278. if (margins_auto[1])
  279. box.SetEdge(Box::MARGIN, Box::RIGHT, margin);
  280. }
  281. // Clamp the calculated width; if the width is changed by the clamp, then the margins need to be recalculated if
  282. // they were set to auto.
  283. float clamped_width = ClampWidth(content_area.x, computed, containing_block.x);
  284. if (clamped_width != content_area.x)
  285. {
  286. content_area.x = clamped_width;
  287. box.SetContent(content_area);
  288. if (num_auto_margins > 0)
  289. {
  290. // Reset the automatic margins.
  291. if (margins_auto[0])
  292. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  293. if (margins_auto[1])
  294. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  295. BuildBoxWidth(box, computed, containing_block, element, allow_shrink, replaced_element);
  296. }
  297. }
  298. else
  299. box.SetContent(content_area);
  300. }
  301. // Builds the block-specific height and vertical margins of a Box.
  302. void LayoutDetails::BuildBoxHeight(Box& box, const ComputedValues& computed, float containing_block_height)
  303. {
  304. RMLUI_ZoneScoped;
  305. Vector2f content_area = box.GetSize();
  306. // Determine if the element has an automatic height, and if not calculate it.
  307. bool height_auto;
  308. if (content_area.y >= 0)
  309. {
  310. height_auto = false;
  311. }
  312. else
  313. {
  314. if (computed.height.type == Style::Height::Auto)
  315. {
  316. height_auto = true;
  317. }
  318. else
  319. {
  320. height_auto = false;
  321. content_area.y = ResolveValue(computed.height, containing_block_height);
  322. }
  323. }
  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. auto* margin_value = (i == 0 ? &computed.margin_top : &computed.margin_bottom);
  330. if (margin_value->type == Style::Margin::Auto)
  331. {
  332. margins_auto[i] = true;
  333. num_auto_margins++;
  334. }
  335. else
  336. {
  337. margins_auto[i] = false;
  338. box.SetEdge(Box::MARGIN, i == 0 ? Box::TOP : Box::BOTTOM, ResolveValue(*margin_value, containing_block_height));
  339. }
  340. }
  341. // If the height is set to auto, we need to calculate the height
  342. if (height_auto)
  343. {
  344. // We resolve any auto margins to 0
  345. if (margins_auto[0])
  346. box.SetEdge(Box::MARGIN, Box::TOP, 0);
  347. if (margins_auto[1])
  348. box.SetEdge(Box::MARGIN, Box::BOTTOM, 0);
  349. // If the height is set to auto for a box in normal flow, the height is set to -1.
  350. content_area.y = -1;
  351. // But if we are dealing with an absolutely positioned element we need to
  352. // consider if the top and bottom properties are set, since the height can be affected.
  353. if (computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed)
  354. {
  355. float top = 0.0f, bottom = 0.0f;
  356. if (computed.top.type != Style::Top::Auto && computed.bottom.type != Style::Bottom::Auto)
  357. {
  358. top = ResolveValue(computed.top, containing_block_height );
  359. bottom = ResolveValue(computed.bottom, containing_block_height );
  360. // The height gets resolved to whatever is left of the containing block
  361. content_area.y = containing_block_height - (top +
  362. box.GetCumulativeEdge(Box::CONTENT, Box::TOP) +
  363. box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM) +
  364. bottom);
  365. content_area.y = Math::Max(0.0f, content_area.y);
  366. }
  367. }
  368. }
  369. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  370. else if (num_auto_margins > 0)
  371. {
  372. float margin;
  373. if (content_area.y >= 0)
  374. {
  375. margin = (containing_block_height - (box.GetCumulativeEdge(Box::CONTENT, Box::TOP) +
  376. box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM) +
  377. content_area.y)) / num_auto_margins;
  378. }
  379. else
  380. margin = 0;
  381. if (margins_auto[0])
  382. box.SetEdge(Box::MARGIN, Box::TOP, margin);
  383. if (margins_auto[1])
  384. box.SetEdge(Box::MARGIN, Box::BOTTOM, margin);
  385. }
  386. if (content_area.y >= 0)
  387. {
  388. // Clamp the calculated height; if the height is changed by the clamp, then the margins need to be recalculated if
  389. // they were set to auto.
  390. float clamped_height = ClampHeight(content_area.y, computed, containing_block_height);
  391. if (clamped_height != content_area.y)
  392. {
  393. content_area.y = clamped_height;
  394. box.SetContent(content_area);
  395. if (num_auto_margins > 0)
  396. {
  397. // Reset the automatic margins.
  398. if (margins_auto[0])
  399. box.SetEdge(Box::MARGIN, Box::TOP, 0);
  400. if (margins_auto[1])
  401. box.SetEdge(Box::MARGIN, Box::BOTTOM, 0);
  402. BuildBoxHeight(box, computed, containing_block_height);
  403. }
  404. return;
  405. }
  406. }
  407. box.SetContent(content_area);
  408. }
  409. } // namespace Rml