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