LayoutInlineBoxText.cpp 5.6 KB

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