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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "samplecode/Sample.h" |
| 9 | #include "tools/Resources.h" |
| 10 | #include "tools/ToolUtils.h" |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkCanvas.h" |
| 13 | #include "include/core/SkFontMetrics.h" |
| 14 | #include "include/core/SkFontMgr.h" |
| 15 | #include "include/core/SkTextBlob.h" |
| 16 | #include "include/core/SkTypeface.h" |
| 17 | #include "include/utils/SkRandom.h" |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 18 | |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 19 | #if SK_SUPPORT_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/gpu/GrContext.h" |
| 21 | #include "src/gpu/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; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 95 | ToolUtils::add_to_text_blob_w_len(&builder, |
| 96 | (const char*)glyphs, |
| 97 | kWordLength * 4, |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame^] | 98 | SkTextEncoding::kUTF32, |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 99 | font, |
| 100 | 0, |
| 101 | 0); |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 102 | |
| 103 | fBlobs.emplace_back(builder.make()); |
| 104 | } |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 105 | |
| 106 | fIndex = 0; |
| 107 | } |
| 108 | |
| 109 | // Construct a random kWordLength character 'word' drawing from the full Chinese set |
| 110 | void createRandomWord(SkUnichar glyphs[kWordLength]) { |
| 111 | for (int i = 0; i < kWordLength; ++i) { |
| 112 | glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0); |
| 113 | } |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool fInitialized = false; |
| 117 | sk_sp<SkTypeface> fTypeface; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 118 | SkFontMetrics fMetrics; |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 119 | SkTArray<sk_sp<SkTextBlob>> fBlobs; |
| 120 | SkRandom fRand; |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 121 | int fIndex; |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 122 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 123 | typedef Sample INHERITED; |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 124 | }; |
| 125 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 126 | class ChineseZoomView : public Sample { |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 127 | public: |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 128 | ChineseZoomView() : fBlobs(kNumBlobs), fScale(15.0f), fTranslate(0.0f) {} |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 129 | |
| 130 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 131 | bool onQuery(Sample::Event* evt) override { |
| 132 | if (Sample::TitleQ(*evt)) { |
| 133 | Sample::TitleR(evt, "chinese-zoom"); |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | SkUnichar uni; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 137 | if (Sample::CharQ(*evt, &uni)) { |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 138 | if ('>' == uni) { |
| 139 | fScale += 0.125f; |
| 140 | return true; |
| 141 | } |
| 142 | if ('<' == uni) { |
| 143 | fScale -= 0.125f; |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | return this->INHERITED::onQuery(evt); |
| 148 | } |
| 149 | |
| 150 | void onDrawContent(SkCanvas* canvas) override { |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 151 | bool afterFirstFrame = fInitialized; |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 152 | if (!fInitialized) { |
| 153 | this->init(); |
| 154 | fInitialized = true; |
| 155 | } |
| 156 | |
| 157 | canvas->clear(0xFFDDDDDD); |
| 158 | |
| 159 | SkPaint paint; |
| 160 | paint.setAntiAlias(true); |
| 161 | paint.setColor(0xDE000000); |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 162 | |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 163 | if (afterFirstFrame) { |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 164 | #if SK_SUPPORT_GPU |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 165 | GrContext* grContext = canvas->getGrContext(); |
| 166 | if (grContext) { |
| 167 | sk_sp<SkImage> image = |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 168 | grContext->priv().testingOnly_getFontAtlasImage( |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 169 | GrMaskFormat::kA8_GrMaskFormat, 0); |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 170 | canvas->drawImageRect(image, |
| 171 | SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0), &paint); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 172 | image = grContext->priv().testingOnly_getFontAtlasImage( |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 173 | GrMaskFormat::kA8_GrMaskFormat, 1); |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 174 | canvas->drawImageRect(image, |
| 175 | SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f), &paint); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 176 | image = grContext->priv().testingOnly_getFontAtlasImage( |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 177 | GrMaskFormat::kA8_GrMaskFormat, 2); |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 178 | canvas->drawImageRect(image, |
| 179 | SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f), &paint); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 180 | image = grContext->priv().testingOnly_getFontAtlasImage( |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 181 | GrMaskFormat::kA8_GrMaskFormat, 3); |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 182 | canvas->drawImageRect(image, |
| 183 | SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f), &paint); |
| 184 | } |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 185 | #endif |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 186 | } |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 187 | |
| 188 | canvas->scale(fScale, fScale); |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 189 | canvas->translate(0, fTranslate); |
| 190 | fTranslate -= 0.5f; |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 191 | |
| 192 | // draw a consistent run of the 'words' - one word per line |
| 193 | SkScalar y = 0; |
| 194 | for (int index = 0; index < kNumBlobs; ++index) { |
| 195 | y += -fMetrics.fAscent; |
| 196 | canvas->drawTextBlob(fBlobs[index], 0, y, paint); |
| 197 | |
| 198 | y += 3*(fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | private: |
| 203 | static constexpr auto kNumBlobs = 8; |
| 204 | static constexpr auto kParagraphLength = 175; |
| 205 | |
| 206 | void init() { |
| 207 | fTypeface = chinese_typeface(); |
| 208 | |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 209 | SkFont font(fTypeface, 11); |
| 210 | font.getMetrics(&fMetrics); |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 211 | |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 212 | SkPaint paint; |
| 213 | paint.setColor(0xDE000000); |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 214 | |
| 215 | SkUnichar glyphs[45]; |
| 216 | for (int32_t i = 0; i < kNumBlobs; ++i) { |
| 217 | SkTextBlobBuilder builder; |
| 218 | auto paragraphLength = kParagraphLength; |
| 219 | SkScalar y = 0; |
| 220 | while (paragraphLength - 45 > 0) { |
| 221 | auto currentLineLength = SkTMin(45, paragraphLength - 45); |
| 222 | this->createRandomLine(glyphs, currentLineLength); |
| 223 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 224 | ToolUtils::add_to_text_blob_w_len(&builder, |
| 225 | (const char*)glyphs, |
| 226 | currentLineLength * 4, |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame^] | 227 | SkTextEncoding::kUTF32, |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 228 | font, |
| 229 | 0, |
| 230 | y); |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 231 | y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading; |
| 232 | paragraphLength -= 45; |
| 233 | } |
| 234 | fBlobs.emplace_back(builder.make()); |
| 235 | } |
| 236 | |
| 237 | fIndex = 0; |
| 238 | } |
| 239 | |
| 240 | // Construct a random kWordLength character 'word' drawing from the full Chinese set |
| 241 | void createRandomLine(SkUnichar glyphs[45], int lineLength) { |
| 242 | for (auto i = 0; i < lineLength; ++i) { |
| 243 | glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | bool fInitialized = false; |
| 248 | sk_sp<SkTypeface> fTypeface; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 249 | SkFontMetrics fMetrics; |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 250 | SkTArray<sk_sp<SkTextBlob>> fBlobs; |
| 251 | SkRandom fRand; |
| 252 | SkScalar fScale; |
Jim Van Verth | cad0acf | 2018-02-16 18:41:41 -0500 | [diff] [blame] | 253 | SkScalar fTranslate; |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 254 | int fIndex; |
| 255 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 256 | typedef Sample INHERITED; |
Jim Van Verth | 87d18ce | 2018-01-22 12:45:47 -0500 | [diff] [blame] | 257 | }; |
| 258 | |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 259 | ////////////////////////////////////////////////////////////////////////////// |
| 260 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 261 | DEF_SAMPLE( return new ChineseFlingView(); ) |
| 262 | DEF_SAMPLE( return new ChineseZoomView(); ) |