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