64d9ff992155f8cc04ac3fc73a5abf08f89623c6
[WebKit-https.git] / Source / WebCore / rendering / RenderGrid.h
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  * Copyright (C) 2013, 2014 Igalia S.L.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifndef RenderGrid_h
28 #define RenderGrid_h
29
30 #if ENABLE(CSS_GRID_LAYOUT)
31
32 #include "GridPositionsResolver.h"
33 #include "OrderIterator.h"
34 #include "RenderBlock.h"
35
36 namespace WebCore {
37
38 class GridArea;
39 class GridSpan;
40 class GridTrack;
41 class GridItemWithSpan;
42
43 struct ContentAlignmentData;
44
45 enum GridAxisPosition {GridAxisStart, GridAxisEnd, GridAxisCenter};
46
47 enum TrackSizeComputationPhase {
48     ResolveIntrinsicMinimums,
49     ResolveContentBasedMinimums,
50     ResolveMaxContentMinimums,
51     ResolveIntrinsicMaximums,
52     ResolveMaxContentMaximums,
53     MaximizeTracks,
54 };
55
56 class RenderGrid final : public RenderBlock {
57 public:
58     RenderGrid(Element&, RenderStyle&&);
59     virtual ~RenderGrid();
60
61     Element& element() const { return downcast<Element>(nodeForNonAnonymous()); }
62
63     void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
64     void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) override;
65
66     bool avoidsFloats() const override { return true; }
67     bool canDropAnonymousBlockChild() const override { return false; }
68
69     Vector<LayoutUnit> trackSizesForComputedStyle(GridTrackSizingDirection) const;
70
71     const Vector<LayoutUnit>& columnPositions() const { return m_columnPositions; }
72     const Vector<LayoutUnit>& rowPositions() const { return m_rowPositions; }
73
74     size_t autoRepeatCountForDirection(GridTrackSizingDirection) const;
75
76 private:
77     const char* renderName() const override;
78     bool isRenderGrid() const override { return true; }
79     void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const override;
80
81     Optional<LayoutUnit> computeIntrinsicLogicalContentHeightUsing(Length logicalHeightLength, Optional<LayoutUnit> intrinsicContentHeight, LayoutUnit borderAndPadding) const override;
82
83     class GridIterator;
84     class GridSizingData;
85     enum SizingOperation { TrackSizing, IntrinsicSizeComputation };
86     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&, LayoutUnit& baseSizesWithoutMaximization, LayoutUnit& growthLimitsWithoutMaximization) const;
87     void computeFlexSizedTracksGrowth(GridTrackSizingDirection, SizingOperation, Vector<GridTrack>&, const Vector<unsigned>& flexibleSizedTracksIndex, double flexFraction, Vector<LayoutUnit>& increments, LayoutUnit& totalGrowth) const;
88     LayoutUnit computeUsedBreadthOfMinLength(const GridTrackSize&, LayoutUnit maxSize) const;
89     LayoutUnit computeUsedBreadthOfMaxLength(const GridTrackSize&, LayoutUnit usedBreadth, LayoutUnit maxSize) const;
90     void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection, GridSizingData&) const;
91
92     void ensureGridSize(unsigned maximumRowSize, unsigned maximumColumnSize);
93     void insertItemIntoGrid(RenderBox&, const GridArea&);
94
95     unsigned computeAutoRepeatTracksCount(GridTrackSizingDirection, SizingOperation) const;
96
97     typedef ListHashSet<size_t> OrderedTrackIndexSet;
98     std::unique_ptr<OrderedTrackIndexSet> computeEmptyTracksForAutoRepeat(GridTrackSizingDirection) const;
99
100     bool hasAutoRepeatEmptyTracks(GridTrackSizingDirection) const;
101     bool isEmptyAutoRepeatTrack(GridTrackSizingDirection, unsigned track) const;
102
103     void placeItemsOnGrid(SizingOperation);
104     void populateExplicitGridAndOrderIterator();
105     std::unique_ptr<GridArea> createEmptyGridAreaAtSpecifiedPositionsOutsideGrid(const RenderBox&, GridTrackSizingDirection, const GridSpan&) const;
106     void placeSpecifiedMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
107     void placeAutoMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
108     typedef std::pair<unsigned, unsigned> AutoPlacementCursor;
109     void placeAutoMajorAxisItemOnGrid(RenderBox&, AutoPlacementCursor&);
110     GridTrackSizingDirection autoPlacementMajorAxisDirection() const;
111     GridTrackSizingDirection autoPlacementMinorAxisDirection() const;
112
113     bool canPerformSimplifiedLayout() const final;
114     void prepareChildForPositionedLayout(RenderBox&);
115     void layoutPositionedObject(RenderBox&, bool relayoutChildren, bool fixedPositionObjectsOnly) override;
116     void offsetAndBreadthForPositionedChild(const RenderBox&, GridTrackSizingDirection, LayoutUnit& offset, LayoutUnit& breadth);
117
118     void computeIntrinsicLogicalHeight(GridSizingData&);
119     LayoutUnit computeTrackBasedLogicalHeight(const GridSizingData&) const;
120     void computeTrackSizesForDirection(GridTrackSizingDirection, GridSizingData&, LayoutUnit freeSpace);
121
122     void repeatTracksSizingIfNeeded(GridSizingData&, LayoutUnit availableSpaceForColumns, LayoutUnit availableSpaceForRows);
123
124     void layoutGridItems(GridSizingData&);
125     void populateGridPositionsForDirection(GridSizingData&, GridTrackSizingDirection);
126     void clearGrid();
127
128     static bool shouldProcessTrackForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
129     static bool trackShouldGrowBeyondGrowthLimitsForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
130     static void markAsInfinitelyGrowableForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
131     static void updateTrackSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
132     LayoutUnit currentItemSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
133
134     typedef struct GridItemsSpanGroupRange GridItemsSpanGroupRange;
135     void resolveContentBasedTrackSizingFunctionsForNonSpanningItems(GridTrackSizingDirection, const GridSpan&, RenderBox& gridItem, GridTrack&, GridSizingData&) const;
136     template <TrackSizeComputationPhase> void resolveContentBasedTrackSizingFunctionsForItems(GridTrackSizingDirection, GridSizingData&, const GridItemsSpanGroupRange&) const;
137     template <TrackSizeComputationPhase> void distributeSpaceToTracks(Vector<GridTrack*>&, Vector<GridTrack*>* growBeyondGrowthLimitsTracks, LayoutUnit& availableLogicalSpace) const;
138
139     typedef HashSet<unsigned, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> TrackIndexSet;
140     double computeFlexFactorUnitSize(const Vector<GridTrack>&, GridTrackSizingDirection, SizingOperation, double flexFactorSum, LayoutUnit leftOverSpace, const Vector<unsigned, 8>& flexibleTracksIndexes, std::unique_ptr<TrackIndexSet> tracksToTreatAsInflexible = nullptr) const;
141     double findFlexFactorUnitSize(const Vector<GridTrack>&, const GridSpan&, GridTrackSizingDirection, SizingOperation, LayoutUnit spaceToFill) const;
142
143     const GridTrackSize& rawGridTrackSize(GridTrackSizingDirection, unsigned) const;
144     GridTrackSize gridTrackSize(GridTrackSizingDirection, unsigned, SizingOperation) const;
145
146     bool updateOverrideContainingBlockContentSizeForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
147     LayoutUnit logicalHeightForChild(RenderBox&) const;
148     LayoutUnit minSizeForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
149     LayoutUnit minContentForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
150     LayoutUnit maxContentForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
151     GridAxisPosition columnAxisPositionForChild(const RenderBox&) const;
152     GridAxisPosition rowAxisPositionForChild(const RenderBox&) const;
153     LayoutUnit columnAxisOffsetForChild(const RenderBox&) const;
154     LayoutUnit rowAxisOffsetForChild(const RenderBox&) const;
155     ContentAlignmentData computeContentPositionAndDistributionOffset(GridTrackSizingDirection, const LayoutUnit& availableFreeSpace, unsigned numberOfGridTracks) const;
156     LayoutPoint findChildLogicalPosition(const RenderBox&) const;
157     GridArea cachedGridArea(const RenderBox&) const;
158     GridSpan cachedGridSpan(const RenderBox&, GridTrackSizingDirection) const;
159
160
161     LayoutUnit gridAreaBreadthForChild(const RenderBox& child, GridTrackSizingDirection, const GridSizingData&) const;
162     LayoutUnit gridAreaBreadthForChildIncludingAlignmentOffsets(const RenderBox&, GridTrackSizingDirection, const GridSizingData&) const;
163     LayoutUnit assumedRowsSizeForOrthogonalChild(const RenderBox&, SizingOperation) const;
164
165     void applyStretchAlignmentToTracksIfNeeded(GridTrackSizingDirection, GridSizingData&);
166
167     void paintChildren(PaintInfo& forSelf, const LayoutPoint& paintOffset, PaintInfo& forChild, bool usePrintRect) override;
168     bool needToStretchChildLogicalHeight(const RenderBox&) const;
169     LayoutUnit marginLogicalHeightForChild(const RenderBox&) const;
170     LayoutUnit computeMarginLogicalSizeForChild(GridTrackSizingDirection, const RenderBox&) const;
171     LayoutUnit availableAlignmentSpaceForChildBeforeStretching(LayoutUnit gridAreaBreadthForChild, const RenderBox&) const;
172     StyleSelfAlignmentData justifySelfForChild(const RenderBox&) const;
173     StyleSelfAlignmentData alignSelfForChild(const RenderBox&) const;
174     void applyStretchAlignmentToChildIfNeeded(RenderBox&);
175     bool hasAutoSizeInColumnAxis(const RenderBox& child) const { return isHorizontalWritingMode() ? child.style().height().isAuto() : child.style().width().isAuto(); }
176     bool hasAutoSizeInRowAxis(const RenderBox& child) const { return isHorizontalWritingMode() ? child.style().width().isAuto() : child.style().height().isAuto(); }
177     bool allowedToStretchChildAlongColumnAxis(const RenderBox& child) const { return alignSelfForChild(child).position() == ItemPositionStretch && hasAutoSizeInColumnAxis(child) && !hasAutoMarginsInColumnAxis(child); }
178     bool allowedToStretchChildAlongRowAxis(const RenderBox& child) const { return justifySelfForChild(child).position() == ItemPositionStretch && hasAutoSizeInRowAxis(child) && !hasAutoMarginsInRowAxis(child); }
179     bool hasAutoMarginsInColumnAxis(const RenderBox&) const;
180     bool hasAutoMarginsInRowAxis(const RenderBox&) const;
181     void resetAutoMarginsAndLogicalTopInColumnAxis(RenderBox& child);
182     void updateAutoMarginsInColumnAxisIfNeeded(RenderBox&);
183     void updateAutoMarginsInRowAxisIfNeeded(RenderBox&);
184
185 #ifndef NDEBUG
186     bool tracksAreWiderThanMinTrackBreadth(GridTrackSizingDirection, GridSizingData&);
187 #endif
188
189     LayoutUnit gridGapForDirection(GridTrackSizingDirection) const;
190     LayoutUnit guttersSize(GridTrackSizingDirection, unsigned startLine, unsigned span) const;
191
192     bool spanningItemCrossesFlexibleSizedTracks(const GridSpan&, GridTrackSizingDirection, SizingOperation) const;
193
194     unsigned gridColumnCount() const;
195     unsigned gridRowCount() const;
196
197     LayoutUnit translateRTLCoordinate(LayoutUnit) const;
198
199     bool isOrthogonalChild(const RenderBox&) const;
200     GridTrackSizingDirection flowAwareDirectionForChild(const RenderBox&, GridTrackSizingDirection) const;
201
202     Vector<Vector<Vector<RenderBox*, 1>>> m_grid;
203     Vector<LayoutUnit> m_columnPositions;
204     Vector<LayoutUnit> m_rowPositions;
205     LayoutUnit m_offsetBetweenColumns;
206     LayoutUnit m_offsetBetweenRows;
207     HashMap<const RenderBox*, GridArea> m_gridItemArea;
208     OrderIterator m_orderIterator;
209
210     Optional<LayoutUnit> m_minContentHeight;
211     Optional<LayoutUnit> m_maxContentHeight;
212
213     int m_smallestColumnStart;
214     int m_smallestRowStart;
215
216     unsigned m_autoRepeatColumns { 0 };
217     unsigned m_autoRepeatRows { 0 };
218
219     bool m_hasAnyOrthogonalChild;
220
221     bool m_gridIsDirty { true };
222
223     std::unique_ptr<OrderedTrackIndexSet> m_autoRepeatEmptyColumns { nullptr };
224     std::unique_ptr<OrderedTrackIndexSet> m_autoRepeatEmptyRows { nullptr };
225 };
226
227 size_t inline RenderGrid::autoRepeatCountForDirection(GridTrackSizingDirection direction) const
228 {
229     return direction == ForColumns ? m_autoRepeatColumns : m_autoRepeatRows;
230 }
231
232 } // namespace WebCore
233
234 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderGrid, isRenderGrid())
235
236 #endif /* ENABLE(CSS_GRID_LAYOUT) */
237
238 #endif // RenderGrid_h