blob: cf4500819a45233ecbe9bea8f3ebe6786b599402 [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:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000027 virtual uint32_t onGetFlags() const SK_OVERRIDE {
28 return kSkipTiled_Flag;
29 }
30
djsollen@google.com6def2a22013-09-17 15:30:21 +000031 virtual void onOnceBeforeDraw() SK_OVERRIDE {
tfarinabcbc1782014-06-18 14:32:48 -070032 SkString filename = GetResourcePath();
djsollen@google.com6def2a22013-09-17 15:30:21 +000033 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() {
tfarinaf5393182014-06-09 23:59:03 -070049 return SkISize::Make(640, 480);
djsollen@google.com6def2a22013-09-17 15:30:21 +000050 }
51
52 virtual void onDraw(SkCanvas* canvas) {
53
54 canvas->drawColor(SK_ColorGRAY);
55
56 SkPaint paint;
57 paint.setTypeface(fTypeface);
djsollen@google.com6def2a22013-09-17 15:30:21 +000058
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) {
djsollen@google.comd8807972013-09-17 16:17:52 +000066 paint.setTextSize(SkIntToScalar(textSize[y]));
67 canvas->drawText(text, strlen(text), 10, SkIntToScalar(textYOffset[y]), paint);
djsollen@google.com6def2a22013-09-17 15:30:21 +000068 }
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
109private:
110 SkTypeface* fTypeface;
111
112 typedef GM INHERITED;
113};
114
115//////////////////////////////////////////////////////////////////////////////
116
117static GM* MyFactory(void*) { return new ColorEmojiGM; }
118static GMRegistry reg(MyFactory);
djsollen@google.com6def2a22013-09-17 15:30:21 +0000119
120}