xref: /aosp_15_r20/external/skia/gm/slug.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 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/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontStyle.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkTextBlob.h"
22 #include "include/core/SkTypeface.h"
23 #include "include/core/SkTypes.h"
24 #include "include/private/base/SkTDArray.h"
25 #include "include/private/chromium/Slug.h"
26 #include "tools/ToolUtils.h"
27 #include "tools/fonts/FontToolUtils.h"
28 
29 #if defined(SK_GRAPHITE)
30 #include "include/gpu/graphite/ContextOptions.h"
31 #endif
32 
33 #if defined(SK_GANESH) || defined(SK_GRAPHITE)
34 #include "include/gpu/ganesh/GrContextOptions.h"
35 
36 class SlugGM : public skiagm::GM {
37 public:
SlugGM(const char * txt)38     SlugGM(const char* txt) : fText(txt) {}
39 
40 protected:
modifyGrContextOptions(GrContextOptions * ctxOptions)41     void modifyGrContextOptions(GrContextOptions* ctxOptions) override {
42         ctxOptions->fSupportBilerpFromGlyphAtlas = true;
43     }
44 
45 #if defined(SK_GRAPHITE)
modifyGraphiteContextOptions(skgpu::graphite::ContextOptions * options) const46     void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions* options) const override {
47         options->fSupportBilerpFromGlyphAtlas = true;
48     }
49 #endif
50 
onOnceBeforeDraw()51     void onOnceBeforeDraw() override {
52         fTypeface = ToolUtils::CreatePortableTypeface("serif", SkFontStyle());
53         SkFont font(fTypeface);
54         size_t txtLen = strlen(fText);
55         int glyphCount = font.countText(fText, txtLen, SkTextEncoding::kUTF8);
56 
57         fGlyphs.append(glyphCount);
58         font.textToGlyphs(fText, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), glyphCount);
59     }
60 
getName() const61     SkString getName() const override { return SkString("slug"); }
62 
getISize()63     SkISize getISize() override { return SkISize::Make(1000, 480); }
64 
onDraw(SkCanvas * canvas)65     void onDraw(SkCanvas* canvas) override {
66         sk_sp<SkTextBlob> blob(this->makeBlob());
67         SkPaint p;
68         p.setAntiAlias(true);
69         canvas->clipIRect(SkIRect::MakeSize(this->getISize()).makeInset(40, 50));
70         canvas->scale(1.3f, 1.3f);
71         sk_sp<sktext::gpu::Slug> slug = sktext::gpu::Slug::ConvertBlob(canvas, *blob, {10, 10}, p);
72         if (slug == nullptr) {
73             return;
74         }
75         canvas->translate(0.5, 0.5);
76         canvas->translate(30, 30);
77         canvas->drawTextBlob(blob, 10, 10, p);
78         canvas->translate(370, 0);
79         slug->draw(canvas, p);
80         for (float scale = 1.5; scale < 4; scale += 0.5) {
81             canvas->translate(-370, 20 * scale);
82             canvas->save();
83             canvas->scale(scale, scale);
84             canvas->rotate(5);
85             canvas->drawTextBlob(blob, 10, 10, p);
86             canvas->restore();
87             canvas->translate(370, 0);
88             canvas->save();
89             canvas->scale(scale, scale);
90             canvas->rotate(5);
91 
92             slug->draw(canvas, p);
93             canvas->restore();
94         }
95     }
96 
97 private:
makeBlob()98     sk_sp<SkTextBlob> makeBlob() {
99         SkTextBlobBuilder builder;
100 
101         SkFont font;
102         font.setSubpixel(true);
103         font.setEdging(SkFont::Edging::kAntiAlias);
104         font.setTypeface(fTypeface);
105         font.setSize(16);
106 
107         const SkTextBlobBuilder::RunBuffer& buf = builder.allocRun(font, fGlyphs.size(), 0, 0);
108         memcpy(buf.glyphs, fGlyphs.begin(), fGlyphs.size() * sizeof(uint16_t));
109         return builder.make();
110     }
111 
112     SkTDArray<uint16_t> fGlyphs;
113     sk_sp<SkTypeface>   fTypeface;
114     const char*         fText;
115     using INHERITED = skiagm::GM;
116 };
117 
118 DEF_GM(return new SlugGM("hamburgefons");)
119 #endif
120