blob: 8b62a6a15016a04d759cf4062ee8825c908ef5d1 [file] [log] [blame]
caryclarka8d2ffb2014-06-24 07:55:11 -07001/*
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
10bool TightBounds(const SkPath& path, SkRect* result) {
caryclark61c21cd2016-10-05 13:23:00 -070011 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 }
Florin Malita14a64302017-05-24 14:53:44 -040050 SkSTArenaAlloc<4096> allocator; // FIXME: constant-ize, tune
caryclark54359292015-03-26 07:52:43 -070051 SkOpContour contour;
caryclark624637c2015-05-11 07:21:27 -070052 SkOpContourHead* contourList = static_cast<SkOpContourHead*>(&contour);
caryclark55888e42016-07-18 10:01:36 -070053 SkOpGlobalState globalState(contourList, &allocator SkDEBUGPARAMS(false)
caryclarkdae6b972016-06-08 04:28:19 -070054 SkDEBUGPARAMS(nullptr));
caryclarka8d2ffb2014-06-24 07:55:11 -070055 // turn path into list of segments
caryclark55888e42016-07-18 10:01:36 -070056 SkScalar scaleFactor = ScaleFactor(path);
57 SkPath scaledPath;
58 const SkPath* workingPath;
59 if (scaleFactor > SK_Scalar1) {
60 ScalePath(path, 1.f / scaleFactor, &scaledPath);
61 workingPath = &scaledPath;
62 } else {
63 workingPath = &path;
64 }
caryclarkeed356d2016-09-14 07:18:20 -070065 SkOpEdgeBuilder builder(*workingPath, contourList, &globalState);
caryclark55888e42016-07-18 10:01:36 -070066 if (!builder.finish()) {
caryclarka8d2ffb2014-06-24 07:55:11 -070067 return false;
68 }
caryclark624637c2015-05-11 07:21:27 -070069 if (!SortContourList(&contourList, false, false)) {
caryclark61c21cd2016-10-05 13:23:00 -070070 *result = moveBounds;
caryclarka8d2ffb2014-06-24 07:55:11 -070071 return true;
72 }
caryclark624637c2015-05-11 07:21:27 -070073 SkOpContour* current = contourList;
caryclarka8d2ffb2014-06-24 07:55:11 -070074 SkPathOpsBounds bounds = current->bounds();
caryclark624637c2015-05-11 07:21:27 -070075 while ((current = current->next())) {
caryclarka8d2ffb2014-06-24 07:55:11 -070076 bounds.add(current->bounds());
77 }
Fredrik Söderquistb60e8642017-01-04 13:31:47 +010078 if (scaleFactor > SK_Scalar1) {
79 bounds.set(bounds.left() * scaleFactor, bounds.top() * scaleFactor,
80 bounds.right() * scaleFactor, bounds.bottom() * scaleFactor);
81 }
caryclarka8d2ffb2014-06-24 07:55:11 -070082 *result = bounds;
caryclark61c21cd2016-10-05 13:23:00 -070083 if (!moveBounds.isEmpty()) {
84 result->join(moveBounds);
85 }
caryclarka8d2ffb2014-06-24 07:55:11 -070086 return true;
87}