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 int kPointSize = 300;
46
47 class ShaderText3GM : public GM {
48 public:
ShaderText3GM()49 ShaderText3GM() {
50 this->setBGColor(0xFFDDDDDD);
51 }
52
53 protected:
54
onShortName()55 SkString onShortName() override {
56 return SkString("shadertext3");
57 }
58
onISize()59 SkISize onISize() override{ return SkISize::Make(820, 930); }
60
onOnceBeforeDraw()61 void onOnceBeforeDraw() override {
62 makebm(&fBmp, kPointSize / 4, kPointSize / 4);
63 }
64
onDraw(SkCanvas * canvas)65 void onDraw(SkCanvas* canvas) override {
66
67 SkPaint bmpPaint;
68 bmpPaint.setAntiAlias(true);
69 bmpPaint.setFilterQuality(kLow_SkFilterQuality);
70 bmpPaint.setAlphaf(0.5f);
71 canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint);
72
73 SkFont font(sk_tool_utils::create_portable_typeface(), SkIntToScalar(kPointSize));
74 SkPaint outlinePaint;
75 outlinePaint.setStyle(SkPaint::kStroke_Style);
76 outlinePaint.setStrokeWidth(0.f);
77
78 canvas->translate(15.f, 15.f);
79
80 // draw glyphs scaled up
81 canvas->scale(2.f, 2.f);
82
83 constexpr SkShader::TileMode kTileModes[] = {
84 SkShader::kRepeat_TileMode,
85 SkShader::kMirror_TileMode,
86 };
87
88 // position the baseline of the first run
89 canvas->translate(0.f, 0.75f * kPointSize);
90
91 canvas->save();
92 int i = 0;
93 for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
94 for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
95 SkMatrix localM;
96 localM.setTranslate(5.f, 5.f);
97 localM.postRotate(20);
98 localM.postScale(1.15f, .85f);
99
100 SkPaint fillPaint;
101 fillPaint.setAntiAlias(true);
102 fillPaint.setFilterQuality(kLow_SkFilterQuality);
103 fillPaint.setShader(SkShader::MakeBitmapShader(fBmp, kTileModes[tm0],
104 kTileModes[tm1], &localM));
105
106 constexpr char kText[] = "B";
107 canvas->drawString(kText, 0, 0, font, fillPaint);
108 canvas->drawString(kText, 0, 0, font, outlinePaint);
109 SkScalar w = font.measureText(kText, strlen(kText), kUTF8_SkTextEncoding);
110 canvas->translate(w + 10.f, 0.f);
111 ++i;
112 if (!(i % 2)) {
113 canvas->restore();
114 canvas->translate(0, 0.75f * kPointSize);
115 canvas->save();
116 }
117 }
118 }
119 canvas->restore();
120 }
121
122 private:
123 SkBitmap fBmp;
124 typedef GM INHERITED;
125 };
126
127 ///////////////////////////////////////////////////////////////////////////////
128
129 DEF_GM( return new ShaderText3GM; )
130 }
131