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 | |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1945 | if (!fBoundsIsDirty) { |
| 1946 | SkRect bounds; |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 1947 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1948 | bool isFinite = compute_pt_bounds(&bounds, *fPathRef.get()); |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 1949 | SkASSERT(SkToBool(fIsFinite) == isFinite); |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 1950 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1951 | if (fPathRef->countPoints() <= 1) { |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1952 | // if we're empty, fBounds may be empty but translated, so we can't |
| 1953 | // necessarily compare to bounds directly |
| 1954 | // try path.addOval(2, 2, 2, 2) which is empty, but the bounds will |
| 1955 | // be [2, 2, 2, 2] |
| 1956 | SkASSERT(bounds.isEmpty()); |
| 1957 | SkASSERT(fBounds.isEmpty()); |
| 1958 | } else { |
reed@google.com | eac52bd | 2011-11-14 18:13:59 +0000 | [diff] [blame] | 1959 | if (bounds.isEmpty()) { |
| 1960 | SkASSERT(fBounds.isEmpty()); |
| 1961 | } else { |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 1962 | if (!fBounds.isEmpty()) { |
| 1963 | SkASSERT(fBounds.contains(bounds)); |
| 1964 | } |
reed@google.com | eac52bd | 2011-11-14 18:13:59 +0000 | [diff] [blame] | 1965 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1966 | } |
| 1967 | } |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 1968 | |
| 1969 | uint32_t mask = 0; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1970 | const uint8_t* verbs = const_cast<const SkPathRef*>(fPathRef.get())->verbs(); |
| 1971 | for (int i = 0; i < fPathRef->countVerbs(); i++) { |
| 1972 | switch (verbs[~i]) { |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 1973 | case kLine_Verb: |
| 1974 | mask |= kLine_SegmentMask; |
| 1975 | break; |
| 1976 | case kQuad_Verb: |
| 1977 | mask |= kQuad_SegmentMask; |
| 1978 | break; |
| 1979 | case kCubic_Verb: |
| 1980 | mask |= kCubic_SegmentMask; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1981 | case kMove_Verb: // these verbs aren't included in the segment mask. |
| 1982 | case kClose_Verb: |
| 1983 | break; |
| 1984 | case kDone_Verb: |
| 1985 | SkDEBUGFAIL("Done verb shouldn't be recorded."); |
| 1986 | break; |
| 1987 | default: |
| 1988 | SkDEBUGFAIL("Unknown Verb"); |
| 1989 | break; |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 1990 | } |
| 1991 | } |
| 1992 | SkASSERT(mask == fSegmentMask); |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1993 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1994 | #endif |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1995 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1996 | /////////////////////////////////////////////////////////////////////////////// |
| 1997 | |
reed@google.com | 0b7b982 | 2011-05-16 12:29:27 +0000 | [diff] [blame] | 1998 | static int sign(SkScalar x) { return x < 0; } |
| 1999 | #define kValueNeverReturnedBySign 2 |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2000 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2001 | static int CrossProductSign(const SkVector& a, const SkVector& b) { |
bsalomon@google.com | 647a804 | 2011-08-23 14:39:01 +0000 | [diff] [blame] | 2002 | return SkScalarSignAsInt(SkPoint::CrossProduct(a, b)); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | // only valid for a single contour |
| 2006 | struct Convexicator { |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 2007 | Convexicator() : fPtCount(0), fConvexity(SkPath::kConvex_Convexity) { |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2008 | fSign = 0; |
| 2009 | // warnings |
| 2010 | fCurrPt.set(0, 0); |
| 2011 | fVec0.set(0, 0); |
| 2012 | fVec1.set(0, 0); |
| 2013 | fFirstVec.set(0, 0); |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2014 | |
| 2015 | fDx = fDy = 0; |
reed@google.com | 0b7b982 | 2011-05-16 12:29:27 +0000 | [diff] [blame] | 2016 | fSx = fSy = kValueNeverReturnedBySign; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | SkPath::Convexity getConvexity() const { return fConvexity; } |
| 2020 | |
| 2021 | void addPt(const SkPoint& pt) { |
| 2022 | if (SkPath::kConcave_Convexity == fConvexity) { |
| 2023 | return; |
| 2024 | } |
| 2025 | |
| 2026 | if (0 == fPtCount) { |
| 2027 | fCurrPt = pt; |
| 2028 | ++fPtCount; |
| 2029 | } else { |
| 2030 | SkVector vec = pt - fCurrPt; |
| 2031 | if (vec.fX || vec.fY) { |
| 2032 | fCurrPt = pt; |
| 2033 | if (++fPtCount == 2) { |
| 2034 | fFirstVec = fVec1 = vec; |
| 2035 | } else { |
| 2036 | SkASSERT(fPtCount > 2); |
| 2037 | this->addVec(vec); |
| 2038 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2039 | |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2040 | int sx = sign(vec.fX); |
| 2041 | int sy = sign(vec.fY); |
| 2042 | fDx += (sx != fSx); |
| 2043 | fDy += (sy != fSy); |
| 2044 | fSx = sx; |
| 2045 | fSy = sy; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2046 | |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2047 | if (fDx > 3 || fDy > 3) { |
| 2048 | fConvexity = SkPath::kConcave_Convexity; |
| 2049 | } |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2050 | } |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | void close() { |
| 2055 | if (fPtCount > 2) { |
| 2056 | this->addVec(fFirstVec); |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | private: |
| 2061 | void addVec(const SkVector& vec) { |
| 2062 | SkASSERT(vec.fX || vec.fY); |
| 2063 | fVec0 = fVec1; |
| 2064 | fVec1 = vec; |
| 2065 | int sign = CrossProductSign(fVec0, fVec1); |
| 2066 | if (0 == fSign) { |
| 2067 | fSign = sign; |
| 2068 | } else if (sign) { |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 2069 | if (fSign != sign) { |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2070 | fConvexity = SkPath::kConcave_Convexity; |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | SkPoint fCurrPt; |
| 2076 | SkVector fVec0, fVec1, fFirstVec; |
| 2077 | int fPtCount; // non-degenerate points |
| 2078 | int fSign; |
| 2079 | SkPath::Convexity fConvexity; |
reed@google.com | 0b7b982 | 2011-05-16 12:29:27 +0000 | [diff] [blame] | 2080 | int fDx, fDy, fSx, fSy; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2081 | }; |
| 2082 | |
| 2083 | SkPath::Convexity SkPath::ComputeConvexity(const SkPath& path) { |
| 2084 | SkPoint pts[4]; |
| 2085 | SkPath::Verb verb; |
| 2086 | SkPath::Iter iter(path, true); |
| 2087 | |
| 2088 | int contourCount = 0; |
| 2089 | int count; |
| 2090 | Convexicator state; |
| 2091 | |
| 2092 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 2093 | switch (verb) { |
| 2094 | case kMove_Verb: |
| 2095 | if (++contourCount > 1) { |
| 2096 | return kConcave_Convexity; |
| 2097 | } |
| 2098 | pts[1] = pts[0]; |
| 2099 | count = 1; |
| 2100 | break; |
| 2101 | case kLine_Verb: count = 1; break; |
| 2102 | case kQuad_Verb: count = 2; break; |
| 2103 | case kCubic_Verb: count = 3; break; |
| 2104 | case kClose_Verb: |
| 2105 | state.close(); |
| 2106 | count = 0; |
| 2107 | break; |
| 2108 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 2109 | SkDEBUGFAIL("bad verb"); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2110 | return kConcave_Convexity; |
| 2111 | } |
| 2112 | |
| 2113 | for (int i = 1; i <= count; i++) { |
| 2114 | state.addPt(pts[i]); |
| 2115 | } |
| 2116 | // early exit |
| 2117 | if (kConcave_Convexity == state.getConvexity()) { |
| 2118 | return kConcave_Convexity; |
| 2119 | } |
| 2120 | } |
| 2121 | return state.getConvexity(); |
| 2122 | } |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2123 | |
| 2124 | /////////////////////////////////////////////////////////////////////////////// |
| 2125 | |
| 2126 | class ContourIter { |
| 2127 | public: |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2128 | ContourIter(const SkPathRef& pathRef); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2129 | |
| 2130 | bool done() const { return fDone; } |
| 2131 | // if !done() then these may be called |
| 2132 | int count() const { return fCurrPtCount; } |
| 2133 | const SkPoint* pts() const { return fCurrPt; } |
| 2134 | void next(); |
| 2135 | |
| 2136 | private: |
| 2137 | int fCurrPtCount; |
| 2138 | const SkPoint* fCurrPt; |
| 2139 | const uint8_t* fCurrVerb; |
| 2140 | const uint8_t* fStopVerbs; |
| 2141 | bool fDone; |
reed@google.com | d1ab932 | 2012-01-10 18:40:03 +0000 | [diff] [blame] | 2142 | SkDEBUGCODE(int fContourCounter;) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2143 | }; |
| 2144 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2145 | ContourIter::ContourIter(const SkPathRef& pathRef) { |
| 2146 | fStopVerbs = pathRef.verbsMemBegin(); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2147 | fDone = false; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2148 | fCurrPt = pathRef.points(); |
| 2149 | fCurrVerb = pathRef.verbs(); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2150 | fCurrPtCount = 0; |
reed@google.com | d1ab932 | 2012-01-10 18:40:03 +0000 | [diff] [blame] | 2151 | SkDEBUGCODE(fContourCounter = 0;) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2152 | this->next(); |
| 2153 | } |
| 2154 | |
| 2155 | void ContourIter::next() { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2156 | if (fCurrVerb <= fStopVerbs) { |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2157 | fDone = true; |
| 2158 | } |
| 2159 | if (fDone) { |
| 2160 | return; |
| 2161 | } |
| 2162 | |
| 2163 | // skip pts of prev contour |
| 2164 | fCurrPt += fCurrPtCount; |
| 2165 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2166 | SkASSERT(SkPath::kMove_Verb == fCurrVerb[~0]); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2167 | int ptCount = 1; // moveTo |
| 2168 | const uint8_t* verbs = fCurrVerb; |
| 2169 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2170 | for (--verbs; verbs > fStopVerbs; --verbs) { |
| 2171 | switch (verbs[~0]) { |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2172 | case SkPath::kMove_Verb: |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2173 | goto CONTOUR_END; |
| 2174 | case SkPath::kLine_Verb: |
| 2175 | ptCount += 1; |
| 2176 | break; |
| 2177 | case SkPath::kQuad_Verb: |
| 2178 | ptCount += 2; |
| 2179 | break; |
| 2180 | case SkPath::kCubic_Verb: |
| 2181 | ptCount += 3; |
| 2182 | break; |
| 2183 | default: // kClose_Verb, just keep going |
| 2184 | break; |
| 2185 | } |
| 2186 | } |
| 2187 | CONTOUR_END: |
| 2188 | fCurrPtCount = ptCount; |
| 2189 | fCurrVerb = verbs; |
reed@google.com | d1ab932 | 2012-01-10 18:40:03 +0000 | [diff] [blame] | 2190 | SkDEBUGCODE(++fContourCounter;) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 2193 | // returns cross product of (p1 - p0) and (p2 - p0) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2194 | 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] | 2195 | SkScalar cross = SkPoint::CrossProduct(p1 - p0, p2 - p0); |
| 2196 | // We may get 0 when the above subtracts underflow. We expect this to be |
| 2197 | // very rare and lazily promote to double. |
| 2198 | if (0 == cross) { |
| 2199 | double p0x = SkScalarToDouble(p0.fX); |
| 2200 | double p0y = SkScalarToDouble(p0.fY); |
| 2201 | |
| 2202 | double p1x = SkScalarToDouble(p1.fX); |
| 2203 | double p1y = SkScalarToDouble(p1.fY); |
| 2204 | |
| 2205 | double p2x = SkScalarToDouble(p2.fX); |
| 2206 | double p2y = SkScalarToDouble(p2.fY); |
| 2207 | |
| 2208 | cross = SkDoubleToScalar((p1x - p0x) * (p2y - p0y) - |
| 2209 | (p1y - p0y) * (p2x - p0x)); |
| 2210 | |
| 2211 | } |
| 2212 | return cross; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2215 | // Returns the first pt with the maximum Y coordinate |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2216 | static int find_max_y(const SkPoint pts[], int count) { |
| 2217 | SkASSERT(count > 0); |
| 2218 | SkScalar max = pts[0].fY; |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2219 | int firstIndex = 0; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2220 | for (int i = 1; i < count; ++i) { |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2221 | SkScalar y = pts[i].fY; |
| 2222 | if (y > max) { |
| 2223 | max = y; |
| 2224 | firstIndex = i; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2225 | } |
| 2226 | } |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2227 | return firstIndex; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2230 | static int find_diff_pt(const SkPoint pts[], int index, int n, int inc) { |
| 2231 | int i = index; |
| 2232 | for (;;) { |
| 2233 | i = (i + inc) % n; |
| 2234 | if (i == index) { // we wrapped around, so abort |
| 2235 | break; |
| 2236 | } |
| 2237 | if (pts[index] != pts[i]) { // found a different point, success! |
| 2238 | break; |
| 2239 | } |
| 2240 | } |
| 2241 | return i; |
| 2242 | } |
| 2243 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2244 | /** |
| 2245 | * Starting at index, and moving forward (incrementing), find the xmin and |
| 2246 | * xmax of the contiguous points that have the same Y. |
| 2247 | */ |
| 2248 | static int find_min_max_x_at_y(const SkPoint pts[], int index, int n, |
| 2249 | int* maxIndexPtr) { |
| 2250 | const SkScalar y = pts[index].fY; |
| 2251 | SkScalar min = pts[index].fX; |
| 2252 | SkScalar max = min; |
| 2253 | int minIndex = index; |
| 2254 | int maxIndex = index; |
| 2255 | for (int i = index + 1; i < n; ++i) { |
| 2256 | if (pts[i].fY != y) { |
| 2257 | break; |
| 2258 | } |
| 2259 | SkScalar x = pts[i].fX; |
| 2260 | if (x < min) { |
| 2261 | min = x; |
| 2262 | minIndex = i; |
| 2263 | } else if (x > max) { |
| 2264 | max = x; |
| 2265 | maxIndex = i; |
| 2266 | } |
| 2267 | } |
| 2268 | *maxIndexPtr = maxIndex; |
| 2269 | return minIndex; |
| 2270 | } |
| 2271 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2272 | static bool crossToDir(SkScalar cross, SkPath::Direction* dir) { |
| 2273 | if (dir) { |
| 2274 | *dir = cross > 0 ? SkPath::kCW_Direction : SkPath::kCCW_Direction; |
| 2275 | } |
| 2276 | return true; |
| 2277 | } |
| 2278 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2279 | #if 0 |
| 2280 | #include "SkString.h" |
| 2281 | #include "../utils/SkParsePath.h" |
| 2282 | static void dumpPath(const SkPath& path) { |
| 2283 | SkString str; |
| 2284 | SkParsePath::ToSVGString(path, &str); |
| 2285 | SkDebugf("%s\n", str.c_str()); |
| 2286 | } |
| 2287 | #endif |
| 2288 | |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 2289 | namespace { |
| 2290 | // for use with convex_dir_test |
| 2291 | double mul(double a, double b) { return a * b; } |
| 2292 | SkScalar mul(SkScalar a, SkScalar b) { return SkScalarMul(a, b); } |
| 2293 | double toDouble(SkScalar a) { return SkScalarToDouble(a); } |
| 2294 | SkScalar toScalar(SkScalar a) { return a; } |
| 2295 | |
| 2296 | // determines the winding direction of a convex polygon with the precision |
| 2297 | // of T. CAST_SCALAR casts an SkScalar to T. |
| 2298 | template <typename T, T (CAST_SCALAR)(SkScalar)> |
| 2299 | bool convex_dir_test(int n, const SkPoint pts[], SkPath::Direction* dir) { |
| 2300 | // we find the first three points that form a non-degenerate |
| 2301 | // triangle. If there are no such points then the path is |
| 2302 | // degenerate. The first is always point 0. Now we find the second |
| 2303 | // point. |
| 2304 | int i = 0; |
| 2305 | enum { kX = 0, kY = 1 }; |
| 2306 | T v0[2]; |
| 2307 | while (1) { |
| 2308 | v0[kX] = CAST_SCALAR(pts[i].fX) - CAST_SCALAR(pts[0].fX); |
| 2309 | v0[kY] = CAST_SCALAR(pts[i].fY) - CAST_SCALAR(pts[0].fY); |
| 2310 | if (v0[kX] || v0[kY]) { |
| 2311 | break; |
| 2312 | } |
| 2313 | if (++i == n - 1) { |
| 2314 | return false; |
| 2315 | } |
| 2316 | } |
| 2317 | // now find a third point that is not colinear with the first two |
| 2318 | // points and check the orientation of the triangle (which will be |
| 2319 | // the same as the orientation of the path). |
| 2320 | for (++i; i < n; ++i) { |
| 2321 | T v1[2]; |
| 2322 | v1[kX] = CAST_SCALAR(pts[i].fX) - CAST_SCALAR(pts[0].fX); |
| 2323 | v1[kY] = CAST_SCALAR(pts[i].fY) - CAST_SCALAR(pts[0].fY); |
| 2324 | T cross = mul(v0[kX], v1[kY]) - mul(v0[kY], v1[kX]); |
| 2325 | if (0 != cross) { |
| 2326 | *dir = cross > 0 ? SkPath::kCW_Direction : SkPath::kCCW_Direction; |
| 2327 | return true; |
| 2328 | } |
| 2329 | } |
| 2330 | return false; |
| 2331 | } |
| 2332 | } |
| 2333 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2334 | /* |
| 2335 | * We loop through all contours, and keep the computed cross-product of the |
| 2336 | * contour that contained the global y-max. If we just look at the first |
| 2337 | * contour, we may find one that is wound the opposite way (correctly) since |
| 2338 | * it is the interior of a hole (e.g. 'o'). Thus we must find the contour |
| 2339 | * that is outer most (or at least has the global y-max) before we can consider |
| 2340 | * its cross product. |
| 2341 | */ |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2342 | bool SkPath::cheapComputeDirection(Direction* dir) const { |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2343 | // dumpPath(*this); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2344 | // don't want to pay the cost for computing this if it |
| 2345 | // is unknown, so we don't call isConvex() |
| 2346 | const Convexity conv = this->getConvexityOrUnknown(); |
| 2347 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2348 | ContourIter iter(*fPathRef.get()); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2349 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2350 | // initialize with our logical y-min |
| 2351 | SkScalar ymax = this->getBounds().fTop; |
| 2352 | SkScalar ymaxCross = 0; |
| 2353 | |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2354 | for (; !iter.done(); iter.next()) { |
| 2355 | int n = iter.count(); |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2356 | if (n < 3) { |
| 2357 | continue; |
| 2358 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 2359 | |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2360 | const SkPoint* pts = iter.pts(); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2361 | SkScalar cross = 0; |
| 2362 | if (kConvex_Convexity == conv) { |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 2363 | // We try first at scalar precision, and then again at double |
| 2364 | // precision. This is because the vectors computed between distant |
| 2365 | // points may lose too much precision. |
| 2366 | if (convex_dir_test<SkScalar, toScalar>(n, pts, dir)) { |
| 2367 | return true; |
| 2368 | } |
| 2369 | if (convex_dir_test<double, toDouble>(n, pts, dir)) { |
| 2370 | return true; |
| 2371 | } else { |
| 2372 | return false; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2373 | } |
| 2374 | } else { |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2375 | int index = find_max_y(pts, n); |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2376 | if (pts[index].fY < ymax) { |
| 2377 | continue; |
| 2378 | } |
| 2379 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2380 | // If there is more than 1 distinct point at the y-max, we take the |
| 2381 | // x-min and x-max of them and just subtract to compute the dir. |
| 2382 | if (pts[(index + 1) % n].fY == pts[index].fY) { |
| 2383 | int maxIndex; |
| 2384 | 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] | 2385 | if (minIndex == maxIndex) { |
| 2386 | goto TRY_CROSSPROD; |
| 2387 | } |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2388 | SkASSERT(pts[minIndex].fY == pts[index].fY); |
| 2389 | SkASSERT(pts[maxIndex].fY == pts[index].fY); |
| 2390 | SkASSERT(pts[minIndex].fX <= pts[maxIndex].fX); |
| 2391 | // we just subtract the indices, and let that auto-convert to |
| 2392 | // SkScalar, since we just want - or + to signal the direction. |
| 2393 | cross = minIndex - maxIndex; |
| 2394 | } else { |
reed@google.com | 3e44e4d | 2012-01-31 15:25:22 +0000 | [diff] [blame] | 2395 | TRY_CROSSPROD: |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2396 | // Find a next and prev index to use for the cross-product test, |
| 2397 | // but we try to find pts that form non-zero vectors from pts[index] |
| 2398 | // |
| 2399 | // Its possible that we can't find two non-degenerate vectors, so |
| 2400 | // we have to guard our search (e.g. all the pts could be in the |
| 2401 | // same place). |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2402 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2403 | // we pass n - 1 instead of -1 so we don't foul up % operator by |
| 2404 | // passing it a negative LH argument. |
| 2405 | int prev = find_diff_pt(pts, index, n, n - 1); |
| 2406 | if (prev == index) { |
| 2407 | // completely degenerate, skip to next contour |
| 2408 | continue; |
| 2409 | } |
| 2410 | int next = find_diff_pt(pts, index, n, 1); |
| 2411 | SkASSERT(next != index); |
| 2412 | cross = cross_prod(pts[prev], pts[index], pts[next]); |
| 2413 | // if we get a zero, but the pts aren't on top of each other, then |
| 2414 | // we can just look at the direction |
| 2415 | if (0 == cross) { |
| 2416 | // construct the subtract so we get the correct Direction below |
| 2417 | cross = pts[index].fX - pts[next].fX; |
| 2418 | } |
reed@google.com | 188bfcf | 2012-01-17 18:26:38 +0000 | [diff] [blame] | 2419 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2420 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2421 | if (cross) { |
| 2422 | // record our best guess so far |
| 2423 | ymax = pts[index].fY; |
| 2424 | ymaxCross = cross; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2425 | } |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2426 | } |
| 2427 | } |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2428 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2429 | return ymaxCross ? crossToDir(ymaxCross, dir) : false; |
| 2430 | } |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2431 | |
| 2432 | /////////////////////////////////////////////////////////////////////////////// |
| 2433 | |
| 2434 | static SkScalar eval_cubic_coeff(SkScalar A, SkScalar B, SkScalar C, |
| 2435 | SkScalar D, SkScalar t) { |
| 2436 | return SkScalarMulAdd(SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C), t, D); |
| 2437 | } |
| 2438 | |
| 2439 | static SkScalar eval_cubic_pts(SkScalar c0, SkScalar c1, SkScalar c2, SkScalar c3, |
| 2440 | SkScalar t) { |
| 2441 | SkScalar A = c3 + 3*(c1 - c2) - c0; |
| 2442 | SkScalar B = 3*(c2 - c1 - c1 + c0); |
| 2443 | SkScalar C = 3*(c1 - c0); |
| 2444 | SkScalar D = c0; |
| 2445 | return eval_cubic_coeff(A, B, C, D, t); |
| 2446 | } |
| 2447 | |
| 2448 | /* Given 4 cubic points (either Xs or Ys), and a target X or Y, compute the |
| 2449 | t value such that cubic(t) = target |
| 2450 | */ |
| 2451 | static bool chopMonoCubicAt(SkScalar c0, SkScalar c1, SkScalar c2, SkScalar c3, |
| 2452 | SkScalar target, SkScalar* t) { |
| 2453 | // SkASSERT(c0 <= c1 && c1 <= c2 && c2 <= c3); |
| 2454 | SkASSERT(c0 < target && target < c3); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2455 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2456 | SkScalar D = c0 - target; |
| 2457 | SkScalar A = c3 + 3*(c1 - c2) - c0; |
| 2458 | SkScalar B = 3*(c2 - c1 - c1 + c0); |
| 2459 | SkScalar C = 3*(c1 - c0); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2460 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2461 | const SkScalar TOLERANCE = SK_Scalar1 / 4096; |
| 2462 | SkScalar minT = 0; |
| 2463 | SkScalar maxT = SK_Scalar1; |
| 2464 | SkScalar mid; |
| 2465 | int i; |
| 2466 | for (i = 0; i < 16; i++) { |
| 2467 | mid = SkScalarAve(minT, maxT); |
| 2468 | SkScalar delta = eval_cubic_coeff(A, B, C, D, mid); |
| 2469 | if (delta < 0) { |
| 2470 | minT = mid; |
| 2471 | delta = -delta; |
| 2472 | } else { |
| 2473 | maxT = mid; |
| 2474 | } |
| 2475 | if (delta < TOLERANCE) { |
| 2476 | break; |
| 2477 | } |
| 2478 | } |
| 2479 | *t = mid; |
| 2480 | return true; |
| 2481 | } |
| 2482 | |
| 2483 | template <size_t N> static void find_minmax(const SkPoint pts[], |
| 2484 | SkScalar* minPtr, SkScalar* maxPtr) { |
| 2485 | SkScalar min, max; |
| 2486 | min = max = pts[0].fX; |
| 2487 | for (size_t i = 1; i < N; ++i) { |
| 2488 | min = SkMinScalar(min, pts[i].fX); |
| 2489 | max = SkMaxScalar(max, pts[i].fX); |
| 2490 | } |
| 2491 | *minPtr = min; |
| 2492 | *maxPtr = max; |
| 2493 | } |
| 2494 | |
| 2495 | static int winding_mono_cubic(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2496 | SkPoint storage[4]; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2497 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2498 | int dir = 1; |
| 2499 | if (pts[0].fY > pts[3].fY) { |
| 2500 | storage[0] = pts[3]; |
| 2501 | storage[1] = pts[2]; |
| 2502 | storage[2] = pts[1]; |
| 2503 | storage[3] = pts[0]; |
| 2504 | pts = storage; |
| 2505 | dir = -1; |
| 2506 | } |
| 2507 | if (y < pts[0].fY || y >= pts[3].fY) { |
| 2508 | return 0; |
| 2509 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2510 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2511 | // quickreject or quickaccept |
| 2512 | SkScalar min, max; |
| 2513 | find_minmax<4>(pts, &min, &max); |
| 2514 | if (x < min) { |
| 2515 | return 0; |
| 2516 | } |
| 2517 | if (x > max) { |
| 2518 | return dir; |
| 2519 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2520 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2521 | // compute the actual x(t) value |
| 2522 | SkScalar t, xt; |
| 2523 | if (chopMonoCubicAt(pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY, y, &t)) { |
| 2524 | xt = eval_cubic_pts(pts[0].fX, pts[1].fX, pts[2].fX, pts[3].fX, t); |
| 2525 | } else { |
| 2526 | SkScalar mid = SkScalarAve(pts[0].fY, pts[3].fY); |
| 2527 | xt = y < mid ? pts[0].fX : pts[3].fX; |
| 2528 | } |
| 2529 | return xt < x ? dir : 0; |
| 2530 | } |
| 2531 | |
| 2532 | static int winding_cubic(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2533 | SkPoint dst[10]; |
| 2534 | int n = SkChopCubicAtYExtrema(pts, dst); |
| 2535 | int w = 0; |
| 2536 | for (int i = 0; i <= n; ++i) { |
| 2537 | w += winding_mono_cubic(&dst[i * 3], x, y); |
| 2538 | } |
| 2539 | return w; |
| 2540 | } |
| 2541 | |
| 2542 | static int winding_mono_quad(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2543 | SkScalar y0 = pts[0].fY; |
| 2544 | SkScalar y2 = pts[2].fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2545 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2546 | int dir = 1; |
| 2547 | if (y0 > y2) { |
| 2548 | SkTSwap(y0, y2); |
| 2549 | dir = -1; |
| 2550 | } |
| 2551 | if (y < y0 || y >= y2) { |
| 2552 | return 0; |
| 2553 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2554 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2555 | // bounds check on X (not required. is it faster?) |
| 2556 | #if 0 |
| 2557 | if (pts[0].fX > x && pts[1].fX > x && pts[2].fX > x) { |
| 2558 | return 0; |
| 2559 | } |
| 2560 | #endif |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2561 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2562 | SkScalar roots[2]; |
| 2563 | int n = SkFindUnitQuadRoots(pts[0].fY - 2 * pts[1].fY + pts[2].fY, |
| 2564 | 2 * (pts[1].fY - pts[0].fY), |
| 2565 | pts[0].fY - y, |
| 2566 | roots); |
| 2567 | SkASSERT(n <= 1); |
| 2568 | SkScalar xt; |
| 2569 | if (0 == n) { |
| 2570 | SkScalar mid = SkScalarAve(y0, y2); |
| 2571 | // Need [0] and [2] if dir == 1 |
| 2572 | // and [2] and [0] if dir == -1 |
| 2573 | xt = y < mid ? pts[1 - dir].fX : pts[dir - 1].fX; |
| 2574 | } else { |
| 2575 | SkScalar t = roots[0]; |
| 2576 | SkScalar C = pts[0].fX; |
| 2577 | SkScalar A = pts[2].fX - 2 * pts[1].fX + C; |
| 2578 | SkScalar B = 2 * (pts[1].fX - C); |
| 2579 | xt = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); |
| 2580 | } |
| 2581 | return xt < x ? dir : 0; |
| 2582 | } |
| 2583 | |
| 2584 | static bool is_mono_quad(SkScalar y0, SkScalar y1, SkScalar y2) { |
| 2585 | // return SkScalarSignAsInt(y0 - y1) + SkScalarSignAsInt(y1 - y2) != 0; |
| 2586 | if (y0 == y1) { |
| 2587 | return true; |
| 2588 | } |
| 2589 | if (y0 < y1) { |
| 2590 | return y1 <= y2; |
| 2591 | } else { |
| 2592 | return y1 >= y2; |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | static int winding_quad(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2597 | SkPoint dst[5]; |
| 2598 | int n = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2599 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2600 | if (!is_mono_quad(pts[0].fY, pts[1].fY, pts[2].fY)) { |
| 2601 | n = SkChopQuadAtYExtrema(pts, dst); |
| 2602 | pts = dst; |
| 2603 | } |
| 2604 | int w = winding_mono_quad(pts, x, y); |
| 2605 | if (n > 0) { |
| 2606 | w += winding_mono_quad(&pts[2], x, y); |
| 2607 | } |
| 2608 | return w; |
| 2609 | } |
| 2610 | |
| 2611 | static int winding_line(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2612 | SkScalar x0 = pts[0].fX; |
| 2613 | SkScalar y0 = pts[0].fY; |
| 2614 | SkScalar x1 = pts[1].fX; |
| 2615 | SkScalar y1 = pts[1].fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2616 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2617 | SkScalar dy = y1 - y0; |
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 | int dir = 1; |
| 2620 | if (y0 > y1) { |
| 2621 | SkTSwap(y0, y1); |
| 2622 | dir = -1; |
| 2623 | } |
| 2624 | if (y < y0 || y >= y1) { |
| 2625 | return 0; |
| 2626 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2627 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2628 | SkScalar cross = SkScalarMul(x1 - x0, y - pts[0].fY) - |
| 2629 | SkScalarMul(dy, x - pts[0].fX); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2630 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2631 | if (SkScalarSignAsInt(cross) == dir) { |
| 2632 | dir = 0; |
| 2633 | } |
| 2634 | return dir; |
| 2635 | } |
| 2636 | |
| 2637 | bool SkPath::contains(SkScalar x, SkScalar y) const { |
| 2638 | bool isInverse = this->isInverseFillType(); |
| 2639 | if (this->isEmpty()) { |
| 2640 | return isInverse; |
| 2641 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2642 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2643 | const SkRect& bounds = this->getBounds(); |
| 2644 | if (!bounds.contains(x, y)) { |
| 2645 | return isInverse; |
| 2646 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2647 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2648 | SkPath::Iter iter(*this, true); |
| 2649 | bool done = false; |
| 2650 | int w = 0; |
| 2651 | do { |
| 2652 | SkPoint pts[4]; |
| 2653 | switch (iter.next(pts, false)) { |
| 2654 | case SkPath::kMove_Verb: |
| 2655 | case SkPath::kClose_Verb: |
| 2656 | break; |
| 2657 | case SkPath::kLine_Verb: |
| 2658 | w += winding_line(pts, x, y); |
| 2659 | break; |
| 2660 | case SkPath::kQuad_Verb: |
| 2661 | w += winding_quad(pts, x, y); |
| 2662 | break; |
| 2663 | case SkPath::kCubic_Verb: |
| 2664 | w += winding_cubic(pts, x, y); |
| 2665 | break; |
| 2666 | case SkPath::kDone_Verb: |
| 2667 | done = true; |
| 2668 | break; |
| 2669 | } |
| 2670 | } while (!done); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2671 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2672 | switch (this->getFillType()) { |
| 2673 | case SkPath::kEvenOdd_FillType: |
| 2674 | case SkPath::kInverseEvenOdd_FillType: |
| 2675 | w &= 1; |
| 2676 | break; |
reed@google.com | e9bb623 | 2012-07-11 18:56:10 +0000 | [diff] [blame] | 2677 | default: |
| 2678 | break; |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2679 | } |
| 2680 | return SkToBool(w); |
| 2681 | } |
| 2682 | |