xref: /aosp_15_r20/external/skia/gm/getpostextpath.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
strokePath(SkCanvas * canvas,const SkPath & path)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     SkPath path;
41 
42     SkFont font = ToolUtils::DefaultPortableFont();
43     font.setSize(48);
44 
45     SkPaint paint;
46     paint.setAntiAlias(true);
47 
48     canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
49 
50     canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, 0, 0, font, paint);
51     ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, nullptr);
52     strokePath(canvas, path);
53     path.reset();
54 
55     SkAutoToGlyphs atg(font, text, len, SkTextEncoding::kUTF8);
56     const int count = atg.count();
57     AutoTArray<SkPoint>  pos(count);
58     AutoTArray<SkScalar> widths(count);
59     font.getWidths(atg.glyphs(), count, &widths[0]);
60 
61     SkRandom rand;
62     SkScalar x = SkIntToScalar(20);
63     SkScalar y = SkIntToScalar(100);
64     for (int i = 0; i < count; ++i) {
65         pos[i].set(x, y + rand.nextSScalar1() * 24);
66         x += widths[i];
67     }
68 
69     canvas->translate(0, SkIntToScalar(64));
70 
71     canvas->drawTextBlob(SkTextBlob::MakeFromPosText(text, len, &pos[0], font), 0, 0, paint);
72     ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, &pos[0]);
73     strokePath(canvas, path);
74 }
75