LayoutBlockBox.cpp 26 KB

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