Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2006 The Android Open Source Project |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #include "SkDashPathEffect.h" |
michael@0 | 11 | #include "SkReadBuffer.h" |
michael@0 | 12 | #include "SkWriteBuffer.h" |
michael@0 | 13 | #include "SkPathMeasure.h" |
michael@0 | 14 | |
michael@0 | 15 | static inline int is_even(int x) { |
michael@0 | 16 | return (~x) << 31; |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | static SkScalar FindFirstInterval(const SkScalar intervals[], SkScalar phase, |
michael@0 | 20 | int32_t* index, int count) { |
michael@0 | 21 | for (int i = 0; i < count; ++i) { |
michael@0 | 22 | if (phase > intervals[i]) { |
michael@0 | 23 | phase -= intervals[i]; |
michael@0 | 24 | } else { |
michael@0 | 25 | *index = i; |
michael@0 | 26 | return intervals[i] - phase; |
michael@0 | 27 | } |
michael@0 | 28 | } |
michael@0 | 29 | // If we get here, phase "appears" to be larger than our length. This |
michael@0 | 30 | // shouldn't happen with perfect precision, but we can accumulate errors |
michael@0 | 31 | // during the initial length computation (rounding can make our sum be too |
michael@0 | 32 | // big or too small. In that event, we just have to eat the error here. |
michael@0 | 33 | *index = 0; |
michael@0 | 34 | return intervals[0]; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count, |
michael@0 | 38 | SkScalar phase, bool scaleToFit) |
michael@0 | 39 | : fScaleToFit(scaleToFit) { |
michael@0 | 40 | SkASSERT(intervals); |
michael@0 | 41 | SkASSERT(count > 1 && SkAlign2(count) == count); |
michael@0 | 42 | |
michael@0 | 43 | fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count); |
michael@0 | 44 | fCount = count; |
michael@0 | 45 | |
michael@0 | 46 | SkScalar len = 0; |
michael@0 | 47 | for (int i = 0; i < count; i++) { |
michael@0 | 48 | SkASSERT(intervals[i] >= 0); |
michael@0 | 49 | fIntervals[i] = intervals[i]; |
michael@0 | 50 | len += intervals[i]; |
michael@0 | 51 | } |
michael@0 | 52 | fIntervalLength = len; |
michael@0 | 53 | |
michael@0 | 54 | // watch out for values that might make us go out of bounds |
michael@0 | 55 | if ((len > 0) && SkScalarIsFinite(phase) && SkScalarIsFinite(len)) { |
michael@0 | 56 | |
michael@0 | 57 | // Adjust phase to be between 0 and len, "flipping" phase if negative. |
michael@0 | 58 | // e.g., if len is 100, then phase of -20 (or -120) is equivalent to 80 |
michael@0 | 59 | if (phase < 0) { |
michael@0 | 60 | phase = -phase; |
michael@0 | 61 | if (phase > len) { |
michael@0 | 62 | phase = SkScalarMod(phase, len); |
michael@0 | 63 | } |
michael@0 | 64 | phase = len - phase; |
michael@0 | 65 | |
michael@0 | 66 | // Due to finite precision, it's possible that phase == len, |
michael@0 | 67 | // even after the subtract (if len >>> phase), so fix that here. |
michael@0 | 68 | // This fixes http://crbug.com/124652 . |
michael@0 | 69 | SkASSERT(phase <= len); |
michael@0 | 70 | if (phase == len) { |
michael@0 | 71 | phase = 0; |
michael@0 | 72 | } |
michael@0 | 73 | } else if (phase >= len) { |
michael@0 | 74 | phase = SkScalarMod(phase, len); |
michael@0 | 75 | } |
michael@0 | 76 | SkASSERT(phase >= 0 && phase < len); |
michael@0 | 77 | |
michael@0 | 78 | fInitialDashLength = FindFirstInterval(intervals, phase, |
michael@0 | 79 | &fInitialDashIndex, count); |
michael@0 | 80 | |
michael@0 | 81 | SkASSERT(fInitialDashLength >= 0); |
michael@0 | 82 | SkASSERT(fInitialDashIndex >= 0 && fInitialDashIndex < fCount); |
michael@0 | 83 | } else { |
michael@0 | 84 | fInitialDashLength = -1; // signal bad dash intervals |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | SkDashPathEffect::~SkDashPathEffect() { |
michael@0 | 89 | sk_free(fIntervals); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | static void outset_for_stroke(SkRect* rect, const SkStrokeRec& rec) { |
michael@0 | 93 | SkScalar radius = SkScalarHalf(rec.getWidth()); |
michael@0 | 94 | if (0 == radius) { |
michael@0 | 95 | radius = SK_Scalar1; // hairlines |
michael@0 | 96 | } |
michael@0 | 97 | if (SkPaint::kMiter_Join == rec.getJoin()) { |
michael@0 | 98 | radius = SkScalarMul(radius, rec.getMiter()); |
michael@0 | 99 | } |
michael@0 | 100 | rect->outset(radius, radius); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | // Only handles lines for now. If returns true, dstPath is the new (smaller) |
michael@0 | 104 | // path. If returns false, then dstPath parameter is ignored. |
michael@0 | 105 | static bool cull_path(const SkPath& srcPath, const SkStrokeRec& rec, |
michael@0 | 106 | const SkRect* cullRect, SkScalar intervalLength, |
michael@0 | 107 | SkPath* dstPath) { |
michael@0 | 108 | if (NULL == cullRect) { |
michael@0 | 109 | return false; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | SkPoint pts[2]; |
michael@0 | 113 | if (!srcPath.isLine(pts)) { |
michael@0 | 114 | return false; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | SkRect bounds = *cullRect; |
michael@0 | 118 | outset_for_stroke(&bounds, rec); |
michael@0 | 119 | |
michael@0 | 120 | SkScalar dx = pts[1].x() - pts[0].x(); |
michael@0 | 121 | SkScalar dy = pts[1].y() - pts[0].y(); |
michael@0 | 122 | |
michael@0 | 123 | // just do horizontal lines for now (lazy) |
michael@0 | 124 | if (dy) { |
michael@0 | 125 | return false; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | SkScalar minX = pts[0].fX; |
michael@0 | 129 | SkScalar maxX = pts[1].fX; |
michael@0 | 130 | |
michael@0 | 131 | if (maxX < bounds.fLeft || minX > bounds.fRight) { |
michael@0 | 132 | return false; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | if (dx < 0) { |
michael@0 | 136 | SkTSwap(minX, maxX); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | // Now we actually perform the chop, removing the excess to the left and |
michael@0 | 140 | // right of the bounds (keeping our new line "in phase" with the dash, |
michael@0 | 141 | // hence the (mod intervalLength). |
michael@0 | 142 | |
michael@0 | 143 | if (minX < bounds.fLeft) { |
michael@0 | 144 | minX = bounds.fLeft - SkScalarMod(bounds.fLeft - minX, |
michael@0 | 145 | intervalLength); |
michael@0 | 146 | } |
michael@0 | 147 | if (maxX > bounds.fRight) { |
michael@0 | 148 | maxX = bounds.fRight + SkScalarMod(maxX - bounds.fRight, |
michael@0 | 149 | intervalLength); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | SkASSERT(maxX >= minX); |
michael@0 | 153 | if (dx < 0) { |
michael@0 | 154 | SkTSwap(minX, maxX); |
michael@0 | 155 | } |
michael@0 | 156 | pts[0].fX = minX; |
michael@0 | 157 | pts[1].fX = maxX; |
michael@0 | 158 | |
michael@0 | 159 | dstPath->moveTo(pts[0]); |
michael@0 | 160 | dstPath->lineTo(pts[1]); |
michael@0 | 161 | return true; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | class SpecialLineRec { |
michael@0 | 165 | public: |
michael@0 | 166 | bool init(const SkPath& src, SkPath* dst, SkStrokeRec* rec, |
michael@0 | 167 | int intervalCount, SkScalar intervalLength) { |
michael@0 | 168 | if (rec->isHairlineStyle() || !src.isLine(fPts)) { |
michael@0 | 169 | return false; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | // can relax this in the future, if we handle square and round caps |
michael@0 | 173 | if (SkPaint::kButt_Cap != rec->getCap()) { |
michael@0 | 174 | return false; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | SkScalar pathLength = SkPoint::Distance(fPts[0], fPts[1]); |
michael@0 | 178 | |
michael@0 | 179 | fTangent = fPts[1] - fPts[0]; |
michael@0 | 180 | if (fTangent.isZero()) { |
michael@0 | 181 | return false; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | fPathLength = pathLength; |
michael@0 | 185 | fTangent.scale(SkScalarInvert(pathLength)); |
michael@0 | 186 | fTangent.rotateCCW(&fNormal); |
michael@0 | 187 | fNormal.scale(SkScalarHalf(rec->getWidth())); |
michael@0 | 188 | |
michael@0 | 189 | // now estimate how many quads will be added to the path |
michael@0 | 190 | // resulting segments = pathLen * intervalCount / intervalLen |
michael@0 | 191 | // resulting points = 4 * segments |
michael@0 | 192 | |
michael@0 | 193 | SkScalar ptCount = SkScalarMulDiv(pathLength, |
michael@0 | 194 | SkIntToScalar(intervalCount), |
michael@0 | 195 | intervalLength); |
michael@0 | 196 | int n = SkScalarCeilToInt(ptCount) << 2; |
michael@0 | 197 | dst->incReserve(n); |
michael@0 | 198 | |
michael@0 | 199 | // we will take care of the stroking |
michael@0 | 200 | rec->setFillStyle(); |
michael@0 | 201 | return true; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | void addSegment(SkScalar d0, SkScalar d1, SkPath* path) const { |
michael@0 | 205 | SkASSERT(d0 < fPathLength); |
michael@0 | 206 | // clamp the segment to our length |
michael@0 | 207 | if (d1 > fPathLength) { |
michael@0 | 208 | d1 = fPathLength; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | SkScalar x0 = fPts[0].fX + SkScalarMul(fTangent.fX, d0); |
michael@0 | 212 | SkScalar x1 = fPts[0].fX + SkScalarMul(fTangent.fX, d1); |
michael@0 | 213 | SkScalar y0 = fPts[0].fY + SkScalarMul(fTangent.fY, d0); |
michael@0 | 214 | SkScalar y1 = fPts[0].fY + SkScalarMul(fTangent.fY, d1); |
michael@0 | 215 | |
michael@0 | 216 | SkPoint pts[4]; |
michael@0 | 217 | pts[0].set(x0 + fNormal.fX, y0 + fNormal.fY); // moveTo |
michael@0 | 218 | pts[1].set(x1 + fNormal.fX, y1 + fNormal.fY); // lineTo |
michael@0 | 219 | pts[2].set(x1 - fNormal.fX, y1 - fNormal.fY); // lineTo |
michael@0 | 220 | pts[3].set(x0 - fNormal.fX, y0 - fNormal.fY); // lineTo |
michael@0 | 221 | |
michael@0 | 222 | path->addPoly(pts, SK_ARRAY_COUNT(pts), false); |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | private: |
michael@0 | 226 | SkPoint fPts[2]; |
michael@0 | 227 | SkVector fTangent; |
michael@0 | 228 | SkVector fNormal; |
michael@0 | 229 | SkScalar fPathLength; |
michael@0 | 230 | }; |
michael@0 | 231 | |
michael@0 | 232 | bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src, |
michael@0 | 233 | SkStrokeRec* rec, const SkRect* cullRect) const { |
michael@0 | 234 | // we do nothing if the src wants to be filled, or if our dashlength is 0 |
michael@0 | 235 | if (rec->isFillStyle() || fInitialDashLength < 0) { |
michael@0 | 236 | return false; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | const SkScalar* intervals = fIntervals; |
michael@0 | 240 | SkScalar dashCount = 0; |
michael@0 | 241 | int segCount = 0; |
michael@0 | 242 | |
michael@0 | 243 | SkPath cullPathStorage; |
michael@0 | 244 | const SkPath* srcPtr = &src; |
michael@0 | 245 | if (cull_path(src, *rec, cullRect, fIntervalLength, &cullPathStorage)) { |
michael@0 | 246 | srcPtr = &cullPathStorage; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | SpecialLineRec lineRec; |
michael@0 | 250 | bool specialLine = lineRec.init(*srcPtr, dst, rec, fCount >> 1, fIntervalLength); |
michael@0 | 251 | |
michael@0 | 252 | SkPathMeasure meas(*srcPtr, false); |
michael@0 | 253 | |
michael@0 | 254 | do { |
michael@0 | 255 | bool skipFirstSegment = meas.isClosed(); |
michael@0 | 256 | bool addedSegment = false; |
michael@0 | 257 | SkScalar length = meas.getLength(); |
michael@0 | 258 | int index = fInitialDashIndex; |
michael@0 | 259 | SkScalar scale = SK_Scalar1; |
michael@0 | 260 | |
michael@0 | 261 | // Since the path length / dash length ratio may be arbitrarily large, we can exert |
michael@0 | 262 | // significant memory pressure while attempting to build the filtered path. To avoid this, |
michael@0 | 263 | // we simply give up dashing beyond a certain threshold. |
michael@0 | 264 | // |
michael@0 | 265 | // The original bug report (http://crbug.com/165432) is based on a path yielding more than |
michael@0 | 266 | // 90 million dash segments and crashing the memory allocator. A limit of 1 million |
michael@0 | 267 | // segments seems reasonable: at 2 verbs per segment * 9 bytes per verb, this caps the |
michael@0 | 268 | // maximum dash memory overhead at roughly 17MB per path. |
michael@0 | 269 | static const SkScalar kMaxDashCount = 1000000; |
michael@0 | 270 | dashCount += length * (fCount >> 1) / fIntervalLength; |
michael@0 | 271 | if (dashCount > kMaxDashCount) { |
michael@0 | 272 | dst->reset(); |
michael@0 | 273 | return false; |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | if (fScaleToFit) { |
michael@0 | 277 | if (fIntervalLength >= length) { |
michael@0 | 278 | scale = SkScalarDiv(length, fIntervalLength); |
michael@0 | 279 | } else { |
michael@0 | 280 | SkScalar div = SkScalarDiv(length, fIntervalLength); |
michael@0 | 281 | int n = SkScalarFloorToInt(div); |
michael@0 | 282 | scale = SkScalarDiv(length, n * fIntervalLength); |
michael@0 | 283 | } |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | // Using double precision to avoid looping indefinitely due to single precision rounding |
michael@0 | 287 | // (for extreme path_length/dash_length ratios). See test_infinite_dash() unittest. |
michael@0 | 288 | double distance = 0; |
michael@0 | 289 | double dlen = SkScalarMul(fInitialDashLength, scale); |
michael@0 | 290 | |
michael@0 | 291 | while (distance < length) { |
michael@0 | 292 | SkASSERT(dlen >= 0); |
michael@0 | 293 | addedSegment = false; |
michael@0 | 294 | if (is_even(index) && dlen > 0 && !skipFirstSegment) { |
michael@0 | 295 | addedSegment = true; |
michael@0 | 296 | ++segCount; |
michael@0 | 297 | |
michael@0 | 298 | if (specialLine) { |
michael@0 | 299 | lineRec.addSegment(SkDoubleToScalar(distance), |
michael@0 | 300 | SkDoubleToScalar(distance + dlen), |
michael@0 | 301 | dst); |
michael@0 | 302 | } else { |
michael@0 | 303 | meas.getSegment(SkDoubleToScalar(distance), |
michael@0 | 304 | SkDoubleToScalar(distance + dlen), |
michael@0 | 305 | dst, true); |
michael@0 | 306 | } |
michael@0 | 307 | } |
michael@0 | 308 | distance += dlen; |
michael@0 | 309 | |
michael@0 | 310 | // clear this so we only respect it the first time around |
michael@0 | 311 | skipFirstSegment = false; |
michael@0 | 312 | |
michael@0 | 313 | // wrap around our intervals array if necessary |
michael@0 | 314 | index += 1; |
michael@0 | 315 | SkASSERT(index <= fCount); |
michael@0 | 316 | if (index == fCount) { |
michael@0 | 317 | index = 0; |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | // fetch our next dlen |
michael@0 | 321 | dlen = SkScalarMul(intervals[index], scale); |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | // extend if we ended on a segment and we need to join up with the (skipped) initial segment |
michael@0 | 325 | if (meas.isClosed() && is_even(fInitialDashIndex) && |
michael@0 | 326 | fInitialDashLength > 0) { |
michael@0 | 327 | meas.getSegment(0, SkScalarMul(fInitialDashLength, scale), dst, !addedSegment); |
michael@0 | 328 | ++segCount; |
michael@0 | 329 | } |
michael@0 | 330 | } while (meas.nextContour()); |
michael@0 | 331 | |
michael@0 | 332 | if (segCount > 1) { |
michael@0 | 333 | dst->setConvexity(SkPath::kConcave_Convexity); |
michael@0 | 334 | } |
michael@0 | 335 | |
michael@0 | 336 | return true; |
michael@0 | 337 | } |
michael@0 | 338 | |
michael@0 | 339 | // Currently asPoints is more restrictive then it needs to be. In the future |
michael@0 | 340 | // we need to: |
michael@0 | 341 | // allow kRound_Cap capping (could allow rotations in the matrix with this) |
michael@0 | 342 | // allow paths to be returned |
michael@0 | 343 | bool SkDashPathEffect::asPoints(PointData* results, |
michael@0 | 344 | const SkPath& src, |
michael@0 | 345 | const SkStrokeRec& rec, |
michael@0 | 346 | const SkMatrix& matrix, |
michael@0 | 347 | const SkRect* cullRect) const { |
michael@0 | 348 | // width < 0 -> fill && width == 0 -> hairline so requiring width > 0 rules both out |
michael@0 | 349 | if (fInitialDashLength < 0 || 0 >= rec.getWidth()) { |
michael@0 | 350 | return false; |
michael@0 | 351 | } |
michael@0 | 352 | |
michael@0 | 353 | // TODO: this next test could be eased up. We could allow any number of |
michael@0 | 354 | // intervals as long as all the ons match and all the offs match. |
michael@0 | 355 | // Additionally, they do not necessarily need to be integers. |
michael@0 | 356 | // We cannot allow arbitrary intervals since we want the returned points |
michael@0 | 357 | // to be uniformly sized. |
michael@0 | 358 | if (fCount != 2 || |
michael@0 | 359 | !SkScalarNearlyEqual(fIntervals[0], fIntervals[1]) || |
michael@0 | 360 | !SkScalarIsInt(fIntervals[0]) || |
michael@0 | 361 | !SkScalarIsInt(fIntervals[1])) { |
michael@0 | 362 | return false; |
michael@0 | 363 | } |
michael@0 | 364 | |
michael@0 | 365 | // TODO: this next test could be eased up. The rescaling should not impact |
michael@0 | 366 | // the equality of the ons & offs. However, we would need to remove the |
michael@0 | 367 | // integer intervals restriction first |
michael@0 | 368 | if (fScaleToFit) { |
michael@0 | 369 | return false; |
michael@0 | 370 | } |
michael@0 | 371 | |
michael@0 | 372 | SkPoint pts[2]; |
michael@0 | 373 | |
michael@0 | 374 | if (!src.isLine(pts)) { |
michael@0 | 375 | return false; |
michael@0 | 376 | } |
michael@0 | 377 | |
michael@0 | 378 | // TODO: this test could be eased up to allow circles |
michael@0 | 379 | if (SkPaint::kButt_Cap != rec.getCap()) { |
michael@0 | 380 | return false; |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | // TODO: this test could be eased up for circles. Rotations could be allowed. |
michael@0 | 384 | if (!matrix.rectStaysRect()) { |
michael@0 | 385 | return false; |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | SkScalar length = SkPoint::Distance(pts[1], pts[0]); |
michael@0 | 389 | |
michael@0 | 390 | SkVector tangent = pts[1] - pts[0]; |
michael@0 | 391 | if (tangent.isZero()) { |
michael@0 | 392 | return false; |
michael@0 | 393 | } |
michael@0 | 394 | |
michael@0 | 395 | tangent.scale(SkScalarInvert(length)); |
michael@0 | 396 | |
michael@0 | 397 | // TODO: make this test for horizontal & vertical lines more robust |
michael@0 | 398 | bool isXAxis = true; |
michael@0 | 399 | if (SK_Scalar1 == tangent.fX || -SK_Scalar1 == tangent.fX) { |
michael@0 | 400 | results->fSize.set(SkScalarHalf(fIntervals[0]), SkScalarHalf(rec.getWidth())); |
michael@0 | 401 | } else if (SK_Scalar1 == tangent.fY || -SK_Scalar1 == tangent.fY) { |
michael@0 | 402 | results->fSize.set(SkScalarHalf(rec.getWidth()), SkScalarHalf(fIntervals[0])); |
michael@0 | 403 | isXAxis = false; |
michael@0 | 404 | } else if (SkPaint::kRound_Cap != rec.getCap()) { |
michael@0 | 405 | // Angled lines don't have axis-aligned boxes. |
michael@0 | 406 | return false; |
michael@0 | 407 | } |
michael@0 | 408 | |
michael@0 | 409 | if (NULL != results) { |
michael@0 | 410 | results->fFlags = 0; |
michael@0 | 411 | SkScalar clampedInitialDashLength = SkMinScalar(length, fInitialDashLength); |
michael@0 | 412 | |
michael@0 | 413 | if (SkPaint::kRound_Cap == rec.getCap()) { |
michael@0 | 414 | results->fFlags |= PointData::kCircles_PointFlag; |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | results->fNumPoints = 0; |
michael@0 | 418 | SkScalar len2 = length; |
michael@0 | 419 | if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) { |
michael@0 | 420 | SkASSERT(len2 >= clampedInitialDashLength); |
michael@0 | 421 | if (0 == fInitialDashIndex) { |
michael@0 | 422 | if (clampedInitialDashLength > 0) { |
michael@0 | 423 | if (clampedInitialDashLength >= fIntervals[0]) { |
michael@0 | 424 | ++results->fNumPoints; // partial first dash |
michael@0 | 425 | } |
michael@0 | 426 | len2 -= clampedInitialDashLength; |
michael@0 | 427 | } |
michael@0 | 428 | len2 -= fIntervals[1]; // also skip first space |
michael@0 | 429 | if (len2 < 0) { |
michael@0 | 430 | len2 = 0; |
michael@0 | 431 | } |
michael@0 | 432 | } else { |
michael@0 | 433 | len2 -= clampedInitialDashLength; // skip initial partial empty |
michael@0 | 434 | } |
michael@0 | 435 | } |
michael@0 | 436 | int numMidPoints = SkScalarFloorToInt(SkScalarDiv(len2, fIntervalLength)); |
michael@0 | 437 | results->fNumPoints += numMidPoints; |
michael@0 | 438 | len2 -= numMidPoints * fIntervalLength; |
michael@0 | 439 | bool partialLast = false; |
michael@0 | 440 | if (len2 > 0) { |
michael@0 | 441 | if (len2 < fIntervals[0]) { |
michael@0 | 442 | partialLast = true; |
michael@0 | 443 | } else { |
michael@0 | 444 | ++numMidPoints; |
michael@0 | 445 | ++results->fNumPoints; |
michael@0 | 446 | } |
michael@0 | 447 | } |
michael@0 | 448 | |
michael@0 | 449 | results->fPoints = new SkPoint[results->fNumPoints]; |
michael@0 | 450 | |
michael@0 | 451 | SkScalar distance = 0; |
michael@0 | 452 | int curPt = 0; |
michael@0 | 453 | |
michael@0 | 454 | if (clampedInitialDashLength > 0 || 0 == fInitialDashIndex) { |
michael@0 | 455 | SkASSERT(clampedInitialDashLength <= length); |
michael@0 | 456 | |
michael@0 | 457 | if (0 == fInitialDashIndex) { |
michael@0 | 458 | if (clampedInitialDashLength > 0) { |
michael@0 | 459 | // partial first block |
michael@0 | 460 | SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles |
michael@0 | 461 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, SkScalarHalf(clampedInitialDashLength)); |
michael@0 | 462 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, SkScalarHalf(clampedInitialDashLength)); |
michael@0 | 463 | SkScalar halfWidth, halfHeight; |
michael@0 | 464 | if (isXAxis) { |
michael@0 | 465 | halfWidth = SkScalarHalf(clampedInitialDashLength); |
michael@0 | 466 | halfHeight = SkScalarHalf(rec.getWidth()); |
michael@0 | 467 | } else { |
michael@0 | 468 | halfWidth = SkScalarHalf(rec.getWidth()); |
michael@0 | 469 | halfHeight = SkScalarHalf(clampedInitialDashLength); |
michael@0 | 470 | } |
michael@0 | 471 | if (clampedInitialDashLength < fIntervals[0]) { |
michael@0 | 472 | // This one will not be like the others |
michael@0 | 473 | results->fFirst.addRect(x - halfWidth, y - halfHeight, |
michael@0 | 474 | x + halfWidth, y + halfHeight); |
michael@0 | 475 | } else { |
michael@0 | 476 | SkASSERT(curPt < results->fNumPoints); |
michael@0 | 477 | results->fPoints[curPt].set(x, y); |
michael@0 | 478 | ++curPt; |
michael@0 | 479 | } |
michael@0 | 480 | |
michael@0 | 481 | distance += clampedInitialDashLength; |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | distance += fIntervals[1]; // skip over the next blank block too |
michael@0 | 485 | } else { |
michael@0 | 486 | distance += clampedInitialDashLength; |
michael@0 | 487 | } |
michael@0 | 488 | } |
michael@0 | 489 | |
michael@0 | 490 | if (0 != numMidPoints) { |
michael@0 | 491 | distance += SkScalarHalf(fIntervals[0]); |
michael@0 | 492 | |
michael@0 | 493 | for (int i = 0; i < numMidPoints; ++i) { |
michael@0 | 494 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance); |
michael@0 | 495 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance); |
michael@0 | 496 | |
michael@0 | 497 | SkASSERT(curPt < results->fNumPoints); |
michael@0 | 498 | results->fPoints[curPt].set(x, y); |
michael@0 | 499 | ++curPt; |
michael@0 | 500 | |
michael@0 | 501 | distance += fIntervalLength; |
michael@0 | 502 | } |
michael@0 | 503 | |
michael@0 | 504 | distance -= SkScalarHalf(fIntervals[0]); |
michael@0 | 505 | } |
michael@0 | 506 | |
michael@0 | 507 | if (partialLast) { |
michael@0 | 508 | // partial final block |
michael@0 | 509 | SkASSERT(SkPaint::kRound_Cap != rec.getCap()); // can't handle partial circles |
michael@0 | 510 | SkScalar temp = length - distance; |
michael@0 | 511 | SkASSERT(temp < fIntervals[0]); |
michael@0 | 512 | SkScalar x = pts[0].fX + SkScalarMul(tangent.fX, distance + SkScalarHalf(temp)); |
michael@0 | 513 | SkScalar y = pts[0].fY + SkScalarMul(tangent.fY, distance + SkScalarHalf(temp)); |
michael@0 | 514 | SkScalar halfWidth, halfHeight; |
michael@0 | 515 | if (isXAxis) { |
michael@0 | 516 | halfWidth = SkScalarHalf(temp); |
michael@0 | 517 | halfHeight = SkScalarHalf(rec.getWidth()); |
michael@0 | 518 | } else { |
michael@0 | 519 | halfWidth = SkScalarHalf(rec.getWidth()); |
michael@0 | 520 | halfHeight = SkScalarHalf(temp); |
michael@0 | 521 | } |
michael@0 | 522 | results->fLast.addRect(x - halfWidth, y - halfHeight, |
michael@0 | 523 | x + halfWidth, y + halfHeight); |
michael@0 | 524 | } |
michael@0 | 525 | |
michael@0 | 526 | SkASSERT(curPt == results->fNumPoints); |
michael@0 | 527 | } |
michael@0 | 528 | |
michael@0 | 529 | return true; |
michael@0 | 530 | } |
michael@0 | 531 | |
michael@0 | 532 | SkFlattenable::Factory SkDashPathEffect::getFactory() const { |
michael@0 | 533 | return CreateProc; |
michael@0 | 534 | } |
michael@0 | 535 | |
michael@0 | 536 | void SkDashPathEffect::flatten(SkWriteBuffer& buffer) const { |
michael@0 | 537 | this->INHERITED::flatten(buffer); |
michael@0 | 538 | buffer.writeInt(fInitialDashIndex); |
michael@0 | 539 | buffer.writeScalar(fInitialDashLength); |
michael@0 | 540 | buffer.writeScalar(fIntervalLength); |
michael@0 | 541 | buffer.writeBool(fScaleToFit); |
michael@0 | 542 | buffer.writeScalarArray(fIntervals, fCount); |
michael@0 | 543 | } |
michael@0 | 544 | |
michael@0 | 545 | SkFlattenable* SkDashPathEffect::CreateProc(SkReadBuffer& buffer) { |
michael@0 | 546 | return SkNEW_ARGS(SkDashPathEffect, (buffer)); |
michael@0 | 547 | } |
michael@0 | 548 | |
michael@0 | 549 | SkDashPathEffect::SkDashPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) { |
michael@0 | 550 | fInitialDashIndex = buffer.readInt(); |
michael@0 | 551 | fInitialDashLength = buffer.readScalar(); |
michael@0 | 552 | fIntervalLength = buffer.readScalar(); |
michael@0 | 553 | fScaleToFit = buffer.readBool(); |
michael@0 | 554 | |
michael@0 | 555 | fCount = buffer.getArrayCount(); |
michael@0 | 556 | size_t allocSize = sizeof(SkScalar) * fCount; |
michael@0 | 557 | if (buffer.validateAvailable(allocSize)) { |
michael@0 | 558 | fIntervals = (SkScalar*)sk_malloc_throw(allocSize); |
michael@0 | 559 | buffer.readScalarArray(fIntervals, fCount); |
michael@0 | 560 | } else { |
michael@0 | 561 | fIntervals = NULL; |
michael@0 | 562 | } |
michael@0 | 563 | } |