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#pragma once
27
28#if ENABLE(LAYOUT_FORMATTING_CONTEXT)
29
30#include "FormattingContext.h"
31#include "MarginTypes.h"
32#include <wtf/HashMap.h>
33#include <wtf/IsoMalloc.h>
34
35namespace WebCore {
36
37class LayoutUnit;
38
39namespace Layout {
40
41class BlockFormattingState;
42class Box;
43class FloatingContext;
44
45// This class implements the layout logic for block formatting contexts.
46// https://www.w3.org/TR/CSS22/visuren.html#block-formatting
47class BlockFormattingContext : public FormattingContext {
48 WTF_MAKE_ISO_ALLOCATED(BlockFormattingContext);
49public:
50 BlockFormattingContext(const Box& formattingContextRoot, BlockFormattingState&);
51
52 void layout() const override;
53
54private:
55 void layoutFormattingContextRoot(FloatingContext&, const Box&) const;
56 void placeInFlowPositionedChildren(const Box&) const;
57
58 void computeWidthAndMargin(const Box&) const;
59 void computeHeightAndMargin(const Box&) const;
60
61 void computeStaticPosition(const FloatingContext&, const Box&) const;
62 void computeFloatingPosition(const FloatingContext&, const Box&) const;
63 void computePositionToAvoidFloats(const FloatingContext&, const Box&) const;
64
65 void computeEstimatedVerticalPosition(const Box&) const;
66 void computeEstimatedVerticalPositionForAncestors(const Box&) const;
67 void computeEstimatedVerticalPositionForFormattingRoot(const Box&) const;
68 void computeEstimatedVerticalPositionForFloatClear(const FloatingContext&, const Box&) const;
69
70 void computeIntrinsicWidthConstraints() const override;
71 LayoutUnit verticalPositionWithMargin(const Box&, const UsedVerticalMargin&) const;
72
73 // This class implements positioning and sizing for boxes participating in a block formatting context.
74 class Geometry : public FormattingContext::Geometry {
75 public:
76 static HeightAndMargin inFlowHeightAndMargin(const LayoutState&, const Box&, UsedVerticalValues);
77 static WidthAndMargin inFlowWidthAndMargin(const LayoutState&, const Box&, UsedHorizontalValues);
78
79 static Point staticPosition(const LayoutState&, const Box&);
80
81 static bool intrinsicWidthConstraintsNeedChildrenWidth(const Box&);
82 static IntrinsicWidthConstraints intrinsicWidthConstraints(const LayoutState&, const Box&);
83
84 private:
85 static HeightAndMargin inFlowNonReplacedHeightAndMargin(const LayoutState&, const Box&, UsedVerticalValues);
86 static WidthAndMargin inFlowNonReplacedWidthAndMargin(const LayoutState&, const Box&, UsedHorizontalValues);
87 static WidthAndMargin inFlowReplacedWidthAndMargin(const LayoutState&, const Box&, UsedHorizontalValues);
88 static Point staticPositionForOutOfFlowPositioned(const LayoutState&, const Box&);
89 };
90
91 // This class implements margin collapsing for block formatting context.
92 class MarginCollapse {
93 public:
94 static UsedVerticalMargin::CollapsedValues collapsedVerticalValues(const LayoutState&, const Box&, const UsedVerticalMargin::NonCollapsedValues&);
95
96 static EstimatedMarginBefore estimatedMarginBefore(const LayoutState&, const Box&);
97 static LayoutUnit marginBeforeIgnoringCollapsingThrough(const LayoutState&, const Box&, const UsedVerticalMargin::NonCollapsedValues&);
98 static void updateMarginAfterForPreviousSibling(const LayoutState&, const Box&);
99 static void updatePositiveNegativeMarginValues(const LayoutState&, const Box&);
100
101 static bool marginBeforeCollapsesWithParentMarginBefore(const LayoutState&, const Box&);
102 static bool marginBeforeCollapsesWithFirstInFlowChildMarginBefore(const LayoutState&, const Box&);
103 static bool marginBeforeCollapsesWithParentMarginAfter(const LayoutState&, const Box&);
104 static bool marginBeforeCollapsesWithPreviousSiblingMarginAfter(const LayoutState&, const Box&);
105
106 static bool marginAfterCollapsesWithParentMarginAfter(const LayoutState&, const Box&);
107 static bool marginAfterCollapsesWithLastInFlowChildMarginAfter(const LayoutState&, const Box&);
108 static bool marginAfterCollapsesWithParentMarginBefore(const LayoutState&, const Box&);
109 static bool marginAfterCollapsesWithNextSiblingMarginBefore(const LayoutState&, const Box&);
110 static bool marginAfterCollapsesWithSiblingMarginBeforeWithClearance(const LayoutState&, const Box&);
111
112 static bool marginsCollapseThrough(const LayoutState&, const Box&);
113
114 private:
115 enum class MarginType { Before, After };
116 static PositiveAndNegativeVerticalMargin::Values positiveNegativeValues(const LayoutState&, const Box&, MarginType);
117 static PositiveAndNegativeVerticalMargin::Values positiveNegativeMarginBefore(const LayoutState&, const Box&, const UsedVerticalMargin::NonCollapsedValues&);
118 static PositiveAndNegativeVerticalMargin::Values positiveNegativeMarginAfter(const LayoutState&, const Box&, const UsedVerticalMargin::NonCollapsedValues&);
119 };
120
121 class Quirks {
122 public:
123 static bool needsStretching(const LayoutState&, const Box&);
124 static HeightAndMargin stretchedInFlowHeight(const LayoutState&, const Box&, HeightAndMargin);
125
126 static bool shouldIgnoreCollapsedQuirkMargin(const LayoutState&, const Box&);
127 static bool shouldIgnoreMarginBefore(const LayoutState&, const Box&);
128 static bool shouldIgnoreMarginAfter(const LayoutState&, const Box&);
129 };
130
131 void setEstimatedMarginBefore(const Box&, const EstimatedMarginBefore&) const;
132 void removeEstimatedMarginBefore(const Box& layoutBox) const { m_estimatedMarginBeforeList.remove(&layoutBox); }
133 bool hasEstimatedMarginBefore(const Box&) const;
134#ifndef NDEBUG
135 EstimatedMarginBefore estimatedMarginBefore(const Box& layoutBox) const { return m_estimatedMarginBeforeList.get(&layoutBox); }
136 bool hasPrecomputedMarginBefore(const Box&) const;
137#endif
138
139 BlockFormattingState& formattingState() const { return downcast<BlockFormattingState>(FormattingContext::formattingState()); }
140
141private:
142 mutable HashMap<const Box*, EstimatedMarginBefore> m_estimatedMarginBeforeList;
143};
144
145}
146}
147#endif
148