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