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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkRandom.h" |
reed@google.com | 0058324 | 2012-07-20 11:34:36 +0000 | [diff] [blame] | 12 | #include "SkTemplates.h" |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 13 | |
| 14 | class GetPosTextPathGM : public skiagm::GM { |
| 15 | public: |
| 16 | GetPosTextPathGM() {} |
| 17 | |
| 18 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 19 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 20 | SkString onShortName() override { |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 21 | return SkString("getpostextpath"); |
| 22 | } |
| 23 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 24 | SkISize onISize() override { return SkISize::Make(480, 780); } |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 25 | |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 26 | static void strokePath(SkCanvas* canvas, const SkPath& path) { |
| 27 | SkPaint paint; |
| 28 | paint.setAntiAlias(true); |
| 29 | paint.setColor(SK_ColorRED); |
| 30 | paint.setStyle(SkPaint::kStroke_Style); |
| 31 | canvas->drawPath(path, paint); |
| 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 35 | // explicitly add spaces, to test a prev. bug |
| 36 | const char* text = "Ham bur ge fons"; |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 37 | int len = SkToInt(strlen(text)); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 38 | SkPath path; |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 39 | |
| 40 | SkPaint paint; |
| 41 | paint.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 42 | sk_tool_utils::set_portable_typeface(&paint); |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 43 | paint.setTextSize(SkIntToScalar(48)); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 44 | |
| 45 | canvas->translate(SkIntToScalar(10), SkIntToScalar(64)); |
| 46 | |
| 47 | canvas->drawText(text, len, 0, 0, paint); |
| 48 | paint.getTextPath(text, len, 0, 0, &path); |
| 49 | strokePath(canvas, path); |
| 50 | path.reset(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 51 | |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 52 | SkAutoTArray<SkPoint> pos(len); |
| 53 | SkAutoTArray<SkScalar> widths(len); |
| 54 | paint.getTextWidths(text, len, &widths[0]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 55 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 56 | SkRandom rand; |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 57 | SkScalar x = SkIntToScalar(20); |
| 58 | SkScalar y = SkIntToScalar(100); |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 59 | for (int i = 0; i < len; ++i) { |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 60 | pos[i].set(x, y + rand.nextSScalar1() * 24); |
| 61 | x += widths[i]; |
| 62 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 63 | |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 64 | canvas->translate(0, SkIntToScalar(64)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 65 | |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 66 | canvas->drawPosText(text, len, &pos[0], paint); |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 67 | paint.getPosTextPath(text, len, &pos[0], &path); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 68 | strokePath(canvas, path); |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 69 | } |
| 70 | }; |
| 71 | |
| 72 | ////////////////////////////////////////////////////////////////////////////// |
| 73 | |
| 74 | static skiagm::GM* F(void*) { return new GetPosTextPathGM; } |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 75 | static skiagm::GMRegistry gR(F); |