blob: 2eab864c0df1fb96ec6e3e8117cabb5d6d7d5fa7 [file] [log] [blame]
djsollen@google.com6def2a22013-09-17 15:30:21 +00001/*
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"
tfarinabcbc1782014-06-18 14:32:48 -07009
10#include "Resources.h"
djsollen@google.com6def2a22013-09-17 15:30:21 +000011#include "SkCanvas.h"
12#include "SkStream.h"
13#include "SkTypeface.h"
14
15namespace skiagm {
16
17class ColorEmojiGM : public GM {
18public:
19 ColorEmojiGM() {
20 fTypeface = NULL;
21 }
22
23 ~ColorEmojiGM() {
24 SkSafeUnref(fTypeface);
25 }
26protected:
27 virtual void onOnceBeforeDraw() SK_OVERRIDE {
tfarinac846f4a2014-07-01 12:35:49 -070028 SkString filename = GetResourcePath("/Funkster.ttf");
djsollen@google.com6def2a22013-09-17 15:30:21 +000029 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() {
tfarinaf5393182014-06-09 23:59:03 -070043 return SkISize::Make(640, 480);
djsollen@google.com6def2a22013-09-17 15:30:21 +000044 }
45
46 virtual void onDraw(SkCanvas* canvas) {
47
48 canvas->drawColor(SK_ColorGRAY);
49
50 SkPaint paint;
51 paint.setTypeface(fTypeface);
djsollen@google.com6def2a22013-09-17 15:30:21 +000052
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) {
djsollen@google.comd8807972013-09-17 16:17:52 +000060 paint.setTextSize(SkIntToScalar(textSize[y]));
61 canvas->drawText(text, strlen(text), 10, SkIntToScalar(textYOffset[y]), paint);
djsollen@google.com6def2a22013-09-17 15:30:21 +000062 }
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
103private:
104 SkTypeface* fTypeface;
105
106 typedef GM INHERITED;
107};
108
109//////////////////////////////////////////////////////////////////////////////
110
111static GM* MyFactory(void*) { return new ColorEmojiGM; }
112static GMRegistry reg(MyFactory);
djsollen@google.com6def2a22013-09-17 15:30:21 +0000113
114}