2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkFontStyle.h"
15 #include "include/core/SkFontTypes.h"
16 #include "include/core/SkImageInfo.h"
17 #include "include/core/SkMatrix.h"
18 #include "include/core/SkPaint.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkScalar.h"
22 #include "include/core/SkShader.h"
23 #include "include/core/SkSize.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkTileMode.h"
26 #include "include/core/SkTypeface.h"
27 #include "include/core/SkTypes.h"
28 #include "include/effects/SkGradientShader.h"
29 #include "include/utils/SkTextUtils.h"
30 #include "src/utils/SkUTF.h"
31 #include "tools/ToolUtils.h"
37 static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
39 class ColorEmojiBlendModesGM : public skiagm::GM {
41 const static int W = 64;
42 const static int H = 64;
43 ColorEmojiBlendModesGM() {}
46 void onOnceBeforeDraw() override {
47 const SkColor colors[] = {
48 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
49 SK_ColorMAGENTA, SK_ColorCYAN, SK_ColorYELLOW
54 paint.setAntiAlias(true);
55 paint.setShader(SkGradientShader::MakeSweep(0, 0, colors, nullptr, SK_ARRAY_COUNT(colors),
58 sk_sp<SkTypeface> orig(ToolUtils::create_portable_typeface("serif", SkFontStyle::Bold()));
59 if (nullptr == orig) {
60 orig = SkTypeface::MakeDefault();
62 fColorType = ToolUtils::emoji_typeface();
64 fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
65 kOpaque_SkAlphaType), gData, 4);
68 SkString onShortName() override {
69 return SkString("coloremoji_blendmodes");
72 SkISize onISize() override {
76 void onDraw(SkCanvas* canvas) override {
77 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
79 const SkBlendMode gModes[] = {
83 SkBlendMode::kSrcOver,
84 SkBlendMode::kDstOver,
89 SkBlendMode::kSrcATop,
90 SkBlendMode::kDstATop,
94 SkBlendMode::kModulate,
96 SkBlendMode::kOverlay,
98 SkBlendMode::kLighten,
99 SkBlendMode::kColorDodge,
100 SkBlendMode::kColorBurn,
101 SkBlendMode::kHardLight,
102 SkBlendMode::kSoftLight,
103 SkBlendMode::kDifference,
104 SkBlendMode::kExclusion,
105 SkBlendMode::kMultiply,
107 SkBlendMode::kSaturation,
109 SkBlendMode::kLuminosity,
112 const SkScalar w = SkIntToScalar(W);
113 const SkScalar h = SkIntToScalar(H);
115 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
116 auto s = fBG.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(), m);
118 SkFont labelFont(ToolUtils::create_portable_typeface());
121 textP.setAntiAlias(true);
122 SkFont textFont(fColorType, 70);
128 SkScalar x = x0, y = y0;
129 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
131 r.setLTRB(x, y, x+w, y+h);
134 p.setStyle(SkPaint::kFill_Style);
136 canvas->drawRect(r, p);
138 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
139 p.setStyle(SkPaint::kStroke_Style);
140 p.setShader(nullptr);
141 canvas->drawRect(r, p);
144 SkAutoCanvasRestore arc(canvas, true);
146 textP.setBlendMode(gModes[i]);
147 const char* text = ToolUtils::emoji_sample_text();
148 SkUnichar unichar = SkUTF::NextUTF8(&text, text + strlen(text));
149 SkASSERT(unichar >= 0);
150 canvas->drawSimpleText(&unichar, 4, SkTextEncoding::kUTF32,
151 x+ w/10.f, y + 7.f*h/8.f, textFont, textP);
154 const char* label = SkBlendMode_Name(gModes[i]);
155 SkTextUtils::DrawString(canvas, label, x + w/2, y - labelFont.getSize()/2,
156 labelFont, SkPaint(), SkTextUtils::kCenter_Align);
158 x += w + SkIntToScalar(10);
159 if ((i % kWrap) == kWrap - 1) {
161 y += h + SkIntToScalar(30);
168 sk_sp<SkTypeface> fColorType;
170 using INHERITED = GM;
174 DEF_GM( return new ColorEmojiBlendModesGM; )