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/style/GridPositionsResolver.h" | |
35 #include "wtf/Allocator.h" | |
36 #include "wtf/HashMap.h" | |
37 #include "wtf/PassOwnPtr.h" | |
38 #include "wtf/text/WTFString.h" | |
39 #include <algorithm> | |
40 | |
41 namespace blink { | |
42 | |
43 // Recommended maximum size for both explicit and implicit grids. | |
44 const int kGridMaxTracks = 1000000; | |
45 | |
46 // A span in a single direction (either rows or columns). Note that |resolvedIni
tialPosition| | |
47 // and |resolvedFinalPosition| are grid lines' indexes. | |
48 // Iterating over the span shouldn't include |resolvedFinalPosition| to be corre
ct. | |
49 struct GridSpan { | |
50 USING_FAST_MALLOC(GridSpan); | |
51 public: | |
52 | |
53 static GridSpan untranslatedDefiniteGridSpan(int resolvedInitialPosition, in
t resolvedFinalPosition) | |
54 { | |
55 return GridSpan(resolvedInitialPosition, resolvedFinalPosition, Untransl
atedDefinite); | |
56 } | |
57 | |
58 static GridSpan translatedDefiniteGridSpan(size_t resolvedInitialPosition, s
ize_t resolvedFinalPosition) | |
59 { | |
60 return GridSpan(resolvedInitialPosition, resolvedFinalPosition, Translat
edDefinite); | |
61 } | |
62 | |
63 static GridSpan indefiniteGridSpan() | |
64 { | |
65 return GridSpan(0, 1, Indefinite); | |
66 } | |
67 | |
68 bool operator==(const GridSpan& o) const | |
69 { | |
70 return m_type == o.m_type && m_resolvedInitialPosition == o.m_resolvedIn
itialPosition && m_resolvedFinalPosition == o.m_resolvedFinalPosition; | |
71 } | |
72 | |
73 size_t integerSpan() const | |
74 { | |
75 ASSERT(isTranslatedDefinite()); | |
76 ASSERT(m_resolvedFinalPosition > m_resolvedInitialPosition); | |
77 return m_resolvedFinalPosition - m_resolvedInitialPosition; | |
78 } | |
79 | |
80 int untranslatedResolvedInitialPosition() const | |
81 { | |
82 ASSERT(m_type == UntranslatedDefinite); | |
83 return m_resolvedInitialPosition; | |
84 } | |
85 | |
86 int untranslatedResolvedFinalPosition() const | |
87 { | |
88 ASSERT(m_type == UntranslatedDefinite); | |
89 return m_resolvedFinalPosition; | |
90 } | |
91 | |
92 size_t resolvedInitialPosition() const | |
93 { | |
94 ASSERT(isTranslatedDefinite()); | |
95 ASSERT(m_resolvedInitialPosition >= 0); | |
96 return m_resolvedInitialPosition; | |
97 } | |
98 | |
99 size_t resolvedFinalPosition() const | |
100 { | |
101 ASSERT(isTranslatedDefinite()); | |
102 ASSERT(m_resolvedFinalPosition > 0); | |
103 return m_resolvedFinalPosition; | |
104 } | |
105 | |
106 struct GridSpanIterator { | |
107 GridSpanIterator(size_t v) : value(v) {} | |
108 | |
109 size_t operator*() const { return value; } | |
110 size_t operator++() { return value++; } | |
111 bool operator!=(GridSpanIterator other) const { return value != other.va
lue; } | |
112 | |
113 size_t value; | |
114 }; | |
115 | |
116 GridSpanIterator begin() const | |
117 { | |
118 ASSERT(isTranslatedDefinite()); | |
119 return m_resolvedInitialPosition; | |
120 } | |
121 | |
122 GridSpanIterator end() const | |
123 { | |
124 ASSERT(isTranslatedDefinite()); | |
125 return m_resolvedFinalPosition; | |
126 } | |
127 | |
128 bool isTranslatedDefinite() const | |
129 { | |
130 return m_type == TranslatedDefinite; | |
131 } | |
132 | |
133 bool isIndefinite() const | |
134 { | |
135 return m_type == Indefinite; | |
136 } | |
137 | |
138 void translate(size_t offset) | |
139 { | |
140 ASSERT(m_type == UntranslatedDefinite); | |
141 | |
142 m_type = TranslatedDefinite; | |
143 m_resolvedInitialPosition += offset; | |
144 m_resolvedFinalPosition += offset; | |
145 | |
146 ASSERT(m_resolvedInitialPosition >= 0); | |
147 ASSERT(m_resolvedFinalPosition > 0); | |
148 } | |
149 | |
150 private: | |
151 | |
152 enum GridSpanType {UntranslatedDefinite, TranslatedDefinite, Indefinite}; | |
153 | |
154 GridSpan(int resolvedInitialPosition, int resolvedFinalPosition, GridSpanTyp
e type) | |
155 : m_type(type) | |
156 { | |
157 #if ENABLE(ASSERT) | |
158 ASSERT(resolvedInitialPosition < resolvedFinalPosition); | |
159 if (type == TranslatedDefinite) { | |
160 ASSERT(resolvedInitialPosition >= 0); | |
161 ASSERT(resolvedFinalPosition > 0); | |
162 } | |
163 #endif | |
164 | |
165 if (resolvedInitialPosition >= 0) | |
166 m_resolvedInitialPosition = std::min(resolvedInitialPosition, kGridM
axTracks - 1); | |
167 else | |
168 m_resolvedInitialPosition = std::max(resolvedInitialPosition, -kGrid
MaxTracks); | |
169 | |
170 if (resolvedFinalPosition >= 0) | |
171 m_resolvedFinalPosition = std::min(resolvedFinalPosition, kGridMaxTr
acks); | |
172 else | |
173 m_resolvedFinalPosition = std::max(resolvedFinalPosition, -kGridMaxT
racks + 1); | |
174 } | |
175 | |
176 int m_resolvedInitialPosition; | |
177 int m_resolvedFinalPosition; | |
178 GridSpanType m_type; | |
179 }; | |
180 | |
181 // This represents a grid area that spans in both rows' and columns' direction. | |
182 struct GridCoordinate { | |
183 USING_FAST_MALLOC(GridCoordinate); | |
184 public: | |
185 // HashMap requires a default constuctor. | |
186 GridCoordinate() | |
187 : columns(GridSpan::indefiniteGridSpan()) | |
188 , rows(GridSpan::indefiniteGridSpan()) | |
189 { | |
190 } | |
191 | |
192 GridCoordinate(const GridSpan& r, const GridSpan& c) | |
193 : columns(c) | |
194 , rows(r) | |
195 { | |
196 } | |
197 | |
198 bool operator==(const GridCoordinate& o) const | |
199 { | |
200 return columns == o.columns && rows == o.rows; | |
201 } | |
202 | |
203 bool operator!=(const GridCoordinate& o) const | |
204 { | |
205 return !(*this == o); | |
206 } | |
207 | |
208 GridSpan columns; | |
209 GridSpan rows; | |
210 }; | |
211 | |
212 typedef HashMap<String, GridCoordinate> NamedGridAreaMap; | |
213 | |
214 } // namespace blink | |
215 | |
216 #endif // GridCoordinate_h | |
OLD | NEW |