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 "SkPath.h"
12 #include "SkRandom.h"
13 #include "SkTemplates.h"
14 
strokePath(SkCanvas * canvas,const SkPath & path)15 static void strokePath(SkCanvas* canvas, const SkPath& path) {
16         SkPaint paint;
17         paint.setAntiAlias(true);
18         paint.setColor(SK_ColorRED);
19         paint.setStyle(SkPaint::kStroke_Style);
20         canvas->drawPath(path, paint);
21 }
22 DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) {
23         // explicitly add spaces, to test a prev. bug
24         const char* text = "Ham bur ge fons";
25         int len = SkToInt(strlen(text));
26         SkPath path;
27 
28         SkPaint paint;
29         paint.setAntiAlias(true);
30         sk_tool_utils::set_portable_typeface(&paint);
31         paint.setTextSize(SkIntToScalar(48));
32 
33         canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
34 
35         canvas->drawText(text, len, 0, 0, paint);
36         paint.getTextPath(text, len, 0, 0, &path);
37         strokePath(canvas, path);
38         path.reset();
39 
40         SkAutoTArray<SkPoint>  pos(len);
41         SkAutoTArray<SkScalar> widths(len);
42         paint.getTextWidths(text, len, &widths[0]);
43 
44         SkRandom rand;
45         SkScalar x = SkIntToScalar(20);
46         SkScalar y = SkIntToScalar(100);
47         for (int i = 0; i < len; ++i) {
48             pos[i].set(x, y + rand.nextSScalar1() * 24);
49             x += widths[i];
50         }
51 
52         canvas->translate(0, SkIntToScalar(64));
53 
54         canvas->drawPosText(text, len, &pos[0], paint);
55         paint.getPosTextPath(text, len, &pos[0], &path);
56         strokePath(canvas, path);
57 }
58