blob: 9090fde98370123c9ba581c0a8195cab22ff7bfe [file] [log] [blame]
Hal Canary6c8422c2020-01-10 15:22:09 -05001// Copyright 2020 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3#include "tools/fiddle/examples.h"
4REG_FIDDLE(PaintDump, 256, 256, true, 0) {
5static const char* str(SkPaint::Cap v) {
6 switch (v) {
7 case SkPaint::kButt_Cap: return "SkPaint::kButt_Cap";
8 case SkPaint::kRound_Cap: return "SkPaint::kRound_Cap";
9 case SkPaint::kSquare_Cap: return "SkPaint::kSquare_Cap";
10 default: return "?";
11 }
12}
13static const char* str(SkPaint::Join v) {
14 switch (v) {
15 case SkPaint::kMiter_Join: return "SkPaint::kMiter_Join";
16 case SkPaint::kRound_Join: return "SkPaint::kRound_Join";
17 case SkPaint::kBevel_Join: return "SkPaint::kBevel_Join";
18 default: return "?";
19 }
20}
21static const char* str(SkPaint::Style v) {
22 switch (v) {
23 case SkPaint::kFill_Style: return "SkPaint::kFill_Style";
24 case SkPaint::kStroke_Style: return "SkPaint::kStroke_Style";
25 case SkPaint::kStrokeAndFill_Style: return "SkPaint::kStrokeAndFill_Style";
26 default: return "?";
27 }
28}
29
30static const char* str(SkFilterQuality v) {
31 switch (v) {
32 case kNone_SkFilterQuality: return "kNone_SkFilterQuality";
33 case kLow_SkFilterQuality: return "kLow_SkFilterQuality";
34 case kMedium_SkFilterQuality: return "kMedium_SkFilterQuality";
35 case kHigh_SkFilterQuality: return "kHigh_SkFilterQuality";
36 default: return "?";
37 }
38}
39
40static const char* str(bool v) { return v ? "true" : "false"; }
41
42SkString PaintStringDump(const SkPaint& p) {
43 SkString s("SkPaint p;\n");
44 SkPaint d;
45 if (d.getStrokeWidth() != p.getStrokeWidth()) {
46 s.appendf("p.setStrokeWidth(%.9g);\n", p.getStrokeWidth());
47 }
48 if (d.getStrokeMiter() != p.getStrokeMiter()) {
49 s.appendf("p.setStrokeMiter(%.9g);\n", p.getStrokeMiter());
50 }
51 SkColor4f c = p.getColor4f();
52 if (c != d.getColor4f()) {
53 s.appendf("p.setColor4f({%.9g, %.9g, %.9g, %.9g}, nullptr);\n", c.fR, c.fG, c.fB, c.fA);
54 }
55 if (d.isAntiAlias() != p.isAntiAlias()) {
56 s.appendf("p.setAntiAlias(%s);\n", str(p.isAntiAlias()));
57 }
58 if (d.isDither() != p.isDither()) {
59 s.appendf("p.setDither(%s);\n", str(p.isDither()));
60 }
61 if (d.getStrokeCap() != p.getStrokeCap()) {
62 s.appendf("p.setStrokeCap(%s);\n", str(p.getStrokeCap()));
63 }
64 if (d.getStrokeJoin() != p.getStrokeJoin()) {
65 s.appendf("p.setStrokeJoin(%s);\n", str(p.getStrokeJoin()));
66 }
67 if (d.getStyle() != p.getStyle()) {
68 s.appendf("p.setStyle(%s);\n", str(p.getStyle()));
69 }
70 if (d.getFilterQuality() != p.getFilterQuality()) {
71 s.appendf("p.setFilterQuality(%s);\n", str(p.getFilterQuality()));
72 }
73 if (d.getBlendMode() != p.getBlendMode()) {
74 s.appendf("p.setBlendMode(SkBlendMode::k%s);\n", SkBlendMode_Name(p.getBlendMode()));
75 }
76 if (p.getPathEffect()) {
77 s.appendf("p.setPathEffect(/*FIXME*/);\n");
78 }
79 if (p.getShader()) {
80 s.appendf("p.setShader(/*FIXME*/);\n");
81 }
82 if (p.getMaskFilter()) {
83 s.appendf("p.setMaskFilter(/*FIXME*/);\n");
84 }
85 if (p.getColorFilter()) {
86 s.appendf("p.setColorFilter(/*FIXME*/);\n");
87 }
88 if (p.getImageFilter()) {
89 s.appendf("p.setImageFilter(/*FIXME*/);\n");
90 }
91 return s;
92}
93
94void draw(SkCanvas* canvas) {
95 SkPaint p;
96 p.setColor(SK_ColorRED);
97 p.setAntiAlias(true);
98 p.setStyle(SkPaint::kStroke_Style);
99 p.setStrokeWidth(10);
100 p.setBlendMode(SkBlendMode::kDstOver);
101 p.setStrokeCap(SkPaint::kRound_Cap);
102 p.setShader(image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat));
103
104 auto s = PaintStringDump(p);
105 SkDebugf("%s", s.c_str());
106}
107
108} // END FIDDLE