blob: cde2099fdf91a6dcce86e45e699560ed0d1f000a [file] [log] [blame]
reed@google.comca0062e2012-07-20 11:20:32 +00001/*
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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040010
reed@google.comca0062e2012-07-20 11:20:32 +000011#include "SkCanvas.h"
Mike Reed38810f32018-12-21 10:58:25 -050012#include "SkFontPriv.h"
reed@google.comca0062e2012-07-20 11:20:32 +000013#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkPath.h"
reed@google.comca0062e2012-07-20 11:20:32 +000015#include "SkRandom.h"
reed@google.com00583242012-07-20 11:34:36 +000016#include "SkTemplates.h"
Mike Reed38810f32018-12-21 10:58:25 -050017#include "SkTextBlob.h"
reed@google.comca0062e2012-07-20 11:20:32 +000018
halcanary2a243382015-09-09 08:16:41 -070019static void strokePath(SkCanvas* canvas, const SkPath& path) {
Mike Reed38810f32018-12-21 10:58:25 -050020 SkPaint paint;
21 paint.setAntiAlias(true);
22 paint.setColor(SK_ColorRED);
23 paint.setStyle(SkPaint::kStroke_Style);
24 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -070025}
26DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) {
Mike Reed38810f32018-12-21 10:58:25 -050027 // 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;
reed@google.comca0062e2012-07-20 11:20:32 +000031
Mike Reed38810f32018-12-21 10:58:25 -050032 SkFont font;
33 font.setTypeface(sk_tool_utils::create_portable_typeface());
34 font.setSize(48);
reed@google.com7b4531f2012-08-07 15:53:00 +000035
Mike Reed38810f32018-12-21 10:58:25 -050036 SkPaint paint;
37 paint.setAntiAlias(true);
reed@google.com7b4531f2012-08-07 15:53:00 +000038
Mike Reed38810f32018-12-21 10:58:25 -050039 canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
rmistry@google.comd6176b02012-08-23 18:14:13 +000040
Mike Reed38810f32018-12-21 10:58:25 -050041 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();
rmistry@google.comd6176b02012-08-23 18:14:13 +000045
Mike Reed38810f32018-12-21 10:58:25 -050046 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]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
Mike Reed38810f32018-12-21 10:58:25 -050052 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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000059
Mike Reed38810f32018-12-21 10:58:25 -050060 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);
halcanary2a243382015-09-09 08:16:41 -070065}