InlineBox.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "InlineBox.h"
  29. #include "../../../Include/RmlUi/Core/Box.h"
  30. #include "../../../Include/RmlUi/Core/Element.h"
  31. #include "../../../Include/RmlUi/Core/FontMetrics.h"
  32. namespace Rml {
  33. static void ZeroBoxEdge(Box& box, BoxEdge edge)
  34. {
  35. box.SetEdge(BoxArea::Padding, edge, 0.f);
  36. box.SetEdge(BoxArea::Border, edge, 0.f);
  37. box.SetEdge(BoxArea::Margin, edge, 0.f);
  38. }
  39. InlineLevelBox* InlineBoxBase::AddChild(UniquePtr<InlineLevelBox> child)
  40. {
  41. auto result = child.get();
  42. children.push_back(std::move(child));
  43. return result;
  44. }
  45. void InlineBoxBase::GetStrut(float& out_total_height_above, float& out_total_depth_below) const
  46. {
  47. const FontMetrics& font_metrics = GetFontMetrics();
  48. const float line_height = GetElement()->GetLineHeight();
  49. const float half_leading = 0.5f * (line_height - (font_metrics.ascent + font_metrics.descent));
  50. out_total_height_above = font_metrics.ascent + half_leading;
  51. out_total_depth_below = line_height - out_total_height_above;
  52. }
  53. String InlineBoxBase::DebugDumpTree(int depth) const
  54. {
  55. String value = InlineLevelBox::DebugDumpTree(depth);
  56. for (auto&& child : children)
  57. value += child->DebugDumpTree(depth + 1);
  58. return value;
  59. }
  60. InlineBoxBase::InlineBoxBase(Element* element) : InlineLevelBox(element) {}
  61. InlineBoxRoot::InlineBoxRoot(Element* element) : InlineBoxBase(element)
  62. {
  63. float height_above_baseline, depth_below_baseline;
  64. GetStrut(height_above_baseline, depth_below_baseline);
  65. SetHeight(height_above_baseline, depth_below_baseline);
  66. }
  67. FragmentConstructor InlineBoxRoot::CreateFragment(InlineLayoutMode /*mode*/, float /*available_width*/, float /*right_spacing_width*/,
  68. bool /*first_box*/, LayoutOverflowHandle /*overflow_handle*/)
  69. {
  70. RMLUI_ERROR;
  71. return {};
  72. }
  73. void InlineBoxRoot::Submit(const PlacedFragment& /*placed_fragment*/)
  74. {
  75. RMLUI_ERROR;
  76. }
  77. InlineBox::InlineBox(const InlineLevelBox* parent, Element* element, const Box& _box) : InlineBoxBase(element), box(_box)
  78. {
  79. RMLUI_ASSERT(box.GetSize().x < 0.f && box.GetSize().y < 0.f);
  80. const FontMetrics& font_metrics = GetFontMetrics();
  81. // The inline box content height does not depend on the 'line-height' property, only on the font, and is not exactly
  82. // specified by CSS. Here we choose to size the content height equal to the default line-height for the font-size.
  83. const float inner_height = 1.2f * (float)font_metrics.size;
  84. box.SetContent(Vector2f(-1.f, inner_height));
  85. float height_above_baseline, depth_below_baseline;
  86. GetStrut(height_above_baseline, depth_below_baseline);
  87. SetHeightAndVerticalAlignment(height_above_baseline, depth_below_baseline, parent);
  88. const float edge_left = box.GetCumulativeEdge(BoxArea::Padding, BoxEdge::Left);
  89. const float edge_right = box.GetCumulativeEdge(BoxArea::Padding, BoxEdge::Right);
  90. SetInlineBoxSpacing(edge_left, edge_right);
  91. // Vertically position the box so that its content box is equally spaced around its font ascent and descent metrics.
  92. const float half_leading = 0.5f * (inner_height - (font_metrics.ascent + font_metrics.descent));
  93. baseline_to_border_height =
  94. font_metrics.ascent + half_leading + box.GetEdge(BoxArea::Border, BoxEdge::Top) + box.GetEdge(BoxArea::Padding, BoxEdge::Top);
  95. }
  96. FragmentConstructor InlineBox::CreateFragment(InlineLayoutMode mode, float available_width, float right_spacing_width, bool /*first_box*/,
  97. LayoutOverflowHandle /*overflow_handle*/)
  98. {
  99. if (mode != InlineLayoutMode::WrapAny || right_spacing_width <= available_width + GetSpacingLeft())
  100. return FragmentConstructor{FragmentType::InlineBox, -1.f, {}, {}};
  101. return {};
  102. }
  103. void InlineBox::Submit(const PlacedFragment& placed_fragment)
  104. {
  105. Element* element = GetElement();
  106. RMLUI_ASSERT(element && element != placed_fragment.offset_parent);
  107. Box element_box = box;
  108. element_box.SetContent({placed_fragment.layout_width, element_box.GetSize().y});
  109. if (placed_fragment.split_left)
  110. ZeroBoxEdge(element_box, BoxEdge::Left);
  111. if (placed_fragment.split_right)
  112. ZeroBoxEdge(element_box, BoxEdge::Right);
  113. // In inline layout, fragments are positioned in terms of (x: margin edge, y: baseline), while element offsets are
  114. // specified relative to their border box. Thus, find the offset from the fragment position to the border edge.
  115. const Vector2f border_position =
  116. placed_fragment.position + Vector2f{element_box.GetEdge(BoxArea::Margin, BoxEdge::Left), -baseline_to_border_height};
  117. // We can determine the principal fragment based on its left split: Only the principal one has its left side intact,
  118. // subsequent fragments have their left side split.
  119. const bool principal_box = !placed_fragment.split_left;
  120. if (principal_box)
  121. {
  122. element_offset = border_position;
  123. element->SetOffset(border_position, placed_fragment.offset_parent);
  124. element->SetBox(element_box);
  125. SubmitElementOnLayout();
  126. }
  127. else
  128. {
  129. element->AddBox(element_box, border_position - element_offset);
  130. }
  131. }
  132. } // namespace Rml