Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
textblobshader.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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"
18#include "include/core/SkSize.h"
26#include "tools/ToolUtils.h"
28
29#include <math.h>
30#include <string.h>
31
32// This GM exercises drawTextBlob offset vs. shader space behavior.
34public:
36
37private:
38 void onOnceBeforeDraw() override {
39 {
41 const char* txt = "Blobber";
42 size_t txtLen = strlen(txt);
43 fGlyphs.append(font.countText(txt, txtLen, SkTextEncoding::kUTF8));
44 font.textToGlyphs(txt, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), fGlyphs.size());
45 }
46
48 font.setSubpixel(true);
49 font.setEdging(SkFont::Edging::kAntiAlias);
50 font.setSize(30);
51
52 SkTextBlobBuilder builder;
53 int glyphCount = fGlyphs.size();
55
56 run = &builder.allocRun(font, glyphCount, 10, 10, nullptr);
57 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
58
59 run = &builder.allocRunPosH(font, glyphCount, 80, nullptr);
60 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
61 for (int i = 0; i < glyphCount; ++i) {
62 run->pos[i] = font.getSize() * i * .75f;
63 }
64
65 run = &builder.allocRunPos(font, glyphCount, nullptr);
66 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
67 for (int i = 0; i < glyphCount; ++i) {
68 run->pos[i * 2] = font.getSize() * i * .75f;
69 run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount);
70 }
71
72 fBlob = builder.make();
73
74 SkColor colors[2];
75 colors[0] = SK_ColorRED;
76 colors[1] = SK_ColorGREEN;
77
78 SkScalar pos[std::size(colors)];
79 for (unsigned i = 0; i < std::size(pos); ++i) {
80 pos[i] = (float)i / (std::size(pos) - 1);
81 }
82
83 SkISize sz = this->getISize();
85 SkIntToScalar(sz.height() / 2)),
86 sz.width() * .66f, colors, pos,
87 std::size(colors),
89 }
90
91 SkString getName() const override { return SkString("textblobshader"); }
92
93 SkISize getISize() override { return SkISize::Make(640, 480); }
94
95 void onDraw(SkCanvas* canvas) override {
96 SkPaint p;
97 p.setAntiAlias(true);
98 p.setStyle(SkPaint::kFill_Style);
99 p.setShader(fShader);
100
101 SkISize sz = this->getISize();
102 constexpr int kXCount = 4;
103 constexpr int kYCount = 3;
104 for (int i = 0; i < kXCount; ++i) {
105 for (int j = 0; j < kYCount; ++j) {
106 canvas->drawTextBlob(fBlob,
107 SkIntToScalar(i * sz.width() / kXCount),
108 SkIntToScalar(j * sz.height() / kYCount),
109 p);
110 }
111 }
112 }
113
114 SkTDArray<uint16_t> fGlyphs;
115 sk_sp<SkTextBlob> fBlob;
116 sk_sp<SkShader> fShader;
117
118 using INHERITED = skiagm::GM;
119};
120
121DEF_GM(return new TextBlobShaderGM;)
SkPoint pos
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SkIntToScalar(x)
Definition SkScalar.h:57
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
@ kAntiAlias
may have transparent pixels on glyph edges
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
int size() const
Definition SkTDArray.h:138
T * begin()
Definition SkTDArray.h:150
T * append()
Definition SkTDArray.h:191
void onOnceBeforeDraw() override
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
SkString getName() const override
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
SkFont DefaultPortableFont()
Definition run.py:1
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int32_t height() const
Definition SkSize.h:37
static constexpr SkPoint Make(float x, float y)