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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2017-08-15 17:28:42 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFontMetrics.h"
|
|
|
|
#include "include/core/SkFontMgr.h"
|
|
|
|
#include "include/core/SkTextBlob.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
2017-08-15 17:28:42 +00:00
|
|
|
|
2018-01-22 17:45:47 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2020-07-09 17:25:17 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2018-01-22 17:45:47 +00:00
|
|
|
#endif
|
|
|
|
|
2017-08-15 17:28:42 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class ChineseFlingView : public Sample {
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kNumBlobs = 200;
|
|
|
|
inline static constexpr int kWordLength = 16;
|
2019-07-17 13:08:11 +00:00
|
|
|
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
|
|
|
SkFontMetrics fMetrics;
|
|
|
|
sk_sp<SkTextBlob> fBlobs[kNumBlobs];
|
|
|
|
SkRandom fRand;
|
|
|
|
int fIndex = 0;
|
2017-08-15 17:28:42 +00:00
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("chinese-fling"); }
|
2017-08-15 17:28:42 +00:00
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
canvas->clear(0xFFDDDDDD);
|
|
|
|
|
|
|
|
SkPaint paint;
|
2018-12-22 03:22:31 +00:00
|
|
|
paint.setColor(0xDE000000);
|
2017-08-15 17:28:42 +00:00
|
|
|
|
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;
|
2019-07-17 13:08:11 +00:00
|
|
|
index %= kNumBlobs;
|
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);
|
2019-07-17 13:08:11 +00:00
|
|
|
fIndex %= kNumBlobs;
|
2017-08-15 17:28:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 13:08:11 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2017-08-15 17:28:42 +00:00
|
|
|
fTypeface = chinese_typeface();
|
|
|
|
|
2018-12-22 03:22:31 +00:00
|
|
|
SkFont font(fTypeface, 56);
|
|
|
|
font.getMetrics(&fMetrics);
|
2017-08-15 17:28:42 +00:00
|
|
|
|
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;
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob_w_len(&builder,
|
|
|
|
(const char*)glyphs,
|
|
|
|
kWordLength * 4,
|
2019-05-07 19:38:46 +00:00
|
|
|
SkTextEncoding::kUTF32,
|
2019-03-20 16:12:10 +00:00
|
|
|
font,
|
|
|
|
0,
|
|
|
|
0);
|
2017-08-15 17:28:42 +00:00
|
|
|
|
2019-07-17 13:08:11 +00:00
|
|
|
fBlobs[i] = builder.make();
|
2017-08-15 17:28:42 +00:00
|
|
|
}
|
2017-09-28 19:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class ChineseZoomView : public Sample {
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kNumBlobs = 8;
|
|
|
|
inline static constexpr int kParagraphLength = 175;
|
2019-07-17 13:08:11 +00:00
|
|
|
|
|
|
|
bool fAfterFirstFrame = false;
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
|
|
|
SkFontMetrics fMetrics;
|
|
|
|
sk_sp<SkTextBlob> fBlobs[kNumBlobs];
|
|
|
|
SkRandom fRand;
|
|
|
|
SkScalar fScale = 15;
|
|
|
|
SkScalar fTranslate = 0;
|
2018-01-22 17:45:47 +00:00
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("chinese-zoom"); }
|
|
|
|
|
2019-07-03 19:53:04 +00:00
|
|
|
bool onChar(SkUnichar uni) override {
|
2018-01-22 17:45:47 +00:00
|
|
|
if ('>' == uni) {
|
|
|
|
fScale += 0.125f;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ('<' == uni) {
|
|
|
|
fScale -= 0.125f;
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-03 19:53:04 +00:00
|
|
|
return false;
|
2018-01-22 17:45:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
canvas->clear(0xFFDDDDDD);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(0xDE000000);
|
|
|
|
|
2019-07-17 13:08:11 +00:00
|
|
|
if (fAfterFirstFrame) {
|
2018-01-22 17:45:47 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2020-07-09 17:25:17 +00:00
|
|
|
auto direct = GrAsDirectContext(canvas->recordingContext());
|
|
|
|
if (direct) {
|
|
|
|
sk_sp<SkImage> image = direct->priv().testingOnly_getFontAtlasImage(
|
2019-07-17 13:08:11 +00:00
|
|
|
GrMaskFormat::kA8_GrMaskFormat, 0);
|
2018-02-16 23:41:41 +00:00
|
|
|
canvas->drawImageRect(image,
|
2021-01-22 20:26:41 +00:00
|
|
|
SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0),
|
|
|
|
SkSamplingOptions(), &paint);
|
2020-07-09 17:25:17 +00:00
|
|
|
image = direct->priv().testingOnly_getFontAtlasImage(
|
2019-07-17 13:08:11 +00:00
|
|
|
GrMaskFormat::kA8_GrMaskFormat, 1);
|
2018-02-16 23:41:41 +00:00
|
|
|
canvas->drawImageRect(image,
|
2021-01-22 20:26:41 +00:00
|
|
|
SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f),
|
|
|
|
SkSamplingOptions(), &paint);
|
2020-07-09 17:25:17 +00:00
|
|
|
image = direct->priv().testingOnly_getFontAtlasImage(
|
2019-07-17 13:08:11 +00:00
|
|
|
GrMaskFormat::kA8_GrMaskFormat, 2);
|
2018-02-16 23:41:41 +00:00
|
|
|
canvas->drawImageRect(image,
|
2021-01-22 20:26:41 +00:00
|
|
|
SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f),
|
|
|
|
SkSamplingOptions(), &paint);
|
2020-07-09 17:25:17 +00:00
|
|
|
image = direct->priv().testingOnly_getFontAtlasImage(
|
2019-07-17 13:08:11 +00:00
|
|
|
GrMaskFormat::kA8_GrMaskFormat, 3);
|
2018-02-16 23:41:41 +00:00
|
|
|
canvas->drawImageRect(image,
|
2021-01-22 20:26:41 +00:00
|
|
|
SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f),
|
|
|
|
SkSamplingOptions(), &paint);
|
2018-02-16 23:41:41 +00:00
|
|
|
}
|
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);
|
|
|
|
}
|
2019-07-17 13:08:11 +00:00
|
|
|
if (!fAfterFirstFrame) {
|
|
|
|
fAfterFirstFrame = true;
|
|
|
|
}
|
2018-01-22 17:45:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 13:08:11 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2018-01-22 17:45:47 +00:00
|
|
|
fTypeface = chinese_typeface();
|
|
|
|
|
2018-12-22 03:22:31 +00:00
|
|
|
SkFont font(fTypeface, 11);
|
|
|
|
font.getMetrics(&fMetrics);
|
|
|
|
|
2018-01-22 17:45:47 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xDE000000);
|
|
|
|
|
|
|
|
SkUnichar glyphs[45];
|
|
|
|
for (int32_t i = 0; i < kNumBlobs; ++i) {
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
auto paragraphLength = kParagraphLength;
|
|
|
|
SkScalar y = 0;
|
|
|
|
while (paragraphLength - 45 > 0) {
|
2020-02-07 15:36:46 +00:00
|
|
|
auto currentLineLength = std::min(45, paragraphLength - 45);
|
2018-01-22 17:45:47 +00:00
|
|
|
this->createRandomLine(glyphs, currentLineLength);
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob_w_len(&builder,
|
|
|
|
(const char*)glyphs,
|
|
|
|
currentLineLength * 4,
|
2019-05-07 19:38:46 +00:00
|
|
|
SkTextEncoding::kUTF32,
|
2019-03-20 16:12:10 +00:00
|
|
|
font,
|
|
|
|
0,
|
|
|
|
y);
|
2018-01-22 17:45:47 +00:00
|
|
|
y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading;
|
|
|
|
paragraphLength -= 45;
|
|
|
|
}
|
2019-07-17 13:08:11 +00:00
|
|
|
fBlobs[i] = builder.make();
|
2018-01-22 17:45:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-15 17:28:42 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new ChineseFlingView(); )
|
|
|
|
DEF_SAMPLE( return new ChineseZoomView(); )
|