blob: a176cf87e660ade5202aa1de1e2d42b600780ed2 [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"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000011#include "SkFlattenableBuffers.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkPathMeasure.h"
13
reed@google.comfd4be262012-05-25 01:04:12 +000014bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000015 SkPathMeasure meas(src, false);
16 do {
17 SkScalar length = meas.getLength();
18 SkScalar distance = this->begin(length);
reed@google.comd7a6fb92011-08-12 14:04:15 +000019 while (distance < length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 SkScalar delta = this->next(dst, distance, meas);
reed@google.comd7a6fb92011-08-12 14:04:15 +000021 if (delta <= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 break;
reed@google.comd7a6fb92011-08-12 14:04:15 +000023 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 distance += delta;
25 }
26 } while (meas.nextContour());
27 return true;
28}
29
reed@google.comd7a6fb92011-08-12 14:04:15 +000030///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000031
32SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance,
33 SkScalar phase, Style style) : fPath(path)
34{
reed@google.comd7a6fb92011-08-12 14:04:15 +000035 if (advance <= 0 || path.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
37 fAdvance = 0; // signals we can't draw anything
vandebo@chromium.orga728e352012-03-28 20:29:38 +000038 fInitialOffset = 0;
39 fStyle = kStyleCount;
reed@google.comd7a6fb92011-08-12 14:04:15 +000040 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 // cleanup their phase parameter, inverting it so that it becomes an
42 // offset along the path (to match the interpretation in PostScript)
reed@google.comd7a6fb92011-08-12 14:04:15 +000043 if (phase < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 phase = -phase;
reed@google.comd7a6fb92011-08-12 14:04:15 +000045 if (phase > advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 phase = SkScalarMod(phase, advance);
reed@google.comd7a6fb92011-08-12 14:04:15 +000047 }
48 } else {
49 if (phase > advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 phase = SkScalarMod(phase, advance);
reed@google.comd7a6fb92011-08-12 14:04:15 +000051 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 phase = advance - phase;
53 }
54 // now catch the edge case where phase == advance (within epsilon)
reed@google.comd7a6fb92011-08-12 14:04:15 +000055 if (phase >= advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 phase = 0;
reed@google.comd7a6fb92011-08-12 14:04:15 +000057 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 SkASSERT(phase >= 0);
59
60 fAdvance = advance;
61 fInitialOffset = phase;
62
63 if ((unsigned)style >= kStyleCount) {
64 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
65 }
66 fStyle = style;
67 }
68}
69
reed@google.comd7a6fb92011-08-12 14:04:15 +000070bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.comfd4be262012-05-25 01:04:12 +000071 SkStrokeRec* rec) {
reed@google.comd7a6fb92011-08-12 14:04:15 +000072 if (fAdvance > 0) {
reed@google.comfd4be262012-05-25 01:04:12 +000073 rec->setFillStyle();
74 return this->INHERITED::filterPath(dst, src, rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 }
76 return false;
77}
78
reed@google.comf3edf9f2012-04-12 19:44:38 +000079static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
reed@google.comd7a6fb92011-08-12 14:04:15 +000080 SkPathMeasure& meas, SkScalar dist) {
81 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 SkPoint pos;
83 SkVector tangent;
84
85 SkScalar sx = src[i].fX;
86 SkScalar sy = src[i].fY;
87
reed@google.comf3edf9f2012-04-12 19:44:38 +000088 if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
89 return false;
90 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000091
92 SkMatrix matrix;
93 SkPoint pt;
94
95 pt.set(sx, sy);
96 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
97 matrix.preTranslate(-sx, 0);
98 matrix.postTranslate(pos.fX, pos.fY);
99 matrix.mapPoints(&dst[i], &pt, 1);
100 }
reed@google.comf3edf9f2012-04-12 19:44:38 +0000101 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102}
103
104/* TODO
105
106Need differentially more subdivisions when the follow-path is curvy. Not sure how to
107determine that, but we need it. I guess a cheap answer is let the caller tell us,
108but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
109*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000110static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
111 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 SkPath::Iter iter(src, false);
113 SkPoint srcP[4], dstP[3];
114 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000115
116 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 switch (verb) {
118 case SkPath::kMove_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000119 if (morphpoints(dstP, srcP, 1, meas, dist)) {
120 dst->moveTo(dstP[0]);
121 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 break;
123 case SkPath::kLine_Verb:
124 srcP[2] = srcP[1];
125 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
126 SkScalarAve(srcP[0].fY, srcP[2].fY));
127 // fall through to quad
128 case SkPath::kQuad_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000129 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
130 dst->quadTo(dstP[0], dstP[1]);
131 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 break;
133 case SkPath::kCubic_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000134 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
135 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
136 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 break;
138 case SkPath::kClose_Verb:
139 dst->close();
140 break;
141 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000142 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 break;
144 }
145 }
146}
147
reed@google.comd7a6fb92011-08-12 14:04:15 +0000148SkPath1DPathEffect::SkPath1DPathEffect(SkFlattenableReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 fAdvance = buffer.readScalar();
150 if (fAdvance > 0) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000151 buffer.readPath(&fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 fInitialOffset = buffer.readScalar();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000153 fStyle = (Style) buffer.readUInt();
vandebo@chromium.orga728e352012-03-28 20:29:38 +0000154 } else {
155 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
156 // Make Coverity happy.
157 fInitialOffset = 0;
158 fStyle = kStyleCount;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 }
160}
161
reed@google.comd7a6fb92011-08-12 14:04:15 +0000162SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 return fInitialOffset;
164}
165
djsollen@google.com54924242012-03-29 15:18:04 +0000166void SkPath1DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
167 this->INHERITED::flatten(buffer);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 buffer.writeScalar(fAdvance);
169 if (fAdvance > 0) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000170 buffer.writePath(fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 buffer.writeScalar(fInitialOffset);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000172 buffer.writeUInt(fStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 }
174}
175
reed@google.comd7a6fb92011-08-12 14:04:15 +0000176SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
177 SkPathMeasure& meas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 switch (fStyle) {
reed@google.comd7a6fb92011-08-12 14:04:15 +0000179 case kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 SkPoint pos;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000181 if (meas.getPosTan(distance, &pos, NULL)) {
182 dst->addPath(fPath, pos.fX, pos.fY);
183 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000184 } break;
185 case kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 SkMatrix matrix;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000187 if (meas.getMatrix(distance, &matrix)) {
188 dst->addPath(fPath, matrix);
189 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000190 } break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 case kMorph_Style:
192 morphpath(dst, fPath, meas, distance);
193 break;
194 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000195 SkDEBUGFAIL("unknown Style enum");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 break;
197 }
198 return fAdvance;
199}
200
reed@google.come28b9172011-08-09 18:14:31 +0000201///////////////////////////////////////////////////////////////////////////////
202
caryclark@google.comd26147a2011-12-15 14:16:43 +0000203SK_DEFINE_FLATTENABLE_REGISTRAR(SkPath1DPathEffect)
reed@google.come28b9172011-08-09 18:14:31 +0000204