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
15 // Since we are stepping by a float, the do/while loop might go on forever (or nearly so).
16 // Put in a governor to limit crash values from looping too long (and allocating too much ram).
17 #define MAX_REASONABLE_ITERATIONS 100000
18
filterPath(SkPath * dst,const SkPath & src,SkStrokeRec *,const SkRect *) const19 bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
20 SkStrokeRec*, const SkRect*) const {
21 SkPathMeasure meas(src, false);
22 do {
23 int governor = MAX_REASONABLE_ITERATIONS;
24 SkScalar length = meas.getLength();
25 SkScalar distance = this->begin(length);
26 while (distance < length && --governor >= 0) {
27 SkScalar delta = this->next(dst, distance, meas);
28 if (delta <= 0) {
29 break;
30 }
31 distance += delta;
32 }
33 } while (meas.nextContour());
34 return true;
35 }
36
37 ///////////////////////////////////////////////////////////////////////////////
38
SkPath1DPathEffect(const SkPath & path,SkScalar advance,SkScalar phase,Style style)39 SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance, SkScalar phase,
40 Style style) : fPath(path) {
41 SkASSERT(advance > 0 && !path.isEmpty());
42 SkASSERT((unsigned)style <= kMorph_Style);
43
44 // cleanup their phase parameter, inverting it so that it becomes an
45 // offset along the path (to match the interpretation in PostScript)
46 if (phase < 0) {
47 phase = -phase;
48 if (phase > advance) {
49 phase = SkScalarMod(phase, advance);
50 }
51 } else {
52 if (phase > advance) {
53 phase = SkScalarMod(phase, advance);
54 }
55 phase = advance - phase;
56 }
57 // now catch the edge case where phase == advance (within epsilon)
58 if (phase >= advance) {
59 phase = 0;
60 }
61 SkASSERT(phase >= 0);
62
63 fAdvance = advance;
64 fInitialOffset = phase;
65
66 if ((unsigned)style > kMorph_Style) {
67 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
68 }
69 fStyle = style;
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 rec->setFillStyle();
75 return this->INHERITED::filterPath(dst, src, rec, cullRect);
76 }
77
morphpoints(SkPoint dst[],const SkPoint src[],int count,SkPathMeasure & meas,SkScalar dist)78 static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
79 SkPathMeasure& meas, SkScalar dist) {
80 for (int i = 0; i < count; i++) {
81 SkPoint pos;
82 SkVector tangent;
83
84 SkScalar sx = src[i].fX;
85 SkScalar sy = src[i].fY;
86
87 if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
88 return false;
89 }
90
91 SkMatrix matrix;
92 SkPoint pt;
93
94 pt.set(sx, sy);
95 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
96 matrix.preTranslate(-sx, 0);
97 matrix.postTranslate(pos.fX, pos.fY);
98 matrix.mapPoints(&dst[i], &pt, 1);
99 }
100 return true;
101 }
102
103 /* TODO
104
105 Need differentially more subdivisions when the follow-path is curvy. Not sure how to
106 determine that, but we need it. I guess a cheap answer is let the caller tell us,
107 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
108 */
morphpath(SkPath * dst,const SkPath & src,SkPathMeasure & meas,SkScalar dist)109 static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
110 SkScalar dist) {
111 SkPath::Iter iter(src, false);
112 SkPoint srcP[4], dstP[3];
113 SkPath::Verb verb;
114
115 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
116 switch (verb) {
117 case SkPath::kMove_Verb:
118 if (morphpoints(dstP, srcP, 1, meas, dist)) {
119 dst->moveTo(dstP[0]);
120 }
121 break;
122 case SkPath::kLine_Verb:
123 srcP[2] = srcP[1];
124 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
125 SkScalarAve(srcP[0].fY, srcP[2].fY));
126 // fall through to quad
127 case SkPath::kQuad_Verb:
128 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
129 dst->quadTo(dstP[0], dstP[1]);
130 }
131 break;
132 case SkPath::kConic_Verb:
133 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
134 dst->conicTo(dstP[0], dstP[1], iter.conicWeight());
135 }
136 break;
137 case SkPath::kCubic_Verb:
138 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
139 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
140 }
141 break;
142 case SkPath::kClose_Verb:
143 dst->close();
144 break;
145 default:
146 SkDEBUGFAIL("unknown verb");
147 break;
148 }
149 }
150 }
151
begin(SkScalar contourLength) const152 SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
153 return fInitialOffset;
154 }
155
CreateProc(SkReadBuffer & buffer)156 sk_sp<SkFlattenable> SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
157 SkScalar advance = buffer.readScalar();
158 SkPath path;
159 buffer.readPath(&path);
160 SkScalar phase = buffer.readScalar();
161 Style style = buffer.read32LE(kLastEnum_Style);
162 return buffer.isValid() ? SkPath1DPathEffect::Make(path, advance, phase, style) : nullptr;
163 }
164
flatten(SkWriteBuffer & buffer) const165 void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
166 buffer.writeScalar(fAdvance);
167 buffer.writePath(fPath);
168 buffer.writeScalar(fInitialOffset);
169 buffer.writeUInt(fStyle);
170 }
171
next(SkPath * dst,SkScalar distance,SkPathMeasure & meas) const172 SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
173 SkPathMeasure& meas) const {
174 switch (fStyle) {
175 case kTranslate_Style: {
176 SkPoint pos;
177 if (meas.getPosTan(distance, &pos, nullptr)) {
178 dst->addPath(fPath, pos.fX, pos.fY);
179 }
180 } break;
181 case kRotate_Style: {
182 SkMatrix matrix;
183 if (meas.getMatrix(distance, &matrix)) {
184 dst->addPath(fPath, matrix);
185 }
186 } break;
187 case kMorph_Style:
188 morphpath(dst, fPath, meas, distance);
189 break;
190 default:
191 SkDEBUGFAIL("unknown Style enum");
192 break;
193 }
194 return fAdvance;
195 }
196
197
198 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const199 void SkPath1DPathEffect::toString(SkString* str) const {
200 str->appendf("SkPath1DPathEffect: (");
201 // TODO: add path and style
202 str->appendf("advance: %.2f phase %.2f", fAdvance, fInitialOffset);
203 str->appendf(")");
204 }
205 #endif
206
207 ///////////////////////////////////////////////////////////////////////////////////////////////////
208
Make(const SkPath & path,SkScalar advance,SkScalar phase,Style style)209 sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase,
210 Style style) {
211 if (advance <= 0 || !SkScalarIsFinite(advance) || !SkScalarIsFinite(phase) || path.isEmpty()) {
212 return nullptr;
213 }
214 return sk_sp<SkPathEffect>(new SkPath1DPathEffect(path, advance, phase, style));
215 }
216