• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
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 
9 #include "Sk1DPathEffect.h"
10 #include "SkReadBuffer.h"
11 #include "SkWriteBuffer.h"
12 #include "SkPathMeasure.h"
13 #include "SkStrokeRec.h"
14 
filterPath(SkPath * dst,const SkPath & src,SkStrokeRec *,const SkRect *) const15 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 
SkPath1DPathEffect(const SkPath & path,SkScalar advance,SkScalar phase,Style style)34 SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance,
35     SkScalar phase, Style style) : fPath(path)
36 {
37     SkASSERT(advance > 0 && !path.isEmpty());
38     // cleanup their phase parameter, inverting it so that it becomes an
39     // offset along the path (to match the interpretation in PostScript)
40     if (phase < 0) {
41         phase = -phase;
42         if (phase > advance) {
43             phase = SkScalarMod(phase, advance);
44         }
45     } else {
46         if (phase > advance) {
47             phase = SkScalarMod(phase, advance);
48         }
49         phase = advance - phase;
50     }
51     // now catch the edge case where phase == advance (within epsilon)
52     if (phase >= advance) {
53         phase = 0;
54     }
55     SkASSERT(phase >= 0);
56 
57     fAdvance = advance;
58     fInitialOffset = phase;
59 
60     if ((unsigned)style > kMorph_Style) {
61         SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
62     }
63     fStyle = style;
64 }
65 
filterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * cullRect) const66 bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
67                             SkStrokeRec* rec, const SkRect* cullRect) const {
68     if (fAdvance > 0) {
69         rec->setFillStyle();
70         return this->INHERITED::filterPath(dst, src, rec, cullRect);
71     }
72     return false;
73 }
74 
morphpoints(SkPoint dst[],const SkPoint src[],int count,SkPathMeasure & meas,SkScalar dist)75 static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
76                         SkPathMeasure& meas, SkScalar dist) {
77     for (int i = 0; i < count; i++) {
78         SkPoint pos;
79         SkVector tangent;
80 
81         SkScalar sx = src[i].fX;
82         SkScalar sy = src[i].fY;
83 
84         if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
85             return false;
86         }
87 
88         SkMatrix    matrix;
89         SkPoint     pt;
90 
91         pt.set(sx, sy);
92         matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
93         matrix.preTranslate(-sx, 0);
94         matrix.postTranslate(pos.fX, pos.fY);
95         matrix.mapPoints(&dst[i], &pt, 1);
96     }
97     return true;
98 }
99 
100 /*  TODO
101 
102 Need differentially more subdivisions when the follow-path is curvy. Not sure how to
103 determine that, but we need it. I guess a cheap answer is let the caller tell us,
104 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
105 */
morphpath(SkPath * dst,const SkPath & src,SkPathMeasure & meas,SkScalar dist)106 static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
107                       SkScalar dist) {
108     SkPath::Iter    iter(src, false);
109     SkPoint         srcP[4], dstP[3];
110     SkPath::Verb    verb;
111 
112     while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
113         switch (verb) {
114             case SkPath::kMove_Verb:
115                 if (morphpoints(dstP, srcP, 1, meas, dist)) {
116                     dst->moveTo(dstP[0]);
117                 }
118                 break;
119             case SkPath::kLine_Verb:
120                 srcP[2] = srcP[1];
121                 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
122                             SkScalarAve(srcP[0].fY, srcP[2].fY));
123                 // fall through to quad
124             case SkPath::kQuad_Verb:
125                 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
126                     dst->quadTo(dstP[0], dstP[1]);
127                 }
128                 break;
129             case SkPath::kConic_Verb:
130                 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
131                     dst->conicTo(dstP[0], dstP[1], iter.conicWeight());
132                 }
133                 break;
134             case SkPath::kCubic_Verb:
135                 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
136                     dst->cubicTo(dstP[0], dstP[1], dstP[2]);
137                 }
138                 break;
139             case SkPath::kClose_Verb:
140                 dst->close();
141                 break;
142             default:
143                 SkDEBUGFAIL("unknown verb");
144                 break;
145         }
146     }
147 }
148 
begin(SkScalar contourLength) const149 SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
150     return fInitialOffset;
151 }
152 
CreateProc(SkReadBuffer & buffer)153 sk_sp<SkFlattenable> SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
154     SkScalar advance = buffer.readScalar();
155     if (advance > 0) {
156         SkPath path;
157         buffer.readPath(&path);
158         SkScalar phase = buffer.readScalar();
159         Style style = (Style)buffer.readUInt();
160         return SkPath1DPathEffect::Make(path, advance, phase, style);
161     }
162     return nullptr;
163 }
164 
flatten(SkWriteBuffer & buffer) const165 void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
166     buffer.writeScalar(fAdvance);
167     if (fAdvance > 0) {
168         buffer.writePath(fPath);
169         buffer.writeScalar(fInitialOffset);
170         buffer.writeUInt(fStyle);
171     }
172 }
173 
next(SkPath * dst,SkScalar distance,SkPathMeasure & meas) const174 SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
175                                   SkPathMeasure& meas) const {
176     switch (fStyle) {
177         case kTranslate_Style: {
178             SkPoint pos;
179             if (meas.getPosTan(distance, &pos, nullptr)) {
180                 dst->addPath(fPath, pos.fX, pos.fY);
181             }
182         } break;
183         case kRotate_Style: {
184             SkMatrix matrix;
185             if (meas.getMatrix(distance, &matrix)) {
186                 dst->addPath(fPath, matrix);
187             }
188         } break;
189         case kMorph_Style:
190             morphpath(dst, fPath, meas, distance);
191             break;
192         default:
193             SkDEBUGFAIL("unknown Style enum");
194             break;
195     }
196     return fAdvance;
197 }
198 
199 
200 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const201 void SkPath1DPathEffect::toString(SkString* str) const {
202     str->appendf("SkPath1DPathEffect: (");
203     // TODO: add path and style
204     str->appendf("advance: %.2f phase %.2f", fAdvance, fInitialOffset);
205     str->appendf(")");
206 }
207 #endif
208 
209 ///////////////////////////////////////////////////////////////////////////////////////////////////
210 
Make(const SkPath & path,SkScalar advance,SkScalar phase,Style style)211 sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase,
212                                              Style style) {
213     if (advance <= 0 || !SkScalarIsFinite(advance) ||
214         !SkScalarIsFinite(phase) || path.isEmpty()) {
215         return nullptr;
216     }
217     return sk_sp<SkPathEffect>(new SkPath1DPathEffect(path, advance, phase, style));
218 }
219