blob: 62ca7b39b8c5ac1e0aad9355ffe47f8aea9899d4 [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
reed@google.comd7a6fb92011-08-12 14:04:15 +000013bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000014 SkPathMeasure meas(src, false);
15 do {
16 SkScalar length = meas.getLength();
17 SkScalar distance = this->begin(length);
reed@google.comd7a6fb92011-08-12 14:04:15 +000018 while (distance < length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000019 SkScalar delta = this->next(dst, distance, meas);
reed@google.comd7a6fb92011-08-12 14:04:15 +000020 if (delta <= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021 break;
reed@google.comd7a6fb92011-08-12 14:04:15 +000022 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 distance += delta;
24 }
25 } while (meas.nextContour());
26 return true;
27}
28
reed@google.comd7a6fb92011-08-12 14:04:15 +000029///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000030
31SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance,
32 SkScalar phase, Style style) : fPath(path)
33{
reed@google.comd7a6fb92011-08-12 14:04:15 +000034 if (advance <= 0 || path.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
36 fAdvance = 0; // signals we can't draw anything
vandebo@chromium.orga728e352012-03-28 20:29:38 +000037 fInitialOffset = 0;
38 fStyle = kStyleCount;
reed@google.comd7a6fb92011-08-12 14:04:15 +000039 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 // cleanup their phase parameter, inverting it so that it becomes an
41 // offset along the path (to match the interpretation in PostScript)
reed@google.comd7a6fb92011-08-12 14:04:15 +000042 if (phase < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 phase = -phase;
reed@google.comd7a6fb92011-08-12 14:04:15 +000044 if (phase > advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 phase = SkScalarMod(phase, advance);
reed@google.comd7a6fb92011-08-12 14:04:15 +000046 }
47 } else {
48 if (phase > advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 phase = SkScalarMod(phase, advance);
reed@google.comd7a6fb92011-08-12 14:04:15 +000050 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 phase = advance - phase;
52 }
53 // now catch the edge case where phase == advance (within epsilon)
reed@google.comd7a6fb92011-08-12 14:04:15 +000054 if (phase >= advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 phase = 0;
reed@google.comd7a6fb92011-08-12 14:04:15 +000056 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 SkASSERT(phase >= 0);
58
59 fAdvance = advance;
60 fInitialOffset = phase;
61
62 if ((unsigned)style >= kStyleCount) {
63 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
64 }
65 fStyle = style;
66 }
67}
68
reed@google.comd7a6fb92011-08-12 14:04:15 +000069bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
70 SkScalar* width) {
71 if (fAdvance > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 *width = -1;
73 return this->INHERITED::filterPath(dst, src, width);
74 }
75 return false;
76}
77
78static void morphpoints(SkPoint dst[], const SkPoint src[], int count,
reed@google.comd7a6fb92011-08-12 14:04:15 +000079 SkPathMeasure& meas, SkScalar dist) {
80 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkPoint pos;
82 SkVector tangent;
83
84 SkScalar sx = src[i].fX;
85 SkScalar sy = src[i].fY;
86
87 meas.getPosTan(dist + sx, &pos, &tangent);
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}
99
100/* TODO
101
102Need differentially more subdivisions when the follow-path is curvy. Not sure how to
103determine that, but we need it. I guess a cheap answer is let the caller tell us,
104but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
105*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000106static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
107 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 SkPath::Iter iter(src, false);
109 SkPoint srcP[4], dstP[3];
110 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000111
112 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 switch (verb) {
114 case SkPath::kMove_Verb:
115 morphpoints(dstP, srcP, 1, meas, dist);
116 dst->moveTo(dstP[0]);
117 break;
118 case SkPath::kLine_Verb:
119 srcP[2] = srcP[1];
120 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
121 SkScalarAve(srcP[0].fY, srcP[2].fY));
122 // fall through to quad
123 case SkPath::kQuad_Verb:
124 morphpoints(dstP, &srcP[1], 2, meas, dist);
125 dst->quadTo(dstP[0], dstP[1]);
126 break;
127 case SkPath::kCubic_Verb:
128 morphpoints(dstP, &srcP[1], 3, meas, dist);
129 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
130 break;
131 case SkPath::kClose_Verb:
132 dst->close();
133 break;
134 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000135 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 break;
137 }
138 }
139}
140
reed@google.comd7a6fb92011-08-12 14:04:15 +0000141SkPath1DPathEffect::SkPath1DPathEffect(SkFlattenableReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 fAdvance = buffer.readScalar();
143 if (fAdvance > 0) {
144 fPath.unflatten(buffer);
145 fInitialOffset = buffer.readScalar();
146 fStyle = (Style) buffer.readU8();
vandebo@chromium.orga728e352012-03-28 20:29:38 +0000147 } else {
148 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
149 // Make Coverity happy.
150 fInitialOffset = 0;
151 fStyle = kStyleCount;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 }
153}
154
reed@google.comd7a6fb92011-08-12 14:04:15 +0000155SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 return fInitialOffset;
157}
158
djsollen@google.com54924242012-03-29 15:18:04 +0000159void SkPath1DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
160 this->INHERITED::flatten(buffer);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 buffer.writeScalar(fAdvance);
162 if (fAdvance > 0) {
163 fPath.flatten(buffer);
164 buffer.writeScalar(fInitialOffset);
165 buffer.write8(fStyle);
166 }
167}
168
reed@google.comd7a6fb92011-08-12 14:04:15 +0000169SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
170 SkPathMeasure& meas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 switch (fStyle) {
reed@google.comd7a6fb92011-08-12 14:04:15 +0000172 case kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 SkPoint pos;
174 meas.getPosTan(distance, &pos, NULL);
175 dst->addPath(fPath, pos.fX, pos.fY);
reed@google.comd7a6fb92011-08-12 14:04:15 +0000176 } break;
177 case kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 SkMatrix matrix;
179 meas.getMatrix(distance, &matrix);
180 dst->addPath(fPath, matrix);
reed@google.comd7a6fb92011-08-12 14:04:15 +0000181 } break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 case kMorph_Style:
183 morphpath(dst, fPath, meas, distance);
184 break;
185 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000186 SkDEBUGFAIL("unknown Style enum");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 break;
188 }
189 return fAdvance;
190}
191
reed@google.come28b9172011-08-09 18:14:31 +0000192///////////////////////////////////////////////////////////////////////////////
193
caryclark@google.comd26147a2011-12-15 14:16:43 +0000194SK_DEFINE_FLATTENABLE_REGISTRAR(SkPath1DPathEffect)
reed@google.come28b9172011-08-09 18:14:31 +0000195