blob: 01366316d083dcaf2317617bc8e37134196a0d8f [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
Hal Canary53e5e7d2017-12-08 14:25:14 -050030 return MakeResourceAsTypeface("fonts/NotoSansCJK-Regular.ttc");
Robert Phillips4c72b262017-08-15 13:28:42 -040031#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
84private:
Jim Van Verthc3269ae2017-09-28 15:04:00 -040085 static constexpr auto kNumBlobs = 200;
86 static constexpr auto kWordLength = 16;
87
Robert Phillips4c72b262017-08-15 13:28:42 -040088 void init() {
89 fTypeface = chinese_typeface();
90
91 SkPaint paint;
92 make_paint(&paint, fTypeface);
93
94 paint.getFontMetrics(&fMetrics);
95
Jim Van Verthc3269ae2017-09-28 15:04:00 -040096 SkUnichar glyphs[kWordLength];
97 for (int32_t i = 0; i < kNumBlobs; ++i) {
98 this->createRandomWord(glyphs);
Robert Phillips4c72b262017-08-15 13:28:42 -040099
100 SkTextBlobBuilder builder;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400101 sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs, kWordLength*4,
102 paint, 0, 0);
Robert Phillips4c72b262017-08-15 13:28:42 -0400103
104 fBlobs.emplace_back(builder.make());
105 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400106
107 fIndex = 0;
108 }
109
110 // Construct a random kWordLength character 'word' drawing from the full Chinese set
111 void createRandomWord(SkUnichar glyphs[kWordLength]) {
112 for (int i = 0; i < kWordLength; ++i) {
113 glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
114 }
Robert Phillips4c72b262017-08-15 13:28:42 -0400115 }
116
117 bool fInitialized = false;
118 sk_sp<SkTypeface> fTypeface;
119 SkPaint::FontMetrics fMetrics;
120 SkTArray<sk_sp<SkTextBlob>> fBlobs;
121 SkRandom fRand;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400122 int fIndex;
Robert Phillips4c72b262017-08-15 13:28:42 -0400123
124 typedef SkView INHERITED;
125};
126
127//////////////////////////////////////////////////////////////////////////////
128
129static SkView* MyFactory() { return new ChineseFlingView; }
130static SkViewRegister reg(MyFactory);