blob: 3137d933f21266be428c308e05aa08252e97b0cb [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkFontTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkPaint.h"
14#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkPoint.h"
16#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkTextBlob.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkTypeface.h"
Kevin Lubick46572b42023-01-18 13:11:06 -050019#include "include/private/base/SkTemplates.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050020#include "src/base/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkFontPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040023#include "tools/fonts/FontToolUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024
25#include <string.h>
reed@google.comca0062e2012-07-20 11:20:32 +000026
Herb Derby3b3bcf02023-01-17 15:12:15 -050027using namespace skia_private;
28
halcanary2a243382015-09-09 08:16:41 -070029static void strokePath(SkCanvas* canvas, const SkPath& path) {
Mike Reed38810f32018-12-21 10:58:25 -050030 SkPaint paint;
31 paint.setAntiAlias(true);
32 paint.setColor(SK_ColorRED);
33 paint.setStyle(SkPaint::kStroke_Style);
34 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -070035}
36DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) {
Mike Reed38810f32018-12-21 10:58:25 -050037 // 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;
reed@google.comca0062e2012-07-20 11:20:32 +000041
Kevin Lubickbca43ec2023-10-30 10:11:22 -040042 SkFont font = ToolUtils::DefaultPortableFont();
Mike Reed38810f32018-12-21 10:58:25 -050043 font.setSize(48);
reed@google.com7b4531f2012-08-07 15:53:00 +000044
Mike Reed38810f32018-12-21 10:58:25 -050045 SkPaint paint;
46 paint.setAntiAlias(true);
reed@google.com7b4531f2012-08-07 15:53:00 +000047
Mike Reed38810f32018-12-21 10:58:25 -050048 canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
rmistry@google.comd6176b02012-08-23 18:14:13 +000049
Ben Wagner51e15a62019-05-07 15:38:46 -040050 canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, 0, 0, font, paint);
51 ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, nullptr);
Mike Reed38810f32018-12-21 10:58:25 -050052 strokePath(canvas, path);
53 path.reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
Ben Wagner51e15a62019-05-07 15:38:46 -040055 SkAutoToGlyphs atg(font, text, len, SkTextEncoding::kUTF8);
Mike Reed38810f32018-12-21 10:58:25 -050056 const int count = atg.count();
Herb Derby3b3bcf02023-01-17 15:12:15 -050057 AutoTArray<SkPoint> pos(count);
58 AutoTArray<SkScalar> widths(count);
Mike Reed38810f32018-12-21 10:58:25 -050059 font.getWidths(atg.glyphs(), count, &widths[0]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000060
Mike Reed38810f32018-12-21 10:58:25 -050061 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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000068
Mike Reed38810f32018-12-21 10:58:25 -050069 canvas->translate(0, SkIntToScalar(64));
70
71 canvas->drawTextBlob(SkTextBlob::MakeFromPosText(text, len, &pos[0], font), 0, 0, paint);
Ben Wagner51e15a62019-05-07 15:38:46 -040072 ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, &pos[0]);
Mike Reed38810f32018-12-21 10:58:25 -050073 strokePath(canvas, path);
halcanary2a243382015-09-09 08:16:41 -070074}