blob: 054f1d5cb7b840e7c3a3388651bd64b5057238aa [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
reed@google.comd7a6fb92011-08-12 14:04:15 +000037 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 // cleanup their phase parameter, inverting it so that it becomes an
39 // offset along the path (to match the interpretation in PostScript)
reed@google.comd7a6fb92011-08-12 14:04:15 +000040 if (phase < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 phase = -phase;
reed@google.comd7a6fb92011-08-12 14:04:15 +000042 if (phase > advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 phase = SkScalarMod(phase, advance);
reed@google.comd7a6fb92011-08-12 14:04:15 +000044 }
45 } else {
46 if (phase > advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 phase = SkScalarMod(phase, advance);
reed@google.comd7a6fb92011-08-12 14:04:15 +000048 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 phase = advance - phase;
50 }
51 // now catch the edge case where phase == advance (within epsilon)
reed@google.comd7a6fb92011-08-12 14:04:15 +000052 if (phase >= advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 phase = 0;
reed@google.comd7a6fb92011-08-12 14:04:15 +000054 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 SkASSERT(phase >= 0);
56
57 fAdvance = advance;
58 fInitialOffset = phase;
59
60 if ((unsigned)style >= kStyleCount) {
61 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
62 }
63 fStyle = style;
64 }
65}
66
reed@google.comd7a6fb92011-08-12 14:04:15 +000067bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
68 SkScalar* width) {
69 if (fAdvance > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 *width = -1;
71 return this->INHERITED::filterPath(dst, src, width);
72 }
73 return false;
74}
75
76static void morphpoints(SkPoint dst[], const SkPoint src[], int count,
reed@google.comd7a6fb92011-08-12 14:04:15 +000077 SkPathMeasure& meas, SkScalar dist) {
78 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 SkPoint pos;
80 SkVector tangent;
81
82 SkScalar sx = src[i].fX;
83 SkScalar sy = src[i].fY;
84
85 meas.getPosTan(dist + sx, &pos, &tangent);
86
87 SkMatrix matrix;
88 SkPoint pt;
89
90 pt.set(sx, sy);
91 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
92 matrix.preTranslate(-sx, 0);
93 matrix.postTranslate(pos.fX, pos.fY);
94 matrix.mapPoints(&dst[i], &pt, 1);
95 }
96}
97
98/* TODO
99
100Need differentially more subdivisions when the follow-path is curvy. Not sure how to
101determine that, but we need it. I guess a cheap answer is let the caller tell us,
102but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
103*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000104static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
105 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 SkPath::Iter iter(src, false);
107 SkPoint srcP[4], dstP[3];
108 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000109
110 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 switch (verb) {
112 case SkPath::kMove_Verb:
113 morphpoints(dstP, srcP, 1, meas, dist);
114 dst->moveTo(dstP[0]);
115 break;
116 case SkPath::kLine_Verb:
117 srcP[2] = srcP[1];
118 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
119 SkScalarAve(srcP[0].fY, srcP[2].fY));
120 // fall through to quad
121 case SkPath::kQuad_Verb:
122 morphpoints(dstP, &srcP[1], 2, meas, dist);
123 dst->quadTo(dstP[0], dstP[1]);
124 break;
125 case SkPath::kCubic_Verb:
126 morphpoints(dstP, &srcP[1], 3, meas, dist);
127 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
128 break;
129 case SkPath::kClose_Verb:
130 dst->close();
131 break;
132 default:
133 SkASSERT(!"unknown verb");
134 break;
135 }
136 }
137}
138
reed@google.comd7a6fb92011-08-12 14:04:15 +0000139SkPath1DPathEffect::SkPath1DPathEffect(SkFlattenableReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 fAdvance = buffer.readScalar();
141 if (fAdvance > 0) {
142 fPath.unflatten(buffer);
143 fInitialOffset = buffer.readScalar();
144 fStyle = (Style) buffer.readU8();
145 }
146}
147
reed@google.comd7a6fb92011-08-12 14:04:15 +0000148SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 return fInitialOffset;
150}
151
reed@google.comd7a6fb92011-08-12 14:04:15 +0000152void SkPath1DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 buffer.writeScalar(fAdvance);
154 if (fAdvance > 0) {
155 fPath.flatten(buffer);
156 buffer.writeScalar(fInitialOffset);
157 buffer.write8(fStyle);
158 }
159}
160
reed@google.comd7a6fb92011-08-12 14:04:15 +0000161SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
162 SkPathMeasure& meas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 switch (fStyle) {
reed@google.comd7a6fb92011-08-12 14:04:15 +0000164 case kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 SkPoint pos;
166 meas.getPosTan(distance, &pos, NULL);
167 dst->addPath(fPath, pos.fX, pos.fY);
reed@google.comd7a6fb92011-08-12 14:04:15 +0000168 } break;
169 case kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 SkMatrix matrix;
171 meas.getMatrix(distance, &matrix);
172 dst->addPath(fPath, matrix);
reed@google.comd7a6fb92011-08-12 14:04:15 +0000173 } break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 case kMorph_Style:
175 morphpath(dst, fPath, meas, distance);
176 break;
177 default:
178 SkASSERT(!"unknown Style enum");
179 break;
180 }
181 return fAdvance;
182}
183
reed@google.come28b9172011-08-09 18:14:31 +0000184///////////////////////////////////////////////////////////////////////////////
185
186static SkFlattenable::Registrar gReg("SkPath1DPathEffect",
187 SkPath1DPathEffect::CreateProc);
188