LayoutNode.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_LAYOUTNODE_H
  29. #define RMLUI_CORE_LAYOUT_LAYOUTNODE_H
  30. #include "../../../Include/RmlUi/Core/Box.h"
  31. #include "../../../Include/RmlUi/Core/Element.h"
  32. #include "../../../Include/RmlUi/Core/Types.h"
  33. namespace Rml {
  34. enum class DirtyLayoutType : uint8_t {
  35. None = 0,
  36. DOM = 1 << 0,
  37. PropertyForcingLayout = 1 << 1,
  38. TableAttribute = 1 << 2,
  39. ReplacedElement = 1 << 3,
  40. IntrinsicSize = 1 << 4, // Closely tied to ReplacedElement, combine them?
  41. Text = 1 << 5,
  42. Child = 1 << 6,
  43. };
  44. inline DirtyLayoutType operator|(DirtyLayoutType lhs, DirtyLayoutType rhs)
  45. {
  46. using T = std::underlying_type_t<DirtyLayoutType>;
  47. return DirtyLayoutType(T(lhs) | T(rhs));
  48. }
  49. inline DirtyLayoutType operator&(DirtyLayoutType lhs, DirtyLayoutType rhs)
  50. {
  51. using T = std::underlying_type_t<DirtyLayoutType>;
  52. return DirtyLayoutType(T(lhs) & T(rhs));
  53. }
  54. struct CommittedLayout {
  55. Vector2f containing_block_size;
  56. Vector2f absolutely_positioning_containing_block_size;
  57. Optional<Box> override_box;
  58. bool layout_constraint;
  59. Vector2f visible_overflow_size;
  60. float max_content_width;
  61. Optional<float> baseline_of_last_line;
  62. };
  63. /*
  64. A LayoutNode TODO
  65. */
  66. class LayoutNode {
  67. public:
  68. enum class Type { Undefined, Root, BlockContainer, InlineContainer, FlexContainer, TableWrapper, Replaced };
  69. LayoutNode(Element* element) : element(element) {}
  70. // Assumes valid computed values.
  71. void PropagateDirtyToParent();
  72. void ClearDirty();
  73. void SetDirty(DirtyLayoutType dirty_type);
  74. bool IsChildDirty() const { return (dirty_flag & DirtyLayoutType::Child) != DirtyLayoutType::None; }
  75. bool IsDirty() const { return dirty_flag != DirtyLayoutType::None; }
  76. bool IsSelfDirty() const { return !(dirty_flag == DirtyLayoutType::None || dirty_flag == DirtyLayoutType::Child); }
  77. void CommitLayout(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size, const Box* override_box,
  78. bool layout_constraint, Vector2f visible_overflow_size, float max_content_width, Optional<float> baseline_of_last_line);
  79. // TODO: Currently, should only be used by `Context::SetDimensions`. Ideally, we would remove this and "commit" the
  80. // containing block (without the layout result, see comment in `CommitLayout`).
  81. void ClearCommittedLayout()
  82. {
  83. committed_layout.reset();
  84. committed_max_content_width.reset();
  85. }
  86. bool CommittedLayoutMatches(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size, const Box* override_box,
  87. bool layout_constraint) const
  88. {
  89. if (IsDirty())
  90. return false;
  91. if (!committed_layout.has_value())
  92. return false;
  93. if (committed_layout->containing_block_size != containing_block_size ||
  94. committed_layout->absolutely_positioning_containing_block_size != absolutely_positioning_containing_block_size)
  95. return false;
  96. // Layout under a constraint may make some simplifications that requires re-evaluation under a normal formatting mode.
  97. if (committed_layout->layout_constraint && !layout_constraint)
  98. return false;
  99. if (!override_box)
  100. return !committed_layout->override_box.has_value();
  101. const Box& compare_box = committed_layout->override_box.has_value() ? *committed_layout->override_box : element->GetBox();
  102. // In some situations, if we have an indefinite size on the committed box, we could see if the layed-out size
  103. // matches the input override box and use the cache here. However, because of cyclic-percentage rules with
  104. // containing block sizes, this is only correct in certain situations, particularly when the vertical size of
  105. // the containing block is indefinite. In this case the used containing block size should resolve to indefinite,
  106. // even after it is sized. Although, we don't actually implement this behavior for now, but once we do, we could
  107. // implement caching here in this case. For the horizontal axis, we are always required to re-evaluate any
  108. // children for which this box acts as a containing block for, thus we cannot use a cache mechanism here. See:
  109. // https://drafts.csswg.org/css-sizing/#cyclic-percentage-contribution
  110. return *override_box == compare_box;
  111. }
  112. bool HasCommittedLayout() const { return committed_layout.has_value(); }
  113. Optional<float> GetMaxContentWidthIfCached() const { return committed_max_content_width; }
  114. void CommitMaxContentWidth(float width) { committed_max_content_width = width; }
  115. Optional<float> GetMaxContentHeightIfCached() const { return committed_max_content_height; }
  116. void CommitMaxContentHeight(float height) { committed_max_content_height = height; }
  117. // TODO: Remove and replace with a better interface.
  118. const Optional<CommittedLayout>& GetCommittedLayout() const { return committed_layout; }
  119. // A.k.a. reflow root.
  120. bool IsLayoutBoundary() const;
  121. private:
  122. Element* element;
  123. Type type = Type::Undefined;
  124. DirtyLayoutType dirty_flag = DirtyLayoutType::None;
  125. // Store this for partial layout updates / reflow. If this changes, always do layout again, regardless of any cache.
  126. Vector2f containing_block;
  127. Optional<CommittedLayout> committed_layout;
  128. Optional<float> committed_max_content_width;
  129. Optional<float> committed_max_content_height;
  130. };
  131. } // namespace Rml
  132. #endif