blob: f075600e470de26f092e1df428e63582177e9954 [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 "gm.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11
12#define STROKE_WIDTH SkIntToScalar(20)
13
14static void draw_path(SkCanvas* canvas, const SkPath& path, const SkRect& rect, SkPaint::Join join) {
15 SkPaint paint;
16 paint.setAntiAlias(true);
17 paint.setStyle(SkPaint::kStroke_Style);
18
19 paint.setColor(SK_ColorGRAY);
20 paint.setStrokeWidth(STROKE_WIDTH);
21 paint.setStrokeJoin(join);
22 canvas->drawRect(rect, paint);
23
24 paint.setStrokeWidth(0);
25 paint.setColor(SK_ColorRED);
26 canvas->drawPath(path, paint);
27
28 paint.setStrokeWidth(3);
29 paint.setStrokeJoin(SkPaint::kMiter_Join);
30 int n = path.countPoints();
31 SkAutoTArray<SkPoint> points(n);
32 path.getPoints(points.get(), n);
33 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), paint);
34}
35
36/*
37 * Test calling SkStroker for rectangles. Cases to cover:
38 *
39 * geometry: normal, small (smaller than stroke-width), empty, inverted
40 * joint-type for the corners
41 */
42class StrokeRectGM : public skiagm::GM {
43public:
44 StrokeRectGM() {}
45
46protected:
47 virtual SkString onShortName() {
48 return SkString("strokerect");
49 }
50
51 virtual SkISize onISize() {
52 return SkISize::Make(1024, 480);
53 }
54
55 virtual void onDraw(SkCanvas* canvas) {
56 canvas->drawColor(SK_ColorWHITE);
57 canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2);
58
59 SkPaint paint;
60 paint.setStyle(SkPaint::kStroke_Style);
61 paint.setStrokeWidth(STROKE_WIDTH);
62
63 static const SkPaint::Join gJoins[] = {
64 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
65 };
66
67 static const SkScalar W = 80;
68 static const SkScalar H = 110;
69 static const SkRect gRects[] = {
70 { 0, 0, W, H },
71 { W, 0, 0, H },
72 { 0, H, W, 0 },
73 { 0, 0, STROKE_WIDTH, H },
74 { 0, 0, W, STROKE_WIDTH },
75 { 0, 0, STROKE_WIDTH/2, STROKE_WIDTH/2 },
76 { 0, 0, W, 0 },
77 { 0, 0, 0, H },
78 { 0, 0, 0, 0 },
79 };
80
81 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); ++i) {
82 SkPaint::Join join = gJoins[i];
83 paint.setStrokeJoin(join);
84
85 SkAutoCanvasRestore acr(canvas, true);
86 for (size_t j = 0; j < SK_ARRAY_COUNT(gRects); ++j) {
87 const SkRect& r = gRects[j];
88
89 SkPath path, fillPath;
90 path.addRect(r);
91 paint.getFillPath(path, &fillPath);
92 draw_path(canvas, fillPath, r, join);
93
94 canvas->translate(W + 2 * STROKE_WIDTH, 0);
95 }
96 acr.restore();
97 canvas->translate(0, H + 2 * STROKE_WIDTH);
98 }
99 }
100
101private:
102 typedef GM INHERITED;
103};
104
105///////////////////////////////////////////////////////////////////////////////////////////////////
106
107DEF_GM(return new StrokeRectGM;)
108