blob: 78b15989e50ce26067b081d14af0faac52794b63 [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 {
caryclark3b0f98e2015-07-23 11:52:01 -070022 sk_tool_utils::emoji_typeface(&fEmojiTypeface);
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
36 static void rotate_about(SkCanvas* canvas,
37 SkScalar degrees,
38 SkScalar px, SkScalar py) {
39 canvas->translate(px, py);
40 canvas->rotate(degrees);
41 canvas->translate(-px, -py);
42 }
43
mtklein36352bf2015-03-25 18:17:31 -070044 virtual void onDraw(SkCanvas* inputCanvas) override {
jvanverthed14f1d2014-10-06 12:50:54 -070045 SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f };
jvanverth9564ce62014-09-16 05:45:19 -070046 SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f };
47
48 // set up offscreen rendering with distance field text
49#if SK_SUPPORT_GPU
50 GrContext* ctx = inputCanvas->getGrContext();
51 SkImageInfo info = SkImageInfo::MakeN32Premul(onISize());
bsalomonafcd7cd2015-08-31 12:39:41 -070052 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
reed4a8126e2014-09-22 07:29:03 -070053 SkSurfaceProps::kLegacyFontHost_InitType);
bsalomonafe30052015-01-16 07:32:33 -080054 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted,
55 info, 0, &props));
jvanverth76ce81e2014-09-22 14:26:53 -070056 SkCanvas* canvas = surface.get() ? surface->getCanvas() : inputCanvas;
jvanverthf9669f42014-10-07 07:59:45 -070057 // init our new canvas with the old canvas's matrix
58 canvas->setMatrix(inputCanvas->getTotalMatrix());
jvanverth9564ce62014-09-16 05:45:19 -070059#else
60 SkCanvas* canvas = inputCanvas;
61#endif
jvanverth76ce81e2014-09-22 14:26:53 -070062 // apply global scale to test glyph positioning
63 canvas->scale(1.05f, 1.05f);
jvanverth9564ce62014-09-16 05:45:19 -070064 canvas->clear(0xffffffff);
65
66 SkPaint paint;
67 paint.setAntiAlias(true);
68 paint.setSubpixelText(true);
jvanverth4854d132015-06-22 06:46:56 -070069
caryclark1818acb2015-07-24 12:09:25 -070070 sk_tool_utils::set_portable_typeface(&paint, "serif", SkTypeface::kNormal);
jvanverth9564ce62014-09-16 05:45:19 -070071
72 const char* text = "Hamburgefons";
73 const size_t textLen = strlen(text);
74
75 // check scaling up
76 SkScalar x = SkIntToScalar(0);
77 SkScalar y = SkIntToScalar(78);
78 for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) {
79 SkAutoCanvasRestore acr(canvas, true);
80 canvas->translate(x, y);
81 canvas->scale(scales[i], scales[i]);
82 paint.setTextSize(textSizes[i]);
83 canvas->drawText(text, textLen, 0, 0, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070084 y += paint.getFontMetrics(nullptr)*scales[i];
jvanverth9564ce62014-09-16 05:45:19 -070085 }
86
87 // check rotation
88 for (size_t i = 0; i < 5; ++i) {
89 SkScalar rotX = SkIntToScalar(10);
90 SkScalar rotY = y;
91
92 SkAutoCanvasRestore acr(canvas, true);
93 canvas->translate(SkIntToScalar(10 + i * 200), -80);
94 rotate_about(canvas, SkIntToScalar(i * 5), rotX, rotY);
95 for (int ps = 6; ps <= 32; ps += 3) {
96 paint.setTextSize(SkIntToScalar(ps));
97 canvas->drawText(text, textLen, rotX, rotY, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070098 rotY += paint.getFontMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -070099 }
100 }
101
102 // check scaling down
103 paint.setLCDRenderText(true);
jvanverth76ce81e2014-09-22 14:26:53 -0700104 x = SkIntToScalar(680);
jvanverth9564ce62014-09-16 05:45:19 -0700105 y = SkIntToScalar(20);
106 size_t arraySize = SK_ARRAY_COUNT(textSizes);
107 for (size_t i = 0; i < arraySize; ++i) {
108 SkAutoCanvasRestore acr(canvas, true);
109 canvas->translate(x, y);
110 SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]);
111 canvas->scale(scaleFactor, scaleFactor);
112 paint.setTextSize(textSizes[i]);
113 canvas->drawText(text, textLen, 0, 0, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700114 y += paint.getFontMetrics(nullptr)*scaleFactor;
jvanverth9564ce62014-09-16 05:45:19 -0700115 }
116
jvanverth76ce81e2014-09-22 14:26:53 -0700117 // check pos text
118 {
119 SkAutoCanvasRestore acr(canvas, true);
120
121 canvas->scale(2.0f, 2.0f);
122
bsalomonfbaace02014-12-12 16:41:46 -0800123 SkAutoTArray<SkPoint> pos(SkToInt(textLen));
124 SkAutoTArray<SkScalar> widths(SkToInt(textLen));
jvanverth76ce81e2014-09-22 14:26:53 -0700125 paint.setTextSize(textSizes[0]);
126
127 paint.getTextWidths(text, textLen, &widths[0]);
128
129 SkScalar x = SkIntToScalar(340);
130 SkScalar y = SkIntToScalar(75);
131 for (unsigned int i = 0; i < textLen; ++i) {
132 pos[i].set(x, y);
133 x += widths[i];
134 }
135
136 canvas->drawPosText(text, textLen, &pos[0], paint);
137 }
138
139
jvanverth9564ce62014-09-16 05:45:19 -0700140 // check gamma-corrected blending
141 const SkColor fg[] = {
142 0xFFFFFFFF,
143 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
144 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
145 0xFF000000,
146 };
147
caryclarkc3dcb672015-07-21 12:27:36 -0700148 paint.setColor(0xFFF7F3F7);
caryclarkba9757a2015-07-23 14:07:58 -0700149 SkRect r = SkRect::MakeLTRB(670, 215, 820, 397);
jvanverth9564ce62014-09-16 05:45:19 -0700150 canvas->drawRect(r, paint);
151
jvanverth76ce81e2014-09-22 14:26:53 -0700152 x = SkIntToScalar(680);
caryclarkba9757a2015-07-23 14:07:58 -0700153 y = SkIntToScalar(235);
jvanverthed14f1d2014-10-06 12:50:54 -0700154 paint.setTextSize(SkIntToScalar(19));
jvanverth9564ce62014-09-16 05:45:19 -0700155 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
156 paint.setColor(fg[i]);
157
158 canvas->drawText(text, textLen, x, y, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700159 y += paint.getFontMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -0700160 }
161
caryclarkc3dcb672015-07-21 12:27:36 -0700162 paint.setColor(0xFF181C18);
caryclarkba9757a2015-07-23 14:07:58 -0700163 r = SkRect::MakeLTRB(820, 215, 970, 397);
jvanverth9564ce62014-09-16 05:45:19 -0700164 canvas->drawRect(r, paint);
165
jvanverth76ce81e2014-09-22 14:26:53 -0700166 x = SkIntToScalar(830);
caryclarkba9757a2015-07-23 14:07:58 -0700167 y = SkIntToScalar(235);
jvanverthed14f1d2014-10-06 12:50:54 -0700168 paint.setTextSize(SkIntToScalar(19));
jvanverth9564ce62014-09-16 05:45:19 -0700169 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
170 paint.setColor(fg[i]);
171
172 canvas->drawText(text, textLen, x, y, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700173 y += paint.getFontMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -0700174 }
175
jvanverth354eba52015-03-16 11:32:49 -0700176 // check skew
177 {
178 paint.setLCDRenderText(false);
179 SkAutoCanvasRestore acr(canvas, true);
180 canvas->skew(0.0f, 0.151515f);
jvanverthd68a5502015-03-16 12:58:43 -0700181 paint.setTextSize(SkIntToScalar(32));
jvanverth354eba52015-03-16 11:32:49 -0700182 canvas->drawText(text, textLen, 745, 70, paint);
183 }
184 {
185 paint.setLCDRenderText(true);
186 SkAutoCanvasRestore acr(canvas, true);
187 canvas->skew(0.5f, 0.0f);
jvanverthd68a5502015-03-16 12:58:43 -0700188 paint.setTextSize(SkIntToScalar(32));
caryclarkba9757a2015-07-23 14:07:58 -0700189 canvas->drawText(text, textLen, 580, 125, paint);
jvanverth354eba52015-03-16 11:32:49 -0700190 }
191
jvanverthfca302c2014-10-20 13:12:54 -0700192 // check color emoji
caryclarkc3dcb672015-07-21 12:27:36 -0700193 if (fEmojiTypeface) {
194 paint.setTypeface(fEmojiTypeface);
195 paint.setTextSize(SkIntToScalar(19));
caryclarkba9757a2015-07-23 14:07:58 -0700196 canvas->drawText(fEmojiText, strlen(fEmojiText), 670, 90, paint);
caryclarkc3dcb672015-07-21 12:27:36 -0700197 }
jvanverth9564ce62014-09-16 05:45:19 -0700198#if SK_SUPPORT_GPU
199 // render offscreen buffer
jvanverth76ce81e2014-09-22 14:26:53 -0700200 if (surface) {
jvanverthf9669f42014-10-07 07:59:45 -0700201 SkAutoCanvasRestore acr(inputCanvas, true);
202 // since we prepended this matrix already, we blit using identity
203 inputCanvas->resetMatrix();
jvanverth76ce81e2014-09-22 14:26:53 -0700204 SkImage* image = surface->newImageSnapshot();
halcanary96fcdcc2015-08-27 07:41:13 -0700205 inputCanvas->drawImage(image, 0, 0, nullptr);
jvanverth76ce81e2014-09-22 14:26:53 -0700206 image->unref();
207 }
jvanverth9564ce62014-09-16 05:45:19 -0700208#endif
209 }
210
211private:
caryclarkc3dcb672015-07-21 12:27:36 -0700212 SkAutoTUnref<SkTypeface> fEmojiTypeface;
213 const char* fEmojiText;
jvanverthfca302c2014-10-20 13:12:54 -0700214
215 typedef skiagm::GM INHERITED;
jvanverth9564ce62014-09-16 05:45:19 -0700216};
217
halcanary385fe4d2015-08-26 13:07:48 -0700218DEF_GM(return new DFTextGM;)