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: |
| 19 | SkString onShortName() { |
| 20 | return SkString("getpostextpath"); |
| 21 | } |
| 22 | |
| 23 | SkISize onISize() { return skiagm::make_isize(480, 780); } |
| 24 | |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 25 | static void strokePath(SkCanvas* canvas, const SkPath& path) { |
| 26 | SkPaint paint; |
| 27 | paint.setAntiAlias(true); |
| 28 | paint.setColor(SK_ColorRED); |
| 29 | paint.setStyle(SkPaint::kStroke_Style); |
| 30 | canvas->drawPath(path, paint); |
| 31 | } |
| 32 | |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 33 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 34 | // explicitly add spaces, to test a prev. bug |
| 35 | const char* text = "Ham bur ge fons"; |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 36 | size_t len = strlen(text); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 37 | SkPath path; |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 38 | |
| 39 | SkPaint paint; |
| 40 | paint.setAntiAlias(true); |
| 41 | paint.setTextSize(SkIntToScalar(48)); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 42 | |
| 43 | canvas->translate(SkIntToScalar(10), SkIntToScalar(64)); |
| 44 | |
| 45 | canvas->drawText(text, len, 0, 0, paint); |
| 46 | paint.getTextPath(text, len, 0, 0, &path); |
| 47 | strokePath(canvas, path); |
| 48 | path.reset(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 49 | |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 50 | SkAutoTArray<SkPoint> pos(len); |
| 51 | SkAutoTArray<SkScalar> widths(len); |
| 52 | paint.getTextWidths(text, len, &widths[0]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 53 | |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 54 | SkRandom rand; |
| 55 | SkScalar x = SkIntToScalar(20); |
| 56 | SkScalar y = SkIntToScalar(100); |
| 57 | for (size_t i = 0; i < len; ++i) { |
| 58 | pos[i].set(x, y + rand.nextSScalar1() * 24); |
| 59 | x += widths[i]; |
| 60 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 61 | |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 62 | canvas->translate(0, SkIntToScalar(64)); |
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->drawPosText(text, len, &pos[0], paint); |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 65 | paint.getPosTextPath(text, len, &pos[0], &path); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 66 | strokePath(canvas, path); |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 67 | } |
| 68 | }; |
| 69 | |
| 70 | ////////////////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | static skiagm::GM* F(void*) { return new GetPosTextPathGM; } |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 73 | static skiagm::GMRegistry gR(F); |