LayoutInlineBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "LayoutInlineBox.h"
  29. #include "FontFaceHandle.h"
  30. #include "LayoutBlockBox.h"
  31. #include "LayoutEngine.h"
  32. #include "../../Include/Rocket/Core/ElementText.h"
  33. #include "../../Include/Rocket/Core/ElementUtilities.h"
  34. #include "../../Include/Rocket/Core/Property.h"
  35. #include "../../Include/Rocket/Core/StyleSheetKeywords.h"
  36. namespace Rocket {
  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 = NULL;
  42. parent = NULL;
  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 != NULL)
  56. {
  57. height = (float) ElementUtilities::GetLineHeight(element);
  58. baseline = (height - font_face->GetLineHeight()) * 0.5f + font_face->GetBaseline();
  59. }
  60. else
  61. {
  62. height = 0;
  63. baseline = 0;
  64. }
  65. }
  66. const Property* property = element->GetVerticalAlignProperty();
  67. if (property->unit == Property::KEYWORD)
  68. vertical_align_property = property->value.Get< int >();
  69. else
  70. vertical_align_property = -1;
  71. chained = false;
  72. chain = NULL;
  73. }
  74. // Constructs a new inline box for a split box.
  75. LayoutInlineBox::LayoutInlineBox(LayoutInlineBox* _chain) : position(0, 0), box(_chain->GetBox())
  76. {
  77. line = NULL;
  78. parent = NULL;
  79. element = _chain->element;
  80. width = 0;
  81. height = _chain->height;
  82. baseline = _chain->baseline;
  83. vertical_align_property = _chain->vertical_align_property;
  84. _chain->chain = this;
  85. chain = NULL;
  86. chained = true;
  87. // As we're a split box, our left side is cleared and content set back to (-1, -1).
  88. box.SetEdge(Box::PADDING, Box::LEFT, 0);
  89. box.SetEdge(Box::BORDER, Box::LEFT, 0);
  90. box.SetEdge(Box::MARGIN, Box::LEFT, 0);
  91. box.SetContent(Vector2f(-1, -1));
  92. }
  93. LayoutInlineBox::~LayoutInlineBox()
  94. {
  95. }
  96. // Sets the inline box's line.
  97. void LayoutInlineBox::SetLine(LayoutLineBox* _line)
  98. {
  99. line = _line;
  100. }
  101. // Sets the inline box's parent.
  102. void LayoutInlineBox::SetParent(LayoutInlineBox* _parent)
  103. {
  104. parent = _parent;
  105. if (parent != NULL)
  106. parent->children.push_back(this);
  107. }
  108. // Closes the box.
  109. void LayoutInlineBox::Close()
  110. {
  111. if (chain)
  112. chain->Close();
  113. else
  114. {
  115. ROCKET_ASSERT(line != NULL);
  116. line->CloseInlineBox(this);
  117. }
  118. }
  119. // Returns true if this box needs to close on its line.
  120. bool LayoutInlineBox::CanOverflow() const
  121. {
  122. return box.GetSize().x < 0;
  123. }
  124. // Returns true if this box's element is the last child of its parent.
  125. bool LayoutInlineBox::IsLastChild() const
  126. {
  127. Element* parent = element->GetParentNode();
  128. if (parent == NULL)
  129. return true;
  130. return parent->GetLastChild() == element;
  131. }
  132. // Flows the inline box's content into its parent line.
  133. LayoutInlineBox* LayoutInlineBox::FlowContent(bool ROCKET_UNUSED_PARAMETER(first_box), float ROCKET_UNUSED_PARAMETER(available_width), float ROCKET_UNUSED_PARAMETER(right_spacing_width))
  134. {
  135. ROCKET_UNUSED(first_box);
  136. ROCKET_UNUSED(available_width);
  137. ROCKET_UNUSED(right_spacing_width);
  138. // If we're representing a sized element, then add our element's width onto our parent's.
  139. if (parent != NULL &&
  140. box.GetSize().x > 0)
  141. parent->width += box.GetSize(Box::MARGIN).x;
  142. // Nothing else to do here; static elements will automatically be 'flowed' into their lines when they are placed.
  143. return NULL;
  144. }
  145. // Computes and sets the vertical position of this element, relative to its parent box.
  146. void LayoutInlineBox::CalculateBaseline(float& ascender, float& descender)
  147. {
  148. // We're vertically-aligned with one of the standard types.
  149. switch (vertical_align_property)
  150. {
  151. // Aligned with our parent box's baseline, our relative vertical position is set to 0.
  152. case VERTICAL_ALIGN_BASELINE:
  153. {
  154. SetVerticalPosition(0);
  155. }
  156. break;
  157. // The middle of this box is aligned with the baseline of its parent's plus half an ex.
  158. case VERTICAL_ALIGN_MIDDLE:
  159. {
  160. FontFaceHandle* parent_font = GetParentFont();
  161. int x_height = 0;
  162. if (parent_font != NULL)
  163. x_height = parent_font->GetXHeight() / -2;
  164. SetVerticalPosition(x_height + (height / 2 - baseline));
  165. }
  166. break;
  167. // This box's baseline is offset from its parent's so it is appropriate for rendering subscript.
  168. case VERTICAL_ALIGN_SUB:
  169. {
  170. FontFaceHandle* parent_font = GetParentFont();
  171. if (parent_font == NULL)
  172. SetVerticalPosition(0);
  173. else
  174. SetVerticalPosition(LayoutEngine::Round(parent_font->GetLineHeight() * 0.2f));
  175. }
  176. break;
  177. // This box's baseline is offset from its parent's so it is appropriate for rendering superscript.
  178. case VERTICAL_ALIGN_SUPER:
  179. {
  180. FontFaceHandle* parent_font = GetParentFont();
  181. if (parent_font == NULL)
  182. SetVerticalPosition(0);
  183. else
  184. SetVerticalPosition(-1 * LayoutEngine::Round(parent_font->GetLineHeight() * 0.4f));
  185. }
  186. break;
  187. // The top of this box is aligned to the top of its parent's font.
  188. case VERTICAL_ALIGN_TEXT_TOP:
  189. {
  190. FontFaceHandle* parent_font = GetParentFont();
  191. if (parent_font == NULL)
  192. SetVerticalPosition(0);
  193. else
  194. SetVerticalPosition((height - baseline) - (parent_font->GetLineHeight() - parent_font->GetBaseline()));
  195. }
  196. break;
  197. // The bottom of this box is aligned to the bottom of its parent's font (not the baseline).
  198. case VERTICAL_ALIGN_TEXT_BOTTOM:
  199. {
  200. FontFaceHandle* parent_font = GetParentFont();
  201. if (parent_font == NULL)
  202. SetVerticalPosition(0);
  203. else
  204. SetVerticalPosition(parent_font->GetBaseline() - baseline);
  205. }
  206. break;
  207. // This box is aligned with the line box, not an inline box, so we can't position it yet.
  208. case VERTICAL_ALIGN_TOP:
  209. case VERTICAL_ALIGN_BOTTOM:
  210. break;
  211. // The baseline of this box is offset by a fixed amount from its parent's baseline.
  212. default:
  213. {
  214. SetVerticalPosition(-1 * element->ResolveProperty(element->GetVerticalAlignProperty(), GetParentLineHeight()));
  215. }
  216. break;
  217. }
  218. // Set the ascender and descender relative to this element. If we're an unsized element (span, em, etc) then we
  219. // have no dimensions ourselves.
  220. if (box.GetSize() == Vector2f(-1, -1))
  221. {
  222. ascender = 0;
  223. descender = 0;
  224. }
  225. else
  226. {
  227. ascender = height - baseline;
  228. descender = height - ascender;
  229. }
  230. for (size_t i = 0; i < children.size(); ++i)
  231. {
  232. // Don't include any of our children that are aligned relative to the line box; the line box treats them
  233. // separately.
  234. if (children[i]->GetVerticalAlignProperty() != VERTICAL_ALIGN_TOP &&
  235. children[i]->GetVerticalAlignProperty() != VERTICAL_ALIGN_BOTTOM)
  236. {
  237. float child_ascender, child_descender;
  238. children[i]->CalculateBaseline(child_ascender, child_descender);
  239. ascender = Math::Max(ascender, child_ascender - children[i]->GetPosition().y);
  240. descender = Math::Max(descender, child_descender + children[i]->GetPosition().y);
  241. }
  242. }
  243. }
  244. // Offsets the baseline of this box, and all of its children, by the ascender of the parent line box.
  245. void LayoutInlineBox::OffsetBaseline(float ascender)
  246. {
  247. for (size_t i = 0; i < children.size(); ++i)
  248. {
  249. // Don't offset any of our children that are aligned relative to the line box; the line box will take care of
  250. // them separately.
  251. if (children[i]->GetVerticalAlignProperty() != VERTICAL_ALIGN_TOP &&
  252. children[i]->GetVerticalAlignProperty() != VERTICAL_ALIGN_BOTTOM)
  253. children[i]->OffsetBaseline(ascender + position.y);
  254. }
  255. position.y += (ascender - (height - baseline));
  256. }
  257. // Returns the inline box's offset from its parent's content area.
  258. const Vector2f& LayoutInlineBox::GetPosition() const
  259. {
  260. return position;
  261. }
  262. // Sets the inline box's relative horizontal offset from its parent's content area.
  263. void LayoutInlineBox::SetHorizontalPosition(float _position)
  264. {
  265. position.x = _position;
  266. }
  267. // Sets the inline box's relative vertical offset from its parent's content area.
  268. void LayoutInlineBox::SetVerticalPosition(float _position)
  269. {
  270. position.y = _position;
  271. }
  272. void LayoutInlineBox::PositionElement()
  273. {
  274. if (box.GetSize() == Vector2f(-1, -1))
  275. {
  276. // If this unsised element has any top margins, border or padding, then shift the position up so the borders
  277. // and background will render in the right place.
  278. position.y -= box.GetCumulativeEdge(Box::CONTENT, Box::TOP);
  279. }
  280. // Otherwise; we're a sized element (replaced or inline-block), so we need to offset our element's vertical
  281. // position by our top margin (as the origin of an element is the top-left of the border, not the margin).
  282. else
  283. position.y += box.GetEdge(Box::MARGIN, Box::TOP);
  284. if (!chained)
  285. element->SetOffset(line->GetRelativePosition() + position, line->GetBlockBox()->GetOffsetParent()->GetElement());
  286. }
  287. // Sizes the inline box's element.
  288. void LayoutInlineBox::SizeElement(bool split)
  289. {
  290. // Resize the box for an unsized inline element.
  291. if (box.GetSize() == Vector2f(-1, -1))
  292. {
  293. box.SetContent(Vector2f(width, (float) ElementUtilities::GetLineHeight(element)));
  294. if (parent != NULL)
  295. parent->width += width;
  296. }
  297. Box element_box = box;
  298. if (split)
  299. {
  300. element_box.SetEdge(Box::MARGIN, Box::RIGHT, 0);
  301. element_box.SetEdge(Box::BORDER, Box::RIGHT, 0);
  302. element_box.SetEdge(Box::PADDING, Box::RIGHT, 0);
  303. }
  304. // The elements of a chained box have already had their positions set by the first link.
  305. if (chained)
  306. {
  307. element_box.SetOffset((line->GetPosition() + position) - element->GetRelativeOffset(Box::BORDER));
  308. element->AddBox(element_box);
  309. if (chain != NULL)
  310. element->OnLayout();
  311. }
  312. else
  313. {
  314. element->SetBox(element_box);
  315. element->OnLayout();
  316. }
  317. }
  318. // Returns the vertical align property of the box's element.
  319. int LayoutInlineBox::GetVerticalAlignProperty() const
  320. {
  321. return vertical_align_property;
  322. }
  323. // Returns the inline box's element.
  324. Element* LayoutInlineBox::GetElement()
  325. {
  326. return element;
  327. }
  328. // Returns the inline box's parent.
  329. LayoutInlineBox* LayoutInlineBox::GetParent()
  330. {
  331. return parent;
  332. }
  333. // Returns the inline box's dimension box.
  334. const Box& LayoutInlineBox::GetBox() const
  335. {
  336. return box;
  337. }
  338. // Returns the height of the inline box.
  339. float LayoutInlineBox::GetHeight() const
  340. {
  341. return height;
  342. }
  343. // Returns the baseline of the inline box.
  344. float LayoutInlineBox::GetBaseline() const
  345. {
  346. return baseline;
  347. }
  348. void* LayoutInlineBox::operator new(size_t size)
  349. {
  350. return LayoutEngine::AllocateLayoutChunk(size);
  351. }
  352. void LayoutInlineBox::operator delete(void* chunk)
  353. {
  354. LayoutEngine::DeallocateLayoutChunk(chunk);
  355. }
  356. // Returns our parent box's font face handle.
  357. FontFaceHandle* LayoutInlineBox::GetParentFont() const
  358. {
  359. if (parent == NULL)
  360. return line->GetBlockBox()->GetParent()->GetElement()->GetFontFaceHandle();
  361. else
  362. return parent->GetElement()->GetFontFaceHandle();
  363. }
  364. // Returns our parent box's line height.
  365. float LayoutInlineBox::GetParentLineHeight() const
  366. {
  367. if (parent == NULL)
  368. return (float) ElementUtilities::GetLineHeight(line->GetBlockBox()->GetParent()->GetElement());
  369. else
  370. return (float) ElementUtilities::GetLineHeight(parent->GetElement());
  371. }
  372. }
  373. }