2017-08-15 17:28:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "sk_tool_utils.h"
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkFontMgr.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkTextBlob.h"
|
|
|
|
|
2018-01-22 17:45:47 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContext.h"
|
2018-03-06 13:20:37 +00:00
|
|
|
#include "GrContextPriv.h"
|
2018-01-22 17:45:47 +00:00
|
|
|
#endif
|
|
|
|
|
2017-08-15 17:28:42 +00:00
|
|
|
static void make_paint(SkPaint* paint, sk_sp<SkTypeface> typeface) {
|
|
|
|
static const int kTextSize = 56;
|
|
|
|
|
|
|
|
paint->setAntiAlias(true);
|
|
|
|
paint->setColor(0xDE000000);
|
|
|
|
paint->setTypeface(typeface);
|
|
|
|
paint->setTextSize(kTextSize);
|
|
|
|
paint->setTextEncoding(SkPaint::kUTF32_TextEncoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkTypeface> chinese_typeface() {
|
|
|
|
#ifdef SK_BUILD_FOR_ANDROID
|
2017-12-08 19:25:14 +00:00
|
|
|
return MakeResourceAsTypeface("fonts/NotoSansCJK-Regular.ttc");
|
2018-01-24 17:42:55 +00:00
|
|
|
#elif defined(SK_BUILD_FOR_WIN)
|
2017-08-15 17:28:42 +00:00
|
|
|
return SkTypeface::MakeFromName("SimSun", SkFontStyle());
|
|
|
|
#elif defined(SK_BUILD_FOR_MAC)
|
|
|
|
return SkTypeface::MakeFromName("Hiragino Sans GB W3", SkFontStyle());
|
|
|
|
#elif defined(SK_BUILD_FOR_IOS)
|
|
|
|
return SkTypeface::MakeFromName("Hiragino Sans GB W3", SkFontStyle());
|
|
|
|
#elif defined(SK_BUILD_FOR_UNIX)
|
|
|
|
return SkTypeface::MakeFromName("Noto Sans CJK SC", SkFontStyle());
|
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChineseFlingView : public SampleView {
|
|
|
|
public:
|
2017-09-28 19:04:00 +00:00
|
|
|
ChineseFlingView() : fBlobs(kNumBlobs) {}
|
2017-08-15 17:28:42 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool onQuery(SkEvent* evt) override {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "chinese-fling");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
if (!fInitialized) {
|
|
|
|
this->init();
|
|
|
|
fInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->clear(0xFFDDDDDD);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
make_paint(&paint, fTypeface);
|
|
|
|
|
2017-09-28 19:04:00 +00:00
|
|
|
// draw a consistent run of the 'words' - one word per line
|
|
|
|
int index = fIndex;
|
2017-08-15 17:28:42 +00:00
|
|
|
for (SkScalar y = 0.0f; y < 1024.0f; ) {
|
|
|
|
|
|
|
|
y += -fMetrics.fAscent;
|
|
|
|
canvas->drawTextBlob(fBlobs[index], 0, y, paint);
|
|
|
|
|
|
|
|
y += fMetrics.fDescent + fMetrics.fLeading;
|
2017-09-28 19:04:00 +00:00
|
|
|
++index;
|
|
|
|
index %= fBlobs.count();
|
2017-08-15 17:28:42 +00:00
|
|
|
}
|
2017-09-28 19:04:00 +00:00
|
|
|
// now "fling" a random amount
|
|
|
|
fIndex += fRand.nextRangeU(5, 20);
|
|
|
|
fIndex %= fBlobs.count();
|
2017-08-15 17:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-09-28 19:04:00 +00:00
|
|
|
static constexpr auto kNumBlobs = 200;
|
|
|
|
static constexpr auto kWordLength = 16;
|
|
|
|
|
2017-08-15 17:28:42 +00:00
|
|
|
void init() {
|
|
|
|
fTypeface = chinese_typeface();
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
make_paint(&paint, fTypeface);
|
|
|
|
|
|
|
|
paint.getFontMetrics(&fMetrics);
|
|
|
|
|
2017-09-28 19:04:00 +00:00
|
|
|
SkUnichar glyphs[kWordLength];
|
|
|
|
for (int32_t i = 0; i < kNumBlobs; ++i) {
|
|
|
|
this->createRandomWord(glyphs);
|
2017-08-15 17:28:42 +00:00
|
|
|
|
|
|
|
SkTextBlobBuilder builder;
|
2017-09-28 19:04:00 +00:00
|
|
|
sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs, kWordLength*4,
|
|
|
|
paint, 0, 0);
|
2017-08-15 17:28:42 +00:00
|
|
|
|
|
|
|
fBlobs.emplace_back(builder.make());
|
|
|
|
}
|
2017-09-28 19:04:00 +00:00
|
|
|
|
|
|
|
fIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a random kWordLength character 'word' drawing from the full Chinese set
|
|
|
|
void createRandomWord(SkUnichar glyphs[kWordLength]) {
|
|
|
|
for (int i = 0; i < kWordLength; ++i) {
|
|
|
|
glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
|
|
|
|
}
|
2017-08-15 17:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool fInitialized = false;
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
|
|
|
SkPaint::FontMetrics fMetrics;
|
|
|
|
SkTArray<sk_sp<SkTextBlob>> fBlobs;
|
|
|
|
SkRandom fRand;
|
2017-09-28 19:04:00 +00:00
|
|
|
int fIndex;
|
2017-08-15 17:28:42 +00:00
|
|
|
|
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
2018-01-22 17:45:47 +00:00
|
|
|
class ChineseZoomView : public SampleView {
|
|
|
|
public:
|
2018-02-16 23:41:41 +00:00
|
|
|
ChineseZoomView() : fBlobs(kNumBlobs), fScale(15.0f), fTranslate(0.0f) {}
|
2018-01-22 17:45:47 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool onQuery(SkEvent* evt) override {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "chinese-zoom");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkUnichar uni;
|
|
|
|
if (SampleCode::CharQ(*evt, &uni)) {
|
|
|
|
if ('>' == uni) {
|
|
|
|
fScale += 0.125f;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ('<' == uni) {
|
|
|
|
fScale -= 0.125f;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2018-02-16 23:41:41 +00:00
|
|
|
bool afterFirstFrame = fInitialized;
|
2018-01-22 17:45:47 +00:00
|
|
|
if (!fInitialized) {
|
|
|
|
this->init();
|
|
|
|
fInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->clear(0xFFDDDDDD);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(0xDE000000);
|
|
|
|
paint.setTypeface(fTypeface);
|
|
|
|
paint.setTextSize(11);
|
|
|
|
paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
|
|
|
|
|
2018-02-16 23:41:41 +00:00
|
|
|
if (afterFirstFrame) {
|
2018-01-22 17:45:47 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2018-02-16 23:41:41 +00:00
|
|
|
GrContext* grContext = canvas->getGrContext();
|
|
|
|
if (grContext) {
|
|
|
|
sk_sp<SkImage> image =
|
2018-03-06 13:20:37 +00:00
|
|
|
grContext->contextPriv().getFontAtlasImage_ForTesting(
|
|
|
|
GrMaskFormat::kA8_GrMaskFormat, 0);
|
2018-02-16 23:41:41 +00:00
|
|
|
canvas->drawImageRect(image,
|
|
|
|
SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0), &paint);
|
2018-03-06 13:20:37 +00:00
|
|
|
image = grContext->contextPriv().getFontAtlasImage_ForTesting(
|
|
|
|
GrMaskFormat::kA8_GrMaskFormat, 1);
|
2018-02-16 23:41:41 +00:00
|
|
|
canvas->drawImageRect(image,
|
|
|
|
SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f), &paint);
|
2018-03-06 13:20:37 +00:00
|
|
|
image = grContext->contextPriv().getFontAtlasImage_ForTesting(
|
|
|
|
GrMaskFormat::kA8_GrMaskFormat, 2);
|
2018-02-16 23:41:41 +00:00
|
|
|
canvas->drawImageRect(image,
|
|
|
|
SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f), &paint);
|
2018-03-06 13:20:37 +00:00
|
|
|
image = grContext->contextPriv().getFontAtlasImage_ForTesting(
|
|
|
|
GrMaskFormat::kA8_GrMaskFormat, 3);
|
2018-02-16 23:41:41 +00:00
|
|
|
canvas->drawImageRect(image,
|
|
|
|
SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f), &paint);
|
|
|
|
}
|
2018-01-22 17:45:47 +00:00
|
|
|
#endif
|
2018-02-16 23:41:41 +00:00
|
|
|
}
|
2018-01-22 17:45:47 +00:00
|
|
|
|
|
|
|
canvas->scale(fScale, fScale);
|
2018-02-16 23:41:41 +00:00
|
|
|
canvas->translate(0, fTranslate);
|
|
|
|
fTranslate -= 0.5f;
|
2018-01-22 17:45:47 +00:00
|
|
|
|
|
|
|
// draw a consistent run of the 'words' - one word per line
|
|
|
|
SkScalar y = 0;
|
|
|
|
for (int index = 0; index < kNumBlobs; ++index) {
|
|
|
|
y += -fMetrics.fAscent;
|
|
|
|
canvas->drawTextBlob(fBlobs[index], 0, y, paint);
|
|
|
|
|
|
|
|
y += 3*(fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr auto kNumBlobs = 8;
|
|
|
|
static constexpr auto kParagraphLength = 175;
|
|
|
|
|
|
|
|
void init() {
|
|
|
|
fTypeface = chinese_typeface();
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(0xDE000000);
|
|
|
|
paint.setTypeface(fTypeface);
|
|
|
|
paint.setTextSize(11);
|
|
|
|
paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
|
|
|
|
|
|
|
|
paint.getFontMetrics(&fMetrics);
|
|
|
|
|
|
|
|
SkUnichar glyphs[45];
|
|
|
|
for (int32_t i = 0; i < kNumBlobs; ++i) {
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
auto paragraphLength = kParagraphLength;
|
|
|
|
SkScalar y = 0;
|
|
|
|
while (paragraphLength - 45 > 0) {
|
|
|
|
auto currentLineLength = SkTMin(45, paragraphLength - 45);
|
|
|
|
this->createRandomLine(glyphs, currentLineLength);
|
|
|
|
|
|
|
|
sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs,
|
|
|
|
currentLineLength*4, paint, 0, y);
|
|
|
|
y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading;
|
|
|
|
paragraphLength -= 45;
|
|
|
|
}
|
|
|
|
fBlobs.emplace_back(builder.make());
|
|
|
|
}
|
|
|
|
|
|
|
|
fIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a random kWordLength character 'word' drawing from the full Chinese set
|
|
|
|
void createRandomLine(SkUnichar glyphs[45], int lineLength) {
|
|
|
|
for (auto i = 0; i < lineLength; ++i) {
|
|
|
|
glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fInitialized = false;
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
|
|
|
SkPaint::FontMetrics fMetrics;
|
|
|
|
SkTArray<sk_sp<SkTextBlob>> fBlobs;
|
|
|
|
SkRandom fRand;
|
|
|
|
SkScalar fScale;
|
2018-02-16 23:41:41 +00:00
|
|
|
SkScalar fTranslate;
|
2018-01-22 17:45:47 +00:00
|
|
|
int fIndex;
|
|
|
|
|
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
2017-08-15 17:28:42 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-01-22 17:45:47 +00:00
|
|
|
static SkView* FlingFactory() { return new ChineseFlingView; }
|
|
|
|
static SkViewRegister regFling(FlingFactory);
|
|
|
|
|
|
|
|
static SkView* ZoomFactory() { return new ChineseZoomView; }
|
|
|
|
static SkViewRegister regZoom(ZoomFactory);
|