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" |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 9 | #include "Sample.h" |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 10 | #include "sk_tool_utils.h" |
| 11 | |
| 12 | #include "SkCanvas.h" |
Mike Reed | 9cd2a5c | 2019-01-22 15:17:59 -0500 | [diff] [blame^] | 13 | #include "SkFontMetrics.h" |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 14 | #include "SkFontMgr.h" |
| 15 | #include "SkRandom.h" |
| 16 | #include "SkTypeface.h" |
| 17 | #include "SkTextBlob.h" |
| 18 | |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 19 | #if SK_SUPPORT_GPU |
| 20 | #include "GrContext.h" |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 21 | #include "GrContextPriv.h" |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 22 | #endif |
| 23 | |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 24 | static sk_sp<SkTypeface> chinese_typeface() { |
| 25 | #ifdef SK_BUILD_FOR_ANDROID |
Hal Canary | 53e5e7d | 2017-12-08 14:25:14 -0500 | [diff] [blame] | 26 | return MakeResourceAsTypeface("fonts/NotoSansCJK-Regular.ttc"); |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 27 | #elif defined(SK_BUILD_FOR_WIN) |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 28 | return SkTypeface::MakeFromName("SimSun", SkFontStyle()); |
| 29 | #elif defined(SK_BUILD_FOR_MAC) |
| 30 | return SkTypeface::MakeFromName("Hiragino Sans GB W3", SkFontStyle()); |
| 31 | #elif defined(SK_BUILD_FOR_IOS) |
| 32 | return SkTypeface::MakeFromName("Hiragino Sans GB W3", SkFontStyle()); |
| 33 | #elif defined(SK_BUILD_FOR_UNIX) |
| 34 | return SkTypeface::MakeFromName("Noto Sans CJK SC", SkFontStyle()); |
| 35 | #else |
| 36 | return nullptr; |
| 37 | #endif |
| 38 | } |
| 39 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 40 | class ChineseFlingView : public Sample { |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 41 | public: |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 42 | ChineseFlingView() : fBlobs(kNumBlobs) {} |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 43 | |
| 44 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 45 | bool onQuery(Sample::Event* evt) override { |
| 46 | if (Sample::TitleQ(*evt)) { |
| 47 | Sample::TitleR(evt, "chinese-fling"); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 48 | return true; |
| 49 | } |
| 50 | return this->INHERITED::onQuery(evt); |
| 51 | } |
| 52 | |
| 53 | void onDrawContent(SkCanvas* canvas) override { |
| 54 | if (!fInitialized) { |
| 55 | this->init(); |
| 56 | fInitialized = true; |
| 57 | } |
| 58 | |
| 59 | canvas->clear(0xFFDDDDDD); |
| 60 | |
| 61 | SkPaint paint; |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 62 | paint.setColor(0xDE000000); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 63 | |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 64 | // draw a consistent run of the 'words' - one word per line |
| 65 | int index = fIndex; |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 66 | for (SkScalar y = 0.0f; y < 1024.0f; ) { |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 67 | |
| 68 | y += -fMetrics.fAscent; |
| 69 | canvas->drawTextBlob(fBlobs[index], 0, y, paint); |
| 70 | |
| 71 | y += fMetrics.fDescent + fMetrics.fLeading; |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 72 | ++index; |
| 73 | index %= fBlobs.count(); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 74 | } |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 75 | // now "fling" a random amount |
| 76 | fIndex += fRand.nextRangeU(5, 20); |
| 77 | fIndex %= fBlobs.count(); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | private: |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 81 | static constexpr auto kNumBlobs = 200; |
| 82 | static constexpr auto kWordLength = 16; |
| 83 | |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 84 | void init() { |
| 85 | fTypeface = chinese_typeface(); |
| 86 | |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 87 | SkFont font(fTypeface, 56); |
| 88 | font.getMetrics(&fMetrics); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 89 | |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 90 | SkUnichar glyphs[kWordLength]; |
| 91 | for (int32_t i = 0; i < kNumBlobs; ++i) { |
| 92 | this->createRandomWord(glyphs); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 93 | |
| 94 | SkTextBlobBuilder builder; |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 95 | sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs, kWordLength*4, |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 96 | kUTF32_SkTextEncoding, font, 0, 0); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 97 | |
| 98 | fBlobs.emplace_back(builder.make()); |
| 99 | } |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 100 | |
| 101 | fIndex = 0; |
| 102 | } |
| 103 | |
| 104 | // Construct a random kWordLength character 'word' drawing from the full Chinese set |
| 105 | void createRandomWord(SkUnichar glyphs[kWordLength]) { |
| 106 | for (int i = 0; i < kWordLength; ++i) { |
| 107 | glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0); |
| 108 | } |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool fInitialized = false; |
| 112 | sk_sp<SkTypeface> fTypeface; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 113 | SkFontMetrics fMetrics; |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 114 | SkTArray<sk_sp<SkTextBlob>> fBlobs; |
| 115 | SkRandom fRand; |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 116 | int fIndex; |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 117 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 118 | typedef Sample INHERITED; |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 119 | }; |
| 120 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 121 | class ChineseZoomView : public Sample { |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 122 | public: |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 123 | ChineseZoomView() : fBlobs(kNumBlobs), fScale(15.0f), fTranslate(0.0f) {} |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 124 | |
| 125 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 126 | bool onQuery(Sample::Event* evt) override { |
| 127 | if (Sample::TitleQ(*evt)) { |
| 128 | Sample::TitleR(evt, "chinese-zoom"); |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | SkUnichar uni; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 132 | if (Sample::CharQ(*evt, &uni)) { |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 133 | if ('>' == uni) { |
| 134 | fScale += 0.125f; |
| 135 | return true; |
| 136 | } |
| 137 | if ('<' == uni) { |
| 138 | fScale -= 0.125f; |
| 139 | return true; |
| 140 | } |
| 141 | } |
| 142 | return this->INHERITED::onQuery(evt); |
| 143 | } |
| 144 | |
| 145 | void onDrawContent(SkCanvas* canvas) override { |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 146 | bool afterFirstFrame = fInitialized; |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 147 | if (!fInitialized) { |
| 148 | this->init(); |
| 149 | fInitialized = true; |
| 150 | } |
| 151 | |
| 152 | canvas->clear(0xFFDDDDDD); |
| 153 | |
| 154 | SkPaint paint; |
| 155 | paint.setAntiAlias(true); |
| 156 | paint.setColor(0xDE000000); |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 157 | |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 158 | if (afterFirstFrame) { |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 159 | #if SK_SUPPORT_GPU |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 160 | GrContext* grContext = canvas->getGrContext(); |
| 161 | if (grContext) { |
| 162 | sk_sp<SkImage> image = |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 163 | grContext->contextPriv().getFontAtlasImage_ForTesting( |
| 164 | GrMaskFormat::kA8_GrMaskFormat, 0); |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 165 | canvas->drawImageRect(image, |
| 166 | SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0), &paint); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 167 | image = grContext->contextPriv().getFontAtlasImage_ForTesting( |
| 168 | GrMaskFormat::kA8_GrMaskFormat, 1); |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 169 | canvas->drawImageRect(image, |
| 170 | SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f), &paint); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 171 | image = grContext->contextPriv().getFontAtlasImage_ForTesting( |
| 172 | GrMaskFormat::kA8_GrMaskFormat, 2); |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 173 | canvas->drawImageRect(image, |
| 174 | SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f), &paint); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 175 | image = grContext->contextPriv().getFontAtlasImage_ForTesting( |
| 176 | GrMaskFormat::kA8_GrMaskFormat, 3); |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 177 | canvas->drawImageRect(image, |
| 178 | SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f), &paint); |
| 179 | } |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 180 | #endif |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 181 | } |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 182 | |
| 183 | canvas->scale(fScale, fScale); |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 184 | canvas->translate(0, fTranslate); |
| 185 | fTranslate -= 0.5f; |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 186 | |
| 187 | // draw a consistent run of the 'words' - one word per line |
| 188 | SkScalar y = 0; |
| 189 | for (int index = 0; index < kNumBlobs; ++index) { |
| 190 | y += -fMetrics.fAscent; |
| 191 | canvas->drawTextBlob(fBlobs[index], 0, y, paint); |
| 192 | |
| 193 | y += 3*(fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | static constexpr auto kNumBlobs = 8; |
| 199 | static constexpr auto kParagraphLength = 175; |
| 200 | |
| 201 | void init() { |
| 202 | fTypeface = chinese_typeface(); |
| 203 | |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 204 | SkFont font(fTypeface, 11); |
| 205 | font.getMetrics(&fMetrics); |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 206 | |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 207 | SkPaint paint; |
| 208 | paint.setColor(0xDE000000); |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 209 | |
| 210 | SkUnichar glyphs[45]; |
| 211 | for (int32_t i = 0; i < kNumBlobs; ++i) { |
| 212 | SkTextBlobBuilder builder; |
| 213 | auto paragraphLength = kParagraphLength; |
| 214 | SkScalar y = 0; |
| 215 | while (paragraphLength - 45 > 0) { |
| 216 | auto currentLineLength = SkTMin(45, paragraphLength - 45); |
| 217 | this->createRandomLine(glyphs, currentLineLength); |
| 218 | |
| 219 | sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs, |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 220 | currentLineLength*4, kUTF32_SkTextEncoding, |
| 221 | font, 0, y); |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 222 | y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading; |
| 223 | paragraphLength -= 45; |
| 224 | } |
| 225 | fBlobs.emplace_back(builder.make()); |
| 226 | } |
| 227 | |
| 228 | fIndex = 0; |
| 229 | } |
| 230 | |
| 231 | // Construct a random kWordLength character 'word' drawing from the full Chinese set |
| 232 | void createRandomLine(SkUnichar glyphs[45], int lineLength) { |
| 233 | for (auto i = 0; i < lineLength; ++i) { |
| 234 | glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | bool fInitialized = false; |
| 239 | sk_sp<SkTypeface> fTypeface; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 240 | SkFontMetrics fMetrics; |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 241 | SkTArray<sk_sp<SkTextBlob>> fBlobs; |
| 242 | SkRandom fRand; |
| 243 | SkScalar fScale; |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 244 | SkScalar fTranslate; |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 245 | int fIndex; |
| 246 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 247 | typedef Sample INHERITED; |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 248 | }; |
| 249 | |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 250 | ////////////////////////////////////////////////////////////////////////////// |
| 251 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 252 | DEF_SAMPLE( return new ChineseFlingView(); ) |
| 253 | DEF_SAMPLE( return new ChineseZoomView(); ) |