2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 typedef SkScalar (*MakePathProc)(SkPath*);
13 static SkScalar make_triangle(SkPath* path) {
14 constexpr int gCoord[] = {
17 path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
18 path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
19 path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
21 path->offset(SkIntToScalar(10), SkIntToScalar(0));
22 return SkIntToScalar(30);
25 static SkScalar make_rect(SkPath* path) {
26 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
27 SkIntToScalar(30), SkIntToScalar(30) };
29 path->offset(SkIntToScalar(10), SkIntToScalar(0));
30 return SkIntToScalar(30);
33 static SkScalar make_oval(SkPath* path) {
34 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
35 SkIntToScalar(30), SkIntToScalar(30) };
37 path->offset(SkIntToScalar(10), SkIntToScalar(0));
38 return SkIntToScalar(30);
41 static SkScalar make_star(SkPath* path, int n) {
42 const SkScalar c = SkIntToScalar(45);
43 const SkScalar r = SkIntToScalar(20);
45 SkScalar rad = -SK_ScalarPI / 2;
46 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
48 path->moveTo(c, c - r);
49 for (int i = 1; i < n; i++) {
51 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
52 path->lineTo(c + cosV * r, c + sinV * r);
58 static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
59 static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
61 static SkScalar make_three_line(SkPath* path) {
62 static SkScalar xOffset = 34.f;
63 static SkScalar yOffset = 50.f;
64 path->moveTo(-32.5f + xOffset, 0.0f + yOffset);
65 path->lineTo(32.5f + xOffset, 0.0f + yOffset);
67 path->moveTo(-32.5f + xOffset, 19 + yOffset);
68 path->lineTo(32.5f + xOffset, 19 + yOffset);
70 path->moveTo(-32.5f + xOffset, -19 + yOffset);
71 path->lineTo(32.5f + xOffset, -19 + yOffset);
72 path->lineTo(-32.5f + xOffset, -19 + yOffset);
76 return SkIntToScalar(70);
79 static SkScalar make_arrow(SkPath* path) {
80 static SkScalar xOffset = 34.f;
81 static SkScalar yOffset = 40.f;
82 path->moveTo(-26.f + xOffset, 0.0f + yOffset);
83 path->lineTo(26.f + xOffset, 0.0f + yOffset);
85 path->moveTo(-28.f + xOffset, -2.4748745f + yOffset);
86 path->lineTo(0 + xOffset, 25.525126f + yOffset);
88 path->moveTo(-28.f + xOffset, 2.4748745f + yOffset);
89 path->lineTo(0 + xOffset, -25.525126f + yOffset);
90 path->lineTo(-28.f + xOffset, 2.4748745f + yOffset);
94 return SkIntToScalar(70);
97 static SkScalar make_curve(SkPath* path) {
98 static SkScalar xOffset = -382.f;
99 static SkScalar yOffset = -50.f;
100 path->moveTo(491 + xOffset, 56 + yOffset);
101 path->conicTo(435.93292f + xOffset, 56.000031f + yOffset,
102 382.61078f + xOffset, 69.752716f + yOffset,
105 return SkIntToScalar(40);
108 static SkScalar make_battery(SkPath* path) {
109 static SkScalar xOffset = 5.f;
111 path->moveTo(24.67f + xOffset, 0.33000004f);
112 path->lineTo(8.3299999f + xOffset, 0.33000004f);
113 path->lineTo(8.3299999f + xOffset, 5.3299999f);
114 path->lineTo(0.33000004f + xOffset, 5.3299999f);
115 path->lineTo(0.33000004f + xOffset, 50.669998f);
116 path->lineTo(32.669998f + xOffset, 50.669998f);
117 path->lineTo(32.669998f + xOffset, 5.3299999f);
118 path->lineTo(24.67f + xOffset, 5.3299999f);
119 path->lineTo(24.67f + xOffset, 0.33000004f);
122 path->moveTo(25.727224f + xOffset, 12.886665f);
123 path->lineTo(10.907918f + xOffset, 12.886665f);
124 path->lineTo(7.5166659f + xOffset, 28.683645f);
125 path->lineTo(14.810181f + xOffset, 28.683645f);
126 path->lineTo(7.7024879f + xOffset, 46.135998f);
127 path->lineTo(28.049999f + xOffset, 25.136419f);
128 path->lineTo(16.854223f + xOffset, 25.136419f);
129 path->lineTo(25.727224f + xOffset, 12.886665f);
131 return SkIntToScalar(50);
134 static SkScalar make_battery2(SkPath* path) {
135 static SkScalar xOffset = 5.f;
137 path->moveTo(32.669998f + xOffset, 9.8640003f);
138 path->lineTo(0.33000004f + xOffset, 9.8640003f);
139 path->lineTo(0.33000004f + xOffset, 50.669998f);
140 path->lineTo(32.669998f + xOffset, 50.669998f);
141 path->lineTo(32.669998f + xOffset, 9.8640003f);
144 path->moveTo(10.907918f + xOffset, 12.886665f);
145 path->lineTo(25.727224f + xOffset, 12.886665f);
146 path->lineTo(16.854223f + xOffset, 25.136419f);
147 path->lineTo(28.049999f + xOffset, 25.136419f);
148 path->lineTo(7.7024879f + xOffset, 46.135998f);
149 path->lineTo(14.810181f + xOffset, 28.683645f);
150 path->lineTo(7.5166659f + xOffset, 28.683645f);
151 path->lineTo(10.907918f + xOffset, 12.886665f);
154 return SkIntToScalar(70);
157 constexpr MakePathProc gProcs[] = {
170 constexpr SkScalar gWidths[] = {
183 constexpr SkScalar gMiters[] = {
196 #define N SK_ARRAY_COUNT(gProcs)
198 // This GM tests out drawing small paths (i.e., for Ganesh, using the Distance
199 // Field path renderer) which are filled, stroked and filledAndStroked. In
200 // particular this ensures that any cache keys in use include the stroking
202 class SmallPathsGM : public skiagm::GM {
206 void onOnceBeforeDraw() override {
207 for (size_t i = 0; i < N; i++) {
208 fDY[i] = gProcs[i](&fPath[i]);
212 SkString onShortName() override {
213 return SkString("smallpaths");
216 SkISize onISize() override {
217 return SkISize::Make(640, 480);
220 void onDraw(SkCanvas* canvas) override {
222 paint.setAntiAlias(true);
224 // first column: filled paths
226 for (size_t i = 0; i < N; i++) {
227 canvas->drawPath(fPath[i], paint);
228 canvas->translate(SkIntToScalar(0), fDY[i]);
231 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
233 // second column: stroked paths
235 paint.setStyle(SkPaint::kStroke_Style);
236 paint.setStrokeCap(SkPaint::kButt_Cap);
237 for (size_t i = 0; i < N; i++) {
238 paint.setStrokeWidth(gWidths[i]);
239 paint.setStrokeMiter(gMiters[i]);
240 canvas->drawPath(fPath[i], paint);
241 canvas->translate(SkIntToScalar(0), fDY[i]);
244 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
246 // third column: stroked paths with different widths
248 paint.setStyle(SkPaint::kStroke_Style);
249 paint.setStrokeCap(SkPaint::kButt_Cap);
250 for (size_t i = 0; i < N; i++) {
251 paint.setStrokeWidth(gWidths[i] + 2.0f);
252 paint.setStrokeMiter(gMiters[i]);
253 canvas->drawPath(fPath[i], paint);
254 canvas->translate(SkIntToScalar(0), fDY[i]);
257 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
259 // fourth column: stroked and filled paths
260 paint.setStyle(SkPaint::kStrokeAndFill_Style);
261 paint.setStrokeCap(SkPaint::kButt_Cap);
262 for (size_t i = 0; i < N; i++) {
263 paint.setStrokeWidth(gWidths[i]);
264 paint.setStrokeMiter(gMiters[i]);
265 canvas->drawPath(fPath[i], paint);
266 canvas->translate(SkIntToScalar(0), fDY[i]);
272 typedef skiagm::GM INHERITED;
275 DEF_GM(return new SmallPathsGM;)