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