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
begin(SkScalar contourLength) const150 SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
151 return fInitialOffset;
152 }
153
CreateProc(SkReadBuffer & buffer)154 SkFlattenable* SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
155 SkScalar advance = buffer.readScalar();
156 if (advance > 0) {
157 SkPath path;
158 buffer.readPath(&path);
159 SkScalar phase = buffer.readScalar();
160 Style style = (Style)buffer.readUInt();
161 return SkPath1DPathEffect::Create(path, advance, phase, style);
162 }
163 return NULL;
164 }
165
flatten(SkWriteBuffer & buffer) const166 void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
167 buffer.writeScalar(fAdvance);
168 if (fAdvance > 0) {
169 buffer.writePath(fPath);
170 buffer.writeScalar(fInitialOffset);
171 buffer.writeUInt(fStyle);
172 }
173 }
174
next(SkPath * dst,SkScalar distance,SkPathMeasure & meas) const175 SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
176 SkPathMeasure& meas) const {
177 switch (fStyle) {
178 case kTranslate_Style: {
179 SkPoint pos;
180 if (meas.getPosTan(distance, &pos, NULL)) {
181 dst->addPath(fPath, pos.fX, pos.fY);
182 }
183 } break;
184 case kRotate_Style: {
185 SkMatrix matrix;
186 if (meas.getMatrix(distance, &matrix)) {
187 dst->addPath(fPath, matrix);
188 }
189 } break;
190 case kMorph_Style:
191 morphpath(dst, fPath, meas, distance);
192 break;
193 default:
194 SkDEBUGFAIL("unknown Style enum");
195 break;
196 }
197 return fAdvance;
198 }
199
200
201 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const202 void SkPath1DPathEffect::toString(SkString* str) const {
203 str->appendf("SkPath1DPathEffect: (");
204 // TODO: add path and style
205 str->appendf("advance: %.2f phase %.2f", fAdvance, fInitialOffset);
206 str->appendf(")");
207 }
208 #endif
209