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