scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "gm.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkString.h" |
| 12 | #include "SkTypeface.h" |
| 13 | #include "SkTypes.h" |
| 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | const char* gFaces[] = { |
| 18 | "Times Roman", |
| 19 | "Hiragino Maru Gothic Pro", |
| 20 | "Papyrus", |
| 21 | "Helvetica", |
| 22 | "Courier New" |
| 23 | }; |
| 24 | |
| 25 | class TypefaceGM : public GM { |
| 26 | public: |
| 27 | TypefaceGM() { |
| 28 | fFaces = new SkTypeface*[SK_ARRAY_COUNT(gFaces)]; |
| 29 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFaces); i++) { |
| 30 | fFaces[i] = SkTypeface::CreateFromName(gFaces[i], SkTypeface::kNormal); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | virtual ~TypefaceGM() { |
| 35 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFaces); i++) { |
| 36 | fFaces[i]->unref(); |
| 37 | } |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame^] | 38 | delete [] fFaces; |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | protected: |
| 42 | virtual SkString onShortName() SK_OVERRIDE { |
| 43 | return SkString("typeface"); |
| 44 | } |
| 45 | |
| 46 | virtual SkISize onISize() SK_OVERRIDE { |
| 47 | return SkISize::Make(640, 480); |
| 48 | } |
| 49 | |
| 50 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 51 | SkString text("Typefaces are fun!"); |
| 52 | SkScalar y = 0; |
| 53 | |
| 54 | SkPaint paint; |
| 55 | paint.setAntiAlias(true); |
| 56 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFaces); i++) { |
| 57 | this->drawWithFace(text, i, y, paint, canvas); |
| 58 | } |
| 59 | // Now go backwards |
| 60 | for (int i = SK_ARRAY_COUNT(gFaces) - 1; i >= 0; i--) { |
| 61 | this->drawWithFace(text, i, y, paint, canvas); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | void drawWithFace(const SkString& text, int i, SkScalar& y, SkPaint& paint, |
| 67 | SkCanvas* canvas) { |
| 68 | paint.setTypeface(fFaces[i]); |
| 69 | y += paint.getFontMetrics(NULL); |
| 70 | canvas->drawText(text.c_str(), text.size(), 0, y, paint); |
| 71 | } |
| 72 | |
| 73 | SkTypeface** fFaces; |
| 74 | |
| 75 | typedef GM INHERITED; |
| 76 | }; |
| 77 | |
| 78 | //////////////////////////////////////////////////////////////////////////////// |
| 79 | |
| 80 | static GM* MyFactory(void*) { return new TypefaceGM; } |
| 81 | static GMRegistry reg(MyFactory); |
| 82 | |
| 83 | } // skiagm |