jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 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" |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 8 | #include "Resources.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkStream.h" |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 11 | #include "SkSurface.h" |
| 12 | #include "SkTypeface.h" |
| 13 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 14 | class DFTextGM : public skiagm::GM { |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 15 | public: |
| 16 | DFTextGM() { |
| 17 | this->setBGColor(0xFFFFFFFF); |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 18 | fTypeface = NULL; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | virtual ~DFTextGM() { |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 22 | SkSafeUnref(fTypeface); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 26 | void onOnceBeforeDraw() SK_OVERRIDE { |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 27 | SkString filename = GetResourcePath("/Funkster.ttf"); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 28 | SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(filename.c_str())); |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 29 | if (!stream->isValid()) { |
| 30 | SkDebugf("Could not find Funkster.ttf, please set --resourcePath correctly.\n"); |
| 31 | return; |
| 32 | } |
| 33 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 34 | fTypeface = SkTypeface::CreateFromStream(stream.detach()); |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 35 | } |
| 36 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 37 | SkString onShortName() SK_OVERRIDE { |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 38 | return SkString("dftext"); |
| 39 | } |
| 40 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 41 | SkISize onISize() SK_OVERRIDE { |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 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 | |
rmistry | 465206a | 2015-02-02 12:08:18 -0800 | [diff] [blame] | 53 | virtual void onDraw(SkCanvas* inputCanvas) SK_OVERRIDE { |
djsollen | 901704b | 2014-10-08 07:12:11 -0700 | [diff] [blame] | 54 | #ifdef SK_BUILD_FOR_ANDROID |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 55 | SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f }; |
| 56 | #else |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 57 | SkScalar textSizes[] = { 11.0f, 11.0f*2.0f, 11.0f*5.0f, 11.0f*2.0f*5.0f }; |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 58 | #endif |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 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()); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 65 | SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, |
| 66 | SkSurfaceProps::kLegacyFontHost_InitType); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 67 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, |
| 68 | info, 0, &props)); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 69 | SkCanvas* canvas = surface.get() ? surface->getCanvas() : inputCanvas; |
jvanverth | f9669f4 | 2014-10-07 07:59:45 -0700 | [diff] [blame] | 70 | // init our new canvas with the old canvas's matrix |
| 71 | canvas->setMatrix(inputCanvas->getTotalMatrix()); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 72 | #else |
| 73 | SkCanvas* canvas = inputCanvas; |
| 74 | #endif |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 75 | // apply global scale to test glyph positioning |
| 76 | canvas->scale(1.05f, 1.05f); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 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); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 119 | x = SkIntToScalar(680); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 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 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 132 | // check pos text |
| 133 | { |
| 134 | SkAutoCanvasRestore acr(canvas, true); |
| 135 | |
| 136 | canvas->scale(2.0f, 2.0f); |
| 137 | |
bsalomon | fbaace0 | 2014-12-12 16:41:46 -0800 | [diff] [blame] | 138 | SkAutoTArray<SkPoint> pos(SkToInt(textLen)); |
| 139 | SkAutoTArray<SkScalar> widths(SkToInt(textLen)); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 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 | |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 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); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 164 | SkRect r = SkRect::MakeLTRB(670, 250, 820, 460); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 165 | canvas->drawRect(r, paint); |
| 166 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 167 | x = SkIntToScalar(680); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 168 | y = SkIntToScalar(270); |
djsollen | 901704b | 2014-10-08 07:12:11 -0700 | [diff] [blame] | 169 | #ifdef SK_BUILD_FOR_ANDROID |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 170 | paint.setTextSize(SkIntToScalar(19)); |
| 171 | #else |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 172 | paint.setTextSize(SkIntToScalar(22)); |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 173 | #endif |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 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); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 182 | r = SkRect::MakeLTRB(820, 250, 970, 460); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 183 | canvas->drawRect(r, paint); |
| 184 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 185 | x = SkIntToScalar(830); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 186 | y = SkIntToScalar(270); |
djsollen | 901704b | 2014-10-08 07:12:11 -0700 | [diff] [blame] | 187 | #ifdef SK_BUILD_FOR_ANDROID |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 188 | paint.setTextSize(SkIntToScalar(19)); |
| 189 | #else |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 190 | paint.setTextSize(SkIntToScalar(22)); |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 191 | #endif |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 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 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 199 | // check color emoji |
| 200 | paint.setTypeface(fTypeface); |
| 201 | canvas->drawText(text, textLen, 670, 100, paint); |
| 202 | |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 203 | #if SK_SUPPORT_GPU |
| 204 | // render offscreen buffer |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 205 | if (surface) { |
jvanverth | f9669f4 | 2014-10-07 07:59:45 -0700 | [diff] [blame] | 206 | SkAutoCanvasRestore acr(inputCanvas, true); |
| 207 | // since we prepended this matrix already, we blit using identity |
| 208 | inputCanvas->resetMatrix(); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 209 | SkImage* image = surface->newImageSnapshot(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 210 | inputCanvas->drawImage(image, 0, 0, NULL); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 211 | image->unref(); |
| 212 | } |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 213 | #endif |
| 214 | } |
| 215 | |
| 216 | private: |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 217 | SkTypeface* fTypeface; |
| 218 | |
| 219 | typedef skiagm::GM INHERITED; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 222 | DEF_GM( return SkNEW(DFTextGM); ) |