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