LayoutInlineBox.cpp 12 KB

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