blob: 54ef7671d67d63875036e405d090936b42f454f8 [file] [log] [blame]
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkCanvas.h"
#include "include/core/SkFont.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPathEffect.h"
#include "include/core/SkRRect.h"
#include "include/core/SkSize.h"
#include "include/core/SkStream.h"
#include "include/core/SkStrokeRec.h"
#include "include/core/SkSurface.h"
#include "include/private/SkTo.h"
#include "include/utils/SkNullCanvas.h"
#include "include/utils/SkParse.h"
#include "include/utils/SkParsePath.h"
#include "include/utils/SkRandom.h"
#include "src/core/SkAutoMalloc.h"
#include "src/core/SkGeometry.h"
#include "src/core/SkPathPriv.h"
#include "src/core/SkReader32.h"
#include "src/core/SkWriter32.h"
#include "tests/Test.h"
#include <cmath>
#include <utility>
#include <vector>
static void set_radii(SkVector radii[4], int index, float rad) {
sk_bzero(radii, sizeof(SkVector) * 4);
radii[index].set(rad, rad);
}
static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
const SkVector radii[4]) {
SkRRect rrect;
rrect.setRectRadii(bounds, radii);
REPORTER_ASSERT(reporter, bounds == rrect.rect());
SkPath path;
// this line should not assert in the debug build (from validate)
path.addRRect(rrect);
REPORTER_ASSERT(reporter, bounds == path.getBounds());
}
static void test_skbug_3469(skiatest::Reporter* reporter) {
SkPath path;
path.moveTo(20, 20);
path.quadTo(20, 50, 80, 50);
path.quadTo(20, 50, 20, 80);
REPORTER_ASSERT(reporter, !path.isConvex());
}
static void test_skbug_3239(skiatest::Reporter* reporter) {
const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
const float rad = 33436320;
const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
SkVector radii[4];
for (int i = 0; i < 4; ++i) {
set_radii(radii, i, rad);
test_add_rrect(reporter, rectx, radii);
test_add_rrect(reporter, recty, radii);
}
}
static void make_path_crbug364224(SkPath* path) {
path->reset();
path->moveTo(3.747501373f, 2.724499941f);
path->lineTo(3.747501373f, 3.75f);
path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
path->lineTo(0.7475013733f, 4.0f);
path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
path->lineTo(0.4975013733f, 1.0f);
path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
path->lineTo(3.497501373f, 0.75f);
path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
path->lineTo(3.715001345f, 0.5512499809f);
path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
path->lineTo(0.7475013733f, 0.4999999702f);
path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
path->lineTo(0.2475013733f, 3.75f);
path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
path->lineTo(3.497501373f, 4.25f);
path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
path->lineTo(3.997501373f, 2.474750042f);
path->lineTo(3.747501373f, 2.724499941f);
path->close();
}
static void make_path_crbug364224_simplified(SkPath* path) {
path->moveTo(3.747501373f, 2.724499941f);
path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
path->close();
}
static void test_sect_with_horizontal_needs_pinning() {
// Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the
// intersection.
SkPath path;
path.reset();
path.moveTo(-540000, -720000);
path.lineTo(-9.10000017e-05f, 9.99999996e-13f);
path.lineTo(1, 1);
// Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper
SkPaint paint;
SkSurface::MakeRasterN32Premul(10, 10)->getCanvas()->drawPath(path, paint);
}
static void test_path_crbug364224() {
SkPath path;
SkPaint paint;
auto surface(SkSurface::MakeRasterN32Premul(84, 88));
SkCanvas* canvas = surface->getCanvas();
make_path_crbug364224_simplified(&path);
canvas->drawPath(path, paint);
make_path_crbug364224(&path);
canvas->drawPath(path, paint);
}
static void test_draw_AA_path(int width, int height, const SkPath& path) {
auto surface(SkSurface::MakeRasterN32Premul(width, height));
SkCanvas* canvas = surface->getCanvas();
SkPaint paint;
paint.setAntiAlias(true);
canvas->drawPath(path, paint);
}
// this is a unit test instead of a GM because it doesn't draw anything
static void test_fuzz_crbug_638223() {
SkPath path;
path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01)); // 50474, 161.113f
path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000),
SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0),
SkBits2Float(0x6bf9abea)); // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f
test_draw_AA_path(250, 250, path);
}
static void test_fuzz_crbug_643933() {
SkPath path;
path.moveTo(0, 0);
path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff), // 2.93943e-39f, 14.125f
SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f
SkBits2Float(0x721aee0c)); // 3.0687e+30f
test_draw_AA_path(250, 250, path);
path.reset();
path.moveTo(0, 0);
path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff), // 4.58981e-41f, 14.625f
SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04), // 510.269f, 29.8643f
SkBits2Float(0x5d06eff8)); // 6.07704e+17f
test_draw_AA_path(250, 250, path);
}
static void test_fuzz_crbug_647922() {
SkPath path;
path.moveTo(0, 0);
path.conicTo(SkBits2Float(0x00003939), SkBits2Float(0x42487fff), // 2.05276e-41f, 50.125f
SkBits2Float(0x48082361), SkBits2Float(0x4408e8e9), // 139406, 547.639f
SkBits2Float(0x4d1ade0f)); // 1.6239e+08f
test_draw_AA_path(250, 250, path);
}
static void test_fuzz_crbug_662780() {
auto surface(SkSurface::MakeRasterN32Premul(250, 250));
SkCanvas* canvas = surface->getCanvas();
SkPaint paint;
paint.setAntiAlias(true);
SkPath path;
path.moveTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x42f00000)); // 8, 120
// 8, 8, 8.00002f, 8, 0.707107f
path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x41000000),
SkBits2Float(0x41000010), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000)); // 308, 8
// 308, 8, 308, 8, 0.707107f
path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000),
SkBits2Float(0x439a0000), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000)); // 308, 158
// 308, 158, 308, 158, 0.707107f
path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000),
SkBits2Float(0x439a0000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
// 8, 158, 8, 158, 0.707107f
path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000),
SkBits2Float(0x41000000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
path.close();
canvas->clipPath(path, true);
canvas->drawRect(SkRect::MakeWH(250, 250), paint);
}
static void test_mask_overflow() {
SkPath path;
path.moveTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
path.lineTo(SkBits2Float(0x43de6000), SkBits2Float(0x43aa8000)); // 444.75f, 341
// 440.47f, 341, 437, 344.47f, 437, 348.75f
path.cubicTo(SkBits2Float(0x43dc3c29), SkBits2Float(0x43aa8000),
SkBits2Float(0x43da8000), SkBits2Float(0x43ac3c29),
SkBits2Float(0x43da8000), SkBits2Float(0x43ae6000));
path.lineTo(SkBits2Float(0x43da8000), SkBits2Float(0x43b18000)); // 437, 355
path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43b18000)); // 453, 355
path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
test_draw_AA_path(500, 500, path);
}
static void test_fuzz_crbug_668907() {
SkPath path;
path.moveTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
path.quadTo(SkBits2Float(0x41410041), SkBits2Float(0xc1414141), SkBits2Float(0x41414141),
SkBits2Float(0x414100ff)); // 12.0626f, -12.0784f, 12.0784f, 12.0627f
path.lineTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
path.close();
test_draw_AA_path(400, 500, path);
}
/**
* In debug mode, this path was causing an assertion to fail in
* SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
*/
static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
SkPoint orig, p1, p2, p3;
orig = SkPoint::Make(1.f, 1.f);
p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
p3 = SkPoint::Make(2.f, 2.f);
path->reset();
path->moveTo(orig);
path->cubicTo(p1, p2, p3);
path->close();
}
static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
SkPath path;
make_path_crbugskia2820(&path, reporter);
SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
stroke.setStrokeStyle(2 * SK_Scalar1);
stroke.applyToPath(&path, path);
}
static void test_path_crbugskia5995() {
SkPath path;
path.moveTo(SkBits2Float(0x40303030), SkBits2Float(0x3e303030)); // 2.75294f, 0.172059f
path.quadTo(SkBits2Float(0x41d63030), SkBits2Float(0x30303030), SkBits2Float(0x41013030),
SkBits2Float(0x00000000)); // 26.7735f, 6.40969e-10f, 8.07426f, 0
path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
test_draw_AA_path(500, 500, path);
}
static void make_path0(SkPath* path) {
// from * https://code.google.com/p/skia/issues/detail?id=1706
path->moveTo(146.939f, 1012.84f);
path->lineTo(181.747f, 1009.18f);
path->lineTo(182.165f, 1013.16f);
path->lineTo(147.357f, 1016.82f);
path->lineTo(146.939f, 1012.84f);
path->close();
}
static void make_path1(SkPath* path) {
path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
}
typedef void (*PathProc)(SkPath*);
/*
* Regression test: we used to crash (overwrite internal storage) during
* construction of the region when the path was INVERSE. That is now fixed,
* so test these regions (which used to assert/crash).
*
* https://code.google.com/p/skia/issues/detail?id=1706
*/
static void test_path_to_region(skiatest::Reporter* reporter) {
PathProc procs[] = {
make_path0,
make_path1,
};
SkRegion clip;
clip.setRect({0, 0, 1255, 1925});
for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
SkPath path;
procs[i](&path);
SkRegion rgn;
rgn.setPath(path, clip);
path.toggleInverseFillType();
rgn.setPath(path, clip);
}
}
#ifdef SK_BUILD_FOR_WIN
#define SUPPRESS_VISIBILITY_WARNING
#else
#define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
#endif
static void test_path_close_issue1474(skiatest::Reporter* reporter) {
// This test checks that r{Line,Quad,Conic,Cubic}To following a close()
// are relative to the point we close to, not relative to the point we close from.
SkPath path;
SkPoint last;
// Test rLineTo().
path.rLineTo(0, 100);
path.rLineTo(100, 0);
path.close(); // Returns us back to 0,0.
path.rLineTo(50, 50); // This should go to 50,50.
path.getLastPt(&last);
REPORTER_ASSERT(reporter, 50 == last.fX);
REPORTER_ASSERT(reporter, 50 == last.fY);
// Test rQuadTo().
path.rewind();
path.rLineTo(0, 100);
path.rLineTo(100, 0);
path.close();
path.rQuadTo(50, 50, 75, 75);
path.getLastPt(&last);
REPORTER_ASSERT(reporter, 75 == last.fX);
REPORTER_ASSERT(reporter, 75 == last.fY);
// Test rConicTo().
path.rewind();
path.rLineTo(0, 100);
path.rLineTo(100, 0);
path.close();
path.rConicTo(50, 50, 85, 85, 2);
path.getLastPt(&last);
REPORTER_ASSERT(reporter, 85 == last.fX);
REPORTER_ASSERT(reporter, 85 == last.fY);
// Test rCubicTo().
path.rewind();
path.rLineTo(0, 100);
path.rLineTo(100, 0);
path.close();
path.rCubicTo(50, 50, 85, 85, 95, 95);
path.getLastPt(&last);
REPORTER_ASSERT(reporter, 95 == last.fX);
REPORTER_ASSERT(reporter, 95 == last.fY);
}
static void test_gen_id(skiatest::Reporter* reporter) {
SkPath a, b;
REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
a.moveTo(0, 0);
const uint32_t z = a.getGenerationID();
REPORTER_ASSERT(reporter, z != b.getGenerationID());
a.reset();
REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
a.moveTo(1, 1);
const uint32_t y = a.getGenerationID();
REPORTER_ASSERT(reporter, z != y);
b.moveTo(2, 2);
const uint32_t x = b.getGenerationID();
REPORTER_ASSERT(reporter, x != y && x != z);
a.swap(b);
REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
b = a;
REPORTER_ASSERT(reporter, b.getGenerationID() == x);
SkPath c(a);
REPORTER_ASSERT(reporter, c.getGenerationID() == x);
c.lineTo(3, 3);
const uint32_t w = c.getGenerationID();
REPORTER_ASSERT(reporter, b.getGenerationID() == x);
REPORTER_ASSERT(reporter, a.getGenerationID() == x);
REPORTER_ASSERT(reporter, w != x);
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
static bool kExpectGenIDToIgnoreFill = false;
#else
static bool kExpectGenIDToIgnoreFill = true;
#endif
c.toggleInverseFillType();
const uint32_t v = c.getGenerationID();
REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
c.rewind();
REPORTER_ASSERT(reporter, v != c.getGenerationID());
}
// This used to assert in the debug build, as the edges did not all line-up.
static void test_bad_cubic_crbug234190() {
SkPath path;
path.moveTo(13.8509f, 3.16858f);
path.cubicTo(-2.35893e+08f, -4.21044e+08f,
-2.38991e+08f, -4.26573e+08f,
-2.41016e+08f, -4.30188e+08f);
test_draw_AA_path(84, 88, path);
}
static void test_bad_cubic_crbug229478() {
const SkPoint pts[] = {
{ 4595.91064f, -11596.9873f },
{ 4597.2168f, -11595.9414f },
{ 4598.52344f, -11594.8955f },
{ 4599.83008f, -11593.8496f },
};
SkPath path;
path.moveTo(pts[0]);
path.cubicTo(pts[1], pts[2], pts[3]);
SkPaint paint;
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(20);
SkPath dst;
// Before the fix, this would infinite-recurse, and run out of stack
// because we would keep trying to subdivide a degenerate cubic segment.
paint.getFillPath(path, &dst, nullptr);
}
static void build_path_170666(SkPath& path) {
path.moveTo(17.9459f, 21.6344f);
path.lineTo(139.545f, -47.8105f);
path.lineTo(139.545f, -47.8105f);
path.lineTo(131.07f, -47.3888f);
path.lineTo(131.07f, -47.3888f);
path.lineTo(122.586f, -46.9532f);
path.lineTo(122.586f, -46.9532f);
path.lineTo(18076.6f, 31390.9f);
path.lineTo(18076.6f, 31390.9f);
path.lineTo(18085.1f, 31390.5f);
path.lineTo(18085.1f, 31390.5f);
path.lineTo(18076.6f, 31390.9f);
path.lineTo(18076.6f, 31390.9f);
path.lineTo(17955, 31460.3f);
path.lineTo(17955, 31460.3f);
path.lineTo(17963.5f, 31459.9f);
path.lineTo(17963.5f, 31459.9f);
path.lineTo(17971.9f, 31459.5f);
path.lineTo(17971.9f, 31459.5f);
path.lineTo(17.9551f, 21.6205f);
path.lineTo(17.9551f, 21.6205f);
path.lineTo(9.47091f, 22.0561f);
path.lineTo(9.47091f, 22.0561f);
path.lineTo(17.9459f, 21.6344f);
path.lineTo(17.9459f, 21.6344f);
path.close();path.moveTo(0.995934f, 22.4779f);
path.lineTo(0.986725f, 22.4918f);
path.lineTo(0.986725f, 22.4918f);
path.lineTo(17955, 31460.4f);
path.lineTo(17955, 31460.4f);
path.lineTo(17971.9f, 31459.5f);
path.lineTo(17971.9f, 31459.5f);
path.lineTo(18093.6f, 31390.1f);
path.lineTo(18093.6f, 31390.1f);
path.lineTo(18093.6f, 31390);
path.lineTo(18093.6f, 31390);
path.lineTo(139.555f, -47.8244f);
path.lineTo(139.555f, -47.8244f);
path.lineTo(122.595f, -46.9671f);
path.lineTo(122.595f, -46.9671f);
path.lineTo(0.995934f, 22.4779f);
path.lineTo(0.995934f, 22.4779f);
path.close();
path.moveTo(5.43941f, 25.5223f);
path.lineTo(798267, -28871.1f);
path.lineTo(798267, -28871.1f);
path.lineTo(3.12512e+06f, -113102);
path.lineTo(3.12512e+06f, -113102);
path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
path.lineTo(2.78271e+08f, -1.00733e+07f);
path.lineTo(2.78271e+08f, -1.00733e+07f);
path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
path.lineTo(2.77473e+08f, -1.00444e+07f);
path.lineTo(2.77473e+08f, -1.00444e+07f);
path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
path.lineTo(798284, -28872);
path.lineTo(798284, -28872);
path.lineTo(22.4044f, 24.6677f);
path.lineTo(22.4044f, 24.6677f);
path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
path.close();
}
static void build_path_simple_170666(SkPath& path) {
path.moveTo(126.677f, 24.1591f);
path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
}
// This used to assert in the SK_DEBUG build, as the clip step would fail with
// too-few interations in our cubic-line intersection code. That code now runs
// 24 interations (instead of 16).
static void test_crbug_170666() {
SkPath path;
build_path_simple_170666(path);
test_draw_AA_path(1000, 1000, path);
build_path_170666(path);
test_draw_AA_path(1000, 1000, path);
}
static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
SkScalar tx, SkScalar ty, SkScalar scale) {
SkPath smallPath;
SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
bool smallConvex = smallPath.isConvex();
SkPath largePath;
SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
SkMatrix matrix;
matrix.reset();
matrix.preTranslate(100, 100);
matrix.preScale(scale, scale);
largePath.transform(matrix);
bool largeConvex = largePath.isConvex();
REPORTER_ASSERT(reporter, smallConvex == largeConvex);
}
static void test_crbug_493450(skiatest::Reporter* reporter) {
const char reducedCase[] =
"M0,0"
"L0.0002, 0"
"L0.0002, 0.0002"
"L0.0001, 0.0001"
"L0,0.0002"
"Z";
test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
const char originalFiddleData[] =
"M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
"L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
"L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
"L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
"L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
826357.3384828606f);
}
static void test_crbug_495894(skiatest::Reporter* reporter) {
const char originalFiddleData[] =
"M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
"L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
"L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
"L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
"L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
"L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
"L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
"L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
"L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
"L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
"L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
"L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
"L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
"L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
"L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
"L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
"L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
"M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
"L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
"L-0.33993994441916414,-0.11288906492739594Z";
test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
65536);
}
static void test_crbug_613918() {
SkPath path;
path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
// This call could lead to an assert or uninitialized read due to a failure
// to check the return value from SkCubicClipper::ChopMonoAtY.
path.contains(-1.84817e-08f, 1.15465e-08f);
}
static void test_addrect(skiatest::Reporter* reporter) {
SkPath path;
path.lineTo(0, 0);
path.addRect(SkRect::MakeWH(50, 100));
REPORTER_ASSERT(reporter, path.isRect(nullptr));
path.reset();
path.lineTo(FLT_EPSILON, FLT_EPSILON);
path.addRect(SkRect::MakeWH(50, 100));
REPORTER_ASSERT(reporter, !path.isRect(nullptr));
path.reset();
path.quadTo(0, 0, 0, 0);
path.addRect(SkRect::MakeWH(50, 100));
REPORTER_ASSERT(reporter, !path.isRect(nullptr));
path.reset();
path.conicTo(0, 0, 0, 0, 0.5f);
path.addRect(SkRect::MakeWH(50, 100));
REPORTER_ASSERT(reporter, !path.isRect(nullptr));
path.reset();
path.cubicTo(0, 0, 0, 0, 0, 0);
path.addRect(SkRect::MakeWH(50, 100));
REPORTER_ASSERT(reporter, !path.isRect(nullptr));
}
// Make sure we stay non-finite once we get there (unless we reset or rewind).
static void test_addrect_isfinite(skiatest::Reporter* reporter) {
SkPath path;
path.addRect(SkRect::MakeWH(50, 100));
REPORTER_ASSERT(reporter, path.isFinite());
path.moveTo(0, 0);
path.lineTo(SK_ScalarInfinity, 42);
REPORTER_ASSERT(reporter, !path.isFinite());
path.addRect(SkRect::MakeWH(50, 100));
REPORTER_ASSERT(reporter, !path.isFinite());
path.reset();
REPORTER_ASSERT(reporter, path.isFinite());
path.addRect(SkRect::MakeWH(50, 100));
REPORTER_ASSERT(reporter, path.isFinite());
}
static void build_big_path(SkPath* path, bool reducedCase) {
if (reducedCase) {
path->moveTo(577330, 1971.72f);
path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
} else {
path->moveTo(60.1631f, 7.70567f);
path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
path->lineTo(577379, 1977.77f);
path->quadTo(577364, 1979.57f, 577325, 1980.26f);
path->quadTo(577286, 1980.95f, 577245, 1980.13f);
path->quadTo(577205, 1979.3f, 577187, 1977.45f);
path->quadTo(577168, 1975.6f, 577183, 1973.8f);
path->quadTo(577198, 1972, 577238, 1971.31f);
path->quadTo(577277, 1970.62f, 577317, 1971.45f);
path->quadTo(577330, 1971.72f, 577341, 1972.11f);
path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
path->moveTo(306.718f, -32.912f);
path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
}
}
static void test_clipped_cubic() {
auto surface(SkSurface::MakeRasterN32Premul(640, 480));
// This path used to assert, because our cubic-chopping code incorrectly
// moved control points after the chop. This test should be run in SK_DEBUG
// mode to ensure that we no long assert.
SkPath path;
for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
build_big_path(&path, SkToBool(doReducedCase));
SkPaint paint;
for (int doAA = 0; doAA <= 1; ++doAA) {
paint.setAntiAlias(SkToBool(doAA));
surface->getCanvas()->drawPath(path, paint);
}
}
}
static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
if (expected != bounds) {
ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
expected.left(), expected.top(), expected.right(), expected.bottom());
}
}
static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
SkPath path;
#if 0
// As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
// rewritten them to avoid this (compiler-bug?).
REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
path.moveTo(-5, -8);
REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
path.moveTo(1, 2);
REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
#else
dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
path.moveTo(-5, -8); // should set the bounds
dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
path.moveTo(1, 2); // don't expect this to have changed the bounds
dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
#endif
}
#include "include/core/SkSurface.h"
static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
SkPath path;
path.moveTo(0, 0);
path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
test_draw_AA_path(100, 100, path);
}
// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
// which triggered an assert, from a tricky cubic. This test replicates that
// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
// assert in the SK_DEBUG build.
static void test_tricky_cubic() {
const SkPoint pts[] = {
{ SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
{ SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
{ SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
{ SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
};
SkPath path;
path.moveTo(pts[0]);
path.cubicTo(pts[1], pts[2], pts[3]);
test_draw_AA_path(19, 130, path);
}
// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
//
static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
SkPath path;
path.quadTo(157, 366, 286, 208);
path.arcTo(37, 442, 315, 163, 957494590897113.0f);
SkMatrix matrix;
matrix.setScale(1000*1000, 1000*1000);
// Be sure that path::transform correctly updates isFinite and the bounds
// if the transformation overflows. The previous bug was that isFinite was
// set to true in this case, but the bounds were not set to empty (which
// they should be).
while (path.isFinite()) {
REPORTER_ASSERT(reporter, path.getBounds().isFinite());
REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
path.transform(matrix);
}
REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
matrix.setTranslate(SK_Scalar1, SK_Scalar1);
path.transform(matrix);
// we need to still be non-finite
REPORTER_ASSERT(reporter, !path.isFinite());
REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
}
static void add_corner_arc(SkPath* path, const SkRect& rect,
SkScalar xIn, SkScalar yIn,
int startAngle)
{
SkScalar rx = SkMinScalar(rect.width(), xIn);
SkScalar ry = SkMinScalar(rect.height(), yIn);
SkRect arcRect;
arcRect.setLTRB(-rx, -ry, rx, ry);
switch (startAngle) {
case 0:
arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
break;
case 90:
arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
break;
case 180:
arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
break;
case 270:
arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
break;
default:
break;
}
path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
}
static void make_arb_round_rect(SkPath* path, const SkRect& r,
SkScalar xCorner, SkScalar yCorner) {
// we are lazy here and use the same x & y for each corner
add_corner_arc(path, r, xCorner, yCorner, 270);
add_corner_arc(path, r, xCorner, yCorner, 0);
add_corner_arc(path, r, xCorner, yCorner, 90);
add_corner_arc(path, r, xCorner, yCorner, 180);
path->close();
}
// Chrome creates its own round rects with each corner possibly being different.
// Performance will suffer if they are not convex.
// Note: PathBench::ArbRoundRectBench performs almost exactly
// the same test (but with drawing)
static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
SkRandom rand;
SkRect r;
for (int i = 0; i < 5000; ++i) {
SkScalar size = rand.nextUScalar1() * 30;
if (size < SK_Scalar1) {
continue;
}
r.fLeft = rand.nextUScalar1() * 300;
r.fTop = rand.nextUScalar1() * 300;
r.fRight = r.fLeft + 2 * size;
r.fBottom = r.fTop + 2 * size;
SkPath temp;
make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
REPORTER_ASSERT(reporter, temp.isConvex());
}
}
// Chrome will sometimes create a 0 radius round rect. The degenerate
// quads prevent the path from being converted to a rect
// Note: PathBench::ArbRoundRectBench performs almost exactly
// the same test (but with drawing)
static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
SkRandom rand;
SkRect r;
for (int i = 0; i < 5000; ++i) {
SkScalar size = rand.nextUScalar1() * 30;
if (size < SK_Scalar1) {
continue;
}
r.fLeft = rand.nextUScalar1() * 300;
r.fTop = rand.nextUScalar1() * 300;
r.fRight = r.fLeft + 2 * size;
r.fBottom = r.fTop + 2 * size;
SkPath temp;
make_arb_round_rect(&temp, r, 0, 0);
SkRect result;
REPORTER_ASSERT(reporter, temp.isRect(&result));
REPORTER_ASSERT(reporter, r == result);
}
}
static void test_rect_isfinite(skiatest::Reporter* reporter) {
const SkScalar inf = SK_ScalarInfinity;
const SkScalar negInf = SK_ScalarNegativeInfinity;
const SkScalar nan = SK_ScalarNaN;
SkRect r;
r.setEmpty();
REPORTER_ASSERT(reporter, r.isFinite());
r.setLTRB(0, 0, inf, negInf);
REPORTER_ASSERT(reporter, !r.isFinite());
r.setLTRB(0, 0, nan, 0);
REPORTER_ASSERT(reporter, !r.isFinite());
SkPoint pts[] = {
{ 0, 0 },
{ SK_Scalar1, 0 },
{ 0, SK_Scalar1 },
};
bool isFine = r.setBoundsCheck(pts, 3);
REPORTER_ASSERT(reporter, isFine);
REPORTER_ASSERT(reporter, !r.isEmpty());
pts[1].set(inf, 0);
isFine = r.setBoundsCheck(pts, 3);
REPORTER_ASSERT(reporter, !isFine);
REPORTER_ASSERT(reporter, r.isEmpty());
pts[1].set(nan, 0);
isFine = r.setBoundsCheck(pts, 3);
REPORTER_ASSERT(reporter, !isFine);
REPORTER_ASSERT(reporter, r.isEmpty());
}
static void test_path_isfinite(skiatest::Reporter* reporter) {
const SkScalar inf = SK_ScalarInfinity;
const SkScalar negInf = SK_ScalarNegativeInfinity;
const SkScalar nan = SK_ScalarNaN;
SkPath path;
REPORTER_ASSERT(reporter, path.isFinite());
path.reset();
REPORTER_ASSERT(reporter, path.isFinite());
path.reset();
path.moveTo(SK_Scalar1, 0);
REPORTER_ASSERT(reporter, path.isFinite());
path.reset();
path.moveTo(inf, negInf);
REPORTER_ASSERT(reporter, !path.isFinite());
path.reset();
path.moveTo(nan, 0);
REPORTER_ASSERT(reporter, !path.isFinite());
}
static void test_isfinite(skiatest::Reporter* reporter) {
test_rect_isfinite(reporter);
test_path_isfinite(reporter);
}
static void test_islastcontourclosed(skiatest::Reporter* reporter) {
SkPath path;
REPORTER_ASSERT(reporter, !path.isLastContourClosed());
path.moveTo(0, 0);
REPORTER_ASSERT(reporter, !path.isLastContourClosed());
path.close();
REPORTER_ASSERT(reporter, path.isLastContourClosed());
path.lineTo(100, 100);
REPORTER_ASSERT(reporter, !path.isLastContourClosed());
path.moveTo(200, 200);
REPORTER_ASSERT(reporter, !path.isLastContourClosed());
path.close();
REPORTER_ASSERT(reporter, path.isLastContourClosed());
path.moveTo(0, 0);
REPORTER_ASSERT(reporter, !path.isLastContourClosed());
}
// assert that we always
// start with a moveTo
// only have 1 moveTo
// only have Lines after that
// end with a single close
// only have (at most) 1 close
//
static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
const SkPoint srcPts[], bool expectClose) {
SkPath::RawIter iter(path);
SkPoint pts[4];
bool firstTime = true;
bool foundClose = false;
for (;;) {
switch (iter.next(pts)) {
case SkPath::kMove_Verb:
REPORTER_ASSERT(reporter, firstTime);
REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
srcPts++;
firstTime = false;
break;
case SkPath::kLine_Verb:
REPORTER_ASSERT(reporter, !firstTime);
REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
srcPts++;
break;
case SkPath::kQuad_Verb:
REPORTER_ASSERT(reporter, false, "unexpected quad verb");
break;
case SkPath::kConic_Verb:
REPORTER_ASSERT(reporter, false, "unexpected conic verb");
break;
case SkPath::kCubic_Verb:
REPORTER_ASSERT(reporter, false, "unexpected cubic verb");
break;
case SkPath::kClose_Verb:
REPORTER_ASSERT(reporter, !firstTime);
REPORTER_ASSERT(reporter, !foundClose);
REPORTER_ASSERT(reporter, expectClose);
foundClose = true;
break;
case SkPath::kDone_Verb:
goto DONE;
}
}
DONE:
REPORTER_ASSERT(reporter, foundClose == expectClose);
}
static void test_addPoly(skiatest::Reporter* reporter) {
SkPoint pts[32];
SkRandom rand;
for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
pts[i].fX = rand.nextSScalar1();
pts[i].fY = rand.nextSScalar1();
}
for (int doClose = 0; doClose <= 1; ++doClose) {
for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
SkPath path;
path.addPoly(pts, SkToInt(count), SkToBool(doClose));
test_poly(reporter, path, pts, SkToBool(doClose));
}
}
}
static void test_strokerec(skiatest::Reporter* reporter) {
SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
REPORTER_ASSERT(reporter, rec.isFillStyle());
rec.setHairlineStyle();
REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rec.setStrokeStyle(SK_Scalar1, false);
REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rec.setStrokeStyle(SK_Scalar1, true);
REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rec.setStrokeStyle(0, false);
REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rec.setStrokeStyle(0, true);
REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
}
// Set this for paths that don't have a consistent direction such as a bowtie.
// (cheapComputeDirection is not expected to catch these.)
// Legal values are CW (0), CCW (1) and Unknown (2), leaving 3 as a convenient sentinel.
const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(3);
static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
SkPathPriv::FirstDirection expected) {
if (expected == kDontCheckDir) {
return;
}
SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
SkPathPriv::FirstDirection dir;
if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
REPORTER_ASSERT(reporter, dir == expected);
} else {
REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
}
}
static void test_direction(skiatest::Reporter* reporter) {
size_t i;
SkPath path;
REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
static const char* gDegen[] = {
"M 10 10",
"M 10 10 M 20 20",
"M 10 10 L 20 20",
"M 10 10 L 10 10 L 10 10",
"M 10 10 Q 10 10 10 10",
"M 10 10 C 10 10 10 10 10 10",
};
for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
path.reset();
bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
REPORTER_ASSERT(reporter, valid);
REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
}
static const char* gCW[] = {
"M 10 10 L 10 10 Q 20 10 20 20",
"M 10 10 C 20 10 20 20 20 20",
"M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max
// rect with top two corners replaced by cubics with identical middle
// control points
"M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
"M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
};
for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
path.reset();
bool valid = SkParsePath::FromSVGString(gCW[i], &path);
REPORTER_ASSERT(reporter, valid);
check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
}
static const char* gCCW[] = {
"M 10 10 L 10 10 Q 20 10 20 -20",
"M 10 10 C 20 10 20 -20 20 -20",
"M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
// rect with top two corners replaced by cubics with identical middle
// control points
"M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
"M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
};
for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
path.reset();
bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
REPORTER_ASSERT(reporter, valid);
check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
}
// Test two donuts, each wound a different direction. Only the outer contour
// determines the cheap direction
path.reset();
path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
path.reset();
path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
// triangle with one point really far from the origin.
path.reset();
// the first point is roughly 1.05e10, 1.05e10
path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
path.reset();
path.conicTo(20, 0, 20, 20, 0.5f);
path.close();
check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
path.reset();
path.lineTo(1, 1e7f);
path.lineTo(1e7f, 2e7f);
path.close();
REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
}
static void add_rect(SkPath* path, const SkRect& r) {
path->moveTo(r.fLeft, r.fTop);
path->lineTo(r.fRight, r.fTop);
path->lineTo(r.fRight, r.fBottom);
path->lineTo(r.fLeft, r.fBottom);
path->close();
}
static void test_bounds(skiatest::Reporter* reporter) {
static const SkRect rects[] = {
{ SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
{ SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
{ SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
{ SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
};
SkPath path0, path1;
for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
path0.addRect(rects[i]);
add_rect(&path1, rects[i]);
}
REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
}
static void stroke_cubic(const SkPoint pts[4]) {
SkPath path;
path.moveTo(pts[0]);
path.cubicTo(pts[1], pts[2], pts[3]);
SkPaint paint;
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(SK_Scalar1 * 2);
SkPath fill;
paint.getFillPath(path, &fill);
}
// just ensure this can run w/o any SkASSERTS firing in the debug build
// we used to assert due to differences in how we determine a degenerate vector
// but that was fixed with the introduction of SkPoint::CanNormalize
static void stroke_tiny_cubic() {
SkPoint p0[] = {
{ 372.0f, 92.0f },
{ 372.0f, 92.0f },
{ 372.0f, 92.0f },
{ 372.0f, 92.0f },
};
stroke_cubic(p0);
SkPoint p1[] = {
{ 372.0f, 92.0f },
{ 372.0007f, 92.000755f },
{ 371.99927f, 92.003922f },
{ 371.99826f, 92.003899f },
};
stroke_cubic(p1);
}
static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
for (int i = 0; i < 2; ++i) {
SkPath::Iter iter(path, SkToBool(i));
SkPoint mv;
SkPoint pts[4];
SkPath::Verb v;
int nMT = 0;
int nCL = 0;
mv.set(0, 0);
while (SkPath::kDone_Verb != (v = iter.next(pts))) {
switch (v) {
case SkPath::kMove_Verb:
mv = pts[0];
++nMT;
break;
case SkPath::kClose_Verb:
REPORTER_ASSERT(reporter, mv == pts[0]);
++nCL;
break;
default:
break;
}
}
// if we force a close on the interator we should have a close
// for every moveTo
REPORTER_ASSERT(reporter, !i || nMT == nCL);
}
}
static void test_close(skiatest::Reporter* reporter) {
SkPath closePt;
closePt.moveTo(0, 0);
closePt.close();
check_close(reporter, closePt);
SkPath openPt;
openPt.moveTo(0, 0);
check_close(reporter, openPt);
SkPath empty;
check_close(reporter, empty);
empty.close();
check_close(reporter, empty);
SkPath rect;
rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
check_close(reporter, rect);
rect.close();
check_close(reporter, rect);
SkPath quad;
quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
check_close(reporter, quad);
quad.close();
check_close(reporter, quad);
SkPath cubic;
quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
check_close(reporter, cubic);
cubic.close();
check_close(reporter, cubic);
SkPath line;
line.moveTo(SK_Scalar1, SK_Scalar1);
line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
check_close(reporter, line);
line.close();
check_close(reporter, line);
SkPath rect2;
rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
rect2.close();
rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
check_close(reporter, rect2);
rect2.close();
check_close(reporter, rect2);
SkPath oval3;
oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
oval3.close();
oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
check_close(reporter, oval3);
oval3.close();
check_close(reporter, oval3);
SkPath moves;
moves.moveTo(SK_Scalar1, SK_Scalar1);
moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
check_close(reporter, moves);
stroke_tiny_cubic();
}
static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
SkPath::Convexity expected) {
SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
SkPath::Convexity c = copy.getConvexity();
REPORTER_ASSERT(reporter, c == expected);
#ifndef SK_LEGACY_PATH_CONVEXITY
// test points-by-array interface
SkPath::Iter iter(path, true);
int initialMoves = 0;
SkPoint pts[4];
while (SkPath::kMove_Verb == iter.next(pts)) {
++initialMoves;
}
if (initialMoves > 0) {
std::vector<SkPoint> points;
points.resize(path.getPoints(nullptr, 0));
(void) path.getPoints(&points.front(), points.size());
int skip = initialMoves - 1;
bool isConvex = SkPathPriv::IsConvex(&points.front() + skip, points.size() - skip);
REPORTER_ASSERT(reporter, isConvex == (SkPath::kConvex_Convexity == expected));
}
#endif
}
static void test_path_crbug389050(skiatest::Reporter* reporter) {
SkPath tinyConvexPolygon;
tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
tinyConvexPolygon.close();
tinyConvexPolygon.getConvexity();
// This is convex, but so small that it fails many of our checks, and the three "backwards"
// bends convince the checker that it's concave. That's okay though, we draw it correctly.
check_convexity(reporter, tinyConvexPolygon, SkPath::kConcave_Convexity);
check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
SkPath platTriangle;
platTriangle.moveTo(0, 0);
platTriangle.lineTo(200, 0);
platTriangle.lineTo(100, 0.04f);
platTriangle.close();
platTriangle.getConvexity();
check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
platTriangle.reset();
platTriangle.moveTo(0, 0);
platTriangle.lineTo(200, 0);
platTriangle.lineTo(100, 0.03f);
platTriangle.close();
platTriangle.getConvexity();
check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
}
static void test_convexity2(skiatest::Reporter* reporter) {
SkPath pt;
pt.moveTo(0, 0);
pt.close();
check_convexity(reporter, pt, SkPath::kConvex_Convexity);
check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
SkPath line;
line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
line.close();
check_convexity(reporter, line, SkPath::kConvex_Convexity);
check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
SkPath triLeft;
triLeft.moveTo(0, 0);
triLeft.lineTo(SK_Scalar1, 0);
triLeft.lineTo(SK_Scalar1, SK_Scalar1);
triLeft.close();
check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
SkPath triRight;
triRight.moveTo(0, 0);
triRight.lineTo(-SK_Scalar1, 0);
triRight.lineTo(SK_Scalar1, SK_Scalar1);
triRight.close();
check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
SkPath square;
square.moveTo(0, 0);
square.lineTo(SK_Scalar1, 0);
square.lineTo(SK_Scalar1, SK_Scalar1);
square.lineTo(0, SK_Scalar1);
square.close();
check_convexity(reporter, square, SkPath::kConvex_Convexity);
check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
SkPath redundantSquare;
redundantSquare.moveTo(0, 0);
redundantSquare.lineTo(0, 0);
redundantSquare.lineTo(0, 0);
redundantSquare.lineTo(SK_Scalar1, 0);
redundantSquare.lineTo(SK_Scalar1, 0);
redundantSquare.lineTo(SK_Scalar1, 0);
redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
redundantSquare.lineTo(0, SK_Scalar1);
redundantSquare.lineTo(0, SK_Scalar1);
redundantSquare.lineTo(0, SK_Scalar1);
redundantSquare.close();
check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
SkPath bowTie;
bowTie.moveTo(0, 0);
bowTie.lineTo(0, 0);
bowTie.lineTo(0, 0);
bowTie.lineTo(SK_Scalar1, SK_Scalar1);
bowTie.lineTo(SK_Scalar1, SK_Scalar1);
bowTie.lineTo(SK_Scalar1, SK_Scalar1);
bowTie.lineTo(SK_Scalar1, 0);
bowTie.lineTo(SK_Scalar1, 0);
bowTie.lineTo(SK_Scalar1, 0);
bowTie.lineTo(0, SK_Scalar1);
bowTie.lineTo(0, SK_Scalar1);
bowTie.lineTo(0, SK_Scalar1);
bowTie.close();
check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
check_direction(reporter, bowTie, kDontCheckDir);
SkPath spiral;
spiral.moveTo(0, 0);
spiral.lineTo(100*SK_Scalar1, 0);
spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
spiral.lineTo(0, 100*SK_Scalar1);
spiral.lineTo(0, 50*SK_Scalar1);
spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
spiral.close();
check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
check_direction(reporter, spiral, kDontCheckDir);
SkPath dent;
dent.moveTo(0, 0);
dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
dent.lineTo(0, 100*SK_Scalar1);
dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
dent.close();
check_convexity(reporter, dent, SkPath::kConcave_Convexity);
check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
// https://bug.skia.org/2235
SkPath strokedSin;
for (int i = 0; i < 2000; i++) {
SkScalar x = SkIntToScalar(i) / 2;
SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
if (0 == i) {
strokedSin.moveTo(x, y);
} else {
strokedSin.lineTo(x, y);
}
}
SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
stroke.setStrokeStyle(2 * SK_Scalar1);
stroke.applyToPath(&strokedSin, strokedSin);
check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
check_direction(reporter, strokedSin, kDontCheckDir);
// http://crbug.com/412640
SkPath degenerateConcave;
degenerateConcave.moveTo(148.67912f, 191.875f);
degenerateConcave.lineTo(470.37695f, 7.5f);
degenerateConcave.lineTo(148.67912f, 191.875f);
degenerateConcave.lineTo(41.446522f, 376.25f);
degenerateConcave.lineTo(-55.971577f, 460.0f);
degenerateConcave.lineTo(41.446522f, 376.25f);
check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
// http://crbug.com/433683
SkPath badFirstVector;
badFirstVector.moveTo(501.087708f, 319.610352f);
badFirstVector.lineTo(501.087708f, 319.610352f);
badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
badFirstVector.lineTo(301.557678f, 98.044601f);
badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
badFirstVector.lineTo(504.912292f, 316.389648f);
badFirstVector.lineTo(501.087708f, 319.610352f);
badFirstVector.close();
check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
// http://crbug.com/993330
SkPath falseBackEdge;
falseBackEdge.moveTo(-217.83430557928145f, -382.14948768484857f);
falseBackEdge.lineTo(-227.73867866614847f, -399.52485512718323f);
falseBackEdge.cubicTo(-158.3541047666846f, -439.0757140459542f,
-79.8654464485281f, -459.875f,
-1.1368683772161603e-13f, -459.875f);
falseBackEdge.lineTo(-8.08037266162413e-14f, -439.875f);
falseBackEdge.lineTo(-8.526512829121202e-14f, -439.87499999999994f);
falseBackEdge.cubicTo(-76.39209188702645f, -439.87499999999994f,
-151.46727226799754f, -419.98027663161537f,
-217.83430557928145f, -382.14948768484857f);
falseBackEdge.close();
check_convexity(reporter, falseBackEdge, SkPath::kConcave_Convexity);
}
static void test_convexity_doubleback(skiatest::Reporter* reporter) {
SkPath doubleback;
doubleback.lineTo(1, 1);
check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
doubleback.lineTo(2, 2);
check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
doubleback.reset();
doubleback.lineTo(1, 0);
check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
doubleback.lineTo(2, 0);
check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
doubleback.lineTo(1, 0);
check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
doubleback.reset();
doubleback.quadTo(1, 1, 2, 2);
check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
doubleback.reset();
doubleback.quadTo(1, 0, 2, 0);
check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
doubleback.quadTo(1, 0, 0, 0);
check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
}
static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
const SkRect& bounds) {
REPORTER_ASSERT(reporter, p.isConvex());
REPORTER_ASSERT(reporter, p.getBounds() == bounds);
SkPath p2(p);
REPORTER_ASSERT(reporter, p2.isConvex());
REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
SkPath other;
other.swap(p2);
REPORTER_ASSERT(reporter, other.isConvex());
REPORTER_ASSERT(reporter, other.getBounds() == bounds);
}
static void setFromString(SkPath* path, const char str[]) {
bool first = true;
while (str) {
SkScalar x, y;
str = SkParse::FindScalar(str, &x);
if (nullptr == str) {
break;
}
str = SkParse::FindScalar(str, &y);
SkASSERT(str);
if (first) {
path->moveTo(x, y);
first = false;
} else {
path->lineTo(x, y);
}
}
}
static void test_convexity(skiatest::Reporter* reporter) {
SkPath path;
check_convexity(reporter, path, SkPath::kConvex_Convexity);
path.addCircle(0, 0, SkIntToScalar(10));
check_convexity(reporter, path, SkPath::kConvex_Convexity);
path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
check_convexity(reporter, path, SkPath::kConcave_Convexity);
path.reset();
path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
check_convexity(reporter, path, SkPath::kConvex_Convexity);
REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
path.reset();
path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
check_convexity(reporter, path, SkPath::kConvex_Convexity);
REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
path.reset();
path.quadTo(100, 100, 50, 50); // This from GM:convexpaths
check_convexity(reporter, path, SkPath::kConvex_Convexity);
static const struct {
const char* fPathStr;
SkPath::Convexity fExpectedConvexity;
SkPathPriv::FirstDirection fExpectedDirection;
} gRec[] = {
{ "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
{ "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
{ "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
{ "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
{ "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
{ "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
{ "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
{ "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
};
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
path.reset();
setFromString(&path, gRec[i].fPathStr);
check_convexity(reporter, path, gRec[i].fExpectedConvexity);
check_direction(reporter, path, gRec[i].fExpectedDirection);
// check after setting the initial convex and direction
if (kDontCheckDir != gRec[i].fExpectedDirection) {
SkPath copy(path);
SkPathPriv::FirstDirection dir;
bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
^ foundDir);
REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
}
REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
check_direction(reporter, path, gRec[i].fExpectedDirection);
}
static const SkPoint nonFinitePts[] = {
{ SK_ScalarInfinity, 0 },
{ 0, SK_ScalarInfinity },
{ SK_ScalarInfinity, SK_ScalarInfinity },
{ SK_ScalarNegativeInfinity, 0},
{ 0, SK_ScalarNegativeInfinity },
{ SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
{ SK_ScalarNegativeInfinity, SK_ScalarInfinity },
{ SK_ScalarInfinity, SK_ScalarNegativeInfinity },
{ SK_ScalarNaN, 0 },
{ 0, SK_ScalarNaN },
{ SK_ScalarNaN, SK_ScalarNaN },
};
const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
static const SkPoint axisAlignedPts[] = {
{ SK_ScalarMax, 0 },
{ 0, SK_ScalarMax },
{ SK_ScalarMin, 0 },
{ 0, SK_ScalarMin },
};
const size_t axisAlignedPtsCount = sizeof(axisAlignedPts) / sizeof(axisAlignedPts[0]);
for (int index = 0; index < (int) (13 * nonFinitePtsCount * axisAlignedPtsCount); ++index) {
int i = (int) (index % nonFinitePtsCount);
int f = (int) (index % axisAlignedPtsCount);
int g = (int) ((f + 1) % axisAlignedPtsCount);
path.reset();
switch (index % 13) {
case 0: path.lineTo(nonFinitePts[i]); break;
case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
case 2: path.quadTo(nonFinitePts[i], axisAlignedPts[f]); break;
case 3: path.quadTo(axisAlignedPts[f], nonFinitePts[i]); break;
case 4: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[f]); break;
case 5: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], axisAlignedPts[f]); break;
case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], nonFinitePts[i]); break;
case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], axisAlignedPts[f]); break;
case 8: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], nonFinitePts[i]); break;
case 9: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], nonFinitePts[i]); break;
case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
case 11: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[g]); break;
case 12: path.moveTo(nonFinitePts[i]); break;
}
check_convexity(reporter, path, SkPath::kUnknown_Convexity);
}
for (int index = 0; index < (int) (11 * axisAlignedPtsCount); ++index) {
int f = (int) (index % axisAlignedPtsCount);
int g = (int) ((f + 1) % axisAlignedPtsCount);
path.reset();
int curveSelect = index % 11;
switch (curveSelect) {
case 0: path.moveTo(axisAlignedPts[f]); break;
case 1: path.lineTo(axisAlignedPts[f]); break;
case 2: path.quadTo(axisAlignedPts[f], axisAlignedPts[f]); break;
case 3: path.quadTo(axisAlignedPts[f], axisAlignedPts[g]); break;
case 4: path.quadTo(axisAlignedPts[g], axisAlignedPts[f]); break;
case 5: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[f]); break;
case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[g]); break;
case 7: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[f]); break;
case 8: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[g]); break;
case 9: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[f]); break;
case 10: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[g]); break;
}
if (curveSelect == 0 || curveSelect == 1 || curveSelect == 2 || curveSelect == 5) {
check_convexity(reporter, path, SkPath::kConvex_Convexity);
} else {
SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
SkPath::Convexity c = copy.getConvexity();
REPORTER_ASSERT(reporter, SkPath::kUnknown_Convexity == c
|| SkPath::kConcave_Convexity == c);
}
}
static const SkPoint diagonalPts[] = {
{ SK_ScalarMax, SK_ScalarMax },
{ SK_ScalarMin, SK_ScalarMin },
};
const size_t diagonalPtsCount = sizeof(diagonalPts) / sizeof(diagonalPts[0]);
for (int index = 0; index < (int) (7 * diagonalPtsCount); ++index) {
int f = (int) (index % diagonalPtsCount);
int g = (int) ((f + 1) % diagonalPtsCount);
path.reset();
int curveSelect = index % 11;
switch (curveSelect) {
case 0: path.moveTo(diagonalPts[f]); break;
case 1: path.lineTo(diagonalPts[f]); break;
case 2: path.quadTo(diagonalPts[f], diagonalPts[f]); break;
case 3: path.quadTo(axisAlignedPts[f], diagonalPts[g]); break;
case 4: path.quadTo(diagonalPts[g], axisAlignedPts[f]); break;
case 5: path.cubicTo(diagonalPts[f], diagonalPts[f], diagonalPts[f]); break;
case 6: path.cubicTo(diagonalPts[f], diagonalPts[f], axisAlignedPts[g]); break;
case 7: path.cubicTo(diagonalPts[f], axisAlignedPts[g], diagonalPts[f]); break;
case 8: path.cubicTo(axisAlignedPts[f], diagonalPts[g], diagonalPts[g]); break;
case 9: path.cubicTo(diagonalPts[g], diagonalPts[f], axisAlignedPts[f]); break;
case 10: path.cubicTo(diagonalPts[g], axisAlignedPts[f], diagonalPts[g]); break;
}
if (curveSelect == 0) {
check_convexity(reporter, path, SkPath::kConvex_Convexity);
} else {
SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
SkPath::Convexity c = copy.getConvexity();
REPORTER_ASSERT(reporter, SkPath::kUnknown_Convexity == c
|| SkPath::kConcave_Convexity == c);
}
}
path.reset();
path.moveTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eea38)); // -0.284072f, -0.0622351f
path.lineTo(SkBits2Float(0xbe9171a0), SkBits2Float(0xbd7ee5a7)); // -0.28407f, -0.0622307f
path.lineTo(SkBits2Float(0xbe917147), SkBits2Float(0xbd7ed886)); // -0.284067f, -0.0622182f
path.lineTo(SkBits2Float(0xbe917378), SkBits2Float(0xbd7ee1a9)); // -0.284084f, -0.0622269f
path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
path.close();
check_convexity(reporter, path, SkPath::kConcave_Convexity);
}
static void test_isLine(skiatest::Reporter* reporter) {
SkPath path;
SkPoint pts[2];
const SkScalar value = SkIntToScalar(5);
REPORTER_ASSERT(reporter, !path.isLine(nullptr));
// set some non-zero values
pts[0].set(value, value);
pts[1].set(value, value);
REPORTER_ASSERT(reporter, !path.isLine(pts));
// check that pts was untouched
REPORTER_ASSERT(reporter, pts[0].equals(value, value));
REPORTER_ASSERT(reporter, pts[1].equals(value, value));
const SkScalar moveX = SkIntToScalar(1);
const SkScalar moveY = SkIntToScalar(2);
REPORTER_ASSERT(reporter, value != moveX && value != moveY);
path.moveTo(moveX, moveY);
REPORTER_ASSERT(reporter, !path.isLine(nullptr));
REPORTER_ASSERT(reporter, !path.isLine(pts));
// check that pts was untouched
REPORTER_ASSERT(reporter, pts[0].equals(value, value));
REPORTER_ASSERT(reporter, pts[1].equals(value, value));
const SkScalar lineX = SkIntToScalar(2);
const SkScalar lineY = SkIntToScalar(2);
REPORTER_ASSERT(reporter, value != lineX && value != lineY);
path.lineTo(lineX, lineY);
REPORTER_ASSERT(reporter, path.isLine(nullptr));
REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
REPORTER_ASSERT(reporter, path.isLine(pts));
REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
path.lineTo(0, 0); // too many points/verbs
REPORTER_ASSERT(reporter, !path.isLine(nullptr));
REPORTER_ASSERT(reporter, !path.isLine(pts));
REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
path.reset();
path.quadTo(1, 1, 2, 2);
REPORTER_ASSERT(reporter, !path.isLine(nullptr));
}
static void test_conservativelyContains(skiatest::Reporter* reporter) {
SkPath path;
// kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
// A circle that bounds kBaseRect (with a significant amount of slop)
SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
circleR *= 1.75f / 2;
static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
// round-rect radii
static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
static const struct SUPPRESS_VISIBILITY_WARNING {
SkRect fQueryRect;
bool fInRect;
bool fInCircle;
bool fInRR;
bool fInCubicRR;
} kQueries[] = {
{kBaseRect, true, true, false, false},
// rect well inside of kBaseRect
{SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
kBaseRect.fTop + 0.25f*kBaseRect.height(),
kBaseRect.fRight - 0.25f*kBaseRect.width(),
kBaseRect.fBottom - 0.25f*kBaseRect.height()),
true, true, true, true},
// rects with edges off by one from kBaseRect's edges
{SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
kBaseRect.width(), kBaseRect.height() + 1),
false, true, false, false},
{SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
kBaseRect.width() + 1, kBaseRect.height()),
false, true, false, false},
{SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
kBaseRect.width() + 1, kBaseRect.height() + 1),
false, true, false, false},
{SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
kBaseRect.width(), kBaseRect.height()),
false, true, false, false},
{SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
kBaseRect.width(), kBaseRect.height()),
false, true, false, false},
{SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
kBaseRect.width() + 2, kBaseRect.height()),
false, true, false, false},
{SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
kBaseRect.width() + 2, kBaseRect.height()),
false, true, false, false},
// zero-w/h rects at each corner of kBaseRect
{SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
{SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
{SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
{SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
// far away rect
{SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
SkIntToScalar(10), SkIntToScalar(10)),
false, false, false, false},
// very large rect containing kBaseRect
{SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
kBaseRect.fTop - 5 * kBaseRect.height(),
11 * kBaseRect.width(), 11 * kBaseRect.height()),
false, false, false, false},
// skinny rect that spans same y-range as kBaseRect
{SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
SkIntToScalar(1), kBaseRect.height()),
true, true, true, true},
// short rect that spans same x-range as kBaseRect
{SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
true, true, true, true},
// skinny rect that spans slightly larger y-range than kBaseRect
{SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
SkIntToScalar(1), kBaseRect.height() + 1),
false, true, false, false},
// short rect that spans slightly larger x-range than kBaseRect
{SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
kBaseRect.width() + 1, SkScalar(1)),
false, true, false, false},
};
for (int inv = 0; inv < 4; ++inv) {
for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
SkRect qRect = kQueries[q].fQueryRect;
if (inv & 0x1) {
using std::swap;
swap(qRect.fLeft, qRect.fRight);
}
if (inv & 0x2) {
using std::swap;
swap(qRect.fTop, qRect.fBottom);
}
for (int d = 0; d < 2; ++d) {
SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
path.reset();
path.addRect(kBaseRect, dir);
REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
path.conservativelyContainsRect(qRect));
path.reset();
path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
path.conservativelyContainsRect(qRect));
path.reset();
path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
path.conservativelyContainsRect(qRect));
path.reset();
path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
path.close();
REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
path.conservativelyContainsRect(qRect));
}
// Slightly non-convex shape, shouldn't contain any rects.
path.reset();
path.moveTo(0, 0);
path.lineTo(SkIntToScalar(50), 0.05f);
path.lineTo(SkIntToScalar(100), 0);
path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
path.lineTo(0, SkIntToScalar(100));
path.close();
REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
}
}
// make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
path.reset();
path.moveTo(0, 0);
path.lineTo(SkIntToScalar(100), 0);
path.lineTo(0, SkIntToScalar(100));
// inside, on along top edge
REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
SkIntToScalar(10),
SkIntToScalar(10))));
// above
REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
SkRect::MakeXYWH(SkIntToScalar(50),
SkIntToScalar(-10),
SkIntToScalar(10),
SkIntToScalar(10))));
// to the left
REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
SkIntToScalar(5),
SkIntToScalar(5),
SkIntToScalar(5))));
// outside the diagonal edge
REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
SkIntToScalar(200),
SkIntToScalar(20),
SkIntToScalar(5))));
// Test that multiple move commands do not cause asserts.
path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
SkIntToScalar(10),
SkIntToScalar(10))));
// Same as above path and first test but with an extra moveTo.
path.reset();
path.moveTo(100, 100);
path.moveTo(0, 0);
path.lineTo(SkIntToScalar(100), 0);
path.lineTo(0, SkIntToScalar(100));
// Convexity logic is now more conservative, so that multiple (non-trailing) moveTos make a
// path non-convex.
REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
SkRect::MakeXYWH(SkIntToScalar(50), 0,
SkIntToScalar(10),
SkIntToScalar(10))));
// Same as above path and first test but with the extra moveTo making a degenerate sub-path
// following the non-empty sub-path. Verifies that this does not trigger assertions.
path.reset();
path.moveTo(0, 0);
path.lineTo(SkIntToScalar(100), 0);
path.lineTo(0, SkIntToScalar(100));
path.moveTo(100, 100);
REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
SkIntToScalar(10),
SkIntToScalar(10))));
// Test that multiple move commands do not cause asserts and that the function
// is not confused by the multiple moves.
path.reset();
path.moveTo(0, 0);
path.lineTo(SkIntToScalar(100), 0);
path.lineTo(0, SkIntToScalar(100));
path.moveTo(0, SkIntToScalar(200));
path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
path.lineTo(0, SkIntToScalar(300));
REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
SkRect::MakeXYWH(SkIntToScalar(50), 0,
SkIntToScalar(10),
SkIntToScalar(10))));
path.reset();
path.lineTo(100, 100);
REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
// An empty path should not contain any rectangle. It's questionable whether an empty path
// contains an empty rectangle. However, since it is a conservative test it is ok to
// return false.
path.reset();
REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(1,1)));
REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(0,0)));
}
static void test_isRect_open_close(skiatest::Reporter* reporter) {
SkPath path;
bool isClosed;
path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
path.close();
REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
REPORTER_ASSERT(reporter, isClosed);
}
// Simple isRect test is inline TestPath, below.
// test_isRect provides more extensive testing.
static void test_isRect(skiatest::Reporter* reporter) {
test_isRect_open_close(reporter);
// passing tests (all moveTo / lineTo...
SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
// failing tests
SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
// no close, but we should detect them as fillably the same as a rect
SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
// like c2, but we double-back on ourselves
SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
// like c2, but we overshoot the start point
SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
struct IsRectTest {
SkPoint *fPoints;
int fPointCount;
bool fClose;
bool fIsRect;
} tests[] = {
{ r1, SK_ARRAY_COUNT(r1), true, true },
{ r2, SK_ARRAY_COUNT(r2), true, true },
{ r3, SK_ARRAY_COUNT(r3), true, true },
{ r4, SK_ARRAY_COUNT(r4), true, true },
{ r5, SK_ARRAY_COUNT(r5), true, true },
{ r6, SK_ARRAY_COUNT(r6), true, true },
{ r7, SK_ARRAY_COUNT(r7), true, true },
{ r8, SK_ARRAY_COUNT(r8), true, true },
{ r9, SK_ARRAY_COUNT(r9), true, true },
{ ra, SK_ARRAY_COUNT(ra), true, true },
{ rb, SK_ARRAY_COUNT(rb), true, true },
{ rc, SK_ARRAY_COUNT(rc), true, true },
{ rd, SK_ARRAY_COUNT(rd), true, true },
{ re, SK_ARRAY_COUNT(re), true, true },
{ rf, SK_ARRAY_COUNT(rf), true, true },
{ f1, SK_ARRAY_COUNT(f1), true, false },
{ f2, SK_ARRAY_COUNT(f2), true, false },
{ f3, SK_ARRAY_COUNT(f3), true, false },
{ f4, SK_ARRAY_COUNT(f4), true, false },
{ f5, SK_ARRAY_COUNT(f5), true, false },
{ f6, SK_ARRAY_COUNT(f6), true, false },
{ f7, SK_ARRAY_COUNT(f7), true, false },
{ f8, SK_ARRAY_COUNT(f8), true, false },
{ f9, SK_ARRAY_COUNT(f9), true, false },
{ fa, SK_ARRAY_COUNT(fa), true, false },
{ fb, SK_ARRAY_COUNT(fb), true, false },
{ c1, SK_ARRAY_COUNT(c1), false, true },
{ c2, SK_ARRAY_COUNT(c2), false, true },
{ c3, SK_ARRAY_COUNT(c3), false, true },
{ d1, SK_ARRAY_COUNT(d1), false, false },
{ d2, SK_ARRAY_COUNT(d2), false, true },
{ d3, SK_ARRAY_COUNT(d3), false, false },
};
const size_t testCount = SK_ARRAY_COUNT(tests);
int index;
for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
SkPath path;
path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
for (index = 1; index < tests[testIndex].fPointCount; ++index) {
path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
}
if (tests[testIndex].fClose) {
path.close();
}
REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
if (tests[testIndex].fIsRect) {
SkRect computed, expected;
bool isClosed;
SkPath::Direction direction;
SkPathPriv::FirstDirection cheapDirection;
int pointCount = tests[testIndex].fPointCount - (d2 == tests[testIndex].fPoints);
expected.setBounds(tests[testIndex].fPoints, pointCount);
REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
REPORTER_ASSERT(reporter, expected == computed);
REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
} else {
SkRect computed;
computed.setLTRB(123, 456, 789, 1011);
for (auto c : {true, false})
for (auto d : {SkPath::kCW_Direction, SkPath::kCCW_Direction}) {
bool isClosed = c;
SkPath::Direction direction = d;
REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
REPORTER_ASSERT(reporter, isClosed == c);
REPORTER_ASSERT(reporter, direction == d);
}
}
}
// fail, close then line
SkPath path1;
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
path1.lineTo(r1[index].fX, r1[index].fY);
}
path1.close();
path1.lineTo(1, 0);
REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
// fail, move in the middle
path1.reset();
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
if (index == 2) {
path1.moveTo(1, .5f);
}
path1.lineTo(r1[index].fX, r1[index].fY);
}
path1.close();
REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
// fail, move on the edge
path1.reset();
for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
path1.lineTo(r1[index].fX, r1[index].fY);
}
path1.close();
REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
// fail, quad
path1.reset();
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
if (index == 2) {
path1.quadTo(1, .5f, 1, .5f);
}
path1.lineTo(r1[index].fX, r1[index].fY);
}
path1.close();
REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
// fail, cubic
path1.reset();
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
if (index == 2) {
path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
}
path1.lineTo(r1[index].fX, r1[index].fY);
}
path1.close();
REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
}
static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
const SkRect& rect, SkPath::Direction dir, unsigned start) {
SkRect r = SkRect::MakeEmpty();
SkPath::Direction d = SkPath::kCCW_Direction;
unsigned s = ~0U;
REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
REPORTER_ASSERT(reporter, r == rect);
REPORTER_ASSERT(reporter, d == dir);
REPORTER_ASSERT(reporter, s == start);
}
static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
using std::swap;
SkRect r = SkRect::MakeEmpty();
SkPath::Direction d = SkPath::kCCW_Direction;
unsigned s = ~0U;
const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
const SkRect emptyRect = SkRect::MakeEmpty();
SkPath path;
for (int start = 0; start < 4; ++start) {
for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
SkPath path;
path.addRect(testRect, dir, start);
check_simple_closed_rect(reporter, path, testRect, dir, start);
path.close();
check_simple_closed_rect(reporter, path, testRect, dir, start);
SkPath path2 = path;
path2.lineTo(10, 10);
REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
path2 = path;
path2.moveTo(10, 10);
REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
path2 = path;
path2.addRect(testRect, dir, start);
REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
// Make the path by hand, manually closing it.
path2.reset();
SkPath::RawIter iter(path);
SkPath::Verb v;
SkPoint verbPts[4];
SkPoint firstPt = {0.f, 0.f};
while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
switch(v) {
case SkPath::kMove_Verb:
firstPt = verbPts[0];
path2.moveTo(verbPts[0]);
break;
case SkPath::kLine_Verb:
path2.lineTo(verbPts[1]);
break;
default:
break;
}
}
// We haven't closed it yet...
REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
// ... now we do and test again.
path2.lineTo(firstPt);
check_simple_closed_rect(reporter, path2, testRect, dir, start);
// A redundant close shouldn't cause a failure.
path2.close();
check_simple_closed_rect(reporter, path2, testRect, dir, start);
// Degenerate point and line rects are not allowed
path2.reset();
path2.addRect(emptyRect, dir, start);
REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
SkRect degenRect = testRect;
degenRect.fLeft = degenRect.fRight;
path2.reset();
path2.addRect(degenRect, dir, start);
REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
degenRect = testRect;
degenRect.fTop = degenRect.fBottom;
path2.reset();
path2.addRect(degenRect, dir, start);
REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
// An inverted rect makes a rect path, but changes the winding dir and start point.
SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
? SkPath::kCCW_Direction
: SkPath::kCW_Direction;
static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
SkRect swapRect = testRect;
swap(swapRect.fLeft, swapRect.fRight);
path2.reset();
path2.addRect(swapRect, dir, start);
check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
swapRect = testRect;
swap(swapRect.fTop, swapRect.fBottom);
path2.reset();
path2.addRect(swapRect, dir, start);
check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
}
}
// down, up, left, close
path.reset();
path.moveTo(1, 1);
path.lineTo(1, 2);
path.lineTo(1, 1);
path.lineTo(0, 1);
SkRect rect;
SkPath::Direction dir;
unsigned start;
path.close();
REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
// right, left, up, close
path.reset();
path.moveTo(1, 1);
path.lineTo(2, 1);
path.lineTo(1, 1);
path.lineTo(1, 0);
path.close();
REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
// parallelogram with horizontal edges
path.reset();
path.moveTo(1, 0);
path.lineTo(3, 0);
path.lineTo(2, 1);
path.lineTo(0, 1);
path.close();
REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
// parallelogram with vertical edges
path.reset();
path.moveTo(0, 1);
path.lineTo(0, 3);
path.lineTo(1, 2);
path.lineTo(1, 0);
path.close();
REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
}
static void test_isNestedFillRects(skiatest::Reporter* reporter) {
// passing tests (all moveTo / lineTo...
SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0},