blob: f89c4afcc7cb33557c203923e4732b0e7c3e6bc6 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
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 "SkAddIntersections.h"
8#include "SkOpEdgeBuilder.h"
9#include "SkPathOpsCommon.h"
10#include "SkPathWriter.h"
11
caryclark@google.comd892bd82013-06-17 14:10:36 +000012static bool bridgeWinding(SkTArray<SkOpContour*, true>& contourList, SkPathWriter* simple) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000013 bool firstContour = true;
14 bool unsortable = false;
15 bool topUnsortable = false;
16 SkPoint topLeft = {SK_ScalarMin, SK_ScalarMin};
17 do {
18 int index, endIndex;
19 bool topDone;
20 SkOpSegment* current = FindSortableTop(contourList, &firstContour, &index, &endIndex,
21 &topLeft, &topUnsortable, &topDone, false);
22 if (!current) {
23 if (topUnsortable || !topDone) {
24 topUnsortable = false;
25 SkASSERT(topLeft.fX != SK_ScalarMin && topLeft.fY != SK_ScalarMin);
26 topLeft.fX = topLeft.fY = SK_ScalarMin;
27 continue;
28 }
29 break;
30 }
31 SkTDArray<SkOpSpan*> chaseArray;
32 do {
33 if (current->activeWinding(index, endIndex)) {
34 do {
caryclark@google.com07393ca2013-04-08 11:47:37 +000035 if (!unsortable && current->done()) {
caryclark@google.coma5e55922013-05-07 18:51:31 +000036 #if DEBUG_ACTIVE_SPANS
caryclark@google.com07393ca2013-04-08 11:47:37 +000037 DebugShowActiveSpans(contourList);
caryclark@google.com07393ca2013-04-08 11:47:37 +000038 #endif
caryclark@google.coma5e55922013-05-07 18:51:31 +000039 if (simple->isEmpty()) {
40 simple->init();
41 break;
42 }
43 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000044 SkASSERT(unsortable || !current->done());
45 int nextStart = index;
46 int nextEnd = endIndex;
47 SkOpSegment* next = current->findNextWinding(&chaseArray, &nextStart, &nextEnd,
48 &unsortable);
49 if (!next) {
50 if (!unsortable && simple->hasMove()
51 && current->verb() != SkPath::kLine_Verb
52 && !simple->isClosed()) {
53 current->addCurveTo(index, endIndex, simple, true);
54 SkASSERT(simple->isClosed());
55 }
56 break;
57 }
58 #if DEBUG_FLOW
59 SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__,
60 current->debugID(), current->xyAtT(index).fX, current->xyAtT(index).fY,
61 current->xyAtT(endIndex).fX, current->xyAtT(endIndex).fY);
62 #endif
63 current->addCurveTo(index, endIndex, simple, true);
64 current = next;
65 index = nextStart;
66 endIndex = nextEnd;
67 } while (!simple->isClosed() && (!unsortable
68 || !current->done(SkMin32(index, endIndex))));
69 if (current->activeWinding(index, endIndex) && !simple->isClosed()) {
caryclark@google.coma5e55922013-05-07 18:51:31 +000070 SkASSERT(unsortable || simple->isEmpty());
caryclark@google.com07393ca2013-04-08 11:47:37 +000071 int min = SkMin32(index, endIndex);
72 if (!current->done(min)) {
73 current->addCurveTo(index, endIndex, simple, true);
74 current->markDoneUnary(min);
75 }
76 }
77 simple->close();
78 } else {
79 SkOpSpan* last = current->markAndChaseDoneUnary(index, endIndex);
80 if (last && !last->fLoop) {
81 *chaseArray.append() = last;
82 }
83 }
84 current = FindChase(chaseArray, index, endIndex);
85 #if DEBUG_ACTIVE_SPANS
86 DebugShowActiveSpans(contourList);
87 #endif
88 if (!current) {
89 break;
90 }
91 } while (true);
92 } while (true);
93 return simple->someAssemblyRequired();
94}
95
96// returns true if all edges were processed
caryclark@google.comd892bd82013-06-17 14:10:36 +000097static bool bridgeXor(SkTArray<SkOpContour*, true>& contourList, SkPathWriter* simple) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000098 SkOpSegment* current;
99 int start, end;
100 bool unsortable = false;
101 bool closable = true;
102 while ((current = FindUndone(contourList, &start, &end))) {
103 do {
104 #if DEBUG_ACTIVE_SPANS
105 if (!unsortable && current->done()) {
106 DebugShowActiveSpans(contourList);
107 }
108 #endif
109 SkASSERT(unsortable || !current->done());
110 int nextStart = start;
111 int nextEnd = end;
112 SkOpSegment* next = current->findNextXor(&nextStart, &nextEnd, &unsortable);
113 if (!next) {
114 if (!unsortable && simple->hasMove()
115 && current->verb() != SkPath::kLine_Verb
116 && !simple->isClosed()) {
117 current->addCurveTo(start, end, simple, true);
118 SkASSERT(simple->isClosed());
119 }
120 break;
121 }
122 #if DEBUG_FLOW
123 SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__,
124 current->debugID(), current->xyAtT(start).fX, current->xyAtT(start).fY,
125 current->xyAtT(end).fX, current->xyAtT(end).fY);
126 #endif
127 current->addCurveTo(start, end, simple, true);
128 current = next;
129 start = nextStart;
130 end = nextEnd;
131 } while (!simple->isClosed() && (!unsortable || !current->done(SkMin32(start, end))));
132 if (!simple->isClosed()) {
133 SkASSERT(unsortable);
134 int min = SkMin32(start, end);
135 if (!current->done(min)) {
136 current->addCurveTo(start, end, simple, true);
137 current->markDone(min, 1);
138 }
139 closable = false;
140 }
141 simple->close();
142 #if DEBUG_ACTIVE_SPANS
143 DebugShowActiveSpans(contourList);
144 #endif
145 }
146 return closable;
147}
148
149// FIXME : add this as a member of SkPath
caryclark@google.com66560ca2013-04-26 19:51:16 +0000150bool Simplify(const SkPath& path, SkPath* result) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000151#if DEBUG_SORT || DEBUG_SWAP_TOP
152 gDebugSortCount = gDebugSortCountDefault;
153#endif
154 // returns 1 for evenodd, -1 for winding, regardless of inverse-ness
caryclark@google.com7dfbb072013-04-22 14:37:05 +0000155 SkPath::FillType fillType = path.isInverseFillType() ? SkPath::kInverseEvenOdd_FillType
156 : SkPath::kEvenOdd_FillType;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000157
158 // turn path into list of segments
159 SkTArray<SkOpContour> contours;
160 SkOpEdgeBuilder builder(path, contours);
caryclark@google.com66560ca2013-04-26 19:51:16 +0000161 if (!builder.finish()) {
162 return false;
163 }
caryclark@google.comd892bd82013-06-17 14:10:36 +0000164 SkTArray<SkOpContour*, true> contourList;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000165 MakeContourList(contours, contourList, false, false);
166 SkOpContour** currentPtr = contourList.begin();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000167 result->setFillType(fillType);
168 result->reset();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000169 if (!currentPtr) {
caryclark@google.com66560ca2013-04-26 19:51:16 +0000170 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000171 }
172 SkOpContour** listEnd = contourList.end();
173 // find all intersections between segments
174 do {
175 SkOpContour** nextPtr = currentPtr;
176 SkOpContour* current = *currentPtr++;
177 if (current->containsCubics()) {
178 AddSelfIntersectTs(current);
179 }
180 SkOpContour* next;
181 do {
182 next = *nextPtr++;
183 } while (AddIntersectTs(current, next) && nextPtr != listEnd);
184 } while (currentPtr != listEnd);
185 // eat through coincident edges
186 CoincidenceCheck(&contourList, 0);
187 FixOtherTIndex(&contourList);
188 SortSegments(&contourList);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000189#if DEBUG_ACTIVE_SPANS || DEBUG_ACTIVE_SPANS_FIRST_ONLY
caryclark@google.com07393ca2013-04-08 11:47:37 +0000190 DebugShowActiveSpans(contourList);
191#endif
192 // construct closed contours
caryclark@google.com66560ca2013-04-26 19:51:16 +0000193 SkPathWriter simple(*result);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000194 if (builder.xorMask() == kWinding_PathOpsMask ? bridgeWinding(contourList, &simple)
195 : !bridgeXor(contourList, &simple))
196 { // if some edges could not be resolved, assemble remaining fragments
197 SkPath temp;
caryclark@google.com7dfbb072013-04-22 14:37:05 +0000198 temp.setFillType(fillType);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000199 SkPathWriter assembled(temp);
200 Assemble(simple, &assembled);
201 *result = *assembled.nativePath();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000202 result->setFillType(fillType);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000203 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000204 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000205}