LayoutLineBox.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 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 "LayoutLineBox.h"
  29. #include "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/ElementText.h"
  31. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  32. #include "../../Include/RmlUi/Core/Profiling.h"
  33. #include "../../Include/RmlUi/Core/Property.h"
  34. #include "LayoutBlockBox.h"
  35. #include "LayoutEngine.h"
  36. #include "LayoutInlineBoxText.h"
  37. #include <stack>
  38. namespace Rml {
  39. static float GetSpacing(const Box& box, Box::Edge edge)
  40. {
  41. return box.GetEdge(Box::PADDING, edge) +
  42. box.GetEdge(Box::BORDER, edge) +
  43. box.GetEdge(Box::MARGIN, edge);
  44. }
  45. LayoutLineBox::LayoutLineBox(LayoutBlockBox* _parent) : position(-1, -1), dimensions(-1, -1)
  46. {
  47. parent = _parent;
  48. box_cursor = 0;
  49. open_inline_box = nullptr;
  50. position_set = false;
  51. wrap_content = false;
  52. }
  53. LayoutLineBox::~LayoutLineBox()
  54. {
  55. }
  56. // Closes the line box, positioning all inline elements within it.
  57. LayoutInlineBox* LayoutLineBox::Close(UniquePtr<LayoutInlineBox> overflow)
  58. {
  59. RMLUI_ZoneScoped;
  60. // If we haven't positioned this line yet, and it has elements in it, then this is a great opportunity to do so.
  61. if (!position_set &&
  62. !inline_boxes.empty())
  63. {
  64. parent->PositionLineBox(position, dimensions.x, wrap_content, Vector2f(0, 0));
  65. dimensions.y = 0;
  66. position_set = true;
  67. }
  68. // If the line has been positioned and our content is greater than our original size (for example, if we aren't
  69. // wrapping or had to render a very long word), then we push our dimensions out to compensate.
  70. else
  71. dimensions.x = Math::Max(dimensions.x, box_cursor);
  72. // Now we calculate the baselines of each of our inline boxes relative to their parent box's baseline; either us,
  73. // or another of our inline boxes. The maximum distance each element is above and below our baseline is calculated
  74. // from that, and therefore our height.
  75. float ascender = 0;
  76. float descender = 0;
  77. float minimum_height = 0;
  78. for (size_t i = 0; i < inline_boxes.size(); ++i)
  79. {
  80. LayoutInlineBox* inline_box = inline_boxes[i].get();
  81. // Check if we've got an element aligned to the line box rather than a baseline.
  82. if (inline_box->GetVerticalAlignProperty().type == Style::VerticalAlign::Top ||
  83. inline_box->GetVerticalAlignProperty().type == Style::VerticalAlign::Bottom)
  84. {
  85. // Get this element to calculate the baseline offsets of its children; it can't calculate its own baseline
  86. // because we don't know the height of the line box yet. We don't actually care about its ascender or
  87. // descender either, just its height.
  88. float box_ascender, box_descender;
  89. inline_box->CalculateBaseline(box_ascender, box_descender);
  90. minimum_height = Math::Max(minimum_height, inline_box->GetHeight());
  91. }
  92. // Otherwise, we have an element anchored to a baseline, so we can fetch its ascender and descender relative
  93. // to our baseline.
  94. else if (inline_box->GetParent() == nullptr)
  95. {
  96. float box_ascender, box_descender;
  97. inline_box->CalculateBaseline(box_ascender, box_descender);
  98. ascender = Math::Max(ascender, box_ascender - inline_box->GetPosition().y);
  99. descender = Math::Max(descender, box_descender + inline_box->GetPosition().y);
  100. }
  101. }
  102. // We've now got the maximum ascender and descender, we can calculate the dimensions of the line box.
  103. dimensions.y = Math::Max(minimum_height, ascender + descender);
  104. // And from that, we can now set the final baseline of each box.
  105. for (size_t i = 0; i < inline_boxes.size(); ++i)
  106. {
  107. LayoutInlineBox* inline_box = inline_boxes[i].get();
  108. // Check again if this element is aligned to the line box. We don't need to worry about offsetting an element
  109. // tied to the top of the line box, as its position will always stay at exactly 0.
  110. if (inline_box->GetVerticalAlignProperty().type == Style::VerticalAlign::Top||
  111. inline_box->GetVerticalAlignProperty().type == Style::VerticalAlign::Bottom)
  112. {
  113. if (inline_box->GetVerticalAlignProperty().type == Style::VerticalAlign::Top)
  114. inline_box->OffsetBaseline(inline_box->GetHeight() - inline_box->GetBaseline());
  115. else
  116. inline_box->OffsetBaseline(dimensions.y - inline_box->GetBaseline());
  117. }
  118. // Otherwise, this element is tied to a baseline.
  119. else if (inline_box->GetParent() == nullptr)
  120. inline_box->OffsetBaseline(ascender);
  121. }
  122. // Position all the boxes horizontally in the line. We only need to reposition the elements if they're set to
  123. // centre or right; the element are already placed left-aligned, and justification occurs at the text level.
  124. Style::TextAlign text_align_property = parent->GetParent()->GetElement()->GetComputedValues().text_align();
  125. if (text_align_property == Style::TextAlign::Center ||
  126. text_align_property == Style::TextAlign::Right)
  127. {
  128. float element_offset = 0;
  129. switch (text_align_property)
  130. {
  131. case Style::TextAlign::Center: element_offset = (dimensions.x - box_cursor) * 0.5f; break;
  132. case Style::TextAlign::Right: element_offset = (dimensions.x - box_cursor); break;
  133. default: break;
  134. }
  135. if (element_offset != 0)
  136. {
  137. for (size_t i = 0; i < inline_boxes.size(); i++)
  138. inline_boxes[i]->SetHorizontalPosition(inline_boxes[i]->GetPosition().x + element_offset);
  139. }
  140. }
  141. // Get each line box to set the position of their element, relative to their parents.
  142. for (int i = (int) inline_boxes.size() - 1; i >= 0; --i)
  143. {
  144. inline_boxes[i]->PositionElement();
  145. // Check if this inline box is part of the open box chain.
  146. bool inline_box_open = false;
  147. LayoutInlineBox* open_box = open_inline_box;
  148. while (open_box != nullptr &&
  149. !inline_box_open)
  150. {
  151. if (inline_boxes[i].get() == open_box)
  152. inline_box_open = true;
  153. open_box = open_box->GetParent();
  154. }
  155. inline_boxes[i]->SizeElement(inline_box_open);
  156. }
  157. return parent->CloseLineBox(this, std::move(overflow), open_inline_box);
  158. }
  159. // Closes one of the line box's inline boxes.
  160. void LayoutLineBox::CloseInlineBox(LayoutInlineBox* inline_box)
  161. {
  162. RMLUI_ASSERT(open_inline_box == inline_box);
  163. open_inline_box = inline_box->GetParent();
  164. box_cursor += GetSpacing(inline_box->GetBox(), Box::RIGHT);
  165. }
  166. // Attempts to add a new element to this line box.
  167. LayoutInlineBox* LayoutLineBox::AddElement(Element* element, const Box& box)
  168. {
  169. RMLUI_ZoneScoped;
  170. ElementText* element_text = rmlui_dynamic_cast<ElementText*>(element);
  171. if (element_text)
  172. return AddBox(MakeUnique<LayoutInlineBoxText>(element_text));
  173. else
  174. return AddBox(MakeUnique<LayoutInlineBox>(element, box));
  175. }
  176. // Attempts to add a new inline box to this line.
  177. LayoutInlineBox* LayoutLineBox::AddBox(UniquePtr<LayoutInlineBox> box_ptr)
  178. {
  179. RMLUI_ZoneScoped;
  180. // Set to true if we're flowing the first box (with content) on the line.
  181. bool first_box = false;
  182. // Set to true for the last box in the line so that we can trim end spacing.
  183. bool last_box = false;
  184. // The spacing this element must leave on the right of the line, to account not only for its margins and padding,
  185. // but also for its parents which will close immediately after it.
  186. float right_spacing;
  187. // If this line is unplaced, then this is the first inline box; if it is sized, then we can place and size this
  188. // line.
  189. if (!position_set)
  190. {
  191. // Add the new box to the list of boxes in the line box. As this line box has not been placed, we don't have to
  192. // check if it can fit yet.
  193. LayoutInlineBox* box = AppendBox(std::move(box_ptr));
  194. // If the new box has a physical prescence, then we must place this line once we've figured out how wide it has to
  195. // be.
  196. if (box->GetBox().GetSize().x >= 0)
  197. {
  198. // Calculate the dimensions for the box we need to fit.
  199. Vector2f minimum_dimensions = box->GetBox().GetSize();
  200. // Add the width of any empty, already closed tags, or still opened spaced tags.
  201. minimum_dimensions.x += box_cursor;
  202. // Calculate the right spacing for the element.
  203. right_spacing = GetSpacing(box->GetBox(), Box::RIGHT);
  204. // Add the right spacing for any ancestor elements that must close immediately after it.
  205. LayoutInlineBox* closing_box = box;
  206. while (closing_box && closing_box->IsLastChild())
  207. {
  208. closing_box = closing_box->GetParent();
  209. if (closing_box)
  210. right_spacing += GetSpacing(closing_box->GetBox(), Box::RIGHT);
  211. }
  212. if (!box->CanOverflow())
  213. minimum_dimensions.x += right_spacing;
  214. parent->PositionLineBox(position, dimensions.x, wrap_content, minimum_dimensions);
  215. dimensions.y = minimum_dimensions.y;
  216. first_box = true;
  217. last_box = box->IsLastChild();
  218. position_set = true;
  219. }
  220. else
  221. return box;
  222. }
  223. // This line has already been placed and sized, so we'll check if we can fit this new inline box on the line.
  224. else
  225. {
  226. LayoutInlineBox* box = box_ptr.get();
  227. // Build up the spacing required on the right side of this element. This consists of the right spacing on the
  228. // new element, and the right spacing on all parent element that will close next.
  229. right_spacing = GetSpacing(box->GetBox(), Box::RIGHT);
  230. if (open_inline_box != nullptr &&
  231. box->IsLastChild())
  232. {
  233. LayoutInlineBox* closing_box = open_inline_box;
  234. while (closing_box != nullptr &&
  235. closing_box->IsLastChild())
  236. {
  237. closing_box = closing_box->GetParent();
  238. if (closing_box != nullptr)
  239. right_spacing += GetSpacing(closing_box->GetBox(), Box::RIGHT);
  240. }
  241. }
  242. // Determine the inline box's spacing requirements (before we get onto it's actual content width).
  243. float element_width = box->GetBox().GetPosition(Box::CONTENT).x;
  244. if (!box->CanOverflow())
  245. element_width += right_spacing;
  246. // Add on the box's content area (if it has content).
  247. if (box->GetBox().GetSize().x >= 0)
  248. element_width += box->GetBox().GetSize().x;
  249. if (wrap_content && box_cursor + element_width > dimensions.x)
  250. {
  251. // We can't fit the new inline element into this box! So we'll close this line box, and send the inline box
  252. // onto the next line.
  253. return Close(std::move(box_ptr));
  254. }
  255. // We can fit the new inline element into this box.
  256. AppendBox(std::move(box_ptr));
  257. last_box = box->IsLastChild();
  258. }
  259. float available_width = -1;
  260. if (wrap_content)
  261. available_width = Math::RoundUpFloat(dimensions.x - (open_inline_box->GetPosition().x + open_inline_box->GetBox().GetPosition(Box::CONTENT).x));
  262. // Flow the box's content into the line.
  263. UniquePtr<LayoutInlineBox> overflow_box = open_inline_box->FlowContent(first_box, last_box, available_width, right_spacing);
  264. box_cursor += open_inline_box->GetBox().GetSize().x;
  265. // If our box overflowed, then we'll close this line (as no more content will fit onto it) and tell our block box
  266. // to make a new line.
  267. if (overflow_box)
  268. {
  269. open_inline_box = open_inline_box->GetParent();
  270. return Close(std::move(overflow_box));
  271. }
  272. return open_inline_box;
  273. }
  274. // Adds an inline box as a chained hierarchy overflowing to this line.
  275. void LayoutLineBox::AddChainedBox(LayoutInlineBox* chained_box)
  276. {
  277. Stack< LayoutInlineBox* > hierarchy;
  278. LayoutInlineBox* chain = chained_box;
  279. while (chain != nullptr)
  280. {
  281. hierarchy.push(chain);
  282. chain = chain->GetParent();
  283. }
  284. while (!hierarchy.empty())
  285. {
  286. AddBox(MakeUnique<LayoutInlineBox>(hierarchy.top()));
  287. hierarchy.pop();
  288. }
  289. }
  290. // Returns the position of the line box, relative to its parent's block box's content area.
  291. Vector2f LayoutLineBox::GetPosition() const
  292. {
  293. return position;
  294. }
  295. // Returns the position of the line box, relative to its parent's block box's offset parent.
  296. Vector2f LayoutLineBox::GetRelativePosition() const
  297. {
  298. return position - (parent->GetOffsetParent()->GetPosition() - parent->GetOffsetRoot()->GetPosition());
  299. }
  300. // Returns the dimensions of the line box.
  301. Vector2f LayoutLineBox::GetDimensions() const
  302. {
  303. return dimensions;
  304. }
  305. // Returns the line box's open inline box.
  306. LayoutInlineBox* LayoutLineBox::GetOpenInlineBox()
  307. {
  308. return open_inline_box;
  309. }
  310. // Returns the line's containing block box.
  311. LayoutBlockBox* LayoutLineBox::GetBlockBox()
  312. {
  313. return parent;
  314. }
  315. float LayoutLineBox::GetBoxCursor() const
  316. {
  317. return box_cursor;
  318. }
  319. bool LayoutLineBox::GetBaselineOfLastLine(float& baseline) const
  320. {
  321. if (inline_boxes.empty())
  322. return false;
  323. baseline = inline_boxes.back()->GetBaseline();
  324. return true;
  325. }
  326. void* LayoutLineBox::operator new(size_t size)
  327. {
  328. return LayoutEngine::AllocateLayoutChunk(size);
  329. }
  330. void LayoutLineBox::operator delete(void* chunk, size_t size)
  331. {
  332. LayoutEngine::DeallocateLayoutChunk(chunk, size);
  333. }
  334. // Appends an inline box to the end of the line box's list of inline boxes.
  335. LayoutInlineBox* LayoutLineBox::AppendBox(UniquePtr<LayoutInlineBox> box_ptr)
  336. {
  337. LayoutInlineBox* box = box_ptr.get();
  338. inline_boxes.push_back(std::move(box_ptr));
  339. box->SetParent(open_inline_box);
  340. box->SetLine(this);
  341. box->SetHorizontalPosition(box_cursor + box->GetBox().GetEdge(Box::MARGIN, Box::LEFT));
  342. box_cursor += GetSpacing(box->GetBox(), Box::LEFT);
  343. open_inline_box = box;
  344. return box;
  345. }
  346. } // namespace Rml