LayoutInlineBoxText.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "LayoutInlineBoxText.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/Log.h"
  35. #include "../../Include/RmlUi/Core/Profiling.h"
  36. #include "../../Include/RmlUi/Core/Property.h"
  37. #include "ComputeProperty.h"
  38. #include "LayoutEngine.h"
  39. #include "LayoutLineBox.h"
  40. namespace Rml {
  41. String FontFaceDescription(const String& font_family, Style::FontStyle style, Style::FontWeight weight)
  42. {
  43. String font_attributes;
  44. if (style == Style::FontStyle::Italic)
  45. font_attributes += "italic, ";
  46. if (weight == Style::FontWeight::Bold)
  47. font_attributes += "bold, ";
  48. else if (weight != Style::FontWeight::Auto && weight != Style::FontWeight::Normal)
  49. font_attributes += "weight=" + ToString((int)weight) + ", ";
  50. if (font_attributes.empty())
  51. font_attributes = "regular";
  52. else
  53. font_attributes.resize(font_attributes.size() - 2);
  54. return CreateString(font_attributes.size() + font_family.size() + 8, "'%s' [%s]", font_family.c_str(), font_attributes.c_str());
  55. }
  56. LayoutInlineBoxText::LayoutInlineBoxText(ElementText* element, int _line_begin) : LayoutInlineBox(static_cast<Element*>(element), Box())
  57. {
  58. line_begin = _line_begin;
  59. // Build the box to represent the dimensions of the first word.
  60. BuildWordBox();
  61. }
  62. LayoutInlineBoxText::~LayoutInlineBoxText()
  63. {
  64. }
  65. // Returns true if this box is capable of overflowing, or if it must be rendered on a single line.
  66. bool LayoutInlineBoxText::CanOverflow() const
  67. {
  68. return line_segmented;
  69. }
  70. // Flows the inline box's content into its parent line.
  71. UniquePtr<LayoutInlineBox> LayoutInlineBoxText::FlowContent(bool first_box, bool last_box, float available_width, float right_spacing_width)
  72. {
  73. ElementText* text_element = GetTextElement();
  74. RMLUI_ASSERT(text_element != nullptr);
  75. int line_length;
  76. float line_width;
  77. bool overflow = !text_element->GenerateLine(line_contents, line_length, line_width, line_begin, available_width, right_spacing_width, first_box, last_box, true);
  78. Vector2f content_area;
  79. content_area.x = line_width;
  80. content_area.y = box.GetSize().y;
  81. box.SetContent(content_area);
  82. // Call the base-class's FlowContent() to increment the width of our parent's box.
  83. LayoutInlineBox::FlowContent(first_box, last_box, available_width, right_spacing_width);
  84. if (overflow)
  85. return MakeUnique<LayoutInlineBoxText>(GetTextElement(), line_begin + line_length);
  86. return nullptr;
  87. }
  88. // Computes and sets the vertical position of this element, relative to its parent inline box (or block box, for an un-nested inline box).
  89. void LayoutInlineBoxText::CalculateBaseline(float& ascender, float& descender)
  90. {
  91. ascender = height - baseline;
  92. descender = height - ascender;
  93. }
  94. // Offsets the baseline of this box, and all of its children, by the ascender of the parent line box.
  95. void LayoutInlineBoxText::OffsetBaseline(float ascender)
  96. {
  97. // Offset by the ascender.
  98. position.y += (ascender - (height - baseline));
  99. // Calculate the leading (the difference between font height and line height).
  100. float leading = 0;
  101. FontFaceHandle font_face_handle = element->GetFontFaceHandle();
  102. if (font_face_handle != 0)
  103. leading = height - GetFontEngineInterface()->GetLineHeight(font_face_handle);
  104. // Offset by the half-leading.
  105. position.y += leading * 0.5f;
  106. }
  107. // Positions the inline box's element.
  108. void LayoutInlineBoxText::PositionElement()
  109. {
  110. if (line_begin == 0)
  111. {
  112. LayoutInlineBox::PositionElement();
  113. GetTextElement()->ClearLines();
  114. GetTextElement()->AddLine(Vector2f(0, 0), line_contents);
  115. }
  116. else
  117. {
  118. GetTextElement()->AddLine(line->GetRelativePosition() + position - element->GetRelativeOffset(Box::BORDER), line_contents);
  119. }
  120. }
  121. // Sizes the inline box's element.
  122. void LayoutInlineBoxText::SizeElement(bool RMLUI_UNUSED_PARAMETER(split))
  123. {
  124. RMLUI_UNUSED(split);
  125. }
  126. void* LayoutInlineBoxText::operator new(size_t size)
  127. {
  128. return LayoutEngine::AllocateLayoutChunk(size);
  129. }
  130. void LayoutInlineBoxText::operator delete(void* chunk, size_t size)
  131. {
  132. LayoutEngine::DeallocateLayoutChunk(chunk, size);
  133. }
  134. // Returns the box's element as a text element.
  135. ElementText* LayoutInlineBoxText::GetTextElement()
  136. {
  137. RMLUI_ASSERT(rmlui_dynamic_cast<ElementText*>(element));
  138. return static_cast< ElementText* >(element);
  139. }
  140. // Builds a box for the first word of the element.
  141. void LayoutInlineBoxText::BuildWordBox()
  142. {
  143. RMLUI_ZoneScoped;
  144. ElementText* text_element = GetTextElement();
  145. RMLUI_ASSERT(text_element != nullptr);
  146. FontFaceHandle font_face_handle = text_element->GetFontFaceHandle();
  147. if (font_face_handle == 0)
  148. {
  149. height = 0;
  150. baseline = 0;
  151. const ComputedValues& computed = text_element->GetComputedValues();
  152. const String font_family_property = computed.font_family();
  153. if (font_family_property.empty())
  154. {
  155. Log::Message(
  156. Log::LT_WARNING,
  157. "No font face defined. Missing 'font-family' property, please add it to your RCSS. On element %s",
  158. text_element->GetAddress().c_str()
  159. );
  160. }
  161. else
  162. {
  163. const String font_face_description = FontFaceDescription(font_family_property, computed.font_style(), computed.font_weight());
  164. Log::Message(
  165. Log::LT_WARNING,
  166. "No font face defined. Ensure (1) that Context::Update is run after new elements are constructed, before Context::Render, "
  167. "and (2) that the specified font face %s has been successfully loaded. "
  168. "Please see previous log messages for all successfully loaded fonts. On element %s",
  169. font_face_description.c_str(),
  170. text_element->GetAddress().c_str()
  171. );
  172. }
  173. return;
  174. }
  175. Vector2f content_area;
  176. line_segmented = !text_element->GenerateToken(content_area.x, line_begin);
  177. content_area.y = text_element->GetLineHeight();
  178. box.SetContent(content_area);
  179. }
  180. } // namespace Rml