2014-09-19 18:40:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2014-09-19 18:40:51 +00:00
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkTextBlob.h"
|
|
|
|
#include "SkTDArray.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
|
|
|
// This GM exercises drawTextBlob offset vs. shader space behavior.
|
|
|
|
class TextBlobShaderGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
TextBlobShaderGM(const char* txt) {
|
|
|
|
SkPaint p;
|
2015-07-28 18:55:49 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&p);
|
2014-09-19 18:40:51 +00:00
|
|
|
size_t txtLen = strlen(txt);
|
2015-08-27 14:41:13 +00:00
|
|
|
fGlyphs.append(p.textToGlyphs(txt, txtLen, nullptr));
|
2014-09-19 18:40:51 +00:00
|
|
|
p.textToGlyphs(txt, txtLen, fGlyphs.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2018-10-26 20:33:00 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setFlags(font.getFlags() | SkFont::kSubpixel_Flag | SkFont::kDEPRECATED_Antialias_Flag);
|
|
|
|
font.setSize(30);
|
|
|
|
font.setTypeface(sk_tool_utils::create_portable_typeface());
|
2014-09-19 18:40:51 +00:00
|
|
|
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
int glyphCount = fGlyphs.count();
|
|
|
|
const SkTextBlobBuilder::RunBuffer* run;
|
|
|
|
|
2018-10-26 20:33:00 +00:00
|
|
|
run = &builder.allocRun(font, glyphCount, 10, 10, nullptr);
|
2014-09-19 18:40:51 +00:00
|
|
|
memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
|
|
|
|
|
2018-10-26 20:33:00 +00:00
|
|
|
run = &builder.allocRunPosH(font, glyphCount, 80, nullptr);
|
2014-09-19 18:40:51 +00:00
|
|
|
memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
|
|
|
|
for (int i = 0; i < glyphCount; ++i) {
|
2018-10-26 20:33:00 +00:00
|
|
|
run->pos[i] = font.getSize() * i * .75f;
|
2014-09-19 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 20:33:00 +00:00
|
|
|
run = &builder.allocRunPos(font, glyphCount, nullptr);
|
2014-09-19 18:40:51 +00:00
|
|
|
memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
|
|
|
|
for (int i = 0; i < glyphCount; ++i) {
|
2018-10-26 20:33:00 +00:00
|
|
|
run->pos[i * 2] = font.getSize() * i * .75f;
|
2014-09-19 18:40:51 +00:00
|
|
|
run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount);
|
|
|
|
}
|
|
|
|
|
2016-09-13 17:00:23 +00:00
|
|
|
fBlob = builder.make();
|
2014-09-19 18:40:51 +00:00
|
|
|
|
|
|
|
SkColor colors[2];
|
|
|
|
colors[0] = SK_ColorRED;
|
|
|
|
colors[1] = SK_ColorGREEN;
|
|
|
|
|
|
|
|
SkScalar pos[SK_ARRAY_COUNT(colors)];
|
|
|
|
for (unsigned i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
|
|
|
|
pos[i] = (float)i / (SK_ARRAY_COUNT(pos) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize sz = this->onISize();
|
2016-03-13 21:13:58 +00:00
|
|
|
fShader = SkGradientShader::MakeRadial(SkPoint::Make(SkIntToScalar(sz.width() / 2),
|
|
|
|
SkIntToScalar(sz.height() / 2)),
|
|
|
|
sz.width() * .66f, colors, pos,
|
|
|
|
SK_ARRAY_COUNT(colors),
|
|
|
|
SkShader::kRepeat_TileMode);
|
2014-09-19 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-09-19 18:40:51 +00:00
|
|
|
return SkString("textblobshader");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-09-19 18:40:51 +00:00
|
|
|
return SkISize::Make(640, 480);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-09-19 18:40:51 +00:00
|
|
|
SkPaint p;
|
2018-10-26 20:33:00 +00:00
|
|
|
p.setAntiAlias(true);
|
2014-09-19 18:40:51 +00:00
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
|
|
|
p.setShader(fShader);
|
|
|
|
|
|
|
|
SkISize sz = this->onISize();
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int kXCount = 4;
|
|
|
|
constexpr int kYCount = 3;
|
2014-09-19 18:40:51 +00:00
|
|
|
for (int i = 0; i < kXCount; ++i) {
|
|
|
|
for (int j = 0; j < kYCount; ++j) {
|
|
|
|
canvas->drawTextBlob(fBlob,
|
|
|
|
SkIntToScalar(i * sz.width() / kXCount),
|
|
|
|
SkIntToScalar(j * sz.height() / kYCount),
|
|
|
|
p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-13 17:00:23 +00:00
|
|
|
SkTDArray<uint16_t> fGlyphs;
|
|
|
|
sk_sp<SkTextBlob> fBlob;
|
|
|
|
sk_sp<SkShader> fShader;
|
2014-09-19 18:40:51 +00:00
|
|
|
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new TextBlobShaderGM("Blobber");)
|