djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 9 | |
| 10 | #include "Resources.h" |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkStream.h" |
| 13 | #include "SkTypeface.h" |
| 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | class ColorEmojiGM : public GM { |
| 18 | public: |
| 19 | ColorEmojiGM() { |
| 20 | fTypeface = NULL; |
| 21 | } |
| 22 | |
| 23 | ~ColorEmojiGM() { |
| 24 | SkSafeUnref(fTypeface); |
| 25 | } |
| 26 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 27 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 28 | return kSkipTiled_Flag; |
| 29 | } |
| 30 | |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 31 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame^] | 32 | SkString filename = GetResourcePath("/Funkster.ttf"); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 33 | SkAutoTUnref<SkFILEStream> stream(new SkFILEStream(filename.c_str())); |
| 34 | if (!stream->isValid()) { |
| 35 | SkDebugf("Could not find Funkster.ttf, please set --resourcePath correctly.\n"); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | fTypeface = SkTypeface::CreateFromStream(stream); |
| 40 | } |
| 41 | |
| 42 | virtual SkString onShortName() { |
| 43 | return SkString("coloremoji"); |
| 44 | } |
| 45 | |
| 46 | virtual SkISize onISize() { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 47 | return SkISize::Make(640, 480); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | virtual void onDraw(SkCanvas* canvas) { |
| 51 | |
| 52 | canvas->drawColor(SK_ColorGRAY); |
| 53 | |
| 54 | SkPaint paint; |
| 55 | paint.setTypeface(fTypeface); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 56 | |
| 57 | const char* text = "hamburgerfons"; |
| 58 | |
| 59 | // draw text at different point sizes |
| 60 | const int textSize[] = { 10, 30, 50 }; |
| 61 | const int textYOffset[] = { 10, 40, 100}; |
| 62 | SkASSERT(sizeof(textSize) == sizeof(textYOffset)); |
| 63 | for (size_t y = 0; y < sizeof(textSize) / sizeof(int); ++y) { |
djsollen@google.com | d880797 | 2013-09-17 16:17:52 +0000 | [diff] [blame] | 64 | paint.setTextSize(SkIntToScalar(textSize[y])); |
| 65 | canvas->drawText(text, strlen(text), 10, SkIntToScalar(textYOffset[y]), paint); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // setup work needed to draw text with different clips |
| 69 | canvas->translate(10, 160); |
| 70 | paint.setTextSize(40); |
| 71 | |
| 72 | // compute the bounds of the text |
| 73 | SkRect bounds; |
| 74 | paint.measureText(text, strlen(text), &bounds); |
| 75 | |
| 76 | const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf; |
| 77 | const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf; |
| 78 | const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf; |
| 79 | const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf; |
| 80 | |
| 81 | SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(), |
| 82 | boundsHalfWidth, boundsHalfHeight); |
| 83 | SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(), |
| 84 | boundsHalfWidth, boundsHalfHeight); |
| 85 | SkRect interiorClip = bounds; |
| 86 | interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight); |
| 87 | |
| 88 | const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip }; |
| 89 | |
| 90 | SkPaint clipHairline; |
| 91 | clipHairline.setColor(SK_ColorWHITE); |
| 92 | clipHairline.setStyle(SkPaint::kStroke_Style); |
| 93 | |
| 94 | for (size_t x = 0; x < sizeof(clipRects) / sizeof(SkRect); ++x) { |
| 95 | canvas->save(); |
| 96 | canvas->drawRect(clipRects[x], clipHairline); |
| 97 | paint.setAlpha(0x20); |
| 98 | canvas->drawText(text, strlen(text), 0, 0, paint); |
| 99 | canvas->clipRect(clipRects[x]); |
| 100 | paint.setAlpha(0xFF); |
| 101 | canvas->drawText(text, strlen(text), 0, 0, paint); |
| 102 | canvas->restore(); |
| 103 | canvas->translate(0, bounds.height() + SkIntToScalar(25)); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | SkTypeface* fTypeface; |
| 109 | |
| 110 | typedef GM INHERITED; |
| 111 | }; |
| 112 | |
| 113 | ////////////////////////////////////////////////////////////////////////////// |
| 114 | |
| 115 | static GM* MyFactory(void*) { return new ColorEmojiGM; } |
| 116 | static GMRegistry reg(MyFactory); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 117 | |
| 118 | } |