blob: 971f6288fa4741033a9ecbfcb636c44104295980 [file] [log] [blame]
fmalita84833262014-09-19 11:40:51 -07001/*
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.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
fmalita84833262014-09-19 11:40:51 -070010
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkPoint.h"
14#include "SkShader.h"
15#include "SkTextBlob.h"
16#include "SkTDArray.h"
17#include "SkTypeface.h"
18
19// This GM exercises drawTextBlob offset vs. shader space behavior.
20class TextBlobShaderGM : public skiagm::GM {
21public:
Mike Klein0fe39ba2018-12-28 11:12:50 -050022 TextBlobShaderGM() {}
fmalita84833262014-09-19 11:40:51 -070023
Mike Klein0fe39ba2018-12-28 11:12:50 -050024private:
mtklein36352bf2015-03-25 18:17:31 -070025 void onOnceBeforeDraw() override {
Mike Klein0fe39ba2018-12-28 11:12:50 -050026 {
27 SkFont font(sk_tool_utils::create_portable_typeface());
28 const char* txt = "Blobber";
29 size_t txtLen = strlen(txt);
30 fGlyphs.append(font.countText(txt, txtLen, kUTF8_SkTextEncoding));
31 font.textToGlyphs(txt, txtLen, kUTF8_SkTextEncoding, fGlyphs.begin(), fGlyphs.count());
32 }
33
Mike Reed3185f902018-10-26 16:33:00 -040034 SkFont font;
Mike Reed5f50f572018-11-12 13:19:37 -050035 font.setSubpixel(true);
36 font.setEdging(SkFont::Edging::kAntiAlias);
Mike Reed3185f902018-10-26 16:33:00 -040037 font.setSize(30);
38 font.setTypeface(sk_tool_utils::create_portable_typeface());
fmalita84833262014-09-19 11:40:51 -070039
40 SkTextBlobBuilder builder;
41 int glyphCount = fGlyphs.count();
42 const SkTextBlobBuilder::RunBuffer* run;
43
Mike Reed3185f902018-10-26 16:33:00 -040044 run = &builder.allocRun(font, glyphCount, 10, 10, nullptr);
fmalita84833262014-09-19 11:40:51 -070045 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
46
Mike Reed3185f902018-10-26 16:33:00 -040047 run = &builder.allocRunPosH(font, glyphCount, 80, nullptr);
fmalita84833262014-09-19 11:40:51 -070048 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
49 for (int i = 0; i < glyphCount; ++i) {
Mike Reed3185f902018-10-26 16:33:00 -040050 run->pos[i] = font.getSize() * i * .75f;
fmalita84833262014-09-19 11:40:51 -070051 }
52
Mike Reed3185f902018-10-26 16:33:00 -040053 run = &builder.allocRunPos(font, glyphCount, nullptr);
fmalita84833262014-09-19 11:40:51 -070054 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
55 for (int i = 0; i < glyphCount; ++i) {
Mike Reed3185f902018-10-26 16:33:00 -040056 run->pos[i * 2] = font.getSize() * i * .75f;
fmalita84833262014-09-19 11:40:51 -070057 run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount);
58 }
59
fmalita37283c22016-09-13 10:00:23 -070060 fBlob = builder.make();
fmalita84833262014-09-19 11:40:51 -070061
62 SkColor colors[2];
63 colors[0] = SK_ColorRED;
64 colors[1] = SK_ColorGREEN;
65
66 SkScalar pos[SK_ARRAY_COUNT(colors)];
67 for (unsigned i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
68 pos[i] = (float)i / (SK_ARRAY_COUNT(pos) - 1);
69 }
70
71 SkISize sz = this->onISize();
reed1a9b9642016-03-13 14:13:58 -070072 fShader = SkGradientShader::MakeRadial(SkPoint::Make(SkIntToScalar(sz.width() / 2),
73 SkIntToScalar(sz.height() / 2)),
74 sz.width() * .66f, colors, pos,
75 SK_ARRAY_COUNT(colors),
76 SkShader::kRepeat_TileMode);
fmalita84833262014-09-19 11:40:51 -070077 }
78
mtklein36352bf2015-03-25 18:17:31 -070079 SkString onShortName() override {
fmalita84833262014-09-19 11:40:51 -070080 return SkString("textblobshader");
81 }
82
mtklein36352bf2015-03-25 18:17:31 -070083 SkISize onISize() override {
fmalita84833262014-09-19 11:40:51 -070084 return SkISize::Make(640, 480);
85 }
86
mtklein36352bf2015-03-25 18:17:31 -070087 void onDraw(SkCanvas* canvas) override {
fmalita84833262014-09-19 11:40:51 -070088 SkPaint p;
Mike Reed3185f902018-10-26 16:33:00 -040089 p.setAntiAlias(true);
fmalita84833262014-09-19 11:40:51 -070090 p.setStyle(SkPaint::kFill_Style);
91 p.setShader(fShader);
92
93 SkISize sz = this->onISize();
mtkleindbfd7ab2016-09-01 11:24:54 -070094 constexpr int kXCount = 4;
95 constexpr int kYCount = 3;
fmalita84833262014-09-19 11:40:51 -070096 for (int i = 0; i < kXCount; ++i) {
97 for (int j = 0; j < kYCount; ++j) {
98 canvas->drawTextBlob(fBlob,
99 SkIntToScalar(i * sz.width() / kXCount),
100 SkIntToScalar(j * sz.height() / kYCount),
101 p);
102 }
103 }
104 }
105
fmalita37283c22016-09-13 10:00:23 -0700106 SkTDArray<uint16_t> fGlyphs;
107 sk_sp<SkTextBlob> fBlob;
108 sk_sp<SkShader> fShader;
fmalita84833262014-09-19 11:40:51 -0700109
110 typedef skiagm::GM INHERITED;
111};
112
Mike Klein0fe39ba2018-12-28 11:12:50 -0500113DEF_GM(return new TextBlobShaderGM;)