blob: a556bd6a5452afefdceecf5cc67bf4f0969be454 [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);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500156
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500157 if (afterFirstFrame) {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500158#if SK_SUPPORT_GPU
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500159 GrContext* grContext = canvas->getGrContext();
160 if (grContext) {
161 sk_sp<SkImage> image =
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500162 grContext->contextPriv().getFontAtlasImage_ForTesting(
163 GrMaskFormat::kA8_GrMaskFormat, 0);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500164 canvas->drawImageRect(image,
165 SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0), &paint);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500166 image = grContext->contextPriv().getFontAtlasImage_ForTesting(
167 GrMaskFormat::kA8_GrMaskFormat, 1);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500168 canvas->drawImageRect(image,
169 SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f), &paint);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500170 image = grContext->contextPriv().getFontAtlasImage_ForTesting(
171 GrMaskFormat::kA8_GrMaskFormat, 2);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500172 canvas->drawImageRect(image,
173 SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f), &paint);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500174 image = grContext->contextPriv().getFontAtlasImage_ForTesting(
175 GrMaskFormat::kA8_GrMaskFormat, 3);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500176 canvas->drawImageRect(image,
177 SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f), &paint);
178 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500179#endif
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500180 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500181
182 canvas->scale(fScale, fScale);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500183 canvas->translate(0, fTranslate);
184 fTranslate -= 0.5f;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500185
186 // draw a consistent run of the 'words' - one word per line
187 SkScalar y = 0;
188 for (int index = 0; index < kNumBlobs; ++index) {
189 y += -fMetrics.fAscent;
190 canvas->drawTextBlob(fBlobs[index], 0, y, paint);
191
192 y += 3*(fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading);
193 }
194 }
195
196private:
197 static constexpr auto kNumBlobs = 8;
198 static constexpr auto kParagraphLength = 175;
199
200 void init() {
201 fTypeface = chinese_typeface();
202
Mike Reed12a6d452018-12-21 22:22:31 -0500203 SkFont font(fTypeface, 11);
204 font.getMetrics(&fMetrics);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500205
Mike Reed12a6d452018-12-21 22:22:31 -0500206 SkPaint paint;
207 paint.setColor(0xDE000000);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500208
209 SkUnichar glyphs[45];
210 for (int32_t i = 0; i < kNumBlobs; ++i) {
211 SkTextBlobBuilder builder;
212 auto paragraphLength = kParagraphLength;
213 SkScalar y = 0;
214 while (paragraphLength - 45 > 0) {
215 auto currentLineLength = SkTMin(45, paragraphLength - 45);
216 this->createRandomLine(glyphs, currentLineLength);
217
218 sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs,
Mike Reed12a6d452018-12-21 22:22:31 -0500219 currentLineLength*4, kUTF32_SkTextEncoding,
220 font, 0, y);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500221 y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading;
222 paragraphLength -= 45;
223 }
224 fBlobs.emplace_back(builder.make());
225 }
226
227 fIndex = 0;
228 }
229
230 // Construct a random kWordLength character 'word' drawing from the full Chinese set
231 void createRandomLine(SkUnichar glyphs[45], int lineLength) {
232 for (auto i = 0; i < lineLength; ++i) {
233 glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
234 }
235 }
236
237 bool fInitialized = false;
238 sk_sp<SkTypeface> fTypeface;
Mike Reedb5784ac2018-11-12 09:35:15 -0500239 SkFontMetrics fMetrics;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500240 SkTArray<sk_sp<SkTextBlob>> fBlobs;
241 SkRandom fRand;
242 SkScalar fScale;
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500243 SkScalar fTranslate;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500244 int fIndex;
245
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400246 typedef Sample INHERITED;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500247};
248
Robert Phillips4c72b262017-08-15 13:28:42 -0400249//////////////////////////////////////////////////////////////////////////////
250
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400251DEF_SAMPLE( return new ChineseFlingView(); )
252DEF_SAMPLE( return new ChineseZoomView(); )