2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef GridCoordinate_h
32 #define GridCoordinate_h
34 #include "core/rendering/style/GridResolvedPosition.h"
35 #include "wtf/HashMap.h"
36 #include "wtf/PassOwnPtr.h"
37 #include "wtf/text/WTFString.h"
41 // A span in a single direction (either rows or columns). Note that |resolvedInitialPosition|
42 // and |resolvedFinalPosition| are grid areas' indexes, NOT grid lines'. Iterating over the
43 // span should include both |resolvedInitialPosition| and |resolvedFinalPosition| to be correct.
45 static PassOwnPtr<GridSpan> create(const GridResolvedPosition& resolvedInitialPosition, const GridResolvedPosition& resolvedFinalPosition)
47 return adoptPtr(new GridSpan(resolvedInitialPosition, resolvedFinalPosition));
50 static PassOwnPtr<GridSpan> createWithSpanAgainstOpposite(const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, GridPositionSide side)
52 // 'span 1' is contained inside a single grid track regardless of the direction.
53 // That's why the CSS span value is one more than the offset we apply.
54 size_t positionOffset = position.spanPosition() - 1;
55 if (side == ColumnStartSide || side == RowStartSide) {
56 GridResolvedPosition initialResolvedPosition = GridResolvedPosition(std::max<int>(0, resolvedOppositePosition.toInt() - positionOffset));
57 return GridSpan::create(initialResolvedPosition, resolvedOppositePosition);
60 return GridSpan::create(resolvedOppositePosition, GridResolvedPosition(resolvedOppositePosition.toInt() + positionOffset));
63 static PassOwnPtr<GridSpan> createWithNamedSpanAgainstOpposite(const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, GridPositionSide side, const Vector<size_t>& gridLines)
65 if (side == RowStartSide || side == ColumnStartSide)
66 return createWithInitialNamedSpanAgainstOpposite(resolvedOppositePosition, position, gridLines);
68 return createWithFinalNamedSpanAgainstOpposite(resolvedOppositePosition, position, gridLines);
71 static PassOwnPtr<GridSpan> createWithInitialNamedSpanAgainstOpposite(const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, const Vector<size_t>& gridLines)
73 // The grid line inequality needs to be strict (which doesn't match the after / end case) because |resolvedOppositePosition|
74 // is already converted to an index in our grid representation (ie one was removed from the grid line to account for the side).
75 size_t firstLineBeforeOppositePositionIndex = 0;
76 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLines.begin(), gridLines.end(), resolvedOppositePosition.toInt());
77 if (firstLineBeforeOppositePosition != gridLines.end()) {
78 if (*firstLineBeforeOppositePosition > resolvedOppositePosition.toInt() && firstLineBeforeOppositePosition != gridLines.begin())
79 --firstLineBeforeOppositePosition;
81 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePosition - gridLines.begin();
84 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionIndex - position.spanPosition() + 1);
85 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition(gridLines[gridLineIndex]);
86 if (resolvedGridLinePosition > resolvedOppositePosition)
87 resolvedGridLinePosition = resolvedOppositePosition;
88 return GridSpan::create(resolvedGridLinePosition, resolvedOppositePosition);
91 static PassOwnPtr<GridSpan> createWithFinalNamedSpanAgainstOpposite(const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, const Vector<size_t>& gridLines)
93 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1;
94 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLines.begin(), gridLines.end(), resolvedOppositePosition.toInt());
95 if (firstLineAfterOppositePosition != gridLines.end())
96 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition - gridLines.begin();
98 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOppositePositionIndex + position.spanPosition() - 1);
99 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition::adjustGridPositionForAfterEndSide(gridLines[gridLineIndex]);
100 if (resolvedGridLinePosition < resolvedOppositePosition)
101 resolvedGridLinePosition = resolvedOppositePosition;
102 return GridSpan::create(resolvedOppositePosition, resolvedGridLinePosition);
105 GridSpan(const GridResolvedPosition& resolvedInitialPosition, const GridResolvedPosition& resolvedFinalPosition)
106 : resolvedInitialPosition(resolvedInitialPosition)
107 , resolvedFinalPosition(resolvedFinalPosition)
109 ASSERT(resolvedInitialPosition <= resolvedFinalPosition);
112 bool operator==(const GridSpan& o) const
114 return resolvedInitialPosition == o.resolvedInitialPosition && resolvedFinalPosition == o.resolvedFinalPosition;
117 size_t integerSpan() const
119 return resolvedFinalPosition.toInt() - resolvedInitialPosition.toInt() + 1;
122 GridResolvedPosition resolvedInitialPosition;
123 GridResolvedPosition resolvedFinalPosition;
125 typedef GridResolvedPosition iterator;
127 iterator begin() const
129 return resolvedInitialPosition;
134 return resolvedFinalPosition.next();
138 // This represents a grid area that spans in both rows' and columns' direction.
139 struct GridCoordinate {
140 // HashMap requires a default constuctor.
147 GridCoordinate(const GridSpan& r, const GridSpan& c)
153 bool operator==(const GridCoordinate& o) const
155 return columns == o.columns && rows == o.rows;
158 bool operator!=(const GridCoordinate& o) const
160 return !(*this == o);
163 GridResolvedPosition positionForSide(GridPositionSide side) const
166 case ColumnStartSide:
167 return columns.resolvedInitialPosition;
169 return columns.resolvedFinalPosition;
171 return rows.resolvedInitialPosition;
173 return rows.resolvedFinalPosition;
175 ASSERT_NOT_REACHED();
183 typedef HashMap<String, GridCoordinate> NamedGridAreaMap;
187 #endif // GridCoordinate_h