blob: ce43d8d08327377a859ab78440267e7dec8d563a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkPoint.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkScalar.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
18#include "include/core/SkTypes.h"
19#include "include/private/SkTemplates.h"
20
21#include <float.h>
reed@google.com603dbed2012-11-20 19:00:28 +000022
23#define STROKE_WIDTH SkIntToScalar(20)
24
reed@google.com99033952012-11-20 21:32:30 +000025static void draw_path(SkCanvas* canvas, const SkPath& path, const SkRect& rect,
26 SkPaint::Join join, int doFill) {
reed@google.com603dbed2012-11-20 19:00:28 +000027 SkPaint paint;
28 paint.setAntiAlias(true);
reed@google.com99033952012-11-20 21:32:30 +000029 paint.setStyle(doFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style);
reed@google.com603dbed2012-11-20 19:00:28 +000030
Mike Kleind46dce32018-08-16 10:17:03 -040031 paint.setColor(SK_ColorGRAY);
reed@google.com603dbed2012-11-20 19:00:28 +000032 paint.setStrokeWidth(STROKE_WIDTH);
33 paint.setStrokeJoin(join);
34 canvas->drawRect(rect, paint);
35
reed@google.com99033952012-11-20 21:32:30 +000036 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com603dbed2012-11-20 19:00:28 +000037 paint.setStrokeWidth(0);
38 paint.setColor(SK_ColorRED);
39 canvas->drawPath(path, paint);
40
41 paint.setStrokeWidth(3);
42 paint.setStrokeJoin(SkPaint::kMiter_Join);
43 int n = path.countPoints();
44 SkAutoTArray<SkPoint> points(n);
45 path.getPoints(points.get(), n);
46 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), paint);
47}
48
49/*
50 * Test calling SkStroker for rectangles. Cases to cover:
51 *
52 * geometry: normal, small (smaller than stroke-width), empty, inverted
53 * joint-type for the corners
54 */
55class StrokeRectGM : public skiagm::GM {
56public:
57 StrokeRectGM() {}
58
59protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000060
mtklein36352bf2015-03-25 18:17:31 -070061 SkString onShortName() override {
reed@google.com603dbed2012-11-20 19:00:28 +000062 return SkString("strokerect");
63 }
64
mtklein36352bf2015-03-25 18:17:31 -070065 SkISize onISize() override {
bsalomon9c673812016-07-06 06:30:36 -070066 return SkISize::Make(1400, 740);
reed@google.com603dbed2012-11-20 19:00:28 +000067 }
68
mtklein36352bf2015-03-25 18:17:31 -070069 void onDraw(SkCanvas* canvas) override {
reed@google.com603dbed2012-11-20 19:00:28 +000070 canvas->drawColor(SK_ColorWHITE);
71 canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2);
72
73 SkPaint paint;
74 paint.setStyle(SkPaint::kStroke_Style);
75 paint.setStrokeWidth(STROKE_WIDTH);
76
mtkleindbfd7ab2016-09-01 11:24:54 -070077 constexpr SkPaint::Join gJoins[] = {
reed@google.com603dbed2012-11-20 19:00:28 +000078 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
79 };
80
mtkleindbfd7ab2016-09-01 11:24:54 -070081 constexpr SkScalar W = 80;
82 constexpr SkScalar H = 80;
83 constexpr SkRect gRects[] = {
reed@google.com603dbed2012-11-20 19:00:28 +000084 { 0, 0, W, H },
85 { W, 0, 0, H },
86 { 0, H, W, 0 },
87 { 0, 0, STROKE_WIDTH, H },
88 { 0, 0, W, STROKE_WIDTH },
89 { 0, 0, STROKE_WIDTH/2, STROKE_WIDTH/2 },
90 { 0, 0, W, 0 },
91 { 0, 0, 0, H },
92 { 0, 0, 0, 0 },
caryclarkc53b82e2015-12-22 07:50:16 -080093 { 0, 0, W, FLT_EPSILON },
94 { 0, 0, FLT_EPSILON, H },
95 { 0, 0, FLT_EPSILON, FLT_EPSILON },
reed@google.com603dbed2012-11-20 19:00:28 +000096 };
reed@google.com603dbed2012-11-20 19:00:28 +000097
reed@google.com99033952012-11-20 21:32:30 +000098 for (int doFill = 0; doFill <= 1; ++doFill) {
99 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); ++i) {
100 SkPaint::Join join = gJoins[i];
101 paint.setStrokeJoin(join);
reed@google.com603dbed2012-11-20 19:00:28 +0000102
reed@google.com99033952012-11-20 21:32:30 +0000103 SkAutoCanvasRestore acr(canvas, true);
104 for (size_t j = 0; j < SK_ARRAY_COUNT(gRects); ++j) {
105 const SkRect& r = gRects[j];
reed@google.com603dbed2012-11-20 19:00:28 +0000106
reed@google.com99033952012-11-20 21:32:30 +0000107 SkPath path, fillPath;
108 path.addRect(r);
109 paint.getFillPath(path, &fillPath);
110 draw_path(canvas, fillPath, r, join, doFill);
111
112 canvas->translate(W + 2 * STROKE_WIDTH, 0);
113 }
114 acr.restore();
115 canvas->translate(0, H + 2 * STROKE_WIDTH);
reed@google.com603dbed2012-11-20 19:00:28 +0000116 }
reed@google.com99033952012-11-20 21:32:30 +0000117 paint.setStyle(SkPaint::kStrokeAndFill_Style);
reed@google.com603dbed2012-11-20 19:00:28 +0000118 }
119 }
120
121private:
122 typedef GM INHERITED;
123};
reed81511032016-06-10 13:21:07 -0700124DEF_GM(return new StrokeRectGM;)
reed@google.com603dbed2012-11-20 19:00:28 +0000125
126///////////////////////////////////////////////////////////////////////////////////////////////////
127
reed81511032016-06-10 13:21:07 -0700128/*
129 * Exercise rect-stroking (which is specialized from paths) when the resulting stroke-width is
130 * non-square. See https://bugs.chromium.org/p/skia/issues/detail?id=5408
131 */
132DEF_SIMPLE_GM(strokerect_anisotropic_5408, canvas, 200, 50) {
133 SkPaint p;
134 p.setStyle(SkPaint::kStroke_Style);
135 p.setStrokeWidth(6);
136
137 canvas->scale(10, 1);
138 SkRect r = SkRect::MakeXYWH(5, 20, 10, 10);
139 canvas->drawRect(r, p);
140}