aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/smallpaths.cpp
blob: b6a3b33cdc959d88c55d4f2b0527a37fb03a8390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm.h"
#include "SkPath.h"

typedef SkScalar (*MakePathProc)(SkPath*);

static SkScalar make_triangle(SkPath* path) {
    constexpr int gCoord[] = {
        10, 20, 15, 5, 30, 30
    };
    path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
    path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
    path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
    path->close();
    path->offset(SkIntToScalar(10), SkIntToScalar(0));
    return SkIntToScalar(30);
}

static SkScalar make_rect(SkPath* path) {
    SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
                 SkIntToScalar(30), SkIntToScalar(30) };
    path->addRect(r);
    path->offset(SkIntToScalar(10), SkIntToScalar(0));
    return SkIntToScalar(30);
}

static SkScalar make_oval(SkPath* path) {
    SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
                 SkIntToScalar(30), SkIntToScalar(30) };
    path->addOval(r);
    path->offset(SkIntToScalar(10), SkIntToScalar(0));
    return SkIntToScalar(30);
}

static SkScalar make_star(SkPath* path, int n) {
    const SkScalar c = SkIntToScalar(45);
    const SkScalar r = SkIntToScalar(20);

    SkScalar rad = -SK_ScalarPI / 2;
    const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;

    path->moveTo(c, c - r);
    for (int i = 1; i < n; i++) {
        rad += drad;
        SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
        path->lineTo(c + cosV * r, c + sinV * r);
    }
    path->close();
    return r * 2 * 6 / 5;
}

static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }

static SkScalar make_three_line(SkPath* path) {
    static SkScalar xOffset = 34.f;
    static SkScalar yOffset = 50.f;
    path->moveTo(-32.5f + xOffset, 0.0f + yOffset);
    path->lineTo(32.5f + xOffset, 0.0f + yOffset);

    path->moveTo(-32.5f + xOffset, 19 + yOffset);
    path->lineTo(32.5f + xOffset, 19 + yOffset);

    path->moveTo(-32.5f + xOffset, -19 + yOffset);
    path->lineTo(32.5f + xOffset, -19 + yOffset);
    path->lineTo(-32.5f + xOffset, -19 + yOffset);

    path->close();

    return SkIntToScalar(70);
}

static SkScalar make_arrow(SkPath* path) {
    static SkScalar xOffset = 34.f;
    static SkScalar yOffset = 40.f;
    path->moveTo(-26.f + xOffset, 0.0f + yOffset);
    path->lineTo(26.f + xOffset, 0.0f + yOffset);

    path->moveTo(-28.f + xOffset, -2.4748745f + yOffset);
    path->lineTo(0 + xOffset, 25.525126f + yOffset);

    path->moveTo(-28.f + xOffset, 2.4748745f + yOffset);
    path->lineTo(0 + xOffset, -25.525126f + yOffset);
    path->lineTo(-28.f + xOffset, 2.4748745f + yOffset);

    path->close();

    return SkIntToScalar(70);
}

static SkScalar make_curve(SkPath* path) {
    static SkScalar xOffset = -382.f;
    static SkScalar yOffset = -50.f;
    path->moveTo(491 + xOffset, 56 + yOffset);
    path->conicTo(435.93292f + xOffset, 56.000031f + yOffset,
                  382.61078f + xOffset, 69.752716f + yOffset,
                  0.9920463f);

    return SkIntToScalar(40);
}

static SkScalar make_battery(SkPath* path) {
    static SkScalar xOffset = 5.f;

    path->moveTo(24.67f + xOffset, 0.33000004f);
    path->lineTo(8.3299999f + xOffset, 0.33000004f);
    path->lineTo(8.3299999f + xOffset, 5.3299999f);
    path->lineTo(0.33000004f + xOffset, 5.3299999f);
    path->lineTo(0.33000004f + xOffset, 50.669998f);
    path->lineTo(32.669998f + xOffset, 50.669998f);
    path->lineTo(32.669998f + xOffset, 5.3299999f);
    path->lineTo(24.67f + xOffset, 5.3299999f);
    path->lineTo(24.67f + xOffset, 0.33000004f);
    path->close();

    path->moveTo(25.727224f + xOffset, 12.886665f);
    path->lineTo(10.907918f + xOffset, 12.886665f);
    path->lineTo(7.5166659f + xOffset, 28.683645f);
    path->lineTo(14.810181f + xOffset, 28.683645f);
    path->lineTo(7.7024879f + xOffset, 46.135998f);
    path->lineTo(28.049999f + xOffset, 25.136419f);
    path->lineTo(16.854223f + xOffset, 25.136419f);
    path->lineTo(25.727224f + xOffset, 12.886665f);
    path->close();
    return SkIntToScalar(50);
}

