• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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     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 
filterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * cullRect) const72 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 
morphpoints(SkPoint dst[],const SkPoint src[],int count,SkPathMeasure & meas,SkScalar dist)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 */
morphpath(SkPath * dst,const SkPath & src,SkPathMeasure & meas,SkScalar dist)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 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
SkPath1DPathEffect(SkReadBuffer & buffer)151 SkPath1DPathEffect::SkPath1DPathEffect(SkReadBuffer& buffer) {
152     fAdvance = buffer.readScalar();
153     if (fAdvance > 0) {
154         buffer.readPath(&fPath);
155         fInitialOffset = buffer.readScalar();
156         fStyle = (Style) buffer.readUInt();
157     } else {
158         SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
159         // Make Coverity happy.
160         fInitialOffset = 0;
161         fStyle = kStyleCount;
162     }
163 }
164 #endif
165 
begin(SkScalar contourLength) const166 SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
167     return fInitialOffset;
168 }
169 
CreateProc(SkReadBuffer & buffer)170 SkFlattenable* SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
171     SkScalar advance = buffer.readScalar();
172     if (advance > 0) {
173         SkPath path;
174         buffer.readPath(&path);
175         SkScalar phase = buffer.readScalar();
176         Style style = (Style)buffer.readUInt();
177         return SkPath1DPathEffect::Create(path, advance, phase, style);
178     }
179     return NULL;
180 }
181 
flatten(SkWriteBuffer & buffer) const182 void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
183     buffer.writeScalar(fAdvance);
184     if (fAdvance > 0) {
185         buffer.writePath(fPath);
186         buffer.writeScalar(fInitialOffset);
187         buffer.writeUInt(fStyle);
188     }
189 }
190 
next(SkPath * dst,SkScalar distance,SkPathMeasure & meas) const191 SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
192                                   SkPathMeasure& meas) const {
193     switch (fStyle) {
194         case kTranslate_Style: {
195             SkPoint pos;
196             if (meas.getPosTan(distance, &pos, NULL)) {
197                 dst->addPath(fPath, pos.fX, pos.fY);
198             }
199         } break;
200         case kRotate_Style: {
201             SkMatrix matrix;
202             if (meas.getMatrix(distance, &matrix)) {
203                 dst->addPath(fPath, matrix);
204             }
205         } break;
206         case kMorph_Style:
207             morphpath(dst, fPath, meas, distance);
208             break;
209         default:
210             SkDEBUGFAIL("unknown Style enum");
211             break;
212     }
213     return fAdvance;
214 }
215