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 "sk_tool_utils.h"
10
11 #include "SkCanvas.h"
12 #include "SkFontPriv.h"
13 #include "SkPaint.h"
14 #include "SkPath.h"
15 #include "SkRandom.h"
16 #include "SkTemplates.h"
17 #include "SkTextBlob.h"
18
strokePath(SkCanvas * canvas,const SkPath & path)19 static void strokePath(SkCanvas* canvas, const SkPath& path) {
20 SkPaint paint;
21 paint.setAntiAlias(true);
22 paint.setColor(SK_ColorRED);
23 paint.setStyle(SkPaint::kStroke_Style);
24 canvas->drawPath(path, paint);
25 }
26 DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) {
27 // explicitly add spaces, to test a prev. bug
28 const char* text = "Ham bur ge fons";
29 size_t len = strlen(text);
30 SkPath path;
31
32 SkFont font;
33 font.setTypeface(sk_tool_utils::create_portable_typeface());
34 font.setSize(48);
35
36 SkPaint paint;
37 paint.setAntiAlias(true);
38
39 canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
40
41 canvas->drawSimpleText(text, len, kUTF8_SkTextEncoding, 0, 0, font, paint);
42 sk_tool_utils::get_text_path(font, text, len, kUTF8_SkTextEncoding, &path, nullptr);
43 strokePath(canvas, path);
44 path.reset();
45
46 SkAutoToGlyphs atg(font, text, len, kUTF8_SkTextEncoding);
47 const int count = atg.count();
48 SkAutoTArray<SkPoint> pos(count);
49 SkAutoTArray<SkScalar> widths(count);
50 font.getWidths(atg.glyphs(), count, &widths[0]);
51
52 SkRandom rand;
53 SkScalar x = SkIntToScalar(20);
54 SkScalar y = SkIntToScalar(100);
55 for (int i = 0; i < count; ++i) {
56 pos[i].set(x, y + rand.nextSScalar1() * 24);
57 x += widths[i];
58 }
59
60 canvas->translate(0, SkIntToScalar(64));
61
62 canvas->drawTextBlob(SkTextBlob::MakeFromPosText(text, len, &pos[0], font), 0, 0, paint);
63 sk_tool_utils::get_text_path(font, text, len, kUTF8_SkTextEncoding, &path, &pos[0]);
64 strokePath(canvas, path);
65 }
66