blob: 8be5f1d086734deafa587d9ba76207e4b8f0165d [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 "SkDashPathEffect.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
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000015static inline int is_even(int x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000016 return (~x) << 31;
17}
18
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000019static SkScalar FindFirstInterval(const SkScalar intervals[], SkScalar phase,
reed@google.comd9ee3482012-08-06 14:58:35 +000020 int32_t* index, int count) {
21 for (int i = 0; i < count; ++i) {
22 if (phase > intervals[i]) {
23 phase -= intervals[i];
24 } else {
25 *index = i;
26 return intervals[i] - phase;
27 }
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000028 }
reed@google.comd9ee3482012-08-06 14:58:35 +000029 // If we get here, phase "appears" to be larger than our length. This
30 // shouldn't happen with perfect precision, but we can accumulate errors
31 // during the initial length computation (rounding can make our sum be too
32 // big or too small. In that event, we just have to eat the error here.
33 *index = 0;
34 return intervals[0];
reed@android.com8a1c16f2008-12-17 15:59:43 +000035}
36
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000037SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count,
38 SkScalar phase, bool scaleToFit)
39 : fScaleToFit(scaleToFit) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 SkASSERT(intervals);
41 SkASSERT(count > 1 && SkAlign2(count) == count);
42
43 fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count);
44 fCount = count;
45
46 SkScalar len = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000047 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 SkASSERT(intervals[i] >= 0);
49 fIntervals[i] = intervals[i];
50 len += intervals[i];
51 }
52 fIntervalLength = len;
53
epoger@google.com20bf4ca2012-04-27 13:34:52 +000054 // watch out for values that might make us go out of bounds
55 if ((len > 0) && SkScalarIsFinite(phase) && SkScalarIsFinite(len)) {
56
57 // Adjust phase to be between 0 and len, "flipping" phase if negative.
58 // e.g., if len is 100, then phase of -20 (or -120) is equivalent to 80
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000059 if (phase < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 phase = -phase;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000061 if (phase > len) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 phase = SkScalarMod(phase, len);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000063 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 phase = len - phase;
epoger@google.com20bf4ca2012-04-27 13:34:52 +000065
66 // Due to finite precision, it's possible that phase == len,
67 // even after the subtract (if len >>> phase), so fix that here.
68 // This fixes http://crbug.com/124652 .
reed@google.com1df888b2012-04-24 22:47:21 +000069 SkASSERT(phase <= len);
70 if (phase == len) {
71 phase = 0;
72 }
epoger@google.com20bf4ca2012-04-27 13:34:52 +000073 } else if (phase >= len) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 phase = SkScalarMod(phase, len);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000075 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 SkASSERT(phase >= 0 && phase < len);
epoger@google.com20bf4ca2012-04-27 13:34:52 +000077
reed@google.comd9ee3482012-08-06 14:58:35 +000078 fInitialDashLength = FindFirstInterval(intervals, phase,
79 &fInitialDashIndex, count);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080
81 SkASSERT(fInitialDashLength >= 0);
82 SkASSERT(fInitialDashIndex >= 0 && fInitialDashIndex < fCount);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000083 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 fInitialDashLength = -1; // signal bad dash intervals
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000085 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000086}
87
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000088SkDashPathEffect::~SkDashPathEffect() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 sk_free(fIntervals);
90}
91
reed@google.com4bbdeac2013-01-24 21:03:11 +000092static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) {
93 SkScalar radius = SkScalarHalf(rec.getWidth());
94 if (0 == radius) {
95 radius = SK_Scalar1; // hairlines
96 }
97 if (SkPaint::kMiter_Join == rec.getJoin()) {
98 radius = SkScalarMul(radius, rec.getMiter());
99 }
100 rect->outset(radius, radius);
101}
102
103// Only handles lines for now. If returns true, dstPath is the new (smaller)
104// path. If returns false, then dstPath parameter is ignored.
105static bool cull_path(const SkPath& srcPath, const SkStrokeRec& rec,
106 const SkRect* cullRect, SkScalar intervalLength,
107 SkPath* dstPath) {
108 if (NULL == cullRect) {
109 return false;
110 }
111
112 SkPoint pts[2];
113 if (!srcPath.isLine(pts)) {
114 return false;
115 }
116
117 SkRect bounds = *cullRect;
118 outset_for_stroke(&bounds, rec);
119
120 SkScalar dx = pts[1].x() - pts[0].x();
121 SkScalar dy = pts[1].y() - pts[0].y();
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000122
reed@google.com4bbdeac2013-01-24 21:03:11 +0000123 // just do horizontal lines for now (lazy)
124 if (dy) {
125 return false;
126 }
127
128 SkScalar minX = pts[0].fX;
129 SkScalar maxX = pts[1].fX;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000130
reed@google.com4bbdeac2013-01-24 21:03:11 +0000131 if (maxX < bounds.fLeft || minX > bounds.fRight) {
132 return false;
133 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000134
reed@google.com4bbdeac2013-01-24 21:03:11 +0000135 if (dx < 0) {
136 SkTSwap(minX, maxX);
137 }
138
139 // Now we actually perform the chop, removing the excess to the left and
140 // right of the bounds (keeping our new line "in phase" with the dash,
141 // hence the (mod intervalLength).
142
143 if (minX < bounds.fLeft) {
144 minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX,
145 intervalLength);
146 }
147 if (maxX > bounds.fRight) {
148 maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight,
149 intervalLength);
150 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000151
robertphillips@google.com68f670f2013-01-28 20:55:42 +0000152 SkASSERT(maxX >= minX);
reed@google.com4bbdeac2013-01-24 21:03:11 +0000153 if (dx < 0) {
154 SkTSwap(minX, maxX);
155 }
156 pts[0].fX = minX;
157 pts[1].fX = maxX;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000158
reed@google.com4bbdeac2013-01-24 21:03:11 +0000159 dstPath->moveTo(pts[0]);
160 dstPath->lineTo(pts[1]);
161 return true;
162}
163
reed@google.com3ec68f02012-05-29 20:48:50 +0000164class SpecialLineRec {
165public:
166 bool init(const SkPath& src, SkPath* dst, SkStrokeRec* rec,
reed@google.com3ec68f02012-05-29 20:48:50 +0000167 int intervalCount, SkScalar intervalLength) {
168 if (rec->isHairlineStyle() || !src.isLine(fPts)) {
169 return false;
170 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000171
reed@google.com3ec68f02012-05-29 20:48:50 +0000172 // can relax this in the future, if we handle square and round caps
173 if (SkPaint::kButt_Cap != rec->getCap()) {
174 return false;
175 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000176
reed@google.com4bbdeac2013-01-24 21:03:11 +0000177 SkScalar pathLength = SkPoint::Distance(fPts[0], fPts[1]);
178
reed@google.com3ec68f02012-05-29 20:48:50 +0000179 fTangent = fPts[1] - fPts[0];
180 if (fTangent.isZero()) {
181 return false;
182 }
183
184 fPathLength = pathLength;
185 fTangent.scale(SkScalarInvert(pathLength));
186 fTangent.rotateCCW(&fNormal);
187 fNormal.scale(SkScalarHalf(rec->getWidth()));
188
189 // now estimate how many quads will be added to the path
190 // resulting segments = pathLen * intervalCount / intervalLen
191 // resulting points = 4 * segments
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000192
reed@google.com3ec68f02012-05-29 20:48:50 +0000193 SkScalar ptCount = SkScalarMulDiv(pathLength,
194 SkIntToScalar(intervalCount),
195 intervalLength);
196 int n = SkScalarCeilToInt(ptCount) << 2;
197 dst->incReserve(n);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000198
reed@google.com3ec68f02012-05-29 20:48:50 +0000199 // we will take care of the stroking
200 rec->setFillStyle();
201 return true;
202 }
203
204 void addSegment(SkScalar d0, SkScalar d1, SkPath* path) const {
205 SkASSERT(d0 < fPathLength);
206 // clamp the segment to our length
207 if (d1 > fPathLength) {
208 d1 = fPathLength;
209 }
210
211 SkScalar x0 = fPts[0].fX + SkScalarMul(fTangent.fX, d0);
212 SkScalar x1 = fPts[0].fX + SkScalarMul(fTangent.fX, d1);
213 SkScalar y0 = fPts[0].fY + SkScalarMul(fTangent.fY, d0);
214 SkScalar y1 = fPts[0].fY + SkScalarMul(fTangent.fY, d1);
215
216 SkPoint pts[4];
217 pts[0].set(x0 + fNormal.fX, y0 + fNormal.fY); // moveTo
218 pts[1].set(x1 + fNormal.fX, y1 + fNormal.fY); // lineTo
219 pts[2].set(x1 - fNormal.fX, y1 - fNormal.fY); // lineTo
220 pts[3].set(x0 - fNormal.fX, y0 - fNormal.fY); // lineTo
221
222 path->addPoly(pts, SK_ARRAY_COUNT(pts), false);
223 }
224
225private:
226 SkPoint fPts[2];
227 SkVector fTangent;
228 SkVector fNormal;
229 SkScalar fPathLength;
230};
231
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000232bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000233 SkStrokeRec* rec, const SkRect* cullRect) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 // we do nothing if the src wants to be filled, or if our dashlength is 0
reed@google.comfd4be262012-05-25 01:04:12 +0000235 if (rec->isFillStyle() || fInitialDashLength < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 return false;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000237 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 const SkScalar* intervals = fIntervals;
fmalita@google.com6b18d242012-12-17 16:27:34 +0000240 SkScalar dashCount = 0;
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000241 int segCount = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242
reed@google.com4bbdeac2013-01-24 21:03:11 +0000243 SkPath cullPathStorage;
244 const SkPath* srcPtr = &src;
245 if (cull_path(src, *rec, cullRect, fIntervalLength, &cullPathStorage)) {
246 srcPtr = &cullPathStorage;
247 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000248
reed@google.com3ec68f02012-05-29 20:48:50 +0000249 SpecialLineRec lineRec;
reed@google.com70375522013-01-25 14:52:11 +0000250 bool specialLine = lineRec.init(*srcPtr, dst, rec, fCount >> 1, fIntervalLength);
reed@google.com4bbdeac2013-01-24 21:03:11 +0000251
252 SkPathMeasure meas(*srcPtr, false);
reed@google.com3ec68f02012-05-29 20:48:50 +0000253
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254 do {
255 bool skipFirstSegment = meas.isClosed();
256 bool addedSegment = false;
257 SkScalar length = meas.getLength();
258 int index = fInitialDashIndex;
259 SkScalar scale = SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000260
fmalita@google.com6b18d242012-12-17 16:27:34 +0000261 // Since the path length / dash length ratio may be arbitrarily large, we can exert
262 // significant memory pressure while attempting to build the filtered path. To avoid this,
263 // we simply give up dashing beyond a certain threshold.
264 //
265 // The original bug report (http://crbug.com/165432) is based on a path yielding more than
266 // 90 million dash segments and crashing the memory allocator. A limit of 1 million
267 // segments seems reasonable: at 2 verbs per segment * 9 bytes per verb, this caps the
268 // maximum dash memory overhead at roughly 17MB per path.
269 static const SkScalar kMaxDashCount = 1000000;
270 dashCount += length * (fCount >> 1) / fIntervalLength;
271 if (dashCount > kMaxDashCount) {
272 dst->reset();
273 return false;
274 }
275
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000276 if (fScaleToFit) {
277 if (fIntervalLength >= length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 scale = SkScalarDiv(length, fIntervalLength);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000279 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 SkScalar div = SkScalarDiv(length, fIntervalLength);
reed@google.come1ca7052013-12-17 19:22:07 +0000281 int n = SkScalarFloorToInt(div);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282 scale = SkScalarDiv(length, n * fIntervalLength);
283 }
284 }
285
fmalita@google.combfa04012012-12-12 22:13:58 +0000286 // Using double precision to avoid looping indefinitely due to single precision rounding
287 // (for extreme path_length/dash_length ratios). See test_infinite_dash() unittest.
288 double distance = 0;
289 double dlen = SkScalarMul(fInitialDashLength, scale);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000291 while (distance < length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 SkASSERT(dlen >= 0);
293 addedSegment = false;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000294 if (is_even(index) && dlen > 0 && !skipFirstSegment) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 addedSegment = true;
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000296 ++segCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000297
reed@google.com3ec68f02012-05-29 20:48:50 +0000298 if (specialLine) {
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000299 lineRec.addSegment(SkDoubleToScalar(distance),
300 SkDoubleToScalar(distance + dlen),
robertphillips@google.com441a0052012-12-14 13:55:06 +0000301 dst);
reed@google.com3ec68f02012-05-29 20:48:50 +0000302 } else {
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000303 meas.getSegment(SkDoubleToScalar(distance),
304 SkDoubleToScalar(distance + dlen),
robertphillips@google.com441a0052012-12-14 13:55:06 +0000305 dst, true);
reed@google.com3ec68f02012-05-29 20:48:50 +0000306 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307 }
308 distance += dlen;
309
310 // clear this so we only respect it the first time around
311 skipFirstSegment = false;
312
313 // wrap around our intervals array if necessary
314 index += 1;
315 SkASSERT(index <= fCount);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000316 if (index == fCount) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 index = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000318 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319
320 // fetch our next dlen
321 dlen = SkScalarMul(intervals[index], scale);
322 }
323
324 // extend if we ended on a segment and we need to join up with the (skipped) initial segment
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000325 if (meas.isClosed() && is_even(fInitialDashIndex) &&
326 fInitialDashLength > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000327 meas.getSegment(0, SkScalarMul(fInitialDashLength, scale), dst, !addedSegment);
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000328 ++segCount;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000329 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 } while (meas.nextContour());
reed@google.com3ec68f02012-05-29 20:48:50 +0000331
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000332 if (segCount > 1) {
333 dst->setConvexity(SkPath::kConcave_Convexity);
334 }
335
reed@android.com8a1c16f2008-12-17 15:59:43 +0000336 return true;
337}
338
robertphillips@google.com629ab542012-11-28 17:18:11 +0000339// Currently asPoints is more restrictive then it needs to be. In the future
340// we need to:
341// allow kRound_Cap capping (could allow rotations in the matrix with this)
robertphillips@google.com6d875572012-12-17 18:56:29 +0000342// allow paths to be returned
robertphillips@google.com629ab542012-11-28 17:18:11 +0000343bool SkDashPathEffect::asPoints(PointData* results,
344 const SkPath& src,
345 const SkStrokeRec& rec,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000346 const SkMatrix& matrix,
347 const SkRect* cullRect) const {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000348 // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out
349 if (fInitialDashLength < 0 || 0 >= rec.getWidth()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000350 return false;
351 }
352
robertphillips@google.com6d875572012-12-17 18:56:29 +0000353 // TODO: this next test could be eased up. We could allow any number of
354 // intervals as long as all the ons match and all the offs match.
355 // Additionally, they do not necessarily need to be integers.
356 // We cannot allow arbitrary intervals since we want the returned points
357 // to be uniformly sized.
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000358 if (fCount != 2 ||
robertphillips@google.com6d875572012-12-17 18:56:29 +0000359 !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) ||
360 !SkScalarIsInt(fIntervals[0]) ||
361 !SkScalarIsInt(fIntervals[1])) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000362 return false;
363 }
364
robertphillips@google.com6d875572012-12-17 18:56:29 +0000365 // TODO: this next test could be eased up. The rescaling should not impact
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000366 // the equality of the ons & offs. However, we would need to remove the
robertphillips@google.com6d875572012-12-17 18:56:29 +0000367 // integer intervals restriction first
368 if (fScaleToFit) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000369 return false;
370 }
371
372 SkPoint pts[2];
373
robertphillips@google.com6d875572012-12-17 18:56:29 +0000374 if (!src.isLine(pts)) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000375 return false;
376 }
377
robertphillips@google.com6d875572012-12-17 18:56:29 +0000378 // TODO: this test could be eased up to allow circles
robertphillips@google.com629ab542012-11-28 17:18:11 +0000379 if (SkPaint::kButt_Cap != rec.getCap()) {
380 return false;
381 }
382
robertphillips@google.com6d875572012-12-17 18:56:29 +0000383 // TODO: this test could be eased up for circles. Rotations could be allowed.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000384 if (!matrix.rectStaysRect()) {
385 return false;
386 }
387
robertphillips@google.com6d875572012-12-17 18:56:29 +0000388 SkScalar length = SkPoint::Distance(pts[1], pts[0]);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000389
robertphillips@google.com6d875572012-12-17 18:56:29 +0000390 SkVector tangent = pts[1] - pts[0];
391 if (tangent.isZero()) {
392 return false;
393 }
394
395 tangent.scale(SkScalarInvert(length));
396
397 // TODO: make this test for horizontal & vertical lines more robust
398 bool isXAxis = true;
399 if (SK_Scalar1 == tangent.fX || -SK_Scalar1 == tangent.fX) {
400 results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth()));
401 } else if (SK_Scalar1 == tangent.fY || -SK_Scalar1 == tangent.fY) {
402 results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0]));
403 isXAxis = false;
404 } else if (SkPaint::kRound_Cap != rec.getCap()) {
405 // Angled lines don't have axis-aligned boxes.
robertphillips@google.com629ab542012-11-28 17:18:11 +0000406 return false;
407 }
408
409 if (NULL != results) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000410 results->fFlags = 0;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000411 SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000412
robertphillips@google.com6d875572012-12-17 18:56:29 +0000413 if (SkPaint::kRound_Cap == rec.getCap()) {
414 results->fFlags |= PointData::kCircles_PointFlag;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000415 }
416
robertphillips@google.com6d875572012-12-17 18:56:29 +0000417 results->fNumPoints = 0;
418 SkScalar len2 = length;
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000419 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
420 SkASSERT(len2 >= clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000421 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000422 if (clampedInitialDashLength > 0) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000423 if (clampedInitialDashLength >= fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000424 ++results->fNumPoints; // partial first dash
425 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000426 len2 -= clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000427 }
428 len2 -= fIntervals[1]; // also skip first space
429 if (len2 < 0) {
430 len2 = 0;
431 }
432 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000433 len2 -= clampedInitialDashLength; // skip initial partial empty
robertphillips@google.com6d875572012-12-17 18:56:29 +0000434 }
435 }
436 int numMidPoints = SkScalarFloorToInt(SkScalarDiv(len2, fIntervalLength));
437 results->fNumPoints += numMidPoints;
438 len2 -= numMidPoints * fIntervalLength;
439 bool partialLast = false;
440 if (len2 > 0) {
441 if (len2 < fIntervals[0]) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000442 partialLast = true;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000443 } else {
444 ++numMidPoints;
445 ++results->fNumPoints;
446 }
447 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000448
robertphillips@google.com935ad022012-12-05 19:07:21 +0000449 results->fPoints = new SkPoint[results->fNumPoints];
robertphillips@google.com629ab542012-11-28 17:18:11 +0000450
robertphillips@google.com6d875572012-12-17 18:56:29 +0000451 SkScalar distance = 0;
452 int curPt = 0;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000453
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000454 if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) {
455 SkASSERT(clampedInitialDashLength <= length);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000456
robertphillips@google.com6d875572012-12-17 18:56:29 +0000457 if (0 == fInitialDashIndex) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000458 if (clampedInitialDashLength > 0) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000459 // partial first block
460 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000461 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, SkScalarHalf(clampedInitialDashLength));
462 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, SkScalarHalf(clampedInitialDashLength));
robertphillips@google.com6d875572012-12-17 18:56:29 +0000463 SkScalar halfWidth, halfHeight;
464 if (isXAxis) {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000465 halfWidth = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000466 halfHeight = SkScalarHalf(rec.getWidth());
467 } else {
468 halfWidth = SkScalarHalf(rec.getWidth());
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000469 halfHeight = SkScalarHalf(clampedInitialDashLength);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000470 }
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000471 if (clampedInitialDashLength < fIntervals[0]) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000472 // This one will not be like the others
473 results->fFirst.addRect(x - halfWidth, y - halfHeight,
474 x + halfWidth, y + halfHeight);
475 } else {
476 SkASSERT(curPt < results->fNumPoints);
477 results->fPoints[curPt].set(x, y);
478 ++curPt;
479 }
480
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000481 distance += clampedInitialDashLength;
robertphillips@google.com6d875572012-12-17 18:56:29 +0000482 }
483
484 distance += fIntervals[1]; // skip over the next blank block too
485 } else {
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000486 distance += clampedInitialDashLength;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000487 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000488 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000489
robertphillips@google.com6d875572012-12-17 18:56:29 +0000490 if (0 != numMidPoints) {
491 distance += SkScalarHalf(fIntervals[0]);
492
493 for (int i = 0; i < numMidPoints; ++i) {
494 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance);
495 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance);
496
497 SkASSERT(curPt < results->fNumPoints);
498 results->fPoints[curPt].set(x, y);
499 ++curPt;
500
501 distance += fIntervalLength;
502 }
503
504 distance -= SkScalarHalf(fIntervals[0]);
505 }
506
507 if (partialLast) {
508 // partial final block
509 SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles
510 SkScalar temp = length - distance;
511 SkASSERT(temp < fIntervals[0]);
512 SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance + SkScalarHalf(temp));
513 SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance + SkScalarHalf(temp));
514 SkScalar halfWidth, halfHeight;
515 if (isXAxis) {
516 halfWidth = SkScalarHalf(temp);
517 halfHeight = SkScalarHalf(rec.getWidth());
518 } else {
519 halfWidth = SkScalarHalf(rec.getWidth());
520 halfHeight = SkScalarHalf(temp);
521 }
522 results->fLast.addRect(x - halfWidth, y - halfHeight,
523 x + halfWidth, y + halfHeight);
524 }
525
526 SkASSERT(curPt == results->fNumPoints);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000527 }
528
529 return true;
530}
531
robertphillips@google.comc2eae472013-10-21 12:26:10 +0000532SkFlattenable::Factory SkDashPathEffect::getFactory() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533 return fInitialDashLength < 0 ? NULL : CreateProc;
534}
535
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000536void SkDashPathEffect::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000537 SkASSERT(fInitialDashLength >= 0);
538
djsollen@google.com54924242012-03-29 15:18:04 +0000539 this->INHERITED::flatten(buffer);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000540 buffer.writeInt(fInitialDashIndex);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000541 buffer.writeScalar(fInitialDashLength);
542 buffer.writeScalar(fIntervalLength);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000543 buffer.writeBool(fScaleToFit);
544 buffer.writeScalarArray(fIntervals, fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000545}
546
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000547SkFlattenable* SkDashPathEffect::CreateProc(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000548 return SkNEW_ARGS(SkDashPathEffect, (buffer));
549}
550
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000551SkDashPathEffect::SkDashPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000552 fInitialDashIndex = buffer.readInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000553 fInitialDashLength = buffer.readScalar();
554 fIntervalLength = buffer.readScalar();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000555 fScaleToFit = buffer.readBool();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000556
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000557 fCount = buffer.getArrayCount();
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +0000558 size_t allocSize = sizeof(SkScalar) * fCount;
559 if (buffer.validateAvailable(allocSize)) {
560 fIntervals = (SkScalar*)sk_malloc_throw(allocSize);
561 buffer.readScalarArray(fIntervals, fCount);
562 } else {
563 fIntervals = NULL;
564 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000565}