LayoutBlockBox.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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 "LayoutBlockBox.h"
  29. #include "LayoutBlockBoxSpace.h"
  30. #include "LayoutEngine.h"
  31. #include "LayoutDetails.h"
  32. #include "../../Include/RmlUi/Core/Element.h"
  33. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  34. #include "../../Include/RmlUi/Core/ElementScroll.h"
  35. #include "../../Include/RmlUi/Core/Property.h"
  36. #include "../../Include/RmlUi/Core/Profiling.h"
  37. #include <float.h>
  38. namespace Rml {
  39. // Creates a new block box for rendering a block element.
  40. LayoutBlockBox::LayoutBlockBox(LayoutBlockBox* _parent, Element* _element, const Box& _box, float _min_height, float _max_height)
  41. : position(0), box(_box), min_height(_min_height), max_height(_max_height)
  42. {
  43. RMLUI_ZoneScoped;
  44. space_owner = MakeUnique<LayoutBlockBoxSpace>(this);
  45. space = space_owner.get();
  46. parent = _parent;
  47. context = BLOCK;
  48. element = _element;
  49. interrupted_chain = nullptr;
  50. box_cursor = 0;
  51. vertical_overflow = false;
  52. // Get our offset root from our parent, if it has one; otherwise, our element is the offset parent.
  53. if (parent && parent->offset_root->GetElement())
  54. offset_root = parent->offset_root;
  55. else
  56. offset_root = this;
  57. // Determine the offset parent for this element.
  58. const LayoutBlockBox* self_offset_parent;
  59. if (parent && parent->offset_parent->GetElement())
  60. self_offset_parent = parent->offset_parent;
  61. else
  62. self_offset_parent = this;
  63. // Determine the offset parent for our children.
  64. if (parent &&
  65. parent->offset_parent->GetElement() &&
  66. (!element || element->GetPosition() == Style::Position::Static))
  67. offset_parent = parent->offset_parent;
  68. else
  69. offset_parent = this;
  70. // Build the box for our element, and position it if we can.
  71. if (parent)
  72. {
  73. space->ImportSpace(*parent->space);
  74. // Position ourselves within our containing block (if we have a valid offset parent).
  75. if (parent->GetElement())
  76. {
  77. if (self_offset_parent != this)
  78. {
  79. // Get the next position within our offset parent's containing block.
  80. parent->PositionBlockBox(position, box, element ? element->GetComputedValues().clear : Style::Clear::None);
  81. element->SetOffset(position - (self_offset_parent->GetPosition() - offset_root->GetPosition()), self_offset_parent->GetElement());
  82. }
  83. else
  84. element->SetOffset(position, nullptr);
  85. }
  86. }
  87. if (element)
  88. {
  89. const auto& computed = element->GetComputedValues();
  90. wrap_content = computed.white_space != Style::WhiteSpace::Nowrap;
  91. // Determine if this element should have scrollbars or not, and create them if so.
  92. overflow_x_property = computed.overflow_x;
  93. overflow_y_property = computed.overflow_y;
  94. if (overflow_x_property == Style::Overflow::Scroll)
  95. element->GetElementScroll()->EnableScrollbar(ElementScroll::HORIZONTAL, box.GetSize(Box::PADDING).x);
  96. else
  97. element->GetElementScroll()->DisableScrollbar(ElementScroll::HORIZONTAL);
  98. if (overflow_y_property == Style::Overflow::Scroll)
  99. element->GetElementScroll()->EnableScrollbar(ElementScroll::VERTICAL, box.GetSize(Box::PADDING).x);
  100. else
  101. element->GetElementScroll()->DisableScrollbar(ElementScroll::VERTICAL);
  102. }
  103. else
  104. {
  105. wrap_content = true;
  106. overflow_x_property = Style::Overflow::Visible;
  107. overflow_y_property = Style::Overflow::Visible;
  108. }
  109. }
  110. // Creates a new block box in an inline context.
  111. LayoutBlockBox::LayoutBlockBox(LayoutBlockBox* _parent) : position(-1, -1)
  112. {
  113. RMLUI_ASSERT(_parent);
  114. parent = _parent;
  115. offset_parent = parent->offset_parent;
  116. offset_root = parent->offset_root;
  117. space = _parent->space;
  118. context = INLINE;
  119. line_boxes.push_back(MakeUnique<LayoutLineBox>(this));
  120. wrap_content = parent->wrap_content;
  121. element = nullptr;
  122. interrupted_chain = nullptr;
  123. box_cursor = 0;
  124. vertical_overflow = false;
  125. const Vector2f containing_block = LayoutDetails::GetContainingBlock(parent);
  126. box.SetContent(Vector2f(containing_block.x, -1));
  127. parent->PositionBlockBox(position, box, Style::Clear::None);
  128. // Reset the min and max heights; they're not valid for inline block boxes.
  129. min_height = 0;
  130. max_height = FLT_MAX;
  131. }
  132. // Releases the block box.
  133. LayoutBlockBox::~LayoutBlockBox()
  134. {
  135. }
  136. // Closes the box.
  137. LayoutBlockBox::CloseResult LayoutBlockBox::Close()
  138. {
  139. // If the last child of this block box is an inline box, then we haven't closed it; close it now!
  140. if (context == BLOCK)
  141. {
  142. CloseResult result = CloseInlineBlockBox();
  143. if (result != OK)
  144. return LAYOUT_SELF;
  145. }
  146. // Otherwise, we're an inline context box; so close our last line, which will still be open.
  147. else
  148. {
  149. line_boxes.back()->Close();
  150. // Expand our content area if any line boxes had to push themselves out.
  151. Vector2f content_area = box.GetSize();
  152. for (size_t i = 0; i < line_boxes.size(); i++)
  153. content_area.x = Math::Max(content_area.x, line_boxes[i]->GetDimensions().x);
  154. box.SetContent(content_area);
  155. }
  156. // Set this box's height, if necessary.
  157. if (box.GetSize(Box::CONTENT).y < 0)
  158. {
  159. Vector2f content_area = box.GetSize();
  160. content_area.y = Math::Clamp(box_cursor, min_height, max_height);
  161. if (element != nullptr)
  162. content_area.y = Math::Max(content_area.y, space->GetDimensions().y);
  163. box.SetContent(content_area);
  164. }
  165. visible_overflow_size = Vector2f(0);
  166. RMLUI_ASSERTMSG(!(context == INLINE && element), "The following assumes inline contexts do not represent a particular element.");
  167. // Set the computed box on the element.
  168. if (context == BLOCK && element)
  169. {
  170. // Calculate the dimensions of the box's *internal* content; this is the tightest-fitting box around all of the
  171. // internal elements, plus this element's padding.
  172. // Start with the inner content size, as set by the child blocks boxes or external formatting contexts.
  173. Vector2f content_box = inner_content_size;
  174. // Check how big our floated area is.
  175. const Vector2f space_box = space->GetDimensions();
  176. content_box.x = Math::Max(content_box.x, space_box.x);
  177. // If our content is larger than our window, we can enable the horizontal scrollbar if
  178. // we're set to auto-scrollbars. If we're set to always use scrollbars, then the horiontal
  179. // scrollbar will already have been enabled in the constructor.
  180. if (content_box.x > box.GetSize().x + 0.5f)
  181. {
  182. if (overflow_x_property == Style::Overflow::Auto)
  183. {
  184. element->GetElementScroll()->EnableScrollbar(ElementScroll::HORIZONTAL, box.GetSize(Box::PADDING).x);
  185. if (!CatchVerticalOverflow())
  186. return LAYOUT_SELF;
  187. }
  188. }
  189. content_box.y = Math::Max(content_box.y, box_cursor);
  190. content_box.y = Math::Max(content_box.y, space_box.y);
  191. if (!CatchVerticalOverflow(content_box.y))
  192. return LAYOUT_SELF;
  193. const Vector2f padding_edges = Vector2f(
  194. box.GetEdge(Box::PADDING, Box::LEFT) + box.GetEdge(Box::PADDING, Box::RIGHT),
  195. box.GetEdge(Box::PADDING, Box::TOP) + box.GetEdge(Box::PADDING, Box::BOTTOM)
  196. );
  197. element->SetBox(box);
  198. element->SetContentBox(space->GetOffset(), content_box + padding_edges);
  199. const Vector2f margin_size = box.GetSize(Box::MARGIN);
  200. // Set the visible overflow size so that ancestors can catch any overflow produced by us. That is, hiding it or providing a scrolling mechanism.
  201. // If we catch our own overflow here, then just use the normal margin box as that will effectively remove the overflow from our ancestor's perspective.
  202. if (overflow_x_property != Style::Overflow::Visible)
  203. visible_overflow_size.x = margin_size.x;
  204. else
  205. visible_overflow_size.x = Math::Max(margin_size.x, content_box.x + box.GetEdge(Box::MARGIN, Box::LEFT) + box.GetEdge(Box::BORDER, Box::LEFT) + box.GetEdge(Box::PADDING, Box::LEFT));
  206. if (overflow_y_property != Style::Overflow::Visible)
  207. visible_overflow_size.y = margin_size.y;
  208. else
  209. visible_overflow_size.y = Math::Max(margin_size.y, content_box.y + box.GetEdge(Box::MARGIN, Box::TOP) + box.GetEdge(Box::BORDER, Box::TOP) + box.GetEdge(Box::PADDING, Box::TOP));
  210. // Format any scrollbars which were enabled on this element.
  211. element->GetElementScroll()->FormatScrollbars();
  212. }
  213. else if (context == INLINE)
  214. {
  215. // Find the largest line in this layout block
  216. for (size_t i = 0; i < line_boxes.size(); i++)
  217. {
  218. LayoutLineBox* line_box = line_boxes[i].get();
  219. visible_overflow_size.x = Math::Max(visible_overflow_size.x, line_box->GetBoxCursor());
  220. }
  221. }
  222. // Increment the parent's cursor.
  223. if (parent != nullptr)
  224. {
  225. // If this close fails, it means this block box has caused our parent block box to generate an automatic vertical scrollbar.
  226. if (!parent->CloseBlockBox(this))
  227. return LAYOUT_PARENT;
  228. }
  229. if (context == BLOCK && element)
  230. {
  231. // If we represent a positioned element, then we can now (as we've been sized) act as the containing block for all
  232. // the absolutely-positioned elements of our descendants.
  233. if (element->GetPosition() != Style::Position::Static)
  234. {
  235. CloseAbsoluteElements();
  236. }
  237. // Set the baseline for inline-block elements to the baseline of the last line of the element.
  238. // This is a special rule for inline-blocks (see CSS 2.1 Sec. 10.8.1).
  239. if (element->GetDisplay() == Style::Display::InlineBlock)
  240. {
  241. bool found_baseline = false;
  242. float baseline = 0;
  243. for (int i = (int)block_boxes.size() - 1; i >= 0; i--)
  244. {
  245. if (block_boxes[i]->context == INLINE)
  246. {
  247. const LineBoxList& line_boxes = block_boxes[i]->line_boxes;
  248. for (int j = (int)line_boxes.size() - 1; j >= 0; j--)
  249. {
  250. found_baseline = line_boxes[j]->GetBaselineOfLastLine(baseline);
  251. if (found_baseline)
  252. break;
  253. }
  254. if (found_baseline)
  255. break;
  256. }
  257. }
  258. if (found_baseline)
  259. {
  260. if (baseline < 0 && (overflow_x_property != Style::Overflow::Visible || overflow_x_property != Style::Overflow::Visible))
  261. {
  262. baseline = 0;
  263. }
  264. element->SetBaseline(baseline);
  265. }
  266. }
  267. }
  268. return OK;
  269. }
  270. // Called by a closing block box child.
  271. bool LayoutBlockBox::CloseBlockBox(LayoutBlockBox* child)
  272. {
  273. RMLUI_ASSERT(context == BLOCK);
  274. const float child_position_y = child->GetPosition().y - child->box.GetEdge(Box::MARGIN, Box::TOP) - (box.GetPosition().y + position.y);
  275. box_cursor = child_position_y + child->GetBox().GetSize(Box::MARGIN).y;
  276. // Extend the inner content size. The vertical size can be larger than the box_cursor due to overflow.
  277. inner_content_size.x = Math::Max(inner_content_size.x, child->visible_overflow_size.x);
  278. inner_content_size.y = Math::Max(inner_content_size.y, child_position_y + child->visible_overflow_size.y);
  279. return CatchVerticalOverflow();
  280. }
  281. // Called by a closing line box child.
  282. LayoutInlineBox* LayoutBlockBox::CloseLineBox(LayoutLineBox* child, UniquePtr<LayoutInlineBox> overflow, LayoutInlineBox* overflow_chain)
  283. {
  284. RMLUI_ZoneScoped;
  285. RMLUI_ASSERT(context == INLINE);
  286. if (child->GetDimensions().x > 0)
  287. box_cursor = (child->GetPosition().y - (box.GetPosition().y + position.y)) + child->GetDimensions().y;
  288. // If we have any pending floating elements for our parent, then this would be an ideal time to position them.
  289. if (!float_elements.empty())
  290. {
  291. for (size_t i = 0; i < float_elements.size(); ++i)
  292. parent->PositionFloat(float_elements[i], box_cursor);
  293. float_elements.clear();
  294. }
  295. // Add a new line box.
  296. line_boxes.push_back(MakeUnique<LayoutLineBox>(this));
  297. if (overflow_chain)
  298. line_boxes.back()->AddChainedBox(overflow_chain);
  299. if (overflow)
  300. return line_boxes.back()->AddBox(std::move(overflow));
  301. return nullptr;
  302. }
  303. // Adds a new block element to this block box.
  304. LayoutBlockBox* LayoutBlockBox::AddBlockElement(Element* element, const Box& box, float min_height, float max_height)
  305. {
  306. RMLUI_ZoneScoped;
  307. RMLUI_ASSERT(context == BLOCK);
  308. // Check if our most previous block box is rendering in an inline context.
  309. if (!block_boxes.empty() &&
  310. block_boxes.back()->context == INLINE)
  311. {
  312. LayoutBlockBox* inline_block_box = block_boxes.back().get();
  313. LayoutInlineBox* open_inline_box = inline_block_box->line_boxes.back()->GetOpenInlineBox();
  314. if (open_inline_box != nullptr)
  315. {
  316. // There's an open inline box chain, which means this block element is parented to it. The chain needs to
  317. // be positioned (if it hasn't already), closed and duplicated after this block box closes. Also, this
  318. // block needs to be aware of its parentage, so it can correctly compute its relative position. First of
  319. // all, we need to close the inline box; this will position the last line if necessary, but it will also
  320. // create a new line in the inline block box; we want this line to be in an inline box after our block
  321. // element.
  322. if (inline_block_box->Close() != OK)
  323. return nullptr;
  324. interrupted_chain = open_inline_box;
  325. }
  326. else
  327. {
  328. // There are no open inline boxes, so this inline box just needs to be closed.
  329. if (CloseInlineBlockBox() != OK)
  330. return nullptr;
  331. }
  332. }
  333. block_boxes.push_back(MakeUnique<LayoutBlockBox>(this, element, box, min_height, max_height));
  334. return block_boxes.back().get();
  335. }
  336. // Adds a new inline element to this inline box.
  337. LayoutInlineBox* LayoutBlockBox::AddInlineElement(Element* element, const Box& box)
  338. {
  339. RMLUI_ZoneScoped;
  340. if (context == BLOCK)
  341. {
  342. LayoutInlineBox* inline_box;
  343. // If we have an open child rendering in an inline context, we can add this element into it.
  344. if (!block_boxes.empty() &&
  345. block_boxes.back()->context == INLINE)
  346. inline_box = block_boxes.back()->AddInlineElement(element, box);
  347. // No dice! Ah well, nothing for it but to open a new inline context block box.
  348. else
  349. {
  350. block_boxes.push_back(MakeUnique<LayoutBlockBox>(this));
  351. if (interrupted_chain != nullptr)
  352. {
  353. block_boxes.back()->line_boxes.back()->AddChainedBox(interrupted_chain);
  354. interrupted_chain = nullptr;
  355. }
  356. inline_box = block_boxes.back()->AddInlineElement(element, box);
  357. }
  358. return inline_box;
  359. }
  360. else
  361. {
  362. // We're an inline context box, so we'll add this new inline element into our line boxes.
  363. return line_boxes.back()->AddElement(element, box);
  364. }
  365. }
  366. // Adds a line-break to this block box.
  367. void LayoutBlockBox::AddBreak()
  368. {
  369. float line_height = element->GetLineHeight();
  370. // Check for an inline box as our last child; if so, we can simply end its line and bail.
  371. if (!block_boxes.empty())
  372. {
  373. LayoutBlockBox* block_box = block_boxes.back().get();
  374. if (block_box->context == INLINE)
  375. {
  376. LayoutLineBox* last_line = block_box->line_boxes.back().get();
  377. if (last_line->GetDimensions().y < 0)
  378. block_box->box_cursor += line_height;
  379. else
  380. last_line->Close();
  381. return;
  382. }
  383. }
  384. // No inline box as our last child; no problem, just increment the cursor by the line height of this element.
  385. box_cursor += line_height;
  386. }
  387. // Adds an element to this block box to be handled as a floating element.
  388. bool LayoutBlockBox::AddFloatElement(Element* element)
  389. {
  390. // If we have an open inline block box, then we have to position the box a little differently.
  391. if (!block_boxes.empty() &&
  392. block_boxes.back()->context == INLINE)
  393. block_boxes.back()->float_elements.push_back(element);
  394. // Nope ... just place it!
  395. else
  396. PositionFloat(element);
  397. return true;
  398. }
  399. // Adds an element to this block box to be handled as an absolutely-positioned element.
  400. void LayoutBlockBox::AddAbsoluteElement(Element* element)
  401. {
  402. RMLUI_ASSERT(context == BLOCK);
  403. AbsoluteElement absolute_element;
  404. absolute_element.element = element;
  405. PositionBox(absolute_element.position, 0);
  406. // If we have an open inline-context block box as our last child, then the absolute element must appear after it,
  407. // but not actually close the box.
  408. if (!block_boxes.empty()
  409. && block_boxes.back()->context == INLINE)
  410. {
  411. LayoutBlockBox* inline_context_box = block_boxes.back().get();
  412. float last_line_height = inline_context_box->line_boxes.back()->GetDimensions().y;
  413. absolute_element.position.y += (inline_context_box->box_cursor + Math::Max(0.0f, last_line_height));
  414. }
  415. // Find the positioned parent for this element.
  416. LayoutBlockBox* absolute_parent = this;
  417. while (absolute_parent != absolute_parent->offset_parent)
  418. absolute_parent = absolute_parent->parent;
  419. absolute_parent->absolute_elements.push_back(absolute_element);
  420. }
  421. // Lays out, sizes, and positions all absolute elements in this block relative to the containing block.
  422. void LayoutBlockBox::CloseAbsoluteElements()
  423. {
  424. if (!absolute_elements.empty())
  425. {
  426. // The size of the containing box, including the padding. This is used to resolve relative offsets.
  427. Vector2f containing_block = GetBox().GetSize(Box::PADDING);
  428. for (size_t i = 0; i < absolute_elements.size(); i++)
  429. {
  430. Element* absolute_element = absolute_elements[i].element;
  431. Vector2f absolute_position = absolute_elements[i].position;
  432. absolute_position -= position - offset_root->GetPosition();
  433. // Lay out the element.
  434. LayoutEngine::FormatElement(absolute_element, containing_block);
  435. // Now that the element's box has been built, we can offset the position we determined was appropriate for
  436. // it by the element's margin. This is necessary because the coordinate system for the box begins at the
  437. // border, not the margin.
  438. absolute_position.x += absolute_element->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  439. absolute_position.y += absolute_element->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  440. // Set the offset of the element; the element itself will take care of any RCSS-defined positional offsets.
  441. absolute_element->SetOffset(absolute_position, element);
  442. }
  443. absolute_elements.clear();
  444. }
  445. }
  446. // Returns the offset from the top-left corner of this box that the next child box will be positioned at.
  447. void LayoutBlockBox::PositionBox(Vector2f& box_position, float top_margin, Style::Clear clear_property) const
  448. {
  449. // If our element is establishing a new offset hierarchy, then any children of ours don't inherit our offset.
  450. box_position = GetPosition();
  451. box_position += box.GetPosition();
  452. box_position.y += box_cursor;
  453. float clear_margin = space->ClearBoxes(box_position.y + top_margin, clear_property) - (box_position.y + top_margin);
  454. if (clear_margin > 0)
  455. box_position.y += clear_margin;
  456. else
  457. {
  458. // Check for a collapsing vertical margin.
  459. if (!block_boxes.empty() &&
  460. block_boxes.back()->context == BLOCK)
  461. {
  462. float bottom_margin = block_boxes.back()->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM);
  463. box_position.y -= Math::Min(top_margin, bottom_margin);
  464. }
  465. }
  466. }
  467. // Returns the offset from the top-left corner of this box's offset element the next child block box, of the given
  468. // dimensions, will be positioned at. This will include the margins on the new block box.
  469. void LayoutBlockBox::PositionBlockBox(Vector2f& box_position, const Box& box, Style::Clear clear_property) const
  470. {
  471. PositionBox(box_position, box.GetEdge(Box::MARGIN, Box::TOP), clear_property);
  472. box_position.x += box.GetEdge(Box::MARGIN, Box::LEFT);
  473. box_position.y += box.GetEdge(Box::MARGIN, Box::TOP);
  474. }
  475. // Returns the offset from the top-left corner of this box for the next line.
  476. void LayoutBlockBox::PositionLineBox(Vector2f& box_position, float& box_width, bool& _wrap_content, const Vector2f& dimensions) const
  477. {
  478. Vector2f cursor;
  479. PositionBox(cursor);
  480. space->PositionBox(box_position, box_width, cursor.y, dimensions);
  481. // Also, probably shouldn't check for widths when positioning the box?
  482. _wrap_content = wrap_content;
  483. }
  484. // Calculate the dimensions of the box's internal content width; i.e. the size of the largest line.
  485. float LayoutBlockBox::GetShrinkToFitWidth() const
  486. {
  487. float content_width = 0.0f;
  488. if (context == BLOCK)
  489. {
  490. auto get_content_width_from_children = [this, &content_width]() {
  491. for (size_t i = 0; i < block_boxes.size(); i++)
  492. {
  493. const Box& box = block_boxes[i]->GetBox();
  494. const float edge_size = box.GetCumulativeEdge(Box::PADDING, Box::LEFT) + box.GetCumulativeEdge(Box::PADDING, Box::RIGHT);
  495. content_width = Math::Max(content_width, block_boxes[i]->GetShrinkToFitWidth() + edge_size);
  496. }
  497. };
  498. // Block boxes with definite sizes should use that size. Otherwise, find the maximum content width of our children.
  499. // Alternative solution: Add some 'intrinsic_width' property to every 'LayoutBlockBox' and have that propagate up.
  500. if (element)
  501. {
  502. auto& computed = element->GetComputedValues();
  503. const float block_width = box.GetSize(Box::CONTENT).x;
  504. if(computed.width.type == Style::Width::Auto)
  505. {
  506. get_content_width_from_children();
  507. }
  508. else
  509. {
  510. float width_value = ResolveValue(computed.width, block_width);
  511. content_width = Math::Max(content_width, width_value);
  512. }
  513. float min_width, max_width;
  514. LayoutDetails::GetMinMaxWidth(min_width, max_width, computed, box, block_width);
  515. content_width = Math::Clamp(content_width, min_width, max_width);
  516. }
  517. else
  518. {
  519. get_content_width_from_children();
  520. }
  521. // Can add the dimensions of floating elements here if we want to support that.
  522. }
  523. else
  524. {
  525. // Find the largest line in this layout block
  526. for (size_t i = 0; i < line_boxes.size(); i++)
  527. {
  528. // Perhaps a more robust solution is to modify how we set the line box dimension on 'line_box->close()'
  529. // and use that, or add another value in the line_box ... but seems to work for now.
  530. LayoutLineBox* line_box = line_boxes[i].get();
  531. content_width = Math::Max(content_width, line_box->GetBoxCursor());
  532. }
  533. content_width = Math::Min(content_width, box.GetSize(Box::CONTENT).x);
  534. }
  535. return content_width;
  536. }
  537. Vector2f LayoutBlockBox::GetVisibleOverflowSize() const
  538. {
  539. return visible_overflow_size;
  540. }
  541. void LayoutBlockBox::ExtendInnerContentSize(Vector2f _inner_content_size)
  542. {
  543. inner_content_size.x = Math::Max(inner_content_size.x, _inner_content_size.x);
  544. inner_content_size.y = Math::Max(inner_content_size.y, _inner_content_size.y);
  545. }
  546. // Returns the block box's element.
  547. Element* LayoutBlockBox::GetElement() const
  548. {
  549. return element;
  550. }
  551. // Returns the block box's parent.
  552. LayoutBlockBox* LayoutBlockBox::GetParent() const
  553. {
  554. return parent;
  555. }
  556. // Returns the position of the block box, relative to its parent's content area.
  557. const Vector2f& LayoutBlockBox::GetPosition() const
  558. {
  559. return position;
  560. }
  561. // Returns the element against which all positions of boxes in the hierarchy are calculated relative to.
  562. const LayoutBlockBox* LayoutBlockBox::GetOffsetParent() const
  563. {
  564. return offset_parent;
  565. }
  566. // Returns the block box against which all positions of boxes in the hierarchy are calculated relative to.
  567. const LayoutBlockBox* LayoutBlockBox::GetOffsetRoot() const
  568. {
  569. return offset_root;
  570. }
  571. // Returns the block box's dimension box.
  572. Box& LayoutBlockBox::GetBox()
  573. {
  574. return box;
  575. }
  576. // Returns the block box's dimension box.
  577. const Box& LayoutBlockBox::GetBox() const
  578. {
  579. return box;
  580. }
  581. void* LayoutBlockBox::operator new(size_t size)
  582. {
  583. void* memory = LayoutEngine::AllocateLayoutChunk(size);
  584. return memory;
  585. }
  586. void LayoutBlockBox::operator delete(void* chunk)
  587. {
  588. LayoutEngine::DeallocateLayoutChunk(chunk);
  589. }
  590. // Closes our last block box, if it is an open inline block box.
  591. LayoutBlockBox::CloseResult LayoutBlockBox::CloseInlineBlockBox()
  592. {
  593. if (!block_boxes.empty() &&
  594. block_boxes.back()->context == INLINE)
  595. return block_boxes.back()->Close();
  596. return OK;
  597. }
  598. // Positions a floating element within this block box.
  599. void LayoutBlockBox::PositionFloat(Element* element, float offset)
  600. {
  601. Vector2f box_position;
  602. PositionBox(box_position);
  603. space->PositionBox(box_position.y + offset, element);
  604. }
  605. // Checks if we have a new vertical overflow on an auto-scrolling element.
  606. bool LayoutBlockBox::CatchVerticalOverflow(float cursor)
  607. {
  608. if (cursor == -1)
  609. cursor = Math::Max(box_cursor, inner_content_size.y);
  610. float box_height = box.GetSize().y;
  611. if (box_height < 0)
  612. box_height = max_height;
  613. // If we're auto-scrolling and our height is fixed, we have to check if this box has exceeded our client height.
  614. if (!vertical_overflow &&
  615. box_height >= 0 &&
  616. overflow_y_property == Style::Overflow::Auto)
  617. {
  618. if (cursor > box_height - element->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL) + 0.5f)
  619. {
  620. RMLUI_ZoneScopedC(0xDD3322);
  621. vertical_overflow = true;
  622. element->GetElementScroll()->EnableScrollbar(ElementScroll::VERTICAL, box.GetSize(Box::PADDING).x);
  623. block_boxes.clear();
  624. space_owner = MakeUnique<LayoutBlockBoxSpace>(this);
  625. space = space_owner.get();
  626. box_cursor = 0;
  627. interrupted_chain = nullptr;
  628. return false;
  629. }
  630. }
  631. return true;
  632. }
  633. } // namespace Rml