BlockContainer.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #include "BlockContainer.h"
  2. #include "../../../Include/RmlUi/Core/ComputedValues.h"
  3. #include "../../../Include/RmlUi/Core/Element.h"
  4. #include "../../../Include/RmlUi/Core/ElementScroll.h"
  5. #include "../../../Include/RmlUi/Core/Profiling.h"
  6. #include "FloatedBoxSpace.h"
  7. #include "InlineContainer.h"
  8. #include "LayoutDetails.h"
  9. #include "LineBox.h"
  10. namespace Rml {
  11. BlockContainer::BlockContainer(ContainerBox* _parent_container, FloatedBoxSpace* _space, Element* _element, const Box& _box, float _min_height,
  12. float _max_height) :
  13. ContainerBox(Type::BlockContainer, _element, _parent_container), box(_box), min_height(_min_height), max_height(_max_height), space(_space)
  14. {
  15. RMLUI_ASSERT(element);
  16. RMLUI_ASSERT(box.GetSize().x >= 0.f);
  17. if (!space)
  18. {
  19. // We are the root of the formatting context, establish a new space for floated boxes.
  20. root_space = MakeUnique<FloatedBoxSpace>();
  21. space = root_space.get();
  22. }
  23. }
  24. BlockContainer::~BlockContainer() {}
  25. bool BlockContainer::Close(BlockContainer* parent_block_container)
  26. {
  27. // If the last child of this block box is an inline box, then we haven't closed it; close it now!
  28. if (!CloseOpenInlineContainer())
  29. return false;
  30. // Set this box's height, if necessary.
  31. if (box.GetSize().y < 0)
  32. {
  33. float content_height = box_cursor;
  34. if (!parent_block_container)
  35. content_height = Math::Max(content_height, space->GetDimensions(FloatedBoxEdge::Margin).y - (position.y + box.GetPosition().y));
  36. content_height = Math::Clamp(content_height, min_height, max_height);
  37. box.SetContent({box.GetSize().x, content_height});
  38. }
  39. // Find the size of our content.
  40. const Vector2f space_box = space->GetDimensions(FloatedBoxEdge::Overflow) - (position + box.GetPosition());
  41. Vector2f content_box = Math::Max(inner_content_size, space_box);
  42. content_box.y = Math::Max(content_box.y, box_cursor);
  43. if (!SubmitBox(content_box, box, max_height))
  44. return false;
  45. // If we are the root of our block formatting context, this will be null. Otherwise increment our parent's cursor to account for this box.
  46. if (parent_block_container)
  47. {
  48. RMLUI_ASSERTMSG(GetParent() == parent_block_container, "Mismatched parent box.");
  49. // If this close fails, it means this block box has caused our parent box to generate an automatic vertical scrollbar.
  50. if (!parent_block_container->EncloseChildBox(this, position, box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Border),
  51. box.GetEdge(BoxArea::Margin, BoxEdge::Bottom)))
  52. return false;
  53. }
  54. // Now that we have been sized, we can proceed with formatting and placing positioned elements that this container
  55. // acts as a containing block for.
  56. ClosePositionedElements();
  57. // Find the element baseline which is the distance from the margin bottom of the element to its baseline.
  58. float element_baseline = 0;
  59. // For inline-blocks with visible overflow, this is the baseline of the last line of the element (see CSS2 10.8.1).
  60. if (element->GetDisplay() == Style::Display::InlineBlock && !IsScrollContainer())
  61. {
  62. float baseline = 0;
  63. bool found_baseline = GetBaselineOfLastLine(baseline);
  64. // The retrieved baseline is the vertical distance from the top of our root space (the coordinate system of our
  65. // local block formatting context), convert it to the element's local coordinates.
  66. if (found_baseline)
  67. {
  68. const float bottom_position =
  69. position.y + box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Border) + box.GetEdge(BoxArea::Margin, BoxEdge::Bottom);
  70. element_baseline = bottom_position - baseline;
  71. }
  72. }
  73. SetElementBaseline(element_baseline);
  74. EnsureEmptyInterruptedLineBox();
  75. SubmitElementLayout();
  76. return true;
  77. }
  78. bool BlockContainer::EncloseChildBox(LayoutBox* child, Vector2f child_position, float child_height, float child_margin_bottom)
  79. {
  80. child_position -= (box.GetPosition() + position);
  81. box_cursor = child_position.y + child_height + child_margin_bottom;
  82. // Extend the inner content size. The vertical size can be larger than the box_cursor due to overflow.
  83. inner_content_size = Math::Max(inner_content_size, child_position + child->GetVisibleOverflowSize());
  84. const Vector2f content_size = Math::Max(Vector2f{box.GetSize().x, box_cursor}, inner_content_size);
  85. const bool result = CatchOverflow(content_size, box, max_height);
  86. return result;
  87. }
  88. BlockContainer* BlockContainer::OpenBlockBox(Element* child_element, const Box& child_box, float min_height, float max_height)
  89. {
  90. if (!CloseOpenInlineContainer())
  91. return nullptr;
  92. auto child_container_ptr = MakeUnique<BlockContainer>(this, space, child_element, child_box, min_height, max_height);
  93. BlockContainer* child_container = child_container_ptr.get();
  94. child_container->position = NextBoxPosition(child_box, child_element->GetComputedValues().clear());
  95. child_element->SetOffset(child_container->position - position, element);
  96. child_container->ResetScrollbars(child_box);
  97. // Store relatively positioned elements with their containing block so that their offset can be updated after
  98. // their containing block has been sized.
  99. if (child_element->GetPosition() == Style::Position::Relative)
  100. AddRelativeElement(child_element);
  101. child_boxes.push_back(std::move(child_container_ptr));
  102. return child_container;
  103. }
  104. LayoutBox* BlockContainer::AddBlockLevelBox(UniquePtr<LayoutBox> block_level_box_ptr, Element* child_element, const Box& child_box)
  105. {
  106. RMLUI_ASSERT(child_box.GetSize().y >= 0.f); // Assumes child element already formatted and sized.
  107. if (!CloseOpenInlineContainer())
  108. return nullptr;
  109. // Clear any floats to avoid overlapping them. In CSS, it is allowed to instead shrink the box and place it next to
  110. // any floats, but we keep it simple here for now and just clear them.
  111. Vector2f child_position = NextBoxPosition(child_box, Style::Clear::Both);
  112. child_element->SetOffset(child_position - position, element);
  113. if (child_element->GetPosition() == Style::Position::Relative)
  114. AddRelativeElement(child_element);
  115. LayoutBox* block_level_box = block_level_box_ptr.get();
  116. child_boxes.push_back(std::move(block_level_box_ptr));
  117. if (!EncloseChildBox(block_level_box, child_position, child_box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Border),
  118. child_box.GetEdge(BoxArea::Margin, BoxEdge::Bottom)))
  119. return nullptr;
  120. return block_level_box;
  121. }
  122. InlineBoxHandle BlockContainer::AddInlineElement(Element* element, const Box& child_box)
  123. {
  124. RMLUI_ZoneScoped;
  125. // Inline-level elements need to be added to an inline container, open one if needed.
  126. InlineContainer* inline_container = EnsureOpenInlineContainer();
  127. InlineBox* inline_box = inline_container->AddInlineElement(element, child_box);
  128. if (element->GetPosition() == Style::Position::Relative)
  129. AddRelativeElement(element);
  130. return {inline_box};
  131. }
  132. void BlockContainer::CloseInlineElement(InlineBoxHandle handle)
  133. {
  134. // If the inline-level element did not generate an inline box, then there is no need to close anything.
  135. if (!handle.inline_box)
  136. return;
  137. // Usually the inline container the box was placed in is still the open box, and we can just close the inline
  138. // element in it. However, it is possible that an intermediary block-level element was placed, thereby splitting the
  139. // inline element into multiple inline containers around the block-level box. If we don't have an open inline
  140. // container at all, open a new one, even if the sole purpose of the new line is to close this inline element.
  141. EnsureOpenInlineContainer()->CloseInlineElement(handle.inline_box);
  142. }
  143. void BlockContainer::AddBreak()
  144. {
  145. const float line_height = element->GetLineHeight();
  146. // Check for an inline box as our last child; if so, we can simply end its line and bail.
  147. if (InlineContainer* inline_container = GetOpenInlineContainer())
  148. {
  149. inline_container->AddBreak(line_height);
  150. return;
  151. }
  152. // No inline box as our last child; no problem, just increment the cursor by the line height of this element.
  153. box_cursor += line_height;
  154. }
  155. void BlockContainer::AddFloatElement(Element* element, Vector2f visible_overflow_size)
  156. {
  157. if (InlineContainer* inline_container = GetOpenInlineContainer())
  158. {
  159. // Try to add the float to our inline container, placing it next to any open line if possible. Otherwise, queue it for later.
  160. bool float_placed = false;
  161. float line_position_top = 0.f;
  162. Vector2f line_size;
  163. if (queued_float_elements.empty() && inline_container->GetOpenLineBoxDimensions(line_position_top, line_size))
  164. {
  165. const Vector2f margin_size = element->GetBox().GetSize(BoxArea::Margin);
  166. const Style::Float float_property = element->GetComputedValues().float_();
  167. const Style::Clear clear_property = element->GetComputedValues().clear();
  168. float available_width = 0.f;
  169. const Vector2f float_position =
  170. space->NextFloatPosition(this, available_width, line_position_top, margin_size, float_property, clear_property);
  171. const float line_position_bottom = line_position_top + line_size.y;
  172. const float line_and_element_width = margin_size.x + line_size.x;
  173. // If the float can be positioned on the open line, and it can fit next to the line's contents, place it now.
  174. if (float_position.y < line_position_bottom && line_and_element_width <= available_width)
  175. {
  176. PlaceFloat(element, line_position_top, visible_overflow_size);
  177. inline_container->UpdateOpenLineBoxPlacement();
  178. float_placed = true;
  179. }
  180. }
  181. if (!float_placed)
  182. queued_float_elements.push_back({element, visible_overflow_size});
  183. }
  184. else
  185. {
  186. // There is no inline container, so just place it!
  187. const Vector2f box_position = NextBoxPosition();
  188. PlaceFloat(element, box_position.y, visible_overflow_size);
  189. }
  190. if (element->GetPosition() == Style::Position::Relative)
  191. AddRelativeElement(element);
  192. }
  193. Vector2f BlockContainer::GetOpenStaticPosition(Style::Display display) const
  194. {
  195. // Estimate the next box as if it had static position (10.6.4). If the element is inline-level, position it on the
  196. // open line if we have one. Otherwise, block-level elements are positioned on a hypothetical next line.
  197. Vector2f static_position = NextBoxPosition();
  198. if (const InlineContainer* inline_container = GetOpenInlineContainer())
  199. {
  200. const bool inline_level_element = (display == Style::Display::Inline || display == Style::Display::InlineBlock);
  201. static_position += inline_container->GetStaticPositionEstimate(inline_level_element);
  202. }
  203. return static_position;
  204. }
  205. Vector2f BlockContainer::NextBoxPosition() const
  206. {
  207. Vector2f box_position = position + box.GetPosition();
  208. box_position.y += box_cursor;
  209. return box_position;
  210. }
  211. Vector2f BlockContainer::NextBoxPosition(const Box& child_box, Style::Clear clear_property) const
  212. {
  213. const float child_top_margin = child_box.GetEdge(BoxArea::Margin, BoxEdge::Top);
  214. Vector2f box_position = NextBoxPosition();
  215. box_position.x += child_box.GetEdge(BoxArea::Margin, BoxEdge::Left);
  216. box_position.y += child_top_margin;
  217. float clear_margin = space->DetermineClearPosition(box_position.y, clear_property) - box_position.y;
  218. if (clear_margin > 0.f)
  219. {
  220. box_position.y += clear_margin;
  221. }
  222. else if (const LayoutBox* block_box = GetOpenLayoutBox())
  223. {
  224. // Check for a collapsing vertical margin with our last child, which will be vertically adjacent to the new box.
  225. if (const Box* open_box = block_box->GetIfBox())
  226. {
  227. const float open_bottom_margin = open_box->GetEdge(BoxArea::Margin, BoxEdge::Bottom);
  228. const float margin_sum = open_bottom_margin + child_top_margin;
  229. // The collapsed margin size depends on the sign of each margin, according to CSS behavior. The margins have
  230. // already been added to the 'box_position', so subtract their sum as needed.
  231. const int num_negative_margins = int(child_top_margin < 0.f) + int(open_bottom_margin < 0.f);
  232. switch (num_negative_margins)
  233. {
  234. case 0:
  235. // Use the largest margin.
  236. box_position.y += Math::Max(child_top_margin, open_bottom_margin) - margin_sum;
  237. break;
  238. case 1:
  239. // Use the sum of the positive and negative margin. These are already added to the position, so do nothing.
  240. break;
  241. case 2:
  242. // Use the most negative margin.
  243. box_position.y += Math::Min(child_top_margin, open_bottom_margin) - margin_sum;
  244. break;
  245. }
  246. }
  247. }
  248. return box_position;
  249. }
  250. void BlockContainer::PlaceQueuedFloats(float vertical_position)
  251. {
  252. if (!queued_float_elements.empty())
  253. {
  254. for (QueuedFloat entry : queued_float_elements)
  255. PlaceFloat(entry.element, vertical_position, entry.visible_overflow_size);
  256. queued_float_elements.clear();
  257. }
  258. }
  259. float BlockContainer::GetShrinkToFitWidth() const
  260. {
  261. auto& computed = element->GetComputedValues();
  262. float content_width = 0.0f;
  263. if (computed.width().type == Style::Width::Length)
  264. {
  265. // We have a definite width, so use that size.
  266. content_width = box.GetSize().x;
  267. }
  268. else
  269. {
  270. // Nope, then use the largest outer shrink-to-fit width of our children. Percentage sizing would be relative to
  271. // our containing block width and is treated just like 'auto' in this context.
  272. for (const auto& block_box : child_boxes)
  273. {
  274. const float child_inner_width = block_box->GetShrinkToFitWidth();
  275. float child_edges_width = 0.f;
  276. if (const Box* child_box = block_box->GetIfBox())
  277. child_edges_width = child_box->GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin, BoxArea::Padding);
  278. content_width = Math::Max(content_width, child_edges_width + child_inner_width);
  279. }
  280. }
  281. if (root_space)
  282. {
  283. // Since we are the root of the block formatting context, add the width contributions of the floated boxes in
  284. // our context. The basic algorithm used can produce overestimates, since floats may not be located next to the
  285. // rest of the content.
  286. const float edge_left = box.GetPosition().x;
  287. const float edge_right = edge_left + box.GetSize().x;
  288. content_width += space->GetShrinkToFitWidth(edge_left, edge_right);
  289. }
  290. float min_width, max_width;
  291. LayoutDetails::GetMinMaxWidth(min_width, max_width, computed, box, 0.f);
  292. content_width = Math::Clamp(content_width, min_width, max_width);
  293. return content_width;
  294. }
  295. const Box* BlockContainer::GetIfBox() const
  296. {
  297. return &box;
  298. }
  299. Element* BlockContainer::GetElement() const
  300. {
  301. return element;
  302. }
  303. const FloatedBoxSpace* BlockContainer::GetBlockBoxSpace() const
  304. {
  305. return space;
  306. }
  307. Vector2f BlockContainer::GetPosition() const
  308. {
  309. return position;
  310. }
  311. Box& BlockContainer::GetBox()
  312. {
  313. return box;
  314. }
  315. const Box& BlockContainer::GetBox() const
  316. {
  317. return box;
  318. }
  319. void BlockContainer::ResetContents()
  320. {
  321. RMLUI_ZoneScopedC(0xDD3322);
  322. if (root_space)
  323. root_space->Reset();
  324. child_boxes.clear();
  325. queued_float_elements.clear();
  326. box_cursor = 0;
  327. interrupted_line_box.reset();
  328. inner_content_size = {};
  329. }
  330. String BlockContainer::DebugDumpTree(int depth) const
  331. {
  332. String value = String(depth * 2, ' ') + "BlockContainer" + " | " + LayoutDetails::GetDebugElementName(element) + '\n';
  333. for (auto&& block_box : child_boxes)
  334. value += block_box->DumpLayoutTree(depth + 1);
  335. return value;
  336. }
  337. InlineContainer* BlockContainer::GetOpenInlineContainer()
  338. {
  339. return const_cast<InlineContainer*>(static_cast<const BlockContainer&>(*this).GetOpenInlineContainer());
  340. }
  341. const InlineContainer* BlockContainer::GetOpenInlineContainer() const
  342. {
  343. if (!child_boxes.empty() && child_boxes.back()->GetType() == Type::InlineContainer)
  344. return rmlui_static_cast<InlineContainer*>(child_boxes.back().get());
  345. return nullptr;
  346. }
  347. InlineContainer* BlockContainer::EnsureOpenInlineContainer()
  348. {
  349. // First check to see if we already have an open inline container.
  350. InlineContainer* inline_container = GetOpenInlineContainer();
  351. // Otherwise, we open a new one.
  352. if (!inline_container)
  353. {
  354. const float scrollbar_width = (IsScrollContainer() ? element->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL) : 0.f);
  355. const float available_width = box.GetSize().x - scrollbar_width;
  356. auto inline_container_ptr = MakeUnique<InlineContainer>(this, available_width);
  357. inline_container = inline_container_ptr.get();
  358. child_boxes.push_back(std::move(inline_container_ptr));
  359. if (interrupted_line_box)
  360. {
  361. inline_container->AddChainedBox(std::move(interrupted_line_box));
  362. interrupted_line_box.reset();
  363. }
  364. }
  365. return inline_container;
  366. }
  367. const LayoutBox* BlockContainer::GetOpenLayoutBox() const
  368. {
  369. if (!child_boxes.empty())
  370. return child_boxes.back().get();
  371. return nullptr;
  372. }
  373. bool BlockContainer::CloseOpenInlineContainer()
  374. {
  375. if (InlineContainer* inline_container = GetOpenInlineContainer())
  376. {
  377. EnsureEmptyInterruptedLineBox();
  378. Vector2f child_position;
  379. float child_height = 0.f;
  380. inline_container->Close(&interrupted_line_box, child_position, child_height);
  381. // Increment our cursor. If this close fails, it means this block container generated an automatic scrollbar.
  382. if (!EncloseChildBox(inline_container, child_position, child_height, 0.f))
  383. return false;
  384. }
  385. return true;
  386. }
  387. void BlockContainer::EnsureEmptyInterruptedLineBox()
  388. {
  389. if (interrupted_line_box)
  390. {
  391. RMLUI_ERROR; // Internal error: Interrupted line box leaked.
  392. interrupted_line_box.reset();
  393. }
  394. }
  395. void BlockContainer::PlaceFloat(Element* element, float vertical_position, Vector2f visible_overflow_size)
  396. {
  397. const Box& element_box = element->GetBox();
  398. const Vector2f border_size = element_box.GetSize(BoxArea::Border);
  399. visible_overflow_size = Math::Max(border_size, visible_overflow_size);
  400. const Vector2f margin_top_left = {element_box.GetEdge(BoxArea::Margin, BoxEdge::Left), element_box.GetEdge(BoxArea::Margin, BoxEdge::Top)};
  401. const Vector2f margin_bottom_right = {element_box.GetEdge(BoxArea::Margin, BoxEdge::Right),
  402. element_box.GetEdge(BoxArea::Margin, BoxEdge::Bottom)};
  403. const Vector2f margin_size = border_size + margin_top_left + margin_bottom_right;
  404. Style::Float float_property = element->GetComputedValues().float_();
  405. Style::Clear clear_property = element->GetComputedValues().clear();
  406. float unused_box_width = 0.f;
  407. const Vector2f margin_position = space->NextFloatPosition(this, unused_box_width, vertical_position, margin_size, float_property, clear_property);
  408. const Vector2f border_position = margin_position + margin_top_left;
  409. space->PlaceFloat(float_property, margin_position, margin_size, border_position, visible_overflow_size);
  410. // Shift the offset into this container's space, which acts as the float element's containing block.
  411. element->SetOffset(border_position - position, GetElement());
  412. }
  413. bool BlockContainer::GetBaselineOfLastLine(float& out_baseline) const
  414. {
  415. // Return the baseline of our last child that itself has a baseline.
  416. for (int i = (int)child_boxes.size() - 1; i >= 0; i--)
  417. {
  418. if (child_boxes[i]->GetBaselineOfLastLine(out_baseline))
  419. return true;
  420. }
  421. return false;
  422. }
  423. } // namespace Rml