epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkDashPathEffect.h" |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 9 | |
| 10 | #include "SkDashPathPriv.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
halcanary | 435657f | 2015-09-15 12:53:07 -0700 | [diff] [blame] | 13 | #include "SkStrokeRec.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
mtklein | 1c577cd | 2014-07-09 09:54:10 -0700 | [diff] [blame] | 15 | SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count, SkScalar phase) |
| 16 | : fPhase(0) |
bungeman | 6f0749c | 2016-03-18 05:10:23 -0700 | [diff] [blame^] | 17 | , fInitialDashLength(0) |
mtklein | 1c577cd | 2014-07-09 09:54:10 -0700 | [diff] [blame] | 18 | , fInitialDashIndex(0) |
| 19 | , fIntervalLength(0) { |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 20 | SkASSERT(intervals); |
| 21 | SkASSERT(count > 1 && SkAlign2(count) == count); |
| 22 | |
| 23 | fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count); |
| 24 | fCount = count; |
| 25 | for (int i = 0; i < count; i++) { |
bungeman | 6f0749c | 2016-03-18 05:10:23 -0700 | [diff] [blame^] | 26 | SkASSERT(intervals[i] >= 0); |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 27 | fIntervals[i] = intervals[i]; |
| 28 | } |
| 29 | |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 30 | // set the internal data members |
mtklein | 1c577cd | 2014-07-09 09:54:10 -0700 | [diff] [blame] | 31 | SkDashPath::CalcDashParameters(phase, fIntervals, fCount, |
| 32 | &fInitialDashLength, &fInitialDashIndex, &fIntervalLength, &fPhase); |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 33 | } |
| 34 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 35 | SkDashPathEffect::~SkDashPathEffect() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | sk_free(fIntervals); |
| 37 | } |
| 38 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 39 | bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src, |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 40 | SkStrokeRec* rec, const SkRect* cullRect) const { |
bungeman | 6f0749c | 2016-03-18 05:10:23 -0700 | [diff] [blame^] | 41 | return SkDashPath::FilterDashPath(dst, src, rec, cullRect, fIntervals, fCount, |
egdaniel | a22ea18 | 2014-06-11 06:51:51 -0700 | [diff] [blame] | 42 | fInitialDashLength, fInitialDashIndex, fIntervalLength); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | } |
| 44 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 45 | static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) { |
| 46 | SkScalar radius = SkScalarHalf(rec.getWidth()); |
| 47 | if (0 == radius) { |
| 48 | radius = SK_Scalar1; // hairlines |
| 49 | } |
| 50 | if (SkPaint::kMiter_Join == rec.getJoin()) { |
| 51 | radius = SkScalarMul(radius, rec.getMiter()); |
| 52 | } |
| 53 | rect->outset(radius, radius); |
| 54 | } |
| 55 | |
mtklein | 88fd0fb | 2014-12-01 06:56:38 -0800 | [diff] [blame] | 56 | // Attempt to trim the line to minimally cover the cull rect (currently |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 57 | // only works for horizontal and vertical lines). |
| 58 | // Return true if processing should continue; false otherwise. |
| 59 | static bool cull_line(SkPoint* pts, const SkStrokeRec& rec, |
| 60 | const SkMatrix& ctm, const SkRect* cullRect, |
| 61 | const SkScalar intervalLength) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | if (nullptr == cullRect) { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 63 | SkASSERT(false); // Shouldn't ever occur in practice |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | SkScalar dx = pts[1].x() - pts[0].x(); |
| 68 | SkScalar dy = pts[1].y() - pts[0].y(); |
| 69 | |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 70 | if ((dx && dy) || (!dx && !dy)) { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
| 74 | SkRect bounds = *cullRect; |
| 75 | outset_for_stroke(&bounds, rec); |
| 76 | |
| 77 | // cullRect is in device space while pts are in the local coordinate system |
| 78 | // defined by the ctm. We want our answer in the local coordinate system. |
| 79 | |
| 80 | SkASSERT(ctm.rectStaysRect()); |
| 81 | SkMatrix inv; |
| 82 | if (!ctm.invert(&inv)) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | inv.mapRect(&bounds); |
| 87 | |
| 88 | if (dx) { |
| 89 | SkASSERT(dx && !dy); |
| 90 | SkScalar minX = pts[0].fX; |
| 91 | SkScalar maxX = pts[1].fX; |
| 92 | |
| 93 | if (dx < 0) { |
| 94 | SkTSwap(minX, maxX); |
| 95 | } |
| 96 | |
| 97 | SkASSERT(minX < maxX); |
reed | ccc12ed | 2014-12-12 12:48:50 -0800 | [diff] [blame] | 98 | if (maxX <= bounds.fLeft || minX >= bounds.fRight) { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | |
| 102 | // Now we actually perform the chop, removing the excess to the left and |
| 103 | // right of the bounds (keeping our new line "in phase" with the dash, |
| 104 | // hence the (mod intervalLength). |
| 105 | |
| 106 | if (minX < bounds.fLeft) { |
| 107 | minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX, intervalLength); |
| 108 | } |
| 109 | if (maxX > bounds.fRight) { |
| 110 | maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight, intervalLength); |
| 111 | } |
| 112 | |
| 113 | SkASSERT(maxX > minX); |
| 114 | if (dx < 0) { |
| 115 | SkTSwap(minX, maxX); |
| 116 | } |
| 117 | pts[0].fX = minX; |
| 118 | pts[1].fX = maxX; |
| 119 | } else { |
| 120 | SkASSERT(dy && !dx); |
| 121 | SkScalar minY = pts[0].fY; |
| 122 | SkScalar maxY = pts[1].fY; |
| 123 | |
| 124 | if (dy < 0) { |
| 125 | SkTSwap(minY, maxY); |
| 126 | } |
| 127 | |
| 128 | SkASSERT(minY < maxY); |
reed | ccc12ed | 2014-12-12 12:48:50 -0800 | [diff] [blame] | 129 | if (maxY <= bounds.fTop || minY >= bounds.fBottom) { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
| 133 | // Now we actually perform the chop, removing the excess to the top and |
| 134 | // bottom of the bounds (keeping our new line "in phase" with the dash, |
| 135 | // hence the (mod intervalLength). |
| 136 | |
| 137 | if (minY < bounds.fTop) { |
| 138 | minY = bounds.fTop - SkScalarMod(bounds.fTop - minY, intervalLength); |
| 139 | } |
| 140 | if (maxY > bounds.fBottom) { |
| 141 | maxY = bounds.fBottom + SkScalarMod(maxY - bounds.fBottom, intervalLength); |
| 142 | } |
| 143 | |
| 144 | SkASSERT(maxY > minY); |
| 145 | if (dy < 0) { |
| 146 | SkTSwap(minY, maxY); |
| 147 | } |
| 148 | pts[0].fY = minY; |
| 149 | pts[1].fY = maxY; |
| 150 | } |
| 151 | |
| 152 | return true; |
| 153 | } |
| 154 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 155 | // Currently asPoints is more restrictive then it needs to be. In the future |
| 156 | // we need to: |
| 157 | // 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] | 158 | // allow paths to be returned |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 159 | bool SkDashPathEffect::asPoints(PointData* results, |
| 160 | const SkPath& src, |
| 161 | const SkStrokeRec& rec, |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 162 | const SkMatrix& matrix, |
| 163 | const SkRect* cullRect) const { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 164 | // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out |
bungeman | 6f0749c | 2016-03-18 05:10:23 -0700 | [diff] [blame^] | 165 | if (fInitialDashLength < 0 || 0 >= rec.getWidth()) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 169 | // TODO: this next test could be eased up. We could allow any number of |
| 170 | // intervals as long as all the ons match and all the offs match. |
| 171 | // Additionally, they do not necessarily need to be integers. |
| 172 | // We cannot allow arbitrary intervals since we want the returned points |
| 173 | // to be uniformly sized. |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 174 | if (fCount != 2 || |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 175 | !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) || |
| 176 | !SkScalarIsInt(fIntervals[0]) || |
| 177 | !SkScalarIsInt(fIntervals[1])) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 181 | SkPoint pts[2]; |
| 182 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 183 | if (!src.isLine(pts)) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 187 | // TODO: this test could be eased up to allow circles |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 188 | if (SkPaint::kButt_Cap != rec.getCap()) { |
| 189 | return false; |
| 190 | } |
| 191 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 192 | // 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] | 193 | if (!matrix.rectStaysRect()) { |
| 194 | return false; |
| 195 | } |
| 196 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 197 | // See if the line can be limited to something plausible. |
| 198 | if (!cull_line(pts, rec, matrix, cullRect, fIntervalLength)) { |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | SkScalar length = SkPoint::Distance(pts[1], pts[0]); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 203 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 204 | SkVector tangent = pts[1] - pts[0]; |
| 205 | if (tangent.isZero()) { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | tangent.scale(SkScalarInvert(length)); |
| 210 | |
| 211 | // TODO: make this test for horizontal & vertical lines more robust |
| 212 | bool isXAxis = true; |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 213 | if (SkScalarNearlyEqual(SK_Scalar1, tangent.fX) || |
| 214 | SkScalarNearlyEqual(-SK_Scalar1, tangent.fX)) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 215 | results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth())); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 216 | } else if (SkScalarNearlyEqual(SK_Scalar1, tangent.fY) || |
| 217 | SkScalarNearlyEqual(-SK_Scalar1, tangent.fY)) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 218 | results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0])); |
| 219 | isXAxis = false; |
| 220 | } else if (SkPaint::kRound_Cap != rec.getCap()) { |
| 221 | // Angled lines don't have axis-aligned boxes. |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 222 | return false; |
| 223 | } |
| 224 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 225 | if (results) { |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 226 | results->fFlags = 0; |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 227 | SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 228 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 229 | if (SkPaint::kRound_Cap == rec.getCap()) { |
| 230 | results->fFlags |= PointData::kCircles_PointFlag; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 231 | } |
| 232 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 233 | results->fNumPoints = 0; |
| 234 | SkScalar len2 = length; |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 235 | if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) { |
| 236 | SkASSERT(len2 >= clampedInitialDashLength); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 237 | if (0 == fInitialDashIndex) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 238 | if (clampedInitialDashLength > 0) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 239 | if (clampedInitialDashLength >= fIntervals[0]) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 240 | ++results->fNumPoints; // partial first dash |
| 241 | } |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 242 | len2 -= clampedInitialDashLength; |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 243 | } |
| 244 | len2 -= fIntervals[1]; // also skip first space |
| 245 | if (len2 < 0) { |
| 246 | len2 = 0; |
| 247 | } |
| 248 | } else { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 249 | len2 -= clampedInitialDashLength; // skip initial partial empty |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 252 | int numMidPoints = SkScalarFloorToInt(len2 / fIntervalLength); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 253 | results->fNumPoints += numMidPoints; |
| 254 | len2 -= numMidPoints * fIntervalLength; |
| 255 | bool partialLast = false; |
| 256 | if (len2 > 0) { |
| 257 | if (len2 < fIntervals[0]) { |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 258 | partialLast = true; |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 259 | } else { |
| 260 | ++numMidPoints; |
| 261 | ++results->fNumPoints; |
| 262 | } |
| 263 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 264 | |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 265 | results->fPoints = new SkPoint[results->fNumPoints]; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 266 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 267 | SkScalar distance = 0; |
| 268 | int curPt = 0; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 269 | |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 270 | if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) { |
| 271 | SkASSERT(clampedInitialDashLength <= length); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 272 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 273 | if (0 == fInitialDashIndex) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 274 | if (clampedInitialDashLength > 0) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 275 | // partial first block |
| 276 | SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 277 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, SkScalarHalf(clampedInitialDashLength)); |
| 278 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, SkScalarHalf(clampedInitialDashLength)); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 279 | SkScalar halfWidth, halfHeight; |
| 280 | if (isXAxis) { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 281 | halfWidth = SkScalarHalf(clampedInitialDashLength); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 282 | halfHeight = SkScalarHalf(rec.getWidth()); |
| 283 | } else { |
| 284 | halfWidth = SkScalarHalf(rec.getWidth()); |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 285 | halfHeight = SkScalarHalf(clampedInitialDashLength); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 286 | } |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 287 | if (clampedInitialDashLength < fIntervals[0]) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 288 | // This one will not be like the others |
| 289 | results->fFirst.addRect(x - halfWidth, y - halfHeight, |
| 290 | x + halfWidth, y + halfHeight); |
| 291 | } else { |
| 292 | SkASSERT(curPt < results->fNumPoints); |
| 293 | results->fPoints[curPt].set(x, y); |
| 294 | ++curPt; |
| 295 | } |
| 296 | |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 297 | distance += clampedInitialDashLength; |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | distance += fIntervals[1]; // skip over the next blank block too |
| 301 | } else { |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 302 | distance += clampedInitialDashLength; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 303 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 304 | } |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 305 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 306 | if (0 != numMidPoints) { |
| 307 | distance += SkScalarHalf(fIntervals[0]); |
| 308 | |
| 309 | for (int i = 0; i < numMidPoints; ++i) { |
| 310 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance); |
| 311 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance); |
| 312 | |
| 313 | SkASSERT(curPt < results->fNumPoints); |
| 314 | results->fPoints[curPt].set(x, y); |
| 315 | ++curPt; |
| 316 | |
| 317 | distance += fIntervalLength; |
| 318 | } |
| 319 | |
| 320 | distance -= SkScalarHalf(fIntervals[0]); |
| 321 | } |
| 322 | |
| 323 | if (partialLast) { |
| 324 | // partial final block |
| 325 | SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles |
| 326 | SkScalar temp = length - distance; |
| 327 | SkASSERT(temp < fIntervals[0]); |
| 328 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance + SkScalarHalf(temp)); |
| 329 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance + SkScalarHalf(temp)); |
| 330 | SkScalar halfWidth, halfHeight; |
| 331 | if (isXAxis) { |
| 332 | halfWidth = SkScalarHalf(temp); |
| 333 | halfHeight = SkScalarHalf(rec.getWidth()); |
| 334 | } else { |
| 335 | halfWidth = SkScalarHalf(rec.getWidth()); |
| 336 | halfHeight = SkScalarHalf(temp); |
| 337 | } |
| 338 | results->fLast.addRect(x - halfWidth, y - halfHeight, |
| 339 | x + halfWidth, y + halfHeight); |
| 340 | } |
| 341 | |
| 342 | SkASSERT(curPt == results->fNumPoints); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | return true; |
| 346 | } |
| 347 | |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 348 | SkPathEffect::DashType SkDashPathEffect::asADash(DashInfo* info) const { |
| 349 | if (info) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 350 | if (info->fCount >= fCount && info->fIntervals) { |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 351 | memcpy(info->fIntervals, fIntervals, fCount * sizeof(SkScalar)); |
| 352 | } |
| 353 | info->fCount = fCount; |
| 354 | info->fPhase = fPhase; |
| 355 | } |
| 356 | return kDash_DashType; |
| 357 | } |
| 358 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 359 | void SkDashPathEffect::flatten(SkWriteBuffer& buffer) const { |
commit-bot@chromium.org | aec1438 | 2014-04-22 15:21:18 +0000 | [diff] [blame] | 360 | buffer.writeScalar(fPhase); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 361 | buffer.writeScalarArray(fIntervals, fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | } |
| 363 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 364 | SkFlattenable* SkDashPathEffect::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 365 | const SkScalar phase = buffer.readScalar(); |
| 366 | uint32_t count = buffer.getArrayCount(); |
| 367 | SkAutoSTArray<32, SkScalar> intervals(count); |
| 368 | if (buffer.readScalarArray(intervals.get(), count)) { |
| 369 | return Create(intervals.get(), SkToInt(count), phase); |
| 370 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 371 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 372 | } |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 373 | |
| 374 | #ifndef SK_IGNORE_TO_STRING |
| 375 | void SkDashPathEffect::toString(SkString* str) const { |
| 376 | str->appendf("SkDashPathEffect: ("); |
| 377 | str->appendf("count: %d phase %.2f intervals: (", fCount, fPhase); |
| 378 | for (int i = 0; i < fCount; ++i) { |
| 379 | str->appendf("%.2f", fIntervals[i]); |
| 380 | if (i < fCount-1) { |
| 381 | str->appendf(", "); |
| 382 | } |
| 383 | } |
| 384 | str->appendf("))"); |
| 385 | } |
| 386 | #endif |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 387 | |
| 388 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 389 | |
| 390 | SkPathEffect* SkDashPathEffect::Create(const SkScalar intervals[], int count, SkScalar phase) { |
bungeman | 6f0749c | 2016-03-18 05:10:23 -0700 | [diff] [blame^] | 391 | if ((count < 2) || !SkIsAlign2(count)) { |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 392 | return nullptr; |
| 393 | } |
bungeman | 6f0749c | 2016-03-18 05:10:23 -0700 | [diff] [blame^] | 394 | for (int i = 0; i < count; i++) { |
| 395 | if (intervals[i] < 0) { |
| 396 | return nullptr; |
| 397 | } |
| 398 | } |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 399 | return new SkDashPathEffect(intervals, count, phase); |
| 400 | } |