blob: ab7f1063e2ae64ec91c265578d01fd6346511993 [file] [log] [blame]
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001/*
2 * Copyright 2011 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 */
bungemand3ebb482015-08-05 13:57:49 -07007
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/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPaint.h"
13#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkRect.h"
15#include "include/core/SkScalar.h"
16#include "include/core/SkTypeface.h"
17#include "include/core/SkTypes.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050018#include "src/base/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040020#include "tools/fonts/FontToolUtils.h"
schenney@chromium.org4da06ab2011-12-20 15:14:18 +000021
halcanary2a243382015-09-09 08:16:41 -070022static void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
23 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
Mike Reed7d34dc72019-11-26 12:17:17 -050024 SkPaint::Style style, SkPathFillType fill,
halcanary2a243382015-09-09 08:16:41 -070025 SkScalar strokeWidth) {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000026 path.setFillType(fill);
27 SkPaint paint;
28 paint.setStrokeCap(cap);
29 paint.setStrokeWidth(strokeWidth);
30 paint.setStrokeJoin(join);
31 paint.setColor(color);
32 paint.setStyle(style);
33 canvas->save();
34 canvas->clipRect(clip);
35 canvas->drawPath(path, paint);
36 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -070037}
rmistry@google.comd6176b02012-08-23 18:14:13 +000038
halcanary2a243382015-09-09 08:16:41 -070039static void draw(SkCanvas* canvas, bool doClose) {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000040 struct FillAndName {
Mike Reed7d34dc72019-11-26 12:17:17 -050041 SkPathFillType fFill;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000042 const char* fName;
43 };
mtkleindbfd7ab2016-09-01 11:24:54 -070044 constexpr FillAndName gFills[] = {
Mike Reed7d34dc72019-11-26 12:17:17 -050045 {SkPathFillType::kWinding, "Winding"},
46 {SkPathFillType::kEvenOdd, "Even / Odd"},
47 {SkPathFillType::kInverseWinding, "Inverse Winding"},
48 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000049 };
50 struct StyleAndName {
51 SkPaint::Style fStyle;
52 const char* fName;
53 };
mtkleindbfd7ab2016-09-01 11:24:54 -070054 constexpr StyleAndName gStyles[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000055 {SkPaint::kFill_Style, "Fill"},
56 {SkPaint::kStroke_Style, "Stroke"},
57 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
58 };
59 struct CapAndName {
60 SkPaint::Cap fCap;
61 SkPaint::Join fJoin;
62 const char* fName;
63 };
mtkleindbfd7ab2016-09-01 11:24:54 -070064 constexpr CapAndName gCaps[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000065 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
66 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
67 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
68 };
69 struct PathAndName {
70 SkPath fPath;
71 const char* fName;
72 };
73 PathAndName path;
74 path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1);
75 path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1);
halcanary2a243382015-09-09 08:16:41 -070076 if (doClose) {
77 path.fPath.close();
78 path.fName = "moveTo-line-close";
79 } else {
80 path.fName = "moveTo-line";
81 }
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000082
83 SkPaint titlePaint;
84 titlePaint.setColor(SK_ColorBLACK);
85 titlePaint.setAntiAlias(true);
Hal Canarydf2d27e2019-01-08 09:38:02 -050086
Kevin Lubicke836c3a2023-10-20 06:55:35 -040087 SkFont font(ToolUtils::DefaultPortableTypeface(), 15.0f);
Hal Canarydf2d27e2019-01-08 09:38:02 -050088
halcanary2a243382015-09-09 08:16:41 -070089 const char titleNoClose[] = "Line Drawn Into Rectangle Clips With "
90 "Indicated Style, Fill and Linecaps, with stroke width 10";
91 const char titleClose[] = "Line Closed Drawn Into Rectangle Clips With "
92 "Indicated Style, Fill and Linecaps, with stroke width 10";
93 const char* title = doClose ? titleClose : titleNoClose;
Hal Canarydf2d27e2019-01-08 09:38:02 -050094 canvas->drawString(title, 20.0f, 20.0f, font, titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000095
scroggof9d61012014-12-15 12:54:51 -080096 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000097 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
98 canvas->save();
99 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
100 canvas->save();
Herb Derbyc37b3862022-06-21 09:49:17 -0400101 for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000102 if (0 < cap) {
Herb Derbyc37b3862022-06-21 09:49:17 -0400103 canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000104 }
105 canvas->save();
Herb Derbyc37b3862022-06-21 09:49:17 -0400106 for (size_t fill = 0; fill < std::size(gFills); ++fill) {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000107 if (0 < fill) {
108 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
109 }
110 canvas->save();
Herb Derbyc37b3862022-06-21 09:49:17 -0400111 for (size_t style = 0; style < std::size(gStyles); ++style) {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000112 if (0 < style) {
113 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000114 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115
Mike Kleinea3f0142019-03-20 11:12:10 -0500116 SkColor color = ToolUtils::color_to_565(0xff007000);
halcanary2a243382015-09-09 08:16:41 -0700117 drawPath(path.fPath, canvas, color, rect,
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000118 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
119 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000121 SkPaint rectPaint;
122 rectPaint.setColor(SK_ColorBLACK);
123 rectPaint.setStyle(SkPaint::kStroke_Style);
124 rectPaint.setStrokeWidth(-1);
125 rectPaint.setAntiAlias(true);
126 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000127
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000128 SkPaint labelPaint;
129 labelPaint.setColor(color);
Hal Canarydf2d27e2019-01-08 09:38:02 -0500130 font.setSize(10);
131 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f,
132 font, labelPaint);
133 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f,
134 font, labelPaint);
135 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f,
136 font, labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000137 }
138 canvas->restore();
139 }
140 canvas->restore();
141 }
142 canvas->restore();
143 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -0700144}
145DEF_SIMPLE_GM(linepath, canvas, 1240, 390) {
146 draw(canvas, false);
147}
148DEF_SIMPLE_GM(lineclosepath, canvas, 1240, 390) {
149 draw(canvas, true);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000150}