blob: 9f3321156e1bd82079f19fbe504fc70394f9c543 [file] [log] [blame]
Robert Phillips4c72b262017-08-15 13:28:42 -04001/*
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 Wagnerb2c4ea62018-08-08 11:36:17 -04009#include "Sample.h"
Robert Phillips4c72b262017-08-15 13:28:42 -040010#include "sk_tool_utils.h"
11
12#include "SkCanvas.h"
Mike Reed9cd2a5c2019-01-22 15:17:59 -050013#include "SkFontMetrics.h"
Robert Phillips4c72b262017-08-15 13:28:42 -040014#include "SkFontMgr.h"
15#include "SkRandom.h"
16#include "SkTypeface.h"
17#include "SkTextBlob.h"
18
Jim Van Verth87d18ce2018-01-22 12:45:47 -050019#if SK_SUPPORT_GPU
20#include "GrContext.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -050021#include "GrContextPriv.h"
Jim Van Verth87d18ce2018-01-22 12:45:47 -050022#endif
23
Robert Phillips4c72b262017-08-15 13:28:42 -040024static sk_sp<SkTypeface> chinese_typeface() {
25#ifdef SK_BUILD_FOR_ANDROID
Hal Canary53e5e7d2017-12-08 14:25:14 -050026 return MakeResourceAsTypeface("fonts/NotoSansCJK-Regular.ttc");
Mike Klein8f11d4d2018-01-24 12:42:55 -050027#elif defined(SK_BUILD_FOR_WIN)
Robert Phillips4c72b262017-08-15 13:28:42 -040028 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 Wagnerb2c4ea62018-08-08 11:36:17 -040040class ChineseFlingView : public Sample {
Robert Phillips4c72b262017-08-15 13:28:42 -040041public:
Jim Van Verthc3269ae2017-09-28 15:04:00 -040042 ChineseFlingView() : fBlobs(kNumBlobs) {}
Robert Phillips4c72b262017-08-15 13:28:42 -040043
44protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040045 bool onQuery(Sample::Event* evt) override {
46 if (Sample::TitleQ(*evt)) {
47 Sample::TitleR(evt, "chinese-fling");
Robert Phillips4c72b262017-08-15 13:28:42 -040048 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 Reed12a6d452018-12-21 22:22:31 -050062 paint.setColor(0xDE000000);
Robert Phillips4c72b262017-08-15 13:28:42 -040063
Jim Van Verthc3269ae2017-09-28 15:04:00 -040064 // draw a consistent run of the 'words' - one word per line
65 int index = fIndex;
Robert Phillips4c72b262017-08-15 13:28:42 -040066 for (SkScalar y = 0.0f; y < 1024.0f; ) {
Robert Phillips4c72b262017-08-15 13:28:42 -040067
68 y += -fMetrics.fAscent;
69 canvas->drawTextBlob(fBlobs[index], 0, y, paint);
70
71 y += fMetrics.fDescent + fMetrics.fLeading;
Jim Van Verthc3269ae2017-09-28 15:04:00 -040072 ++index;
73 index %= fBlobs.count();
Robert Phillips4c72b262017-08-15 13:28:42 -040074 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -040075 // now "fling" a random amount
76 fIndex += fRand.nextRangeU(5, 20);
77 fIndex %= fBlobs.count();
Robert Phillips4c72b262017-08-15 13:28:42 -040078 }
79
80private:
Jim Van Verthc3269ae2017-09-28 15:04:00 -040081 static constexpr auto kNumBlobs = 200;
82 static constexpr auto kWordLength = 16;
83
Robert Phillips4c72b262017-08-15 13:28:42 -040084 void init() {
85 fTypeface = chinese_typeface();
86
Mike Reed12a6d452018-12-21 22:22:31 -050087 SkFont font(fTypeface, 56);
88 font.getMetrics(&fMetrics);
Robert Phillips4c72b262017-08-15 13:28:42 -040089
Jim Van Verthc3269ae2017-09-28 15:04:00 -040090 SkUnichar glyphs[kWordLength];
91 for (int32_t i = 0; i < kNumBlobs; ++i) {
92 this->createRandomWord(glyphs);
Robert Phillips4c72b262017-08-15 13:28:42 -040093
94 SkTextBlobBuilder builder;
Jim Van Verthc3269ae2017-09-28 15:04:00 -040095 sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs, kWordLength*4,
Mike Reed12a6d452018-12-21 22:22:31 -050096 kUTF32_SkTextEncoding, font, 0, 0);
Robert Phillips4c72b262017-08-15 13:28:42 -040097
98 fBlobs.emplace_back(builder.make());
99 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400100
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 Phillips4c72b262017-08-15 13:28:42 -0400109 }
110
111 bool fInitialized = false;
112 sk_sp<SkTypeface> fTypeface;
Mike Reedb5784ac2018-11-12 09:35:15 -0500113 SkFontMetrics fMetrics;
Robert Phillips4c72b262017-08-15 13:28:42 -0400114 SkTArray<sk_sp<SkTextBlob>> fBlobs;
115 SkRandom fRand;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400116 int fIndex;
Robert Phillips4c72b262017-08-15 13:28:42 -0400117
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400118 typedef Sample INHERITED;
Robert Phillips4c72b262017-08-15 13:28:42 -0400119};
120
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400121class ChineseZoomView : public Sample {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500122public:
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500123 ChineseZoomView() : fBlobs(kNumBlobs), fScale(15.0f), fTranslate(0.0f) {}
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500124
125protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400126 bool onQuery(Sample::Event* evt) override {
127 if (Sample::TitleQ(*evt)) {
128 Sample::TitleR(evt, "chinese-zoom");
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500129 return true;
130 }
131 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400132 if (Sample::CharQ(*evt, &uni)) {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500133 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 Verthcad0acf2018-02-16 18:41:41 -0500146 bool afterFirstFrame = fInitialized;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500147 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 Verth87d18ce2018-01-22 12:45:47 -0500157
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500158 if (afterFirstFrame) {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500159#if SK_SUPPORT_GPU
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500160 GrContext* grContext = canvas->getGrContext();
161 if (grContext) {
162 sk_sp<SkImage> image =
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500163 grContext->contextPriv().getFontAtlasImage_ForTesting(
164 GrMaskFormat::kA8_GrMaskFormat, 0);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500165 canvas->drawImageRect(image,
166 SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0), &paint);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500167 image = grContext->contextPriv().getFontAtlasImage_ForTesting(
168 GrMaskFormat::kA8_GrMaskFormat, 1);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500169 canvas->drawImageRect(image,
170 SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f), &paint);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500171 image = grContext->contextPriv().getFontAtlasImage_ForTesting(
172 GrMaskFormat::kA8_GrMaskFormat, 2);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500173 canvas->drawImageRect(image,
174 SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f), &paint);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500175 image = grContext->contextPriv().getFontAtlasImage_ForTesting(
176 GrMaskFormat::kA8_GrMaskFormat, 3);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500177 canvas->drawImageRect(image,
178 SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f), &paint);
179 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500180#endif
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500181 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500182
183 canvas->scale(fScale, fScale);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500184 canvas->translate(0, fTranslate);
185 fTranslate -= 0.5f;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500186
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
197private:
198 static constexpr auto kNumBlobs = 8;
199 static constexpr auto kParagraphLength = 175;
200
201 void init() {
202 fTypeface = chinese_typeface();
203
Mike Reed12a6d452018-12-21 22:22:31 -0500204 SkFont font(fTypeface, 11);
205 font.getMetrics(&fMetrics);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500206
Mike Reed12a6d452018-12-21 22:22:31 -0500207 SkPaint paint;
208 paint.setColor(0xDE000000);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500209
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 Reed12a6d452018-12-21 22:22:31 -0500220 currentLineLength*4, kUTF32_SkTextEncoding,
221 font, 0, y);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500222 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 Reedb5784ac2018-11-12 09:35:15 -0500240 SkFontMetrics fMetrics;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500241 SkTArray<sk_sp<SkTextBlob>> fBlobs;
242 SkRandom fRand;
243 SkScalar fScale;
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500244 SkScalar fTranslate;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500245 int fIndex;
246
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400247 typedef Sample INHERITED;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500248};
249
Robert Phillips4c72b262017-08-15 13:28:42 -0400250//////////////////////////////////////////////////////////////////////////////
251
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400252DEF_SAMPLE( return new ChineseFlingView(); )
253DEF_SAMPLE( return new ChineseZoomView(); )