blob: 085f7b004acd67c3c482ab6eb3d00adf97807de0 [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 {
fmalita84833262014-09-19 11:40:51 -070033 SkPaint p;
34 p.setAntiAlias(true);
35 p.setSubpixelText(true);
36 p.setTextSize(30);
37 p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
caryclarkf597c422015-07-28 10:37:53 -070038 sk_tool_utils::set_portable_typeface(&p);
fmalita84833262014-09-19 11:40:51 -070039
40 SkTextBlobBuilder builder;
41 int glyphCount = fGlyphs.count();
42 const SkTextBlobBuilder::RunBuffer* run;
43
halcanary96fcdcc2015-08-27 07:41:13 -070044 run = &builder.allocRun(p, glyphCount, 10, 10, nullptr);
fmalita84833262014-09-19 11:40:51 -070045 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
46
halcanary96fcdcc2015-08-27 07:41:13 -070047 run = &builder.allocRunPosH(p, 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) {
50 run->pos[i] = p.getTextSize() * i * .75f;
51 }
52
halcanary96fcdcc2015-08-27 07:41:13 -070053 run = &builder.allocRunPos(p, 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) {
56 run->pos[i * 2] = p.getTextSize() * i * .75f;
57 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;
89 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");)