blob: f1b972892ec6a63e49b67dbf5c814c5ee28951ae [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 */
7#include "gm.h"
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"
12#include "SkTypeface.h"
13
jvanverthfca302c2014-10-20 13:12:54 -070014class DFTextGM : public skiagm::GM {
jvanverth9564ce62014-09-16 05:45:19 -070015public:
16 DFTextGM() {
17 this->setBGColor(0xFFFFFFFF);
jvanverth9564ce62014-09-16 05:45:19 -070018 }
19
20protected:
mtklein36352bf2015-03-25 18:17:31 -070021 void onOnceBeforeDraw() override {
bungeman13b9c952016-05-12 10:09:30 -070022 fEmojiTypeface = sk_tool_utils::emoji_typeface();
caryclarkc3dcb672015-07-21 12:27:36 -070023 fEmojiText = sk_tool_utils::emoji_sample_text();
jvanverthfca302c2014-10-20 13:12:54 -070024 }
25
mtklein36352bf2015-03-25 18:17:31 -070026 SkString onShortName() override {
caryclarkc3dcb672015-07-21 12:27:36 -070027 SkString name("dftext");
28 name.append(sk_tool_utils::platform_os_emoji());
29 return name;
jvanverth9564ce62014-09-16 05:45:19 -070030 }
31
mtklein36352bf2015-03-25 18:17:31 -070032 SkISize onISize() override {
jvanverth9564ce62014-09-16 05:45:19 -070033 return SkISize::Make(1024, 768);
34 }
35
mtklein36352bf2015-03-25 18:17:31 -070036 virtual void onDraw(SkCanvas* inputCanvas) override {
jvanverthed14f1d2014-10-06 12:50:54 -070037 SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f };
jvanverth9564ce62014-09-16 05:45:19 -070038 SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f };
39
40 // set up offscreen rendering with distance field text
41#if SK_SUPPORT_GPU
42 GrContext* ctx = inputCanvas->getGrContext();
brianosman52ede1d2016-06-20 08:25:02 -070043 SkISize size = onISize();
44 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType,
45 sk_ref_sp(inputCanvas->imageInfo().colorSpace()));
brianosman3a0dbde2016-07-26 11:36:05 -070046 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
reed4a8126e2014-09-22 07:29:03 -070047 SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -070048 auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props));
49 SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas;
jvanverthf9669f42014-10-07 07:59:45 -070050 // init our new canvas with the old canvas's matrix
51 canvas->setMatrix(inputCanvas->getTotalMatrix());
jvanverth9564ce62014-09-16 05:45:19 -070052#else
53 SkCanvas* canvas = inputCanvas;
54#endif
jvanverth76ce81e2014-09-22 14:26:53 -070055 // apply global scale to test glyph positioning
56 canvas->scale(1.05f, 1.05f);
jvanverth9564ce62014-09-16 05:45:19 -070057 canvas->clear(0xffffffff);
58
59 SkPaint paint;
60 paint.setAntiAlias(true);
61 paint.setSubpixelText(true);
jvanverth4854d132015-06-22 06:46:56 -070062
mbocee6a9912016-05-31 11:42:36 -070063 sk_tool_utils::set_portable_typeface(&paint, "serif");
jvanverth9564ce62014-09-16 05:45:19 -070064
65 const char* text = "Hamburgefons";
66 const size_t textLen = strlen(text);
67
68 // check scaling up
69 SkScalar x = SkIntToScalar(0);
70 SkScalar y = SkIntToScalar(78);
71 for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) {
72 SkAutoCanvasRestore acr(canvas, true);
73 canvas->translate(x, y);
74 canvas->scale(scales[i], scales[i]);
75 paint.setTextSize(textSizes[i]);
76 canvas->drawText(text, textLen, 0, 0, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070077 y += paint.getFontMetrics(nullptr)*scales[i];
jvanverth9564ce62014-09-16 05:45:19 -070078 }
79
80 // check rotation
81 for (size_t i = 0; i < 5; ++i) {
82 SkScalar rotX = SkIntToScalar(10);
83 SkScalar rotY = y;
84
85 SkAutoCanvasRestore acr(canvas, true);
86 canvas->translate(SkIntToScalar(10 + i * 200), -80);
bungeman7438bfc2016-07-12 15:01:19 -070087 canvas->rotate(SkIntToScalar(i * 5), rotX, rotY);
jvanverth9564ce62014-09-16 05:45:19 -070088 for (int ps = 6; ps <= 32; ps += 3) {
89 paint.setTextSize(SkIntToScalar(ps));
90 canvas->drawText(text, textLen, rotX, rotY, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070091 rotY += paint.getFontMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -070092 }
93 }
94
95 // check scaling down
96 paint.setLCDRenderText(true);
jvanverth76ce81e2014-09-22 14:26:53 -070097 x = SkIntToScalar(680);
jvanverth9564ce62014-09-16 05:45:19 -070098 y = SkIntToScalar(20);
99 size_t arraySize = SK_ARRAY_COUNT(textSizes);
100 for (size_t i = 0; i < arraySize; ++i) {
101 SkAutoCanvasRestore acr(canvas, true);
102 canvas->translate(x, y);
103 SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]);
104 canvas->scale(scaleFactor, scaleFactor);
105 paint.setTextSize(textSizes[i]);
106 canvas->drawText(text, textLen, 0, 0, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700107 y += paint.getFontMetrics(nullptr)*scaleFactor;
jvanverth9564ce62014-09-16 05:45:19 -0700108 }
109
jvanverth76ce81e2014-09-22 14:26:53 -0700110 // check pos text
111 {
112 SkAutoCanvasRestore acr(canvas, true);
113
114 canvas->scale(2.0f, 2.0f);
115
bsalomonfbaace02014-12-12 16:41:46 -0800116 SkAutoTArray<SkPoint> pos(SkToInt(textLen));
117 SkAutoTArray<SkScalar> widths(SkToInt(textLen));
jvanverth76ce81e2014-09-22 14:26:53 -0700118 paint.setTextSize(textSizes[0]);
119
120 paint.getTextWidths(text, textLen, &widths[0]);
121
122 SkScalar x = SkIntToScalar(340);
123 SkScalar y = SkIntToScalar(75);
124 for (unsigned int i = 0; i < textLen; ++i) {
125 pos[i].set(x, y);
126 x += widths[i];
127 }
128
129 canvas->drawPosText(text, textLen, &pos[0], paint);
130 }
131
132
jvanverth9564ce62014-09-16 05:45:19 -0700133 // check gamma-corrected blending
134 const SkColor fg[] = {
135 0xFFFFFFFF,
136 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
137 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
138 0xFF000000,
139 };
140
caryclarkc3dcb672015-07-21 12:27:36 -0700141 paint.setColor(0xFFF7F3F7);
caryclarkba9757a2015-07-23 14:07:58 -0700142 SkRect r = SkRect::MakeLTRB(670, 215, 820, 397);
jvanverth9564ce62014-09-16 05:45:19 -0700143 canvas->drawRect(r, paint);
144
jvanverth76ce81e2014-09-22 14:26:53 -0700145 x = SkIntToScalar(680);
caryclarkba9757a2015-07-23 14:07:58 -0700146 y = SkIntToScalar(235);
jvanverthed14f1d2014-10-06 12:50:54 -0700147 paint.setTextSize(SkIntToScalar(19));
jvanverth9564ce62014-09-16 05:45:19 -0700148 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
149 paint.setColor(fg[i]);
150
151 canvas->drawText(text, textLen, x, y, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700152 y += paint.getFontMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -0700153 }
154
caryclarkc3dcb672015-07-21 12:27:36 -0700155 paint.setColor(0xFF181C18);
caryclarkba9757a2015-07-23 14:07:58 -0700156 r = SkRect::MakeLTRB(820, 215, 970, 397);
jvanverth9564ce62014-09-16 05:45:19 -0700157 canvas->drawRect(r, paint);
158
jvanverth76ce81e2014-09-22 14:26:53 -0700159 x = SkIntToScalar(830);
caryclarkba9757a2015-07-23 14:07:58 -0700160 y = SkIntToScalar(235);
jvanverthed14f1d2014-10-06 12:50:54 -0700161 paint.setTextSize(SkIntToScalar(19));
jvanverth9564ce62014-09-16 05:45:19 -0700162 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
163 paint.setColor(fg[i]);
164
165 canvas->drawText(text, textLen, x, y, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700166 y += paint.getFontMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -0700167 }
168
jvanverth354eba52015-03-16 11:32:49 -0700169 // check skew
170 {
171 paint.setLCDRenderText(false);
172 SkAutoCanvasRestore acr(canvas, true);
173 canvas->skew(0.0f, 0.151515f);
jvanverthd68a5502015-03-16 12:58:43 -0700174 paint.setTextSize(SkIntToScalar(32));
jvanverth354eba52015-03-16 11:32:49 -0700175 canvas->drawText(text, textLen, 745, 70, paint);
176 }
177 {
178 paint.setLCDRenderText(true);
179 SkAutoCanvasRestore acr(canvas, true);
180 canvas->skew(0.5f, 0.0f);
jvanverthd68a5502015-03-16 12:58:43 -0700181 paint.setTextSize(SkIntToScalar(32));
caryclarkba9757a2015-07-23 14:07:58 -0700182 canvas->drawText(text, textLen, 580, 125, paint);
jvanverth354eba52015-03-16 11:32:49 -0700183 }
184
jvanverthfca302c2014-10-20 13:12:54 -0700185 // check color emoji
caryclarkc3dcb672015-07-21 12:27:36 -0700186 if (fEmojiTypeface) {
187 paint.setTypeface(fEmojiTypeface);
188 paint.setTextSize(SkIntToScalar(19));
caryclarkba9757a2015-07-23 14:07:58 -0700189 canvas->drawText(fEmojiText, strlen(fEmojiText), 670, 90, paint);
caryclarkc3dcb672015-07-21 12:27:36 -0700190 }
jvanverth9564ce62014-09-16 05:45:19 -0700191#if SK_SUPPORT_GPU
192 // render offscreen buffer
jvanverth76ce81e2014-09-22 14:26:53 -0700193 if (surface) {
jvanverthf9669f42014-10-07 07:59:45 -0700194 SkAutoCanvasRestore acr(inputCanvas, true);
195 // since we prepended this matrix already, we blit using identity
196 inputCanvas->resetMatrix();
reed9ce9d672016-03-17 10:51:11 -0700197 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
jvanverth76ce81e2014-09-22 14:26:53 -0700198 }
jvanverth9564ce62014-09-16 05:45:19 -0700199#endif
200 }
201
202private:
bungeman13b9c952016-05-12 10:09:30 -0700203 sk_sp<SkTypeface> fEmojiTypeface;
caryclarkc3dcb672015-07-21 12:27:36 -0700204 const char* fEmojiText;
jvanverthfca302c2014-10-20 13:12:54 -0700205
206 typedef skiagm::GM INHERITED;
jvanverth9564ce62014-09-16 05:45:19 -0700207};
208
halcanary385fe4d2015-08-26 13:07:48 -0700209DEF_GM(return new DFTextGM;)