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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/private/SkTemplates.h" |
| 20 | #include "include/utils/SkRandom.h" |
| 21 | #include "src/core/SkFontPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include "tools/ToolUtils.h" |
| 23 | |
| 24 | #include <string.h> |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 25 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 26 | static void strokePath(SkCanvas* canvas, const SkPath& path) { |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 27 | SkPaint paint; |
| 28 | paint.setAntiAlias(true); |
| 29 | paint.setColor(SK_ColorRED); |
| 30 | paint.setStyle(SkPaint::kStroke_Style); |
| 31 | canvas->drawPath(path, paint); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 32 | } |
| 33 | DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) { |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 34 | // 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.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 38 | |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 39 | SkFont font; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 40 | font.setTypeface(ToolUtils::create_portable_typeface()); |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 41 | font.setSize(48); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 42 | |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 43 | SkPaint paint; |
| 44 | paint.setAntiAlias(true); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 45 | |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 46 | canvas->translate(SkIntToScalar(10), SkIntToScalar(64)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 47 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 48 | canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, 0, 0, font, paint); |
| 49 | ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, nullptr); |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 50 | strokePath(canvas, path); |
| 51 | path.reset(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 52 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 53 | SkAutoToGlyphs atg(font, text, len, SkTextEncoding::kUTF8); |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 54 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 59 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 66 | |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 67 | canvas->translate(0, SkIntToScalar(64)); |
| 68 | |
| 69 | canvas->drawTextBlob(SkTextBlob::MakeFromPosText(text, len, &pos[0], font), 0, 0, paint); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 70 | ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, &pos[0]); |
Mike Reed | 38810f3 | 2018-12-21 10:58:25 -0500 | [diff] [blame] | 71 | strokePath(canvas, path); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 72 | } |