blob: 041886e1db9f146f870a034d19b3e5deb4c2e4f4 [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"
halcanary435657f2015-09-15 12:53:07 -070014#include "SkStrokeRec.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
reed@google.com548a1f32012-12-18 16:12:09 +000016bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000017 SkStrokeRec*, const SkRect*) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000018 SkPathMeasure meas(src, false);
19 do {
20 SkScalar length = meas.getLength();
21 SkScalar distance = this->begin(length);
reed@google.comd7a6fb92011-08-12 14:04:15 +000022 while (distance < length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 SkScalar delta = this->next(dst, distance, meas);
reed@google.comd7a6fb92011-08-12 14:04:15 +000024 if (delta <= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 break;
reed@google.comd7a6fb92011-08-12 14:04:15 +000026 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 distance += delta;
28 }
29 } while (meas.nextContour());
30 return true;
31}
32
reed@google.comd7a6fb92011-08-12 14:04:15 +000033///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance,
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 SkScalar phase, Style style) : fPath(path)
37{
reed@google.comd7a6fb92011-08-12 14:04:15 +000038 if (advance <= 0 || path.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 SkDEBUGF(("SkPath1DPathEffect can't use advance <= 0\n"));
40 fAdvance = 0; // signals we can't draw anything
vandebo@chromium.orga728e352012-03-28 20:29:38 +000041 fInitialOffset = 0;
42 fStyle = kStyleCount;
reed@google.comd7a6fb92011-08-12 14:04:15 +000043 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 // cleanup their phase parameter, inverting it so that it becomes an
45 // offset along the path (to match the interpretation in PostScript)
reed@google.comd7a6fb92011-08-12 14:04:15 +000046 if (phase < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 phase = -phase;
reed@google.comd7a6fb92011-08-12 14:04:15 +000048 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 }
51 } else {
52 if (phase > advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 phase = SkScalarMod(phase, advance);
reed@google.comd7a6fb92011-08-12 14:04:15 +000054 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 phase = advance - phase;
56 }
57 // now catch the edge case where phase == advance (within epsilon)
reed@google.comd7a6fb92011-08-12 14:04:15 +000058 if (phase >= advance) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 phase = 0;
reed@google.comd7a6fb92011-08-12 14:04:15 +000060 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 SkASSERT(phase >= 0);
62
63 fAdvance = advance;
64 fInitialOffset = phase;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 if ((unsigned)style >= kStyleCount) {
67 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
68 }
69 fStyle = style;
70 }
71}
72
reed@google.comd7a6fb92011-08-12 14:04:15 +000073bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000074 SkStrokeRec* rec, const SkRect* cullRect) const {
reed@google.comd7a6fb92011-08-12 14:04:15 +000075 if (fAdvance > 0) {
reed@google.comfd4be262012-05-25 01:04:12 +000076 rec->setFillStyle();
reed@google.com4bbdeac2013-01-24 21:03:11 +000077 return this->INHERITED::filterPath(dst, src, rec, cullRect);
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 }
79 return false;
80}
81
reed@google.comf3edf9f2012-04-12 19:44:38 +000082static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
reed@google.comd7a6fb92011-08-12 14:04:15 +000083 SkPathMeasure& meas, SkScalar dist) {
84 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 SkPoint pos;
86 SkVector tangent;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 SkScalar sx = src[i].fX;
89 SkScalar sy = src[i].fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090
reed@google.comf3edf9f2012-04-12 19:44:38 +000091 if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
92 return false;
93 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 SkMatrix matrix;
96 SkPoint pt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 pt.set(sx, sy);
99 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
100 matrix.preTranslate(-sx, 0);
101 matrix.postTranslate(pos.fX, pos.fY);
102 matrix.mapPoints(&dst[i], &pt, 1);
103 }
reed@google.comf3edf9f2012-04-12 19:44:38 +0000104 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105}
106
107/* TODO
108
109Need differentially more subdivisions when the follow-path is curvy. Not sure how to
110determine that, but we need it. I guess a cheap answer is let the caller tell us,
111but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
112*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000113static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
114 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 SkPath::Iter iter(src, false);
116 SkPoint srcP[4], dstP[3];
117 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000118
119 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 switch (verb) {
121 case SkPath::kMove_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000122 if (morphpoints(dstP, srcP, 1, meas, dist)) {
123 dst->moveTo(dstP[0]);
124 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 break;
126 case SkPath::kLine_Verb:
127 srcP[2] = srcP[1];
128 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
129 SkScalarAve(srcP[0].fY, srcP[2].fY));
130 // fall through to quad
131 case SkPath::kQuad_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000132 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
133 dst->quadTo(dstP[0], dstP[1]);
134 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 break;
136 case SkPath::kCubic_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000137 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
138 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
139 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 break;
141 case SkPath::kClose_Verb:
142 dst->close();
143 break;
144 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000145 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 break;
147 }
148 }
149}
150
reed@google.com548a1f32012-12-18 16:12:09 +0000151SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 return fInitialOffset;
153}
154
reed9fa60da2014-08-21 07:59:51 -0700155SkFlattenable* SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
156 SkScalar advance = buffer.readScalar();
157 if (advance > 0) {
158 SkPath path;
159 buffer.readPath(&path);
160 SkScalar phase = buffer.readScalar();
161 Style style = (Style)buffer.readUInt();
162 return SkPath1DPathEffect::Create(path, advance, phase, style);
163 }
halcanary96fcdcc2015-08-27 07:41:13 -0700164 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700165}
166
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000167void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
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,
reed@google.com548a1f32012-12-18 16:12:09 +0000177 SkPathMeasure& meas) const {
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;
halcanary96fcdcc2015-08-27 07:41:13 -0700181 if (meas.getPosTan(distance, &pos, nullptr)) {
reed@google.comf3edf9f2012-04-12 19:44:38 +0000182 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}
robertphillips42dbfa82015-01-26 06:08:52 -0800200
201
202#ifndef SK_IGNORE_TO_STRING
203void SkPath1DPathEffect::toString(SkString* str) const {
204 str->appendf("SkPath1DPathEffect: (");
205 // TODO: add path and style
206 str->appendf("advance: %.2f phase %.2f", fAdvance, fInitialOffset);
207 str->appendf(")");
208}
209#endif