Flutter Engine
The Flutter Engine
slug.cpp
Go to the documentation of this file.
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"
11#include "include/core/SkFont.h"
16#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
26#include "tools/ToolUtils.h"
28
29#if defined(SK_GRAPHITE)
31#endif
32
33#if defined(SK_GANESH) || defined(SK_GRAPHITE)
35
36class SlugGM : public skiagm::GM {
37public:
38 SlugGM(const char* txt) : fText(txt) {}
39
40protected:
41 void modifyGrContextOptions(GrContextOptions* ctxOptions) override {
42 ctxOptions->fSupportBilerpFromGlyphAtlas = true;
43 }
44
45#if defined(SK_GRAPHITE)
47 options->fSupportBilerpFromGlyphAtlas = true;
48 }
49#endif
50
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
61 SkString getName() const override { return SkString("slug"); }
62
63 SkISize getISize() override { return SkISize::Make(1000, 480); }
64
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
97private:
98 sk_sp<SkTextBlob> makeBlob() {
100
101 SkFont font;
102 font.setSubpixel(true);
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
118DEF_GM(return new SlugGM("hamburgefons");)
119#endif
const char * options
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clipIRect(const SkIRect &irect, SkClipOp op=SkClipOp::kIntersect)
Definition: SkCanvas.h:991
void rotate(SkScalar degrees)
Definition: SkCanvas.cpp:1300
int save()
Definition: SkCanvas.cpp:447
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2484
Definition: SkFont.h:35
@ kAntiAlias
may have transparent pixels on glyph edges
Definition: gm.h:110
virtual SkISize getISize()=0
virtual void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions *) const
Definition: gm.h:179
virtual void onOnceBeforeDraw()
Definition: gm.cpp:167
virtual SkString getName() const =0
virtual void modifyGrContextOptions(GrContextOptions *)
Definition: gm.h:178
virtual DrawResult onDraw(SkCanvas *, SkString *errorMsg)
Definition: gm.cpp:139
static sk_sp< Slug > ConvertBlob(SkCanvas *canvas, const SkTextBlob &blob, SkPoint origin, const SkPaint &paint)
Definition: Slug.cpp:20
void draw(SkCanvas *canvas, const SkPaint &paint) const
Definition: Slug.cpp:50
#define DEF_GM(CODE)
Definition: gm.h:40
sk_sp< SkTypeface > CreatePortableTypeface(const char *name, SkFontStyle style)
font
Font Metadata and Metrics.
const Scalar scale
static constexpr SkIRect MakeSize(const SkISize &size)
Definition: SkRect.h:66
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
SkGlyphID * glyphs
storage for glyph indexes in run
Definition: SkTextBlob.h:329