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