caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Cary Clark | cadc506 | 2018-08-06 17:24:04 -0400 | [diff] [blame] | 7 | #include "SkOpSegment.h" |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 8 | #include "SkOpSpan.h" |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 9 | #include "SkPathOpsPoint.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 10 | #include "SkPathWriter.h" |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 11 | #include "SkTSort.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 12 | |
| 13 | // wrap path to keep track of whether the contour is initialized and non-empty |
| 14 | SkPathWriter::SkPathWriter(SkPath& path) |
| 15 | : fPathPtr(&path) |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 16 | { |
| 17 | init(); |
| 18 | } |
| 19 | |
| 20 | void SkPathWriter::close() { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 21 | if (fCurrent.isEmpty()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 22 | return; |
| 23 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 24 | SkASSERT(this->isClosed()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 25 | #if DEBUG_PATH_CONSTRUCTION |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 26 | SkDebugf("path.close();\n"); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 27 | #endif |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 28 | fCurrent.close(); |
| 29 | fPathPtr->addPath(fCurrent); |
| 30 | fCurrent.reset(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 31 | init(); |
| 32 | } |
| 33 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 34 | void SkPathWriter::conicTo(const SkPoint& pt1, const SkOpPtT* pt2, SkScalar weight) { |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 35 | SkPoint pt2pt = this->update(pt2); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 36 | #if DEBUG_PATH_CONSTRUCTION |
| 37 | SkDebugf("path.conicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g);\n", |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 38 | pt1.fX, pt1.fY, pt2pt.fX, pt2pt.fY, weight); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 39 | #endif |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 40 | fCurrent.conicTo(pt1, pt2pt, weight); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 41 | } |
| 42 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 43 | void SkPathWriter::cubicTo(const SkPoint& pt1, const SkPoint& pt2, const SkOpPtT* pt3) { |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 44 | SkPoint pt3pt = this->update(pt3); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 45 | #if DEBUG_PATH_CONSTRUCTION |
| 46 | SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n", |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 47 | pt1.fX, pt1.fY, pt2.fX, pt2.fY, pt3pt.fX, pt3pt.fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 48 | #endif |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 49 | fCurrent.cubicTo(pt1, pt2, pt3pt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 50 | } |
| 51 | |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 52 | bool SkPathWriter::deferredLine(const SkOpPtT* pt) { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 53 | SkASSERT(fFirstPtT); |
| 54 | SkASSERT(fDefer[0]); |
| 55 | if (fDefer[0] == pt) { |
| 56 | // FIXME: why we're adding a degenerate line? Caller should have preflighted this. |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 57 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 58 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 59 | if (pt->contains(fDefer[0])) { |
| 60 | // FIXME: why we're adding a degenerate line? |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 61 | return true; |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 62 | } |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 63 | if (this->matchedLast(pt)) { |
| 64 | return false; |
| 65 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 66 | if (fDefer[1] && this->changedSlopes(pt)) { |
| 67 | this->lineTo(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 68 | fDefer[0] = fDefer[1]; |
| 69 | } |
| 70 | fDefer[1] = pt; |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 71 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 72 | } |
| 73 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 74 | void SkPathWriter::deferredMove(const SkOpPtT* pt) { |
| 75 | if (!fDefer[1]) { |
| 76 | fFirstPtT = fDefer[0] = pt; |
| 77 | return; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 78 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 79 | SkASSERT(fDefer[0]); |
| 80 | if (!this->matchedLast(pt)) { |
| 81 | this->finishContour(); |
| 82 | fFirstPtT = fDefer[0] = pt; |
| 83 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 84 | } |
| 85 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 86 | void SkPathWriter::finishContour() { |
| 87 | if (!this->matchedLast(fDefer[0])) { |
caryclark | 1c10607 | 2016-09-22 12:52:20 -0700 | [diff] [blame] | 88 | if (!fDefer[1]) { |
| 89 | return; |
| 90 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 91 | this->lineTo(); |
| 92 | } |
| 93 | if (fCurrent.isEmpty()) { |
| 94 | return; |
| 95 | } |
| 96 | if (this->isClosed()) { |
| 97 | this->close(); |
| 98 | } else { |
| 99 | SkASSERT(fDefer[1]); |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 100 | fEndPtTs.push_back(fFirstPtT); |
| 101 | fEndPtTs.push_back(fDefer[1]); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 102 | fPartials.push_back(fCurrent); |
| 103 | this->init(); |
| 104 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void SkPathWriter::init() { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 108 | fCurrent.reset(); |
| 109 | fFirstPtT = fDefer[0] = fDefer[1] = nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool SkPathWriter::isClosed() const { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 113 | return this->matchedLast(fFirstPtT); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void SkPathWriter::lineTo() { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 117 | if (fCurrent.isEmpty()) { |
| 118 | this->moveTo(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 119 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 120 | #if DEBUG_PATH_CONSTRUCTION |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 121 | SkDebugf("path.lineTo(%1.9g,%1.9g);\n", fDefer[1]->fPt.fX, fDefer[1]->fPt.fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 122 | #endif |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 123 | fCurrent.lineTo(fDefer[1]->fPt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 124 | } |
| 125 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 126 | bool SkPathWriter::matchedLast(const SkOpPtT* test) const { |
| 127 | if (test == fDefer[1]) { |
| 128 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 129 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 130 | if (!test) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 131 | return false; |
| 132 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 133 | if (!fDefer[1]) { |
| 134 | return false; |
| 135 | } |
| 136 | return test->contains(fDefer[1]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void SkPathWriter::moveTo() { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 140 | #if DEBUG_PATH_CONSTRUCTION |
| 141 | SkDebugf("path.moveTo(%1.9g,%1.9g);\n", fFirstPtT->fPt.fX, fFirstPtT->fPt.fY); |
| 142 | #endif |
| 143 | fCurrent.moveTo(fFirstPtT->fPt); |
| 144 | } |
| 145 | |
| 146 | void SkPathWriter::quadTo(const SkPoint& pt1, const SkOpPtT* pt2) { |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 147 | SkPoint pt2pt = this->update(pt2); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 148 | #if DEBUG_PATH_CONSTRUCTION |
| 149 | SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n", |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 150 | pt1.fX, pt1.fY, pt2pt.fX, pt2pt.fY); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 151 | #endif |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 152 | fCurrent.quadTo(pt1, pt2pt); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 155 | // if last point to be written matches the current path's first point, alter the |
| 156 | // last to avoid writing a degenerate lineTo when the path is closed |
| 157 | SkPoint SkPathWriter::update(const SkOpPtT* pt) { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 158 | if (!fDefer[1]) { |
| 159 | this->moveTo(); |
| 160 | } else if (!this->matchedLast(fDefer[0])) { |
| 161 | this->lineTo(); |
| 162 | } |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 163 | SkPoint result = pt->fPt; |
| 164 | if (fFirstPtT && result != fFirstPtT->fPt && fFirstPtT->contains(pt)) { |
| 165 | result = fFirstPtT->fPt; |
| 166 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 167 | fDefer[0] = fDefer[1] = pt; // set both to know that there is not a pending deferred line |
Cary Clark | 98900b5 | 2018-08-17 12:38:22 -0400 | [diff] [blame] | 168 | return result; |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | bool SkPathWriter::someAssemblyRequired() { |
| 172 | this->finishContour(); |
| 173 | return fEndPtTs.count() > 0; |
| 174 | } |
| 175 | |
| 176 | bool SkPathWriter::changedSlopes(const SkOpPtT* ptT) const { |
| 177 | if (matchedLast(fDefer[0])) { |
| 178 | return false; |
| 179 | } |
| 180 | SkVector deferDxdy = fDefer[1]->fPt - fDefer[0]->fPt; |
| 181 | SkVector lineDxdy = ptT->fPt - fDefer[1]->fPt; |
| 182 | return deferDxdy.fX * lineDxdy.fY != deferDxdy.fY * lineDxdy.fX; |
| 183 | } |
| 184 | |
| 185 | class DistanceLessThan { |
| 186 | public: |
| 187 | DistanceLessThan(double* distances) : fDistances(distances) { } |
| 188 | double* fDistances; |
| 189 | bool operator()(const int one, const int two) { |
| 190 | return fDistances[one] < fDistances[two]; |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | /* |
| 195 | check start and end of each contour |
| 196 | if not the same, record them |
| 197 | match them up |
| 198 | connect closest |
| 199 | reassemble contour pieces into new path |
| 200 | */ |
| 201 | void SkPathWriter::assemble() { |
| 202 | #if DEBUG_SHOW_TEST_NAME |
| 203 | SkDebugf("</div>\n"); |
| 204 | #endif |
| 205 | if (!this->someAssemblyRequired()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 206 | return; |
| 207 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 208 | #if DEBUG_PATH_CONSTRUCTION |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 209 | SkDebugf("%s\n", __FUNCTION__); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 210 | #endif |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 211 | SkOpPtT const* const* runs = fEndPtTs.begin(); // starts, ends of partial contours |
| 212 | int endCount = fEndPtTs.count(); // all starts and ends |
| 213 | SkASSERT(endCount > 0); |
| 214 | SkASSERT(endCount == fPartials.count() * 2); |
| 215 | #if DEBUG_ASSEMBLE |
| 216 | for (int index = 0; index < endCount; index += 2) { |
| 217 | const SkOpPtT* eStart = runs[index]; |
| 218 | const SkOpPtT* eEnd = runs[index + 1]; |
| 219 | SkASSERT(eStart != eEnd); |
| 220 | SkASSERT(!eStart->contains(eEnd)); |
| 221 | SkDebugf("%s contour start=(%1.9g,%1.9g) end=(%1.9g,%1.9g)\n", __FUNCTION__, |
| 222 | eStart->fPt.fX, eStart->fPt.fY, eEnd->fPt.fX, eEnd->fPt.fY); |
| 223 | } |
| 224 | #endif |
Cary Clark | cadc506 | 2018-08-06 17:24:04 -0400 | [diff] [blame] | 225 | // lengthen any partial contour adjacent to a simple segment |
| 226 | for (int pIndex = 0; pIndex < endCount; pIndex++) { |
| 227 | SkOpPtT* opPtT = const_cast<SkOpPtT*>(runs[pIndex]); |
| 228 | SkPath dummy; |
| 229 | SkPathWriter partWriter(dummy); |
| 230 | do { |
| 231 | if (!zero_or_one(opPtT->fT)) { |
| 232 | break; |
| 233 | } |
| 234 | SkOpSpanBase* opSpanBase = opPtT->span(); |
| 235 | SkOpSpanBase* start = opPtT->fT ? opSpanBase->prev() : opSpanBase->upCast()->next(); |
| 236 | int step = opPtT->fT ? 1 : -1; |
| 237 | const SkOpSegment* opSegment = opSpanBase->segment(); |
| 238 | const SkOpSegment* nextSegment = opSegment->isSimple(&start, &step); |
| 239 | if (!nextSegment) { |
| 240 | break; |
| 241 | } |
| 242 | SkOpSpanBase* opSpanEnd = start->t() ? start->prev() : start->upCast()->next(); |
| 243 | if (start->starter(opSpanEnd)->alreadyAdded()) { |
| 244 | break; |
| 245 | } |
| 246 | nextSegment->addCurveTo(start, opSpanEnd, &partWriter); |
| 247 | opPtT = opSpanEnd->ptT(); |
| 248 | SkOpPtT** runsPtr = const_cast<SkOpPtT**>(&runs[pIndex]); |
| 249 | *runsPtr = opPtT; |
| 250 | } while (true); |
| 251 | partWriter.finishContour(); |
| 252 | const SkTArray<SkPath>& partPartials = partWriter.partials(); |
| 253 | if (!partPartials.count()) { |
| 254 | continue; |
| 255 | } |
| 256 | // if pIndex is even, reverse and prepend to fPartials; otherwise, append |
| 257 | SkPath& partial = const_cast<SkPath&>(fPartials[pIndex >> 1]); |
| 258 | const SkPath& part = partPartials[0]; |
| 259 | if (pIndex & 1) { |
| 260 | partial.addPath(part, SkPath::kExtend_AddPathMode); |
| 261 | } else { |
| 262 | SkPath reverse; |
| 263 | reverse.reverseAddPath(part); |
| 264 | reverse.addPath(partial, SkPath::kExtend_AddPathMode); |
| 265 | partial = reverse; |
| 266 | } |
| 267 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 268 | SkTDArray<int> sLink, eLink; |
| 269 | int linkCount = endCount / 2; // number of partial contours |
| 270 | sLink.append(linkCount); |
| 271 | eLink.append(linkCount); |
| 272 | int rIndex, iIndex; |
| 273 | for (rIndex = 0; rIndex < linkCount; ++rIndex) { |
| 274 | sLink[rIndex] = eLink[rIndex] = SK_MaxS32; |
| 275 | } |
| 276 | const int entries = endCount * (endCount - 1) / 2; // folded triangle |
| 277 | SkSTArray<8, double, true> distances(entries); |
| 278 | SkSTArray<8, int, true> sortedDist(entries); |
| 279 | SkSTArray<8, int, true> distLookup(entries); |
| 280 | int rRow = 0; |
| 281 | int dIndex = 0; |
| 282 | for (rIndex = 0; rIndex < endCount - 1; ++rIndex) { |
| 283 | const SkOpPtT* oPtT = runs[rIndex]; |
| 284 | for (iIndex = rIndex + 1; iIndex < endCount; ++iIndex) { |
| 285 | const SkOpPtT* iPtT = runs[iIndex]; |
| 286 | double dx = iPtT->fPt.fX - oPtT->fPt.fX; |
| 287 | double dy = iPtT->fPt.fY - oPtT->fPt.fY; |
| 288 | double dist = dx * dx + dy * dy; |
| 289 | distLookup.push_back(rRow + iIndex); |
| 290 | distances.push_back(dist); // oStart distance from iStart |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 291 | sortedDist.push_back(dIndex++); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 292 | } |
| 293 | rRow += endCount; |
| 294 | } |
| 295 | SkASSERT(dIndex == entries); |
| 296 | SkTQSort<int>(sortedDist.begin(), sortedDist.end() - 1, DistanceLessThan(distances.begin())); |
| 297 | int remaining = linkCount; // number of start/end pairs |
| 298 | for (rIndex = 0; rIndex < entries; ++rIndex) { |
| 299 | int pair = sortedDist[rIndex]; |
| 300 | pair = distLookup[pair]; |
| 301 | int row = pair / endCount; |
| 302 | int col = pair - row * endCount; |
| 303 | int ndxOne = row >> 1; |
| 304 | bool endOne = row & 1; |
| 305 | int* linkOne = endOne ? eLink.begin() : sLink.begin(); |
| 306 | if (linkOne[ndxOne] != SK_MaxS32) { |
| 307 | continue; |
| 308 | } |
| 309 | int ndxTwo = col >> 1; |
| 310 | bool endTwo = col & 1; |
| 311 | int* linkTwo = endTwo ? eLink.begin() : sLink.begin(); |
| 312 | if (linkTwo[ndxTwo] != SK_MaxS32) { |
| 313 | continue; |
| 314 | } |
| 315 | SkASSERT(&linkOne[ndxOne] != &linkTwo[ndxTwo]); |
| 316 | bool flip = endOne == endTwo; |
| 317 | linkOne[ndxOne] = flip ? ~ndxTwo : ndxTwo; |
| 318 | linkTwo[ndxTwo] = flip ? ~ndxOne : ndxOne; |
| 319 | if (!--remaining) { |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | SkASSERT(!remaining); |
| 324 | #if DEBUG_ASSEMBLE |
| 325 | for (rIndex = 0; rIndex < linkCount; ++rIndex) { |
| 326 | int s = sLink[rIndex]; |
| 327 | int e = eLink[rIndex]; |
| 328 | SkDebugf("%s %c%d <- s%d - e%d -> %c%d\n", __FUNCTION__, s < 0 ? 's' : 'e', |
| 329 | s < 0 ? ~s : s, rIndex, rIndex, e < 0 ? 'e' : 's', e < 0 ? ~e : e); |
| 330 | } |
| 331 | #endif |
| 332 | rIndex = 0; |
| 333 | do { |
| 334 | bool forward = true; |
| 335 | bool first = true; |
| 336 | int sIndex = sLink[rIndex]; |
| 337 | SkASSERT(sIndex != SK_MaxS32); |
| 338 | sLink[rIndex] = SK_MaxS32; |
| 339 | int eIndex; |
| 340 | if (sIndex < 0) { |
| 341 | eIndex = sLink[~sIndex]; |
| 342 | sLink[~sIndex] = SK_MaxS32; |
| 343 | } else { |
| 344 | eIndex = eLink[sIndex]; |
| 345 | eLink[sIndex] = SK_MaxS32; |
| 346 | } |
| 347 | SkASSERT(eIndex != SK_MaxS32); |
| 348 | #if DEBUG_ASSEMBLE |
| 349 | SkDebugf("%s sIndex=%c%d eIndex=%c%d\n", __FUNCTION__, sIndex < 0 ? 's' : 'e', |
| 350 | sIndex < 0 ? ~sIndex : sIndex, eIndex < 0 ? 's' : 'e', |
| 351 | eIndex < 0 ? ~eIndex : eIndex); |
| 352 | #endif |
| 353 | do { |
| 354 | const SkPath& contour = fPartials[rIndex]; |
Cary Clark | 1d31443 | 2018-07-10 10:57:54 -0400 | [diff] [blame] | 355 | if (!first) { |
| 356 | SkPoint prior, next; |
| 357 | SkAssertResult(fPathPtr->getLastPt(&prior)); |
| 358 | if (forward) { |
| 359 | next = contour.getPoint(0); |
| 360 | } else { |
| 361 | SkAssertResult(contour.getLastPt(&next)); |
| 362 | } |
| 363 | if (prior != next) { |
| 364 | /* TODO: if there is a gap between open path written so far and path to come, |
| 365 | connect by following segments from one to the other, rather than introducing |
| 366 | a diagonal to connect the two. |
| 367 | */ |
| 368 | SkDebugf(""); |
| 369 | } |
| 370 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 371 | if (forward) { |
| 372 | fPathPtr->addPath(contour, |
| 373 | first ? SkPath::kAppend_AddPathMode : SkPath::kExtend_AddPathMode); |
| 374 | } else { |
| 375 | SkASSERT(!first); |
caryclark | 51c5678 | 2016-11-07 05:09:28 -0800 | [diff] [blame] | 376 | fPathPtr->reversePathTo(contour); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 377 | } |
| 378 | if (first) { |
| 379 | first = false; |
| 380 | } |
| 381 | #if DEBUG_ASSEMBLE |
| 382 | SkDebugf("%s rIndex=%d eIndex=%s%d close=%d\n", __FUNCTION__, rIndex, |
| 383 | eIndex < 0 ? "~" : "", eIndex < 0 ? ~eIndex : eIndex, |
| 384 | sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)); |
| 385 | #endif |
| 386 | if (sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)) { |
| 387 | fPathPtr->close(); |
| 388 | break; |
| 389 | } |
| 390 | if (forward) { |
| 391 | eIndex = eLink[rIndex]; |
| 392 | SkASSERT(eIndex != SK_MaxS32); |
| 393 | eLink[rIndex] = SK_MaxS32; |
| 394 | if (eIndex >= 0) { |
| 395 | SkASSERT(sLink[eIndex] == rIndex); |
| 396 | sLink[eIndex] = SK_MaxS32; |
| 397 | } else { |
| 398 | SkASSERT(eLink[~eIndex] == ~rIndex); |
| 399 | eLink[~eIndex] = SK_MaxS32; |
| 400 | } |
| 401 | } else { |
| 402 | eIndex = sLink[rIndex]; |
| 403 | SkASSERT(eIndex != SK_MaxS32); |
| 404 | sLink[rIndex] = SK_MaxS32; |
| 405 | if (eIndex >= 0) { |
| 406 | SkASSERT(eLink[eIndex] == rIndex); |
| 407 | eLink[eIndex] = SK_MaxS32; |
| 408 | } else { |
| 409 | SkASSERT(sLink[~eIndex] == ~rIndex); |
| 410 | sLink[~eIndex] = SK_MaxS32; |
| 411 | } |
| 412 | } |
| 413 | rIndex = eIndex; |
| 414 | if (rIndex < 0) { |
| 415 | forward ^= 1; |
| 416 | rIndex = ~rIndex; |
| 417 | } |
| 418 | } while (true); |
| 419 | for (rIndex = 0; rIndex < linkCount; ++rIndex) { |
| 420 | if (sLink[rIndex] != SK_MaxS32) { |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | } while (rIndex < linkCount); |
| 425 | #if DEBUG_ASSEMBLE |
| 426 | for (rIndex = 0; rIndex < linkCount; ++rIndex) { |
| 427 | SkASSERT(sLink[rIndex] == SK_MaxS32); |
| 428 | SkASSERT(eLink[rIndex] == SK_MaxS32); |
| 429 | } |
| 430 | #endif |
| 431 | return; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 432 | } |