blob: e9631f26a89d593f810607443fb6fee8afa926ae [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "samplecode/Sample.h"
9#include "tools/Resources.h"
10#include "tools/ToolUtils.h"
Robert Phillips4c72b262017-08-15 13:28:42 -040011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#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 Phillips4c72b262017-08-15 13:28:42 -040018
Jim Van Verth87d18ce2018-01-22 12:45:47 -050019#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/gpu/GrContext.h"
21#include "src/gpu/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;
Mike Kleinea3f0142019-03-20 11:12:10 -050095 ToolUtils::add_to_text_blob_w_len(&builder,
96 (const char*)glyphs,
97 kWordLength * 4,
Ben Wagner51e15a62019-05-07 15:38:46 -040098 SkTextEncoding::kUTF32,
Mike Kleinea3f0142019-03-20 11:12:10 -050099 font,
100 0,
101 0);
Robert Phillips4c72b262017-08-15 13:28:42 -0400102
103 fBlobs.emplace_back(builder.make());
104 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400105
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 Phillips4c72b262017-08-15 13:28:42 -0400114 }
115
116 bool fInitialized = false;
117 sk_sp<SkTypeface> fTypeface;
Mike Reedb5784ac2018-11-12 09:35:15 -0500118 SkFontMetrics fMetrics;
Robert Phillips4c72b262017-08-15 13:28:42 -0400119 SkTArray<sk_sp<SkTextBlob>> fBlobs;
120 SkRandom fRand;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400121 int fIndex;
Robert Phillips4c72b262017-08-15 13:28:42 -0400122
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400123 typedef Sample INHERITED;
Robert Phillips4c72b262017-08-15 13:28:42 -0400124};
125
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400126class ChineseZoomView : public Sample {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500127public:
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500128 ChineseZoomView() : fBlobs(kNumBlobs), fScale(15.0f), fTranslate(0.0f) {}
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500129
130protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400131 bool onQuery(Sample::Event* evt) override {
132 if (Sample::TitleQ(*evt)) {
133 Sample::TitleR(evt, "chinese-zoom");
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500134 return true;
135 }
136 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400137 if (Sample::CharQ(*evt, &uni)) {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500138 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 Verthcad0acf2018-02-16 18:41:41 -0500151 bool afterFirstFrame = fInitialized;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500152 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 Verth87d18ce2018-01-22 12:45:47 -0500162
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500163 if (afterFirstFrame) {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500164#if SK_SUPPORT_GPU
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500165 GrContext* grContext = canvas->getGrContext();
166 if (grContext) {
167 sk_sp<SkImage> image =
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500168 grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500169 GrMaskFormat::kA8_GrMaskFormat, 0);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500170 canvas->drawImageRect(image,
171 SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0), &paint);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500172 image = grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500173 GrMaskFormat::kA8_GrMaskFormat, 1);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500174 canvas->drawImageRect(image,
175 SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f), &paint);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500176 image = grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500177 GrMaskFormat::kA8_GrMaskFormat, 2);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500178 canvas->drawImageRect(image,
179 SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f), &paint);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500180 image = grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500181 GrMaskFormat::kA8_GrMaskFormat, 3);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500182 canvas->drawImageRect(image,
183 SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f), &paint);
184 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500185#endif
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500186 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500187
188 canvas->scale(fScale, fScale);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500189 canvas->translate(0, fTranslate);
190 fTranslate -= 0.5f;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500191
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
202private:
203 static constexpr auto kNumBlobs = 8;
204 static constexpr auto kParagraphLength = 175;
205
206 void init() {
207 fTypeface = chinese_typeface();
208
Mike Reed12a6d452018-12-21 22:22:31 -0500209 SkFont font(fTypeface, 11);
210 font.getMetrics(&fMetrics);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500211
Mike Reed12a6d452018-12-21 22:22:31 -0500212 SkPaint paint;
213 paint.setColor(0xDE000000);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500214
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 Kleinea3f0142019-03-20 11:12:10 -0500224 ToolUtils::add_to_text_blob_w_len(&builder,
225 (const char*)glyphs,
226 currentLineLength * 4,
Ben Wagner51e15a62019-05-07 15:38:46 -0400227 SkTextEncoding::kUTF32,
Mike Kleinea3f0142019-03-20 11:12:10 -0500228 font,
229 0,
230 y);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500231 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 Reedb5784ac2018-11-12 09:35:15 -0500249 SkFontMetrics fMetrics;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500250 SkTArray<sk_sp<SkTextBlob>> fBlobs;
251 SkRandom fRand;
252 SkScalar fScale;
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500253 SkScalar fTranslate;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500254 int fIndex;
255
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400256 typedef Sample INHERITED;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500257};
258
Robert Phillips4c72b262017-08-15 13:28:42 -0400259//////////////////////////////////////////////////////////////////////////////
260
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400261DEF_SAMPLE( return new ChineseFlingView(); )
262DEF_SAMPLE( return new ChineseZoomView(); )