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 | |
| 9 | #include "SkCornerPathEffect.h" |
| 10 | #include "SkPath.h" |
| 11 | #include "SkPoint.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
| 13 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
Mike Reed | 85fdbe2 | 2018-02-22 10:03:35 -0500 | [diff] [blame] | 15 | SkCornerPathEffect::SkCornerPathEffect(SkScalar radius) { |
| 16 | // check with ! to catch NaNs |
| 17 | if (!(radius > 0)) { |
| 18 | radius = 0; |
| 19 | } |
| 20 | fRadius = radius; |
| 21 | } |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 22 | SkCornerPathEffect::~SkCornerPathEffect() {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 24 | static bool ComputeStep(const SkPoint& a, const SkPoint& b, SkScalar radius, |
| 25 | SkPoint* step) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | SkScalar dist = SkPoint::Distance(a, b); |
| 27 | |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 28 | *step = b - a; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | if (dist <= radius * 2) { |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 30 | *step *= SK_ScalarHalf; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | return false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 32 | } else { |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 33 | *step *= radius / dist; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | return true; |
| 35 | } |
| 36 | } |
| 37 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 38 | bool SkCornerPathEffect::filterPath(SkPath* dst, const SkPath& src, |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 39 | SkStrokeRec*, const SkRect*) const { |
Mike Reed | 85fdbe2 | 2018-02-22 10:03:35 -0500 | [diff] [blame] | 40 | if (fRadius <= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | return false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 42 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | |
| 44 | SkPath::Iter iter(src, false); |
| 45 | SkPath::Verb verb, prevVerb = (SkPath::Verb)-1; |
| 46 | SkPoint pts[4]; |
| 47 | |
| 48 | bool closed; |
| 49 | SkPoint moveTo, lastCorner; |
| 50 | SkVector firstStep, step; |
| 51 | bool prevIsValid = true; |
| 52 | |
| 53 | // to avoid warnings |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 54 | step.set(0, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | moveTo.set(0, 0); |
| 56 | firstStep.set(0, 0); |
| 57 | lastCorner.set(0, 0); |
| 58 | |
| 59 | for (;;) { |
reed@google.com | 4a3b714 | 2012-05-16 17:16:46 +0000 | [diff] [blame] | 60 | switch (verb = iter.next(pts, false)) { |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 61 | case SkPath::kMove_Verb: |
| 62 | // close out the previous (open) contour |
| 63 | if (SkPath::kLine_Verb == prevVerb) { |
| 64 | dst->lineTo(lastCorner); |
| 65 | } |
| 66 | closed = iter.isClosedContour(); |
| 67 | if (closed) { |
| 68 | moveTo = pts[0]; |
| 69 | prevIsValid = false; |
| 70 | } else { |
| 71 | dst->moveTo(pts[0]); |
| 72 | prevIsValid = true; |
| 73 | } |
| 74 | break; |
| 75 | case SkPath::kLine_Verb: { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | bool drawSegment = ComputeStep(pts[0], pts[1], fRadius, &step); |
| 77 | // prev corner |
| 78 | if (!prevIsValid) { |
| 79 | dst->moveTo(moveTo + step); |
| 80 | prevIsValid = true; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 81 | } else { |
| 82 | dst->quadTo(pts[0].fX, pts[0].fY, pts[0].fX + step.fX, |
| 83 | pts[0].fY + step.fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | } |
| 85 | if (drawSegment) { |
| 86 | dst->lineTo(pts[1].fX - step.fX, pts[1].fY - step.fY); |
| 87 | } |
| 88 | lastCorner = pts[1]; |
| 89 | prevIsValid = true; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 90 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 92 | case SkPath::kQuad_Verb: |
| 93 | // TBD - just replicate the curve for now |
| 94 | if (!prevIsValid) { |
| 95 | dst->moveTo(pts[0]); |
| 96 | prevIsValid = true; |
| 97 | } |
| 98 | dst->quadTo(pts[1], pts[2]); |
| 99 | lastCorner = pts[2]; |
| 100 | firstStep.set(0, 0); |
| 101 | break; |
reed | 2b9445b | 2014-12-17 05:50:55 -0800 | [diff] [blame] | 102 | case SkPath::kConic_Verb: |
| 103 | // TBD - just replicate the curve for now |
| 104 | if (!prevIsValid) { |
| 105 | dst->moveTo(pts[0]); |
| 106 | prevIsValid = true; |
| 107 | } |
| 108 | dst->conicTo(pts[1], pts[2], iter.conicWeight()); |
| 109 | lastCorner = pts[2]; |
| 110 | firstStep.set(0, 0); |
| 111 | break; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 112 | case SkPath::kCubic_Verb: |
| 113 | if (!prevIsValid) { |
| 114 | dst->moveTo(pts[0]); |
| 115 | prevIsValid = true; |
| 116 | } |
| 117 | // TBD - just replicate the curve for now |
| 118 | dst->cubicTo(pts[1], pts[2], pts[3]); |
| 119 | lastCorner = pts[3]; |
| 120 | firstStep.set(0, 0); |
| 121 | break; |
| 122 | case SkPath::kClose_Verb: |
| 123 | if (firstStep.fX || firstStep.fY) { |
| 124 | dst->quadTo(lastCorner.fX, lastCorner.fY, |
| 125 | lastCorner.fX + firstStep.fX, |
| 126 | lastCorner.fY + firstStep.fY); |
| 127 | } |
| 128 | dst->close(); |
reed | 2b9445b | 2014-12-17 05:50:55 -0800 | [diff] [blame] | 129 | prevIsValid = false; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 130 | break; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 131 | case SkPath::kDone_Verb: |
reed | 2b9445b | 2014-12-17 05:50:55 -0800 | [diff] [blame] | 132 | if (prevIsValid) { |
| 133 | dst->lineTo(lastCorner); |
| 134 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 135 | goto DONE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | } |
| 137 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 138 | if (SkPath::kMove_Verb == prevVerb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | firstStep = step; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 140 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | prevVerb = verb; |
| 142 | } |
| 143 | DONE: |
| 144 | return true; |
| 145 | } |
| 146 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 147 | sk_sp<SkFlattenable> SkCornerPathEffect::CreateProc(SkReadBuffer& buffer) { |
| 148 | return SkCornerPathEffect::Make(buffer.readScalar()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 149 | } |
| 150 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 151 | void SkCornerPathEffect::flatten(SkWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | buffer.writeScalar(fRadius); |
| 153 | } |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 154 | |
| 155 | #ifndef SK_IGNORE_TO_STRING |
| 156 | void SkCornerPathEffect::toString(SkString* str) const { |
| 157 | str->appendf("SkCornerPathEffect: ("); |
| 158 | str->appendf("radius: %.2f", fRadius); |
| 159 | str->appendf(")"); |
| 160 | } |
| 161 | #endif |