LayoutNode.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "LayoutNode.h"
  29. #include "../../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../../Include/RmlUi/Core/Element.h"
  31. #include "../../../Include/RmlUi/Core/Log.h"
  32. #include "FormattingContext.h"
  33. namespace Rml {
  34. void LayoutNode::SetDirty(DirtyLayoutType dirty_type)
  35. {
  36. // Log::Message(Log::LT_INFO, "SetDirty. Self %d Child %d Element: %s", (dirty_type & DirtyLayoutType::DOM) != DirtyLayoutType::None,
  37. // (dirty_type & DirtyLayoutType::Child) != DirtyLayoutType::None, element->GetAddress().c_str());
  38. dirty_flag = dirty_flag | dirty_type;
  39. committed_max_content_width.reset();
  40. committed_max_content_height.reset();
  41. }
  42. void LayoutNode::ClearDirty()
  43. {
  44. // Log::Message(Log::LT_INFO, "ClearDirty (was Self %d Child %d) Element: %s", (dirty_flag & DirtyLayoutType::DOM) != DirtyLayoutType::None,
  45. // (dirty_flag & DirtyLayoutType::Child) != DirtyLayoutType::None, element->GetAddress().c_str());
  46. dirty_flag = DirtyLayoutType::None;
  47. }
  48. void LayoutNode::PropagateDirtyToParent()
  49. {
  50. auto DirtyParentNode = [](Element* element) {
  51. if (Element* parent = element->GetParentNode())
  52. parent->GetLayoutNode()->SetDirty(DirtyLayoutType::Child);
  53. };
  54. if (IsSelfDirty())
  55. {
  56. // @performance We may be able to skip formatting in ancestor elements if this is a layout boundary, in some
  57. // scenarios. Consider the following for illustration:
  58. //
  59. // 1. Absolute element. `display: block` to `display: none`. This does not need parents to be layed out.
  60. // 2. Absolute element. `width` or `margin`. Same, this does not need parents to be layed out. Only the current
  61. // element needs to be reformatted, not ancestors.
  62. // 3. Absolute element. `display: none` to `display: block`. This *does* need parents to be layed out, since we don't know
  63. // our static position or containing block. We could in principle ignore static position in some situations where
  64. // it is not used, and could in principle find our containing block.
  65. // 4. Flex container contents changed. If (and only if) it results in a new layed out size, its parent needs to be
  66. // reformatted again. If so, it should be able to reuse the flex container's layout cache.
  67. //
  68. // We don't attempt to optimize these situations for now, simply continue with dirtying the parent node.
  69. DirtyParentNode(element);
  70. return;
  71. }
  72. if (IsChildDirty() && !IsLayoutBoundary())
  73. {
  74. DirtyParentNode(element);
  75. return;
  76. }
  77. }
  78. void LayoutNode::CommitLayout(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size, const Box* override_box,
  79. bool layout_constraint, Vector2f visible_overflow_size, float max_content_width, Optional<float> baseline_of_last_line)
  80. {
  81. // TODO: This is mixing slightly different concepts. Rather, it might be advantageous to separate what is the input
  82. // to the layout of the current element, and what is the output. That way we can e.g. set the containing block size
  83. // even if there is nothing to format (for example due to `display: none`), or if the element itself can be cached
  84. // despite ancestor changes. This way we can resume the layout here, without formatting its ancestors, if it is
  85. // dirtied in a non-parent-mutable way.
  86. // - E.g. consider scenario 3 above with Absolute element `display: none` to `display: block`.
  87. //
  88. // Conversely, the output of the layout passed in here can later be used by when formatting ancestors, when the
  89. // current element does not need a new layout by itself.
  90. committed_layout.emplace(CommittedLayout{
  91. containing_block_size,
  92. absolutely_positioning_containing_block_size,
  93. override_box ? Optional<Box>(*override_box) : Optional<Box>(),
  94. layout_constraint,
  95. visible_overflow_size,
  96. max_content_width,
  97. baseline_of_last_line,
  98. });
  99. ClearDirty();
  100. }
  101. bool LayoutNode::CommittedLayoutMatches(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size,
  102. const Box* override_box, bool layout_constraint) const
  103. {
  104. if (IsDirty())
  105. return false;
  106. if (!committed_layout.has_value())
  107. return false;
  108. if (committed_layout->containing_block_size != containing_block_size ||
  109. committed_layout->absolutely_positioning_containing_block_size != absolutely_positioning_containing_block_size)
  110. return false;
  111. // Layout under a constraint may make some simplifications that requires re-evaluation under a normal formatting mode.
  112. if (committed_layout->layout_constraint && !layout_constraint)
  113. return false;
  114. if (!override_box)
  115. return !committed_layout->override_box.has_value();
  116. const Box& compare_box = committed_layout->override_box.has_value() ? *committed_layout->override_box : element->GetBox();
  117. // In some situations, if we have an indefinite size on the committed box, we could see if the laid-out size
  118. // matches the input override box and use the cache here. However, because of cyclic-percentage rules with
  119. // containing block sizes, this is only correct in certain situations, particularly when the vertical size of
  120. // the containing block is indefinite. In this case the used containing block size should resolve to indefinite,
  121. // even after it is sized. Although, we don't actually implement this behavior for now, but once we do, we could
  122. // implement caching here in this case. For the horizontal axis, we are always required to re-evaluate any
  123. // children for which this box acts as a containing block for, thus we cannot use a cache mechanism here. See:
  124. // https://drafts.csswg.org/css-sizing/#cyclic-percentage-contribution
  125. return *override_box == compare_box;
  126. }
  127. bool LayoutNode::IsLayoutBoundary() const
  128. {
  129. // Layout boundary, a.k.a. reflow root.
  130. using namespace Style;
  131. auto& computed = element->GetComputedValues();
  132. // TODO: Should this be moved into PropagateDirtyToParent() instead? It's not really a layout boundary, or
  133. // maybe it's okay?
  134. if (computed.display() == Display::None)
  135. return true;
  136. const FormattingContextType formatting_context = FormattingContext::GetFormattingContextType(element);
  137. if (formatting_context == FormattingContextType::None)
  138. return false;
  139. if (computed.position() == Position::Absolute || computed.position() == Position::Fixed)
  140. return true;
  141. return false;
  142. }
  143. } // namespace Rml