8276ea8974ecd42464e52811689884ec6c07a0dc
[platform/upstream/libSkiaSharp.git] / gm / coloremoji.cpp
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
10 #include "Resources.h"
11 #include "SkBlurImageFilter.h"
12 #include "SkColorFilterImageFilter.h"
13 #include "SkColorMatrixFilter.h"
14 #include "SkCanvas.h"
15 #include "SkGradientShader.h"
16 #include "SkStream.h"
17 #include "SkTypeface.h"
18
19 /*
20  * Spits out a dummy gradient to test blur with shader on paint
21  */
22 static SkShader* MakeLinear() {
23     static const SkPoint     kPts[] = { { 0, 0 }, { 32, 32 } };
24     static const SkScalar    kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
25     static const SkColor kColors[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
26     return SkGradientShader::CreateLinear(kPts, kColors, kPos,
27                                           SK_ARRAY_COUNT(kColors), SkShader::kClamp_TileMode);
28 }
29
30 static SkImageFilter* make_grayscale(SkImageFilter* input = NULL) {
31     SkScalar matrix[20];
32     memset(matrix, 0, 20 * sizeof(SkScalar));
33     matrix[0] = matrix[5] = matrix[10] = 0.2126f;
34     matrix[1] = matrix[6] = matrix[11] = 0.7152f;
35     matrix[2] = matrix[7] = matrix[12] = 0.0722f;
36     matrix[18] = 1.0f;
37     SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
38     return SkColorFilterImageFilter::Create(filter, input);
39 }
40
41 static SkImageFilter* make_blur(float amount, SkImageFilter* input = NULL) {
42     return SkBlurImageFilter::Create(amount, amount, input);
43 }
44
45 namespace skiagm {
46
47 class ColorEmojiGM : public GM {
48 public:
49     ColorEmojiGM() : fCBDT_CBLC_Typeface(NULL), fSBIX_Typeface(NULL) { }
50
51 protected:
52     struct EmojiFont {
53         SkTypeface* typeface;
54         const char* text;
55     } emojiFonts[2];
56     virtual void onOnceBeforeDraw() SK_OVERRIDE {
57         SkString filename = GetResourcePath("/Funkster.ttf");
58         SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(filename.c_str()));
59         if (stream->isValid()) {
60             fCBDT_CBLC_Typeface.reset(SkTypeface::CreateFromStream(stream.detach()));
61             emojiFonts[0].typeface = fCBDT_CBLC_Typeface;
62         } else {
63             SkDebugf("Could not find Funkster.ttf, please set --resourcePath correctly.\n");
64             emojiFonts[0].typeface = NULL;
65         }
66         emojiFonts[0].text = "hamburgerfons";
67
68         fSBIX_Typeface.reset(SkTypeface::CreateFromName("Apple Color Emoji", SkTypeface::kNormal));
69         emojiFonts[1].typeface = fSBIX_Typeface;
70         emojiFonts[1].text = "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" // πŸ’°πŸ‘πŸŽ…
71                              "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80" // πŸͺπŸ•πŸš€
72                              "\xF0\x9F\x9A\xBB" "\xF0\x9F\x92\xA9" "\xF0\x9F\x93\xB7" // πŸš»πŸ’©πŸ“·
73                              "\xF0\x9F\x93\xA6" // πŸ“¦
74                              "\xF0\x9F\x87\xBA" "\xF0\x9F\x87\xB8" "\xF0\x9F\x87\xA6"; // πŸ‡ΊπŸ‡ΈπŸ‡¦
75     }
76
77     SkString onShortName() SK_OVERRIDE {
78         return SkString("coloremoji");
79     }
80
81     SkISize onISize() SK_OVERRIDE {
82         return SkISize::Make(650, 900);
83     }
84
85     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
86
87         canvas->drawColor(SK_ColorGRAY);
88
89         for (size_t i = 0; i < SK_ARRAY_COUNT(emojiFonts); ++i) {
90             SkPaint paint;
91             paint.setTypeface(emojiFonts[i].typeface);
92             const char* text = emojiFonts[i].text;
93
94             // draw text at different point sizes
95             const int textSize[] = { 10, 30, 50, };
96             const int textYOffset[] = { 10, 40, 100, };
97             SkASSERT(sizeof(textSize) == sizeof(textYOffset));
98             size_t y_offset = 0;
99             for (size_t y = 0; y < sizeof(textSize) / sizeof(int); y++) {
100                 paint.setTextSize(SkIntToScalar(textSize[y]));
101                 canvas->drawText(text, strlen(text), 10, SkIntToScalar(textYOffset[y]), paint);
102                 y_offset += textYOffset[y];
103             }
104
105             // draw with shaders and image filters
106             for (int makeLinear = 0; makeLinear < 2; makeLinear++) {
107                 for (int makeBlur = 0; makeBlur < 2; makeBlur++) {
108                     for (int makeGray = 0; makeGray < 2; makeGray++) {
109                         SkPaint shaderPaint;
110                         shaderPaint.setTypeface(paint.getTypeface());
111                         if (SkToBool(makeLinear)) {
112                             shaderPaint.setShader(MakeLinear())->unref();
113                         }
114
115                         if (SkToBool(makeBlur) && SkToBool(makeGray)) {
116                             SkAutoTUnref<SkImageFilter> grayScale(make_grayscale(NULL));
117                             SkAutoTUnref<SkImageFilter> blur(make_blur(3.0f, grayScale));
118                             shaderPaint.setImageFilter(blur);
119                         } else if (SkToBool(makeBlur)) {
120                             SkAutoTUnref<SkImageFilter> blur(make_blur(3.0f, NULL));
121                             shaderPaint.setImageFilter(blur);
122                         } else if (SkToBool(makeGray)) {
123                             SkAutoTUnref<SkImageFilter> grayScale(make_grayscale(NULL));
124                             shaderPaint.setImageFilter(grayScale);
125                         }
126                         shaderPaint.setTextSize(30);
127                         canvas->drawText(text, strlen(text), 380, SkIntToScalar(y_offset),
128                                          shaderPaint);
129                         y_offset += 32;
130                     }
131                 }
132             }
133
134             // setup work needed to draw text with different clips
135             canvas->translate(10, 160);
136             paint.setTextSize(40);
137
138             // compute the bounds of the text
139             SkRect bounds;
140             paint.measureText(text, strlen(text), &bounds);
141
142             const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
143             const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
144             const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
145             const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
146
147             SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
148                                                     boundsHalfWidth, boundsHalfHeight);
149             SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
150                                                      boundsHalfWidth, boundsHalfHeight);
151             SkRect interiorClip = bounds;
152             interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
153
154             const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip };
155
156             SkPaint clipHairline;
157             clipHairline.setColor(SK_ColorWHITE);
158             clipHairline.setStyle(SkPaint::kStroke_Style);
159
160             for (size_t x = 0; x < sizeof(clipRects) / sizeof(SkRect); ++x) {
161                 canvas->save();
162                 canvas->drawRect(clipRects[x], clipHairline);
163                 paint.setAlpha(0x20);
164                 canvas->drawText(text, strlen(text), 0, 0, paint);
165                 canvas->clipRect(clipRects[x]);
166                 paint.setAlpha(0xFF);
167                 canvas->drawText(text, strlen(text), 0, 0, paint);
168                 canvas->restore();
169                 canvas->translate(0, bounds.height() + SkIntToScalar(25));
170             }
171         }
172     }
173
174 private:
175     SkAutoTUnref<SkTypeface> fCBDT_CBLC_Typeface;
176     SkAutoTUnref<SkTypeface> fSBIX_Typeface;
177
178     typedef GM INHERITED;
179 };
180
181 //////////////////////////////////////////////////////////////////////////////
182
183 static GM* MyFactory(void*) { return new ColorEmojiGM; }
184 static GMRegistry reg(MyFactory);
185
186 }