blob: 1278bd4b853c435d0b2c93b527762edf2f5875ff [file] [log] [blame]
reed@google.com603dbed2012-11-20 19:00:28 +00001/*
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 "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@google.com603dbed2012-11-20 19:00:28 +000010#include "SkPaint.h"
11#include "SkPath.h"
12#include "SkRect.h"
13#include "SkStroke.h"
14
15static bool equal(const SkRect& a, const SkRect& b) {
16 return SkScalarNearlyEqual(a.left(), b.left()) &&
17 SkScalarNearlyEqual(a.top(), b.top()) &&
18 SkScalarNearlyEqual(a.right(), b.right()) &&
19 SkScalarNearlyEqual(a.bottom(), b.bottom());
20}
21
22static void test_strokerect(skiatest::Reporter* reporter) {
23 const SkScalar width = SkIntToScalar(10);
24 SkPaint paint;
25
26 paint.setStyle(SkPaint::kStroke_Style);
27 paint.setStrokeWidth(width);
28
29 SkRect r = { 0, 0, SkIntToScalar(200), SkIntToScalar(100) };
30
31 SkRect outer(r);
32 outer.outset(width/2, width/2);
33
34 static const SkPaint::Join joins[] = {
35 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
36 };
37
38 for (size_t i = 0; i < SK_ARRAY_COUNT(joins); ++i) {
39 paint.setStrokeJoin(joins[i]);
40
41 SkPath path, fillPath;
42 path.addRect(r);
43 paint.getFillPath(path, &fillPath);
44
45 REPORTER_ASSERT(reporter, equal(outer, fillPath.getBounds()));
skia.committer@gmail.comb0a327e2012-11-21 02:02:25 +000046
reed@google.com603dbed2012-11-20 19:00:28 +000047 bool isMiter = SkPaint::kMiter_Join == joins[i];
48 SkRect nested[2];
49 REPORTER_ASSERT(reporter, fillPath.isNestedRects(nested) == isMiter);
50 if (isMiter) {
51 SkRect inner(r);
52 inner.inset(width/2, width/2);
53 REPORTER_ASSERT(reporter, equal(nested[0], outer));
54 REPORTER_ASSERT(reporter, equal(nested[1], inner));
55 }
56 }
57}
58
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000059DEF_TEST(Stroke, reporter) {
reed@google.com603dbed2012-11-20 19:00:28 +000060 test_strokerect(reporter);
61}