| 1 |
/* |
| 2 |
* Copyright 2018 Google Inc. |
| 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/effects/SkTrimPathEffect.h" |
| 9 |
|
| 10 |
#include "include/core/SkFlattenable.h" |
| 11 |
#include "include/core/SkPath.h" |
| 12 |
#include "include/core/SkPathEffect.h" |
| 13 |
#include "include/core/SkPathMeasure.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkTypes.h" |
| 17 |
#include "include/private/base/SkFloatingPoint.h" |
| 18 |
#include "include/private/base/SkTPin.h" |
| 19 |
#include "src/core/SkReadBuffer.h" |
| 20 |
#include "src/core/SkWriteBuffer.h" |
| 21 |
#include "src/effects/SkTrimPE.h" |
| 22 |
|
| 23 |
#include <cstddef> |
| 24 |
#include <cstdint> |
| 25 |
|
| 26 |
class SkMatrix; |
| 27 |
class SkPathBuilder; |
| 28 |
class SkStrokeRec; |
| 29 |
struct SkRect; |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
// Returns the number of contours iterated to satisfy the request. |
| 34 |
static size_t add_segments(const SkPath& src, SkScalar start, SkScalar stop, SkPathBuilder* dst, |
| 35 |
bool requires_moveto = true) { |
| 36 |
SkASSERT(start < stop); |
| 37 |
|
| 38 |
SkPathMeasure measure(src, false); |
| 39 |
|
| 40 |
SkScalar current_segment_offset = 0; |
| 41 |
size_t contour_count = 1; |
| 42 |
|
| 43 |
do { |
| 44 |
const auto next_offset = current_segment_offset + measure.getLength(); |
| 45 |
|
| 46 |
if (start < next_offset) { |
| 47 |
measure.getSegment(start - current_segment_offset, |
| 48 |
stop - current_segment_offset, |
| 49 |
dst, requires_moveto); |
| 50 |
|
| 51 |
if (stop <= next_offset) |
| 52 |
break; |
| 53 |
} |
| 54 |
|
| 55 |
contour_count++; |
| 56 |
current_segment_offset = next_offset; |
| 57 |
} while (measure.nextContour()); |
| 58 |
|
| 59 |
return contour_count; |
| 60 |
} |
| 61 |
|
| 62 |
} // namespace |
| 63 |
|
| 64 |
SkTrimPE::SkTrimPE(SkScalar startT, SkScalar stopT, SkTrimPathEffect::Mode mode) |
| 65 |
: fStartT(startT), fStopT(stopT), fMode(mode) {} |
| 66 |
|
| 67 |
bool SkTrimPE::onFilterPath(SkPathBuilder* dst, const SkPath& src, SkStrokeRec*, const SkRect*, |
| 68 |
const SkMatrix&) const { |
| 69 |
if (fStartT >= fStopT) { |
| 70 |
SkASSERT(fMode == SkTrimPathEffect::Mode::kNormal); |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
// First pass: compute the total len. |
| 75 |
SkScalar len = 0; |
| 76 |
SkPathMeasure meas(src, false); |
| 77 |
do { |
| 78 |
len += meas.getLength(); |
| 79 |
} while (meas.nextContour()); |
| 80 |
|
| 81 |
const auto arcStart = len * fStartT, |
| 82 |
arcStop = len * fStopT; |
| 83 |
|
| 84 |
// Second pass: actually add segments. |
| 85 |
if (fMode == SkTrimPathEffect::Mode::kNormal) { |
| 86 |
// Normal mode -> one span. |
| 87 |
if (arcStart < arcStop) { |
| 88 |
add_segments(src, arcStart, arcStop, dst); |
| 89 |
} |
| 90 |
} else { |
| 91 |
// Inverted mode -> one logical span which wraps around at the end -> two actual spans. |
| 92 |
// In order to preserve closed path continuity: |
| 93 |
// |
| 94 |
// 1) add the second/tail span first |
| 95 |
// |
| 96 |
// 2) skip the head span move-to for single-closed-contour paths |
| 97 |
|
| 98 |
bool requires_moveto = true; |
| 99 |
if (arcStop < len) { |
| 100 |
// since we're adding the "tail" first, this is the total number of contours |
| 101 |
const auto contour_count = add_segments(src, arcStop, len, dst); |
| 102 |
|
| 103 |
// if the path consists of a single closed contour, we don't want to disconnect |
| 104 |
// the two parts with a moveto. |
| 105 |
if (contour_count == 1 && src.isLastContourClosed()) { |
| 106 |
requires_moveto = false; |
| 107 |
} |
| 108 |
} |
| 109 |
if (0 < arcStart) { |
| 110 |
add_segments(src, 0, arcStart, dst, requires_moveto); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
void SkTrimPE::flatten(SkWriteBuffer& buffer) const { |
| 118 |
buffer.writeScalar(fStartT); |
| 119 |
buffer.writeScalar(fStopT); |
| 120 |
buffer.writeUInt(static_cast<uint32_t>(fMode)); |
| 121 |
} |
| 122 |
|
| 123 |
sk_sp<SkFlattenable> SkTrimPE::CreateProc(SkReadBuffer& buffer) { |
| 124 |
const auto start = buffer.readScalar(), |
| 125 |
stop = buffer.readScalar(); |
| 126 |
const auto mode = buffer.readUInt(); |
| 127 |
|
| 128 |
return SkTrimPathEffect::Make(start, stop, |
| 129 |
(mode & 1) ? SkTrimPathEffect::Mode::kInverted : SkTrimPathEffect::Mode::kNormal); |
| 130 |
} |
| 131 |
|
| 132 |
////////////////////////////////////////////////////////////////////////////////////////////////// |
| 133 |
|
| 134 |
sk_sp<SkPathEffect> SkTrimPathEffect::Make(SkScalar startT, SkScalar stopT, Mode mode) { |
| 135 |
if (!SkIsFinite(startT, stopT)) { |
| 136 |
return nullptr; |
| 137 |
} |
| 138 |
|
| 139 |
if (startT <= 0 && stopT >= 1 && mode == Mode::kNormal) { |
| 140 |
return nullptr; |
| 141 |
} |
| 142 |
|
| 143 |
startT = SkTPin(startT, 0.f, 1.f); |
| 144 |
stopT = SkTPin(stopT, 0.f, 1.f); |
| 145 |
|
| 146 |
if (startT >= stopT && mode == Mode::kInverted) { |
| 147 |
return nullptr; |
| 148 |
} |
| 149 |
|
| 150 |
return sk_sp<SkPathEffect>(new SkTrimPE(startT, stopT, mode)); |
| 151 |
} |