LayoutInlineBox.cpp 12 KB

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