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