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(count_verbs, 256, 256, false, 0) {
5#include "include/utils/SkTextUtils.h"
6
7static SkPath make_path(const SkFont& font) {
8    SkPath path;
9    const char text[] = "SKIA";
10    SkTextUtils::GetPath(text, strlen(text), SkTextEncoding::kUTF8, 0, 0, font, &path);
11    return path;
12}
13
14static void count_verbs(const SkPath& path, int counts[6]) {
15    SkPath::Iter it(path, false);
16    for (int i = 0; i < 6; ++i) {
17        counts[i] = 0;
18    }
19    while (true) {
20        SkPoint pts[4];
21        SkPath::Verb verb = it.next(pts);
22        if (verb == SkPath::kDone_Verb) {
23            break;
24        }
25        if ((unsigned)verb < 6) {
26            counts[(unsigned)verb]++;
27        }
28    }
29}
30
31void draw(SkCanvas* canvas) {
32    SkFont font(SkTypeface::MakeFromName("DejaVu Sans Mono", SkFontStyle()), 30);
33    SkPath path = make_path(font);
34    int counts[6];
35    count_verbs(path, counts);
36
37    // output results:
38    const char* verbs[6] = {"Move", "Line", "Quad", "Conic", "Cubic", "Close"};
39    SkPoint pt = SkPoint::Make(10.0f, 5.0f + font.getSpacing());
40    SkPaint p;
41    canvas->clear(SK_ColorWHITE);
42    for (int i = 0; i < 6; ++i) {
43        canvas->drawString(SkStringPrintf("%-5s %3d", verbs[i], counts[i]), pt.fX, pt.fY, font,
44                           p);
45        pt.fY += font.getSpacing();
46    }
47}
48}  // END FIDDLE
49