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