Debian Codegraph @

tools/viewer/PathOverstrokeSlide.cpp

raw
1 /*
2 * Copyright 2016 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 */
7 #include "include/core/SkCanvas.h"
8 #include "include/core/SkPath.h"
9 #include "include/core/SkPathBuilder.h"
10 #include "include/core/SkPathUtils.h"
11 #include "tools/viewer/Slide.h"
12
13 #include <iostream>
14 #include <cmath>
15
16 #define PI SK_ScalarPI
17
18 #define LIN_SEGMENTS 10
19
20 class OverstrokeSlide : public Slide {
21 SkScalar fStroke;
22 int fPathType; // super lazy enum
23 bool fClosePath;
24 bool fDrawFillPath;
25 bool fDumpHex;
26
27 public:
28 OverstrokeSlide() {
29 fStroke = 5;
30 fPathType = 0;
31 fClosePath = false;
32 fDrawFillPath = false;
33 fDumpHex = false;
34 fName = "PathOverstroke";
35 }
36
37 bool onChar(SkUnichar uni) override {
38 switch (uni) {
39 case ',':
40 fStroke += 1.0;
41 return true;
42 case '.':
43 fStroke -= 1.0;
44 return true;
45 case 'x':
46 fPathType = (fPathType + 1) % 4;
47 return true;
48 case 'c':
49 fClosePath = !fClosePath;
50 return true;
51 case 'f':
52 fDrawFillPath = !fDrawFillPath;
53 return true;
54 case 'D':
55 fDumpHex = !fDumpHex;
56 return true;
57 default:
58 break;
59 }
60 return false;
61 }
62
63 SkPath quadPath(SkPoint p1, SkPoint p2) {
64 SkASSERT(p1.y() == p2.y());
65
66 SkPathBuilder path;
67 path.moveTo(p1);
68 path.lineTo(p2);
69
70 SkPoint p3 = SkPoint::Make((p1.x() + p2.x()) / 2.0f, p1.y() * 0.7f);
71
72 path.quadTo(p3, p1);
73
74 return path.detach();
75 }
76
77 SkPath cubicPath(SkPoint p1, SkPoint p2) {
78 SkASSERT(p1.y() == p2.y());
79
80 SkPathBuilder path;
81 path.moveTo(p1);
82
83 SkPoint p3 = SkPoint::Make((p1.x() + p2.x()) / 3.0f, p1.y() * 0.7f);
84 SkPoint p4 = SkPoint::Make(2.0f*(p1.x() + p2.x()) / 3.0f, p1.y() * 1.5f);
85
86 path.cubicTo(p3, p4, p2);
87
88 return path.detach();
89 }
90
91 SkPath linSemicirclePath(SkPoint p1, SkPoint p2) {
92 SkASSERT(p1.y() == p2.y());
93
94 SkPathBuilder path;
95 path.moveTo(p1);
96 path.lineTo(p2);
97
98 SkPoint pt;
99
100 for (int i = 0; i < LIN_SEGMENTS; i++) {
101 float theta = i * PI / (LIN_SEGMENTS);
102 SkScalar x = 65 + 15 * cos(theta);
103 SkScalar y = 50 - 15 * sin(theta);
104 pt = SkPoint::Make(x, y);
105 path.lineTo(pt);
106 }
107 path.lineTo(p1);
108
109 return path.detach();
110 }
111
112 SkPath rectPath(SkPoint p1) {
113 SkRect r = SkRect::MakeXYWH(p1.fX, p1.fY, 20, 20);
114 return SkPath::Rect(r);
115 }
116
117 void draw(SkCanvas* canvas) override {
118 canvas->clear(0xFFFFFFFF);
119 const float SCALE = 1;
120
121 canvas->translate(30, 40);
122 canvas->scale(SCALE, SCALE);
123
124 SkPoint p1 = SkPoint::Make(50, 50);
125 SkPoint p2 = SkPoint::Make(80, 50);
126
127 SkPath path;
128 switch (fPathType) {
129 case 0:
130 path = quadPath(p1, p2);
131 break;
132 case 1:
133 path = cubicPath(p1, p2);
134 break;
135 case 2:
136 path = rectPath(p1);
137 break;
138 case 3:
139 path = linSemicirclePath(p1, p2);
140 break;
141 default:
142 path = quadPath(p1, p2);
143 break;
144 }
145
146 if (fClosePath) {
147 path = SkPathBuilder(path).close().detach();
148 }
149
150 SkPaint p;
151 p.setColor(SK_ColorRED);
152 p.setAntiAlias(true);
153 p.setStyle(SkPaint::kStroke_Style);
154 p.setStrokeWidth(fStroke);
155
156 canvas->drawPath(path, p);
157
158 if (fDumpHex) {
159 std::cerr << "path dumpHex" << std::endl;
160 path.dumpHex();
161 }
162
163 SkPaint hairp;
164 hairp.setColor(SK_ColorBLACK);
165 hairp.setAntiAlias(true);
166 hairp.setStyle(SkPaint::kStroke_Style);
167
168 if (fDrawFillPath) {
169 SkPath fillpath = skpathutils::FillPathWithPaint(path, p);
170
171 canvas->drawPath(fillpath, hairp);
172
173 if (fDumpHex) {
174 std::cerr << "fillpath dumpHex" << std::endl;
175 fillpath.dumpHex();
176 }
177 }
178
179 if (fDumpHex) {
180 std::cerr << std::endl;
181
182 fDumpHex = false;
183 }
184
185 // draw original path with green hairline
186 hairp.setColor(SK_ColorGREEN);
187 canvas->drawPath(path, hairp);
188 }
189 };
190
191 ///////////////////////////////////////////////////////////////////////////////
192
193 DEF_SLIDE( return new OverstrokeSlide(); )