xref: /third_party/skia/gm/shadertext3.cpp (revision cb93a386)
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/SkFont.h"
13#include "include/core/SkFontTypes.h"
14#include "include/core/SkMatrix.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkPoint.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkShader.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
21#include "include/core/SkTileMode.h"
22#include "include/core/SkTypeface.h"
23#include "include/core/SkTypes.h"
24#include "include/effects/SkGradientShader.h"
25#include "tools/ToolUtils.h"
26
27#include <string.h>
28
29namespace skiagm {
30
31static void makebm(SkBitmap* bm, int w, int h) {
32    bm->allocN32Pixels(w, h);
33    bm->eraseColor(SK_ColorTRANSPARENT);
34
35    SkCanvas    canvas(*bm);
36    SkScalar    s = SkIntToScalar(std::min(w, h));
37    const SkPoint     kPts0[] = { { 0, 0 }, { s, s } };
38    const SkPoint     kPts1[] = { { s/2, 0 }, { s/2, s } };
39    const SkScalar    kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
40    const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
41    const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
42
43
44    SkPaint     paint;
45
46    paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos,
47                    SK_ARRAY_COUNT(kColors0), SkTileMode::kClamp));
48    canvas.drawPaint(paint);
49    paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
50                    SK_ARRAY_COUNT(kColors1), SkTileMode::kClamp));
51    canvas.drawPaint(paint);
52}
53
54///////////////////////////////////////////////////////////////////////////////
55
56struct LabeledMatrix {
57    SkMatrix    fMatrix;
58    const char* fLabel;
59};
60
61constexpr int kPointSize = 300;
62
63class ShaderText3GM : public GM {
64public:
65    ShaderText3GM() {
66        this->setBGColor(0xFFDDDDDD);
67    }
68
69protected:
70
71    SkString onShortName() override {
72        return SkString("shadertext3");
73    }
74
75    SkISize onISize() override { return SkISize::Make(820, 930); }
76
77    void onOnceBeforeDraw() override {
78        makebm(&fBmp, kPointSize / 4, kPointSize / 4);
79    }
80
81    void onDraw(SkCanvas* canvas) override {
82
83        SkPaint bmpPaint;
84        bmpPaint.setAntiAlias(true);
85        bmpPaint.setAlphaf(0.5f);
86        SkSamplingOptions sampling(SkFilterMode::kLinear);
87
88        canvas->drawImage(fBmp.asImage(), 5.f, 5.f, sampling, &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.setShader(fBmp.makeShader(kTileModes[tm0], kTileModes[tm1],
120                                                    sampling, 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
138private:
139    SkBitmap fBmp;
140    using INHERITED = GM;
141};
142
143///////////////////////////////////////////////////////////////////////////////
144
145DEF_GM( return new ShaderText3GM; )
146}  // namespace skiagm
147