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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontTypes.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPath.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkTextBlob.h"
18 #include "include/core/SkTypeface.h"
19 #include "include/private/SkTemplates.h"
20 #include "include/utils/SkRandom.h"
21 #include "src/core/SkFontPriv.h"
22 #include "tools/ToolUtils.h"
23
24 #include <string.h>
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 DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) {
34 // explicitly add spaces, to test a prev. bug
35 const char* text = "Ham bur ge fons";
36 size_t len = strlen(text);
37 SkPath path;
38
39 SkFont font;
40 font.setTypeface(ToolUtils::create_portable_typeface());
41 font.setSize(48);
42
43 SkPaint paint;
44 paint.setAntiAlias(true);
45
46 canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
47
48 canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, 0, 0, font, paint);
49 ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, nullptr);
50 strokePath(canvas, path);
51 path.reset();
52
53 SkAutoToGlyphs atg(font, text, len, SkTextEncoding::kUTF8);
54 const int count = atg.count();
55 SkAutoTArray<SkPoint> pos(count);
56 SkAutoTArray<SkScalar> widths(count);
57 font.getWidths(atg.glyphs(), count, &widths[0]);
58
59 SkRandom rand;
60 SkScalar x = SkIntToScalar(20);
61 SkScalar y = SkIntToScalar(100);
62 for (int i = 0; i < count; ++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->drawTextBlob(SkTextBlob::MakeFromPosText(text, len, &pos[0], font), 0, 0, paint);
70 ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, &pos[0]);
71 strokePath(canvas, path);
72 }
73