2013-09-17 15:30:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
|
|
|
|
#include "Resources.h"
|
2014-12-19 21:45:20 +00:00
|
|
|
#include "SkBlurImageFilter.h"
|
|
|
|
#include "SkColorFilterImageFilter.h"
|
|
|
|
#include "SkColorMatrixFilter.h"
|
2013-09-17 15:30:21 +00:00
|
|
|
#include "SkCanvas.h"
|
2014-12-19 21:45:20 +00:00
|
|
|
#include "SkGradientShader.h"
|
2013-09-17 15:30:21 +00:00
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
2014-12-19 21:45:20 +00:00
|
|
|
/*
|
|
|
|
* Spits out a dummy gradient to test blur with shader on paint
|
|
|
|
*/
|
2016-03-09 17:50:50 +00:00
|
|
|
static sk_sp<SkShader> MakeLinear() {
|
2014-12-19 21:45:20 +00:00
|
|
|
static const SkPoint kPts[] = { { 0, 0 }, { 32, 32 } };
|
|
|
|
static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
|
|
|
|
static const SkColor kColors[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
|
2016-03-09 17:50:50 +00:00
|
|
|
return SkGradientShader::MakeLinear(kPts, kColors, kPos, SK_ARRAY_COUNT(kColors),
|
|
|
|
SkShader::kClamp_TileMode);
|
2014-12-19 21:45:20 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
static SkImageFilter* make_grayscale(SkImageFilter* input = nullptr) {
|
2014-12-19 21:45:20 +00:00
|
|
|
SkScalar matrix[20];
|
|
|
|
memset(matrix, 0, 20 * sizeof(SkScalar));
|
|
|
|
matrix[0] = matrix[5] = matrix[10] = 0.2126f;
|
|
|
|
matrix[1] = matrix[6] = matrix[11] = 0.7152f;
|
|
|
|
matrix[2] = matrix[7] = matrix[12] = 0.0722f;
|
|
|
|
matrix[18] = 1.0f;
|
2016-03-22 17:17:23 +00:00
|
|
|
auto filter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
|
|
|
|
return SkColorFilterImageFilter::Create(filter.get(), input);
|
2014-12-19 21:45:20 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
static SkImageFilter* make_blur(float amount, SkImageFilter* input = nullptr) {
|
2014-12-19 21:45:20 +00:00
|
|
|
return SkBlurImageFilter::Create(amount, amount, input);
|
|
|
|
}
|
|
|
|
|
2013-09-17 15:30:21 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class ColorEmojiGM : public GM {
|
|
|
|
public:
|
2015-07-21 19:27:36 +00:00
|
|
|
ColorEmojiGM() { }
|
2013-09-17 15:30:21 +00:00
|
|
|
|
|
|
|
protected:
|
2015-01-26 22:08:52 +00:00
|
|
|
struct EmojiFont {
|
2015-07-23 18:52:01 +00:00
|
|
|
SkAutoTUnref<SkTypeface> typeface;
|
2015-01-26 22:08:52 +00:00
|
|
|
const char* text;
|
2015-07-21 19:27:36 +00:00
|
|
|
} emojiFont;
|
2015-03-26 01:17:31 +00:00
|
|
|
virtual void onOnceBeforeDraw() override {
|
2015-07-23 18:52:01 +00:00
|
|
|
sk_tool_utils::emoji_typeface(&emojiFont.typeface);
|
2015-07-21 19:27:36 +00:00
|
|
|
emojiFont.text = sk_tool_utils::emoji_sample_text();
|
2013-09-17 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2015-07-21 19:27:36 +00:00
|
|
|
SkString name("coloremoji");
|
|
|
|
name.append(sk_tool_utils::platform_os_emoji());
|
|
|
|
return name;
|
2013-09-17 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2015-01-26 22:08:52 +00:00
|
|
|
return SkISize::Make(650, 900);
|
2013-09-17 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-09-17 15:30:21 +00:00
|
|
|
|
2015-07-21 19:27:36 +00:00
|
|
|
canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setTypeface(emojiFont.typeface);
|
|
|
|
const char* text = emojiFont.text;
|
|
|
|
|
|
|
|
// draw text at different point sizes
|
|
|
|
const int textSize[] = { 10, 30, 50, };
|
|
|
|
const int textYOffset[] = { 10, 40, 100, };
|
|
|
|
SkASSERT(sizeof(textSize) == sizeof(textYOffset));
|
|
|
|
size_t y_offset = 0;
|
|
|
|
for (size_t y = 0; y < sizeof(textSize) / sizeof(int); y++) {
|
|
|
|
paint.setTextSize(SkIntToScalar(textSize[y]));
|
|
|
|
canvas->drawText(text, strlen(text), 10, SkIntToScalar(textYOffset[y]), paint);
|
|
|
|
y_offset += textYOffset[y];
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw with shaders and image filters
|
|
|
|
for (int makeLinear = 0; makeLinear < 2; makeLinear++) {
|
|
|
|
for (int makeBlur = 0; makeBlur < 2; makeBlur++) {
|
|
|
|
for (int makeGray = 0; makeGray < 2; makeGray++) {
|
|
|
|
SkPaint shaderPaint;
|
|
|
|
shaderPaint.setTypeface(paint.getTypeface());
|
|
|
|
if (SkToBool(makeLinear)) {
|
2016-03-09 17:50:50 +00:00
|
|
|
shaderPaint.setShader(MakeLinear());
|
2015-07-21 19:27:36 +00:00
|
|
|
}
|
2014-12-19 21:45:20 +00:00
|
|
|
|
2015-07-21 19:27:36 +00:00
|
|
|
if (SkToBool(makeBlur) && SkToBool(makeGray)) {
|
2015-08-27 14:41:13 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> grayScale(make_grayscale(nullptr));
|
2015-07-21 19:27:36 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> blur(make_blur(3.0f, grayScale));
|
|
|
|
shaderPaint.setImageFilter(blur);
|
|
|
|
} else if (SkToBool(makeBlur)) {
|
2015-08-27 14:41:13 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> blur(make_blur(3.0f, nullptr));
|
2015-07-21 19:27:36 +00:00
|
|
|
shaderPaint.setImageFilter(blur);
|
|
|
|
} else if (SkToBool(makeGray)) {
|
2015-08-27 14:41:13 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> grayScale(make_grayscale(nullptr));
|
2015-07-21 19:27:36 +00:00
|
|
|
shaderPaint.setImageFilter(grayScale);
|
2014-12-19 21:45:20 +00:00
|
|
|
}
|
2015-07-21 19:27:36 +00:00
|
|
|
shaderPaint.setTextSize(30);
|
|
|
|
canvas->drawText(text, strlen(text), 380, SkIntToScalar(y_offset),
|
|
|
|
shaderPaint);
|
|
|
|
y_offset += 32;
|
2014-12-19 21:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-21 19:27:36 +00:00
|
|
|
}
|
2013-09-17 15:30:21 +00:00
|
|
|
|
2015-07-21 19:27:36 +00:00
|
|
|
// setup work needed to draw text with different clips
|
|
|
|
canvas->translate(10, 160);
|
|
|
|
paint.setTextSize(40);
|
|
|
|
|
|
|
|
// compute the bounds of the text
|
|
|
|
SkRect bounds;
|
|
|
|
paint.measureText(text, strlen(text), &bounds);
|
|
|
|
|
|
|
|
const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
|
|
|
|
const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
|
|
|
|
const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
|
|
|
|
const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
|
|
|
|
|
|
|
|
SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
|
|
|
|
boundsHalfWidth, boundsHalfHeight);
|
|
|
|
SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
|
|
|
|
boundsHalfWidth, boundsHalfHeight);
|
|
|
|
SkRect interiorClip = bounds;
|
|
|
|
interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
|
|
|
|
|
|
|
|
const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip };
|
|
|
|
|
|
|
|
SkPaint clipHairline;
|
|
|
|
clipHairline.setColor(SK_ColorWHITE);
|
|
|
|
clipHairline.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
for (size_t x = 0; x < sizeof(clipRects) / sizeof(SkRect); ++x) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->drawRect(clipRects[x], clipHairline);
|
|
|
|
paint.setAlpha(0x20);
|
|
|
|
canvas->drawText(text, strlen(text), 0, 0, paint);
|
|
|
|
canvas->clipRect(clipRects[x]);
|
|
|
|
paint.setAlpha(0xFF);
|
|
|
|
canvas->drawText(text, strlen(text), 0, 0, paint);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, bounds.height() + SkIntToScalar(25));
|
2013-09-17 15:30:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new ColorEmojiGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|