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 9 10 #include "Sk1DPathEffect.h" 11 #include "SkReadBuffer.h" 12 #include "SkWriteBuffer.h" 13 #include "SkPathMeasure.h" 14 15 bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src, 16 SkStrokeRec*, const SkRect*) const { 17 SkPathMeasure meas(src, false); 18 do { 19 SkScalar length = meas.getLength(); 20 SkScalar distance = this->begin(length); 21 while (distance < length) { 22 SkScalar delta = this->next(dst, distance, meas); 23 if (delta <= 0) { 24 break; 25 } 26 distance += delta; 27 } 28 } while (meas.nextContour()); 29 return true; 30 } 31 32 /////////////////////////////////////////////////////////////////////////////// 33 34 SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance, 35 SkScalar phase, Style style) : fPath(path) 36 { 37 if (advance <= 0 || path.isEmpty()) { 38 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n")); 39 fAdvance = 0; // signals we can't draw anything 40 fInitialOffset = 0; 41 fStyle = kStyleCount; 42 } else { 43 // cleanup their phase parameter, inverting it so that it becomes an 44 // offset along the path (to match the interpretation in PostScript) 45 if (phase < 0) { 46 phase = -phase; 47 if (phase > advance) { 48 phase = SkScalarMod(phase, advance); 49 } 50 } else { 51 if (phase > advance) { 52 phase = SkScalarMod(phase, advance); 53 } 54 phase = advance - phase; 55 } 56 // now catch the edge case where phase == advance (within epsilon) 57 if (phase >= advance) { 58 phase = 0; 59 } 60 SkASSERT(phase >= 0); 61 62 fAdvance = advance; 63 fInitialOffset = phase; 64 65 if ((unsigned)style >= kStyleCount) { 66 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style)); 67 } 68 fStyle = style; 69 } 70 } 71 72 bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src, 73 SkStrokeRec* rec, const SkRect* cullRect) const { 74 if (fAdvance > 0) { 75 rec->setFillStyle(); 76 return this->INHERITED::filterPath(dst, src, rec, cullRect); 77 } 78 return false; 79 } 80 81 static bool morphpoints(SkPoint dst[], const SkPoint src[], int count, 82 SkPathMeasure& meas, SkScalar dist) { 83 for (int i = 0; i < count; i++) { 84 SkPoint pos; 85 SkVector tangent; 86 87 SkScalar sx = src[i].fX; 88 SkScalar sy = src[i].fY; 89 90 if (!meas.getPosTan(dist + sx, &pos, &tangent)) { 91 return false; 92 } 93 94 SkMatrix matrix; 95 SkPoint pt; 96 97 pt.set(sx, sy); 98 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0); 99 matrix.preTranslate(-sx, 0); 100 matrix.postTranslate(pos.fX, pos.fY); 101 matrix.mapPoints(&dst[i], &pt, 1); 102 } 103 return true; 104 } 105 106 /* TODO 107 108 Need differentially more subdivisions when the follow-path is curvy. Not sure how to 109 determine that, but we need it. I guess a cheap answer is let the caller tell us, 110 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out. 111 */ 112 static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas, 113 SkScalar dist) { 114 SkPath::Iter iter(src, false); 115 SkPoint srcP[4], dstP[3]; 116 SkPath::Verb verb; 117 118 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) { 119 switch (verb) { 120 case SkPath::kMove_Verb: 121 if (morphpoints(dstP, srcP, 1, meas, dist)) { 122 dst->moveTo(dstP[0]); 123 } 124 break; 125 case SkPath::kLine_Verb: 126 srcP[2] = srcP[1]; 127 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX), 128 SkScalarAve(srcP[0].fY, srcP[2].fY)); 129 // fall through to quad 130 case SkPath::kQuad_Verb: 131 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) { 132 dst->quadTo(dstP[0], dstP[1]); 133 } 134 break; 135 case SkPath::kCubic_Verb: 136 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) { 137 dst->cubicTo(dstP[0], dstP[1], dstP[2]); 138 } 139 break; 140 case SkPath::kClose_Verb: 141 dst->close(); 142 break; 143 default: 144 SkDEBUGFAIL("unknown verb"); 145 break; 146 } 147 } 148 } 149 150 SkPath1DPathEffect::SkPath1DPathEffect(SkReadBuffer& buffer) { 151 fAdvance = buffer.readScalar(); 152 if (fAdvance > 0) { 153 buffer.readPath(&fPath); 154 fInitialOffset = buffer.readScalar(); 155 fStyle = (Style) buffer.readUInt(); 156 } else { 157 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n")); 158 // Make Coverity happy. 159 fInitialOffset = 0; 160 fStyle = kStyleCount; 161 } 162 } 163 164 SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const { 165 return fInitialOffset; 166 } 167 168 void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const { 169 this->INHERITED::flatten(buffer); 170 buffer.writeScalar(fAdvance); 171 if (fAdvance > 0) { 172 buffer.writePath(fPath); 173 buffer.writeScalar(fInitialOffset); 174 buffer.writeUInt(fStyle); 175 } 176 } 177 178 SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance, 179 SkPathMeasure& meas) const { 180 switch (fStyle) { 181 case kTranslate_Style: { 182 SkPoint pos; 183 if (meas.getPosTan(distance, &pos, NULL)) { 184 dst->addPath(fPath, pos.fX, pos.fY); 185 } 186 } break; 187 case kRotate_Style: { 188 SkMatrix matrix; 189 if (meas.getMatrix(distance, &matrix)) { 190 dst->addPath(fPath, matrix); 191 } 192 } break; 193 case kMorph_Style: 194 morphpath(dst, fPath, meas, distance); 195 break; 196 default: 197 SkDEBUGFAIL("unknown Style enum"); 198 break; 199 } 200 return fAdvance; 201 } 202