1 /*
2 * Copyright 2011 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
8 #include "gm.h"
9 #include "sk_tool_utils.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12
13 namespace skiagm {
14
makebm(SkBitmap * bm,int w,int h)15 static void makebm(SkBitmap* bm, int w, int h) {
16 bm->allocN32Pixels(w, h);
17 bm->eraseColor(SK_ColorTRANSPARENT);
18
19 SkCanvas canvas(*bm);
20 SkScalar s = SkIntToScalar(SkMin32(w, h));
21 const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
22 const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
23 const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
24 const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
25 const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
26
27
28 SkPaint paint;
29
30 paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos,
31 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode));
32 canvas.drawPaint(paint);
33 paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
34 SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode));
35 canvas.drawPaint(paint);
36 }
37
38 ///////////////////////////////////////////////////////////////////////////////
39
40 struct LabeledMatrix {
41 SkMatrix fMatrix;
42 const char* fLabel;
43 };
44
45 constexpr char kText[] = "B";
46 constexpr int kTextLen = SK_ARRAY_COUNT(kText) - 1;
47 constexpr int kPointSize = 300;
48
49 class ShaderText3GM : public GM {
50 public:
ShaderText3GM()51 ShaderText3GM() {
52 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
53 }
54
55 protected:
56
onShortName()57 SkString onShortName() override {
58 return SkString("shadertext3");
59 }
60
onISize()61 SkISize onISize() override{ return SkISize::Make(820, 930); }
62
onOnceBeforeDraw()63 void onOnceBeforeDraw() override {
64 makebm(&fBmp, kPointSize / 4, kPointSize / 4);
65 }
66
onDraw(SkCanvas * canvas)67 void onDraw(SkCanvas* canvas) override {
68
69 SkPaint bmpPaint;
70 bmpPaint.setAntiAlias(true);
71 bmpPaint.setFilterQuality(kLow_SkFilterQuality);
72 bmpPaint.setAlpha(0x80);
73 canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint);
74
75 SkPaint outlinePaint;
76 outlinePaint.setAntiAlias(true);
77 sk_tool_utils::set_portable_typeface(&outlinePaint);
78 outlinePaint.setTextSize(SkIntToScalar(kPointSize));
79 outlinePaint.setStyle(SkPaint::kStroke_Style);
80 outlinePaint.setStrokeWidth(0.f);
81
82 canvas->translate(15.f, 15.f);
83
84 // draw glyphs scaled up
85 canvas->scale(2.f, 2.f);
86
87 constexpr SkShader::TileMode kTileModes[] = {
88 SkShader::kRepeat_TileMode,
89 SkShader::kMirror_TileMode,
90 };
91
92 // position the baseline of the first run
93 canvas->translate(0.f, 0.75f * kPointSize);
94
95 canvas->save();
96 int i = 0;
97 for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
98 for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
99 SkMatrix localM;
100 localM.setTranslate(5.f, 5.f);
101 localM.postRotate(20);
102 localM.postScale(1.15f, .85f);
103
104 SkPaint fillPaint;
105 fillPaint.setAntiAlias(true);
106 sk_tool_utils::set_portable_typeface(&fillPaint);
107 fillPaint.setTextSize(SkIntToScalar(kPointSize));
108 fillPaint.setFilterQuality(kLow_SkFilterQuality);
109 fillPaint.setShader(SkShader::MakeBitmapShader(fBmp, kTileModes[tm0],
110 kTileModes[tm1], &localM));
111
112 canvas->drawText(kText, kTextLen, 0, 0, fillPaint);
113 canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
114 SkScalar w = fillPaint.measureText(kText, kTextLen);
115 canvas->translate(w + 10.f, 0.f);
116 ++i;
117 if (!(i % 2)) {
118 canvas->restore();
119 canvas->translate(0, 0.75f * kPointSize);
120 canvas->save();
121 }
122 }
123 }
124 canvas->restore();
125 }
126
127 private:
128 SkBitmap fBmp;
129 typedef GM INHERITED;
130 };
131
132 ///////////////////////////////////////////////////////////////////////////////
133
MyFactory(void *)134 static GM* MyFactory(void*) { return new ShaderText3GM; }
135 static GMRegistry reg(MyFactory);
136 }
137