blob: 1cdf784369eb024965a2e7184e5de0a7ac7671d4 [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 {
Hal Canaryd7639af2019-07-17 09:08:11 -040041 static constexpr int kNumBlobs = 200;
42 static constexpr int kWordLength = 16;
Robert Phillips4c72b262017-08-15 13:28:42 -040043
Hal Canaryd7639af2019-07-17 09:08:11 -040044 sk_sp<SkTypeface> fTypeface;
45 SkFontMetrics fMetrics;
46 sk_sp<SkTextBlob> fBlobs[kNumBlobs];
47 SkRandom fRand;
48 int fIndex = 0;
49
Hal Canary8a027312019-07-03 10:55:44 -040050 SkString name() override { return SkString("chinese-fling"); }
Robert Phillips4c72b262017-08-15 13:28:42 -040051
52 void onDrawContent(SkCanvas* canvas) override {
Robert Phillips4c72b262017-08-15 13:28:42 -040053 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;
Hal Canaryd7639af2019-07-17 09:08:11 -040067 index %= kNumBlobs;
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);
Hal Canaryd7639af2019-07-17 09:08:11 -040071 fIndex %= kNumBlobs;
Robert Phillips4c72b262017-08-15 13:28:42 -040072 }
73
Hal Canaryd7639af2019-07-17 09:08:11 -040074 void onOnceBeforeDraw() override {
Robert Phillips4c72b262017-08-15 13:28:42 -040075 fTypeface = chinese_typeface();
76
Mike Reed12a6d452018-12-21 22:22:31 -050077 SkFont font(fTypeface, 56);
78 font.getMetrics(&fMetrics);
Robert Phillips4c72b262017-08-15 13:28:42 -040079
Jim Van Verthc3269ae2017-09-28 15:04:00 -040080 SkUnichar glyphs[kWordLength];
81 for (int32_t i = 0; i < kNumBlobs; ++i) {
82 this->createRandomWord(glyphs);
Robert Phillips4c72b262017-08-15 13:28:42 -040083
84 SkTextBlobBuilder builder;
Mike Kleinea3f0142019-03-20 11:12:10 -050085 ToolUtils::add_to_text_blob_w_len(&builder,
86 (const char*)glyphs,
87 kWordLength * 4,
Ben Wagner51e15a62019-05-07 15:38:46 -040088 SkTextEncoding::kUTF32,
Mike Kleinea3f0142019-03-20 11:12:10 -050089 font,
90 0,
91 0);
Robert Phillips4c72b262017-08-15 13:28:42 -040092
Hal Canaryd7639af2019-07-17 09:08:11 -040093 fBlobs[i] = builder.make();
Robert Phillips4c72b262017-08-15 13:28:42 -040094 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -040095 }
96
97 // Construct a random kWordLength character 'word' drawing from the full Chinese set
98 void createRandomWord(SkUnichar glyphs[kWordLength]) {
99 for (int i = 0; i < kWordLength; ++i) {
100 glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
101 }
Robert Phillips4c72b262017-08-15 13:28:42 -0400102 }
Robert Phillips4c72b262017-08-15 13:28:42 -0400103};
104
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400105class ChineseZoomView : public Sample {
Hal Canaryd7639af2019-07-17 09:08:11 -0400106 static constexpr int kNumBlobs = 8;
107 static constexpr int kParagraphLength = 175;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500108
Hal Canaryd7639af2019-07-17 09:08:11 -0400109 bool fAfterFirstFrame = false;
110 sk_sp<SkTypeface> fTypeface;
111 SkFontMetrics fMetrics;
112 sk_sp<SkTextBlob> fBlobs[kNumBlobs];
113 SkRandom fRand;
114 SkScalar fScale = 15;
115 SkScalar fTranslate = 0;
116
Hal Canary8a027312019-07-03 10:55:44 -0400117 SkString name() override { return SkString("chinese-zoom"); }
118
Hal Canary6cc65e12019-07-03 15:53:04 -0400119 bool onChar(SkUnichar uni) override {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500120 if ('>' == uni) {
121 fScale += 0.125f;
122 return true;
123 }
124 if ('<' == uni) {
125 fScale -= 0.125f;
126 return true;
127 }
Hal Canary6cc65e12019-07-03 15:53:04 -0400128 return false;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500129 }
130
131 void onDrawContent(SkCanvas* canvas) override {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500132 canvas->clear(0xFFDDDDDD);
133
134 SkPaint paint;
135 paint.setAntiAlias(true);
136 paint.setColor(0xDE000000);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500137
Hal Canaryd7639af2019-07-17 09:08:11 -0400138 if (fAfterFirstFrame) {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500139#if SK_SUPPORT_GPU
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500140 GrContext* grContext = canvas->getGrContext();
141 if (grContext) {
Hal Canaryd7639af2019-07-17 09:08:11 -0400142 sk_sp<SkImage> image = grContext->priv().testingOnly_getFontAtlasImage(
143 GrMaskFormat::kA8_GrMaskFormat, 0);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500144 canvas->drawImageRect(image,
145 SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0), &paint);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500146 image = grContext->priv().testingOnly_getFontAtlasImage(
Hal Canaryd7639af2019-07-17 09:08:11 -0400147 GrMaskFormat::kA8_GrMaskFormat, 1);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500148 canvas->drawImageRect(image,
149 SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f), &paint);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500150 image = grContext->priv().testingOnly_getFontAtlasImage(
Hal Canaryd7639af2019-07-17 09:08:11 -0400151 GrMaskFormat::kA8_GrMaskFormat, 2);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500152 canvas->drawImageRect(image,
153 SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f), &paint);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500154 image = grContext->priv().testingOnly_getFontAtlasImage(
Hal Canaryd7639af2019-07-17 09:08:11 -0400155 GrMaskFormat::kA8_GrMaskFormat, 3);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500156 canvas->drawImageRect(image,
157 SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f), &paint);
158 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500159#endif
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500160 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500161
162 canvas->scale(fScale, fScale);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500163 canvas->translate(0, fTranslate);
164 fTranslate -= 0.5f;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500165
166 // draw a consistent run of the 'words' - one word per line
167 SkScalar y = 0;
168 for (int index = 0; index < kNumBlobs; ++index) {
169 y += -fMetrics.fAscent;
170 canvas->drawTextBlob(fBlobs[index], 0, y, paint);
171
172 y += 3*(fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading);
173 }
Hal Canaryd7639af2019-07-17 09:08:11 -0400174 if (!fAfterFirstFrame) {
175 fAfterFirstFrame = true;
176 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500177 }
178
Hal Canaryd7639af2019-07-17 09:08:11 -0400179 void onOnceBeforeDraw() override {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500180 fTypeface = chinese_typeface();
181
Mike Reed12a6d452018-12-21 22:22:31 -0500182 SkFont font(fTypeface, 11);
183 font.getMetrics(&fMetrics);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500184
Mike Reed12a6d452018-12-21 22:22:31 -0500185 SkPaint paint;
186 paint.setColor(0xDE000000);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500187
188 SkUnichar glyphs[45];
189 for (int32_t i = 0; i < kNumBlobs; ++i) {
190 SkTextBlobBuilder builder;
191 auto paragraphLength = kParagraphLength;
192 SkScalar y = 0;
193 while (paragraphLength - 45 > 0) {
194 auto currentLineLength = SkTMin(45, paragraphLength - 45);
195 this->createRandomLine(glyphs, currentLineLength);
196
Mike Kleinea3f0142019-03-20 11:12:10 -0500197 ToolUtils::add_to_text_blob_w_len(&builder,
198 (const char*)glyphs,
199 currentLineLength * 4,
Ben Wagner51e15a62019-05-07 15:38:46 -0400200 SkTextEncoding::kUTF32,
Mike Kleinea3f0142019-03-20 11:12:10 -0500201 font,
202 0,
203 y);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500204 y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading;
205 paragraphLength -= 45;
206 }
Hal Canaryd7639af2019-07-17 09:08:11 -0400207 fBlobs[i] = builder.make();
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500208 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500209 }
210
211 // Construct a random kWordLength character 'word' drawing from the full Chinese set
212 void createRandomLine(SkUnichar glyphs[45], int lineLength) {
213 for (auto i = 0; i < lineLength; ++i) {
214 glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
215 }
216 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500217};
218
Robert Phillips4c72b262017-08-15 13:28:42 -0400219//////////////////////////////////////////////////////////////////////////////
220
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400221DEF_SAMPLE( return new ChineseFlingView(); )
222DEF_SAMPLE( return new ChineseZoomView(); )