LayoutNode.cpp 5.7 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. #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::PropagateDirtyToParent()
  35. {
  36. auto DirtyParentNode = [](Element* element) {
  37. if (Element* parent = element->GetParentNode())
  38. parent->GetLayoutNode()->SetDirty(DirtyLayoutType::Child);
  39. };
  40. if (IsSelfDirty())
  41. {
  42. // We may be able to skip formatting in ancestor elements if this is a layout boundary, in some scenarios. Consider
  43. // some scenarios for illustration:
  44. //
  45. // 1. Absolute element. `display: block` to `display: none`. This does not need a new layout. Same with margin and
  46. // size, only the current element need to be reformatted, not ancestors.
  47. // 2. Absolute element. `display: none` to `display: block`. This *does* need to be layed out, since we don't know
  48. // our static position or containing block. We could in principle ignore static position in some situations where
  49. // it is not used, and could in principle find our containing block. But it is tricky.
  50. // 3. Flex container contents changed. If (and only if) it results in a new layed out size, its parent needs to be
  51. // reformatted again. If so, it should be able to reuse the flex container's layout cache.
  52. //
  53. // Currently, we don't have all of this information here. So skip this for now.
  54. // - TODO: This information could be provided as part of DirtyLayout.
  55. // - TODO: Consider if some of this logic should be moved to the layout engine.
  56. //
  57. // ```
  58. // if (IsLayoutBoundary()) return;
  59. // ```
  60. DirtyParentNode(element);
  61. return;
  62. }
  63. if (IsChildDirty() && !IsLayoutBoundary())
  64. {
  65. DirtyParentNode(element);
  66. return;
  67. }
  68. }
  69. void LayoutNode::ClearDirty()
  70. {
  71. // Log::Message(Log::LT_INFO, "ClearDirty (was Self %d Child %d) Element: %s", (dirty_flag & DirtyLayoutType::DOM) != DirtyLayoutType::None,
  72. // (dirty_flag & DirtyLayoutType::Child) != DirtyLayoutType::None, element->GetAddress().c_str());
  73. dirty_flag = DirtyLayoutType::None;
  74. }
  75. void LayoutNode::SetDirty(DirtyLayoutType dirty_type)
  76. {
  77. // Log::Message(Log::LT_INFO, "SetDirty. Self %d Child %d Element: %s", (dirty_type & DirtyLayoutType::DOM) != DirtyLayoutType::None,
  78. // (dirty_type & DirtyLayoutType::Child) != DirtyLayoutType::None, element->GetAddress().c_str());
  79. dirty_flag = dirty_flag | dirty_type;
  80. committed_max_content_width.reset();
  81. committed_max_content_height.reset();
  82. }
  83. void LayoutNode::CommitLayout(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size, const Box* override_box,
  84. bool layout_constraint, Vector2f visible_overflow_size, Optional<float> baseline_of_last_line)
  85. {
  86. // TODO: This is mixing slightly different concepts. Rather, it might be advantageous to separate what is the input
  87. // to the layout of the current element, and what is the output. That way we can e.g. set the containing block size
  88. // even if there is nothing to format (for example due to `display: none`), or if the element itself can be cached
  89. // despite ancestor changes. This way we can resume the layout here, without formatting its ancestors, if it is
  90. // dirtied in a non-parent-mutable way.
  91. // - E.g. consider scenario 2 above with Absolute element `display: none` to `display: block`.
  92. //
  93. // Conversely, the output of the layout passed in here can later be used by when formatting ancestors, when the
  94. // current element does not need a new layout by itself.
  95. committed_layout.emplace(CommittedLayout{
  96. containing_block_size,
  97. absolutely_positioning_containing_block_size,
  98. override_box ? Optional<Box>(*override_box) : Optional<Box>(),
  99. layout_constraint,
  100. visible_overflow_size,
  101. baseline_of_last_line,
  102. });
  103. ClearDirty();
  104. }
  105. bool LayoutNode::IsLayoutBoundary() const
  106. {
  107. using namespace Style;
  108. auto& computed = element->GetComputedValues();
  109. // TODO: Should this be moved into PropagateDirtyToParent() instead? It's not really a layout boundary, or
  110. // maybe it's okay?
  111. if (computed.display() == Display::None)
  112. return true;
  113. const FormattingContextType formatting_context = FormattingContext::GetFormattingContextType(element);
  114. if (formatting_context == FormattingContextType::None)
  115. return false;
  116. if (computed.position() == Position::Absolute || computed.position() == Position::Fixed)
  117. return true;
  118. return false;
  119. }
  120. } // namespace Rml