StrokeTest.cpp revision 95bc5f349561fef2d6fbae71adb08cf5c2eec0c9
1/*
2 * Copyright 2012 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
8#include "SkPaint.h"
9#include "SkPath.h"
10#include "SkRect.h"
11#include "SkStroke.h"
12#include "Test.h"
13
14static bool equal(const SkRect& a, const SkRect& b) {
15    return  SkScalarNearlyEqual(a.left(), b.left()) &&
16            SkScalarNearlyEqual(a.top(), b.top()) &&
17            SkScalarNearlyEqual(a.right(), b.right()) &&
18            SkScalarNearlyEqual(a.bottom(), b.bottom());
19}
20
21static void test_strokecubic(skiatest::Reporter* reporter) {
22    uint32_t hexCubicVals[] = {
23        0x424c1086, 0x44bcf0cb,  // fX=51.0161362 fY=1511.52478
24        0x424c107c, 0x44bcf0cb,  // fX=51.0160980 fY=1511.52478
25        0x424c10c2, 0x44bcf0cb,  // fX=51.0163651 fY=1511.52478
26        0x424c1119, 0x44bcf0ca,  // fX=51.0166969 fY=1511.52466
27    };
28    SkPoint cubicVals[] = {
29        {51.0161362f, 1511.52478f },
30        {51.0160980f, 1511.52478f },
31        {51.0163651f, 1511.52478f },
32        {51.0166969f, 1511.52466f },
33    };
34    SkPaint paint;
35
36    paint.setStyle(SkPaint::kStroke_Style);
37    paint.setStrokeWidth(0.394537568f);
38    SkPath path, fillPath;
39    path.moveTo(cubicVals[0]);
40    path.cubicTo(cubicVals[1], cubicVals[2], cubicVals[3]);
41    paint.getFillPath(path, &fillPath);
42    path.reset();
43    path.moveTo(SkBits2Float(hexCubicVals[0]), SkBits2Float(hexCubicVals[1]));
44    path.cubicTo(SkBits2Float(hexCubicVals[2]), SkBits2Float(hexCubicVals[3]),
45            SkBits2Float(hexCubicVals[4]), SkBits2Float(hexCubicVals[5]),
46            SkBits2Float(hexCubicVals[6]), SkBits2Float(hexCubicVals[7]));
47    paint.getFillPath(path, &fillPath);
48}
49
50static void test_strokerect(skiatest::Reporter* reporter) {
51    const SkScalar width = SkIntToScalar(10);
52    SkPaint paint;
53
54    paint.setStyle(SkPaint::kStroke_Style);
55    paint.setStrokeWidth(width);
56
57    SkRect r = { 0, 0, SkIntToScalar(200), SkIntToScalar(100) };
58
59    SkRect outer(r);
60    outer.outset(width/2, width/2);
61
62    static const SkPaint::Join joins[] = {
63        SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
64    };
65
66    for (size_t i = 0; i < SK_ARRAY_COUNT(joins); ++i) {
67        paint.setStrokeJoin(joins[i]);
68
69        SkPath path, fillPath;
70        path.addRect(r);
71        paint.getFillPath(path, &fillPath);
72
73        REPORTER_ASSERT(reporter, equal(outer, fillPath.getBounds()));
74
75        bool isMiter = SkPaint::kMiter_Join == joins[i];
76        SkRect nested[2];
77        REPORTER_ASSERT(reporter, fillPath.isNestedFillRects(nested) == isMiter);
78        if (isMiter) {
79            SkRect inner(r);
80            inner.inset(width/2, width/2);
81            REPORTER_ASSERT(reporter, equal(nested[0], outer));
82            REPORTER_ASSERT(reporter, equal(nested[1], inner));
83        }
84    }
85}
86
87DEF_TEST(Stroke, reporter) {
88    test_strokecubic(reporter);
89    test_strokerect(reporter);
90}
91