[LFC][BFC] Display::Box interface should reflect that padding is optional.
[WebKit-https.git] / Source / WebCore / layout / FormattingContext.cpp
1 /*
2  * Copyright (C) 2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "FormattingContext.h"
28
29 #if ENABLE(LAYOUT_FORMATTING_CONTEXT)
30
31 #include "DisplayBox.h"
32 #include "LayoutBox.h"
33 #include "LayoutContainer.h"
34 #include "LayoutContext.h"
35 #include "LayoutDescendantIterator.h"
36 #include "Logging.h"
37 #include <wtf/IsoMallocInlines.h>
38 #include <wtf/text/TextStream.h>
39
40 namespace WebCore {
41 namespace Layout {
42
43 WTF_MAKE_ISO_ALLOCATED_IMPL(FormattingContext);
44
45 FormattingContext::FormattingContext(const Box& formattingContextRoot)
46     : m_root(makeWeakPtr(const_cast<Box&>(formattingContextRoot)))
47 {
48 }
49
50 FormattingContext::~FormattingContext()
51 {
52 }
53
54 void FormattingContext::computeFloatingHeightAndMargin(LayoutContext& layoutContext, const Box& layoutBox, Display::Box& displayBox) const
55 {
56     auto heightAndMargin = Geometry::floatingHeightAndMargin(layoutContext, layoutBox);
57     displayBox.setContentBoxHeight(heightAndMargin.height);
58     displayBox.moveVertically(heightAndMargin.margin.top);
59     ASSERT(!heightAndMargin.collapsedMargin);
60     displayBox.setVerticalMargin(heightAndMargin.margin);
61     displayBox.setVerticalNonCollapsedMargin(heightAndMargin.margin);
62 }
63
64 void FormattingContext::computeFloatingWidthAndMargin(LayoutContext& layoutContext, const Box& layoutBox, Display::Box& displayBox) const
65 {
66     auto widthAndMargin = Geometry::floatingWidthAndMargin(layoutContext, *this, layoutBox);
67     displayBox.setContentBoxWidth(widthAndMargin.width);
68     displayBox.moveHorizontally(widthAndMargin.margin.left);
69     displayBox.setHorizontalMargin(widthAndMargin.margin);
70 }
71
72 void FormattingContext::computeOutOfFlowHorizontalGeometry(LayoutContext& layoutContext, const Box& layoutBox, Display::Box& displayBox) const
73 {
74     auto horizontalGeometry = Geometry::outOfFlowHorizontalGeometry(layoutContext, *this, layoutBox);
75     displayBox.setLeft(horizontalGeometry.left + horizontalGeometry.widthAndMargin.margin.left);
76     displayBox.setContentBoxWidth(horizontalGeometry.widthAndMargin.width);
77     displayBox.setHorizontalMargin(horizontalGeometry.widthAndMargin.margin);
78 }
79
80 void FormattingContext::computeOutOfFlowVerticalGeometry(LayoutContext& layoutContext, const Box& layoutBox, Display::Box& displayBox) const
81 {
82     auto verticalGeometry = Geometry::outOfFlowVerticalGeometry(layoutContext, layoutBox);
83     displayBox.setTop(verticalGeometry.top + verticalGeometry.heightAndMargin.margin.top);
84     displayBox.setContentBoxHeight(verticalGeometry.heightAndMargin.height);
85     ASSERT(!verticalGeometry.heightAndMargin.collapsedMargin);
86     displayBox.setVerticalMargin(verticalGeometry.heightAndMargin.margin);
87     displayBox.setVerticalNonCollapsedMargin(verticalGeometry.heightAndMargin.margin);
88 }
89
90 void FormattingContext::computeBorderAndPadding(LayoutContext& layoutContext, const Box& layoutBox, Display::Box& displayBox) const
91 {
92     displayBox.setBorder(Geometry::computedBorder(layoutContext, layoutBox));
93     displayBox.setPadding(Geometry::computedPadding(layoutContext, layoutBox));
94 }
95
96 void FormattingContext::placeInFlowPositionedChildren(LayoutContext& layoutContext, const Container& container) const
97 {
98     // If this container also establishes a formatting context, then positioning already has happend in that the formatting context.
99     if (container.establishesFormattingContext() && &container != &root())
100         return;
101
102     LOG_WITH_STREAM(FormattingContextLayout, stream << "Start: move in-flow positioned children -> context: " << &layoutContext << " parent: " << &container);
103     for (auto& layoutBox : childrenOfType<Box>(container)) {
104         if (!layoutBox.isInFlowPositioned())
105             continue;
106         computeInFlowPositionedPosition(layoutContext, layoutBox, *layoutContext.displayBoxForLayoutBox(layoutBox));
107     }
108     LOG_WITH_STREAM(FormattingContextLayout, stream << "End: move in-flow positioned children -> context: " << &layoutContext << " parent: " << &container);
109 }
110
111 void FormattingContext::layoutOutOfFlowDescendants(LayoutContext& layoutContext, const Box& layoutBox) const
112 {
113     // Initial containing block by definition is a containing block.
114     if (!layoutBox.isPositioned() && !layoutBox.isInitialContainingBlock())
115         return;
116
117     if (!is<Container>(layoutBox))
118         return;
119
120     auto& container = downcast<Container>(layoutBox);
121     if (!container.hasChild())
122         return;
123
124     LOG_WITH_STREAM(FormattingContextLayout, stream << "Start: layout out-of-flow descendants -> context: " << &layoutContext << " root: " << &root());
125
126     for (auto& outOfFlowBox : container.outOfFlowDescendants()) {
127         auto& layoutBox = *outOfFlowBox;
128         auto& displayBox = layoutContext.createDisplayBox(layoutBox);
129
130         ASSERT(layoutBox.establishesFormattingContext());
131         auto formattingContext = layoutContext.formattingContext(layoutBox);
132
133         computeBorderAndPadding(layoutContext, layoutBox, displayBox);
134         computeOutOfFlowHorizontalGeometry(layoutContext, layoutBox, displayBox);
135
136         formattingContext->layout(layoutContext, layoutContext.establishedFormattingState(layoutBox));
137
138         computeOutOfFlowVerticalGeometry(layoutContext, layoutBox, displayBox);
139         layoutOutOfFlowDescendants(layoutContext, layoutBox);
140     }
141     LOG_WITH_STREAM(FormattingContextLayout, stream << "End: layout out-of-flow descendants -> context: " << &layoutContext << " root: " << &root());
142 }
143
144 Display::Box FormattingContext::mapBoxToAncestor(const LayoutContext& layoutContext, const Box& layoutBox, const Container& ancestor)
145 {
146     ASSERT(layoutBox.isDescendantOf(ancestor));
147
148     auto* displayBox = layoutContext.displayBoxForLayoutBox(layoutBox);
149     ASSERT(displayBox);
150     auto topLeft = displayBox->topLeft();
151
152     auto* containingBlock = layoutBox.containingBlock();
153     for (; containingBlock && containingBlock != &ancestor; containingBlock = containingBlock->containingBlock())
154         topLeft.moveBy(layoutContext.displayBoxForLayoutBox(*containingBlock)->topLeft());
155
156     if (!containingBlock) {
157         ASSERT_NOT_REACHED();
158         return Display::Box(*displayBox);
159     }
160
161     auto mappedDisplayBox = Display::Box(*displayBox);
162     mappedDisplayBox.setTopLeft(topLeft);
163     return mappedDisplayBox;
164 }
165
166 Position FormattingContext::mapTopLeftToAncestor(const LayoutContext& layoutContext, const Box& layoutBox, const Container& ancestor)
167 {
168     ASSERT(layoutBox.isDescendantOf(ancestor));
169     return mapCoordinateToAncestor(layoutContext, layoutContext.displayBoxForLayoutBox(layoutBox)->topLeft(), *layoutBox.containingBlock(), ancestor);
170 }
171
172 Position FormattingContext::mapCoordinateToAncestor(const LayoutContext& layoutContext, Position position, const Container& containingBlock, const Container& ancestor)
173 {
174     auto mappedPosition = position;
175     auto* container = &containingBlock;
176     for (; container && container != &ancestor; container = container->containingBlock())
177         mappedPosition.moveBy(layoutContext.displayBoxForLayoutBox(*container)->topLeft());
178
179     if (!container) {
180         ASSERT_NOT_REACHED();
181         return position;
182     }
183
184     return mappedPosition;
185 }
186
187 #ifndef NDEBUG
188 void FormattingContext::validateGeometryConstraintsAfterLayout(const LayoutContext& layoutContext) const
189 {
190     if (!is<Container>(root()))
191         return;
192     auto& formattingContextRoot = downcast<Container>(root());
193     // FIXME: add a descendantsOfType<> flavor that stops at nested formatting contexts
194     for (auto& layoutBox : descendantsOfType<Box>(formattingContextRoot)) {
195         if (&layoutBox.formattingContextRoot() != &formattingContextRoot)
196             continue;
197         auto& containingBlockDisplayBox = *layoutContext.displayBoxForLayoutBox(*layoutBox.containingBlock());
198         auto* displayBox = layoutContext.displayBoxForLayoutBox(layoutBox);
199         ASSERT(displayBox);
200
201         // 10.3.3 Block-level, non-replaced elements in normal flow
202         // 10.3.7 Absolutely positioned, non-replaced elements
203         if ((layoutBox.isBlockLevelBox() || layoutBox.isOutOfFlowPositioned()) && !layoutBox.replaced()) {
204             // margin-left + border-left-width + padding-left + width + padding-right + border-right-width + margin-right = width of containing block
205             auto containingBlockWidth = containingBlockDisplayBox.contentBoxWidth();
206             ASSERT(displayBox->marginLeft() + displayBox->borderLeft() + displayBox->paddingLeft().value_or(0) + displayBox->contentBoxWidth()
207                 + displayBox->paddingRight().value_or(0) + displayBox->borderRight() + displayBox->marginRight() == containingBlockWidth);
208         }
209
210         // 10.6.4 Absolutely positioned, non-replaced elements
211         if (layoutBox.isOutOfFlowPositioned() && !layoutBox.replaced()) {
212             // top + margin-top + border-top-width + padding-top + height + padding-bottom + border-bottom-width + margin-bottom + bottom = height of containing block
213             auto containingBlockHeight = containingBlockDisplayBox.contentBoxHeight();
214             ASSERT(displayBox->top() + displayBox->marginTop() + displayBox->borderTop() + displayBox->paddingTop().value_or(0) + displayBox->contentBoxHeight()
215                 + displayBox->paddingBottom().value_or(0) + displayBox->borderBottom() + displayBox->marginBottom() == containingBlockHeight);
216         }
217     }
218 }
219 #endif
220
221 }
222 }
223 #endif