Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / style / GridCoordinate.h
blob52c4e55482032e5f459729279ef73508748ed43f
1 /*
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
6 * met:
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.
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/style/GridResolvedPosition.h"
35 #include "wtf/FastAllocBase.h"
36 #include "wtf/HashMap.h"
37 #include "wtf/PassOwnPtr.h"
38 #include "wtf/text/WTFString.h"
39 #include <algorithm>
41 namespace blink {
43 // Recommended maximum size for both explicit and implicit grids.
44 const size_t kGridMaxTracks = 1000000;
46 // A span in a single direction (either rows or columns). Note that |resolvedInitialPosition|
47 // and |resolvedFinalPosition| are grid areas' indexes, NOT grid lines'. Iterating over the
48 // span should include both |resolvedInitialPosition| and |resolvedFinalPosition| to be correct.
49 struct GridSpan {
50 WTF_MAKE_FAST_ALLOCATED(GridSpan);
51 public:
52 static PassOwnPtr<GridSpan> create(const GridResolvedPosition& resolvedInitialPosition, const GridResolvedPosition& resolvedFinalPosition)
54 return adoptPtr(new GridSpan(resolvedInitialPosition, resolvedFinalPosition));
57 static PassOwnPtr<GridSpan> createWithSpanAgainstOpposite(const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, GridPositionSide side)
59 // 'span 1' is contained inside a single grid track regardless of the direction.
60 // That's why the CSS span value is one more than the offset we apply.
61 size_t positionOffset = position.spanPosition() - 1;
62 if (side == ColumnStartSide || side == RowStartSide) {
63 GridResolvedPosition initialResolvedPosition = GridResolvedPosition(std::max<int>(0, resolvedOppositePosition.toInt() - positionOffset));
64 return GridSpan::create(initialResolvedPosition, resolvedOppositePosition);
67 return GridSpan::create(resolvedOppositePosition, GridResolvedPosition(resolvedOppositePosition.toInt() + positionOffset));
70 static PassOwnPtr<GridSpan> createWithNamedSpanAgainstOpposite(const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, GridPositionSide side, const Vector<size_t>& gridLines)
72 if (side == RowStartSide || side == ColumnStartSide)
73 return createWithInitialNamedSpanAgainstOpposite(resolvedOppositePosition, position, gridLines);
75 return createWithFinalNamedSpanAgainstOpposite(resolvedOppositePosition, position, gridLines);
78 static PassOwnPtr<GridSpan> createWithInitialNamedSpanAgainstOpposite(const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, const Vector<size_t>& gridLines)
80 // The grid line inequality needs to be strict (which doesn't match the after / end case) because |resolvedOppositePosition|
81 // is already converted to an index in our grid representation (ie one was removed from the grid line to account for the side).
82 size_t firstLineBeforeOppositePositionIndex = 0;
83 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLines.begin(), gridLines.end(), resolvedOppositePosition.toInt());
84 if (firstLineBeforeOppositePosition != gridLines.end()) {
85 if (*firstLineBeforeOppositePosition > resolvedOppositePosition.toInt() && firstLineBeforeOppositePosition != gridLines.begin())
86 --firstLineBeforeOppositePosition;
88 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePosition - gridLines.begin();
91 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionIndex - position.spanPosition() + 1);
92 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition(gridLines[gridLineIndex]);
93 if (resolvedGridLinePosition > resolvedOppositePosition)
94 resolvedGridLinePosition = resolvedOppositePosition;
95 return GridSpan::create(resolvedGridLinePosition, resolvedOppositePosition);
98 static PassOwnPtr<GridSpan> createWithFinalNamedSpanAgainstOpposite(const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, const Vector<size_t>& gridLines)
100 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1;
101 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLines.begin(), gridLines.end(), resolvedOppositePosition.toInt());
102 if (firstLineAfterOppositePosition != gridLines.end())
103 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition - gridLines.begin();
105 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOppositePositionIndex + position.spanPosition() - 1);
106 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition::adjustGridPositionForAfterEndSide(gridLines[gridLineIndex]);
107 if (resolvedGridLinePosition < resolvedOppositePosition)
108 resolvedGridLinePosition = resolvedOppositePosition;
109 return GridSpan::create(resolvedOppositePosition, resolvedGridLinePosition);
112 GridSpan(const GridResolvedPosition& resolvedInitialPosition, const GridResolvedPosition& resolvedFinalPosition)
113 : resolvedInitialPosition(std::min(resolvedInitialPosition.toInt(), kGridMaxTracks - 1))
114 , resolvedFinalPosition(std::min(resolvedFinalPosition.toInt(), kGridMaxTracks))
116 ASSERT(resolvedInitialPosition <= resolvedFinalPosition);
119 bool operator==(const GridSpan& o) const
121 return resolvedInitialPosition == o.resolvedInitialPosition && resolvedFinalPosition == o.resolvedFinalPosition;
124 size_t integerSpan() const
126 return resolvedFinalPosition.toInt() - resolvedInitialPosition.toInt() + 1;
129 GridResolvedPosition resolvedInitialPosition;
130 GridResolvedPosition resolvedFinalPosition;
132 typedef GridResolvedPosition iterator;
134 iterator begin() const
136 return resolvedInitialPosition;
139 iterator end() const
141 return resolvedFinalPosition.next();
145 // This represents a grid area that spans in both rows' and columns' direction.
146 struct GridCoordinate {
147 WTF_MAKE_FAST_ALLOCATED(GridCoordinate);
148 public:
149 // HashMap requires a default constuctor.
150 GridCoordinate()
151 : columns(0, 0)
152 , rows(0, 0)
156 GridCoordinate(const GridSpan& r, const GridSpan& c)
157 : columns(c)
158 , rows(r)
162 bool operator==(const GridCoordinate& o) const
164 return columns == o.columns && rows == o.rows;
167 bool operator!=(const GridCoordinate& o) const
169 return !(*this == o);
172 GridResolvedPosition positionForSide(GridPositionSide side) const
174 switch (side) {
175 case ColumnStartSide:
176 return columns.resolvedInitialPosition;
177 case ColumnEndSide:
178 return columns.resolvedFinalPosition;
179 case RowStartSide:
180 return rows.resolvedInitialPosition;
181 case RowEndSide:
182 return rows.resolvedFinalPosition;
184 ASSERT_NOT_REACHED();
185 return 0;
188 GridSpan columns;
189 GridSpan rows;
192 typedef HashMap<String, GridCoordinate> NamedGridAreaMap;
194 } // namespace blink
196 #endif // GridCoordinate_h