blob: 779b5b9c3355670b734f9ed93dc600f99e24c9a3 [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:
22 TextBlobShaderGM(const char* txt) {
23 SkPaint p;
caryclarkefa1ece2015-07-28 11:55:49 -070024 sk_tool_utils::set_portable_typeface(&p);
fmalita84833262014-09-19 11:40:51 -070025 size_t txtLen = strlen(txt);
halcanary96fcdcc2015-08-27 07:41:13 -070026 fGlyphs.append(p.textToGlyphs(txt, txtLen, nullptr));
fmalita84833262014-09-19 11:40:51 -070027 p.textToGlyphs(txt, txtLen, fGlyphs.begin());
28 }
29
30protected:
31
mtklein36352bf2015-03-25 18:17:31 -070032 void onOnceBeforeDraw() override {
Mike Reed3185f902018-10-26 16:33:00 -040033 SkFont font;
Mike Reed5f50f572018-11-12 13:19:37 -050034 font.setSubpixel(true);
35 font.setEdging(SkFont::Edging::kAntiAlias);
Mike Reed3185f902018-10-26 16:33:00 -040036 font.setSize(30);
37 font.setTypeface(sk_tool_utils::create_portable_typeface());
fmalita84833262014-09-19 11:40:51 -070038
39 SkTextBlobBuilder builder;
40 int glyphCount = fGlyphs.count();
41 const SkTextBlobBuilder::RunBuffer* run;
42
Mike Reed3185f902018-10-26 16:33:00 -040043 run = &builder.allocRun(font, glyphCount, 10, 10, nullptr);
fmalita84833262014-09-19 11:40:51 -070044 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
45
Mike Reed3185f902018-10-26 16:33:00 -040046 run = &builder.allocRunPosH(font, glyphCount, 80, nullptr);
fmalita84833262014-09-19 11:40:51 -070047 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
48 for (int i = 0; i < glyphCount; ++i) {
Mike Reed3185f902018-10-26 16:33:00 -040049 run->pos[i] = font.getSize() * i * .75f;
fmalita84833262014-09-19 11:40:51 -070050 }
51
Mike Reed3185f902018-10-26 16:33:00 -040052 run = &builder.allocRunPos(font, glyphCount, nullptr);
fmalita84833262014-09-19 11:40:51 -070053 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
54 for (int i = 0; i < glyphCount; ++i) {
Mike Reed3185f902018-10-26 16:33:00 -040055 run->pos[i * 2] = font.getSize() * i * .75f;
fmalita84833262014-09-19 11:40:51 -070056 run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount);
57 }
58
fmalita37283c22016-09-13 10:00:23 -070059 fBlob = builder.make();
fmalita84833262014-09-19 11:40:51 -070060
61 SkColor colors[2];
62 colors[0] = SK_ColorRED;
63 colors[1] = SK_ColorGREEN;
64
65 SkScalar pos[SK_ARRAY_COUNT(colors)];
66 for (unsigned i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
67 pos[i] = (float)i / (SK_ARRAY_COUNT(pos) - 1);
68 }
69
70 SkISize sz = this->onISize();
reed1a9b9642016-03-13 14:13:58 -070071 fShader = SkGradientShader::MakeRadial(SkPoint::Make(SkIntToScalar(sz.width() / 2),
72 SkIntToScalar(sz.height() / 2)),
73 sz.width() * .66f, colors, pos,
74 SK_ARRAY_COUNT(colors),
75 SkShader::kRepeat_TileMode);
fmalita84833262014-09-19 11:40:51 -070076 }
77
mtklein36352bf2015-03-25 18:17:31 -070078 SkString onShortName() override {
fmalita84833262014-09-19 11:40:51 -070079 return SkString("textblobshader");
80 }
81
mtklein36352bf2015-03-25 18:17:31 -070082 SkISize onISize() override {
fmalita84833262014-09-19 11:40:51 -070083 return SkISize::Make(640, 480);
84 }
85
mtklein36352bf2015-03-25 18:17:31 -070086 void onDraw(SkCanvas* canvas) override {
fmalita84833262014-09-19 11:40:51 -070087 SkPaint p;
Mike Reed3185f902018-10-26 16:33:00 -040088 p.setAntiAlias(true);
fmalita84833262014-09-19 11:40:51 -070089 p.setStyle(SkPaint::kFill_Style);
90 p.setShader(fShader);
91
92 SkISize sz = this->onISize();
mtkleindbfd7ab2016-09-01 11:24:54 -070093 constexpr int kXCount = 4;
94 constexpr int kYCount = 3;
fmalita84833262014-09-19 11:40:51 -070095 for (int i = 0; i < kXCount; ++i) {
96 for (int j = 0; j < kYCount; ++j) {
97 canvas->drawTextBlob(fBlob,
98 SkIntToScalar(i * sz.width() / kXCount),
99 SkIntToScalar(j * sz.height() / kYCount),
100 p);
101 }
102 }
103 }
104
105private:
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
halcanary385fe4d2015-08-26 13:07:48 -0700113DEF_GM(return new TextBlobShaderGM("Blobber");)