2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkSurface.h"
12 #include "SkTypeface.h"
14 class DFTextGM : public skiagm::GM {
17 this->setBGColor(0xFFFFFFFF);
22 SkSafeUnref(fTypeface);
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");
34 fTypeface = SkTypeface::CreateFromStream(stream.detach());
37 SkString onShortName() override {
38 return SkString("dftext");
41 SkISize onISize() override {
42 return SkISize::Make(1024, 768);
45 static void rotate_about(SkCanvas* canvas,
47 SkScalar px, SkScalar py) {
48 canvas->translate(px, py);
49 canvas->rotate(degrees);
50 canvas->translate(-px, -py);
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 };
57 SkScalar textSizes[] = { 11.0f, 11.0f*2.0f, 11.0f*5.0f, 11.0f*2.0f*5.0f };
59 SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f };
61 // set up offscreen rendering with distance field text
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,
69 SkCanvas* canvas = surface.get() ? surface->getCanvas() : inputCanvas;
70 // init our new canvas with the old canvas's matrix
71 canvas->setMatrix(inputCanvas->getTotalMatrix());
73 SkCanvas* canvas = inputCanvas;
75 // apply global scale to test glyph positioning
76 canvas->scale(1.05f, 1.05f);
77 canvas->clear(0xffffffff);
80 paint.setAntiAlias(true);
81 paint.setSubpixelText(true);
83 paint.setDistanceFieldTextTEMP(true);
85 sk_tool_utils::set_portable_typeface(&paint, "Times New Roman", SkTypeface::kNormal);
87 const char* text = "Hamburgefons";
88 const size_t textLen = strlen(text);
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];
103 for (size_t i = 0; i < 5; ++i) {
104 SkScalar rotX = SkIntToScalar(10);
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);
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;
134 SkAutoCanvasRestore acr(canvas, true);
136 canvas->scale(2.0f, 2.0f);
138 SkAutoTArray<SkPoint> pos(SkToInt(textLen));
139 SkAutoTArray<SkScalar> widths(SkToInt(textLen));
140 paint.setTextSize(textSizes[0]);
142 paint.getTextWidths(text, textLen, &widths[0]);
144 SkScalar x = SkIntToScalar(340);
145 SkScalar y = SkIntToScalar(75);
146 for (unsigned int i = 0; i < textLen; ++i) {
151 canvas->drawPosText(text, textLen, &pos[0], paint);
155 // check gamma-corrected blending
156 const SkColor fg[] = {
158 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
159 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
163 paint.setColor(0xFFF1F1F1);
164 SkRect r = SkRect::MakeLTRB(670, 250, 820, 460);
165 canvas->drawRect(r, paint);
167 x = SkIntToScalar(680);
168 y = SkIntToScalar(270);
169 #ifdef SK_BUILD_FOR_ANDROID
170 paint.setTextSize(SkIntToScalar(19));
172 paint.setTextSize(SkIntToScalar(22));
174 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
175 paint.setColor(fg[i]);
177 canvas->drawText(text, textLen, x, y, paint);
178 y += paint.getFontMetrics(NULL);
181 paint.setColor(0xFF1F1F1F);
182 r = SkRect::MakeLTRB(820, 250, 970, 460);
183 canvas->drawRect(r, paint);
185 x = SkIntToScalar(830);
186 y = SkIntToScalar(270);
187 #ifdef SK_BUILD_FOR_ANDROID
188 paint.setTextSize(SkIntToScalar(19));
190 paint.setTextSize(SkIntToScalar(22));
192 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
193 paint.setColor(fg[i]);
195 canvas->drawText(text, textLen, x, y, paint);
196 y += paint.getFontMetrics(NULL);
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);
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);
216 paint.setTypeface(fTypeface);
217 #ifdef SK_BUILD_FOR_ANDROID
218 paint.setTextSize(SkIntToScalar(19));
220 paint.setTextSize(SkIntToScalar(22));
222 canvas->drawText(text, textLen, 670, 100, paint);
225 // render offscreen buffer
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);
238 SkTypeface* fTypeface;
240 typedef skiagm::GM INHERITED;
243 DEF_GM( return SkNEW(DFTextGM); )