blob: bd5ea221d125fb4a1cca1484f0db764403031659 [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"
9
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkPoint.h"
13#include "SkShader.h"
14#include "SkTextBlob.h"
15#include "SkTDArray.h"
16#include "SkTypeface.h"
17
18// This GM exercises drawTextBlob offset vs. shader space behavior.
19class TextBlobShaderGM : public skiagm::GM {
20public:
21 TextBlobShaderGM(const char* txt) {
22 SkPaint p;
23 size_t txtLen = strlen(txt);
24 fGlyphs.append(p.textToGlyphs(txt, txtLen, NULL));
25 p.textToGlyphs(txt, txtLen, fGlyphs.begin());
26 }
27
28protected:
29
mtklein36352bf2015-03-25 18:17:31 -070030 void onOnceBeforeDraw() override {
fmalita84833262014-09-19 11:40:51 -070031 SkPaint p;
32 p.setAntiAlias(true);
33 p.setSubpixelText(true);
34 p.setTextSize(30);
35 p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
caryclarkf597c422015-07-28 10:37:53 -070036 sk_tool_utils::set_portable_typeface(&p);
fmalita84833262014-09-19 11:40:51 -070037
38 SkTextBlobBuilder builder;
39 int glyphCount = fGlyphs.count();
40 const SkTextBlobBuilder::RunBuffer* run;
41
42 run = &builder.allocRun(p, glyphCount, 10, 10, NULL);
43 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
44
45 run = &builder.allocRunPosH(p, glyphCount, 80, NULL);
46 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
47 for (int i = 0; i < glyphCount; ++i) {
48 run->pos[i] = p.getTextSize() * i * .75f;
49 }
50
51 run = &builder.allocRunPos(p, glyphCount, NULL);
52 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
53 for (int i = 0; i < glyphCount; ++i) {
54 run->pos[i * 2] = p.getTextSize() * i * .75f;
55 run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount);
56 }
57
58 fBlob.reset(builder.build());
59
60 SkColor colors[2];
61 colors[0] = SK_ColorRED;
62 colors[1] = SK_ColorGREEN;
63
64 SkScalar pos[SK_ARRAY_COUNT(colors)];
65 for (unsigned i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
66 pos[i] = (float)i / (SK_ARRAY_COUNT(pos) - 1);
67 }
68
69 SkISize sz = this->onISize();
70 fShader.reset(SkGradientShader::CreateRadial(SkPoint::Make(SkIntToScalar(sz.width() / 2),
71 SkIntToScalar(sz.height() / 2)),
72 sz.width() * .66f, colors, pos,
73 SK_ARRAY_COUNT(colors),
74 SkShader::kRepeat_TileMode));
75 }
76
mtklein36352bf2015-03-25 18:17:31 -070077 SkString onShortName() override {
fmalita84833262014-09-19 11:40:51 -070078 return SkString("textblobshader");
79 }
80
mtklein36352bf2015-03-25 18:17:31 -070081 SkISize onISize() override {
fmalita84833262014-09-19 11:40:51 -070082 return SkISize::Make(640, 480);
83 }
84
mtklein36352bf2015-03-25 18:17:31 -070085 void onDraw(SkCanvas* canvas) override {
fmalita84833262014-09-19 11:40:51 -070086 SkPaint p;
87 p.setStyle(SkPaint::kFill_Style);
88 p.setShader(fShader);
89
90 SkISize sz = this->onISize();
91 static const int kXCount = 4;
92 static const int kYCount = 3;
93 for (int i = 0; i < kXCount; ++i) {
94 for (int j = 0; j < kYCount; ++j) {
95 canvas->drawTextBlob(fBlob,
96 SkIntToScalar(i * sz.width() / kXCount),
97 SkIntToScalar(j * sz.height() / kYCount),
98 p);
99 }
100 }
101 }
102
103private:
104 SkTDArray<uint16_t> fGlyphs;
105 SkAutoTUnref<const SkTextBlob> fBlob;
106 SkAutoTUnref<SkShader> fShader;
107
108 typedef skiagm::GM INHERITED;
109};
110
111DEF_GM( return SkNEW_ARGS(TextBlobShaderGM, ("Blobber")); )