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