blob: 0df8cbd9c1fa27637c31cad9ad62c0f4d96db9eb [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "Sk1DPathEffect.h"
11#include "SkPathMeasure.h"
12
13bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width)
14{
15 SkPathMeasure meas(src, false);
16 do {
17 SkScalar length = meas.getLength();
18 SkScalar distance = this->begin(length);
19 while (distance < length)
20 {
21 SkScalar delta = this->next(dst, distance, meas);
22 if (delta <= 0)
23 break;
24 distance += delta;
25 }
26 } while (meas.nextContour());
27 return true;
28}
29
30///////////////////////////////////////////////////////////////////////////////////////////
31
32SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance,
33 SkScalar phase, Style style) : fPath(path)
34{
35 if (advance <= 0 || path.isEmpty())
36 {
37 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
38 fAdvance = 0; // signals we can't draw anything
39 }
40 else
41 {
42 // cleanup their phase parameter, inverting it so that it becomes an
43 // offset along the path (to match the interpretation in PostScript)
44 if (phase < 0)
45 {
46 phase = -phase;
47 if (phase > advance)
48 phase = SkScalarMod(phase, advance);
49 }
50 else
51 {
52 if (phase > advance)
53 phase = SkScalarMod(phase, advance);
54 phase = advance - phase;
55 }
56 // now catch the edge case where phase == advance (within epsilon)
57 if (phase >= advance)
58 phase = 0;
59 SkASSERT(phase >= 0);
60
61 fAdvance = advance;
62 fInitialOffset = phase;
63
64 if ((unsigned)style >= kStyleCount) {
65 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
66 }
67 fStyle = style;
68 }
69}
70
71bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width)
72{
73 if (fAdvance > 0)
74 {
75 *width = -1;
76 return this->INHERITED::filterPath(dst, src, width);
77 }
78 return false;
79}
80
81static void morphpoints(SkPoint dst[], const SkPoint src[], int count,
82 SkPathMeasure& meas, SkScalar dist)
83{
84 for (int i = 0; i < count; i++)
85 {
86 SkPoint pos;
87 SkVector tangent;
88
89 SkScalar sx = src[i].fX;
90 SkScalar sy = src[i].fY;
91
92 meas.getPosTan(dist + sx, &pos, &tangent);
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}
104
105/* TODO
106
107Need differentially more subdivisions when the follow-path is curvy. Not sure how to
108determine that, but we need it. I guess a cheap answer is let the caller tell us,
109but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
110*/
111static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas, SkScalar dist)
112{
113 SkPath::Iter iter(src, false);
114 SkPoint srcP[4], dstP[3];
115 SkPath::Verb verb;
116
117 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb)
118 {
119 switch (verb) {
120 case SkPath::kMove_Verb:
121 morphpoints(dstP, srcP, 1, meas, dist);
122 dst->moveTo(dstP[0]);
123 break;
124 case SkPath::kLine_Verb:
125 srcP[2] = srcP[1];
126 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
127 SkScalarAve(srcP[0].fY, srcP[2].fY));
128 // fall through to quad
129 case SkPath::kQuad_Verb:
130 morphpoints(dstP, &srcP[1], 2, meas, dist);
131 dst->quadTo(dstP[0], dstP[1]);
132 break;
133 case SkPath::kCubic_Verb:
134 morphpoints(dstP, &srcP[1], 3, meas, dist);
135 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
136 break;
137 case SkPath::kClose_Verb:
138 dst->close();
139 break;
140 default:
141 SkASSERT(!"unknown verb");
142 break;
143 }
144 }
145}
146
147SkPath1DPathEffect::SkPath1DPathEffect(SkFlattenableReadBuffer& buffer)
148{
149 fAdvance = buffer.readScalar();
150 if (fAdvance > 0) {
151 fPath.unflatten(buffer);
152 fInitialOffset = buffer.readScalar();
153 fStyle = (Style) buffer.readU8();
154 }
155}
156
157SkScalar SkPath1DPathEffect::begin(SkScalar contourLength)
158{
159 return fInitialOffset;
160}
161
162void SkPath1DPathEffect::flatten(SkFlattenableWriteBuffer& buffer)
163{
164 buffer.writeScalar(fAdvance);
165 if (fAdvance > 0) {
166 fPath.flatten(buffer);
167 buffer.writeScalar(fInitialOffset);
168 buffer.write8(fStyle);
169 }
170}
171
172SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance, SkPathMeasure& meas)
173{
174 switch (fStyle) {
175 case kTranslate_Style:
176 {
177 SkPoint pos;
178 meas.getPosTan(distance, &pos, NULL);
179 dst->addPath(fPath, pos.fX, pos.fY);
180 }
181 break;
182 case kRotate_Style:
183 {
184 SkMatrix matrix;
185 meas.getMatrix(distance, &matrix);
186 dst->addPath(fPath, matrix);
187 }
188 break;
189 case kMorph_Style:
190 morphpath(dst, fPath, meas, distance);
191 break;
192 default:
193 SkASSERT(!"unknown Style enum");
194 break;
195 }
196 return fAdvance;
197}
198
reed@google.come28b9172011-08-09 18:14:31 +0000199///////////////////////////////////////////////////////////////////////////////
200
201static SkFlattenable::Registrar gReg("SkPath1DPathEffect",
202 SkPath1DPathEffect::CreateProc);
203