OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "SampleCode.h" | 7 #include "SampleCode.h" |
8 #include "SkView.h" | 8 #include "SkView.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkPath.h" | 10 #include "SkPath.h" |
11 | 11 |
| 12 #include <iostream> |
12 #include <cmath> | 13 #include <cmath> |
13 | 14 |
14 #define PI SK_ScalarPI | 15 #define PI SK_ScalarPI |
15 | 16 |
16 #define LIN_SEGMENTS 10 | 17 #define LIN_SEGMENTS 10 |
17 | 18 |
18 class OverstrokeView : public SampleView { | 19 class OverstrokeView : public SampleView { |
19 public: | 20 public: |
20 SkScalar fStroke; | 21 SkScalar fStroke; |
21 int fPathType; // super lazy enum | 22 int fPathType; // super lazy enum |
22 bool fClosePath; | 23 bool fClosePath; |
23 bool fDrawFillPath; | 24 bool fDrawFillPath; |
| 25 bool fDumpHex; |
24 OverstrokeView() { | 26 OverstrokeView() { |
25 fStroke = 5; | 27 fStroke = 5; |
26 fPathType = 0; | 28 fPathType = 0; |
27 fClosePath = false; | 29 fClosePath = false; |
28 fDrawFillPath = false; | 30 fDrawFillPath = false; |
| 31 fDumpHex = false; |
29 this->setBGColor(0xFFFFFFFF); | 32 this->setBGColor(0xFFFFFFFF); |
30 } | 33 } |
31 | 34 |
32 protected: | 35 protected: |
33 bool onQuery(SkEvent* evt) override { | 36 bool onQuery(SkEvent* evt) override { |
34 if (SampleCode::TitleQ(*evt)) { | 37 if (SampleCode::TitleQ(*evt)) { |
35 SampleCode::TitleR(evt, "PathOverstroke"); | 38 SampleCode::TitleR(evt, "PathOverstroke"); |
36 return true; | 39 return true; |
37 } | 40 } |
38 SkUnichar uni; | 41 SkUnichar uni; |
39 if (SampleCode::CharQ(*evt, &uni)) { | 42 if (SampleCode::CharQ(*evt, &uni)) { |
40 switch (uni) { | 43 switch (uni) { |
41 case ',': | 44 case ',': |
42 fStroke += 1.0; | 45 fStroke += 1.0; |
43 this->inval(nullptr); | 46 this->inval(nullptr); |
44 return true; | 47 return true; |
45 case '.': | 48 case '.': |
46 fStroke -= 1.0; | 49 fStroke -= 1.0; |
47 this->inval(nullptr); | 50 this->inval(nullptr); |
48 return true; | 51 return true; |
49 case 'x': | 52 case 'x': |
50 fPathType = (fPathType + 1) % 3; | 53 fPathType = (fPathType + 1) % 4; |
51 this->inval(nullptr); | 54 this->inval(nullptr); |
52 return true; | 55 return true; |
53 case 'c': | 56 case 'c': |
54 fClosePath = !fClosePath; | 57 fClosePath = !fClosePath; |
55 this->inval(nullptr); | 58 this->inval(nullptr); |
56 return true; | 59 return true; |
57 case 'f': | 60 case 'f': |
58 fDrawFillPath = !fDrawFillPath; | 61 fDrawFillPath = !fDrawFillPath; |
59 this->inval(nullptr); | 62 this->inval(nullptr); |
60 return true; | 63 return true; |
| 64 case 'D': |
| 65 fDumpHex = !fDumpHex; |
| 66 this->inval(nullptr); |
| 67 return true; |
61 default: | 68 default: |
62 break; | 69 break; |
63 } | 70 } |
64 } | 71 } |
65 return this->INHERITED::onQuery(evt); | 72 return this->INHERITED::onQuery(evt); |
66 } | 73 } |
67 | 74 |
68 SkPath quadPath(SkPoint p1, SkPoint p2) { | 75 SkPath quadPath(SkPoint p1, SkPoint p2) { |
69 SkASSERT(p1.y() == p2.y()); | 76 SkASSERT(p1.y() == p2.y()); |
70 | 77 |
71 SkPath path; | 78 SkPath path; |
72 path.moveTo(p1); | 79 path.moveTo(p1); |
73 path.lineTo(p2); | 80 path.lineTo(p2); |
74 | 81 |
75 SkPoint p3 = SkPoint::Make((p1.x() + p2.x()) / 2.0f, p1.y() * 0.7f); | 82 SkPoint p3 = SkPoint::Make((p1.x() + p2.x()) / 2.0f, p1.y() * 0.7f); |
76 | 83 |
77 path.quadTo(p3, p1); | 84 path.quadTo(p3, p1); |
78 | 85 |
79 return path; | 86 return path; |
80 } | 87 } |
81 | 88 |
| 89 SkPath cubicPath(SkPoint p1, SkPoint p2) { |
| 90 SkASSERT(p1.y() == p2.y()); |
| 91 |
| 92 SkPath path; |
| 93 path.moveTo(p1); |
| 94 |
| 95 SkPoint p3 = SkPoint::Make((p1.x() + p2.x()) / 3.0f, p1.y() * 0.7f); |
| 96 SkPoint p4 = SkPoint::Make(2.0f*(p1.x() + p2.x()) / 3.0f, p1.y() * 1.5f)
; |
| 97 |
| 98 path.cubicTo(p3, p4, p2); |
| 99 |
| 100 return path; |
| 101 } |
| 102 |
82 SkPath linSemicirclePath(SkPoint p1, SkPoint p2) { | 103 SkPath linSemicirclePath(SkPoint p1, SkPoint p2) { |
83 SkASSERT(p1.y() == p2.y()); | 104 SkASSERT(p1.y() == p2.y()); |
84 | 105 |
85 SkPath path; | 106 SkPath path; |
86 path.moveTo(p1); | 107 path.moveTo(p1); |
87 path.lineTo(p2); | 108 path.lineTo(p2); |
88 | 109 |
89 SkPoint pt; | 110 SkPoint pt; |
90 | 111 |
91 for (int i = 0; i < LIN_SEGMENTS; i++) { | 112 for (int i = 0; i < LIN_SEGMENTS; i++) { |
(...skipping 24 matching lines...) Expand all Loading... |
116 | 137 |
117 SkPoint p1 = SkPoint::Make(50, 50); | 138 SkPoint p1 = SkPoint::Make(50, 50); |
118 SkPoint p2 = SkPoint::Make(80, 50); | 139 SkPoint p2 = SkPoint::Make(80, 50); |
119 | 140 |
120 SkPath path; | 141 SkPath path; |
121 switch (fPathType) { | 142 switch (fPathType) { |
122 case 0: | 143 case 0: |
123 path = quadPath(p1, p2); | 144 path = quadPath(p1, p2); |
124 break; | 145 break; |
125 case 1: | 146 case 1: |
126 path = linSemicirclePath(p1, p2); | 147 path = cubicPath(p1, p2); |
127 break; | 148 break; |
128 case 2: | 149 case 2: |
129 path = rectPath(p1); | 150 path = rectPath(p1); |
130 break; | 151 break; |
| 152 case 3: |
| 153 path = linSemicirclePath(p1, p2); |
| 154 break; |
131 default: | 155 default: |
132 path = quadPath(p1, p2); | 156 path = quadPath(p1, p2); |
133 break; | 157 break; |
134 } | 158 } |
135 | 159 |
136 if (fClosePath) { | 160 if (fClosePath) { |
137 path.close(); | 161 path.close(); |
138 } | 162 } |
139 | 163 |
140 SkPaint p; | 164 SkPaint p; |
141 p.setColor(SK_ColorRED); | 165 p.setColor(SK_ColorRED); |
142 p.setAntiAlias(true); | 166 p.setAntiAlias(true); |
143 p.setStyle(SkPaint::kStroke_Style); | 167 p.setStyle(SkPaint::kStroke_Style); |
144 p.setStrokeWidth(fStroke); | 168 p.setStrokeWidth(fStroke); |
145 | 169 |
146 canvas->drawPath(path, p); | 170 canvas->drawPath(path, p); |
147 | 171 |
| 172 if (fDumpHex) { |
| 173 std::cerr << "path dumpHex" << std::endl; |
| 174 path.dumpHex(); |
| 175 } |
| 176 |
| 177 SkPaint hairp; |
| 178 hairp.setColor(SK_ColorBLACK); |
| 179 hairp.setAntiAlias(true); |
| 180 hairp.setStyle(SkPaint::kStroke_Style); |
| 181 |
148 if (fDrawFillPath) { | 182 if (fDrawFillPath) { |
149 SkPath fillpath; | 183 SkPath fillpath; |
150 p.getFillPath(path, &fillpath); | 184 p.getFillPath(path, &fillpath); |
151 | 185 |
152 SkPaint fillp; | 186 canvas->drawPath(fillpath, hairp); |
153 fillp.setColor(SK_ColorBLACK); | |
154 fillp.setAntiAlias(true); | |
155 fillp.setStyle(SkPaint::kStroke_Style); | |
156 | 187 |
157 canvas->drawPath(fillpath, fillp); | 188 if (fDumpHex) { |
| 189 std::cerr << "fillpath dumpHex" << std::endl; |
| 190 fillpath.dumpHex(); |
| 191 } |
158 } | 192 } |
| 193 |
| 194 if (fDumpHex) { |
| 195 std::cerr << std::endl; |
| 196 |
| 197 fDumpHex = false; |
| 198 } |
| 199 |
| 200 // draw original path with green hairline |
| 201 hairp.setColor(SK_ColorGREEN); |
| 202 canvas->drawPath(path, hairp); |
159 } | 203 } |
160 | 204 |
161 private: | 205 private: |
162 typedef SampleView INHERITED; | 206 typedef SampleView INHERITED; |
163 }; | 207 }; |
164 | 208 |
165 /////////////////////////////////////////////////////////////////////////////// | 209 /////////////////////////////////////////////////////////////////////////////// |
166 | 210 |
167 static SkView* MyFactory() { return new OverstrokeView; } | 211 static SkView* MyFactory() { return new OverstrokeView; } |
168 static SkViewRegister reg(MyFactory); | 212 static SkViewRegister reg(MyFactory); |
OLD | NEW |