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 | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 26 | void onOnceBeforeDraw() override { |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 27 | fTypeface = GetResourceAsTypeface("/fonts/Funkster.ttf"); |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 28 | } |
| 29 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkString onShortName() override { |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 31 | return SkString("dftext"); |
| 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkISize onISize() override { |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 35 | 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | virtual void onDraw(SkCanvas* inputCanvas) override { |
djsollen | 901704b | 2014-10-08 07:12:11 -0700 | [diff] [blame] | 47 | #ifdef SK_BUILD_FOR_ANDROID |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 48 | SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f }; |
| 49 | #else |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 50 | 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] | 51 | #endif |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 52 | 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()); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 58 | SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, |
| 59 | SkSurfaceProps::kLegacyFontHost_InitType); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 60 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, |
| 61 | info, 0, &props)); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 62 | SkCanvas* canvas = surface.get() ? surface->getCanvas() : inputCanvas; |
jvanverth | f9669f4 | 2014-10-07 07:59:45 -0700 | [diff] [blame] | 63 | // init our new canvas with the old canvas's matrix |
| 64 | canvas->setMatrix(inputCanvas->getTotalMatrix()); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 65 | #else |
| 66 | SkCanvas* canvas = inputCanvas; |
| 67 | #endif |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 68 | // apply global scale to test glyph positioning |
| 69 | canvas->scale(1.05f, 1.05f); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 70 | canvas->clear(0xffffffff); |
| 71 | |
| 72 | SkPaint paint; |
| 73 | paint.setAntiAlias(true); |
| 74 | paint.setSubpixelText(true); |
jvanverth | 4854d13 | 2015-06-22 06:46:56 -0700 | [diff] [blame] | 75 | |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 76 | 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); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 110 | x = SkIntToScalar(680); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 111 | 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 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 123 | // check pos text |
| 124 | { |
| 125 | SkAutoCanvasRestore acr(canvas, true); |
| 126 | |
| 127 | canvas->scale(2.0f, 2.0f); |
| 128 | |
bsalomon | fbaace0 | 2014-12-12 16:41:46 -0800 | [diff] [blame] | 129 | SkAutoTArray<SkPoint> pos(SkToInt(textLen)); |
| 130 | SkAutoTArray<SkScalar> widths(SkToInt(textLen)); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 131 | 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 | |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 146 | // 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); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 155 | SkRect r = SkRect::MakeLTRB(670, 250, 820, 460); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 156 | canvas->drawRect(r, paint); |
| 157 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 158 | x = SkIntToScalar(680); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 159 | y = SkIntToScalar(270); |
djsollen | 901704b | 2014-10-08 07:12:11 -0700 | [diff] [blame] | 160 | #ifdef SK_BUILD_FOR_ANDROID |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 161 | paint.setTextSize(SkIntToScalar(19)); |
| 162 | #else |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 163 | paint.setTextSize(SkIntToScalar(22)); |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 164 | #endif |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 165 | 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); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 173 | r = SkRect::MakeLTRB(820, 250, 970, 460); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 174 | canvas->drawRect(r, paint); |
| 175 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 176 | x = SkIntToScalar(830); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 177 | y = SkIntToScalar(270); |
djsollen | 901704b | 2014-10-08 07:12:11 -0700 | [diff] [blame] | 178 | #ifdef SK_BUILD_FOR_ANDROID |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 179 | paint.setTextSize(SkIntToScalar(19)); |
| 180 | #else |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 181 | paint.setTextSize(SkIntToScalar(22)); |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 182 | #endif |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 183 | 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 | |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 190 | // check skew |
| 191 | { |
| 192 | paint.setLCDRenderText(false); |
| 193 | SkAutoCanvasRestore acr(canvas, true); |
| 194 | canvas->skew(0.0f, 0.151515f); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 195 | paint.setTextSize(SkIntToScalar(32)); |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 196 | 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); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 202 | paint.setTextSize(SkIntToScalar(32)); |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 203 | canvas->drawText(text, textLen, 580, 230, paint); |
| 204 | } |
| 205 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 206 | // check color emoji |
| 207 | paint.setTypeface(fTypeface); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 208 | #ifdef SK_BUILD_FOR_ANDROID |
| 209 | paint.setTextSize(SkIntToScalar(19)); |
| 210 | #else |
| 211 | paint.setTextSize(SkIntToScalar(22)); |
| 212 | #endif |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 213 | canvas->drawText(text, textLen, 670, 100, paint); |
| 214 | |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 215 | #if SK_SUPPORT_GPU |
| 216 | // render offscreen buffer |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 217 | if (surface) { |
jvanverth | f9669f4 | 2014-10-07 07:59:45 -0700 | [diff] [blame] | 218 | SkAutoCanvasRestore acr(inputCanvas, true); |
| 219 | // since we prepended this matrix already, we blit using identity |
| 220 | inputCanvas->resetMatrix(); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 221 | SkImage* image = surface->newImageSnapshot(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 222 | inputCanvas->drawImage(image, 0, 0, NULL); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 223 | image->unref(); |
| 224 | } |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 225 | #endif |
| 226 | } |
| 227 | |
| 228 | private: |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 229 | SkTypeface* fTypeface; |
| 230 | |
| 231 | typedef skiagm::GM INHERITED; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 234 | DEF_GM( return SkNEW(DFTextGM); ) |