LayoutInlineBoxText.cpp 7.3 KB

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