blob: 443c34a7775f5dd5bc9fd089e70a0e1fbcbebc76 [file] [log] [blame]
Herb Derby25c0e1a2021-11-10 08:34:51 -05001/*
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"
Kevin Lubickdc6cc022023-01-13 11:24:27 -050024#include "include/private/base/SkTDArray.h"
Jim Van Verthe55e0162022-05-20 15:01:58 -040025#include "include/private/chromium/Slug.h"
Herb Derby25c0e1a2021-11-10 08:34:51 -050026#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040027#include "tools/fonts/FontToolUtils.h"
Herb Derby25c0e1a2021-11-10 08:34:51 -050028
Robert Phillips9360fae2023-06-20 13:34:37 -040029#if defined(SK_GRAPHITE)
30#include "include/gpu/graphite/ContextOptions.h"
31#endif
32
33#if defined(SK_GANESH) || defined(SK_GRAPHITE)
Herb Derbya889f612022-04-21 15:40:13 -060034#include "include/gpu/GrContextOptions.h"
35
Herb Derby25c0e1a2021-11-10 08:34:51 -050036class SlugGM : public skiagm::GM {
37public:
Herb Derbya889f612022-04-21 15:40:13 -060038 SlugGM(const char* txt) : fText(txt) {}
Herb Derby25c0e1a2021-11-10 08:34:51 -050039
40protected:
Herb Derbya889f612022-04-21 15:40:13 -060041 void modifyGrContextOptions(GrContextOptions* ctxOptions) override {
42 ctxOptions->fSupportBilerpFromGlyphAtlas = true;
43 }
44
Robert Phillips9360fae2023-06-20 13:34:37 -040045#if defined(SK_GRAPHITE)
46 void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions* options) const override {
47 options->fSupportBilerpFromGlyphAtlas = true;
48 }
49#endif
50
Herb Derby25c0e1a2021-11-10 08:34:51 -050051 void onOnceBeforeDraw() override {
Kevin Lubicke836c3a2023-10-20 06:55:35 -040052 fTypeface = ToolUtils::CreatePortableTypeface("serif", SkFontStyle());
Herb Derby25c0e1a2021-11-10 08:34:51 -050053 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
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000061 SkString getName() const override { return SkString("slug"); }
Herb Derby25c0e1a2021-11-10 08:34:51 -050062
Leandro Lovisolo8f023882023-08-15 21:13:52 +000063 SkISize getISize() override { return SkISize::Make(1000, 480); }
Herb Derby25c0e1a2021-11-10 08:34:51 -050064
65 void onDraw(SkCanvas* canvas) override {
66 sk_sp<SkTextBlob> blob(this->makeBlob());
67 SkPaint p;
68 p.setAntiAlias(true);
Herb Derby672062d2021-12-02 15:49:41 -050069 canvas->clipIRect(SkIRect::MakeSize(this->getISize()).makeInset(40, 50));
Herb Derby25c0e1a2021-11-10 08:34:51 -050070 canvas->scale(1.3f, 1.3f);
Jim Van Verthe55e0162022-05-20 15:01:58 -040071 sk_sp<sktext::gpu::Slug> slug = sktext::gpu::Slug::ConvertBlob(canvas, *blob, {10, 10}, p);
Herb Derby25c0e1a2021-11-10 08:34:51 -050072 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);
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);
93 canvas->restore();
94 }
95 }
96
97private:
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
Herb Derby3050ef52022-09-29 21:12:37 -0400107 const SkTextBlobBuilder::RunBuffer& buf = builder.allocRun(font, fGlyphs.size(), 0, 0);
108 memcpy(buf.glyphs, fGlyphs.begin(), fGlyphs.size() * sizeof(uint16_t));
Herb Derby25c0e1a2021-11-10 08:34:51 -0500109 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
118DEF_GM(return new SlugGM("hamburgefons");)
119#endif