blob: 7ec75b504de73b48676fff86c730c646ca98cf08 [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
30 virtual void onOnceBeforeDraw() SK_OVERRIDE {
31 SkPaint p;
32 p.setAntiAlias(true);
33 p.setSubpixelText(true);
34 p.setTextSize(30);
35 p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
36
37 SkTextBlobBuilder builder;
38 int glyphCount = fGlyphs.count();
39 const SkTextBlobBuilder::RunBuffer* run;
40
41 run = &builder.allocRun(p, glyphCount, 10, 10, NULL);
42 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
43
44 run = &builder.allocRunPosH(p, glyphCount, 80, NULL);
45 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
46 for (int i = 0; i < glyphCount; ++i) {
47 run->pos[i] = p.getTextSize() * i * .75f;
48 }
49
50 run = &builder.allocRunPos(p, glyphCount, NULL);
51 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
52 for (int i = 0; i < glyphCount; ++i) {
53 run->pos[i * 2] = p.getTextSize() * i * .75f;
54 run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount);
55 }
56
57 fBlob.reset(builder.build());
58
59 SkColor colors[2];
60 colors[0] = SK_ColorRED;
61 colors[1] = SK_ColorGREEN;
62
63 SkScalar pos[SK_ARRAY_COUNT(colors)];
64 for (unsigned i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
65 pos[i] = (float)i / (SK_ARRAY_COUNT(pos) - 1);
66 }
67
68 SkISize sz = this->onISize();
69 fShader.reset(SkGradientShader::CreateRadial(SkPoint::Make(SkIntToScalar(sz.width() / 2),
70 SkIntToScalar(sz.height() / 2)),
71 sz.width() * .66f, colors, pos,
72 SK_ARRAY_COUNT(colors),
73 SkShader::kRepeat_TileMode));
74 }
75
76 virtual uint32_t onGetFlags() const SK_OVERRIDE {
77 return kSkip565_Flag;
78 }
79
80 virtual SkString onShortName() SK_OVERRIDE {
81 return SkString("textblobshader");
82 }
83
84 virtual SkISize onISize() SK_OVERRIDE {
85 return SkISize::Make(640, 480);
86 }
87
88 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
89 SkPaint p;
90 p.setStyle(SkPaint::kFill_Style);
91 p.setShader(fShader);
92
93 SkISize sz = this->onISize();
94 static const int kXCount = 4;
95 static const int kYCount = 3;
96 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
106private:
107 SkTDArray<uint16_t> fGlyphs;
108 SkAutoTUnref<const SkTextBlob> fBlob;
109 SkAutoTUnref<SkShader> fShader;
110
111 typedef skiagm::GM INHERITED;
112};
113
114DEF_GM( return SkNEW_ARGS(TextBlobShaderGM, ("Blobber")); )