InlineContainer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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-2023 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 "InlineContainer.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/ElementText.h"
  33. #include "../../../Include/RmlUi/Core/ElementUtilities.h"
  34. #include "../../../Include/RmlUi/Core/Profiling.h"
  35. #include "../../../Include/RmlUi/Core/Property.h"
  36. #include "BlockContainer.h"
  37. #include "FloatedBoxSpace.h"
  38. #include "InlineLevelBox.h"
  39. #include "LayoutDetails.h"
  40. #include "LineBox.h"
  41. namespace Rml {
  42. InlineContainer::InlineContainer(BlockContainer* _parent, float _available_width) :
  43. LayoutBox(Type::InlineContainer), parent(_parent), root_inline_box(_parent->GetElement())
  44. {
  45. RMLUI_ASSERT(_parent);
  46. box_size = {_available_width, -1.f};
  47. position = parent->NextBoxPosition();
  48. const auto& computed = parent->GetElement()->GetComputedValues();
  49. element_line_height = computed.line_height().value;
  50. wrap_content = (computed.white_space() != Style::WhiteSpace::Nowrap);
  51. text_align = computed.text_align();
  52. }
  53. InlineContainer::~InlineContainer() {}
  54. InlineBox* InlineContainer::AddInlineNode(Node* node, const Box& box)
  55. {
  56. RMLUI_ASSERT(node);
  57. InlineBox* inline_box = nullptr;
  58. InlineLevelBox* inline_level_box = nullptr;
  59. InlineBoxBase* parent_box = GetOpenInlineBox();
  60. if (auto text_node = AsIf<ElementText*>(node))
  61. {
  62. inline_level_box = parent_box->AddChild(MakeUnique<InlineLevelBox_Text>(text_node));
  63. }
  64. else if (auto element = AsIf<Element*>(node))
  65. {
  66. if (box.GetSize().x >= 0.f)
  67. {
  68. inline_level_box = parent_box->AddChild(MakeUnique<InlineLevelBox_Atomic>(parent_box, element, box));
  69. }
  70. else
  71. {
  72. auto inline_box_ptr = MakeUnique<InlineBox>(parent_box, element, box);
  73. inline_box = inline_box_ptr.get();
  74. inline_level_box = parent_box->AddChild(std::move(inline_box_ptr));
  75. }
  76. }
  77. else
  78. {
  79. RMLUI_ERRORMSG("Expected Element or Text node in InlineContainer::AddInlineNode");
  80. }
  81. const float minimum_line_height =
  82. Math::Max(element_line_height, (box.GetSize().y >= 0.f ? box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin) : 0.f));
  83. LayoutOverflowHandle overflow_handle = {};
  84. float minimum_width_next = 0.f;
  85. while (true)
  86. {
  87. LineBox* line_box = EnsureOpenLineBox();
  88. UpdateLineBoxPlacement(line_box, minimum_width_next, minimum_line_height);
  89. InlineLayoutMode layout_mode = InlineLayoutMode::Nowrap;
  90. if (wrap_content)
  91. {
  92. const bool line_shrinked_by_floats = (line_box->GetLineWidth() + 0.5f < box_size.x && minimum_width_next < box_size.x);
  93. const bool can_wrap_any = (line_shrinked_by_floats || line_box->HasContent());
  94. layout_mode = (can_wrap_any ? InlineLayoutMode::WrapAny : InlineLayoutMode::WrapAfterContent);
  95. }
  96. const bool add_new_line = line_box->AddBox(inline_level_box, layout_mode, overflow_handle);
  97. if (!add_new_line)
  98. break;
  99. minimum_width_next = (line_box->HasContent() ? 0.f : line_box->GetLineWidth() + 1.f);
  100. // Keep adding boxes on a new line, either because the box couldn't fit on the current line at all, or because it had to be split.
  101. CloseOpenLineBox(false);
  102. }
  103. return inline_box;
  104. }
  105. void InlineContainer::CloseInlineNode(InlineBox* inline_box)
  106. {
  107. if (LineBox* line_box = GetOpenLineBox())
  108. {
  109. line_box->CloseInlineBox(inline_box);
  110. }
  111. else
  112. {
  113. RMLUI_ERROR;
  114. }
  115. }
  116. void InlineContainer::AddBreak(float line_height)
  117. {
  118. // Simply end the line if one is open, otherwise increment by the line height.
  119. if (GetOpenLineBox())
  120. CloseOpenLineBox(true);
  121. else
  122. box_cursor += line_height;
  123. }
  124. void InlineContainer::AddChainedBox(UniquePtr<LineBox> open_line_box)
  125. {
  126. RMLUI_ASSERT(line_boxes.empty());
  127. RMLUI_ASSERT(open_line_box && !open_line_box->IsClosed());
  128. line_boxes.push_back(std::move(open_line_box));
  129. }
  130. void InlineContainer::Close(UniquePtr<LineBox>* out_open_line_box, Vector2f& out_position, float& out_height)
  131. {
  132. RMLUI_ZoneScoped;
  133. // The parent container may need the open line box to be split and resumed.
  134. CloseOpenLineBox(true, out_open_line_box);
  135. // It is possible that floats were queued between closing the last line and closing this container, if so place them now.
  136. parent->PlaceQueuedFloats(position.y + box_cursor);
  137. // Set this box's height.
  138. box_size.y = Math::Max(box_cursor, 0.f);
  139. // Find the overflow size for our content, relative to our local space.
  140. Vector2f visible_overflow_size = {0.f, box_size.y};
  141. for (const auto& line_box : line_boxes)
  142. {
  143. visible_overflow_size.x = Math::Max(visible_overflow_size.x, line_box->GetPosition().x - position.x + line_box->GetExtentRight());
  144. }
  145. visible_overflow_size.x = Math::RoundDown(visible_overflow_size.x);
  146. SetVisibleOverflowSize(visible_overflow_size);
  147. out_position = position;
  148. out_height = box_size.y;
  149. }
  150. void InlineContainer::CloseOpenLineBox(bool split_all_open_boxes, UniquePtr<LineBox>* out_split_line)
  151. {
  152. if (LineBox* line_box = GetOpenLineBox())
  153. {
  154. float height_of_line = 0.f;
  155. UniquePtr<LineBox> split_line_box = line_box->DetermineVerticalPositioning(&root_inline_box, split_all_open_boxes, height_of_line);
  156. // If the final height of the line is larger than previously considered, we might need to push the line down to
  157. // clear overlapping floats.
  158. if (height_of_line > line_box->GetLineMinimumHeight())
  159. UpdateLineBoxPlacement(line_box, 0.f, height_of_line);
  160. // Now that the line has been given a final position and size, close the line box to submit all the fragments.
  161. // Our parent block container acts as the containing block for our inline boxes.
  162. line_box->Close(parent->GetElement(), parent->GetPosition(), text_align);
  163. // Move the cursor down, unless we should collapse the line.
  164. if (!line_box->CanCollapseLine())
  165. box_cursor = (line_box->GetPosition().y - position.y) + height_of_line;
  166. // If we have any pending floating elements for our parent, then this would be an ideal time to place them.
  167. parent->PlaceQueuedFloats(position.y + box_cursor);
  168. if (split_line_box)
  169. {
  170. if (out_split_line)
  171. *out_split_line = std::move(split_line_box);
  172. else
  173. line_boxes.push_back(std::move(split_line_box));
  174. }
  175. }
  176. }
  177. bool InlineContainer::GetOpenLineBoxDimensions(float& out_vertical_position, Vector2f& out_tentative_size) const
  178. {
  179. if (LineBox* line_box = GetOpenLineBox())
  180. {
  181. out_vertical_position = position.y + box_cursor;
  182. out_tentative_size = {line_box->GetBoxCursor(), line_box->GetLineMinimumHeight()};
  183. return true;
  184. }
  185. return false;
  186. }
  187. void InlineContainer::UpdateOpenLineBoxPlacement()
  188. {
  189. if (LineBox* line_box = GetOpenLineBox())
  190. UpdateLineBoxPlacement(line_box, 0.f, element_line_height);
  191. }
  192. void InlineContainer::UpdateLineBoxPlacement(LineBox* line_box, float minimum_width, float minimum_height)
  193. {
  194. RMLUI_ASSERT(line_box);
  195. Vector2f minimum_dimensions = {
  196. Math::Max(minimum_width, line_box->GetBoxCursor()),
  197. Math::Max(minimum_height, line_box->GetLineMinimumHeight()),
  198. };
  199. // @performance: We might benefit from doing this search only when the minimum dimensions change, or if we get new inline floats.
  200. const float ideal_position_y = position.y + box_cursor;
  201. float available_width = 0.f;
  202. const Vector2f line_position =
  203. parent->GetBlockBoxSpace()->NextBoxPosition(parent, available_width, ideal_position_y, minimum_dimensions, !wrap_content);
  204. available_width = Math::Max(available_width, 0.f);
  205. line_box->SetLineBox(line_position, available_width, minimum_dimensions.y);
  206. }
  207. float InlineContainer::GetShrinkToFitWidth() const
  208. {
  209. float content_width = 0.0f;
  210. // Simply find our widest line.
  211. for (const auto& line_box : line_boxes)
  212. content_width = Math::Max(content_width, line_box->GetBoxCursor());
  213. return content_width;
  214. }
  215. Vector2f InlineContainer::GetStaticPositionEstimate(bool inline_level_box) const
  216. {
  217. Vector2f result = {0.f, box_cursor};
  218. if (const LineBox* line_box = GetOpenLineBox())
  219. {
  220. if (inline_level_box)
  221. result.x += line_box->GetBoxCursor();
  222. else
  223. result.y += element_line_height;
  224. }
  225. return result;
  226. }
  227. bool InlineContainer::GetBaselineOfLastLine(float& out_baseline) const
  228. {
  229. if (!line_boxes.empty())
  230. {
  231. out_baseline = line_boxes.back()->GetPosition().y + line_boxes.back()->GetBaseline();
  232. return true;
  233. }
  234. return false;
  235. }
  236. LineBox* InlineContainer::EnsureOpenLineBox()
  237. {
  238. if (line_boxes.empty() || line_boxes.back()->IsClosed())
  239. {
  240. line_boxes.push_back(MakeUnique<LineBox>());
  241. }
  242. return line_boxes.back().get();
  243. }
  244. LineBox* InlineContainer::GetOpenLineBox() const
  245. {
  246. if (line_boxes.empty() || line_boxes.back()->IsClosed())
  247. return nullptr;
  248. return line_boxes.back().get();
  249. }
  250. InlineBoxBase* InlineContainer::GetOpenInlineBox()
  251. {
  252. if (LineBox* line_box = GetOpenLineBox())
  253. {
  254. if (InlineBox* inline_box = line_box->GetOpenInlineBox())
  255. return inline_box;
  256. }
  257. return &root_inline_box;
  258. }
  259. String InlineContainer::DebugDumpTree(int depth) const
  260. {
  261. String value = String(depth * 2, ' ') + "InlineContainer" + '\n';
  262. value += root_inline_box.DebugDumpTree(depth + 1);
  263. for (const auto& line_box : line_boxes)
  264. value += line_box->DebugDumpTree(depth + 1);
  265. return value;
  266. }
  267. } // namespace Rml