LayoutInlineBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "LayoutInlineBox.h"
  29. #include "LayoutBlockBox.h"
  30. #include "LayoutEngine.h"
  31. #include "../../Include/RmlUi/Core/Core.h"
  32. #include "../../Include/RmlUi/Core/ElementText.h"
  33. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  34. #include "../../Include/RmlUi/Core/FontEngineInterface.h"
  35. #include "../../Include/RmlUi/Core/Property.h"
  36. namespace Rml {
  37. // Constructs a new inline box for an element.
  38. LayoutInlineBox::LayoutInlineBox(Element* _element, const Box& _box) : position(0, 0), box(_box)
  39. {
  40. line = nullptr;
  41. parent = nullptr;
  42. element = _element;
  43. width = 0;
  44. // If this box has intrinsic dimensions, then we set our height to the total height of the element; otherwise, it is zero height.
  45. if (box.GetSize().y > 0)
  46. {
  47. height = box.GetSize(Box::MARGIN).y;
  48. baseline = element->GetBaseline() + box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM);
  49. }
  50. else
  51. {
  52. FontFaceHandle font_face = element->GetFontFaceHandle();
  53. if (font_face != 0)
  54. {
  55. height = element->GetLineHeight();
  56. baseline = (height - GetFontEngineInterface()->GetLineHeight(font_face)) * 0.5f + GetFontEngineInterface()->GetBaseline(font_face);
  57. }
  58. else
  59. {
  60. height = 0;
  61. baseline = 0;
  62. }
  63. }
  64. vertical_align_property = element->GetComputedValues().vertical_align;
  65. chained = false;
  66. chain = nullptr;
  67. }
  68. // Constructs a new inline box for a split box.
  69. LayoutInlineBox::LayoutInlineBox(LayoutInlineBox* _chain) : position(0, 0), box(_chain->GetBox())
  70. {
  71. line = nullptr;
  72. parent = nullptr;
  73. element = _chain->element;
  74. width = 0;
  75. height = _chain->height;
  76. baseline = _chain->baseline;
  77. vertical_align_property = _chain->vertical_align_property;
  78. _chain->chain = this;
  79. chain = nullptr;
  80. chained = true;
  81. // As we're a split box, our left side is cleared and content set back to (-1, -1).
  82. box.SetEdge(Box::PADDING, Box::LEFT, 0);
  83. box.SetEdge(Box::BORDER, Box::LEFT, 0);
  84. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  85. box.SetContent(Vector2f(-1, -1));
  86. }
  87. LayoutInlineBox::~LayoutInlineBox()
  88. {
  89. }
  90. // Sets the inline box's line.
  91. void LayoutInlineBox::SetLine(LayoutLineBox* _line)
  92. {
  93. line = _line;
  94. }
  95. // Sets the inline box's parent.
  96. void LayoutInlineBox::SetParent(LayoutInlineBox* _parent)
  97. {
  98. parent = _parent;
  99. if (parent != nullptr)
  100. parent->children.push_back(this);
  101. }
  102. // Closes the box.
  103. void LayoutInlineBox::Close()
  104. {
  105. if (chain)
  106. chain->Close();
  107. else
  108. {
  109. RMLUI_ASSERT(line != nullptr);
  110. line->CloseInlineBox(this);
  111. }
  112. }
  113. // Returns true if this box needs to close on its line.
  114. bool LayoutInlineBox::CanOverflow() const
  115. {
  116. return box.GetSize().x < 0;
  117. }
  118. // Returns true if this box's element is the last child of its parent.
  119. bool LayoutInlineBox::IsLastChild() const
  120. {
  121. Element* parent = element->GetParentNode();
  122. if (parent == nullptr)
  123. return true;
  124. return parent->GetLastChild() == element;
  125. }
  126. // Flows the inline box's content into its parent line.
  127. UniquePtr<LayoutInlineBox> LayoutInlineBox::FlowContent(bool RMLUI_UNUSED_PARAMETER(first_box), float RMLUI_UNUSED_PARAMETER(available_width), float RMLUI_UNUSED_PARAMETER(right_spacing_width))
  128. {
  129. RMLUI_UNUSED(first_box);
  130. RMLUI_UNUSED(available_width);
  131. RMLUI_UNUSED(right_spacing_width);
  132. // If we're representing a sized element, then add our element's width onto our parent's.
  133. if (parent != nullptr &&
  134. box.GetSize().x > 0)
  135. parent->width += box.GetSize(Box::MARGIN).x;
  136. // Nothing else to do here; static elements will automatically be 'flowed' into their lines when they are placed.
  137. return nullptr;
  138. }
  139. // Computes and sets the vertical position of this element, relative to its parent box.
  140. void LayoutInlineBox::CalculateBaseline(float& ascender, float& descender)
  141. {
  142. using namespace Style;
  143. // We're vertically-aligned with one of the standard types.
  144. switch (vertical_align_property.type)
  145. {
  146. // Aligned with our parent box's baseline, our relative vertical position is set to 0.
  147. case VerticalAlign::Baseline:
  148. {
  149. SetVerticalPosition(0);
  150. }
  151. break;
  152. // The middle of this box is aligned with the baseline of its parent's plus half an ex.
  153. case VerticalAlign::Middle:
  154. {
  155. FontFaceHandle parent_font = GetParentFont();
  156. int x_height = 0;
  157. if (parent_font != 0)
  158. x_height = GetFontEngineInterface()->GetXHeight(parent_font) / -2;
  159. SetVerticalPosition(x_height + (height / 2 - baseline));
  160. }
  161. break;
  162. // This box's baseline is offset from its parent's so it is appropriate for rendering subscript.
  163. case VerticalAlign::Sub:
  164. {
  165. FontFaceHandle parent_font = GetParentFont();
  166. if (parent_font == 0)
  167. SetVerticalPosition(0);
  168. else
  169. SetVerticalPosition(float(GetFontEngineInterface()->GetLineHeight(parent_font)) * 0.2f);
  170. }
  171. break;
  172. // This box's baseline is offset from its parent's so it is appropriate for rendering superscript.
  173. case VerticalAlign::Super:
  174. {
  175. FontFaceHandle parent_font = GetParentFont();
  176. if (parent_font == 0)
  177. SetVerticalPosition(0);
  178. else
  179. SetVerticalPosition(float(-1 * GetFontEngineInterface()->GetLineHeight(parent_font)) * 0.4f);
  180. }
  181. break;
  182. // The top of this box is aligned to the top of its parent's font.
  183. case VerticalAlign::TextTop:
  184. {
  185. FontFaceHandle parent_font = GetParentFont();
  186. if (parent_font == 0)
  187. SetVerticalPosition(0);
  188. else
  189. SetVerticalPosition((height - baseline) - (GetFontEngineInterface()->GetLineHeight(parent_font) - GetFontEngineInterface()->GetBaseline(parent_font)));
  190. }
  191. break;
  192. // The bottom of this box is aligned to the bottom of its parent's font (not the baseline).
  193. case VerticalAlign::TextBottom:
  194. {
  195. FontFaceHandle parent_font = GetParentFont();
  196. if (parent_font == 0)
  197. SetVerticalPosition(0);
  198. else
  199. SetVerticalPosition(GetFontEngineInterface()->GetBaseline(parent_font) - baseline);
  200. }
  201. break;
  202. // This box is aligned with the line box, not an inline box, so we can't position it yet.
  203. case VerticalAlign::Top:
  204. case VerticalAlign::Bottom:
  205. break;
  206. // The baseline of this box is offset by a fixed amount from its parent's baseline.
  207. case VerticalAlign::Length:
  208. default:
  209. {
  210. SetVerticalPosition(-1.f * vertical_align_property.value);
  211. }
  212. break;
  213. }
  214. // Set the ascender and descender relative to this element. If we're an unsized element (span, em, etc) then we
  215. // have no dimensions ourselves.
  216. if (box.GetSize() == Vector2f(-1, -1))
  217. {
  218. ascender = 0;
  219. descender = 0;
  220. }
  221. else
  222. {
  223. ascender = height - baseline;
  224. descender = height - ascender;
  225. }
  226. for (size_t i = 0; i < children.size(); ++i)
  227. {
  228. // Don't include any of our children that are aligned relative to the line box; the line box treats them
  229. // separately.
  230. if (children[i]->GetVerticalAlignProperty().type != Style::VerticalAlign::Top &&
  231. children[i]->GetVerticalAlignProperty().type != Style::VerticalAlign::Bottom)
  232. {
  233. float child_ascender, child_descender;
  234. children[i]->CalculateBaseline(child_ascender, child_descender);
  235. ascender = Math::Max(ascender, child_ascender - children[i]->GetPosition().y);
  236. descender = Math::Max(descender, child_descender + children[i]->GetPosition().y);
  237. }
  238. }
  239. }
  240. // Offsets the baseline of this box, and all of its children, by the ascender of the parent line box.
  241. void LayoutInlineBox::OffsetBaseline(float ascender)
  242. {
  243. for (size_t i = 0; i < children.size(); ++i)
  244. {
  245. // Don't offset any of our children that are aligned relative to the line box; the line box will take care of
  246. // them separately.
  247. if (children[i]->GetVerticalAlignProperty().type != Style::VerticalAlign::Top &&
  248. children[i]->GetVerticalAlignProperty().type != Style::VerticalAlign::Bottom)
  249. children[i]->OffsetBaseline(ascender + position.y);
  250. }
  251. position.y += (ascender - (height - baseline));
  252. }
  253. // Returns the inline box's offset from its parent's content area.
  254. const Vector2f& LayoutInlineBox::GetPosition() const
  255. {
  256. return position;
  257. }
  258. // Sets the inline box's relative horizontal offset from its parent's content area.
  259. void LayoutInlineBox::SetHorizontalPosition(float _position)
  260. {
  261. position.x = _position;
  262. }
  263. // Sets the inline box's relative vertical offset from its parent's content area.
  264. void LayoutInlineBox::SetVerticalPosition(float _position)
  265. {
  266. position.y = _position;
  267. }
  268. void LayoutInlineBox::PositionElement()
  269. {
  270. if (box.GetSize() == Vector2f(-1, -1))
  271. {
  272. // If this unsised element has any top margins, border or padding, then shift the position up so the borders
  273. // and background will render in the right place.
  274. position.y -= box.GetCumulativeEdge(Box::CONTENT, Box::TOP);
  275. }
  276. // Otherwise; we're a sized element (replaced or inline-block), so we need to offset our element's vertical
  277. // position by our top margin (as the origin of an element is the top-left of the border, not the margin).
  278. else
  279. position.y += box.GetEdge(Box::MARGIN, Box::TOP);
  280. if (!chained)
  281. element->SetOffset(line->GetRelativePosition() + position, line->GetBlockBox()->GetOffsetParent()->GetElement());
  282. }
  283. // Sizes the inline box's element.
  284. void LayoutInlineBox::SizeElement(bool split)
  285. {
  286. // Resize the box for an unsized inline element.
  287. if (box.GetSize() == Vector2f(-1, -1))
  288. {
  289. box.SetContent(Vector2f(width, element->GetLineHeight()));
  290. if (parent != nullptr)
  291. parent->width += width;
  292. }
  293. Box element_box = box;
  294. if (split)
  295. {
  296. element_box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  297. element_box.SetEdge(Box::BORDER, Box::RIGHT, 0);
  298. element_box.SetEdge(Box::PADDING, Box::RIGHT, 0);
  299. }
  300. // The elements of a chained box have already had their positions set by the first link.
  301. if (chained)
  302. {
  303. const Vector2f box_offset = (line->GetPosition() + position) - element->GetRelativeOffset(Box::BORDER);
  304. element->AddBox(element_box, box_offset);
  305. if (chain != nullptr)
  306. element->OnLayout();
  307. }
  308. else
  309. {
  310. element->SetBox(element_box);
  311. element->OnLayout();
  312. }
  313. }
  314. // Returns the vertical align property of the box's element.
  315. Style::VerticalAlign LayoutInlineBox::GetVerticalAlignProperty() const
  316. {
  317. return vertical_align_property;
  318. }
  319. // Returns the inline box's element.
  320. Element* LayoutInlineBox::GetElement()
  321. {
  322. return element;
  323. }
  324. // Returns the inline box's parent.
  325. LayoutInlineBox* LayoutInlineBox::GetParent()
  326. {
  327. return parent;
  328. }
  329. // Returns the inline box's dimension box.
  330. const Box& LayoutInlineBox::GetBox() const
  331. {
  332. return box;
  333. }
  334. // Returns the height of the inline box.
  335. float LayoutInlineBox::GetHeight() const
  336. {
  337. return height;
  338. }
  339. // Returns the baseline of the inline box.
  340. float LayoutInlineBox::GetBaseline() const
  341. {
  342. return baseline;
  343. }
  344. void* LayoutInlineBox::operator new(size_t size)
  345. {
  346. return LayoutEngine::AllocateLayoutChunk(size);
  347. }
  348. void LayoutInlineBox::operator delete(void* chunk)
  349. {
  350. LayoutEngine::DeallocateLayoutChunk(chunk);
  351. }
  352. // Returns our parent box's font face handle.
  353. FontFaceHandle LayoutInlineBox::GetParentFont() const
  354. {
  355. if (parent == nullptr)
  356. return line->GetBlockBox()->GetParent()->GetElement()->GetFontFaceHandle();
  357. else
  358. return parent->GetElement()->GetFontFaceHandle();
  359. }
  360. } // namespace Rml