[css-grid] Remove compilation flag ENABLE_CSS_GRID_LAYOUT
[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 #pragma once
28
29 #include "Grid.h"
30 #include "RenderBlock.h"
31
32 namespace WebCore {
33
34 class GridArea;
35 class GridSpan;
36 class GridTrack;
37 class GridItemWithSpan;
38
39 struct ContentAlignmentData;
40
41 enum GridAxisPosition {GridAxisStart, GridAxisEnd, GridAxisCenter};
42
43 enum TrackSizeComputationPhase {
44     ResolveIntrinsicMinimums,
45     ResolveContentBasedMinimums,
46     ResolveMaxContentMinimums,
47     ResolveIntrinsicMaximums,
48     ResolveMaxContentMaximums,
49     MaximizeTracks,
50 };
51
52 class RenderGrid final : public RenderBlock {
53 public:
54     RenderGrid(Element&, RenderStyle&&);
55     virtual ~RenderGrid();
56
57     Element& element() const { return downcast<Element>(nodeForNonAnonymous()); }
58
59     void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
60     void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) override;
61
62     bool avoidsFloats() const override { return true; }
63     bool canDropAnonymousBlockChild() const override { return false; }
64
65     void dirtyGrid();
66     Vector<LayoutUnit> trackSizesForComputedStyle(GridTrackSizingDirection) const;
67
68     const Vector<LayoutUnit>& columnPositions() const { return m_columnPositions; }
69     const Vector<LayoutUnit>& rowPositions() const { return m_rowPositions; }
70
71     unsigned autoRepeatCountForDirection(GridTrackSizingDirection direction) const { return m_grid.autoRepeatTracks(direction); }
72
73 private:
74     const char* renderName() const override;
75     bool isRenderGrid() const override { return true; }
76     void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const override;
77
78     void addChild(RenderObject* newChild, RenderObject* beforeChild) final;
79     void removeChild(RenderObject&) final;
80
81     bool explicitGridDidResize(const RenderStyle&) const;
82     bool namedGridLinesDefinitionDidChange(const RenderStyle&) const;
83
84     std::optional<LayoutUnit> computeIntrinsicLogicalContentHeightUsing(Length logicalHeightLength, std::optional<LayoutUnit> intrinsicContentHeight, LayoutUnit borderAndPadding) const override;
85
86     class GridSizingData;
87     enum SizingOperation { TrackSizing, IntrinsicSizeComputation };
88     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&, LayoutUnit& baseSizesWithoutMaximization, LayoutUnit& growthLimitsWithoutMaximization) const;
89     void computeFlexSizedTracksGrowth(GridTrackSizingDirection, const GridSizingData&, Vector<GridTrack>&, const Vector<unsigned>& flexibleSizedTracksIndex, double flexFraction, Vector<LayoutUnit>& increments, LayoutUnit& totalGrowth) const;
90     LayoutUnit computeUsedBreadthOfMinLength(const GridTrackSize&, LayoutUnit maxSize) const;
91     LayoutUnit computeUsedBreadthOfMaxLength(const GridTrackSize&, LayoutUnit usedBreadth, LayoutUnit maxSize) const;
92     void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection, GridSizingData&) const;
93
94     unsigned computeAutoRepeatTracksCount(GridTrackSizingDirection, SizingOperation) const;
95
96     std::unique_ptr<OrderedTrackIndexSet> computeEmptyTracksForAutoRepeat(Grid&, GridTrackSizingDirection) const;
97
98     void placeItemsOnGrid(Grid&, SizingOperation) const;
99     void populateExplicitGridAndOrderIterator(Grid&) const;
100     std::unique_ptr<GridArea> createEmptyGridAreaAtSpecifiedPositionsOutsideGrid(Grid&, const RenderBox&, GridTrackSizingDirection, const GridSpan&) const;
101     void placeSpecifiedMajorAxisItemsOnGrid(Grid&, const Vector<RenderBox*>&) const;
102     void placeAutoMajorAxisItemsOnGrid(Grid&, const Vector<RenderBox*>&) const;
103     typedef std::pair<unsigned, unsigned> AutoPlacementCursor;
104     void placeAutoMajorAxisItemOnGrid(Grid&, RenderBox&, AutoPlacementCursor&) const;
105     GridTrackSizingDirection autoPlacementMajorAxisDirection() const;
106     GridTrackSizingDirection autoPlacementMinorAxisDirection() const;
107
108     bool canPerformSimplifiedLayout() const final;
109     void prepareChildForPositionedLayout(RenderBox&);
110     void layoutPositionedObject(RenderBox&, bool relayoutChildren, bool fixedPositionObjectsOnly) override;
111     void offsetAndBreadthForPositionedChild(const RenderBox&, GridTrackSizingDirection, LayoutUnit& offset, LayoutUnit& breadth);
112
113     void computeIntrinsicLogicalHeight(GridSizingData&);
114     LayoutUnit computeTrackBasedLogicalHeight(const GridSizingData&) const;
115     void computeTrackSizesForDirection(GridTrackSizingDirection, GridSizingData&, LayoutUnit freeSpace);
116
117     void repeatTracksSizingIfNeeded(GridSizingData&, LayoutUnit availableSpaceForColumns, LayoutUnit availableSpaceForRows);
118
119     void layoutGridItems(GridSizingData&);
120     void populateGridPositionsForDirection(GridSizingData&, GridTrackSizingDirection);
121
122     static bool shouldProcessTrackForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
123     static bool trackShouldGrowBeyondGrowthLimitsForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
124     static void markAsInfinitelyGrowableForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
125     static void updateTrackSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
126     LayoutUnit currentItemSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
127
128     typedef struct GridItemsSpanGroupRange GridItemsSpanGroupRange;
129     void resolveContentBasedTrackSizingFunctionsForNonSpanningItems(GridTrackSizingDirection, const GridSpan&, RenderBox& gridItem, GridTrack&, GridSizingData&) const;
130     template <TrackSizeComputationPhase> void resolveContentBasedTrackSizingFunctionsForItems(GridTrackSizingDirection, GridSizingData&, const GridItemsSpanGroupRange&) const;
131     template <TrackSizeComputationPhase> void distributeSpaceToTracks(Vector<GridTrack*>&, Vector<GridTrack*>* growBeyondGrowthLimitsTracks, LayoutUnit& availableLogicalSpace) const;
132
133     typedef HashSet<unsigned, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> TrackIndexSet;
134     double computeFlexFactorUnitSize(const Vector<GridTrack>&, GridTrackSizingDirection, const GridSizingData&, double flexFactorSum, LayoutUnit leftOverSpace, const Vector<unsigned, 8>& flexibleTracksIndexes, std::unique_ptr<TrackIndexSet> tracksToTreatAsInflexible = nullptr) const;
135     double findFlexFactorUnitSize(const Vector<GridTrack>&, const GridSpan&, GridTrackSizingDirection, LayoutUnit spaceToFill, const GridSizingData&) const;
136
137     const GridTrackSize& rawGridTrackSize(GridTrackSizingDirection, unsigned, const GridSizingData&) const;
138     GridTrackSize gridTrackSize(GridTrackSizingDirection, unsigned, const GridSizingData&) const;
139
140     bool updateOverrideContainingBlockContentSizeForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
141     LayoutUnit logicalHeightForChild(RenderBox&) const;
142     LayoutUnit minSizeForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
143     LayoutUnit minContentForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
144     LayoutUnit maxContentForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
145     GridAxisPosition columnAxisPositionForChild(const RenderBox&) const;
146     GridAxisPosition rowAxisPositionForChild(const RenderBox&) const;
147     LayoutUnit columnAxisOffsetForChild(const RenderBox&, const GridSizingData&) const;
148     LayoutUnit rowAxisOffsetForChild(const RenderBox&, const GridSizingData&) const;
149     ContentAlignmentData computeContentPositionAndDistributionOffset(GridTrackSizingDirection, const LayoutUnit& availableFreeSpace, unsigned numberOfGridTracks) const;
150     LayoutPoint findChildLogicalPosition(const RenderBox&, const GridSizingData&) const;
151     GridArea cachedGridArea(const RenderBox&) const;
152     GridSpan cachedGridSpan(const RenderBox&, GridTrackSizingDirection) const;
153
154
155     LayoutUnit gridAreaBreadthForChild(const RenderBox& child, GridTrackSizingDirection, const GridSizingData&) const;
156     LayoutUnit gridAreaBreadthForChildIncludingAlignmentOffsets(const RenderBox&, GridTrackSizingDirection, const GridSizingData&) const;
157     LayoutUnit assumedRowsSizeForOrthogonalChild(const RenderBox&, const GridSizingData&) const;
158
159     void applyStretchAlignmentToTracksIfNeeded(GridTrackSizingDirection, GridSizingData&);
160
161     void paintChildren(PaintInfo& forSelf, const LayoutPoint& paintOffset, PaintInfo& forChild, bool usePrintRect) override;
162     bool needToStretchChildLogicalHeight(const RenderBox&) const;
163     LayoutUnit marginLogicalHeightForChild(const RenderBox&) const;
164     LayoutUnit computeMarginLogicalSizeForChild(GridTrackSizingDirection, const RenderBox&) const;
165     LayoutUnit availableAlignmentSpaceForChildBeforeStretching(LayoutUnit gridAreaBreadthForChild, const RenderBox&) const;
166     StyleSelfAlignmentData justifySelfForChild(const RenderBox&) const;
167     StyleSelfAlignmentData alignSelfForChild(const RenderBox&) const;
168     void applyStretchAlignmentToChildIfNeeded(RenderBox&);
169     bool hasAutoSizeInColumnAxis(const RenderBox& child) const { return isHorizontalWritingMode() ? child.style().height().isAuto() : child.style().width().isAuto(); }
170     bool hasAutoSizeInRowAxis(const RenderBox& child) const { return isHorizontalWritingMode() ? child.style().width().isAuto() : child.style().height().isAuto(); }
171     bool allowedToStretchChildAlongColumnAxis(const RenderBox& child) const { return alignSelfForChild(child).position() == ItemPositionStretch && hasAutoSizeInColumnAxis(child) && !hasAutoMarginsInColumnAxis(child); }
172     bool allowedToStretchChildAlongRowAxis(const RenderBox& child) const { return justifySelfForChild(child).position() == ItemPositionStretch && hasAutoSizeInRowAxis(child) && !hasAutoMarginsInRowAxis(child); }
173     bool hasAutoMarginsInColumnAxis(const RenderBox&) const;
174     bool hasAutoMarginsInRowAxis(const RenderBox&) const;
175     void resetAutoMarginsAndLogicalTopInColumnAxis(RenderBox& child);
176     void updateAutoMarginsInColumnAxisIfNeeded(RenderBox&);
177     void updateAutoMarginsInRowAxisIfNeeded(RenderBox&);
178
179     int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const final;
180     std::optional<int> firstLineBaseline() const final;
181     std::optional<int> inlineBlockBaseline(LineDirectionMode) const final;
182     bool isInlineBaselineAlignedChild(const RenderBox&) const;
183
184 #ifndef NDEBUG
185     bool tracksAreWiderThanMinTrackBreadth(GridTrackSizingDirection, GridSizingData&);
186 #endif
187
188     LayoutUnit gridGapForDirection(GridTrackSizingDirection) const;
189     LayoutUnit guttersSize(const Grid&, GridTrackSizingDirection, unsigned startLine, unsigned span) const;
190
191     bool spanningItemCrossesFlexibleSizedTracks(const GridSpan&, GridTrackSizingDirection, const GridSizingData&) const;
192
193     unsigned numTracks(GridTrackSizingDirection, const Grid&) const;
194
195     LayoutUnit translateRTLCoordinate(LayoutUnit) const;
196
197     bool isOrthogonalChild(const RenderBox&) const;
198     GridTrackSizingDirection flowAwareDirectionForChild(const RenderBox&, GridTrackSizingDirection) const;
199
200     Grid m_grid;
201
202     Vector<LayoutUnit> m_columnPositions;
203     Vector<LayoutUnit> m_rowPositions;
204     LayoutUnit m_offsetBetweenColumns;
205     LayoutUnit m_offsetBetweenRows;
206
207     std::optional<LayoutUnit> m_minContentHeight;
208     std::optional<LayoutUnit> m_maxContentHeight;
209 };
210
211 } // namespace WebCore
212
213 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderGrid, isRenderGrid())