2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "Resources.h"
11 #include "SkBlurImageFilter.h"
12 #include "SkColorFilterImageFilter.h"
13 #include "SkColorMatrixFilter.h"
15 #include "SkGradientShader.h"
17 #include "SkTypeface.h"
20 * Spits out a dummy gradient to test blur with shader on paint
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);
30 static SkImageFilter* make_grayscale(SkImageFilter* input = NULL) {
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;
37 SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
38 return SkColorFilterImageFilter::Create(filter, input);
41 static SkImageFilter* make_blur(float amount, SkImageFilter* input = NULL) {
42 return SkBlurImageFilter::Create(amount, amount, input);
47 class ColorEmojiGM : public GM {
49 ColorEmojiGM() : fCBDT_CBLC_Typeface(NULL), fSBIX_Typeface(NULL) { }
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;
63 SkDebugf("Could not find Funkster.ttf, please set --resourcePath correctly.\n");
64 emojiFonts[0].typeface = NULL;
66 emojiFonts[0].text = "hamburgerfons";
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"; // πΊπΈπ¦
77 SkString onShortName() SK_OVERRIDE {
78 return SkString("coloremoji");
81 SkISize onISize() SK_OVERRIDE {
82 return SkISize::Make(650, 900);
85 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
87 canvas->drawColor(SK_ColorGRAY);
89 for (size_t i = 0; i < SK_ARRAY_COUNT(emojiFonts); ++i) {
91 paint.setTypeface(emojiFonts[i].typeface);
92 const char* text = emojiFonts[i].text;
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));
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];
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++) {
110 shaderPaint.setTypeface(paint.getTypeface());
111 if (SkToBool(makeLinear)) {
112 shaderPaint.setShader(MakeLinear())->unref();
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);
126 shaderPaint.setTextSize(30);
127 canvas->drawText(text, strlen(text), 380, SkIntToScalar(y_offset),
134 // setup work needed to draw text with different clips
135 canvas->translate(10, 160);
136 paint.setTextSize(40);
138 // compute the bounds of the text
140 paint.measureText(text, strlen(text), &bounds);
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;
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);
154 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip };
156 SkPaint clipHairline;
157 clipHairline.setColor(SK_ColorWHITE);
158 clipHairline.setStyle(SkPaint::kStroke_Style);
160 for (size_t x = 0; x < sizeof(clipRects) / sizeof(SkRect); ++x) {
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);
169 canvas->translate(0, bounds.height() + SkIntToScalar(25));
175 SkAutoTUnref<SkTypeface> fCBDT_CBLC_Typeface;
176 SkAutoTUnref<SkTypeface> fSBIX_Typeface;
178 typedef GM INHERITED;
181 //////////////////////////////////////////////////////////////////////////////
183 static GM* MyFactory(void*) { return new ColorEmojiGM; }
184 static GMRegistry reg(MyFactory);