epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkDashPathEffect.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 11 | #include "SkFlattenableBuffers.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkPathMeasure.h" |
| 13 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 14 | static inline int is_even(int x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | return (~x) << 31; |
| 16 | } |
| 17 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 18 | static SkScalar FindFirstInterval(const SkScalar intervals[], SkScalar phase, |
reed@google.com | d9ee348 | 2012-08-06 14:58:35 +0000 | [diff] [blame] | 19 | int32_t* index, int count) { |
| 20 | for (int i = 0; i < count; ++i) { |
| 21 | if (phase > intervals[i]) { |
| 22 | phase -= intervals[i]; |
| 23 | } else { |
| 24 | *index = i; |
| 25 | return intervals[i] - phase; |
| 26 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 27 | } |
reed@google.com | d9ee348 | 2012-08-06 14:58:35 +0000 | [diff] [blame] | 28 | // If we get here, phase "appears" to be larger than our length. This |
| 29 | // shouldn't happen with perfect precision, but we can accumulate errors |
| 30 | // during the initial length computation (rounding can make our sum be too |
| 31 | // big or too small. In that event, we just have to eat the error here. |
| 32 | *index = 0; |
| 33 | return intervals[0]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | } |
| 35 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 36 | SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count, |
| 37 | SkScalar phase, bool scaleToFit) |
| 38 | : fScaleToFit(scaleToFit) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | SkASSERT(intervals); |
| 40 | SkASSERT(count > 1 && SkAlign2(count) == count); |
| 41 | |
| 42 | fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count); |
| 43 | fCount = count; |
| 44 | |
| 45 | SkScalar len = 0; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 46 | for (int i = 0; i < count; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | SkASSERT(intervals[i] >= 0); |
| 48 | fIntervals[i] = intervals[i]; |
| 49 | len += intervals[i]; |
| 50 | } |
| 51 | fIntervalLength = len; |
| 52 | |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 53 | // watch out for values that might make us go out of bounds |
| 54 | if ((len > 0) && SkScalarIsFinite(phase) && SkScalarIsFinite(len)) { |
| 55 | |
| 56 | // Adjust phase to be between 0 and len, "flipping" phase if negative. |
| 57 | // e.g., if len is 100, then phase of -20 (or -120) is equivalent to 80 |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 58 | if (phase < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | phase = -phase; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 60 | if (phase > len) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | phase = SkScalarMod(phase, len); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 62 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | phase = len - phase; |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 64 | |
| 65 | // Due to finite precision, it's possible that phase == len, |
| 66 | // even after the subtract (if len >>> phase), so fix that here. |
| 67 | // This fixes http://crbug.com/124652 . |
reed@google.com | 1df888b | 2012-04-24 22:47:21 +0000 | [diff] [blame] | 68 | SkASSERT(phase <= len); |
| 69 | if (phase == len) { |
| 70 | phase = 0; |
| 71 | } |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 72 | } else if (phase >= len) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | phase = SkScalarMod(phase, len); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 74 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | SkASSERT(phase >= 0 && phase < len); |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 76 | |
reed@google.com | d9ee348 | 2012-08-06 14:58:35 +0000 | [diff] [blame] | 77 | fInitialDashLength = FindFirstInterval(intervals, phase, |
| 78 | &fInitialDashIndex, count); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | |
| 80 | SkASSERT(fInitialDashLength >= 0); |
| 81 | SkASSERT(fInitialDashIndex >= 0 && fInitialDashIndex < fCount); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 82 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | fInitialDashLength = -1; // signal bad dash intervals |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 84 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | } |
| 86 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 87 | SkDashPathEffect::~SkDashPathEffect() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | sk_free(fIntervals); |
| 89 | } |
| 90 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 91 | class SpecialLineRec { |
| 92 | public: |
| 93 | bool init(const SkPath& src, SkPath* dst, SkStrokeRec* rec, |
| 94 | SkScalar pathLength, |
| 95 | int intervalCount, SkScalar intervalLength) { |
| 96 | if (rec->isHairlineStyle() || !src.isLine(fPts)) { |
| 97 | return false; |
| 98 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 99 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 100 | // can relax this in the future, if we handle square and round caps |
| 101 | if (SkPaint::kButt_Cap != rec->getCap()) { |
| 102 | return false; |
| 103 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 104 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 105 | fTangent = fPts[1] - fPts[0]; |
| 106 | if (fTangent.isZero()) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | fPathLength = pathLength; |
| 111 | fTangent.scale(SkScalarInvert(pathLength)); |
| 112 | fTangent.rotateCCW(&fNormal); |
| 113 | fNormal.scale(SkScalarHalf(rec->getWidth())); |
| 114 | |
| 115 | // now estimate how many quads will be added to the path |
| 116 | // resulting segments = pathLen * intervalCount / intervalLen |
| 117 | // resulting points = 4 * segments |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 118 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 119 | SkScalar ptCount = SkScalarMulDiv(pathLength, |
| 120 | SkIntToScalar(intervalCount), |
| 121 | intervalLength); |
| 122 | int n = SkScalarCeilToInt(ptCount) << 2; |
| 123 | dst->incReserve(n); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 124 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 125 | // we will take care of the stroking |
| 126 | rec->setFillStyle(); |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | void addSegment(SkScalar d0, SkScalar d1, SkPath* path) const { |
| 131 | SkASSERT(d0 < fPathLength); |
| 132 | // clamp the segment to our length |
| 133 | if (d1 > fPathLength) { |
| 134 | d1 = fPathLength; |
| 135 | } |
| 136 | |
| 137 | SkScalar x0 = fPts[0].fX + SkScalarMul(fTangent.fX, d0); |
| 138 | SkScalar x1 = fPts[0].fX + SkScalarMul(fTangent.fX, d1); |
| 139 | SkScalar y0 = fPts[0].fY + SkScalarMul(fTangent.fY, d0); |
| 140 | SkScalar y1 = fPts[0].fY + SkScalarMul(fTangent.fY, d1); |
| 141 | |
| 142 | SkPoint pts[4]; |
| 143 | pts[0].set(x0 + fNormal.fX, y0 + fNormal.fY); // moveTo |
| 144 | pts[1].set(x1 + fNormal.fX, y1 + fNormal.fY); // lineTo |
| 145 | pts[2].set(x1 - fNormal.fX, y1 - fNormal.fY); // lineTo |
| 146 | pts[3].set(x0 - fNormal.fX, y0 - fNormal.fY); // lineTo |
| 147 | |
| 148 | path->addPoly(pts, SK_ARRAY_COUNT(pts), false); |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | SkPoint fPts[2]; |
| 153 | SkVector fTangent; |
| 154 | SkVector fNormal; |
| 155 | SkScalar fPathLength; |
| 156 | }; |
| 157 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 158 | bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src, |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 159 | SkStrokeRec* rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | // we do nothing if the src wants to be filled, or if our dashlength is 0 |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 161 | if (rec->isFillStyle() || fInitialDashLength < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | return false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 163 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | |
| 165 | SkPathMeasure meas(src, false); |
| 166 | const SkScalar* intervals = fIntervals; |
fmalita@google.com | 6b18d24 | 2012-12-17 16:27:34 +0000 | [diff] [blame] | 167 | SkScalar dashCount = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 169 | SpecialLineRec lineRec; |
| 170 | const bool specialLine = lineRec.init(src, dst, rec, meas.getLength(), |
| 171 | fCount >> 1, fIntervalLength); |
| 172 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | do { |
| 174 | bool skipFirstSegment = meas.isClosed(); |
| 175 | bool addedSegment = false; |
| 176 | SkScalar length = meas.getLength(); |
| 177 | int index = fInitialDashIndex; |
| 178 | SkScalar scale = SK_Scalar1; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 179 | |
fmalita@google.com | 6b18d24 | 2012-12-17 16:27:34 +0000 | [diff] [blame] | 180 | // Since the path length / dash length ratio may be arbitrarily large, we can exert |
| 181 | // significant memory pressure while attempting to build the filtered path. To avoid this, |
| 182 | // we simply give up dashing beyond a certain threshold. |
| 183 | // |
| 184 | // The original bug report (http://crbug.com/165432) is based on a path yielding more than |
| 185 | // 90 million dash segments and crashing the memory allocator. A limit of 1 million |
| 186 | // segments seems reasonable: at 2 verbs per segment * 9 bytes per verb, this caps the |
| 187 | // maximum dash memory overhead at roughly 17MB per path. |
| 188 | static const SkScalar kMaxDashCount = 1000000; |
| 189 | dashCount += length * (fCount >> 1) / fIntervalLength; |
| 190 | if (dashCount > kMaxDashCount) { |
| 191 | dst->reset(); |
| 192 | return false; |
| 193 | } |
| 194 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 195 | if (fScaleToFit) { |
| 196 | if (fIntervalLength >= length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | scale = SkScalarDiv(length, fIntervalLength); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 198 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 199 | SkScalar div = SkScalarDiv(length, fIntervalLength); |
| 200 | int n = SkScalarFloor(div); |
| 201 | scale = SkScalarDiv(length, n * fIntervalLength); |
| 202 | } |
| 203 | } |
| 204 | |
fmalita@google.com | bfa0401 | 2012-12-12 22:13:58 +0000 | [diff] [blame] | 205 | // Using double precision to avoid looping indefinitely due to single precision rounding |
| 206 | // (for extreme path_length/dash_length ratios). See test_infinite_dash() unittest. |
| 207 | double distance = 0; |
| 208 | double dlen = SkScalarMul(fInitialDashLength, scale); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 210 | while (distance < length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 211 | SkASSERT(dlen >= 0); |
| 212 | addedSegment = false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 213 | if (is_even(index) && dlen > 0 && !skipFirstSegment) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | addedSegment = true; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 215 | |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 216 | if (specialLine) { |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 217 | lineRec.addSegment(SkDoubleToScalar(distance), |
| 218 | SkDoubleToScalar(distance + dlen), |
robertphillips@google.com | 441a005 | 2012-12-14 13:55:06 +0000 | [diff] [blame] | 219 | dst); |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 220 | } else { |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 221 | meas.getSegment(SkDoubleToScalar(distance), |
| 222 | SkDoubleToScalar(distance + dlen), |
robertphillips@google.com | 441a005 | 2012-12-14 13:55:06 +0000 | [diff] [blame] | 223 | dst, true); |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 224 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 225 | } |
| 226 | distance += dlen; |
| 227 | |
| 228 | // clear this so we only respect it the first time around |
| 229 | skipFirstSegment = false; |
| 230 | |
| 231 | // wrap around our intervals array if necessary |
| 232 | index += 1; |
| 233 | SkASSERT(index <= fCount); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 234 | if (index == fCount) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | index = 0; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 236 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | |
| 238 | // fetch our next dlen |
| 239 | dlen = SkScalarMul(intervals[index], scale); |
| 240 | } |
| 241 | |
| 242 | // extend if we ended on a segment and we need to join up with the (skipped) initial segment |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 243 | if (meas.isClosed() && is_even(fInitialDashIndex) && |
| 244 | fInitialDashLength > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | meas.getSegment(0, SkScalarMul(fInitialDashLength, scale), dst, !addedSegment); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 246 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | } while (meas.nextContour()); |
reed@google.com | 3ec68f0 | 2012-05-29 20:48:50 +0000 | [diff] [blame] | 248 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 252 | // Currently asPoints is more restrictive then it needs to be. In the future |
| 253 | // we need to: |
| 254 | // allow kRound_Cap capping (could allow rotations in the matrix with this) |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 255 | // allow paths to be returned |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 256 | bool SkDashPathEffect::asPoints(PointData* results, |
| 257 | const SkPath& src, |
| 258 | const SkStrokeRec& rec, |
| 259 | const SkMatrix& matrix) const { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 260 | // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out |
| 261 | if (fInitialDashLength < 0 || 0 >= rec.getWidth()) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 265 | // TODO: this next test could be eased up. We could allow any number of |
| 266 | // intervals as long as all the ons match and all the offs match. |
| 267 | // Additionally, they do not necessarily need to be integers. |
| 268 | // We cannot allow arbitrary intervals since we want the returned points |
| 269 | // to be uniformly sized. |
| 270 | if (fCount != 2 || |
| 271 | !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) || |
| 272 | !SkScalarIsInt(fIntervals[0]) || |
| 273 | !SkScalarIsInt(fIntervals[1])) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 274 | return false; |
| 275 | } |
| 276 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 277 | // TODO: this next test could be eased up. The rescaling should not impact |
| 278 | // the equality of the ons & offs. However, we would need to remove the |
| 279 | // integer intervals restriction first |
| 280 | if (fScaleToFit) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
| 284 | SkPoint pts[2]; |
| 285 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 286 | if (!src.isLine(pts)) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 287 | return false; |
| 288 | } |
| 289 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 290 | // TODO: this test could be eased up to allow circles |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 291 | if (SkPaint::kButt_Cap != rec.getCap()) { |
| 292 | return false; |
| 293 | } |
| 294 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 295 | // TODO: this test could be eased up for circles. Rotations could be allowed. |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 296 | if (!matrix.rectStaysRect()) { |
| 297 | return false; |
| 298 | } |
| 299 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 300 | SkScalar length = SkPoint::Distance(pts[1], pts[0]); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 301 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 302 | SkVector tangent = pts[1] - pts[0]; |
| 303 | if (tangent.isZero()) { |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | tangent.scale(SkScalarInvert(length)); |
| 308 | |
| 309 | // TODO: make this test for horizontal & vertical lines more robust |
| 310 | bool isXAxis = true; |
| 311 | if (SK_Scalar1 == tangent.fX || -SK_Scalar1 == tangent.fX) { |
| 312 | results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth())); |
| 313 | } else if (SK_Scalar1 == tangent.fY || -SK_Scalar1 == tangent.fY) { |
| 314 | results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0])); |
| 315 | isXAxis = false; |
| 316 | } else if (SkPaint::kRound_Cap != rec.getCap()) { |
| 317 | // Angled lines don't have axis-aligned boxes. |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | |
| 321 | if (NULL != results) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 322 | results->fFlags = 0; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 323 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 324 | if (SkPaint::kRound_Cap == rec.getCap()) { |
| 325 | results->fFlags |= PointData::kCircles_PointFlag; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 326 | } |
| 327 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 328 | results->fNumPoints = 0; |
| 329 | SkScalar len2 = length; |
| 330 | bool partialFirst = false; |
| 331 | if (fInitialDashLength > 0 || 0 == fInitialDashIndex) { |
| 332 | SkASSERT(len2 >= fInitialDashLength); |
| 333 | if (0 == fInitialDashIndex) { |
| 334 | if (fInitialDashLength > 0) { |
| 335 | partialFirst = true; |
| 336 | if (fInitialDashLength >= fIntervals[0]) { |
| 337 | ++results->fNumPoints; // partial first dash |
| 338 | } |
| 339 | len2 -= fInitialDashLength; |
| 340 | } |
| 341 | len2 -= fIntervals[1]; // also skip first space |
| 342 | if (len2 < 0) { |
| 343 | len2 = 0; |
| 344 | } |
| 345 | } else { |
| 346 | len2 -= fInitialDashLength; // skip initial partial empty |
| 347 | } |
| 348 | } |
| 349 | int numMidPoints = SkScalarFloorToInt(SkScalarDiv(len2, fIntervalLength)); |
| 350 | results->fNumPoints += numMidPoints; |
| 351 | len2 -= numMidPoints * fIntervalLength; |
| 352 | bool partialLast = false; |
| 353 | if (len2 > 0) { |
| 354 | if (len2 < fIntervals[0]) { |
| 355 | partialLast = true; |
| 356 | } else { |
| 357 | ++numMidPoints; |
| 358 | ++results->fNumPoints; |
| 359 | } |
| 360 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 361 | |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 362 | results->fPoints = new SkPoint[results->fNumPoints]; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 363 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 364 | SkScalar distance = 0; |
| 365 | int curPt = 0; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 366 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 367 | if (fInitialDashLength > 0 || 0 == fInitialDashIndex) { |
| 368 | SkASSERT(fInitialDashLength <= length); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 369 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 370 | if (0 == fInitialDashIndex) { |
| 371 | if (fInitialDashLength > 0) { |
| 372 | // partial first block |
| 373 | SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles |
| 374 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, SkScalarHalf(fInitialDashLength)); |
| 375 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, SkScalarHalf(fInitialDashLength)); |
| 376 | SkScalar halfWidth, halfHeight; |
| 377 | if (isXAxis) { |
| 378 | halfWidth = SkScalarHalf(fInitialDashLength); |
| 379 | halfHeight = SkScalarHalf(rec.getWidth()); |
| 380 | } else { |
| 381 | halfWidth = SkScalarHalf(rec.getWidth()); |
| 382 | halfHeight = SkScalarHalf(fInitialDashLength); |
| 383 | } |
| 384 | if (fInitialDashLength < fIntervals[0]) { |
| 385 | // This one will not be like the others |
| 386 | results->fFirst.addRect(x - halfWidth, y - halfHeight, |
| 387 | x + halfWidth, y + halfHeight); |
| 388 | } else { |
| 389 | SkASSERT(curPt < results->fNumPoints); |
| 390 | results->fPoints[curPt].set(x, y); |
| 391 | ++curPt; |
| 392 | } |
| 393 | |
| 394 | distance += fInitialDashLength; |
| 395 | } |
| 396 | |
| 397 | distance += fIntervals[1]; // skip over the next blank block too |
| 398 | } else { |
| 399 | distance += fInitialDashLength; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 400 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 401 | } |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 402 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame^] | 403 | if (0 != numMidPoints) { |
| 404 | distance += SkScalarHalf(fIntervals[0]); |
| 405 | |
| 406 | for (int i = 0; i < numMidPoints; ++i) { |
| 407 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance); |
| 408 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance); |
| 409 | |
| 410 | SkASSERT(curPt < results->fNumPoints); |
| 411 | results->fPoints[curPt].set(x, y); |
| 412 | ++curPt; |
| 413 | |
| 414 | distance += fIntervalLength; |
| 415 | } |
| 416 | |
| 417 | distance -= SkScalarHalf(fIntervals[0]); |
| 418 | } |
| 419 | |
| 420 | if (partialLast) { |
| 421 | // partial final block |
| 422 | SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles |
| 423 | SkScalar temp = length - distance; |
| 424 | SkASSERT(temp < fIntervals[0]); |
| 425 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance + SkScalarHalf(temp)); |
| 426 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance + SkScalarHalf(temp)); |
| 427 | SkScalar halfWidth, halfHeight; |
| 428 | if (isXAxis) { |
| 429 | halfWidth = SkScalarHalf(temp); |
| 430 | halfHeight = SkScalarHalf(rec.getWidth()); |
| 431 | } else { |
| 432 | halfWidth = SkScalarHalf(rec.getWidth()); |
| 433 | halfHeight = SkScalarHalf(temp); |
| 434 | } |
| 435 | results->fLast.addRect(x - halfWidth, y - halfHeight, |
| 436 | x + halfWidth, y + halfHeight); |
| 437 | } |
| 438 | |
| 439 | SkASSERT(curPt == results->fNumPoints); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | return true; |
| 443 | } |
| 444 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 445 | SkFlattenable::Factory SkDashPathEffect::getFactory() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 446 | return fInitialDashLength < 0 ? NULL : CreateProc; |
| 447 | } |
| 448 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 449 | void SkDashPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 450 | SkASSERT(fInitialDashLength >= 0); |
| 451 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 452 | this->INHERITED::flatten(buffer); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 453 | buffer.writeInt(fInitialDashIndex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 454 | buffer.writeScalar(fInitialDashLength); |
| 455 | buffer.writeScalar(fIntervalLength); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 456 | buffer.writeBool(fScaleToFit); |
| 457 | buffer.writeScalarArray(fIntervals, fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 458 | } |
| 459 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 460 | SkFlattenable* SkDashPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 461 | return SkNEW_ARGS(SkDashPathEffect, (buffer)); |
| 462 | } |
| 463 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 464 | SkDashPathEffect::SkDashPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
| 465 | fInitialDashIndex = buffer.readInt(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 466 | fInitialDashLength = buffer.readScalar(); |
| 467 | fIntervalLength = buffer.readScalar(); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 468 | fScaleToFit = buffer.readBool(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 469 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 470 | fCount = buffer.getArrayCount(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 471 | fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * fCount); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 472 | buffer.readScalarArray(fIntervals); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 473 | } |