blob: 536f2c2abdd46ae1c92e005751a6087c00d1dc7c [file] [log] [blame]
Robert Phillips4c72b262017-08-15 13:28:42 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Resources.h"
9#include "SampleCode.h"
10#include "sk_tool_utils.h"
11
12#include "SkCanvas.h"
13#include "SkFontMgr.h"
14#include "SkRandom.h"
15#include "SkTypeface.h"
16#include "SkTextBlob.h"
17
18static void make_paint(SkPaint* paint, sk_sp<SkTypeface> typeface) {
19 static const int kTextSize = 56;
20
21 paint->setAntiAlias(true);
22 paint->setColor(0xDE000000);
23 paint->setTypeface(typeface);
24 paint->setTextSize(kTextSize);
25 paint->setTextEncoding(SkPaint::kUTF32_TextEncoding);
26}
27
Robert Phillips4c72b262017-08-15 13:28:42 -040028static sk_sp<SkTypeface> chinese_typeface() {
29#ifdef SK_BUILD_FOR_ANDROID
30 return MakeResourceAsTypeface("/fonts/NotoSansCJK-Regular.ttc");
31#elif defined(SK_BUILD_FOR_WIN32)
32 return SkTypeface::MakeFromName("SimSun", SkFontStyle());
33#elif defined(SK_BUILD_FOR_MAC)
34 return SkTypeface::MakeFromName("Hiragino Sans GB W3", SkFontStyle());
35#elif defined(SK_BUILD_FOR_IOS)
36 return SkTypeface::MakeFromName("Hiragino Sans GB W3", SkFontStyle());
37#elif defined(SK_BUILD_FOR_UNIX)
38 return SkTypeface::MakeFromName("Noto Sans CJK SC", SkFontStyle());
39#else
40 return nullptr;
41#endif
42}
43
44class ChineseFlingView : public SampleView {
45public:
Jim Van Verthc3269ae2017-09-28 15:04:00 -040046 ChineseFlingView() : fBlobs(kNumBlobs) {}
Robert Phillips4c72b262017-08-15 13:28:42 -040047
48protected:
49 bool onQuery(SkEvent* evt) override {
50 if (SampleCode::TitleQ(*evt)) {
51 SampleCode::TitleR(evt, "chinese-fling");
52 return true;
53 }
54 return this->INHERITED::onQuery(evt);
55 }
56
57 void onDrawContent(SkCanvas* canvas) override {
58 if (!fInitialized) {
59 this->init();
60 fInitialized = true;
61 }
62
63 canvas->clear(0xFFDDDDDD);
64
65 SkPaint paint;
66 make_paint(&paint, fTypeface);
67
Jim Van Verthc3269ae2017-09-28 15:04:00 -040068 // draw a consistent run of the 'words' - one word per line
69 int index = fIndex;
Robert Phillips4c72b262017-08-15 13:28:42 -040070 for (SkScalar y = 0.0f; y < 1024.0f; ) {
Robert Phillips4c72b262017-08-15 13:28:42 -040071
72 y += -fMetrics.fAscent;
73 canvas->drawTextBlob(fBlobs[index], 0, y, paint);
74
75 y += fMetrics.fDescent + fMetrics.fLeading;
Jim Van Verthc3269ae2017-09-28 15:04:00 -040076 ++index;
77 index %= fBlobs.count();
Robert Phillips4c72b262017-08-15 13:28:42 -040078 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -040079 // now "fling" a random amount
80 fIndex += fRand.nextRangeU(5, 20);
81 fIndex %= fBlobs.count();
Robert Phillips4c72b262017-08-15 13:28:42 -040082
83 this->inval(nullptr);
84 }
85
86private:
Jim Van Verthc3269ae2017-09-28 15:04:00 -040087 static constexpr auto kNumBlobs = 200;
88 static constexpr auto kWordLength = 16;
89
Robert Phillips4c72b262017-08-15 13:28:42 -040090 void init() {
91 fTypeface = chinese_typeface();
92
93 SkPaint paint;
94 make_paint(&paint, fTypeface);
95
96 paint.getFontMetrics(&fMetrics);
97
Jim Van Verthc3269ae2017-09-28 15:04:00 -040098 SkUnichar glyphs[kWordLength];
99 for (int32_t i = 0; i < kNumBlobs; ++i) {
100 this->createRandomWord(glyphs);
Robert Phillips4c72b262017-08-15 13:28:42 -0400101
102 SkTextBlobBuilder builder;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400103 sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs, kWordLength*4,
104 paint, 0, 0);
Robert Phillips4c72b262017-08-15 13:28:42 -0400105
106 fBlobs.emplace_back(builder.make());
107 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400108
109 fIndex = 0;
110 }
111
112 // Construct a random kWordLength character 'word' drawing from the full Chinese set
113 void createRandomWord(SkUnichar glyphs[kWordLength]) {
114 for (int i = 0; i < kWordLength; ++i) {
115 glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
116 }
Robert Phillips4c72b262017-08-15 13:28:42 -0400117 }
118
119 bool fInitialized = false;
120 sk_sp<SkTypeface> fTypeface;
121 SkPaint::FontMetrics fMetrics;
122 SkTArray<sk_sp<SkTextBlob>> fBlobs;
123 SkRandom fRand;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400124 int fIndex;
Robert Phillips4c72b262017-08-15 13:28:42 -0400125
126 typedef SkView INHERITED;
127};
128
129//////////////////////////////////////////////////////////////////////////////
130
131static SkView* MyFactory() { return new ChineseFlingView; }
132static SkViewRegister reg(MyFactory);