InlineContainer.cpp 8.7 KB

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