// returns true if all edges were processed static bool bridgeXor(SkTDArray<SkOpContour*>& contourList, SkPathWriter* simple) { SkOpSegment* current; int start, end; bool unsortable = false; bool closable = true; while ((current = FindUndone(contourList, &start, &end))) { do { #if DEBUG_ACTIVE_SPANS if (!unsortable && current->done()) { DebugShowActiveSpans(contourList); } #endif SkASSERT(unsortable || !current->done()); int nextStart = start; int nextEnd = end; SkOpSegment* next = current->findNextXor(&nextStart, &nextEnd, &unsortable); if (!next) { if (!unsortable && simple->hasMove() && current->verb() != SkPath::kLine_Verb && !simple->isClosed()) { current->addCurveTo(start, end, simple, true); SkASSERT(simple->isClosed()); } break; } #if DEBUG_FLOW SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, current->debugID(), current->xyAtT(start).fX, current->xyAtT(start).fY, current->xyAtT(end).fX, current->xyAtT(end).fY); #endif current->addCurveTo(start, end, simple, true); current = next; start = nextStart; end = nextEnd; } while (!simple->isClosed() && (!unsortable || !current->done(SkMin32(start, end)))); if (!simple->isClosed()) { SkASSERT(unsortable); int min = SkMin32(start, end); if (!current->done(min)) { current->addCurveTo(start, end, simple, true); current->markDone(min, 1); } closable = false; } simple->close(); #if DEBUG_ACTIVE_SPANS DebugShowActiveSpans(contourList); #endif } return closable; }
bool HandleCoincidence(SkTArray<SkOpContour*, true>* contourList, int total) { #if DEBUG_SHOW_WINDING SkOpContour::debugShowWindingValues(contourList); #endif CoincidenceCheck(contourList, total); #if DEBUG_SHOW_WINDING SkOpContour::debugShowWindingValues(contourList); #endif fixOtherTIndex(contourList); checkEnds(contourList); // check if connecting curve intersected at the same end bool hasM = checkMultiples(contourList); // check if intersections agree on t and point values SkTDArray<SkOpSegment::AlignedSpan> aligned; if (hasM) { alignMultiples(contourList, &aligned); // align pairs of identical points alignCoincidence(contourList, aligned); } checkDuplicates(contourList); // check if spans have the same number on the other end checkTiny(contourList); // if pair have the same end points, mark them as parallel checkSmall(contourList); // a pair of curves with a small span may turn into coincident lines joinCoincidence(contourList); // join curves that connect to a coincident pair sortSegments(contourList); if (!calcAngles(contourList)) { return false; } sortAngles(contourList); #if DEBUG_ACTIVE_SPANS || DEBUG_ACTIVE_SPANS_FIRST_ONLY DebugShowActiveSpans(*contourList); #endif return true; }
bool HandleCoincidence(SkOpContourHead* contourList, SkOpCoincidence* coincidence, SkChunkAlloc* allocator) { SkOpGlobalState* globalState = contourList->globalState(); // combine t values when multiple intersections occur on some segments but not others moveMultiples(contourList); findCollapsed(contourList); // move t values and points together to eliminate small/tiny gaps moveNearby(contourList); align(contourList); // give all span members common values coincidence->fixAligned(); // aligning may have marked a coincidence pt-t deleted #if DEBUG_VALIDATE globalState->setPhase(SkOpGlobalState::kIntersecting); #endif // look for intersections on line segments formed by moving end points addAlignIntersections(contourList, allocator); coincidence->addMissing(allocator); #if DEBUG_VALIDATE globalState->setPhase(SkOpGlobalState::kWalking); #endif // check to see if, loosely, coincident ranges may be expanded if (coincidence->expand()) { coincidence->addExpanded(allocator PATH_OPS_DEBUG_VALIDATE_PARAMS(globalState)); } // the expanded ranges may not align -- add the missing spans coincidence->mark(); // mark spans of coincident segments as coincident // look for coincidence missed earlier if (missingCoincidence(contourList, coincidence, allocator)) { (void) coincidence->expand(); coincidence->addExpanded(allocator PATH_OPS_DEBUG_VALIDATE_PARAMS(globalState)); coincidence->mark(); } SkOpCoincidence overlaps; do { SkOpCoincidence* pairs = overlaps.isEmpty() ? coincidence : &overlaps; if (!pairs->apply()) { // adjust the winding value to account for coincident edges return false; } // For each coincident pair that overlaps another, when the receivers (the 1st of the pair) // are different, construct a new pair to resolve their mutual span pairs->findOverlaps(&overlaps, allocator); } while (!overlaps.isEmpty()); calcAngles(contourList, allocator); sortAngles(contourList); if (globalState->angleCoincidence()) { (void) missingCoincidence(contourList, coincidence, allocator); if (!coincidence->apply()) { return false; } } #if DEBUG_ACTIVE_SPANS coincidence->debugShowCoincidence(); DebugShowActiveSpans(contourList); #endif return true; }
// FIXME : add this as a member of SkPath void Simplify(const SkPath& path, SkPath* result) { #if DEBUG_SORT || DEBUG_SWAP_TOP gDebugSortCount = gDebugSortCountDefault; #endif // returns 1 for evenodd, -1 for winding, regardless of inverse-ness result->reset(); result->setFillType(SkPath::kEvenOdd_FillType); SkPathWriter simple(*result); // turn path into list of segments SkTArray<SkOpContour> contours; SkOpEdgeBuilder builder(path, contours); builder.finish(); SkTDArray<SkOpContour*> contourList; MakeContourList(contours, contourList, false, false); SkOpContour** currentPtr = contourList.begin(); if (!currentPtr) { return; } SkOpContour** listEnd = contourList.end(); // find all intersections between segments do { SkOpContour** nextPtr = currentPtr; SkOpContour* current = *currentPtr++; if (current->containsCubics()) { AddSelfIntersectTs(current); } SkOpContour* next; do { next = *nextPtr++; } while (AddIntersectTs(current, next) && nextPtr != listEnd); } while (currentPtr != listEnd); // eat through coincident edges CoincidenceCheck(&contourList, 0); FixOtherTIndex(&contourList); SortSegments(&contourList); #if DEBUG_ACTIVE_SPANS DebugShowActiveSpans(contourList); #endif // construct closed contours if (builder.xorMask() == kWinding_PathOpsMask ? bridgeWinding(contourList, &simple) : !bridgeXor(contourList, &simple)) { // if some edges could not be resolved, assemble remaining fragments SkPath temp; temp.setFillType(SkPath::kEvenOdd_FillType); SkPathWriter assembled(temp); Assemble(simple, &assembled); *result = *assembled.nativePath(); } }
static bool bridgeOp(SkTArray<SkOpContour*, true>& contourList, const SkPathOp op, const int xorMask, const int xorOpMask, SkPathWriter* simple) { bool firstContour = true; bool unsortable = false; bool topUnsortable = false; SkPoint topLeft = {SK_ScalarMin, SK_ScalarMin}; do { int index, endIndex; bool done; SkOpSegment* current = FindSortableTop(contourList, SkOpAngle::kBinarySingle, &firstContour, &index, &endIndex, &topLeft, &topUnsortable, &done); if (!current) { if (topUnsortable || !done) { topUnsortable = false; SkASSERT(topLeft.fX != SK_ScalarMin && topLeft.fY != SK_ScalarMin); topLeft.fX = topLeft.fY = SK_ScalarMin; continue; } break; } SkTDArray<SkOpSpan*> chaseArray; do { if (current->activeOp(index, endIndex, xorMask, xorOpMask, op)) { do { if (!unsortable && current->done()) { #if DEBUG_ACTIVE_SPANS DebugShowActiveSpans(contourList); #endif if (simple->isEmpty()) { simple->init(); } break; } SkASSERT(unsortable || !current->done()); int nextStart = index; int nextEnd = endIndex; SkOpSegment* next = current->findNextOp(&chaseArray, &nextStart, &nextEnd, &unsortable, op, xorMask, xorOpMask); if (!next) { if (!unsortable && simple->hasMove() && current->verb() != SkPath::kLine_Verb && !simple->isClosed()) { current->addCurveTo(index, endIndex, simple, true); SkASSERT(simple->isClosed()); } break; } #if DEBUG_FLOW SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, current->debugID(), current->xyAtT(index).fX, current->xyAtT(index).fY, current->xyAtT(endIndex).fX, current->xyAtT(endIndex).fY); #endif current->addCurveTo(index, endIndex, simple, true); current = next; index = nextStart; endIndex = nextEnd; } while (!simple->isClosed() && (!unsortable || !current->done(SkMin32(index, endIndex)))); if (current->activeWinding(index, endIndex) && !simple->isClosed()) { // FIXME : add to simplify, xor cpaths int min = SkMin32(index, endIndex); if (!unsortable && !simple->isEmpty()) { unsortable = current->checkSmall(min); } SkASSERT(unsortable || simple->isEmpty()); if (!current->done(min)) { current->addCurveTo(index, endIndex, simple, true); current->markDoneBinary(min); } } simple->close(); } else { SkOpSpan* last = current->markAndChaseDoneBinary(index, endIndex); if (last && !last->fLoop) { *chaseArray.append() = last; } } current = findChaseOp(chaseArray, index, endIndex); #if DEBUG_ACTIVE_SPANS DebugShowActiveSpans(contourList); #endif if (!current) { break; } } while (true); } while (true); return simple->someAssemblyRequired(); }
static bool bridgeOp(SkTArray<SkOpContour*, true>& contourList, const SkPathOp op, const int xorMask, const int xorOpMask, SkPathWriter* simple) { bool firstContour = true; bool unsortable = false; bool topUnsortable = false; bool firstPass = true; SkPoint lastTopLeft; SkPoint topLeft = {SK_ScalarMin, SK_ScalarMin}; do { int index, endIndex; bool topDone; bool onlyVertical = false; lastTopLeft = topLeft; SkOpSegment* current = FindSortableTop(contourList, SkOpAngle::kBinarySingle, &firstContour, &index, &endIndex, &topLeft, &topUnsortable, &topDone, &onlyVertical, firstPass); if (!current) { if ((!topUnsortable || firstPass) && !topDone) { SkASSERT(topLeft.fX != SK_ScalarMin && topLeft.fY != SK_ScalarMin); if (lastTopLeft.fX == SK_ScalarMin && lastTopLeft.fY == SK_ScalarMin) { if (firstPass) { firstPass = false; } else { break; } } topLeft.fX = topLeft.fY = SK_ScalarMin; continue; } break; } else if (onlyVertical) { break; } firstPass = !topUnsortable || lastTopLeft != topLeft; SkTDArray<SkOpSpan*> chase; do { if (current->activeOp(index, endIndex, xorMask, xorOpMask, op)) { do { if (!unsortable && current->done()) { break; } SkASSERT(unsortable || !current->done()); int nextStart = index; int nextEnd = endIndex; SkOpSegment* next = current->findNextOp(&chase, &nextStart, &nextEnd, &unsortable, op, xorMask, xorOpMask); if (!next) { if (!unsortable && simple->hasMove() && current->verb() != SkPath::kLine_Verb && !simple->isClosed()) { current->addCurveTo(index, endIndex, simple, true); #if DEBUG_ACTIVE_SPANS if (!simple->isClosed()) { DebugShowActiveSpans(contourList); } #endif // SkASSERT(simple->isClosed()); } break; } #if DEBUG_FLOW SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, current->debugID(), current->xyAtT(index).fX, current->xyAtT(index).fY, current->xyAtT(endIndex).fX, current->xyAtT(endIndex).fY); #endif current->addCurveTo(index, endIndex, simple, true); current = next; index = nextStart; endIndex = nextEnd; } while (!simple->isClosed() && (!unsortable || !current->done(SkMin32(index, endIndex)))); if (current->activeWinding(index, endIndex) && !simple->isClosed()) { // FIXME : add to simplify, xor cpaths int min = SkMin32(index, endIndex); if (!unsortable && !simple->isEmpty()) { unsortable = current->checkSmall(min); } if (!current->done(min)) { current->addCurveTo(index, endIndex, simple, true); current->markDoneBinary(min); } } simple->close(); } else { SkOpSpan* last = current->markAndChaseDoneBinary(index, endIndex); if (last && !last->fChased && !last->fLoop) { last->fChased = true; SkASSERT(!SkPathOpsDebug::ChaseContains(chase, last)); *chase.append() = last; #if DEBUG_WINDING SkDebugf("%s chase.append id=%d windSum=%d small=%d\n", __FUNCTION__, last->fOther->span(last->fOtherIndex).fOther->debugID(), last->fWindSum, last->fSmall); #endif } } current = findChaseOp(chase, &index, &endIndex); #if DEBUG_ACTIVE_SPANS DebugShowActiveSpans(contourList); #endif if (!current) { break; } } while (true); } while (true); return simple->someAssemblyRequired(); }
bool Op(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result) { #if DEBUG_SHOW_TEST_NAME char* debugName = DEBUG_FILENAME_STRING; if (debugName && debugName[0]) { SkPathOpsDebug::BumpTestName(debugName); SkPathOpsDebug::ShowPath(one, two, op, debugName); } #endif op = gOpInverse[op][one.isInverseFillType()][two.isInverseFillType()]; SkPath::FillType fillType = gOutInverse[op][one.isInverseFillType()][two.isInverseFillType()] ? SkPath::kInverseEvenOdd_FillType : SkPath::kEvenOdd_FillType; const SkPath* minuend = &one; const SkPath* subtrahend = &two; if (op == kReverseDifference_PathOp) { minuend = &two; subtrahend = &one; op = kDifference_PathOp; } #if DEBUG_SORT || DEBUG_SWAP_TOP SkPathOpsDebug::gSortCount = SkPathOpsDebug::gSortCountDefault; #endif // turn path into list of segments SkTArray<SkOpContour> contours; // FIXME: add self-intersecting cubics' T values to segment SkOpEdgeBuilder builder(*minuend, contours); const int xorMask = builder.xorMask(); builder.addOperand(*subtrahend); if (!builder.finish()) { return false; } result->reset(); result->setFillType(fillType); const int xorOpMask = builder.xorMask(); SkTArray<SkOpContour*, true> contourList; MakeContourList(contours, contourList, xorMask == kEvenOdd_PathOpsMask, xorOpMask == kEvenOdd_PathOpsMask); SkOpContour** currentPtr = contourList.begin(); if (!currentPtr) { return true; } SkOpContour** listEnd = contourList.end(); // find all intersections between segments do { SkOpContour** nextPtr = currentPtr; SkOpContour* current = *currentPtr++; if (current->containsCubics()) { AddSelfIntersectTs(current); } SkOpContour* next; do { next = *nextPtr++; } while (AddIntersectTs(current, next) && nextPtr != listEnd); } while (currentPtr != listEnd); // eat through coincident edges int total = 0; int index; for (index = 0; index < contourList.count(); ++index) { total += contourList[index]->segments().count(); } #if DEBUG_SHOW_WINDING SkOpContour::debugShowWindingValues(contourList); #endif CoincidenceCheck(&contourList, total); #if DEBUG_SHOW_WINDING SkOpContour::debugShowWindingValues(contourList); #endif FixOtherTIndex(&contourList); CheckEnds(&contourList); CheckTiny(&contourList); SortSegments(&contourList); #if DEBUG_ACTIVE_SPANS || DEBUG_ACTIVE_SPANS_FIRST_ONLY DebugShowActiveSpans(contourList); #endif // construct closed contours SkPathWriter wrapper(*result); bridgeOp(contourList, op, xorMask, xorOpMask, &wrapper); { // if some edges could not be resolved, assemble remaining fragments SkPath temp; temp.setFillType(fillType); SkPathWriter assembled(temp); Assemble(wrapper, &assembled); *result = *assembled.nativePath(); result->setFillType(fillType); } return true; }