InlineContainer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_INLINECONTAINER_H
  29. #define RMLUI_CORE_LAYOUT_INLINECONTAINER_H
  30. #include "../../../Include/RmlUi/Core/Box.h"
  31. #include "../../../Include/RmlUi/Core/Types.h"
  32. #include "InlineBox.h"
  33. #include "LayoutBox.h"
  34. namespace Rml {
  35. class BlockContainer;
  36. class LineBox;
  37. /**
  38. A container for inline-level boxes.
  39. Always a direct child of a block container where it acts as a block-level box, and starts a new inline formatting
  40. context. It maintains a stack of line boxes in which fragments generated from inline-level boxes are placed within.
  41. Not a proper CSS term, but effectively a "block container that only contains inline-level boxes".
  42. */
  43. class InlineContainer final : public LayoutBox {
  44. public:
  45. /// Creates a new block box in an inline context.
  46. InlineContainer(BlockContainer* parent, float available_width);
  47. ~InlineContainer();
  48. /// Adds a new inline-level node to this inline-context box.
  49. /// @param[in] node The new inline-level node.
  50. /// @param[in] box The box defining the node's bounds.
  51. /// @return The inline box if one was generated for the node, otherwise nullptr.
  52. /// @note Any non-null return value must be closed with a call to CloseInlineElement().
  53. InlineBox* AddInlineNode(Node* node, const Box& box);
  54. /// Closes the previously added inline node.
  55. /// @param[in] inline_box The box to close.
  56. /// @note Calls to this function should be submitted in reverse order to AddInlineNode().
  57. void CloseInlineNode(InlineBox* inline_box);
  58. /// Add a break to the last line.
  59. void AddBreak(float line_height);
  60. /// Adds a line box for resuming one that was previously split.
  61. /// @param[in] open_line_box The line box overflowing from a previous inline container.
  62. void AddChainedBox(UniquePtr<LineBox> open_line_box);
  63. /// Closes the box. This will determine the element's height (if it was unspecified).
  64. /// @param[out] out_open_line_box Optionally, output the open inline box.
  65. /// @param[out] out_position Outputs the position of this container.
  66. /// @param[out] out_height Outputs the height of this container.
  67. void Close(UniquePtr<LineBox>* out_open_line_box, Vector2f& out_position, float& out_height);
  68. /// Update the placement of the open line box, e.g. to account for newly placed floats.
  69. void UpdateOpenLineBoxPlacement();
  70. /// Returns the position and tentative size of the currently open line box, if any.
  71. bool GetOpenLineBoxDimensions(float& out_vertical_position, Vector2f& out_tentative_size) const;
  72. /// Returns an estimate for the position of a hypothetical next box to be placed, relative to the content box of this container.
  73. Vector2f GetStaticPositionEstimate(bool inline_level_box) const;
  74. // -- Inherited from LayoutBox --
  75. float GetShrinkToFitWidth() const override;
  76. bool GetBaselineOfLastLine(float& out_baseline) const override;
  77. String DebugDumpTree(int depth) const override;
  78. private:
  79. using LineBoxList = Vector<UniquePtr<LineBox>>;
  80. LineBox* EnsureOpenLineBox();
  81. LineBox* GetOpenLineBox() const;
  82. InlineBoxBase* GetOpenInlineBox();
  83. /// Close any open line box.
  84. /// @param[in] split_all_open_boxes Split all open inline boxes, even if they have no content.
  85. /// @param[out] out_split_line Optionally return any resulting split line, otherwise it will be added as a new line box to this container.
  86. void CloseOpenLineBox(bool split_all_open_boxes, UniquePtr<LineBox>* out_split_line = nullptr);
  87. /// Find and set the position and line width for the currently open line box.
  88. /// @param[in] line_box The currently open line box.
  89. /// @param[in] minimum_width The minimum line width to consider for this search.
  90. /// @param[in] minimum_height The minimum line height to to be considered for this and future searches.
  91. void UpdateLineBoxPlacement(LineBox* line_box, float minimum_width, float minimum_height);
  92. BlockContainer* parent; // [not-null]
  93. Vector2f position;
  94. Vector2f box_size;
  95. // The element's computed line-height. Not necessarily the same as the height of our lines.
  96. float element_line_height = 0.f;
  97. // True if content should wrap instead of overflowing the line box.
  98. bool wrap_content = false;
  99. // The element's text-align property.
  100. Style::TextAlign text_align = {};
  101. // The vertical position of the currently open line, or otherwise the next one to be placed, relative to the top of this box.
  102. float box_cursor = 0;
  103. // The root of the tree of inline boxes located in this inline container.
  104. InlineBoxRoot root_inline_box;
  105. // The list of line boxes, each of which flows fragments generated by inline boxes.
  106. // @performance Investigate using a list backed by the pools instead, to avoid allocations.
  107. LineBoxList line_boxes;
  108. };
  109. } // namespace Rml
  110. #endif