blob: bb749e52dcf31db306353cb211f8a55fa73877ae [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
Hal Canary6cc65e12019-07-03 15:53:04 -0400127 bool onChar(SkUnichar uni) override {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500128 if ('>' == uni) {
129 fScale += 0.125f;
130 return true;
131 }
132 if ('<' == uni) {
133 fScale -= 0.125f;
134 return true;
135 }
Hal Canary6cc65e12019-07-03 15:53:04 -0400136 return false;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500137 }
138
139 void onDrawContent(SkCanvas* canvas) override {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500140 bool afterFirstFrame = fInitialized;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500141 if (!fInitialized) {
142 this->init();
143 fInitialized = true;
144 }
145
146 canvas->clear(0xFFDDDDDD);
147
148 SkPaint paint;
149 paint.setAntiAlias(true);
150 paint.setColor(0xDE000000);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500151
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500152 if (afterFirstFrame) {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500153#if SK_SUPPORT_GPU
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500154 GrContext* grContext = canvas->getGrContext();
155 if (grContext) {
156 sk_sp<SkImage> image =
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500157 grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500158 GrMaskFormat::kA8_GrMaskFormat, 0);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500159 canvas->drawImageRect(image,
160 SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0), &paint);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500161 image = grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500162 GrMaskFormat::kA8_GrMaskFormat, 1);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500163 canvas->drawImageRect(image,
164 SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f), &paint);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500165 image = grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500166 GrMaskFormat::kA8_GrMaskFormat, 2);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500167 canvas->drawImageRect(image,
168 SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f), &paint);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500169 image = grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500170 GrMaskFormat::kA8_GrMaskFormat, 3);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500171 canvas->drawImageRect(image,
172 SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f), &paint);
173 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500174#endif
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500175 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500176
177 canvas->scale(fScale, fScale);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500178 canvas->translate(0, fTranslate);
179 fTranslate -= 0.5f;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500180
181 // draw a consistent run of the 'words' - one word per line
182 SkScalar y = 0;
183 for (int index = 0; index < kNumBlobs; ++index) {
184 y += -fMetrics.fAscent;
185 canvas->drawTextBlob(fBlobs[index], 0, y, paint);
186
187 y += 3*(fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading);
188 }
189 }
190
191private:
192 static constexpr auto kNumBlobs = 8;
193 static constexpr auto kParagraphLength = 175;
194
195 void init() {
196 fTypeface = chinese_typeface();
197
Mike Reed12a6d452018-12-21 22:22:31 -0500198 SkFont font(fTypeface, 11);
199 font.getMetrics(&fMetrics);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500200
Mike Reed12a6d452018-12-21 22:22:31 -0500201 SkPaint paint;
202 paint.setColor(0xDE000000);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500203
204 SkUnichar glyphs[45];
205 for (int32_t i = 0; i < kNumBlobs; ++i) {
206 SkTextBlobBuilder builder;
207 auto paragraphLength = kParagraphLength;
208 SkScalar y = 0;
209 while (paragraphLength - 45 > 0) {
210 auto currentLineLength = SkTMin(45, paragraphLength - 45);
211 this->createRandomLine(glyphs, currentLineLength);
212
Mike Kleinea3f0142019-03-20 11:12:10 -0500213 ToolUtils::add_to_text_blob_w_len(&builder,
214 (const char*)glyphs,
215 currentLineLength * 4,
Ben Wagner51e15a62019-05-07 15:38:46 -0400216 SkTextEncoding::kUTF32,
Mike Kleinea3f0142019-03-20 11:12:10 -0500217 font,
218 0,
219 y);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500220 y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading;
221 paragraphLength -= 45;
222 }
223 fBlobs.emplace_back(builder.make());
224 }
225
226 fIndex = 0;
227 }
228
229 // Construct a random kWordLength character 'word' drawing from the full Chinese set
230 void createRandomLine(SkUnichar glyphs[45], int lineLength) {
231 for (auto i = 0; i < lineLength; ++i) {
232 glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
233 }
234 }
235
236 bool fInitialized = false;
237 sk_sp<SkTypeface> fTypeface;
Mike Reedb5784ac2018-11-12 09:35:15 -0500238 SkFontMetrics fMetrics;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500239 SkTArray<sk_sp<SkTextBlob>> fBlobs;
240 SkRandom fRand;
241 SkScalar fScale;
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500242 SkScalar fTranslate;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500243 int fIndex;
244
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400245 typedef Sample INHERITED;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500246};
247
Robert Phillips4c72b262017-08-15 13:28:42 -0400248//////////////////////////////////////////////////////////////////////////////
249
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400250DEF_SAMPLE( return new ChineseFlingView(); )
251DEF_SAMPLE( return new ChineseZoomView(); )