blob: a053066191e955add6f6b8525a19ebc1c5c90b7e [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
mtklein1c577cd2014-07-09 09:54:10 -070014SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count, SkScalar phase)
15 : fPhase(0)
16 , fInitialDashLength(0)
17 , fInitialDashIndex(0)
18 , fIntervalLength(0) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +000019 SkASSERT(intervals);
20 SkASSERT(count > 1 && SkAlign2(count) == count);
21
22 fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count);
23 fCount = count;
24 for (int i = 0; i < count; i++) {
25 SkASSERT(intervals[i] >= 0);
26 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@reedtribe.org3334c3a2011-04-20 11:39:28 +000034SkDashPathEffect::~SkDashPathEffect() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 sk_free(fIntervals);
36}
37
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000038bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000039 SkStrokeRec* rec, const SkRect* cullRect) const {
egdaniela22ea182014-06-11 06:51:51 -070040 return SkDashPath::FilterDashPath(dst, src, rec, cullRect, fIntervals, fCount,
41 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()) {
50 radius = SkScalarMul(radius, rec.getMiter());
51 }
52 rect->outset(radius, radius);
53}
54
55// Attempt to trim the line to minimally cover the cull rect (currently
56// 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) {
61 if (NULL == cullRect) {
62 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
69 if (dx && dy) {
70 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);
97 if (maxX < bounds.fLeft || minX > bounds.fRight) {
98 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);
128 if (maxY < bounds.fTop || minY > bounds.fBottom) {
129 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
robertphillips@google.com629ab542012-11-28 17:18:11 +0000158bool SkDashPathEffect::asPoints(PointData* results,
159 const SkPath& src,
160 const SkStrokeRec& rec,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000161 const SkMatrix& matrix,
162 const SkRect* cullRect) const {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000163 // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out
164 if (fInitialDashLength < 0 || 0 >= rec.getWidth()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000165 return false;
166 }
167
robertphillips@google.com6d875572012-12-17 18:56:29 +0000168 // TODO: this next test could be eased up. We could allow any number of
169 // intervals as long as all the ons match and all the offs match.
170 // Additionally, they do not necessarily need to be integers.
171 // We cannot allow arbitrary intervals since we want the returned points
172 // to be uniformly sized.
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000173 if (fCount != 2 ||
robertphillips@google.com6d875572012-12-17 18:56:29 +0000174 !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) ||
175 !SkScalarIsInt(fIntervals[0]) ||
176 !SkScalarIsInt(fIntervals[1])) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000177 return false;
178 }
179
robertphillips@google.com629ab542012-11-28 17:18:11 +0000180 SkPoint pts[2];
181
robertphillips@google.com6d875572012-12-17 18:56:29 +0000182 if (!src.isLine(pts)) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000183 return false;
184 }
185
robertphillips@google.com6d875572012-12-17 18:56:29 +0000186 // TODO: this test could be eased up to allow circles
robertphillips@google.com629ab542012-11-28 17:18:11 +0000187 if (SkPaint::kButt_Cap != rec.getCap()) {
188 return false;
189 }
190
robertphillips@google.com6d875572012-12-17 18:56:29 +0000191 // TODO: this test could be eased up for circles. Rotations could be allowed.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000192 if (!matrix.rectStaysRect()) {
193 return false;
194 }
195
robertphillips9f2251c2014-11-04 13:33:50 -0800196 // See if the line can be limited to something plausible.
197 if (!cull_line(pts, rec, matrix, cullRect, fIntervalLength)) {
198 return false;
199 }
200
201 SkScalar length = SkPoint::Distance(pts[1], pts[0]);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000202
robertphillips@google.com6d875572012-12-17 18:56:29 +0000203 SkVector tangent = pts[1] - pts[0];
204 if (tangent.isZero()) {
205 return false;
206 }
207
208 tangent.scale(SkScalarInvert(length));
209
210 // TODO: make this test for horizontal & vertical lines more robust
211 bool isXAxis = true;
robertphillips9f2251c2014-11-04 13:33:50 -0800212 if (SkScalarNearlyEqual(SK_Scalar1, tangent.fX) ||
213 SkScalarNearlyEqual(-SK_Scalar1, tangent.fX)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000214 results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth()));
robertphillips9f2251c2014-11-04 13:33:50 -0800215 } else if (SkScalarNearlyEqual(SK_Scalar1, tangent.fY) ||
216 SkScalarNearlyEqual(-SK_Scalar1, tangent.fY)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000217 results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0]));
218 isXAxis = false;
219 } else if (SkPaint::kRound_Cap != rec.getCap()) {
220 // Angled lines don't have axis-aligned boxes.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000221 return false;
222 }
223
bsalomon49f085d2014-09-05 13:34:00 -0700224 if (results) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000225 results->fFlags = 0;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000226 SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000227
robertphillips@google.com6d875572012-12-17 18:56:29 +0000228 if (SkPaint::kRound_Cap == rec.getCap()) {
229 results->fFlags |= PointData::kCircles_PointFlag;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000230 }
231
robertphillips@google.com6d875572012-12-17 18:56:29 +0000232 results->fNumPoints = 0;
233 SkScalar len2 = length;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000234 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
235 SkASSERT(len2 >= clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000236 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000237 if (clampedInitialDashLength > 0) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000238 if (clampedInitialDashLength >= fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000239 ++results->fNumPoints; // partial first dash
240 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000241 len2 -= clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000242 }
243 len2 -= fIntervals[1]; // also skip first space
244 if (len2 < 0) {
245 len2 = 0;
246 }
247 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000248 len2 -= clampedInitialDashLength; // skip initial partial empty
robertphillips@google.com6d875572012-12-17 18:56:29 +0000249 }
250 }
251 int numMidPoints = SkScalarFloorToInt(SkScalarDiv(len2, fIntervalLength));
252 results->fNumPoints += numMidPoints;
253 len2 -= numMidPoints * fIntervalLength;
254 bool partialLast = false;
255 if (len2 > 0) {
256 if (len2 < fIntervals[0]) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000257 partialLast = true;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000258 } else {
259 ++numMidPoints;
260 ++results->fNumPoints;
261 }
262 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000263
robertphillips@google.com935ad022012-12-05 19:07:21 +0000264 results->fPoints = new SkPoint[results->fNumPoints];
robertphillips@google.com629ab542012-11-28 17:18:11 +0000265
robertphillips@google.com6d875572012-12-17 18:56:29 +0000266 SkScalar distance = 0;
267 int curPt = 0;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000268
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000269 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
270 SkASSERT(clampedInitialDashLength <= length);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000271
robertphillips@google.com6d875572012-12-17 18:56:29 +0000272 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000273 if (clampedInitialDashLength > 0) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000274 // partial first block
275 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000276 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, SkScalarHalf(clampedInitialDashLength));
277 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, SkScalarHalf(clampedInitialDashLength));
robertphillips@google.com6d875572012-12-17 18:56:29 +0000278 SkScalar halfWidth, halfHeight;
279 if (isXAxis) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000280 halfWidth = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000281 halfHeight = SkScalarHalf(rec.getWidth());
282 } else {
283 halfWidth = SkScalarHalf(rec.getWidth());
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000284 halfHeight = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000285 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000286 if (clampedInitialDashLength < fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000287 // This one will not be like the others
288 results->fFirst.addRect(x - halfWidth, y - halfHeight,
289 x + halfWidth, y + halfHeight);
290 } else {
291 SkASSERT(curPt < results->fNumPoints);
292 results->fPoints[curPt].set(x, y);
293 ++curPt;
294 }
295
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000296 distance += clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000297 }
298
299 distance += fIntervals[1]; // skip over the next blank block too
300 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000301 distance += clampedInitialDashLength;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000302 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000303 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000304
robertphillips@google.com6d875572012-12-17 18:56:29 +0000305 if (0 != numMidPoints) {
306 distance += SkScalarHalf(fIntervals[0]);
307
308 for (int i = 0; i < numMidPoints; ++i) {
309 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance);
310 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance);
311
312 SkASSERT(curPt < results->fNumPoints);
313 results->fPoints[curPt].set(x, y);
314 ++curPt;
315
316 distance += fIntervalLength;
317 }
318
319 distance -= SkScalarHalf(fIntervals[0]);
320 }
321
322 if (partialLast) {
323 // partial final block
324 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
325 SkScalar temp = length - distance;
326 SkASSERT(temp < fIntervals[0]);
327 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance + SkScalarHalf(temp));
328 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance + SkScalarHalf(temp));
329 SkScalar halfWidth, halfHeight;
330 if (isXAxis) {
331 halfWidth = SkScalarHalf(temp);
332 halfHeight = SkScalarHalf(rec.getWidth());
333 } else {
334 halfWidth = SkScalarHalf(rec.getWidth());
335 halfHeight = SkScalarHalf(temp);
336 }
337 results->fLast.addRect(x - halfWidth, y - halfHeight,
338 x + halfWidth, y + halfHeight);
339 }
340
341 SkASSERT(curPt == results->fNumPoints);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000342 }
343
344 return true;
345}
346
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000347SkPathEffect::DashType SkDashPathEffect::asADash(DashInfo* info) const {
348 if (info) {
bsalomon49f085d2014-09-05 13:34:00 -0700349 if (info->fCount >= fCount && info->fIntervals) {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000350 memcpy(info->fIntervals, fIntervals, fCount * sizeof(SkScalar));
351 }
352 info->fCount = fCount;
353 info->fPhase = fPhase;
354 }
355 return kDash_DashType;
356}
357
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000358void SkDashPathEffect::flatten(SkWriteBuffer& buffer) const {
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000359 buffer.writeScalar(fPhase);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000360 buffer.writeScalarArray(fIntervals, fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361}
362
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000363SkFlattenable* SkDashPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700364 const SkScalar phase = buffer.readScalar();
365 uint32_t count = buffer.getArrayCount();
366 SkAutoSTArray<32, SkScalar> intervals(count);
367 if (buffer.readScalarArray(intervals.get(), count)) {
368 return Create(intervals.get(), SkToInt(count), phase);
369 }
370 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371}
372
reed9fa60da2014-08-21 07:59:51 -0700373#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
mtklein1c577cd2014-07-09 09:54:10 -0700374SkDashPathEffect::SkDashPathEffect(SkReadBuffer& buffer)
375 : INHERITED(buffer)
376 , fPhase(0)
377 , fInitialDashLength(0)
378 , fInitialDashIndex(0)
379 , fIntervalLength(0) {
commit-bot@chromium.org7ed173b2014-05-20 17:31:08 +0000380 bool useOldPic = buffer.isVersionLT(SkReadBuffer::kDashWritesPhaseIntervals_Version);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000381 if (useOldPic) {
382 fInitialDashIndex = buffer.readInt();
383 fInitialDashLength = buffer.readScalar();
384 fIntervalLength = buffer.readScalar();
385 buffer.readBool(); // Dummy for old ScalarToFit field
386 } else {
387 fPhase = buffer.readScalar();
388 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000389
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000390 fCount = buffer.getArrayCount();
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000391 size_t allocSize = sizeof(SkScalar) * fCount;
392 if (buffer.validateAvailable(allocSize)) {
393 fIntervals = (SkScalar*)sk_malloc_throw(allocSize);
394 buffer.readScalarArray(fIntervals, fCount);
395 } else {
396 fIntervals = NULL;
397 }
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000398
399 if (useOldPic) {
400 fPhase = 0;
commit-bot@chromium.org6b3eebc2014-05-13 14:45:29 +0000401 if (fInitialDashLength != -1) { // Signal for bad dash interval
402 for (int i = 0; i < fInitialDashIndex; ++i) {
403 fPhase += fIntervals[i];
404 }
405 fPhase += fIntervals[fInitialDashIndex] - fInitialDashLength;
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000406 }
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000407 } else {
egdaniela22ea182014-06-11 06:51:51 -0700408 // set the internal data members, fPhase should have been between 0 and intervalLength
409 // when written to buffer so no need to adjust it
mtklein1c577cd2014-07-09 09:54:10 -0700410 SkDashPath::CalcDashParameters(fPhase, fIntervals, fCount,
411 &fInitialDashLength, &fInitialDashIndex, &fIntervalLength);
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000412 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000413}
reed9fa60da2014-08-21 07:59:51 -0700414#endif
415