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