blob: 489f7b4d3fba798ce2d9304d780499e3b7c0e89e [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
reed@google.com99033952012-11-20 21:32:30 +000014static void draw_path(SkCanvas* canvas, const SkPath& path, const SkRect& rect,
15 SkPaint::Join join, int doFill) {
reed@google.com603dbed2012-11-20 19:00:28 +000016 SkPaint paint;
17 paint.setAntiAlias(true);
reed@google.com99033952012-11-20 21:32:30 +000018 paint.setStyle(doFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style);
reed@google.com603dbed2012-11-20 19:00:28 +000019
20 paint.setColor(SK_ColorGRAY);
21 paint.setStrokeWidth(STROKE_WIDTH);
22 paint.setStrokeJoin(join);
23 canvas->drawRect(rect, paint);
24
reed@google.com99033952012-11-20 21:32:30 +000025 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com603dbed2012-11-20 19:00:28 +000026 paint.setStrokeWidth(0);
27 paint.setColor(SK_ColorRED);
28 canvas->drawPath(path, paint);
29
30 paint.setStrokeWidth(3);
31 paint.setStrokeJoin(SkPaint::kMiter_Join);
32 int n = path.countPoints();
33 SkAutoTArray<SkPoint> points(n);
34 path.getPoints(points.get(), n);
35 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), paint);
36}
37
38/*
39 * Test calling SkStroker for rectangles. Cases to cover:
40 *
41 * geometry: normal, small (smaller than stroke-width), empty, inverted
42 * joint-type for the corners
43 */
44class StrokeRectGM : public skiagm::GM {
45public:
46 StrokeRectGM() {}
47
48protected:
49 virtual SkString onShortName() {
50 return SkString("strokerect");
51 }
52
53 virtual SkISize onISize() {
reed@google.com99033952012-11-20 21:32:30 +000054 return SkISize::Make(1024, 740);
reed@google.com603dbed2012-11-20 19:00:28 +000055 }
56
57 virtual void onDraw(SkCanvas* canvas) {
58 canvas->drawColor(SK_ColorWHITE);
59 canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2);
60
61 SkPaint paint;
62 paint.setStyle(SkPaint::kStroke_Style);
63 paint.setStrokeWidth(STROKE_WIDTH);
64
65 static const SkPaint::Join gJoins[] = {
66 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
67 };
68
69 static const SkScalar W = 80;
reed@google.com99033952012-11-20 21:32:30 +000070 static const SkScalar H = 80;
reed@google.com603dbed2012-11-20 19:00:28 +000071 static const SkRect gRects[] = {
72 { 0, 0, W, H },
73 { W, 0, 0, H },
74 { 0, H, W, 0 },
75 { 0, 0, STROKE_WIDTH, H },
76 { 0, 0, W, STROKE_WIDTH },
77 { 0, 0, STROKE_WIDTH/2, STROKE_WIDTH/2 },
78 { 0, 0, W, 0 },
79 { 0, 0, 0, H },
80 { 0, 0, 0, 0 },
81 };
reed@google.com603dbed2012-11-20 19:00:28 +000082
reed@google.com99033952012-11-20 21:32:30 +000083 for (int doFill = 0; doFill <= 1; ++doFill) {
84 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); ++i) {
85 SkPaint::Join join = gJoins[i];
86 paint.setStrokeJoin(join);
reed@google.com603dbed2012-11-20 19:00:28 +000087
reed@google.com99033952012-11-20 21:32:30 +000088 SkAutoCanvasRestore acr(canvas, true);
89 for (size_t j = 0; j < SK_ARRAY_COUNT(gRects); ++j) {
90 const SkRect& r = gRects[j];
reed@google.com603dbed2012-11-20 19:00:28 +000091
reed@google.com99033952012-11-20 21:32:30 +000092 SkPath path, fillPath;
93 path.addRect(r);
94 paint.getFillPath(path, &fillPath);
95 draw_path(canvas, fillPath, r, join, doFill);
96
97 canvas->translate(W + 2 * STROKE_WIDTH, 0);
98 }
99 acr.restore();
100 canvas->translate(0, H + 2 * STROKE_WIDTH);
reed@google.com603dbed2012-11-20 19:00:28 +0000101 }
reed@google.com99033952012-11-20 21:32:30 +0000102 paint.setStyle(SkPaint::kStrokeAndFill_Style);
reed@google.com603dbed2012-11-20 19:00:28 +0000103 }
104 }
105
106private:
107 typedef GM INHERITED;
108};
109
110///////////////////////////////////////////////////////////////////////////////////////////////////
111
112DEF_GM(return new StrokeRectGM;)