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 | |
| 28 | static void get_unicode_row(SkUnichar base, SkUnichar glyphs[16]) { |
| 29 | for (int i = 0x0; i <= 0xF; ++i) { |
| 30 | glyphs[i] = base; |
| 31 | glyphs[i] |= i; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | static sk_sp<SkTypeface> chinese_typeface() { |
| 36 | #ifdef SK_BUILD_FOR_ANDROID |
| 37 | return MakeResourceAsTypeface("/fonts/NotoSansCJK-Regular.ttc"); |
| 38 | #elif defined(SK_BUILD_FOR_WIN32) |
| 39 | return SkTypeface::MakeFromName("SimSun", SkFontStyle()); |
| 40 | #elif defined(SK_BUILD_FOR_MAC) |
| 41 | return SkTypeface::MakeFromName("Hiragino Sans GB W3", SkFontStyle()); |
| 42 | #elif defined(SK_BUILD_FOR_IOS) |
| 43 | return SkTypeface::MakeFromName("Hiragino Sans GB W3", SkFontStyle()); |
| 44 | #elif defined(SK_BUILD_FOR_UNIX) |
| 45 | return SkTypeface::MakeFromName("Noto Sans CJK SC", SkFontStyle()); |
| 46 | #else |
| 47 | return nullptr; |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | class ChineseFlingView : public SampleView { |
| 52 | public: |
| 53 | ChineseFlingView() {} |
| 54 | |
| 55 | protected: |
| 56 | bool onQuery(SkEvent* evt) override { |
| 57 | if (SampleCode::TitleQ(*evt)) { |
| 58 | SampleCode::TitleR(evt, "chinese-fling"); |
| 59 | return true; |
| 60 | } |
| 61 | return this->INHERITED::onQuery(evt); |
| 62 | } |
| 63 | |
| 64 | void onDrawContent(SkCanvas* canvas) override { |
| 65 | if (!fInitialized) { |
| 66 | this->init(); |
| 67 | fInitialized = true; |
| 68 | } |
| 69 | |
| 70 | canvas->clear(0xFFDDDDDD); |
| 71 | |
| 72 | SkPaint paint; |
| 73 | make_paint(&paint, fTypeface); |
| 74 | |
| 75 | for (SkScalar y = 0.0f; y < 1024.0f; ) { |
| 76 | int index = fRand.nextRangeU(0, fBlobs.count()-1); |
| 77 | |
| 78 | y += -fMetrics.fAscent; |
| 79 | canvas->drawTextBlob(fBlobs[index], 0, y, paint); |
| 80 | |
| 81 | y += fMetrics.fDescent + fMetrics.fLeading; |
| 82 | } |
| 83 | |
| 84 | this->inval(nullptr); |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | void init() { |
| 89 | fTypeface = chinese_typeface(); |
| 90 | |
| 91 | SkPaint paint; |
| 92 | make_paint(&paint, fTypeface); |
| 93 | |
| 94 | paint.getFontMetrics(&fMetrics); |
| 95 | |
| 96 | SkUnichar glyphs[16]; |
| 97 | |
| 98 | for (int32_t i = 0x4F00; i < 0x9FA0; i += 0x10) { |
| 99 | |
| 100 | get_unicode_row(i, glyphs); |
| 101 | |
| 102 | SkTextBlobBuilder builder; |
| 103 | |
| 104 | sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs, 16*4, paint, |
| 105 | 0, 0); |
| 106 | |
| 107 | fBlobs.emplace_back(builder.make()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | bool fInitialized = false; |
| 112 | sk_sp<SkTypeface> fTypeface; |
| 113 | SkPaint::FontMetrics fMetrics; |
| 114 | SkTArray<sk_sp<SkTextBlob>> fBlobs; |
| 115 | SkRandom fRand; |
| 116 | |
| 117 | typedef SkView INHERITED; |
| 118 | }; |
| 119 | |
| 120 | ////////////////////////////////////////////////////////////////////////////// |
| 121 | |
| 122 | static SkView* MyFactory() { return new ChineseFlingView; } |
| 123 | static SkViewRegister reg(MyFactory); |