InlineLevelBox.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifndef RMLUI_CORE_LAYOUT_INLINELEVELBOX_H
  29. #define RMLUI_CORE_LAYOUT_INLINELEVELBOX_H
  30. #include "../../../Include/RmlUi/Core/Box.h"
  31. #include "../../../Include/RmlUi/Core/StyleTypes.h"
  32. #include "InlineTypes.h"
  33. namespace Rml {
  34. class ElementText;
  35. struct FontMetrics;
  36. /**
  37. A box that takes part in inline layout.
  38. The inline-level box is used to generate fragments that are placed within line boxes.
  39. */
  40. class InlineLevelBox {
  41. public:
  42. virtual ~InlineLevelBox();
  43. // Create a fragment from this box, if it can fit within the available width.
  44. virtual FragmentConstructor CreateFragment(InlineLayoutMode mode, float available_width, float right_spacing_width, bool first_box,
  45. LayoutOverflowHandle overflow_handle) = 0;
  46. // Submit a fragment's position and size to be displayed on the underlying element.
  47. virtual void Submit(const PlacedFragment& placed_fragment) = 0;
  48. float GetHeightAboveBaseline() const { return height_above_baseline; }
  49. float GetDepthBelowBaseline() const { return depth_below_baseline; }
  50. Style::VerticalAlign::Type GetVerticalAlign() const { return vertical_align_type; }
  51. float GetVerticalOffsetFromParent() const { return vertical_offset_from_parent; }
  52. float GetSpacingLeft() const { return spacing_left; }
  53. float GetSpacingRight() const { return spacing_right; }
  54. virtual String DebugDumpNameValue() const = 0;
  55. virtual String DebugDumpTree(int depth) const;
  56. void* operator new(size_t size);
  57. void operator delete(void* chunk, size_t size);
  58. protected:
  59. InlineLevelBox(Element* element) : element(element) { RMLUI_ASSERT(element); }
  60. Element* GetElement() const { return element; }
  61. const FontMetrics& GetFontMetrics() const;
  62. // Set the height used for inline layout, and the vertical offset relative to our parent box.
  63. void SetHeightAndVerticalAlignment(float height_above_baseline, float depth_below_baseline, const InlineLevelBox* parent);
  64. // Set the height used for inline layout.
  65. void SetHeight(float height_above_baseline, float depth_below_baseline);
  66. // Set the inner-to-outer spacing (margin + border + padding) for inline boxes.
  67. void SetInlineBoxSpacing(float spacing_left, float spacing_right);
  68. // Calls Element::OnLayout (proxy for private access to Element).
  69. void SubmitElementOnLayout();
  70. private:
  71. float height_above_baseline = 0.f;
  72. float depth_below_baseline = 0.f;
  73. Style::VerticalAlign::Type vertical_align_type = {};
  74. float vertical_offset_from_parent = 0.f;
  75. float spacing_left = 0.f; // Left margin-border-padding for inline boxes.
  76. float spacing_right = 0.f; // Right margin-border-padding for inline boxes.
  77. Element* element;
  78. };
  79. /**
  80. Atomic inline-level boxes are sized boxes that cannot be split.
  81. This includes inline-block elements, replaced inline-level elements, inline tables, and inline flex containers.
  82. */
  83. class InlineLevelBox_Atomic final : public InlineLevelBox {
  84. public:
  85. InlineLevelBox_Atomic(const InlineLevelBox* parent, Element* element, const Box& box);
  86. FragmentConstructor CreateFragment(InlineLayoutMode mode, float available_width, float right_spacing_width, bool first_box,
  87. LayoutOverflowHandle overflow_handle) override;
  88. void Submit(const PlacedFragment& placed_fragment) override;
  89. String DebugDumpNameValue() const override { return "InlineLevelBox_Atomic"; }
  90. private:
  91. Box box;
  92. };
  93. /**
  94. Inline-level text boxes represent text nodes.
  95. Generates fragments to display its text, splitting it up as necessary to fit in the available space.
  96. */
  97. class InlineLevelBox_Text final : public InlineLevelBox {
  98. public:
  99. InlineLevelBox_Text(ElementText* element);
  100. FragmentConstructor CreateFragment(InlineLayoutMode mode, float available_width, float right_spacing_width, bool first_box,
  101. LayoutOverflowHandle overflow_handle) override;
  102. void Submit(const PlacedFragment& placed_fragment) override;
  103. String DebugDumpNameValue() const override;
  104. private:
  105. ElementText* GetTextElement();
  106. Vector2f element_offset;
  107. StringList fragments;
  108. };
  109. } // namespace Rml
  110. #endif