LineBox.cpp 16 KB

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