• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkTDArray.h"
25 #include "include/private/chromium/GrSlug.h"
26 #include "tools/ToolUtils.h"
27 
28 #if SK_SUPPORT_GPU && defined(SK_EXPERIMENTAL_SIMULATE_DRAWGLYPHRUNLIST_WITH_SLUG)
29 class SlugGM : public skiagm::GM {
30 public:
SlugGM(const char * txt)31     SlugGM(const char* txt)
32             : fText(txt) {
33     }
34 
35 protected:
onOnceBeforeDraw()36     void onOnceBeforeDraw() override {
37         fTypeface = ToolUtils::create_portable_typeface("serif", SkFontStyle());
38         SkFont font(fTypeface);
39         size_t txtLen = strlen(fText);
40         int glyphCount = font.countText(fText, txtLen, SkTextEncoding::kUTF8);
41 
42         fGlyphs.append(glyphCount);
43         font.textToGlyphs(fText, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), glyphCount);
44     }
45 
onShortName()46     SkString onShortName() override {
47         return SkString("slug");
48     }
49 
onISize()50     SkISize onISize() override {
51         return SkISize::Make(1000, 480);
52     }
53 
onDraw(SkCanvas * canvas)54     void onDraw(SkCanvas* canvas) override {
55         sk_sp<SkTextBlob> blob(this->makeBlob());
56         SkPaint p;
57         p.setAntiAlias(true);
58         canvas->clipIRect(SkIRect::MakeSize(this->getISize()).makeInset(40, 50));
59         canvas->scale(1.3f, 1.3f);
60         sk_sp<GrSlug> slug = GrSlug::ConvertBlob(canvas, *blob, {10, 10}, p);
61         if (slug == nullptr) {
62             return;
63         }
64         canvas->translate(0.5, 0.5);
65         canvas->translate(30, 30);
66         canvas->drawTextBlob(blob, 10, 10, p);
67         canvas->translate(370, 0);
68         slug->draw(canvas);
69         for (float scale = 1.5; scale < 4; scale += 0.5) {
70             canvas->translate(-370, 20 * scale);
71             canvas->save();
72             canvas->scale(scale, scale);
73             canvas->rotate(5);
74             canvas->drawTextBlob(blob, 10, 10, p);
75             canvas->restore();
76             canvas->translate(370, 0);
77             canvas->save();
78             canvas->scale(scale, scale);
79             canvas->rotate(5);
80 
81             slug->draw(canvas);
82             canvas->restore();
83         }
84     }
85 
86 private:
makeBlob()87     sk_sp<SkTextBlob> makeBlob() {
88         SkTextBlobBuilder builder;
89 
90         SkFont font;
91         font.setSubpixel(true);
92         font.setEdging(SkFont::Edging::kAntiAlias);
93         font.setTypeface(fTypeface);
94         font.setSize(16);
95 
96         const SkTextBlobBuilder::RunBuffer& buf = builder.allocRun(font, fGlyphs.count(), 0, 0);
97         memcpy(buf.glyphs, fGlyphs.begin(), fGlyphs.count() * sizeof(uint16_t));
98         return builder.make();
99     }
100 
101     SkTDArray<uint16_t> fGlyphs;
102     sk_sp<SkTypeface>   fTypeface;
103     const char*         fText;
104     using INHERITED = skiagm::GM;
105 };
106 
107 DEF_GM(return new SlugGM("hamburgefons");)
108 #endif
109