• 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 "SkPathMeasure.h"
12 
filterPath(SkPath * dst,const SkPath & src,SkScalar * width)13 bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width) {
14     SkPathMeasure   meas(src, false);
15     do {
16         SkScalar    length = meas.getLength();
17         SkScalar    distance = this->begin(length);
18         while (distance < length) {
19             SkScalar delta = this->next(dst, distance, meas);
20             if (delta <= 0) {
21                 break;
22             }
23             distance += delta;
24         }
25     } while (meas.nextContour());
26     return true;
27 }
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 
SkPath1DPathEffect(const SkPath & path,SkScalar advance,SkScalar phase,Style style)31 SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance,
32     SkScalar phase, Style style) : fPath(path)
33 {
34     if (advance <= 0 || path.isEmpty()) {
35         SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
36         fAdvance = 0;   // signals we can't draw anything
37     } else {
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 >= kStyleCount) {
61             SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
62         }
63         fStyle = style;
64     }
65 }
66 
filterPath(SkPath * dst,const SkPath & src,SkScalar * width)67 bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
68                                     SkScalar* width) {
69     if (fAdvance > 0) {
70         *width = -1;
71         return this->INHERITED::filterPath(dst, src, width);
72     }
73     return false;
74 }
75 
morphpoints(SkPoint dst[],const SkPoint src[],int count,SkPathMeasure & meas,SkScalar dist)76 static void morphpoints(SkPoint dst[], const SkPoint src[], int count,
77                         SkPathMeasure& meas, SkScalar dist) {
78     for (int i = 0; i < count; i++) {
79         SkPoint pos;
80         SkVector tangent;
81 
82         SkScalar sx = src[i].fX;
83         SkScalar sy = src[i].fY;
84 
85         meas.getPosTan(dist + sx, &pos, &tangent);
86 
87         SkMatrix    matrix;
88         SkPoint     pt;
89 
90         pt.set(sx, sy);
91         matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
92         matrix.preTranslate(-sx, 0);
93         matrix.postTranslate(pos.fX, pos.fY);
94         matrix.mapPoints(&dst[i], &pt, 1);
95     }
96 }
97 
98 /*  TODO
99 
100 Need differentially more subdivisions when the follow-path is curvy. Not sure how to
101 determine that, but we need it. I guess a cheap answer is let the caller tell us,
102 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
103 */
morphpath(SkPath * dst,const SkPath & src,SkPathMeasure & meas,SkScalar dist)104 static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
105                       SkScalar dist) {
106     SkPath::Iter    iter(src, false);
107     SkPoint         srcP[4], dstP[3];
108     SkPath::Verb    verb;
109 
110     while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
111         switch (verb) {
112             case SkPath::kMove_Verb:
113                 morphpoints(dstP, srcP, 1, meas, dist);
114                 dst->moveTo(dstP[0]);
115                 break;
116             case SkPath::kLine_Verb:
117                 srcP[2] = srcP[1];
118                 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
119                             SkScalarAve(srcP[0].fY, srcP[2].fY));
120                 // fall through to quad
121             case SkPath::kQuad_Verb:
122                 morphpoints(dstP, &srcP[1], 2, meas, dist);
123                 dst->quadTo(dstP[0], dstP[1]);
124                 break;
125             case SkPath::kCubic_Verb:
126                 morphpoints(dstP, &srcP[1], 3, meas, dist);
127                 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
128                 break;
129             case SkPath::kClose_Verb:
130                 dst->close();
131                 break;
132             default:
133                 SkDEBUGFAIL("unknown verb");
134                 break;
135         }
136     }
137 }
138 
SkPath1DPathEffect(SkFlattenableReadBuffer & buffer)139 SkPath1DPathEffect::SkPath1DPathEffect(SkFlattenableReadBuffer& buffer) {
140     fAdvance = buffer.readScalar();
141     if (fAdvance > 0) {
142         fPath.unflatten(buffer);
143         fInitialOffset = buffer.readScalar();
144         fStyle = (Style) buffer.readU8();
145     }
146 }
147 
begin(SkScalar contourLength)148 SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) {
149     return fInitialOffset;
150 }
151 
flatten(SkFlattenableWriteBuffer & buffer)152 void SkPath1DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) {
153     buffer.writeScalar(fAdvance);
154     if (fAdvance > 0) {
155         fPath.flatten(buffer);
156         buffer.writeScalar(fInitialOffset);
157         buffer.write8(fStyle);
158     }
159 }
160 
next(SkPath * dst,SkScalar distance,SkPathMeasure & meas)161 SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
162                                   SkPathMeasure& meas) {
163     switch (fStyle) {
164         case kTranslate_Style: {
165             SkPoint pos;
166             meas.getPosTan(distance, &pos, NULL);
167             dst->addPath(fPath, pos.fX, pos.fY);
168         } break;
169         case kRotate_Style: {
170             SkMatrix matrix;
171             meas.getMatrix(distance, &matrix);
172             dst->addPath(fPath, matrix);
173         } break;
174         case kMorph_Style:
175             morphpath(dst, fPath, meas, distance);
176             break;
177         default:
178             SkDEBUGFAIL("unknown Style enum");
179             break;
180     }
181     return fAdvance;
182 }
183 
184 ///////////////////////////////////////////////////////////////////////////////
185 
186 SK_DEFINE_FLATTENABLE_REGISTRAR(SkPath1DPathEffect)
187 
188