Debian Codegraph @

gm/rsxtext.cpp

raw
1 /*
2 * Copyright 2020 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/SkCanvas.h"
10 #include "include/core/SkRSXform.h"
11 #include "include/core/SkShader.h"
12 #include "include/core/SkSurface.h"
13 #include "include/core/SkTextBlob.h"
14 #include "include/core/SkTileMode.h"
15 #include "tools/ToolUtils.h"
16 #include "tools/fonts/FontToolUtils.h"
17
18 // Exercises RSX text blobs + shader with various local matrix combinations.
19 // Yellow grid should stay aligned for text vs. background.
20 class RSXShaderGM : public skiagm::GM {
21 public:
22 private:
23 SkString getName() const override { return SkString("rsx_blob_shader"); }
24
25 SkISize getISize() override { return SkISize::Make(kSZ * kScale * 2.1f, kSZ * kScale * 2.1f); }
26
27 void onOnceBeforeDraw() override {
28 const SkFontStyle style(SkFontStyle::kExtraBlack_Weight,
29 SkFontStyle::kNormal_Width,
30 SkFontStyle::kUpright_Slant);
31 SkFont font(ToolUtils::CreatePortableTypeface("Sans", style), kFontSZ);
32 font.setEdging(SkFont::Edging::kAntiAlias);
33
34 static constexpr char txt[] = "TEST";
35 SkGlyphID glyphs[16];
36 float widths[16];
37 const auto glyph_count = font.textToGlyphs(txt, strlen(txt), SkTextEncoding::kUTF8, glyphs);
38 font.getWidths({glyphs, glyph_count}, {widths, glyph_count});
39
40 SkTextBlobBuilder builder;
41 const auto& buf = builder.allocRunRSXform(font, glyph_count);
42 std::copy(glyphs, glyphs + glyph_count, buf.glyphs);
43
44 float x = 0;
45 for (size_t i = 0; i < glyph_count; ++i) {
46 buf.xforms()[i] = {
47 1, 0,
48 x, 0,
49 };
50 x += widths[i];
51 }
52
53 fBlob = builder.make();
54 }
55
56 void onDraw(SkCanvas* canvas) override {
57 canvas->scale(kScale, kScale);
58 this->draw_one(canvas,
59 {0, 0}, SkMatrix::I(), SkMatrix::I());
60 this->draw_one(canvas,
61 {kSZ*1.1f, 0}, SkMatrix::Scale(2, 2), SkMatrix::I());
62 this->draw_one(canvas,
63 {0, kSZ*1.1f}, SkMatrix::I(), SkMatrix::RotateDeg(45));
64 this->draw_one(canvas,
65 {kSZ*1.1f, kSZ*1.1f}, SkMatrix::Scale(2, 2), SkMatrix::RotateDeg(45));
66 }
67
68 void draw_one(SkCanvas* canvas, SkPoint pos, const SkMatrix& lm,
69 const SkMatrix& outer_lm) const {
70 SkAutoCanvasRestore acr(canvas, true);
71 canvas->translate(pos.fX, pos.fY);
72
73 SkPaint p;
74 p.setShader(make_shader(lm, outer_lm));
75 p.setAlphaf(0.75f);
76 canvas->drawRect(SkRect::MakeWH(kSZ, kSZ), p);
77
78 p.setAlphaf(1);
79 canvas->drawTextBlob(fBlob, 0, kFontSZ*1, p);
80 canvas->drawTextBlob(fBlob, 0, kFontSZ*2, p);
81 }
82
83 static sk_sp<SkShader> make_shader(const SkMatrix& lm, const SkMatrix& outer_lm) {
84 static constexpr SkISize kTileSize = { 30, 30 };
85 auto surface = SkSurfaces::Raster(
86 SkImageInfo::MakeN32Premul(kTileSize.width(), kTileSize.height()));
87
88 SkPaint p;
89 p.setColor(0xffffff00);
90 surface->getCanvas()->drawPaint(p);
91 p.setColor(0xff008000);
92 surface->getCanvas()
93 ->drawRect({0, 0, kTileSize.width()*0.9f, kTileSize.height()*0.9f}, p);
94
95 return surface->makeImageSnapshot()
96 ->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
97 SkSamplingOptions(SkFilterMode::kLinear), &lm)
98 ->makeWithLocalMatrix(outer_lm);
99 }
100
101 inline static constexpr float kSZ = 300,
102 kFontSZ = kSZ * 0.38,
103 kScale = 1.4f;
104
105 sk_sp<SkTextBlob> fBlob;
106 };
107
108 DEF_GM(return new RSXShaderGM;)