InlineLevelBox.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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(Node* node) : node(node) { RMLUI_ASSERT(node); }
  60. Node* GetNode() const { return node; }
  61. Element* GetElement() const { return rmlui_static_cast<Element*>(node); }
  62. const FontMetrics& GetFontMetrics() const;
  63. // Set the height used for inline layout, and the vertical offset relative to our parent box.
  64. void SetHeightAndVerticalAlignment(float height_above_baseline, float depth_below_baseline, const InlineLevelBox* parent);
  65. // Set the height used for inline layout.
  66. void SetHeight(float height_above_baseline, float depth_below_baseline);
  67. // Set the inner-to-outer spacing (margin + border + padding) for inline boxes.
  68. void SetInlineBoxSpacing(float spacing_left, float spacing_right);
  69. // Calls Element::OnLayout (proxy for private access to Element).
  70. void SubmitElementOnLayout();
  71. private:
  72. float height_above_baseline = 0.f;
  73. float depth_below_baseline = 0.f;
  74. Style::VerticalAlign::Type vertical_align_type = {};
  75. float vertical_offset_from_parent = 0.f;
  76. float spacing_left = 0.f; // Left margin-border-padding for inline boxes.
  77. float spacing_right = 0.f; // Right margin-border-padding for inline boxes.
  78. Node* node;
  79. };
  80. /**
  81. Atomic inline-level boxes are sized boxes that cannot be split.
  82. This includes inline-block elements, replaced inline-level elements, inline tables, and inline flex containers.
  83. */
  84. class InlineLevelBox_Atomic final : public InlineLevelBox {
  85. public:
  86. InlineLevelBox_Atomic(const InlineLevelBox* parent, Element* element, const Box& box);
  87. FragmentConstructor CreateFragment(InlineLayoutMode mode, float available_width, float right_spacing_width, bool first_box,
  88. LayoutOverflowHandle overflow_handle) override;
  89. void Submit(const PlacedFragment& placed_fragment) override;
  90. String DebugDumpNameValue() const override { return "InlineLevelBox_Atomic"; }
  91. private:
  92. Box box;
  93. };
  94. /**
  95. Inline-level text boxes represent text nodes.
  96. Generates fragments to display its text, splitting it up as necessary to fit in the available space.
  97. */
  98. class InlineLevelBox_Text final : public InlineLevelBox {
  99. public:
  100. InlineLevelBox_Text(ElementText* element);
  101. FragmentConstructor CreateFragment(InlineLayoutMode mode, float available_width, float right_spacing_width, bool first_box,
  102. LayoutOverflowHandle overflow_handle) override;
  103. void Submit(const PlacedFragment& placed_fragment) override;
  104. String DebugDumpNameValue() const override;
  105. private:
  106. ElementText* GetTextElement();
  107. Vector2f element_offset;
  108. StringList fragments;
  109. };
  110. } // namespace Rml
  111. #endif