epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 10 | #include "SkBuffer.h" |
humper@google.com | 75e3ca1 | 2013-04-08 21:44:11 +0000 | [diff] [blame] | 11 | #include "SkErrorInternals.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkMath.h" |
humper@google.com | 75e3ca1 | 2013-04-08 21:44:11 +0000 | [diff] [blame] | 13 | #include "SkPath.h" |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 14 | #include "SkPathRef.h" |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 15 | #include "SkRRect.h" |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 16 | #include "SkThread.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 65a87cc | 2012-08-14 13:15:44 +0000 | [diff] [blame] | 18 | SK_DEFINE_INST_COUNT(SkPath); |
| 19 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 20 | // This value is just made-up for now. When count is 4, calling memset was much |
| 21 | // slower than just writing the loop. This seems odd, and hopefully in the |
| 22 | // future this we appear to have been a fluke... |
| 23 | #define MIN_COUNT_FOR_MEMSET_TO_BE_FAST 16 |
| 24 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | //////////////////////////////////////////////////////////////////////////// |
| 26 | |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Path.bounds is defined to be the bounds of all the control points. |
| 29 | * If we called bounds.join(r) we would skip r if r was empty, which breaks |
| 30 | * our promise. Hence we have a custom joiner that doesn't look at emptiness |
| 31 | */ |
| 32 | static void joinNoEmptyChecks(SkRect* dst, const SkRect& src) { |
| 33 | dst->fLeft = SkMinScalar(dst->fLeft, src.fLeft); |
| 34 | dst->fTop = SkMinScalar(dst->fTop, src.fTop); |
| 35 | dst->fRight = SkMaxScalar(dst->fRight, src.fRight); |
| 36 | dst->fBottom = SkMaxScalar(dst->fBottom, src.fBottom); |
| 37 | } |
| 38 | |
bsalomon@google.com | fb6f0f6 | 2012-01-31 18:44:34 +0000 | [diff] [blame] | 39 | static bool is_degenerate(const SkPath& path) { |
| 40 | SkPath::Iter iter(path, false); |
| 41 | SkPoint pts[4]; |
| 42 | return SkPath::kDone_Verb == iter.next(pts); |
| 43 | } |
| 44 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 45 | class SkAutoDisableOvalCheck { |
| 46 | public: |
| 47 | SkAutoDisableOvalCheck(SkPath* path) : fPath(path) { |
| 48 | fSaved = fPath->fIsOval; |
| 49 | } |
| 50 | |
| 51 | ~SkAutoDisableOvalCheck() { |
| 52 | fPath->fIsOval = fSaved; |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | SkPath* fPath; |
| 57 | bool fSaved; |
| 58 | }; |
| 59 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 60 | class SkAutoDisableDirectionCheck { |
| 61 | public: |
| 62 | SkAutoDisableDirectionCheck(SkPath* path) : fPath(path) { |
| 63 | fSaved = static_cast<SkPath::Direction>(fPath->fDirection); |
| 64 | } |
| 65 | |
| 66 | ~SkAutoDisableDirectionCheck() { |
| 67 | fPath->fDirection = fSaved; |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | SkPath* fPath; |
| 72 | SkPath::Direction fSaved; |
| 73 | }; |
| 74 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | /* This guy's constructor/destructor bracket a path editing operation. It is |
| 76 | used when we know the bounds of the amount we are going to add to the path |
| 77 | (usually a new contour, but not required). |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 78 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | 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] | 80 | 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] | 81 | avoiding the need to revisit all of the points in getBounds(). |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 82 | |
bsalomon@google.com | fb6f0f6 | 2012-01-31 18:44:34 +0000 | [diff] [blame] | 83 | It also notes if the path was originally degenerate, and if so, sets |
| 84 | isConvex to true. Thus it can only be used if the contour being added is |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 85 | convex (which is always true since we only allow the addition of rects). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | */ |
| 87 | class SkAutoPathBoundsUpdate { |
| 88 | public: |
| 89 | SkAutoPathBoundsUpdate(SkPath* path, const SkRect& r) : fRect(r) { |
| 90 | this->init(path); |
| 91 | } |
| 92 | |
| 93 | SkAutoPathBoundsUpdate(SkPath* path, SkScalar left, SkScalar top, |
| 94 | SkScalar right, SkScalar bottom) { |
| 95 | fRect.set(left, top, right, bottom); |
| 96 | this->init(path); |
| 97 | } |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 98 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | ~SkAutoPathBoundsUpdate() { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 100 | fPath->setConvexity(fDegenerate ? SkPath::kConvex_Convexity |
| 101 | : SkPath::kUnknown_Convexity); |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 102 | if (fEmpty || fHasValidBounds) { |
| 103 | fPath->setBounds(fRect); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 106 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | private: |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 108 | SkPath* fPath; |
| 109 | SkRect fRect; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 110 | bool fHasValidBounds; |
bsalomon@google.com | fb6f0f6 | 2012-01-31 18:44:34 +0000 | [diff] [blame] | 111 | bool fDegenerate; |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 112 | bool fEmpty; |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 113 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 114 | void init(SkPath* path) { |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 115 | // Cannot use fRect for our bounds unless we know it is sorted |
| 116 | fRect.sort(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | fPath = path; |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 118 | // Mark the path's bounds as dirty if (1) they are, or (2) the path |
| 119 | // is non-finite, and therefore its bounds are not meaningful |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 120 | fHasValidBounds = path->hasComputedBounds() && path->isFinite(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | fEmpty = path->isEmpty(); |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 122 | if (fHasValidBounds && !fEmpty) { |
| 123 | joinNoEmptyChecks(&fRect, fPath->getBounds()); |
| 124 | } |
| 125 | fDegenerate = is_degenerate(*path); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | } |
| 127 | }; |
| 128 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | //////////////////////////////////////////////////////////////////////////// |
| 130 | |
| 131 | /* |
| 132 | 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] | 133 | - 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] | 134 | - we insert a Move(0,0) if Line | Quad | Cubic is our first command |
| 135 | |
| 136 | The iterator does more cleanup, especially if forceClose == true |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 137 | 1. If we encounter degenerate segments, remove them |
| 138 | 2. if we encounter Close, return a cons'd up Line() first (if the curr-pt != start-pt) |
| 139 | 3. if we encounter Move without a preceeding Close, and forceClose is true, goto #2 |
| 140 | 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] | 141 | */ |
| 142 | |
| 143 | //////////////////////////////////////////////////////////////////////////// |
| 144 | |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 145 | // flag to require a moveTo if we begin with something else, like lineTo etc. |
| 146 | #define INITIAL_LASTMOVETOINDEX_VALUE ~0 |
| 147 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 148 | SkPath::SkPath() |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 149 | : fPathRef(SkPathRef::CreateEmpty()) |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 150 | #ifdef SK_BUILD_FOR_ANDROID |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 151 | , fSourcePath(NULL) |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 152 | #endif |
| 153 | { |
| 154 | this->resetFields(); |
| 155 | } |
| 156 | |
| 157 | void SkPath::resetFields() { |
| 158 | //fPathRef is assumed to have been emptied by the caller. |
| 159 | fLastMoveToIndex = INITIAL_LASTMOVETOINDEX_VALUE; |
| 160 | fFillType = kWinding_FillType; |
| 161 | fSegmentMask = 0; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 162 | fConvexity = kUnknown_Convexity; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 163 | fDirection = kUnknown_Direction; |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 164 | fIsOval = false; |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 165 | |
| 166 | // We don't touch Android's fSourcePath. It's used to track texture garbage collection, so we |
| 167 | // don't want to muck with it if it's been set to something non-NULL. |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 168 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 170 | SkPath::SkPath(const SkPath& that) |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 171 | : fPathRef(SkRef(that.fPathRef.get())) { |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 172 | this->copyFields(that); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 173 | #ifdef SK_BUILD_FOR_ANDROID |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 174 | fSourcePath = that.fSourcePath; |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 175 | #endif |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 176 | SkDEBUGCODE(that.validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | SkPath::~SkPath() { |
| 180 | SkDEBUGCODE(this->validate();) |
| 181 | } |
| 182 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 183 | SkPath& SkPath::operator=(const SkPath& that) { |
| 184 | SkDEBUGCODE(that.validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 186 | if (this != &that) { |
| 187 | fPathRef.reset(SkRef(that.fPathRef.get())); |
| 188 | this->copyFields(that); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 189 | #ifdef SK_BUILD_FOR_ANDROID |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 190 | fSourcePath = that.fSourcePath; |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 191 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | } |
| 193 | SkDEBUGCODE(this->validate();) |
| 194 | return *this; |
| 195 | } |
| 196 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 197 | void SkPath::copyFields(const SkPath& that) { |
| 198 | //fPathRef is assumed to have been set by the caller. |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 199 | fLastMoveToIndex = that.fLastMoveToIndex; |
| 200 | fFillType = that.fFillType; |
| 201 | fSegmentMask = that.fSegmentMask; |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 202 | fConvexity = that.fConvexity; |
| 203 | fDirection = that.fDirection; |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 204 | fIsOval = that.fIsOval; |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 205 | } |
| 206 | |
djsollen@google.com | 9c1a967 | 2013-08-09 13:49:13 +0000 | [diff] [blame] | 207 | bool operator==(const SkPath& a, const SkPath& b) { |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 208 | // note: don't need to look at isConvex or bounds, since just comparing the |
| 209 | // raw data is sufficient. |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 210 | |
| 211 | // We explicitly check fSegmentMask as a quick-reject. We could skip it, |
| 212 | // since it is only a cache of info in the fVerbs, but its a fast way to |
| 213 | // notice a difference |
| 214 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 215 | return &a == &b || |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 216 | (a.fFillType == b.fFillType && a.fSegmentMask == b.fSegmentMask && |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 217 | *a.fPathRef.get() == *b.fPathRef.get()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 218 | } |
| 219 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 220 | void SkPath::swap(SkPath& that) { |
| 221 | SkASSERT(&that != NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 223 | if (this != &that) { |
| 224 | fPathRef.swap(&that.fPathRef); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 225 | SkTSwap<int>(fLastMoveToIndex, that.fLastMoveToIndex); |
| 226 | SkTSwap<uint8_t>(fFillType, that.fFillType); |
| 227 | SkTSwap<uint8_t>(fSegmentMask, that.fSegmentMask); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 228 | SkTSwap<uint8_t>(fConvexity, that.fConvexity); |
| 229 | SkTSwap<uint8_t>(fDirection, that.fDirection); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 230 | SkTSwap<SkBool8>(fIsOval, that.fIsOval); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 231 | #ifdef SK_BUILD_FOR_ANDROID |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 232 | SkTSwap<const SkPath*>(fSourcePath, that.fSourcePath); |
| 233 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 237 | static inline bool check_edge_against_rect(const SkPoint& p0, |
| 238 | const SkPoint& p1, |
| 239 | const SkRect& rect, |
| 240 | SkPath::Direction dir) { |
| 241 | const SkPoint* edgeBegin; |
| 242 | SkVector v; |
| 243 | if (SkPath::kCW_Direction == dir) { |
| 244 | v = p1 - p0; |
| 245 | edgeBegin = &p0; |
| 246 | } else { |
| 247 | v = p0 - p1; |
| 248 | edgeBegin = &p1; |
| 249 | } |
| 250 | if (v.fX || v.fY) { |
| 251 | // check the cross product of v with the vec from edgeBegin to each rect corner |
| 252 | SkScalar yL = SkScalarMul(v.fY, rect.fLeft - edgeBegin->fX); |
| 253 | SkScalar xT = SkScalarMul(v.fX, rect.fTop - edgeBegin->fY); |
| 254 | SkScalar yR = SkScalarMul(v.fY, rect.fRight - edgeBegin->fX); |
| 255 | SkScalar xB = SkScalarMul(v.fX, rect.fBottom - edgeBegin->fY); |
| 256 | if ((xT < yL) || (xT < yR) || (xB < yL) || (xB < yR)) { |
| 257 | return false; |
| 258 | } |
| 259 | } |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | bool SkPath::conservativelyContainsRect(const SkRect& rect) const { |
| 264 | // This only handles non-degenerate convex paths currently. |
| 265 | if (kConvex_Convexity != this->getConvexity()) { |
| 266 | return false; |
| 267 | } |
skia.committer@gmail.com | cec8de6 | 2012-11-14 02:01:22 +0000 | [diff] [blame] | 268 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 269 | Direction direction; |
| 270 | if (!this->cheapComputeDirection(&direction)) { |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | SkPoint firstPt; |
| 275 | SkPoint prevPt; |
| 276 | RawIter iter(*this); |
| 277 | SkPath::Verb verb; |
| 278 | SkPoint pts[4]; |
| 279 | SkDEBUGCODE(int moveCnt = 0;) |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 280 | SkDEBUGCODE(int segmentCount = 0;) |
| 281 | SkDEBUGCODE(int closeCount = 0;) |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 282 | |
| 283 | while ((verb = iter.next(pts)) != kDone_Verb) { |
| 284 | int nextPt = -1; |
| 285 | switch (verb) { |
| 286 | case kMove_Verb: |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 287 | SkASSERT(!segmentCount && !closeCount); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 288 | SkDEBUGCODE(++moveCnt); |
| 289 | firstPt = prevPt = pts[0]; |
| 290 | break; |
| 291 | case kLine_Verb: |
| 292 | nextPt = 1; |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 293 | SkASSERT(moveCnt && !closeCount); |
| 294 | SkDEBUGCODE(++segmentCount); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 295 | break; |
| 296 | case kQuad_Verb: |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 297 | case kConic_Verb: |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 298 | SkASSERT(moveCnt && !closeCount); |
| 299 | SkDEBUGCODE(++segmentCount); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 300 | nextPt = 2; |
| 301 | break; |
| 302 | case kCubic_Verb: |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 303 | SkASSERT(moveCnt && !closeCount); |
| 304 | SkDEBUGCODE(++segmentCount); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 305 | nextPt = 3; |
| 306 | break; |
| 307 | case kClose_Verb: |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 308 | SkDEBUGCODE(++closeCount;) |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 309 | break; |
| 310 | default: |
| 311 | SkDEBUGFAIL("unknown verb"); |
| 312 | } |
| 313 | if (-1 != nextPt) { |
| 314 | if (!check_edge_against_rect(prevPt, pts[nextPt], rect, direction)) { |
| 315 | return false; |
| 316 | } |
| 317 | prevPt = pts[nextPt]; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return check_edge_against_rect(prevPt, firstPt, rect, direction); |
| 322 | } |
| 323 | |
robertphillips@google.com | 7101abe | 2013-10-29 22:45:37 +0000 | [diff] [blame] | 324 | uint32_t SkPath::getGenerationID() const { |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 325 | uint32_t genID = fPathRef->genID(); |
| 326 | #ifdef SK_BUILD_FOR_ANDROID |
| 327 | SkASSERT((unsigned)fFillType < (1 << (32 - kPathRefGenIDBitCnt))); |
| 328 | genID |= static_cast<uint32_t>(fFillType) << kPathRefGenIDBitCnt; |
| 329 | #endif |
| 330 | return genID; |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 331 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 332 | |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 333 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 334 | const SkPath* SkPath::getSourcePath() const { |
| 335 | return fSourcePath; |
| 336 | } |
| 337 | |
| 338 | void SkPath::setSourcePath(const SkPath* path) { |
| 339 | fSourcePath = path; |
| 340 | } |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 341 | #endif |
| 342 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | void SkPath::reset() { |
| 344 | SkDEBUGCODE(this->validate();) |
| 345 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 346 | fPathRef.reset(SkPathRef::CreateEmpty()); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 347 | this->resetFields(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void SkPath::rewind() { |
| 351 | SkDEBUGCODE(this->validate();) |
| 352 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 353 | SkPathRef::Rewind(&fPathRef); |
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 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 357 | bool SkPath::isLine(SkPoint line[2]) const { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 358 | int verbCount = fPathRef->countVerbs(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 359 | |
commit-bot@chromium.org | a62efcc | 2013-08-05 13:23:13 +0000 | [diff] [blame] | 360 | if (2 == verbCount) { |
| 361 | SkASSERT(kMove_Verb == fPathRef->atVerb(0)); |
| 362 | if (kLine_Verb == fPathRef->atVerb(1)) { |
| 363 | SkASSERT(2 == fPathRef->countPoints()); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 364 | if (line) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 365 | const SkPoint* pts = fPathRef->points(); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 366 | line[0] = pts[0]; |
| 367 | line[1] = pts[1]; |
| 368 | } |
| 369 | return true; |
| 370 | } |
| 371 | } |
| 372 | return false; |
| 373 | } |
| 374 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 375 | /* |
| 376 | Determines if path is a rect by keeping track of changes in direction |
| 377 | and looking for a loop either clockwise or counterclockwise. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 378 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 379 | The direction is computed such that: |
| 380 | 0: vertical up |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 381 | 1: horizontal left |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 382 | 2: vertical down |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 383 | 3: horizontal right |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 384 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 385 | A rectangle cycles up/right/down/left or up/left/down/right. |
| 386 | |
| 387 | The test fails if: |
| 388 | The path is closed, and followed by a line. |
| 389 | A second move creates a new endpoint. |
| 390 | A diagonal line is parsed. |
| 391 | There's more than four changes of direction. |
| 392 | There's a discontinuity on the line (e.g., a move in the middle) |
| 393 | The line reverses direction. |
| 394 | The rectangle doesn't complete a cycle. |
| 395 | The path contains a quadratic or cubic. |
| 396 | The path contains fewer than four points. |
| 397 | The final point isn't equal to the first point. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 398 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 399 | It's OK if the path has: |
| 400 | Several colinear line segments composing a rectangle side. |
| 401 | Single points on the rectangle side. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 402 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 403 | The direction takes advantage of the corners found since opposite sides |
| 404 | must travel in opposite directions. |
| 405 | |
| 406 | FIXME: Allow colinear quads and cubics to be treated like lines. |
| 407 | FIXME: If the API passes fill-only, return true if the filled stroke |
| 408 | is a rectangle, though the caller failed to close the path. |
| 409 | */ |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 410 | bool SkPath::isRectContour(bool allowPartial, int* currVerb, const SkPoint** ptsPtr, |
| 411 | bool* isClosed, Direction* direction) const { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 412 | int corners = 0; |
| 413 | SkPoint first, last; |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 414 | const SkPoint* pts = *ptsPtr; |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 415 | const SkPoint* savePts = NULL; |
tomhudson@google.com | 2c2508d | 2011-07-29 13:44:30 +0000 | [diff] [blame] | 416 | first.set(0, 0); |
| 417 | last.set(0, 0); |
| 418 | int firstDirection = 0; |
| 419 | int lastDirection = 0; |
| 420 | int nextDirection = 0; |
| 421 | bool closedOrMoved = false; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 422 | bool autoClose = false; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 423 | int verbCnt = fPathRef->countVerbs(); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 424 | while (*currVerb < verbCnt && (!allowPartial || !autoClose)) { |
| 425 | switch (fPathRef->atVerb(*currVerb)) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 426 | case kClose_Verb: |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 427 | savePts = pts; |
| 428 | pts = *ptsPtr; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 429 | autoClose = true; |
| 430 | case kLine_Verb: { |
| 431 | SkScalar left = last.fX; |
| 432 | SkScalar top = last.fY; |
| 433 | SkScalar right = pts->fX; |
| 434 | SkScalar bottom = pts->fY; |
| 435 | ++pts; |
| 436 | if (left != right && top != bottom) { |
| 437 | return false; // diagonal |
| 438 | } |
| 439 | if (left == right && top == bottom) { |
| 440 | break; // single point on side OK |
| 441 | } |
| 442 | nextDirection = (left != right) << 0 | |
| 443 | (left < right || top < bottom) << 1; |
| 444 | if (0 == corners) { |
| 445 | firstDirection = nextDirection; |
| 446 | first = last; |
| 447 | last = pts[-1]; |
| 448 | corners = 1; |
| 449 | closedOrMoved = false; |
| 450 | break; |
| 451 | } |
| 452 | if (closedOrMoved) { |
| 453 | return false; // closed followed by a line |
| 454 | } |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 455 | if (autoClose && nextDirection == firstDirection) { |
| 456 | break; // colinear with first |
| 457 | } |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 458 | closedOrMoved = autoClose; |
| 459 | if (lastDirection != nextDirection) { |
| 460 | if (++corners > 4) { |
| 461 | return false; // too many direction changes |
| 462 | } |
| 463 | } |
| 464 | last = pts[-1]; |
| 465 | if (lastDirection == nextDirection) { |
| 466 | break; // colinear segment |
| 467 | } |
| 468 | // Possible values for corners are 2, 3, and 4. |
| 469 | // When corners == 3, nextDirection opposes firstDirection. |
| 470 | // Otherwise, nextDirection at corner 2 opposes corner 4. |
tomhudson@google.com | 2c2508d | 2011-07-29 13:44:30 +0000 | [diff] [blame] | 471 | int turn = firstDirection ^ (corners - 1); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 472 | int directionCycle = 3 == corners ? 0 : nextDirection ^ turn; |
| 473 | if ((directionCycle ^ turn) != nextDirection) { |
| 474 | return false; // direction didn't follow cycle |
| 475 | } |
| 476 | break; |
| 477 | } |
| 478 | case kQuad_Verb: |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 479 | case kConic_Verb: |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 480 | case kCubic_Verb: |
| 481 | return false; // quadratic, cubic not allowed |
| 482 | case kMove_Verb: |
| 483 | last = *pts++; |
| 484 | closedOrMoved = true; |
| 485 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 486 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 487 | SkDEBUGFAIL("unexpected verb"); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 488 | break; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 489 | } |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 490 | *currVerb += 1; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 491 | lastDirection = nextDirection; |
| 492 | } |
| 493 | // Success if 4 corners and first point equals last |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 494 | bool result = 4 == corners && (first == last || autoClose); |
| 495 | if (savePts) { |
| 496 | *ptsPtr = savePts; |
| 497 | } |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 498 | if (result && isClosed) { |
| 499 | *isClosed = autoClose; |
| 500 | } |
| 501 | if (result && direction) { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 502 | *direction = firstDirection == ((lastDirection + 1) & 3) ? kCCW_Direction : kCW_Direction; |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 503 | } |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 504 | return result; |
| 505 | } |
| 506 | |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 507 | bool SkPath::isRect(SkRect* rect) const { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 508 | SkDEBUGCODE(this->validate();) |
| 509 | int currVerb = 0; |
| 510 | const SkPoint* pts = fPathRef->points(); |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 511 | bool result = isRectContour(false, &currVerb, &pts, NULL, NULL); |
| 512 | if (result && rect) { |
| 513 | *rect = getBounds(); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 514 | } |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 515 | return result; |
| 516 | } |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 517 | |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 518 | bool SkPath::isRect(bool* isClosed, Direction* direction) const { |
| 519 | SkDEBUGCODE(this->validate();) |
| 520 | int currVerb = 0; |
| 521 | const SkPoint* pts = fPathRef->points(); |
| 522 | return isRectContour(false, &currVerb, &pts, isClosed, direction); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 523 | } |
| 524 | |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 525 | bool SkPath::isNestedRects(SkRect rects[2], Direction dirs[2]) const { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 526 | SkDEBUGCODE(this->validate();) |
| 527 | int currVerb = 0; |
| 528 | const SkPoint* pts = fPathRef->points(); |
| 529 | const SkPoint* first = pts; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 530 | Direction testDirs[2]; |
| 531 | if (!isRectContour(true, &currVerb, &pts, NULL, &testDirs[0])) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 532 | return false; |
| 533 | } |
| 534 | const SkPoint* last = pts; |
| 535 | SkRect testRects[2]; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 536 | if (isRectContour(false, &currVerb, &pts, NULL, &testDirs[1])) { |
scroggo@google.com | 614f9e3 | 2013-05-09 18:05:32 +0000 | [diff] [blame] | 537 | testRects[0].set(first, SkToS32(last - first)); |
| 538 | testRects[1].set(last, SkToS32(pts - last)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 539 | if (testRects[0].contains(testRects[1])) { |
| 540 | if (rects) { |
| 541 | rects[0] = testRects[0]; |
| 542 | rects[1] = testRects[1]; |
| 543 | } |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 544 | if (dirs) { |
| 545 | dirs[0] = testDirs[0]; |
| 546 | dirs[1] = testDirs[1]; |
| 547 | } |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 548 | return true; |
| 549 | } |
| 550 | if (testRects[1].contains(testRects[0])) { |
| 551 | if (rects) { |
| 552 | rects[0] = testRects[1]; |
| 553 | rects[1] = testRects[0]; |
| 554 | } |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 555 | if (dirs) { |
| 556 | dirs[0] = testDirs[1]; |
| 557 | dirs[1] = testDirs[0]; |
| 558 | } |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 559 | return true; |
| 560 | } |
| 561 | } |
| 562 | return false; |
| 563 | } |
| 564 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 565 | int SkPath::countPoints() const { |
| 566 | return fPathRef->countPoints(); |
| 567 | } |
| 568 | |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 569 | int SkPath::getPoints(SkPoint dst[], int max) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 570 | SkDEBUGCODE(this->validate();) |
| 571 | |
| 572 | SkASSERT(max >= 0); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 573 | SkASSERT(!max || dst); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 574 | int count = SkMin32(max, fPathRef->countPoints()); |
| 575 | memcpy(dst, fPathRef->points(), count * sizeof(SkPoint)); |
| 576 | return fPathRef->countPoints(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 577 | } |
| 578 | |
reed@android.com | d3aa4ff | 2010-02-09 16:38:45 +0000 | [diff] [blame] | 579 | SkPoint SkPath::getPoint(int index) const { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 580 | if ((unsigned)index < (unsigned)fPathRef->countPoints()) { |
| 581 | return fPathRef->atPoint(index); |
reed@android.com | d3aa4ff | 2010-02-09 16:38:45 +0000 | [diff] [blame] | 582 | } |
| 583 | return SkPoint::Make(0, 0); |
| 584 | } |
| 585 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 586 | int SkPath::countVerbs() const { |
| 587 | return fPathRef->countVerbs(); |
| 588 | } |
| 589 | |
| 590 | static inline void copy_verbs_reverse(uint8_t* inorderDst, |
| 591 | const uint8_t* reversedSrc, |
| 592 | int count) { |
| 593 | for (int i = 0; i < count; ++i) { |
| 594 | inorderDst[i] = reversedSrc[~i]; |
| 595 | } |
| 596 | } |
| 597 | |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 598 | int SkPath::getVerbs(uint8_t dst[], int max) const { |
| 599 | SkDEBUGCODE(this->validate();) |
| 600 | |
| 601 | SkASSERT(max >= 0); |
| 602 | SkASSERT(!max || dst); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 603 | int count = SkMin32(max, fPathRef->countVerbs()); |
| 604 | copy_verbs_reverse(dst, fPathRef->verbs(), count); |
| 605 | return fPathRef->countVerbs(); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 606 | } |
| 607 | |
reed@google.com | 294dd7b | 2011-10-11 11:58:32 +0000 | [diff] [blame] | 608 | bool SkPath::getLastPt(SkPoint* lastPt) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 609 | SkDEBUGCODE(this->validate();) |
| 610 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 611 | int count = fPathRef->countPoints(); |
reed@google.com | 294dd7b | 2011-10-11 11:58:32 +0000 | [diff] [blame] | 612 | if (count > 0) { |
| 613 | if (lastPt) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 614 | *lastPt = fPathRef->atPoint(count - 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 615 | } |
reed@google.com | 294dd7b | 2011-10-11 11:58:32 +0000 | [diff] [blame] | 616 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 617 | } |
reed@google.com | 294dd7b | 2011-10-11 11:58:32 +0000 | [diff] [blame] | 618 | if (lastPt) { |
| 619 | lastPt->set(0, 0); |
| 620 | } |
| 621 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | void SkPath::setLastPt(SkScalar x, SkScalar y) { |
| 625 | SkDEBUGCODE(this->validate();) |
| 626 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 627 | int count = fPathRef->countPoints(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 628 | if (count == 0) { |
| 629 | this->moveTo(x, y); |
| 630 | } else { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 631 | fIsOval = false; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 632 | SkPathRef::Editor ed(&fPathRef); |
| 633 | ed.atPoint(count-1)->set(x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 637 | void SkPath::setConvexity(Convexity c) { |
| 638 | if (fConvexity != c) { |
| 639 | fConvexity = c; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 643 | ////////////////////////////////////////////////////////////////////////////// |
| 644 | // Construction methods |
| 645 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 646 | #define DIRTY_AFTER_EDIT \ |
| 647 | do { \ |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 648 | fConvexity = kUnknown_Convexity; \ |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 649 | fDirection = kUnknown_Direction; \ |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 650 | fIsOval = false; \ |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 651 | } while (0) |
| 652 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 653 | void SkPath::incReserve(U16CPU inc) { |
| 654 | SkDEBUGCODE(this->validate();) |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 655 | SkPathRef::Editor(&fPathRef, inc, inc); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 656 | SkDEBUGCODE(this->validate();) |
| 657 | } |
| 658 | |
| 659 | void SkPath::moveTo(SkScalar x, SkScalar y) { |
| 660 | SkDEBUGCODE(this->validate();) |
| 661 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 662 | SkPathRef::Editor ed(&fPathRef); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 663 | |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 664 | // remember our index |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 665 | fLastMoveToIndex = ed.pathRef()->countPoints(); |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 666 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 667 | ed.growForVerb(kMove_Verb)->set(x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void SkPath::rMoveTo(SkScalar x, SkScalar y) { |
| 671 | SkPoint pt; |
| 672 | this->getLastPt(&pt); |
| 673 | this->moveTo(pt.fX + x, pt.fY + y); |
| 674 | } |
| 675 | |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 676 | void SkPath::injectMoveToIfNeeded() { |
| 677 | if (fLastMoveToIndex < 0) { |
| 678 | SkScalar x, y; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 679 | if (fPathRef->countVerbs() == 0) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 680 | x = y = 0; |
| 681 | } else { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 682 | const SkPoint& pt = fPathRef->atPoint(~fLastMoveToIndex); |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 683 | x = pt.fX; |
| 684 | y = pt.fY; |
| 685 | } |
| 686 | this->moveTo(x, y); |
| 687 | } |
| 688 | } |
| 689 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 690 | void SkPath::lineTo(SkScalar x, SkScalar y) { |
| 691 | SkDEBUGCODE(this->validate();) |
| 692 | |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 693 | this->injectMoveToIfNeeded(); |
| 694 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 695 | SkPathRef::Editor ed(&fPathRef); |
| 696 | ed.growForVerb(kLine_Verb)->set(x, y); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 697 | fSegmentMask |= kLine_SegmentMask; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 698 | |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 699 | DIRTY_AFTER_EDIT; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | void SkPath::rLineTo(SkScalar x, SkScalar y) { |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 703 | this->injectMoveToIfNeeded(); // This can change the result of this->getLastPt(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 704 | SkPoint pt; |
| 705 | this->getLastPt(&pt); |
| 706 | this->lineTo(pt.fX + x, pt.fY + y); |
| 707 | } |
| 708 | |
| 709 | void SkPath::quadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
| 710 | SkDEBUGCODE(this->validate();) |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 711 | |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 712 | this->injectMoveToIfNeeded(); |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 713 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 714 | SkPathRef::Editor ed(&fPathRef); |
| 715 | SkPoint* pts = ed.growForVerb(kQuad_Verb); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 716 | pts[0].set(x1, y1); |
| 717 | pts[1].set(x2, y2); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 718 | fSegmentMask |= kQuad_SegmentMask; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 719 | |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 720 | DIRTY_AFTER_EDIT; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | 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] | 724 | this->injectMoveToIfNeeded(); // This can change the result of this->getLastPt(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 725 | SkPoint pt; |
| 726 | this->getLastPt(&pt); |
| 727 | this->quadTo(pt.fX + x1, pt.fY + y1, pt.fX + x2, pt.fY + y2); |
| 728 | } |
| 729 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 730 | void SkPath::conicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 731 | SkScalar w) { |
| 732 | // check for <= 0 or NaN with this test |
| 733 | if (!(w > 0)) { |
| 734 | this->lineTo(x2, y2); |
| 735 | } else if (!SkScalarIsFinite(w)) { |
| 736 | this->lineTo(x1, y1); |
| 737 | this->lineTo(x2, y2); |
| 738 | } else if (SK_Scalar1 == w) { |
| 739 | this->quadTo(x1, y1, x2, y2); |
| 740 | } else { |
| 741 | SkDEBUGCODE(this->validate();) |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 742 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 743 | this->injectMoveToIfNeeded(); |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 744 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 745 | SkPathRef::Editor ed(&fPathRef); |
| 746 | SkPoint* pts = ed.growForConic(w); |
| 747 | pts[0].set(x1, y1); |
| 748 | pts[1].set(x2, y2); |
| 749 | fSegmentMask |= kConic_SegmentMask; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 750 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 751 | DIRTY_AFTER_EDIT; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | void SkPath::rConicTo(SkScalar dx1, SkScalar dy1, SkScalar dx2, SkScalar dy2, |
| 756 | SkScalar w) { |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 757 | this->injectMoveToIfNeeded(); // This can change the result of this->getLastPt(). |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 758 | SkPoint pt; |
| 759 | this->getLastPt(&pt); |
| 760 | this->conicTo(pt.fX + dx1, pt.fY + dy1, pt.fX + dx2, pt.fY + dy2, w); |
| 761 | } |
| 762 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 763 | void SkPath::cubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 764 | SkScalar x3, SkScalar y3) { |
| 765 | SkDEBUGCODE(this->validate();) |
| 766 | |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 767 | this->injectMoveToIfNeeded(); |
| 768 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 769 | SkPathRef::Editor ed(&fPathRef); |
| 770 | SkPoint* pts = ed.growForVerb(kCubic_Verb); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 771 | pts[0].set(x1, y1); |
| 772 | pts[1].set(x2, y2); |
| 773 | pts[2].set(x3, y3); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 774 | fSegmentMask |= kCubic_SegmentMask; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 775 | |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 776 | DIRTY_AFTER_EDIT; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | void SkPath::rCubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 780 | SkScalar x3, SkScalar y3) { |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 781 | this->injectMoveToIfNeeded(); // This can change the result of this->getLastPt(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 782 | SkPoint pt; |
| 783 | this->getLastPt(&pt); |
| 784 | this->cubicTo(pt.fX + x1, pt.fY + y1, pt.fX + x2, pt.fY + y2, |
| 785 | pt.fX + x3, pt.fY + y3); |
| 786 | } |
| 787 | |
| 788 | void SkPath::close() { |
| 789 | SkDEBUGCODE(this->validate();) |
| 790 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 791 | int count = fPathRef->countVerbs(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 792 | if (count > 0) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 793 | switch (fPathRef->atVerb(count - 1)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 794 | case kLine_Verb: |
| 795 | case kQuad_Verb: |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 796 | case kConic_Verb: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 797 | case kCubic_Verb: |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 798 | case kMove_Verb: { |
| 799 | SkPathRef::Editor ed(&fPathRef); |
| 800 | ed.growForVerb(kClose_Verb); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 801 | break; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 802 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 803 | case kClose_Verb: |
reed@google.com | fa2f2a4 | 2013-05-30 15:29:48 +0000 | [diff] [blame] | 804 | // 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] | 805 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 806 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 807 | SkDEBUGFAIL("unexpected verb"); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 808 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 811 | |
| 812 | // signal that we need a moveTo to follow us (unless we're done) |
| 813 | #if 0 |
| 814 | if (fLastMoveToIndex >= 0) { |
| 815 | fLastMoveToIndex = ~fLastMoveToIndex; |
| 816 | } |
| 817 | #else |
| 818 | fLastMoveToIndex ^= ~fLastMoveToIndex >> (8 * sizeof(fLastMoveToIndex) - 1); |
| 819 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 823 | |
reed@google.com | a8a3b3d | 2012-11-26 18:16:27 +0000 | [diff] [blame] | 824 | static void assert_known_direction(int dir) { |
| 825 | SkASSERT(SkPath::kCW_Direction == dir || SkPath::kCCW_Direction == dir); |
| 826 | } |
| 827 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 828 | void SkPath::addRect(const SkRect& rect, Direction dir) { |
| 829 | this->addRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, dir); |
| 830 | } |
| 831 | |
| 832 | void SkPath::addRect(SkScalar left, SkScalar top, SkScalar right, |
| 833 | SkScalar bottom, Direction dir) { |
reed@google.com | a8a3b3d | 2012-11-26 18:16:27 +0000 | [diff] [blame] | 834 | assert_known_direction(dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 835 | fDirection = this->hasOnlyMoveTos() ? dir : kUnknown_Direction; |
| 836 | SkAutoDisableDirectionCheck addc(this); |
| 837 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 838 | SkAutoPathBoundsUpdate apbu(this, left, top, right, bottom); |
| 839 | |
| 840 | this->incReserve(5); |
| 841 | |
| 842 | this->moveTo(left, top); |
| 843 | if (dir == kCCW_Direction) { |
| 844 | this->lineTo(left, bottom); |
| 845 | this->lineTo(right, bottom); |
| 846 | this->lineTo(right, top); |
| 847 | } else { |
| 848 | this->lineTo(right, top); |
| 849 | this->lineTo(right, bottom); |
| 850 | this->lineTo(left, bottom); |
| 851 | } |
| 852 | this->close(); |
| 853 | } |
| 854 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 855 | void SkPath::addPoly(const SkPoint pts[], int count, bool close) { |
| 856 | SkDEBUGCODE(this->validate();) |
| 857 | if (count <= 0) { |
| 858 | return; |
| 859 | } |
| 860 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 861 | SkPathRef::Editor ed(&fPathRef); |
| 862 | fLastMoveToIndex = ed.pathRef()->countPoints(); |
| 863 | uint8_t* vb; |
| 864 | SkPoint* p; |
bsalomon@google.com | 6c5418e | 2012-09-14 20:30:37 +0000 | [diff] [blame] | 865 | // +close makes room for the extra kClose_Verb |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 866 | ed.grow(count + close, count, &vb, &p); |
bsalomon@google.com | 6c5418e | 2012-09-14 20:30:37 +0000 | [diff] [blame] | 867 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 868 | memcpy(p, pts, count * sizeof(SkPoint)); |
| 869 | vb[~0] = kMove_Verb; |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 870 | if (count > 1) { |
| 871 | // cast to unsigned, so if MIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to |
| 872 | // be 0, the compiler will remove the test/branch entirely. |
| 873 | if ((unsigned)count >= MIN_COUNT_FOR_MEMSET_TO_BE_FAST) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 874 | memset(vb - count, kLine_Verb, count - 1); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 875 | } else { |
| 876 | for (int i = 1; i < count; ++i) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 877 | vb[~i] = kLine_Verb; |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | fSegmentMask |= kLine_SegmentMask; |
| 881 | } |
| 882 | if (close) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 883 | vb[~count] = kClose_Verb; |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 884 | } |
| 885 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 886 | DIRTY_AFTER_EDIT; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 887 | SkDEBUGCODE(this->validate();) |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 888 | } |
| 889 | |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 890 | #include "SkGeometry.h" |
| 891 | |
| 892 | static int build_arc_points(const SkRect& oval, SkScalar startAngle, |
| 893 | SkScalar sweepAngle, |
| 894 | SkPoint pts[kSkBuildQuadArcStorage]) { |
| 895 | |
| 896 | if (0 == sweepAngle && |
| 897 | (0 == startAngle || SkIntToScalar(360) == startAngle)) { |
| 898 | // Chrome uses this path to move into and out of ovals. If not |
| 899 | // treated as a special case the moves can distort the oval's |
| 900 | // bounding box (and break the circle special case). |
| 901 | pts[0].set(oval.fRight, oval.centerY()); |
| 902 | return 1; |
| 903 | } else if (0 == oval.width() && 0 == oval.height()) { |
| 904 | // Chrome will sometimes create 0 radius round rects. Having degenerate |
| 905 | // quad segments in the path prevents the path from being recognized as |
| 906 | // a rect. |
| 907 | // TODO: optimizing the case where only one of width or height is zero |
| 908 | // should also be considered. This case, however, doesn't seem to be |
| 909 | // as common as the single point case. |
| 910 | pts[0].set(oval.fRight, oval.fTop); |
| 911 | return 1; |
| 912 | } |
| 913 | |
| 914 | SkVector start, stop; |
| 915 | |
| 916 | start.fY = SkScalarSinCos(SkDegreesToRadians(startAngle), &start.fX); |
| 917 | stop.fY = SkScalarSinCos(SkDegreesToRadians(startAngle + sweepAngle), |
| 918 | &stop.fX); |
| 919 | |
| 920 | /* If the sweep angle is nearly (but less than) 360, then due to precision |
| 921 | loss in radians-conversion and/or sin/cos, we may end up with coincident |
| 922 | vectors, which will fool SkBuildQuadArc into doing nothing (bad) instead |
| 923 | of drawing a nearly complete circle (good). |
| 924 | e.g. canvas.drawArc(0, 359.99, ...) |
| 925 | -vs- canvas.drawArc(0, 359.9, ...) |
| 926 | We try to detect this edge case, and tweak the stop vector |
| 927 | */ |
| 928 | if (start == stop) { |
| 929 | SkScalar sw = SkScalarAbs(sweepAngle); |
| 930 | if (sw < SkIntToScalar(360) && sw > SkIntToScalar(359)) { |
| 931 | SkScalar stopRad = SkDegreesToRadians(startAngle + sweepAngle); |
| 932 | // make a guess at a tiny angle (in radians) to tweak by |
| 933 | SkScalar deltaRad = SkScalarCopySign(SK_Scalar1/512, sweepAngle); |
| 934 | // not sure how much will be enough, so we use a loop |
| 935 | do { |
| 936 | stopRad -= deltaRad; |
| 937 | stop.fY = SkScalarSinCos(stopRad, &stop.fX); |
| 938 | } while (start == stop); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | SkMatrix matrix; |
| 943 | |
| 944 | matrix.setScale(SkScalarHalf(oval.width()), SkScalarHalf(oval.height())); |
| 945 | matrix.postTranslate(oval.centerX(), oval.centerY()); |
| 946 | |
| 947 | return SkBuildQuadArc(start, stop, |
| 948 | sweepAngle > 0 ? kCW_SkRotationDirection : |
| 949 | kCCW_SkRotationDirection, |
| 950 | &matrix, pts); |
| 951 | } |
| 952 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 953 | static void add_corner_arc(SkPath* path, const SkRect& rect, |
| 954 | SkScalar rx, SkScalar ry, int startAngle, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 955 | SkPath::Direction dir, bool forceMoveTo) { |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 956 | // These two asserts are not sufficient, since really we want to know |
| 957 | // that the pair of radii (e.g. left and right, or top and bottom) sum |
| 958 | // to <= dimension, but we don't have that data here, so we just have |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 959 | // these conservative asserts. |
| 960 | SkASSERT(0 <= rx && rx <= rect.width()); |
| 961 | SkASSERT(0 <= ry && ry <= rect.height()); |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 962 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 963 | SkRect r; |
| 964 | r.set(-rx, -ry, rx, ry); |
| 965 | |
| 966 | switch (startAngle) { |
| 967 | case 0: |
| 968 | r.offset(rect.fRight - r.fRight, rect.fBottom - r.fBottom); |
| 969 | break; |
| 970 | case 90: |
| 971 | r.offset(rect.fLeft - r.fLeft, rect.fBottom - r.fBottom); |
| 972 | break; |
| 973 | case 180: r.offset(rect.fLeft - r.fLeft, rect.fTop - r.fTop); break; |
| 974 | case 270: r.offset(rect.fRight - r.fRight, rect.fTop - r.fTop); break; |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 975 | default: SkDEBUGFAIL("unexpected startAngle in add_corner_arc"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 976 | } |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 977 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 978 | SkScalar start = SkIntToScalar(startAngle); |
| 979 | SkScalar sweep = SkIntToScalar(90); |
| 980 | if (SkPath::kCCW_Direction == dir) { |
| 981 | start += sweep; |
| 982 | sweep = -sweep; |
| 983 | } |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 984 | |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 985 | path->arcTo(r, start, sweep, forceMoveTo); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 986 | } |
| 987 | |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 988 | void SkPath::addRoundRect(const SkRect& rect, const SkScalar radii[], |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 989 | Direction dir) { |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 990 | SkRRect rrect; |
| 991 | rrect.setRectRadii(rect, (const SkVector*) radii); |
| 992 | this->addRRect(rrect, dir); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 993 | } |
| 994 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 995 | void SkPath::addRRect(const SkRRect& rrect, Direction dir) { |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 996 | assert_known_direction(dir); |
| 997 | |
| 998 | if (rrect.isEmpty()) { |
| 999 | return; |
| 1000 | } |
| 1001 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1002 | const SkRect& bounds = rrect.getBounds(); |
| 1003 | |
| 1004 | if (rrect.isRect()) { |
| 1005 | this->addRect(bounds, dir); |
| 1006 | } else if (rrect.isOval()) { |
| 1007 | this->addOval(bounds, dir); |
| 1008 | } else if (rrect.isSimple()) { |
| 1009 | const SkVector& rad = rrect.getSimpleRadii(); |
| 1010 | this->addRoundRect(bounds, rad.x(), rad.y(), dir); |
| 1011 | } else { |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 1012 | SkAutoPathBoundsUpdate apbu(this, bounds); |
| 1013 | |
| 1014 | if (kCW_Direction == dir) { |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1015 | add_corner_arc(this, bounds, rrect.fRadii[0].fX, rrect.fRadii[0].fY, 180, dir, true); |
| 1016 | add_corner_arc(this, bounds, rrect.fRadii[1].fX, rrect.fRadii[1].fY, 270, dir, false); |
| 1017 | add_corner_arc(this, bounds, rrect.fRadii[2].fX, rrect.fRadii[2].fY, 0, dir, false); |
| 1018 | add_corner_arc(this, bounds, rrect.fRadii[3].fX, rrect.fRadii[3].fY, 90, dir, false); |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 1019 | } else { |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1020 | add_corner_arc(this, bounds, rrect.fRadii[0].fX, rrect.fRadii[0].fY, 180, dir, true); |
| 1021 | add_corner_arc(this, bounds, rrect.fRadii[3].fX, rrect.fRadii[3].fY, 90, dir, false); |
| 1022 | add_corner_arc(this, bounds, rrect.fRadii[2].fX, rrect.fRadii[2].fY, 0, dir, false); |
| 1023 | add_corner_arc(this, bounds, rrect.fRadii[1].fX, rrect.fRadii[1].fY, 270, dir, false); |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 1024 | } |
| 1025 | this->close(); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1029 | bool SkPath::hasOnlyMoveTos() const { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1030 | int count = fPathRef->countVerbs(); |
| 1031 | const uint8_t* verbs = const_cast<const SkPathRef*>(fPathRef.get())->verbsMemBegin(); |
| 1032 | for (int i = 0; i < count; ++i) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1033 | if (*verbs == kLine_Verb || |
| 1034 | *verbs == kQuad_Verb || |
| 1035 | *verbs == kCubic_Verb) { |
| 1036 | return false; |
| 1037 | } |
| 1038 | ++verbs; |
| 1039 | } |
| 1040 | return true; |
| 1041 | } |
| 1042 | |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1043 | #ifdef SK_IGNORE_QUAD_RR_CORNERS_OPT |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1044 | #define CUBIC_ARC_FACTOR ((SK_ScalarSqrt2 - SK_Scalar1) * 4 / 3) |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1045 | #endif |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1046 | |
| 1047 | void SkPath::addRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, |
| 1048 | Direction dir) { |
| 1049 | assert_known_direction(dir); |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 1050 | |
humper@google.com | 75e3ca1 | 2013-04-08 21:44:11 +0000 | [diff] [blame] | 1051 | if (rx < 0 || ry < 0) { |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 1052 | SkErrorInternals::SetError( kInvalidArgument_SkError, |
humper@google.com | 75e3ca1 | 2013-04-08 21:44:11 +0000 | [diff] [blame] | 1053 | "I got %f and %f as radii to SkPath::AddRoundRect, " |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 1054 | "but negative radii are not allowed.", |
humper@google.com | 75e3ca1 | 2013-04-08 21:44:11 +0000 | [diff] [blame] | 1055 | SkScalarToDouble(rx), SkScalarToDouble(ry) ); |
| 1056 | return; |
| 1057 | } |
skia.committer@gmail.com | d9f65e3 | 2013-01-04 12:07:46 +0000 | [diff] [blame] | 1058 | |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1059 | SkScalar w = rect.width(); |
| 1060 | SkScalar halfW = SkScalarHalf(w); |
| 1061 | SkScalar h = rect.height(); |
| 1062 | SkScalar halfH = SkScalarHalf(h); |
skia.committer@gmail.com | d9f65e3 | 2013-01-04 12:07:46 +0000 | [diff] [blame] | 1063 | |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1064 | if (halfW <= 0 || halfH <= 0) { |
| 1065 | return; |
| 1066 | } |
skia.committer@gmail.com | d9f65e3 | 2013-01-04 12:07:46 +0000 | [diff] [blame] | 1067 | |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1068 | bool skip_hori = rx >= halfW; |
| 1069 | bool skip_vert = ry >= halfH; |
skia.committer@gmail.com | d9f65e3 | 2013-01-04 12:07:46 +0000 | [diff] [blame] | 1070 | |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1071 | if (skip_hori && skip_vert) { |
| 1072 | this->addOval(rect, dir); |
| 1073 | return; |
| 1074 | } |
skia.committer@gmail.com | d9f65e3 | 2013-01-04 12:07:46 +0000 | [diff] [blame] | 1075 | |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1076 | fDirection = this->hasOnlyMoveTos() ? dir : kUnknown_Direction; |
skia.committer@gmail.com | d9f65e3 | 2013-01-04 12:07:46 +0000 | [diff] [blame] | 1077 | |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1078 | SkAutoPathBoundsUpdate apbu(this, rect); |
| 1079 | SkAutoDisableDirectionCheck(this); |
skia.committer@gmail.com | d9f65e3 | 2013-01-04 12:07:46 +0000 | [diff] [blame] | 1080 | |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1081 | if (skip_hori) { |
| 1082 | rx = halfW; |
| 1083 | } else if (skip_vert) { |
| 1084 | ry = halfH; |
| 1085 | } |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1086 | #ifdef SK_IGNORE_QUAD_RR_CORNERS_OPT |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1087 | SkScalar sx = SkScalarMul(rx, CUBIC_ARC_FACTOR); |
| 1088 | SkScalar sy = SkScalarMul(ry, CUBIC_ARC_FACTOR); |
skia.committer@gmail.com | d9f65e3 | 2013-01-04 12:07:46 +0000 | [diff] [blame] | 1089 | |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1090 | this->incReserve(17); |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1091 | #else |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1092 | // The mid point of the quadratic arc approximation is half way between the two |
| 1093 | // control points. The float epsilon adjustment moves the on curve point out by |
| 1094 | // two bits, distributing the convex test error between the round rect approximation |
| 1095 | // and the convex cross product sign equality test. |
| 1096 | SkScalar midPtX = rx * (SK_Scalar1 + SK_ScalarTanPIOver8 + FLT_EPSILON * 4) / 2; |
| 1097 | SkScalar midPtY = ry * (SK_Scalar1 + SK_ScalarTanPIOver8 + FLT_EPSILON * 4) / 2; |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1098 | |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1099 | SkScalar offPtX = rx * SK_ScalarTanPIOver8; |
| 1100 | SkScalar offPtY = ry * SK_ScalarTanPIOver8; |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1101 | |
| 1102 | this->incReserve(21); |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1103 | #endif |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1104 | this->moveTo(rect.fRight - rx, rect.fTop); // top-right |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1105 | if (dir == kCCW_Direction) { |
| 1106 | if (!skip_hori) { |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1107 | this->lineTo(rect.fLeft + rx, rect.fTop); // top |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1108 | } |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1109 | #ifdef SK_IGNORE_QUAD_RR_CORNERS_OPT |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1110 | this->cubicTo(rect.fLeft + rx - sx, rect.fTop, |
| 1111 | rect.fLeft, rect.fTop + ry - sy, |
| 1112 | rect.fLeft, rect.fTop + ry); // top-left |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1113 | #else |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1114 | this->quadTo(rect.fLeft + rx - offPtX, rect.fTop, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1115 | rect.fLeft + rx - midPtX, rect.fTop + ry - midPtY); |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1116 | this->quadTo(rect.fLeft, rect.fTop + ry - offPtY, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1117 | rect.fLeft, rect.fTop + ry); |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1118 | #endif |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1119 | if (!skip_vert) { |
| 1120 | this->lineTo(rect.fLeft, rect.fBottom - ry); // left |
| 1121 | } |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1122 | #ifdef SK_IGNORE_QUAD_RR_CORNERS_OPT |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1123 | this->cubicTo(rect.fLeft, rect.fBottom - ry + sy, |
| 1124 | rect.fLeft + rx - sx, rect.fBottom, |
| 1125 | rect.fLeft + rx, rect.fBottom); // bot-left |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1126 | #else |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1127 | this->quadTo(rect.fLeft, rect.fBottom - ry + offPtY, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1128 | rect.fLeft + rx - midPtX, rect.fBottom - ry + midPtY); |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1129 | this->quadTo(rect.fLeft + rx - offPtX, rect.fBottom, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1130 | rect.fLeft + rx, rect.fBottom); |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1131 | #endif |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1132 | if (!skip_hori) { |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1133 | this->lineTo(rect.fRight - rx, rect.fBottom); // bottom |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1134 | } |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1135 | #ifdef SK_IGNORE_QUAD_RR_CORNERS_OPT |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1136 | this->cubicTo(rect.fRight - rx + sx, rect.fBottom, |
| 1137 | rect.fRight, rect.fBottom - ry + sy, |
| 1138 | rect.fRight, rect.fBottom - ry); // bot-right |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1139 | #else |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1140 | this->quadTo(rect.fRight - rx + offPtX, rect.fBottom, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1141 | rect.fRight - rx + midPtX, rect.fBottom - ry + midPtY); |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1142 | this->quadTo(rect.fRight, rect.fBottom - ry + offPtY, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1143 | rect.fRight, rect.fBottom - ry); |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1144 | #endif |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1145 | if (!skip_vert) { |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1146 | this->lineTo(rect.fRight, rect.fTop + ry); // right |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1147 | } |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1148 | #ifdef SK_IGNORE_QUAD_RR_CORNERS_OPT |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1149 | this->cubicTo(rect.fRight, rect.fTop + ry - sy, |
| 1150 | rect.fRight - rx + sx, rect.fTop, |
| 1151 | rect.fRight - rx, rect.fTop); // top-right |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1152 | #else |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1153 | this->quadTo(rect.fRight, rect.fTop + ry - offPtY, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1154 | rect.fRight - rx + midPtX, rect.fTop + ry - midPtY); |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1155 | this->quadTo(rect.fRight - rx + offPtX, rect.fTop, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1156 | rect.fRight - rx, rect.fTop); |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1157 | #endif |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1158 | } else { |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1159 | #ifdef SK_IGNORE_QUAD_RR_CORNERS_OPT |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1160 | this->cubicTo(rect.fRight - rx + sx, rect.fTop, |
| 1161 | rect.fRight, rect.fTop + ry - sy, |
| 1162 | rect.fRight, rect.fTop + ry); // top-right |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1163 | #else |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1164 | this->quadTo(rect.fRight - rx + offPtX, rect.fTop, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1165 | rect.fRight - rx + midPtX, rect.fTop + ry - midPtY); |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1166 | this->quadTo(rect.fRight, rect.fTop + ry - offPtY, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1167 | rect.fRight, rect.fTop + ry); |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1168 | #endif |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1169 | if (!skip_vert) { |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1170 | this->lineTo(rect.fRight, rect.fBottom - ry); // right |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1171 | } |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1172 | #ifdef SK_IGNORE_QUAD_RR_CORNERS_OPT |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1173 | this->cubicTo(rect.fRight, rect.fBottom - ry + sy, |
| 1174 | rect.fRight - rx + sx, rect.fBottom, |
| 1175 | rect.fRight - rx, rect.fBottom); // bot-right |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1176 | #else |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1177 | this->quadTo(rect.fRight, rect.fBottom - ry + offPtY, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1178 | rect.fRight - rx + midPtX, rect.fBottom - ry + midPtY); |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1179 | this->quadTo(rect.fRight - rx + offPtX, rect.fBottom, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1180 | rect.fRight - rx, rect.fBottom); |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1181 | #endif |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1182 | if (!skip_hori) { |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1183 | this->lineTo(rect.fLeft + rx, rect.fBottom); // bottom |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1184 | } |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1185 | #ifdef SK_IGNORE_QUAD_RR_CORNERS_OPT |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1186 | this->cubicTo(rect.fLeft + rx - sx, rect.fBottom, |
| 1187 | rect.fLeft, rect.fBottom - ry + sy, |
| 1188 | rect.fLeft, rect.fBottom - ry); // bot-left |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1189 | #else |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1190 | this->quadTo(rect.fLeft + rx - offPtX, rect.fBottom, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1191 | rect.fLeft + rx - midPtX, rect.fBottom - ry + midPtY); |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1192 | this->quadTo(rect.fLeft, rect.fBottom - ry + offPtY, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1193 | rect.fLeft, rect.fBottom - ry); |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1194 | #endif |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1195 | if (!skip_vert) { |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1196 | this->lineTo(rect.fLeft, rect.fTop + ry); // left |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1197 | } |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1198 | #ifdef SK_IGNORE_QUAD_RR_CORNERS_OPT |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1199 | this->cubicTo(rect.fLeft, rect.fTop + ry - sy, |
| 1200 | rect.fLeft + rx - sx, rect.fTop, |
| 1201 | rect.fLeft + rx, rect.fTop); // top-left |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1202 | #else |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1203 | this->quadTo(rect.fLeft, rect.fTop + ry - offPtY, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1204 | rect.fLeft + rx - midPtX, rect.fTop + ry - midPtY); |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 1205 | this->quadTo(rect.fLeft + rx - offPtX, rect.fTop, |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1206 | rect.fLeft + rx, rect.fTop); |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1207 | #endif |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1208 | if (!skip_hori) { |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1209 | this->lineTo(rect.fRight - rx, rect.fTop); // top |
mike@reedtribe.org | b16033a | 2013-01-04 03:16:52 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | this->close(); |
| 1213 | } |
| 1214 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1215 | void SkPath::addOval(const SkRect& oval, Direction dir) { |
reed@google.com | a8a3b3d | 2012-11-26 18:16:27 +0000 | [diff] [blame] | 1216 | assert_known_direction(dir); |
| 1217 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1218 | /* If addOval() is called after previous moveTo(), |
| 1219 | this path is still marked as an oval. This is used to |
| 1220 | fit into WebKit's calling sequences. |
| 1221 | We can't simply check isEmpty() in this case, as additional |
| 1222 | moveTo() would mark the path non empty. |
| 1223 | */ |
| 1224 | fIsOval = hasOnlyMoveTos(); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1225 | if (fIsOval) { |
| 1226 | fDirection = dir; |
| 1227 | } else { |
| 1228 | fDirection = kUnknown_Direction; |
| 1229 | } |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1230 | |
| 1231 | SkAutoDisableOvalCheck adoc(this); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1232 | SkAutoDisableDirectionCheck addc(this); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1233 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1234 | SkAutoPathBoundsUpdate apbu(this, oval); |
| 1235 | |
| 1236 | SkScalar cx = oval.centerX(); |
| 1237 | SkScalar cy = oval.centerY(); |
| 1238 | SkScalar rx = SkScalarHalf(oval.width()); |
| 1239 | SkScalar ry = SkScalarHalf(oval.height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1240 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1241 | SkScalar sx = SkScalarMul(rx, SK_ScalarTanPIOver8); |
| 1242 | SkScalar sy = SkScalarMul(ry, SK_ScalarTanPIOver8); |
| 1243 | SkScalar mx = SkScalarMul(rx, SK_ScalarRoot2Over2); |
| 1244 | SkScalar my = SkScalarMul(ry, SK_ScalarRoot2Over2); |
| 1245 | |
| 1246 | /* |
| 1247 | To handle imprecision in computing the center and radii, we revert to |
| 1248 | the provided bounds when we can (i.e. use oval.fLeft instead of cx-rx) |
| 1249 | to ensure that we don't exceed the oval's bounds *ever*, since we want |
| 1250 | to use oval for our fast-bounds, rather than have to recompute it. |
| 1251 | */ |
| 1252 | const SkScalar L = oval.fLeft; // cx - rx |
| 1253 | const SkScalar T = oval.fTop; // cy - ry |
| 1254 | const SkScalar R = oval.fRight; // cx + rx |
| 1255 | const SkScalar B = oval.fBottom; // cy + ry |
| 1256 | |
| 1257 | this->incReserve(17); // 8 quads + close |
| 1258 | this->moveTo(R, cy); |
| 1259 | if (dir == kCCW_Direction) { |
| 1260 | this->quadTo( R, cy - sy, cx + mx, cy - my); |
| 1261 | this->quadTo(cx + sx, T, cx , T); |
| 1262 | this->quadTo(cx - sx, T, cx - mx, cy - my); |
| 1263 | this->quadTo( L, cy - sy, L, cy ); |
| 1264 | this->quadTo( L, cy + sy, cx - mx, cy + my); |
| 1265 | this->quadTo(cx - sx, B, cx , B); |
| 1266 | this->quadTo(cx + sx, B, cx + mx, cy + my); |
| 1267 | this->quadTo( R, cy + sy, R, cy ); |
| 1268 | } else { |
| 1269 | this->quadTo( R, cy + sy, cx + mx, cy + my); |
| 1270 | this->quadTo(cx + sx, B, cx , B); |
| 1271 | this->quadTo(cx - sx, B, cx - mx, cy + my); |
| 1272 | this->quadTo( L, cy + sy, L, cy ); |
| 1273 | this->quadTo( L, cy - sy, cx - mx, cy - my); |
| 1274 | this->quadTo(cx - sx, T, cx , T); |
| 1275 | this->quadTo(cx + sx, T, cx + mx, cy - my); |
| 1276 | this->quadTo( R, cy - sy, R, cy ); |
| 1277 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1278 | this->close(); |
| 1279 | } |
| 1280 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1281 | bool SkPath::isOval(SkRect* rect) const { |
| 1282 | if (fIsOval && rect) { |
| 1283 | *rect = getBounds(); |
| 1284 | } |
| 1285 | |
| 1286 | return fIsOval; |
| 1287 | } |
| 1288 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1289 | void SkPath::addCircle(SkScalar x, SkScalar y, SkScalar r, Direction dir) { |
| 1290 | if (r > 0) { |
| 1291 | SkRect rect; |
| 1292 | rect.set(x - r, y - r, x + r, y + r); |
| 1293 | this->addOval(rect, dir); |
| 1294 | } |
| 1295 | } |
| 1296 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1297 | void SkPath::arcTo(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, |
| 1298 | bool forceMoveTo) { |
| 1299 | if (oval.width() < 0 || oval.height() < 0) { |
| 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | SkPoint pts[kSkBuildQuadArcStorage]; |
| 1304 | int count = build_arc_points(oval, startAngle, sweepAngle, pts); |
| 1305 | SkASSERT((count & 1) == 1); |
| 1306 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1307 | if (fPathRef->countVerbs() == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1308 | forceMoveTo = true; |
| 1309 | } |
| 1310 | this->incReserve(count); |
| 1311 | forceMoveTo ? this->moveTo(pts[0]) : this->lineTo(pts[0]); |
| 1312 | for (int i = 1; i < count; i += 2) { |
| 1313 | this->quadTo(pts[i], pts[i+1]); |
| 1314 | } |
| 1315 | } |
| 1316 | |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 1317 | void SkPath::addArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1318 | if (oval.isEmpty() || 0 == sweepAngle) { |
| 1319 | return; |
| 1320 | } |
| 1321 | |
| 1322 | const SkScalar kFullCircleAngle = SkIntToScalar(360); |
| 1323 | |
| 1324 | if (sweepAngle >= kFullCircleAngle || sweepAngle <= -kFullCircleAngle) { |
| 1325 | this->addOval(oval, sweepAngle > 0 ? kCW_Direction : kCCW_Direction); |
| 1326 | return; |
| 1327 | } |
| 1328 | |
| 1329 | SkPoint pts[kSkBuildQuadArcStorage]; |
| 1330 | int count = build_arc_points(oval, startAngle, sweepAngle, pts); |
| 1331 | |
| 1332 | this->incReserve(count); |
| 1333 | this->moveTo(pts[0]); |
| 1334 | for (int i = 1; i < count; i += 2) { |
| 1335 | this->quadTo(pts[i], pts[i+1]); |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | /* |
| 1340 | Need to handle the case when the angle is sharp, and our computed end-points |
| 1341 | for the arc go behind pt1 and/or p2... |
| 1342 | */ |
| 1343 | void SkPath::arcTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 1344 | SkScalar radius) { |
| 1345 | SkVector before, after; |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1346 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1347 | // need to know our prev pt so we can construct tangent vectors |
| 1348 | { |
| 1349 | SkPoint start; |
| 1350 | this->getLastPt(&start); |
senorblanco@chromium.org | 60eaa39 | 2010-10-13 18:47:00 +0000 | [diff] [blame] | 1351 | // Handle degenerate cases by adding a line to the first point and |
| 1352 | // bailing out. |
| 1353 | if ((x1 == start.fX && y1 == start.fY) || |
| 1354 | (x1 == x2 && y1 == y2) || |
| 1355 | radius == 0) { |
| 1356 | this->lineTo(x1, y1); |
| 1357 | return; |
| 1358 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1359 | before.setNormalize(x1 - start.fX, y1 - start.fY); |
| 1360 | after.setNormalize(x2 - x1, y2 - y1); |
| 1361 | } |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1362 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1363 | SkScalar cosh = SkPoint::DotProduct(before, after); |
| 1364 | SkScalar sinh = SkPoint::CrossProduct(before, after); |
| 1365 | |
| 1366 | if (SkScalarNearlyZero(sinh)) { // angle is too tight |
senorblanco@chromium.org | 60eaa39 | 2010-10-13 18:47:00 +0000 | [diff] [blame] | 1367 | this->lineTo(x1, y1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1368 | return; |
| 1369 | } |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1370 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1371 | SkScalar dist = SkScalarMulDiv(radius, SK_Scalar1 - cosh, sinh); |
| 1372 | if (dist < 0) { |
| 1373 | dist = -dist; |
| 1374 | } |
| 1375 | |
| 1376 | SkScalar xx = x1 - SkScalarMul(dist, before.fX); |
| 1377 | SkScalar yy = y1 - SkScalarMul(dist, before.fY); |
| 1378 | SkRotationDirection arcDir; |
| 1379 | |
| 1380 | // now turn before/after into normals |
| 1381 | if (sinh > 0) { |
| 1382 | before.rotateCCW(); |
| 1383 | after.rotateCCW(); |
| 1384 | arcDir = kCW_SkRotationDirection; |
| 1385 | } else { |
| 1386 | before.rotateCW(); |
| 1387 | after.rotateCW(); |
| 1388 | arcDir = kCCW_SkRotationDirection; |
| 1389 | } |
| 1390 | |
| 1391 | SkMatrix matrix; |
| 1392 | SkPoint pts[kSkBuildQuadArcStorage]; |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1393 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1394 | matrix.setScale(radius, radius); |
| 1395 | matrix.postTranslate(xx - SkScalarMul(radius, before.fX), |
| 1396 | yy - SkScalarMul(radius, before.fY)); |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1397 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1398 | int count = SkBuildQuadArc(before, after, arcDir, &matrix, pts); |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1399 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1400 | this->incReserve(count); |
| 1401 | // [xx,yy] == pts[0] |
| 1402 | this->lineTo(xx, yy); |
| 1403 | for (int i = 1; i < count; i += 2) { |
| 1404 | this->quadTo(pts[i], pts[i+1]); |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | /////////////////////////////////////////////////////////////////////////////// |
| 1409 | |
| 1410 | void SkPath::addPath(const SkPath& path, SkScalar dx, SkScalar dy) { |
| 1411 | SkMatrix matrix; |
| 1412 | |
| 1413 | matrix.setTranslate(dx, dy); |
| 1414 | this->addPath(path, matrix); |
| 1415 | } |
| 1416 | |
| 1417 | void SkPath::addPath(const SkPath& path, const SkMatrix& matrix) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1418 | SkPathRef::Editor(&fPathRef, path.countVerbs(), path.countPoints()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1419 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1420 | fIsOval = false; |
| 1421 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1422 | RawIter iter(path); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1423 | SkPoint pts[4]; |
| 1424 | Verb verb; |
| 1425 | |
| 1426 | SkMatrix::MapPtsProc proc = matrix.getMapPtsProc(); |
| 1427 | |
| 1428 | while ((verb = iter.next(pts)) != kDone_Verb) { |
| 1429 | switch (verb) { |
| 1430 | case kMove_Verb: |
| 1431 | proc(matrix, &pts[0], &pts[0], 1); |
| 1432 | this->moveTo(pts[0]); |
| 1433 | break; |
| 1434 | case kLine_Verb: |
| 1435 | proc(matrix, &pts[1], &pts[1], 1); |
| 1436 | this->lineTo(pts[1]); |
| 1437 | break; |
| 1438 | case kQuad_Verb: |
| 1439 | proc(matrix, &pts[1], &pts[1], 2); |
| 1440 | this->quadTo(pts[1], pts[2]); |
| 1441 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1442 | case kConic_Verb: |
| 1443 | proc(matrix, &pts[1], &pts[1], 2); |
| 1444 | this->conicTo(pts[1], pts[2], iter.conicWeight()); |
| 1445 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1446 | case kCubic_Verb: |
| 1447 | proc(matrix, &pts[1], &pts[1], 3); |
| 1448 | this->cubicTo(pts[1], pts[2], pts[3]); |
| 1449 | break; |
| 1450 | case kClose_Verb: |
| 1451 | this->close(); |
| 1452 | break; |
| 1453 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1454 | SkDEBUGFAIL("unknown verb"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | /////////////////////////////////////////////////////////////////////////////// |
| 1460 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1461 | static int pts_in_verb(unsigned verb) { |
| 1462 | static const uint8_t gPtsInVerb[] = { |
| 1463 | 1, // kMove |
| 1464 | 1, // kLine |
| 1465 | 2, // kQuad |
| 1466 | 2, // kConic |
| 1467 | 3, // kCubic |
| 1468 | 0, // kClose |
| 1469 | 0 // kDone |
| 1470 | }; |
| 1471 | |
| 1472 | SkASSERT(verb < SK_ARRAY_COUNT(gPtsInVerb)); |
| 1473 | return gPtsInVerb[verb]; |
| 1474 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1475 | |
| 1476 | // ignore the initial moveto, and stop when the 1st contour ends |
| 1477 | void SkPath::pathTo(const SkPath& path) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1478 | int i, vcount = path.fPathRef->countVerbs(); |
| 1479 | // exit early if the path is empty, or just has a moveTo. |
| 1480 | if (vcount < 2) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1481 | return; |
| 1482 | } |
| 1483 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1484 | SkPathRef::Editor(&fPathRef, vcount, path.countPoints()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1485 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1486 | fIsOval = false; |
| 1487 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1488 | const uint8_t* verbs = path.fPathRef->verbs(); |
| 1489 | // skip the initial moveTo |
| 1490 | const SkPoint* pts = path.fPathRef->points() + 1; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1491 | const SkScalar* conicWeight = path.fPathRef->conicWeights(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1492 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1493 | SkASSERT(verbs[~0] == kMove_Verb); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1494 | for (i = 1; i < vcount; i++) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1495 | switch (verbs[~i]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1496 | case kLine_Verb: |
| 1497 | this->lineTo(pts[0].fX, pts[0].fY); |
| 1498 | break; |
| 1499 | case kQuad_Verb: |
| 1500 | this->quadTo(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY); |
| 1501 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1502 | case kConic_Verb: |
| 1503 | this->conicTo(pts[0], pts[1], *conicWeight++); |
| 1504 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1505 | case kCubic_Verb: |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1506 | this->cubicTo(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1507 | break; |
| 1508 | case kClose_Verb: |
| 1509 | return; |
| 1510 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1511 | pts += pts_in_verb(verbs[~i]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | // ignore the last point of the 1st contour |
| 1516 | void SkPath::reversePathTo(const SkPath& path) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1517 | int i, vcount = path.fPathRef->countVerbs(); |
| 1518 | // exit early if the path is empty, or just has a moveTo. |
| 1519 | if (vcount < 2) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1520 | return; |
| 1521 | } |
| 1522 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1523 | SkPathRef::Editor(&fPathRef, vcount, path.countPoints()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1524 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1525 | fIsOval = false; |
| 1526 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1527 | const uint8_t* verbs = path.fPathRef->verbs(); |
| 1528 | const SkPoint* pts = path.fPathRef->points(); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1529 | const SkScalar* conicWeights = path.fPathRef->conicWeights(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1530 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1531 | SkASSERT(verbs[~0] == kMove_Verb); |
| 1532 | for (i = 1; i < vcount; ++i) { |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1533 | unsigned v = verbs[~i]; |
| 1534 | int n = pts_in_verb(v); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1535 | if (n == 0) { |
| 1536 | break; |
| 1537 | } |
| 1538 | pts += n; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1539 | conicWeights += (SkPath::kConic_Verb == v); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | while (--i > 0) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1543 | switch (verbs[~i]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1544 | case kLine_Verb: |
| 1545 | this->lineTo(pts[-1].fX, pts[-1].fY); |
| 1546 | break; |
| 1547 | case kQuad_Verb: |
| 1548 | this->quadTo(pts[-1].fX, pts[-1].fY, pts[-2].fX, pts[-2].fY); |
| 1549 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1550 | case kConic_Verb: |
| 1551 | this->conicTo(pts[-1], pts[-2], *--conicWeights); |
| 1552 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1553 | case kCubic_Verb: |
| 1554 | this->cubicTo(pts[-1].fX, pts[-1].fY, pts[-2].fX, pts[-2].fY, |
| 1555 | pts[-3].fX, pts[-3].fY); |
| 1556 | break; |
| 1557 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1558 | SkDEBUGFAIL("bad verb"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1559 | break; |
| 1560 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1561 | pts -= pts_in_verb(verbs[~i]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1565 | void SkPath::reverseAddPath(const SkPath& src) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1566 | SkPathRef::Editor ed(&fPathRef, src.fPathRef->countPoints(), src.fPathRef->countVerbs()); |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1567 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1568 | const SkPoint* pts = src.fPathRef->pointsEnd(); |
| 1569 | // we will iterator through src's verbs backwards |
| 1570 | const uint8_t* verbs = src.fPathRef->verbsMemBegin(); // points at the last verb |
| 1571 | 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] | 1572 | const SkScalar* conicWeights = src.fPathRef->conicWeightsEnd(); |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1573 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1574 | fIsOval = false; |
| 1575 | |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1576 | bool needMove = true; |
| 1577 | bool needClose = false; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1578 | while (verbs < verbsEnd) { |
| 1579 | uint8_t v = *(verbs++); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1580 | int n = pts_in_verb(v); |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1581 | |
| 1582 | if (needMove) { |
| 1583 | --pts; |
| 1584 | this->moveTo(pts->fX, pts->fY); |
| 1585 | needMove = false; |
| 1586 | } |
| 1587 | pts -= n; |
| 1588 | switch (v) { |
| 1589 | case kMove_Verb: |
| 1590 | if (needClose) { |
| 1591 | this->close(); |
| 1592 | needClose = false; |
| 1593 | } |
| 1594 | needMove = true; |
| 1595 | pts += 1; // so we see the point in "if (needMove)" above |
| 1596 | break; |
| 1597 | case kLine_Verb: |
| 1598 | this->lineTo(pts[0]); |
| 1599 | break; |
| 1600 | case kQuad_Verb: |
| 1601 | this->quadTo(pts[1], pts[0]); |
| 1602 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1603 | case kConic_Verb: |
| 1604 | this->conicTo(pts[1], pts[0], *--conicWeights); |
| 1605 | break; |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1606 | case kCubic_Verb: |
| 1607 | this->cubicTo(pts[2], pts[1], pts[0]); |
| 1608 | break; |
| 1609 | case kClose_Verb: |
| 1610 | needClose = true; |
| 1611 | break; |
| 1612 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1613 | SkDEBUGFAIL("unexpected verb"); |
reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1618 | /////////////////////////////////////////////////////////////////////////////// |
| 1619 | |
| 1620 | void SkPath::offset(SkScalar dx, SkScalar dy, SkPath* dst) const { |
| 1621 | SkMatrix matrix; |
| 1622 | |
| 1623 | matrix.setTranslate(dx, dy); |
| 1624 | this->transform(matrix, dst); |
| 1625 | } |
| 1626 | |
| 1627 | #include "SkGeometry.h" |
| 1628 | |
| 1629 | static void subdivide_quad_to(SkPath* path, const SkPoint pts[3], |
| 1630 | int level = 2) { |
| 1631 | if (--level >= 0) { |
| 1632 | SkPoint tmp[5]; |
| 1633 | |
| 1634 | SkChopQuadAtHalf(pts, tmp); |
| 1635 | subdivide_quad_to(path, &tmp[0], level); |
| 1636 | subdivide_quad_to(path, &tmp[2], level); |
| 1637 | } else { |
| 1638 | path->quadTo(pts[1], pts[2]); |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | static void subdivide_cubic_to(SkPath* path, const SkPoint pts[4], |
| 1643 | int level = 2) { |
| 1644 | if (--level >= 0) { |
| 1645 | SkPoint tmp[7]; |
| 1646 | |
| 1647 | SkChopCubicAtHalf(pts, tmp); |
| 1648 | subdivide_cubic_to(path, &tmp[0], level); |
| 1649 | subdivide_cubic_to(path, &tmp[3], level); |
| 1650 | } else { |
| 1651 | path->cubicTo(pts[1], pts[2], pts[3]); |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | void SkPath::transform(const SkMatrix& matrix, SkPath* dst) const { |
| 1656 | SkDEBUGCODE(this->validate();) |
| 1657 | if (dst == NULL) { |
| 1658 | dst = (SkPath*)this; |
| 1659 | } |
| 1660 | |
tomhudson@google.com | 8d43018 | 2011-06-06 19:11:19 +0000 | [diff] [blame] | 1661 | if (matrix.hasPerspective()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1662 | SkPath tmp; |
| 1663 | tmp.fFillType = fFillType; |
| 1664 | |
| 1665 | SkPath::Iter iter(*this, false); |
| 1666 | SkPoint pts[4]; |
| 1667 | SkPath::Verb verb; |
| 1668 | |
reed@google.com | 4a3b714 | 2012-05-16 17:16:46 +0000 | [diff] [blame] | 1669 | while ((verb = iter.next(pts, false)) != kDone_Verb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1670 | switch (verb) { |
| 1671 | case kMove_Verb: |
| 1672 | tmp.moveTo(pts[0]); |
| 1673 | break; |
| 1674 | case kLine_Verb: |
| 1675 | tmp.lineTo(pts[1]); |
| 1676 | break; |
| 1677 | case kQuad_Verb: |
| 1678 | subdivide_quad_to(&tmp, pts); |
| 1679 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1680 | case kConic_Verb: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1681 | SkDEBUGFAIL("TODO: compute new weight"); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1682 | tmp.conicTo(pts[1], pts[2], iter.conicWeight()); |
| 1683 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1684 | case kCubic_Verb: |
| 1685 | subdivide_cubic_to(&tmp, pts); |
| 1686 | break; |
| 1687 | case kClose_Verb: |
| 1688 | tmp.close(); |
| 1689 | break; |
| 1690 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1691 | SkDEBUGFAIL("unknown verb"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1692 | break; |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | dst->swap(tmp); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1697 | SkPathRef::Editor ed(&dst->fPathRef); |
| 1698 | matrix.mapPoints(ed.points(), ed.pathRef()->countPoints()); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1699 | dst->fDirection = kUnknown_Direction; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1700 | } else { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1701 | SkPathRef::CreateTransformedCopy(&dst->fPathRef, *fPathRef.get(), matrix); |
| 1702 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1703 | if (this != dst) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1704 | dst->fFillType = fFillType; |
reed@google.com | b3b0a5b | 2011-09-21 12:58:51 +0000 | [diff] [blame] | 1705 | dst->fSegmentMask = fSegmentMask; |
reed@google.com | 2a6f8ab | 2011-10-25 18:41:23 +0000 | [diff] [blame] | 1706 | dst->fConvexity = fConvexity; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1707 | } |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1708 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1709 | if (kUnknown_Direction == fDirection) { |
| 1710 | dst->fDirection = kUnknown_Direction; |
| 1711 | } else { |
| 1712 | SkScalar det2x2 = |
| 1713 | SkScalarMul(matrix.get(SkMatrix::kMScaleX), matrix.get(SkMatrix::kMScaleY)) - |
| 1714 | SkScalarMul(matrix.get(SkMatrix::kMSkewX), matrix.get(SkMatrix::kMSkewY)); |
| 1715 | if (det2x2 < 0) { |
| 1716 | dst->fDirection = SkPath::OppositeDirection(static_cast<Direction>(fDirection)); |
| 1717 | } else if (det2x2 > 0) { |
| 1718 | dst->fDirection = fDirection; |
| 1719 | } else { |
| 1720 | dst->fDirection = kUnknown_Direction; |
| 1721 | } |
| 1722 | } |
| 1723 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1724 | // It's an oval only if it stays a rect. |
| 1725 | dst->fIsOval = fIsOval && matrix.rectStaysRect(); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1726 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1727 | SkDEBUGCODE(dst->validate();) |
| 1728 | } |
| 1729 | } |
| 1730 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1731 | /////////////////////////////////////////////////////////////////////////////// |
| 1732 | /////////////////////////////////////////////////////////////////////////////// |
| 1733 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1734 | enum SegmentState { |
schenney@chromium.org | fde6b41 | 2012-01-19 15:31:01 +0000 | [diff] [blame] | 1735 | kEmptyContour_SegmentState, // The current contour is empty. We may be |
| 1736 | // starting processing or we may have just |
| 1737 | // closed a contour. |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1738 | kAfterMove_SegmentState, // We have seen a move, but nothing else. |
| 1739 | kAfterPrimitive_SegmentState // We have seen a primitive but not yet |
| 1740 | // closed the path. Also the initial state. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1741 | }; |
| 1742 | |
| 1743 | SkPath::Iter::Iter() { |
| 1744 | #ifdef SK_DEBUG |
| 1745 | fPts = NULL; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1746 | fConicWeights = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1747 | fMoveTo.fX = fMoveTo.fY = fLastPt.fX = fLastPt.fY = 0; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1748 | fForceClose = fCloseLine = false; |
schenney@chromium.org | fde6b41 | 2012-01-19 15:31:01 +0000 | [diff] [blame] | 1749 | fSegmentState = kEmptyContour_SegmentState; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1750 | #endif |
| 1751 | // need to init enough to make next() harmlessly return kDone_Verb |
| 1752 | fVerbs = NULL; |
| 1753 | fVerbStop = NULL; |
| 1754 | fNeedClose = false; |
| 1755 | } |
| 1756 | |
| 1757 | SkPath::Iter::Iter(const SkPath& path, bool forceClose) { |
| 1758 | this->setPath(path, forceClose); |
| 1759 | } |
| 1760 | |
| 1761 | void SkPath::Iter::setPath(const SkPath& path, bool forceClose) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1762 | fPts = path.fPathRef->points(); |
| 1763 | fVerbs = path.fPathRef->verbs(); |
| 1764 | fVerbStop = path.fPathRef->verbsMemBegin(); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1765 | fConicWeights = path.fPathRef->conicWeights() - 1; // begin one behind |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1766 | fLastPt.fX = fLastPt.fY = 0; |
schenney@chromium.org | 72785c4 | 2011-12-29 21:03:28 +0000 | [diff] [blame] | 1767 | fMoveTo.fX = fMoveTo.fY = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1768 | fForceClose = SkToU8(forceClose); |
| 1769 | fNeedClose = false; |
schenney@chromium.org | fde6b41 | 2012-01-19 15:31:01 +0000 | [diff] [blame] | 1770 | fSegmentState = kEmptyContour_SegmentState; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | bool SkPath::Iter::isClosedContour() const { |
| 1774 | if (fVerbs == NULL || fVerbs == fVerbStop) { |
| 1775 | return false; |
| 1776 | } |
| 1777 | if (fForceClose) { |
| 1778 | return true; |
| 1779 | } |
| 1780 | |
| 1781 | const uint8_t* verbs = fVerbs; |
| 1782 | const uint8_t* stop = fVerbStop; |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 1783 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1784 | if (kMove_Verb == *(verbs - 1)) { |
| 1785 | verbs -= 1; // skip the initial moveto |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1788 | while (verbs > stop) { |
| 1789 | // verbs points one beyond the current verb, decrement first. |
| 1790 | unsigned v = *(--verbs); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1791 | if (kMove_Verb == v) { |
| 1792 | break; |
| 1793 | } |
| 1794 | if (kClose_Verb == v) { |
| 1795 | return true; |
| 1796 | } |
| 1797 | } |
| 1798 | return false; |
| 1799 | } |
| 1800 | |
| 1801 | SkPath::Verb SkPath::Iter::autoClose(SkPoint pts[2]) { |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1802 | SkASSERT(pts); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1803 | if (fLastPt != fMoveTo) { |
reed@android.com | 4ddfe35 | 2009-03-20 12:16:09 +0000 | [diff] [blame] | 1804 | // A special case: if both points are NaN, SkPoint::operation== returns |
| 1805 | // false, but the iterator expects that they are treated as the same. |
| 1806 | // (consider SkPoint is a 2-dimension float point). |
reed@android.com | 9da1ae3 | 2009-07-22 17:06:15 +0000 | [diff] [blame] | 1807 | if (SkScalarIsNaN(fLastPt.fX) || SkScalarIsNaN(fLastPt.fY) || |
| 1808 | SkScalarIsNaN(fMoveTo.fX) || SkScalarIsNaN(fMoveTo.fY)) { |
reed@android.com | 4ddfe35 | 2009-03-20 12:16:09 +0000 | [diff] [blame] | 1809 | return kClose_Verb; |
| 1810 | } |
| 1811 | |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1812 | pts[0] = fLastPt; |
| 1813 | pts[1] = fMoveTo; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1814 | fLastPt = fMoveTo; |
| 1815 | fCloseLine = true; |
| 1816 | return kLine_Verb; |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 1817 | } else { |
| 1818 | pts[0] = fMoveTo; |
| 1819 | return kClose_Verb; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1820 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1823 | const SkPoint& SkPath::Iter::cons_moveTo() { |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1824 | if (fSegmentState == kAfterMove_SegmentState) { |
| 1825 | // Set the first return pt to the move pt |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1826 | fSegmentState = kAfterPrimitive_SegmentState; |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1827 | return fMoveTo; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1828 | } else { |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1829 | SkASSERT(fSegmentState == kAfterPrimitive_SegmentState); |
| 1830 | // 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] | 1831 | return fPts[-1]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1832 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1835 | void SkPath::Iter::consumeDegenerateSegments() { |
| 1836 | // We need to step over anything that will not move the current draw point |
| 1837 | // forward before the next move is seen |
| 1838 | const uint8_t* lastMoveVerb = 0; |
| 1839 | const SkPoint* lastMovePt = 0; |
| 1840 | SkPoint lastPt = fLastPt; |
| 1841 | while (fVerbs != fVerbStop) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1842 | unsigned verb = *(fVerbs - 1); // fVerbs is one beyond the current verb |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1843 | switch (verb) { |
| 1844 | case kMove_Verb: |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1845 | // Keep a record of this most recent move |
| 1846 | lastMoveVerb = fVerbs; |
| 1847 | lastMovePt = fPts; |
schenney@chromium.org | b0af6da | 2011-12-21 20:43:13 +0000 | [diff] [blame] | 1848 | lastPt = fPts[0]; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1849 | fVerbs--; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1850 | fPts++; |
| 1851 | break; |
| 1852 | |
| 1853 | case kClose_Verb: |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1854 | // A close when we are in a segment is always valid except when it |
| 1855 | // follows a move which follows a segment. |
| 1856 | if (fSegmentState == kAfterPrimitive_SegmentState && !lastMoveVerb) { |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1857 | return; |
| 1858 | } |
| 1859 | // A close at any other time must be ignored |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1860 | fVerbs--; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1861 | break; |
| 1862 | |
| 1863 | case kLine_Verb: |
| 1864 | if (!IsLineDegenerate(lastPt, fPts[0])) { |
| 1865 | if (lastMoveVerb) { |
| 1866 | fVerbs = lastMoveVerb; |
| 1867 | fPts = lastMovePt; |
| 1868 | return; |
| 1869 | } |
| 1870 | return; |
| 1871 | } |
| 1872 | // Ignore this line and continue |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1873 | fVerbs--; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1874 | fPts++; |
| 1875 | break; |
| 1876 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1877 | case kConic_Verb: |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1878 | case kQuad_Verb: |
| 1879 | if (!IsQuadDegenerate(lastPt, fPts[0], fPts[1])) { |
| 1880 | if (lastMoveVerb) { |
| 1881 | fVerbs = lastMoveVerb; |
| 1882 | fPts = lastMovePt; |
| 1883 | return; |
| 1884 | } |
| 1885 | return; |
| 1886 | } |
| 1887 | // Ignore this line and continue |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1888 | fVerbs--; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1889 | fPts += 2; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1890 | fConicWeights += (kConic_Verb == verb); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1891 | break; |
| 1892 | |
| 1893 | case kCubic_Verb: |
| 1894 | if (!IsCubicDegenerate(lastPt, fPts[0], fPts[1], fPts[2])) { |
| 1895 | if (lastMoveVerb) { |
| 1896 | fVerbs = lastMoveVerb; |
| 1897 | fPts = lastMovePt; |
| 1898 | return; |
| 1899 | } |
| 1900 | return; |
| 1901 | } |
| 1902 | // Ignore this line and continue |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1903 | fVerbs--; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1904 | fPts += 3; |
| 1905 | break; |
schenney@chromium.org | b0af6da | 2011-12-21 20:43:13 +0000 | [diff] [blame] | 1906 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1907 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1908 | SkDEBUGFAIL("Should never see kDone_Verb"); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1909 | } |
| 1910 | } |
| 1911 | } |
| 1912 | |
reed@google.com | 4a3b714 | 2012-05-16 17:16:46 +0000 | [diff] [blame] | 1913 | SkPath::Verb SkPath::Iter::doNext(SkPoint ptsParam[4]) { |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1914 | SkASSERT(ptsParam); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1915 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1916 | if (fVerbs == fVerbStop) { |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1917 | // Close the curve if requested and if there is some curve to close |
| 1918 | if (fNeedClose && fSegmentState == kAfterPrimitive_SegmentState) { |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1919 | if (kLine_Verb == this->autoClose(ptsParam)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1920 | return kLine_Verb; |
| 1921 | } |
| 1922 | fNeedClose = false; |
| 1923 | return kClose_Verb; |
| 1924 | } |
| 1925 | return kDone_Verb; |
| 1926 | } |
| 1927 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1928 | // fVerbs is one beyond the current verb, decrement first |
| 1929 | unsigned verb = *(--fVerbs); |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1930 | const SkPoint* SK_RESTRICT srcPts = fPts; |
| 1931 | SkPoint* SK_RESTRICT pts = ptsParam; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1932 | |
| 1933 | switch (verb) { |
| 1934 | case kMove_Verb: |
| 1935 | if (fNeedClose) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1936 | fVerbs++; // move back one verb |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1937 | verb = this->autoClose(pts); |
| 1938 | if (verb == kClose_Verb) { |
| 1939 | fNeedClose = false; |
| 1940 | } |
| 1941 | return (Verb)verb; |
| 1942 | } |
| 1943 | if (fVerbs == fVerbStop) { // might be a trailing moveto |
| 1944 | return kDone_Verb; |
| 1945 | } |
schenney@chromium.org | b0af6da | 2011-12-21 20:43:13 +0000 | [diff] [blame] | 1946 | fMoveTo = *srcPts; |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1947 | pts[0] = *srcPts; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1948 | srcPts += 1; |
schenney@chromium.org | b0af6da | 2011-12-21 20:43:13 +0000 | [diff] [blame] | 1949 | fSegmentState = kAfterMove_SegmentState; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1950 | fLastPt = fMoveTo; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1951 | fNeedClose = fForceClose; |
| 1952 | break; |
| 1953 | case kLine_Verb: |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1954 | pts[0] = this->cons_moveTo(); |
| 1955 | pts[1] = srcPts[0]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1956 | fLastPt = srcPts[0]; |
| 1957 | fCloseLine = false; |
| 1958 | srcPts += 1; |
| 1959 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1960 | case kConic_Verb: |
| 1961 | fConicWeights += 1; |
| 1962 | // fall-through |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1963 | case kQuad_Verb: |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1964 | pts[0] = this->cons_moveTo(); |
| 1965 | memcpy(&pts[1], srcPts, 2 * sizeof(SkPoint)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1966 | fLastPt = srcPts[1]; |
| 1967 | srcPts += 2; |
| 1968 | break; |
| 1969 | case kCubic_Verb: |
reed@google.com | 9e25dbf | 2012-05-15 17:05:38 +0000 | [diff] [blame] | 1970 | pts[0] = this->cons_moveTo(); |
| 1971 | memcpy(&pts[1], srcPts, 3 * sizeof(SkPoint)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1972 | fLastPt = srcPts[2]; |
| 1973 | srcPts += 3; |
| 1974 | break; |
| 1975 | case kClose_Verb: |
| 1976 | verb = this->autoClose(pts); |
| 1977 | if (verb == kLine_Verb) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1978 | fVerbs++; // move back one verb |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1979 | } else { |
| 1980 | fNeedClose = false; |
schenney@chromium.org | fde6b41 | 2012-01-19 15:31:01 +0000 | [diff] [blame] | 1981 | fSegmentState = kEmptyContour_SegmentState; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1982 | } |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1983 | fLastPt = fMoveTo; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1984 | break; |
| 1985 | } |
| 1986 | fPts = srcPts; |
| 1987 | return (Verb)verb; |
| 1988 | } |
| 1989 | |
| 1990 | /////////////////////////////////////////////////////////////////////////////// |
| 1991 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1992 | SkPath::RawIter::RawIter() { |
| 1993 | #ifdef SK_DEBUG |
| 1994 | fPts = NULL; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1995 | fConicWeights = NULL; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1996 | fMoveTo.fX = fMoveTo.fY = fLastPt.fX = fLastPt.fY = 0; |
| 1997 | #endif |
| 1998 | // need to init enough to make next() harmlessly return kDone_Verb |
| 1999 | fVerbs = NULL; |
| 2000 | fVerbStop = NULL; |
| 2001 | } |
| 2002 | |
| 2003 | SkPath::RawIter::RawIter(const SkPath& path) { |
| 2004 | this->setPath(path); |
| 2005 | } |
| 2006 | |
| 2007 | void SkPath::RawIter::setPath(const SkPath& path) { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2008 | fPts = path.fPathRef->points(); |
| 2009 | fVerbs = path.fPathRef->verbs(); |
| 2010 | fVerbStop = path.fPathRef->verbsMemBegin(); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2011 | fConicWeights = path.fPathRef->conicWeights() - 1; // begin one behind |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2012 | fMoveTo.fX = fMoveTo.fY = 0; |
| 2013 | fLastPt.fX = fLastPt.fY = 0; |
| 2014 | } |
| 2015 | |
| 2016 | SkPath::Verb SkPath::RawIter::next(SkPoint pts[4]) { |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 2017 | SkASSERT(NULL != pts); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2018 | if (fVerbs == fVerbStop) { |
| 2019 | return kDone_Verb; |
| 2020 | } |
| 2021 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2022 | // fVerbs points one beyond next verb so decrement first. |
| 2023 | unsigned verb = *(--fVerbs); |
| 2024 | const SkPoint* srcPts = fPts; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2025 | |
| 2026 | switch (verb) { |
| 2027 | case kMove_Verb: |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 2028 | pts[0] = *srcPts; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2029 | fMoveTo = srcPts[0]; |
| 2030 | fLastPt = fMoveTo; |
| 2031 | srcPts += 1; |
| 2032 | break; |
| 2033 | case kLine_Verb: |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 2034 | pts[0] = fLastPt; |
| 2035 | pts[1] = srcPts[0]; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2036 | fLastPt = srcPts[0]; |
| 2037 | srcPts += 1; |
| 2038 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2039 | case kConic_Verb: |
| 2040 | fConicWeights += 1; |
| 2041 | // fall-through |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2042 | case kQuad_Verb: |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 2043 | pts[0] = fLastPt; |
| 2044 | memcpy(&pts[1], srcPts, 2 * sizeof(SkPoint)); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2045 | fLastPt = srcPts[1]; |
| 2046 | srcPts += 2; |
| 2047 | break; |
| 2048 | case kCubic_Verb: |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 2049 | pts[0] = fLastPt; |
| 2050 | memcpy(&pts[1], srcPts, 3 * sizeof(SkPoint)); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2051 | fLastPt = srcPts[2]; |
| 2052 | srcPts += 3; |
| 2053 | break; |
| 2054 | case kClose_Verb: |
| 2055 | fLastPt = fMoveTo; |
bsalomon@google.com | f6d3c5a | 2012-06-07 17:47:33 +0000 | [diff] [blame] | 2056 | pts[0] = fMoveTo; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2057 | break; |
| 2058 | } |
| 2059 | fPts = srcPts; |
| 2060 | return (Verb)verb; |
| 2061 | } |
| 2062 | |
| 2063 | /////////////////////////////////////////////////////////////////////////////// |
| 2064 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2065 | /* |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 2066 | Format in compressed buffer: [ptCount, verbCount, pts[], verbs[]] |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2067 | */ |
| 2068 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 2069 | uint32_t SkPath::writeToMemory(void* storage) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2070 | SkDEBUGCODE(this->validate();) |
| 2071 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 2072 | if (NULL == storage) { |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 2073 | const int byteCount = sizeof(int32_t) + fPathRef->writeSize(); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 2074 | return SkAlign4(byteCount); |
| 2075 | } |
| 2076 | |
| 2077 | SkWBuffer buffer(storage); |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 2078 | |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 2079 | int32_t packed = ((fIsOval & 1) << kIsOval_SerializationShift) | |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 2080 | (fConvexity << kConvexity_SerializationShift) | |
| 2081 | (fFillType << kFillType_SerializationShift) | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2082 | (fSegmentMask << kSegmentMask_SerializationShift) | |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 2083 | (fDirection << kDirection_SerializationShift) |
| 2084 | #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TOO |
| 2085 | | (0x1 << kNewFormat_SerializationShift); |
| 2086 | #endif |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 2087 | |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 2088 | buffer.write32(packed); |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 2089 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2090 | fPathRef->writeToBuffer(&buffer); |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 2091 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 2092 | buffer.padToAlign4(); |
scroggo@google.com | 614f9e3 | 2013-05-09 18:05:32 +0000 | [diff] [blame] | 2093 | return SkToU32(buffer.pos()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 2096 | uint32_t SkPath::readFromMemory(const void* storage) { |
| 2097 | SkRBuffer buffer(storage); |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 2098 | |
reed@google.com | 98b11f1 | 2011-09-21 18:40:27 +0000 | [diff] [blame] | 2099 | uint32_t packed = buffer.readS32(); |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 2100 | fIsOval = (packed >> kIsOval_SerializationShift) & 1; |
| 2101 | fConvexity = (packed >> kConvexity_SerializationShift) & 0xFF; |
| 2102 | fFillType = (packed >> kFillType_SerializationShift) & 0xFF; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2103 | fSegmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2104 | fDirection = (packed >> kDirection_SerializationShift) & 0x3; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 2105 | #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TOO |
| 2106 | bool newFormat = (packed >> kNewFormat_SerializationShift) & 1; |
| 2107 | #endif |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 2108 | |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 2109 | fPathRef.reset(SkPathRef::CreateFromBuffer(&buffer |
| 2110 | #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TOO |
| 2111 | , newFormat, packed) |
| 2112 | #endif |
| 2113 | ); |
robertphillips@google.com | 01ec2eb | 2012-08-17 10:58:49 +0000 | [diff] [blame] | 2114 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 2115 | buffer.skipToAlign4(); |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 2116 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2117 | SkDEBUGCODE(this->validate();) |
scroggo@google.com | 614f9e3 | 2013-05-09 18:05:32 +0000 | [diff] [blame] | 2118 | return SkToU32(buffer.pos()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2122 | |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2123 | #include "SkString.h" |
| 2124 | |
| 2125 | static void append_scalar(SkString* str, SkScalar value) { |
| 2126 | SkString tmp; |
| 2127 | tmp.printf("%g", value); |
| 2128 | if (tmp.contains('.')) { |
| 2129 | tmp.appendUnichar('f'); |
| 2130 | } |
| 2131 | str->append(tmp); |
| 2132 | } |
| 2133 | |
| 2134 | static void append_params(SkString* str, const char label[], const SkPoint pts[], |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2135 | int count, SkScalar conicWeight = -1) { |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2136 | str->append(label); |
| 2137 | str->append("("); |
skia.committer@gmail.com | 15dd300 | 2013-01-18 07:07:28 +0000 | [diff] [blame] | 2138 | |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2139 | const SkScalar* values = &pts[0].fX; |
| 2140 | count *= 2; |
| 2141 | |
| 2142 | for (int i = 0; i < count; ++i) { |
| 2143 | append_scalar(str, values[i]); |
| 2144 | if (i < count - 1) { |
| 2145 | str->append(", "); |
| 2146 | } |
| 2147 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2148 | if (conicWeight >= 0) { |
| 2149 | str->append(", "); |
| 2150 | append_scalar(str, conicWeight); |
| 2151 | } |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2152 | str->append(");\n"); |
| 2153 | } |
| 2154 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2155 | void SkPath::dump(bool forceClose, const char title[]) const { |
| 2156 | Iter iter(*this, forceClose); |
| 2157 | SkPoint pts[4]; |
| 2158 | Verb verb; |
| 2159 | |
| 2160 | SkDebugf("path: forceClose=%s %s\n", forceClose ? "true" : "false", |
| 2161 | title ? title : ""); |
| 2162 | |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2163 | SkString builder; |
| 2164 | |
reed@google.com | 4a3b714 | 2012-05-16 17:16:46 +0000 | [diff] [blame] | 2165 | while ((verb = iter.next(pts, false)) != kDone_Verb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2166 | switch (verb) { |
| 2167 | case kMove_Verb: |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2168 | append_params(&builder, "path.moveTo", &pts[0], 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2169 | break; |
| 2170 | case kLine_Verb: |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2171 | append_params(&builder, "path.lineTo", &pts[1], 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2172 | break; |
| 2173 | case kQuad_Verb: |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2174 | append_params(&builder, "path.quadTo", &pts[1], 2); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2175 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2176 | case kConic_Verb: |
| 2177 | append_params(&builder, "path.conicTo", &pts[1], 2, iter.conicWeight()); |
| 2178 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2179 | case kCubic_Verb: |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2180 | append_params(&builder, "path.cubicTo", &pts[1], 3); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2181 | break; |
| 2182 | case kClose_Verb: |
reed@google.com | f919b72 | 2013-01-18 17:53:36 +0000 | [diff] [blame] | 2183 | builder.append("path.close();\n"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2184 | break; |
| 2185 | default: |
| 2186 | SkDebugf(" path: UNKNOWN VERB %d, aborting dump...\n", verb); |
| 2187 | verb = kDone_Verb; // stop the loop |
| 2188 | break; |
| 2189 | } |
| 2190 | } |
reed@google.com | 51bbe75 | 2013-01-17 22:07:50 +0000 | [diff] [blame] | 2191 | SkDebugf("%s\n", builder.c_str()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2194 | void SkPath::dump() const { |
| 2195 | this->dump(false); |
| 2196 | } |
| 2197 | |
| 2198 | #ifdef SK_DEBUG |
| 2199 | void SkPath::validate() const { |
| 2200 | SkASSERT(this != NULL); |
| 2201 | SkASSERT((fFillType & ~3) == 0); |
reed@google.com | abf15c1 | 2011-01-18 20:35:51 +0000 | [diff] [blame] | 2202 | |
djsollen@google.com | 077348c | 2012-10-22 20:23:32 +0000 | [diff] [blame] | 2203 | #ifdef SK_DEBUG_PATH |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2204 | if (!fBoundsIsDirty) { |
| 2205 | SkRect bounds; |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 2206 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2207 | bool isFinite = compute_pt_bounds(&bounds, *fPathRef.get()); |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 2208 | SkASSERT(SkToBool(fIsFinite) == isFinite); |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 2209 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2210 | if (fPathRef->countPoints() <= 1) { |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2211 | // if we're empty, fBounds may be empty but translated, so we can't |
| 2212 | // necessarily compare to bounds directly |
| 2213 | // try path.addOval(2, 2, 2, 2) which is empty, but the bounds will |
| 2214 | // be [2, 2, 2, 2] |
| 2215 | SkASSERT(bounds.isEmpty()); |
| 2216 | SkASSERT(fBounds.isEmpty()); |
| 2217 | } else { |
reed@google.com | eac52bd | 2011-11-14 18:13:59 +0000 | [diff] [blame] | 2218 | if (bounds.isEmpty()) { |
| 2219 | SkASSERT(fBounds.isEmpty()); |
| 2220 | } else { |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 2221 | if (!fBounds.isEmpty()) { |
| 2222 | SkASSERT(fBounds.contains(bounds)); |
| 2223 | } |
reed@google.com | eac52bd | 2011-11-14 18:13:59 +0000 | [diff] [blame] | 2224 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2225 | } |
| 2226 | } |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2227 | |
| 2228 | uint32_t mask = 0; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2229 | const uint8_t* verbs = const_cast<const SkPathRef*>(fPathRef.get())->verbs(); |
| 2230 | for (int i = 0; i < fPathRef->countVerbs(); i++) { |
| 2231 | switch (verbs[~i]) { |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2232 | case kLine_Verb: |
| 2233 | mask |= kLine_SegmentMask; |
| 2234 | break; |
| 2235 | case kQuad_Verb: |
| 2236 | mask |= kQuad_SegmentMask; |
| 2237 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2238 | case kConic_Verb: |
| 2239 | mask |= kConic_SegmentMask; |
| 2240 | break; |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2241 | case kCubic_Verb: |
| 2242 | mask |= kCubic_SegmentMask; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2243 | case kMove_Verb: // these verbs aren't included in the segment mask. |
| 2244 | case kClose_Verb: |
| 2245 | break; |
| 2246 | case kDone_Verb: |
| 2247 | SkDEBUGFAIL("Done verb shouldn't be recorded."); |
| 2248 | break; |
| 2249 | default: |
| 2250 | SkDEBUGFAIL("Unknown Verb"); |
| 2251 | break; |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | SkASSERT(mask == fSegmentMask); |
djsollen@google.com | 077348c | 2012-10-22 20:23:32 +0000 | [diff] [blame] | 2255 | #endif // SK_DEBUG_PATH |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2256 | } |
djsollen@google.com | 077348c | 2012-10-22 20:23:32 +0000 | [diff] [blame] | 2257 | #endif // SK_DEBUG |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 2258 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2259 | /////////////////////////////////////////////////////////////////////////////// |
| 2260 | |
reed@google.com | 0b7b982 | 2011-05-16 12:29:27 +0000 | [diff] [blame] | 2261 | static int sign(SkScalar x) { return x < 0; } |
| 2262 | #define kValueNeverReturnedBySign 2 |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2263 | |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 2264 | static bool AlmostEqual(SkScalar compA, SkScalar compB) { |
| 2265 | // The error epsilon was empirically derived; worse case round rects |
| 2266 | // with a mid point outset by 2x float epsilon in tests had an error |
| 2267 | // of 12. |
| 2268 | const int epsilon = 16; |
| 2269 | if (!SkScalarIsFinite(compA) || !SkScalarIsFinite(compB)) { |
| 2270 | return false; |
| 2271 | } |
| 2272 | if (sk_float_abs(compA) <= FLT_EPSILON && sk_float_abs(compB) <= FLT_EPSILON) { |
| 2273 | return true; |
| 2274 | } |
| 2275 | int aBits = SkFloatAs2sCompliment(compA); |
| 2276 | int bBits = SkFloatAs2sCompliment(compB); |
| 2277 | return aBits < bBits + epsilon && bBits < aBits + epsilon; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | // only valid for a single contour |
| 2281 | struct Convexicator { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2282 | Convexicator() |
| 2283 | : fPtCount(0) |
| 2284 | , fConvexity(SkPath::kConvex_Convexity) |
| 2285 | , fDirection(SkPath::kUnknown_Direction) { |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2286 | fSign = 0; |
| 2287 | // warnings |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 2288 | fLastPt.set(0, 0); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2289 | fCurrPt.set(0, 0); |
| 2290 | fVec0.set(0, 0); |
| 2291 | fVec1.set(0, 0); |
| 2292 | fFirstVec.set(0, 0); |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2293 | |
| 2294 | fDx = fDy = 0; |
reed@google.com | 0b7b982 | 2011-05-16 12:29:27 +0000 | [diff] [blame] | 2295 | fSx = fSy = kValueNeverReturnedBySign; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | SkPath::Convexity getConvexity() const { return fConvexity; } |
| 2299 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2300 | /** The direction returned is only valid if the path is determined convex */ |
| 2301 | SkPath::Direction getDirection() const { return fDirection; } |
| 2302 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2303 | void addPt(const SkPoint& pt) { |
| 2304 | if (SkPath::kConcave_Convexity == fConvexity) { |
| 2305 | return; |
| 2306 | } |
| 2307 | |
| 2308 | if (0 == fPtCount) { |
| 2309 | fCurrPt = pt; |
| 2310 | ++fPtCount; |
| 2311 | } else { |
| 2312 | SkVector vec = pt - fCurrPt; |
| 2313 | if (vec.fX || vec.fY) { |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 2314 | fLastPt = fCurrPt; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2315 | fCurrPt = pt; |
| 2316 | if (++fPtCount == 2) { |
| 2317 | fFirstVec = fVec1 = vec; |
| 2318 | } else { |
| 2319 | SkASSERT(fPtCount > 2); |
| 2320 | this->addVec(vec); |
| 2321 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2322 | |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2323 | int sx = sign(vec.fX); |
| 2324 | int sy = sign(vec.fY); |
| 2325 | fDx += (sx != fSx); |
| 2326 | fDy += (sy != fSy); |
| 2327 | fSx = sx; |
| 2328 | fSy = sy; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2329 | |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 2330 | if (fDx > 3 || fDy > 3) { |
| 2331 | fConvexity = SkPath::kConcave_Convexity; |
| 2332 | } |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2333 | } |
| 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | void close() { |
| 2338 | if (fPtCount > 2) { |
| 2339 | this->addVec(fFirstVec); |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | private: |
| 2344 | void addVec(const SkVector& vec) { |
| 2345 | SkASSERT(vec.fX || vec.fY); |
| 2346 | fVec0 = fVec1; |
| 2347 | fVec1 = vec; |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 2348 | SkScalar cross = SkPoint::CrossProduct(fVec0, fVec1); |
| 2349 | SkScalar smallest = SkTMin(fCurrPt.fX, SkTMin(fCurrPt.fY, SkTMin(fLastPt.fX, fLastPt.fY))); |
| 2350 | SkScalar largest = SkTMax(fCurrPt.fX, SkTMax(fCurrPt.fY, SkTMax(fLastPt.fX, fLastPt.fY))); |
| 2351 | largest = SkTMax(largest, -smallest); |
| 2352 | int sign = AlmostEqual(largest, largest + cross) ? 0 : SkScalarSignAsInt(cross); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2353 | if (0 == fSign) { |
| 2354 | fSign = sign; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2355 | if (1 == sign) { |
| 2356 | fDirection = SkPath::kCW_Direction; |
| 2357 | } else if (-1 == sign) { |
| 2358 | fDirection = SkPath::kCCW_Direction; |
| 2359 | } |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2360 | } else if (sign) { |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 2361 | if (fSign != sign) { |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2362 | fConvexity = SkPath::kConcave_Convexity; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2363 | fDirection = SkPath::kUnknown_Direction; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2364 | } |
| 2365 | } |
| 2366 | } |
| 2367 | |
commit-bot@chromium.org | f91aaec | 2013-11-01 15:24:55 +0000 | [diff] [blame^] | 2368 | SkPoint fLastPt; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2369 | SkPoint fCurrPt; |
| 2370 | SkVector fVec0, fVec1, fFirstVec; |
| 2371 | int fPtCount; // non-degenerate points |
| 2372 | int fSign; |
| 2373 | SkPath::Convexity fConvexity; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2374 | SkPath::Direction fDirection; |
reed@google.com | 0b7b982 | 2011-05-16 12:29:27 +0000 | [diff] [blame] | 2375 | int fDx, fDy, fSx, fSy; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2376 | }; |
| 2377 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2378 | SkPath::Convexity SkPath::internalGetConvexity() const { |
| 2379 | SkASSERT(kUnknown_Convexity == fConvexity); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2380 | SkPoint pts[4]; |
| 2381 | SkPath::Verb verb; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2382 | SkPath::Iter iter(*this, true); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2383 | |
| 2384 | int contourCount = 0; |
| 2385 | int count; |
| 2386 | Convexicator state; |
| 2387 | |
| 2388 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 2389 | switch (verb) { |
| 2390 | case kMove_Verb: |
| 2391 | if (++contourCount > 1) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2392 | fConvexity = kConcave_Convexity; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2393 | return kConcave_Convexity; |
| 2394 | } |
| 2395 | pts[1] = pts[0]; |
| 2396 | count = 1; |
| 2397 | break; |
| 2398 | case kLine_Verb: count = 1; break; |
| 2399 | case kQuad_Verb: count = 2; break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2400 | case kConic_Verb: count = 2; break; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2401 | case kCubic_Verb: count = 3; break; |
| 2402 | case kClose_Verb: |
| 2403 | state.close(); |
| 2404 | count = 0; |
| 2405 | break; |
| 2406 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 2407 | SkDEBUGFAIL("bad verb"); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2408 | fConvexity = kConcave_Convexity; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2409 | return kConcave_Convexity; |
| 2410 | } |
| 2411 | |
| 2412 | for (int i = 1; i <= count; i++) { |
| 2413 | state.addPt(pts[i]); |
| 2414 | } |
| 2415 | // early exit |
| 2416 | if (kConcave_Convexity == state.getConvexity()) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2417 | fConvexity = kConcave_Convexity; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2418 | return kConcave_Convexity; |
| 2419 | } |
| 2420 | } |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2421 | fConvexity = state.getConvexity(); |
| 2422 | if (kConvex_Convexity == fConvexity && kUnknown_Direction == fDirection) { |
| 2423 | fDirection = state.getDirection(); |
| 2424 | } |
| 2425 | return static_cast<Convexity>(fConvexity); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2426 | } |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2427 | |
| 2428 | /////////////////////////////////////////////////////////////////////////////// |
| 2429 | |
| 2430 | class ContourIter { |
| 2431 | public: |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2432 | ContourIter(const SkPathRef& pathRef); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2433 | |
| 2434 | bool done() const { return fDone; } |
| 2435 | // if !done() then these may be called |
| 2436 | int count() const { return fCurrPtCount; } |
| 2437 | const SkPoint* pts() const { return fCurrPt; } |
| 2438 | void next(); |
| 2439 | |
| 2440 | private: |
| 2441 | int fCurrPtCount; |
| 2442 | const SkPoint* fCurrPt; |
| 2443 | const uint8_t* fCurrVerb; |
| 2444 | const uint8_t* fStopVerbs; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2445 | const SkScalar* fCurrConicWeight; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2446 | bool fDone; |
reed@google.com | d1ab932 | 2012-01-10 18:40:03 +0000 | [diff] [blame] | 2447 | SkDEBUGCODE(int fContourCounter;) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2448 | }; |
| 2449 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2450 | ContourIter::ContourIter(const SkPathRef& pathRef) { |
| 2451 | fStopVerbs = pathRef.verbsMemBegin(); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2452 | fDone = false; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2453 | fCurrPt = pathRef.points(); |
| 2454 | fCurrVerb = pathRef.verbs(); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2455 | fCurrConicWeight = pathRef.conicWeights(); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2456 | fCurrPtCount = 0; |
reed@google.com | d1ab932 | 2012-01-10 18:40:03 +0000 | [diff] [blame] | 2457 | SkDEBUGCODE(fContourCounter = 0;) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2458 | this->next(); |
| 2459 | } |
| 2460 | |
| 2461 | void ContourIter::next() { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2462 | if (fCurrVerb <= fStopVerbs) { |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2463 | fDone = true; |
| 2464 | } |
| 2465 | if (fDone) { |
| 2466 | return; |
| 2467 | } |
| 2468 | |
| 2469 | // skip pts of prev contour |
| 2470 | fCurrPt += fCurrPtCount; |
| 2471 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2472 | SkASSERT(SkPath::kMove_Verb == fCurrVerb[~0]); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2473 | int ptCount = 1; // moveTo |
| 2474 | const uint8_t* verbs = fCurrVerb; |
| 2475 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2476 | for (--verbs; verbs > fStopVerbs; --verbs) { |
| 2477 | switch (verbs[~0]) { |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2478 | case SkPath::kMove_Verb: |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2479 | goto CONTOUR_END; |
| 2480 | case SkPath::kLine_Verb: |
| 2481 | ptCount += 1; |
| 2482 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2483 | case SkPath::kConic_Verb: |
| 2484 | fCurrConicWeight += 1; |
| 2485 | // fall-through |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2486 | case SkPath::kQuad_Verb: |
| 2487 | ptCount += 2; |
| 2488 | break; |
| 2489 | case SkPath::kCubic_Verb: |
| 2490 | ptCount += 3; |
| 2491 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2492 | case SkPath::kClose_Verb: |
| 2493 | break; |
| 2494 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 2495 | SkDEBUGFAIL("unexpected verb"); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2496 | break; |
| 2497 | } |
| 2498 | } |
| 2499 | CONTOUR_END: |
| 2500 | fCurrPtCount = ptCount; |
| 2501 | fCurrVerb = verbs; |
reed@google.com | d1ab932 | 2012-01-10 18:40:03 +0000 | [diff] [blame] | 2502 | SkDEBUGCODE(++fContourCounter;) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 2505 | // returns cross product of (p1 - p0) and (p2 - p0) |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2506 | 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] | 2507 | SkScalar cross = SkPoint::CrossProduct(p1 - p0, p2 - p0); |
| 2508 | // We may get 0 when the above subtracts underflow. We expect this to be |
| 2509 | // very rare and lazily promote to double. |
| 2510 | if (0 == cross) { |
| 2511 | double p0x = SkScalarToDouble(p0.fX); |
| 2512 | double p0y = SkScalarToDouble(p0.fY); |
| 2513 | |
| 2514 | double p1x = SkScalarToDouble(p1.fX); |
| 2515 | double p1y = SkScalarToDouble(p1.fY); |
| 2516 | |
| 2517 | double p2x = SkScalarToDouble(p2.fX); |
| 2518 | double p2y = SkScalarToDouble(p2.fY); |
| 2519 | |
| 2520 | cross = SkDoubleToScalar((p1x - p0x) * (p2y - p0y) - |
| 2521 | (p1y - p0y) * (p2x - p0x)); |
| 2522 | |
| 2523 | } |
| 2524 | return cross; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2527 | // Returns the first pt with the maximum Y coordinate |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2528 | static int find_max_y(const SkPoint pts[], int count) { |
| 2529 | SkASSERT(count > 0); |
| 2530 | SkScalar max = pts[0].fY; |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2531 | int firstIndex = 0; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2532 | for (int i = 1; i < count; ++i) { |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2533 | SkScalar y = pts[i].fY; |
| 2534 | if (y > max) { |
| 2535 | max = y; |
| 2536 | firstIndex = i; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2537 | } |
| 2538 | } |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2539 | return firstIndex; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2542 | static int find_diff_pt(const SkPoint pts[], int index, int n, int inc) { |
| 2543 | int i = index; |
| 2544 | for (;;) { |
| 2545 | i = (i + inc) % n; |
| 2546 | if (i == index) { // we wrapped around, so abort |
| 2547 | break; |
| 2548 | } |
| 2549 | if (pts[index] != pts[i]) { // found a different point, success! |
| 2550 | break; |
| 2551 | } |
| 2552 | } |
| 2553 | return i; |
| 2554 | } |
| 2555 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2556 | /** |
| 2557 | * Starting at index, and moving forward (incrementing), find the xmin and |
| 2558 | * xmax of the contiguous points that have the same Y. |
| 2559 | */ |
| 2560 | static int find_min_max_x_at_y(const SkPoint pts[], int index, int n, |
| 2561 | int* maxIndexPtr) { |
| 2562 | const SkScalar y = pts[index].fY; |
| 2563 | SkScalar min = pts[index].fX; |
| 2564 | SkScalar max = min; |
| 2565 | int minIndex = index; |
| 2566 | int maxIndex = index; |
| 2567 | for (int i = index + 1; i < n; ++i) { |
| 2568 | if (pts[i].fY != y) { |
| 2569 | break; |
| 2570 | } |
| 2571 | SkScalar x = pts[i].fX; |
| 2572 | if (x < min) { |
| 2573 | min = x; |
| 2574 | minIndex = i; |
| 2575 | } else if (x > max) { |
| 2576 | max = x; |
| 2577 | maxIndex = i; |
| 2578 | } |
| 2579 | } |
| 2580 | *maxIndexPtr = maxIndex; |
| 2581 | return minIndex; |
| 2582 | } |
| 2583 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2584 | static void crossToDir(SkScalar cross, SkPath::Direction* dir) { |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2585 | if (dir) { |
| 2586 | *dir = cross > 0 ? SkPath::kCW_Direction : SkPath::kCCW_Direction; |
| 2587 | } |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2590 | #if 0 |
| 2591 | #include "SkString.h" |
| 2592 | #include "../utils/SkParsePath.h" |
| 2593 | static void dumpPath(const SkPath& path) { |
| 2594 | SkString str; |
| 2595 | SkParsePath::ToSVGString(path, &str); |
| 2596 | SkDebugf("%s\n", str.c_str()); |
| 2597 | } |
| 2598 | #endif |
| 2599 | |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 2600 | namespace { |
| 2601 | // for use with convex_dir_test |
| 2602 | double mul(double a, double b) { return a * b; } |
| 2603 | SkScalar mul(SkScalar a, SkScalar b) { return SkScalarMul(a, b); } |
| 2604 | double toDouble(SkScalar a) { return SkScalarToDouble(a); } |
| 2605 | SkScalar toScalar(SkScalar a) { return a; } |
| 2606 | |
| 2607 | // determines the winding direction of a convex polygon with the precision |
| 2608 | // of T. CAST_SCALAR casts an SkScalar to T. |
| 2609 | template <typename T, T (CAST_SCALAR)(SkScalar)> |
| 2610 | bool convex_dir_test(int n, const SkPoint pts[], SkPath::Direction* dir) { |
| 2611 | // we find the first three points that form a non-degenerate |
| 2612 | // triangle. If there are no such points then the path is |
| 2613 | // degenerate. The first is always point 0. Now we find the second |
| 2614 | // point. |
| 2615 | int i = 0; |
| 2616 | enum { kX = 0, kY = 1 }; |
| 2617 | T v0[2]; |
| 2618 | while (1) { |
| 2619 | v0[kX] = CAST_SCALAR(pts[i].fX) - CAST_SCALAR(pts[0].fX); |
| 2620 | v0[kY] = CAST_SCALAR(pts[i].fY) - CAST_SCALAR(pts[0].fY); |
| 2621 | if (v0[kX] || v0[kY]) { |
| 2622 | break; |
| 2623 | } |
| 2624 | if (++i == n - 1) { |
| 2625 | return false; |
| 2626 | } |
| 2627 | } |
| 2628 | // now find a third point that is not colinear with the first two |
| 2629 | // points and check the orientation of the triangle (which will be |
| 2630 | // the same as the orientation of the path). |
| 2631 | for (++i; i < n; ++i) { |
| 2632 | T v1[2]; |
| 2633 | v1[kX] = CAST_SCALAR(pts[i].fX) - CAST_SCALAR(pts[0].fX); |
| 2634 | v1[kY] = CAST_SCALAR(pts[i].fY) - CAST_SCALAR(pts[0].fY); |
| 2635 | T cross = mul(v0[kX], v1[kY]) - mul(v0[kY], v1[kX]); |
| 2636 | if (0 != cross) { |
| 2637 | *dir = cross > 0 ? SkPath::kCW_Direction : SkPath::kCCW_Direction; |
| 2638 | return true; |
| 2639 | } |
| 2640 | } |
| 2641 | return false; |
| 2642 | } |
| 2643 | } |
| 2644 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2645 | /* |
| 2646 | * We loop through all contours, and keep the computed cross-product of the |
| 2647 | * contour that contained the global y-max. If we just look at the first |
| 2648 | * contour, we may find one that is wound the opposite way (correctly) since |
| 2649 | * it is the interior of a hole (e.g. 'o'). Thus we must find the contour |
| 2650 | * that is outer most (or at least has the global y-max) before we can consider |
| 2651 | * its cross product. |
| 2652 | */ |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2653 | bool SkPath::cheapComputeDirection(Direction* dir) const { |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2654 | // dumpPath(*this); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2655 | // don't want to pay the cost for computing this if it |
| 2656 | // is unknown, so we don't call isConvex() |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2657 | |
| 2658 | if (kUnknown_Direction != fDirection) { |
| 2659 | *dir = static_cast<Direction>(fDirection); |
| 2660 | return true; |
| 2661 | } |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2662 | const Convexity conv = this->getConvexityOrUnknown(); |
| 2663 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 2664 | ContourIter iter(*fPathRef.get()); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2665 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2666 | // initialize with our logical y-min |
| 2667 | SkScalar ymax = this->getBounds().fTop; |
| 2668 | SkScalar ymaxCross = 0; |
| 2669 | |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2670 | for (; !iter.done(); iter.next()) { |
| 2671 | int n = iter.count(); |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2672 | if (n < 3) { |
| 2673 | continue; |
| 2674 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 2675 | |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2676 | const SkPoint* pts = iter.pts(); |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2677 | SkScalar cross = 0; |
| 2678 | if (kConvex_Convexity == conv) { |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 2679 | // We try first at scalar precision, and then again at double |
| 2680 | // precision. This is because the vectors computed between distant |
| 2681 | // points may lose too much precision. |
| 2682 | if (convex_dir_test<SkScalar, toScalar>(n, pts, dir)) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2683 | fDirection = *dir; |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 2684 | return true; |
| 2685 | } |
| 2686 | if (convex_dir_test<double, toDouble>(n, pts, dir)) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2687 | fDirection = *dir; |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 2688 | return true; |
| 2689 | } else { |
| 2690 | return false; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2691 | } |
| 2692 | } else { |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2693 | int index = find_max_y(pts, n); |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2694 | if (pts[index].fY < ymax) { |
| 2695 | continue; |
| 2696 | } |
| 2697 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2698 | // If there is more than 1 distinct point at the y-max, we take the |
| 2699 | // x-min and x-max of them and just subtract to compute the dir. |
| 2700 | if (pts[(index + 1) % n].fY == pts[index].fY) { |
| 2701 | int maxIndex; |
| 2702 | int minIndex = find_min_max_x_at_y(pts, index, n, &maxIndex); |
reed@google.com | 3e44e4d | 2012-01-31 15:25:22 +0000 | [diff] [blame] | 2703 | if (minIndex == maxIndex) { |
| 2704 | goto TRY_CROSSPROD; |
| 2705 | } |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2706 | SkASSERT(pts[minIndex].fY == pts[index].fY); |
| 2707 | SkASSERT(pts[maxIndex].fY == pts[index].fY); |
| 2708 | SkASSERT(pts[minIndex].fX <= pts[maxIndex].fX); |
| 2709 | // we just subtract the indices, and let that auto-convert to |
| 2710 | // SkScalar, since we just want - or + to signal the direction. |
| 2711 | cross = minIndex - maxIndex; |
| 2712 | } else { |
reed@google.com | 3e44e4d | 2012-01-31 15:25:22 +0000 | [diff] [blame] | 2713 | TRY_CROSSPROD: |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2714 | // Find a next and prev index to use for the cross-product test, |
| 2715 | // but we try to find pts that form non-zero vectors from pts[index] |
| 2716 | // |
| 2717 | // Its possible that we can't find two non-degenerate vectors, so |
| 2718 | // we have to guard our search (e.g. all the pts could be in the |
| 2719 | // same place). |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2720 | |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2721 | // we pass n - 1 instead of -1 so we don't foul up % operator by |
| 2722 | // passing it a negative LH argument. |
| 2723 | int prev = find_diff_pt(pts, index, n, n - 1); |
| 2724 | if (prev == index) { |
| 2725 | // completely degenerate, skip to next contour |
| 2726 | continue; |
| 2727 | } |
| 2728 | int next = find_diff_pt(pts, index, n, 1); |
| 2729 | SkASSERT(next != index); |
| 2730 | cross = cross_prod(pts[prev], pts[index], pts[next]); |
skia.committer@gmail.com | fbb0ed9 | 2012-11-13 21:46:06 +0000 | [diff] [blame] | 2731 | // if we get a zero and the points are horizontal, then we look at the spread in |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2732 | // x-direction. We really should continue to walk away from the degeneracy until |
| 2733 | // there is a divergence. |
| 2734 | if (0 == cross && pts[prev].fY == pts[index].fY && pts[next].fY == pts[index].fY) { |
reed@google.com | c1ea60a | 2012-01-31 15:15:36 +0000 | [diff] [blame] | 2735 | // construct the subtract so we get the correct Direction below |
| 2736 | cross = pts[index].fX - pts[next].fX; |
| 2737 | } |
reed@google.com | 188bfcf | 2012-01-17 18:26:38 +0000 | [diff] [blame] | 2738 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2739 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2740 | if (cross) { |
| 2741 | // record our best guess so far |
| 2742 | ymax = pts[index].fY; |
| 2743 | ymaxCross = cross; |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2744 | } |
reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame] | 2745 | } |
| 2746 | } |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2747 | if (ymaxCross) { |
| 2748 | crossToDir(ymaxCross, dir); |
| 2749 | fDirection = *dir; |
| 2750 | return true; |
| 2751 | } else { |
| 2752 | return false; |
| 2753 | } |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 2754 | } |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2755 | |
| 2756 | /////////////////////////////////////////////////////////////////////////////// |
| 2757 | |
| 2758 | static SkScalar eval_cubic_coeff(SkScalar A, SkScalar B, SkScalar C, |
| 2759 | SkScalar D, SkScalar t) { |
| 2760 | return SkScalarMulAdd(SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C), t, D); |
| 2761 | } |
| 2762 | |
| 2763 | static SkScalar eval_cubic_pts(SkScalar c0, SkScalar c1, SkScalar c2, SkScalar c3, |
| 2764 | SkScalar t) { |
| 2765 | SkScalar A = c3 + 3*(c1 - c2) - c0; |
| 2766 | SkScalar B = 3*(c2 - c1 - c1 + c0); |
| 2767 | SkScalar C = 3*(c1 - c0); |
| 2768 | SkScalar D = c0; |
| 2769 | return eval_cubic_coeff(A, B, C, D, t); |
| 2770 | } |
| 2771 | |
| 2772 | /* Given 4 cubic points (either Xs or Ys), and a target X or Y, compute the |
| 2773 | t value such that cubic(t) = target |
| 2774 | */ |
| 2775 | static bool chopMonoCubicAt(SkScalar c0, SkScalar c1, SkScalar c2, SkScalar c3, |
| 2776 | SkScalar target, SkScalar* t) { |
| 2777 | // SkASSERT(c0 <= c1 && c1 <= c2 && c2 <= c3); |
| 2778 | SkASSERT(c0 < target && target < c3); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2779 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2780 | SkScalar D = c0 - target; |
| 2781 | SkScalar A = c3 + 3*(c1 - c2) - c0; |
| 2782 | SkScalar B = 3*(c2 - c1 - c1 + c0); |
| 2783 | SkScalar C = 3*(c1 - c0); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2784 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2785 | const SkScalar TOLERANCE = SK_Scalar1 / 4096; |
| 2786 | SkScalar minT = 0; |
| 2787 | SkScalar maxT = SK_Scalar1; |
| 2788 | SkScalar mid; |
| 2789 | int i; |
| 2790 | for (i = 0; i < 16; i++) { |
| 2791 | mid = SkScalarAve(minT, maxT); |
| 2792 | SkScalar delta = eval_cubic_coeff(A, B, C, D, mid); |
| 2793 | if (delta < 0) { |
| 2794 | minT = mid; |
| 2795 | delta = -delta; |
| 2796 | } else { |
| 2797 | maxT = mid; |
| 2798 | } |
| 2799 | if (delta < TOLERANCE) { |
| 2800 | break; |
| 2801 | } |
| 2802 | } |
| 2803 | *t = mid; |
| 2804 | return true; |
| 2805 | } |
| 2806 | |
| 2807 | template <size_t N> static void find_minmax(const SkPoint pts[], |
| 2808 | SkScalar* minPtr, SkScalar* maxPtr) { |
| 2809 | SkScalar min, max; |
| 2810 | min = max = pts[0].fX; |
| 2811 | for (size_t i = 1; i < N; ++i) { |
| 2812 | min = SkMinScalar(min, pts[i].fX); |
| 2813 | max = SkMaxScalar(max, pts[i].fX); |
| 2814 | } |
| 2815 | *minPtr = min; |
| 2816 | *maxPtr = max; |
| 2817 | } |
| 2818 | |
| 2819 | static int winding_mono_cubic(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2820 | SkPoint storage[4]; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2821 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2822 | int dir = 1; |
| 2823 | if (pts[0].fY > pts[3].fY) { |
| 2824 | storage[0] = pts[3]; |
| 2825 | storage[1] = pts[2]; |
| 2826 | storage[2] = pts[1]; |
| 2827 | storage[3] = pts[0]; |
| 2828 | pts = storage; |
| 2829 | dir = -1; |
| 2830 | } |
| 2831 | if (y < pts[0].fY || y >= pts[3].fY) { |
| 2832 | return 0; |
| 2833 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2834 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2835 | // quickreject or quickaccept |
| 2836 | SkScalar min, max; |
| 2837 | find_minmax<4>(pts, &min, &max); |
| 2838 | if (x < min) { |
| 2839 | return 0; |
| 2840 | } |
| 2841 | if (x > max) { |
| 2842 | return dir; |
| 2843 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2844 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2845 | // compute the actual x(t) value |
| 2846 | SkScalar t, xt; |
| 2847 | if (chopMonoCubicAt(pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY, y, &t)) { |
| 2848 | xt = eval_cubic_pts(pts[0].fX, pts[1].fX, pts[2].fX, pts[3].fX, t); |
| 2849 | } else { |
| 2850 | SkScalar mid = SkScalarAve(pts[0].fY, pts[3].fY); |
| 2851 | xt = y < mid ? pts[0].fX : pts[3].fX; |
| 2852 | } |
| 2853 | return xt < x ? dir : 0; |
| 2854 | } |
| 2855 | |
| 2856 | static int winding_cubic(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2857 | SkPoint dst[10]; |
| 2858 | int n = SkChopCubicAtYExtrema(pts, dst); |
| 2859 | int w = 0; |
| 2860 | for (int i = 0; i <= n; ++i) { |
| 2861 | w += winding_mono_cubic(&dst[i * 3], x, y); |
| 2862 | } |
| 2863 | return w; |
| 2864 | } |
| 2865 | |
| 2866 | static int winding_mono_quad(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2867 | SkScalar y0 = pts[0].fY; |
| 2868 | SkScalar y2 = pts[2].fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2869 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2870 | int dir = 1; |
| 2871 | if (y0 > y2) { |
| 2872 | SkTSwap(y0, y2); |
| 2873 | dir = -1; |
| 2874 | } |
| 2875 | if (y < y0 || y >= y2) { |
| 2876 | return 0; |
| 2877 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2878 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2879 | // bounds check on X (not required. is it faster?) |
| 2880 | #if 0 |
| 2881 | if (pts[0].fX > x && pts[1].fX > x && pts[2].fX > x) { |
| 2882 | return 0; |
| 2883 | } |
| 2884 | #endif |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2885 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2886 | SkScalar roots[2]; |
| 2887 | int n = SkFindUnitQuadRoots(pts[0].fY - 2 * pts[1].fY + pts[2].fY, |
| 2888 | 2 * (pts[1].fY - pts[0].fY), |
| 2889 | pts[0].fY - y, |
| 2890 | roots); |
| 2891 | SkASSERT(n <= 1); |
| 2892 | SkScalar xt; |
| 2893 | if (0 == n) { |
| 2894 | SkScalar mid = SkScalarAve(y0, y2); |
| 2895 | // Need [0] and [2] if dir == 1 |
| 2896 | // and [2] and [0] if dir == -1 |
| 2897 | xt = y < mid ? pts[1 - dir].fX : pts[dir - 1].fX; |
| 2898 | } else { |
| 2899 | SkScalar t = roots[0]; |
| 2900 | SkScalar C = pts[0].fX; |
| 2901 | SkScalar A = pts[2].fX - 2 * pts[1].fX + C; |
| 2902 | SkScalar B = 2 * (pts[1].fX - C); |
| 2903 | xt = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); |
| 2904 | } |
| 2905 | return xt < x ? dir : 0; |
| 2906 | } |
| 2907 | |
| 2908 | static bool is_mono_quad(SkScalar y0, SkScalar y1, SkScalar y2) { |
| 2909 | // return SkScalarSignAsInt(y0 - y1) + SkScalarSignAsInt(y1 - y2) != 0; |
| 2910 | if (y0 == y1) { |
| 2911 | return true; |
| 2912 | } |
| 2913 | if (y0 < y1) { |
| 2914 | return y1 <= y2; |
| 2915 | } else { |
| 2916 | return y1 >= y2; |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | static int winding_quad(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2921 | SkPoint dst[5]; |
| 2922 | int n = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2923 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2924 | if (!is_mono_quad(pts[0].fY, pts[1].fY, pts[2].fY)) { |
| 2925 | n = SkChopQuadAtYExtrema(pts, dst); |
| 2926 | pts = dst; |
| 2927 | } |
| 2928 | int w = winding_mono_quad(pts, x, y); |
| 2929 | if (n > 0) { |
| 2930 | w += winding_mono_quad(&pts[2], x, y); |
| 2931 | } |
| 2932 | return w; |
| 2933 | } |
| 2934 | |
| 2935 | static int winding_line(const SkPoint pts[], SkScalar x, SkScalar y) { |
| 2936 | SkScalar x0 = pts[0].fX; |
| 2937 | SkScalar y0 = pts[0].fY; |
| 2938 | SkScalar x1 = pts[1].fX; |
| 2939 | SkScalar y1 = pts[1].fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2940 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2941 | SkScalar dy = y1 - y0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2942 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2943 | int dir = 1; |
| 2944 | if (y0 > y1) { |
| 2945 | SkTSwap(y0, y1); |
| 2946 | dir = -1; |
| 2947 | } |
| 2948 | if (y < y0 || y >= y1) { |
| 2949 | return 0; |
| 2950 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2951 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2952 | SkScalar cross = SkScalarMul(x1 - x0, y - pts[0].fY) - |
| 2953 | SkScalarMul(dy, x - pts[0].fX); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2954 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2955 | if (SkScalarSignAsInt(cross) == dir) { |
| 2956 | dir = 0; |
| 2957 | } |
| 2958 | return dir; |
| 2959 | } |
| 2960 | |
reed@google.com | 4db592c | 2013-10-30 17:39:43 +0000 | [diff] [blame] | 2961 | static bool contains_inclusive(const SkRect& r, SkScalar x, SkScalar y) { |
| 2962 | return r.fLeft <= x && x <= r.fRight && r.fTop <= y && y <= r.fBottom; |
| 2963 | } |
| 2964 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2965 | bool SkPath::contains(SkScalar x, SkScalar y) const { |
| 2966 | bool isInverse = this->isInverseFillType(); |
| 2967 | if (this->isEmpty()) { |
| 2968 | return isInverse; |
| 2969 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2970 | |
reed@google.com | 4db592c | 2013-10-30 17:39:43 +0000 | [diff] [blame] | 2971 | if (!contains_inclusive(this->getBounds(), x, y)) { |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2972 | return isInverse; |
| 2973 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2974 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2975 | SkPath::Iter iter(*this, true); |
| 2976 | bool done = false; |
| 2977 | int w = 0; |
| 2978 | do { |
| 2979 | SkPoint pts[4]; |
| 2980 | switch (iter.next(pts, false)) { |
| 2981 | case SkPath::kMove_Verb: |
| 2982 | case SkPath::kClose_Verb: |
| 2983 | break; |
| 2984 | case SkPath::kLine_Verb: |
| 2985 | w += winding_line(pts, x, y); |
| 2986 | break; |
| 2987 | case SkPath::kQuad_Verb: |
| 2988 | w += winding_quad(pts, x, y); |
| 2989 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2990 | case SkPath::kConic_Verb: |
| 2991 | SkASSERT(0); |
| 2992 | break; |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 2993 | case SkPath::kCubic_Verb: |
| 2994 | w += winding_cubic(pts, x, y); |
| 2995 | break; |
| 2996 | case SkPath::kDone_Verb: |
| 2997 | done = true; |
| 2998 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2999 | } |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 3000 | } while (!done); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 3001 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 3002 | switch (this->getFillType()) { |
| 3003 | case SkPath::kEvenOdd_FillType: |
| 3004 | case SkPath::kInverseEvenOdd_FillType: |
| 3005 | w &= 1; |
| 3006 | break; |
reed@google.com | e9bb623 | 2012-07-11 18:56:10 +0000 | [diff] [blame] | 3007 | default: |
| 3008 | break; |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 3009 | } |
| 3010 | return SkToBool(w); |
| 3011 | } |