OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "SkOpEdgeBuilder.h" | 7 #include "SkOpEdgeBuilder.h" |
8 #include "SkPathOpsCommon.h" | 8 #include "SkPathOpsCommon.h" |
9 | 9 |
10 bool TightBounds(const SkPath& path, SkRect* result) { | 10 bool TightBounds(const SkPath& path, SkRect* result) { |
| 11 SkPath::RawIter iter(path); |
| 12 SkRect moveBounds = { SK_ScalarMax, SK_ScalarMax, SK_ScalarMin, SK_ScalarMin
}; |
| 13 bool wellBehaved = true; |
| 14 SkPath::Verb verb; |
| 15 do { |
| 16 SkPoint pts[4]; |
| 17 verb = iter.next(pts); |
| 18 switch (verb) { |
| 19 case SkPath::kMove_Verb: |
| 20 moveBounds.fLeft = SkTMin(moveBounds.fLeft, pts[0].fX); |
| 21 moveBounds.fTop = SkTMin(moveBounds.fTop, pts[0].fY); |
| 22 moveBounds.fRight = SkTMax(moveBounds.fRight, pts[0].fX); |
| 23 moveBounds.fBottom = SkTMax(moveBounds.fBottom, pts[0].fY); |
| 24 break; |
| 25 case SkPath::kQuad_Verb: |
| 26 case SkPath::kConic_Verb: |
| 27 if (!wellBehaved) { |
| 28 break; |
| 29 } |
| 30 wellBehaved &= between(pts[0].fX, pts[1].fX, pts[2].fX); |
| 31 wellBehaved &= between(pts[0].fY, pts[1].fY, pts[2].fY); |
| 32 break; |
| 33 case SkPath::kCubic_Verb: |
| 34 if (!wellBehaved) { |
| 35 break; |
| 36 } |
| 37 wellBehaved &= between(pts[0].fX, pts[1].fX, pts[3].fX); |
| 38 wellBehaved &= between(pts[0].fY, pts[1].fY, pts[3].fY); |
| 39 wellBehaved &= between(pts[0].fX, pts[2].fX, pts[3].fX); |
| 40 wellBehaved &= between(pts[0].fY, pts[2].fY, pts[3].fY); |
| 41 break; |
| 42 default: |
| 43 break; |
| 44 } |
| 45 } while (verb != SkPath::kDone_Verb); |
| 46 if (wellBehaved) { |
| 47 *result = path.getBounds(); |
| 48 return true; |
| 49 } |
11 SkChunkAlloc allocator(4096); // FIXME: constant-ize, tune | 50 SkChunkAlloc allocator(4096); // FIXME: constant-ize, tune |
12 SkOpContour contour; | 51 SkOpContour contour; |
13 SkOpContourHead* contourList = static_cast<SkOpContourHead*>(&contour); | 52 SkOpContourHead* contourList = static_cast<SkOpContourHead*>(&contour); |
14 SkOpGlobalState globalState(contourList, &allocator SkDEBUGPARAMS(false) | 53 SkOpGlobalState globalState(contourList, &allocator SkDEBUGPARAMS(false) |
15 SkDEBUGPARAMS(nullptr)); | 54 SkDEBUGPARAMS(nullptr)); |
16 // turn path into list of segments | 55 // turn path into list of segments |
17 SkScalar scaleFactor = ScaleFactor(path); | 56 SkScalar scaleFactor = ScaleFactor(path); |
18 SkPath scaledPath; | 57 SkPath scaledPath; |
19 const SkPath* workingPath; | 58 const SkPath* workingPath; |
20 if (scaleFactor > SK_Scalar1) { | 59 if (scaleFactor > SK_Scalar1) { |
21 ScalePath(path, 1.f / scaleFactor, &scaledPath); | 60 ScalePath(path, 1.f / scaleFactor, &scaledPath); |
22 workingPath = &scaledPath; | 61 workingPath = &scaledPath; |
23 } else { | 62 } else { |
24 workingPath = &path; | 63 workingPath = &path; |
25 } | 64 } |
26 SkOpEdgeBuilder builder(*workingPath, contourList, &globalState); | 65 SkOpEdgeBuilder builder(*workingPath, contourList, &globalState); |
27 if (!builder.finish()) { | 66 if (!builder.finish()) { |
28 return false; | 67 return false; |
29 } | 68 } |
30 if (!SortContourList(&contourList, false, false)) { | 69 if (!SortContourList(&contourList, false, false)) { |
31 result->setEmpty(); | 70 *result = moveBounds; |
32 return true; | 71 return true; |
33 } | 72 } |
34 SkOpContour* current = contourList; | 73 SkOpContour* current = contourList; |
35 SkPathOpsBounds bounds = current->bounds(); | 74 SkPathOpsBounds bounds = current->bounds(); |
36 while ((current = current->next())) { | 75 while ((current = current->next())) { |
37 bounds.add(current->bounds()); | 76 bounds.add(current->bounds()); |
38 } | 77 } |
39 *result = bounds; | 78 *result = bounds; |
| 79 if (!moveBounds.isEmpty()) { |
| 80 result->join(moveBounds); |
| 81 } |
40 return true; | 82 return true; |
41 } | 83 } |
OLD | NEW |