InlineContainer.cpp 10 KB

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