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