• 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 "include/core/SkPathMeasure.h"
10 #include "include/core/SkStrokeRec.h"
11 #include "include/effects/Sk1DPathEffect.h"
12 #include "src/core/SkPathEffectBase.h"
13 #include "src/core/SkReadBuffer.h"
14 #include "src/core/SkWriteBuffer.h"
15 
16 // Since we are stepping by a float, the do/while loop might go on forever (or nearly so).
17 // Put in a governor to limit crash values from looping too long (and allocating too much ram).
18 #define MAX_REASONABLE_ITERATIONS   100000
19 
20 class Sk1DPathEffect : public SkPathEffectBase {
21 public:
22 protected:
onFilterPath(SkPath * dst,const SkPath & src,SkStrokeRec *,const SkRect *,const SkMatrix &) const23     bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec*, const SkRect*,
24                       const SkMatrix&) const override {
25         SkPathMeasure   meas(src, false);
26         do {
27             int governor = MAX_REASONABLE_ITERATIONS;
28             SkScalar    length = meas.getLength();
29             SkScalar    distance = this->begin(length);
30             while (distance < length && --governor >= 0) {
31                 SkScalar delta = this->next(dst, distance, meas);
32                 if (delta <= 0) {
33                     break;
34                 }
35                 distance += delta;
36             }
37         } while (meas.nextContour());
38         return true;
39     }
40 
41     /** Called at the start of each contour, returns the initial offset
42         into that contour.
43     */
44     virtual SkScalar begin(SkScalar contourLength) const = 0;
45     /** Called with the current distance along the path, with the current matrix
46         for the point/tangent at the specified distance.
47         Return the distance to travel for the next call. If return <= 0, then that
48         contour is done.
49     */
50     virtual SkScalar next(SkPath* dst, SkScalar dist, SkPathMeasure&) const = 0;
51 
52 private:
53     // For simplicity, assume fast bounds cannot be computed
computeFastBounds(SkRect *) const54     bool computeFastBounds(SkRect*) const override { return false; }
55 
56     using INHERITED = SkPathEffect;
57 };
58 
59 ///////////////////////////////////////////////////////////////////////////////
60 
61 class SkPath1DPathEffectImpl : public Sk1DPathEffect {
62 public:
SkPath1DPathEffectImpl(const SkPath & path,SkScalar advance,SkScalar phase,SkPath1DPathEffect::Style style)63     SkPath1DPathEffectImpl(const SkPath& path, SkScalar advance, SkScalar phase,
64                            SkPath1DPathEffect::Style style) : fPath(path) {
65         SkASSERT(advance > 0 && !path.isEmpty());
66 
67         // Make the path thread-safe.
68         fPath.updateBoundsCache();
69         (void)fPath.getGenerationID();
70 
71         // cleanup their phase parameter, inverting it so that it becomes an
72         // offset along the path (to match the interpretation in PostScript)
73         if (phase < 0) {
74             phase = -phase;
75             if (phase > advance) {
76                 phase = SkScalarMod(phase, advance);
77             }
78         } else {
79             if (phase > advance) {
80                 phase = SkScalarMod(phase, advance);
81             }
82             phase = advance - phase;
83         }
84         // now catch the edge case where phase == advance (within epsilon)
85         if (phase >= advance) {
86             phase = 0;
87         }
88         SkASSERT(phase >= 0);
89 
90         fAdvance = advance;
91         fInitialOffset = phase;
92         fStyle = style;
93     }
94 
onFilterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * cullRect,const SkMatrix & ctm) const95     bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
96                       const SkRect* cullRect, const SkMatrix& ctm) const override {
97         rec->setFillStyle();
98         return this->INHERITED::onFilterPath(dst, src, rec, cullRect, ctm);
99     }
100 
begin(SkScalar contourLength) const101     SkScalar begin(SkScalar contourLength) const override {
102         return fInitialOffset;
103     }
104 
105     SkScalar next(SkPath*, SkScalar, SkPathMeasure&) const override;
106 
CreateProc(SkReadBuffer & buffer)107     static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) {
108         SkScalar advance = buffer.readScalar();
109         SkPath path;
110         buffer.readPath(&path);
111         SkScalar phase = buffer.readScalar();
112         SkPath1DPathEffect::Style style = buffer.read32LE(SkPath1DPathEffect::kLastEnum_Style);
113         return buffer.isValid() ? SkPath1DPathEffect::Make(path, advance, phase, style) : nullptr;
114     }
115 
flatten(SkWriteBuffer & buffer) const116     void flatten(SkWriteBuffer& buffer) const override {
117         buffer.writeScalar(fAdvance);
118         buffer.writePath(fPath);
119         buffer.writeScalar(fInitialOffset);
120         buffer.writeUInt(fStyle);
121     }
122 
getFactory() const123     Factory getFactory() const override { return CreateProc; }
getTypeName() const124     const char* getTypeName() const override { return "SkPath1DPathEffect"; }
125 
126 private:
127     SkPath                      fPath;          // copied from constructor
128     SkScalar                    fAdvance;       // copied from constructor
129     SkScalar                    fInitialOffset; // computed from phase
130     SkPath1DPathEffect::Style   fStyle;         // copied from constructor
131 
132     using INHERITED = Sk1DPathEffect;
133 };
134 
morphpoints(SkPoint dst[],const SkPoint src[],int count,SkPathMeasure & meas,SkScalar dist)135 static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
136                         SkPathMeasure& meas, SkScalar dist) {
137     for (int i = 0; i < count; i++) {
138         SkPoint pos;
139         SkVector tangent;
140 
141         SkScalar sx = src[i].fX;
142         SkScalar sy = src[i].fY;
143 
144         if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
145             return false;
146         }
147 
148         SkMatrix    matrix;
149         SkPoint     pt;
150 
151         pt.set(sx, sy);
152         matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
153         matrix.preTranslate(-sx, 0);
154         matrix.postTranslate(pos.fX, pos.fY);
155         matrix.mapPoints(&dst[i], &pt, 1);
156     }
157     return true;
158 }
159 
160 /*  TODO
161 
162 Need differentially more subdivisions when the follow-path is curvy. Not sure how to
163 determine that, but we need it. I guess a cheap answer is let the caller tell us,
164 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
165 */
morphpath(SkPath * dst,const SkPath & src,SkPathMeasure & meas,SkScalar dist)166 static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
167                       SkScalar dist) {
168     SkPath::Iter    iter(src, false);
169     SkPoint         srcP[4], dstP[3];
170     SkPath::Verb    verb;
171 
172     while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
173         switch (verb) {
174             case SkPath::kMove_Verb:
175                 if (morphpoints(dstP, srcP, 1, meas, dist)) {
176                     dst->moveTo(dstP[0]);
177                 }
178                 break;
179             case SkPath::kLine_Verb:
180                 srcP[2] = srcP[1];
181                 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
182                             SkScalarAve(srcP[0].fY, srcP[2].fY));
183                 [[fallthrough]];
184             case SkPath::kQuad_Verb:
185                 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
186                     dst->quadTo(dstP[0], dstP[1]);
187                 }
188                 break;
189             case SkPath::kConic_Verb:
190                 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
191                     dst->conicTo(dstP[0], dstP[1], iter.conicWeight());
192                 }
193                 break;
194             case SkPath::kCubic_Verb:
195                 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
196                     dst->cubicTo(dstP[0], dstP[1], dstP[2]);
197                 }
198                 break;
199             case SkPath::kClose_Verb:
200                 dst->close();
201                 break;
202             default:
203                 SkDEBUGFAIL("unknown verb");
204                 break;
205         }
206     }
207 }
208 
next(SkPath * dst,SkScalar distance,SkPathMeasure & meas) const209 SkScalar SkPath1DPathEffectImpl::next(SkPath* dst, SkScalar distance,
210                                       SkPathMeasure& meas) const {
211 #if defined(SK_BUILD_FOR_FUZZER)
212     if (dst->countPoints() > 100000) {
213         return fAdvance;
214     }
215 #endif
216     switch (fStyle) {
217         case SkPath1DPathEffect::kTranslate_Style: {
218             SkPoint pos;
219             if (meas.getPosTan(distance, &pos, nullptr)) {
220                 dst->addPath(fPath, pos.fX, pos.fY);
221             }
222         } break;
223         case SkPath1DPathEffect::kRotate_Style: {
224             SkMatrix matrix;
225             if (meas.getMatrix(distance, &matrix)) {
226                 dst->addPath(fPath, matrix);
227             }
228         } break;
229         case SkPath1DPathEffect::kMorph_Style:
230             morphpath(dst, fPath, meas, distance);
231             break;
232     }
233     return fAdvance;
234 }
235 
236 ///////////////////////////////////////////////////////////////////////////////////////////////////
237 
Make(const SkPath & path,SkScalar advance,SkScalar phase,Style style)238 sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase,
239                                              Style style) {
240     if (advance <= 0 || !SkScalarIsFinite(advance) || !SkScalarIsFinite(phase) || path.isEmpty()) {
241         return nullptr;
242     }
243     return sk_sp<SkPathEffect>(new SkPath1DPathEffectImpl(path, advance, phase, style));
244 }
245 
RegisterFlattenables()246 void SkPath1DPathEffect::RegisterFlattenables() {
247     SK_REGISTER_FLATTENABLE(SkPath1DPathEffectImpl);
248 }
249