Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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) { |
| 63 GridResolvedPosition initialResolvedPosition = GridResolvedPosition( std::max<int>(0, resolvedOppositePosition.toInt() - positionOffset)); | 61 GridResolvedPosition initialResolvedPosition = GridResolvedPosition( std::max<int>(0, resolvedOppositePosition.toInt() - positionOffset)); |
| 64 return GridSpan::create(initialResolvedPosition, resolvedOppositePos ition); | 62 return GridSpan::create(initialResolvedPosition, resolvedOppositePos ition); |
| 65 } | 63 } |
| 66 | 64 |
| 67 return GridSpan::create(resolvedOppositePosition, GridResolvedPosition(r esolvedOppositePosition.toInt() + positionOffset)); | 65 return GridSpan::create(resolvedOppositePosition, GridResolvedPosition(r esolvedOppositePosition.toInt() + positionOffset)); |
| 68 } | 66 } |
| 69 | 67 |
| 70 static PassOwnPtr<GridSpan> createWithNamedSpanAgainstOpposite(const GridRes olvedPosition& resolvedOppositePosition, const GridPosition& position, GridPosit ionSide side, const Vector<size_t>& gridLines) | 68 static PassOwnPtr<GridSpan> createWithNamedSpanAgainstOpposite(const GridRes olvedPosition& resolvedOppositePosition, const GridPosition& position, GridPosit ionSide side, const Vector<size_t>& gridLines) |
| 71 { | 69 { |
| 72 if (side == RowStartSide || side == ColumnStartSide) | 70 if (side == RowStartSide || side == ColumnStartSide) { |
| 71 if (resolvedOppositePosition == 0) | |
| 72 return GridSpan::create(resolvedOppositePosition, resolvedOpposi tePosition.next()); | |
|
svillar
2015/11/17 11:37:05
I think it's better to move this check to createWi
Manuel Rego
2015/11/17 14:30:49
Done.
| |
| 73 return createWithInitialNamedSpanAgainstOpposite(resolvedOppositePos ition, position, gridLines); | 73 return createWithInitialNamedSpanAgainstOpposite(resolvedOppositePos ition, position, gridLines); |
| 74 } | |
| 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 // 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 size_t firstLineBeforeOppositePositionIndex = 0; | 81 size_t firstLineBeforeOppositePositionIndex = 0; |
| 83 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLin es.begin(), gridLines.end(), resolvedOppositePosition.toInt()); | 82 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLin es.begin(), gridLines.end(), resolvedOppositePosition.toInt()); |
| 84 if (firstLineBeforeOppositePosition != gridLines.end()) { | 83 if (firstLineBeforeOppositePosition != gridLines.end()) |
| 85 if (*firstLineBeforeOppositePosition > resolvedOppositePosition.toIn t() && firstLineBeforeOppositePosition != gridLines.begin()) | |
| 86 --firstLineBeforeOppositePosition; | |
| 87 | |
| 88 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePositi on - gridLines.begin(); | 84 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePositi on - gridLines.begin(); |
| 89 } | 85 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]); | 86 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition(gri dLines[gridLineIndex]); |
| 93 if (resolvedGridLinePosition > resolvedOppositePosition) | 87 if (resolvedGridLinePosition >= resolvedOppositePosition) |
| 94 resolvedGridLinePosition = resolvedOppositePosition; | 88 resolvedGridLinePosition = resolvedOppositePosition.prev(); |
| 95 return GridSpan::create(resolvedGridLinePosition, resolvedOppositePositi on); | 89 return GridSpan::create(resolvedGridLinePosition, resolvedOppositePositi on); |
| 96 } | 90 } |
| 97 | 91 |
| 98 static PassOwnPtr<GridSpan> createWithFinalNamedSpanAgainstOpposite(const Gr idResolvedPosition& resolvedOppositePosition, const GridPosition& position, cons t Vector<size_t>& gridLines) | 92 static PassOwnPtr<GridSpan> createWithFinalNamedSpanAgainstOpposite(const Gr idResolvedPosition& resolvedOppositePosition, const GridPosition& position, cons t Vector<size_t>& gridLines) |
| 99 { | 93 { |
| 100 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1; | 94 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1; |
| 101 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLine s.begin(), gridLines.end(), resolvedOppositePosition.toInt()); | 95 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLine s.begin(), gridLines.end(), resolvedOppositePosition.toInt()); |
| 102 if (firstLineAfterOppositePosition != gridLines.end()) | 96 if (firstLineAfterOppositePosition != gridLines.end()) |
| 103 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition - gridLines.begin(); | 97 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition - gridLines.begin(); |
| 98 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOppo sitePositionIndex + position.spanPosition() - 1); | |
| 99 GridResolvedPosition resolvedGridLinePosition = gridLines[gridLineIndex] ; | |
| 100 if (resolvedGridLinePosition <= resolvedOppositePosition) | |
| 101 resolvedGridLinePosition = resolvedOppositePosition.next(); | |
| 104 | 102 |
| 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); | 103 return GridSpan::create(resolvedOppositePosition, resolvedGridLinePositi on); |
| 110 } | 104 } |
| 111 | 105 |
| 112 GridSpan(const GridResolvedPosition& resolvedInitialPosition, const GridReso lvedPosition& resolvedFinalPosition) | 106 GridSpan(const GridResolvedPosition& resolvedInitialPosition, const GridReso lvedPosition& resolvedFinalPosition) |
| 113 : resolvedInitialPosition(std::min(resolvedInitialPosition.toInt(), kGri dMaxTracks - 1)) | 107 : resolvedInitialPosition(std::min(resolvedInitialPosition.toInt(), kGri dMaxTracks - 1)) |
| 114 , resolvedFinalPosition(std::min(resolvedFinalPosition.toInt(), kGridMax Tracks)) | 108 , resolvedFinalPosition(std::min(resolvedFinalPosition.toInt(), kGridMax Tracks)) |
| 115 { | 109 { |
| 116 ASSERT(resolvedInitialPosition <= resolvedFinalPosition); | 110 ASSERT(resolvedInitialPosition < resolvedFinalPosition); |
| 117 } | 111 } |
| 118 | 112 |
| 119 bool operator==(const GridSpan& o) const | 113 bool operator==(const GridSpan& o) const |
| 120 { | 114 { |
| 121 return resolvedInitialPosition == o.resolvedInitialPosition && resolvedF inalPosition == o.resolvedFinalPosition; | 115 return resolvedInitialPosition == o.resolvedInitialPosition && resolvedF inalPosition == o.resolvedFinalPosition; |
| 122 } | 116 } |
| 123 | 117 |
| 124 size_t integerSpan() const | 118 size_t integerSpan() const |
| 125 { | 119 { |
| 126 return resolvedFinalPosition.toInt() - resolvedInitialPosition.toInt() + 1; | 120 return resolvedFinalPosition.toInt() - resolvedInitialPosition.toInt(); |
| 127 } | 121 } |
| 128 | 122 |
| 129 GridResolvedPosition resolvedInitialPosition; | 123 GridResolvedPosition resolvedInitialPosition; |
| 130 GridResolvedPosition resolvedFinalPosition; | 124 GridResolvedPosition resolvedFinalPosition; |
| 131 | 125 |
| 132 typedef GridResolvedPosition iterator; | 126 typedef GridResolvedPosition iterator; |
| 133 | 127 |
| 134 iterator begin() const | 128 iterator begin() const |
| 135 { | 129 { |
| 136 return resolvedInitialPosition; | 130 return resolvedInitialPosition; |
| 137 } | 131 } |
| 138 | 132 |
| 139 iterator end() const | 133 iterator end() const |
| 140 { | 134 { |
| 141 return resolvedFinalPosition.next(); | 135 return resolvedFinalPosition; |
| 142 } | 136 } |
| 143 }; | 137 }; |
| 144 | 138 |
| 145 // This represents a grid area that spans in both rows' and columns' direction. | 139 // This represents a grid area that spans in both rows' and columns' direction. |
| 146 struct GridCoordinate { | 140 struct GridCoordinate { |
| 147 USING_FAST_MALLOC(GridCoordinate); | 141 USING_FAST_MALLOC(GridCoordinate); |
| 148 public: | 142 public: |
| 149 // HashMap requires a default constuctor. | 143 // HashMap requires a default constuctor. |
| 150 GridCoordinate() | 144 GridCoordinate() |
| 151 : columns(0, 0) | 145 : columns(0, 1) |
| 152 , rows(0, 0) | 146 , rows(0, 1) |
| 153 { | 147 { |
| 154 } | 148 } |
| 155 | 149 |
| 156 GridCoordinate(const GridSpan& r, const GridSpan& c) | 150 GridCoordinate(const GridSpan& r, const GridSpan& c) |
| 157 : columns(c) | 151 : columns(c) |
| 158 , rows(r) | 152 , rows(r) |
| 159 { | 153 { |
| 160 } | 154 } |
| 161 | 155 |
| 162 bool operator==(const GridCoordinate& o) const | 156 bool operator==(const GridCoordinate& o) const |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 187 | 181 |
| 188 GridSpan columns; | 182 GridSpan columns; |
| 189 GridSpan rows; | 183 GridSpan rows; |
| 190 }; | 184 }; |
| 191 | 185 |
| 192 typedef HashMap<String, GridCoordinate> NamedGridAreaMap; | 186 typedef HashMap<String, GridCoordinate> NamedGridAreaMap; |
| 193 | 187 |
| 194 } // namespace blink | 188 } // namespace blink |
| 195 | 189 |
| 196 #endif // GridCoordinate_h | 190 #endif // GridCoordinate_h |
| OLD | NEW |