blob: 57f2d2b6cecf57d1b1a7b01fdba4d886ae12b6ae [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
Mike Reed22234652018-02-27 09:48:39 -050015// Since we are stepping by a float, the do/while loop might go on forever (or nearly so).
16// Put in a governor to limit crash values from looping too long (and allocating too much ram).
17#define MAX_REASONABLE_ITERATIONS 100000
18
reed@google.com548a1f32012-12-18 16:12:09 +000019bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000020 SkStrokeRec*, const SkRect*) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021 SkPathMeasure meas(src, false);
22 do {
Mike Reed22234652018-02-27 09:48:39 -050023 int governor = MAX_REASONABLE_ITERATIONS;
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 SkScalar length = meas.getLength();
25 SkScalar distance = this->begin(length);
Mike Reed22234652018-02-27 09:48:39 -050026 while (distance < length && --governor >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 SkScalar delta = this->next(dst, distance, meas);
reed@google.comd7a6fb92011-08-12 14:04:15 +000028 if (delta <= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 break;
reed@google.comd7a6fb92011-08-12 14:04:15 +000030 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 distance += delta;
32 }
33 } while (meas.nextContour());
34 return true;
35}
36
reed@google.comd7a6fb92011-08-12 14:04:15 +000037///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
Mike Reedfc015d22018-02-24 09:51:47 -050039SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance, SkScalar phase,
40 Style style) : fPath(path) {
reedca726ab2016-02-22 12:50:25 -080041 SkASSERT(advance > 0 && !path.isEmpty());
Mike Reedfc015d22018-02-24 09:51:47 -050042 SkASSERT((unsigned)style <= kMorph_Style);
43
reedca726ab2016-02-22 12:50:25 -080044 // cleanup their phase parameter, inverting it so that it becomes an
45 // offset along the path (to match the interpretation in PostScript)
46 if (phase < 0) {
47 phase = -phase;
48 if (phase > advance) {
49 phase = SkScalarMod(phase, advance);
50 }
reed@google.comd7a6fb92011-08-12 14:04:15 +000051 } else {
reedca726ab2016-02-22 12:50:25 -080052 if (phase > advance) {
53 phase = SkScalarMod(phase, advance);
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 }
reedca726ab2016-02-22 12:50:25 -080055 phase = advance - phase;
reed653db512016-02-22 06:34:47 -080056 }
reedca726ab2016-02-22 12:50:25 -080057 // now catch the edge case where phase == advance (within epsilon)
58 if (phase >= advance) {
59 phase = 0;
60 }
61 SkASSERT(phase >= 0);
62
63 fAdvance = advance;
64 fInitialOffset = phase;
65
66 if ((unsigned)style > kMorph_Style) {
67 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
68 }
69 fStyle = style;
reed@android.com8a1c16f2008-12-17 15:59:43 +000070}
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 {
Mike Reedfc015d22018-02-24 09:51:47 -050074 rec->setFillStyle();
75 return this->INHERITED::filterPath(dst, src, rec, cullRect);
reed@android.com8a1c16f2008-12-17 15:59:43 +000076}
77
reed@google.comf3edf9f2012-04-12 19:44:38 +000078static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
reed@google.comd7a6fb92011-08-12 14:04:15 +000079 SkPathMeasure& meas, SkScalar dist) {
80 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkPoint pos;
82 SkVector tangent;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000083
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 SkScalar sx = src[i].fX;
85 SkScalar sy = src[i].fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000086
reed@google.comf3edf9f2012-04-12 19:44:38 +000087 if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
88 return false;
89 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 SkMatrix matrix;
92 SkPoint pt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 pt.set(sx, sy);
95 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
96 matrix.preTranslate(-sx, 0);
97 matrix.postTranslate(pos.fX, pos.fY);
98 matrix.mapPoints(&dst[i], &pt, 1);
99 }
reed@google.comf3edf9f2012-04-12 19:44:38 +0000100 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101}
102
103/* TODO
104
105Need differentially more subdivisions when the follow-path is curvy. Not sure how to
106determine that, but we need it. I guess a cheap answer is let the caller tell us,
107but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
108*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000109static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
110 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 SkPath::Iter iter(src, false);
112 SkPoint srcP[4], dstP[3];
113 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000114
115 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 switch (verb) {
117 case SkPath::kMove_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000118 if (morphpoints(dstP, srcP, 1, meas, dist)) {
119 dst->moveTo(dstP[0]);
120 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 break;
122 case SkPath::kLine_Verb:
123 srcP[2] = srcP[1];
124 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
125 SkScalarAve(srcP[0].fY, srcP[2].fY));
126 // fall through to quad
127 case SkPath::kQuad_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000128 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
129 dst->quadTo(dstP[0], dstP[1]);
130 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 break;
Mike Reedb50e3852018-01-29 15:27:33 -0500132 case SkPath::kConic_Verb:
133 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
134 dst->conicTo(dstP[0], dstP[1], iter.conicWeight());
135 }
136 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 case SkPath::kCubic_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000138 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
139 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
140 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 break;
142 case SkPath::kClose_Verb:
143 dst->close();
144 break;
145 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000146 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 break;
148 }
149 }
150}
151
reed@google.com548a1f32012-12-18 16:12:09 +0000152SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 return fInitialOffset;
154}
155
reed60c9b582016-04-03 09:11:13 -0700156sk_sp<SkFlattenable> SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700157 SkScalar advance = buffer.readScalar();
Mike Reedfc015d22018-02-24 09:51:47 -0500158 SkPath path;
159 buffer.readPath(&path);
160 SkScalar phase = buffer.readScalar();
161 Style style = buffer.read32LE(kLastEnum_Style);
162 return buffer.isValid() ? SkPath1DPathEffect::Make(path, advance, phase, style) : nullptr;
reed9fa60da2014-08-21 07:59:51 -0700163}
164
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000165void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 buffer.writeScalar(fAdvance);
Mike Reedfc015d22018-02-24 09:51:47 -0500167 buffer.writePath(fPath);
168 buffer.writeScalar(fInitialOffset);
169 buffer.writeUInt(fStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170}
171
reed@google.comd7a6fb92011-08-12 14:04:15 +0000172SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
reed@google.com548a1f32012-12-18 16:12:09 +0000173 SkPathMeasure& meas) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 switch (fStyle) {
reed@google.comd7a6fb92011-08-12 14:04:15 +0000175 case kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 SkPoint pos;
halcanary96fcdcc2015-08-27 07:41:13 -0700177 if (meas.getPosTan(distance, &pos, nullptr)) {
reed@google.comf3edf9f2012-04-12 19:44:38 +0000178 dst->addPath(fPath, pos.fX, pos.fY);
179 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000180 } break;
181 case kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 SkMatrix matrix;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000183 if (meas.getMatrix(distance, &matrix)) {
184 dst->addPath(fPath, matrix);
185 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000186 } break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 case kMorph_Style:
188 morphpath(dst, fPath, meas, distance);
189 break;
190 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000191 SkDEBUGFAIL("unknown Style enum");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 break;
193 }
194 return fAdvance;
195}
robertphillips42dbfa82015-01-26 06:08:52 -0800196
Cary Clark32a49102018-05-20 23:15:43 +0000197
198void SkPath1DPathEffect::toString(SkString* str) const {
199 str->appendf("SkPath1DPathEffect: (");
200 // TODO: add path and style
201 str->appendf("advance: %.2f phase %.2f", fAdvance, fInitialOffset);
202 str->appendf(")");
203}
204
reedca726ab2016-02-22 12:50:25 -0800205///////////////////////////////////////////////////////////////////////////////////////////////////
206
reeda4393342016-03-18 11:22:57 -0700207sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase,
208 Style style) {
Mike Reedfc015d22018-02-24 09:51:47 -0500209 if (advance <= 0 || !SkScalarIsFinite(advance) || !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}