2014-08-21 15:53:26 +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"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFontStyle.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFontTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTextBlob.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkTDArray.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2018-03-14 19:07:43 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
2014-08-21 15:53:26 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum Pos {
|
|
|
|
kDefault_Pos = 0,
|
|
|
|
kScalar_Pos = 1,
|
|
|
|
kPoint_Pos = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct BlobCfg {
|
|
|
|
unsigned count;
|
|
|
|
Pos pos;
|
|
|
|
SkScalar scale;
|
|
|
|
} blobConfigs[][3][3] = {
|
|
|
|
{
|
|
|
|
{ { 1024, kDefault_Pos, 1 }, { 0, kDefault_Pos, 0 }, { 0, kDefault_Pos, 0 } },
|
|
|
|
{ { 1024, kScalar_Pos, 1 }, { 0, kScalar_Pos, 0 }, { 0, kScalar_Pos, 0 } },
|
|
|
|
{ { 1024, kPoint_Pos, 1 }, { 0, kPoint_Pos, 0 }, { 0, kPoint_Pos, 0 } },
|
|
|
|
},
|
|
|
|
{
|
2014-08-22 16:01:19 +00:00
|
|
|
{ { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 } },
|
|
|
|
{ { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 } },
|
|
|
|
{ { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 } },
|
2014-08-21 15:53:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2014-08-22 16:01:19 +00:00
|
|
|
{ { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 } },
|
|
|
|
{ { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 } },
|
|
|
|
{ { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 } },
|
2014-08-21 15:53:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2014-08-22 16:01:19 +00:00
|
|
|
{ { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 } },
|
|
|
|
{ { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 } },
|
|
|
|
{ { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 } },
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
{ { 4, kDefault_Pos, .75f }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1.25f } },
|
|
|
|
{ { 4, kScalar_Pos, .75f }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1.25f } },
|
|
|
|
{ { 4, kPoint_Pos, .75f }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1.25f } },
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
{ { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, .75f }, { 4, kPoint_Pos, 1.25f } },
|
|
|
|
{ { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, .75f }, { 4, kDefault_Pos, 1.25f } },
|
|
|
|
{ { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, .75f }, { 4, kScalar_Pos, 1.25f } },
|
2014-08-21 15:53:26 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkScalar kFontSize = 16;
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace
|
2014-08-21 15:53:26 +00:00
|
|
|
|
|
|
|
class TextBlobGM : public skiagm::GM {
|
|
|
|
public:
|
2014-09-17 00:58:34 +00:00
|
|
|
TextBlobGM(const char* txt)
|
2015-05-21 13:15:28 +00:00
|
|
|
: fText(txt) {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
fTypeface = ToolUtils::create_portable_typeface("serif", SkFontStyle());
|
2018-12-26 03:06:17 +00:00
|
|
|
SkFont font(fTypeface);
|
2015-05-21 13:15:28 +00:00
|
|
|
size_t txtLen = strlen(fText);
|
2019-05-07 19:38:46 +00:00
|
|
|
int glyphCount = font.countText(fText, txtLen, SkTextEncoding::kUTF8);
|
2014-08-21 15:53:26 +00:00
|
|
|
|
|
|
|
fGlyphs.append(glyphCount);
|
2019-05-07 19:38:46 +00:00
|
|
|
font.textToGlyphs(fText, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), glyphCount);
|
2014-08-21 15:53:26 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-08-21 15:53:26 +00:00
|
|
|
return SkString("textblob");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-08-21 15:53:26 +00:00
|
|
|
return SkISize::Make(640, 480);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2022-06-21 13:49:17 +00:00
|
|
|
for (unsigned b = 0; b < std::size(blobConfigs); ++b) {
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob(this->makeBlob(b));
|
2014-08-21 15:53:26 +00:00
|
|
|
|
|
|
|
SkPaint p;
|
2018-10-26 20:33:00 +00:00
|
|
|
p.setAntiAlias(true);
|
2014-08-21 15:53:26 +00:00
|
|
|
SkPoint offset = SkPoint::Make(SkIntToScalar(10 + 300 * (b % 2)),
|
|
|
|
SkIntToScalar(20 + 150 * (b / 2)));
|
|
|
|
|
|
|
|
canvas->drawTextBlob(blob, offset.x(), offset.y(), p);
|
|
|
|
|
|
|
|
p.setColor(SK_ColorBLUE);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
SkRect box = blob->bounds();
|
|
|
|
box.offset(offset);
|
2018-10-26 20:33:00 +00:00
|
|
|
p.setAntiAlias(false);
|
2014-08-21 15:53:26 +00:00
|
|
|
canvas->drawRect(box, p);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> makeBlob(unsigned blobIndex) {
|
2014-08-21 15:53:26 +00:00
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
|
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);
|
2014-09-17 00:58:34 +00:00
|
|
|
font.setTypeface(fTypeface);
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2022-06-21 13:49:17 +00:00
|
|
|
for (unsigned l = 0; l < std::size(blobConfigs[blobIndex]); ++l) {
|
2014-08-21 15:53:26 +00:00
|
|
|
unsigned currentGlyph = 0;
|
|
|
|
|
2022-06-21 13:49:17 +00:00
|
|
|
for (unsigned c = 0; c < std::size(blobConfigs[blobIndex][l]); ++c) {
|
2014-08-21 15:53:26 +00:00
|
|
|
const BlobCfg* cfg = &blobConfigs[blobIndex][l][c];
|
|
|
|
unsigned count = cfg->count;
|
|
|
|
|
|
|
|
if (count > fGlyphs.count() - currentGlyph) {
|
|
|
|
count = fGlyphs.count() - currentGlyph;
|
|
|
|
}
|
|
|
|
if (0 == count) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-10-26 20:33:00 +00:00
|
|
|
font.setSize(kFontSize * cfg->scale);
|
|
|
|
const SkScalar advanceX = font.getSize() * 0.85f;
|
|
|
|
const SkScalar advanceY = font.getSize() * 1.5f;
|
2014-08-21 15:53:26 +00:00
|
|
|
|
|
|
|
SkPoint offset = SkPoint::Make(currentGlyph * advanceX + c * advanceX,
|
|
|
|
advanceY * l);
|
|
|
|
switch (cfg->pos) {
|
|
|
|
case kDefault_Pos: {
|
|
|
|
const SkTextBlobBuilder::RunBuffer& buf = builder.allocRun(font, count,
|
|
|
|
offset.x(),
|
|
|
|
offset.y());
|
|
|
|
memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t));
|
|
|
|
} break;
|
|
|
|
case kScalar_Pos: {
|
|
|
|
const SkTextBlobBuilder::RunBuffer& buf = builder.allocRunPosH(font, count,
|
|
|
|
offset.y());
|
|
|
|
SkTDArray<SkScalar> pos;
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
|
|
*pos.append() = offset.x() + i * advanceX;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t));
|
|
|
|
memcpy(buf.pos, pos.begin(), count * sizeof(SkScalar));
|
|
|
|
} break;
|
|
|
|
case kPoint_Pos: {
|
|
|
|
const SkTextBlobBuilder::RunBuffer& buf = builder.allocRunPos(font, count);
|
|
|
|
|
|
|
|
SkTDArray<SkScalar> pos;
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
|
|
*pos.append() = offset.x() + i * advanceX;
|
|
|
|
*pos.append() = offset.y() + i * (advanceY / count);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t));
|
|
|
|
memcpy(buf.pos, pos.begin(), count * sizeof(SkScalar) * 2);
|
|
|
|
} break;
|
|
|
|
default:
|
2016-01-29 16:51:04 +00:00
|
|
|
SK_ABORT("unhandled pos value");
|
2014-08-21 15:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
currentGlyph += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 17:00:23 +00:00
|
|
|
return builder.make();
|
2014-08-21 15:53:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
SkTDArray<uint16_t> fGlyphs;
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
|
|
|
const char* fText;
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2014-08-21 15:53:26 +00:00
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new TextBlobGM("hamburgefons");)
|