[CSS Grid Layout] Support sparse in auto-placement algorithm
[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 "GridResolvedPosition.h"
33 #include "OrderIterator.h"
34 #include "RenderBlock.h"
35
36 namespace WebCore {
37
38 class GridCoordinate;
39 class GridSpan;
40 class GridTrack;
41
42 class RenderGrid final : public RenderBlock {
43 public:
44     RenderGrid(Element&, PassRef<RenderStyle>);
45     virtual ~RenderGrid();
46
47     Element& element() const { return toElement(nodeForNonAnonymous()); }
48
49     virtual void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) override;
50
51     virtual bool avoidsFloats() const override { return true; }
52     virtual bool canCollapseAnonymousBlockChild() const override { return false; }
53
54     const Vector<LayoutUnit>& columnPositions() const { return m_columnPositions; }
55     const Vector<LayoutUnit>& rowPositions() const { return m_rowPositions; }
56
57 private:
58     virtual const char* renderName() const override;
59     virtual bool isRenderGrid() const override { return true; }
60     virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const override;
61     virtual void computePreferredLogicalWidths() override;
62
63     class GridIterator;
64     class GridSizingData;
65     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&);
66     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&, LayoutUnit& availableLogicalSpace);
67     bool gridElementIsShrinkToFit();
68     LayoutUnit computeUsedBreadthOfMinLength(GridTrackSizingDirection, const GridLength&) const;
69     LayoutUnit computeUsedBreadthOfMaxLength(GridTrackSizingDirection, const GridLength&, LayoutUnit usedBreadth) const;
70     LayoutUnit computeUsedBreadthOfSpecifiedLength(GridTrackSizingDirection, const Length&) const;
71     void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection, GridSizingData&);
72
73     void ensureGridSize(size_t maximumRowIndex, size_t maximumColumnIndex);
74     void insertItemIntoGrid(RenderBox*, const GridCoordinate&);
75     void placeItemsOnGrid();
76     void populateExplicitGridAndOrderIterator();
77     std::unique_ptr<GridCoordinate> createEmptyGridAreaAtSpecifiedPositionsOutsideGrid(const RenderBox*, GridTrackSizingDirection, const GridSpan&) const;
78     void placeSpecifiedMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
79     void placeAutoMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
80     typedef std::pair<size_t, size_t> AutoPlacementCursor;
81     void placeAutoMajorAxisItemOnGrid(RenderBox*, AutoPlacementCursor&);
82     GridTrackSizingDirection autoPlacementMajorAxisDirection() const;
83     GridTrackSizingDirection autoPlacementMinorAxisDirection() const;
84
85     void layoutGridItems();
86     void populateGridPositions(const GridSizingData&);
87     void clearGrid();
88
89     typedef LayoutUnit (RenderGrid::* SizingFunction)(RenderBox*, GridTrackSizingDirection, Vector<GridTrack>&);
90     typedef LayoutUnit (GridTrack::* AccumulatorGetter)() const;
91     typedef void (GridTrack::* AccumulatorGrowFunction)(LayoutUnit);
92     typedef bool (GridTrackSize::* FilterFunction)() const;
93     void resolveContentBasedTrackSizingFunctionsForItems(GridTrackSizingDirection, GridSizingData&, RenderBox*, FilterFunction, SizingFunction, AccumulatorGetter, AccumulatorGrowFunction);
94     void distributeSpaceToTracks(Vector<GridTrack*>&, Vector<GridTrack*>* tracksForGrowthAboveMaxBreadth, AccumulatorGetter, AccumulatorGrowFunction, GridSizingData&, LayoutUnit& availableLogicalSpace);
95
96     double computeNormalizedFractionBreadth(Vector<GridTrack>&, const GridSpan& tracksSpan, GridTrackSizingDirection, LayoutUnit availableLogicalSpace) const;
97
98     const GridTrackSize& gridTrackSize(GridTrackSizingDirection, size_t) const;
99
100     LayoutUnit logicalContentHeightForChild(RenderBox*, Vector<GridTrack>&);
101     LayoutUnit minContentForChild(RenderBox*, GridTrackSizingDirection, Vector<GridTrack>& columnTracks);
102     LayoutUnit maxContentForChild(RenderBox*, GridTrackSizingDirection, Vector<GridTrack>& columnTracks);
103     LayoutPoint findChildLogicalPosition(RenderBox*, const GridSizingData&);
104     GridCoordinate cachedGridCoordinate(const RenderBox*) const;
105
106     LayoutUnit gridAreaBreadthForChild(const RenderBox* child, GridTrackSizingDirection, const Vector<GridTrack>&) const;
107
108     virtual void paintChildren(PaintInfo& forSelf, const LayoutPoint& paintOffset, PaintInfo& forChild, bool usePrintRect) override final;
109
110 #ifndef NDEBUG
111     bool tracksAreWiderThanMinTrackBreadth(GridTrackSizingDirection, const Vector<GridTrack>&);
112     bool gridWasPopulated() const { return !m_grid.isEmpty() && !m_grid[0].isEmpty(); }
113 #endif
114
115     size_t gridColumnCount() const
116     {
117         ASSERT(gridWasPopulated());
118         return m_grid[0].size();
119     }
120     size_t gridRowCount() const
121     {
122         ASSERT(gridWasPopulated());
123         return m_grid.size();
124     }
125
126     Vector<Vector<Vector<RenderBox*, 1>>> m_grid;
127     Vector<LayoutUnit> m_columnPositions;
128     Vector<LayoutUnit> m_rowPositions;
129     HashMap<const RenderBox*, GridCoordinate> m_gridItemCoordinate;
130     OrderIterator m_orderIterator;
131 };
132
133 RENDER_OBJECT_TYPE_CASTS(RenderGrid, isRenderGrid())
134
135 } // namespace WebCore
136
137 #endif /* ENABLE(CSS_GRID_LAYOUT) */
138
139 #endif // RenderGrid_h