blob: 6eb62f02bfb1e640bc40c36f16fc06df99ef27f6 [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"
Kevin Lubick177a7122022-12-12 10:25:15 -050013#include "include/core/SkPathUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkPoint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTypes.h"
Kevin Lubick46572b42023-01-18 13:11:06 -050020#include "include/private/base/SkTemplates.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021
22#include <float.h>
reed@google.com603dbed2012-11-20 19:00:28 +000023
Herb Derby3b3bcf02023-01-17 15:12:15 -050024using namespace skia_private;
25
reed@google.com603dbed2012-11-20 19:00:28 +000026#define STROKE_WIDTH SkIntToScalar(20)
27
reed@google.com99033952012-11-20 21:32:30 +000028static void draw_path(SkCanvas* canvas, const SkPath& path, const SkRect& rect,
29 SkPaint::Join join, int doFill) {
reed@google.com603dbed2012-11-20 19:00:28 +000030 SkPaint paint;
31 paint.setAntiAlias(true);
reed@google.com99033952012-11-20 21:32:30 +000032 paint.setStyle(doFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style);
reed@google.com603dbed2012-11-20 19:00:28 +000033
Mike Kleind46dce32018-08-16 10:17:03 -040034 paint.setColor(SK_ColorGRAY);
reed@google.com603dbed2012-11-20 19:00:28 +000035 paint.setStrokeWidth(STROKE_WIDTH);
36 paint.setStrokeJoin(join);
37 canvas->drawRect(rect, paint);
38
reed@google.com99033952012-11-20 21:32:30 +000039 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com603dbed2012-11-20 19:00:28 +000040 paint.setStrokeWidth(0);
41 paint.setColor(SK_ColorRED);
42 canvas->drawPath(path, paint);
43
44 paint.setStrokeWidth(3);
45 paint.setStrokeJoin(SkPaint::kMiter_Join);
46 int n = path.countPoints();
Herb Derby3b3bcf02023-01-17 15:12:15 -050047 AutoTArray<SkPoint> points(n);
reed@google.com603dbed2012-11-20 19:00:28 +000048 path.getPoints(points.get(), n);
49 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), paint);
50}
51
52/*
53 * Test calling SkStroker for rectangles. Cases to cover:
54 *
55 * geometry: normal, small (smaller than stroke-width), empty, inverted
56 * joint-type for the corners
57 */
58class StrokeRectGM : public skiagm::GM {
59public:
60 StrokeRectGM() {}
61
62protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000063 SkString getName() const override { return SkString("strokerect"); }
reed@google.com603dbed2012-11-20 19:00:28 +000064
Leandro Lovisolo8f023882023-08-15 21:13:52 +000065 SkISize getISize() override { return SkISize::Make(1400, 740); }
reed@google.com603dbed2012-11-20 19:00:28 +000066
mtklein36352bf2015-03-25 18:17:31 -070067 void onDraw(SkCanvas* canvas) override {
reed@google.com603dbed2012-11-20 19:00:28 +000068 canvas->drawColor(SK_ColorWHITE);
69 canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2);
70
71 SkPaint paint;
72 paint.setStyle(SkPaint::kStroke_Style);
73 paint.setStrokeWidth(STROKE_WIDTH);
74
mtkleindbfd7ab2016-09-01 11:24:54 -070075 constexpr SkPaint::Join gJoins[] = {
reed@google.com603dbed2012-11-20 19:00:28 +000076 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
77 };
78
mtkleindbfd7ab2016-09-01 11:24:54 -070079 constexpr SkScalar W = 80;
80 constexpr SkScalar H = 80;
81 constexpr SkRect gRects[] = {
reed@google.com603dbed2012-11-20 19:00:28 +000082 { 0, 0, W, H },
83 { W, 0, 0, H },
84 { 0, H, W, 0 },
85 { 0, 0, STROKE_WIDTH, H },
86 { 0, 0, W, STROKE_WIDTH },
87 { 0, 0, STROKE_WIDTH/2, STROKE_WIDTH/2 },
88 { 0, 0, W, 0 },
89 { 0, 0, 0, H },
90 { 0, 0, 0, 0 },
caryclarkc53b82e2015-12-22 07:50:16 -080091 { 0, 0, W, FLT_EPSILON },
92 { 0, 0, FLT_EPSILON, H },
93 { 0, 0, FLT_EPSILON, FLT_EPSILON },
reed@google.com603dbed2012-11-20 19:00:28 +000094 };
reed@google.com603dbed2012-11-20 19:00:28 +000095
reed@google.com99033952012-11-20 21:32:30 +000096 for (int doFill = 0; doFill <= 1; ++doFill) {
Herb Derbyc37b3862022-06-21 09:49:17 -040097 for (size_t i = 0; i < std::size(gJoins); ++i) {
reed@google.com99033952012-11-20 21:32:30 +000098 SkPaint::Join join = gJoins[i];
99 paint.setStrokeJoin(join);
reed@google.com603dbed2012-11-20 19:00:28 +0000100
reed@google.com99033952012-11-20 21:32:30 +0000101 SkAutoCanvasRestore acr(canvas, true);
Herb Derbyc37b3862022-06-21 09:49:17 -0400102 for (size_t j = 0; j < std::size(gRects); ++j) {
reed@google.com99033952012-11-20 21:32:30 +0000103 const SkRect& r = gRects[j];
reed@google.com603dbed2012-11-20 19:00:28 +0000104
reed@google.com99033952012-11-20 21:32:30 +0000105 SkPath path, fillPath;
106 path.addRect(r);
Kevin Lubickf5491282022-12-15 08:37:49 -0500107 skpathutils::FillPathWithPaint(path, paint, &fillPath);
reed@google.com99033952012-11-20 21:32:30 +0000108 draw_path(canvas, fillPath, r, join, doFill);
109
110 canvas->translate(W + 2 * STROKE_WIDTH, 0);
111 }
112 acr.restore();
113 canvas->translate(0, H + 2 * STROKE_WIDTH);
reed@google.com603dbed2012-11-20 19:00:28 +0000114 }
reed@google.com99033952012-11-20 21:32:30 +0000115 paint.setStyle(SkPaint::kStrokeAndFill_Style);
reed@google.com603dbed2012-11-20 19:00:28 +0000116 }
117 }
118
119private:
John Stiles7571f9e2020-09-02 22:42:33 -0400120 using INHERITED = GM;
reed@google.com603dbed2012-11-20 19:00:28 +0000121};
reed81511032016-06-10 13:21:07 -0700122DEF_GM(return new StrokeRectGM;)
reed@google.com603dbed2012-11-20 19:00:28 +0000123
124///////////////////////////////////////////////////////////////////////////////////////////////////
125
reed81511032016-06-10 13:21:07 -0700126/*
127 * Exercise rect-stroking (which is specialized from paths) when the resulting stroke-width is
128 * non-square. See https://bugs.chromium.org/p/skia/issues/detail?id=5408
129 */
130DEF_SIMPLE_GM(strokerect_anisotropic_5408, canvas, 200, 50) {
131 SkPaint p;
132 p.setStyle(SkPaint::kStroke_Style);
133 p.setStrokeWidth(6);
134
135 canvas->scale(10, 1);
136 SkRect r = SkRect::MakeXYWH(5, 20, 10, 10);
137 canvas->drawRect(r, p);
138}