| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /*
- * This source file is part of RmlUi, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://github.com/mikke89/RmlUi
- *
- * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
- * Copyright (c) 2019-2023 The RmlUi Team, and contributors
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
- #include "LayoutNode.h"
- #include "../../../Include/RmlUi/Core/ComputedValues.h"
- #include "../../../Include/RmlUi/Core/Element.h"
- #include "../../../Include/RmlUi/Core/Log.h"
- #include "FormattingContext.h"
- namespace Rml {
- void LayoutNode::SetDirty(DirtyLayoutType dirty_type)
- {
- // Log::Message(Log::LT_INFO, "SetDirty. Self %d Child %d Element: %s", (dirty_type & DirtyLayoutType::DOM) != DirtyLayoutType::None,
- // (dirty_type & DirtyLayoutType::Child) != DirtyLayoutType::None, element->GetAddress().c_str());
- dirty_flag = dirty_flag | dirty_type;
- committed_max_content_width.reset();
- committed_max_content_height.reset();
- }
- void LayoutNode::ClearDirty()
- {
- // Log::Message(Log::LT_INFO, "ClearDirty (was Self %d Child %d) Element: %s", (dirty_flag & DirtyLayoutType::DOM) != DirtyLayoutType::None,
- // (dirty_flag & DirtyLayoutType::Child) != DirtyLayoutType::None, element->GetAddress().c_str());
- dirty_flag = DirtyLayoutType::None;
- }
- void LayoutNode::PropagateDirtyToParent()
- {
- auto DirtyParentNode = [](Element* element) {
- if (Element* parent = element->GetParentNode())
- parent->GetLayoutNode()->SetDirty(DirtyLayoutType::Child);
- };
- if (IsSelfDirty())
- {
- // @performance We may be able to skip formatting in ancestor elements if this is a layout boundary, in some
- // scenarios. Consider the following for illustration:
- //
- // 1. Absolute element. `display: block` to `display: none`. This does not need parents to be layed out.
- // 2. Absolute element. `width` or `margin`. Same, this does not need parents to be layed out. Only the current
- // element needs to be reformatted, not ancestors.
- // 3. Absolute element. `display: none` to `display: block`. This *does* need parents to be layed out, since we don't know
- // our static position or containing block. We could in principle ignore static position in some situations where
- // it is not used, and could in principle find our containing block.
- // 4. Flex container contents changed. If (and only if) it results in a new layed out size, its parent needs to be
- // reformatted again. If so, it should be able to reuse the flex container's layout cache.
- //
- // We don't attempt to optimize these situations for now, simply continue with dirtying the parent node.
- DirtyParentNode(element);
- return;
- }
- if (IsChildDirty() && !IsLayoutBoundary())
- {
- DirtyParentNode(element);
- return;
- }
- }
- void LayoutNode::CommitLayout(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size, const Box* override_box,
- bool layout_constraint, Vector2f visible_overflow_size, float max_content_width, Optional<float> baseline_of_last_line)
- {
- // TODO: This is mixing slightly different concepts. Rather, it might be advantageous to separate what is the input
- // to the layout of the current element, and what is the output. That way we can e.g. set the containing block size
- // even if there is nothing to format (for example due to `display: none`), or if the element itself can be cached
- // despite ancestor changes. This way we can resume the layout here, without formatting its ancestors, if it is
- // dirtied in a non-parent-mutable way.
- // - E.g. consider scenario 3 above with Absolute element `display: none` to `display: block`.
- //
- // Conversely, the output of the layout passed in here can later be used by when formatting ancestors, when the
- // current element does not need a new layout by itself.
- committed_layout.emplace(CommittedLayout{
- containing_block_size,
- absolutely_positioning_containing_block_size,
- override_box ? Optional<Box>(*override_box) : Optional<Box>(),
- layout_constraint,
- visible_overflow_size,
- max_content_width,
- baseline_of_last_line,
- });
- ClearDirty();
- }
- bool LayoutNode::CommittedLayoutMatches(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size,
- const Box* override_box, bool layout_constraint) const
- {
- if (IsDirty())
- return false;
- if (!committed_layout.has_value())
- return false;
- if (committed_layout->containing_block_size != containing_block_size ||
- committed_layout->absolutely_positioning_containing_block_size != absolutely_positioning_containing_block_size)
- return false;
- // Layout under a constraint may make some simplifications that requires re-evaluation under a normal formatting mode.
- if (committed_layout->layout_constraint && !layout_constraint)
- return false;
- if (!override_box)
- return !committed_layout->override_box.has_value();
- const Box& compare_box = committed_layout->override_box.has_value() ? *committed_layout->override_box : element->GetBox();
- // In some situations, if we have an indefinite size on the committed box, we could see if the laid-out size
- // matches the input override box and use the cache here. However, because of cyclic-percentage rules with
- // containing block sizes, this is only correct in certain situations, particularly when the vertical size of
- // the containing block is indefinite. In this case the used containing block size should resolve to indefinite,
- // even after it is sized. Although, we don't actually implement this behavior for now, but once we do, we could
- // implement caching here in this case. For the horizontal axis, we are always required to re-evaluate any
- // children for which this box acts as a containing block for, thus we cannot use a cache mechanism here. See:
- // https://drafts.csswg.org/css-sizing/#cyclic-percentage-contribution
- return *override_box == compare_box;
- }
- bool LayoutNode::IsLayoutBoundary() const
- {
- // Layout boundary, a.k.a. reflow root.
- using namespace Style;
- auto& computed = element->GetComputedValues();
- // TODO: Should this be moved into PropagateDirtyToParent() instead? It's not really a layout boundary, or
- // maybe it's okay?
- if (computed.display() == Display::None)
- return true;
- const FormattingContextType formatting_context = FormattingContext::GetFormattingContextType(element);
- if (formatting_context == FormattingContextType::None)
- return false;
- if (computed.position() == Position::Absolute || computed.position() == Position::Fixed)
- return true;
- return false;
- }
- } // namespace Rml
|