blob: 04f9e9e85f8454f1c962853a3087c640b0cf1a1a [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"
Mike Reed76f70622017-05-23 23:00:14 -04009#include "SkDashImpl.h"
egdaniela22ea182014-06-11 06:51:51 -070010#include "SkDashPathPriv.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
12#include "SkWriteBuffer.h"
halcanary435657f2015-09-15 12:53:07 -070013#include "SkStrokeRec.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
Mike Reed76f70622017-05-23 23:00:14 -040015SkDashImpl::SkDashImpl(const SkScalar intervals[], int count, SkScalar phase)
mtklein1c577cd2014-07-09 09:54:10 -070016 : fPhase(0)
caryclarkeb75c7d2016-03-18 06:04:26 -070017 , fInitialDashLength(-1)
mtklein1c577cd2014-07-09 09:54:10 -070018 , fInitialDashIndex(0)
19 , fIntervalLength(0) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000020 SkASSERT(intervals);
cblumeeafe9d12016-09-26 04:22:59 -070021 SkASSERT(count > 1 && SkIsAlign2(count));
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000022
23 fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count);
24 fCount = count;
25 for (int i = 0; i < count; i++) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000026 fIntervals[i] = intervals[i];
27 }
28
egdaniela22ea182014-06-11 06:51:51 -070029 // set the internal data members
mtklein1c577cd2014-07-09 09:54:10 -070030 SkDashPath::CalcDashParameters(phase, fIntervals, fCount,
31 &fInitialDashLength, &fInitialDashIndex, &fIntervalLength, &fPhase);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000032}
33
Mike Reed76f70622017-05-23 23:00:14 -040034SkDashImpl::~SkDashImpl() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 sk_free(fIntervals);
36}
37
Mike Reed76f70622017-05-23 23:00:14 -040038bool SkDashImpl::filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
39 const SkRect* cullRect) const {
caryclarkeb75c7d2016-03-18 06:04:26 -070040 return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals, fCount,
egdaniela22ea182014-06-11 06:51:51 -070041 fInitialDashLength, fInitialDashIndex, fIntervalLength);
reed@android.com8a1c16f2008-12-17 15:59:43 +000042}
43
robertphillips9f2251c2014-11-04 13:33:50 -080044static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) {
45 SkScalar radius = SkScalarHalf(rec.getWidth());
46 if (0 == radius) {
47 radius = SK_Scalar1; // hairlines
48 }
49 if (SkPaint::kMiter_Join == rec.getJoin()) {
Mike Reed8be952a2017-02-13 20:44:33 -050050 radius *= rec.getMiter();
robertphillips9f2251c2014-11-04 13:33:50 -080051 }
52 rect->outset(radius, radius);
53}
54
mtklein88fd0fb2014-12-01 06:56:38 -080055// Attempt to trim the line to minimally cover the cull rect (currently
robertphillips9f2251c2014-11-04 13:33:50 -080056// only works for horizontal and vertical lines).
57// Return true if processing should continue; false otherwise.
58static bool cull_line(SkPoint* pts, const SkStrokeRec& rec,
59 const SkMatrix& ctm, const SkRect* cullRect,
60 const SkScalar intervalLength) {
halcanary96fcdcc2015-08-27 07:41:13 -070061 if (nullptr == cullRect) {
robertphillips9f2251c2014-11-04 13:33:50 -080062 SkASSERT(false); // Shouldn't ever occur in practice
63 return false;
64 }
65
66 SkScalar dx = pts[1].x() - pts[0].x();
67 SkScalar dy = pts[1].y() - pts[0].y();
68
egdaniel21402e32014-11-05 05:02:27 -080069 if ((dx && dy) || (!dx && !dy)) {
robertphillips9f2251c2014-11-04 13:33:50 -080070 return false;
71 }
72
73 SkRect bounds = *cullRect;
74 outset_for_stroke(&bounds, rec);
75
76 // cullRect is in device space while pts are in the local coordinate system
77 // defined by the ctm. We want our answer in the local coordinate system.
78
79 SkASSERT(ctm.rectStaysRect());
80 SkMatrix inv;
81 if (!ctm.invert(&inv)) {
82 return false;
83 }
84
85 inv.mapRect(&bounds);
86
87 if (dx) {
88 SkASSERT(dx && !dy);
89 SkScalar minX = pts[0].fX;
90 SkScalar maxX = pts[1].fX;
91
92 if (dx < 0) {
93 SkTSwap(minX, maxX);
94 }
95
96 SkASSERT(minX < maxX);
reedccc12ed2014-12-12 12:48:50 -080097 if (maxX <= bounds.fLeft || minX >= bounds.fRight) {
robertphillips9f2251c2014-11-04 13:33:50 -080098 return false;
99 }
100
101 // Now we actually perform the chop, removing the excess to the left and
102 // right of the bounds (keeping our new line "in phase" with the dash,
103 // hence the (mod intervalLength).
104
105 if (minX < bounds.fLeft) {
106 minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX, intervalLength);
107 }
108 if (maxX > bounds.fRight) {
109 maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight, intervalLength);
110 }
111
112 SkASSERT(maxX > minX);
113 if (dx < 0) {
114 SkTSwap(minX, maxX);
115 }
116 pts[0].fX = minX;
117 pts[1].fX = maxX;
118 } else {
119 SkASSERT(dy && !dx);
120 SkScalar minY = pts[0].fY;
121 SkScalar maxY = pts[1].fY;
122
123 if (dy < 0) {
124 SkTSwap(minY, maxY);
125 }
126
127 SkASSERT(minY < maxY);
reedccc12ed2014-12-12 12:48:50 -0800128 if (maxY <= bounds.fTop || minY >= bounds.fBottom) {
robertphillips9f2251c2014-11-04 13:33:50 -0800129 return false;
130 }
131
132 // Now we actually perform the chop, removing the excess to the top and
133 // bottom of the bounds (keeping our new line "in phase" with the dash,
134 // hence the (mod intervalLength).
135
136 if (minY < bounds.fTop) {
137 minY = bounds.fTop - SkScalarMod(bounds.fTop - minY, intervalLength);
138 }
139 if (maxY > bounds.fBottom) {
140 maxY = bounds.fBottom + SkScalarMod(maxY - bounds.fBottom, intervalLength);
141 }
142
143 SkASSERT(maxY > minY);
144 if (dy < 0) {
145 SkTSwap(minY, maxY);
146 }
147 pts[0].fY = minY;
148 pts[1].fY = maxY;
149 }
150
151 return true;
152}
153
robertphillips@google.com629ab542012-11-28 17:18:11 +0000154// Currently asPoints is more restrictive then it needs to be. In the future
155// we need to:
156// allow kRound_Cap capping (could allow rotations in the matrix with this)
robertphillips@google.com6d875572012-12-17 18:56:29 +0000157// allow paths to be returned
Mike Reed76f70622017-05-23 23:00:14 -0400158bool SkDashImpl::asPoints(PointData* results, const SkPath& src, const SkStrokeRec& rec,
159 const SkMatrix& matrix, const SkRect* cullRect) const {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000160 // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out
caryclarkeb75c7d2016-03-18 06:04:26 -0700161 if (0 >= rec.getWidth()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000162 return false;
163 }
164
robertphillips@google.com6d875572012-12-17 18:56:29 +0000165 // TODO: this next test could be eased up. We could allow any number of
166 // intervals as long as all the ons match and all the offs match.
167 // Additionally, they do not necessarily need to be integers.
168 // We cannot allow arbitrary intervals since we want the returned points
169 // to be uniformly sized.
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000170 if (fCount != 2 ||
robertphillips@google.com6d875572012-12-17 18:56:29 +0000171 !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) ||
172 !SkScalarIsInt(fIntervals[0]) ||
173 !SkScalarIsInt(fIntervals[1])) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000174 return false;
175 }
176
robertphillips@google.com629ab542012-11-28 17:18:11 +0000177 SkPoint pts[2];
178
robertphillips@google.com6d875572012-12-17 18:56:29 +0000179 if (!src.isLine(pts)) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000180 return false;
181 }
182
robertphillips@google.com6d875572012-12-17 18:56:29 +0000183 // TODO: this test could be eased up to allow circles
robertphillips@google.com629ab542012-11-28 17:18:11 +0000184 if (SkPaint::kButt_Cap != rec.getCap()) {
185 return false;
186 }
187
robertphillips@google.com6d875572012-12-17 18:56:29 +0000188 // TODO: this test could be eased up for circles. Rotations could be allowed.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000189 if (!matrix.rectStaysRect()) {
190 return false;
191 }
192
robertphillips9f2251c2014-11-04 13:33:50 -0800193 // See if the line can be limited to something plausible.
194 if (!cull_line(pts, rec, matrix, cullRect, fIntervalLength)) {
195 return false;
196 }
197
198 SkScalar length = SkPoint::Distance(pts[1], pts[0]);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000199
robertphillips@google.com6d875572012-12-17 18:56:29 +0000200 SkVector tangent = pts[1] - pts[0];
201 if (tangent.isZero()) {
202 return false;
203 }
204
205 tangent.scale(SkScalarInvert(length));
206
207 // TODO: make this test for horizontal & vertical lines more robust
208 bool isXAxis = true;
robertphillips9f2251c2014-11-04 13:33:50 -0800209 if (SkScalarNearlyEqual(SK_Scalar1, tangent.fX) ||
210 SkScalarNearlyEqual(-SK_Scalar1, tangent.fX)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000211 results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth()));
robertphillips9f2251c2014-11-04 13:33:50 -0800212 } else if (SkScalarNearlyEqual(SK_Scalar1, tangent.fY) ||
213 SkScalarNearlyEqual(-SK_Scalar1, tangent.fY)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000214 results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0]));
215 isXAxis = false;
216 } else if (SkPaint::kRound_Cap != rec.getCap()) {
217 // Angled lines don't have axis-aligned boxes.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000218 return false;
219 }
220
bsalomon49f085d2014-09-05 13:34:00 -0700221 if (results) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000222 results->fFlags = 0;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000223 SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000224
robertphillips@google.com6d875572012-12-17 18:56:29 +0000225 if (SkPaint::kRound_Cap == rec.getCap()) {
226 results->fFlags |= PointData::kCircles_PointFlag;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000227 }
228
robertphillips@google.com6d875572012-12-17 18:56:29 +0000229 results->fNumPoints = 0;
230 SkScalar len2 = length;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000231 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
232 SkASSERT(len2 >= clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000233 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000234 if (clampedInitialDashLength > 0) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000235 if (clampedInitialDashLength >= fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000236 ++results->fNumPoints; // partial first dash
237 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000238 len2 -= clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000239 }
240 len2 -= fIntervals[1]; // also skip first space
241 if (len2 < 0) {
242 len2 = 0;
243 }
244 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000245 len2 -= clampedInitialDashLength; // skip initial partial empty
robertphillips@google.com6d875572012-12-17 18:56:29 +0000246 }
247 }
lsalzmanf41ae2f2016-07-21 09:37:59 -0700248 // Too many midpoints can cause results->fNumPoints to overflow or
249 // otherwise cause the results->fPoints allocation below to OOM.
250 // Cap it to a sane value.
251 SkScalar numIntervals = len2 / fIntervalLength;
252 if (!SkScalarIsFinite(numIntervals) || numIntervals > SkDashPath::kMaxDashCount) {
253 return false;
254 }
255 int numMidPoints = SkScalarFloorToInt(numIntervals);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000256 results->fNumPoints += numMidPoints;
257 len2 -= numMidPoints * fIntervalLength;
258 bool partialLast = false;
259 if (len2 > 0) {
260 if (len2 < fIntervals[0]) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000261 partialLast = true;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000262 } else {
263 ++numMidPoints;
264 ++results->fNumPoints;
265 }
266 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000267
robertphillips@google.com935ad022012-12-05 19:07:21 +0000268 results->fPoints = new SkPoint[results->fNumPoints];
robertphillips@google.com629ab542012-11-28 17:18:11 +0000269
robertphillips@google.com6d875572012-12-17 18:56:29 +0000270 SkScalar distance = 0;
271 int curPt = 0;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000272
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000273 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
274 SkASSERT(clampedInitialDashLength <= length);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000275
robertphillips@google.com6d875572012-12-17 18:56:29 +0000276 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000277 if (clampedInitialDashLength > 0) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000278 // partial first block
279 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
Mike Reed8be952a2017-02-13 20:44:33 -0500280 SkScalar x = pts[0].fX + tangent.fX * SkScalarHalf(clampedInitialDashLength);
281 SkScalar y = pts[0].fY + tangent.fY * SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000282 SkScalar halfWidth, halfHeight;
283 if (isXAxis) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000284 halfWidth = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000285 halfHeight = SkScalarHalf(rec.getWidth());
286 } else {
287 halfWidth = SkScalarHalf(rec.getWidth());
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000288 halfHeight = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000289 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000290 if (clampedInitialDashLength < fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000291 // This one will not be like the others
292 results->fFirst.addRect(x - halfWidth, y - halfHeight,
293 x + halfWidth, y + halfHeight);
294 } else {
295 SkASSERT(curPt < results->fNumPoints);
296 results->fPoints[curPt].set(x, y);
297 ++curPt;
298 }
299
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000300 distance += clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000301 }
302
303 distance += fIntervals[1]; // skip over the next blank block too
304 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000305 distance += clampedInitialDashLength;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000306 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000307 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000308
robertphillips@google.com6d875572012-12-17 18:56:29 +0000309 if (0 != numMidPoints) {
310 distance += SkScalarHalf(fIntervals[0]);
311
312 for (int i = 0; i < numMidPoints; ++i) {
Mike Reed8be952a2017-02-13 20:44:33 -0500313 SkScalar x = pts[0].fX + tangent.fX * distance;
314 SkScalar y = pts[0].fY + tangent.fY * distance;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000315
316 SkASSERT(curPt < results->fNumPoints);
317 results->fPoints[curPt].set(x, y);
318 ++curPt;
319
320 distance += fIntervalLength;
321 }
322
323 distance -= SkScalarHalf(fIntervals[0]);
324 }
325
326 if (partialLast) {
327 // partial final block
328 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
329 SkScalar temp = length - distance;
330 SkASSERT(temp < fIntervals[0]);
Mike Reed8be952a2017-02-13 20:44:33 -0500331 SkScalar x = pts[0].fX + tangent.fX * (distance + SkScalarHalf(temp));
332 SkScalar y = pts[0].fY + tangent.fY * (distance + SkScalarHalf(temp));
robertphillips@google.com6d875572012-12-17 18:56:29 +0000333 SkScalar halfWidth, halfHeight;
334 if (isXAxis) {
335 halfWidth = SkScalarHalf(temp);
336 halfHeight = SkScalarHalf(rec.getWidth());
337 } else {
338 halfWidth = SkScalarHalf(rec.getWidth());
339 halfHeight = SkScalarHalf(temp);
340 }
341 results->fLast.addRect(x - halfWidth, y - halfHeight,
342 x + halfWidth, y + halfHeight);
343 }
344
345 SkASSERT(curPt == results->fNumPoints);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000346 }
347
348 return true;
349}
350
Mike Reed76f70622017-05-23 23:00:14 -0400351SkPathEffect::DashType SkDashImpl::asADash(DashInfo* info) const {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000352 if (info) {
bsalomon49f085d2014-09-05 13:34:00 -0700353 if (info->fCount >= fCount && info->fIntervals) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000354 memcpy(info->fIntervals, fIntervals, fCount * sizeof(SkScalar));
355 }
356 info->fCount = fCount;
357 info->fPhase = fPhase;
358 }
359 return kDash_DashType;
360}
361
Mike Reed76f70622017-05-23 23:00:14 -0400362void SkDashImpl::flatten(SkWriteBuffer& buffer) const {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000363 buffer.writeScalar(fPhase);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000364 buffer.writeScalarArray(fIntervals, fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365}
366
Mike Reed76f70622017-05-23 23:00:14 -0400367sk_sp<SkFlattenable> SkDashImpl::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700368 const SkScalar phase = buffer.readScalar();
369 uint32_t count = buffer.getArrayCount();
370 SkAutoSTArray<32, SkScalar> intervals(count);
371 if (buffer.readScalarArray(intervals.get(), count)) {
Mike Reed76f70622017-05-23 23:00:14 -0400372 return SkDashPathEffect::Make(intervals.get(), SkToInt(count), phase);
reed9fa60da2014-08-21 07:59:51 -0700373 }
halcanary96fcdcc2015-08-27 07:41:13 -0700374 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375}
robertphillips42dbfa82015-01-26 06:08:52 -0800376
377#ifndef SK_IGNORE_TO_STRING
Mike Reed76f70622017-05-23 23:00:14 -0400378void SkDashImpl::toString(SkString* str) const {
robertphillips42dbfa82015-01-26 06:08:52 -0800379 str->appendf("SkDashPathEffect: (");
380 str->appendf("count: %d phase %.2f intervals: (", fCount, fPhase);
381 for (int i = 0; i < fCount; ++i) {
382 str->appendf("%.2f", fIntervals[i]);
383 if (i < fCount-1) {
384 str->appendf(", ");
385 }
386 }
387 str->appendf("))");
388}
389#endif
reedca726ab2016-02-22 12:50:25 -0800390
391//////////////////////////////////////////////////////////////////////////////////////////////////
392
reeda4393342016-03-18 11:22:57 -0700393sk_sp<SkPathEffect> SkDashPathEffect::Make(const SkScalar intervals[], int count, SkScalar phase) {
caryclarkeb75c7d2016-03-18 06:04:26 -0700394 if (!SkDashPath::ValidDashPath(phase, intervals, count)) {
reedca726ab2016-02-22 12:50:25 -0800395 return nullptr;
396 }
Mike Reed76f70622017-05-23 23:00:14 -0400397 return sk_sp<SkPathEffect>(new SkDashImpl(intervals, count, phase));
reedca726ab2016-02-22 12:50:25 -0800398}