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