Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | static 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 Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 28 | static 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 | |
| 44 | class ChineseFlingView : public SampleView { |
| 45 | public: |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 46 | ChineseFlingView() : fBlobs(kNumBlobs) {} |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 47 | |
| 48 | protected: |
| 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 Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 68 | // draw a consistent run of the 'words' - one word per line |
| 69 | int index = fIndex; |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 70 | for (SkScalar y = 0.0f; y < 1024.0f; ) { |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 71 | |
| 72 | y += -fMetrics.fAscent; |
| 73 | canvas->drawTextBlob(fBlobs[index], 0, y, paint); |
| 74 | |
| 75 | y += fMetrics.fDescent + fMetrics.fLeading; |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 76 | ++index; |
| 77 | index %= fBlobs.count(); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 78 | } |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 79 | // now "fling" a random amount |
| 80 | fIndex += fRand.nextRangeU(5, 20); |
| 81 | fIndex %= fBlobs.count(); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 82 | |
| 83 | this->inval(nullptr); |
| 84 | } |
| 85 | |
| 86 | private: |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 87 | static constexpr auto kNumBlobs = 200; |
| 88 | static constexpr auto kWordLength = 16; |
| 89 | |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 90 | void init() { |
| 91 | fTypeface = chinese_typeface(); |
| 92 | |
| 93 | SkPaint paint; |
| 94 | make_paint(&paint, fTypeface); |
| 95 | |
| 96 | paint.getFontMetrics(&fMetrics); |
| 97 | |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 98 | SkUnichar glyphs[kWordLength]; |
| 99 | for (int32_t i = 0; i < kNumBlobs; ++i) { |
| 100 | this->createRandomWord(glyphs); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 101 | |
| 102 | SkTextBlobBuilder builder; |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 103 | sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs, kWordLength*4, |
| 104 | paint, 0, 0); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 105 | |
| 106 | fBlobs.emplace_back(builder.make()); |
| 107 | } |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 108 | |
| 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 Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 117 | } |
| 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 Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 124 | int fIndex; |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 125 | |
| 126 | typedef SkView INHERITED; |
| 127 | }; |
| 128 | |
| 129 | ////////////////////////////////////////////////////////////////////////////// |
| 130 | |
| 131 | static SkView* MyFactory() { return new ChineseFlingView; } |
| 132 | static SkViewRegister reg(MyFactory); |