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