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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkShader.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTextBlob.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTileMode.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypeface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/private/SkTDArray.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
2014-09-19 18:40:51 +00:00
|
|
|
|
|
|
|
// This GM exercises drawTextBlob offset vs. shader space behavior.
|
|
|
|
class TextBlobShaderGM : public skiagm::GM {
|
|
|
|
public:
|
2018-12-28 16:12:50 +00:00
|
|
|
TextBlobShaderGM() {}
|
2014-09-19 18:40:51 +00:00
|
|
|
|
2018-12-28 16:12:50 +00:00
|
|
|
private:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2018-12-28 16:12:50 +00:00
|
|
|
{
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface());
|
2018-12-28 16:12:50 +00:00
|
|
|
const char* txt = "Blobber";
|
|
|
|
size_t txtLen = strlen(txt);
|
2019-05-07 19:38:46 +00:00
|
|
|
fGlyphs.append(font.countText(txt, txtLen, SkTextEncoding::kUTF8));
|
|
|
|
font.textToGlyphs(txt, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), fGlyphs.count());
|
2018-12-28 16:12:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 20:33:00 +00:00
|
|
|
SkFont font;
|
2018-11-12 18:19:37 +00:00
|
|
|
font.setSubpixel(true);
|
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
2018-10-26 20:33:00 +00:00
|
|
|
font.setSize(30);
|
2019-03-20 16:12:10 +00:00
|
|
|
font.setTypeface(ToolUtils::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),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kRepeat);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2018-12-28 16:12:50 +00:00
|
|
|
DEF_GM(return new TextBlobShaderGM;)
|