C++11 override should now be supported by all of {bots,Chrome,Android,Mozilla}
[platform/upstream/libSkiaSharp.git] / gm / dftext.cpp
1 /*
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"
8 #include "Resources.h"
9 #include "SkCanvas.h"
10 #include "SkStream.h"
11 #include "SkSurface.h"
12 #include "SkTypeface.h"
13
14 class DFTextGM : public skiagm::GM {
15 public:
16     DFTextGM() {
17         this->setBGColor(0xFFFFFFFF);
18         fTypeface = NULL;
19     }
20
21     virtual ~DFTextGM() {
22         SkSafeUnref(fTypeface);
23     }
24
25 protected:
26     void onOnceBeforeDraw() override {
27         SkString filename = GetResourcePath("/Funkster.ttf");
28         SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(filename.c_str()));
29         if (!stream->isValid()) {
30             SkDebugf("Could not find Funkster.ttf, please set --resourcePath correctly.\n");
31             return;
32         }
33
34         fTypeface = SkTypeface::CreateFromStream(stream.detach());
35     }
36
37     SkString onShortName() override {
38         return SkString("dftext");
39     }
40
41     SkISize onISize() override {
42         return SkISize::Make(1024, 768);
43     }
44
45     static void rotate_about(SkCanvas* canvas,
46         SkScalar degrees,
47         SkScalar px, SkScalar py) {
48         canvas->translate(px, py);
49         canvas->rotate(degrees);
50         canvas->translate(-px, -py);
51     }
52
53     virtual void onDraw(SkCanvas* inputCanvas) override {
54 #ifdef SK_BUILD_FOR_ANDROID
55         SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f };
56 #else
57         SkScalar textSizes[] = { 11.0f, 11.0f*2.0f, 11.0f*5.0f, 11.0f*2.0f*5.0f };
58 #endif
59         SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f };
60
61         // set up offscreen rendering with distance field text
62 #if SK_SUPPORT_GPU
63         GrContext* ctx = inputCanvas->getGrContext();
64         SkImageInfo info = SkImageInfo::MakeN32Premul(onISize());
65         SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag,
66                              SkSurfaceProps::kLegacyFontHost_InitType);
67         SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted,
68                                                                    info, 0, &props));
69         SkCanvas* canvas = surface.get() ? surface->getCanvas() : inputCanvas;
70         // init our new canvas with the old canvas's matrix
71         canvas->setMatrix(inputCanvas->getTotalMatrix());
72 #else
73         SkCanvas* canvas = inputCanvas;
74 #endif
75         // apply global scale to test glyph positioning
76         canvas->scale(1.05f, 1.05f);
77         canvas->clear(0xffffffff);
78
79         SkPaint paint;
80         paint.setAntiAlias(true);
81         paint.setSubpixelText(true);
82 #if !SK_SUPPORT_GPU
83         paint.setDistanceFieldTextTEMP(true);
84 #endif
85         sk_tool_utils::set_portable_typeface(&paint, "Times New Roman", SkTypeface::kNormal);
86
87         const char* text = "Hamburgefons";
88         const size_t textLen = strlen(text);
89
90         // check scaling up
91         SkScalar x = SkIntToScalar(0);
92         SkScalar y = SkIntToScalar(78);
93         for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) {
94             SkAutoCanvasRestore acr(canvas, true);
95             canvas->translate(x, y);
96             canvas->scale(scales[i], scales[i]);
97             paint.setTextSize(textSizes[i]);
98             canvas->drawText(text, textLen, 0, 0, paint);
99             y += paint.getFontMetrics(NULL)*scales[i];
100         }
101
102         // check rotation
103         for (size_t i = 0; i < 5; ++i) {
104             SkScalar rotX = SkIntToScalar(10);
105             SkScalar rotY = y;
106
107             SkAutoCanvasRestore acr(canvas, true);
108             canvas->translate(SkIntToScalar(10 + i * 200), -80);
109             rotate_about(canvas, SkIntToScalar(i * 5), rotX, rotY);
110             for (int ps = 6; ps <= 32; ps += 3) {
111                 paint.setTextSize(SkIntToScalar(ps));
112                 canvas->drawText(text, textLen, rotX, rotY, paint);
113                 rotY += paint.getFontMetrics(NULL);
114             }
115         }
116
117         // check scaling down
118         paint.setLCDRenderText(true);
119         x = SkIntToScalar(680);
120         y = SkIntToScalar(20);
121         size_t arraySize = SK_ARRAY_COUNT(textSizes);
122         for (size_t i = 0; i < arraySize; ++i) {
123             SkAutoCanvasRestore acr(canvas, true);
124             canvas->translate(x, y);
125             SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]);
126             canvas->scale(scaleFactor, scaleFactor);
127             paint.setTextSize(textSizes[i]);
128             canvas->drawText(text, textLen, 0, 0, paint);
129             y += paint.getFontMetrics(NULL)*scaleFactor;
130         }
131
132         // check pos text
133         {
134             SkAutoCanvasRestore acr(canvas, true);
135
136             canvas->scale(2.0f, 2.0f);
137
138             SkAutoTArray<SkPoint>  pos(SkToInt(textLen));
139             SkAutoTArray<SkScalar> widths(SkToInt(textLen));
140             paint.setTextSize(textSizes[0]);
141
142             paint.getTextWidths(text, textLen, &widths[0]);
143
144             SkScalar x = SkIntToScalar(340);
145             SkScalar y = SkIntToScalar(75);
146             for (unsigned int i = 0; i < textLen; ++i) {
147                 pos[i].set(x, y);
148                 x += widths[i];
149             }
150
151             canvas->drawPosText(text, textLen, &pos[0], paint);
152         }
153
154
155         // check gamma-corrected blending
156         const SkColor fg[] = {
157             0xFFFFFFFF,
158             0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
159             0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
160             0xFF000000,
161         };
162
163         paint.setColor(0xFFF1F1F1);
164         SkRect r = SkRect::MakeLTRB(670, 250, 820, 460);
165         canvas->drawRect(r, paint);
166
167         x = SkIntToScalar(680);
168         y = SkIntToScalar(270);
169 #ifdef SK_BUILD_FOR_ANDROID
170         paint.setTextSize(SkIntToScalar(19));
171 #else
172         paint.setTextSize(SkIntToScalar(22));
173 #endif
174         for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
175             paint.setColor(fg[i]);
176
177             canvas->drawText(text, textLen, x, y, paint);
178             y += paint.getFontMetrics(NULL);
179         }
180
181         paint.setColor(0xFF1F1F1F);
182         r = SkRect::MakeLTRB(820, 250, 970, 460);
183         canvas->drawRect(r, paint);
184
185         x = SkIntToScalar(830);
186         y = SkIntToScalar(270);
187 #ifdef SK_BUILD_FOR_ANDROID
188         paint.setTextSize(SkIntToScalar(19));
189 #else
190         paint.setTextSize(SkIntToScalar(22));
191 #endif
192         for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
193             paint.setColor(fg[i]);
194
195             canvas->drawText(text, textLen, x, y, paint);
196             y += paint.getFontMetrics(NULL);
197         }
198
199         // check skew
200         {
201             paint.setLCDRenderText(false);
202             SkAutoCanvasRestore acr(canvas, true);
203             canvas->skew(0.0f, 0.151515f);
204             paint.setTextSize(SkIntToScalar(32));
205             canvas->drawText(text, textLen, 745, 70, paint);
206         }
207         {
208             paint.setLCDRenderText(true);
209             SkAutoCanvasRestore acr(canvas, true);
210             canvas->skew(0.5f, 0.0f);
211             paint.setTextSize(SkIntToScalar(32));
212             canvas->drawText(text, textLen, 580, 230, paint);
213         }
214
215         // check color emoji
216         paint.setTypeface(fTypeface);
217 #ifdef SK_BUILD_FOR_ANDROID
218         paint.setTextSize(SkIntToScalar(19));
219 #else
220         paint.setTextSize(SkIntToScalar(22));
221 #endif
222         canvas->drawText(text, textLen, 670, 100, paint);
223
224 #if SK_SUPPORT_GPU
225         // render offscreen buffer
226         if (surface) {
227             SkAutoCanvasRestore acr(inputCanvas, true);
228             // since we prepended this matrix already, we blit using identity
229             inputCanvas->resetMatrix();
230             SkImage* image = surface->newImageSnapshot();
231             inputCanvas->drawImage(image, 0, 0, NULL);
232             image->unref();
233         }
234 #endif
235     }
236
237 private:
238     SkTypeface* fTypeface;
239
240     typedef skiagm::GM INHERITED;
241 };
242
243 DEF_GM( return SkNEW(DFTextGM); )