epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "Sk1DPathEffect.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkPathMeasure.h" |
halcanary | 435657f | 2015-09-15 12:53:07 -0700 | [diff] [blame] | 14 | #include "SkStrokeRec.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 16 | bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src, |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 17 | SkStrokeRec*, const SkRect*) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | SkPathMeasure meas(src, false); |
| 19 | do { |
| 20 | SkScalar length = meas.getLength(); |
| 21 | SkScalar distance = this->begin(length); |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 22 | while (distance < length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | SkScalar delta = this->next(dst, distance, meas); |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 24 | if (delta <= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | break; |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 26 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | distance += delta; |
| 28 | } |
| 29 | } while (meas.nextContour()); |
| 30 | return true; |
| 31 | } |
| 32 | |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 33 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 35 | SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | SkScalar phase, Style style) : fPath(path) |
| 37 | { |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame^] | 38 | SkASSERT(advance > 0 && !path.isEmpty()); |
| 39 | // cleanup their phase parameter, inverting it so that it becomes an |
| 40 | // offset along the path (to match the interpretation in PostScript) |
| 41 | if (phase < 0) { |
| 42 | phase = -phase; |
| 43 | if (phase > advance) { |
| 44 | phase = SkScalarMod(phase, advance); |
| 45 | } |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 46 | } else { |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame^] | 47 | if (phase > advance) { |
| 48 | phase = SkScalarMod(phase, advance); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | } |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame^] | 50 | phase = advance - phase; |
reed | 653db51 | 2016-02-22 06:34:47 -0800 | [diff] [blame] | 51 | } |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame^] | 52 | // now catch the edge case where phase == advance (within epsilon) |
| 53 | if (phase >= advance) { |
| 54 | phase = 0; |
| 55 | } |
| 56 | SkASSERT(phase >= 0); |
| 57 | |
| 58 | fAdvance = advance; |
| 59 | fInitialOffset = phase; |
| 60 | |
| 61 | if ((unsigned)style > kMorph_Style) { |
| 62 | SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style)); |
| 63 | } |
| 64 | fStyle = style; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | } |
| 66 | |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 67 | bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src, |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 68 | SkStrokeRec* rec, const SkRect* cullRect) const { |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 69 | if (fAdvance > 0) { |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 70 | rec->setFillStyle(); |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 71 | return this->INHERITED::filterPath(dst, src, rec, cullRect); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 76 | static bool morphpoints(SkPoint dst[], const SkPoint src[], int count, |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 77 | SkPathMeasure& meas, SkScalar dist) { |
| 78 | for (int i = 0; i < count; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | SkPoint pos; |
| 80 | SkVector tangent; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 81 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | SkScalar sx = src[i].fX; |
| 83 | SkScalar sy = src[i].fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 85 | if (!meas.getPosTan(dist + sx, &pos, &tangent)) { |
| 86 | return false; |
| 87 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 88 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | SkMatrix matrix; |
| 90 | SkPoint pt; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 91 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | pt.set(sx, sy); |
| 93 | matrix.setSinCos(tangent.fY, tangent.fX, 0, 0); |
| 94 | matrix.preTranslate(-sx, 0); |
| 95 | matrix.postTranslate(pos.fX, pos.fY); |
| 96 | matrix.mapPoints(&dst[i], &pt, 1); |
| 97 | } |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 98 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* TODO |
| 102 | |
| 103 | Need differentially more subdivisions when the follow-path is curvy. Not sure how to |
| 104 | determine that, but we need it. I guess a cheap answer is let the caller tell us, |
| 105 | but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out. |
| 106 | */ |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 107 | static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas, |
| 108 | SkScalar dist) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | SkPath::Iter iter(src, false); |
| 110 | SkPoint srcP[4], dstP[3]; |
| 111 | SkPath::Verb verb; |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 112 | |
| 113 | while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | switch (verb) { |
| 115 | case SkPath::kMove_Verb: |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 116 | if (morphpoints(dstP, srcP, 1, meas, dist)) { |
| 117 | dst->moveTo(dstP[0]); |
| 118 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | break; |
| 120 | case SkPath::kLine_Verb: |
| 121 | srcP[2] = srcP[1]; |
| 122 | srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX), |
| 123 | SkScalarAve(srcP[0].fY, srcP[2].fY)); |
| 124 | // fall through to quad |
| 125 | case SkPath::kQuad_Verb: |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 126 | if (morphpoints(dstP, &srcP[1], 2, meas, dist)) { |
| 127 | dst->quadTo(dstP[0], dstP[1]); |
| 128 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | break; |
| 130 | case SkPath::kCubic_Verb: |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 131 | if (morphpoints(dstP, &srcP[1], 3, meas, dist)) { |
| 132 | dst->cubicTo(dstP[0], dstP[1], dstP[2]); |
| 133 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | break; |
| 135 | case SkPath::kClose_Verb: |
| 136 | dst->close(); |
| 137 | break; |
| 138 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 139 | SkDEBUGFAIL("unknown verb"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | break; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 145 | SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | return fInitialOffset; |
| 147 | } |
| 148 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 149 | SkFlattenable* SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) { |
| 150 | SkScalar advance = buffer.readScalar(); |
| 151 | if (advance > 0) { |
| 152 | SkPath path; |
| 153 | buffer.readPath(&path); |
| 154 | SkScalar phase = buffer.readScalar(); |
| 155 | Style style = (Style)buffer.readUInt(); |
| 156 | return SkPath1DPathEffect::Create(path, advance, phase, style); |
| 157 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 158 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 159 | } |
| 160 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 161 | void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | buffer.writeScalar(fAdvance); |
| 163 | if (fAdvance > 0) { |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 164 | buffer.writePath(fPath); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | buffer.writeScalar(fInitialOffset); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 166 | buffer.writeUInt(fStyle); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 170 | SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance, |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 171 | SkPathMeasure& meas) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | switch (fStyle) { |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 173 | case kTranslate_Style: { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | SkPoint pos; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 175 | if (meas.getPosTan(distance, &pos, nullptr)) { |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 176 | dst->addPath(fPath, pos.fX, pos.fY); |
| 177 | } |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 178 | } break; |
| 179 | case kRotate_Style: { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | SkMatrix matrix; |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 181 | if (meas.getMatrix(distance, &matrix)) { |
| 182 | dst->addPath(fPath, matrix); |
| 183 | } |
reed@google.com | d7a6fb9 | 2011-08-12 14:04:15 +0000 | [diff] [blame] | 184 | } break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | case kMorph_Style: |
| 186 | morphpath(dst, fPath, meas, distance); |
| 187 | break; |
| 188 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 189 | SkDEBUGFAIL("unknown Style enum"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | return fAdvance; |
| 193 | } |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 194 | |
| 195 | |
| 196 | #ifndef SK_IGNORE_TO_STRING |
| 197 | void SkPath1DPathEffect::toString(SkString* str) const { |
| 198 | str->appendf("SkPath1DPathEffect: ("); |
| 199 | // TODO: add path and style |
| 200 | str->appendf("advance: %.2f phase %.2f", fAdvance, fInitialOffset); |
| 201 | str->appendf(")"); |
| 202 | } |
| 203 | #endif |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame^] | 204 | |
| 205 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 206 | |
| 207 | SkPathEffect* SkPath1DPathEffect::Create(const SkPath& path, SkScalar advance, SkScalar phase, |
| 208 | Style style) { |
| 209 | if (advance <= 0 || path.isEmpty()) { |
| 210 | return nullptr; |
| 211 | } |
| 212 | return new SkPath1DPathEffect(path, advance, phase, style); |
| 213 | } |