OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkDashPathPriv.h" | 8 #include "SkDashPathPriv.h" |
9 #include "SkPathMeasure.h" | 9 #include "SkPathMeasure.h" |
10 #include "SkStrokeRec.h" | 10 #include "SkStrokeRec.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 } | 32 } |
33 | 33 |
34 void SkDashPath::CalcDashParameters(SkScalar phase, const SkScalar intervals[],
int32_t count, | 34 void SkDashPath::CalcDashParameters(SkScalar phase, const SkScalar intervals[],
int32_t count, |
35 SkScalar* initialDashLength, int32_t* initia
lDashIndex, | 35 SkScalar* initialDashLength, int32_t* initia
lDashIndex, |
36 SkScalar* intervalLength, SkScalar* adjusted
Phase) { | 36 SkScalar* intervalLength, SkScalar* adjusted
Phase) { |
37 SkScalar len = 0; | 37 SkScalar len = 0; |
38 for (int i = 0; i < count; i++) { | 38 for (int i = 0; i < count; i++) { |
39 len += intervals[i]; | 39 len += intervals[i]; |
40 } | 40 } |
41 *intervalLength = len; | 41 *intervalLength = len; |
42 | 42 // Adjust phase to be between 0 and len, "flipping" phase if negative. |
43 // watch out for values that might make us go out of bounds | 43 // e.g., if len is 100, then phase of -20 (or -120) is equivalent to 80 |
44 if ((len > 0) && SkScalarIsFinite(phase) && SkScalarIsFinite(len)) { | 44 if (adjustedPhase) { |
45 | 45 if (phase < 0) { |
46 // Adjust phase to be between 0 and len, "flipping" phase if negative. | 46 phase = -phase; |
47 // e.g., if len is 100, then phase of -20 (or -120) is equivalent to 80 | 47 if (phase > len) { |
48 if (adjustedPhase) { | |
49 if (phase < 0) { | |
50 phase = -phase; | |
51 if (phase > len) { | |
52 phase = SkScalarMod(phase, len); | |
53 } | |
54 phase = len - phase; | |
55 | |
56 // Due to finite precision, it's possible that phase == len, | |
57 // even after the subtract (if len >>> phase), so fix that here. | |
58 // This fixes http://crbug.com/124652 . | |
59 SkASSERT(phase <= len); | |
60 if (phase == len) { | |
61 phase = 0; | |
62 } | |
63 } else if (phase >= len) { | |
64 phase = SkScalarMod(phase, len); | 48 phase = SkScalarMod(phase, len); |
65 } | 49 } |
66 *adjustedPhase = phase; | 50 phase = len - phase; |
| 51 |
| 52 // Due to finite precision, it's possible that phase == len, |
| 53 // even after the subtract (if len >>> phase), so fix that here. |
| 54 // This fixes http://crbug.com/124652 . |
| 55 SkASSERT(phase <= len); |
| 56 if (phase == len) { |
| 57 phase = 0; |
| 58 } |
| 59 } else if (phase >= len) { |
| 60 phase = SkScalarMod(phase, len); |
67 } | 61 } |
68 SkASSERT(phase >= 0 && phase < len); | 62 *adjustedPhase = phase; |
| 63 } |
| 64 SkASSERT(phase >= 0 && phase < len); |
69 | 65 |
70 *initialDashLength = find_first_interval(intervals, phase, | 66 *initialDashLength = find_first_interval(intervals, phase, |
71 initialDashIndex, count); | 67 initialDashIndex, count); |
72 | 68 |
73 SkASSERT(*initialDashLength >= 0); | 69 SkASSERT(*initialDashLength >= 0); |
74 SkASSERT(*initialDashIndex >= 0 && *initialDashIndex < count); | 70 SkASSERT(*initialDashIndex >= 0 && *initialDashIndex < count); |
75 } else { | |
76 *initialDashLength = -1; // signal bad dash intervals | |
77 } | |
78 } | 71 } |
79 | 72 |
80 static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) { | 73 static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) { |
81 SkScalar radius = SkScalarHalf(rec.getWidth()); | 74 SkScalar radius = SkScalarHalf(rec.getWidth()); |
82 if (0 == radius) { | 75 if (0 == radius) { |
83 radius = SK_Scalar1; // hairlines | 76 radius = SK_Scalar1; // hairlines |
84 } | 77 } |
85 if (SkPaint::kMiter_Join == rec.getJoin()) { | 78 if (SkPaint::kMiter_Join == rec.getJoin()) { |
86 radius = SkScalarMul(radius, rec.getMiter()); | 79 radius = SkScalarMul(radius, rec.getMiter()); |
87 } | 80 } |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 } | 205 } |
213 | 206 |
214 private: | 207 private: |
215 SkPoint fPts[2]; | 208 SkPoint fPts[2]; |
216 SkVector fTangent; | 209 SkVector fTangent; |
217 SkVector fNormal; | 210 SkVector fNormal; |
218 SkScalar fPathLength; | 211 SkScalar fPathLength; |
219 }; | 212 }; |
220 | 213 |
221 | 214 |
222 bool SkDashPath::FilterDashPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec
, | 215 bool SkDashPath::InternalFilter(SkPath* dst, const SkPath& src, SkStrokeRec* rec
, |
223 const SkRect* cullRect, const SkScalar aInterval
s[], | 216 const SkRect* cullRect, const SkScalar aInterval
s[], |
224 int32_t count, SkScalar initialDashLength, int32
_t initialDashIndex, | 217 int32_t count, SkScalar initialDashLength, int32
_t initialDashIndex, |
225 SkScalar intervalLength) { | 218 SkScalar intervalLength) { |
226 | 219 |
227 // we do nothing if the src wants to be filled, or if our dashlength is 0 | 220 // we do nothing if the src wants to be filled |
228 if (rec->isFillStyle() || initialDashLength < 0) { | 221 if (rec->isFillStyle()) { |
229 return false; | 222 return false; |
230 } | 223 } |
231 | 224 |
232 const SkScalar* intervals = aIntervals; | 225 const SkScalar* intervals = aIntervals; |
233 SkScalar dashCount = 0; | 226 SkScalar dashCount = 0; |
234 int segCount = 0; | 227 int segCount = 0; |
235 | 228 |
236 SkPath cullPathStorage; | 229 SkPath cullPathStorage; |
237 const SkPath* srcPtr = &src; | 230 const SkPath* srcPtr = &src; |
238 if (cull_path(src, *rec, cullRect, intervalLength, &cullPathStorage)) { | 231 if (cull_path(src, *rec, cullRect, intervalLength, &cullPathStorage)) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 if (index == count) { | 291 if (index == count) { |
299 index = 0; | 292 index = 0; |
300 } | 293 } |
301 | 294 |
302 // fetch our next dlen | 295 // fetch our next dlen |
303 dlen = intervals[index]; | 296 dlen = intervals[index]; |
304 } | 297 } |
305 | 298 |
306 // extend if we ended on a segment and we need to join up with the (skip
ped) initial segment | 299 // extend if we ended on a segment and we need to join up with the (skip
ped) initial segment |
307 if (meas.isClosed() && is_even(initialDashIndex) && | 300 if (meas.isClosed() && is_even(initialDashIndex) && |
308 initialDashLength > 0) { | 301 initialDashLength >= 0) { |
309 meas.getSegment(0, initialDashLength, dst, !addedSegment); | 302 meas.getSegment(0, initialDashLength, dst, !addedSegment); |
310 ++segCount; | 303 ++segCount; |
311 } | 304 } |
312 } while (meas.nextContour()); | 305 } while (meas.nextContour()); |
313 | 306 |
314 if (segCount > 1) { | 307 if (segCount > 1) { |
315 dst->setConvexity(SkPath::kConcave_Convexity); | 308 dst->setConvexity(SkPath::kConcave_Convexity); |
316 } | 309 } |
317 | 310 |
318 return true; | 311 return true; |
319 } | 312 } |
320 | 313 |
321 bool SkDashPath::FilterDashPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec
, | 314 bool SkDashPath::FilterDashPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec
, |
322 const SkRect* cullRect, const SkPathEffect::Dash
Info& info) { | 315 const SkRect* cullRect, const SkPathEffect::Dash
Info& info) { |
| 316 if (!ValidDashPath(info.fPhase, info.fIntervals, info.fCount)) { |
| 317 return false; |
| 318 } |
323 SkScalar initialDashLength = 0; | 319 SkScalar initialDashLength = 0; |
324 int32_t initialDashIndex = 0; | 320 int32_t initialDashIndex = 0; |
325 SkScalar intervalLength = 0; | 321 SkScalar intervalLength = 0; |
326 CalcDashParameters(info.fPhase, info.fIntervals, info.fCount, | 322 CalcDashParameters(info.fPhase, info.fIntervals, info.fCount, |
327 &initialDashLength, &initialDashIndex, &intervalLength); | 323 &initialDashLength, &initialDashIndex, &intervalLength); |
328 return FilterDashPath(dst, src, rec, cullRect, info.fIntervals, info.fCount,
initialDashLength, | 324 return InternalFilter(dst, src, rec, cullRect, info.fIntervals, info.fCount,
initialDashLength, |
329 initialDashIndex, intervalLength); | 325 initialDashIndex, intervalLength); |
330 } | 326 } |
| 327 |
| 328 bool SkDashPath::ValidDashPath(SkScalar phase, const SkScalar intervals[], int32
_t count) { |
| 329 if (count < 2 || !SkIsAlign2(count)) { |
| 330 return false; |
| 331 } |
| 332 SkScalar length = 0; |
| 333 for (int i = 0; i < count; i++) { |
| 334 if (intervals[i] < 0) { |
| 335 return false; |
| 336 } |
| 337 length += intervals[i]; |
| 338 } |
| 339 // watch out for values that might make us go out of bounds |
| 340 return length > 0 && SkScalarIsFinite(phase) && SkScalarIsFinite(length); |
| 341 } |
OLD | NEW |