1/*
2 * Copyright 2012 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
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12#include "SkTemplates.h"
13
14class GetPosTextPathGM : public skiagm::GM {
15public:
16    GetPosTextPathGM() {}
17
18protected:
19    virtual uint32_t onGetFlags() const SK_OVERRIDE {
20        return kSkipTiled_Flag;
21    }
22
23    SkString onShortName() {
24        return SkString("getpostextpath");
25    }
26
27    SkISize onISize() { return SkISize::Make(480, 780); }
28
29    static void strokePath(SkCanvas* canvas, const SkPath& path) {
30        SkPaint paint;
31        paint.setAntiAlias(true);
32        paint.setColor(SK_ColorRED);
33        paint.setStyle(SkPaint::kStroke_Style);
34        canvas->drawPath(path, paint);
35    }
36
37    virtual void onDraw(SkCanvas* canvas) {
38        // explicitly add spaces, to test a prev. bug
39        const char* text = "Ham bur ge fons";
40        int len = SkToInt(strlen(text));
41        SkPath path;
42
43        SkPaint paint;
44        paint.setAntiAlias(true);
45        sk_tool_utils::set_portable_typeface(&paint);
46        paint.setTextSize(SkIntToScalar(48));
47
48        canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
49
50        canvas->drawText(text, len, 0, 0, paint);
51        paint.getTextPath(text, len, 0, 0, &path);
52        strokePath(canvas, path);
53        path.reset();
54
55        SkAutoTArray<SkPoint>  pos(len);
56        SkAutoTArray<SkScalar> widths(len);
57        paint.getTextWidths(text, len, &widths[0]);
58
59        SkLCGRandom rand;
60        SkScalar x = SkIntToScalar(20);
61        SkScalar y = SkIntToScalar(100);
62        for (int i = 0; i < len; ++i) {
63            pos[i].set(x, y + rand.nextSScalar1() * 24);
64            x += widths[i];
65        }
66
67        canvas->translate(0, SkIntToScalar(64));
68
69        canvas->drawPosText(text, len, &pos[0], paint);
70        paint.getPosTextPath(text, len, &pos[0], &path);
71        strokePath(canvas, path);
72    }
73};
74
75//////////////////////////////////////////////////////////////////////////////
76
77static skiagm::GM* F(void*) { return new GetPosTextPathGM; }
78static skiagm::GMRegistry gR(F);
79