blob: d630a8ed6ca9658ce9c163e4b81ee0ac8256e87c [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
reed@google.com548a1f32012-12-18 16:12:09 +0000150SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 return fInitialOffset;
152}
153
reed9fa60da2014-08-21 07:59:51 -0700154SkFlattenable* SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
155 SkScalar advance = buffer.readScalar();
156 if (advance > 0) {
157 SkPath path;
158 buffer.readPath(&path);
159 SkScalar phase = buffer.readScalar();
160 Style style = (Style)buffer.readUInt();
161 return SkPath1DPathEffect::Create(path, advance, phase, style);
162 }
halcanary96fcdcc2015-08-27 07:41:13 -0700163 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700164}
165
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000166void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 buffer.writeScalar(fAdvance);
168 if (fAdvance > 0) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000169 buffer.writePath(fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 buffer.writeScalar(fInitialOffset);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000171 buffer.writeUInt(fStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 }
173}
174
reed@google.comd7a6fb92011-08-12 14:04:15 +0000175SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
reed@google.com548a1f32012-12-18 16:12:09 +0000176 SkPathMeasure& meas) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 switch (fStyle) {
reed@google.comd7a6fb92011-08-12 14:04:15 +0000178 case kTranslate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 SkPoint pos;
halcanary96fcdcc2015-08-27 07:41:13 -0700180 if (meas.getPosTan(distance, &pos, nullptr)) {
reed@google.comf3edf9f2012-04-12 19:44:38 +0000181 dst->addPath(fPath, pos.fX, pos.fY);
182 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000183 } break;
184 case kRotate_Style: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 SkMatrix matrix;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000186 if (meas.getMatrix(distance, &matrix)) {
187 dst->addPath(fPath, matrix);
188 }
reed@google.comd7a6fb92011-08-12 14:04:15 +0000189 } break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 case kMorph_Style:
191 morphpath(dst, fPath, meas, distance);
192 break;
193 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000194 SkDEBUGFAIL("unknown Style enum");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 break;
196 }
197 return fAdvance;
198}
robertphillips42dbfa82015-01-26 06:08:52 -0800199
200
201#ifndef SK_IGNORE_TO_STRING
202void SkPath1DPathEffect::toString(SkString* str) const {
203 str->appendf("SkPath1DPathEffect: (");
204 // TODO: add path and style
205 str->appendf("advance: %.2f phase %.2f", fAdvance, fInitialOffset);
206 str->appendf(")");
207}
208#endif