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