LayoutInlineBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/ElementText.h"
  32. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  33. #include "../../Include/RmlUi/Core/FontEngineInterface.h"
  34. #include "../../Include/RmlUi/Core/Property.h"
  35. #include "LayoutBlockBox.h"
  36. #include "LayoutEngine.h"
  37. namespace Rml {
  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 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. UniquePtr<LayoutInlineBox> LayoutInlineBox::FlowContent(bool /*first_box*/, bool /*last_box*/, float /*available_width*/,
  129. float /*right_spacing_width*/)
  130. {
  131. // If we're representing a sized element, then add our element's width onto our parent's.
  132. if (parent != nullptr &&
  133. box.GetSize().x > 0)
  134. parent->width += box.GetSize(Box::MARGIN).x;
  135. // Nothing else to do here; static elements will automatically be 'flowed' into their lines when they are placed.
  136. return nullptr;
  137. }
  138. // Computes and sets the vertical position of this element, relative to its parent box.
  139. void LayoutInlineBox::CalculateBaseline(float& ascender, float& descender)
  140. {
  141. using namespace Style;
  142. // We're vertically-aligned with one of the standard types.
  143. switch (vertical_align_property.type)
  144. {
  145. // Aligned with our parent box's baseline, our relative vertical position is set to 0.
  146. case VerticalAlign::Baseline:
  147. {
  148. SetVerticalPosition(0);
  149. }
  150. break;
  151. // The middle of this box is aligned with the baseline of its parent's plus half an ex.
  152. case VerticalAlign::Middle:
  153. {
  154. FontFaceHandle parent_font = GetParentFont();
  155. int x_height = 0;
  156. if (parent_font != 0)
  157. x_height = GetFontEngineInterface()->GetXHeight(parent_font) / -2;
  158. SetVerticalPosition(x_height + (height / 2 - baseline));
  159. }
  160. break;
  161. // This box's baseline is offset from its parent's so it is appropriate for rendering subscript.
  162. case VerticalAlign::Sub:
  163. {
  164. FontFaceHandle parent_font = GetParentFont();
  165. if (parent_font == 0)
  166. SetVerticalPosition(0);
  167. else
  168. SetVerticalPosition(float(GetFontEngineInterface()->GetLineHeight(parent_font)) * 0.2f);
  169. }
  170. break;
  171. // This box's baseline is offset from its parent's so it is appropriate for rendering superscript.
  172. case VerticalAlign::Super:
  173. {
  174. FontFaceHandle parent_font = GetParentFont();
  175. if (parent_font == 0)
  176. SetVerticalPosition(0);
  177. else
  178. SetVerticalPosition(float(-1 * GetFontEngineInterface()->GetLineHeight(parent_font)) * 0.4f);
  179. }
  180. break;
  181. // The top of this box is aligned to the top of its parent's font.
  182. case VerticalAlign::TextTop:
  183. {
  184. FontFaceHandle parent_font = GetParentFont();
  185. if (parent_font == 0)
  186. SetVerticalPosition(0);
  187. else
  188. SetVerticalPosition((height - baseline) - (GetFontEngineInterface()->GetLineHeight(parent_font) - GetFontEngineInterface()->GetBaseline(parent_font)));
  189. }
  190. break;
  191. // The bottom of this box is aligned to the bottom of its parent's font (not the baseline).
  192. case VerticalAlign::TextBottom:
  193. {
  194. FontFaceHandle parent_font = GetParentFont();
  195. if (parent_font == 0)
  196. SetVerticalPosition(0);
  197. else
  198. SetVerticalPosition(GetFontEngineInterface()->GetBaseline(parent_font) - baseline);
  199. }
  200. break;
  201. // This box is aligned with the line box, not an inline box, so we can't position it yet.
  202. case VerticalAlign::Top:
  203. case VerticalAlign::Bottom: break;
  204. // The baseline of this box is offset by a fixed amount from its parent's baseline.
  205. case VerticalAlign::Length: SetVerticalPosition(-1.f * vertical_align_property.value); break;
  206. }
  207. // Set the ascender and descender relative to this element. If we're an unsized element (span, em, etc) then we
  208. // have no dimensions ourselves.
  209. if (box.GetSize() == Vector2f(-1, -1))
  210. {
  211. ascender = 0;
  212. descender = 0;
  213. }
  214. else
  215. {
  216. ascender = height - baseline;
  217. descender = height - ascender;
  218. }
  219. for (size_t i = 0; i < children.size(); ++i)
  220. {
  221. // Don't include any of our children that are aligned relative to the line box; the line box treats them
  222. // separately.
  223. if (children[i]->GetVerticalAlignProperty().type != Style::VerticalAlign::Top &&
  224. children[i]->GetVerticalAlignProperty().type != Style::VerticalAlign::Bottom)
  225. {
  226. float child_ascender, child_descender;
  227. children[i]->CalculateBaseline(child_ascender, child_descender);
  228. ascender = Math::Max(ascender, child_ascender - children[i]->GetPosition().y);
  229. descender = Math::Max(descender, child_descender + children[i]->GetPosition().y);
  230. }
  231. }
  232. }
  233. // Offsets the baseline of this box, and all of its children, by the ascender of the parent line box.
  234. void LayoutInlineBox::OffsetBaseline(float ascender)
  235. {
  236. for (size_t i = 0; i < children.size(); ++i)
  237. {
  238. // Don't offset any of our children that are aligned relative to the line box; the line box will take care of
  239. // them separately.
  240. if (children[i]->GetVerticalAlignProperty().type != Style::VerticalAlign::Top &&
  241. children[i]->GetVerticalAlignProperty().type != Style::VerticalAlign::Bottom)
  242. children[i]->OffsetBaseline(ascender + position.y);
  243. }
  244. position.y += (ascender - (height - baseline));
  245. }
  246. // Returns the inline box's offset from its parent's content area.
  247. Vector2f LayoutInlineBox::GetPosition() const
  248. {
  249. return position;
  250. }
  251. // Sets the inline box's relative horizontal offset from its parent's content area.
  252. void LayoutInlineBox::SetHorizontalPosition(float _position)
  253. {
  254. position.x = _position;
  255. }
  256. // Sets the inline box's relative vertical offset from its parent's content area.
  257. void LayoutInlineBox::SetVerticalPosition(float _position)
  258. {
  259. position.y = _position;
  260. }
  261. void LayoutInlineBox::PositionElement()
  262. {
  263. if (box.GetSize() == Vector2f(-1, -1))
  264. {
  265. // If this unsised element has any top margins, border or padding, then shift the position up so the borders
  266. // and background will render in the right place.
  267. position.y -= box.GetCumulativeEdge(Box::CONTENT, Box::TOP);
  268. }
  269. // Otherwise; we're a sized element (replaced or inline-block), so we need to offset our element's vertical
  270. // position by our top margin (as the origin of an element is the top-left of the border, not the margin).
  271. else
  272. position.y += box.GetEdge(Box::MARGIN, Box::TOP);
  273. if (!chained)
  274. element->SetOffset(line->GetRelativePosition() + position, line->GetBlockBox()->GetOffsetParent()->GetElement());
  275. }
  276. // Sizes the inline box's element.
  277. void LayoutInlineBox::SizeElement(bool split)
  278. {
  279. // Resize the box for an unsized inline element.
  280. if (box.GetSize() == Vector2f(-1, -1))
  281. {
  282. box.SetContent(Vector2f(width, element->GetLineHeight()));
  283. if (parent != nullptr)
  284. parent->width += width;
  285. }
  286. Box element_box = box;
  287. if (split)
  288. {
  289. element_box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  290. element_box.SetEdge(Box::BORDER, Box::RIGHT, 0);
  291. element_box.SetEdge(Box::PADDING, Box::RIGHT, 0);
  292. }
  293. // The elements of a chained box have already had their positions set by the first link.
  294. if (chained)
  295. {
  296. const Vector2f box_offset = (line->GetPosition() + position) - element->GetRelativeOffset(Box::BORDER);
  297. element->AddBox(element_box, box_offset);
  298. if (chain != nullptr)
  299. element->OnLayout();
  300. }
  301. else
  302. {
  303. element->SetBox(element_box);
  304. element->OnLayout();
  305. }
  306. }
  307. // Returns the vertical align property of the box's element.
  308. Style::VerticalAlign LayoutInlineBox::GetVerticalAlignProperty() const
  309. {
  310. return vertical_align_property;
  311. }
  312. // Returns the inline box's element.
  313. Element* LayoutInlineBox::GetElement()
  314. {
  315. return element;
  316. }
  317. // Returns the inline box's parent.
  318. LayoutInlineBox* LayoutInlineBox::GetParent()
  319. {
  320. return parent;
  321. }
  322. // Returns the inline box's dimension box.
  323. const Box& LayoutInlineBox::GetBox() const
  324. {
  325. return box;
  326. }
  327. // Returns the height of the inline box.
  328. float LayoutInlineBox::GetHeight() const
  329. {
  330. return height;
  331. }
  332. // Returns the baseline of the inline box.
  333. float LayoutInlineBox::GetBaseline() const
  334. {
  335. return baseline;
  336. }
  337. void* LayoutInlineBox::operator new(size_t size)
  338. {
  339. return LayoutEngine::AllocateLayoutChunk(size);
  340. }
  341. void LayoutInlineBox::operator delete(void* chunk, size_t size)
  342. {
  343. LayoutEngine::DeallocateLayoutChunk(chunk, size);
  344. }
  345. // Returns our parent box's font face handle.
  346. FontFaceHandle LayoutInlineBox::GetParentFont() const
  347. {
  348. if (parent == nullptr)
  349. return line->GetBlockBox()->GetParent()->GetElement()->GetFontFaceHandle();
  350. else
  351. return parent->GetElement()->GetFontFaceHandle();
  352. }
  353. } // namespace Rml