bool TightBounds(const SkPath& path, SkRect* result) { SkChunkAlloc allocator(4096); // FIXME: constant-ize, tune SkOpContour contour; SkOpContourHead* contourList = static_cast<SkOpContourHead*>(&contour); SkOpGlobalState globalState(contourList, &allocator SkDEBUGPARAMS(false) SkDEBUGPARAMS(nullptr)); // turn path into list of segments SkScalar scaleFactor = ScaleFactor(path); SkPath scaledPath; const SkPath* workingPath; if (scaleFactor > SK_Scalar1) { ScalePath(path, 1.f / scaleFactor, &scaledPath); workingPath = &scaledPath; } else { workingPath = &path; } SkOpEdgeBuilder builder(*workingPath, &contour, &globalState); if (!builder.finish()) { return false; } if (!SortContourList(&contourList, false, false)) { result->setEmpty(); return true; } SkOpContour* current = contourList; SkPathOpsBounds bounds = current->bounds(); while ((current = current->next())) { bounds.add(current->bounds()); } *result = bounds; return true; }
bool TightBounds(const SkPath& path, SkRect* result) { SkChunkAlloc allocator(4096); // FIXME: constant-ize, tune SkOpContour contour; SkOpContourHead* contourList = static_cast<SkOpContourHead*>(&contour); SkOpGlobalState globalState(nullptr, contourList SkDEBUGPARAMS(nullptr)); // turn path into list of segments SkOpEdgeBuilder builder(path, &contour, &allocator, &globalState); if (!builder.finish(&allocator)) { return false; } if (!SortContourList(&contourList, false, false)) { result->setEmpty(); return true; } SkOpContour* current = contourList; SkPathOpsBounds bounds = current->bounds(); while ((current = current->next())) { bounds.add(current->bounds()); } *result = bounds; return true; }
DEF_TEST(PathOpsBounds, reporter) { for (size_t index = 0; index < sectTestsCount; ++index) { const SkPathOpsBounds& bounds1 = static_cast<const SkPathOpsBounds&>(sectTests[index][0]); SkASSERT(ValidBounds(bounds1)); const SkPathOpsBounds& bounds2 = static_cast<const SkPathOpsBounds&>(sectTests[index][1]); SkASSERT(ValidBounds(bounds2)); bool touches = SkPathOpsBounds::Intersects(bounds1, bounds2); REPORTER_ASSERT(reporter, touches); } for (size_t index = 0; index < noSectTestsCount; ++index) { const SkPathOpsBounds& bounds1 = static_cast<const SkPathOpsBounds&>(noSectTests[index][0]); SkASSERT(ValidBounds(bounds1)); const SkPathOpsBounds& bounds2 = static_cast<const SkPathOpsBounds&>(noSectTests[index][1]); SkASSERT(ValidBounds(bounds2)); bool touches = SkPathOpsBounds::Intersects(bounds1, bounds2); REPORTER_ASSERT(reporter, !touches); } SkPathOpsBounds bounds; bounds.setEmpty(); bounds.add(1, 2, 3, 4); SkPathOpsBounds expected; expected.set(0, 0, 3, 4); REPORTER_ASSERT(reporter, bounds == expected); bounds.setEmpty(); SkPathOpsBounds ordinal; ordinal.set(1, 2, 3, 4); bounds.add(ordinal); REPORTER_ASSERT(reporter, bounds == expected); SkPoint topLeft = {0, 0}; bounds.setPointBounds(topLeft); SkPoint botRight = {3, 4}; bounds.add(botRight); REPORTER_ASSERT(reporter, bounds == expected); for (size_t index = 0; index < emptyTestsCount; ++index) { const SkPathOpsBounds& bounds = static_cast<const SkPathOpsBounds&>(reallyEmpty[index]); // SkASSERT(ValidBounds(bounds)); // don't check because test may contain nan bool empty = bounds.isReallyEmpty(); REPORTER_ASSERT(reporter, empty); } for (size_t index = 0; index < notEmptyTestsCount; ++index) { const SkPathOpsBounds& bounds = static_cast<const SkPathOpsBounds&>(notReallyEmpty[index]); SkASSERT(ValidBounds(bounds)); bool empty = bounds.isReallyEmpty(); REPORTER_ASSERT(reporter, !empty); } const SkPoint curvePts[] = {{0, 0}, {1, 2}, {3, 4}, {5, 6}}; bounds.setLineBounds(curvePts, 1); expected.set(0, 0, 1, 2); REPORTER_ASSERT(reporter, bounds == expected); (bounds.*SetCurveBounds[SkPath::kLine_Verb])(curvePts, 1); REPORTER_ASSERT(reporter, bounds == expected); bounds.setQuadBounds(curvePts, 1); expected.set(0, 0, 3, 4); REPORTER_ASSERT(reporter, bounds == expected); (bounds.*SetCurveBounds[SkPath::kQuad_Verb])(curvePts, 1); REPORTER_ASSERT(reporter, bounds == expected); bounds.setCubicBounds(curvePts, 1); expected.set(0, 0, 5, 6); REPORTER_ASSERT(reporter, bounds == expected); (bounds.*SetCurveBounds[SkPath::kCubic_Verb])(curvePts, 1); REPORTER_ASSERT(reporter, bounds == expected); }