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 |
Hal Canary | 53e5e7d | 2017-12-08 14:25:14 -0500 | [diff] [blame^] | 30 | return MakeResourceAsTypeface("fonts/NotoSansCJK-Regular.ttc"); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 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 | |
| 84 | private: |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 85 | static constexpr auto kNumBlobs = 200; |
| 86 | static constexpr auto kWordLength = 16; |
| 87 | |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 88 | void init() { |
| 89 | fTypeface = chinese_typeface(); |
| 90 | |
| 91 | SkPaint paint; |
| 92 | make_paint(&paint, fTypeface); |
| 93 | |
| 94 | paint.getFontMetrics(&fMetrics); |
| 95 | |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 96 | SkUnichar glyphs[kWordLength]; |
| 97 | for (int32_t i = 0; i < kNumBlobs; ++i) { |
| 98 | this->createRandomWord(glyphs); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 99 | |
| 100 | SkTextBlobBuilder builder; |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 101 | sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs, kWordLength*4, |
| 102 | paint, 0, 0); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 103 | |
| 104 | fBlobs.emplace_back(builder.make()); |
| 105 | } |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 106 | |
| 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 Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 115 | } |
| 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 Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 122 | int fIndex; |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 123 | |
| 124 | typedef SkView INHERITED; |
| 125 | }; |
| 126 | |
| 127 | ////////////////////////////////////////////////////////////////////////////// |
| 128 | |
| 129 | static SkView* MyFactory() { return new ChineseFlingView; } |
| 130 | static SkViewRegister reg(MyFactory); |