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