InlineLevelBox.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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-2023 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 "InlineLevelBox.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/FontEngineInterface.h"
  33. #include "LayoutDetails.h"
  34. #include "LayoutPools.h"
  35. namespace Rml {
  36. void* InlineLevelBox::operator new(size_t size)
  37. {
  38. return LayoutPools::AllocateLayoutChunk(size);
  39. }
  40. void InlineLevelBox::operator delete(void* chunk, size_t size)
  41. {
  42. LayoutPools::DeallocateLayoutChunk(chunk, size);
  43. }
  44. InlineLevelBox::~InlineLevelBox() {}
  45. const FontMetrics& InlineLevelBox::GetFontMetrics() const
  46. {
  47. if (FontFaceHandle handle = element->GetFontFaceHandle())
  48. return GetFontEngineInterface()->GetFontMetrics(handle);
  49. // If there is no font face defined then we provide zero'd out font metrics. This situation can affect the layout,
  50. // in particular in terms of inline box sizing and vertical alignment. Thus, this is potentially a situation where
  51. // we might want to log a warning. However, in many cases it will produce the same layout with or without the font,
  52. // so in that sense the warnings can produce false positives.
  53. //
  54. // For now, we wait until we try to actually place text before producing any warnings, since that is a clear
  55. // erroneous situation producing no text. See 'LogMissingFontFace' in ElementText.cpp, which also lists some
  56. // possible reasons for the missing font face.
  57. static const FontMetrics font_metrics = {};
  58. return font_metrics;
  59. }
  60. void InlineLevelBox::SetHeightAndVerticalAlignment(float _height_above_baseline, float _depth_below_baseline, const InlineLevelBox* parent)
  61. {
  62. RMLUI_ASSERT(parent);
  63. using Style::VerticalAlign;
  64. SetHeight(_height_above_baseline, _depth_below_baseline);
  65. const Style::VerticalAlign vertical_align = element->GetComputedValues().vertical_align();
  66. vertical_align_type = vertical_align.type;
  67. // Determine the offset from the parent baseline.
  68. float parent_baseline_offset = 0.f; // The anchor on the parent, as an offset from its baseline.
  69. float self_baseline_offset = 0.f; // The offset from the parent anchor to our baseline.
  70. switch (vertical_align.type)
  71. {
  72. case VerticalAlign::Baseline: parent_baseline_offset = 0.f; break;
  73. case VerticalAlign::Length: parent_baseline_offset = -vertical_align.value; break;
  74. case VerticalAlign::Sub: parent_baseline_offset = (1.f / 5.f) * (float)parent->GetFontMetrics().size; break;
  75. case VerticalAlign::Super: parent_baseline_offset = (-1.f / 3.f) * (float)parent->GetFontMetrics().size; break;
  76. case VerticalAlign::TextTop:
  77. parent_baseline_offset = -parent->GetFontMetrics().ascent;
  78. self_baseline_offset = height_above_baseline;
  79. break;
  80. case VerticalAlign::TextBottom:
  81. parent_baseline_offset = parent->GetFontMetrics().descent;
  82. self_baseline_offset = -depth_below_baseline;
  83. break;
  84. case VerticalAlign::Middle:
  85. parent_baseline_offset = -0.5f * parent->GetFontMetrics().x_height;
  86. self_baseline_offset = 0.5f * (height_above_baseline - depth_below_baseline);
  87. break;
  88. case VerticalAlign::Top:
  89. case VerticalAlign::Center:
  90. case VerticalAlign::Bottom:
  91. // These are relative to the line box and handled later.
  92. break;
  93. }
  94. vertical_offset_from_parent = parent_baseline_offset + self_baseline_offset;
  95. }
  96. void InlineLevelBox::SetHeight(float _height_above_baseline, float _depth_below_baseline)
  97. {
  98. height_above_baseline = _height_above_baseline;
  99. depth_below_baseline = _depth_below_baseline;
  100. }
  101. void InlineLevelBox::SetInlineBoxSpacing(float _spacing_left, float _spacing_right)
  102. {
  103. spacing_left = _spacing_left;
  104. spacing_right = _spacing_right;
  105. }
  106. String InlineLevelBox::DebugDumpTree(int depth) const
  107. {
  108. String value = String(depth * 2, ' ') + DebugDumpNameValue() + " | " + LayoutDetails::GetDebugElementName(GetElement()) + '\n';
  109. return value;
  110. }
  111. InlineLevelBox_Atomic::InlineLevelBox_Atomic(const InlineLevelBox* parent, Element* element, const Box& box) : InlineLevelBox(element), box(box)
  112. {
  113. RMLUI_ASSERT(parent && element);
  114. RMLUI_ASSERT(box.GetSize().x >= 0.f && box.GetSize().y >= 0.f);
  115. const float outer_height = box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin);
  116. const float descent = GetElement()->GetBaseline();
  117. const float ascent = outer_height - descent;
  118. SetHeightAndVerticalAlignment(ascent, descent, parent);
  119. }
  120. FragmentConstructor InlineLevelBox_Atomic::CreateFragment(InlineLayoutMode mode, float available_width, float right_spacing_width, bool /*first_box*/,
  121. LayoutOverflowHandle /*overflow_handle*/)
  122. {
  123. const float outer_width = box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin);
  124. if (available_width < 0.f || mode != InlineLayoutMode::WrapAny || outer_width + right_spacing_width <= available_width)
  125. return FragmentConstructor{FragmentType::SizedBox, outer_width, {}, {}};
  126. return {};
  127. }
  128. void InlineLevelBox_Atomic::Submit(const PlacedFragment& placed_fragment)
  129. {
  130. const Vector2f margin_position = {placed_fragment.position.x, placed_fragment.position.y - GetHeightAboveBaseline()};
  131. const Vector2f margin_edge = {box.GetEdge(BoxArea::Margin, BoxEdge::Left), box.GetEdge(BoxArea::Margin, BoxEdge::Top)};
  132. const Vector2f border_position = margin_position + margin_edge;
  133. GetElement()->SetOffset(border_position, placed_fragment.offset_parent);
  134. GetElement()->SetBox(box);
  135. }
  136. InlineLevelBox_Text::InlineLevelBox_Text(ElementText* element) : InlineLevelBox(element) {}
  137. FragmentConstructor InlineLevelBox_Text::CreateFragment(InlineLayoutMode mode, float available_width, float right_spacing_width, bool first_box,
  138. LayoutOverflowHandle in_overflow_handle)
  139. {
  140. ElementText* text_element = GetTextElement();
  141. const bool allow_empty = (mode == InlineLayoutMode::WrapAny);
  142. const bool decode_escape_characters = true;
  143. String line_contents;
  144. int line_begin = in_overflow_handle;
  145. int line_length = 0;
  146. float line_width = 0.f;
  147. bool overflow = !text_element->GenerateLine(line_contents, line_length, line_width, line_begin, available_width, right_spacing_width, first_box,
  148. decode_escape_characters, allow_empty);
  149. if (overflow && line_contents.empty())
  150. // We couldn't fit anything on this line.
  151. return {};
  152. LayoutOverflowHandle out_overflow_handle = {};
  153. if (overflow)
  154. out_overflow_handle = line_begin + line_length;
  155. LayoutFragmentHandle fragment_handle = (LayoutFragmentHandle)fragments.size();
  156. fragments.push_back(std::move(line_contents));
  157. return FragmentConstructor{FragmentType::TextRun, line_width, fragment_handle, out_overflow_handle};
  158. }
  159. void InlineLevelBox_Text::Submit(const PlacedFragment& placed_fragment)
  160. {
  161. RMLUI_ASSERT((size_t)placed_fragment.handle < fragments.size());
  162. const int fragment_index = (int)placed_fragment.handle;
  163. const bool principal_box = (fragment_index == 0);
  164. ElementText* text_element = GetTextElement();
  165. Vector2f line_offset;
  166. if (principal_box)
  167. {
  168. element_offset = placed_fragment.position;
  169. text_element->SetOffset(placed_fragment.position, placed_fragment.offset_parent);
  170. text_element->ClearLines();
  171. }
  172. else
  173. {
  174. line_offset = placed_fragment.position - element_offset;
  175. }
  176. text_element->AddLine(line_offset, std::move(fragments[fragment_index]));
  177. }
  178. String InlineLevelBox_Text::DebugDumpNameValue() const
  179. {
  180. return "InlineLevelBox_Text";
  181. }
  182. ElementText* InlineLevelBox_Text::GetTextElement()
  183. {
  184. return rmlui_static_cast<ElementText*>(GetElement());
  185. }
  186. } // namespace Rml