LayoutInlineBox.cpp 12 KB

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