1 /*
2 * Copyright 2014 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 #include "gm.h"
8 #include "sk_tool_utils.h"
9 #include "SkBitmap.h"
10 #include "SkGradientShader.h"
11 #include "SkShader.h"
12 #include "SkUtils.h"
13
14 namespace skiagm {
15
16 static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
17
18 class ColorEmojiBlendModesGM : public GM {
19 public:
20 const static int W = 64;
21 const static int H = 64;
ColorEmojiBlendModesGM()22 ColorEmojiBlendModesGM() {}
23
24 protected:
onOnceBeforeDraw()25 void onOnceBeforeDraw() override {
26 const SkColor colors[] = {
27 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
28 SK_ColorMAGENTA, SK_ColorCYAN, SK_ColorYELLOW
29 };
30 SkMatrix local;
31 local.setRotate(180);
32 SkPaint paint;
33 paint.setAntiAlias(true);
34 paint.setShader(SkGradientShader::MakeSweep(0, 0, colors, nullptr, SK_ARRAY_COUNT(colors),
35 0, &local));
36
37 sk_sp<SkTypeface> orig(sk_tool_utils::create_portable_typeface("serif",
38 SkFontStyle::FromOldStyle(SkTypeface::kBold)));
39 if (nullptr == orig) {
40 orig = SkTypeface::MakeDefault();
41 }
42 fColorType = sk_tool_utils::emoji_typeface();
43
44 fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
45 kOpaque_SkAlphaType), gData, 4);
46 }
47
onShortName()48 virtual SkString onShortName() override {
49 SkString name("coloremoji_blendmodes");
50 name.append(sk_tool_utils::platform_os_emoji());
51 return name;
52 }
53
onISize()54 virtual SkISize onISize() override {
55 return SkISize::Make(400, 640);
56 }
57
onDraw(SkCanvas * canvas)58 virtual void onDraw(SkCanvas* canvas) override {
59 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
60
61 const SkBlendMode gModes[] = {
62 SkBlendMode::kClear,
63 SkBlendMode::kSrc,
64 SkBlendMode::kDst,
65 SkBlendMode::kSrcOver,
66 SkBlendMode::kDstOver,
67 SkBlendMode::kSrcIn,
68 SkBlendMode::kDstIn,
69 SkBlendMode::kSrcOut,
70 SkBlendMode::kDstOut,
71 SkBlendMode::kSrcATop,
72 SkBlendMode::kDstATop,
73
74 SkBlendMode::kXor,
75 SkBlendMode::kPlus,
76 SkBlendMode::kModulate,
77 SkBlendMode::kScreen,
78 SkBlendMode::kOverlay,
79 SkBlendMode::kDarken,
80 SkBlendMode::kLighten,
81 SkBlendMode::kColorDodge,
82 SkBlendMode::kColorBurn,
83 SkBlendMode::kHardLight,
84 SkBlendMode::kSoftLight,
85 SkBlendMode::kDifference,
86 SkBlendMode::kExclusion,
87 SkBlendMode::kMultiply,
88 SkBlendMode::kHue,
89 SkBlendMode::kSaturation,
90 SkBlendMode::kColor,
91 SkBlendMode::kLuminosity,
92 };
93
94 const SkScalar w = SkIntToScalar(W);
95 const SkScalar h = SkIntToScalar(H);
96 SkMatrix m;
97 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
98 auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode,
99 SkShader::kRepeat_TileMode, &m);
100
101 SkPaint labelP;
102 labelP.setAntiAlias(true);
103 sk_tool_utils::set_portable_typeface(&labelP);
104 labelP.setTextAlign(SkPaint::kCenter_Align);
105
106 SkPaint textP;
107 textP.setAntiAlias(true);
108 textP.setTypeface(fColorType);
109 textP.setTextSize(SkIntToScalar(70));
110
111 const int W = 5;
112
113 SkScalar x0 = 0;
114 SkScalar y0 = 0;
115 SkScalar x = x0, y = y0;
116 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
117 SkRect r;
118 r.set(x, y, x+w, y+h);
119
120 SkPaint p;
121 p.setStyle(SkPaint::kFill_Style);
122 p.setShader(s);
123 canvas->drawRect(r, p);
124
125 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
126 p.setStyle(SkPaint::kStroke_Style);
127 p.setShader(nullptr);
128 canvas->drawRect(r, p);
129
130 {
131 SkAutoCanvasRestore arc(canvas, true);
132 canvas->clipRect(r);
133 textP.setBlendMode(gModes[i]);
134 textP.setTextEncoding(SkPaint::kUTF32_TextEncoding);
135 const char* text = sk_tool_utils::emoji_sample_text();
136 SkUnichar unichar = SkUTF8_ToUnichar(text);
137 canvas->drawText(&unichar, 4, x+ w/10.f, y + 7.f*h/8.f, textP);
138 }
139 #if 1
140 const char* label = SkBlendMode_Name(gModes[i]);
141 canvas->drawString(label, x + w/2, y - labelP.getTextSize()/2, labelP);
142 #endif
143 x += w + SkIntToScalar(10);
144 if ((i % W) == W - 1) {
145 x = x0;
146 y += h + SkIntToScalar(30);
147 }
148 }
149 }
150
151 private:
152 SkBitmap fBG;
153 sk_sp<SkTypeface> fColorType;
154
155 typedef GM INHERITED;
156 };
157
158 //////////////////////////////////////////////////////////////////////////////
159
MyFactory(void *)160 static GM* MyFactory(void*) { return new ColorEmojiBlendModesGM; }
161 static GMRegistry reg(MyFactory);
162
163 }
164