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: |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 26 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 27 | SkString filename = GetResourcePath("/Funkster.ttf"); |
| 28 | SkAutoTUnref<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); |
| 35 | } |
| 36 | |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 37 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 38 | return kGPUOnly_Flag; |
| 39 | } |
| 40 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 41 | virtual SkString onShortName() SK_OVERRIDE { |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 42 | return SkString("dftext"); |
| 43 | } |
| 44 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 45 | virtual SkISize onISize() SK_OVERRIDE { |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 46 | return SkISize::Make(1024, 768); |
| 47 | } |
| 48 | |
| 49 | static void rotate_about(SkCanvas* canvas, |
| 50 | SkScalar degrees, |
| 51 | SkScalar px, SkScalar py) { |
| 52 | canvas->translate(px, py); |
| 53 | canvas->rotate(degrees); |
| 54 | canvas->translate(-px, -py); |
| 55 | } |
| 56 | |
| 57 | virtual void onDraw(SkCanvas* inputCanvas) { |
djsollen | 901704b | 2014-10-08 07:12:11 -0700 | [diff] [blame] | 58 | #ifdef SK_BUILD_FOR_ANDROID |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 59 | SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f }; |
| 60 | #else |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 61 | 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] | 62 | #endif |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 63 | SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f }; |
| 64 | |
| 65 | // set up offscreen rendering with distance field text |
| 66 | #if SK_SUPPORT_GPU |
| 67 | GrContext* ctx = inputCanvas->getGrContext(); |
| 68 | SkImageInfo info = SkImageInfo::MakeN32Premul(onISize()); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 69 | SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, |
| 70 | SkSurfaceProps::kLegacyFontHost_InitType); |
| 71 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, info, 0, &props)); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 72 | SkCanvas* canvas = surface.get() ? surface->getCanvas() : inputCanvas; |
jvanverth | f9669f4 | 2014-10-07 07:59:45 -0700 | [diff] [blame] | 73 | // init our new canvas with the old canvas's matrix |
| 74 | canvas->setMatrix(inputCanvas->getTotalMatrix()); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 75 | #else |
| 76 | SkCanvas* canvas = inputCanvas; |
| 77 | #endif |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 78 | // apply global scale to test glyph positioning |
| 79 | canvas->scale(1.05f, 1.05f); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 80 | canvas->clear(0xffffffff); |
| 81 | |
| 82 | SkPaint paint; |
| 83 | paint.setAntiAlias(true); |
| 84 | paint.setSubpixelText(true); |
| 85 | #if !SK_SUPPORT_GPU |
| 86 | paint.setDistanceFieldTextTEMP(true); |
| 87 | #endif |
| 88 | sk_tool_utils::set_portable_typeface(&paint, "Times New Roman", SkTypeface::kNormal); |
| 89 | |
| 90 | const char* text = "Hamburgefons"; |
| 91 | const size_t textLen = strlen(text); |
| 92 | |
| 93 | // check scaling up |
| 94 | SkScalar x = SkIntToScalar(0); |
| 95 | SkScalar y = SkIntToScalar(78); |
| 96 | for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) { |
| 97 | SkAutoCanvasRestore acr(canvas, true); |
| 98 | canvas->translate(x, y); |
| 99 | canvas->scale(scales[i], scales[i]); |
| 100 | paint.setTextSize(textSizes[i]); |
| 101 | canvas->drawText(text, textLen, 0, 0, paint); |
| 102 | y += paint.getFontMetrics(NULL)*scales[i]; |
| 103 | } |
| 104 | |
| 105 | // check rotation |
| 106 | for (size_t i = 0; i < 5; ++i) { |
| 107 | SkScalar rotX = SkIntToScalar(10); |
| 108 | SkScalar rotY = y; |
| 109 | |
| 110 | SkAutoCanvasRestore acr(canvas, true); |
| 111 | canvas->translate(SkIntToScalar(10 + i * 200), -80); |
| 112 | rotate_about(canvas, SkIntToScalar(i * 5), rotX, rotY); |
| 113 | for (int ps = 6; ps <= 32; ps += 3) { |
| 114 | paint.setTextSize(SkIntToScalar(ps)); |
| 115 | canvas->drawText(text, textLen, rotX, rotY, paint); |
| 116 | rotY += paint.getFontMetrics(NULL); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // check scaling down |
| 121 | paint.setLCDRenderText(true); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 122 | x = SkIntToScalar(680); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 123 | y = SkIntToScalar(20); |
| 124 | size_t arraySize = SK_ARRAY_COUNT(textSizes); |
| 125 | for (size_t i = 0; i < arraySize; ++i) { |
| 126 | SkAutoCanvasRestore acr(canvas, true); |
| 127 | canvas->translate(x, y); |
| 128 | SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]); |
| 129 | canvas->scale(scaleFactor, scaleFactor); |
| 130 | paint.setTextSize(textSizes[i]); |
| 131 | canvas->drawText(text, textLen, 0, 0, paint); |
| 132 | y += paint.getFontMetrics(NULL)*scaleFactor; |
| 133 | } |
| 134 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 135 | // check pos text |
| 136 | { |
| 137 | SkAutoCanvasRestore acr(canvas, true); |
| 138 | |
| 139 | canvas->scale(2.0f, 2.0f); |
| 140 | |
| 141 | SkAutoTArray<SkPoint> pos(textLen); |
| 142 | SkAutoTArray<SkScalar> widths(textLen); |
| 143 | paint.setTextSize(textSizes[0]); |
| 144 | |
| 145 | paint.getTextWidths(text, textLen, &widths[0]); |
| 146 | |
| 147 | SkScalar x = SkIntToScalar(340); |
| 148 | SkScalar y = SkIntToScalar(75); |
| 149 | for (unsigned int i = 0; i < textLen; ++i) { |
| 150 | pos[i].set(x, y); |
| 151 | x += widths[i]; |
| 152 | } |
| 153 | |
| 154 | canvas->drawPosText(text, textLen, &pos[0], paint); |
| 155 | } |
| 156 | |
| 157 | |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 158 | // check gamma-corrected blending |
| 159 | const SkColor fg[] = { |
| 160 | 0xFFFFFFFF, |
| 161 | 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, |
| 162 | 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, |
| 163 | 0xFF000000, |
| 164 | }; |
| 165 | |
| 166 | paint.setColor(0xFFF1F1F1); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 167 | SkRect r = SkRect::MakeLTRB(670, 250, 820, 460); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 168 | canvas->drawRect(r, paint); |
| 169 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 170 | x = SkIntToScalar(680); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 171 | y = SkIntToScalar(270); |
djsollen | 901704b | 2014-10-08 07:12:11 -0700 | [diff] [blame] | 172 | #ifdef SK_BUILD_FOR_ANDROID |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 173 | paint.setTextSize(SkIntToScalar(19)); |
| 174 | #else |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 175 | paint.setTextSize(SkIntToScalar(22)); |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 176 | #endif |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 177 | for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 178 | paint.setColor(fg[i]); |
| 179 | |
| 180 | canvas->drawText(text, textLen, x, y, paint); |
| 181 | y += paint.getFontMetrics(NULL); |
| 182 | } |
| 183 | |
| 184 | paint.setColor(0xFF1F1F1F); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 185 | r = SkRect::MakeLTRB(820, 250, 970, 460); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 186 | canvas->drawRect(r, paint); |
| 187 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 188 | x = SkIntToScalar(830); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 189 | y = SkIntToScalar(270); |
djsollen | 901704b | 2014-10-08 07:12:11 -0700 | [diff] [blame] | 190 | #ifdef SK_BUILD_FOR_ANDROID |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 191 | paint.setTextSize(SkIntToScalar(19)); |
| 192 | #else |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 193 | paint.setTextSize(SkIntToScalar(22)); |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 194 | #endif |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 195 | for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 196 | paint.setColor(fg[i]); |
| 197 | |
| 198 | canvas->drawText(text, textLen, x, y, paint); |
| 199 | y += paint.getFontMetrics(NULL); |
| 200 | } |
| 201 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 202 | // check color emoji |
| 203 | paint.setTypeface(fTypeface); |
| 204 | canvas->drawText(text, textLen, 670, 100, paint); |
| 205 | |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 206 | #if SK_SUPPORT_GPU |
| 207 | // render offscreen buffer |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 208 | if (surface) { |
jvanverth | f9669f4 | 2014-10-07 07:59:45 -0700 | [diff] [blame] | 209 | SkAutoCanvasRestore acr(inputCanvas, true); |
| 210 | // since we prepended this matrix already, we blit using identity |
| 211 | inputCanvas->resetMatrix(); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 212 | SkImage* image = surface->newImageSnapshot(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 213 | inputCanvas->drawImage(image, 0, 0, NULL); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 214 | image->unref(); |
| 215 | } |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 216 | #endif |
| 217 | } |
| 218 | |
| 219 | private: |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 220 | SkTypeface* fTypeface; |
| 221 | |
| 222 | typedef skiagm::GM INHERITED; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 225 | DEF_GM( return SkNEW(DFTextGM); ) |