blob: c4e6ffd94698c0c22794e42bc9946cee8fac389c [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/utils/SkRandom.h"
19#include "tools/ToolUtils.h"
schenney@chromium.org4da06ab2011-12-20 15:14:18 +000020
halcanary2a243382015-09-09 08:16:41 -070021static void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
22 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
Mike Reed7d34dc72019-11-26 12:17:17 -050023 SkPaint::Style style, SkPathFillType fill,
halcanary2a243382015-09-09 08:16:41 -070024 SkScalar strokeWidth) {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000025 path.setFillType(fill);
26 SkPaint paint;
27 paint.setStrokeCap(cap);
28 paint.setStrokeWidth(strokeWidth);
29 paint.setStrokeJoin(join);
30 paint.setColor(color);
31 paint.setStyle(style);
32 canvas->save();
33 canvas->clipRect(clip);
34 canvas->drawPath(path, paint);
35 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -070036}
rmistry@google.comd6176b02012-08-23 18:14:13 +000037
halcanary2a243382015-09-09 08:16:41 -070038static void draw(SkCanvas* canvas, bool doClose) {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000039 struct FillAndName {
Mike Reed7d34dc72019-11-26 12:17:17 -050040 SkPathFillType fFill;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000041 const char* fName;
42 };
mtkleindbfd7ab2016-09-01 11:24:54 -070043 constexpr FillAndName gFills[] = {
Mike Reed7d34dc72019-11-26 12:17:17 -050044 {SkPathFillType::kWinding, "Winding"},
45 {SkPathFillType::kEvenOdd, "Even / Odd"},
46 {SkPathFillType::kInverseWinding, "Inverse Winding"},
47 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000048 };
49 struct StyleAndName {
50 SkPaint::Style fStyle;
51 const char* fName;
52 };
mtkleindbfd7ab2016-09-01 11:24:54 -070053 constexpr StyleAndName gStyles[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000054 {SkPaint::kFill_Style, "Fill"},
55 {SkPaint::kStroke_Style, "Stroke"},
56 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
57 };
58 struct CapAndName {
59 SkPaint::Cap fCap;
60 SkPaint::Join fJoin;
61 const char* fName;
62 };
mtkleindbfd7ab2016-09-01 11:24:54 -070063 constexpr CapAndName gCaps[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000064 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
65 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
66 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
67 };
68 struct PathAndName {
69 SkPath fPath;
70 const char* fName;
71 };
72 PathAndName path;
73 path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1);
74 path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1);
halcanary2a243382015-09-09 08:16:41 -070075 if (doClose) {
76 path.fPath.close();
77 path.fName = "moveTo-line-close";
78 } else {
79 path.fName = "moveTo-line";
80 }
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000081
82 SkPaint titlePaint;
83 titlePaint.setColor(SK_ColorBLACK);
84 titlePaint.setAntiAlias(true);
Hal Canarydf2d27e2019-01-08 09:38:02 -050085
Mike Kleinea3f0142019-03-20 11:12:10 -050086 SkFont font(ToolUtils::create_portable_typeface(), 15.0f);
Hal Canarydf2d27e2019-01-08 09:38:02 -050087
halcanary2a243382015-09-09 08:16:41 -070088 const char titleNoClose[] = "Line Drawn Into Rectangle Clips With "
89 "Indicated Style, Fill and Linecaps, with stroke width 10";
90 const char titleClose[] = "Line Closed Drawn Into Rectangle Clips With "
91 "Indicated Style, Fill and Linecaps, with stroke width 10";
92 const char* title = doClose ? titleClose : titleNoClose;
Hal Canarydf2d27e2019-01-08 09:38:02 -050093 canvas->drawString(title, 20.0f, 20.0f, font, titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000094
scroggof9d61012014-12-15 12:54:51 -080095 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000096 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
97 canvas->save();
98 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
99 canvas->save();
100 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
101 if (0 < cap) {
102 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
103 }
104 canvas->save();
105 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
106 if (0 < fill) {
107 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
108 }
109 canvas->save();
110 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
111 if (0 < style) {
112 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000113 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114
Mike Kleinea3f0142019-03-20 11:12:10 -0500115 SkColor color = ToolUtils::color_to_565(0xff007000);
halcanary2a243382015-09-09 08:16:41 -0700116 drawPath(path.fPath, canvas, color, rect,
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000117 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
118 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000120 SkPaint rectPaint;
121 rectPaint.setColor(SK_ColorBLACK);
122 rectPaint.setStyle(SkPaint::kStroke_Style);
123 rectPaint.setStrokeWidth(-1);
124 rectPaint.setAntiAlias(true);
125 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000126
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000127 SkPaint labelPaint;
128 labelPaint.setColor(color);
Hal Canarydf2d27e2019-01-08 09:38:02 -0500129 font.setSize(10);
130 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f,
131 font, labelPaint);
132 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f,
133 font, labelPaint);
134 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f,
135 font, labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000136 }
137 canvas->restore();
138 }
139 canvas->restore();
140 }
141 canvas->restore();
142 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -0700143}
144DEF_SIMPLE_GM(linepath, canvas, 1240, 390) {
145 draw(canvas, false);
146}
147DEF_SIMPLE_GM(lineclosepath, canvas, 1240, 390) {
148 draw(canvas, true);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000149}