blob: 10a68b9f840fc836d12054063c9ee56e2df5b037 [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.com548a1f32012-12-18 16:12:09 +000014bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000015 SkStrokeRec*, const SkRect*) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000016 SkPathMeasure meas(src, false);
17 do {
18 SkScalar length = meas.getLength();
19 SkScalar distance = this->begin(length);
reed@google.comd7a6fb92011-08-12 14:04:15 +000020 while (distance < length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021 SkScalar delta = this->next(dst, distance, meas);
reed@google.comd7a6fb92011-08-12 14:04:15 +000022 if (delta <= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 break;
reed@google.comd7a6fb92011-08-12 14:04:15 +000024 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 distance += delta;
26 }
27 } while (meas.nextContour());
28 return true;
29}
30
reed@google.comd7a6fb92011-08-12 14:04:15 +000031///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000032
rmistry@google.comfbfcd562012-08-23 18:09:54 +000033SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance,
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 SkScalar phase, Style style) : fPath(path)
35{
reed@google.comd7a6fb92011-08-12 14:04:15 +000036 if (advance <= 0 || path.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
38 fAdvance = 0; // signals we can't draw anything
vandebo@chromium.orga728e352012-03-28 20:29:38 +000039 fInitialOffset = 0;
40 fStyle = kStyleCount;
reed@google.comd7a6fb92011-08-12 14:04:15 +000041 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 // cleanup their phase parameter, inverting it so that it becomes an
43 // offset along the path (to match the interpretation in PostScript)
reed@google.comd7a6fb92011-08-12 14:04:15 +000044 if (phase < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 phase = -phase;
reed@google.comd7a6fb92011-08-12 14:04:15 +000046 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 }
49 } else {
50 if (phase > advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 phase = SkScalarMod(phase, advance);
reed@google.comd7a6fb92011-08-12 14:04:15 +000052 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 phase = advance - phase;
54 }
55 // now catch the edge case where phase == advance (within epsilon)
reed@google.comd7a6fb92011-08-12 14:04:15 +000056 if (phase >= advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 phase = 0;
reed@google.comd7a6fb92011-08-12 14:04:15 +000058 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 SkASSERT(phase >= 0);
60
61 fAdvance = advance;
62 fInitialOffset = phase;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000063
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 if ((unsigned)style >= kStyleCount) {
65 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
66 }
67 fStyle = style;
68 }
69}
70
reed@google.comd7a6fb92011-08-12 14:04:15 +000071bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000072 SkStrokeRec* rec, const SkRect* cullRect) const {
reed@google.comd7a6fb92011-08-12 14:04:15 +000073 if (fAdvance > 0) {
reed@google.comfd4be262012-05-25 01:04:12 +000074 rec->setFillStyle();
reed@google.com4bbdeac2013-01-24 21:03:11 +000075 return this->INHERITED::filterPath(dst, src, rec, cullRect);
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 }
77 return false;
78}
79
reed@google.comf3edf9f2012-04-12 19:44:38 +000080static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
reed@google.comd7a6fb92011-08-12 14:04:15 +000081 SkPathMeasure& meas, SkScalar dist) {
82 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 SkPoint pos;
84 SkVector tangent;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000085
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 SkScalar sx = src[i].fX;
87 SkScalar sy = src[i].fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000088
reed@google.comf3edf9f2012-04-12 19:44:38 +000089 if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
90 return false;
91 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 SkMatrix matrix;
94 SkPoint pt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 pt.set(sx, sy);
97 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
98 matrix.preTranslate(-sx, 0);
99 matrix.postTranslate(pos.fX, pos.fY);
100 matrix.mapPoints(&dst[i], &pt, 1);
101 }
reed@google.comf3edf9f2012-04-12 19:44:38 +0000102 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103}
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*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000111static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
112 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 SkPath::Iter iter(src, false);
114 SkPoint srcP[4], dstP[3];
115 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000116
117 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 switch (verb) {
119 case SkPath::kMove_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000120 if (morphpoints(dstP, srcP, 1, meas, dist)) {
121 dst->moveTo(dstP[0]);
122 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 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:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000130 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
131 dst->quadTo(dstP[0], dstP[1]);
132 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 break;
134 case SkPath::kCubic_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000135 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
136 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
137 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 break;
139 case SkPath::kClose_Verb:
140 dst->close();
141 break;
142 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000143 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 break;
145 }
146 }
147}
148
reed@google.comd7a6fb92011-08-12 14:04:15 +0000149SkPath1DPathEffect::SkPath1DPathEffect(SkFlattenableReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 fAdvance = buffer.readScalar();
151 if (fAdvance > 0) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000152 buffer.readPath(&fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 fInitialOffset = buffer.readScalar();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000154 fStyle = (Style) buffer.readUInt();
vandebo@chromium.orga728e352012-03-28 20:29:38 +0000155 } else {
156 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
157 // Make Coverity happy.
158 fInitialOffset = 0;
159 fStyle = kStyleCount;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 }
161}
162
reed@google.com548a1f32012-12-18 16:12:09 +0000163SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 return fInitialOffset;
165}
166
djsollen@google.com54924242012-03-29 15:18:04 +0000167void SkPath1DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
168 this->INHERITED::flatten(buffer);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 buffer.writeScalar(fAdvance);
170 if (fAdvance > 0) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000171 buffer.writePath(fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 buffer.writeScalar(fInitialOffset);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000173 buffer.writeUInt(fStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 }
175}
176
reed@google.comd7a6fb92011-08-12 14:04:15 +0000177SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
reed@google.com548a1f32012-12-18 16:12:09 +0000178 SkPathMeasure& meas) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 switch (fStyle) {
reed@google.comd7a6fb92011-08-12 14:04:15 +0000180 case kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 SkPoint pos;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000182 if (meas.getPosTan(distance, &pos, NULL)) {
183 dst->addPath(fPath, pos.fX, pos.fY);
184 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000185 } break;
186 case kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 SkMatrix matrix;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000188 if (meas.getMatrix(distance, &matrix)) {
189 dst->addPath(fPath, matrix);
190 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000191 } break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 case kMorph_Style:
193 morphpath(dst, fPath, meas, distance);
194 break;
195 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000196 SkDEBUGFAIL("unknown Style enum");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 break;
198 }
199 return fAdvance;
200}