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"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
|
|
|
|
#include "Resources.h"
|
2014-12-19 21:45:20 +00:00
|
|
|
#include "SkBlurImageFilter.h"
|
2017-01-13 17:02:17 +00:00
|
|
|
#include "SkCanvas.h"
|
2014-12-19 21:45:20 +00:00
|
|
|
#include "SkColorFilterImageFilter.h"
|
|
|
|
#include "SkColorMatrixFilter.h"
|
2018-12-15 18:45:33 +00:00
|
|
|
#include "SkFont.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() {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkPoint kPts[] = { { 0, 0 }, { 32, 32 } };
|
|
|
|
constexpr SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
|
|
|
|
constexpr 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
|
|
|
}
|
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) {
|
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-04-04 11:31:25 +00:00
|
|
|
sk_sp<SkColorFilter> filter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
|
2016-04-05 18:50:42 +00:00
|
|
|
return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
|
2014-12-19 21:45:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
static sk_sp<SkImageFilter> make_blur(float amount, sk_sp<SkImageFilter> input) {
|
|
|
|
return SkBlurImageFilter::Make(amount, amount, std::move(input));
|
2014-12-19 21:45:20 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 17:02:17 +00:00
|
|
|
static sk_sp<SkColorFilter> make_color_filter() {
|
|
|
|
return SkColorMatrixFilter::MakeLightingFilter(SkColorSetRGB(0x00, 0x80, 0xFF),
|
|
|
|
SkColorSetRGB(0xFF, 0x20, 0x00));
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<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 {
|
2016-05-12 17:09:30 +00:00
|
|
|
emojiFont.typeface = sk_tool_utils::emoji_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");
|
2017-11-15 15:42:46 +00:00
|
|
|
name.append(sk_tool_utils::platform_font_manager());
|
2015-07-21 19:27:36 +00:00
|
|
|
return name;
|
2013-09-17 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 17:02:17 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(650, 1200); }
|
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
|
|
|
|
2018-08-16 14:17:03 +00:00
|
|
|
canvas->drawColor(SK_ColorGRAY);
|
2015-07-21 19:27:36 +00:00
|
|
|
|
2018-12-15 18:45:33 +00:00
|
|
|
SkFont font(emojiFont.typeface);
|
2015-07-21 19:27:36 +00:00
|
|
|
const char* text = emojiFont.text;
|
|
|
|
|
|
|
|
// draw text at different point sizes
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar textSizes[] = { 10, 30, 50, };
|
2018-11-12 14:35:15 +00:00
|
|
|
SkFontMetrics metrics;
|
2016-07-21 20:32:09 +00:00
|
|
|
SkScalar y = 0;
|
2017-11-07 20:47:01 +00:00
|
|
|
for (const bool& fakeBold : { false, true }) {
|
2018-12-15 18:45:33 +00:00
|
|
|
font.setEmbolden(fakeBold);
|
2017-11-07 20:47:01 +00:00
|
|
|
for (const SkScalar& textSize : textSizes) {
|
2018-12-15 18:45:33 +00:00
|
|
|
font.setSize(textSize);
|
|
|
|
font.getMetrics(&metrics);
|
2017-11-07 20:47:01 +00:00
|
|
|
y += -metrics.fAscent;
|
2018-12-15 18:45:33 +00:00
|
|
|
canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 10, y, font, SkPaint());
|
2017-11-07 20:47:01 +00:00
|
|
|
y += metrics.fDescent + metrics.fLeading;
|
|
|
|
}
|
2015-07-21 19:27:36 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 20:32:09 +00:00
|
|
|
y += 20;
|
|
|
|
SkScalar savedY = y;
|
2015-07-21 19:27:36 +00:00
|
|
|
// 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++) {
|
2017-01-13 17:02:17 +00:00
|
|
|
for (int makeMode = 0; makeMode < 2; ++makeMode) {
|
|
|
|
for (int alpha = 0; alpha < 2; ++alpha) {
|
2018-12-15 18:45:33 +00:00
|
|
|
SkFont shaderFont(font.refTypeface());
|
2017-01-13 17:02:17 +00:00
|
|
|
SkPaint shaderPaint;
|
|
|
|
if (SkToBool(makeLinear)) {
|
|
|
|
shaderPaint.setShader(MakeLinear());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SkToBool(makeBlur) && SkToBool(makeGray)) {
|
|
|
|
sk_sp<SkImageFilter> grayScale(make_grayscale(nullptr));
|
|
|
|
sk_sp<SkImageFilter> blur(make_blur(3.0f, std::move(grayScale)));
|
|
|
|
shaderPaint.setImageFilter(std::move(blur));
|
|
|
|
} else if (SkToBool(makeBlur)) {
|
|
|
|
shaderPaint.setImageFilter(make_blur(3.0f, nullptr));
|
|
|
|
} else if (SkToBool(makeGray)) {
|
|
|
|
shaderPaint.setImageFilter(make_grayscale(nullptr));
|
|
|
|
}
|
|
|
|
if (makeMode) {
|
|
|
|
shaderPaint.setColorFilter(make_color_filter());
|
|
|
|
}
|
|
|
|
if (alpha) {
|
|
|
|
shaderPaint.setAlpha(0x80);
|
|
|
|
}
|
2018-12-15 18:45:33 +00:00
|
|
|
shaderFont.setSize(30);
|
|
|
|
shaderFont.getMetrics(&metrics);
|
2017-01-13 17:02:17 +00:00
|
|
|
y += -metrics.fAscent;
|
2018-12-15 18:45:33 +00:00
|
|
|
canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 380, y,
|
|
|
|
shaderFont, shaderPaint);
|
2017-01-13 17:02:17 +00:00
|
|
|
y += metrics.fDescent + metrics.fLeading;
|
|
|
|
}
|
2014-12-19 21:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 19:27:36 +00:00
|
|
|
}
|
|
|
|
// setup work needed to draw text with different clips
|
2016-07-21 20:32:09 +00:00
|
|
|
canvas->translate(10, savedY);
|
2018-12-15 18:45:33 +00:00
|
|
|
font.setSize(40);
|
2015-07-21 19:27:36 +00:00
|
|
|
|
|
|
|
// compute the bounds of the text
|
|
|
|
SkRect bounds;
|
2018-12-15 18:45:33 +00:00
|
|
|
font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
|
2015-07-21 19:27:36 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip };
|
2015-07-21 19:27:36 +00:00
|
|
|
|
|
|
|
SkPaint clipHairline;
|
|
|
|
clipHairline.setColor(SK_ColorWHITE);
|
|
|
|
clipHairline.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
2018-12-15 18:45:33 +00:00
|
|
|
SkPaint paint;
|
2016-07-21 20:32:09 +00:00
|
|
|
for (const SkRect& clipRect : clipRects) {
|
|
|
|
canvas->translate(0, bounds.height());
|
2015-07-21 19:27:36 +00:00
|
|
|
canvas->save();
|
2016-07-21 20:32:09 +00:00
|
|
|
canvas->drawRect(clipRect, clipHairline);
|
2015-07-21 19:27:36 +00:00
|
|
|
paint.setAlpha(0x20);
|
2018-12-15 18:45:33 +00:00
|
|
|
canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 0, 0, font, paint);
|
2016-07-21 20:32:09 +00:00
|
|
|
canvas->clipRect(clipRect);
|
2015-07-21 19:27:36 +00:00
|
|
|
paint.setAlpha(0xFF);
|
2018-12-15 18:45:33 +00:00
|
|
|
canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 0, 0, font, paint);
|
2015-07-21 19:27:36 +00:00
|
|
|
canvas->restore();
|
2016-07-21 20:32:09 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(25));
|
2013-09-17 15:30:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-04 11:31:25 +00:00
|
|
|
DEF_GM(return new ColorEmojiGM;)
|
2013-09-17 15:30:21 +00:00
|
|
|
|
|
|
|
}
|