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