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