Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 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 "SkPathMeasure.h" |
| 9 | #include "SkTrimPathEffect.h" |
| 10 | #include "SkTrimPE.h" |
| 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
| 13 | |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | class Segmentator : public SkNoncopyable { |
| 17 | public: |
| 18 | Segmentator(const SkPath& src, SkPath* dst) |
| 19 | : fMeasure(src, false) |
| 20 | , fDst(dst) {} |
| 21 | |
| 22 | void add(SkScalar start, SkScalar stop) { |
| 23 | SkASSERT(start < stop); |
| 24 | |
| 25 | // TODO: we appear to skip zero-length contours. |
| 26 | do { |
| 27 | const auto nextOffset = fCurrentSegmentOffset + fMeasure.getLength(); |
| 28 | |
| 29 | if (start < nextOffset) { |
| 30 | fMeasure.getSegment(start - fCurrentSegmentOffset, |
| 31 | stop - fCurrentSegmentOffset, |
| 32 | fDst, true); |
| 33 | |
| 34 | if (stop < nextOffset) |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | fCurrentSegmentOffset = nextOffset; |
| 39 | } while (fMeasure.nextContour()); |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | SkPathMeasure fMeasure; |
| 44 | SkPath* fDst; |
| 45 | |
| 46 | SkScalar fCurrentSegmentOffset = 0; |
| 47 | |
| 48 | using INHERITED = SkNoncopyable; |
| 49 | }; |
| 50 | |
| 51 | } // namespace |
| 52 | |
| 53 | SkTrimPE::SkTrimPE(SkScalar startT, SkScalar stopT, SkTrimPathEffect::Mode mode) |
| 54 | : fStartT(startT), fStopT(stopT), fMode(mode) {} |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 55 | |
Mike Reed | 6d10f8b | 2018-08-16 13:22:16 -0400 | [diff] [blame^] | 56 | bool SkTrimPE::onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec, |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 57 | const SkRect* cullRect) const { |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame] | 58 | if (fStartT >= fStopT) { |
| 59 | SkASSERT(fMode == SkTrimPathEffect::Mode::kNormal); |
| 60 | return true; |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 61 | } |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame] | 62 | |
| 63 | // First pass: compute the total len. |
| 64 | SkScalar len = 0; |
| 65 | SkPathMeasure meas(src, false); |
| 66 | do { |
| 67 | len += meas.getLength(); |
| 68 | } while (meas.nextContour()); |
| 69 | |
| 70 | const auto arcStart = len * fStartT, |
| 71 | arcStop = len * fStopT; |
| 72 | |
| 73 | // Second pass: actually add segments. |
| 74 | Segmentator segmentator(src, dst); |
| 75 | if (fMode == SkTrimPathEffect::Mode::kNormal) { |
| 76 | if (arcStart < arcStop) segmentator.add(arcStart, arcStop); |
| 77 | } else { |
| 78 | if (0 < arcStart) segmentator.add(0, arcStart); |
| 79 | if (arcStop < len) segmentator.add(arcStop, len); |
| 80 | } |
| 81 | |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
| 85 | void SkTrimPE::flatten(SkWriteBuffer& buffer) const { |
| 86 | buffer.writeScalar(fStartT); |
| 87 | buffer.writeScalar(fStopT); |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame] | 88 | buffer.writeUInt(static_cast<uint32_t>(fMode)); |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | sk_sp<SkFlattenable> SkTrimPE::CreateProc(SkReadBuffer& buffer) { |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 92 | #ifdef WEB_ASSEMBLY |
| 93 | // Should not be reachable by WebAssembly Code. |
| 94 | SkASSERT(false); |
| 95 | return nullptr; |
| 96 | #else |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame] | 97 | const auto start = buffer.readScalar(), |
| 98 | stop = buffer.readScalar(); |
| 99 | const auto mode = buffer.readUInt(); |
| 100 | |
| 101 | return SkTrimPathEffect::Make(start, stop, |
| 102 | (mode & 1) ? SkTrimPathEffect::Mode::kInverted : SkTrimPathEffect::Mode::kNormal); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 103 | #endif |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 106 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 107 | |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame] | 108 | sk_sp<SkPathEffect> SkTrimPathEffect::Make(SkScalar startT, SkScalar stopT, Mode mode) { |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 109 | if (!SkScalarsAreFinite(startT, stopT)) { |
| 110 | return nullptr; |
| 111 | } |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame] | 112 | |
| 113 | if (startT <= 0 && stopT >= 1 && mode == Mode::kNormal) { |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 114 | return nullptr; |
| 115 | } |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame] | 116 | |
| 117 | startT = SkTPin(startT, 0.f, 1.f); |
| 118 | stopT = SkTPin(stopT, 0.f, 1.f); |
| 119 | |
| 120 | if (startT >= stopT && mode == Mode::kInverted) { |
| 121 | return nullptr; |
| 122 | } |
| 123 | |
| 124 | return sk_sp<SkPathEffect>(new SkTrimPE(startT, stopT, mode)); |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 125 | } |