LayoutDetails.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "LayoutDetails.h"
  29. #include "LayoutEngine.h"
  30. #include "../../Include/RmlUi/Core/Element.h"
  31. #include "../../Include/RmlUi/Core/ElementScroll.h"
  32. #include "../../Include/RmlUi/Core/Math.h"
  33. #include "../../Include/RmlUi/Core/Profiling.h"
  34. #include <float.h>
  35. namespace Rml {
  36. // Generates the box for an element.
  37. void LayoutDetails::BuildBox(Box& box, Vector2f containing_block, Element* element, bool inline_element, float override_shrink_to_fit_width)
  38. {
  39. if (element == nullptr)
  40. {
  41. box.SetContent(containing_block);
  42. return;
  43. }
  44. const ComputedValues& computed = element->GetComputedValues();
  45. // Calculate the padding area.
  46. float padding = ResolveValue(computed.padding_top, containing_block.x);
  47. box.SetEdge(Box::PADDING, Box::TOP, Math::Max(0.0f, padding));
  48. padding = ResolveValue(computed.padding_right, containing_block.x);
  49. box.SetEdge(Box::PADDING, Box::RIGHT, Math::Max(0.0f, padding));
  50. padding = ResolveValue(computed.padding_bottom, containing_block.x);
  51. box.SetEdge(Box::PADDING, Box::BOTTOM, Math::Max(0.0f, padding));
  52. padding = ResolveValue(computed.padding_left, containing_block.x);
  53. box.SetEdge(Box::PADDING, Box::LEFT, Math::Max(0.0f, padding));
  54. // Calculate the border area.
  55. box.SetEdge(Box::BORDER, Box::TOP, Math::Max(0.0f, computed.border_top_width));
  56. box.SetEdge(Box::BORDER, Box::RIGHT, Math::Max(0.0f, computed.border_right_width));
  57. box.SetEdge(Box::BORDER, Box::BOTTOM, Math::Max(0.0f, computed.border_bottom_width));
  58. box.SetEdge(Box::BORDER, Box::LEFT, Math::Max(0.0f, computed.border_left_width));
  59. // Calculate the size of the content area.
  60. Vector2f content_area(-1, -1);
  61. bool replaced_element = false;
  62. // If the element has intrinsic dimensions, then we use those as the basis for the content area and only adjust
  63. // them if a non-auto style has been applied to them.
  64. if (element->GetIntrinsicDimensions(content_area))
  65. {
  66. replaced_element = true;
  67. Vector2f original_content_area = content_area;
  68. // The element has resized itself, so we only resize it if a RCSS width or height was set explicitly. A value of
  69. // 'auto' (or 'auto-fit', ie, both keywords) means keep (or adjust) the intrinsic dimensions.
  70. bool auto_width = false, auto_height = false;
  71. if (computed.width.type != Style::Width::Auto)
  72. content_area.x = ResolveValue(computed.width, containing_block.x);
  73. else
  74. auto_width = true;
  75. if (computed.height.type != Style::Height::Auto)
  76. content_area.y = ResolveValue(computed.height, containing_block.y);
  77. else
  78. auto_height = true;
  79. // If one of the dimensions is 'auto' then we need to scale it such that the original ratio is preserved.
  80. if (auto_width && !auto_height)
  81. content_area.x = (content_area.y / original_content_area.y) * original_content_area.x;
  82. else if (auto_height && !auto_width)
  83. content_area.y = (content_area.x / original_content_area.x) * original_content_area.y;
  84. // Reduce the width and height to make up for borders and padding.
  85. content_area.x -= (box.GetEdge(Box::BORDER, Box::LEFT) +
  86. box.GetEdge(Box::PADDING, Box::LEFT) +
  87. box.GetEdge(Box::BORDER, Box::RIGHT) +
  88. box.GetEdge(Box::PADDING, Box::RIGHT));
  89. content_area.y -= (box.GetEdge(Box::BORDER, Box::TOP) +
  90. box.GetEdge(Box::PADDING, Box::TOP) +
  91. box.GetEdge(Box::BORDER, Box::BOTTOM) +
  92. box.GetEdge(Box::PADDING, Box::BOTTOM));
  93. content_area.x = Math::Max(content_area.x, 0.0f);
  94. content_area.y = Math::Max(content_area.y, 0.0f);
  95. }
  96. // If the element is inline, then its calculations are much more straightforward (no worrying about auto margins
  97. // and dimensions, etc). All we do is calculate the margins, set the content area and bail.
  98. if (inline_element)
  99. {
  100. if (replaced_element)
  101. {
  102. content_area.x = ClampWidth(content_area.x, computed, containing_block.x);
  103. content_area.y = ClampHeight(content_area.y, computed, containing_block.y);
  104. }
  105. // If the element was not replaced, then we leave its dimension as unsized (-1, -1) and ignore the width and
  106. // height properties.
  107. box.SetContent(content_area);
  108. // Evaluate the margins. Any declared as 'auto' will resolve to 0.
  109. box.SetEdge(Box::MARGIN, Box::TOP, ResolveValue(computed.margin_top, containing_block.x));
  110. box.SetEdge(Box::MARGIN, Box::RIGHT, ResolveValue(computed.margin_right, containing_block.x));
  111. box.SetEdge(Box::MARGIN, Box::BOTTOM, ResolveValue(computed.margin_bottom, containing_block.x));
  112. box.SetEdge(Box::MARGIN, Box::LEFT, ResolveValue(computed.margin_left, containing_block.x));
  113. }
  114. // The element is block, so we need to run the box through the ringer to potentially evaluate auto margins and
  115. // dimensions.
  116. else
  117. {
  118. box.SetContent(content_area);
  119. BuildBoxWidth(box, computed, containing_block, element, replaced_element, override_shrink_to_fit_width);
  120. BuildBoxHeight(box, computed, containing_block.y);
  121. }
  122. }
  123. // Generates the box for an element placed in a block box.
  124. void LayoutDetails::BuildBox(Box& box, float& min_height, float& max_height, LayoutBlockBox* containing_box, Element* element, bool inline_element, float override_shrink_to_fit_width)
  125. {
  126. Vector2f containing_block = GetContainingBlock(containing_box);
  127. BuildBox(box, containing_block, element, inline_element, override_shrink_to_fit_width);
  128. float box_height = box.GetSize().y;
  129. if (box_height < 0 && element)
  130. {
  131. auto& computed = element->GetComputedValues();
  132. min_height = ResolveValue(computed.min_height, containing_block.y);
  133. max_height = (computed.max_height.value < 0.f ? FLT_MAX : ResolveValue(computed.max_height, containing_block.y));
  134. }
  135. else
  136. {
  137. min_height = box_height;
  138. max_height = box_height;
  139. }
  140. }
  141. // Clamps the width of an element based from its min-width and max-width properties.
  142. float LayoutDetails::ClampWidth(float width, const ComputedValues& computed, float containing_block_width)
  143. {
  144. float min_width = ResolveValue(computed.min_width, containing_block_width);
  145. float max_width = (computed.max_width.value < 0.f ? FLT_MAX : ResolveValue(computed.max_width, containing_block_width));
  146. return Math::Clamp(width, min_width, max_width);
  147. }
  148. // Clamps the height of an element based from its min-height and max-height properties.
  149. float LayoutDetails::ClampHeight(float height, const ComputedValues& computed, float containing_block_height)
  150. {
  151. float min_height = ResolveValue(computed.min_height, containing_block_height);
  152. float max_height = (computed.max_height.value < 0.f ? FLT_MAX : ResolveValue(computed.max_height, containing_block_height));
  153. return Math::Clamp(height, min_height, max_height);
  154. }
  155. // Returns the fully-resolved, fixed-width and -height containing block from a block box.
  156. Vector2f LayoutDetails::GetContainingBlock(const LayoutBlockBox* containing_box)
  157. {
  158. Vector2f containing_block;
  159. containing_block.x = containing_box->GetBox().GetSize(Box::CONTENT).x;
  160. if (containing_box->GetElement() != nullptr)
  161. containing_block.x -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  162. while ((containing_block.y = containing_box->GetBox().GetSize(Box::CONTENT).y) < 0)
  163. {
  164. containing_box = containing_box->GetParent();
  165. if (containing_box == nullptr)
  166. {
  167. RMLUI_ERROR;
  168. containing_block.y = 0;
  169. }
  170. }
  171. if (containing_box != nullptr &&
  172. containing_box->GetElement() != nullptr)
  173. containing_block.y -= containing_box->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL);
  174. containing_block.x = Math::Max(0.0f, containing_block.x);
  175. containing_block.y = Math::Max(0.0f, containing_block.y);
  176. return containing_block;
  177. }
  178. float LayoutDetails::GetShrinkToFitWidth(Element* element, Vector2f containing_block)
  179. {
  180. // First we need to format the element, then we get the shrink-to-fit width based on the largest line or box.
  181. LayoutBlockBox containing_block_box(nullptr, nullptr);
  182. containing_block_box.GetBox().SetContent(containing_block);
  183. // Here we fix the element's width to its containing block so that any content is wrapped at this width.
  184. // We can consider to instead set this to infinity and clamp it to the available width later after formatting,
  185. // but right now the formatting procedure doesn't work well with such numbers.
  186. LayoutBlockBox* block_context_box = containing_block_box.AddBlockElement(element, containing_block.x);
  187. // @performance. Some formatting can be simplified, eg. absolute elements do not contribute to the shrink-to-fit width.
  188. // Also, children of elements with a fixed width and height don't need to be formatted further.
  189. for (int i = 0; i < element->GetNumChildren(); i++)
  190. {
  191. if (!LayoutEngine::FormatElement(block_context_box, element->GetChild(i)))
  192. i = -1;
  193. }
  194. // We only do layouting to get the fit-to-shrink width here, and for this purpose we may get
  195. // away with not closing the boxes. This is avoided for performance reasons.
  196. //block_context_box->Close();
  197. return Math::Min(containing_block.x, block_context_box->GetShrinkToFitWidth());
  198. }
  199. // Builds the block-specific width and horizontal margins of a Box.
  200. void LayoutDetails::BuildBoxWidth(Box& box, const ComputedValues& computed, Vector2f containing_block, Element* element, bool replaced_element, float override_shrink_to_fit_width)
  201. {
  202. RMLUI_ZoneScoped;
  203. Vector2f content_area = box.GetSize();
  204. // Determine if the element has an automatic width, and if not calculate it.
  205. bool width_auto;
  206. if (content_area.x >= 0)
  207. {
  208. width_auto = false;
  209. }
  210. else
  211. {
  212. if (computed.width.type == Style::Width::Auto)
  213. {
  214. width_auto = true;
  215. }
  216. else
  217. {
  218. width_auto = false;
  219. content_area.x = ResolveValue(computed.width, containing_block.x);
  220. }
  221. }
  222. // Determine if the element has automatic margins.
  223. bool margins_auto[2];
  224. int num_auto_margins = 0;
  225. for (int i = 0; i < 2; ++i)
  226. {
  227. auto* margin_value = (i == 0 ? &computed.margin_left : &computed.margin_right);
  228. if (margin_value->type == Style::Margin::Auto)
  229. {
  230. margins_auto[i] = true;
  231. num_auto_margins++;
  232. }
  233. else
  234. {
  235. margins_auto[i] = false;
  236. box.SetEdge(Box::MARGIN, i == 0 ? Box::LEFT : Box::RIGHT, ResolveValue(*margin_value, containing_block.x));
  237. }
  238. }
  239. // If the width is set to auto, we need to calculate the width
  240. if (width_auto)
  241. {
  242. // Apply the shrink-to-fit algorithm here to find the width of the element.
  243. // See CSS 2.1 section 10.3.7 for when this should be applied.
  244. const bool shrink_to_fit = !replaced_element &&
  245. (
  246. (computed.float_ != Style::Float::None) ||
  247. ((computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed) && (computed.left.type == Style::Left::Auto || computed.right.type == Style::Right::Auto)) ||
  248. (computed.display == Style::Display::InlineBlock)
  249. );
  250. float left = 0.0f, right = 0.0f;
  251. // If we are dealing with an absolutely positioned element we need to
  252. // consider if the left and right properties are set, since the width can be affected.
  253. if (computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed)
  254. {
  255. if (computed.left.type != Style::Left::Auto)
  256. left = ResolveValue(computed.left, containing_block.x);
  257. if (computed.right.type != Style::Right::Auto)
  258. right = ResolveValue(computed.right, containing_block.x);
  259. }
  260. if (margins_auto[0])
  261. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  262. if (margins_auto[1])
  263. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  264. if (shrink_to_fit && override_shrink_to_fit_width < 0)
  265. {
  266. content_area.x = GetShrinkToFitWidth(element, containing_block);
  267. override_shrink_to_fit_width = content_area.x;
  268. }
  269. else if (shrink_to_fit)
  270. {
  271. content_area.x = override_shrink_to_fit_width;
  272. }
  273. else
  274. {
  275. // We resolve any auto margins to 0 and the width is set to whatever is left of the containing block.
  276. content_area.x = containing_block.x - (left +
  277. box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  278. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  279. right);
  280. content_area.x = Math::Max(0.0f, content_area.x);
  281. }
  282. }
  283. // Otherwise, the margins that are set to auto will pick up the remaining width of the containing block.
  284. else if (num_auto_margins > 0)
  285. {
  286. float margin = (containing_block.x - (box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  287. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  288. content_area.x)) / num_auto_margins;
  289. if (margins_auto[0])
  290. box.SetEdge(Box::MARGIN, Box::LEFT, margin);
  291. if (margins_auto[1])
  292. box.SetEdge(Box::MARGIN, Box::RIGHT, margin);
  293. }
  294. // Clamp the calculated width; if the width is changed by the clamp, then the margins need to be recalculated if
  295. // they were set to auto.
  296. float clamped_width = ClampWidth(content_area.x, computed, containing_block.x);
  297. if (clamped_width != content_area.x)
  298. {
  299. content_area.x = clamped_width;
  300. box.SetContent(content_area);
  301. if (num_auto_margins > 0)
  302. {
  303. // Reset the automatic margins.
  304. if (margins_auto[0])
  305. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  306. if (margins_auto[1])
  307. box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  308. BuildBoxWidth(box, computed, containing_block, element, replaced_element, override_shrink_to_fit_width);
  309. }
  310. }
  311. else
  312. box.SetContent(content_area);
  313. }
  314. // Builds the block-specific height and vertical margins of a Box.
  315. void LayoutDetails::BuildBoxHeight(Box& box, const ComputedValues& computed, float containing_block_height)
  316. {
  317. RMLUI_ZoneScoped;
  318. Vector2f content_area = box.GetSize();
  319. // Determine if the element has an automatic height, and if not calculate it.
  320. bool height_auto;
  321. if (content_area.y >= 0)
  322. {
  323. height_auto = false;
  324. }
  325. else
  326. {
  327. if (computed.height.type == Style::Height::Auto)
  328. {
  329. height_auto = true;
  330. }
  331. else
  332. {
  333. height_auto = false;
  334. content_area.y = ResolveValue(computed.height, containing_block_height);
  335. }
  336. }
  337. // Determine if the element has automatic margins.
  338. bool margins_auto[2];
  339. int num_auto_margins = 0;
  340. for (int i = 0; i < 2; ++i)
  341. {
  342. auto* margin_value = (i == 0 ? &computed.margin_top : &computed.margin_bottom);
  343. if (margin_value->type == Style::Margin::Auto)
  344. {
  345. margins_auto[i] = true;
  346. num_auto_margins++;
  347. }
  348. else
  349. {
  350. margins_auto[i] = false;
  351. box.SetEdge(Box::MARGIN, i == 0 ? Box::TOP : Box::BOTTOM, ResolveValue(*margin_value, containing_block_height));
  352. }
  353. }
  354. // If the height is set to auto, we need to calculate the height
  355. if (height_auto)
  356. {
  357. // We resolve any auto margins to 0
  358. if (margins_auto[0])
  359. box.SetEdge(Box::MARGIN, Box::TOP, 0);
  360. if (margins_auto[1])
  361. box.SetEdge(Box::MARGIN, Box::BOTTOM, 0);
  362. // If the height is set to auto for a box in normal flow, the height is set to -1.
  363. content_area.y = -1;
  364. // But if we are dealing with an absolutely positioned element we need to
  365. // consider if the top and bottom properties are set, since the height can be affected.
  366. if (computed.position == Style::Position::Absolute || computed.position == Style::Position::Fixed)
  367. {
  368. float top = 0.0f, bottom = 0.0f;
  369. if (computed.top.type != Style::Top::Auto && computed.bottom.type != Style::Bottom::Auto)
  370. {
  371. top = ResolveValue(computed.top, containing_block_height );
  372. bottom = ResolveValue(computed.bottom, containing_block_height );
  373. // The height gets resolved to whatever is left of the containing block
  374. content_area.y = containing_block_height - (top +
  375. box.GetCumulativeEdge(Box::CONTENT, Box::TOP) +
  376. box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM) +
  377. bottom);
  378. content_area.y = Math::Max(0.0f, content_area.y);
  379. }
  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. float margin;
  386. if (content_area.y >= 0)
  387. {
  388. margin = (containing_block_height - (box.GetCumulativeEdge(Box::CONTENT, Box::TOP) +
  389. box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM) +
  390. content_area.y)) / num_auto_margins;
  391. }
  392. else
  393. margin = 0;
  394. if (margins_auto[0])
  395. box.SetEdge(Box::MARGIN, Box::TOP, margin);
  396. if (margins_auto[1])
  397. box.SetEdge(Box::MARGIN, Box::BOTTOM, margin);
  398. }
  399. if (content_area.y >= 0)
  400. {
  401. // Clamp the calculated height; if the height is changed by the clamp, then the margins need to be recalculated if
  402. // they were set to auto.
  403. float clamped_height = ClampHeight(content_area.y, computed, containing_block_height);
  404. if (clamped_height != content_area.y)
  405. {
  406. content_area.y = clamped_height;
  407. box.SetContent(content_area);
  408. if (num_auto_margins > 0)
  409. {
  410. // Reset the automatic margins.
  411. if (margins_auto[0])
  412. box.SetEdge(Box::MARGIN, Box::TOP, 0);
  413. if (margins_auto[1])
  414. box.SetEdge(Box::MARGIN, Box::BOTTOM, 0);
  415. BuildBoxHeight(box, computed, containing_block_height);
  416. }
  417. return;
  418. }
  419. }
  420. box.SetContent(content_area);
  421. }
  422. } // namespace Rml