| 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 |
#ifndef SkPathIter_DEFINED |
| 9 |
#define SkPathIter_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkPathTypes.h" |
| 12 |
#include "include/core/SkPoint.h" |
| 13 |
#include "include/core/SkSpan.h" |
| 14 |
|
| 15 |
#include <array> |
| 16 |
#include <cstddef> |
| 17 |
#include <optional> |
| 18 |
|
| 19 |
class SK_API SkPathIter { |
| 20 |
public: |
| 21 |
struct Rec { |
| 22 |
SkSpan<const SkPoint> fPoints; |
| 23 |
float fConicWeight; |
| 24 |
SkPathVerb fVerb; |
| 25 |
|
| 26 |
float conicWeight() const { |
| 27 |
SkASSERT(fVerb == SkPathVerb::kConic); |
| 28 |
return fConicWeight; |
| 29 |
} |
| 30 |
}; |
| 31 |
|
| 32 |
SkPathIter(SkSpan<const SkPoint> pts, SkSpan<const SkPathVerb> vbs, SkSpan<const float> cns) |
| 33 |
: pIndex(0), vIndex(0), cIndex(0) |
| 34 |
, fPoints(pts), fVerbs(vbs), fConics(cns) |
| 35 |
{ |
| 36 |
// For compat older iterators, we trim off a trailing Move. |
| 37 |
// SkPathData is defined to never create this pattern, so perhaps in the future |
| 38 |
// this check can be removed (or replaced by an assert) |
| 39 |
if (!vbs.empty() && vbs.back() == SkPathVerb::kMove) { |
| 40 |
fVerbs = vbs.first(vbs.size() - 1); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/* Holds the current verb, and its associated points |
| 45 |
* move: pts[0] |
| 46 |
* line: pts[0..1] |
| 47 |
* quad: pts[0..2] |
| 48 |
* conic: pts[0..2] fConicWeight |
| 49 |
* cubic: pts[0..3] |
| 50 |
* close: pts[0..1] ... as if close were a line from pts[0] to pts[1] |
| 51 |
*/ |
| 52 |
std::optional<Rec> next(); |
| 53 |
|
| 54 |
std::optional<SkPathVerb> peekNextVerb() const { |
| 55 |
if (vIndex < fVerbs.size()) { |
| 56 |
return fVerbs[vIndex]; |
| 57 |
} |
| 58 |
return {}; |
| 59 |
} |
| 60 |
|
| 61 |
private: |
| 62 |
size_t pIndex, vIndex, cIndex; |
| 63 |
SkSpan<const SkPoint> fPoints; |
| 64 |
SkSpan<const SkPathVerb> fVerbs; |
| 65 |
SkSpan<const float> fConics; |
| 66 |
std::array<SkPoint, 2> fClosePointStorage; |
| 67 |
}; |
| 68 |
|
| 69 |
class SkPathContourIter { |
| 70 |
public: |
| 71 |
struct Rec { |
| 72 |
SkSpan<const SkPoint> fPoints; |
| 73 |
SkSpan<const SkPathVerb> fVerbs; |
| 74 |
SkSpan<const float> fConics; |
| 75 |
}; |
| 76 |
|
| 77 |
SkPathContourIter(SkSpan<const SkPoint> pts, SkSpan<const SkPathVerb> vbs, |
| 78 |
SkSpan<const float> cns) |
| 79 |
: fPoints(pts), fVerbs(vbs), fConics(cns) |
| 80 |
{} |
| 81 |
|
| 82 |
std::optional<Rec> next(); |
| 83 |
|
| 84 |
private: |
| 85 |
SkSpan<const SkPoint> fPoints; |
| 86 |
SkSpan<const SkPathVerb> fVerbs; |
| 87 |
SkSpan<const float> fConics; |
| 88 |
}; |
| 89 |
|
| 90 |
#endif |