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