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