LineBox.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #include "LineBox.h"
  2. #include "../../../Include/RmlUi/Core/Element.h"
  3. #include "../../../Include/RmlUi/Core/StyleTypes.h"
  4. #include "InlineBox.h"
  5. #include "InlineLevelBox.h"
  6. #include "LayoutPools.h"
  7. #include <float.h>
  8. #include <numeric>
  9. namespace Rml {
  10. LineBox::~LineBox() {}
  11. void LineBox::SetLineBox(Vector2f _line_position, float _line_width, float _line_minimum_height)
  12. {
  13. line_position = _line_position;
  14. line_width = _line_width;
  15. line_minimum_height = _line_minimum_height;
  16. }
  17. bool LineBox::AddBox(InlineLevelBox* box, InlineLayoutMode layout_mode, LayoutOverflowHandle& inout_overflow_handle)
  18. {
  19. RMLUI_ASSERT(!is_closed);
  20. const bool first_box = !HasContent();
  21. // Find the spacing this element must leave on its right side, to account for its parent inline boxes to be closed later.
  22. float open_spacing_right = 0.f;
  23. ForAllOpenFragments([&](const Fragment& fragment) { open_spacing_right += fragment.box->GetSpacingRight(); });
  24. const float box_placement_cursor = box_cursor + open_spacing_left;
  25. float available_width = FLT_MAX;
  26. if (layout_mode != InlineLayoutMode::Nowrap)
  27. {
  28. available_width = Math::RoundUp(line_width - box_placement_cursor);
  29. if (available_width < 0.f)
  30. {
  31. if (layout_mode == InlineLayoutMode::WrapAny)
  32. return true;
  33. else
  34. available_width = 0.f;
  35. }
  36. }
  37. FragmentConstructor constructor = box->CreateFragment(layout_mode, available_width, open_spacing_right, first_box, inout_overflow_handle);
  38. if (constructor.type == FragmentType::Invalid)
  39. {
  40. // Could not place fragment on this line, try again on a new line.
  41. RMLUI_ASSERT(layout_mode == InlineLayoutMode::WrapAny);
  42. return true;
  43. }
  44. const FragmentIndex fragment_index = (FragmentIndex)fragments.size();
  45. fragments.push_back(Fragment{box, constructor, box->GetVerticalAlign(), box_placement_cursor, open_fragments_leaf});
  46. fragments.back().aligned_subtree_root = DetermineAlignedSubtreeRoot(fragment_index);
  47. inout_overflow_handle = {};
  48. bool continue_on_new_line = false;
  49. switch (constructor.type)
  50. {
  51. case FragmentType::InlineBox:
  52. {
  53. RMLUI_ASSERT(constructor.layout_width < 0.f);
  54. RMLUI_ASSERT(rmlui_static_cast<InlineBox*>(box));
  55. open_fragments_leaf = fragment_index;
  56. open_spacing_left += box->GetSpacingLeft();
  57. }
  58. break;
  59. case FragmentType::SizedBox:
  60. case FragmentType::TextRun:
  61. {
  62. RMLUI_ASSERT(constructor.layout_width >= 0.f);
  63. box_cursor = box_placement_cursor + constructor.layout_width;
  64. open_spacing_left = 0.f;
  65. if (constructor.overflow_handle)
  66. {
  67. continue_on_new_line = true;
  68. inout_overflow_handle = constructor.overflow_handle;
  69. }
  70. // Mark open fragments as having content so we later know whether we should split or move them in case of overflow.
  71. ForAllOpenFragments([&](Fragment& fragment) { fragment.has_content = true; });
  72. has_content = true;
  73. }
  74. break;
  75. case FragmentType::Invalid:
  76. RMLUI_ERROR; // Handled above;
  77. break;
  78. }
  79. return continue_on_new_line;
  80. }
  81. void LineBox::CloseFragment(Fragment& open_fragment, float right_inner_edge_position)
  82. {
  83. RMLUI_ASSERT(open_fragment.type == FragmentType::InlineBox);
  84. open_fragment.children_end_index = (int)fragments.size();
  85. const float spacing_left = (open_fragment.split_left ? 0.f : open_fragment.box->GetSpacingLeft());
  86. open_fragment.layout_width = Math::Max(right_inner_edge_position - open_fragment.position.x - spacing_left, 0.f);
  87. }
  88. void LineBox::CloseInlineBox(InlineBox* inline_box)
  89. {
  90. if (open_fragments_leaf == RootFragmentIndex || fragments[open_fragments_leaf].box != inline_box)
  91. {
  92. RMLUI_ERRORMSG("Inline box open/close mismatch.");
  93. return;
  94. }
  95. box_cursor += open_spacing_left;
  96. open_spacing_left = 0.f;
  97. Fragment& fragment = fragments[open_fragments_leaf];
  98. CloseFragment(fragment, box_cursor);
  99. box_cursor += fragment.box->GetSpacingRight();
  100. open_fragments_leaf = fragment.parent;
  101. }
  102. UniquePtr<LineBox> LineBox::SplitLine(bool split_all_open_boxes)
  103. {
  104. if (open_fragments_leaf == RootFragmentIndex)
  105. return nullptr;
  106. int num_open_fragments = 0;
  107. ForAllOpenFragments([&](const Fragment& /*fragment*/) { num_open_fragments += 1; });
  108. // Move the box cursor to account for any newly opened inline boxes to be closed.
  109. box_cursor += open_spacing_left;
  110. open_spacing_left = 0.f;
  111. // Make a new line with the open fragments.
  112. auto new_line = MakeUnique<LineBox>();
  113. new_line->fragments.resize(num_open_fragments);
  114. // Copy all open fragments to the next line. Do it in reverse order of iteration, since we iterate from back to front.
  115. FragmentIndex new_index = num_open_fragments;
  116. ForAllOpenFragments([&](Fragment& old_fragment) {
  117. new_index -= 1;
  118. RMLUI_ASSERT((size_t)new_index < new_line->fragments.size() && old_fragment.children_end_index == 0);
  119. // Copy the old fragment.
  120. Fragment& new_fragment = new_line->fragments[new_index];
  121. new_fragment = old_fragment;
  122. new_fragment.position.x = 0.f;
  123. new_fragment.parent = new_index - 1;
  124. new_fragment.aligned_subtree_root = new_line->DetermineAlignedSubtreeRoot(new_index);
  125. // Only fragments with content placed on the previous line is split, otherwise the fragment is moved down.
  126. if (new_fragment.has_content || split_all_open_boxes)
  127. {
  128. new_fragment.split_left = true;
  129. new_fragment.has_content = false;
  130. CloseFragment(old_fragment, box_cursor);
  131. old_fragment.split_right = true;
  132. }
  133. else
  134. {
  135. const float spacing_left = (new_fragment.split_left ? 0.f : new_fragment.box->GetSpacingLeft());
  136. new_line->open_spacing_left += spacing_left;
  137. // The old fragment is not closed here, which ensures that it will not be placed/submitted on the previous
  138. // line. Backtrack the box cursor since this fragment is moved down to the next line.
  139. box_cursor -= spacing_left;
  140. }
  141. });
  142. new_line->open_fragments_leaf = (int)new_line->fragments.size() - 1;
  143. open_fragments_leaf = RootFragmentIndex;
  144. #ifdef RMLUI_DEBUG
  145. // Verify integrity of the fragment tree after split.
  146. for (int i = 0; i < (int)new_line->fragments.size(); i++)
  147. {
  148. const Fragment& fragment = new_line->fragments[i];
  149. RMLUI_ASSERT(fragment.type == FragmentType::InlineBox);
  150. RMLUI_ASSERT(fragment.parent < i);
  151. RMLUI_ASSERT(fragment.parent == i - 1);
  152. RMLUI_ASSERT(fragment.parent == RootFragmentIndex || new_line->fragments[fragment.parent].type == FragmentType::InlineBox);
  153. RMLUI_ASSERT(
  154. fragment.aligned_subtree_root == RootFragmentIndex || new_line->IsAlignedSubtreeRoot(new_line->fragments[fragment.aligned_subtree_root]));
  155. RMLUI_ASSERT(fragment.children_end_index == 0);
  156. }
  157. #endif
  158. return new_line;
  159. }
  160. UniquePtr<LineBox> LineBox::DetermineVerticalPositioning(const InlineBoxRoot* root_inline_box, bool split_all_open_boxes, float& out_height_of_line)
  161. {
  162. RMLUI_ASSERT(!is_closed && !is_vertically_positioned);
  163. UniquePtr<LineBox> new_line_box = SplitLine(split_all_open_boxes);
  164. RMLUI_ASSERT(open_fragments_leaf == RootFragmentIndex); // Ensure all open fragments are either closed or split.
  165. // Vertical alignment and sizing.
  166. //
  167. // Aligned subtree CSS definition: "The aligned subtree of an inline element contains that element and the aligned
  168. // subtrees of all children inline elements whose computed vertical-align value is not 'top' or 'bottom'."
  169. //
  170. // We have already determined each box's offset relative to its parent baseline, and its layout size above and below
  171. // its baseline. Now, for each aligned subtree, place all fragments belonging to that subtree relative to the
  172. // subtree root baseline. Simultaneously, consider each fragment and keep track of the maximum height above root
  173. // baseline (max_ascent) and maximum depth below root baseline (max_descent). Their sum is the height of that
  174. // aligned subtree.
  175. //
  176. // Next, treat the root inline box like just another aligned subtree. Then the line box height is first determined
  177. // by the height of that subtree. Other aligned subtrees might push out that size to make them fit. After the line
  178. // box size is determined, position each aligned subtree according to its vertical-align, and then position each
  179. // fragment relative to the aligned subtree they belong to.
  180. float max_ascent = root_inline_box->GetHeightAboveBaseline();
  181. float max_descent = root_inline_box->GetDepthBelowBaseline();
  182. {
  183. const int subtree_root_index = RootFragmentIndex;
  184. const int children_end_index = (int)fragments.size();
  185. VerticallyAlignSubtree(subtree_root_index, children_end_index, max_ascent, max_descent);
  186. }
  187. // Find all the aligned subtrees, and vertically align each of them independently.
  188. for (int i = 0; i < (int)fragments.size(); i++)
  189. {
  190. Fragment& fragment = fragments[i];
  191. if (IsAlignedSubtreeRoot(fragment))
  192. {
  193. fragment.max_ascent = fragment.box->GetHeightAboveBaseline();
  194. fragment.max_descent = fragment.box->GetDepthBelowBaseline();
  195. if (fragment.type == FragmentType::InlineBox)
  196. {
  197. const int subtree_root_index = i;
  198. VerticallyAlignSubtree(subtree_root_index, fragment.children_end_index, fragment.max_ascent, fragment.max_descent);
  199. }
  200. const float subtree_height = fragment.max_ascent + fragment.max_descent;
  201. // Increase the line box size to fit all line-relative aligned fragments.
  202. switch (fragment.vertical_align)
  203. {
  204. case VerticalAlignType::Top: max_descent = Math::Max(max_descent, subtree_height - max_ascent); break;
  205. case VerticalAlignType::Bottom: max_ascent = Math::Max(max_ascent, subtree_height - max_descent); break;
  206. case VerticalAlignType::Center:
  207. {
  208. // Distribute the subtree's height equally to the ascent and descent.
  209. const float distribute_height = 0.5f * (subtree_height - (max_ascent + max_descent));
  210. if (distribute_height > 0.f)
  211. {
  212. max_ascent += distribute_height;
  213. max_descent += distribute_height;
  214. }
  215. }
  216. break;
  217. default: RMLUI_ERROR; break;
  218. }
  219. }
  220. }
  221. // Size the line.
  222. out_height_of_line = max_ascent + max_descent;
  223. total_height_above_baseline = max_ascent;
  224. // Now that the line is sized we can set the vertical position of the fragments.
  225. for (Fragment& fragment : fragments)
  226. {
  227. switch (fragment.vertical_align)
  228. {
  229. case VerticalAlignType::Top: fragment.position.y = fragment.max_ascent; break;
  230. case VerticalAlignType::Bottom: fragment.position.y = out_height_of_line - fragment.max_descent; break;
  231. case VerticalAlignType::Center: fragment.position.y = 0.5f * (fragment.max_ascent - fragment.max_descent + out_height_of_line); break;
  232. default:
  233. {
  234. RMLUI_ASSERT(!IsAlignedSubtreeRoot(fragment));
  235. const float aligned_subtree_baseline =
  236. (fragment.aligned_subtree_root < 0 ? max_ascent : fragments[fragment.aligned_subtree_root].position.y);
  237. fragment.position.y = aligned_subtree_baseline + fragment.baseline_offset;
  238. }
  239. break;
  240. }
  241. }
  242. is_vertically_positioned = true;
  243. return new_line_box;
  244. }
  245. void LineBox::Close(Element* offset_parent, Vector2f offset_parent_position, Style::TextAlign text_align)
  246. {
  247. RMLUI_ASSERT(is_vertically_positioned && !is_closed);
  248. // Horizontal alignment using available space on our line.
  249. if (box_cursor < line_width)
  250. {
  251. switch (text_align)
  252. {
  253. case Style::TextAlign::Center: offset_horizontal_alignment = (line_width - box_cursor) * 0.5f; break;
  254. case Style::TextAlign::Right: offset_horizontal_alignment = (line_width - box_cursor); break;
  255. case Style::TextAlign::Left: // Already left-aligned.
  256. case Style::TextAlign::Justify: // Justification occurs at the text level.
  257. break;
  258. }
  259. }
  260. // Position and size all inline-level boxes, place geometry boxes.
  261. for (const Fragment& fragment : fragments)
  262. {
  263. // Skip inline boxes which have not been closed (moved down to next line).
  264. if (fragment.type == FragmentType::InlineBox && fragment.children_end_index == 0)
  265. continue;
  266. RMLUI_ASSERT(fragment.layout_width >= 0.f);
  267. const PlacedFragment placed_fragment = {
  268. offset_parent,
  269. fragment.fragment_handle,
  270. line_position - offset_parent_position + fragment.position + Vector2f(offset_horizontal_alignment, 0.f),
  271. fragment.layout_width,
  272. fragment.split_left,
  273. fragment.split_right,
  274. };
  275. fragment.box->Submit(placed_fragment);
  276. }
  277. is_closed = true;
  278. }
  279. void LineBox::VerticallyAlignSubtree(const int subtree_root_index, const int children_end_index, float& max_ascent, float& max_descent)
  280. {
  281. const int children_begin_index = subtree_root_index + 1;
  282. // Iterate all descendant fragments which belong to our aligned subtree.
  283. for (int i = children_begin_index; i < children_end_index; i++)
  284. {
  285. Fragment& fragment = fragments[i];
  286. if (fragment.aligned_subtree_root != subtree_root_index)
  287. continue;
  288. // Position the baseline of fragments relative to their subtree root.
  289. const float parent_absolute_baseline = (fragment.parent < 0 ? 0.f : fragments[fragment.parent].baseline_offset);
  290. fragment.baseline_offset = parent_absolute_baseline + fragment.box->GetVerticalOffsetFromParent();
  291. // Expand this aligned subtree's height based on the height contributions of its descendants.
  292. if (fragment.type != FragmentType::TextRun)
  293. {
  294. max_ascent = Math::Max(max_ascent, fragment.box->GetHeightAboveBaseline() - fragment.baseline_offset);
  295. max_descent = Math::Max(max_descent, fragment.box->GetDepthBelowBaseline() + fragment.baseline_offset);
  296. }
  297. }
  298. }
  299. InlineBox* LineBox::GetOpenInlineBox()
  300. {
  301. if (open_fragments_leaf == RootFragmentIndex)
  302. return nullptr;
  303. return rmlui_static_cast<InlineBox*>(fragments[open_fragments_leaf].box);
  304. }
  305. bool LineBox::CanCollapseLine() const
  306. {
  307. // Roughly, collapse lines with only empty text fragments or inline boxes not taking up any width or spacing.
  308. for (const Fragment& fragment : fragments)
  309. {
  310. if (fragment.layout_width > 0.f)
  311. return false;
  312. else if (fragment.type == FragmentType::SizedBox)
  313. return false;
  314. else if (fragment.type == FragmentType::InlineBox && fragment.children_end_index > 0)
  315. {
  316. const bool any_spacing_left = (!fragment.split_left && fragment.box->GetSpacingLeft() != 0.f);
  317. const bool any_spacing_right = (!fragment.split_right && fragment.box->GetSpacingRight() != 0.f);
  318. if (any_spacing_left || any_spacing_right)
  319. return false;
  320. }
  321. }
  322. return true;
  323. }
  324. float LineBox::GetExtentRight() const
  325. {
  326. RMLUI_ASSERT(is_closed);
  327. return box_cursor + offset_horizontal_alignment;
  328. }
  329. float LineBox::GetBaseline() const
  330. {
  331. RMLUI_ASSERT(is_closed);
  332. return total_height_above_baseline;
  333. }
  334. String LineBox::DebugDumpTree(int depth) const
  335. {
  336. const String value = String(depth * 2, ' ') + "LineBox (" + ToString(fragments.size()) + " fragment" + (fragments.size() == 1 ? "" : "s") + ")\n";
  337. return value;
  338. }
  339. void* LineBox::operator new(size_t size)
  340. {
  341. return LayoutPools::AllocateLayoutChunk(size);
  342. }
  343. void LineBox::operator delete(void* chunk, size_t size)
  344. {
  345. LayoutPools::DeallocateLayoutChunk(chunk, size);
  346. }
  347. } // namespace Rml