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