LayoutInlineBoxText.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "LayoutInlineBoxText.h"
  29. #include "FontFaceHandle.h"
  30. #include "LayoutEngine.h"
  31. #include "LayoutLineBox.h"
  32. #include "../../Include/Rocket/Core/ElementText.h"
  33. #include "../../Include/Rocket/Core/ElementUtilities.h"
  34. #include "../../Include/Rocket/Core/Log.h"
  35. #include "../../Include/Rocket/Core/Property.h"
  36. namespace Rocket {
  37. namespace Core {
  38. LayoutInlineBoxText::LayoutInlineBoxText(Element* element, int _line_begin) : LayoutInlineBox(element, Box())
  39. {
  40. line_begin = _line_begin;
  41. // Build the box to represent the dimensions of the first word.
  42. BuildWordBox();
  43. }
  44. LayoutInlineBoxText::~LayoutInlineBoxText()
  45. {
  46. }
  47. // Returns true if this box is capable of overflowing, or if it must be rendered on a single line.
  48. bool LayoutInlineBoxText::CanOverflow() const
  49. {
  50. return line_segmented;
  51. }
  52. // Flows the inline box's content into its parent line.
  53. LayoutInlineBox* LayoutInlineBoxText::FlowContent(bool first_box, float available_width, float right_spacing_width)
  54. {
  55. ElementText* text_element = GetTextElement();
  56. ROCKET_ASSERT(text_element != NULL);
  57. int line_length;
  58. float line_width;
  59. bool overflow = !text_element->GenerateLine(line_contents, line_length, line_width, line_begin, available_width, right_spacing_width, first_box);
  60. Vector2f content_area;
  61. content_area.x = line_width;
  62. content_area.y = box.GetSize().y;
  63. box.SetContent(content_area);
  64. // Call the base-class's FlowContent() to increment the width of our parent's box.
  65. LayoutInlineBox::FlowContent(first_box, available_width, right_spacing_width);
  66. if (overflow)
  67. return new LayoutInlineBoxText(element, line_begin + line_length);
  68. return NULL;
  69. }
  70. // Computes and sets the vertical position of this element, relative to its parent inline box (or block box, for an un-nested inline box).
  71. void LayoutInlineBoxText::CalculateBaseline(float& ascender, float& descender)
  72. {
  73. ascender = height - baseline;
  74. descender = height - ascender;
  75. }
  76. // Offsets the baseline of this box, and all of its children, by the ascender of the parent line box.
  77. void LayoutInlineBoxText::OffsetBaseline(float ascender)
  78. {
  79. // Offset by the ascender.
  80. position.y += (ascender - (height - baseline));
  81. // Calculate the leading (the difference between font height and line height).
  82. float leading = 0;
  83. FontFaceHandle* font_face_handle = element->GetFontFaceHandle();
  84. if (font_face_handle != NULL)
  85. leading = height - font_face_handle->GetLineHeight();
  86. // Offset by the half-leading.
  87. position.y += leading * 0.5f;
  88. }
  89. // Positions the inline box's element.
  90. void LayoutInlineBoxText::PositionElement()
  91. {
  92. if (line_begin == 0)
  93. {
  94. LayoutInlineBox::PositionElement();
  95. GetTextElement()->ClearLines();
  96. GetTextElement()->AddLine(Vector2f(0, 0), line_contents);
  97. }
  98. else
  99. {
  100. GetTextElement()->AddLine(line->GetRelativePosition() + position - element->GetRelativeOffset(Box::BORDER), line_contents);
  101. }
  102. }
  103. // Sizes the inline box's element.
  104. void LayoutInlineBoxText::SizeElement(bool ROCKET_UNUSED_PARAMETER(split))
  105. {
  106. ROCKET_UNUSED(split);
  107. }
  108. void* LayoutInlineBoxText::operator new(size_t size)
  109. {
  110. return LayoutEngine::AllocateLayoutChunk(size);
  111. }
  112. void LayoutInlineBoxText::operator delete(void* chunk)
  113. {
  114. LayoutEngine::DeallocateLayoutChunk(chunk);
  115. }
  116. // Returns the box's element as a text element.
  117. ElementText* LayoutInlineBoxText::GetTextElement()
  118. {
  119. return dynamic_cast< ElementText* >(element);
  120. }
  121. // Builds a box for the first word of the element.
  122. void LayoutInlineBoxText::BuildWordBox()
  123. {
  124. ElementText* text_element = GetTextElement();
  125. ROCKET_ASSERT(text_element != NULL);
  126. FontFaceHandle* font_face_handle = text_element->GetFontFaceHandle();
  127. if (font_face_handle == NULL)
  128. {
  129. height = 0;
  130. baseline = 0;
  131. // For unknown reasons, this gets triggered during document load. Seems to cause no trouble, remove warning.
  132. Log::Message(Log::LT_WARNING, "No font face defined on element %s. Please specify a font-family in your RCSS.", text_element->GetAddress().CString());
  133. return;
  134. }
  135. Vector2f content_area;
  136. line_segmented = !text_element->GenerateToken(content_area.x, line_begin);
  137. content_area.y = (float) ElementUtilities::GetLineHeight(element);
  138. box.SetContent(content_area);
  139. }
  140. }
  141. }