blob: 3b73bb213d193cb72df8f79204d6048bb370067f [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"
13#include "SkFontMgr.h"
14#include "SkRandom.h"
15#include "SkTypeface.h"
16#include "SkTextBlob.h"
17
Jim Van Verth87d18ce2018-01-22 12:45:47 -050018#if SK_SUPPORT_GPU
19#include "GrContext.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -050020#include "GrContextPriv.h"
Jim Van Verth87d18ce2018-01-22 12:45:47 -050021#endif
22
Robert Phillips4c72b262017-08-15 13:28:42 -040023static sk_sp<SkTypeface> chinese_typeface() {
24#ifdef SK_BUILD_FOR_ANDROID
Hal Canary53e5e7d2017-12-08 14:25:14 -050025 return MakeResourceAsTypeface("fonts/NotoSansCJK-Regular.ttc");
Mike Klein8f11d4d2018-01-24 12:42:55 -050026#elif defined(SK_BUILD_FOR_WIN)
Robert Phillips4c72b262017-08-15 13:28:42 -040027 return SkTypeface::MakeFromName("SimSun", SkFontStyle());
28#elif defined(SK_BUILD_FOR_MAC)
29 return SkTypeface::MakeFromName("Hiragino Sans GB W3", SkFontStyle());
30#elif defined(SK_BUILD_FOR_IOS)
31 return SkTypeface::MakeFromName("Hiragino Sans GB W3", SkFontStyle());
32#elif defined(SK_BUILD_FOR_UNIX)
33 return SkTypeface::MakeFromName("Noto Sans CJK SC", SkFontStyle());
34#else
35 return nullptr;
36#endif
37}
38
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040039class ChineseFlingView : public Sample {
Robert Phillips4c72b262017-08-15 13:28:42 -040040public:
Jim Van Verthc3269ae2017-09-28 15:04:00 -040041 ChineseFlingView() : fBlobs(kNumBlobs) {}
Robert Phillips4c72b262017-08-15 13:28:42 -040042
43protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040044 bool onQuery(Sample::Event* evt) override {
45 if (Sample::TitleQ(*evt)) {
46 Sample::TitleR(evt, "chinese-fling");
Robert Phillips4c72b262017-08-15 13:28:42 -040047 return true;
48 }
49 return this->INHERITED::onQuery(evt);
50 }
51
52 void onDrawContent(SkCanvas* canvas) override {
53 if (!fInitialized) {
54 this->init();
55 fInitialized = true;
56 }
57
58 canvas->clear(0xFFDDDDDD);
59
60 SkPaint paint;
Mike Reed12a6d452018-12-21 22:22:31 -050061 paint.setColor(0xDE000000);
Robert Phillips4c72b262017-08-15 13:28:42 -040062
Jim Van Verthc3269ae2017-09-28 15:04:00 -040063 // draw a consistent run of the 'words' - one word per line
64 int index = fIndex;
Robert Phillips4c72b262017-08-15 13:28:42 -040065 for (SkScalar y = 0.0f; y < 1024.0f; ) {
Robert Phillips4c72b262017-08-15 13:28:42 -040066
67 y += -fMetrics.fAscent;
68 canvas->drawTextBlob(fBlobs[index], 0, y, paint);
69
70 y += fMetrics.fDescent + fMetrics.fLeading;
Jim Van Verthc3269ae2017-09-28 15:04:00 -040071 ++index;
72 index %= fBlobs.count();
Robert Phillips4c72b262017-08-15 13:28:42 -040073 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -040074 // now "fling" a random amount
75 fIndex += fRand.nextRangeU(5, 20);
76 fIndex %= fBlobs.count();
Robert Phillips4c72b262017-08-15 13:28:42 -040077 }
78
79private:
Jim Van Verthc3269ae2017-09-28 15:04:00 -040080 static constexpr auto kNumBlobs = 200;
81 static constexpr auto kWordLength = 16;
82
Robert Phillips4c72b262017-08-15 13:28:42 -040083 void init() {
84 fTypeface = chinese_typeface();
85
Mike Reed12a6d452018-12-21 22:22:31 -050086 SkFont font(fTypeface, 56);
87 font.getMetrics(&fMetrics);
Robert Phillips4c72b262017-08-15 13:28:42 -040088
Jim Van Verthc3269ae2017-09-28 15:04:00 -040089 SkUnichar glyphs[kWordLength];
90 for (int32_t i = 0; i < kNumBlobs; ++i) {
91 this->createRandomWord(glyphs);
Robert Phillips4c72b262017-08-15 13:28:42 -040092
93 SkTextBlobBuilder builder;
Jim Van Verthc3269ae2017-09-28 15:04:00 -040094 sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs, kWordLength*4,
Mike Reed12a6d452018-12-21 22:22:31 -050095 kUTF32_SkTextEncoding, font, 0, 0);
Robert Phillips4c72b262017-08-15 13:28:42 -040096
97 fBlobs.emplace_back(builder.make());
98 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -040099
100 fIndex = 0;
101 }
102
103 // Construct a random kWordLength character 'word' drawing from the full Chinese set
104 void createRandomWord(SkUnichar glyphs[kWordLength]) {
105 for (int i = 0; i < kWordLength; ++i) {
106 glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
107 }
Robert Phillips4c72b262017-08-15 13:28:42 -0400108 }
109
110 bool fInitialized = false;
111 sk_sp<SkTypeface> fTypeface;
Mike Reedb5784ac2018-11-12 09:35:15 -0500112 SkFontMetrics fMetrics;
Robert Phillips4c72b262017-08-15 13:28:42 -0400113 SkTArray<sk_sp<SkTextBlob>> fBlobs;
114 SkRandom fRand;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400115 int fIndex;
Robert Phillips4c72b262017-08-15 13:28:42 -0400116
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400117 typedef Sample INHERITED;
Robert Phillips4c72b262017-08-15 13:28:42 -0400118};
119
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400120class ChineseZoomView : public Sample {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500121public:
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500122 ChineseZoomView() : fBlobs(kNumBlobs), fScale(15.0f), fTranslate(0.0f) {}
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500123
124protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400125 bool onQuery(Sample::Event* evt) override {
126 if (Sample::TitleQ(*evt)) {
127 Sample::TitleR(evt, "chinese-zoom");
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500128 return true;
129 }
130 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400131 if (Sample::CharQ(*evt, &uni)) {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500132 if ('>' == uni) {
133 fScale += 0.125f;
134 return true;
135 }
136 if ('<' == uni) {
137 fScale -= 0.125f;
138 return true;
139 }
140 }
141 return this->INHERITED::onQuery(evt);
142 }
143
144 void onDrawContent(SkCanvas* canvas) override {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500145 bool afterFirstFrame = fInitialized;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500146 if (!fInitialized) {
147 this->init();
148 fInitialized = true;
149 }
150
151 canvas->clear(0xFFDDDDDD);
152
153 SkPaint paint;
154 paint.setAntiAlias(true);
155 paint.setColor(0xDE000000);
156 paint.setTypeface(fTypeface);
157 paint.setTextSize(11);
Mike Reed97f3cc22018-12-03 09:45:17 -0500158 paint.setTextEncoding(kUTF32_SkTextEncoding);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500159
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500160 if (afterFirstFrame) {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500161#if SK_SUPPORT_GPU
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500162 GrContext* grContext = canvas->getGrContext();
163 if (grContext) {
164 sk_sp<SkImage> image =
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500165 grContext->contextPriv().getFontAtlasImage_ForTesting(
166 GrMaskFormat::kA8_GrMaskFormat, 0);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500167 canvas->drawImageRect(image,
168 SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0), &paint);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500169 image = grContext->contextPriv().getFontAtlasImage_ForTesting(
170 GrMaskFormat::kA8_GrMaskFormat, 1);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500171 canvas->drawImageRect(image,
172 SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f), &paint);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500173 image = grContext->contextPriv().getFontAtlasImage_ForTesting(
174 GrMaskFormat::kA8_GrMaskFormat, 2);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500175 canvas->drawImageRect(image,
176 SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f), &paint);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500177 image = grContext->contextPriv().getFontAtlasImage_ForTesting(
178 GrMaskFormat::kA8_GrMaskFormat, 3);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500179 canvas->drawImageRect(image,
180 SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f), &paint);
181 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500182#endif
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500183 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500184
185 canvas->scale(fScale, fScale);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500186 canvas->translate(0, fTranslate);
187 fTranslate -= 0.5f;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500188
189 // draw a consistent run of the 'words' - one word per line
190 SkScalar y = 0;
191 for (int index = 0; index < kNumBlobs; ++index) {
192 y += -fMetrics.fAscent;
193 canvas->drawTextBlob(fBlobs[index], 0, y, paint);
194
195 y += 3*(fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading);
196 }
197 }
198
199private:
200 static constexpr auto kNumBlobs = 8;
201 static constexpr auto kParagraphLength = 175;
202
203 void init() {
204 fTypeface = chinese_typeface();
205
Mike Reed12a6d452018-12-21 22:22:31 -0500206 SkFont font(fTypeface, 11);
207 font.getMetrics(&fMetrics);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500208
Mike Reed12a6d452018-12-21 22:22:31 -0500209 SkPaint paint;
210 paint.setColor(0xDE000000);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500211
212 SkUnichar glyphs[45];
213 for (int32_t i = 0; i < kNumBlobs; ++i) {
214 SkTextBlobBuilder builder;
215 auto paragraphLength = kParagraphLength;
216 SkScalar y = 0;
217 while (paragraphLength - 45 > 0) {
218 auto currentLineLength = SkTMin(45, paragraphLength - 45);
219 this->createRandomLine(glyphs, currentLineLength);
220
221 sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs,
Mike Reed12a6d452018-12-21 22:22:31 -0500222 currentLineLength*4, kUTF32_SkTextEncoding,
223 font, 0, y);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500224 y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading;
225 paragraphLength -= 45;
226 }
227 fBlobs.emplace_back(builder.make());
228 }
229
230 fIndex = 0;
231 }
232
233 // Construct a random kWordLength character 'word' drawing from the full Chinese set
234 void createRandomLine(SkUnichar glyphs[45], int lineLength) {
235 for (auto i = 0; i < lineLength; ++i) {
236 glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
237 }
238 }
239
240 bool fInitialized = false;
241 sk_sp<SkTypeface> fTypeface;
Mike Reedb5784ac2018-11-12 09:35:15 -0500242 SkFontMetrics fMetrics;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500243 SkTArray<sk_sp<SkTextBlob>> fBlobs;
244 SkRandom fRand;
245 SkScalar fScale;
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500246 SkScalar fTranslate;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500247 int fIndex;
248
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400249 typedef Sample INHERITED;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500250};
251
Robert Phillips4c72b262017-08-15 13:28:42 -0400252//////////////////////////////////////////////////////////////////////////////
253
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400254DEF_SAMPLE( return new ChineseFlingView(); )
255DEF_SAMPLE( return new ChineseZoomView(); )