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