InlineContainer.cpp 10 KB

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