| 1 |
/* |
| 2 |
* Copyright 2025 Google LLC. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkPathIter.h" |
| 9 |
|
| 10 |
/* |
| 11 |
* Close is funny -- it has no explicit point data, but we return 2 points, |
| 12 |
* the logical 2 points that would make up the line connecting the end of |
| 13 |
* the contour, and its beginning. |
| 14 |
* |
| 15 |
* To do this, we have local storage (fClosePointStorage) |
| 16 |
*/ |
| 17 |
std::optional<SkPathIter::Rec> SkPathIter::next() { |
| 18 |
if (vIndex >= fVerbs.size()) { |
| 19 |
return {}; |
| 20 |
} |
| 21 |
|
| 22 |
size_t n = 0; |
| 23 |
|
| 24 |
float w = -1; |
| 25 |
SkPathVerb v; |
| 26 |
switch (v = fVerbs[vIndex++]) { |
| 27 |
case SkPathVerb::kMove: |
| 28 |
fClosePointStorage[1] = fPoints[pIndex++]; // remember for close |
| 29 |
return Rec{{&fClosePointStorage[1], 1}, w, v}; |
| 30 |
case SkPathVerb::kLine: n = 1; break; |
| 31 |
case SkPathVerb::kQuad: n = 2; break; |
| 32 |
case SkPathVerb::kConic: n = 2; w = fConics[cIndex++]; break; |
| 33 |
case SkPathVerb::kCubic: n = 3; break; |
| 34 |
case SkPathVerb::kClose: |
| 35 |
SkASSERT(pIndex > 0); |
| 36 |
fClosePointStorage[0] = fPoints[pIndex-1]; // the last point we saw |
| 37 |
return Rec{fClosePointStorage, w, v}; |
| 38 |
} |
| 39 |
|
| 40 |
SkASSERT(pIndex > 0); |
| 41 |
auto start = pIndex - 1; |
| 42 |
SkASSERT(n >= 1 && n <= 3); |
| 43 |
pIndex += n; |
| 44 |
return Rec{{&fPoints[start], n+1}, w, v}; |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/////////////////////////////////////////////// |
| 49 |
|
| 50 |
std::optional<SkPathContourIter::Rec> SkPathContourIter::next() { |
| 51 |
if (fVerbs.empty()) { |
| 52 |
return {}; |
| 53 |
} |
| 54 |
|
| 55 |
SkASSERT(fVerbs[0] == SkPathVerb::kMove); |
| 56 |
size_t npts = 1, nvbs = 1, nws = 0; |
| 57 |
|
| 58 |
for (size_t i = 1; i < fVerbs.size(); ++i) { |
| 59 |
switch (fVerbs[i]) { |
| 60 |
case SkPathVerb::kMove: goto DONE; |
| 61 |
case SkPathVerb::kLine: npts += 1; break; |
| 62 |
case SkPathVerb::kQuad: npts += 2; break; |
| 63 |
case SkPathVerb::kConic: npts += 2; nws += 1; break; |
| 64 |
case SkPathVerb::kCubic: npts += 3; break; |
| 65 |
case SkPathVerb::kClose: nvbs += 1; goto DONE; |
| 66 |
} |
| 67 |
nvbs += 1; |
| 68 |
} |
| 69 |
DONE: |
| 70 |
Rec rec = { |
| 71 |
fPoints.subspan(0, npts), |
| 72 |
fVerbs.subspan(0, nvbs), |
| 73 |
fConics.subspan(0, nws), |
| 74 |
}; |
| 75 |
fPoints = fPoints.last(fPoints.size() - npts); |
| 76 |
fVerbs = fVerbs.last( fVerbs.size() - nvbs); |
| 77 |
fConics = fConics.last(fConics.size() - nws); |
| 78 |
return rec; |
| 79 |
} |