xref: /aosp_15_r20/external/skia/gm/shadertext3.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "tools/fonts/FontToolUtils.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(std::min(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                     std::size(kColors0), SkTileMode::kClamp));
49     canvas.drawPaint(paint);
50     paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
51                     std::size(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:
getName() const71     SkString getName() const override { return SkString("shadertext3"); }
72 
getISize()73     SkISize getISize() override { return SkISize::Make(820, 930); }
74 
onOnceBeforeDraw()75     void onOnceBeforeDraw() override {
76         makebm(&fBmp, kPointSize / 4, kPointSize / 4);
77     }
78 
onDraw(SkCanvas * canvas)79     void onDraw(SkCanvas* canvas) override {
80 
81         SkPaint bmpPaint;
82         bmpPaint.setAntiAlias(true);
83         bmpPaint.setAlphaf(0.5f);
84         SkSamplingOptions sampling(SkFilterMode::kLinear);
85 
86         canvas->drawImage(fBmp.asImage(), 5.f, 5.f, sampling, &bmpPaint);
87 
88         SkFont  font(ToolUtils::DefaultPortableTypeface(), SkIntToScalar(kPointSize));
89         SkPaint outlinePaint;
90         outlinePaint.setStyle(SkPaint::kStroke_Style);
91         outlinePaint.setStrokeWidth(0.f);
92 
93         canvas->translate(15.f, 15.f);
94 
95         // draw glyphs scaled up
96         canvas->scale(2.f, 2.f);
97 
98         constexpr SkTileMode kTileModes[] = {
99             SkTileMode::kRepeat,
100             SkTileMode::kMirror,
101         };
102 
103         // position the baseline of the first run
104         canvas->translate(0.f, 0.75f * kPointSize);
105 
106         canvas->save();
107         int i = 0;
108         for (size_t tm0 = 0; tm0 < std::size(kTileModes); ++tm0) {
109             for (size_t tm1 = 0; tm1 < std::size(kTileModes); ++tm1) {
110                 SkMatrix localM;
111                 localM.setTranslate(5.f, 5.f);
112                 localM.postRotate(20);
113                 localM.postScale(1.15f, .85f);
114 
115                 SkPaint fillPaint;
116                 fillPaint.setAntiAlias(true);
117                 fillPaint.setShader(fBmp.makeShader(kTileModes[tm0], kTileModes[tm1],
118                                                     sampling, localM));
119 
120                 constexpr char kText[] = "B";
121                 canvas->drawString(kText, 0, 0, font, fillPaint);
122                 canvas->drawString(kText, 0, 0, font, outlinePaint);
123                 SkScalar w = font.measureText(kText, strlen(kText), SkTextEncoding::kUTF8);
124                 canvas->translate(w + 10.f, 0.f);
125                 ++i;
126                 if (!(i % 2)) {
127                     canvas->restore();
128                     canvas->translate(0, 0.75f * kPointSize);
129                     canvas->save();
130                 }
131             }
132         }
133         canvas->restore();
134     }
135 
136 private:
137     SkBitmap fBmp;
138     using INHERITED = GM;
139 };
140 
141 ///////////////////////////////////////////////////////////////////////////////
142 
143 DEF_GM( return new ShaderText3GM; )
144 }  // namespace skiagm
145