static SkScalar make_battery2(SkPath* path) {
    static SkScalar xOffset = 5.f;

    path->moveTo(32.669998f + xOffset, 9.8640003f);
    path->lineTo(0.33000004f + xOffset, 9.8640003f);
    path->lineTo(0.33000004f + xOffset, 50.669998f);
    path->lineTo(32.669998f + xOffset, 50.669998f);
    path->lineTo(32.669998f + xOffset, 9.8640003f);
    path->close();

    path->moveTo(10.907918f + xOffset, 12.886665f);
    path->lineTo(25.727224f + xOffset, 12.886665f);
    path->lineTo(16.854223f + xOffset, 25.136419f);
    path->lineTo(28.049999f + xOffset, 25.136419f);
    path->lineTo(7.7024879f + xOffset, 46.135998f);
    path->lineTo(14.810181f + xOffset, 28.683645f);
    path->lineTo(7.5166659f + xOffset, 28.683645f);
    path->lineTo(10.907918f + xOffset, 12.886665f);
    path->close();

    return SkIntToScalar(70);
}

constexpr MakePathProc gProcs[] = {
    make_triangle,
    make_rect,
    make_oval,
    make_star_5,
    make_star_13,
    make_three_line,
    make_arrow,
    make_curve,
    make_battery,
    make_battery2
};

constexpr SkScalar gWidths[] = {
    2.0f,
    3.0f,
    4.0f,
    5.0f,
    6.0f,
    7.0f,
    7.0f,
    14.0f,
    0.0f,
    0.0f,
};

constexpr SkScalar gMiters[] = {
    2.0f,
    3.0f,
    3.0f,
    3.0f,
    4.0f,
    4.0f,
    4.0f,
    4.0f,
    4.0f,
    4.0f,
};

#define N   SK_ARRAY_COUNT(gProcs)

// This GM tests out drawing small paths (i.e., for Ganesh, using the Distance
// Field path renderer) which are filled, stroked and filledAndStroked. In
// particular this ensures that any cache keys in use include the stroking
// parameters.
class SmallPathsGM : public skiagm::GM {
    SkPath  fPath[N];
    SkScalar fDY[N];
protected:
    void onOnceBeforeDraw() override {
        for (size_t i = 0; i < N; i++) {
            fDY[i] = gProcs[i](&fPath[i]);
        }
    }

    SkString onShortName() override {
        return SkString("smallpaths");
    }

    SkISize onISize() override {
        return SkISize::Make(640, 480);
    }

    void onDraw(SkCanvas* canvas) override {
        SkPaint paint;
        paint.setAntiAlias(true);

        // first column: filled paths
        canvas->save();
        for (size_t i = 0; i < N; i++) {
            canvas->drawPath(fPath[i], paint);
            canvas->translate(SkIntToScalar(0), fDY[i]);
        }
        canvas->restore();
        canvas->translate(SkIntToScalar(120), SkIntToScalar(0));

        // second column: stroked paths
        canvas->save();
        paint.setStyle(SkPaint::kStroke_Style);
        paint.setStrokeCap(SkPaint::kButt_Cap);
        for (size_t i = 0; i < N; i++) {
            paint.setStrokeWidth(gWidths[i]);
            paint.setStrokeMiter(gMiters[i]);
            canvas->drawPath(fPath[i], paint);
            canvas->translate(SkIntToScalar(0), fDY[i]);
        }
        canvas->restore();
        canvas->translate(SkIntToScalar(120), SkIntToScalar(0));

        // third column: stroked paths with different widths
        canvas->save();
        paint.setStyle(SkPaint::kStroke_Style);
        paint.setStrokeCap(SkPaint::kButt_Cap);
        for (size_t i = 0; i < N; i++) {
            paint.setStrokeWidth(gWidths[i] + 2.0f);
            paint.setStrokeMiter(gMiters[i]);
            canvas->drawPath(fPath[i], paint);
            canvas->translate(SkIntToScalar(0), fDY[i]);
        }
        canvas->restore();
        canvas->translate(SkIntToScalar(120), SkIntToScalar(0));

        // fourth column: stroked and filled paths
        paint.setStyle(SkPaint::kStrokeAndFill_Style);
        paint.setStrokeCap(SkPaint::kButt_Cap);
        for (size_t i = 0; i < N; i++) {
            paint.setStrokeWidth(gWidths[i]);
            paint.setStrokeMiter(gMiters[i]);
            canvas->drawPath(fPath[i], paint);
            canvas->translate(SkIntToScalar(0), fDY[i]);
        }

    }

private:
    typedef skiagm::GM INHERITED;
};

DEF_GM(return new SmallPathsGM;)