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"
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(bool v) { return v ? "true" : "false"; }
31
32SkString 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.asBlendMode() != p.asBlendMode()) {
61        s.appendf("p.setBlendMode(SkBlendMode::k%s);\n",
62                  SkBlendMode_Name(p.getBlendMode_or(SkBlendMode::kSrcOver)));
63    }
64    if (p.getPathEffect()) {
65        s.appendf("p.setPathEffect(/*FIXME*/);\n");
66    }
67    if (p.getShader()) {
68        s.appendf("p.setShader(/*FIXME*/);\n");
69    }
70    if (p.getMaskFilter()) {
71        s.appendf("p.setMaskFilter(/*FIXME*/);\n");
72    }
73    if (p.getColorFilter()) {
74        s.appendf("p.setColorFilter(/*FIXME*/);\n");
75    }
76    if (p.getImageFilter()) {
77        s.appendf("p.setImageFilter(/*FIXME*/);\n");
78    }
79    return s;
80}
81
82void draw(SkCanvas* canvas) {
83    SkPaint p;
84    p.setColor(SK_ColorRED);
85    p.setAntiAlias(true);
86    p.setStyle(SkPaint::kStroke_Style);
87    p.setStrokeWidth(10);
88    p.setBlendMode(SkBlendMode::kDstOver);
89    p.setStrokeCap(SkPaint::kRound_Cap);
90    p.setShader(image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions()));
91
92    auto s = PaintStringDump(p);
93    SkDebugf("%s", s.c_str());
94}
95
96}  // END FIDDLE
97