reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkFontTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkPoint.h" |
| 16 | #include "include/core/SkScalar.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkTextBlob.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #include "include/core/SkTypeface.h" |
Kevin Lubick | 46572b4 | 2023-01-18 13:11:06 -0500 | [diff] [blame] | 19 | #include "include/private/base/SkTemplates.h" |
Kevin Lubick | 0d4d114 | 2023-02-13 09:13:10 -0500 | [diff] [blame] | 20 | #include "src/base/SkRandom.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/core/SkFontPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include "tools/ToolUtils.h" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 23 | #include "tools/fonts/FontToolUtils.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | |
| 25 | #include <string.h> |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 26 | |
Herb Derby | 3b3bcf0 | 2023-01-17 15:12:15 -0500 | [diff] [blame] | 27 | using namespace skia_private; |
| 28 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 29 | static void strokePath(SkCanvas* canvas, const SkPath& path) { |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 30 | SkPaint paint; |
| 31 | paint.setAntiAlias(true); |
| 32 | paint.setColor(SK_ColorRED); |
| 33 | paint.setStyle(SkPaint::kStroke_Style); |
| 34 | canvas->drawPath(path, paint); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 35 | } |
| 36 | DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) { |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 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; |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 41 | |
Kevin Lubick | bca43ec | 2023-10-30 10:11:22 -0400 | [diff] [blame] | 42 | SkFont font = ToolUtils::DefaultPortableFont(); |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 43 | font.setSize(48); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 44 | |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 45 | SkPaint paint; |
| 46 | paint.setAntiAlias(true); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 47 | |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 48 | canvas->translate(SkIntToScalar(10), SkIntToScalar(64)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 49 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 50 | canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, 0, 0, font, paint); |
| 51 | ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, nullptr); |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 52 | strokePath(canvas, path); |
| 53 | path.reset(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 54 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 55 | SkAutoToGlyphs atg(font, text, len, SkTextEncoding::kUTF8); |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 56 | const int count = atg.count(); |
Herb Derby | 3b3bcf0 | 2023-01-17 15:12:15 -0500 | [diff] [blame] | 57 | AutoTArray<SkPoint> pos(count); |
| 58 | AutoTArray<SkScalar> widths(count); |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 59 | font.getWidths(atg.glyphs(), count, &widths[0]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 60 | |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 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 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 68 | |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 69 | canvas->translate(0, SkIntToScalar(64)); |
| 70 | |
| 71 | canvas->drawTextBlob(SkTextBlob::MakeFromPosText(text, len, &pos[0], font), 0, 0, paint); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 72 | ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, &pos[0]); |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 73 | strokePath(canvas, path); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 74 | } |