blob: b250da4f78a54428a883aa919be7ec2760431391 [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:
Hal Canary8a027312019-07-03 10:55:44 -040045 SkString name() override { return SkString("chinese-fling"); }
Robert Phillips4c72b262017-08-15 13:28:42 -040046
47 void onDrawContent(SkCanvas* canvas) override {
48 if (!fInitialized) {
49 this->init();
50 fInitialized = true;
51 }
52
53 canvas->clear(0xFFDDDDDD);
54
55 SkPaint paint;
Mike Reed12a6d452018-12-21 22:22:31 -050056 paint.setColor(0xDE000000);
Robert Phillips4c72b262017-08-15 13:28:42 -040057
Jim Van Verthc3269ae2017-09-28 15:04:00 -040058 // draw a consistent run of the 'words' - one word per line
59 int index = fIndex;
Robert Phillips4c72b262017-08-15 13:28:42 -040060 for (SkScalar y = 0.0f; y < 1024.0f; ) {
Robert Phillips4c72b262017-08-15 13:28:42 -040061
62 y += -fMetrics.fAscent;
63 canvas->drawTextBlob(fBlobs[index], 0, y, paint);
64
65 y += fMetrics.fDescent + fMetrics.fLeading;
Jim Van Verthc3269ae2017-09-28 15:04:00 -040066 ++index;
67 index %= fBlobs.count();
Robert Phillips4c72b262017-08-15 13:28:42 -040068 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -040069 // now "fling" a random amount
70 fIndex += fRand.nextRangeU(5, 20);
71 fIndex %= fBlobs.count();
Robert Phillips4c72b262017-08-15 13:28:42 -040072 }
73
74private:
Jim Van Verthc3269ae2017-09-28 15:04:00 -040075 static constexpr auto kNumBlobs = 200;
76 static constexpr auto kWordLength = 16;
77
Robert Phillips4c72b262017-08-15 13:28:42 -040078 void init() {
79 fTypeface = chinese_typeface();
80
Mike Reed12a6d452018-12-21 22:22:31 -050081 SkFont font(fTypeface, 56);
82 font.getMetrics(&fMetrics);
Robert Phillips4c72b262017-08-15 13:28:42 -040083
Jim Van Verthc3269ae2017-09-28 15:04:00 -040084 SkUnichar glyphs[kWordLength];
85 for (int32_t i = 0; i < kNumBlobs; ++i) {
86 this->createRandomWord(glyphs);
Robert Phillips4c72b262017-08-15 13:28:42 -040087
88 SkTextBlobBuilder builder;
Mike Kleinea3f0142019-03-20 11:12:10 -050089 ToolUtils::add_to_text_blob_w_len(&builder,
90 (const char*)glyphs,
91 kWordLength * 4,
Ben Wagner51e15a62019-05-07 15:38:46 -040092 SkTextEncoding::kUTF32,
Mike Kleinea3f0142019-03-20 11:12:10 -050093 font,
94 0,
95 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:
Hal Canary8a027312019-07-03 10:55:44 -0400125 SkString name() override { return SkString("chinese-zoom"); }
126
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400127 bool onQuery(Sample::Event* evt) override {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500128 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400129 if (Sample::CharQ(*evt, &uni)) {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500130 if ('>' == uni) {
131 fScale += 0.125f;
132 return true;
133 }
134 if ('<' == uni) {
135 fScale -= 0.125f;
136 return true;
137 }
138 }
139 return this->INHERITED::onQuery(evt);
140 }
141
142 void onDrawContent(SkCanvas* canvas) override {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500143 bool afterFirstFrame = fInitialized;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500144 if (!fInitialized) {
145 this->init();
146 fInitialized = true;
147 }
148
149 canvas->clear(0xFFDDDDDD);
150
151 SkPaint paint;
152 paint.setAntiAlias(true);
153 paint.setColor(0xDE000000);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500154
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500155 if (afterFirstFrame) {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500156#if SK_SUPPORT_GPU
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500157 GrContext* grContext = canvas->getGrContext();
158 if (grContext) {
159 sk_sp<SkImage> image =
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500160 grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500161 GrMaskFormat::kA8_GrMaskFormat, 0);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500162 canvas->drawImageRect(image,
163 SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0), &paint);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500164 image = grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500165 GrMaskFormat::kA8_GrMaskFormat, 1);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500166 canvas->drawImageRect(image,
167 SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f), &paint);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500168 image = grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500169 GrMaskFormat::kA8_GrMaskFormat, 2);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500170 canvas->drawImageRect(image,
171 SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f), &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, 3);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500174 canvas->drawImageRect(image,
175 SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f), &paint);
176 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500177#endif
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500178 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500179
180 canvas->scale(fScale, fScale);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500181 canvas->translate(0, fTranslate);
182 fTranslate -= 0.5f;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500183
184 // draw a consistent run of the 'words' - one word per line
185 SkScalar y = 0;
186 for (int index = 0; index < kNumBlobs; ++index) {
187 y += -fMetrics.fAscent;
188 canvas->drawTextBlob(fBlobs[index], 0, y, paint);
189
190 y += 3*(fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading);
191 }
192 }
193
194private:
195 static constexpr auto kNumBlobs = 8;
196 static constexpr auto kParagraphLength = 175;
197
198 void init() {
199 fTypeface = chinese_typeface();
200
Mike Reed12a6d452018-12-21 22:22:31 -0500201 SkFont font(fTypeface, 11);
202 font.getMetrics(&fMetrics);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500203
Mike Reed12a6d452018-12-21 22:22:31 -0500204 SkPaint paint;
205 paint.setColor(0xDE000000);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500206
207 SkUnichar glyphs[45];
208 for (int32_t i = 0; i < kNumBlobs; ++i) {
209 SkTextBlobBuilder builder;
210 auto paragraphLength = kParagraphLength;
211 SkScalar y = 0;
212 while (paragraphLength - 45 > 0) {
213 auto currentLineLength = SkTMin(45, paragraphLength - 45);
214 this->createRandomLine(glyphs, currentLineLength);
215
Mike Kleinea3f0142019-03-20 11:12:10 -0500216 ToolUtils::add_to_text_blob_w_len(&builder,
217 (const char*)glyphs,
218 currentLineLength * 4,
Ben Wagner51e15a62019-05-07 15:38:46 -0400219 SkTextEncoding::kUTF32,
Mike Kleinea3f0142019-03-20 11:12:10 -0500220 font,
221 0,
222 y);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500223 y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading;
224 paragraphLength -= 45;
225 }
226 fBlobs.emplace_back(builder.make());
227 }
228
229 fIndex = 0;
230 }
231
232 // Construct a random kWordLength character 'word' drawing from the full Chinese set
233 void createRandomLine(SkUnichar glyphs[45], int lineLength) {
234 for (auto i = 0; i < lineLength; ++i) {
235 glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
236 }
237 }
238
239 bool fInitialized = false;
240 sk_sp<SkTypeface> fTypeface;
Mike Reedb5784ac2018-11-12 09:35:15 -0500241 SkFontMetrics fMetrics;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500242 SkTArray<sk_sp<SkTextBlob>> fBlobs;
243 SkRandom fRand;
244 SkScalar fScale;
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500245 SkScalar fTranslate;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500246 int fIndex;
247
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400248 typedef Sample INHERITED;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500249};
250
Robert Phillips4c72b262017-08-15 13:28:42 -0400251//////////////////////////////////////////////////////////////////////////////
252
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400253DEF_SAMPLE( return new ChineseFlingView(); )
254DEF_SAMPLE( return new ChineseZoomView(); )