blob: 2838b1ff3e2aa4cce43c6bf5d984e303d05d7b41 [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#include "SkDashPathEffect.h"
egdaniela22ea182014-06-11 06:51:51 -07009
10#include "SkDashPathPriv.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
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000014SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count,
15 SkScalar phase) {
16 SkASSERT(intervals);
17 SkASSERT(count > 1 && SkAlign2(count) == count);
18
19 fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count);
20 fCount = count;
21 for (int i = 0; i < count; i++) {
22 SkASSERT(intervals[i] >= 0);
23 fIntervals[i] = intervals[i];
24 }
25
egdaniela22ea182014-06-11 06:51:51 -070026 // set the internal data members
27 SkDashPath::CalcDashParameters(phase, fIntervals, fCount, &fInitialDashLength,
28 &fInitialDashIndex, &fIntervalLength, &fPhase);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000029}
30
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000031SkDashPathEffect::~SkDashPathEffect() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 sk_free(fIntervals);
33}
34
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000035bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000036 SkStrokeRec* rec, const SkRect* cullRect) const {
egdaniela22ea182014-06-11 06:51:51 -070037 return SkDashPath::FilterDashPath(dst, src, rec, cullRect, fIntervals, fCount,
38 fInitialDashLength, fInitialDashIndex, fIntervalLength);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039}
40
robertphillips@google.com629ab542012-11-28 17:18:11 +000041// Currently asPoints is more restrictive then it needs to be. In the future
42// we need to:
43// allow kRound_Cap capping (could allow rotations in the matrix with this)
robertphillips@google.com6d875572012-12-17 18:56:29 +000044// allow paths to be returned
robertphillips@google.com629ab542012-11-28 17:18:11 +000045bool SkDashPathEffect::asPoints(PointData* results,
46 const SkPath& src,
47 const SkStrokeRec& rec,
reed@google.com4bbdeac2013-01-24 21:03:11 +000048 const SkMatrix& matrix,
49 const SkRect* cullRect) const {
robertphillips@google.com6d875572012-12-17 18:56:29 +000050 // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out
51 if (fInitialDashLength < 0 || 0 >= rec.getWidth()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +000052 return false;
53 }
54
robertphillips@google.com6d875572012-12-17 18:56:29 +000055 // TODO: this next test could be eased up. We could allow any number of
56 // intervals as long as all the ons match and all the offs match.
57 // Additionally, they do not necessarily need to be integers.
58 // We cannot allow arbitrary intervals since we want the returned points
59 // to be uniformly sized.
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000060 if (fCount != 2 ||
robertphillips@google.com6d875572012-12-17 18:56:29 +000061 !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) ||
62 !SkScalarIsInt(fIntervals[0]) ||
63 !SkScalarIsInt(fIntervals[1])) {
robertphillips@google.com629ab542012-11-28 17:18:11 +000064 return false;
65 }
66
robertphillips@google.com629ab542012-11-28 17:18:11 +000067 SkPoint pts[2];
68
robertphillips@google.com6d875572012-12-17 18:56:29 +000069 if (!src.isLine(pts)) {
robertphillips@google.com629ab542012-11-28 17:18:11 +000070 return false;
71 }
72
robertphillips@google.com6d875572012-12-17 18:56:29 +000073 // TODO: this test could be eased up to allow circles
robertphillips@google.com629ab542012-11-28 17:18:11 +000074 if (SkPaint::kButt_Cap != rec.getCap()) {
75 return false;
76 }
77
robertphillips@google.com6d875572012-12-17 18:56:29 +000078 // TODO: this test could be eased up for circles. Rotations could be allowed.
robertphillips@google.com629ab542012-11-28 17:18:11 +000079 if (!matrix.rectStaysRect()) {
80 return false;
81 }
82
robertphillips@google.com6d875572012-12-17 18:56:29 +000083 SkScalar length = SkPoint::Distance(pts[1], pts[0]);
robertphillips@google.com629ab542012-11-28 17:18:11 +000084
robertphillips@google.com6d875572012-12-17 18:56:29 +000085 SkVector tangent = pts[1] - pts[0];
86 if (tangent.isZero()) {
87 return false;
88 }
89
90 tangent.scale(SkScalarInvert(length));
91
92 // TODO: make this test for horizontal & vertical lines more robust
93 bool isXAxis = true;
94 if (SK_Scalar1 == tangent.fX || -SK_Scalar1 == tangent.fX) {
95 results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth()));
96 } else if (SK_Scalar1 == tangent.fY || -SK_Scalar1 == tangent.fY) {
97 results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0]));
98 isXAxis = false;
99 } else if (SkPaint::kRound_Cap != rec.getCap()) {
100 // Angled lines don't have axis-aligned boxes.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000101 return false;
102 }
103
104 if (NULL != results) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000105 results->fFlags = 0;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000106 SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000107
robertphillips@google.com6d875572012-12-17 18:56:29 +0000108 if (SkPaint::kRound_Cap == rec.getCap()) {
109 results->fFlags |= PointData::kCircles_PointFlag;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000110 }
111
robertphillips@google.com6d875572012-12-17 18:56:29 +0000112 results->fNumPoints = 0;
113 SkScalar len2 = length;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000114 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
115 SkASSERT(len2 >= clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000116 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000117 if (clampedInitialDashLength > 0) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000118 if (clampedInitialDashLength >= fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000119 ++results->fNumPoints; // partial first dash
120 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000121 len2 -= clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000122 }
123 len2 -= fIntervals[1]; // also skip first space
124 if (len2 < 0) {
125 len2 = 0;
126 }
127 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000128 len2 -= clampedInitialDashLength; // skip initial partial empty
robertphillips@google.com6d875572012-12-17 18:56:29 +0000129 }
130 }
131 int numMidPoints = SkScalarFloorToInt(SkScalarDiv(len2, fIntervalLength));
132 results->fNumPoints += numMidPoints;
133 len2 -= numMidPoints * fIntervalLength;
134 bool partialLast = false;
135 if (len2 > 0) {
136 if (len2 < fIntervals[0]) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000137 partialLast = true;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000138 } else {
139 ++numMidPoints;
140 ++results->fNumPoints;
141 }
142 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000143
robertphillips@google.com935ad022012-12-05 19:07:21 +0000144 results->fPoints = new SkPoint[results->fNumPoints];
robertphillips@google.com629ab542012-11-28 17:18:11 +0000145
robertphillips@google.com6d875572012-12-17 18:56:29 +0000146 SkScalar distance = 0;
147 int curPt = 0;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000148
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000149 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
150 SkASSERT(clampedInitialDashLength <= length);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000151
robertphillips@google.com6d875572012-12-17 18:56:29 +0000152 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000153 if (clampedInitialDashLength > 0) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000154 // partial first block
155 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000156 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, SkScalarHalf(clampedInitialDashLength));
157 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, SkScalarHalf(clampedInitialDashLength));
robertphillips@google.com6d875572012-12-17 18:56:29 +0000158 SkScalar halfWidth, halfHeight;
159 if (isXAxis) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000160 halfWidth = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000161 halfHeight = SkScalarHalf(rec.getWidth());
162 } else {
163 halfWidth = SkScalarHalf(rec.getWidth());
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000164 halfHeight = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000165 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000166 if (clampedInitialDashLength < fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000167 // This one will not be like the others
168 results->fFirst.addRect(x - halfWidth, y - halfHeight,
169 x + halfWidth, y + halfHeight);
170 } else {
171 SkASSERT(curPt < results->fNumPoints);
172 results->fPoints[curPt].set(x, y);
173 ++curPt;
174 }
175
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000176 distance += clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000177 }
178
179 distance += fIntervals[1]; // skip over the next blank block too
180 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000181 distance += clampedInitialDashLength;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000182 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000183 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000184
robertphillips@google.com6d875572012-12-17 18:56:29 +0000185 if (0 != numMidPoints) {
186 distance += SkScalarHalf(fIntervals[0]);
187
188 for (int i = 0; i < numMidPoints; ++i) {
189 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance);
190 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance);
191
192 SkASSERT(curPt < results->fNumPoints);
193 results->fPoints[curPt].set(x, y);
194 ++curPt;
195
196 distance += fIntervalLength;
197 }
198
199 distance -= SkScalarHalf(fIntervals[0]);
200 }
201
202 if (partialLast) {
203 // partial final block
204 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
205 SkScalar temp = length - distance;
206 SkASSERT(temp < fIntervals[0]);
207 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance + SkScalarHalf(temp));
208 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance + SkScalarHalf(temp));
209 SkScalar halfWidth, halfHeight;
210 if (isXAxis) {
211 halfWidth = SkScalarHalf(temp);
212 halfHeight = SkScalarHalf(rec.getWidth());
213 } else {
214 halfWidth = SkScalarHalf(rec.getWidth());
215 halfHeight = SkScalarHalf(temp);
216 }
217 results->fLast.addRect(x - halfWidth, y - halfHeight,
218 x + halfWidth, y + halfHeight);
219 }
220
221 SkASSERT(curPt == results->fNumPoints);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000222 }
223
224 return true;
225}
226
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000227SkPathEffect::DashType SkDashPathEffect::asADash(DashInfo* info) const {
228 if (info) {
229 if (info->fCount >= fCount && NULL != info->fIntervals) {
230 memcpy(info->fIntervals, fIntervals, fCount * sizeof(SkScalar));
231 }
232 info->fCount = fCount;
233 info->fPhase = fPhase;
234 }
235 return kDash_DashType;
236}
237
robertphillips@google.comc2eae472013-10-21 12:26:10 +0000238SkFlattenable::Factory SkDashPathEffect::getFactory() const {
commit-bot@chromium.org7fc22282014-03-07 14:43:00 +0000239 return CreateProc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240}
241
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000242void SkDashPathEffect::flatten(SkWriteBuffer& buffer) const {
djsollen@google.com54924242012-03-29 15:18:04 +0000243 this->INHERITED::flatten(buffer);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000244 buffer.writeScalar(fPhase);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000245 buffer.writeScalarArray(fIntervals, fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246}
247
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000248SkFlattenable* SkDashPathEffect::CreateProc(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 return SkNEW_ARGS(SkDashPathEffect, (buffer));
250}
251
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000252SkDashPathEffect::SkDashPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +0000253 bool useOldPic = buffer.isVersionLT(SkReadBuffer::kDashWritesPhaseIntervals_Version);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000254 if (useOldPic) {
255 fInitialDashIndex = buffer.readInt();
256 fInitialDashLength = buffer.readScalar();
257 fIntervalLength = buffer.readScalar();
258 buffer.readBool(); // Dummy for old ScalarToFit field
259 } else {
260 fPhase = buffer.readScalar();
261 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000262
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000263 fCount = buffer.getArrayCount();
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000264 size_t allocSize = sizeof(SkScalar) * fCount;
265 if (buffer.validateAvailable(allocSize)) {
266 fIntervals = (SkScalar*)sk_malloc_throw(allocSize);
267 buffer.readScalarArray(fIntervals, fCount);
268 } else {
269 fIntervals = NULL;
270 }
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000271
272 if (useOldPic) {
273 fPhase = 0;
commit-bot@chromium.org6b3eebc2014-05-13 14:45:29 +0000274 if (fInitialDashLength != -1) { // Signal for bad dash interval
275 for (int i = 0; i < fInitialDashIndex; ++i) {
276 fPhase += fIntervals[i];
277 }
278 fPhase += fIntervals[fInitialDashIndex] - fInitialDashLength;
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000279 }
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000280 } else {
egdaniela22ea182014-06-11 06:51:51 -0700281 // set the internal data members, fPhase should have been between 0 and intervalLength
282 // when written to buffer so no need to adjust it
283 SkDashPath::CalcDashParameters(fPhase, fIntervals, fCount, &fInitialDashLength,
284 &fInitialDashIndex, &fIntervalLength);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000285 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286}