InlineLevelBox.cpp 7.3 KB

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