blob: 37cd13052e477ca5d68e0c4ab5d1728d57720030 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008
9#include "Sk1DPathEffect.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkPathMeasure.h"
halcanary435657f2015-09-15 12:53:07 -070013#include "SkStrokeRec.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
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{
reedca726ab2016-02-22 12:50:25 -080037 SkASSERT(advance > 0 && !path.isEmpty());
38 // cleanup their phase parameter, inverting it so that it becomes an
39 // offset along the path (to match the interpretation in PostScript)
40 if (phase < 0) {
41 phase = -phase;
42 if (phase > advance) {
43 phase = SkScalarMod(phase, advance);
44 }
reed@google.comd7a6fb92011-08-12 14:04:15 +000045 } else {
reedca726ab2016-02-22 12:50:25 -080046 if (phase > advance) {
47 phase = SkScalarMod(phase, advance);
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 }
reedca726ab2016-02-22 12:50:25 -080049 phase = advance - phase;
reed653db512016-02-22 06:34:47 -080050 }
reedca726ab2016-02-22 12:50:25 -080051 // now catch the edge case where phase == advance (within epsilon)
52 if (phase >= advance) {
53 phase = 0;
54 }
55 SkASSERT(phase >= 0);
56
57 fAdvance = advance;
58 fInitialOffset = phase;
59
60 if ((unsigned)style > kMorph_Style) {
61 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
62 }
63 fStyle = style;
reed@android.com8a1c16f2008-12-17 15:59:43 +000064}
65
reed@google.comd7a6fb92011-08-12 14:04:15 +000066bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000067 SkStrokeRec* rec, const SkRect* cullRect) const {
reed@google.comd7a6fb92011-08-12 14:04:15 +000068 if (fAdvance > 0) {
reed@google.comfd4be262012-05-25 01:04:12 +000069 rec->setFillStyle();
reed@google.com4bbdeac2013-01-24 21:03:11 +000070 return this->INHERITED::filterPath(dst, src, rec, cullRect);
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 }
72 return false;
73}
74
reed@google.comf3edf9f2012-04-12 19:44:38 +000075static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
reed@google.comd7a6fb92011-08-12 14:04:15 +000076 SkPathMeasure& meas, SkScalar dist) {
77 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 SkPoint pos;
79 SkVector tangent;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000080
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkScalar sx = src[i].fX;
82 SkScalar sy = src[i].fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000083
reed@google.comf3edf9f2012-04-12 19:44:38 +000084 if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
85 return false;
86 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 SkMatrix matrix;
89 SkPoint pt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 pt.set(sx, sy);
92 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
93 matrix.preTranslate(-sx, 0);
94 matrix.postTranslate(pos.fX, pos.fY);
95 matrix.mapPoints(&dst[i], &pt, 1);
96 }
reed@google.comf3edf9f2012-04-12 19:44:38 +000097 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +000098}
99
100/* TODO
101
102Need differentially more subdivisions when the follow-path is curvy. Not sure how to
103determine that, but we need it. I guess a cheap answer is let the caller tell us,
104but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
105*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000106static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
107 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 SkPath::Iter iter(src, false);
109 SkPoint srcP[4], dstP[3];
110 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000111
112 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 switch (verb) {
114 case SkPath::kMove_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000115 if (morphpoints(dstP, srcP, 1, meas, dist)) {
116 dst->moveTo(dstP[0]);
117 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 break;
119 case SkPath::kLine_Verb:
120 srcP[2] = srcP[1];
121 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
122 SkScalarAve(srcP[0].fY, srcP[2].fY));
123 // fall through to quad
124 case SkPath::kQuad_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000125 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
126 dst->quadTo(dstP[0], dstP[1]);
127 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 break;
129 case SkPath::kCubic_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000130 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
131 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
132 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 break;
134 case SkPath::kClose_Verb:
135 dst->close();
136 break;
137 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000138 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 break;
140 }
141 }
142}
143
reed@google.com548a1f32012-12-18 16:12:09 +0000144SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 return fInitialOffset;
146}
147
reed60c9b582016-04-03 09:11:13 -0700148sk_sp<SkFlattenable> SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700149 SkScalar advance = buffer.readScalar();
150 if (advance > 0) {
151 SkPath path;
152 buffer.readPath(&path);
153 SkScalar phase = buffer.readScalar();
154 Style style = (Style)buffer.readUInt();
reed60c9b582016-04-03 09:11:13 -0700155 return SkPath1DPathEffect::Make(path, advance, phase, style);
reed9fa60da2014-08-21 07:59:51 -0700156 }
halcanary96fcdcc2015-08-27 07:41:13 -0700157 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700158}
159
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000160void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 buffer.writeScalar(fAdvance);
162 if (fAdvance > 0) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000163 buffer.writePath(fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 buffer.writeScalar(fInitialOffset);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000165 buffer.writeUInt(fStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 }
167}
168
reed@google.comd7a6fb92011-08-12 14:04:15 +0000169SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
reed@google.com548a1f32012-12-18 16:12:09 +0000170 SkPathMeasure& meas) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 switch (fStyle) {
reed@google.comd7a6fb92011-08-12 14:04:15 +0000172 case kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 SkPoint pos;
halcanary96fcdcc2015-08-27 07:41:13 -0700174 if (meas.getPosTan(distance, &pos, nullptr)) {
reed@google.comf3edf9f2012-04-12 19:44:38 +0000175 dst->addPath(fPath, pos.fX, pos.fY);
176 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000177 } break;
178 case kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 SkMatrix matrix;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000180 if (meas.getMatrix(distance, &matrix)) {
181 dst->addPath(fPath, matrix);
182 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000183 } break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 case kMorph_Style:
185 morphpath(dst, fPath, meas, distance);
186 break;
187 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000188 SkDEBUGFAIL("unknown Style enum");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 break;
190 }
191 return fAdvance;
192}
robertphillips42dbfa82015-01-26 06:08:52 -0800193
194
195#ifndef SK_IGNORE_TO_STRING
196void SkPath1DPathEffect::toString(SkString* str) const {
197 str->appendf("SkPath1DPathEffect: (");
198 // TODO: add path and style
199 str->appendf("advance: %.2f phase %.2f", fAdvance, fInitialOffset);
200 str->appendf(")");
201}
202#endif
reedca726ab2016-02-22 12:50:25 -0800203
204///////////////////////////////////////////////////////////////////////////////////////////////////
205
reeda4393342016-03-18 11:22:57 -0700206sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase,
207 Style style) {
Robert Phillips6a307cc2017-02-14 10:03:33 -0500208 if (advance <= 0 || !SkScalarIsFinite(advance) ||
209 !SkScalarIsFinite(phase) || path.isEmpty()) {
reedca726ab2016-02-22 12:50:25 -0800210 return nullptr;
211 }
reeda4393342016-03-18 11:22:57 -0700212 return sk_sp<SkPathEffect>(new SkPath1DPathEffect(path, advance, phase, style));
reedca726ab2016-02-22 12:50:25 -0800213}