blob: 081433c848045afd8697665e7bb03169b0c6bdc6 [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
Robert Phillips30ebcf72020-07-09 13:25:17 -040020#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040021#include "src/gpu/GrDirectContextPriv.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
Robert Phillips30ebcf72020-07-09 13:25:17 -0400140 auto direct = GrAsDirectContext(canvas->recordingContext());
141 if (direct) {
142 sk_sp<SkImage> image = direct->priv().testingOnly_getFontAtlasImage(
Hal Canaryd7639af2019-07-17 09:08:11 -0400143 GrMaskFormat::kA8_GrMaskFormat, 0);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500144 canvas->drawImageRect(image,
Mike Reed34c56a52021-01-22 15:26:41 -0500145 SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0),
146 SkSamplingOptions(), &paint);
Robert Phillips30ebcf72020-07-09 13:25:17 -0400147 image = direct->priv().testingOnly_getFontAtlasImage(
Hal Canaryd7639af2019-07-17 09:08:11 -0400148 GrMaskFormat::kA8_GrMaskFormat, 1);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500149 canvas->drawImageRect(image,
Mike Reed34c56a52021-01-22 15:26:41 -0500150 SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f),
151 SkSamplingOptions(), &paint);
Robert Phillips30ebcf72020-07-09 13:25:17 -0400152 image = direct->priv().testingOnly_getFontAtlasImage(
Hal Canaryd7639af2019-07-17 09:08:11 -0400153 GrMaskFormat::kA8_GrMaskFormat, 2);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500154 canvas->drawImageRect(image,
Mike Reed34c56a52021-01-22 15:26:41 -0500155 SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f),
156 SkSamplingOptions(), &paint);
Robert Phillips30ebcf72020-07-09 13:25:17 -0400157 image = direct->priv().testingOnly_getFontAtlasImage(
Hal Canaryd7639af2019-07-17 09:08:11 -0400158 GrMaskFormat::kA8_GrMaskFormat, 3);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500159 canvas->drawImageRect(image,
Mike Reed34c56a52021-01-22 15:26:41 -0500160 SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f),
161 SkSamplingOptions(), &paint);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500162 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500163#endif
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500164 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500165
166 canvas->scale(fScale, fScale);
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500167 canvas->translate(0, fTranslate);
168 fTranslate -= 0.5f;
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500169
170 // draw a consistent run of the 'words' - one word per line
171 SkScalar y = 0;
172 for (int index = 0; index < kNumBlobs; ++index) {
173 y += -fMetrics.fAscent;
174 canvas->drawTextBlob(fBlobs[index], 0, y, paint);
175
176 y += 3*(fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading);
177 }
Hal Canaryd7639af2019-07-17 09:08:11 -0400178 if (!fAfterFirstFrame) {
179 fAfterFirstFrame = true;
180 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500181 }
182
Hal Canaryd7639af2019-07-17 09:08:11 -0400183 void onOnceBeforeDraw() override {
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500184 fTypeface = chinese_typeface();
185
Mike Reed12a6d452018-12-21 22:22:31 -0500186 SkFont font(fTypeface, 11);
187 font.getMetrics(&fMetrics);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500188
Mike Reed12a6d452018-12-21 22:22:31 -0500189 SkPaint paint;
190 paint.setColor(0xDE000000);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500191
192 SkUnichar glyphs[45];
193 for (int32_t i = 0; i < kNumBlobs; ++i) {
194 SkTextBlobBuilder builder;
195 auto paragraphLength = kParagraphLength;
196 SkScalar y = 0;
197 while (paragraphLength - 45 > 0) {
Brian Osman788b9162020-02-07 10:36:46 -0500198 auto currentLineLength = std::min(45, paragraphLength - 45);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500199 this->createRandomLine(glyphs, currentLineLength);
200
Mike Kleinea3f0142019-03-20 11:12:10 -0500201 ToolUtils::add_to_text_blob_w_len(&builder,
202 (const char*)glyphs,
203 currentLineLength * 4,
Ben Wagner51e15a62019-05-07 15:38:46 -0400204 SkTextEncoding::kUTF32,
Mike Kleinea3f0142019-03-20 11:12:10 -0500205 font,
206 0,
207 y);
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500208 y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading;
209 paragraphLength -= 45;
210 }
Hal Canaryd7639af2019-07-17 09:08:11 -0400211 fBlobs[i] = builder.make();
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500212 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500213 }
214
215 // Construct a random kWordLength character 'word' drawing from the full Chinese set
216 void createRandomLine(SkUnichar glyphs[45], int lineLength) {
217 for (auto i = 0; i < lineLength; ++i) {
218 glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
219 }
220 }
Jim Van Verth87d18ce2018-01-22 12:45:47 -0500221};
222
Robert Phillips4c72b262017-08-15 13:28:42 -0400223//////////////////////////////////////////////////////////////////////////////
224
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400225DEF_SAMPLE( return new ChineseFlingView(); )
226DEF_SAMPLE( return new ChineseZoomView(); )