Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: third_party/WebKit/Source/core/style/GridCoordinate.h

Issue 1451883002: [css-grid] Store lines instead of tracks in GridResolvedPosition (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix ASSERT on the tests Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 26 matching lines...) Expand all
37 #include "wtf/PassOwnPtr.h" 37 #include "wtf/PassOwnPtr.h"
38 #include "wtf/text/WTFString.h" 38 #include "wtf/text/WTFString.h"
39 #include <algorithm> 39 #include <algorithm>
40 40
41 namespace blink { 41 namespace blink {
42 42
43 // Recommended maximum size for both explicit and implicit grids. 43 // Recommended maximum size for both explicit and implicit grids.
44 const size_t kGridMaxTracks = 1000000; 44 const size_t kGridMaxTracks = 1000000;
45 45
46 // A span in a single direction (either rows or columns). Note that |resolvedIni tialPosition| 46 // A span in a single direction (either rows or columns). Note that |resolvedIni tialPosition|
47 // and |resolvedFinalPosition| are grid areas' indexes, NOT grid lines'. Iterati ng over the 47 // and |resolvedFinalPosition| are grid lines' indexes.
48 // span should include both |resolvedInitialPosition| and |resolvedFinalPosition | to be correct. 48 // Iterating over the span shouldn't include |resolvedFinalPosition| to be corre ct.
49 struct GridSpan { 49 struct GridSpan {
50 USING_FAST_MALLOC(GridSpan); 50 USING_FAST_MALLOC(GridSpan);
51 public: 51 public:
52 static PassOwnPtr<GridSpan> create(const GridResolvedPosition& resolvedIniti alPosition, const GridResolvedPosition& resolvedFinalPosition) 52 static PassOwnPtr<GridSpan> create(const GridResolvedPosition& resolvedIniti alPosition, const GridResolvedPosition& resolvedFinalPosition)
53 { 53 {
54 return adoptPtr(new GridSpan(resolvedInitialPosition, resolvedFinalPosit ion)); 54 return adoptPtr(new GridSpan(resolvedInitialPosition, resolvedFinalPosit ion));
55 } 55 }
56 56
57 static PassOwnPtr<GridSpan> createWithSpanAgainstOpposite(const GridResolved Position& resolvedOppositePosition, const GridPosition& position, GridPositionSi de side) 57 static PassOwnPtr<GridSpan> createWithSpanAgainstOpposite(const GridResolved Position& resolvedOppositePosition, const GridPosition& position, GridPositionSi de side)
58 { 58 {
59 // 'span 1' is contained inside a single grid track regardless of the di rection. 59 size_t positionOffset = position.spanPosition();
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) { 60 if (side == ColumnStartSide || side == RowStartSide) {
61 if (resolvedOppositePosition == 0)
62 return GridSpan::create(resolvedOppositePosition, resolvedOpposi tePosition.next());
63
63 GridResolvedPosition initialResolvedPosition = GridResolvedPosition( std::max<int>(0, resolvedOppositePosition.toInt() - positionOffset)); 64 GridResolvedPosition initialResolvedPosition = GridResolvedPosition( std::max<int>(0, resolvedOppositePosition.toInt() - positionOffset));
64 return GridSpan::create(initialResolvedPosition, resolvedOppositePos ition); 65 return GridSpan::create(initialResolvedPosition, resolvedOppositePos ition);
65 } 66 }
66 67
67 return GridSpan::create(resolvedOppositePosition, GridResolvedPosition(r esolvedOppositePosition.toInt() + positionOffset)); 68 return GridSpan::create(resolvedOppositePosition, GridResolvedPosition(r esolvedOppositePosition.toInt() + positionOffset));
68 } 69 }
69 70
70 static PassOwnPtr<GridSpan> createWithNamedSpanAgainstOpposite(const GridRes olvedPosition& resolvedOppositePosition, const GridPosition& position, GridPosit ionSide side, const Vector<size_t>& gridLines) 71 static PassOwnPtr<GridSpan> createWithNamedSpanAgainstOpposite(const GridRes olvedPosition& resolvedOppositePosition, const GridPosition& position, GridPosit ionSide side, const Vector<size_t>& gridLines)
71 { 72 {
72 if (side == RowStartSide || side == ColumnStartSide) 73 if (side == RowStartSide || side == ColumnStartSide)
73 return createWithInitialNamedSpanAgainstOpposite(resolvedOppositePos ition, position, gridLines); 74 return createWithInitialNamedSpanAgainstOpposite(resolvedOppositePos ition, position, gridLines);
74 75
75 return createWithFinalNamedSpanAgainstOpposite(resolvedOppositePosition, position, gridLines); 76 return createWithFinalNamedSpanAgainstOpposite(resolvedOppositePosition, position, gridLines);
76 } 77 }
77 78
78 static PassOwnPtr<GridSpan> createWithInitialNamedSpanAgainstOpposite(const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, co nst Vector<size_t>& gridLines) 79 static PassOwnPtr<GridSpan> createWithInitialNamedSpanAgainstOpposite(const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, co nst Vector<size_t>& gridLines)
79 { 80 {
80 // The grid line inequality needs to be strict (which doesn't match the after / end case) because |resolvedOppositePosition| 81 if (resolvedOppositePosition == 0)
81 // is already converted to an index in our grid representation (ie one w as removed from the grid line to account for the side). 82 return GridSpan::create(resolvedOppositePosition, resolvedOppositePo sition.next());
83
82 size_t firstLineBeforeOppositePositionIndex = 0; 84 size_t firstLineBeforeOppositePositionIndex = 0;
83 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLin es.begin(), gridLines.end(), resolvedOppositePosition.toInt()); 85 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLin es.begin(), gridLines.end(), resolvedOppositePosition.toInt());
84 if (firstLineBeforeOppositePosition != gridLines.end()) { 86 if (firstLineBeforeOppositePosition != gridLines.end())
85 if (*firstLineBeforeOppositePosition > resolvedOppositePosition.toIn t() && firstLineBeforeOppositePosition != gridLines.begin())
86 --firstLineBeforeOppositePosition;
87
88 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePositi on - gridLines.begin(); 87 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePositi on - gridLines.begin();
89 } 88 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionI ndex - position.spanPosition());
90
91 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionI ndex - position.spanPosition() + 1);
92 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition(gri dLines[gridLineIndex]); 89 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition(gri dLines[gridLineIndex]);
93 if (resolvedGridLinePosition > resolvedOppositePosition) 90 if (resolvedGridLinePosition >= resolvedOppositePosition)
94 resolvedGridLinePosition = resolvedOppositePosition; 91 resolvedGridLinePosition = resolvedOppositePosition.prev();
95 return GridSpan::create(resolvedGridLinePosition, resolvedOppositePositi on); 92 return GridSpan::create(resolvedGridLinePosition, resolvedOppositePositi on);
96 } 93 }
97 94
98 static PassOwnPtr<GridSpan> createWithFinalNamedSpanAgainstOpposite(const Gr idResolvedPosition& resolvedOppositePosition, const GridPosition& position, cons t Vector<size_t>& gridLines) 95 static PassOwnPtr<GridSpan> createWithFinalNamedSpanAgainstOpposite(const Gr idResolvedPosition& resolvedOppositePosition, const GridPosition& position, cons t Vector<size_t>& gridLines)
99 { 96 {
100 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1; 97 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1;
101 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLine s.begin(), gridLines.end(), resolvedOppositePosition.toInt()); 98 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLine s.begin(), gridLines.end(), resolvedOppositePosition.toInt());
102 if (firstLineAfterOppositePosition != gridLines.end()) 99 if (firstLineAfterOppositePosition != gridLines.end())
103 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition - gridLines.begin(); 100 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition - gridLines.begin();
101 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOppo sitePositionIndex + position.spanPosition() - 1);
102 GridResolvedPosition resolvedGridLinePosition = gridLines[gridLineIndex] ;
103 if (resolvedGridLinePosition <= resolvedOppositePosition)
104 resolvedGridLinePosition = resolvedOppositePosition.next();
104 105
105 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOppo sitePositionIndex + position.spanPosition() - 1);
106 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition::ad justGridPositionForAfterEndSide(gridLines[gridLineIndex]);
107 if (resolvedGridLinePosition < resolvedOppositePosition)
108 resolvedGridLinePosition = resolvedOppositePosition;
109 return GridSpan::create(resolvedOppositePosition, resolvedGridLinePositi on); 106 return GridSpan::create(resolvedOppositePosition, resolvedGridLinePositi on);
110 } 107 }
111 108
112 GridSpan(const GridResolvedPosition& resolvedInitialPosition, const GridReso lvedPosition& resolvedFinalPosition) 109 GridSpan(const GridResolvedPosition& resolvedInitialPosition, const GridReso lvedPosition& resolvedFinalPosition)
113 : resolvedInitialPosition(std::min(resolvedInitialPosition.toInt(), kGri dMaxTracks - 1)) 110 : resolvedInitialPosition(std::min(resolvedInitialPosition.toInt(), kGri dMaxTracks - 1))
114 , resolvedFinalPosition(std::min(resolvedFinalPosition.toInt(), kGridMax Tracks)) 111 , resolvedFinalPosition(std::min(resolvedFinalPosition.toInt(), kGridMax Tracks))
115 { 112 {
116 ASSERT(resolvedInitialPosition <= resolvedFinalPosition); 113 ASSERT(resolvedInitialPosition < resolvedFinalPosition);
117 } 114 }
118 115
119 bool operator==(const GridSpan& o) const 116 bool operator==(const GridSpan& o) const
120 { 117 {
121 return resolvedInitialPosition == o.resolvedInitialPosition && resolvedF inalPosition == o.resolvedFinalPosition; 118 return resolvedInitialPosition == o.resolvedInitialPosition && resolvedF inalPosition == o.resolvedFinalPosition;
122 } 119 }
123 120
124 size_t integerSpan() const 121 size_t integerSpan() const
125 { 122 {
126 return resolvedFinalPosition.toInt() - resolvedInitialPosition.toInt() + 1; 123 return resolvedFinalPosition.toInt() - resolvedInitialPosition.toInt();
127 } 124 }
128 125
129 GridResolvedPosition resolvedInitialPosition; 126 GridResolvedPosition resolvedInitialPosition;
130 GridResolvedPosition resolvedFinalPosition; 127 GridResolvedPosition resolvedFinalPosition;
131 128
132 typedef GridResolvedPosition iterator; 129 typedef GridResolvedPosition iterator;
133 130
134 iterator begin() const 131 iterator begin() const
135 { 132 {
136 return resolvedInitialPosition; 133 return resolvedInitialPosition;
137 } 134 }
138 135
139 iterator end() const 136 iterator end() const
140 { 137 {
141 return resolvedFinalPosition.next(); 138 return resolvedFinalPosition;
142 } 139 }
143 }; 140 };
144 141
145 // This represents a grid area that spans in both rows' and columns' direction. 142 // This represents a grid area that spans in both rows' and columns' direction.
146 struct GridCoordinate { 143 struct GridCoordinate {
147 USING_FAST_MALLOC(GridCoordinate); 144 USING_FAST_MALLOC(GridCoordinate);
148 public: 145 public:
149 // HashMap requires a default constuctor. 146 // HashMap requires a default constuctor.
150 GridCoordinate() 147 GridCoordinate()
151 : columns(0, 0) 148 : columns(0, 1)
152 , rows(0, 0) 149 , rows(0, 1)
153 { 150 {
154 } 151 }
155 152
156 GridCoordinate(const GridSpan& r, const GridSpan& c) 153 GridCoordinate(const GridSpan& r, const GridSpan& c)
157 : columns(c) 154 : columns(c)
158 , rows(r) 155 , rows(r)
159 { 156 {
160 } 157 }
161 158
162 bool operator==(const GridCoordinate& o) const 159 bool operator==(const GridCoordinate& o) const
(...skipping 24 matching lines...) Expand all
187 184
188 GridSpan columns; 185 GridSpan columns;
189 GridSpan rows; 186 GridSpan rows;
190 }; 187 };
191 188
192 typedef HashMap<String, GridCoordinate> NamedGridAreaMap; 189 typedef HashMap<String, GridCoordinate> NamedGridAreaMap;
193 190
194 } // namespace blink 191 } // namespace blink
195 192
196 #endif // GridCoordinate_h 193 #endif // GridCoordinate_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/paint/GridPainter.cpp ('k') | third_party/WebKit/Source/core/style/GridResolvedPosition.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698