blob: 1fbb131ad875b793010579223fd55ce84abc7e44 [file] [log] [blame]
jvanverth9564ce62014-09-16 05:45:19 -07001/*
2 * Copyright 2011 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 */
Hal Canaryc640d0d2018-06-13 09:59:02 -04007
jvanverthfca302c2014-10-20 13:12:54 -07008#include "Resources.h"
9#include "SkCanvas.h"
10#include "SkStream.h"
jvanverth9564ce62014-09-16 05:45:19 -070011#include "SkSurface.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040012#include "SkTo.h"
jvanverth9564ce62014-09-16 05:45:19 -070013#include "SkTypeface.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040014#include "gm.h"
15#include "sk_tool_utils.h"
jvanverth9564ce62014-09-16 05:45:19 -070016
jvanverthfca302c2014-10-20 13:12:54 -070017class DFTextGM : public skiagm::GM {
jvanverth9564ce62014-09-16 05:45:19 -070018public:
19 DFTextGM() {
20 this->setBGColor(0xFFFFFFFF);
jvanverth9564ce62014-09-16 05:45:19 -070021 }
22
23protected:
mtklein36352bf2015-03-25 18:17:31 -070024 void onOnceBeforeDraw() override {
bungeman13b9c952016-05-12 10:09:30 -070025 fEmojiTypeface = sk_tool_utils::emoji_typeface();
caryclarkc3dcb672015-07-21 12:27:36 -070026 fEmojiText = sk_tool_utils::emoji_sample_text();
jvanverthfca302c2014-10-20 13:12:54 -070027 }
28
mtklein36352bf2015-03-25 18:17:31 -070029 SkString onShortName() override {
caryclarkc3dcb672015-07-21 12:27:36 -070030 SkString name("dftext");
Mike Kleinf436fbc2017-11-15 10:42:46 -050031 name.append(sk_tool_utils::platform_font_manager());
caryclarkc3dcb672015-07-21 12:27:36 -070032 return name;
jvanverth9564ce62014-09-16 05:45:19 -070033 }
34
mtklein36352bf2015-03-25 18:17:31 -070035 SkISize onISize() override {
jvanverth9564ce62014-09-16 05:45:19 -070036 return SkISize::Make(1024, 768);
37 }
38
mtklein36352bf2015-03-25 18:17:31 -070039 virtual void onDraw(SkCanvas* inputCanvas) override {
jvanverthed14f1d2014-10-06 12:50:54 -070040 SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f };
jvanverth9564ce62014-09-16 05:45:19 -070041 SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f };
42
43 // set up offscreen rendering with distance field text
jvanverth9564ce62014-09-16 05:45:19 -070044 GrContext* ctx = inputCanvas->getGrContext();
brianosman52ede1d2016-06-20 08:25:02 -070045 SkISize size = onISize();
46 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType,
Mike Reed693fdbd2017-01-12 10:13:40 -050047 inputCanvas->imageInfo().refColorSpace());
brianosman3a0dbde2016-07-26 11:36:05 -070048 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
reed4a8126e2014-09-22 07:29:03 -070049 SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -070050 auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props));
51 SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas;
jvanverthf9669f42014-10-07 07:59:45 -070052 // init our new canvas with the old canvas's matrix
53 canvas->setMatrix(inputCanvas->getTotalMatrix());
jvanverth76ce81e2014-09-22 14:26:53 -070054 // apply global scale to test glyph positioning
55 canvas->scale(1.05f, 1.05f);
jvanverth9564ce62014-09-16 05:45:19 -070056 canvas->clear(0xffffffff);
57
58 SkPaint paint;
59 paint.setAntiAlias(true);
60 paint.setSubpixelText(true);
jvanverth4854d132015-06-22 06:46:56 -070061
mbocee6a9912016-05-31 11:42:36 -070062 sk_tool_utils::set_portable_typeface(&paint, "serif");
jvanverth9564ce62014-09-16 05:45:19 -070063
64 const char* text = "Hamburgefons";
65 const size_t textLen = strlen(text);
66
67 // check scaling up
68 SkScalar x = SkIntToScalar(0);
69 SkScalar y = SkIntToScalar(78);
70 for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) {
71 SkAutoCanvasRestore acr(canvas, true);
72 canvas->translate(x, y);
73 canvas->scale(scales[i], scales[i]);
74 paint.setTextSize(textSizes[i]);
75 canvas->drawText(text, textLen, 0, 0, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070076 y += paint.getFontMetrics(nullptr)*scales[i];
jvanverth9564ce62014-09-16 05:45:19 -070077 }
78
79 // check rotation
80 for (size_t i = 0; i < 5; ++i) {
81 SkScalar rotX = SkIntToScalar(10);
82 SkScalar rotY = y;
83
84 SkAutoCanvasRestore acr(canvas, true);
85 canvas->translate(SkIntToScalar(10 + i * 200), -80);
bungeman7438bfc2016-07-12 15:01:19 -070086 canvas->rotate(SkIntToScalar(i * 5), rotX, rotY);
jvanverth9564ce62014-09-16 05:45:19 -070087 for (int ps = 6; ps <= 32; ps += 3) {
88 paint.setTextSize(SkIntToScalar(ps));
89 canvas->drawText(text, textLen, rotX, rotY, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070090 rotY += paint.getFontMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -070091 }
92 }
93
94 // check scaling down
95 paint.setLCDRenderText(true);
jvanverth76ce81e2014-09-22 14:26:53 -070096 x = SkIntToScalar(680);
jvanverth9564ce62014-09-16 05:45:19 -070097 y = SkIntToScalar(20);
98 size_t arraySize = SK_ARRAY_COUNT(textSizes);
99 for (size_t i = 0; i < arraySize; ++i) {
100 SkAutoCanvasRestore acr(canvas, true);
101 canvas->translate(x, y);
102 SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]);
103 canvas->scale(scaleFactor, scaleFactor);
104 paint.setTextSize(textSizes[i]);
105 canvas->drawText(text, textLen, 0, 0, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700106 y += paint.getFontMetrics(nullptr)*scaleFactor;
jvanverth9564ce62014-09-16 05:45:19 -0700107 }
108
jvanverth76ce81e2014-09-22 14:26:53 -0700109 // check pos text
110 {
111 SkAutoCanvasRestore acr(canvas, true);
112
113 canvas->scale(2.0f, 2.0f);
114
bsalomonfbaace02014-12-12 16:41:46 -0800115 SkAutoTArray<SkPoint> pos(SkToInt(textLen));
116 SkAutoTArray<SkScalar> widths(SkToInt(textLen));
jvanverth76ce81e2014-09-22 14:26:53 -0700117 paint.setTextSize(textSizes[0]);
118
119 paint.getTextWidths(text, textLen, &widths[0]);
120
121 SkScalar x = SkIntToScalar(340);
122 SkScalar y = SkIntToScalar(75);
123 for (unsigned int i = 0; i < textLen; ++i) {
124 pos[i].set(x, y);
125 x += widths[i];
126 }
127
128 canvas->drawPosText(text, textLen, &pos[0], paint);
129 }
130
131
jvanverth9564ce62014-09-16 05:45:19 -0700132 // check gamma-corrected blending
133 const SkColor fg[] = {
134 0xFFFFFFFF,
135 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
136 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
137 0xFF000000,
138 };
139
caryclarkc3dcb672015-07-21 12:27:36 -0700140 paint.setColor(0xFFF7F3F7);
caryclarkba9757a2015-07-23 14:07:58 -0700141 SkRect r = SkRect::MakeLTRB(670, 215, 820, 397);
jvanverth9564ce62014-09-16 05:45:19 -0700142 canvas->drawRect(r, paint);
143
jvanverth76ce81e2014-09-22 14:26:53 -0700144 x = SkIntToScalar(680);
caryclarkba9757a2015-07-23 14:07:58 -0700145 y = SkIntToScalar(235);
jvanverthed14f1d2014-10-06 12:50:54 -0700146 paint.setTextSize(SkIntToScalar(19));
jvanverth9564ce62014-09-16 05:45:19 -0700147 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
148 paint.setColor(fg[i]);
149
150 canvas->drawText(text, textLen, x, y, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700151 y += paint.getFontMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -0700152 }
153
caryclarkc3dcb672015-07-21 12:27:36 -0700154 paint.setColor(0xFF181C18);
caryclarkba9757a2015-07-23 14:07:58 -0700155 r = SkRect::MakeLTRB(820, 215, 970, 397);
jvanverth9564ce62014-09-16 05:45:19 -0700156 canvas->drawRect(r, paint);
157
jvanverth76ce81e2014-09-22 14:26:53 -0700158 x = SkIntToScalar(830);
caryclarkba9757a2015-07-23 14:07:58 -0700159 y = SkIntToScalar(235);
jvanverthed14f1d2014-10-06 12:50:54 -0700160 paint.setTextSize(SkIntToScalar(19));
jvanverth9564ce62014-09-16 05:45:19 -0700161 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
162 paint.setColor(fg[i]);
163
164 canvas->drawText(text, textLen, x, y, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700165 y += paint.getFontMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -0700166 }
167
jvanverth354eba52015-03-16 11:32:49 -0700168 // check skew
169 {
170 paint.setLCDRenderText(false);
171 SkAutoCanvasRestore acr(canvas, true);
172 canvas->skew(0.0f, 0.151515f);
jvanverthd68a5502015-03-16 12:58:43 -0700173 paint.setTextSize(SkIntToScalar(32));
jvanverth354eba52015-03-16 11:32:49 -0700174 canvas->drawText(text, textLen, 745, 70, paint);
175 }
176 {
177 paint.setLCDRenderText(true);
178 SkAutoCanvasRestore acr(canvas, true);
179 canvas->skew(0.5f, 0.0f);
jvanverthd68a5502015-03-16 12:58:43 -0700180 paint.setTextSize(SkIntToScalar(32));
caryclarkba9757a2015-07-23 14:07:58 -0700181 canvas->drawText(text, textLen, 580, 125, paint);
jvanverth354eba52015-03-16 11:32:49 -0700182 }
183
Jim Van Verth2525a372018-06-07 09:41:28 -0400184 // check perspective
185 {
186 paint.setLCDRenderText(false);
187 SkAutoCanvasRestore acr(canvas, true);
188 SkMatrix persp;
189 persp.setAll(0.9839f, 0, 0,
190 0.2246f, 0.6829f, 0,
191 0.0002352f, -0.0003844f, 1);
192 canvas->concat(persp);
193 canvas->translate(1100, -295);
Jim Van Verthd2b50842018-06-12 12:46:54 -0400194 paint.setTextSize(37.5f);
Jim Van Verth2525a372018-06-07 09:41:28 -0400195 canvas->drawText(text, textLen, 0, 0, paint);
196 }
197 {
Jim Van Verth4ba1a6f2018-06-07 19:54:17 +0000198 paint.setSubpixelText(false);
199 paint.setAntiAlias(false);
Jim Van Verth2525a372018-06-07 09:41:28 -0400200 SkAutoCanvasRestore acr(canvas, true);
201 SkMatrix persp;
202 persp.setAll(0.9839f, 0, 0,
203 0.2246f, 0.6829f, 0,
204 0.0002352f, -0.0003844f, 1);
205 canvas->concat(persp);
206 canvas->translate(1075, -245);
Jim Van Verth4ba1a6f2018-06-07 19:54:17 +0000207 canvas->scale(375, 375);
208 paint.setTextSize(0.1f);
Jim Van Verth2525a372018-06-07 09:41:28 -0400209 canvas->drawText(text, textLen, 0, 0, paint);
210 }
211
jvanverthfca302c2014-10-20 13:12:54 -0700212 // check color emoji
caryclarkc3dcb672015-07-21 12:27:36 -0700213 if (fEmojiTypeface) {
Jim Van Verth2525a372018-06-07 09:41:28 -0400214 SkPaint emojiPaint;
215 emojiPaint.setSubpixelText(true);
216 emojiPaint.setAntiAlias(true);
217 emojiPaint.setTypeface(fEmojiTypeface);
218 emojiPaint.setTextSize(SkIntToScalar(19));
219 canvas->drawString(fEmojiText, 670, 90, emojiPaint);
caryclarkc3dcb672015-07-21 12:27:36 -0700220 }
Jim Van Verth2525a372018-06-07 09:41:28 -0400221
jvanverth9564ce62014-09-16 05:45:19 -0700222 // render offscreen buffer
jvanverth76ce81e2014-09-22 14:26:53 -0700223 if (surface) {
jvanverthf9669f42014-10-07 07:59:45 -0700224 SkAutoCanvasRestore acr(inputCanvas, true);
225 // since we prepended this matrix already, we blit using identity
226 inputCanvas->resetMatrix();
reed9ce9d672016-03-17 10:51:11 -0700227 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
jvanverth76ce81e2014-09-22 14:26:53 -0700228 }
jvanverth9564ce62014-09-16 05:45:19 -0700229 }
230
231private:
bungeman13b9c952016-05-12 10:09:30 -0700232 sk_sp<SkTypeface> fEmojiTypeface;
caryclarkc3dcb672015-07-21 12:27:36 -0700233 const char* fEmojiText;
jvanverthfca302c2014-10-20 13:12:54 -0700234
235 typedef skiagm::GM INHERITED;
jvanverth9564ce62014-09-16 05:45:19 -0700236};
237
halcanary385fe4d2015-08-26 13:07:48 -0700238DEF_GM(return new DFTextGM;)