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