2 * Copyright (C) 2018 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "LayoutContext.h"
29 #if ENABLE(LAYOUT_FORMATTING_CONTEXT)
31 #include "BlockFormattingContext.h"
32 #include "BlockFormattingState.h"
33 #include "DisplayBox.h"
34 #include "InlineFormattingContext.h"
35 #include "InlineFormattingState.h"
36 #include "LayoutBox.h"
37 #include "LayoutContainer.h"
38 #include <wtf/IsoMallocInlines.h>
43 WTF_MAKE_ISO_ALLOCATED_IMPL(LayoutContext);
45 LayoutContext::LayoutContext(const Box& root)
46 : m_root(makeWeakPtr(const_cast<Box&>(root)))
50 void LayoutContext::updateLayout()
52 auto context = formattingContext(*m_root);
53 auto& state = establishedFormattingState(*m_root, *context);
54 context->layout(*this, state);
57 Display::Box& LayoutContext::createDisplayBox(const Box& layoutBox)
59 std::unique_ptr<Display::Box> displayBox(new Display::Box());
60 auto* displayBoxPtr = displayBox.get();
61 m_layoutToDisplayBox.add(&layoutBox, WTFMove(displayBox));
62 return *displayBoxPtr;
65 FormattingState& LayoutContext::formattingStateForBox(const Box& layoutBox) const
67 auto& root = layoutBox.formattingContextRoot();
68 RELEASE_ASSERT(m_formattingStates.contains(&root));
69 return *m_formattingStates.get(&root);
72 FormattingState& LayoutContext::establishedFormattingState(const Box& formattingContextRoot, const FormattingContext& context)
74 return *m_formattingStates.ensure(&formattingContextRoot, [this, &context] {
75 return context.createFormattingState(context.createOrFindFloatingState());
79 std::unique_ptr<FormattingContext> LayoutContext::formattingContext(const Box& formattingContextRoot)
81 if (formattingContextRoot.establishesBlockFormattingContext())
82 return std::make_unique<BlockFormattingContext>(formattingContextRoot, *this);
84 if (formattingContextRoot.establishesInlineFormattingContext())
85 return std::make_unique<InlineFormattingContext>(formattingContextRoot, *this);