LayoutInlineBox.cpp 12 KB

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