blob: c18806e1b7d2f5e6364863c279ad3f40350c947d [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"
9#include "SkCanvas.h"
10#include "SkStream.h"
11#include "SkTypeface.h"
12
13namespace skiagm {
14
15class ColorEmojiGM : public GM {
16public:
17 ColorEmojiGM() {
18 fTypeface = NULL;
19 }
20
21 ~ColorEmojiGM() {
22 SkSafeUnref(fTypeface);
23 }
24protected:
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
39 virtual SkString onShortName() {
40 return SkString("coloremoji");
41 }
42
43 virtual SkISize onISize() {
44 return make_isize(640, 480);
45 }
46
47 virtual void onDraw(SkCanvas* canvas) {
48
49 canvas->drawColor(SK_ColorGRAY);
50
51 SkPaint paint;
52 paint.setTypeface(fTypeface);
53 paint.setEmbeddedBitmapText(true);
54
55 const char* text = "hamburgerfons";
56
57 // draw text at different point sizes
58 const int textSize[] = { 10, 30, 50 };
59 const int textYOffset[] = { 10, 40, 100};
60 SkASSERT(sizeof(textSize) == sizeof(textYOffset));
61 for (size_t y = 0; y < sizeof(textSize) / sizeof(int); ++y) {
djsollen@google.comd8807972013-09-17 16:17:52 +000062 paint.setTextSize(SkIntToScalar(textSize[y]));
63 canvas->drawText(text, strlen(text), 10, SkIntToScalar(textYOffset[y]), paint);
djsollen@google.com6def2a22013-09-17 15:30:21 +000064 }
65
66 // setup work needed to draw text with different clips
67 canvas->translate(10, 160);
68 paint.setTextSize(40);
69
70 // compute the bounds of the text
71 SkRect bounds;
72 paint.measureText(text, strlen(text), &bounds);
73
74 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
75 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
76 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
77 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
78
79 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
80 boundsHalfWidth, boundsHalfHeight);
81 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
82 boundsHalfWidth, boundsHalfHeight);
83 SkRect interiorClip = bounds;
84 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
85
86 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip };
87
88 SkPaint clipHairline;
89 clipHairline.setColor(SK_ColorWHITE);
90 clipHairline.setStyle(SkPaint::kStroke_Style);
91
92 for (size_t x = 0; x < sizeof(clipRects) / sizeof(SkRect); ++x) {
93 canvas->save();
94 canvas->drawRect(clipRects[x], clipHairline);
95 paint.setAlpha(0x20);
96 canvas->drawText(text, strlen(text), 0, 0, paint);
97 canvas->clipRect(clipRects[x]);
98 paint.setAlpha(0xFF);
99 canvas->drawText(text, strlen(text), 0, 0, paint);
100 canvas->restore();
101 canvas->translate(0, bounds.height() + SkIntToScalar(25));
102 }
103 }
104
105private:
106 SkTypeface* fTypeface;
107
108 typedef GM INHERITED;
109};
110
111//////////////////////////////////////////////////////////////////////////////
112
djsollen@google.comd8807972013-09-17 16:17:52 +0000113#if !defined(SK_BUILD_FOR_ANDROID)
114// fail for now until the appropriate freetype changes are submitted
djsollen@google.com6def2a22013-09-17 15:30:21 +0000115static GM* MyFactory(void*) { return new ColorEmojiGM; }
116static GMRegistry reg(MyFactory);
djsollen@google.comd8807972013-09-17 16:17:52 +0000117#endif
djsollen@google.com6def2a22013-09-17 15:30:21 +0000118
119}