InlineContainer.cpp 10 KB

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