InlineLevelBox.cpp 8.7 KB

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