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