blob: 09e8d135b6dddf81f7d7fdfbfe1e5499b1e51ab7 [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.comd3521f12012-05-24 20:32:22 +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,
reed@google.comd3521f12012-05-24 20:32:22 +000070 SkScalar* width) {
reed@google.comd7a6fb92011-08-12 14:04:15 +000071 if (fAdvance > 0) {
reed@google.comd3521f12012-05-24 20:32:22 +000072 *width = -1;
73 return this->INHERITED::filterPath(dst, src, width);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 }
75 return false;
76}
77
reed@google.comf3edf9f2012-04-12 19:44:38 +000078static bool 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
reed@google.comf3edf9f2012-04-12 19:44:38 +000087 if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
88 return false;
89 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
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 }
reed@google.comf3edf9f2012-04-12 19:44:38 +0000100 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101}
102
103/* TODO
104
105Need differentially more subdivisions when the follow-path is curvy. Not sure how to
106determine that, but we need it. I guess a cheap answer is let the caller tell us,
107but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
108*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000109static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
110 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 SkPath::Iter iter(src, false);
112 SkPoint srcP[4], dstP[3];
113 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000114
115 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 switch (verb) {
117 case SkPath::kMove_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000118 if (morphpoints(dstP, srcP, 1, meas, dist)) {
119 dst->moveTo(dstP[0]);
120 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 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:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000128 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
129 dst->quadTo(dstP[0], dstP[1]);
130 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 break;
132 case SkPath::kCubic_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000133 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
134 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
135 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 break;
137 case SkPath::kClose_Verb:
138 dst->close();
139 break;
140 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000141 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 break;
143 }
144 }
145}
146
reed@google.comd7a6fb92011-08-12 14:04:15 +0000147SkPath1DPathEffect::SkPath1DPathEffect(SkFlattenableReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 fAdvance = buffer.readScalar();
149 if (fAdvance > 0) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000150 buffer.readPath(&fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 fInitialOffset = buffer.readScalar();
152 fStyle = (Style) buffer.readU8();
vandebo@chromium.orga728e352012-03-28 20:29:38 +0000153 } else {
154 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
155 // Make Coverity happy.
156 fInitialOffset = 0;
157 fStyle = kStyleCount;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 }
159}
160
reed@google.comd7a6fb92011-08-12 14:04:15 +0000161SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 return fInitialOffset;
163}
164
djsollen@google.com54924242012-03-29 15:18:04 +0000165void SkPath1DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
166 this->INHERITED::flatten(buffer);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 buffer.writeScalar(fAdvance);
168 if (fAdvance > 0) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000169 buffer.writePath(fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 buffer.writeScalar(fInitialOffset);
171 buffer.write8(fStyle);
172 }
173}
174
reed@google.comd7a6fb92011-08-12 14:04:15 +0000175SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
176 SkPathMeasure& meas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 switch (fStyle) {
reed@google.comd7a6fb92011-08-12 14:04:15 +0000178 case kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 SkPoint pos;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000180 if (meas.getPosTan(distance, &pos, NULL)) {
181 dst->addPath(fPath, pos.fX, pos.fY);
182 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000183 } break;
184 case kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 SkMatrix matrix;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000186 if (meas.getMatrix(distance, &matrix)) {
187 dst->addPath(fPath, matrix);
188 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000189 } break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 case kMorph_Style:
191 morphpath(dst, fPath, meas, distance);
192 break;
193 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000194 SkDEBUGFAIL("unknown Style enum");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 break;
196 }
197 return fAdvance;
198}
199
reed@google.come28b9172011-08-09 18:14:31 +0000200///////////////////////////////////////////////////////////////////////////////
201
caryclark@google.comd26147a2011-12-15 14:16:43 +0000202SK_DEFINE_FLATTENABLE_REGISTRAR(SkPath1DPathEffect)
reed@google.come28b9172011-08-09 18:14:31 +0000203