blob: 7354cdacb45b4f344fa13d3b47dee035cf12b844 [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"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
12#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkPathMeasure.h"
14
reed@google.com548a1f32012-12-18 16:12:09 +000015bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000016 SkStrokeRec*, const SkRect*) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000017 SkPathMeasure meas(src, false);
18 do {
19 SkScalar length = meas.getLength();
20 SkScalar distance = this->begin(length);
reed@google.comd7a6fb92011-08-12 14:04:15 +000021 while (distance < length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 SkScalar delta = this->next(dst, distance, meas);
reed@google.comd7a6fb92011-08-12 14:04:15 +000023 if (delta <= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 break;
reed@google.comd7a6fb92011-08-12 14:04:15 +000025 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 distance += delta;
27 }
28 } while (meas.nextContour());
29 return true;
30}
31
reed@google.comd7a6fb92011-08-12 14:04:15 +000032///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000033
rmistry@google.comfbfcd562012-08-23 18:09:54 +000034SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance,
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 SkScalar phase, Style style) : fPath(path)
36{
reed@google.comd7a6fb92011-08-12 14:04:15 +000037 if (advance <= 0 || path.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
39 fAdvance = 0; // signals we can't draw anything
vandebo@chromium.orga728e352012-03-28 20:29:38 +000040 fInitialOffset = 0;
41 fStyle = kStyleCount;
reed@google.comd7a6fb92011-08-12 14:04:15 +000042 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 // cleanup their phase parameter, inverting it so that it becomes an
44 // offset along the path (to match the interpretation in PostScript)
reed@google.comd7a6fb92011-08-12 14:04:15 +000045 if (phase < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 phase = -phase;
reed@google.comd7a6fb92011-08-12 14:04:15 +000047 if (phase > advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 phase = SkScalarMod(phase, advance);
reed@google.comd7a6fb92011-08-12 14:04:15 +000049 }
50 } else {
51 if (phase > advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 phase = SkScalarMod(phase, advance);
reed@google.comd7a6fb92011-08-12 14:04:15 +000053 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 phase = advance - phase;
55 }
56 // now catch the edge case where phase == advance (within epsilon)
reed@google.comd7a6fb92011-08-12 14:04:15 +000057 if (phase >= advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 phase = 0;
reed@google.comd7a6fb92011-08-12 14:04:15 +000059 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 SkASSERT(phase >= 0);
61
62 fAdvance = advance;
63 fInitialOffset = phase;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 if ((unsigned)style >= kStyleCount) {
66 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
67 }
68 fStyle = style;
69 }
70}
71
reed@google.comd7a6fb92011-08-12 14:04:15 +000072bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000073 SkStrokeRec* rec, const SkRect* cullRect) const {
reed@google.comd7a6fb92011-08-12 14:04:15 +000074 if (fAdvance > 0) {
reed@google.comfd4be262012-05-25 01:04:12 +000075 rec->setFillStyle();
reed@google.com4bbdeac2013-01-24 21:03:11 +000076 return this->INHERITED::filterPath(dst, src, rec, cullRect);
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 }
78 return false;
79}
80
reed@google.comf3edf9f2012-04-12 19:44:38 +000081static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
reed@google.comd7a6fb92011-08-12 14:04:15 +000082 SkPathMeasure& meas, SkScalar dist) {
83 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 SkPoint pos;
85 SkVector tangent;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 SkScalar sx = src[i].fX;
88 SkScalar sy = src[i].fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000089
reed@google.comf3edf9f2012-04-12 19:44:38 +000090 if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
91 return false;
92 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 SkMatrix matrix;
95 SkPoint pt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 pt.set(sx, sy);
98 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
99 matrix.preTranslate(-sx, 0);
100 matrix.postTranslate(pos.fX, pos.fY);
101 matrix.mapPoints(&dst[i], &pt, 1);
102 }
reed@google.comf3edf9f2012-04-12 19:44:38 +0000103 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104}
105
106/* TODO
107
108Need differentially more subdivisions when the follow-path is curvy. Not sure how to
109determine that, but we need it. I guess a cheap answer is let the caller tell us,
110but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
111*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000112static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
113 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 SkPath::Iter iter(src, false);
115 SkPoint srcP[4], dstP[3];
116 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000117
118 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 switch (verb) {
120 case SkPath::kMove_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000121 if (morphpoints(dstP, srcP, 1, meas, dist)) {
122 dst->moveTo(dstP[0]);
123 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 break;
125 case SkPath::kLine_Verb:
126 srcP[2] = srcP[1];
127 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
128 SkScalarAve(srcP[0].fY, srcP[2].fY));
129 // fall through to quad
130 case SkPath::kQuad_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000131 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
132 dst->quadTo(dstP[0], dstP[1]);
133 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 break;
135 case SkPath::kCubic_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000136 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
137 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
138 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 break;
140 case SkPath::kClose_Verb:
141 dst->close();
142 break;
143 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000144 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 break;
146 }
147 }
148}
149
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000150SkPath1DPathEffect::SkPath1DPathEffect(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 fAdvance = buffer.readScalar();
152 if (fAdvance > 0) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000153 buffer.readPath(&fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 fInitialOffset = buffer.readScalar();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000155 fStyle = (Style) buffer.readUInt();
vandebo@chromium.orga728e352012-03-28 20:29:38 +0000156 } else {
157 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
158 // Make Coverity happy.
159 fInitialOffset = 0;
160 fStyle = kStyleCount;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 }
162}
163
reed@google.com548a1f32012-12-18 16:12:09 +0000164SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 return fInitialOffset;
166}
167
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000168void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
djsollen@google.com54924242012-03-29 15:18:04 +0000169 this->INHERITED::flatten(buffer);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 buffer.writeScalar(fAdvance);
171 if (fAdvance > 0) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000172 buffer.writePath(fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 buffer.writeScalar(fInitialOffset);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000174 buffer.writeUInt(fStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 }
176}
177
reed@google.comd7a6fb92011-08-12 14:04:15 +0000178SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
reed@google.com548a1f32012-12-18 16:12:09 +0000179 SkPathMeasure& meas) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 switch (fStyle) {
reed@google.comd7a6fb92011-08-12 14:04:15 +0000181 case kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 SkPoint pos;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000183 if (meas.getPosTan(distance, &pos, NULL)) {
184 dst->addPath(fPath, pos.fX, pos.fY);
185 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000186 } break;
187 case kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 SkMatrix matrix;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000189 if (meas.getMatrix(distance, &matrix)) {
190 dst->addPath(fPath, matrix);
191 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000192 } break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 case kMorph_Style:
194 morphpath(dst, fPath, meas, distance);
195 break;
196 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000197 SkDEBUGFAIL("unknown Style enum");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 break;
199 }
200 return fAdvance;
201}