blob: 4be6f975d3bfa792e7cec3fbe35a25c566699850 [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{
reedca726ab2016-02-22 12:50:25 -080038 SkASSERT(advance > 0 && !path.isEmpty());
39 // cleanup their phase parameter, inverting it so that it becomes an
40 // offset along the path (to match the interpretation in PostScript)
41 if (phase < 0) {
42 phase = -phase;
43 if (phase > advance) {
44 phase = SkScalarMod(phase, advance);
45 }
reed@google.comd7a6fb92011-08-12 14:04:15 +000046 } else {
reedca726ab2016-02-22 12:50:25 -080047 if (phase > advance) {
48 phase = SkScalarMod(phase, advance);
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 }
reedca726ab2016-02-22 12:50:25 -080050 phase = advance - phase;
reed653db512016-02-22 06:34:47 -080051 }
reedca726ab2016-02-22 12:50:25 -080052 // now catch the edge case where phase == advance (within epsilon)
53 if (phase >= advance) {
54 phase = 0;
55 }
56 SkASSERT(phase >= 0);
57
58 fAdvance = advance;
59 fInitialOffset = phase;
60
61 if ((unsigned)style > kMorph_Style) {
62 SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
63 }
64 fStyle = style;
reed@android.com8a1c16f2008-12-17 15:59:43 +000065}
66
reed@google.comd7a6fb92011-08-12 14:04:15 +000067bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000068 SkStrokeRec* rec, const SkRect* cullRect) const {
reed@google.comd7a6fb92011-08-12 14:04:15 +000069 if (fAdvance > 0) {
reed@google.comfd4be262012-05-25 01:04:12 +000070 rec->setFillStyle();
reed@google.com4bbdeac2013-01-24 21:03:11 +000071 return this->INHERITED::filterPath(dst, src, rec, cullRect);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 }
73 return false;
74}
75
reed@google.comf3edf9f2012-04-12 19:44:38 +000076static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
reed@google.comd7a6fb92011-08-12 14:04:15 +000077 SkPathMeasure& meas, SkScalar dist) {
78 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 SkPoint pos;
80 SkVector tangent;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000081
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 SkScalar sx = src[i].fX;
83 SkScalar sy = src[i].fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000084
reed@google.comf3edf9f2012-04-12 19:44:38 +000085 if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
86 return false;
87 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 SkMatrix matrix;
90 SkPoint pt;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000091
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 pt.set(sx, sy);
93 matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
94 matrix.preTranslate(-sx, 0);
95 matrix.postTranslate(pos.fX, pos.fY);
96 matrix.mapPoints(&dst[i], &pt, 1);
97 }
reed@google.comf3edf9f2012-04-12 19:44:38 +000098 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +000099}
100
101/* TODO
102
103Need differentially more subdivisions when the follow-path is curvy. Not sure how to
104determine that, but we need it. I guess a cheap answer is let the caller tell us,
105but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
106*/
reed@google.comd7a6fb92011-08-12 14:04:15 +0000107static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
108 SkScalar dist) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 SkPath::Iter iter(src, false);
110 SkPoint srcP[4], dstP[3];
111 SkPath::Verb verb;
reed@google.comd7a6fb92011-08-12 14:04:15 +0000112
113 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 switch (verb) {
115 case SkPath::kMove_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000116 if (morphpoints(dstP, srcP, 1, meas, dist)) {
117 dst->moveTo(dstP[0]);
118 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 break;
120 case SkPath::kLine_Verb:
121 srcP[2] = srcP[1];
122 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
123 SkScalarAve(srcP[0].fY, srcP[2].fY));
124 // fall through to quad
125 case SkPath::kQuad_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000126 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
127 dst->quadTo(dstP[0], dstP[1]);
128 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 break;
130 case SkPath::kCubic_Verb:
reed@google.comf3edf9f2012-04-12 19:44:38 +0000131 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
132 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
133 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 break;
135 case SkPath::kClose_Verb:
136 dst->close();
137 break;
138 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000139 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 break;
141 }
142 }
143}
144
reed@google.com548a1f32012-12-18 16:12:09 +0000145SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 return fInitialOffset;
147}
148
reed9fa60da2014-08-21 07:59:51 -0700149SkFlattenable* SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
150 SkScalar advance = buffer.readScalar();
151 if (advance > 0) {
152 SkPath path;
153 buffer.readPath(&path);
154 SkScalar phase = buffer.readScalar();
155 Style style = (Style)buffer.readUInt();
156 return SkPath1DPathEffect::Create(path, advance, phase, style);
157 }
halcanary96fcdcc2015-08-27 07:41:13 -0700158 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700159}
160
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000161void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 buffer.writeScalar(fAdvance);
163 if (fAdvance > 0) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000164 buffer.writePath(fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 buffer.writeScalar(fInitialOffset);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000166 buffer.writeUInt(fStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 }
168}
169
reed@google.comd7a6fb92011-08-12 14:04:15 +0000170SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
reed@google.com548a1f32012-12-18 16:12:09 +0000171 SkPathMeasure& meas) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 switch (fStyle) {
reed@google.comd7a6fb92011-08-12 14:04:15 +0000173 case kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 SkPoint pos;
halcanary96fcdcc2015-08-27 07:41:13 -0700175 if (meas.getPosTan(distance, &pos, nullptr)) {
reed@google.comf3edf9f2012-04-12 19:44:38 +0000176 dst->addPath(fPath, pos.fX, pos.fY);
177 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000178 } break;
179 case kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 SkMatrix matrix;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000181 if (meas.getMatrix(distance, &matrix)) {
182 dst->addPath(fPath, matrix);
183 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000184 } break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 case kMorph_Style:
186 morphpath(dst, fPath, meas, distance);
187 break;
188 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000189 SkDEBUGFAIL("unknown Style enum");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 break;
191 }
192 return fAdvance;
193}
robertphillips42dbfa82015-01-26 06:08:52 -0800194
195
196#ifndef SK_IGNORE_TO_STRING
197void SkPath1DPathEffect::toString(SkString* str) const {
198 str->appendf("SkPath1DPathEffect: (");
199 // TODO: add path and style
200 str->appendf("advance: %.2f phase %.2f", fAdvance, fInitialOffset);
201 str->appendf(")");
202}
203#endif
reedca726ab2016-02-22 12:50:25 -0800204
205///////////////////////////////////////////////////////////////////////////////////////////////////
206
207SkPathEffect* SkPath1DPathEffect::Create(const SkPath& path, SkScalar advance, SkScalar phase,
208 Style style) {
209 if (advance <= 0 || path.isEmpty()) {
210 return nullptr;
211 }
212 return new SkPath1DPathEffect(path, advance, phase, style);
213}