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 "SkDiscretePathEffect.h" |
| 11 | #include "SkBuffer.h" |
| 12 | #include "SkPathMeasure.h" |
| 13 | #include "SkRandom.h" |
| 14 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 15 | static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | SkVector normal = tangent; |
| 17 | normal.rotateCCW(); |
| 18 | normal.setLength(scale); |
| 19 | *p += normal; |
| 20 | } |
| 21 | |
| 22 | |
| 23 | SkDiscretePathEffect::SkDiscretePathEffect(SkScalar segLength, SkScalar deviation) |
| 24 | : fSegLength(segLength), fPerterb(deviation) |
| 25 | { |
| 26 | } |
| 27 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 28 | bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src, |
reed@google.com | 9797272 | 2012-05-24 20:13:57 +0000 | [diff] [blame^] | 29 | SkStrokeRec* rec) { |
| 30 | bool doFill = rec->isFillStyle(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | |
| 32 | SkPathMeasure meas(src, doFill); |
| 33 | uint32_t seed = SkScalarRound(meas.getLength()); |
| 34 | SkRandom rand(seed ^ ((seed << 16) | (seed >> 16))); |
| 35 | SkScalar scale = fPerterb; |
| 36 | SkPoint p; |
| 37 | SkVector v; |
| 38 | |
| 39 | do { |
| 40 | SkScalar length = meas.getLength(); |
| 41 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 42 | if (fSegLength * (2 + doFill) > length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | meas.getSegment(0, length, dst, true); // to short for us to mangle |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 44 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | int n = SkScalarRound(SkScalarDiv(length, fSegLength)); |
| 46 | SkScalar delta = length / n; |
| 47 | SkScalar distance = 0; |
| 48 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 49 | if (meas.isClosed()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | n -= 1; |
| 51 | distance += delta/2; |
| 52 | } |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 53 | |
| 54 | if (meas.getPosTan(distance, &p, &v)) { |
| 55 | Perterb(&p, v, SkScalarMul(rand.nextSScalar1(), scale)); |
| 56 | dst->moveTo(p); |
| 57 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 58 | while (--n >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | distance += delta; |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 60 | if (meas.getPosTan(distance, &p, &v)) { |
| 61 | Perterb(&p, v, SkScalarMul(rand.nextSScalar1(), scale)); |
| 62 | dst->lineTo(p); |
| 63 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 65 | if (meas.isClosed()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | dst->close(); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 67 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | } |
| 69 | } while (meas.nextContour()); |
| 70 | return true; |
| 71 | } |
| 72 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 73 | void SkDiscretePathEffect::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 74 | this->INHERITED::flatten(buffer); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | buffer.writeScalar(fSegLength); |
| 76 | buffer.writeScalar(fPerterb); |
| 77 | } |
| 78 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 79 | SkDiscretePathEffect::SkDiscretePathEffect(SkFlattenableReadBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | fSegLength = buffer.readScalar(); |
| 81 | fPerterb = buffer.readScalar(); |
| 82 | } |
| 83 | |
reed@google.com | 6bac947 | 2011-06-21 19:24:00 +0000 | [diff] [blame] | 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 86 | SK_DEFINE_FLATTENABLE_REGISTRAR(SkDiscretePathEffect) |
reed@google.com | 6bac947 | 2011-06-21 19:24:00 +0000 | [diff] [blame] | 87 | |