blob: 0ee01e3309daaf890c3b8974ccc9c9a3aee30bd6 [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);
jvanverthfca302c2014-10-20 13:12:54 -070018 fTypeface = NULL;
jvanverth9564ce62014-09-16 05:45:19 -070019 }
20
21 virtual ~DFTextGM() {
jvanverthfca302c2014-10-20 13:12:54 -070022 SkSafeUnref(fTypeface);
jvanverth9564ce62014-09-16 05:45:19 -070023 }
24
25protected:
mtklein36352bf2015-03-25 18:17:31 -070026 void onOnceBeforeDraw() override {
bungeman3ffa1262015-04-30 17:12:58 -040027 fTypeface = GetResourceAsTypeface("/fonts/Funkster.ttf");
jvanverthfca302c2014-10-20 13:12:54 -070028 }
29
mtklein36352bf2015-03-25 18:17:31 -070030 SkString onShortName() override {
jvanverth9564ce62014-09-16 05:45:19 -070031 return SkString("dftext");
32 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 SkISize onISize() override {
jvanverth9564ce62014-09-16 05:45:19 -070035 return SkISize::Make(1024, 768);
36 }
37
38 static void rotate_about(SkCanvas* canvas,
39 SkScalar degrees,
40 SkScalar px, SkScalar py) {
41 canvas->translate(px, py);
42 canvas->rotate(degrees);
43 canvas->translate(-px, -py);
44 }
45
mtklein36352bf2015-03-25 18:17:31 -070046 virtual void onDraw(SkCanvas* inputCanvas) override {
djsollen901704b2014-10-08 07:12:11 -070047#ifdef SK_BUILD_FOR_ANDROID
jvanverthed14f1d2014-10-06 12:50:54 -070048 SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f };
49#else
jvanverth9564ce62014-09-16 05:45:19 -070050 SkScalar textSizes[] = { 11.0f, 11.0f*2.0f, 11.0f*5.0f, 11.0f*2.0f*5.0f };
jvanverthed14f1d2014-10-06 12:50:54 -070051#endif
jvanverth9564ce62014-09-16 05:45:19 -070052 SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f };
53
54 // set up offscreen rendering with distance field text
55#if SK_SUPPORT_GPU
56 GrContext* ctx = inputCanvas->getGrContext();
57 SkImageInfo info = SkImageInfo::MakeN32Premul(onISize());
reed4a8126e2014-09-22 07:29:03 -070058 SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag,
59 SkSurfaceProps::kLegacyFontHost_InitType);
bsalomonafe30052015-01-16 07:32:33 -080060 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted,
61 info, 0, &props));
jvanverth76ce81e2014-09-22 14:26:53 -070062 SkCanvas* canvas = surface.get() ? surface->getCanvas() : inputCanvas;
jvanverthf9669f42014-10-07 07:59:45 -070063 // init our new canvas with the old canvas's matrix
64 canvas->setMatrix(inputCanvas->getTotalMatrix());
jvanverth9564ce62014-09-16 05:45:19 -070065#else
66 SkCanvas* canvas = inputCanvas;
67#endif
jvanverth76ce81e2014-09-22 14:26:53 -070068 // apply global scale to test glyph positioning
69 canvas->scale(1.05f, 1.05f);
jvanverth9564ce62014-09-16 05:45:19 -070070 canvas->clear(0xffffffff);
71
72 SkPaint paint;
73 paint.setAntiAlias(true);
74 paint.setSubpixelText(true);
jvanverth4854d132015-06-22 06:46:56 -070075
jvanverth9564ce62014-09-16 05:45:19 -070076 sk_tool_utils::set_portable_typeface(&paint, "Times New Roman", SkTypeface::kNormal);
77
78 const char* text = "Hamburgefons";
79 const size_t textLen = strlen(text);
80
81 // check scaling up
82 SkScalar x = SkIntToScalar(0);
83 SkScalar y = SkIntToScalar(78);
84 for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) {
85 SkAutoCanvasRestore acr(canvas, true);
86 canvas->translate(x, y);
87 canvas->scale(scales[i], scales[i]);
88 paint.setTextSize(textSizes[i]);
89 canvas->drawText(text, textLen, 0, 0, paint);
90 y += paint.getFontMetrics(NULL)*scales[i];
91 }
92
93 // check rotation
94 for (size_t i = 0; i < 5; ++i) {
95 SkScalar rotX = SkIntToScalar(10);
96 SkScalar rotY = y;
97
98 SkAutoCanvasRestore acr(canvas, true);
99 canvas->translate(SkIntToScalar(10 + i * 200), -80);
100 rotate_about(canvas, SkIntToScalar(i * 5), rotX, rotY);
101 for (int ps = 6; ps <= 32; ps += 3) {
102 paint.setTextSize(SkIntToScalar(ps));
103 canvas->drawText(text, textLen, rotX, rotY, paint);
104 rotY += paint.getFontMetrics(NULL);
105 }
106 }
107
108 // check scaling down
109 paint.setLCDRenderText(true);
jvanverth76ce81e2014-09-22 14:26:53 -0700110 x = SkIntToScalar(680);
jvanverth9564ce62014-09-16 05:45:19 -0700111 y = SkIntToScalar(20);
112 size_t arraySize = SK_ARRAY_COUNT(textSizes);
113 for (size_t i = 0; i < arraySize; ++i) {
114 SkAutoCanvasRestore acr(canvas, true);
115 canvas->translate(x, y);
116 SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]);
117 canvas->scale(scaleFactor, scaleFactor);
118 paint.setTextSize(textSizes[i]);
119 canvas->drawText(text, textLen, 0, 0, paint);
120 y += paint.getFontMetrics(NULL)*scaleFactor;
121 }
122
jvanverth76ce81e2014-09-22 14:26:53 -0700123 // check pos text
124 {
125 SkAutoCanvasRestore acr(canvas, true);
126
127 canvas->scale(2.0f, 2.0f);
128
bsalomonfbaace02014-12-12 16:41:46 -0800129 SkAutoTArray<SkPoint> pos(SkToInt(textLen));
130 SkAutoTArray<SkScalar> widths(SkToInt(textLen));
jvanverth76ce81e2014-09-22 14:26:53 -0700131 paint.setTextSize(textSizes[0]);
132
133 paint.getTextWidths(text, textLen, &widths[0]);
134
135 SkScalar x = SkIntToScalar(340);
136 SkScalar y = SkIntToScalar(75);
137 for (unsigned int i = 0; i < textLen; ++i) {
138 pos[i].set(x, y);
139 x += widths[i];
140 }
141
142 canvas->drawPosText(text, textLen, &pos[0], paint);
143 }
144
145
jvanverth9564ce62014-09-16 05:45:19 -0700146 // check gamma-corrected blending
147 const SkColor fg[] = {
148 0xFFFFFFFF,
149 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
150 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
151 0xFF000000,
152 };
153
154 paint.setColor(0xFFF1F1F1);
jvanverth76ce81e2014-09-22 14:26:53 -0700155 SkRect r = SkRect::MakeLTRB(670, 250, 820, 460);
jvanverth9564ce62014-09-16 05:45:19 -0700156 canvas->drawRect(r, paint);
157
jvanverth76ce81e2014-09-22 14:26:53 -0700158 x = SkIntToScalar(680);
jvanverth9564ce62014-09-16 05:45:19 -0700159 y = SkIntToScalar(270);
djsollen901704b2014-10-08 07:12:11 -0700160#ifdef SK_BUILD_FOR_ANDROID
jvanverthed14f1d2014-10-06 12:50:54 -0700161 paint.setTextSize(SkIntToScalar(19));
162#else
jvanverth9564ce62014-09-16 05:45:19 -0700163 paint.setTextSize(SkIntToScalar(22));
jvanverthed14f1d2014-10-06 12:50:54 -0700164#endif
jvanverth9564ce62014-09-16 05:45:19 -0700165 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
166 paint.setColor(fg[i]);
167
168 canvas->drawText(text, textLen, x, y, paint);
169 y += paint.getFontMetrics(NULL);
170 }
171
172 paint.setColor(0xFF1F1F1F);
jvanverth76ce81e2014-09-22 14:26:53 -0700173 r = SkRect::MakeLTRB(820, 250, 970, 460);
jvanverth9564ce62014-09-16 05:45:19 -0700174 canvas->drawRect(r, paint);
175
jvanverth76ce81e2014-09-22 14:26:53 -0700176 x = SkIntToScalar(830);
jvanverth9564ce62014-09-16 05:45:19 -0700177 y = SkIntToScalar(270);
djsollen901704b2014-10-08 07:12:11 -0700178#ifdef SK_BUILD_FOR_ANDROID
jvanverthed14f1d2014-10-06 12:50:54 -0700179 paint.setTextSize(SkIntToScalar(19));
180#else
jvanverth9564ce62014-09-16 05:45:19 -0700181 paint.setTextSize(SkIntToScalar(22));
jvanverthed14f1d2014-10-06 12:50:54 -0700182#endif
jvanverth9564ce62014-09-16 05:45:19 -0700183 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
184 paint.setColor(fg[i]);
185
186 canvas->drawText(text, textLen, x, y, paint);
187 y += paint.getFontMetrics(NULL);
188 }
189
jvanverth354eba52015-03-16 11:32:49 -0700190 // check skew
191 {
192 paint.setLCDRenderText(false);
193 SkAutoCanvasRestore acr(canvas, true);
194 canvas->skew(0.0f, 0.151515f);
jvanverthd68a5502015-03-16 12:58:43 -0700195 paint.setTextSize(SkIntToScalar(32));
jvanverth354eba52015-03-16 11:32:49 -0700196 canvas->drawText(text, textLen, 745, 70, paint);
197 }
198 {
199 paint.setLCDRenderText(true);
200 SkAutoCanvasRestore acr(canvas, true);
201 canvas->skew(0.5f, 0.0f);
jvanverthd68a5502015-03-16 12:58:43 -0700202 paint.setTextSize(SkIntToScalar(32));
jvanverth354eba52015-03-16 11:32:49 -0700203 canvas->drawText(text, textLen, 580, 230, paint);
204 }
205
jvanverthfca302c2014-10-20 13:12:54 -0700206 // check color emoji
207 paint.setTypeface(fTypeface);
jvanverthd68a5502015-03-16 12:58:43 -0700208#ifdef SK_BUILD_FOR_ANDROID
209 paint.setTextSize(SkIntToScalar(19));
210#else
211 paint.setTextSize(SkIntToScalar(22));
212#endif
jvanverthfca302c2014-10-20 13:12:54 -0700213 canvas->drawText(text, textLen, 670, 100, paint);
214
jvanverth9564ce62014-09-16 05:45:19 -0700215#if SK_SUPPORT_GPU
216 // render offscreen buffer
jvanverth76ce81e2014-09-22 14:26:53 -0700217 if (surface) {
jvanverthf9669f42014-10-07 07:59:45 -0700218 SkAutoCanvasRestore acr(inputCanvas, true);
219 // since we prepended this matrix already, we blit using identity
220 inputCanvas->resetMatrix();
jvanverth76ce81e2014-09-22 14:26:53 -0700221 SkImage* image = surface->newImageSnapshot();
piotaixrb5fae932014-09-24 13:03:30 -0700222 inputCanvas->drawImage(image, 0, 0, NULL);
jvanverth76ce81e2014-09-22 14:26:53 -0700223 image->unref();
224 }
jvanverth9564ce62014-09-16 05:45:19 -0700225#endif
226 }
227
228private:
jvanverthfca302c2014-10-20 13:12:54 -0700229 SkTypeface* fTypeface;
230
231 typedef skiagm::GM INHERITED;
jvanverth9564ce62014-09-16 05:45:19 -0700232};
233
jvanverthfca302c2014-10-20 13:12:54 -0700234DEF_GM( return SkNEW(DFTextGM); )