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