| 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/SkPath.h" |
| 9 |
#include "include/core/SkSpan.h" |
| 10 |
|
| 11 |
#include <optional> |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
namespace { |
| 15 |
|
| 16 |
template <typename T> bool span_equal(SkSpan<T> a, SkSpan<T> b) { |
| 17 |
if (a.size() != b.size()) { |
| 18 |
return false; |
| 19 |
} |
| 20 |
if (a.empty()) { |
| 21 |
return true; |
| 22 |
} |
| 23 |
return (a.data() == b.data()) || std::equal(a.begin(), a.end(), b.begin()); |
| 24 |
} |
| 25 |
|
| 26 |
SkPoint operator*(float s, SkPoint p) { |
| 27 |
return { |
| 28 |
p.fX * s, |
| 29 |
p.fY * s |
| 30 |
}; |
| 31 |
} |
| 32 |
|
| 33 |
SkPoint lerp(SkPoint from, SkPoint to, float t) { |
| 34 |
return from + t * (to - from); |
| 35 |
} |
| 36 |
|
| 37 |
// These can be copy/pasted into Chrome / Android |
| 38 |
// - CanInterpolate() |
| 39 |
// - Interpolate() |
| 40 |
// |
| 41 |
// That would allow us to remove the associated SkPath methods. |
| 42 |
// |
| 43 |
// Note: they require no private access to SkPath internals, and are not used/called by |
| 44 |
// anthing in Skia. |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns true if the two paths can be interpolated. |
| 48 |
* |
| 49 |
* Returns true iff the paths have the same verbs and conicWeights, |
| 50 |
* and the same number of points. |
| 51 |
*/ |
| 52 |
bool CanInterpolate(const SkPath& from, const SkPath& to) { |
| 53 |
return from.points().size() == to.points().size() && |
| 54 |
span_equal(from.verbs(), to.verbs()) && |
| 55 |
span_equal(from.conicWeights(), to.conicWeights()); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Return a path whose points are lineraly interpolated between from and to, |
| 60 |
* using t (typically between 0 and 1). |
| 61 |
* |
| 62 |
* (logically) newPoint = (1 - t) * fromPoint + t * toPoint |
| 63 |
* |
| 64 |
* The result has the same fillType as the 'from' path. |
| 65 |
* |
| 66 |
* If the two paths are not interpolatable, this returns {}. |
| 67 |
*/ |
| 68 |
std::optional<SkPath> Interpolate(const SkPath& from, const SkPath& to, float t) { |
| 69 |
if (!CanInterpolate(from, to)) { |
| 70 |
return {}; |
| 71 |
} |
| 72 |
|
| 73 |
// note, if from and to have different fill-types, that's up to the client to deal with. |
| 74 |
const SkPathFillType fillType = from.getFillType(); |
| 75 |
|
| 76 |
const SkSpan<const SkPoint> fromPts = from.points(), |
| 77 |
toPts = to.points(); |
| 78 |
|
| 79 |
std::vector<SkPoint> dst(fromPts.size()); |
| 80 |
|
| 81 |
for (size_t i = 0; i < fromPts.size(); ++i) { |
| 82 |
dst[i] = lerp(fromPts[i], toPts[i], t); |
| 83 |
} |
| 84 |
return SkPath::Raw({dst.data(), dst.size()}, from.verbs(), from.conicWeights(), fillType); |
| 85 |
} |
| 86 |
|
| 87 |
} // namespace |
| 88 |
|
| 89 |
/////////////////////////////////////////////////////////////////////////////////////////////// |
| 90 |
|
| 91 |
bool SkPath::isInterpolatable(const SkPath& compare) const { |
| 92 |
return CanInterpolate(*this, compare); |
| 93 |
} |
| 94 |
|
| 95 |
SkPath SkPath::makeInterpolate(const SkPath& ending, SkScalar weight) const { |
| 96 |
return Interpolate(*this, ending, 1 - weight).value_or(SkPath()); |
| 97 |
} |
| 98 |
|
| 99 |
bool SkPath::interpolate(const SkPath& ending, SkScalar weight, SkPath* out) const { |
| 100 |
if (auto result = Interpolate(*this, ending, 1 - weight)) { |
| 101 |
*out = *result; |
| 102 |
return true; |
| 103 |
} |
| 104 |
return false; |
| 105 |
} |