OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2013 Google 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 are | |
6 * met: | |
7 * | |
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 | |
13 * distribution. | |
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. | |
17 * | |
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. | |
29 */ | |
30 | |
31 #ifndef GridCoordinate_h | |
32 #define GridCoordinate_h | |
33 | |
34 #include "core/layout/style/GridResolvedPosition.h" | |
35 #include "wtf/HashMap.h" | |
36 #include "wtf/PassOwnPtr.h" | |
37 #include "wtf/text/WTFString.h" | |
38 | |
39 namespace blink { | |
40 | |
41 // Recommended maximum size for both explicit and implicit grids. | |
42 const size_t kGridMaxTracks = 1000000; | |
43 | |
44 // A span in a single direction (either rows or columns). Note that |resolvedIni
tialPosition| | |
45 // and |resolvedFinalPosition| are grid areas' indexes, NOT grid lines'. Iterati
ng over the | |
46 // span should include both |resolvedInitialPosition| and |resolvedFinalPosition
| to be correct. | |
47 struct GridSpan { | |
48 static PassOwnPtr<GridSpan> create(const GridResolvedPosition& resolvedIniti
alPosition, const GridResolvedPosition& resolvedFinalPosition) | |
49 { | |
50 return adoptPtr(new GridSpan(resolvedInitialPosition, resolvedFinalPosit
ion)); | |
51 } | |
52 | |
53 static PassOwnPtr<GridSpan> createWithSpanAgainstOpposite(const GridResolved
Position& resolvedOppositePosition, const GridPosition& position, GridPositionSi
de side) | |
54 { | |
55 // 'span 1' is contained inside a single grid track regardless of the di
rection. | |
56 // That's why the CSS span value is one more than the offset we apply. | |
57 size_t positionOffset = position.spanPosition() - 1; | |
58 if (side == ColumnStartSide || side == RowStartSide) { | |
59 GridResolvedPosition initialResolvedPosition = GridResolvedPosition(
std::max<int>(0, resolvedOppositePosition.toInt() - positionOffset)); | |
60 return GridSpan::create(initialResolvedPosition, resolvedOppositePos
ition); | |
61 } | |
62 | |
63 return GridSpan::create(resolvedOppositePosition, GridResolvedPosition(r
esolvedOppositePosition.toInt() + positionOffset)); | |
64 } | |
65 | |
66 static PassOwnPtr<GridSpan> createWithNamedSpanAgainstOpposite(const GridRes
olvedPosition& resolvedOppositePosition, const GridPosition& position, GridPosit
ionSide side, const Vector<size_t>& gridLines) | |
67 { | |
68 if (side == RowStartSide || side == ColumnStartSide) | |
69 return createWithInitialNamedSpanAgainstOpposite(resolvedOppositePos
ition, position, gridLines); | |
70 | |
71 return createWithFinalNamedSpanAgainstOpposite(resolvedOppositePosition,
position, gridLines); | |
72 } | |
73 | |
74 static PassOwnPtr<GridSpan> createWithInitialNamedSpanAgainstOpposite(const
GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, co
nst Vector<size_t>& gridLines) | |
75 { | |
76 // The grid line inequality needs to be strict (which doesn't match the
after / end case) because |resolvedOppositePosition| | |
77 // is already converted to an index in our grid representation (ie one w
as removed from the grid line to account for the side). | |
78 size_t firstLineBeforeOppositePositionIndex = 0; | |
79 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLin
es.begin(), gridLines.end(), resolvedOppositePosition.toInt()); | |
80 if (firstLineBeforeOppositePosition != gridLines.end()) { | |
81 if (*firstLineBeforeOppositePosition > resolvedOppositePosition.toIn
t() && firstLineBeforeOppositePosition != gridLines.begin()) | |
82 --firstLineBeforeOppositePosition; | |
83 | |
84 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePositi
on - gridLines.begin(); | |
85 } | |
86 | |
87 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionI
ndex - position.spanPosition() + 1); | |
88 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition(gri
dLines[gridLineIndex]); | |
89 if (resolvedGridLinePosition > resolvedOppositePosition) | |
90 resolvedGridLinePosition = resolvedOppositePosition; | |
91 return GridSpan::create(resolvedGridLinePosition, resolvedOppositePositi
on); | |
92 } | |
93 | |
94 static PassOwnPtr<GridSpan> createWithFinalNamedSpanAgainstOpposite(const Gr
idResolvedPosition& resolvedOppositePosition, const GridPosition& position, cons
t Vector<size_t>& gridLines) | |
95 { | |
96 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1; | |
97 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLine
s.begin(), gridLines.end(), resolvedOppositePosition.toInt()); | |
98 if (firstLineAfterOppositePosition != gridLines.end()) | |
99 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition
- gridLines.begin(); | |
100 | |
101 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOppo
sitePositionIndex + position.spanPosition() - 1); | |
102 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition::ad
justGridPositionForAfterEndSide(gridLines[gridLineIndex]); | |
103 if (resolvedGridLinePosition < resolvedOppositePosition) | |
104 resolvedGridLinePosition = resolvedOppositePosition; | |
105 return GridSpan::create(resolvedOppositePosition, resolvedGridLinePositi
on); | |
106 } | |
107 | |
108 GridSpan(const GridResolvedPosition& resolvedInitialPosition, const GridReso
lvedPosition& resolvedFinalPosition) | |
109 : resolvedInitialPosition(std::min(resolvedInitialPosition.toInt(), kGri
dMaxTracks - 1)) | |
110 , resolvedFinalPosition(std::min(resolvedFinalPosition.toInt(), kGridMax
Tracks)) | |
111 { | |
112 ASSERT(resolvedInitialPosition <= resolvedFinalPosition); | |
113 } | |
114 | |
115 bool operator==(const GridSpan& o) const | |
116 { | |
117 return resolvedInitialPosition == o.resolvedInitialPosition && resolvedF
inalPosition == o.resolvedFinalPosition; | |
118 } | |
119 | |
120 size_t integerSpan() const | |
121 { | |
122 return resolvedFinalPosition.toInt() - resolvedInitialPosition.toInt() +
1; | |
123 } | |
124 | |
125 GridResolvedPosition resolvedInitialPosition; | |
126 GridResolvedPosition resolvedFinalPosition; | |
127 | |
128 typedef GridResolvedPosition iterator; | |
129 | |
130 iterator begin() const | |
131 { | |
132 return resolvedInitialPosition; | |
133 } | |
134 | |
135 iterator end() const | |
136 { | |
137 return resolvedFinalPosition.next(); | |
138 } | |
139 }; | |
140 | |
141 // This represents a grid area that spans in both rows' and columns' direction. | |
142 struct GridCoordinate { | |
143 // HashMap requires a default constuctor. | |
144 GridCoordinate() | |
145 : columns(0, 0) | |
146 , rows(0, 0) | |
147 { | |
148 } | |
149 | |
150 GridCoordinate(const GridSpan& r, const GridSpan& c) | |
151 : columns(c) | |
152 , rows(r) | |
153 { | |
154 } | |
155 | |
156 bool operator==(const GridCoordinate& o) const | |
157 { | |
158 return columns == o.columns && rows == o.rows; | |
159 } | |
160 | |
161 bool operator!=(const GridCoordinate& o) const | |
162 { | |
163 return !(*this == o); | |
164 } | |
165 | |
166 GridResolvedPosition positionForSide(GridPositionSide side) const | |
167 { | |
168 switch (side) { | |
169 case ColumnStartSide: | |
170 return columns.resolvedInitialPosition; | |
171 case ColumnEndSide: | |
172 return columns.resolvedFinalPosition; | |
173 case RowStartSide: | |
174 return rows.resolvedInitialPosition; | |
175 case RowEndSide: | |
176 return rows.resolvedFinalPosition; | |
177 } | |
178 ASSERT_NOT_REACHED(); | |
179 return 0; | |
180 } | |
181 | |
182 GridSpan columns; | |
183 GridSpan rows; | |
184 }; | |
185 | |
186 typedef HashMap<String, GridCoordinate> NamedGridAreaMap; | |
187 | |
188 } // namespace blink | |
189 | |
190 #endif // GridCoordinate_h | |
OLD | NEW |