blob: bf3343f74f271dcda4be0d86209ddcd33e83b9e7 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/private/SkTemplates.h"
20#include "include/utils/SkRandom.h"
21#include "src/core/SkFontPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "tools/ToolUtils.h"
23
24#include <string.h>
reed@google.comca0062e2012-07-20 11:20:32 +000025
halcanary2a243382015-09-09 08:16:41 -070026static void strokePath(SkCanvas* canvas, const SkPath& path) {
Mike Reed38810f32018-12-21 10:58:25 -050027 SkPaint paint;
28 paint.setAntiAlias(true);
29 paint.setColor(SK_ColorRED);
30 paint.setStyle(SkPaint::kStroke_Style);
31 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -070032}
33DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) {
Mike Reed38810f32018-12-21 10:58:25 -050034 // 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;
reed@google.comca0062e2012-07-20 11:20:32 +000038
Mike Reed38810f32018-12-21 10:58:25 -050039 SkFont font;
Mike Kleinea3f0142019-03-20 11:12:10 -050040 font.setTypeface(ToolUtils::create_portable_typeface());
Mike Reed38810f32018-12-21 10:58:25 -050041 font.setSize(48);
reed@google.com7b4531f2012-08-07 15:53:00 +000042
Mike Reed38810f32018-12-21 10:58:25 -050043 SkPaint paint;
44 paint.setAntiAlias(true);
reed@google.com7b4531f2012-08-07 15:53:00 +000045
Mike Reed38810f32018-12-21 10:58:25 -050046 canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
rmistry@google.comd6176b02012-08-23 18:14:13 +000047
Ben Wagner51e15a62019-05-07 15:38:46 -040048 canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, 0, 0, font, paint);
49 ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, nullptr);
Mike Reed38810f32018-12-21 10:58:25 -050050 strokePath(canvas, path);
51 path.reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +000052
Ben Wagner51e15a62019-05-07 15:38:46 -040053 SkAutoToGlyphs atg(font, text, len, SkTextEncoding::kUTF8);
Mike Reed38810f32018-12-21 10:58:25 -050054 const int count = atg.count();
55 SkAutoTArray<SkPoint> pos(count);
56 SkAutoTArray<SkScalar> widths(count);
57 font.getWidths(atg.glyphs(), count, &widths[0]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000058
Mike Reed38810f32018-12-21 10:58:25 -050059 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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000066
Mike Reed38810f32018-12-21 10:58:25 -050067 canvas->translate(0, SkIntToScalar(64));
68
69 canvas->drawTextBlob(SkTextBlob::MakeFromPosText(text, len, &pos[0], font), 0, 0, paint);
Ben Wagner51e15a62019-05-07 15:38:46 -040070 ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, &pos[0]);
Mike Reed38810f32018-12-21 10:58:25 -050071 strokePath(canvas, path);
halcanary2a243382015-09-09 08:16:41 -070072}