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 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 10 | #include "SkBuffer.h" |
humper@google.com | 75e3ca1 | 2013-04-08 21:44:11 +0000 | [diff] [blame] | 11 | #include "SkErrorInternals.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkMath.h" |
humper@google.com | 75e3ca1 | 2013-04-08 21:44:11 +0000 | [diff] [blame] | 13 | #include "SkPath.h" |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 14 | #include "SkPathRef.h" |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 15 | #include "SkRRect.h" |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 16 | #include "SkThread.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
| 18 | //////////////////////////////////////////////////////////////////////////// |
| 19 | |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 20 | /** |
| 21 | * Path.bounds is defined to be the bounds of all the control points. |
| 22 | * If we called bounds.join(r) we would skip r if r was empty, which breaks |
| 23 | * our promise. Hence we have a custom joiner that doesn't look at emptiness |
| 24 | */ |
| 25 | static void joinNoEmptyChecks(SkRect* dst, const SkRect& src) { |
| 26 | dst->fLeft = SkMinScalar(dst->fLeft, src.fLeft); |
| 27 | dst->fTop = SkMinScalar(dst->fTop, src.fTop); |
| 28 | dst->fRight = SkMaxScalar(dst->fRight, src.fRight); |
| 29 | dst->fBottom = SkMaxScalar(dst->fBottom, src.fBottom); |
| 30 | } |
| 31 | |
bsalomon@google.com | fb6f0f6 | 2012-01-31 18:44:34 +0000 | [diff] [blame] | 32 | static bool is_degenerate(const SkPath& path) { |
| 33 | SkPath::Iter iter(path, false); |
| 34 | SkPoint pts[4]; |
| 35 | return SkPath::kDone_Verb == iter.next(pts); |
| 36 | } |
| 37 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 38 | class SkAutoDisableDirectionCheck { |
| 39 | public: |
| 40 | SkAutoDisableDirectionCheck(SkPath* path) : fPath(path) { |
| 41 | fSaved = static_cast<SkPath::Direction>(fPath->fDirection); |
| 42 | } |
| 43 | |
| 44 | ~SkAutoDisableDirectionCheck() { |
| 45 | fPath->fDirection = fSaved; |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | SkPath* fPath; |
| 50 | SkPath::Direction fSaved; |
| 51 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 52 | #define SkAutoDisableDirectionCheck(...) SK_REQUIRE_LOCAL_VAR(SkAutoDisableDirectionCheck) |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 53 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | /* This guy's constructor/destructor bracket a path editing operation. It is |
| 55 | used when we know the bounds of the amount we are going to add to the path |
| 56 | (usually a new contour, but not required). |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 57 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | It captures some state about the path up front (i.e. if it already has a |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 59 | cached bounds), and then if it can, it updates the cache bounds explicitly, |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 60 | avoiding the need to revisit all of the points in getBounds(). |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 61 | |
bsalomon@google.com | fb6f0f6 | 2012-01-31 18:44:34 +0000 | [diff] [blame] | 62 | It also notes if the path was originally degenerate, and if so, sets |
| 63 | isConvex to true. Thus it can only be used if the contour being added is |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 64 | convex. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | */ |
| 66 | class SkAutoPathBoundsUpdate { |
| 67 | public: |
| 68 | SkAutoPathBoundsUpdate(SkPath* path, const SkRect& r) : fRect(r) { |
| 69 | this->init(path); |
| 70 | } |
| 71 | |
| 72 | SkAutoPathBoundsUpdate(SkPath* path, SkScalar left, SkScalar top, |
| 73 | SkScalar right, SkScalar bottom) { |
| 74 | fRect.set(left, top, right, bottom); |
| 75 | this->init(path); |
| 76 | } |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 77 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | ~SkAutoPathBoundsUpdate() { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 79 | fPath->setConvexity(fDegenerate ? SkPath::kConvex_Convexity |
| 80 | : SkPath::kUnknown_Convexity); |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 81 | if (fEmpty || fHasValidBounds) { |
| 82 | fPath->setBounds(fRect); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 85 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | private: |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 87 | SkPath* fPath; |
| 88 | SkRect fRect; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 89 | bool fHasValidBounds; |
bsalomon@google.com | fb6f0f6 | 2012-01-31 18:44:34 +0000 | [diff] [blame] | 90 | bool fDegenerate; |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 91 | bool fEmpty; |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 92 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 93 | void init(SkPath* path) { |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 94 | // Cannot use fRect for our bounds unless we know it is sorted |
| 95 | fRect.sort(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | fPath = path; |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 97 | // Mark the path's bounds as dirty if (1) they are, or (2) the path |
| 98 | // is non-finite, and therefore its bounds are not meaningful |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 99 | fHasValidBounds = path->hasComputedBounds() && path->isFinite(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | fEmpty = path->isEmpty(); |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 101 | if (fHasValidBounds && !fEmpty) { |
| 102 | joinNoEmptyChecks(&fRect, fPath->getBounds()); |
| 103 | } |
| 104 | fDegenerate = is_degenerate(*path); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | } |
| 106 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 107 | #define SkAutoPathBoundsUpdate(...) SK_REQUIRE_LOCAL_VAR(SkAutoPathBoundsUpdate) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | //////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | /* |
| 112 | Stores the verbs and points as they are given to us, with exceptions: |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 113 | - we only record "Close" if it was immediately preceeded by Move | Line | Quad | Cubic |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | - we insert a Move(0,0) if Line | Quad | Cubic is our first command |
| 115 | |
| 116 | The iterator does more cleanup, especially if forceClose == true |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 117 | 1. If we encounter degenerate segments, remove them |
| 118 | 2. if we encounter Close, return a cons'd up Line() first (if the curr-pt != start-pt) |
| 119 | 3. if we encounter Move without a preceeding Close, and forceClose is true, goto #2 |
| 120 | 4. if we encounter Line | Quad | Cubic after Close, cons up a Move |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | */ |
| 122 | |
| 123 | //////////////////////////////////////////////////////////////////////////// |
| 124 | |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 125 | // flag to require a moveTo if we begin with something else, like lineTo etc. |
| 126 | #define INITIAL_LASTMOVETOINDEX_VALUE ~0 |
| 127 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 128 | SkPath::SkPath() |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 129 | : fPathRef(SkPathRef::CreateEmpty()) |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 130 | #ifdef SK_BUILD_FOR_ANDROID |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 131 | , fSourcePath(NULL) |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 132 | #endif |
| 133 | { |
| 134 | this->resetFields(); |
| 135 | } |
| 136 | |
| 137 | void SkPath::resetFields() { |
| 138 | //fPathRef is assumed to have been emptied by the caller. |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 139 | fLastMoveToIndex = INITIAL_LASTMOVETOINDEX_VALUE; |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 140 | fFillType = kWinding_FillType; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 141 | fConvexity = kUnknown_Convexity; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 142 | fDirection = kUnknown_Direction; |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 143 | |
| 144 | // We don't touch Android's fSourcePath. It's used to track texture garbage collection, so we |
| 145 | // don't want to muck with it if it's been set to something non-NULL. |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 146 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 148 | SkPath::SkPath(const SkPath& that) |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 149 | : fPathRef(SkRef(that.fPathRef.get())) { |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 150 | this->copyFields(that); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 151 | #ifdef SK_BUILD_FOR_ANDROID |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 152 | fSourcePath = that.fSourcePath; |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 153 | #endif |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 154 | SkDEBUGCODE(that.validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | SkPath::~SkPath() { |
| 158 | SkDEBUGCODE(this->validate();) |
| 159 | } |
| 160 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 161 | SkPath& SkPath::operator=(const SkPath& that) { |
| 162 | SkDEBUGCODE(that.validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 164 | if (this != &that) { |
| 165 | fPathRef.reset(SkRef(that.fPathRef.get())); |
| 166 | this->copyFields(that); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 167 | #ifdef SK_BUILD_FOR_ANDROID |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 168 | fSourcePath = that.fSourcePath; |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 169 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | } |
| 171 | SkDEBUGCODE(this->validate();) |
| 172 | return *this; |
| 173 | } |
| 174 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 175 | void SkPath::copyFields(const SkPath& that) { |
| 176 | //fPathRef is assumed to have been set by the caller. |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 177 | fLastMoveToIndex = that.fLastMoveToIndex; |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 178 | fFillType = that.fFillType; |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 179 | fConvexity = that.fConvexity; |
| 180 | fDirection = that.fDirection; |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 181 | } |
| 182 | |
djsollen@google.com | 9c1a967 | 2013-08-09 13:49:13 +0000 | [diff] [blame] | 183 | bool operator==(const SkPath& a, const SkPath& b) { |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 184 | // note: don't need to look at isConvex or bounds, since just comparing the |
| 185 | // raw data is sufficient. |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 186 | return &a == &b || |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 187 | (a.fFillType == b.fFillType && *a.fPathRef.get() == *b.fPathRef.get()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 188 | } |
| 189 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 190 | void SkPath::swap(SkPath& that) { |
| 191 | SkASSERT(&that != NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 193 | if (this != &that) { |
| 194 | fPathRef.swap(&that.fPathRef); |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 195 | SkTSwap<int>(fLastMoveToIndex, that.fLastMoveToIndex); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 196 | SkTSwap<uint8_t>(fFillType, that.fFillType); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 197 | SkTSwap<uint8_t>(fConvexity, that.fConvexity); |
| 198 | SkTSwap<uint8_t>(fDirection, that.fDirection); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 199 | #ifdef SK_BUILD_FOR_ANDROID |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 200 | SkTSwap<const SkPath*>(fSourcePath, that.fSourcePath); |
| 201 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 205 | static inline bool check_edge_against_rect(const SkPoint& p0, |
| 206 | const SkPoint& p1, |
| 207 | const SkRect& rect, |
| 208 | SkPath::Direction dir) { |
| 209 | const SkPoint* edgeBegin; |
| 210 | SkVector v; |
| 211 | if (SkPath::kCW_Direction == dir) { |
| 212 | v = p1 - p0; |
| 213 | edgeBegin = &p0; |
| 214 | } else { |
| 215 | v = p0 - p1; |
| 216 | edgeBegin = &p1; |
| 217 | } |
| 218 | if (v.fX || v.fY) { |
| 219 | // check the cross product of v with the vec from edgeBegin to each rect corner |
| 220 | SkScalar yL = SkScalarMul(v.fY, rect.fLeft - edgeBegin->fX); |
| 221 | SkScalar xT = SkScalarMul(v.fX, rect.fTop - edgeBegin->fY); |
| 222 | SkScalar yR = SkScalarMul(v.fY, rect.fRight - edgeBegin->fX); |
| 223 | SkScalar xB = SkScalarMul(v.fX, rect.fBottom - edgeBegin->fY); |
| 224 | if ((xT < yL) || (xT < yR) || (xB < yL) || (xB < yR)) { |
| 225 | return false; |
| 226 | } |
| 227 | } |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | bool SkPath::conservativelyContainsRect(const SkRect& rect) const { |
| 232 | // This only handles non-degenerate convex paths currently. |
| 233 | if (kConvex_Convexity != this->getConvexity()) { |
| 234 | return false; |
| 235 | } |
skia.committer@gmail.com | cec8de6 | 2012-11-14 02:01:22 +0000 | [diff] [blame] | 236 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 237 | Direction direction; |
| 238 | if (!this->cheapComputeDirection(&direction)) { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | SkPoint firstPt; |
| 243 | SkPoint prevPt; |
| 244 | RawIter iter(*this); |
| 245 | SkPath::Verb verb; |
| 246 | SkPoint pts[4]; |
| 247 | SkDEBUGCODE(int moveCnt = 0;) |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 248 | SkDEBUGCODE(int segmentCount = 0;) |
| 249 | SkDEBUGCODE(int closeCount = 0;) |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 250 | |
| 251 | while ((verb = iter.next(pts)) != kDone_Verb) { |
| 252 | int nextPt = -1; |
| 253 | switch (verb) { |
| 254 | case kMove_Verb: |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 255 | SkASSERT(!segmentCount && !closeCount); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 256 | SkDEBUGCODE(++moveCnt); |
| 257 | firstPt = prevPt = pts[0]; |
| 258 | break; |
| 259 | case kLine_Verb: |
| 260 | nextPt = 1; |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 261 | SkASSERT(moveCnt && !closeCount); |
| 262 | SkDEBUGCODE(++segmentCount); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 263 | break; |
| 264 | case kQuad_Verb: |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 265 | case kConic_Verb: |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 266 | SkASSERT(moveCnt && !closeCount); |
| 267 | SkDEBUGCODE(++segmentCount); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 268 | nextPt = 2; |
| 269 | break; |
| 270 | case kCubic_Verb: |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 271 | SkASSERT(moveCnt && !closeCount); |
| 272 | SkDEBUGCODE(++segmentCount); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 273 | nextPt = 3; |
| 274 | break; |
| 275 | case kClose_Verb: |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 276 | SkDEBUGCODE(++closeCount;) |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 277 | break; |
| 278 | default: |
| 279 | SkDEBUGFAIL("unknown verb"); |
| 280 | } |
| 281 | if (-1 != nextPt) { |
| 282 | if (!check_edge_against_rect(prevPt, pts[nextPt], rect, direction)) { |
| 283 | return false; |
| 284 | } |
| 285 | prevPt = pts[nextPt]; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return check_edge_against_rect(prevPt, firstPt, rect, direction); |
| 290 | } |
| 291 | |
robertphillips@google.com | 7101abe | 2013-10-29 22:45:37 +0000 | [diff] [blame] | 292 | uint32_t SkPath::getGenerationID() const { |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 293 | uint32_t genID = fPathRef->genID(); |
| 294 | #ifdef SK_BUILD_FOR_ANDROID |
| 295 | SkASSERT((unsigned)fFillType < (1 << (32 - kPathRefGenIDBitCnt))); |
| 296 | genID |= static_cast<uint32_t>(fFillType) << kPathRefGenIDBitCnt; |
| 297 | #endif |
| 298 | return genID; |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 299 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 300 | |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 301 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 302 | const SkPath* SkPath::getSourcePath() const { |
| 303 | return fSourcePath; |
| 304 | } |
| 305 | |
| 306 | void SkPath::setSourcePath(const SkPath* path) { |
| 307 | fSourcePath = path; |
| 308 | } |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 309 | #endif |
| 310 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 311 | void SkPath::reset() { |
| 312 | SkDEBUGCODE(this->validate();) |
| 313 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 314 | fPathRef.reset(SkPathRef::CreateEmpty()); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 315 | this->resetFields(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void SkPath::rewind() { |
| 319 | SkDEBUGCODE(this->validate();) |
| 320 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 321 | SkPathRef::Rewind(&fPathRef); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 322 | this->resetFields(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 323 | } |
| 324 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 325 | bool SkPath::isLine(SkPoint line[2]) const { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 326 | int verbCount = fPathRef->countVerbs(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 327 | |
commit-bot@chromium.org | a62efcc | 2013-08-05 13:23:13 +0000 | [diff] [blame] | 328 | if (2 == verbCount) { |
| 329 | SkASSERT(kMove_Verb == fPathRef->atVerb(0)); |
| 330 | if (kLine_Verb == fPathRef->atVerb(1)) { |
| 331 | SkASSERT(2 == fPathRef->countPoints()); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 332 | if (line) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 333 | const SkPoint* pts = fPathRef->points(); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 334 | line[0] = pts[0]; |
| 335 | line[1] = pts[1]; |
| 336 | } |
| 337 | return true; |
| 338 | } |
| 339 | } |
| 340 | return false; |
| 341 | } |
| 342 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 343 | /* |
| 344 | Determines if path is a rect by keeping track of changes in direction |
| 345 | and looking for a loop either clockwise or counterclockwise. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 346 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 347 | The direction is computed such that: |
| 348 | 0: vertical up |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 349 | 1: horizontal left |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 350 | 2: vertical down |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 351 | 3: horizontal right |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 352 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 353 | A rectangle cycles up/right/down/left or up/left/down/right. |
| 354 | |
| 355 | The test fails if: |
| 356 | The path is closed, and followed by a line. |
| 357 | A second move creates a new endpoint. |
| 358 | A diagonal line is parsed. |
| 359 | There's more than four changes of direction. |
| 360 | There's a discontinuity on the line (e.g., a move in the middle) |
| 361 | The line reverses direction. |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 362 | The path contains a quadratic or cubic. |
| 363 | The path contains fewer than four points. |
commit-bot@chromium.org | 05ec223 | 2014-01-15 18:00:57 +0000 | [diff] [blame] | 364 | *The rectangle doesn't complete a cycle. |
| 365 | *The final point isn't equal to the first point. |
| 366 | |
| 367 | *These last two conditions we relax if we have a 3-edge path that would |
| 368 | form a rectangle if it were closed (as we do when we fill a path) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 369 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 370 | It's OK if the path has: |
| 371 | Several colinear line segments composing a rectangle side. |
| 372 | Single points on the rectangle side. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 373 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 374 | The direction takes advantage of the corners found since opposite sides |
| 375 | must travel in opposite directions. |
| 376 | |
| 377 | FIXME: Allow colinear quads and cubics to be treated like lines. |
| 378 | FIXME: If the API passes fill-only, return true if the filled stroke |
| 379 | is a rectangle, though the caller failed to close the path. |
commit-bot@chromium.org | 05ec223 | 2014-01-15 18:00:57 +0000 | [diff] [blame] | 380 | |
| 381 | first,last,next direction state-machine: |
| 382 | 0x1 is set if the segment is horizontal |
| 383 | 0x2 is set if the segment is moving to the right or down |
| 384 | thus: |
| 385 | two directions are opposites iff (dirA ^ dirB) == 0x2 |
| 386 | two directions are perpendicular iff (dirA ^ dirB) == 0x1 |
skia.committer@gmail.com | f5e67c1 | 2014-01-16 07:01:48 +0000 | [diff] [blame] | 387 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 388 | */ |
commit-bot@chromium.org | 05ec223 | 2014-01-15 18:00:57 +0000 | [diff] [blame] | 389 | static int rect_make_dir(SkScalar dx, SkScalar dy) { |
| 390 | return ((0 != dx) << 0) | ((dx > 0 || dy > 0) << 1); |
| 391 | } |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 392 | bool SkPath::isRectContour(bool allowPartial, int* currVerb, const SkPoint** ptsPtr, |
| 393 | bool* isClosed, Direction* direction) const { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 394 | int corners = 0; |
| 395 | SkPoint first, last; |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 396 | const SkPoint* pts = *ptsPtr; |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 397 | const SkPoint* savePts = NULL; |
tomhudson@google.com | 2c2508d | 2011-07-29 13:44:30 +0000 | [diff] [blame] | 398 | first.set(0, 0); |
| 399 | last.set(0, 0); |
| 400 | int firstDirection = 0; |
| 401 | int lastDirection = 0; |
| 402 | int nextDirection = 0; |
| 403 | bool closedOrMoved = false; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 404 | bool autoClose = false; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 405 | int verbCnt = fPathRef->countVerbs(); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 406 | while (*currVerb < verbCnt && (!allowPartial || !autoClose)) { |
| 407 | switch (fPathRef->atVerb(*currVerb)) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 408 | case kClose_Verb: |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 409 | savePts = pts; |
| 410 | pts = *ptsPtr; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 411 | autoClose = true; |
| 412 | case kLine_Verb: { |
| 413 | SkScalar left = last.fX; |
| 414 | SkScalar top = last.fY; |
| 415 | SkScalar right = pts->fX; |
| 416 | SkScalar bottom = pts->fY; |
| 417 | ++pts; |
| 418 | if (left != right && top != bottom) { |
| 419 | return false; // diagonal |
| 420 | } |
| 421 | if (left == right && top == bottom) { |
| 422 | break; // single point on side OK |
| 423 | } |
commit-bot@chromium.org | 05ec223 | 2014-01-15 18:00:57 +0000 | [diff] [blame] | 424 | nextDirection = rect_make_dir(right - left, bottom - top); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 425 | if (0 == corners) { |
| 426 | firstDirection = nextDirection; |
| 427 | first = last; |
| 428 | last = pts[-1]; |
| 429 | corners = 1; |
| 430 | closedOrMoved = false; |
| 431 | break; |
| 432 | } |
| 433 | if (closedOrMoved) { |
| 434 | return false; // closed followed by a line |
| 435 | } |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 436 | if (autoClose && nextDirection == firstDirection) { |
| 437 | break; // colinear with first |
| 438 | } |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 439 | closedOrMoved = autoClose; |
| 440 | if (lastDirection != nextDirection) { |
| 441 | if (++corners > 4) { |
| 442 | return false; // too many direction changes |
| 443 | } |
| 444 | } |
| 445 | last = pts[-1]; |
| 446 | if (lastDirection == nextDirection) { |
| 447 | break; // colinear segment |
| 448 | } |
| 449 | // Possible values for corners are 2, 3, and 4. |
| 450 | // When corners == 3, nextDirection opposes firstDirection. |
| 451 | // Otherwise, nextDirection at corner 2 opposes corner 4. |
tomhudson@google.com | 2c2508d | 2011-07-29 13:44:30 +0000 | [diff] [blame] | 452 | int turn = firstDirection ^ (corners - 1); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 453 | int directionCycle = 3 == corners ? 0 : nextDirection ^ turn; |
| 454 | if ((directionCycle ^ turn) != nextDirection) { |
| 455 | return false; // direction didn't follow cycle |
| 456 | } |
| 457 | break; |
| 458 | } |
| 459 | case kQuad_Verb: |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 460 | case kConic_Verb: |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 461 | case kCubic_Verb: |
| 462 | return false; // quadratic, cubic not allowed |
| 463 | case kMove_Verb: |
| 464 | last = *pts++; |
| 465 | closedOrMoved = true; |
| 466 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 467 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 468 | SkDEBUGFAIL("unexpected verb"); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 469 | break; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 470 | } |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 471 | *currVerb += 1; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 472 | lastDirection = nextDirection; |
| 473 | } |
| 474 | // Success if 4 corners and first point equals last |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 475 | bool result = 4 == corners && (first == last || autoClose); |
commit-bot@chromium.org | 05ec223 | 2014-01-15 18:00:57 +0000 | [diff] [blame] | 476 | if (!result) { |
| 477 | // check if we are just an incomplete rectangle, in which case we can |
| 478 | // return true, but not claim to be closed. |
| 479 | // e.g. |
| 480 | // 3 sided rectangle |
| 481 | // 4 sided but the last edge is not long enough to reach the start |
| 482 | // |
| 483 | SkScalar closeX = first.x() - last.x(); |
| 484 | SkScalar closeY = first.y() - last.y(); |
| 485 | if (closeX && closeY) { |
| 486 | return false; // we're diagonal, abort (can we ever reach this?) |
| 487 | } |
| 488 | int closeDirection = rect_make_dir(closeX, closeY); |
| 489 | // make sure the close-segment doesn't double-back on itself |
| 490 | if (3 == corners || (4 == corners && closeDirection == lastDirection)) { |
| 491 | result = true; |
| 492 | autoClose = false; // we are not closed |
| 493 | } |
| 494 | } |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 495 | if (savePts) { |
| 496 | *ptsPtr = savePts; |
| 497 | } |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 498 | if (result && isClosed) { |
| 499 | *isClosed = autoClose; |
| 500 | } |
| 501 | if (result && direction) { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 502 | *direction = firstDirection == ((lastDirection + 1) & 3) ? kCCW_Direction : kCW_Direction; |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 503 | } |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 504 | return result; |
| 505 | } |
| 506 | |
commit-bot@chromium.org | c2abd54 | 2014-01-25 16:54:31 +0000 | [diff] [blame] | 507 | SkPath::PathAsRect SkPath::asRect(Direction* direction) const { |
| 508 | SK_COMPILE_ASSERT(0 == kNone_PathAsRect, path_as_rect_mismatch); |
commit-bot@chromium.org | 7e90e8d | 2014-02-11 01:38:30 +0000 | [diff] [blame] | 509 | SK_COMPILE_ASSERT(1 == kFill_PathAsRect, path_as_rect_mismatch); |
| 510 | SK_COMPILE_ASSERT(2 == kStroke_PathAsRect, path_as_rect_mismatch); |
commit-bot@chromium.org | c2abd54 | 2014-01-25 16:54:31 +0000 | [diff] [blame] | 511 | bool isClosed = false; |
| 512 | return (PathAsRect) (isRect(&isClosed, direction) + isClosed); |
| 513 | } |
| 514 | |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 515 | bool SkPath::isRect(SkRect* rect) const { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 516 | SkDEBUGCODE(this->validate();) |
| 517 | int currVerb = 0; |
| 518 | const SkPoint* pts = fPathRef->points(); |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 519 | bool result = isRectContour(false, &currVerb, &pts, NULL, NULL); |
| 520 | if (result && rect) { |
| 521 | *rect = getBounds(); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 522 | } |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 523 | return result; |
| 524 | } |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 525 | |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 526 | bool SkPath::isRect(bool* isClosed, Direction* direction) const { |
| 527 | SkDEBUGCODE(this->validate();) |
| 528 | int currVerb = 0; |
| 529 | const SkPoint* pts = fPathRef->points(); |
| 530 | return isRectContour(false, &currVerb, &pts, isClosed, direction); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 531 | } |
| 532 | |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 533 | bool SkPath::isNestedRects(SkRect rects[2], Direction dirs[2]) const { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 534 | SkDEBUGCODE(this->validate();) |
| 535 | int currVerb = 0; |
| 536 | const SkPoint* pts = fPathRef->points(); |
| 537 | const SkPoint* first = pts; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 538 | Direction testDirs[2]; |
| 539 | if (!isRectContour(true, &currVerb, &pts, NULL, &testDirs[0])) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 540 | return false; |
| 541 | } |
| 542 | const SkPoint* last = pts; |
| 543 | SkRect testRects[2]; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 544 | if (isRectContour(false, &currVerb, &pts, NULL, &testDirs[1])) { |
scroggo@google.com | 614f9e3 | 2013-05-09 18:05:32 +0000 | [diff] [blame] | 545 | testRects[0].set(first, SkToS32(last - first)); |
| 546 | testRects[1].set(last, SkToS32(pts - last)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 547 | if (testRects[0].contains(testRects[1])) { |
| 548 | if (rects) { |
| 549 | rects[0] = testRects[0]; |
| 550 | rects[1] = testRects[1]; |
| 551 | } |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 552 | if (dirs) { |
| 553 | dirs[0] = testDirs[0]; |
| 554 | dirs[1] = testDirs[1]; |
| 555 | } |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 556 | return true; |
| 557 | } |
| 558 | if (testRects[1].contains(testRects[0])) { |
| 559 | if (rects) { |
| 560 | rects[0] = testRects[1]; |
| 561 | rects[1] = testRects[0]; |
| 562 | } |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 563 | if (dirs) { |
| 564 | dirs[0] = testDirs[1]; |
| 565 | dirs[1] = testDirs[0]; |
| 566 | } |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 567 | return true; |
| 568 | } |
| 569 | } |
| 570 | return false; |
| 571 | } |
| 572 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 573 | int SkPath::countPoints() const { |
| 574 | return fPathRef->countPoints(); |
| 575 | } |
| 576 | |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 577 | int SkPath::getPoints(SkPoint dst[], int max) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 578 | SkDEBUGCODE(this->validate();) |
| 579 | |
| 580 | SkASSERT(max >= 0); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 581 | SkASSERT(!max || dst); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 582 | int count = SkMin32(max, fPathRef->countPoints()); |
| 583 | memcpy(dst, fPathRef->points(), count * sizeof(SkPoint)); |
| 584 | return fPathRef->countPoints(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 585 | } |
| 586 | |
reed@android.com | d3aa4ff | 2010-02-09 16:38:45 +0000 | [diff] [blame] | 587 | SkPoint SkPath::getPoint(int index) const { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 588 | if ((unsigned)index < (unsigned)fPathRef->countPoints()) { |
| 589 | return fPathRef->atPoint(index); |
reed@android.com | d3aa4ff | 2010-02-09 16:38:45 +0000 | [diff] [blame] | 590 | } |
| 591 | return SkPoint::Make(0, 0); |
| 592 | } |
| 593 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 594 | int SkPath::countVerbs() const { |
| 595 | return fPathRef->countVerbs(); |
| 596 | } |
| 597 | |
| 598 | static inline void copy_verbs_reverse(uint8_t* inorderDst, |
| 599 | const uint8_t* reversedSrc, |
| 600 | int count) { |
| 601 | for (int i = 0; i < count; ++i) { |
| 602 | inorderDst[i] = reversedSrc[~i]; |
| 603 | } |
| 604 | } |
| 605 | |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 606 | int SkPath::getVerbs(uint8_t dst[], int max) const { |
| 607 | SkDEBUGCODE(this->validate();) |
| 608 | |
| 609 | SkASSERT(max >= 0); |
| 610 | SkASSERT(!max || dst); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 611 | int count = SkMin32(max, fPathRef->countVerbs()); |
| 612 | copy_verbs_reverse(dst, fPathRef->verbs(), count); |
| 613 | return fPathRef->countVerbs(); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 614 | } |
| 615 | |
reed@google.com | 294dd7b | 2011-10-11 11:58:32 +0000 | [diff] [blame] | 616 | bool SkPath::getLastPt(SkPoint* lastPt) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 617 | SkDEBUGCODE(this->validate();) |
| 618 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 619 | int count = fPathRef->countPoints(); |
reed@google.com | 294dd7b | 2011-10-11 11:58:32 +0000 | [diff] [blame] | 620 | if (count > 0) { |
| 621 | if (lastPt) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 622 | *lastPt = fPathRef->atPoint(count - 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 623 | } |
reed@google.com | 294dd7b | 2011-10-11 11:58:32 +0000 | [diff] [blame] | 624 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 625 | } |
reed@google.com | 294dd7b | 2011-10-11 11:58:32 +0000 | [diff] [blame] | 626 | if (lastPt) { |
| 627 | lastPt->set(0, 0); |
| 628 | } |
| 629 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | void SkPath::setLastPt(SkScalar x, SkScalar y) { |
| 633 | SkDEBUGCODE(this->validate();) |
| 634 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 635 | int count = fPathRef->countPoints(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 636 | if (count == 0) { |
| 637 | this->moveTo(x, y); |
| 638 | } else { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 639 | SkPathRef::Editor ed(&fPathRef); |
| 640 | ed.atPoint(count-1)->set(x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 644 | void SkPath::setConvexity(Convexity c) { |
| 645 | if (fConvexity != c) { |
| 646 | fConvexity = c; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 650 | ////////////////////////////////////////////////////////////////////////////// |
| 651 | // Construction methods |
| 652 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 653 | #define DIRTY_AFTER_EDIT \ |
| 654 | do { \ |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 655 | fConvexity = kUnknown_Convexity; \ |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 656 | fDirection = kUnknown_Direction; \ |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 657 | } while (0) |
| 658 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 659 | void SkPath::incReserve(U16CPU inc) { |
| 660 | SkDEBUGCODE(this->validate();) |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 661 | SkPathRef::Editor(&fPathRef, inc, inc); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 662 | SkDEBUGCODE(this->validate();) |
| 663 | } |
| 664 | |
| 665 | void SkPath::moveTo(SkScalar x, SkScalar y) { |
| 666 | SkDEBUGCODE(this->validate();) |
| 667 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 668 | SkPathRef::Editor ed(&fPathRef); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 669 | |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 670 | // remember our index |
| 671 | fLastMoveToIndex = fPathRef->countPoints(); |
| 672 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 673 | ed.growForVerb(kMove_Verb)->set(x, y); |
bsalomon | b17c129 | 2014-08-28 14:04:55 -0700 | [diff] [blame^] | 674 | |
| 675 | DIRTY_AFTER_EDIT; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | void SkPath::rMoveTo(SkScalar x, SkScalar y) { |
| 679 | SkPoint pt; |
| 680 | this->getLastPt(&pt); |
| 681 | this->moveTo(pt.fX + x, pt.fY + y); |
| 682 | } |
| 683 | |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 684 | void SkPath::injectMoveToIfNeeded() { |
| 685 | if (fLastMoveToIndex < 0) { |
| 686 | SkScalar x, y; |
| 687 | if (fPathRef->countVerbs() == 0) { |
| 688 | x = y = 0; |
| 689 | } else { |
| 690 | const SkPoint& pt = fPathRef->atPoint(~fLastMoveToIndex); |
| 691 | x = pt.fX; |
| 692 | y = pt.fY; |
| 693 | } |
| 694 | this->moveTo(x, y); |
| 695 | } |
| 696 | } |
| 697 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 698 | void SkPath::lineTo(SkScalar x, SkScalar y) { |
| 699 | SkDEBUGCODE(this->validate();) |
| 700 | |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 701 | this->injectMoveToIfNeeded(); |
| 702 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 703 | SkPathRef::Editor ed(&fPathRef); |
| 704 | ed.growForVerb(kLine_Verb)->set(x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 705 | |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 706 | DIRTY_AFTER_EDIT; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | void SkPath::rLineTo(SkScalar x, SkScalar y) { |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 710 | this->injectMoveToIfNeeded(); // This can change the result of this->getLastPt(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 711 | SkPoint pt; |
| 712 | this->getLastPt(&pt); |
| 713 | this->lineTo(pt.fX + x, pt.fY + y); |
| 714 | } |
| 715 | |
| 716 | void SkPath::quadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
| 717 | SkDEBUGCODE(this->validate();) |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 718 | |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 719 | this->injectMoveToIfNeeded(); |
| 720 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 721 | SkPathRef::Editor ed(&fPathRef); |
| 722 | SkPoint* pts = ed.growForVerb(kQuad_Verb); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 723 | pts[0].set(x1, y1); |
| 724 | pts[1].set(x2, y2); |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 725 | |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 726 | DIRTY_AFTER_EDIT; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void SkPath::rQuadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 730 | this->injectMoveToIfNeeded(); // This can change the result of this->getLastPt(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 731 | SkPoint pt; |
| 732 | this->getLastPt(&pt); |
| 733 | this->quadTo(pt.fX + x1, pt.fY + y1, pt.fX + x2, pt.fY + y2); |
| 734 | } |
| 735 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 736 | void SkPath::conicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 737 | SkScalar w) { |
| 738 | // check for <= 0 or NaN with this test |
| 739 | if (!(w > 0)) { |
| 740 | this->lineTo(x2, y2); |
| 741 | } else if (!SkScalarIsFinite(w)) { |
| 742 | this->lineTo(x1, y1); |
| 743 | this->lineTo(x2, y2); |
| 744 | } else if (SK_Scalar1 == w) { |
| 745 | this->quadTo(x1, y1, x2, y2); |
| 746 | } else { |
| 747 | SkDEBUGCODE(this->validate();) |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 748 | |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 749 | this->injectMoveToIfNeeded(); |
| 750 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 751 | SkPathRef::Editor ed(&fPathRef); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 752 | SkPoint* pts = ed.growForVerb(kConic_Verb, w); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 753 | pts[0].set(x1, y1); |
| 754 | pts[1].set(x2, y2); |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 755 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 756 | DIRTY_AFTER_EDIT; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | void SkPath::rConicTo(SkScalar dx1, SkScalar dy1, SkScalar dx2, SkScalar dy2, |
| 761 | SkScalar w) { |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 762 | this->injectMoveToIfNeeded(); // This can change the result of this->getLastPt(). |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 763 | SkPoint pt; |
| 764 | this->getLastPt(&pt); |
| 765 | this->conicTo(pt.fX + dx1, pt.fY + dy1, pt.fX + dx2, pt.fY + dy2, w); |
| 766 | } |
| 767 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 768 | void SkPath::cubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 769 | SkScalar x3, SkScalar y3) { |
| 770 | SkDEBUGCODE(this->validate();) |
| 771 | |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 772 | this->injectMoveToIfNeeded(); |
| 773 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 774 | SkPathRef::Editor ed(&fPathRef); |
| 775 | SkPoint* pts = ed.growForVerb(kCubic_Verb); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 776 | pts[0].set(x1, y1); |
| 777 | pts[1].set(x2, y2); |
| 778 | pts[2].set(x3, y3); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 779 | |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 780 | DIRTY_AFTER_EDIT; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | void SkPath::rCubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 784 | SkScalar x3, SkScalar y3) { |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 785 | this->injectMoveToIfNeeded(); // This can change the result of this->getLastPt(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 786 | SkPoint pt; |
| 787 | this->getLastPt(&pt); |
| 788 | this->cubicTo(pt.fX + x1, pt.fY + y1, pt.fX + x2, pt.fY + y2, |
| 789 | pt.fX + x3, pt.fY + y3); |
| 790 | } |
| 791 | |
| 792 | void SkPath::close() { |
| 793 | SkDEBUGCODE(this->validate();) |
| 794 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 795 | int count = fPathRef->countVerbs(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 796 | if (count > 0) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 797 | switch (fPathRef->atVerb(count - 1)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 798 | case kLine_Verb: |
| 799 | case kQuad_Verb: |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 800 | case kConic_Verb: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 801 | case kCubic_Verb: |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 802 | case kMove_Verb: { |
| 803 | SkPathRef::Editor ed(&fPathRef); |
| 804 | ed.growForVerb(kClose_Verb); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 805 | break; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 806 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 807 | case kClose_Verb: |
reed@google.com | fa2f2a4 | 2013-05-30 15:29:48 +0000 | [diff] [blame] | 808 | // don't add a close if it's the first verb or a repeat |
reed@google.com | 7950a9e | 2013-05-30 14:57:55 +0000 | [diff] [blame] | 809 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 810 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 811 | SkDEBUGFAIL("unexpected verb"); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 812 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 815 | |
| 816 | // signal that we need a moveTo to follow us (unless we're done) |
| 817 | #if 0 |
| 818 | if (fLastMoveToIndex >= 0) { |
| 819 | fLastMoveToIndex = ~fLastMoveToIndex; |
| 820 | } |
| 821 | #else |
| 822 | fLastMoveToIndex ^= ~fLastMoveToIndex >> (8 * sizeof(fLastMoveToIndex) - 1); |
| 823 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 827 | |
reed@google.com | a8a3b3d | 2012-11-26 18:16:27 +0000 | [diff] [blame] | 828 | static void assert_known_direction(int dir) { |
| 829 | SkASSERT(SkPath::kCW_Direction == dir || SkPath::kCCW_Direction == dir); |
| 830 | } |
| 831 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 832 | void SkPath::addRect(const SkRect& rect, Direction dir) { |
| 833 | this->addRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, dir); |
| 834 | } |
| 835 | |
| 836 | void SkPath::addRect(SkScalar left, SkScalar top, SkScalar right, |
| 837 | SkScalar bottom, Direction dir) { |
reed@google.com | a8a3b3d | 2012-11-26 18:16:27 +0000 | [diff] [blame] | 838 | assert_known_direction(dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 839 | fDirection = this->hasOnlyMoveTos() ? dir : kUnknown_Direction; |
| 840 | SkAutoDisableDirectionCheck addc(this); |
| 841 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 842 | SkAutoPathBoundsUpdate apbu(this, left, top, right, bottom); |
| 843 | |
| 844 | this->incReserve(5); |
| 845 | |
| 846 | this->moveTo(left, top); |
| 847 | if (dir == kCCW_Direction) { |
| 848 | this->lineTo(left, bottom); |
| 849 | this->lineTo(right, bottom); |
| 850 | this->lineTo(right, top); |
| 851 | } else { |
| 852 | this->lineTo(right, top); |
| 853 | this->lineTo(right, bottom); |
| 854 | this->lineTo(left, bottom); |
| 855 | } |
| 856 | this->close(); |
| 857 | } |
| 858 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 859 | void SkPath::addPoly(const SkPoint pts[], int count, bool close) { |
| 860 | SkDEBUGCODE(this->validate();) |
| 861 | if (count <= 0) { |
| 862 | return; |
| 863 | } |
| 864 | |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 865 | fLastMoveToIndex = fPathRef->countPoints(); |
| 866 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 867 | // +close makes room for the extra kClose_Verb |
| 868 | SkPathRef::Editor ed(&fPathRef, count+close, count); |
| 869 | |
| 870 | ed.growForVerb(kMove_Verb)->set(pts[0].fX, pts[0].fY); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 871 | if (count > 1) { |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 872 | SkPoint* p = ed.growForRepeatedVerb(kLine_Verb, count - 1); |
| 873 | memcpy(p, &pts[1], (count-1) * sizeof(SkPoint)); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 874 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 875 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 876 | if (close) { |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 877 | ed.growForVerb(kClose_Verb); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 878 | } |
| 879 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 880 | DIRTY_AFTER_EDIT; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 881 | SkDEBUGCODE(this->validate();) |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 882 | } |
| 883 | |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 884 | #include "SkGeometry.h" |
| 885 | |
| 886 | static int build_arc_points(const SkRect& oval, SkScalar startAngle, |
| 887 | SkScalar sweepAngle, |
| 888 | SkPoint pts[kSkBuildQuadArcStorage]) { |
| 889 | |
| 890 | if (0 == sweepAngle && |
| 891 | (0 == startAngle || SkIntToScalar(360) == startAngle)) { |
| 892 | // Chrome uses this path to move into and out of ovals. If not |
| 893 | // treated as a special case the moves can distort the oval's |
| 894 | // bounding box (and break the circle special case). |
| 895 | pts[0].set(oval.fRight, oval.centerY()); |
| 896 | return 1; |
| 897 | } else if (0 == oval.width() && 0 == oval.height()) { |
| 898 | // Chrome will sometimes create 0 radius round rects. Having degenerate |
| 899 | // quad segments in the path prevents the path from being recognized as |
| 900 | // a rect. |
| 901 | // TODO: optimizing the case where only one of width or height is zero |
| 902 | // should also be considered. This case, however, doesn't seem to be |
| 903 | // as common as the single point case. |
| 904 | pts[0].set(oval.fRight, oval.fTop); |
| 905 | return 1; |
| 906 | } |
| 907 | |
| 908 | SkVector start, stop; |
| 909 | |
| 910 | start.fY = SkScalarSinCos(SkDegreesToRadians(startAngle), &start.fX); |
| 911 | stop.fY = SkScalarSinCos(SkDegreesToRadians(startAngle + sweepAngle), |
| 912 | &stop.fX); |
| 913 | |
| 914 | /* If the sweep angle is nearly (but less than) 360, then due to precision |
| 915 | loss in radians-conversion and/or sin/cos, we may end up with coincident |
| 916 | vectors, which will fool SkBuildQuadArc into doing nothing (bad) instead |
| 917 | of drawing a nearly complete circle (good). |
| 918 | e.g. canvas.drawArc(0, 359.99, ...) |
| 919 | -vs- canvas.drawArc(0, 359.9, ...) |
| 920 | We try to detect this edge case, and tweak the stop vector |
| 921 | */ |
| 922 | if (start == stop) { |
| 923 | SkScalar sw = SkScalarAbs(sweepAngle); |
| 924 | if (sw < SkIntToScalar(360) && sw > SkIntToScalar(359)) { |
| 925 | SkScalar stopRad = SkDegreesToRadians(startAngle + sweepAngle); |
| 926 | // make a guess at a tiny angle (in radians) to tweak by |
| 927 | SkScalar deltaRad = SkScalarCopySign(SK_Scalar1/512, sweepAngle); |
| 928 | // not sure how much will be enough, so we use a loop |
| 929 | do { |
| 930 | stopRad -= deltaRad; |
| 931 | stop.fY = SkScalarSinCos(stopRad, &stop.fX); |
| 932 | } while (start == stop); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | SkMatrix matrix; |
| 937 | |
| 938 | matrix.setScale(SkScalarHalf(oval.width()), SkScalarHalf(oval.height())); |
| 939 | matrix.postTranslate(oval.centerX(), oval.centerY()); |
| 940 | |
| 941 | return SkBuildQuadArc(start, stop, |
| 942 | sweepAngle > 0 ? kCW_SkRotationDirection : |
| 943 | kCCW_SkRotationDirection, |
| 944 | &matrix, pts); |
| 945 | } |
| 946 | |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 947 | void SkPath::addRoundRect(const SkRect& rect, const SkScalar radii[], |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 948 | Direction dir) { |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 949 | SkRRect rrect; |
| 950 | rrect.setRectRadii(rect, (const SkVector*) radii); |
| 951 | this->addRRect(rrect, dir); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 952 | } |
| 953 | |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 954 | /* The inline clockwise and counterclockwise round rect quad approximations |
| 955 | make it easier to see the symmetry patterns used by add corner quads. |
| 956 | Clockwise corner value |
| 957 | path->lineTo(rect.fLeft, rect.fTop + ry); 0 upper left |
| 958 | path->quadTo(rect.fLeft, rect.fTop + offPtY, |
| 959 | rect.fLeft + midPtX, rect.fTop + midPtY); |
| 960 | path->quadTo(rect.fLeft + offPtX, rect.fTop, |
| 961 | rect.fLeft + rx, rect.fTop); |
| 962 | |
| 963 | path->lineTo(rect.fRight - rx, rect.fTop); 1 upper right |
| 964 | path->quadTo(rect.fRight - offPtX, rect.fTop, |
| 965 | rect.fRight - midPtX, rect.fTop + midPtY); |
| 966 | path->quadTo(rect.fRight, rect.fTop + offPtY, |
| 967 | rect.fRight, rect.fTop + ry); |
| 968 | |
| 969 | path->lineTo(rect.fRight, rect.fBottom - ry); 2 lower right |
| 970 | path->quadTo(rect.fRight, rect.fBottom - offPtY, |
| 971 | rect.fRight - midPtX, rect.fBottom - midPtY); |
| 972 | path->quadTo(rect.fRight - offPtX, rect.fBottom, |
| 973 | rect.fRight - rx, rect.fBottom); |
| 974 | |
| 975 | path->lineTo(rect.fLeft + rx, rect.fBottom); 3 lower left |
| 976 | path->quadTo(rect.fLeft + offPtX, rect.fBottom, |
| 977 | rect.fLeft + midPtX, rect.fBottom - midPtY); |
| 978 | path->quadTo(rect.fLeft, rect.fBottom - offPtY, |
| 979 | rect.fLeft, rect.fBottom - ry); |
| 980 | |
| 981 | Counterclockwise |
| 982 | path->lineTo(rect.fLeft, rect.fBottom - ry); 3 lower left |
| 983 | path->quadTo(rect.fLeft, rect.fBottom - offPtY, |
| 984 | rect.fLeft + midPtX, rect.fBottom - midPtY); |
| 985 | path->quadTo(rect.fLeft + offPtX, rect.fBottom, |
| 986 | rect.fLeft + rx, rect.fBottom); |
| 987 | |
| 988 | path->lineTo(rect.fRight - rx, rect.fBottom); 2 lower right |
| 989 | path->quadTo(rect.fRight - offPtX, rect.fBottom, |
| 990 | rect.fRight - midPtX, rect.fBottom - midPtY); |
| 991 | path->quadTo(rect.fRight, rect.fBottom - offPtY, |
| 992 | rect.fRight, rect.fBottom - ry); |
| 993 | |
| 994 | path->lineTo(rect.fRight, rect.fTop + ry); 1 upper right |
| 995 | path->quadTo(rect.fRight, rect.fTop + offPtY, |
| 996 | rect.fRight - midPtX, rect.fTop + midPtY); |
| 997 | path->quadTo(rect.fRight - offPtX, rect.fTop, |
| 998 | rect.fRight - rx, rect.fTop); |
| 999 | |
| 1000 | path->lineTo(rect.fLeft + rx, rect.fTop); 0 upper left |
| 1001 | path->quadTo(rect.fLeft + offPtX, rect.fTop, |
| 1002 | rect.fLeft + midPtX, rect.fTop + midPtY); |
| 1003 | path->quadTo(rect.fLeft, rect.fTop + offPtY, |
| 1004 | rect.fLeft, rect.fTop + ry); |
| 1005 | */ |
| 1006 | static void add_corner_quads(SkPath* path, const SkRRect& rrect, |
| 1007 | SkRRect::Corner corner, SkPath::Direction dir) { |
| 1008 | const SkRect& rect = rrect.rect(); |
| 1009 | const SkVector& radii = rrect.radii(corner); |
| 1010 | SkScalar rx = radii.fX; |
| 1011 | SkScalar ry = radii.fY; |
| 1012 | // The mid point of the quadratic arc approximation is half way between the two |
| 1013 | // control points. |
caryclark@google.com | 2e1b99e | 2013-11-08 18:05:02 +0000 | [diff] [blame] | 1014 | const SkScalar mid = 1 - (SK_Scalar1 + SK_ScalarTanPIOver8) / 2; |
| 1015 | SkScalar midPtX = rx * mid; |
| 1016 | SkScalar midPtY = ry * mid; |
| 1017 | const SkScalar control = 1 - SK_ScalarTanPIOver8; |
| 1018 | SkScalar offPtX = rx * control; |
| 1019 | SkScalar offPtY = ry * control; |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 1020 | static const int kCornerPts = 5; |
| 1021 | SkScalar xOff[kCornerPts]; |
| 1022 | SkScalar yOff[kCornerPts]; |
| 1023 | |
| 1024 | if ((corner & 1) == (dir == SkPath::kCCW_Direction)) { // corners always alternate direction |
| 1025 | SkASSERT(dir == SkPath::kCCW_Direction |
| 1026 | ? corner == SkRRect::kLowerLeft_Corner || corner == SkRRect::kUpperRight_Corner |
| 1027 | : corner == SkRRect::kUpperLeft_Corner || corner == SkRRect::kLowerRight_Corner); |
| 1028 | xOff[0] = xOff[1] = 0; |
| 1029 | xOff[2] = midPtX; |
| 1030 | xOff[3] = offPtX; |
| 1031 | xOff[4] = rx; |
| 1032 | yOff[0] = ry; |
| 1033 | yOff[1] = offPtY; |
| 1034 | yOff[2] = midPtY; |
| 1035 | yOff[3] = yOff[4] = 0; |
| 1036 | } else { |
| 1037 | xOff[0] = rx; |
| 1038 | xOff[1] = offPtX; |
| 1039 | xOff[2] = midPtX; |
| 1040 | xOff[3] = xOff[4] = 0; |
| 1041 | yOff[0] = yOff[1] = 0; |
| 1042 | yOff[2] = midPtY; |
| 1043 | yOff[3] = offPtY; |
| 1044 | yOff[4] = ry; |
| 1045 | } |
| 1046 | if ((corner - 1) & 2) { |
| 1047 | SkASSERT(corner == SkRRect::kLowerLeft_Corner || corner == SkRRect::kUpperLeft_Corner); |
| 1048 | for (int i = 0; i < kCornerPts; ++i) { |
| 1049 | xOff[i] = rect.fLeft + xOff[i]; |
| 1050 | } |
| 1051 | } else { |
| 1052 | SkASSERT(corner == SkRRect::kLowerRight_Corner || corner == SkRRect::kUpperRight_Corner); |
| 1053 | for (int i = 0; i < kCornerPts; ++i) { |
| 1054 | xOff[i] = rect.fRight - xOff[i]; |
| 1055 | } |
| 1056 | } |
| 1057 | if (corner < SkRRect::kLowerRight_Corner) { |
| 1058 | for (int i = 0; i < kCornerPts; ++i) { |
| 1059 | yOff[i] = rect.fTop + yOff[i]; |
| 1060 | } |
| 1061 | } else { |
| 1062 | for (int i = 0; i < kCornerPts; ++i) { |
| 1063 | yOff[i] = rect.fBottom - yOff[i]; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | SkPoint lastPt; |
| 1068 | SkAssertResult(path->getLastPt(&lastPt)); |
| 1069 | if (lastPt.fX != xOff[0] || lastPt.fY != yOff[0]) { |
| 1070 | path->lineTo(xOff[0], yOff[0]); |
| 1071 | } |
| 1072 | if (rx || ry) { |
| 1073 | path->quadTo(xOff[1], yOff[1], xOff[2], yOff[2]); |
| 1074 | path->quadTo(xOff[3], yOff[3], xOff[4], yOff[4]); |
| 1075 | } else { |
| 1076 | path->lineTo(xOff[2], yOff[2]); |
| 1077 | path->lineTo(xOff[4], yOff[4]); |
| 1078 | } |
| 1079 | } |
| 1080 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1081 | void SkPath::addRRect(const SkRRect& rrect, Direction dir) { |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 1082 | assert_known_direction(dir); |
| 1083 | |
| 1084 | if (rrect.isEmpty()) { |
| 1085 | return; |
| 1086 | } |
| 1087 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1088 | const SkRect& bounds = rrect.getBounds(); |
| 1089 | |
| 1090 | if (rrect.isRect()) { |
| 1091 | this->addRect(bounds, dir); |
| 1092 | } else if (rrect.isOval()) { |
| 1093 | this->addOval(bounds, dir); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1094 | } else { |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 1095 | fDirection = this->hasOnlyMoveTos() ? dir : kUnknown_Direction; |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 1096 | |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 1097 | SkAutoPathBoundsUpdate apbu(this, bounds); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1098 | SkAutoDisableDirectionCheck addc(this); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 1099 | |
| 1100 | this->incReserve(21); |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 1101 | if (kCW_Direction == dir) { |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 1102 | this->moveTo(bounds.fLeft, |
| 1103 | bounds.fBottom - rrect.fRadii[SkRRect::kLowerLeft_Corner].fY); |
| 1104 | add_corner_quads(this, rrect, SkRRect::kUpperLeft_Corner, dir); |
| 1105 | add_corner_quads(this, rrect, SkRRect::kUpperRight_Corner, dir); |
| 1106 | add_corner_quads(this, rrect, SkRRect::kLowerRight_Corner, dir); |
| 1107 | add_corner_quads(this, rrect, SkRRect::kLowerLeft_Corner, dir); |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 1108 | } else { |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 1109 | this->moveTo(bounds.fLeft, |
| 1110 | bounds.fTop + rrect.fRadii[SkRRect::kUpperLeft_Corner].fY); |
| 1111 | add_corner_quads(this, rrect, SkRRect::kLowerLeft_Corner, dir); |
| 1112 | add_corner_quads(this, rrect, SkRRect::kLowerRight_Corner, dir); |
| 1113 | add_corner_quads(this, rrect, SkRRect::kUpperRight_Corner, dir); |
| 1114 | add_corner_quads(this, rrect, SkRRect::kUpperLeft_Corner, dir); |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 1115 | } |
| 1116 | this->close(); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1120 | bool SkPath::hasOnlyMoveTos() const { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1121 | int count = fPathRef->countVerbs(); |
| 1122 | const uint8_t* verbs = const_cast<const SkPathRef*>(fPathRef.get())->verbsMemBegin(); |
| 1123 | for (int i = 0; i < count; ++i) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1124 | if (*verbs == kLine_Verb || |
| 1125 | *verbs == kQuad_Verb || |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1126 | *verbs == kConic_Verb || |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1127 | *verbs == kCubic_Verb) { |
| 1128 | return false; |
| 1129 | } |
| 1130 | ++verbs; |
| 1131 | } |
| 1132 | return true; |
| 1133 | } |
| 1134 | |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1135 | void SkPath::addRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, |
| 1136 | Direction dir) { |
| 1137 | assert_known_direction(dir); |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 1138 | |
humper@google.com | 75e3ca1 | 2013-04-08 21:44:11 +0000 | [diff] [blame] | 1139 | if (rx < 0 || ry < 0) { |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 1140 | SkErrorInternals::SetError( kInvalidArgument_SkError, |
humper@google.com | 75e3ca1 | 2013-04-08 21:44:11 +0000 | [diff] [blame] | 1141 | "I got %f and %f as radii to SkPath::AddRoundRect, " |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 1142 | "but negative radii are not allowed.", |
humper@google.com | 75e3ca1 | 2013-04-08 21:44:11 +0000 | [diff] [blame] | 1143 | SkScalarToDouble(rx), SkScalarToDouble(ry) ); |
| 1144 | return; |
| 1145 | } |
skia.committer@gmail.com | d9f65e3 | 2013-01-04 12:07:46 +0000 | [diff] [blame] | 1146 | |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 1147 | SkRRect rrect; |
| 1148 | rrect.setRectXY(rect, rx, ry); |
| 1149 | this->addRRect(rrect, dir); |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1152 | void SkPath::addOval(const SkRect& oval, Direction dir) { |
reed@google.com | a8a3b3d | 2012-11-26 18:16:27 +0000 | [diff] [blame] | 1153 | assert_known_direction(dir); |
| 1154 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1155 | /* If addOval() is called after previous moveTo(), |
| 1156 | this path is still marked as an oval. This is used to |
| 1157 | fit into WebKit's calling sequences. |
| 1158 | We can't simply check isEmpty() in this case, as additional |
| 1159 | moveTo() would mark the path non empty. |
| 1160 | */ |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 1161 | bool isOval = hasOnlyMoveTos(); |
| 1162 | if (isOval) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1163 | fDirection = dir; |
| 1164 | } else { |
| 1165 | fDirection = kUnknown_Direction; |
| 1166 | } |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1167 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1168 | SkAutoDisableDirectionCheck addc(this); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1169 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1170 | SkAutoPathBoundsUpdate apbu(this, oval); |
| 1171 | |
| 1172 | SkScalar cx = oval.centerX(); |
| 1173 | SkScalar cy = oval.centerY(); |
| 1174 | SkScalar rx = SkScalarHalf(oval.width()); |
| 1175 | SkScalar ry = SkScalarHalf(oval.height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1176 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1177 | SkScalar sx = SkScalarMul(rx, SK_ScalarTanPIOver8); |
| 1178 | SkScalar sy = SkScalarMul(ry, SK_ScalarTanPIOver8); |
| 1179 | SkScalar mx = SkScalarMul(rx, SK_ScalarRoot2Over2); |
| 1180 | SkScalar my = SkScalarMul(ry, SK_ScalarRoot2Over2); |
| 1181 | |
| 1182 | /* |
| 1183 | To handle imprecision in computing the center and radii, we revert to |
| 1184 | the provided bounds when we can (i.e. use oval.fLeft instead of cx-rx) |
| 1185 | to ensure that we don't exceed the oval's bounds *ever*, since we want |
| 1186 | to use oval for our fast-bounds, rather than have to recompute it. |
| 1187 | */ |
| 1188 | const SkScalar L = oval.fLeft; // cx - rx |
| 1189 | const SkScalar T = oval.fTop; // cy - ry |
| 1190 | const SkScalar R = oval.fRight; // cx + rx |
| 1191 | const SkScalar B = oval.fBottom; // cy + ry |
| 1192 | |
| 1193 | this->incReserve(17); // 8 quads + close |
| 1194 | this->moveTo(R, cy); |
| 1195 | if (dir == kCCW_Direction) { |
| 1196 | this->quadTo( R, cy - sy, cx + mx, cy - my); |
| 1197 | this->quadTo(cx + sx, T, cx , T); |
| 1198 | this->quadTo(cx - sx, T, cx - mx, cy - my); |
| 1199 | this->quadTo( L, cy - sy, L, cy ); |
| 1200 | this->quadTo( L, cy + sy, cx - mx, cy + my); |
| 1201 | this->quadTo(cx - sx, B, cx , B); |
| 1202 | this->quadTo(cx + sx, B, cx + mx, cy + my); |
| 1203 | this->quadTo( R, cy + sy, R, cy ); |
| 1204 | } else { |
| 1205 | this->quadTo( R, cy + sy, cx + mx, cy + my); |
| 1206 | this->quadTo(cx + sx, B, cx , B); |
| 1207 | this->quadTo(cx - sx, B, cx - mx, cy + my); |
| 1208 | this->quadTo( L, cy + sy, L, cy ); |
| 1209 | this->quadTo( L, cy - sy, cx - mx, cy - my); |
| 1210 | this->quadTo(cx - sx, T, cx , T); |
| 1211 | this->quadTo(cx + sx, T, cx + mx, cy - my); |
| 1212 | this->quadTo( R, cy - sy, R, cy ); |
| 1213 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1214 | this->close(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1215 | |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 1216 | SkPathRef::Editor ed(&fPathRef); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1217 | |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 1218 | ed.setIsOval(isOval); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1221 | void SkPath::addCircle(SkScalar x, SkScalar y, SkScalar r, Direction dir) { |
| 1222 | if (r > 0) { |
| 1223 | SkRect rect; |
| 1224 | rect.set(x - r, y - r, x + r, y + r); |
| 1225 | this->addOval(rect, dir); |
| 1226 | } |
| 1227 | } |
| 1228 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1229 | void SkPath::arcTo(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, |
| 1230 | bool forceMoveTo) { |
| 1231 | if (oval.width() < 0 || oval.height() < 0) { |
| 1232 | return; |
| 1233 | } |
| 1234 | |
| 1235 | SkPoint pts[kSkBuildQuadArcStorage]; |
| 1236 | int count = build_arc_points(oval, startAngle, sweepAngle, pts); |
| 1237 | SkASSERT((count & 1) == 1); |
| 1238 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1239 | if (fPathRef->countVerbs() == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1240 | forceMoveTo = true; |
| 1241 | } |
| 1242 | this->incReserve(count); |
| 1243 | forceMoveTo ? this->moveTo(pts[0]) : this->lineTo(pts[0]); |
| 1244 | for (int i = 1; i < count; i += 2) { |
| 1245 | this->quadTo(pts[i], pts[i+1]); |
| 1246 | } |
| 1247 | } |
| 1248 | |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1249 | void SkPath::addArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1250 | if (oval.isEmpty() || 0 == sweepAngle) { |
| 1251 | return; |
| 1252 | } |
| 1253 | |
| 1254 | const SkScalar kFullCircleAngle = SkIntToScalar(360); |
| 1255 | |
| 1256 | if (sweepAngle >= kFullCircleAngle || sweepAngle <= -kFullCircleAngle) { |
| 1257 | this->addOval(oval, sweepAngle > 0 ? kCW_Direction : kCCW_Direction); |
| 1258 | return; |
| 1259 | } |
| 1260 | |
| 1261 | SkPoint pts[kSkBuildQuadArcStorage]; |
| 1262 | int count = build_arc_points(oval, startAngle, sweepAngle, pts); |
| 1263 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 1264 | SkDEBUGCODE(this->validate();) |
| 1265 | SkASSERT(count & 1); |
| 1266 | |
commit-bot@chromium.org | 5e1a7f2 | 2014-02-12 17:44:30 +0000 | [diff] [blame] | 1267 | fLastMoveToIndex = fPathRef->countPoints(); |
| 1268 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 1269 | SkPathRef::Editor ed(&fPathRef, 1+(count-1)/2, count); |
| 1270 | |
| 1271 | ed.growForVerb(kMove_Verb)->set(pts[0].fX, pts[0].fY); |
| 1272 | if (count > 1) { |
| 1273 | SkPoint* p = ed.growForRepeatedVerb(kQuad_Verb, (count-1)/2); |
| 1274 | memcpy(p, &pts[1], (count-1) * sizeof(SkPoint)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1275 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 1276 | |
| 1277 | DIRTY_AFTER_EDIT; |
| 1278 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | Need to handle the case when the angle is sharp, and our computed end-points |
| 1283 | for the arc go behind pt1 and/or p2... |
| 1284 | */ |
| 1285 | void SkPath::arcTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 1286 | SkScalar radius) { |
| 1287 | SkVector before, after; |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1288 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1289 | // need to know our prev pt so we can construct tangent vectors |
| 1290 | { |
| 1291 | SkPoint start; |
| 1292 | this->getLastPt(&start); |
senorblanco@chromium.org | 60eaa39 | 2010-10-13 18:47:00 +0000 | [diff] [blame] | 1293 | // Handle degenerate cases by adding a line to the first point and |
| 1294 | // bailing out. |
| 1295 | if ((x1 == start.fX && y1 == start.fY) || |
| 1296 | (x1 == x2 && y1 == y2) || |
| 1297 | radius == 0) { |
| 1298 | this->lineTo(x1, y1); |
| 1299 | return; |
| 1300 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1301 | before.setNormalize(x1 - start.fX, y1 - start.fY); |
| 1302 | after.setNormalize(x2 - x1, y2 - y1); |
| 1303 | } |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1304 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1305 | SkScalar cosh = SkPoint::DotProduct(before, after); |
| 1306 | SkScalar sinh = SkPoint::CrossProduct(before, after); |
| 1307 | |
| 1308 | if (SkScalarNearlyZero(sinh)) { // angle is too tight |
senorblanco@chromium.org | 60eaa39 | 2010-10-13 18:47:00 +0000 | [diff] [blame] | 1309 | this->lineTo(x1, y1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1310 | return; |
| 1311 | } |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1312 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1313 | SkScalar dist = SkScalarMulDiv(radius, SK_Scalar1 - cosh, sinh); |
| 1314 | if (dist < 0) { |
| 1315 | dist = -dist; |
| 1316 | } |
| 1317 | |
| 1318 | SkScalar xx = x1 - SkScalarMul(dist, before.fX); |
| 1319 | SkScalar yy = y1 - SkScalarMul(dist, before.fY); |
| 1320 | SkRotationDirection arcDir; |
| 1321 | |
| 1322 | // now turn before/after into normals |
| 1323 | if (sinh > 0) { |
| 1324 | before.rotateCCW(); |
| 1325 | after.rotateCCW(); |
| 1326 | arcDir = kCW_SkRotationDirection; |
| 1327 | } else { |
| 1328 | before.rotateCW(); |
| 1329 | after.rotateCW(); |
| 1330 | arcDir = kCCW_SkRotationDirection; |
| 1331 | } |
| 1332 | |
| 1333 | SkMatrix matrix; |
| 1334 | SkPoint pts[kSkBuildQuadArcStorage]; |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1335 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1336 | matrix.setScale(radius, radius); |
| 1337 | matrix.postTranslate(xx - SkScalarMul(radius, before.fX), |
| 1338 | yy - SkScalarMul(radius, before.fY)); |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1339 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1340 | int count = SkBuildQuadArc(before, after, arcDir, &matrix, pts); |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1341 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1342 | this->incReserve(count); |
| 1343 | // [xx,yy] == pts[0] |
| 1344 | this->lineTo(xx, yy); |
| 1345 | for (int i = 1; i < count; i += 2) { |
| 1346 | this->quadTo(pts[i], pts[i+1]); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | /////////////////////////////////////////////////////////////////////////////// |
| 1351 | |
commit-bot@chromium.org | 14747e5 | 2014-02-11 21:16:29 +0000 | [diff] [blame] | 1352 | void SkPath::addPath(const SkPath& path, SkScalar dx, SkScalar dy, AddPathMode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1353 | SkMatrix matrix; |
| 1354 | |
| 1355 | matrix.setTranslate(dx, dy); |
commit-bot@chromium.org | 14747e5 | 2014-02-11 21:16:29 +0000 | [diff] [blame] | 1356 | this->addPath(path, matrix, mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
commit-bot@chromium.org | 14747e5 | 2014-02-11 21:16:29 +0000 | [diff] [blame] | 1359 | void SkPath::addPath(const SkPath& path, const SkMatrix& matrix, AddPathMode mode) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1360 | SkPathRef::Editor(&fPathRef, path.countVerbs(), path.countPoints()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1361 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1362 | RawIter iter(path); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1363 | SkPoint pts[4]; |
| 1364 | Verb verb; |
| 1365 | |
| 1366 | SkMatrix::MapPtsProc proc = matrix.getMapPtsProc(); |
commit-bot@chromium.org | 14747e5 | 2014-02-11 21:16:29 +0000 | [diff] [blame] | 1367 | bool firstVerb = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1368 | while ((verb = iter.next(pts)) != kDone_Verb) { |
| 1369 | switch (verb) { |
| 1370 | case kMove_Verb: |
| 1371 | proc(matrix, &pts[0], &pts[0], 1); |
commit-bot@chromium.org | 14747e5 | 2014-02-11 21:16:29 +0000 | [diff] [blame] | 1372 | if (firstVerb && mode == kExtend_AddPathMode && !isEmpty()) { |
| 1373 | injectMoveToIfNeeded(); // In case last contour is closed |
| 1374 | this->lineTo(pts[0]); |
| 1375 | } else { |
| 1376 | this->moveTo(pts[0]); |
| 1377 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1378 | break; |
| 1379 | case kLine_Verb: |
| 1380 | proc(matrix, &pts[1], &pts[1], 1); |
| 1381 | this->lineTo(pts[1]); |
| 1382 | break; |
| 1383 | case kQuad_Verb: |
| 1384 | proc(matrix, &pts[1], &pts[1], 2); |
| 1385 | this->quadTo(pts[1], pts[2]); |
| 1386 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1387 | case kConic_Verb: |
| 1388 | proc(matrix, &pts[1], &pts[1], 2); |
| 1389 | this->conicTo(pts[1], pts[2], iter.conicWeight()); |
| 1390 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1391 | case kCubic_Verb: |
| 1392 | proc(matrix, &pts[1], &pts[1], 3); |
| 1393 | this->cubicTo(pts[1], pts[2], pts[3]); |
| 1394 | break; |
| 1395 | case kClose_Verb: |
| 1396 | this->close(); |
| 1397 | break; |
| 1398 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1399 | SkDEBUGFAIL("unknown verb"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1400 | } |
commit-bot@chromium.org | 14747e5 | 2014-02-11 21:16:29 +0000 | [diff] [blame] | 1401 | firstVerb = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /////////////////////////////////////////////////////////////////////////////// |
| 1406 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1407 | static int pts_in_verb(unsigned verb) { |
| 1408 | static const uint8_t gPtsInVerb[] = { |
| 1409 | 1, // kMove |
| 1410 | 1, // kLine |
| 1411 | 2, // kQuad |
| 1412 | 2, // kConic |
| 1413 | 3, // kCubic |
| 1414 | 0, // kClose |
| 1415 | 0 // kDone |
| 1416 | }; |
| 1417 | |
| 1418 | SkASSERT(verb < SK_ARRAY_COUNT(gPtsInVerb)); |
| 1419 | return gPtsInVerb[verb]; |
| 1420 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1421 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1422 | // ignore the last point of the 1st contour |
| 1423 | void SkPath::reversePathTo(const SkPath& path) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1424 | int i, vcount = path.fPathRef->countVerbs(); |
| 1425 | // exit early if the path is empty, or just has a moveTo. |
| 1426 | if (vcount < 2) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1427 | return; |
| 1428 | } |
| 1429 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1430 | SkPathRef::Editor(&fPathRef, vcount, path.countPoints()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1431 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1432 | const uint8_t* verbs = path.fPathRef->verbs(); |
| 1433 | const SkPoint* pts = path.fPathRef->points(); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1434 | const SkScalar* conicWeights = path.fPathRef->conicWeights(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1435 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1436 | SkASSERT(verbs[~0] == kMove_Verb); |
| 1437 | for (i = 1; i < vcount; ++i) { |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1438 | unsigned v = verbs[~i]; |
| 1439 | int n = pts_in_verb(v); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1440 | if (n == 0) { |
| 1441 | break; |
| 1442 | } |
| 1443 | pts += n; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1444 | conicWeights += (SkPath::kConic_Verb == v); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | while (--i > 0) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1448 | switch (verbs[~i]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1449 | case kLine_Verb: |
| 1450 | this->lineTo(pts[-1].fX, pts[-1].fY); |
| 1451 | break; |
| 1452 | case kQuad_Verb: |
| 1453 | this->quadTo(pts[-1].fX, pts[-1].fY, pts[-2].fX, pts[-2].fY); |
| 1454 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1455 | case kConic_Verb: |
| 1456 | this->conicTo(pts[-1], pts[-2], *--conicWeights); |
| 1457 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1458 | case kCubic_Verb: |
| 1459 | this->cubicTo(pts[-1].fX, pts[-1].fY, pts[-2].fX, pts[-2].fY, |
| 1460 | pts[-3].fX, pts[-3].fY); |
| 1461 | break; |
| 1462 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1463 | SkDEBUGFAIL("bad verb"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1464 | break; |
| 1465 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1466 | pts -= pts_in_verb(verbs[~i]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1470 | void SkPath::reverseAddPath(const SkPath& src) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1471 | SkPathRef::Editor ed(&fPathRef, src.fPathRef->countPoints(), src.fPathRef->countVerbs()); |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1472 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1473 | const SkPoint* pts = src.fPathRef->pointsEnd(); |
| 1474 | // we will iterator through src's verbs backwards |
| 1475 | const uint8_t* verbs = src.fPathRef->verbsMemBegin(); // points at the last verb |
| 1476 | const uint8_t* verbsEnd = src.fPathRef->verbs(); // points just past the first verb |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1477 | const SkScalar* conicWeights = src.fPathRef->conicWeightsEnd(); |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1478 | |
| 1479 | bool needMove = true; |
| 1480 | bool needClose = false; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1481 | while (verbs < verbsEnd) { |
| 1482 | uint8_t v = *(verbs++); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1483 | int n = pts_in_verb(v); |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1484 | |
| 1485 | if (needMove) { |
| 1486 | --pts; |
| 1487 | this->moveTo(pts->fX, pts->fY); |
| 1488 | needMove = false; |
| 1489 | } |
| 1490 | pts -= n; |
| 1491 | switch (v) { |
| 1492 | case kMove_Verb: |
| 1493 | if (needClose) { |
| 1494 | this->close(); |
| 1495 | needClose = false; |
| 1496 | } |
| 1497 | needMove = true; |
| 1498 | pts += 1; // so we see the point in "if (needMove)" above |
| 1499 | break; |
| 1500 | case kLine_Verb: |
| 1501 | this->lineTo(pts[0]); |
| 1502 | break; |
| 1503 | case kQuad_Verb: |
| 1504 | this->quadTo(pts[1], pts[0]); |
| 1505 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1506 | case kConic_Verb: |
| 1507 | this->conicTo(pts[1], pts[0], *--conicWeights); |
| 1508 | break; |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1509 | case kCubic_Verb: |
| 1510 | this->cubicTo(pts[2], pts[1], pts[0]); |
| 1511 | break; |
| 1512 | case kClose_Verb: |
| 1513 | needClose = true; |
| 1514 | break; |
| 1515 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1516 | SkDEBUGFAIL("unexpected verb"); |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | } |
| 1520 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1521 | /////////////////////////////////////////////////////////////////////////////// |
| 1522 | |
| 1523 | void SkPath::offset(SkScalar dx, SkScalar dy, SkPath* dst) const { |
| 1524 | SkMatrix matrix; |
| 1525 | |
| 1526 | matrix.setTranslate(dx, dy); |
| 1527 | this->transform(matrix, dst); |
| 1528 | } |
| 1529 | |
| 1530 | #include "SkGeometry.h" |
| 1531 | |
| 1532 | static void subdivide_quad_to(SkPath* path, const SkPoint pts[3], |
| 1533 | int level = 2) { |
| 1534 | if (--level >= 0) { |
| 1535 | SkPoint tmp[5]; |
| 1536 | |
| 1537 | SkChopQuadAtHalf(pts, tmp); |
| 1538 | subdivide_quad_to(path, &tmp[0], level); |
| 1539 | subdivide_quad_to(path, &tmp[2], level); |
| 1540 | } else { |
| 1541 | path->quadTo(pts[1], pts[2]); |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | static void subdivide_cubic_to(SkPath* path, const SkPoint pts[4], |
| 1546 | int level = 2) { |
| 1547 | if (--level >= 0) { |
| 1548 | SkPoint tmp[7]; |
| 1549 | |
| 1550 | SkChopCubicAtHalf(pts, tmp); |
| 1551 | subdivide_cubic_to(path, &tmp[0], level); |
| 1552 | subdivide_cubic_to(path, &tmp[3], level); |
| 1553 | } else { |
| 1554 | path->cubicTo(pts[1], pts[2], pts[3]); |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | void SkPath::transform(const SkMatrix& matrix, SkPath* dst) const { |
| 1559 | SkDEBUGCODE(this->validate();) |
| 1560 | if (dst == NULL) { |
| 1561 | dst = (SkPath*)this; |
| 1562 | } |
| 1563 | |
tomhudson@google.com | 8d43018 | 2011-06-06 19:11:19 +0000 | [diff] [blame] | 1564 | if (matrix.hasPerspective()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1565 | SkPath tmp; |
| 1566 | tmp.fFillType = fFillType; |
| 1567 | |
| 1568 | SkPath::Iter iter(*this, false); |
| 1569 | SkPoint pts[4]; |
| 1570 | SkPath::Verb verb; |
| 1571 | |
reed@google.com | 4a3b714 | 2012-05-16 17:16:46 +0000 | [diff] [blame] | 1572 | while ((verb = iter.next(pts, false)) != kDone_Verb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1573 | switch (verb) { |
| 1574 | case kMove_Verb: |
| 1575 | tmp.moveTo(pts[0]); |
| 1576 | break; |
| 1577 | case kLine_Verb: |
| 1578 | tmp.lineTo(pts[1]); |
| 1579 | break; |
| 1580 | case kQuad_Verb: |
| 1581 | subdivide_quad_to(&tmp, pts); |
| 1582 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1583 | case kConic_Verb: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1584 | SkDEBUGFAIL("TODO: compute new weight"); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1585 | tmp.conicTo(pts[1], pts[2], iter.conicWeight()); |
| 1586 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1587 | case kCubic_Verb: |
| 1588 | subdivide_cubic_to(&tmp, pts); |
| 1589 | break; |
| 1590 | case kClose_Verb: |
| 1591 | tmp.close(); |
| 1592 | break; |
| 1593 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1594 | SkDEBUGFAIL("unknown verb"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1595 | break; |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | dst->swap(tmp); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1600 | SkPathRef::Editor ed(&dst->fPathRef); |
| 1601 | matrix.mapPoints(ed.points(), ed.pathRef()->countPoints()); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1602 | dst->fDirection = kUnknown_Direction; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1603 | } else { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1604 | SkPathRef::CreateTransformedCopy(&dst->fPathRef, *fPathRef.get(), matrix); |
| 1605 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1606 | if (this != dst) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1607 | dst->fFillType = fFillType; |
reed@google.com | 2a6f8ab | 2011-10-25 18:41:23 +0000 | [diff] [blame] | 1608 | dst->fConvexity = fConvexity; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1609 | } |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1610 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1611 | if (kUnknown_Direction == fDirection) { |
| 1612 | dst->fDirection = kUnknown_Direction; |
| 1613 | } else { |
| 1614 | SkScalar det2x2 = |
| 1615 | SkScalarMul(matrix.get(SkMatrix::kMScaleX), matrix.get(SkMatrix::kMScaleY)) - |
| 1616 | SkScalarMul(matrix.get(SkMatrix::kMSkewX), matrix.get(SkMatrix::kMSkewY)); |
| 1617 | if (det2x2 < 0) { |
| 1618 | dst->fDirection = SkPath::OppositeDirection(static_cast<Direction>(fDirection)); |
| 1619 | } else if (det2x2 > 0) { |
| 1620 | dst->fDirection = fDirection; |
| 1621 | } else { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1622 | dst->fConvexity = kUnknown_Convexity; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1623 | dst->fDirection = kUnknown_Direction; |
| 1624 | } |
| 1625 | } |
| 1626 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1627 | SkDEBUGCODE(dst->validate();) |
| 1628 | } |
| 1629 | } |
| 1630 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1631 | /////////////////////////////////////////////////////////////////////////////// |
| 1632 | /////////////////////////////////////////////////////////////////////////////// |
| 1633 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1634 | enum SegmentState { |
schenney@chromium.org | fde6b41 | 2012-01-19 15:31:01 +0000 | [diff] [blame] | 1635 | kEmptyContour_SegmentState, // The current contour is empty. We may be |
| 1636 | // starting processing or we may have just |
| 1637 | // closed a contour. |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1638 | kAfterMove_SegmentState, // We have seen a move, but nothing else. |
| 1639 | kAfterPrimitive_SegmentState // We have seen a primitive but not yet |
| 1640 | // closed the path. Also the initial state. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1641 | }; |
| 1642 | |
| 1643 | SkPath::Iter::Iter() { |
| 1644 | #ifdef SK_DEBUG |
| 1645 | fPts = NULL; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1646 | fConicWeights = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1647 | fMoveTo.fX = fMoveTo.fY = fLastPt.fX = fLastPt.fY = 0; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1648 | fForceClose = fCloseLine = false; |
schenney@chromium.org | fde6b41 | 2012-01-19 15:31:01 +0000 | [diff] [blame] | 1649 | fSegmentState = kEmptyContour_SegmentState; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1650 | #endif |
| 1651 | // need to init enough to make next() harmlessly return kDone_Verb |
| 1652 | fVerbs = NULL; |
| 1653 | fVerbStop = NULL; |
| 1654 | fNeedClose = false; |
| 1655 | } |
| 1656 | |
| 1657 | SkPath::Iter::Iter(const SkPath& path, bool forceClose) { |
| 1658 | this->setPath(path, forceClose); |
| 1659 | } |
| 1660 | |
| 1661 | void SkPath::Iter::setPath(const SkPath& path, bool forceClose) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1662 | fPts = path.fPathRef->points(); |
| 1663 | fVerbs = path.fPathRef->verbs(); |
| 1664 | fVerbStop = path.fPathRef->verbsMemBegin(); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1665 | fConicWeights = path.fPathRef->conicWeights() - 1; // begin one behind |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1666 | fLastPt.fX = fLastPt.fY = 0; |
schenney@chromium.org | 72785c4 | 2011-12-29 21:03:28 +0000 | [diff] [blame] | 1667 | fMoveTo.fX = fMoveTo.fY = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1668 | fForceClose = SkToU8(forceClose); |
| 1669 | fNeedClose = false; |
schenney@chromium.org | fde6b41 | 2012-01-19 15:31:01 +0000 | [diff] [blame] | 1670 | fSegmentState = kEmptyContour_SegmentState; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | bool SkPath::Iter::isClosedContour() const { |
| 1674 | if (fVerbs == NULL || fVerbs == fVerbStop) { |
| 1675 | return false; |
| 1676 | } |
| 1677 | if (fForceClose) { |
| 1678 | return true; |
| 1679 | } |
| 1680 | |
| 1681 | const uint8_t* verbs = fVerbs; |
| 1682 | const uint8_t* stop = fVerbStop; |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1683 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1684 | if (kMove_Verb == *(verbs - 1)) { |
| 1685 | verbs -= 1; // skip the initial moveto |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1688 | while (verbs > stop) { |
| 1689 | // verbs points one beyond the current verb, decrement first. |
| 1690 | unsigned v = *(--verbs); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1691 | if (kMove_Verb == v) { |
| 1692 | break; |
| 1693 | } |
| 1694 | if (kClose_Verb == v) { |
| 1695 | return true; |
| 1696 | } |
| 1697 | } |
| 1698 | return false; |
| 1699 | } |
| 1700 | |
| 1701 | SkPath::Verb SkPath::Iter::autoClose(SkPoint pts[2]) { |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1702 | SkASSERT(pts); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1703 | if (fLastPt != fMoveTo) { |
reed@android.com | 4ddfe35 | 2009-03-20 12:16:09 +0000 | [diff] [blame] | 1704 | // A special case: if both points are NaN, SkPoint::operation== returns |
| 1705 | // false, but the iterator expects that they are treated as the same. |
| 1706 | // (consider SkPoint is a 2-dimension float point). |
reed@android.com | 9da1ae3 | 2009-07-22 17:06:15 +0000 | [diff] [blame] | 1707 | if (SkScalarIsNaN(fLastPt.fX) || SkScalarIsNaN(fLastPt.fY) || |
| 1708 | SkScalarIsNaN(fMoveTo.fX) || SkScalarIsNaN(fMoveTo.fY)) { |
reed@android.com | 4ddfe35 | 2009-03-20 12:16:09 +0000 | [diff] [blame] | 1709 | return kClose_Verb; |
| 1710 | } |
| 1711 | |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1712 | pts[0] = fLastPt; |
| 1713 | pts[1] = fMoveTo; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1714 | fLastPt = fMoveTo; |
| 1715 | fCloseLine = true; |
| 1716 | return kLine_Verb; |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 1717 | } else { |
| 1718 | pts[0] = fMoveTo; |
| 1719 | return kClose_Verb; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1720 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1723 | const SkPoint& SkPath::Iter::cons_moveTo() { |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1724 | if (fSegmentState == kAfterMove_SegmentState) { |
| 1725 | // Set the first return pt to the move pt |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1726 | fSegmentState = kAfterPrimitive_SegmentState; |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1727 | return fMoveTo; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1728 | } else { |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1729 | SkASSERT(fSegmentState == kAfterPrimitive_SegmentState); |
| 1730 | // Set the first return pt to the last pt of the previous primitive. |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1731 | return fPts[-1]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1732 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1735 | void SkPath::Iter::consumeDegenerateSegments() { |
| 1736 | // We need to step over anything that will not move the current draw point |
| 1737 | // forward before the next move is seen |
| 1738 | const uint8_t* lastMoveVerb = 0; |
| 1739 | const SkPoint* lastMovePt = 0; |
| 1740 | SkPoint lastPt = fLastPt; |
| 1741 | while (fVerbs != fVerbStop) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1742 | unsigned verb = *(fVerbs - 1); // fVerbs is one beyond the current verb |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1743 | switch (verb) { |
| 1744 | case kMove_Verb: |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1745 | // Keep a record of this most recent move |
| 1746 | lastMoveVerb = fVerbs; |
| 1747 | lastMovePt = fPts; |
schenney@chromium.org | b0af6da | 2011-12-21 20:43:13 +0000 | [diff] [blame] | 1748 | lastPt = fPts[0]; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1749 | fVerbs--; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1750 | fPts++; |
| 1751 | break; |
| 1752 | |
| 1753 | case kClose_Verb: |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1754 | // A close when we are in a segment is always valid except when it |
| 1755 | // follows a move which follows a segment. |
| 1756 | if (fSegmentState == kAfterPrimitive_SegmentState && !lastMoveVerb) { |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1757 | return; |
| 1758 | } |
| 1759 | // A close at any other time must be ignored |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1760 | fVerbs--; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1761 | break; |
| 1762 | |
| 1763 | case kLine_Verb: |
| 1764 | if (!IsLineDegenerate(lastPt, fPts[0])) { |
| 1765 | if (lastMoveVerb) { |
| 1766 | fVerbs = lastMoveVerb; |
| 1767 | fPts = lastMovePt; |
| 1768 | return; |
| 1769 | } |
| 1770 | return; |
| 1771 | } |
| 1772 | // Ignore this line and continue |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1773 | fVerbs--; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1774 | fPts++; |
| 1775 | break; |
| 1776 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1777 | case kConic_Verb: |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1778 | case kQuad_Verb: |
| 1779 | if (!IsQuadDegenerate(lastPt, fPts[0], fPts[1])) { |
| 1780 | if (lastMoveVerb) { |
| 1781 | fVerbs = lastMoveVerb; |
| 1782 | fPts = lastMovePt; |
| 1783 | return; |
| 1784 | } |
| 1785 | return; |
| 1786 | } |
| 1787 | // Ignore this line and continue |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1788 | fVerbs--; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1789 | fPts += 2; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1790 | fConicWeights += (kConic_Verb == verb); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1791 | break; |
| 1792 | |
| 1793 | case kCubic_Verb: |
| 1794 | if (!IsCubicDegenerate(lastPt, fPts[0], fPts[1], fPts[2])) { |
| 1795 | if (lastMoveVerb) { |
| 1796 | fVerbs = lastMoveVerb; |
| 1797 | fPts = lastMovePt; |
| 1798 | return; |
| 1799 | } |
| 1800 | return; |
| 1801 | } |
| 1802 | // Ignore this line and continue |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1803 | fVerbs--; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1804 | fPts += 3; |
| 1805 | break; |
schenney@chromium.org | b0af6da | 2011-12-21 20:43:13 +0000 | [diff] [blame] | 1806 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1807 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1808 | SkDEBUGFAIL("Should never see kDone_Verb"); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | |
reed@google.com | 4a3b714 | 2012-05-16 17:16:46 +0000 | [diff] [blame] | 1813 | SkPath::Verb SkPath::Iter::doNext(SkPoint ptsParam[4]) { |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1814 | SkASSERT(ptsParam); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1815 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1816 | if (fVerbs == fVerbStop) { |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1817 | // Close the curve if requested and if there is some curve to close |
| 1818 | if (fNeedClose && fSegmentState == kAfterPrimitive_SegmentState) { |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1819 | if (kLine_Verb == this->autoClose(ptsParam)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1820 | return kLine_Verb; |
| 1821 | } |
| 1822 | fNeedClose = false; |
| 1823 | return kClose_Verb; |
| 1824 | } |
| 1825 | return kDone_Verb; |
| 1826 | } |
| 1827 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1828 | // fVerbs is one beyond the current verb, decrement first |
| 1829 | unsigned verb = *(--fVerbs); |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1830 | const SkPoint* SK_RESTRICT srcPts = fPts; |
| 1831 | SkPoint* SK_RESTRICT pts = ptsParam; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1832 | |
| 1833 | switch (verb) { |
| 1834 | case kMove_Verb: |
| 1835 | if (fNeedClose) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1836 | fVerbs++; // move back one verb |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1837 | verb = this->autoClose(pts); |
| 1838 | if (verb == kClose_Verb) { |
| 1839 | fNeedClose = false; |
| 1840 | } |
| 1841 | return (Verb)verb; |
| 1842 | } |
| 1843 | if (fVerbs == fVerbStop) { // might be a trailing moveto |
| 1844 | return kDone_Verb; |
| 1845 | } |
schenney@chromium.org | b0af6da | 2011-12-21 20:43:13 +0000 | [diff] [blame] | 1846 | fMoveTo = *srcPts; |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1847 | pts[0] = *srcPts; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1848 | srcPts += 1; |
schenney@chromium.org | b0af6da | 2011-12-21 20:43:13 +0000 | [diff] [blame] | 1849 | fSegmentState = kAfterMove_SegmentState; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1850 | fLastPt = fMoveTo; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1851 | fNeedClose = fForceClose; |
| 1852 | break; |
| 1853 | case kLine_Verb: |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1854 | pts[0] = this->cons_moveTo(); |
| 1855 | pts[1] = srcPts[0]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1856 | fLastPt = srcPts[0]; |
| 1857 | fCloseLine = false; |
| 1858 | srcPts += 1; |
| 1859 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1860 | case kConic_Verb: |
| 1861 | fConicWeights += 1; |
| 1862 | // fall-through |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1863 | case kQuad_Verb: |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1864 | pts[0] = this->cons_moveTo(); |
| 1865 | memcpy(&pts[1], srcPts, 2 * sizeof(SkPoint)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1866 | fLastPt = srcPts[1]; |
| 1867 | srcPts += 2; |
| 1868 | break; |
| 1869 | case kCubic_Verb: |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1870 | pts[0] = this->cons_moveTo(); |
| 1871 | memcpy(&pts[1], srcPts, 3 * sizeof(SkPoint)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1872 | fLastPt = srcPts[2]; |
| 1873 | srcPts += 3; |
| 1874 | break; |
| 1875 | case kClose_Verb: |
| 1876 | verb = this->autoClose(pts); |
| 1877 | if (verb == kLine_Verb) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1878 | fVerbs++; // move back one verb |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1879 | } else { |
| 1880 | fNeedClose = false; |
schenney@chromium.org | fde6b41 | 2012-01-19 15:31:01 +0000 | [diff] [blame] | 1881 | fSegmentState = kEmptyContour_SegmentState; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1882 | } |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1883 | fLastPt = fMoveTo; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1884 | break; |
| 1885 | } |
| 1886 | fPts = srcPts; |
| 1887 | return (Verb)verb; |
| 1888 | } |
| 1889 | |
| 1890 | /////////////////////////////////////////////////////////////////////////////// |
| 1891 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1892 | SkPath::RawIter::RawIter() { |
| 1893 | #ifdef SK_DEBUG |
| 1894 | fPts = NULL; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1895 | fConicWeights = NULL; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1896 | fMoveTo.fX = fMoveTo.fY = fLastPt.fX = fLastPt.fY = 0; |
| 1897 | #endif |
| 1898 | // need to init enough to make next() harmlessly return kDone_Verb |
| 1899 | fVerbs = NULL; |
| 1900 | fVerbStop = NULL; |
| 1901 | } |
| 1902 | |
| 1903 | SkPath::RawIter::RawIter(const SkPath& path) { |
| 1904 | this->setPath(path); |
| 1905 | } |
| 1906 | |
| 1907 | void SkPath::RawIter::setPath(const SkPath& path) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1908 | fPts = path.fPathRef->points(); |
| 1909 | fVerbs = path.fPathRef->verbs(); |
| 1910 | fVerbStop = path.fPathRef->verbsMemBegin(); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1911 | fConicWeights = path.fPathRef->conicWeights() - 1; // begin one behind |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1912 | fMoveTo.fX = fMoveTo.fY = 0; |
| 1913 | fLastPt.fX = fLastPt.fY = 0; |
| 1914 | } |
| 1915 | |
| 1916 | SkPath::Verb SkPath::RawIter::next(SkPoint pts[4]) { |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 1917 | SkASSERT(NULL != pts); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1918 | if (fVerbs == fVerbStop) { |
| 1919 | return kDone_Verb; |
| 1920 | } |
| 1921 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1922 | // fVerbs points one beyond next verb so decrement first. |
| 1923 | unsigned verb = *(--fVerbs); |
| 1924 | const SkPoint* srcPts = fPts; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1925 | |
| 1926 | switch (verb) { |
| 1927 | case kMove_Verb: |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 1928 | pts[0] = *srcPts; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1929 | fMoveTo = srcPts[0]; |
| 1930 | fLastPt = fMoveTo; |
| 1931 | srcPts += 1; |
| 1932 | break; |
| 1933 | case kLine_Verb: |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 1934 | pts[0] = fLastPt; |
| 1935 | pts[1] = srcPts[0]; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1936 | fLastPt = srcPts[0]; |
| 1937 | srcPts += 1; |
| 1938 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1939 | case kConic_Verb: |
| 1940 | fConicWeights += 1; |
| 1941 | // fall-through |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1942 | case kQuad_Verb: |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 1943 | pts[0] = fLastPt; |
| 1944 | memcpy(&pts[1], srcPts, 2 * sizeof(SkPoint)); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1945 | fLastPt = srcPts[1]; |
| 1946 | srcPts += 2; |
| 1947 | break; |
| 1948 | case kCubic_Verb: |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 1949 | pts[0] = fLastPt; |
| 1950 | memcpy(&pts[1], srcPts, 3 * sizeof(SkPoint)); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1951 | fLastPt = srcPts[2]; |
| 1952 | srcPts += 3; |
| 1953 | break; |
| 1954 | case kClose_Verb: |
| 1955 | fLastPt = fMoveTo; |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 1956 | pts[0] = fMoveTo; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1957 | break; |
| 1958 | } |
| 1959 | fPts = srcPts; |
| 1960 | return (Verb)verb; |
| 1961 | } |
| 1962 | |
| 1963 | /////////////////////////////////////////////////////////////////////////////// |
| 1964 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1965 | /* |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1966 | Format in compressed buffer: [ptCount, verbCount, pts[], verbs[]] |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1967 | */ |
| 1968 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 1969 | size_t SkPath::writeToMemory(void* storage) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1970 | SkDEBUGCODE(this->validate();) |
| 1971 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1972 | if (NULL == storage) { |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 1973 | const int byteCount = sizeof(int32_t) + fPathRef->writeSize(); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1974 | return SkAlign4(byteCount); |
| 1975 | } |
| 1976 | |
| 1977 | SkWBuffer buffer(storage); |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 1978 | |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 1979 | int32_t packed = (fConvexity << kConvexity_SerializationShift) | |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 1980 | (fFillType << kFillType_SerializationShift) | |
commit-bot@chromium.org | fed2ab6 | 2014-01-23 15:16:05 +0000 | [diff] [blame] | 1981 | (fDirection << kDirection_SerializationShift); |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 1982 | |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 1983 | buffer.write32(packed); |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 1984 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1985 | fPathRef->writeToBuffer(&buffer); |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 1986 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1987 | buffer.padToAlign4(); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 1988 | return buffer.pos(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 1991 | size_t SkPath::readFromMemory(const void* storage, size_t length) { |
| 1992 | SkRBufferWithSizeCheck buffer(storage, length); |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 1993 | |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 1994 | int32_t packed; |
| 1995 | if (!buffer.readS32(&packed)) { |
| 1996 | return 0; |
| 1997 | } |
| 1998 | |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 1999 | fConvexity = (packed >> kConvexity_SerializationShift) & 0xFF; |
| 2000 | fFillType = (packed >> kFillType_SerializationShift) & 0xFF; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2001 | fDirection = (packed >> kDirection_SerializationShift) & 0x3; |
commit-bot@chromium.org | fed2ab6 | 2014-01-23 15:16:05 +0000 | [diff] [blame] | 2002 | SkPathRef* pathRef = SkPathRef::CreateFromBuffer(&buffer); |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 2003 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 2004 | size_t sizeRead = 0; |
| 2005 | if (buffer.isValid()) { |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 2006 | fPathRef.reset(pathRef); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 2007 | SkDEBUGCODE(this->validate();) |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 2008 | buffer.skipToAlign4(); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 2009 | sizeRead = buffer.pos(); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 2010 | } else if (NULL != pathRef) { |
| 2011 | // If the buffer is not valid, pathRef should be NULL |
| 2012 | sk_throw(); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 2013 | } |
| 2014 | return sizeRead; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2018 | |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2019 | #include "SkString.h" |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 2020 | #include "SkStream.h" |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2021 | |
| 2022 | static void append_scalar(SkString* str, SkScalar value) { |
| 2023 | SkString tmp; |
| 2024 | tmp.printf("%g", value); |
| 2025 | if (tmp.contains('.')) { |
| 2026 | tmp.appendUnichar('f'); |
| 2027 | } |
| 2028 | str->append(tmp); |
| 2029 | } |
| 2030 | |
| 2031 | static void append_params(SkString* str, const char label[], const SkPoint pts[], |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2032 | int count, SkScalar conicWeight = -1) { |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2033 | str->append(label); |
| 2034 | str->append("("); |
skia.committer@gmail.com | 15dd300 | 2013-01-18 07:07:28 +0000 | [diff] [blame] | 2035 | |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2036 | const SkScalar* values = &pts[0].fX; |
| 2037 | count *= 2; |
| 2038 | |
| 2039 | for (int i = 0; i < count; ++i) { |
| 2040 | append_scalar(str, values[i]); |
| 2041 | if (i < count - 1) { |
| 2042 | str->append(", "); |
| 2043 | } |
| 2044 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2045 | if (conicWeight >= 0) { |
| 2046 | str->append(", "); |
| 2047 | append_scalar(str, conicWeight); |
| 2048 | } |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2049 | str->append(");\n"); |
| 2050 | } |
| 2051 | |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 2052 | void SkPath::dump(SkWStream* wStream, bool forceClose) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2053 | Iter iter(*this, forceClose); |
| 2054 | SkPoint pts[4]; |
| 2055 | Verb verb; |
| 2056 | |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 2057 | if (!wStream) { |
| 2058 | SkDebugf("path: forceClose=%s\n", forceClose ? "true" : "false"); |
| 2059 | } |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2060 | SkString builder; |
| 2061 | |
reed@google.com | 4a3b714 | 2012-05-16 17:16:46 +0000 | [diff] [blame] | 2062 | while ((verb = iter.next(pts, false)) != kDone_Verb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2063 | switch (verb) { |
| 2064 | case kMove_Verb: |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2065 | append_params(&builder, "path.moveTo", &pts[0], 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2066 | break; |
| 2067 | case kLine_Verb: |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2068 | append_params(&builder, "path.lineTo", &pts[1], 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2069 | break; |
| 2070 | case kQuad_Verb: |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2071 | append_params(&builder, "path.quadTo", &pts[1], 2); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2072 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2073 | case kConic_Verb: |
| 2074 | append_params(&builder, "path.conicTo", &pts[1], 2, iter.conicWeight()); |
| 2075 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2076 | case kCubic_Verb: |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2077 | append_params(&builder, "path.cubicTo", &pts[1], 3); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2078 | break; |
| 2079 | case kClose_Verb: |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 2080 | builder.append("path.close();\n"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2081 | break; |
| 2082 | default: |
| 2083 | SkDebugf(" path: UNKNOWN VERB %d, aborting dump...\n", verb); |
| 2084 | verb = kDone_Verb; // stop the loop |
| 2085 | break; |
| 2086 | } |
| 2087 | } |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 2088 | if (wStream) { |
| 2089 | wStream->writeText(builder.c_str()); |
| 2090 | } else { |
| 2091 | SkDebugf("%s", builder.c_str()); |
| 2092 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2095 | void SkPath::dump() const { |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 2096 | this->dump(NULL, false); |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | #ifdef SK_DEBUG |
| 2100 | void SkPath::validate() const { |
| 2101 | SkASSERT(this != NULL); |
| 2102 | SkASSERT((fFillType & ~3) == 0); |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 2103 | |
djsollen@google.com | 077348c | 2012-10-22 20:23:32 +0000 | [diff] [blame] | 2104 | #ifdef SK_DEBUG_PATH |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2105 | if (!fBoundsIsDirty) { |
| 2106 | SkRect bounds; |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 2107 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2108 | bool isFinite = compute_pt_bounds(&bounds, *fPathRef.get()); |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 2109 | SkASSERT(SkToBool(fIsFinite) == isFinite); |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 2110 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2111 | if (fPathRef->countPoints() <= 1) { |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2112 | // if we're empty, fBounds may be empty but translated, so we can't |
| 2113 | // necessarily compare to bounds directly |
| 2114 | // try path.addOval(2, 2, 2, 2) which is empty, but the bounds will |
| 2115 | // be [2, 2, 2, 2] |
| 2116 | SkASSERT(bounds.isEmpty()); |
| 2117 | SkASSERT(fBounds.isEmpty()); |
| 2118 | } else { |
reed@google.com | eac52bd | 2011-11-14 18:13:59 +0000 | [diff] [blame] | 2119 | if (bounds.isEmpty()) { |
| 2120 | SkASSERT(fBounds.isEmpty()); |
| 2121 | } else { |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 2122 | if (!fBounds.isEmpty()) { |
| 2123 | SkASSERT(fBounds.contains(bounds)); |
| 2124 | } |
reed@google.com | eac52bd | 2011-11-14 18:13:59 +0000 | [diff] [blame] | 2125 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2126 | } |
| 2127 | } |
djsollen@google.com | 077348c | 2012-10-22 20:23:32 +0000 | [diff] [blame] | 2128 | #endif // SK_DEBUG_PATH |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2129 | } |
djsollen@google.com | 077348c | 2012-10-22 20:23:32 +0000 | [diff] [blame] | 2130 | #endif // SK_DEBUG |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2131 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2132 | /////////////////////////////////////////////////////////////////////////////// |
| 2133 | |
reed@google.com | 0b7b982 | 2011-05-16 12:29:27 +0000 | [diff] [blame] | 2134 | static int sign(SkScalar x) { return x < 0; } |
| 2135 | #define kValueNeverReturnedBySign 2 |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2136 | |
commit-bot@chromium.org | 8be07bb | 2014-05-22 14:58:53 +0000 | [diff] [blame] | 2137 | static bool almost_equal(SkScalar compA, SkScalar compB) { |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 2138 | // The error epsilon was empirically derived; worse case round rects |
| 2139 | // with a mid point outset by 2x float epsilon in tests had an error |
| 2140 | // of 12. |
| 2141 | const int epsilon = 16; |
| 2142 | if (!SkScalarIsFinite(compA) || !SkScalarIsFinite(compB)) { |
| 2143 | return false; |
| 2144 | } |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2145 | // no need to check for small numbers because SkPath::Iter has removed degenerate values |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 2146 | int aBits = SkFloatAs2sCompliment(compA); |
| 2147 | int bBits = SkFloatAs2sCompliment(compB); |
| 2148 | return aBits < bBits + epsilon && bBits < aBits + epsilon; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | // only valid for a single contour |
| 2152 | struct Convexicator { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2153 | Convexicator() |
| 2154 | : fPtCount(0) |
| 2155 | , fConvexity(SkPath::kConvex_Convexity) |
| 2156 | , fDirection(SkPath::kUnknown_Direction) { |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2157 | fSign = 0; |
| 2158 | // warnings |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 2159 | fLastPt.set(0, 0); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2160 | fCurrPt.set(0, 0); |
commit-bot@chromium.org | 8be07bb | 2014-05-22 14:58:53 +0000 | [diff] [blame] | 2161 | fLastVec.set(0, 0); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2162 | fFirstVec.set(0, 0); |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2163 | |
| 2164 | fDx = fDy = 0; |
reed@google.com | 0b7b982 | 2011-05-16 12:29:27 +0000 | [diff] [blame] | 2165 | fSx = fSy = kValueNeverReturnedBySign; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | SkPath::Convexity getConvexity() const { return fConvexity; } |
| 2169 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2170 | /** The direction returned is only valid if the path is determined convex */ |
| 2171 | SkPath::Direction getDirection() const { return fDirection; } |
| 2172 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2173 | void addPt(const SkPoint& pt) { |
| 2174 | if (SkPath::kConcave_Convexity == fConvexity) { |
| 2175 | return; |
| 2176 | } |
| 2177 | |
| 2178 | if (0 == fPtCount) { |
| 2179 | fCurrPt = pt; |
| 2180 | ++fPtCount; |
| 2181 | } else { |
| 2182 | SkVector vec = pt - fCurrPt; |
| 2183 | if (vec.fX || vec.fY) { |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 2184 | fLastPt = fCurrPt; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2185 | fCurrPt = pt; |
| 2186 | if (++fPtCount == 2) { |
commit-bot@chromium.org | 8be07bb | 2014-05-22 14:58:53 +0000 | [diff] [blame] | 2187 | fFirstVec = fLastVec = vec; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2188 | } else { |
| 2189 | SkASSERT(fPtCount > 2); |
| 2190 | this->addVec(vec); |
| 2191 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2192 | |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2193 | int sx = sign(vec.fX); |
| 2194 | int sy = sign(vec.fY); |
| 2195 | fDx += (sx != fSx); |
| 2196 | fDy += (sy != fSy); |
| 2197 | fSx = sx; |
| 2198 | fSy = sy; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2199 | |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2200 | if (fDx > 3 || fDy > 3) { |
| 2201 | fConvexity = SkPath::kConcave_Convexity; |
| 2202 | } |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | void close() { |
| 2208 | if (fPtCount > 2) { |
| 2209 | this->addVec(fFirstVec); |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | private: |
| 2214 | void addVec(const SkVector& vec) { |
| 2215 | SkASSERT(vec.fX || vec.fY); |
commit-bot@chromium.org | 8be07bb | 2014-05-22 14:58:53 +0000 | [diff] [blame] | 2216 | SkScalar cross = SkPoint::CrossProduct(fLastVec, vec); |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 2217 | SkScalar smallest = SkTMin(fCurrPt.fX, SkTMin(fCurrPt.fY, SkTMin(fLastPt.fX, fLastPt.fY))); |
| 2218 | SkScalar largest = SkTMax(fCurrPt.fX, SkTMax(fCurrPt.fY, SkTMax(fLastPt.fX, fLastPt.fY))); |
| 2219 | largest = SkTMax(largest, -smallest); |
commit-bot@chromium.org | 8be07bb | 2014-05-22 14:58:53 +0000 | [diff] [blame] | 2220 | if (!almost_equal(largest, largest + cross)) { |
| 2221 | int sign = SkScalarSignAsInt(cross); |
| 2222 | if (0 == fSign) { |
| 2223 | fSign = sign; |
| 2224 | fDirection = (1 == sign) ? SkPath::kCW_Direction : SkPath::kCCW_Direction; |
| 2225 | } else if (sign && fSign != sign) { |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2226 | fConvexity = SkPath::kConcave_Convexity; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2227 | fDirection = SkPath::kUnknown_Direction; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2228 | } |
commit-bot@chromium.org | 8be07bb | 2014-05-22 14:58:53 +0000 | [diff] [blame] | 2229 | fLastVec = vec; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2230 | } |
| 2231 | } |
| 2232 | |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 2233 | SkPoint fLastPt; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2234 | SkPoint fCurrPt; |
commit-bot@chromium.org | 8be07bb | 2014-05-22 14:58:53 +0000 | [diff] [blame] | 2235 | // fLastVec does not necessarily start at fLastPt. We only advance it when the cross product |
| 2236 | // value with the current vec is deemed to be of a significant value. |
| 2237 | SkVector fLastVec, fFirstVec; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2238 | int fPtCount; // non-degenerate points |
| 2239 | int fSign; |
| 2240 | SkPath::Convexity fConvexity; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2241 | SkPath::Direction fDirection; |
reed@google.com | 0b7b982 | 2011-05-16 12:29:27 +0000 | [diff] [blame] | 2242 | int fDx, fDy, fSx, fSy; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2243 | }; |
| 2244 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2245 | SkPath::Convexity SkPath::internalGetConvexity() const { |
| 2246 | SkASSERT(kUnknown_Convexity == fConvexity); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2247 | SkPoint pts[4]; |
| 2248 | SkPath::Verb verb; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2249 | SkPath::Iter iter(*this, true); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2250 | |
| 2251 | int contourCount = 0; |
| 2252 | int count; |
| 2253 | Convexicator state; |
| 2254 | |
| 2255 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 2256 | switch (verb) { |
| 2257 | case kMove_Verb: |
| 2258 | if (++contourCount > 1) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2259 | fConvexity = kConcave_Convexity; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2260 | return kConcave_Convexity; |
| 2261 | } |
| 2262 | pts[1] = pts[0]; |
| 2263 | count = 1; |
| 2264 | break; |
| 2265 | case kLine_Verb: count = 1; break; |
| 2266 | case kQuad_Verb: count = 2; break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2267 | case kConic_Verb: count = 2; break; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2268 | case kCubic_Verb: count = 3; break; |
| 2269 | case kClose_Verb: |
| 2270 | state.close(); |
| 2271 | count = 0; |
| 2272 | break; |
| 2273 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 2274 | SkDEBUGFAIL("bad verb"); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2275 | fConvexity = kConcave_Convexity; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2276 | return kConcave_Convexity; |
| 2277 | } |
| 2278 | |
| 2279 | for (int i = 1; i <= count; i++) { |
| 2280 | state.addPt(pts[i]); |
| 2281 | } |
| 2282 | // early exit |
| 2283 | if (kConcave_Convexity == state.getConvexity()) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2284 | fConvexity = kConcave_Convexity; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2285 | return kConcave_Convexity; |
| 2286 | } |
| 2287 | } |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2288 | fConvexity = state.getConvexity(); |
| 2289 | if (kConvex_Convexity == fConvexity && kUnknown_Direction == fDirection) { |
| 2290 | fDirection = state.getDirection(); |
| 2291 | } |
| 2292 | return static_cast<Convexity>(fConvexity); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2293 | } |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2294 | |
| 2295 | /////////////////////////////////////////////////////////////////////////////// |
| 2296 | |
| 2297 | class ContourIter { |
| 2298 | public: |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2299 | ContourIter(const SkPathRef& pathRef); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2300 | |
| 2301 | bool done() const { return fDone; } |
| 2302 | // if !done() then these may be called |
| 2303 | int count() const { return fCurrPtCount; } |
| 2304 | const SkPoint* pts() const { return fCurrPt; } |
| 2305 | void next(); |
| 2306 | |
| 2307 | private: |
| 2308 | int fCurrPtCount; |
| 2309 | const SkPoint* fCurrPt; |
| 2310 | const uint8_t* fCurrVerb; |
| 2311 | const uint8_t* fStopVerbs; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2312 | const SkScalar* fCurrConicWeight; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2313 | bool fDone; |
reed@google.com | d1ab932 | 2012-01-10 18:40:03 +0000 | [diff] [blame] | 2314 | SkDEBUGCODE(int fContourCounter;) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2315 | }; |
| 2316 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2317 | ContourIter::ContourIter(const SkPathRef& pathRef) { |
| 2318 | fStopVerbs = pathRef.verbsMemBegin(); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2319 | fDone = false; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2320 | fCurrPt = pathRef.points(); |
| 2321 | fCurrVerb = pathRef.verbs(); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2322 | fCurrConicWeight = pathRef.conicWeights(); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2323 | fCurrPtCount = 0; |
reed@google.com | d1ab932 | 2012-01-10 18:40:03 +0000 | [diff] [blame] | 2324 | SkDEBUGCODE(fContourCounter = 0;) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2325 | this->next(); |
| 2326 | } |
| 2327 | |
| 2328 | void ContourIter::next() { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2329 | if (fCurrVerb <= fStopVerbs) { |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2330 | fDone = true; |
| 2331 | } |
| 2332 | if (fDone) { |
| 2333 | return; |
| 2334 | } |
| 2335 | |
| 2336 | // skip pts of prev contour |
| 2337 | fCurrPt += fCurrPtCount; |
| 2338 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2339 | SkASSERT(SkPath::kMove_Verb == fCurrVerb[~0]); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2340 | int ptCount = 1; // moveTo |
| 2341 | const uint8_t* verbs = fCurrVerb; |
| 2342 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2343 | for (--verbs; verbs > fStopVerbs; --verbs) { |
| 2344 | switch (verbs[~0]) { |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2345 | case SkPath::kMove_Verb: |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2346 | goto CONTOUR_END; |
| 2347 | case SkPath::kLine_Verb: |
| 2348 | ptCount += 1; |
| 2349 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2350 | case SkPath::kConic_Verb: |
| 2351 | fCurrConicWeight += 1; |
| 2352 | // fall-through |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2353 | case SkPath::kQuad_Verb: |
| 2354 | ptCount += 2; |
| 2355 | break; |
| 2356 | case SkPath::kCubic_Verb: |
| 2357 | ptCount += 3; |
| 2358 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2359 | case SkPath::kClose_Verb: |
| 2360 | break; |
| 2361 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 2362 | SkDEBUGFAIL("unexpected verb"); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2363 | break; |
| 2364 | } |
| 2365 | } |
| 2366 | CONTOUR_END: |
| 2367 | fCurrPtCount = ptCount; |
| 2368 | fCurrVerb = verbs; |
reed@google.com | d1ab932 | 2012-01-10 18:40:03 +0000 | [diff] [blame] | 2369 | SkDEBUGCODE(++fContourCounter;) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 2372 | // returns cross product of (p1 - p0) and (p2 - p0) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2373 | static SkScalar cross_prod(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) { |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 2374 | SkScalar cross = SkPoint::CrossProduct(p1 - p0, p2 - p0); |
| 2375 | // We may get 0 when the above subtracts underflow. We expect this to be |
| 2376 | // very rare and lazily promote to double. |
| 2377 | if (0 == cross) { |
| 2378 | double p0x = SkScalarToDouble(p0.fX); |
| 2379 | double p0y = SkScalarToDouble(p0.fY); |
| 2380 | |
| 2381 | double p1x = SkScalarToDouble(p1.fX); |
| 2382 | double p1y = SkScalarToDouble(p1.fY); |
| 2383 | |
| 2384 | double p2x = SkScalarToDouble(p2.fX); |
| 2385 | double p2y = SkScalarToDouble(p2.fY); |
| 2386 | |
| 2387 | cross = SkDoubleToScalar((p1x - p0x) * (p2y - p0y) - |
| 2388 | (p1y - p0y) * (p2x - p0x)); |
| 2389 | |
| 2390 | } |
| 2391 | return cross; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2394 | // Returns the first pt with the maximum Y coordinate |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2395 | static int find_max_y(const SkPoint pts[], int count) { |
| 2396 | SkASSERT(count > 0); |
| 2397 | SkScalar max = pts[0].fY; |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2398 | int firstIndex = 0; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2399 | for (int i = 1; i < count; ++i) { |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2400 | SkScalar y = pts[i].fY; |
| 2401 | if (y > max) { |
| 2402 | max = y; |
| 2403 | firstIndex = i; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2404 | } |
| 2405 | } |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2406 | return firstIndex; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2409 | static int find_diff_pt(const SkPoint pts[], int index, int n, int inc) { |
| 2410 | int i = index; |
| 2411 | for (;;) { |
| 2412 | i = (i + inc) % n; |
| 2413 | if (i == index) { // we wrapped around, so abort |
| 2414 | break; |
| 2415 | } |
| 2416 | if (pts[index] != pts[i]) { // found a different point, success! |
| 2417 | break; |
| 2418 | } |
| 2419 | } |
| 2420 | return i; |
| 2421 | } |
| 2422 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2423 | /** |
| 2424 | * Starting at index, and moving forward (incrementing), find the xmin and |
| 2425 | * xmax of the contiguous points that have the same Y. |
| 2426 | */ |
| 2427 | static int find_min_max_x_at_y(const SkPoint pts[], int index, int n, |
| 2428 | int* maxIndexPtr) { |
| 2429 | const SkScalar y = pts[index].fY; |
| 2430 | SkScalar min = pts[index].fX; |
| 2431 | SkScalar max = min; |
| 2432 | int minIndex = index; |
| 2433 | int maxIndex = index; |
| 2434 | for (int i = index + 1; i < n; ++i) { |
| 2435 | if (pts[i].fY != y) { |
| 2436 | break; |
| 2437 | } |
| 2438 | SkScalar x = pts[i].fX; |
| 2439 | if (x < min) { |
| 2440 | min = x; |
| 2441 | minIndex = i; |
| 2442 | } else if (x > max) { |
| 2443 | max = x; |
| 2444 | maxIndex = i; |
| 2445 | } |
| 2446 | } |
| 2447 | *maxIndexPtr = maxIndex; |
| 2448 | return minIndex; |
| 2449 | } |
| 2450 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2451 | static void crossToDir(SkScalar cross, SkPath::Direction* dir) { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2452 | *dir = cross > 0 ? SkPath::kCW_Direction : SkPath::kCCW_Direction; |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2455 | /* |
| 2456 | * We loop through all contours, and keep the computed cross-product of the |
| 2457 | * contour that contained the global y-max. If we just look at the first |
| 2458 | * contour, we may find one that is wound the opposite way (correctly) since |
| 2459 | * it is the interior of a hole (e.g. 'o'). Thus we must find the contour |
| 2460 | * that is outer most (or at least has the global y-max) before we can consider |
| 2461 | * its cross product. |
| 2462 | */ |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2463 | bool SkPath::cheapComputeDirection(Direction* dir) const { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2464 | if (kUnknown_Direction != fDirection) { |
| 2465 | *dir = static_cast<Direction>(fDirection); |
| 2466 | return true; |
| 2467 | } |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2468 | |
| 2469 | // don't want to pay the cost for computing this if it |
| 2470 | // is unknown, so we don't call isConvex() |
| 2471 | if (kConvex_Convexity == this->getConvexityOrUnknown()) { |
| 2472 | SkASSERT(kUnknown_Direction == fDirection); |
| 2473 | *dir = static_cast<Direction>(fDirection); |
| 2474 | return false; |
| 2475 | } |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2476 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2477 | ContourIter iter(*fPathRef.get()); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2478 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2479 | // initialize with our logical y-min |
| 2480 | SkScalar ymax = this->getBounds().fTop; |
| 2481 | SkScalar ymaxCross = 0; |
| 2482 | |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2483 | for (; !iter.done(); iter.next()) { |
| 2484 | int n = iter.count(); |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2485 | if (n < 3) { |
| 2486 | continue; |
| 2487 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 2488 | |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2489 | const SkPoint* pts = iter.pts(); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2490 | SkScalar cross = 0; |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2491 | int index = find_max_y(pts, n); |
| 2492 | if (pts[index].fY < ymax) { |
| 2493 | continue; |
| 2494 | } |
| 2495 | |
| 2496 | // If there is more than 1 distinct point at the y-max, we take the |
| 2497 | // x-min and x-max of them and just subtract to compute the dir. |
| 2498 | if (pts[(index + 1) % n].fY == pts[index].fY) { |
| 2499 | int maxIndex; |
| 2500 | int minIndex = find_min_max_x_at_y(pts, index, n, &maxIndex); |
| 2501 | if (minIndex == maxIndex) { |
| 2502 | goto TRY_CROSSPROD; |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 2503 | } |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2504 | SkASSERT(pts[minIndex].fY == pts[index].fY); |
| 2505 | SkASSERT(pts[maxIndex].fY == pts[index].fY); |
| 2506 | SkASSERT(pts[minIndex].fX <= pts[maxIndex].fX); |
| 2507 | // we just subtract the indices, and let that auto-convert to |
| 2508 | // SkScalar, since we just want - or + to signal the direction. |
| 2509 | cross = minIndex - maxIndex; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2510 | } else { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2511 | TRY_CROSSPROD: |
| 2512 | // Find a next and prev index to use for the cross-product test, |
| 2513 | // but we try to find pts that form non-zero vectors from pts[index] |
| 2514 | // |
| 2515 | // Its possible that we can't find two non-degenerate vectors, so |
| 2516 | // we have to guard our search (e.g. all the pts could be in the |
| 2517 | // same place). |
| 2518 | |
| 2519 | // we pass n - 1 instead of -1 so we don't foul up % operator by |
| 2520 | // passing it a negative LH argument. |
| 2521 | int prev = find_diff_pt(pts, index, n, n - 1); |
| 2522 | if (prev == index) { |
| 2523 | // completely degenerate, skip to next contour |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2524 | continue; |
| 2525 | } |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2526 | int next = find_diff_pt(pts, index, n, 1); |
| 2527 | SkASSERT(next != index); |
| 2528 | cross = cross_prod(pts[prev], pts[index], pts[next]); |
| 2529 | // if we get a zero and the points are horizontal, then we look at the spread in |
| 2530 | // x-direction. We really should continue to walk away from the degeneracy until |
| 2531 | // there is a divergence. |
| 2532 | if (0 == cross && pts[prev].fY == pts[index].fY && pts[next].fY == pts[index].fY) { |
| 2533 | // construct the subtract so we get the correct Direction below |
| 2534 | cross = pts[index].fX - pts[next].fX; |
reed@google.com | 188bfcf | 2012-01-17 18:26:38 +0000 | [diff] [blame] | 2535 | } |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2536 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2537 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2538 | if (cross) { |
| 2539 | // record our best guess so far |
| 2540 | ymax = pts[index].fY; |
| 2541 | ymaxCross = cross; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2542 | } |
| 2543 | } |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2544 | if (ymaxCross) { |
| 2545 | crossToDir(ymaxCross, dir); |
| 2546 | fDirection = *dir; |
| 2547 | return true; |
| 2548 | } else { |
| 2549 | return false; |
| 2550 | } |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2551 | } |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2552 | |
| 2553 | /////////////////////////////////////////////////////////////////////////////// |
| 2554 | |
| 2555 | static SkScalar eval_cubic_coeff(SkScalar A, SkScalar B, SkScalar C, |
| 2556 | SkScalar D, SkScalar t) { |
| 2557 | return SkScalarMulAdd(SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C), t, D); |
| 2558 | } |
| 2559 | |
| 2560 | static SkScalar eval_cubic_pts(SkScalar c0, SkScalar c1, SkScalar c2, SkScalar c3, |
| 2561 | SkScalar t) { |
| 2562 | SkScalar A = c3 + 3*(c1 - c2) - c0; |
| 2563 | SkScalar B = 3*(c2 - c1 - c1 + c0); |
| 2564 | SkScalar C = 3*(c1 - c0); |
| 2565 | SkScalar D = c0; |
| 2566 | return eval_cubic_coeff(A, B, C, D, t); |
| 2567 | } |
| 2568 | |
| 2569 | /* Given 4 cubic points (either Xs or Ys), and a target X or Y, compute the |
| 2570 | t value such that cubic(t) = target |
| 2571 | */ |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2572 | static void chopMonoCubicAt(SkScalar c0, SkScalar c1, SkScalar c2, SkScalar c3, |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2573 | SkScalar target, SkScalar* t) { |
| 2574 | // SkASSERT(c0 <= c1 && c1 <= c2 && c2 <= c3); |
| 2575 | SkASSERT(c0 < target && target < c3); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2576 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2577 | SkScalar D = c0 - target; |
| 2578 | SkScalar A = c3 + 3*(c1 - c2) - c0; |
| 2579 | SkScalar B = 3*(c2 - c1 - c1 + c0); |
| 2580 | SkScalar C = 3*(c1 - c0); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2581 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2582 | const SkScalar TOLERANCE = SK_Scalar1 / 4096; |
| 2583 | SkScalar minT = 0; |
| 2584 | SkScalar maxT = SK_Scalar1; |
| 2585 | SkScalar mid; |
| 2586 | int i; |
| 2587 | for (i = 0; i < 16; i++) { |
| 2588 | mid = SkScalarAve(minT, maxT); |
| 2589 | SkScalar delta = eval_cubic_coeff(A, B, C, D, mid); |
| 2590 | if (delta < 0) { |
| 2591 | minT = mid; |
| 2592 | delta = -delta; |
| 2593 | } else { |
| 2594 | maxT = mid; |
| 2595 | } |
| 2596 | if (delta < TOLERANCE) { |
| 2597 | break; |
| 2598 | } |
| 2599 | } |
| 2600 | *t = mid; |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | template <size_t N> static void find_minmax(const SkPoint pts[], |
| 2604 | SkScalar* minPtr, SkScalar* maxPtr) { |
| 2605 | SkScalar min, max; |
| 2606 | min = max = pts[0].fX; |
| 2607 | for (size_t i = 1; i < N; ++i) { |
| 2608 | min = SkMinScalar(min, pts[i].fX); |
| 2609 | max = SkMaxScalar(max, pts[i].fX); |
| 2610 | } |
| 2611 | *minPtr = min; |
| 2612 | *maxPtr = max; |
| 2613 | } |
| 2614 | |
| 2615 | static int winding_mono_cubic(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2616 | SkPoint storage[4]; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2617 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2618 | int dir = 1; |
| 2619 | if (pts[0].fY > pts[3].fY) { |
| 2620 | storage[0] = pts[3]; |
| 2621 | storage[1] = pts[2]; |
| 2622 | storage[2] = pts[1]; |
| 2623 | storage[3] = pts[0]; |
| 2624 | pts = storage; |
| 2625 | dir = -1; |
| 2626 | } |
| 2627 | if (y < pts[0].fY || y >= pts[3].fY) { |
| 2628 | return 0; |
| 2629 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2630 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2631 | // quickreject or quickaccept |
| 2632 | SkScalar min, max; |
| 2633 | find_minmax<4>(pts, &min, &max); |
| 2634 | if (x < min) { |
| 2635 | return 0; |
| 2636 | } |
| 2637 | if (x > max) { |
| 2638 | return dir; |
| 2639 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2640 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2641 | // compute the actual x(t) value |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2642 | SkScalar t; |
| 2643 | chopMonoCubicAt(pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY, y, &t); |
| 2644 | SkScalar xt = eval_cubic_pts(pts[0].fX, pts[1].fX, pts[2].fX, pts[3].fX, t); |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2645 | return xt < x ? dir : 0; |
| 2646 | } |
| 2647 | |
| 2648 | static int winding_cubic(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2649 | SkPoint dst[10]; |
| 2650 | int n = SkChopCubicAtYExtrema(pts, dst); |
| 2651 | int w = 0; |
| 2652 | for (int i = 0; i <= n; ++i) { |
| 2653 | w += winding_mono_cubic(&dst[i * 3], x, y); |
| 2654 | } |
| 2655 | return w; |
| 2656 | } |
| 2657 | |
| 2658 | static int winding_mono_quad(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2659 | SkScalar y0 = pts[0].fY; |
| 2660 | SkScalar y2 = pts[2].fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2661 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2662 | int dir = 1; |
| 2663 | if (y0 > y2) { |
| 2664 | SkTSwap(y0, y2); |
| 2665 | dir = -1; |
| 2666 | } |
| 2667 | if (y < y0 || y >= y2) { |
| 2668 | return 0; |
| 2669 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2670 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2671 | // bounds check on X (not required. is it faster?) |
| 2672 | #if 0 |
| 2673 | if (pts[0].fX > x && pts[1].fX > x && pts[2].fX > x) { |
| 2674 | return 0; |
| 2675 | } |
| 2676 | #endif |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2677 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2678 | SkScalar roots[2]; |
| 2679 | int n = SkFindUnitQuadRoots(pts[0].fY - 2 * pts[1].fY + pts[2].fY, |
| 2680 | 2 * (pts[1].fY - pts[0].fY), |
| 2681 | pts[0].fY - y, |
| 2682 | roots); |
| 2683 | SkASSERT(n <= 1); |
| 2684 | SkScalar xt; |
| 2685 | if (0 == n) { |
| 2686 | SkScalar mid = SkScalarAve(y0, y2); |
| 2687 | // Need [0] and [2] if dir == 1 |
| 2688 | // and [2] and [0] if dir == -1 |
| 2689 | xt = y < mid ? pts[1 - dir].fX : pts[dir - 1].fX; |
| 2690 | } else { |
| 2691 | SkScalar t = roots[0]; |
| 2692 | SkScalar C = pts[0].fX; |
| 2693 | SkScalar A = pts[2].fX - 2 * pts[1].fX + C; |
| 2694 | SkScalar B = 2 * (pts[1].fX - C); |
| 2695 | xt = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); |
| 2696 | } |
| 2697 | return xt < x ? dir : 0; |
| 2698 | } |
| 2699 | |
| 2700 | static bool is_mono_quad(SkScalar y0, SkScalar y1, SkScalar y2) { |
| 2701 | // return SkScalarSignAsInt(y0 - y1) + SkScalarSignAsInt(y1 - y2) != 0; |
| 2702 | if (y0 == y1) { |
| 2703 | return true; |
| 2704 | } |
| 2705 | if (y0 < y1) { |
| 2706 | return y1 <= y2; |
| 2707 | } else { |
| 2708 | return y1 >= y2; |
| 2709 | } |
| 2710 | } |
| 2711 | |
| 2712 | static int winding_quad(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2713 | SkPoint dst[5]; |
| 2714 | int n = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2715 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2716 | if (!is_mono_quad(pts[0].fY, pts[1].fY, pts[2].fY)) { |
| 2717 | n = SkChopQuadAtYExtrema(pts, dst); |
| 2718 | pts = dst; |
| 2719 | } |
| 2720 | int w = winding_mono_quad(pts, x, y); |
| 2721 | if (n > 0) { |
| 2722 | w += winding_mono_quad(&pts[2], x, y); |
| 2723 | } |
| 2724 | return w; |
| 2725 | } |
| 2726 | |
| 2727 | static int winding_line(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2728 | SkScalar x0 = pts[0].fX; |
| 2729 | SkScalar y0 = pts[0].fY; |
| 2730 | SkScalar x1 = pts[1].fX; |
| 2731 | SkScalar y1 = pts[1].fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2732 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2733 | SkScalar dy = y1 - y0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2734 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2735 | int dir = 1; |
| 2736 | if (y0 > y1) { |
| 2737 | SkTSwap(y0, y1); |
| 2738 | dir = -1; |
| 2739 | } |
| 2740 | if (y < y0 || y >= y1) { |
| 2741 | return 0; |
| 2742 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2743 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2744 | SkScalar cross = SkScalarMul(x1 - x0, y - pts[0].fY) - |
| 2745 | SkScalarMul(dy, x - pts[0].fX); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2746 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2747 | if (SkScalarSignAsInt(cross) == dir) { |
| 2748 | dir = 0; |
| 2749 | } |
| 2750 | return dir; |
| 2751 | } |
| 2752 | |
reed@google.com | 4db592c | 2013-10-30 17:39:43 +0000 | [diff] [blame] | 2753 | static bool contains_inclusive(const SkRect& r, SkScalar x, SkScalar y) { |
| 2754 | return r.fLeft <= x && x <= r.fRight && r.fTop <= y && y <= r.fBottom; |
| 2755 | } |
| 2756 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2757 | bool SkPath::contains(SkScalar x, SkScalar y) const { |
| 2758 | bool isInverse = this->isInverseFillType(); |
| 2759 | if (this->isEmpty()) { |
| 2760 | return isInverse; |
| 2761 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2762 | |
reed@google.com | 4db592c | 2013-10-30 17:39:43 +0000 | [diff] [blame] | 2763 | if (!contains_inclusive(this->getBounds(), x, y)) { |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2764 | return isInverse; |
| 2765 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2766 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2767 | SkPath::Iter iter(*this, true); |
| 2768 | bool done = false; |
| 2769 | int w = 0; |
| 2770 | do { |
| 2771 | SkPoint pts[4]; |
| 2772 | switch (iter.next(pts, false)) { |
| 2773 | case SkPath::kMove_Verb: |
| 2774 | case SkPath::kClose_Verb: |
| 2775 | break; |
| 2776 | case SkPath::kLine_Verb: |
| 2777 | w += winding_line(pts, x, y); |
| 2778 | break; |
| 2779 | case SkPath::kQuad_Verb: |
| 2780 | w += winding_quad(pts, x, y); |
| 2781 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2782 | case SkPath::kConic_Verb: |
| 2783 | SkASSERT(0); |
| 2784 | break; |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2785 | case SkPath::kCubic_Verb: |
| 2786 | w += winding_cubic(pts, x, y); |
| 2787 | break; |
| 2788 | case SkPath::kDone_Verb: |
| 2789 | done = true; |
| 2790 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2791 | } |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2792 | } while (!done); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2793 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2794 | switch (this->getFillType()) { |
| 2795 | case SkPath::kEvenOdd_FillType: |
| 2796 | case SkPath::kInverseEvenOdd_FillType: |
| 2797 | w &= 1; |
| 2798 | break; |
reed@google.com | e9bb623 | 2012-07-11 18:56:10 +0000 | [diff] [blame] | 2799 | default: |
| 2800 | break; |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2801 | } |
| 2802 | return SkToBool(w); |
| 2803 | } |