caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "src/pathops/SkOpEdgeBuilder.h" |
| 8 | #include "src/pathops/SkPathOpsCommon.h" |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 9 | |
| 10 | bool TightBounds(const SkPath& path, SkRect* result) { |
caryclark | 61c21cd | 2016-10-05 13:23:00 -0700 | [diff] [blame] | 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 | } |
Florin Malita | 14a6430 | 2017-05-24 14:53:44 -0400 | [diff] [blame] | 50 | SkSTArenaAlloc<4096> allocator; // FIXME: constant-ize, tune |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 51 | SkOpContour contour; |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 52 | SkOpContourHead* contourList = static_cast<SkOpContourHead*>(&contour); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 53 | SkOpGlobalState globalState(contourList, &allocator SkDEBUGPARAMS(false) |
caryclark | dae6b97 | 2016-06-08 04:28:19 -0700 | [diff] [blame] | 54 | SkDEBUGPARAMS(nullptr)); |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 55 | // turn path into list of segments |
Cary Clark | 5de5233 | 2018-08-30 12:58:23 -0400 | [diff] [blame] | 56 | SkOpEdgeBuilder builder(path, contourList, &globalState); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 57 | if (!builder.finish()) { |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 58 | return false; |
| 59 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 60 | if (!SortContourList(&contourList, false, false)) { |
caryclark | 61c21cd | 2016-10-05 13:23:00 -0700 | [diff] [blame] | 61 | *result = moveBounds; |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 62 | return true; |
| 63 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 64 | SkOpContour* current = contourList; |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 65 | SkPathOpsBounds bounds = current->bounds(); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 66 | while ((current = current->next())) { |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 67 | bounds.add(current->bounds()); |
| 68 | } |
| 69 | *result = bounds; |
caryclark | 61c21cd | 2016-10-05 13:23:00 -0700 | [diff] [blame] | 70 | if (!moveBounds.isEmpty()) { |
| 71 | result->join(moveBounds); |
| 72 | } |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 73 | return true; |
| 74 | } |