bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | #include "gm.h" |
| 8 | #include "SkTypeface.h" |
| 9 | |
| 10 | namespace skiagm { |
| 11 | |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 12 | class FontScalerGM : public GM { |
| 13 | public: |
| 14 | FontScalerGM() { |
| 15 | this->setBGColor(0xFFFFFFFF); |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | virtual ~FontScalerGM() { |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | protected: |
| 22 | virtual SkString onShortName() { |
| 23 | return SkString("fontscaler"); |
| 24 | } |
| 25 | |
| 26 | virtual SkISize onISize() { |
bungeman@google.com | 24babf4 | 2011-11-07 16:33:40 +0000 | [diff] [blame] | 27 | return make_isize(1450, 750); |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 28 | } |
| 29 | |
bungeman@google.com | a682b93 | 2012-01-05 18:10:48 +0000 | [diff] [blame] | 30 | static void rotate_about(SkCanvas* canvas, |
| 31 | SkScalar degrees, |
| 32 | SkScalar px, SkScalar py) { |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 33 | canvas->translate(px, py); |
| 34 | canvas->rotate(degrees); |
| 35 | canvas->translate(-px, -py); |
| 36 | } |
| 37 | |
| 38 | virtual void onDraw(SkCanvas* canvas) { |
| 39 | SkPaint paint; |
| 40 | |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 41 | paint.setAntiAlias(true); |
| 42 | paint.setLCDRenderText(true); |
| 43 | //With freetype the default (normal hinting) can be really ugly. |
| 44 | //Most distros now set slight (vertical hinting only) in any event. |
| 45 | paint.setHinting(SkPaint::kSlight_Hinting); |
| 46 | SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromName("Times Roman", SkTypeface::kNormal))); |
| 47 | |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 48 | const char* text = "Hamburgefons ooo mmm"; |
| 49 | const size_t textLen = strlen(text); |
| 50 | |
| 51 | for (int j = 0; j < 2; ++j) { |
| 52 | for (int i = 0; i < 6; ++i) { |
| 53 | SkScalar x = SkIntToScalar(10); |
| 54 | SkScalar y = SkIntToScalar(20); |
| 55 | |
| 56 | SkAutoCanvasRestore acr(canvas, true); |
| 57 | canvas->translate(SkIntToScalar(50 + i * 230), |
| 58 | SkIntToScalar(20)); |
| 59 | rotate_about(canvas, SkIntToScalar(i * 5), x, y * 10); |
| 60 | |
| 61 | { |
| 62 | SkPaint p; |
| 63 | p.setAntiAlias(true); |
| 64 | SkRect r; |
bungeman@google.com | a682b93 | 2012-01-05 18:10:48 +0000 | [diff] [blame] | 65 | r.set(x - SkIntToScalar(3), SkIntToScalar(15), |
| 66 | x - SkIntToScalar(1), SkIntToScalar(280)); |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 67 | canvas->drawRect(r, p); |
| 68 | } |
| 69 | |
| 70 | int index = 0; |
| 71 | for (int ps = 6; ps <= 22; ps++) { |
| 72 | paint.setTextSize(SkIntToScalar(ps)); |
| 73 | canvas->drawText(text, textLen, x, y, paint); |
| 74 | y += paint.getFontMetrics(NULL); |
| 75 | index += 1; |
| 76 | } |
| 77 | } |
bungeman@google.com | a682b93 | 2012-01-05 18:10:48 +0000 | [diff] [blame] | 78 | canvas->translate(0, SkIntToScalar(360)); |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 79 | paint.setSubpixelText(true); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private: |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 84 | typedef GM INHERITED; |
| 85 | }; |
| 86 | |
| 87 | ////////////////////////////////////////////////////////////////////////////// |
borenet@google.com | 0682ca6 | 2012-06-26 17:23:52 +0000 | [diff] [blame] | 88 | |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 89 | static GM* MyFactory(void*) { return new FontScalerGM; } |
| 90 | static GMRegistry reg(MyFactory); |
borenet@google.com | 0682ca6 | 2012-06-26 17:23:52 +0000 | [diff] [blame] | 91 | |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 92 | } |
| 93 | |