Debian Codegraph @

gm/getpostextpath.cpp

raw
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/base/SkTemplates.h"
20 #include "src/base/SkRandom.h"
21 #include "src/core/SkFontPriv.h"
22 #include "tools/ToolUtils.h"
23 #include "tools/fonts/FontToolUtils.h"
24
25 #include <string.h>
26
27 using namespace skia_private;
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 DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) {
37 // explicitly add spaces, to test a prev. bug
38 const char* text = "Ham bur ge fons";
39 size_t len = strlen(text);
40 SkFont font = ToolUtils::DefaultPortableFont();
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 SkPath path = ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, nullptr);
50 strokePath(canvas, path);
51
52 SkAutoToGlyphs atg(font, text, len, SkTextEncoding::kUTF8);
53 const size_t count = atg.count();
54 AutoTArray<SkPoint> pos(count);
55 AutoTArray<SkScalar> widths(count);
56 font.getWidths(atg.glyphs(), widths);
57
58 SkRandom rand;
59 SkScalar x = SkIntToScalar(20);
60 SkScalar y = SkIntToScalar(100);
61 for (size_t i = 0; i < count; ++i) {
62 pos[i].set(x, y + rand.nextSScalar1() * 24);
63 x += widths[i];
64 }
65
66 canvas->translate(0, SkIntToScalar(64));
67
68 canvas->drawTextBlob(SkTextBlob::MakeFromPosText(text, len, pos, font), 0, 0, paint);
69 path = ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &pos[0]);
70 strokePath(canvas, path);
71 }