LayoutLineBox.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 "LayoutLineBox.h"
  29. #include "LayoutBlockBox.h"
  30. #include "LayoutEngine.h"
  31. #include "LayoutInlineBoxText.h"
  32. #include "FontFaceHandle.h"
  33. #include "../../Include/Rocket/Core/Property.h"
  34. #include "../../Include/Rocket/Core/ElementUtilities.h"
  35. #include "../../Include/Rocket/Core/ElementText.h"
  36. #include "../../Include/Rocket/Core/StyleSheetKeywords.h"
  37. #include <stack>
  38. namespace Rocket {
  39. namespace Core {
  40. static float GetSpacing(const Box& box, Box::Edge edge)
  41. {
  42. return box.GetEdge(Box::PADDING, edge) +
  43. box.GetEdge(Box::BORDER, edge) +
  44. box.GetEdge(Box::MARGIN, edge);
  45. }
  46. LayoutLineBox::LayoutLineBox(LayoutBlockBox* _parent) : position(-1, -1), dimensions(-1, -1)
  47. {
  48. parent = _parent;
  49. box_cursor = 0;
  50. open_inline_box = NULL;
  51. position_set = false;
  52. wrap_content = false;
  53. }
  54. LayoutLineBox::~LayoutLineBox()
  55. {
  56. for (size_t i = 0; i < inline_boxes.size(); i++)
  57. delete inline_boxes[i];
  58. }
  59. // Closes the line box, positioning all inline elements within it.
  60. LayoutInlineBox* LayoutLineBox::Close(LayoutInlineBox* overflow)
  61. {
  62. // If we haven't positioned this line yet, and it has elements in it, then this is a great opportunity to do so.
  63. if (!position_set &&
  64. !inline_boxes.empty())
  65. {
  66. parent->PositionLineBox(position, dimensions.x, wrap_content, Vector2f(0, 0));
  67. dimensions.y = 0;
  68. position_set = true;
  69. }
  70. // If the line has been positioned and our content is greater than our original size (for example, if we aren't
  71. // wrapping or had to render a very long word), then we push our dimensions out to compensate.
  72. else
  73. dimensions.x = Math::Max(dimensions.x, box_cursor);
  74. // Now we calculate the baselines of each of our inline boxes relative to their parent box's baseline; either us,
  75. // or another of our inline boxes. The maximum distance each element is above and below our baseline is calculated
  76. // from that, and therefore our height.
  77. float ascender = 0;
  78. float descender = 0;
  79. float minimum_height = 0;
  80. for (size_t i = 0; i < inline_boxes.size(); ++i)
  81. {
  82. LayoutInlineBox* inline_box = inline_boxes[i];
  83. // Check if we've got an element aligned to the line box rather than a baseline.
  84. if (inline_box->GetVerticalAlignProperty() == VERTICAL_ALIGN_TOP ||
  85. inline_box->GetVerticalAlignProperty() == VERTICAL_ALIGN_BOTTOM)
  86. {
  87. // Get this element to calculate the baseline offsets of its children; it can't calculate its own baseline
  88. // because we don't know the height of the line box yet. We don't actually care about its ascender or
  89. // descender either, just its height.
  90. float box_ascender, box_descender;
  91. inline_box->CalculateBaseline(box_ascender, box_descender);
  92. minimum_height = Math::Max(minimum_height, inline_box->GetHeight());
  93. }
  94. // Otherwise, we have an element anchored to a baseline, so we can fetch its ascender and descender relative
  95. // to our baseline.
  96. else if (inline_box->GetParent() == NULL)
  97. {
  98. float box_ascender, box_descender;
  99. inline_box->CalculateBaseline(box_ascender, box_descender);
  100. ascender = Math::Max(ascender, box_ascender - inline_box->GetPosition().y);
  101. descender = Math::Max(descender, box_descender + inline_box->GetPosition().y);
  102. }
  103. }
  104. // We've now got the maximum ascender and descender, we can calculate the dimensions of the line box.
  105. dimensions.y = Math::Max(minimum_height, ascender + descender);
  106. // And from that, we can now set the final baseline of each box.
  107. for (size_t i = 0; i < inline_boxes.size(); ++i)
  108. {
  109. LayoutInlineBox* inline_box = inline_boxes[i];
  110. // Check again if this element is aligned to the line box. We don't need to worry about offsetting an element
  111. // tied to the top of the line box, as its position will always stay at exactly 0.
  112. if (inline_box->GetVerticalAlignProperty() == VERTICAL_ALIGN_TOP ||
  113. inline_box->GetVerticalAlignProperty() == VERTICAL_ALIGN_BOTTOM)
  114. {
  115. if (inline_box->GetVerticalAlignProperty() == VERTICAL_ALIGN_TOP)
  116. inline_box->OffsetBaseline(inline_box->GetHeight() - inline_box->GetBaseline());
  117. else
  118. inline_box->OffsetBaseline(dimensions.y - inline_box->GetBaseline());
  119. }
  120. // Otherwise, this element is tied to a baseline.
  121. else if (inline_box->GetParent() == NULL)
  122. inline_box->OffsetBaseline(ascender);
  123. }
  124. // Position all the boxes horizontally in the line. We only need to reposition the elements if they're set to
  125. // centre or right; the element are already placed left-aligned, and justification occurs at the text level.
  126. int text_align_property = parent->GetParent()->GetElement()->GetTextAlign();
  127. if (text_align_property == TEXT_ALIGN_CENTER ||
  128. text_align_property == TEXT_ALIGN_RIGHT)
  129. {
  130. float element_offset = 0;
  131. switch (text_align_property)
  132. {
  133. case TEXT_ALIGN_CENTER: element_offset = (dimensions.x - box_cursor) * 0.5f; break;
  134. case TEXT_ALIGN_RIGHT: element_offset = (dimensions.x - box_cursor); break;
  135. }
  136. if (element_offset != 0)
  137. {
  138. element_offset = LayoutEngine::Round(element_offset);
  139. for (size_t i = 0; i < inline_boxes.size(); i++)
  140. inline_boxes[i]->SetHorizontalPosition(inline_boxes[i]->GetPosition().x + element_offset);
  141. }
  142. }
  143. // Get each line box to set the position of their element, relative to their parents.
  144. for (int i = (int) inline_boxes.size() - 1; i >= 0; --i)
  145. {
  146. inline_boxes[i]->PositionElement();
  147. // Check if this inline box is part of the open box chain.
  148. bool inline_box_open = false;
  149. LayoutInlineBox* open_box = open_inline_box;
  150. while (open_box != NULL &&
  151. !inline_box_open)
  152. {
  153. if (inline_boxes[i] == open_box)
  154. inline_box_open = true;
  155. open_box = open_box->GetParent();
  156. }
  157. inline_boxes[i]->SizeElement(inline_box_open);
  158. }
  159. return parent->CloseLineBox(this, overflow, open_inline_box);
  160. }
  161. // Closes one of the line box's inline boxes.
  162. void LayoutLineBox::CloseInlineBox(LayoutInlineBox* inline_box)
  163. {
  164. ROCKET_ASSERT(open_inline_box == inline_box);
  165. open_inline_box = inline_box->GetParent();
  166. box_cursor += GetSpacing(inline_box->GetBox(), Box::RIGHT);
  167. }
  168. // Attempts to add a new element to this line box.
  169. LayoutInlineBox* LayoutLineBox::AddElement(Element* element, const Box& box)
  170. {
  171. if (dynamic_cast< ElementText* >(element) != NULL)
  172. return AddBox(new LayoutInlineBoxText(element));
  173. else
  174. return AddBox(new LayoutInlineBox(element, box));
  175. }
  176. // Attempts to add a new inline box to this line.
  177. LayoutInlineBox* LayoutLineBox::AddBox(LayoutInlineBox* box)
  178. {
  179. // Set to true if we're flowing the first box (with content) on the line.
  180. bool first_box = false;
  181. // The spacing this element must leave on the right of the line, to account not only for its margins and padding,
  182. // but also for its parents which will close immediately after it.
  183. float right_spacing;
  184. // If this line is unplaced, then this is the first inline box; if it is sized, then we can place and size this
  185. // line.
  186. if (!position_set)
  187. {
  188. // Add the new box to the list of boxes in the line box. As this line box has not been placed, we don't have to
  189. // check if it can fit yet.
  190. AppendBox(box);
  191. // If the new box has a physical prescence, then we must place this line once we've figured out how wide it has to
  192. // be.
  193. if (box->GetBox().GetSize().x >= 0)
  194. {
  195. // Calculate the dimensions for the box we need to fit.
  196. Vector2f minimum_dimensions = box->GetBox().GetSize();
  197. // Add the width of any empty, already closed tags, or still opened spaced tags.
  198. minimum_dimensions.x += box_cursor;
  199. // Calculate the right spacing for the element.
  200. right_spacing = GetSpacing(box->GetBox(), Box::RIGHT);
  201. // Add the right spacing for any ancestor elements that must close immediately after it.
  202. LayoutInlineBox* closing_box = box;
  203. while (closing_box != NULL &&
  204. closing_box->IsLastChild())
  205. {
  206. closing_box = closing_box->GetParent();
  207. if (closing_box != NULL)
  208. right_spacing += GetSpacing(closing_box->GetBox(), Box::RIGHT);
  209. }
  210. if (!box->CanOverflow())
  211. minimum_dimensions.x += right_spacing;
  212. parent->PositionLineBox(position, dimensions.x, wrap_content, minimum_dimensions);
  213. dimensions.y = minimum_dimensions.y;
  214. first_box = true;
  215. position_set = true;
  216. }
  217. else
  218. return box;
  219. }
  220. // This line has already been placed and sized, so we'll check if we can fit this new inline box on the line.
  221. else
  222. {
  223. // Build up the spacing required on the right side of this element. This consists of the right spacing on the
  224. // new element, and the right spacing on all parent element that will close next.
  225. right_spacing = GetSpacing(box->GetBox(), Box::RIGHT);
  226. if (open_inline_box != NULL &&
  227. box->IsLastChild())
  228. {
  229. LayoutInlineBox* closing_box = open_inline_box;
  230. while (closing_box != NULL &&
  231. closing_box->IsLastChild())
  232. {
  233. closing_box = closing_box->GetParent();
  234. if (closing_box != NULL)
  235. right_spacing += GetSpacing(closing_box->GetBox(), Box::RIGHT);
  236. }
  237. }
  238. // Determine the inline box's spacing requirements (before we get onto it's actual content width).
  239. float element_width = box->GetBox().GetPosition(Box::CONTENT).x;
  240. if (!box->CanOverflow())
  241. element_width += right_spacing;
  242. // Add on the box's content area (if it has content).
  243. if (box->GetBox().GetSize().x >= 0)
  244. element_width += box->GetBox().GetSize().x;
  245. if (wrap_content &&
  246. box_cursor + element_width > dimensions.x)
  247. {
  248. // We can't fit the new inline element into this box! So we'll close this line box, and send the inline box
  249. // onto the next line.
  250. return Close(box);
  251. }
  252. else
  253. {
  254. // We can fit the new inline element into this box.
  255. AppendBox(box);
  256. }
  257. }
  258. // Flow the box's content into the line.
  259. LayoutInlineBox* overflow_box = open_inline_box->FlowContent(first_box, wrap_content ? dimensions.x - (open_inline_box->GetPosition().x + open_inline_box->GetBox().GetPosition(Box::CONTENT).x) : -1, right_spacing);
  260. box_cursor += open_inline_box->GetBox().GetSize().x;
  261. // If our box overflowed, then we'll close this line (as no more content will fit onto it) and tell our block box
  262. // to make a new line.
  263. if (overflow_box != NULL)
  264. {
  265. open_inline_box = open_inline_box->GetParent();
  266. return Close(overflow_box);
  267. }
  268. return open_inline_box;
  269. }
  270. // Adds an inline box as a chained hierarchy overflowing to this line.
  271. void LayoutLineBox::AddChainedBox(LayoutInlineBox* chained_box)
  272. {
  273. std::stack< LayoutInlineBox* > hierarchy;
  274. LayoutInlineBox* chain = chained_box;
  275. while (chain != NULL)
  276. {
  277. hierarchy.push(chain);
  278. chain = chain->GetParent();
  279. }
  280. while (!hierarchy.empty())
  281. {
  282. AddBox(new LayoutInlineBox(hierarchy.top()));
  283. hierarchy.pop();
  284. }
  285. }
  286. // Returns the position of the line box, relative to its parent's block box's content area.
  287. const Vector2f& LayoutLineBox::GetPosition() const
  288. {
  289. return position;
  290. }
  291. // Returns the position of the line box, relative to its parent's block box's offset parent.
  292. Vector2f LayoutLineBox::GetRelativePosition() const
  293. {
  294. return position - (parent->GetOffsetParent()->GetPosition() - parent->GetOffsetRoot()->GetPosition());
  295. }
  296. // Returns the dimensions of the line box.
  297. const Vector2f& LayoutLineBox::GetDimensions() const
  298. {
  299. return dimensions;
  300. }
  301. // Returns the line box's open inline box.
  302. LayoutInlineBox* LayoutLineBox::GetOpenInlineBox()
  303. {
  304. return open_inline_box;
  305. }
  306. // Returns the line's containing block box.
  307. LayoutBlockBox* LayoutLineBox::GetBlockBox()
  308. {
  309. return parent;
  310. }
  311. void* LayoutLineBox::operator new(size_t size)
  312. {
  313. return LayoutEngine::AllocateLayoutChunk(size);
  314. }
  315. void LayoutLineBox::operator delete(void* chunk)
  316. {
  317. LayoutEngine::DeallocateLayoutChunk(chunk);
  318. }
  319. // Appends an inline box to the end of the line box's list of inline boxes.
  320. void LayoutLineBox::AppendBox(LayoutInlineBox* box)
  321. {
  322. inline_boxes.push_back(box);
  323. box->SetParent(open_inline_box);
  324. box->SetLine(this);
  325. box->SetHorizontalPosition(LayoutEngine::Round(box_cursor + box->GetBox().GetEdge(Box::MARGIN, Box::LEFT)));
  326. box_cursor += GetSpacing(box->GetBox(), Box::LEFT);
  327. open_inline_box = box;
  328. }
  329. }
  330. }