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