2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkGradientShader.h"
14 #include "SkTextBlob.h"
15 #include "SkTDArray.h"
16 #include "SkTypeface.h"
18 // This GM exercises drawTextBlob offset vs. shader space behavior.
19 class TextBlobShaderGM : public skiagm::GM {
21 TextBlobShaderGM(const char* txt) {
23 size_t txtLen = strlen(txt);
24 fGlyphs.append(p.textToGlyphs(txt, txtLen, NULL));
25 p.textToGlyphs(txt, txtLen, fGlyphs.begin());
30 virtual void onOnceBeforeDraw() SK_OVERRIDE {
33 p.setSubpixelText(true);
35 p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
37 SkTextBlobBuilder builder;
38 int glyphCount = fGlyphs.count();
39 const SkTextBlobBuilder::RunBuffer* run;
41 run = &builder.allocRun(p, glyphCount, 10, 10, NULL);
42 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
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;
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);
57 fBlob.reset(builder.build());
60 colors[0] = SK_ColorRED;
61 colors[1] = SK_ColorGREEN;
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);
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));
76 virtual uint32_t onGetFlags() const SK_OVERRIDE {
80 virtual SkString onShortName() SK_OVERRIDE {
81 return SkString("textblobshader");
84 virtual SkISize onISize() SK_OVERRIDE {
85 return SkISize::Make(640, 480);
88 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
90 p.setStyle(SkPaint::kFill_Style);
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),
107 SkTDArray<uint16_t> fGlyphs;
108 SkAutoTUnref<const SkTextBlob> fBlob;
109 SkAutoTUnref<SkShader> fShader;
111 typedef skiagm::GM INHERITED;
114 DEF_GM( return SkNEW_ARGS(TextBlobShaderGM, ("Blobber")); )