1 // 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"
4 REG_FIDDLE(PaintDump, 256, 256, true, 0) {
str(SkPaint::Cap v)5 static 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 }
str(SkPaint::Join v)13 static 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 }
str(SkPaint::Style v)21 static 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 
str(bool v)30 static const char* str(bool v) { return v ? "true" : "false"; }
31 
PaintStringDump(const SkPaint & p)32 SkString PaintStringDump(const SkPaint& p) {
33     SkString s("SkPaint p;\n");
34     SkPaint d;
35     if (d.getStrokeWidth() != p.getStrokeWidth()) {
36         s.appendf("p.setStrokeWidth(%.9g);\n", p.getStrokeWidth());
37     }
38     if (d.getStrokeMiter() != p.getStrokeMiter()) {
39         s.appendf("p.setStrokeMiter(%.9g);\n", p.getStrokeMiter());
40     }
41     SkColor4f c = p.getColor4f();
42     if (c != d.getColor4f()) {
43         s.appendf("p.setColor4f({%.9g, %.9g, %.9g, %.9g}, nullptr);\n", c.fR, c.fG, c.fB, c.fA);
44     }
45     if (d.isAntiAlias() != p.isAntiAlias()) {
46         s.appendf("p.setAntiAlias(%s);\n", str(p.isAntiAlias()));
47     }
48     if (d.isDither() != p.isDither()) {
49         s.appendf("p.setDither(%s);\n", str(p.isDither()));
50     }
51     if (d.getStrokeCap() != p.getStrokeCap()) {
52         s.appendf("p.setStrokeCap(%s);\n", str(p.getStrokeCap()));
53     }
54     if (d.getStrokeJoin() != p.getStrokeJoin()) {
55         s.appendf("p.setStrokeJoin(%s);\n", str(p.getStrokeJoin()));
56     }
57     if (d.getStyle() != p.getStyle()) {
58         s.appendf("p.setStyle(%s);\n", str(p.getStyle()));
59     }
60     if (d.getBlendMode() != p.getBlendMode()) {
61         s.appendf("p.setBlendMode(SkBlendMode::k%s);\n", SkBlendMode_Name(p.getBlendMode()));
62     }
63     if (p.getPathEffect()) {
64         s.appendf("p.setPathEffect(/*FIXME*/);\n");
65     }
66     if (p.getShader()) {
67         s.appendf("p.setShader(/*FIXME*/);\n");
68     }
69     if (p.getMaskFilter()) {
70         s.appendf("p.setMaskFilter(/*FIXME*/);\n");
71     }
72     if (p.getColorFilter()) {
73         s.appendf("p.setColorFilter(/*FIXME*/);\n");
74     }
75     if (p.getImageFilter()) {
76         s.appendf("p.setImageFilter(/*FIXME*/);\n");
77     }
78     return s;
79 }
80 
draw(SkCanvas * canvas)81 void draw(SkCanvas* canvas) {
82     SkPaint p;
83     p.setColor(SK_ColorRED);
84     p.setAntiAlias(true);
85     p.setStyle(SkPaint::kStroke_Style);
86     p.setStrokeWidth(10);
87     p.setBlendMode(SkBlendMode::kDstOver);
88     p.setStrokeCap(SkPaint::kRound_Cap);
89     p.setShader(image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions()));
90 
91     auto s = PaintStringDump(p);
92     SkDebugf("%s", s.c_str());
93 }
94 
95 }  // END FIDDLE
96