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 | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 21 | void onOnceBeforeDraw() override { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 22 | fEmojiTypeface = sk_tool_utils::emoji_typeface(); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 23 | fEmojiText = sk_tool_utils::emoji_sample_text(); |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 24 | } |
| 25 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 26 | SkString onShortName() override { |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 27 | SkString name("dftext"); |
| 28 | name.append(sk_tool_utils::platform_os_emoji()); |
| 29 | return name; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 30 | } |
| 31 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | SkISize onISize() override { |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 33 | return SkISize::Make(1024, 768); |
| 34 | } |
| 35 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 36 | virtual void onDraw(SkCanvas* inputCanvas) override { |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 37 | SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f }; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 38 | SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f }; |
| 39 | |
| 40 | // set up offscreen rendering with distance field text |
| 41 | #if SK_SUPPORT_GPU |
| 42 | GrContext* ctx = inputCanvas->getGrContext(); |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 43 | SkISize size = onISize(); |
| 44 | SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType, |
| 45 | sk_ref_sp(inputCanvas->imageInfo().colorSpace())); |
brianosman | 3a0dbde | 2016-07-26 11:36:05 -0700 | [diff] [blame] | 46 | SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag, |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 47 | SkSurfaceProps::kLegacyFontHost_InitType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 48 | auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props)); |
| 49 | SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas; |
jvanverth | f9669f4 | 2014-10-07 07:59:45 -0700 | [diff] [blame] | 50 | // init our new canvas with the old canvas's matrix |
| 51 | canvas->setMatrix(inputCanvas->getTotalMatrix()); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 52 | #else |
| 53 | SkCanvas* canvas = inputCanvas; |
| 54 | #endif |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 55 | // apply global scale to test glyph positioning |
| 56 | canvas->scale(1.05f, 1.05f); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 57 | canvas->clear(0xffffffff); |
| 58 | |
| 59 | SkPaint paint; |
| 60 | paint.setAntiAlias(true); |
| 61 | paint.setSubpixelText(true); |
jvanverth | 4854d13 | 2015-06-22 06:46:56 -0700 | [diff] [blame] | 62 | |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 63 | sk_tool_utils::set_portable_typeface(&paint, "serif"); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 64 | |
| 65 | const char* text = "Hamburgefons"; |
| 66 | const size_t textLen = strlen(text); |
| 67 | |
| 68 | // check scaling up |
| 69 | SkScalar x = SkIntToScalar(0); |
| 70 | SkScalar y = SkIntToScalar(78); |
| 71 | for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) { |
| 72 | SkAutoCanvasRestore acr(canvas, true); |
| 73 | canvas->translate(x, y); |
| 74 | canvas->scale(scales[i], scales[i]); |
| 75 | paint.setTextSize(textSizes[i]); |
| 76 | canvas->drawText(text, textLen, 0, 0, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | y += paint.getFontMetrics(nullptr)*scales[i]; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // check rotation |
| 81 | for (size_t i = 0; i < 5; ++i) { |
| 82 | SkScalar rotX = SkIntToScalar(10); |
| 83 | SkScalar rotY = y; |
| 84 | |
| 85 | SkAutoCanvasRestore acr(canvas, true); |
| 86 | canvas->translate(SkIntToScalar(10 + i * 200), -80); |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 87 | canvas->rotate(SkIntToScalar(i * 5), rotX, rotY); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 88 | for (int ps = 6; ps <= 32; ps += 3) { |
| 89 | paint.setTextSize(SkIntToScalar(ps)); |
| 90 | canvas->drawText(text, textLen, rotX, rotY, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 91 | rotY += paint.getFontMetrics(nullptr); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | // check scaling down |
| 96 | paint.setLCDRenderText(true); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 97 | x = SkIntToScalar(680); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 98 | y = SkIntToScalar(20); |
| 99 | size_t arraySize = SK_ARRAY_COUNT(textSizes); |
| 100 | for (size_t i = 0; i < arraySize; ++i) { |
| 101 | SkAutoCanvasRestore acr(canvas, true); |
| 102 | canvas->translate(x, y); |
| 103 | SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]); |
| 104 | canvas->scale(scaleFactor, scaleFactor); |
| 105 | paint.setTextSize(textSizes[i]); |
| 106 | canvas->drawText(text, textLen, 0, 0, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 107 | y += paint.getFontMetrics(nullptr)*scaleFactor; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 108 | } |
| 109 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 110 | // check pos text |
| 111 | { |
| 112 | SkAutoCanvasRestore acr(canvas, true); |
| 113 | |
| 114 | canvas->scale(2.0f, 2.0f); |
| 115 | |
bsalomon | fbaace0 | 2014-12-12 16:41:46 -0800 | [diff] [blame] | 116 | SkAutoTArray<SkPoint> pos(SkToInt(textLen)); |
| 117 | SkAutoTArray<SkScalar> widths(SkToInt(textLen)); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 118 | paint.setTextSize(textSizes[0]); |
| 119 | |
| 120 | paint.getTextWidths(text, textLen, &widths[0]); |
| 121 | |
| 122 | SkScalar x = SkIntToScalar(340); |
| 123 | SkScalar y = SkIntToScalar(75); |
| 124 | for (unsigned int i = 0; i < textLen; ++i) { |
| 125 | pos[i].set(x, y); |
| 126 | x += widths[i]; |
| 127 | } |
| 128 | |
| 129 | canvas->drawPosText(text, textLen, &pos[0], paint); |
| 130 | } |
| 131 | |
| 132 | |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 133 | // check gamma-corrected blending |
| 134 | const SkColor fg[] = { |
| 135 | 0xFFFFFFFF, |
| 136 | 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, |
| 137 | 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, |
| 138 | 0xFF000000, |
| 139 | }; |
| 140 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 141 | paint.setColor(0xFFF7F3F7); |
caryclark | ba9757a | 2015-07-23 14:07:58 -0700 | [diff] [blame] | 142 | SkRect r = SkRect::MakeLTRB(670, 215, 820, 397); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 143 | canvas->drawRect(r, paint); |
| 144 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 145 | x = SkIntToScalar(680); |
caryclark | ba9757a | 2015-07-23 14:07:58 -0700 | [diff] [blame] | 146 | y = SkIntToScalar(235); |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 147 | paint.setTextSize(SkIntToScalar(19)); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 148 | for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 149 | paint.setColor(fg[i]); |
| 150 | |
| 151 | canvas->drawText(text, textLen, x, y, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 152 | y += paint.getFontMetrics(nullptr); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 153 | } |
| 154 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 155 | paint.setColor(0xFF181C18); |
caryclark | ba9757a | 2015-07-23 14:07:58 -0700 | [diff] [blame] | 156 | r = SkRect::MakeLTRB(820, 215, 970, 397); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 157 | canvas->drawRect(r, paint); |
| 158 | |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 159 | x = SkIntToScalar(830); |
caryclark | ba9757a | 2015-07-23 14:07:58 -0700 | [diff] [blame] | 160 | y = SkIntToScalar(235); |
jvanverth | ed14f1d | 2014-10-06 12:50:54 -0700 | [diff] [blame] | 161 | paint.setTextSize(SkIntToScalar(19)); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 162 | for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 163 | paint.setColor(fg[i]); |
| 164 | |
| 165 | canvas->drawText(text, textLen, x, y, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 166 | y += paint.getFontMetrics(nullptr); |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 167 | } |
| 168 | |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 169 | // check skew |
| 170 | { |
| 171 | paint.setLCDRenderText(false); |
| 172 | SkAutoCanvasRestore acr(canvas, true); |
| 173 | canvas->skew(0.0f, 0.151515f); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 174 | paint.setTextSize(SkIntToScalar(32)); |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 175 | canvas->drawText(text, textLen, 745, 70, paint); |
| 176 | } |
| 177 | { |
| 178 | paint.setLCDRenderText(true); |
| 179 | SkAutoCanvasRestore acr(canvas, true); |
| 180 | canvas->skew(0.5f, 0.0f); |
jvanverth | d68a550 | 2015-03-16 12:58:43 -0700 | [diff] [blame] | 181 | paint.setTextSize(SkIntToScalar(32)); |
caryclark | ba9757a | 2015-07-23 14:07:58 -0700 | [diff] [blame] | 182 | canvas->drawText(text, textLen, 580, 125, paint); |
jvanverth | 354eba5 | 2015-03-16 11:32:49 -0700 | [diff] [blame] | 183 | } |
| 184 | |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 185 | // check color emoji |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 186 | if (fEmojiTypeface) { |
| 187 | paint.setTypeface(fEmojiTypeface); |
| 188 | paint.setTextSize(SkIntToScalar(19)); |
caryclark | ba9757a | 2015-07-23 14:07:58 -0700 | [diff] [blame] | 189 | canvas->drawText(fEmojiText, strlen(fEmojiText), 670, 90, paint); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 190 | } |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 191 | #if SK_SUPPORT_GPU |
| 192 | // render offscreen buffer |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 193 | if (surface) { |
jvanverth | f9669f4 | 2014-10-07 07:59:45 -0700 | [diff] [blame] | 194 | SkAutoCanvasRestore acr(inputCanvas, true); |
| 195 | // since we prepended this matrix already, we blit using identity |
| 196 | inputCanvas->resetMatrix(); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 197 | inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr); |
jvanverth | 76ce81e | 2014-09-22 14:26:53 -0700 | [diff] [blame] | 198 | } |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 199 | #endif |
| 200 | } |
| 201 | |
| 202 | private: |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 203 | sk_sp<SkTypeface> fEmojiTypeface; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 204 | const char* fEmojiText; |
jvanverth | fca302c | 2014-10-20 13:12:54 -0700 | [diff] [blame] | 205 | |
| 206 | typedef skiagm::GM INHERITED; |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 209 | DEF_GM(return new DFTextGM;) |