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