blob: 15220c4a3b6cdbc814999f0e04567a21c3cd9f74 [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
reed@google.com603dbed2012-11-20 19:00:28 +00008#include "SkPaint.h"
9#include "SkPath.h"
10#include "SkRect.h"
11#include "SkStroke.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "Test.h"
reed@google.com603dbed2012-11-20 19:00:28 +000013
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_strokerect(skiatest::Reporter* reporter) {
22 const SkScalar width = SkIntToScalar(10);
23 SkPaint paint;
24
25 paint.setStyle(SkPaint::kStroke_Style);
26 paint.setStrokeWidth(width);
27
28 SkRect r = { 0, 0, SkIntToScalar(200), SkIntToScalar(100) };
29
30 SkRect outer(r);
31 outer.outset(width/2, width/2);
32
33 static const SkPaint::Join joins[] = {
34 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
35 };
36
37 for (size_t i = 0; i < SK_ARRAY_COUNT(joins); ++i) {
38 paint.setStrokeJoin(joins[i]);
39
40 SkPath path, fillPath;
41 path.addRect(r);
42 paint.getFillPath(path, &fillPath);
43
44 REPORTER_ASSERT(reporter, equal(outer, fillPath.getBounds()));
skia.committer@gmail.comb0a327e2012-11-21 02:02:25 +000045
reed@google.com603dbed2012-11-20 19:00:28 +000046 bool isMiter = SkPaint::kMiter_Join == joins[i];
47 SkRect nested[2];
48 REPORTER_ASSERT(reporter, fillPath.isNestedRects(nested) == isMiter);
49 if (isMiter) {
50 SkRect inner(r);
51 inner.inset(width/2, width/2);
52 REPORTER_ASSERT(reporter, equal(nested[0], outer));
53 REPORTER_ASSERT(reporter, equal(nested[1], inner));
54 }
55 }
56}
57
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000058DEF_TEST(Stroke, reporter) {
reed@google.com603dbed2012-11-20 19:00:28 +000059 test_strokerect(reporter);
60}