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